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

namespace = fallen_empires_awakening

country_event = {
	id = fallen_empires_awakening.0
	hide_window = yes
	is_test_event = yes

	immediate = {
		event_target:fallen_empire_hive_control = {
			country_event = { id = fallen_empires_awakening.10 }
			add_to_galactic_community = yes
			set_global_flag = control_awoke
		}
	}
}

# Sleepers Awake
country_event = {
	id = fallen_empires_awakening.1
	title = OK
	desc = OK
	hide_window = yes
	trigger = {
		is_country_type = fallen_empire
		NOT = { has_country_flag = awakening_not_allowed }
		OR = {
			# Fragged a holy world
			has_country_flag = holy_world_killed
			# 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_machine_empire = 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_playable_country = {
					OR = {
						fleet_power > 70000
						AND = {
							has_federation = yes
							federation = {
								fleet_power > 90000
							}
						}
						any_owned_planet = {
							has_planet_flag = fallen_empire_world
						}
					}
				}
			}
		}
		OR = {
			has_origin = origin_fallen_empire
			AND = {
				has_origin = origin_fallen_empire_hive
				is_same_empire = event_target:strongest_fragment
			}
		}
	}

	mean_time_to_happen = {
		years = 50
		modifier = {
			factor = 0.5
			any_playable_country = {
				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_playable_country = {
				any_owned_planet = {
					has_planet_flag = fallen_empire_world
					NOT = { is_original_owner = root }
				}
			}
		}
		modifier = {
			factor = 0.1
			has_origin = origin_fallen_empire_hive
			event_target:fallen_empire_hive_forgotten = {
				num_fleets = 0
			}
		}
	}

	immediate = {
		set_country_code_flags = { colonizer = yes }
		set_global_flag = sleepers_awake_happened

		if = {
			limit = {
				is_hive_empire = no
			}
			country_event = { id = fallen_empires_awakening.3 }
			save_event_target_as = first_awakener
		}
		else_if = {
			limit = { # Making an extra if check here to not break mods too badly
				is_hive_empire = yes
			}
			awaken_strongest_fe_fragment = yes
		}

		# 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"
				event_target:first_awakener = {
					NOT = { has_country_flag = fallen_empire_hive_growth } #The devouring swarm does not need a war in heaven to have fun
				}
			}
			random_list = {
				40 = { # War in Heaven with Fallen Empire of opposing ethos
					random_country = {
						limit = {
							NOT = { is_same_value = event_target:first_awakener }
							is_country_type = fallen_empire
							is_machine_empire = no
							OR = {
								AND = {
									is_fanatic_xenophobe = yes
									event_target:first_awakener = { is_fanatic_xenophile = yes }
								}
								AND = {
									is_fanatic_xenophile = yes
									event_target:first_awakener = { is_fanatic_xenophobe = yes }
								}
								AND = {
									is_fanatic_spiritualist = yes
									event_target:first_awakener = { is_fanatic_materialist = yes }
								}
								AND = {
									is_fanatic_materialist = yes
									event_target:first_awakener = { is_fanatic_spiritualist = yes }
								}
								AND = {
									is_same_empire = event_target:strongest_fragment
									has_country_flag = fallen_empire_hive_war
									event_target:first_awakener = { is_fanatic_xenophobe = yes }
								}
								AND = {
									is_same_empire = event_target:strongest_fragment
									has_country_flag = fallen_empire_hive_control
									event_target:first_awakener = { is_fanatic_xenophile = yes }
								}
								AND = {
									event_target:first_awakener = { has_country_flag = fallen_empire_hive_war }
									is_fanatic_xenophobe = yes
								}
								AND = {
									event_target:first_awakener = { has_country_flag = fallen_empire_hive_control }
									is_fanatic_xenophile = yes
								}
							}
						}
						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 }
						event_target:first_awakener = { set_timed_country_flag = { flag = ai_no_wars days = 3600 } }
						every_country = {
							limit = { is_country_type = default }
							set_timed_country_flag = { flag = ai_no_wars days = 3600 }
						}
					}
				}
				20 = { # War in Heaven with Fallen Empire of different ethos
					random_country = {
						limit = {
							NOT = { is_same_value = event_target:first_awakener }
							is_country_type = fallen_empire
							is_machine_empire = no
							OR = {
								is_hive_empire = no
								is_same_empire = event_target:strongest_fragment
							}
						}
						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 = {
								is_country_type = default
							}
							set_timed_country_flag = { flag = ai_no_wars days = 3600 }
						}
					}
				}
				40 = { # No War in Heaven
					set_global_flag = no_war_in_heaven
				}
			}
		}
		else = { set_global_flag = no_war_in_heaven }
	}
}

