namespace = nhsc_crisis

## ##################################################### ##
##          Checking if the prerequites are met          ##
## ##################################################### ##

event = {
	id = nhsc_crisis.1
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		is_crises_allowed = yes
		NOR = { 
			has_global_flag = extradimensional_invasion_happened
			has_global_flag = nhsc_unbidden_crisis_triggered
			has_global_flag = nhsc_unbidden_crisis_started
		}
		OR = {
			has_global_flag = nhsc_dangerous_tech_1_researched
			has_global_flag = nhsc_dangerous_tech_2_researched
		}
	}

	immediate = {
		### someone rushed dangerous techs : current year is < 50
		if = {
			limit = { years_passed < 51 }
			random_country = {
				limit = { is_country_type = default }
				country_event = { id = nhsc_crisis.2 days = 27000 } # wait 75 years
			}
		}
		### current year is < 100
		if = {
			limit = {
				years_passed > 50
				years_passed < 101
			}
			random_country = {
				limit = { is_country_type = default }
				country_event = { id = nhsc_crisis.2 days = 18000 } # wait 50 years
			}
        	}
		# current year is >= 100
		if = {
			limit = { years_passed > 100 }
			random_country = {
				limit = { is_country_type = default }
				country_event = { id = nhsc_crisis.2 days = 9000 } # wait 25 years
			}
		}
	}
}

## ##################################################### ##
##    Setting global flag so that we can get rolling     ##
## ##################################################### ##

country_event = {
	id = nhsc_crisis.2
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		NOR = {
			has_global_flag = extradimensional_invasion_happened
			has_global_flag = nhsc_unbidden_crisis_triggered
			has_global_flag = nhsc_unbidden_crisis_started
		}
	}

	immediate = {
		set_global_flag = nhsc_unbidden_crisis_triggered
	}
}

## ##################################################### ##
##  Rolling dice to start the extradimensional invasion  ##
## ##################################################### ##

event = {
	id = nhsc_crisis.3
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		is_crises_allowed = yes
		has_global_flag = nhsc_unbidden_crisis_triggered
		NOT = {
			has_global_flag = extradimensional_invasion_happened
			has_global_flag = nhsc_unbidden_crisis_started
		}
	}

	immediate = {
		random_list = {
			10 = {
				# only less dangerous techs have been researched
				if = {
					limit = {
						has_global_flag = nhsc_dangerous_tech_1_researched
						NOT = { has_global_flag = nhsc_dangerous_tech_2_researched }
					}
					random_list = {
						# crisis starts
						25 = {
							random_country = {
								limit = { is_country_type = default }
								country_event = { id = nhsc_crisis.4 days = 180 random = 90 }
							}
						}
						# nothing happens
						75 = {}
					}
				}
				# highly-dangerous techs have been researched
				if = {
					limit = { has_global_flag = nhsc_dangerous_tech_2_researched }
					random_list = {
						# crisis starts
						50 = {
							random_country = {
								limit = { is_country_type = default }
								country_event = { id = nhsc_crisis.4 days = 180 random = 90 }
							}
						}
						# nothing happens
						50 = {}
					}
				}
			}
			90 = {} # nothing happens
		}
	}
}

## ##################################################### ##
##          Extradimensional invasion begins...          ##
## ##################################################### ##

