
namespace = xilin_crisis_trigger

#ϣΣԭΣ

event = {
	id = xilin_crisis_trigger.1
	hide_window = yes

	is_triggered_only = yes

	trigger = {
		is_crises_allowed = yes
		OR = {
				AND = {
						end_game_years_passed > 29
						NOT = { has_global_flag = first_crisis_happened }
				}
				AND = {
						end_game_years_passed > 59
						NOT = { has_global_flag = second_crisis_happened }
				}
				AND = {
						end_game_years_passed > 89
						NAND= { 
									has_global_flag = Contingency_happened
									has_global_flag = Unbidden_happened
									has_global_flag = Prethoryn_Scourge_happened
						}
				}
		}
		
		#NOT = { has_global_flag = galactic_crisis_happened }
	}

	immediate = {
			if = {
					limit = {
							NOT = { has_global_flag = first_crisis_happened }
					}
					set_global_flag = first_crisis_happened
					set_global_flag = galactic_crisis_happened
					random_country = {
							limit = { is_country_type = global_event }
							country_event = { id = xilin_crisis_trigger.2 }
					}
					else = {
							if = {
									limit = {
											NOT = { has_global_flag = second_crisis_happened }
									}
									set_global_flag = second_crisis_happened
									random_country = {
											limit = { is_country_type = global_event }
											country_event = { id = xilin_crisis_trigger.2 }
									}
									else = {
											if = {
													limit = {
															NAND= { 
																 has_global_flag = Contingency_happened
																 has_global_flag = Unbidden_happened
																 has_global_flag = Prethoryn_Scourge_happened
															}
													}
													random_country = {
															limit = { is_country_type = global_event }
															country_event = { id = xilin_crisis_trigger.2 }
													}
											}
									}
							}
					}
			}			
	}
}

country_event = {
	id = xilin_crisis_trigger.2
	hide_window = yes

	is_triggered_only = yes

	immediate = {
		if = {
			limit = {
					 has_global_flag = Contingency_happened
					 has_global_flag = Unbidden_happened
					 has_global_flag = Prethoryn_Scourge_happened			
			}
		  break = yes
		}
	
			random_list = {

				### Prethoryn Scourge (Swarm)
				10 = { 
					modifier = {
						factor = 0
						has_global_flag = Prethoryn_Scourge_happened
					}
					modifier = {
						factor = 2
						end_game_years_passed >= 60
					}
					modifier = {
						factor = 3
						end_game_years_passed >= 80
					}
					modifier = {
						factor = 4
						end_game_years_passed >= 100
					}
					country_event = { id = crisis.10 days = 200 random = 800 }
					set_global_flag = Prethoryn_Scourge_happened
				}

				### Unbidden (Extradimensionals)
				10 = {
					modifier = {
						factor = 0
						NOT = {
							any_country = {
								is_country_type = default
								OR = {
									has_technology = "tech_psi_jump_drive_1"
									has_technology = "tech_jump_drive_1"
								}							
							}						
						}
					}	
					modifier = {
						factor = 0
						has_global_flag = Unbidden_happened
					}			
					modifier = {
						factor = 4
						any_country = {
							is_country_type = default
							has_technology = "tech_psi_jump_drive_1"
						}
					}
					modifier = {
						factor = 1.5
						any_country = {
							is_country_type = default
							has_country_flag = completed_rogue_scientist
						}
					}
					country_event = { id = crisis.1000 days = 200 random = 800 }
					set_global_flag = Unbidden_happened
				}

				### Contingency (AI)
				10 = {
					modifier = {
						factor = 0
						has_global_flag = Contingency_happened
					}
					modifier = {
						factor = 1.3
						any_country = {
							is_country_type = default
							pop_percentage = {
								percentage > 0.1
								limit = {
									has_trait = "trait_robotic_3" 
									is_growing = no
								}
							}
						}
					}
					modifier = {
						factor = 1.2
						any_country = {
							is_country_type = fallen_empire
							has_authority = auth_machine_intelligence
						}
					}	
					modifier = {
						factor = 1.25
						any_country = {
							has_authority = auth_machine_intelligence
						}
					}
					modifier = {
						factor = 1.5
						any_country = {
							has_authority = auth_machine_intelligence
							count_owned_pops = {
								limit = { has_trait = trait_machine_unit }
								count > 99
							}
						}
					}
					modifier = {
						factor = 2
						any_country = {
							has_authority = auth_machine_intelligence
							count_owned_pops = {
								limit = { has_trait = trait_machine_unit }
								count > 199
							}
						}
					}
					modifier = {
						factor = 3
						any_country = {
							has_authority = auth_machine_intelligence
							count_owned_pops = {
								limit = { has_trait = trait_machine_unit }
								count > 299
							}
						}
					}
					modifier = {
						factor = 2
						any_country = {
							is_country_type = default
							pop_percentage = {
								percentage > 0.25
								limit = {
									has_trait = "trait_robotic_3" 
									is_growing = no
								}
							}
						}
					}
					modifier = {
						factor = 2
						any_country = {
							is_country_type = default
							pop_percentage = {
								percentage > 0.50
								limit = {
									has_trait = "trait_robotic_3" 
									is_growing = no
								}
							}
						}
					}
					modifier = {
						factor = 2
						any_country = {
							is_country_type = default
							pop_percentage = {
								percentage > 0.75
								limit = {
									has_trait = "trait_robotic_3" 
									is_growing = no
								}
							}
						}
					}
					country_event = { id = crisis.2005 days = 200 random = 800 }
					set_global_flag = Contingency_happened
				}
			}
	}

}