namespace = MT_curiosity

# Progressive Astral Rifts spawn
# Copy of astral_planes.2
event = {
	id = MT_curiosity.1
	hide_window = yes

	is_triggered_only = yes

	trigger = {
		has_astral_planes_dlc = yes
		mid_game_years_passed >= 0
		has_tradition = tr_mt_curiosity_bright_ideas
	}

	immediate = {
		every_playable_country = {
			limit = {
				NOT = { has_country_flag = MT_astral_rift_spawn_moratorium }
				is_astral_rift_pool_empty = no
			}

			if = {
				limit = {
					is_nomadic = no
				}
				export_trigger_value_to_variable = {
					trigger = count_system_within_border
					parameters = {
						limit = {
							can_spawn_astral_rift_or_split_astral_scar = yes
						}
					}
					variable = progressive_rift_valid_systems
				}
			}
			else = {
				export_trigger_value_to_variable = {
					trigger = count_owned_nonprimary_starbase
					parameters = {
						limit = {
							is_waystation_starbase = yes
							solar_system? = {
								can_spawn_astral_rift_or_split_astral_scar = yes
							}
						}
					}
					variable = progressive_rift_valid_systems
				}
			}

			if = {
				limit = {
					check_variable = {
						which = progressive_rift_valid_systems
						value > 0
					}
				}

				change_variable = {
					which = years_passed_since_last_rift_spawn
					value = 1
				}

				set_variable_to_random_value = {
					which = astral_rift_spawn_roll
					min = 0
					max = 100
				}

				if = {
					limit = {
						astral_rifts_completed >= @progressive_rift_additional_difficulty_threshold
					}
					change_variable = {
						which = astral_rift_spawn_roll
						value = @progressive_rift_additional_difficulty_past_threshold
					}
				}
			}
			else = {
				clear_variable = progressive_rift_valid_systems
			}
		}

		every_playable_country = {
			limit = {
				is_variable_set = progressive_rift_valid_systems
			}

			if = {
				limit = {
					check_variable_arithmetic  = {
						which = years_passed_since_last_rift_spawn
						multiply = @progressive_rift_spawn_chance_per_year
						multiply = value:progressive_rifts_additional_weight
						multiply = @progressive_rift_system_count_weight_inverted
						add = progressive_rift_valid_systems
						divide = @progressive_rift_system_count_weight_inverted
						value > astral_rift_spawn_roll
					}
				}

				if = {
					limit = {
						is_nomadic = no
					}
					random_planet_within_border = {
						limit = {
							is_astral_scar = yes
							has_anomaly = no
						}

						log = "Splitting Astral Scar to an Astral Rift in system [This.System.GetName] for country [Prev.GetName]."
						split_astral_scar = yes
						prev = {
							set_timed_country_flag = {
								flag = MT_astral_rift_spawn_moratorium
								years = @astral_rift_spawn_moratorium_years
							}
						}
					}

					if = {
						limit = {
							NOT = { has_country_flag = MT_astral_rift_spawn_moratorium }
						}
						random_system_within_border = {
							limit = {
								can_spawn_astral_rift_natural_generation = yes
							}
							log = "Spawning Astral Scar in system [This.GetName] for country [Prev.GetName]."
							spawn_astral_scar = yes
						}
						set_timed_country_flag = {
							flag = MT_astral_rift_spawn_moratorium
							years = @astral_rift_spawn_moratorium_years
						}
					}
				}
				else = {
					save_event_target_as = astral_country
					random_owned_nonprimary_starbase = {
						limit = {
							is_waystation_starbase = yes
							solar_system? = {
								any_system_planet = {
									is_astral_scar = yes
									has_anomaly = no
								}
							}
						}
						solar_system = {
							random_system_planet = {
								limit = {
									is_astral_scar = yes
									has_anomaly = no
								}

								log = "Splitting Astral Scar to an Astral Rift in system [This.System.GetName] for country [event_target:astral_country.GetName]."
								split_astral_scar = yes
								event_target:astral_country = {
									set_timed_country_flag = {
										flag = MT_astral_rift_spawn_moratorium
										years = @astral_rift_spawn_moratorium_years
									}
								}
							}
						}
					}

					if = {
						limit = {
							NOT = { has_country_flag = MT_astral_rift_spawn_moratorium }
						}
						random_owned_nonprimary_starbase = {
							limit = {
								is_waystation_starbase = yes
								solar_system? = {
									can_spawn_astral_rift_natural_generation = yes
								}
							}
							solar_system = {
								log = "Spawning Astral Scar in system [This.GetName] for country [event_target:astral_country.GetName]."
								spawn_astral_scar = yes
							}
						}
						set_timed_country_flag = {
							flag = MT_astral_rift_spawn_moratorium
							years = @astral_rift_spawn_moratorium_years
						}
					}
				}
				clear_variable = years_passed_since_last_rift_spawn
			}
			clear_variable = progressive_rift_valid_systems
			clear_variable = astral_rift_spawn_roll
		}
	}
}


# Scope = Astral Rift
event = {
	id = MT_curiosity.2
	hide_window = yes

	is_triggered_only = yes

	trigger = {
		solar_system = {
			exists = owner
			owner = {
				has_active_tradition = tr_mt_curiosity_call_of_the_unknown
			}
		}
	}

	immediate = {
		solar_system.owner = {
			add_monthly_resource_mult = {
				resource = physics_research
				value = 12
				min = 150
				max = 1000000
			}
		}
	}
}

country_event = {
	id = MT_curiosity.3
	hide_window = yes

	is_triggered_only = yes

	trigger = {
		has_tradition = tr_mt_curiosity_gravity
	}

	immediate = {
		add_monthly_resource_mult = {
			resource = unity
			value = 18
			min = 10
			max = 1000000
		}
	}
}