############################
#
# Fallen Empire 'Awakening' Events
#
# Written by Martin Anward
#
############################

namespace = fallen_empires_awakening

# Sleepers Awake
country_event = {
	id = fallen_empires_awakening.1
	title = OK
	desc = OK
	hide_window = yes
	trigger = {
		is_country_type = fallen_empire
		merg_has_wula_fe = no
		NOR = {
			has_country_flag = awakening_not_allowed
			# Gigastructural Engineering & More
			inline_script = {
				script = merger_of_rules/toggled_code
				code = "
					has_ai_personality = fallen_empire_gamers
					has_ai_personality = fallen_empire_pouchkinn
				"
				toggle = @has_gigastructures
			}
			# Nirvana Empire Project
			inline_script = {
				script = merger_of_rules/toggled_code
				code = "
					has_country_flag = nirvana_servant
				"
				toggle = @nirvana_mod
			}
		}
		OR = {
			# Fragged a holy world
			has_country_flag = holy_world_killed
			# Expanded Events
			AND = {
				has_country_flag = master_archives_restored
				NOT = { has_country_flag = master_archives_restored_timer }
			}
			# Original trigger
			AND = {
				NOT = { has_global_flag = sleepers_awake_happened }
				NOT = { has_global_flag = guardians_of_the_galaxy_happened }
				end_game_years_passed >= 0
				is_gestalt = no
				is_at_war = no
				fleet_power > 60000
				# Wake up if regular empire is growing too strong or has started to conquer other fallen empires
				any_country = {
					merg_is_default_empire = yes
					OR = {
						fleet_power > 70000
						AND = {
							has_federation = yes
							federation = { fleet_power > 90000 }
						}
						any_owned_planet = { has_planet_flag = fallen_empire_world }
					}
				}
			}
		}
		# Warship Girls R and MIST Species
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				NOR = {
					has_authority = auth_wg_fe
					has_authority = auth_sh_fe
				}
			"
			toggle = @wsg_mod
		}
	}

	mean_time_to_happen = {
		years = 50
		modifier = {
			factor = 0.5
			any_country = {
				merg_is_default_empire = yes
				fleet_power > 120000
			}
		}
		modifier = {
			factor = 1.5
			num_fallen_empires > 1
			num_fallen_empires < 3
		}
		modifier = {
			factor = 2.0
			num_fallen_empires > 2
			num_fallen_empires < 4
		}
		modifier = {
			factor = 2.5
			num_fallen_empires > 3
		}
		modifier = {
			factor = 0.1
			any_country = {
				merg_is_default_empire = yes
				any_owned_planet = {
					has_planet_flag = fallen_empire_world
					NOT = { is_original_owner = root }
				}
			}
		}
		# ZOFE
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				modifier = {
					factor = 0.1
					any_country = {
						is_country_type = ascended_empire
						has_country_flag = new_ascended_empire
					}
				}
			"
			toggle = @has_zofe_vanilla
		}
	}

	immediate = {
		set_global_flag = sleepers_awake_happened

		country_event = { id = fallen_empires_awakening.3 }
		# Notify players
		every_country = {
			limit = {
				is_ai = no
			}
			country_event = { id = fallen_empires_awakening.2 }
		}
		observer_event = { id = observer.73 }

		# Fragged a holy world
		if = {
			limit = {
				has_country_flag = holy_world_killed
			}
			country_event = { id = planet_destruction.611 days = 5 }
		}

		# War in Heaven
		if = {
			limit = {
				host_has_dlc = "Leviathans Story Pack"
				has_crisis = no
			}
			random_list = {
				40 = {
					# Warship Girls R and MIST Species
					inline_script = {
						script = merger_of_rules/toggled_code
						code = "
							modifier = {
								factor = 0
								OR = {
									has_authority = auth_wg_fe
									has_authority = auth_sh_fe
								}
							}
						"
						toggle = @wsg_mod
					}
					# War in Heaven with Fallen Empire of opposing ethos
					random_country = {
						limit = {
							NOT = { is_same_value = root }
							is_country_type = fallen_empire
							is_gestalt = no
							# Nirvana Empire Project
							inline_script = {
								script = merger_of_rules/toggled_code
								code = "
									NOT = { has_country_flag = nirvana_servant }
								"
								toggle = @nirvana_mod
							}
							# Vanilla
							OR = {
								AND = {
									has_ethic = ethic_fanatic_xenophobe
									root = { has_ethic = ethic_fanatic_xenophile }
								}
								AND = {
									has_ethic = ethic_fanatic_xenophile
									root = { has_ethic = ethic_fanatic_xenophobe }
								}
								AND = {
									has_ethic = ethic_fanatic_spiritualist
									root = { has_ethic = ethic_fanatic_materialist }
								}
								AND = {
									has_ethic = ethic_fanatic_materialist
									root = { has_ethic = ethic_fanatic_spiritualist }
								}
							}
						}
						set_country_flag = sleepers_awake_ancient_rival
						set_timed_country_flag = { flag = timed_ancient_rival days = 3600 }
						set_timed_country_flag = { flag = ai_no_wars days = 3600 }
						root = { set_timed_country_flag = { flag = ai_no_wars days = 3600 } }
						every_country = {
							limit = { merg_is_default_empire = yes }
							set_timed_country_flag = { flag = ai_no_wars days = 3600 }
						}
					}
				}
				20 = {
					# Warship Girls R and MIST Species
					inline_script = {
						script = merger_of_rules/toggled_code
						code = "
							modifier = {
								factor = 0
								OR = {
									has_authority = auth_wg_fe
									has_authority = auth_sh_fe
								}
							}
						"
						toggle = @wsg_mod
					}
					# War in Heaven with Fallen Empire of different ethos
					random_country = {
						limit = {
							NOT = { is_same_value = root }
							is_country_type = fallen_empire
							is_gestalt = no
						}
						set_country_flag = sleepers_awake_ancient_rival
						set_timed_country_flag = { flag = timed_ancient_rival days = 3600 }
						set_timed_country_flag = { flag = ai_no_wars days = 3600 }
						root = { set_timed_country_flag = { flag = ai_no_wars days = 3600 } }
						every_country = {
							limit = { merg_is_default_empire = yes }
							set_timed_country_flag = { flag = ai_no_wars days = 3600 }
						}
					}
				}
				# No War in Heaven
				40 = { set_global_flag = no_war_in_heaven }
			}
		}
		else = { set_global_flag = no_war_in_heaven }
	}
}

