namespace = est_general

#Sets flag that the mod is in use
event = {
	id = est_general.1
	hide_window = yes
	is_triggered_only = yes
	fire_only_once = yes

	immediate = {
		if = {
			limit = {
				NOT = { 
					has_global_flag = expanded_stellaris_traditions_mod_in_use 
				}
			}
			set_global_flag = expanded_stellaris_traditions_mod_in_use 
		}
	}
}

#Remove planet modifiers from a planet when ownerless or occupation is transfered.
planet_event = {
	id = est_general.2
	hide_window = yes
	is_triggered_only = yes
	
	immediate = {					
		#Aesthetics
		if = {
			limit = { has_modifier = mod_est_aesthetics_adopt }
			remove_modifier = "mod_est_aesthetics_adopt"
		}
		
		#Commerce
		if = {
			limit = { has_modifier = mod_est_commerce_2_energy }
			remove_modifier = "mod_est_commerce_2_energy"
		}
		if = {
			limit = { has_modifier = mod_est_commerce_2_food }
			remove_modifier = "mod_est_commerce_2_food"
		}
		if = {
			limit = { has_modifier = mod_est_commerce_2_minerals }
			remove_modifier = "mod_est_commerce_2_minerals"
		}
				
		#Foundation/Confederacy/Subminds
		if = {
			limit = { has_modifier = mod_est_foundation_adopt }
			remove_modifier = "mod_est_foundation_adopt"
		}
		if = {
			limit = { has_modifier = mod_est_foundation_1 }
			remove_modifier = "mod_est_foundation_1"
		}
		if = {
			limit = { has_modifier = mod_est_foundation_2_1 }
			remove_modifier = "mod_est_foundation_2_1"
		}
		if = {
			limit = { has_modifier = mod_est_foundation_2_2 }
			remove_modifier = "mod_est_foundation_2_2"
		}
		if = {
			limit = { has_modifier = mod_est_foundation_2_3 }
			remove_modifier = "mod_est_foundation_2_3"
		}
		
		if = {
			limit = { has_modifier = mod_est_confederacy_adopt }
			remove_modifier = "mod_est_confederacy_adopt"
		}
		if = {
			limit = { has_modifier = mod_est_confederacy_2 }
			remove_modifier = "mod_est_confederacy_2"
		}
		
		if = {
			limit = { has_modifier = mod_est_subminds_adopt }
			remove_modifier = "mod_est_subminds_adopt"
		}
		if = {
			limit = { has_modifier = mod_est_subminds_2 }
			remove_modifier = "mod_est_subminds_2"
		}
		if = {
			limit = { has_modifier = mod_est_subminds_3_energy }
			remove_modifier = "mod_est_subminds_3_energy"
		}
		if = {
			limit = { has_modifier = mod_est_subminds_3_minerals }
			remove_modifier = "mod_est_subminds_3_minerals"
		}
		if = {
			limit = { has_modifier = mod_est_subminds_3_food }
			remove_modifier = "mod_est_subminds_3_food"
		}
		if = {
			limit = { has_modifier = mod_est_subminds_3_research }
			remove_modifier = "mod_est_subminds_3_research"
		}		
		if = {
			limit = { has_modifier = mod_est_subminds_3_unity }
			remove_modifier = "mod_est_subminds_3_unity"
		}		
		
		#Friendship/Malice/Perseverance (IP)
		if = {
			limit = { has_modifier = mod_est_friendship_finish }
			remove_modifier = "mod_est_friendship_finish"
		}
		
		if = {
			limit = { has_modifier = mod_est_malice_1_no_slaves }
			remove_modifier = "mod_est_malice_1_no_slaves"
		}	

		#Industry
		if = {
			limit = { has_modifier = mod_est_industry_5 }
			remove_modifier = "mod_est_industry_5"
		}
		if = {
			limit = { has_modifier = mod_est_industry_5_GC }
			remove_modifier = "mod_est_industry_5_GC"
		}
		if = {
			limit = { has_modifier = mod_est_industry_5_machines }
			remove_modifier = "mod_est_industry_5_machines"
		}

		#Liberty
		if = {
			limit = { has_modifier = mod_est_liberty_3 }
			remove_modifier = "mod_est_liberty_3"
		}	

		#Cooperativity		
		if = {
			limit = { has_modifier = mod_est_cooperativity_adopt_1 }
			remove_modifier = "mod_est_cooperativity_adopt_1"
		}
		if = {
			limit = { has_modifier = mod_est_cooperativity_adopt_2 }
			remove_modifier = "mod_est_cooperativity_adopt_2"
		}
		if = {
			limit = { has_modifier = mod_est_cooperativity_adopt_3 }
			remove_modifier = "mod_est_cooperativity_adopt_3"
		}	
		if = {
			limit = { has_modifier = mod_est_cooperativity_5 }
			remove_modifier = "mod_est_cooperativity_5"
		}	
		if = {
			limit = { has_modifier = mod_est_cooperativity_5_machines }
			remove_modifier = "mod_est_cooperativity_5_machines"
		}	

		#Spaceborn
		if = {
			limit = { has_modifier = mod_est_spaceborn_5_habitat }
			remove_modifier = "mod_est_spaceborn_5_habitat"
		}	

		#Collectivism/Perseverance
		if = {
			limit = { has_modifier = mod_est_collectivism_3 }
			remove_modifier = "mod_est_collectivism_3"
		}
	}
}

