###########################################
#
# Toxoids Events
# By Pierre du Plessis and Hadrien Buytaers
#
###########################################

namespace = toxoids

planet_event = {
	id = toxoids.1
	is_triggered_only = yes
	hide_window = yes

	trigger = {
		OR = {
			has_building = building_coordinated_fulfillment_center_1
			has_building = building_coordinated_fulfillment_center_2
		}
		NOR = {
			merg_is_nuked = yes
			merg_is_arcology = yes
			merg_is_relic_world = yes
			merg_is_habitat = yes
			merg_is_hab_ringworld = yes
			habitable_structure = yes
			is_artificial = yes
			# Gigas
			inline_script = {
				script = merger_of_rules/toggled_code
				code = "
					is_giga_maginot_world = yes
				"
				toggle = @has_gigastructures
			}
			# Planetary Diversity Ascension Worlds
			inline_script = {
				script = merger_of_rules/toggled_code
				code = "
					pd_aw_is_wasteland_planet = yes
				"
				toggle = @has_planetary_diversity_ascension_worlds
			}
		}
		exists = owner
		owner = { is_relentless_industrialist_empire = yes }
	}

	immediate = {
		if = {
			limit = {
				owner = { has_policy_flag = relentless_industrialists_cleanup }
				check_variable = {
					which = relentless_industrialists_var
					value > 0
				}
			}
			subtract_variable = {
				which = relentless_industrialists_var
				value = 1
			}
		}
		else = {
			if = {
				limit = { has_active_building = building_coordinated_fulfillment_center_1 }
				change_variable = {
					which = relentless_industrialists_var
					value = 1
				}
			}
			else_if = { #ruined or disabled
				limit = { has_building = building_coordinated_fulfillment_center_1 }
				change_variable = {
					which = relentless_industrialists_var
					value = 0.5
				}
			}
			else_if = {
				limit = { has_active_building = building_coordinated_fulfillment_center_2 }
				change_variable = {
					which = relentless_industrialists_var
					value = 2
				}
			}
			else = {
				change_variable = {
					which = relentless_industrialists_var
					value = 1
				}
			}
		}
	}

	after = {
		if = {
			limit = {
				check_variable = {
					which = relentless_industrialists_var
					value >= 30
				}
				NOT = {
					owner = {
						OR = {
							has_policy_flag = relentless_industrialists_cleanup
							country_has_situation = { SITUATION = relentless_industrialists_situation }
						}
					}
				}
			}
			if = {
				limit = {
					NOT = {
						owner = { has_country_flag = relentless_industrialists_situation_happened }
					}
				}
				planet_event = { id = toxoids.5 days = 15 random = 180 }
			}
			else = {
				change_pc = pc_nuked
				if = {
					limit = {
						owner = { has_country_flag = relentless_industrialists_climate_apocalypse_sad }
					}
					add_modifier = {
						modifier = relentless_industrialists_tomb
						years = 10
					}
				}
				create_message = {
					type = TOMB_WORLD_FROM_INDUSTRY_MESSAGE_TYPE
					localization = TOMB_WORLD_FROM_INDUSTRY_MESSAGE_TYPE_DESC
					days = 20
					target = this
				}
				if = {
					limit = {
						OR = {
							is_active_resolution = "resolution_ecology_environmental_control_board"
							is_active_resolution = "resolution_ecology_paradise_initiative"
						}
					}
					owner = {
						set_timed_country_flag = { flag = resolution_breached_terraformed days = 3600 }
					}
				}
			}
		}
	}
}