country_event = {
	id = nhsc_crisis.4
	hide_window = yes
	is_triggered_only = yes
	fire_only_once = yes

	trigger = {
		is_crises_allowed = yes
		NOT = {
			has_global_flag = extradimensional_invasion_happened
			has_global_flag = nhsc_unbidden_crisis_started
		}
	}

	immediate = {
		set_crisis_sound = extradimensional_crisis_ambient_stage_1
		set_crisis_stage_1 = yes
		create_country = {
			name = "NAME_Portal_Holder_1"
			type = portal_holder
			flag = {
				icon= {
					category = "special"
					file = "extradimensional_01.dds"
				}
				background= {
					category = "backgrounds"
					file = "circle.dds"
				}
				colors={
					"black"
					"black"
					"null"
					"null"
				}
			}
			effect = {
				save_global_event_target_as = portal_holder_1
				every_playable_country = {
					establish_communications_no_message = event_target:portal_holder_1
				}
			}
		}
		awaken_guardians_of_the_galaxy = yes

		set_global_flag = extradimensional_invasion_happened
		random_system = {
			limit = {
				NOT = {
					any_country = {
						OR = {
							is_country_type = fallen_empire
							is_country_type = awakened_fallen_empire
						}
						any_system_within_border = {
							is_same_value = prev
						}
					}
				}
			}
			set_star_flag = extradimensional_origin_system
			save_event_target_as = extradimensional_system
			random_system_planet = {
				create_species = {
					name = "NAME_Unbidden"
					class = EXD
					portrait = exd1
					traits = random
				}
				create_country = {
					name = "NAME_Unbidden"
					type = "extradimensional"
					species = last_created
					name_list = "Extradimensional"
					effect = { set_country_flag = unbidden }
					flag = {
						icon= {
							category = "special"
							file = "extradimensional_01.dds"
						}
						background= {
							category = "backgrounds"
							file = "circle.dds"
						}
						colors={
							"indigo"
							"blue"
							"null"
							"null"
						}
					}
					effect = {
						create_ship_design = { design = "NAME_Void_Shaper" }
						add_ship_design = last_created_design
						create_ship_design = { design = "NAME_Unbidden_Anchor" }
						add_ship_design = last_created_design
						set_graphical_culture = extra_dimensional_01
						save_global_event_target_as = extradimensionals
					}
				}
				last_created_species = { save_event_target_as = extradimensional_species }
				last_created_country = {
					save_event_target_as = extradimensionals
					create_fleet = {
						name = "NAME_Dimensional_Portal"
						effect = {
							set_owner = PREV
							create_ship = {
								name = random
								design = "NAME_Unbidden_Portal"
								graphical_culture = "extra_dimensional_01"
								effect = { set_ship_flag = unbidden_portal }
							}
							set_location = {
								target = PREVPREV
								distance = 40
								angle = random
							}
							save_event_target_as = dimensional_portal
							fleet_event = { id = crisis.1003 days = 15 } # Second Fleet Arrives
							fleet_event = { id = crisis.1003 days = 30 } # Third Fleet Arrives
							fleet_event = { id = crisis.1003 days = 55 } # Fourth Fleet Arrives
							fleet_event = { id = crisis.1003 days = 90 } # Fifth Fleet Arrives
							fleet_event = { id = crisis.1003 days = 180 } # Sixth Fleet Arrives
							fleet_event = { id = crisis.1003 days = 265 } # Seventh Fleet Arrives
							fleet_event = { id = crisis.1003 days = 340 } # Eight Fleet Arrives
							fleet_event = { id = crisis.1003 days = 425 } # Ninth Fleet Arrives
							fleet_event = { id = crisis.1003 days = 550 } # Tenth Fleet Arrives
							fleet_event = { id = crisis.1006 days = 20 } # First Constructor Arrives
							fleet_event = { id = crisis.1006 days = 25 } # Second Constructor Arrives
							fleet_event = { id = crisis.1006 days = 160 } # Third Constructor Arrives
							fleet_event = { id = crisis.1006 days = 310 } # Fourth Constructor Arrives
							event_target:extradimensionals = { country_event = { id = crisis.1260 days = 350 } } # Spawn Cycle starts
							#set_owner = event_target:portal_holder_1 #when first anchor built
						}
					}
					create_leader = {
						type = admiral
						species = event_target:extradimensional_species
						name = random
						skill = 3
						traits = {
							trait = leader_trait_ethereal
						}
					}
					create_fleet = {
						effect = {
							set_owner = PREV
							create_ship = {
								name = random
								design = "NAME_Revenant"
								graphical_culture = "extra_dimensional_01"
							}
							assign_leader = last_created_leader
							while = {
								count = 20
								create_ship = {
									name = random
									design = "NAME_Revenant"
									graphical_culture = "extra_dimensional_01"
								}
							}
							while = {
								count = 30
								create_ship = {
									name = random
									design = "NAME_Phantom"
									graphical_culture = "extra_dimensional_01"
								}
							}
							while = {
								count = 45
								create_ship = {
									name = random
									design = "NAME_Wraith"
									graphical_culture = "extra_dimensional_01"
								}
							}
							set_location = {
								target = event_target:dimensional_portal
								distance = 5
								angle = random
							}
							set_fleet_stance = aggressive
							set_aggro_range = 500
							set_aggro_range_measure_from = self
						}
					}
				}
			}
			if = {
				limit = { exists = starbase }
				starbase = { fleet = { destroy_fleet = this } }
			}
			create_starbase = {
				size = starbase_exd
				owner = event_target:extradimensionals
			}
		}
		every_country = {
			limit = {
				OR = {
					is_country_type = default
					is_country_type = fallen_empire
					is_country_type = awakened_fallen_empire
				}
				intel_level = {
					level = high
					system = event_target:extradimensional_system
				}
			}
			country_event = { id = crisis.1007 }
		}
		every_country = {
			limit = {
				OR = {
					is_country_type = default
					is_country_type = fallen_empire
					is_country_type = awakened_fallen_empire
				}
				NOT = {
					intel_level = {
						level = high
						system = event_target:extradimensional_system
					}
				}
			}
			country_event = { id = crisis.1008 }
		}
	}
}


## ##################################################### ##
##                 for testing purposes...               ##
## ##################################################### ##

event = {
	id = nhsc_crisis.999
        hide_window = yes
	is_triggered_only = yes

	trigger = {
		is_crises_allowed = yes
	}

	immediate = {
		random_country = {
			limit = { is_country_type = default }
			country_event = { id = nhsc_crisis.4 days = 10 random = 5 }
		}
	}
}
