war_system_o_0 = {
	entity = "construction_platform_entity"
	construction_entity = "construction_platform_entity"
	portrait = "GFX_megastructure_construction_background"
	place_entity_on_planet_plane = no
	potential = { always = no }

	ai_weight = { factor = 0 }
	resources = {
		category = megastructures
		upkeep = { energy = 15 }
	}
}

war_system_o_1 = {
	entity = "war_system_o_phase_01_entity"
	construction_entity = "war_system_o_phase_01_entity"
	portrait = "GFX_megastructure_moon"
	upgrade_from = { war_system_o_0 }

	build_time = 10800
	resources = {
		category = megastructures
		cost = { alloys = 300000 unity = @giga_tera_unity_cost }
		upkeep = {
			energy = 300
			alloys = 150
		}
	}

	potential = { NOT = { has_global_flag = systemcraft_disabled } }
	possible = {
		from = {
			is_ai = no
			has_technology = tech_o_class_war_system
			has_technology = giga_tech_war_system_1
			has_technology = giga_tech_war_system_2
			has_ascension_perk = ap_systemcraft
		}
	}

	on_build_complete = {
		save_event_target_as = giga_system
		fromfrom.planet = {
			save_event_target_as = giga_planet
		}
		#delete the star
		set_star_class = sc_empty_space
		remove_star_flag = giga_o_star_system
		fromfrom.planet = {
			change_pc = pc_giga_empty_space
			remove_modifier = giga_o_star_modifier 
		}
		giga_remove_system_planets = yes
		giga_remove_system_debris = yes
		set_asteroid_belt = { radius = 0 }
		from = { country_event = { id = giga_dialog.2803 } }	# Notification
	}
}

war_system_o_2 = {
	entity = "war_system_o_phase_02_entity"
	construction_entity = "war_system_o_phase_02_construction_entity"
	portrait = "GFX_megastructure_moon"
	upgrade_from = { war_system_o_1 }

	build_time = 10800
	resources = {
		category = megastructures
		cost = { alloys = 300000 unity = @giga_tera_unity_cost }
		upkeep = {
			energy = 750
			alloys = 300
		}
	}

	potential = { NOT = { has_global_flag = systemcraft_disabled } }
	possible = {
		from = {
			has_technology = tech_o_class_war_system
			has_technology = giga_tech_war_system_1
			has_technology = giga_tech_war_system_2
			has_technology = giga_tech_war_system_3
			has_ascension_perk = ap_systemcraft
		}
		custom_tooltip = {
			fail_text = "requires_behemoth_planetcraft_1_moon_2"
			count_ship_in_system = { limit = { is_ship_size = giga_planet_behemoth }	count >= 1 }
			count_ship_in_system = { limit = { is_ship_size = giga_massive_planet }		count >= 2 }
		}
	}

	on_build_start = {
		random_fleet_in_system = {
			limit = {
				any_owned_ship = { is_ship_size = giga_planet_behemoth }
				exists = owner
				owner = { is_same_value = root.from }
			}

			if = {
				limit = { exists = leader }
				leader = { unassign_leader = this }
			}

			random_owned_ship = {
				limit = { is_ship_size = giga_planet_behemoth }
				delete_ship = this
				root.from = { subtract_variable = { which = giga_current_warplanet value = 1 } }
			}
		}
		while = {
			count = 2
			random_fleet_in_system = {
				limit = {
					any_owned_ship = { is_ship_size = giga_massive_planet }
					exists = owner
					owner = { is_same_value = root.from }
				}

				if = {
					limit = { exists = leader }
					leader = { unassign_leader = this }
				}

				random_owned_ship = {
					limit = { is_ship_size = giga_massive_planet }
					delete_ship = this
					root.from = { subtract_variable = { which = giga_current_warmoon value = 1 } }
				}
			}
		}
	}

	on_build_cancel = {
		from = {
			giga_planet_craft = yes
			while = { count = 2 giga_moon_barren = yes }
		}
	}

	on_build_complete = {
		save_event_target_as = giga_system
		fromfrom.planet = { save_event_target_as = giga_planet }
		from = { country_event = { id = giga_dialog.2804 } }	# Notification
	}
}

