###Sets up scoreboards for advancements and attacks. Utilizes same objectives as nitk for resources###
scoreboard objectives add nitWarCraft dummy
scoreboard objectives add nitHellAttacks dummy
scoreboard objectives add nitBoxProg dummy
scoreboard objectives add nitKeepCraft dummy
advancement grant @a[advancements={nitk:nitk/tyrant_defeat=true},nbt={Dimension:"minecraft:the_nether"}] only nitco:nitco/root

###Create the portal###
place template nitco:portal ~-7 ~-5 ~-7
tp @s ~ ~1 ~
fill ~-2 ~-6 ~-2 ~2 ~-6 ~2 minecraft:repeating_command_block{auto:1,Command:"fill ~ ~ ~ ~ 20 ~ nether_bricks"}

###Place siteplanner to the east###
place template nitco:siteplanner_e ~7 ~1 ~

###Place siteplanner to the west###
place template nitco:siteplanner_w ~-7 ~1 ~

###Place siteplanner to the north###
place template nitco:siteplanner_n ~ ~1 ~-7

###Place siteplanner to the south###
place template nitco:siteplanner_s ~ ~1 ~7

###Remove the strategy###
clear @p written_book{title:"War Strategy"}

###Adds forceload to ensure structure generation###
forceload add ~-100 ~-100 ~100 ~100

###Assigns the fortress' ID###
execute store result score @s nitID run data get entity @s UUID[0]
scoreboard players operation @e[tag=nitNetherbrickStor,limit=1,sort=nearest] nitID = @s nitID
scoreboard players operation @e[tag=nitCGoldStor,limit=1,sort=nearest] nitID = @s nitID
tag @s add nitDenizen

###Summons markers for natural structures###
summon minecraft:marker ~ 128 ~ {Tags:[nitEntity,nitEntityTechnical,nitSpreadCenter,smithed.entity,smithed.strict],Invisible:1b,Invulnerable:1b,NoGravity:1b}
summon minecraft:marker ~ ~ ~ {Tags:[nitEntity,nitEntityTechnical,nitHellShip,nitCSpecialFeature,smithed.entity,smithed.strict],Invisible:1b,Invulnerable:1b,NoGravity:1b}
summon minecraft:marker ~ ~ ~ {Tags:[nitEntity,nitEntityTechnical,nitHellFactory,nitCSpecialFeature,smithed.entity,smithed.strict],Invisible:1b,Invulnerable:1b,NoGravity:1b}
summon minecraft:marker ~ ~ ~ {Tags:[nitEntity,nitEntityTechnical,nitHellDrill,nitCSpecialFeature,smithed.entity,smithed.strict],Invisible:1b,Invulnerable:1b,NoGravity:1b}
summon minecraft:marker ~ ~ ~ {Tags:[nitEntity,nitEntityTechnical,nitWithermaw,nitCSpecialFeature,smithed.entity,smithed.strict],Invisible:1b,Invulnerable:1b,NoGravity:1b}
summon minecraft:marker ~ ~ ~ {Tags:[nitEntity,nitEntityTechnical,nitBoxCenter,nitCSpecialFeature,smithed.entity,smithed.strict],Invisible:1b,Invulnerable:1b,NoGravity:1b}
scoreboard players operation @e[tag=nitCSpecialFeature,limit=5,sort=nearest] nitID = @s nitID

###Spreads the natural structures for the matching fortress###
scoreboard players operation #nitID nitTemp = @s nitID
execute as @e[tag=nitCSpecialFeature,limit=5,sort=nearest] if score @s nitID = #nitID nitTemp run tag @s add nitID_match
execute as @e[tag=nitSpreadCenter] at @e[tag=nitSpreadCenter] run function nitco:spread_features
kill @e[tag=nitSpreadCenter]

###Builds natural structures###
execute as @e[tag=nitCSpecialFeature,tag=nitID_match] at @s run function nitco:special/generate_all

###Summons the War Strategist, who fulfills similar function to Town Planner###
execute at @e[tag=nitWarStrategist] as @s run summon villager ~ ~ ~ {VillagerData:{profession:weaponsmith,level:5,type:swamp},Invulnerable:1,PersistenceRequired:1,NoAI:1,CustomName:'{"text":"War Strategist"}',Offers:{Recipes:[{buy:{id:feather,Count:1},sell:{id:"minecraft:written_book",Count:1},maxUses:9999999},{buy:{id:firework_star,Count:1,tag:{nitk_tyrantHeart:1b,HideFlags:1,display:{Name:'{"text":"Tyrannical Heart","color":"dark_red","italic":false}'},Enchantments:[{id:"minecraft:protection",lvl:0}]}},sell:{id:gold_ingot,Count:3},maxUses:9999999},{buy:{id:firework_star,Count:5,tag:{nitk_tyrantHeart:1b,HideFlags:1,display:{Name:'{"text":"Tyrannical Heart","color":"dark_red","italic":false}'},Enchantments:[{id:"minecraft:protection",lvl:0}]}},sell:{id:compass,Count:1,tag:{nitco_compass:1b,HideFlags:1,display:{Name:'{"text":"Tyranny Spotter","color":"dark_red","italic":false}',Lore:['{"text":"Points to the Tyrant\'s","color":"gray","italic":false}','{"text":"nearest active base","color":"gray","italic":false}','{"text":"with a magical flame.","color":"gray","italic":false}']},Enchantments:[{lvl:0,id:"minecraft:protection"}]}},maxUses:9999999},{buy:{id:gold_ingot,Count:16},sell:{id:ender_eye,Count:1},maxUses:9999999}]},Tags:[nitDenizen,nitCWS,nitEntity,smithed.entity,smithed.strict]}
scoreboard players operation @e[tag=nitCWS,limit=1,sort=nearest] nitID = @s nitID
function nitk:debug/update_all
kill @e[tag=nitWarStrategist]

###Anounces completed process###
forceload remove ~-100 ~-100 ~100 ~100
tellraw @s [{"text":"[۩]","color":"green","bold":true},{"text":": ","color":"white","bold":false},{"text":"You have successfully launched your Counteroffensive!","color":"white","bold":false}]