country_event = {
	id = fallen_empires_awakening.3
	title = OK
	desc = OK

	hide_window = yes

	is_triggered_only = yes

	immediate = {
		set_country_flag = sleepers_awake_first_sleeper
		set_country_type = awakened_fallen_empire
		change_government = {
			civics = {
				civic = civic_revanchist_fervor
				civic = civic_ancient_caches_of_technology
			}
		}

		add_awakened_fallen_empire_resources = yes
		add_awakened_fallen_empire_fleet = yes

		# Establish communications
		every_country = {
			limit = {
				OR = {
					is_country_type = fallen_empire
					is_country_type = awakened_fallen_empire
					merg_is_default_empire = yes
				}
				NOT = { has_communications = root }
				# Nirvana Empire Project
				inline_script = {
					script = merger_of_rules/toggled_code
					code = "
						NOT = { has_country_flag = nirvana_servant }
					"
					toggle = @nirvana_mod
				}
			}
			establish_communications_no_message = root
			root = { save_event_target_as = contact_empire }
			country_event = { id = action.1 }
		}
	}
}

# Guardians of the Galaxy
country_event = {
	id = fallen_empires_awakening.4
	title = OK
	desc = OK
	hide_window = yes

	trigger = {
		merg_has_wula_fe = no
		has_global_flag = galactic_crisis_happened
		NOT = { has_global_flag = sleepers_awake_rival_waking } # Not if WiH happened
		has_country_flag = guardians_of_the_galaxy
		NOT = { has_global_flag = guardians_of_the_galaxy_happened }
		NOT = { has_country_flag = timed_guardians_of_the_galaxy }
		# Warship Girls R and MIST Species
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				NOR = {
					has_authority = auth_wg_fe
					has_authority = auth_sh_fe
				}
			"
			toggle = @wsg_mod
		}
		# Nirvana Empire Project
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				NOT = { has_country_flag = nirvana_servant }
			"
			toggle = @nirvana_mod
		}
	}

	mean_time_to_happen = {
		years = 5
		modifier = {
			factor = 0.01
			NOT = { has_country_flag = timed_guardians_of_the_galaxy_2 }
		}
	}

	immediate = {
		set_global_flag = guardians_of_the_galaxy_happened
		country_event = { id = fallen_empires_awakening.3 }

		every_country = {
			limit = {
				merg_is_default_empire = yes
			}
			random_list = {
				33 = { add_opinion_modifier = { who = root modifier = opinion_crisis_fighter } }
				33 = { add_opinion_modifier = { who = root modifier = opinion_crisis_fighter_small } }
				33 = { }
			}
		}

		# Notify
		every_country = {
			limit = {
				is_ai = no
			}
			country_event = { id = fallen_empires_awakening.5 }
		}
		observer_event = { id = observer.74 }
	}
}