war_system_o_3 = {
	entity = "war_system_o_phase_03_entity"
	construction_entity = "war_system_o_phase_03_construction_entity"
	portrait = "GFX_megastructure_moon"
	upgrade_from = { war_system_o_2 }

	build_time = 10800
	resources = {
		category = megastructures
		cost = { alloys = 300000 unity = @giga_tera_unity_cost }
		upkeep = {
			energy = 1500
			alloys = 450
		}
	}

	potential = { NOT = { has_global_flag = systemcraft_disabled } }
	possible = {
		from = {
			has_technology = tech_o_class_war_system
			has_technology = giga_tech_war_system_1
			has_technology = giga_tech_war_system_2
			has_technology = giga_tech_war_system_3
			has_technology = giga_tech_war_system_4
			has_ascension_perk = ap_systemcraft
		}
		custom_tooltip = {
			fail_text = "requires_moon_2"
			count_ship_in_system = { limit = { is_ship_size = giga_massive_planet } count >= 2 }
		}
	}

	on_build_start = {
		while = {
			count = 2
			random_fleet_in_system = {
				limit = {
					any_owned_ship = { is_ship_size = giga_massive_planet }
					exists = owner
					owner = { is_same_value = root.from }
				}

				if = {
					limit = { exists = leader }
					leader = { unassign_leader = this }
				}

				random_owned_ship = {
					limit = { is_ship_size = giga_massive_planet }
					delete_ship = this
					root.from = { subtract_variable = { which = giga_current_warmoon value = 1 } }
				}
			}
		}
	}

	on_build_cancel = {
		from = {
			while = { count = 2 giga_moon_barren = yes }
		}
	}

	on_build_complete = {
		save_event_target_as = giga_system
		fromfrom.planet = { save_event_target_as = giga_planet }
		from = { country_event = { id = giga_dialog.2805 } }	# Notification
	}
}

war_system_o_4 = {
	entity = "war_system_o_phase_04_entity"
	construction_entity = "war_system_o_phase_04_construction_entity"
	portrait = "GFX_megastructure_moon"
	upgrade_from = { war_system_o_3 }

	build_time = 10800
	resources = {
		category = megastructures
		cost = { alloys = 300000 unity = @giga_tera_unity_cost }
		upkeep = {
			energy = 2250
			alloys = 1500
		}
	}

	potential = { NOT = { has_global_flag = systemcraft_disabled } }
	possible = {
		from = {
			has_technology = tech_o_class_war_system
			has_technology = giga_tech_war_system_1
			has_technology = giga_tech_war_system_2
			has_technology = giga_tech_war_system_3
			has_technology = giga_tech_war_system_4
			has_technology = giga_tech_war_system_5
			has_ascension_perk = ap_systemcraft
		}
		custom_tooltip = {
			fail_text = "requires_behemoth_planetcraft_2"
			count_ship_in_system = { limit = { is_ship_size = giga_planet_behemoth } count >= 2 }
		}
	}

	on_build_start = {
		while = {
			count = 2
			random_fleet_in_system = {
				limit = {
					any_owned_ship = { is_ship_size = giga_planet_behemoth }
					exists = owner
					owner = { is_same_value = root.from }
				}

				if = {
					limit = { exists = leader }
					leader = { unassign_leader = this }
				}

				random_owned_ship = {
					limit = { is_ship_size = giga_planet_behemoth }
					delete_ship = this
					root.from = { subtract_variable = { which = giga_current_warplanet value = 1 } }
				}
			}
		}
	}

	on_build_cancel = {
		from = { while = { count = 2 giga_planet_craft = yes } }
	}

	on_build_complete = {
		save_event_target_as = giga_system
		fromfrom.planet = { save_event_target_as = giga_planet }
		from = { country_event = { id = giga_dialog.2806 } }	# Notification
	}
}

