namespace = dwe_colossus

# Transmuted
carrier_event = {
	id = dwe_colossus.1
	is_triggered_only = yes
	hide_window = yes

	immediate = {
		if = {
			limit = {
				NOT = { is_planet_class = pc_ai }
			}
			from.owner = { country_event = { id = dwe_colossus.2 } }
		}

		#Dismantle observation post
		if = {
			limit = {
				has_observation_outpost = yes
			}
			observation_outpost_owner = {
				country_event = { 
					id = planet_destruction.1000 
					days = 1 
				}
			}
		}

		# Contingency Machine World
		if = {
			limit = {
				is_planet_class = pc_ai
				NOT = { has_planet_flag = machine_lair }
			}
			set_planet_flag = destroyed_by_colossus
			set_planet_flag = planet_cracked
			carrier_event = { id = crisis.2040 }
		}

		# Contingency Final Machine World
		if = {
			limit = {
				is_planet_class = pc_ai
				has_planet_flag = machine_lair
			}
			set_planet_flag = destroyed_by_colossus
			set_planet_flag = planet_cracked
			from.owner = { save_event_target_as = final_machine_world_destroyer }
			stop_crisis_sound = yes
			carrier_event = { id = crisis.2046 }
		}

		# Swarm Situation Log counter
		if = {
			limit = {
				exists = owner
				owner = { is_country_type = swarm }
			}
			every_playable_country = {
				limit = { has_event_chain = "prethoryn_scourge_chain" }
				add_event_chain_counter = {
					event_chain = "prethoryn_scourge_chain"
					counter = "infested_worlds"
					amount = -1
				}
				add_event_chain_counter = {
					event_chain = "prethoryn_scourge_chain"
					counter = "infested_worlds_cleansed"
					amount = 1
				}
			}
		}

		if = {
			limit = {
				OR = {
					is_active_resolution = "resolution_rulesofwar_reverence_for_life"
					is_active_resolution = "resolution_rulesofwar_independent_tribunals"
					is_active_resolution = "resolution_rulesofwar_last_resort_doctrine"
					is_active_resolution = "resolution_rulesofwar_demobilization_initiative"
				}
			}
			from.owner = {
				set_timed_country_flag = { 
					flag = resolution_breached_fired_cracker days = 3600 
				}
			}
		}

		if = {
			limit = {
				exists = owner
				owner = {
					NOT = { is_same_empire = from.owner }
					OR = {
						is_country_type = default
						is_fallen_empire = yes
					}
				}
			}
			# Generate threat
			if = {
				limit = {
					is_colony = yes
				}
				add_threat = { who = from.owner amount = 5 }
				# modifier for allies + those upset by genocide
				every_country = {
					limit = {
						NOR = {
							is_same_empire = from.owner
							is_same_empire = root.owner
							AND = {
								has_federation = yes
								is_in_federation_with = from.owner
							}
						}
						OR = {
							has_communications = from.owner
							has_communications = root.owner
						}
						OR = {
							AND = {
								has_federation = yes
								is_in_federation_with = root.owner
							}
							has_ai_personality = awakened_fallen_empire_xenophile
							AND = {
								is_country_type = default
								OR = {
									is_egalitarian = yes
									is_xenophile = yes
								}
							}
						}
					}
					if = {
						limit = { root = { is_colony = no } }
						add_opinion_modifier = {
							modifier = opinion_cracked_an_uninhabited_world
							who = from.owner
						}
					}
					else = {
						add_opinion_modifier = {
							modifier = opinion_cracked_a_world
							who = from.owner
						}
					}
				}
			}
			else = {
				add_threat = { who = from.owner amount = 1 }
			}
			# modifiers for victim
			owner = {
				if = {
					limit = { root = { is_colony = no } }
					add_opinion_modifier = {
						modifier = opinion_cracked_my_uninhabited_world
						who = from.owner
					}
				}
				else = {
					add_opinion_modifier = {
						modifier = opinion_cracked_my_world
						who = from.owner
					}
				}
				if = {
					limit = { NOT = { has_ethic = ethic_gestalt_consciousness } }
					add_modifier = {
						modifier = colossus_victim
						days = 10800 # 30 years
					}
				}
			}
			if = {
				limit = {
					exists = owner
					owner = { is_country_type = primitive }
				}
				# modifier for allies + those upset by genocide
				every_country = {
					limit = {
						NOT = { is_same_empire = from.owner }
						OR = {
							has_communications = from.owner
							has_communications = root.owner
						}
						OR = {
							has_ai_personality = awakened_fallen_empire_xenophile
							AND = {
								is_country_type = default
								NOR = {
									is_xenophobe = yes
									is_homicidal = yes
									is_xenophile = yes
								}
							}
						}
					}
					add_opinion_modifier = {
						modifier = opinion_cracked_a_primitive_world
						who = from.owner
					}
				}
				every_country = {
					limit = {
						NOT = { is_same_empire = from.owner }
						OR = {
							has_communications = from.owner
							has_communications = root.owner
						}
						OR = {
							has_ai_personality = awakened_fallen_empire_xenophile
							AND = {
								is_country_type = default
								is_xenophile = yes
							}
						}
					}
					add_opinion_modifier = {
						modifier = opinion_cracked_a_primitive_world_phile
						who = from.owner
					}
				}
			}
		}
		# Add war exhaustion to planet owner
		if = {
			limit = {
				exists = owner
			}
			owner = {
				add_static_war_exhaustion = {
					attacker = from.fleet.owner
					location = root
					value_for_planet_destruction = 1.1
				}
			}
		}
		#every_owned_pop = {
		#	kill_pop = yes
		#}
		every_owned_pop_group = {
			kill_pop_group = {
				pop_group = this
				amount = local_pop_amount #5%
				#percentage = 0.10
			}
		}
		remove_all_buildings = yes
		remove_planet = yes
	}
}

country_event = {
	id = dwe_colossus.2
	title = dwe_colossus.2.name
	desc = dwe_colossus.2.desc
	picture = GFX_evt_wormhole
	show_sound = event_mystic_reveal
	location = from


	is_triggered_only = yes

	trigger = {
		NOT = { has_country_flag = fired_nullvoid }
		FROM = {
			is_artificial = no
		}
	}

	immediate = {
		set_country_flag = fired_nullvoid
	}

	option = {
		name = dwe_colossus.2.a
		trigger = {
			NOR = {
				has_valid_civic = civic_fanatic_purifiers
				has_valid_civic = civic_machine_terminator
			}
		}
	}

	option = {
		name = planet_destruction.101.a.purifexter
		trigger = {
			OR = {
				has_valid_civic = civic_fanatic_purifiers
				has_valid_civic = civic_machine_terminator
			}
		}
		add_monthly_resource_mult = {
			resource = unity
			value = 60
			min = 600
			max = 3000
		}
	}
}