war_system_o_0 = {
	entity = "construction_platform_entity"
	construction_entity = "construction_platform_entity"
	portrait = "GFX_megastructure_construction_background"
	place_entity_on_planet_plane = no
	prerequisites = { tech_o_class_war_system }

	ai_weight = { factor = 0 }
	build_time = 1800
	resources = {
		category = megastructures
		cost = {
			influence = 300
			unity = @giga_tera_start_unity_cost
		}
		inline_script = {
			script = megastructures/generic_parts/giga_mega_alloy_cost
			alloys = 10000
		}

		upkeep = { energy = 5 }
	}

	potential = {
		is_ai = no
		has_technology = tech_o_class_war_system
		has_ascension_perk = ap_celestial_printing
		NOT = { has_global_flag = systemcraft_disabled }
		OR = {
			has_global_flag = systemcraft_capped_u
			check_variable = {
				which = giga_current_systemcraft
				value < value:giga_systemcraft_limit
			}
		}
	}

	possible = {
		custom_tooltip = { fail_text = "requires_no_habitable_planets"			NOT = { any_system_planet = { is_colony = yes } } }
		custom_tooltip = { fail_text = "requires_inside_border"				is_inside_border = from }
		custom_tooltip = { fail_text = "requires_no_crisis_system"			NOT = { any_system_planet = { has_planet_flag = crisis_vital_planet } } }
		custom_tooltip = {
			fail_text = "requires_not_capped"
			from = {
				OR = {
					has_global_flag = systemcraft_capped_u
					check_variable = {
						which = giga_current_systemcraft
						value < value:giga_systemcraft_limit
					}
				}
			}
		}
	}

	placement_rules = {
		planet_possible = {
			custom_tooltip = { fail_text = "requires_no_existing_megastructure"			planet_has_no_megastructure = yes }
			custom_tooltip = { fail_text = "must_build_around_star"							is_star = yes }
			custom_tooltip = { fail_text = "requires_no_anomaly"							has_anomaly = no }
			custom_tooltip = {
				fail_text = "requires_standard_o_star_planet_class"
				giga_is_o_star_for_megas= yes ##can build on O stars
				NOT = {
					giga_is_bitrinary = yes
				}
			}
		}
	}

	on_build_complete = {
		save_event_target_as = giga_system
		if = { limit = { exists = fromfrom.planet } fromfrom.planet = { save_event_target_as = giga_planet } }
		from = {
			country_event = { id = giga_dialog.2802 }	# Notification
			change_variable = { which = giga_current_systemcraft value = 1 }
		}
		fromfrom.planet = {
			if = { limit = { exists = orbital_station } orbital_station = { dismantle = yes } }
			set_carrier_flag = megastructure
			giga_set_has_mega_flag = yes
		}
	}
}

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

	outliner_trigger = {
		exists = owner
		NOT = { has_megastructure_flag = giga_outliner_hidden_by_@owner }
	}

	build_time = 10800
	resources = {
		category = giga_gigastructures
		cost = {
			unity = @giga_tera_unity_cost
		}
		inline_script = {
			script = megastructures/generic_parts/giga_mega_alloy_cost
			alloys = 300000
		}

		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_celestial_printing
		}
	}

	on_build_complete = {
		save_event_target_as = giga_system
		fromfrom.planet = {
			save_event_target_as = giga_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

		remove_star_flag = giga_o_star_system

	}
}

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 = giga_gigastructures
		cost = {
			unity = @giga_tera_unity_cost
		}
		inline_script = {
			script = megastructures/generic_parts/giga_mega_alloy_cost
			alloys = 300000
		}

		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_celestial_printing
		}
		custom_tooltip = {
			fail_text = "requires_behemoth_planetcraft_1_moon_2"
			count_ship_in_system = { limit = { is_ship_size = giga_planet_behemoth fleet = { is_leased = no } }	count >= 1 }
			count_ship_in_system = { limit = { is_ship_size = giga_war_moon fleet = { is_leased = no } }		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_war_moon }
					exists = owner
					owner = { is_same_value = root.from }
				}

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

				random_owned_ship = {
					limit = { is_ship_size = giga_war_moon }
					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 = giga_gigastructures
		cost = {
			unity = @giga_tera_unity_cost
		}
		inline_script = {
			script = megastructures/generic_parts/giga_mega_alloy_cost
			alloys = 300000
		}

		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_celestial_printing
		}
		custom_tooltip = {
			fail_text = "requires_moon_2"
			count_ship_in_system = { limit = { is_ship_size = giga_war_moon fleet = { is_leased = no } } count >= 2 }
		}
	}

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

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

				random_owned_ship = {
					limit = { is_ship_size = giga_war_moon }
					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 = giga_gigastructures
		cost = {
			unity = @giga_tera_unity_cost
		}
		inline_script = {
			script = megastructures/generic_parts/giga_mega_alloy_cost
			alloys = 300000
		}

		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_celestial_printing
		}
		custom_tooltip = {
			fail_text = "requires_behemoth_planetcraft_2"
			count_ship_in_system = { limit = { is_ship_size = giga_planet_behemoth fleet = { is_leased = no } } 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 = giga_gigastructures
		cost = {
			unity = @giga_tera_unity_cost
		}
		inline_script = {
			script = megastructures/generic_parts/giga_mega_alloy_cost
			alloys = 300000
		}

		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_celestial_printing
		}
		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 fleet = { is_leased = no } }	count >= 1 }
			count_ship_in_system = { limit = { is_ship_size = giga_war_moon fleet = { is_leased = no } }	count >= 1 }
		}
	}

	on_build_start = {
		from = { remove_country_flag = systemcraft_crew_required }

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

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

			random_owned_ship = {
				limit = { is_ship_size = giga_war_moon }
				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
			#stellar_manip_create_o_star = yes
			stellar_manip_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_fallen_empire = yes
									is_homicidal = yes
								}
								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_fallen_empire = yes
									is_homicidal = yes
								}

								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
	}
}