war_system_o_5 = {
	entity = ""
	construction_entity = "war_system_o_phase_05_construction_entity"
	portrait = "GFX_megastructure_moon"
	upgrade_from = { war_system_o_4 }

	build_time = 10800
	resources = {
		category = megastructures
		cost = { alloys = 100000 unity = @giga_tera_unity_cost }
		upkeep = {
			energy = 2250
			alloys = 1500
		}
	}

	potential = { NOT = { has_global_flag = systemcraft_disabled } }
	possible = {
		from = {
			has_technology = tech_o_class_war_system
			has_technology = giga_tech_war_system_1
			has_technology = giga_tech_war_system_2
			has_technology = giga_tech_war_system_3
			has_technology = giga_tech_war_system_4
			has_technology = giga_tech_war_system_5
			has_technology = giga_tech_war_system_6
			has_ascension_perk = ap_systemcraft
		}
		custom_tooltip = { fail_text = "requires_systemcraft_crew"		from = { has_country_flag = systemcraft_crew_required } }
		custom_tooltip = {
			fail_text = "requires_behemoth_planetcraft_1_moon_1"
			count_ship_in_system = { limit = { is_ship_size = giga_planet_behemoth }	count >= 1 }
			count_ship_in_system = { limit = { is_ship_size = giga_massive_planet }	count >= 1 }
		}
	}

	on_build_start = {
		from = { remove_country_flag = systemcraft_crew_required }

		random_fleet_in_system = {
			limit = {
				any_owned_ship = { is_ship_size = giga_massive_planet }
				exists = owner
				owner = { is_same_value = root.from }
			}

			if = {
				limit = { exists = leader }
				leader = { unassign_leader = this }
			}

			random_owned_ship = {
				limit = { is_ship_size = giga_massive_planet }
				delete_ship = this
				root.from = { subtract_variable = { which = giga_current_warmoon value = 1 } }
			}
		}

		random_fleet_in_system = {
			limit = {
				any_owned_ship = { is_ship_size = giga_planet_behemoth }
				exists = owner
				owner = { is_same_value = root.from }
			}

			if = {
				limit = { exists = leader }
				leader = { unassign_leader = this }
			}

			random_owned_ship = {
				limit = { is_ship_size = giga_planet_behemoth }
				delete_ship = this
				root.from = { subtract_variable = { which = giga_current_warplanet value = 1 } }
			}
		}
	}

	on_build_cancel = {
		from = {
			giga_moon_barren = yes
			giga_planet_craft = yes
		}
	}

	on_build_complete = {
		save_event_target_as = giga_system
		if = {
			limit = {
				NOT = {
					any_system_planet = {
						is_star = yes
						NOT = { is_planet_class = pc_giga_empty_space }
					}
				}
			}
			set_star_class = sc_empty_space
		}
		fromfrom.planet = { save_event_target_as = giga_planet }
		from = {
			country_event = { id = giga_dialog.2807 }	# Notification
			giga_o_system_craft = yes
			if = {
				limit = {
					NOR = {
						has_global_flag = giga_achievements_disabled
						has_country_flag = giga_achievement_91
					}
				}
				set_country_flag = giga_achievement_91
				set_timed_country_flag = { flag = giga_achievement_91_notification days = 30 }
				giga_achievement_sound = yes
			}
		}
		last_created_fleet = { set_location = fromfrom }

		if = {
			limit = {
				from = { is_at_war = yes }
				NOT = { has_global_flag = war_in_heaven_ongoing }
			}
			random_list = {
				30 = {}
				70 = {
					if = {
						limit = {
							any_country = {
								relative_power = { who = root.from category = fleet value <= equivalent }
								is_at_war_with = root.from
								is_ai = yes
								NOR = {
									is_country_type = fallen_empire
									is_country_type = awakened_fallen_empire
									has_civic = civic_machine_terminator
									has_civic = civic_hive_devouring_swarm
									has_civic = civic_fanatic_purifiers
								}
								ehof_default_country = yes
							}
						}
						random_country = {
							limit = {
								relative_power = { who = root.from category = fleet value <= equivalent }
								is_at_war_with = root.from
								is_ai = yes
								NOR = {
									is_country_type = fallen_empire
									is_country_type = awakened_fallen_empire
									has_civic = civic_machine_terminator
									has_civic = civic_hive_devouring_swarm
									has_civic = civic_fanatic_purifiers
								}
								ehof_default_country = yes
							}
							save_event_target_as = systemcraft_surrender_empire
						}
						from = { country_event = { id = giga_dialog.2809 days = 1 } }		# Country surrenders
					}
				}
			}
		}

		remove_megastructure = fromfrom
	}
}