# Ascension Perks Broadened - Inheritance of Giants (ap_inheritance_of_giants)
# Used by events/apb_inheritance_of_giants_events.txt. Scope: country (the perk owner, = root).
#
# Flags (country):
#   apb_giants_ok_<type>     the type can still be offered (set by apb_giants_check_types)
#   apb_giants_offer_<type>  the type is in the current offer of three
#
# A suitable site (system scope) is written out inline wherever it is needed:
#   exists = starbase
#   has_no_non_gate_megastructure = yes                (vanilla: no megastructure or ruin other than gates)
#   NOT = { any_system_planet = { is_planet_class = pc_cosmogenesis_world } }
#   NOT = { has_star_flag = apb_giants_site }

apb_giants_clear_flags = {
	remove_country_flag = apb_giants_ok_think_tank
	remove_country_flag = apb_giants_ok_spy_orb
	remove_country_flag = apb_giants_ok_mega_art_installation
	remove_country_flag = apb_giants_ok_strategic_coordination_center
	remove_country_flag = apb_giants_ok_interstellar_assembly
	remove_country_flag = apb_giants_ok_mega_shipyard
	remove_country_flag = apb_giants_offer_think_tank
	remove_country_flag = apb_giants_offer_spy_orb
	remove_country_flag = apb_giants_offer_mega_art_installation
	remove_country_flag = apb_giants_offer_strategic_coordination_center
	remove_country_flag = apb_giants_offer_interstellar_assembly
	remove_country_flag = apb_giants_offer_mega_shipyard
}

# A type is offered only if the empire owns no megastructure of that line (any stage, ruined or
# restored), has not started building one (the vanilla one-per-empire flag), has no ruin of it
# inside its borders, and has the DLC that the line's technology needs.
apb_giants_check_types = {
	apb_giants_clear_flags = yes

	if = {
		limit = {
			NOT = { has_country_flag = built_think_tank }
			NOT = {
				any_owned_megastructure = {
					OR = {
						is_megastructure_type = think_tank_0
						is_megastructure_type = think_tank_1
						is_megastructure_type = think_tank_2
						is_megastructure_type = think_tank_3
						is_megastructure_type = think_tank_4
						is_megastructure_type = think_tank_ruined
						is_megastructure_type = think_tank_restored
					}
				}
			}
			NOT = {
				any_system_within_border = {
					any_system_megastructure = { is_megastructure_type = think_tank_ruined }
				}
			}
		}
		set_country_flag = apb_giants_ok_think_tank
	}

	if = {
		limit = {
			NOT = { has_country_flag = built_spyorb }
			NOT = {
				any_owned_megastructure = {
					OR = {
						is_megastructure_type = spy_orb_0
						is_megastructure_type = spy_orb_1
						is_megastructure_type = spy_orb_2
						is_megastructure_type = spy_orb_3
						is_megastructure_type = spy_orb_4
						is_megastructure_type = spy_orb_ruined
						is_megastructure_type = spy_orb_restored
					}
				}
			}
			NOT = {
				any_system_within_border = {
					any_system_megastructure = { is_megastructure_type = spy_orb_ruined }
				}
			}
		}
		set_country_flag = apb_giants_ok_spy_orb
	}

	if = {
		limit = {
			has_megacorp_dlc = yes
			NOT = { has_country_flag = built_mega_art_installation }
			NOT = {
				any_owned_megastructure = {
					OR = {
						is_megastructure_type = mega_art_installation_0
						is_megastructure_type = mega_art_installation_1
						is_megastructure_type = mega_art_installation_2
						is_megastructure_type = mega_art_installation_3
						is_megastructure_type = mega_art_installation_4
						is_megastructure_type = mega_art_installation_ruined
						is_megastructure_type = mega_art_installation_restored
						is_megastructure_type = mega_art_installation_restored_2
					}
				}
			}
			NOT = {
				any_system_within_border = {
					any_system_megastructure = { is_megastructure_type = mega_art_installation_ruined }
				}
			}
		}
		set_country_flag = apb_giants_ok_mega_art_installation
	}

	if = {
		limit = {
			has_megacorp_dlc = yes
			NOT = { has_country_flag = built_strategic_coordination_center_site }
			NOT = {
				any_owned_megastructure = {
					OR = {
						is_megastructure_type = strategic_coordination_center_0
						is_megastructure_type = strategic_coordination_center_1
						is_megastructure_type = strategic_coordination_center_2
						is_megastructure_type = strategic_coordination_center_3
						is_megastructure_type = strategic_coordination_center_ruined
						is_megastructure_type = strategic_coordination_center_restored
					}
				}
			}
			NOT = {
				any_system_within_border = {
					any_system_megastructure = { is_megastructure_type = strategic_coordination_center_ruined }
				}
			}
		}
		set_country_flag = apb_giants_ok_strategic_coordination_center
	}

	if = {
		limit = {
			has_megacorp_dlc = yes
			NOT = { has_country_flag = built_interstellar_assembly_site }
			NOT = {
				any_owned_megastructure = {
					OR = {
						is_megastructure_type = interstellar_assembly_0
						is_megastructure_type = interstellar_assembly_1
						is_megastructure_type = interstellar_assembly_2
						is_megastructure_type = interstellar_assembly_3
						is_megastructure_type = interstellar_assembly_4
						is_megastructure_type = interstellar_assembly_ruined
						is_megastructure_type = interstellar_assembly_restored
					}
				}
			}
			NOT = {
				any_system_within_border = {
					any_system_megastructure = { is_megastructure_type = interstellar_assembly_ruined }
				}
			}
		}
		set_country_flag = apb_giants_ok_interstellar_assembly
	}

	if = {
		limit = {
			has_federations_dlc = yes
			NOT = { has_country_flag = built_mega_shipyard_site }
			NOT = {
				any_owned_megastructure = {
					OR = {
						is_megastructure_type = mega_shipyard_0
						is_megastructure_type = mega_shipyard_1
						is_megastructure_type = mega_shipyard_2
						is_megastructure_type = mega_shipyard_3
						is_megastructure_type = mega_shipyard_ruined
						is_megastructure_type = mega_shipyard_restored
					}
				}
			}
			NOT = {
				any_system_within_border = {
					any_system_megastructure = { is_megastructure_type = mega_shipyard_ruined }
				}
			}
		}
		set_country_flag = apb_giants_ok_mega_shipyard
	}
}