country_event = {
	id = fallen_empires_awakening.2
	title = fallen_empires_awakening.2.name
	desc = fallen_empires_awakening.2.desc
	picture = {
		trigger = {
			from = {
				is_hive_empire = no
			}
		}
		picture = GFX_evt_fallen_empire
	}

	picture = {
		trigger = {
			from = {
				is_hive_empire = yes
			}
		}
		picture = GFX_evt_hive_mind_fallen_empire
	}
	show_sound = event_alien_signal
	location = from

	is_triggered_only = yes

	option = {
		name = fallen_empires_awakening.2.a
	}
	option = {
		name = fallen_empires_awakening.2.b
	}
}

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
			}
		}
		if = {
			limit = {
				has_paragon_dlc = yes
			}
			unlock_council_slots = 2
			while = {
				count = 2
				create_leader = {
					class = scientist
					species = owner_main_species
					name = random
					skill = 10
					leader_age_min = 25
					leader_age_max = 45
				}
			}
		}

		add_awakened_fallen_empire_resources = yes
		add_awakened_fallen_empire_fleet = yes

		# Establish communications
		every_country = {
			limit = {
				OR = {
					is_country_type = default
					is_country_type = fallen_empire
					is_country_type = awakened_fallen_empire
				}
				NOT = { has_communications = root }
			}

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

	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
		if = {
			limit = {
				is_hive_empire = no
			}
			country_event = { id = fallen_empires_awakening.3 }
		}
		else = {
			event_target:fallen_empire_hive_war = {
				country_event = { id = fallen_empires_awakening.10 }
			}
		}

		every_country = {
			limit = {
				is_country_type = default
			}
			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 }
	}
}

country_event = {
	id = fallen_empires_awakening.5
	title = fallen_empires_awakening.5.name
	desc = fallen_empires_awakening.5.desc
	picture = {
		trigger = {
			from = {
				is_hive_empire = no
			}
		}
		picture = GFX_evt_fallen_empire
	}

	picture = {
		trigger = {
			from = {
				is_hive_empire = yes
			}
		}
		picture = GFX_evt_hive_mind_fallen_empire
	}
	show_sound = event_alien_signal
	location = from

	is_triggered_only = yes

	option = {
		name = fallen_empires_awakening.5.a
	}
	option = {
		name = fallen_empires_awakening.5.b
	}
}

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

	hide_window = yes

	is_triggered_only = yes

	immediate = {
		set_country_type = awakened_fallen_empire
		set_country_flag = sleepers_awake_first_sleeper
		switch = {
			trigger = has_country_flag
			fallen_empire_hive_war = {
				change_government = {
					civics = {
						civic = civic_restored_mind
						civic = civic_ancient_memories_of_war
					}
				}
			}
			fallen_empire_hive_growth = {
				change_government = {
					civics = {
						civic = civic_restored_mind
						civic = civic_ancient_memories_of_growth
						civic = civic_hive_devouring_swarm
					}
				}
			}
			fallen_empire_hive_control = {
				change_government = {
					civics = {
						civic = civic_restored_mind
						civic = civic_ancient_memories_of_control
					}
				}
				if = {
					limit = {
						is_galactic_community_formed = yes
					}
					add_to_galactic_community = yes
				}
				set_global_flag = control_awoke
				every_playable_country = {
					limit = {
						is_ai = no
					}
					country_event = { id = bio.3400 days = 1 }
				}
			}
		}
		change_country_flag = {
			icon = {
				category = "special"
				file = "hive_fe_flag_restored.dds"
			}
			background = {
				category = "backgrounds"
				file = "flag_BG_39.dds"
			}
			colors = {
				"light_orange"
				"dark_orange"
				"black"
				"null"
			}
		}
		# Capture absorbed fragments' fleet power so add_awakened_fallen_empire_fleet can offset its target;
		# integrate_country_effect transfers fleets but root.fleet_power's cache won't refresh inside this immediate block.
		set_variable = {
			which = awakening_absorbed_power
			value = 0
		}
		every_country = {
			limit = {
				has_ai_personality = fallen_empire_hive_mind
			}
			if = {
				limit = { NOT = { is_same_value = root } }
				root = {
					change_variable = {
						which = awakening_absorbed_power
						value = prev.trigger:fleet_power
					}
				}
			}
			save_event_target_as = assimilated_fe
			set_country_flag = got_assimialted
			every_playable_country = {
				limit = {
					has_communications = root
					NOT = { is_same_value = root }
				}
				country_event = { id = fallen_empires_awakening.11 }
			}
			integrate_country_effect = {
				OWNER = root
			}
		}
		random_system = {
			limit = {
				has_star_flag = last_thought_system
			}
			create_starbase = {
				size = "starbase_outpost"
				owner = root
				effect = {
					while = {
						count = 2
						create_fallen_empire_platform = yes
					}
				}
			}
		}
		event_target:fallen_empire_hive_forgotten = {
			every_owned_fleet = {
				set_owner = root
			}
		}
		add_awakened_fallen_empire_resources = yes
		add_awakened_fallen_empire_fleet = yes

		# Establish communications
		every_country = {
			limit = {
				OR = {
					is_country_type = default
					is_country_type = fallen_empire
					is_country_type = awakened_fallen_empire
				}
				NOT = { has_communications = root }
			}

			establish_communications_no_message = root
			root = { save_event_target_as = contact_empire }
			country_event = { id = action.1 }
		}
	}
}

country_event = {
	id = fallen_empires_awakening.11
	title = fallen_empires_awakening.11.name
	desc = fallen_empires_awakening.11.desc
	show_sound = event_default
	picture = "GFX_evt_city_ruins"
	is_triggered_only = yes

	option = {
		name = OK
	}
}