#Checks that the country have built a robot.
planet_event = {
	id = est_general.3
	hide_window = yes
	is_triggered_only = yes
	
	trigger = {	
		owner = {
			Not = {
				has_country_flag = est_built_robot
			}
		}
	}
	
	immediate = {
		owner = {
			set_country_flag = est_built_robot
		}
	}
}

#A war have begun, triggers other events.
country_event = {
	id = est_general.4
	hide_window = yes
	is_triggered_only = yes
	
	immediate = {
		country_event = {
			id = est_general.7
			days = 1
		}
	}
}

#A war have ended (triggered if one side won), triggers other events.
country_event = {
	id = est_general.5
	hide_window = yes
	is_triggered_only = yes
	
	immediate = {
		fromfrom = {
			every_war_participant = {
				country_event = {
					id = est_general.7
					days = 1
				}
			}
		}
	}
}

#A war have ended (triggered on white peace), triggers other events.
country_event = {
	id = est_general.6
	hide_window = yes
	is_triggered_only = yes
	
	immediate = {
		fromfromfromfrom = {
			every_war_participant = {
				country_event = {
					id = est_general.7
					days = 1
				}
			}
		}
	}
}

#Updates war modifiers
country_event = {
	id = est_general.7
	hide_window = yes
	is_triggered_only = yes
	
	immediate = {
		## (Metallichydra): Depricated. Uses triggered modifier now
		#country_event = {
		#	id = est_aesthetics.3
		#}
		country_event = {
			id = est_friendship.3
		}
		country_event = {
			id = est_friendship.13
		}
		country_event = {
			id = est_isolation.3
		}
		country_event = {
			id = est_isolation.4
		}
		country_event = {
			id = est_proselytism.4
		}
		country_event = {
			id = est_destruction.6
		}
		country_event = {
			id = est_destruction.8
		}
		country_event = {
			id = est_liberty.4
		}
		country_event = {
			id = est_piracy.1
		}
	}
}

country_event = {
	id = est_general.8
	hide_window = yes

	is_triggered_only = yes

	trigger = {
		#has_hydras_more_traditions_active = no # For copying to Expanded Stellaris Traditions
		has_country_flag = cosmogenesis_world_built
		any_owned_planet = {
			is_planet_class = pc_cosmogenesis_world
		}
		OR = {
			has_active_tradition = tr_est_friendship_3
			has_active_tradition = tr_est_proselytism_finish
			has_active_tradition = tr_est_order_5
			has_active_tradition = tr_est_nature_3
			has_active_tradition = tr_est_mystery_finish
			has_active_tradition = tr_est_militarism_1
			has_tradition = tr_est_might_1
			has_tradition = tr_est_aesthetics_2
			has_active_tradition = tr_est_faith_3
			has_active_tradition = tr_est_commerce_finish
			has_tradition = tr_est_philosophy_5
			has_tradition = tr_est_academy_4
		}
	}

	immediate = {
		if = {
			limit = { NOT = { has_global_flag = EST_situation_handler_created } }
			create_EST_situation_handler = yes
		}
		random_owned_planet = {
			limit = {
				is_planet_class = pc_cosmogenesis_world
			}
			if = {
				limit = {
					NOT = {
						any_targeting_situation = {
							is_situation_type = EST_situation_lathe_job_suppression
						}
					}
				}
				save_event_target_as = EST_target_planet
				event_target:EST_situation_handler_country = {
					start_situation = {
						type = EST_situation_lathe_job_suppression
						target = event_target:EST_target_planet
					}
				}
			}
		}
	}
}