# Moves one random type from "ok" to "offer". Does nothing once every ok flag is used up.
apb_giants_pick_one = {
	if = {
		limit = {
			OR = {
				has_country_flag = apb_giants_ok_think_tank
				has_country_flag = apb_giants_ok_spy_orb
				has_country_flag = apb_giants_ok_mega_art_installation
				has_country_flag = apb_giants_ok_strategic_coordination_center
				has_country_flag = apb_giants_ok_interstellar_assembly
				has_country_flag = apb_giants_ok_mega_shipyard
			}
		}
		random_list = {
			10 = {
				modifier = {
					factor = 0
					NOT = { has_country_flag = apb_giants_ok_think_tank }
				}
				remove_country_flag = apb_giants_ok_think_tank
				set_country_flag = apb_giants_offer_think_tank
			}
			10 = {
				modifier = {
					factor = 0
					NOT = { has_country_flag = apb_giants_ok_spy_orb }
				}
				remove_country_flag = apb_giants_ok_spy_orb
				set_country_flag = apb_giants_offer_spy_orb
			}
			10 = {
				modifier = {
					factor = 0
					NOT = { has_country_flag = apb_giants_ok_mega_art_installation }
				}
				remove_country_flag = apb_giants_ok_mega_art_installation
				set_country_flag = apb_giants_offer_mega_art_installation
			}
			10 = {
				modifier = {
					factor = 0
					NOT = { has_country_flag = apb_giants_ok_strategic_coordination_center }
				}
				remove_country_flag = apb_giants_ok_strategic_coordination_center
				set_country_flag = apb_giants_offer_strategic_coordination_center
			}
			10 = {
				modifier = {
					factor = 0
					NOT = { has_country_flag = apb_giants_ok_interstellar_assembly }
				}
				remove_country_flag = apb_giants_ok_interstellar_assembly
				set_country_flag = apb_giants_offer_interstellar_assembly
			}
			10 = {
				modifier = {
					factor = 0
					NOT = { has_country_flag = apb_giants_ok_mega_shipyard }
				}
				remove_country_flag = apb_giants_ok_mega_shipyard
				set_country_flag = apb_giants_offer_mega_shipyard
			}
		}
	}
}

# Spawns the chosen ruin (TYPE = a vanilla *_ruined megastructure) in a random suitable system in
# our borders. spawn_megastructure arguments as in vanilla's ruin initializers
# (mega_shipyard_ruined / interstellar_assembly_ruined: type + planet = star), plus owner = root as
# vanilla's behemoth_egg_ruined spawn does. If no site is left, waits a year and offers again.
apb_giants_spawn_ruin = {
	if = {
		limit = {
			any_system_within_border = {
				exists = starbase
				has_no_non_gate_megastructure = yes
				NOT = { any_system_planet = { is_planet_class = pc_cosmogenesis_world } }
				NOT = { has_star_flag = apb_giants_site }
			}
		}
		random_system_within_border = {
			limit = {
				exists = starbase
				has_no_non_gate_megastructure = yes
				NOT = { any_system_planet = { is_planet_class = pc_cosmogenesis_world } }
				NOT = { has_star_flag = apb_giants_site }
			}
			spawn_megastructure = {
				type = $TYPE$
				planet = star
				owner = root
			}
			set_star_flag = apb_giants_site
			save_event_target_as = apb_giants_site
		}
		if = {
			limit = {
				NOT = { has_technology = tech_mega_engineering }
			}
			add_tech_progress = {
				tech = tech_mega_engineering
				progress = 0.5
			}
		}
		apb_giants_clear_flags = yes
		country_event = { id = apb_giants.5 }
	}
	else = {
		apb_giants_clear_flags = yes
		country_event = { id = apb_giants.4 }
		country_event = { id = apb_giants.2 days = 360 }
	}
}
