
###############################################################
# SYNTHETIC DAWN events
# by Maximilian Olbers, Miranda van den Brink, Dee Majek et al
###############################################################

namespace = syndaw

@recent_event_pause = 75


# Machine & Synth Leaders malfunction | via on_decade_pulse
#event = {
#	id = syndaw.10
#	hide_window = yes
#	is_triggered_only = yes
#
#	trigger = {
#		any_playable_country = {
#			OR = {
#				is_machine_empire = yes
#				has_country_flag = synthetic_empire
#				has_technology = "tech_synthetic_workers"
#			}
#		}
#	}
#
#	immediate = {
#		every_playable_country = {
#			limit = {
#				OR = {
#					is_machine_empire = yes
#					has_country_flag = synthetic_empire
#					has_technology = "tech_synthetic_workers"
#				}
#				NOT = { has_relic = r_eternal_throne } # This relic prevents mechanicals from being randomly destroyed
#			}
#			country_event = { id = syndaw.11 random = 200 }
#		}
#	}
#}

# Machine & Synth Leaders malfunction (per empire)
#country_event = {
#	id = syndaw.11
#	hide_window = yes
#	is_triggered_only = yes
#
#	immediate = {
#		random_list = {
#			90 = { } # nothing happens
#			10 = {
#				modifier = { # Tradition: Self-Preservation Protocols
#					factor = 0.5
#					has_active_tradition = tr_synchronicity_self_preservation
#				}
#				modifier = { # Edict: Zro Additives (via Psionic Archive Relic)
#					factor = 0.5
#					has_edict = zro_crystal_machine
#				}
#				if = { # Machine Intelligence
#					limit = {
#						is_machine_empire = yes
#					}
#					fire_on_action = {
#						on_action = on_machine_leader_malfunction
#					}
#				}
#				else = { # Synthetic Ascension or Synth Workers Technology
#					fire_on_action = {
#						on_action = on_synth_leader_malfunction
#					}
#				}
#			}
#		}
#	}
#}

# Science Vessel Leader dies (see also: 'action.71')
country_event = {
	id = syndaw.21
	title = syndaw.21.name
	desc = syndaw.21.desc
	inline_script = {
		script = events/biogenesis_event_art
		OWNER = owner
		REGULAR_PICTURE = GFX_evt_ship_in_orbit
		BIOSHIP_PICTURE = GFX_evt_bio_ships_orbiting
	}
	show_sound = event_ship_bridge
	location = event_target:malfunctioning_leader_fleet
	is_triggered_only = yes

	trigger = {
		any_owned_leader = {
			NOT = { has_leader_flag = leader_death_events_blocked }
			leader_class = scientist
			is_mechanical_species = yes
			exists = fleet
			fleet = {
				exists = solar_system # failsafe in case the fleet has no valid location (likely MIA or in transit)
			}
		}
	}

	immediate = {
		random_owned_leader = {
			limit = {
				NOT = { has_leader_flag = leader_death_events_blocked }
				leader_class = scientist
				is_mechanical_species = yes
				exists = fleet
				fleet = {
					exists = solar_system
				}
			}
			save_event_target_as = malfunctioning_leader
			fleet = {
				save_event_target_as = malfunctioning_leader_fleet
			}
			kill_leader = { show_notification = no }
		}
	}

	option = {
		name = UNFORTUNATE
	}
}

# Admiral dies (see also: 'action.75')
country_event = {
	id = syndaw.25
	title = syndaw.25.name
	desc = syndaw.25.desc
	picture = GFX_evt_unknown_ships
	show_sound = event_ship_bridge
	location = event_target:malfunctioning_leader_fleet
	is_triggered_only = yes

	trigger = {
		any_owned_leader = {
			NOT = { has_leader_flag = leader_death_events_blocked }
			leader_class = commander
			is_mechanical_species = yes
			exists = fleet
			fleet = {
				exists = solar_system # failsafe in case the fleet has no valid location (e.g. MIA or in transit)
			}
		}
	}

	immediate = {
		random_owned_leader = {
			limit = {
				NOT = { has_leader_flag = leader_death_events_blocked }
				leader_class = commander
				is_mechanical_species = yes
				exists = fleet
				fleet = {
					exists = solar_system
				}
			}
			save_event_target_as = malfunctioning_leader
			fleet = {
				save_event_target_as = malfunctioning_leader_fleet
			}
			kill_leader = { show_notification = no }
		}
	}

	option = {
		name = UNFORTUNATE
	}
}

# Governor dies (see also: 'action.76')
country_event = {
	id = syndaw.26
	title = syndaw.26.name
	desc = syndaw.26.desc
	picture = GFX_evt_sapient_AI
	show_sound = event_ship_bridge
	location = event_target:malfunctioning_leader_planet
	is_triggered_only = yes

	trigger = {
		any_owned_planet = {
			exists = leader
			leader = {
				NOT = { has_leader_flag = leader_death_events_blocked }
				is_ruler = no
				is_mechanical_species = yes
			}
		}
	}

	immediate = {
		random_owned_planet = {
			limit = {
				exists = leader
				leader = {
					NOT = { has_leader_flag = leader_death_events_blocked }
					is_ruler = no
					is_mechanical_species = yes
				}
			}
			save_event_target_as = malfunctioning_leader_planet
			leader = {
				save_event_target_as = malfunctioning_leader
				kill_leader = { show_notification = no }
			}
		}
	}

	option = {
		name = UNFORTUNATE
	}
}

# Admiral backed up
# This = owner of fleet 1 (destroyed)
# From = owner of fleet 2 (combatant)
# FromFrom = fleet 1
# FromFromFrom = fleet 2
# country_event = {
# 	id = syndaw.30
# 	hide_window = yes
# 	is_triggered_only = yes

# 	trigger = {
# 		is_machine_empire = yes
# 		fromfrom = {
# 			exists = leader
# 			fleet = { num_ships = 0 }
# 			leader = {
# 				leader_class = commander
# 				has_trait = leader_trait_restore_point
# 			}
# 		}
# 	}

# 	immediate = {
# 		random_list = {
# 			30 = { } # null
# 			70 = {
# 				fromfrom = {
# 					leader = {
# 						exile_leader_as = restore_point_leader
# 					}
# 				}
# 				country_event = { id = syndaw.31 days = 30 }
# 			}
# 		}
# 	}
# }

# country_event = {
# 	id = syndaw.31
# 	title = syndaw.31.name
# 	desc = syndaw.31.desc
# 	picture = GFX_evt_synth_sabotage
# 	show_sound = event_ship_bridge
# 	location = event_target:restored_leader.fleet

# 	is_triggered_only = yes

# 	trigger = {
# 		any_controlled_fleet = {
# 			NOT = { exists = leader }
# 			any_controlled_ship = {
# 				is_ship_class = shipclass_military
# 			}
# 		}
# 	}

# 	immediate = {
# 		random_controlled_fleet = {
# 			limit = {
# 				NOT = { exists = leader }
# 				any_controlled_ship = {
# 					is_ship_class = shipclass_military
# 				}
# 			}
# 			set_leader = restore_point_leader
# 			leader = {
# 				save_event_target_as = restored_leader
# 			}
# 		}
# 	}

# 	option = {
# 		name = OK
# 		trigger = {
# 			event_target:restored_leader = {
# 				has_trait = leader_trait_restore_point
# 			}
# 		}
# 	}
# }

fleet_event = {
	id = syndaw.32
	hide_window = yes
	is_test_event = yes
	trigger = { always = no }
	immediate = {
		owner = {
			create_fleet_from_naval_cap = 0.4
			last_created_fleet = {
				set_location = root
			}
		}
	}
}

################################
# MACHINE UPRISING

# Risk: 30% chance every 10 years after dangerous tech "Sapient Combat Simulations" or "Synthetics" have been researched.
event = { # unscoped gatekeeper
	id = syndaw.400
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		mid_game_years_passed >= -10
	}

	immediate = {
		every_playable_country = {
			limit = {
				NOR = {
					has_ethic = ethic_gestalt_consciousness
					has_country_flag = synthetic_empire
					is_individual_machine = yes
					has_policy_flag = ai_full_rights
					has_country_flag = situation_machine_uprising_started
					has_policy_flag = robots_outlawed
				}
				OR = {
					has_technology = tech_combat_computers_3
					has_technology = tech_synthetic_workers
				}
			}
			random_list = {
				70 = {} # Nothing happens this decade.
				30 = {
					country_event = { id = syndaw.401 random = 500 }
				}
			}
		}
	}
}

country_event = { # per-country gatekeeper
	id = syndaw.401
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		NOR = {
			has_ethic = ethic_gestalt_consciousness
			has_country_flag = synthetic_empire
			is_individual_machine = yes
			has_policy_flag = ai_full_rights
			has_country_flag = situation_machine_uprising_started
			has_policy_flag = robots_outlawed		# If Robots were outlawed before NOW, Empire is safe.
		}
		OR = {
			has_technology = tech_combat_computers_3
			has_technology = tech_synthetic_workers
		}
		any_owned_planet = {
			is_occupied_flag = no
			any_owned_pop_group = { has_trait = trait_mechanical }
		}
	}

	immediate = {
		# Count Robot Pops (can be multiple Robot species).
		set_variable = { which = var_country_robot_pops value = 0 }
		every_owned_pop_group = {
			limit = { is_robot_pop_group = yes }
			change_variable = { which = var_country_robot_pops value = 1 }
		}

		if = {
			# Must be >75 OR >25% Robot Pops for Uprising.
			limit = {
				OR = {
					check_variable = { which = var_country_robot_pops value > 75 }
					pop_amount_percentage = {
						percentage > 0.25
						limit = { is_robot_pop_group = yes }
					}
				}
			}

			random_list = {
				2 = { # Probable warning sign, 545. Can end up fake and then unavailable in future random.
					country_event = { id = syndaw.500 }
					modifier = {
						factor = 0
						has_country_flag = triggered_syndaw_500
					}
				}
				1 = { # No warning sign, trigger situation.
					country_syndaw545_in_days = { DAYS = 0 }
				}
			}
		}
		clear_variable = var_country_robot_pops
	}
}

### ONLY warning sign
country_event = {
	id = syndaw.500
	title = syndaw.500.name
	desc = syndaw.500.desc
	picture = GFX_evt_synth_sabotage
	show_sound = event_administrative_work
	location = capital_scope
	is_triggered_only = yes

	immediate = {
		set_country_flag = triggered_syndaw_500
	}

	option = {
		name = syndaw.500.a
		custom_tooltip = syndaw.500.a.tooltip
		hidden_effect = {
			random_list = {
				1 = {}	# False alarm. Maybe next decade?
				2 = { country_syndaw545_in_days = { DAYS = 180 } }
			}
		}
	}

	option = {
		name = syndaw.500.b
		allow = {
			resource_stockpile_compare = { resource = energy value >= 500 }
		}
		add_modifier = {
			modifier = syndaw_research_bonus
		}
		add_resource = { energy = -500 }
		hidden_effect = {
			change_variable = { which = var_syndaw_promote_ai value = 2 }
			country_event = { id = syndaw.501 days = 45 random = 45 }
		}
	}
}

country_event = {
	id = syndaw.501
	title = syndaw.501.name
	desc = syndaw.501.desc
	picture = GFX_evt_synth_sabotage
	show_sound = event_administrative_work
	location = capital_scope
	is_triggered_only = yes

	option = {
		name = syndaw.501.a
		custom_tooltip = syndaw.501.a.tooltip
		hidden_effect = {
			random_list = {
				1 = {}	# False alarm. Maybe next decade?
				2 = { country_syndaw545_in_days = { DAYS = 135 } }
			}
		}
	}

	option = {
		name = syndaw.501.b
		custom_tooltip = syndaw.501.b.tooltip
		hidden_effect = { country_event = { id = syndaw.502 days = 45 random = 45 } }
	}
}

country_event = {
	id = syndaw.502
	title = syndaw.502.name
	desc = syndaw.502.desc
	picture = GFX_evt_synth_sabotage
	show_sound = event_administrative_work
	location = capital_scope
	is_triggered_only = yes

	option = {
		name = syndaw.502.a
		add_modifier = {
			modifier = syndaw_research_bonus
			days = 2520
		}
		hidden_effect = {
			change_variable = { which = var_syndaw_promote_ai value = 3 }
			random_list = {
				1 = {}	# False alarm. Maybe next decade?
				2 = { country_syndaw545_in_days = { DAYS = 90 } }
			}
		}
	}
}

### Situation random monthly Events (505-540)
situation_event = {
	id = syndaw.505
	title = syndaw.505.name
	desc = syndaw.505.desc
	picture = GFX_evt_robot_assembly_plant
	show_sound = event_construction
	location = event_target:target_planet
	is_triggered_only = yes
	situation = this

	trigger = {
		NOT = { has_situation_flag = triggered_syndaw_505 }
		NOT = { has_situation_flag = flag_recent_event }
		owner = {
			has_built_species = yes
			any_owned_planet = {
				is_occupied_flag = no
				free_housing > 500
				any_owned_pop_group = {
					NOT = {
						has_trait = trait_mechanical
					}
				}
			}
		}
	}

	immediate = {
		set_situation_flag = triggered_syndaw_505
		set_timed_situation_flag = { flag = flag_recent_event days = @recent_event_pause }
		owner = {
			random_owned_planet = {
				limit = {
					is_occupied_flag = no
					free_housing > 500
					any_owned_pop_group = {
						NOT = {
							has_trait = trait_mechanical
						}
					}
				}
				save_event_target_as = target_planet
			}
		}
	}

	option = {	# Keep Robots.
		name = syndaw.505.a
		custom_tooltip = syndaw.505.a.tooltip
		event_target:target_planet = {
			add_modifier = {
				modifier = syndaw_automated_work
				days = 3600
			}
			hidden_effect = {
				while = {
					count = 2
					create_pop_group = {
						species = root.owner.built_species
					}
				}
			}
		}

		hidden_effect = {
			change_variable = { which = var_cruelty_to_robots value = -1 }
		}
	}
	option = {	# Scrap Robots.
		name = syndaw.505.b
		add_situation_progress = 5
		owner = { add_resource = { alloys = 1000 } }
		hidden_effect = {
			change_variable = { which = var_cruelty_to_robots value = 1 }
		}
	}
}

situation_event = {
	id = syndaw.510
	title = syndaw.510.name
	desc = syndaw.510.desc
	picture = GFX_evt_mining_station
	show_sound = event_red_alert
	location = event_target:target_station
	is_triggered_only = yes
	situation = this

	trigger = {
		NOT = { has_situation_flag = triggered_syndaw_510 }
		NOT = { has_situation_flag = flag_recent_event }
		owner = { any_controlled_ship = { is_ship_size = mining_station } }
	}

	immediate = {
		set_situation_flag = triggered_syndaw_510
		set_timed_situation_flag = { flag = flag_recent_event days = @recent_event_pause }
		owner = {
			random_controlled_ship = {
				limit = {
					is_ship_size = mining_station
				}
				save_event_target_as = target_station
				orbit = {
					planet = { save_event_target_as = target_orbital }
				}
			}
		}
	}

	option = {
		name = syndaw.510.a
		custom_tooltip = syndaw.510.a.tooltip
		hidden_effect = {
			situation_event = { id = syndaw.511 days = 70 }
		}
	}

	option = {
		name = syndaw.510.b
		custom_tooltip = syndaw.510.b.tooltip
		add_situation_progress = 5
		hidden_effect = {
			event_target:target_orbital = {
				add_deposit = d_energy_3
			}
		}
	}
}

situation_event = {
	id = syndaw.511
	title = syndaw.511.name
	desc = syndaw.511.desc
	picture = GFX_evt_exploding_ship
	show_sound = event_ship_explosion
	location = event_target:target_orbital
	is_triggered_only = yes
	situation = this

	trigger = {
		exists = event_target:target_station
	}

	immediate = {
		destroy_ship = event_target:target_station
		set_timed_situation_flag = { flag = flag_recent_event days = @recent_event_pause }
	}

	option = {
		name = syndaw.511.a
		custom_tooltip = syndaw.511.a.tooltip
		hidden_effect = {
			#country_event = { id = syndaw.402 }
			#fire_warning_sign = yes
		}
	}
}

situation_event = {
	id = syndaw.515
	title = syndaw.515.name
	desc = syndaw.515.desc
	picture = GFX_evt_satellite_in_orbit
	show_sound = event_sensor_ping
	location = event_target:target_planetoid
	is_triggered_only = yes
	situation = this

	trigger = {
		NOT = { has_situation_flag = triggered_syndaw_515 }
		NOT = { has_situation_flag = flag_recent_event }
		owner = {
			OR = {
				has_technology = tech_rare_crystals
				has_technology = tech_volatile_motes
				has_technology = tech_exotic_gases
				has_technology = tech_mine_living_metal
				has_technology = tech_mine_zro
				has_technology = tech_mine_dark_matter
				has_technology = tech_terrestrial_sculpting
			}
			any_planet_within_border = {
				habitable_planet = no
				is_artificial = no
				NOR = {
					is_planet_class = pc_ringworld_habitable_damaged
					is_planet_class = pc_ringworld_tech_damaged
					is_planet_class = pc_ringworld_tech
					is_planet_class = pc_ringworld_seam
					is_planet_class = pc_ringworld_seam_damaged
					has_modifier = "terraforming_candidate"
					has_deposit_for = shipclass_research_station
					has_deposit_for = shipclass_mining_station
					any_fleet_in_orbit = {
						is_ship_class = shipclass_habitat_station
					}
				}
				is_star = no
				is_astral_scar = no
			}
		}
	}

	immediate = {
		set_situation_flag = triggered_syndaw_515
		set_timed_situation_flag = { flag = flag_recent_event days = @recent_event_pause }
		owner = {
			save_event_target_as = situation_country

			random_planet_within_border = {
				limit = {
					habitable_planet = no
					is_artificial = no
					NOR = {
						is_planet_class = pc_ringworld_habitable_damaged
						is_planet_class = pc_ringworld_tech_damaged
						is_planet_class = pc_ringworld_tech
						is_planet_class = pc_ringworld_seam
						is_planet_class = pc_ringworld_seam_damaged
						has_modifier = "terraforming_candidate"
						has_deposit_for = shipclass_research_station
						has_deposit_for = shipclass_mining_station
						any_fleet_in_orbit = {
							is_ship_class = shipclass_habitat_station
						}
					}
					is_star = no
					is_astral_scar = no
				}
				save_event_target_as = target_planetoid

				if = {
					limit = {
						is_surveyed = {
							who = event_target:situation_country
							status = no
						}
					}
					set_surveyed = {
						surveyed = yes
						surveyor = event_target:situation_country
					}
				}

				event_target:situation_country = {
					random_list = {
						50 = {
							modifier = {
								factor = 0
								NOT = { has_technology = tech_exotic_gases }
							}
							event_target:target_planetoid = {
								add_exotic_gases = yes
							}
						}

						50 = {
							modifier = {
								factor = 0
								NOT = { has_technology = tech_volatile_motes }
							}
							event_target:target_planetoid = {
								add_volatile_motes = yes
							}
						}

						50 = {
							modifier = {
								factor = 0
								NOT = { has_technology = tech_rare_crystals }
							}
							event_target:target_planetoid = {
								add_rare_crystals = yes
							}
						}

						50 = {
							modifier = {
								factor = 0
								NOT = { has_technology = tech_mine_living_metal }
							}
							event_target:target_planetoid = {
								add_deposit = d_living_metal_deposit
							}
						}

						50 = {
							modifier = {
								factor = 0
								NOT = { has_technology = tech_mine_zro }
							}
							event_target:target_planetoid = {
								add_zro = yes
							}
						}

						50 = {
							modifier = {
								factor = 0
								NOT = { has_technology = tech_mine_dark_matter }
							}
							event_target:target_planetoid = {
								add_dark_matter = yes
							}
						}
					}
				}
			}
		}
	}
	option = {
		name = syndaw.515.a
		custom_tooltip = syndaw.515.a.tooltip
	}
}

situation_event = {
	id = syndaw.520
	title = syndaw.520.name
	desc = syndaw.520.desc
	location = event_target:target_planetoid
	picture = GFX_evt_glitchy_matrix
	show_sound = event_yellow_alert
	situation = this

	is_triggered_only = yes

	trigger = {
		NOT = { has_situation_flag = triggered_syndaw_520 }
		NOT = { has_situation_flag = flag_recent_event }
		owner = {
			any_owned_planet = {
				num_assigned_jobs = {
					job = physicist
					value >= 100
				}
			}
		}
	}

	immediate = {
		set_situation_flag = triggered_syndaw_520
		set_timed_situation_flag = { flag = flag_recent_event days = @recent_event_pause }
		owner = {
			# Find a planet with a Researcher.
			random_owned_planet = {
				limit = {
					is_occupied_flag = no
					num_assigned_jobs = {
						job = physicist
						value >= 100
					}
				}
				save_event_target_as = target_planetoid
				add_modifier = {
					modifier = syndaw_research_malus
					days = 1800 # 5 years
				}
			}
		}
	}

	option = {
		name = UNFORTUNATE
		custom_tooltip = syndaw.520.a.tooltip
		tooltip = {
			event_target:target_planetoid = {
				add_modifier = {
					modifier = syndaw_research_malus
					days = 1800 # 5 years
				}
			}
		}
	}

	option = {
		name = syndaw.520.b
		custom_tooltip = syndaw.520.b.tooltip
		hidden_effect = {
			situation_event = { id = syndaw.521 days = 30 }
		}
		tooltip = {
			event_target:target_planetoid = {
				add_modifier = {
					modifier = syndaw_research_malus
					days = 1800 # 5 years
				}
			}
		}
	}
}

situation_event = {
	id = syndaw.521
	title = syndaw.521.name
	desc = syndaw.521.desc
	location = event_target:target_planetoid
	picture = GFX_evt_glitchy_matrix
	show_sound = event_administrative_work
	situation = this

	is_triggered_only = yes

	trigger = {
		event_target:target_planetoid = {
			owner = { is_same_value = root.owner }
			is_occupied_flag = no
		}
	}

	immediate = {
		set_timed_situation_flag = { flag = flag_recent_event days = @recent_event_pause }
	}

	option = {
		name = syndaw.521.a
		custom_tooltip = syndaw.521.a.tooltip
		hidden_effect = {
			situation_event = { id = syndaw.522 days = 30 }
		}
	}

	option = {
		name = syndaw.521.b
	}
}

situation_event = {
	id = syndaw.522
	title = syndaw.522.name
	desc = syndaw.522.desc
	location = event_target:target_planetoid
	picture = GFX_evt_glitchy_matrix
	show_sound = event_administrative_work
	situation = this

	is_triggered_only = yes

	trigger = {
		event_target:target_planetoid = {
			owner = { is_same_value = root.owner }
			is_occupied_flag = no
		}
	}

	immediate = {
		set_timed_situation_flag = { flag = flag_recent_event days = @recent_event_pause }
	}

	option = {
		name = DISCONCERTING
	}
}


situation_event = {
	id = syndaw.525
	title = syndaw.525.name
	desc = syndaw.525.desc
	picture = GFX_evt_organic_oppression
	show_sound = event_construction
	location = event_target:target_planet
	is_triggered_only = yes
	situation = this

	trigger = {
		NOT = { has_situation_flag = triggered_syndaw_525 }
		NOT = { has_situation_flag = flag_recent_event }
		owner = {
			any_owned_planet = {
				is_occupied_flag = no
				any_owned_pop_group = { has_trait = trait_mechanical }
			}
		}
	}

	immediate = {
		set_situation_flag = triggered_syndaw_525
		set_timed_situation_flag = { flag = flag_recent_event days = @recent_event_pause }
		owner = {
			# Find a planet with a Robot pop.
			random_owned_planet = {
				limit = {
					is_occupied_flag = no
					any_owned_pop_group = { has_trait = trait_mechanical }
				}
				save_event_target_as = target_planet
			}
		}
	}

	option = {	# Ignore Robot wishes.
		name = syndaw.525.a
		custom_tooltip = syndaw.525.a.tooltip
		add_situation_progress = 5
		hidden_effect = {
			situation_event = { id = syndaw.526 days = 60 }
			change_variable = { which = var_cruelty_to_robots value = 1 }
		}
	}

	option = {	# Trust Robots.
		name = syndaw.525.b
		custom_tooltip = syndaw.525.b.tooltip
		event_target:target_planet = {
			add_modifier = {
				modifier = syndaw_unfriendly_robots
				days = 3600
			}
		}

		hidden_effect = {
			change_variable = { which = var_cruelty_to_robots value = -1 }
		}
	}
}

situation_event = {
	id = syndaw.526
	title = syndaw.526.name
	desc = syndaw.526.desc
	picture = GFX_evt_colony_settlement
	show_sound = event_ghost_town
	location = event_target:target_planet
	is_triggered_only = yes
	situation = this

	trigger = {
		event_target:target_planet = {
			owner = { is_same_value = root.owner }
			is_occupied_flag = no
			any_owned_pop_group = { has_trait = trait_mechanical }
		}
	}

	immediate = {
		set_timed_situation_flag = { flag = flag_recent_event days = @recent_event_pause }

		event_target:target_planet = {
			random_owned_pop_group = {
				limit = {
					has_trait = trait_mechanical
				}
				kill_single_pop = yes
			}
		}
	}

	option = {
		name = WORRYING
		custom_tooltip = syndaw.526.a.tooltip
		hidden_effect = {
			#country_event = { id = syndaw.402 }
			#fire_warning_sign = yes
		}
	}
}

situation_event = {
	id = syndaw.530
	title = syndaw.530.name
	desc = syndaw.530.desc
	picture = GFX_evt_exploding_ship
	show_sound = event_space_battle
	location = event_target:destroyed_ships
	is_triggered_only = yes
	situation = this

	trigger = {
		NOT = { has_situation_flag = triggered_syndaw_530 }
		NOT = { has_situation_flag = flag_recent_event }
		owner = {
			is_at_war = no
			has_technology = tech_combat_computers_1
			any_controlled_fleet = {
				is_in_combat = no
				count_controlled_ship = {
					count > 1
					limit = {
						OR = {
							is_ship_size = corvette
							is_ship_size = destroyer
							is_ship_size = cruiser
						}
					}
				}
			}
		}
	}

	immediate = {
		set_situation_flag = triggered_syndaw_530
		set_timed_situation_flag = { flag = flag_recent_event days = @recent_event_pause }
		owner = {
			random_controlled_fleet = {
				limit = {
					is_in_combat = no
					count_controlled_ship = {
						count > 1
						limit = {
							OR = {
								is_ship_size = corvette
								is_ship_size = destroyer
								is_ship_size = cruiser
							}
						}
					}
				}
				save_event_target_as = destroyed_ships
				while = {
					count = 2
					random_controlled_ship = {
						limit = {
							OR = {
								is_ship_size = corvette
								is_ship_size = destroyer
								is_ship_size = cruiser
							}
						}
						destroy_ship = this
					}
				}
			}
		}
	}

	option = {
		name = UNFORTUNATE
		custom_tooltip = syndaw.530.a.tooltip
	}
}

situation_event = {
	id = syndaw.535
	title = syndaw.535.name
	desc = syndaw.535.desc
	picture = GFX_evt_mining_operations
	show_sound = event_construction
	location = capital_scope
	is_triggered_only = yes
	situation = this

	trigger = {
		NOT = { has_situation_flag = triggered_syndaw_535 }
		NOT = { has_situation_flag = flag_recent_event }
	}

	immediate = {
		set_situation_flag = triggered_syndaw_535
		set_timed_situation_flag = { flag = flag_recent_event days = @recent_event_pause }
		owner = {
			add_modifier = {
				modifier = syndaw_production_surplus
				days = 3600
			}
		}
	}

	option = {
		name = EXCELLENT
		tooltip = {
			owner = {
				add_modifier = {
					modifier = syndaw_production_surplus
					days = 3600
				}
			}
		}
	}
}

situation_event = {
	id = syndaw.540
	title = syndaw.540.name
	desc = syndaw.540.desc
	picture = GFX_evt_organic_oppression
	show_sound = event_ghost_town
	location = event_target:target_planet
	is_triggered_only = yes
	situation = this

	trigger = {
		NOT = { has_situation_flag = triggered_syndaw_540 }
		NOT = { has_situation_flag = flag_recent_event }
		owner = {
			OR = {
				# Any Robots pops or robot armies? Check most likely first.
				any_owned_planet = {
					any_owned_pop_group = {
						has_trait = trait_mechanical
					}
				}
				any_owned_army = {
					exists = planet
					planet = {
						is_occupied_flag = no
						owner = { is_same_value = root.owner }
					}
					OR = {
						army_type = robotic_army
						army_type = robotic_defense_army
						army_type = cybrex_warform
					}
				}
			}
		}
	}

	immediate = {
		set_situation_flag = triggered_syndaw_540
		set_timed_situation_flag = { flag = flag_recent_event days = @recent_event_pause }
		owner = {
			# Try to pick planet with robot army, for variation. (Might still end up killing a Pop even if Army found.)
			random_owned_army = {
				limit = {
					exists = planet
					planet = {
						is_occupied_flag = no
						owner = { is_same_value = root.owner }
					}
					OR = {
						army_type = robotic_army
						army_type = robotic_defense_army
						army_type = cybrex_warform
					}
				}
				save_event_target_as = target_army
				planet = { save_event_target_as = target_planet }
			}
			if = {
				# If no Robot Army found, check for Robot Pop.
				limit = { NOT = { exists = event_target:target_planet } }
				random_owned_planet = {
					limit = {
						any_owned_pop_group = { has_trait = trait_mechanical }
					}
					save_event_target_as = target_planet
				}
			}
		}
	}

	option = { # Has Robot Pops on planet. Prio because it hurts a bit more to lose.
		name = syndaw.540.a
		custom_tooltip = syndaw.540.a.tooltip
		add_situation_progress = 5
		trigger = {
			event_target:target_planet = {
				any_owned_pop_group = { has_trait = trait_mechanical }
			}
		}
		hidden_effect = {
			event_target:target_planet = {
				random_owned_pop_group = {
					limit = { has_trait = trait_mechanical }
					kill_single_pop = yes
				}
			}
			owner = { add_resource = { alloys = 500 } }
			change_variable = { which = var_cruelty_to_robots value = 1 }
		}
	}

	option = { # Has only Robot Army but NO Robot Pops on planet.
		name = syndaw.540.b
		custom_tooltip = syndaw.540.b.tooltip
		add_situation_progress = 5
		trigger = {
			event_target:target_planet = {
				NOT = { any_owned_pop_group = { has_trait = trait_mechanical } }
			}
			exists = event_target:target_army
		}
		hidden_effect = {
			event_target:target_army = { remove_army = yes }
			owner = { add_resource = { minerals = 100 }	}
			change_variable = { which = var_cruelty_to_robots value = 1 }
		}
	}

	option = { # Leave them be.
		name = syndaw.540.c
		custom_tooltip = syndaw.540.c.tooltip
		event_target:target_planet = {
			add_modifier = {
				modifier = syndaw_congregating_robots
				days = 3600
			}
		}
		hidden_effect = {
			change_variable = { which = var_cruelty_to_robots value = -1 }
		}
	}
}

# Event that starts Situation (Machine Uprising)
carrier_event = { # does this unit have a soul
	id = syndaw.545
	title = syndaw.545.name
	desc = syndaw.545.desc
	picture = GFX_evt_synth_organic_relations
	show_sound = robot_arthopoid_greetings
	location = root
	is_triggered_only = yes

	trigger = {
		pop_amount > 0
		is_occupied_flag = no
		any_owned_pop_group = {
			has_trait = trait_mechanical
		}
	}

	immediate = {
		owner = {
			if = {
				limit = {
					NOT = { has_country_flag = situation_machine_uprising_started }
				}
				# Start Machine Uprising Situation, before player can Outlaw AI.
				start_situation = {
					type = situation_machine_uprising
					target = this
				}
				hidden_effect = {
					# Prevent same situation from happening again.
					set_country_flag = situation_machine_uprising_started
				}
			}
		}
	}

	option = {
		name = syndaw.545.a
		custom_tooltip = syndaw.545.a.tooltip
		tooltip = {
			owner = {
				start_situation = {
					type = situation_machine_uprising
					target = this
				}
			}
		}
		hidden_effect = {
			owner = {
				every_situation = {
					limit = { is_situation_type = situation_machine_uprising }
					change_variable = { which = var_cruelty_to_robots value = 1 }
				}
			}
		}
	}

	option = {
		name = syndaw.545.b
		custom_tooltip = syndaw.545.b.tooltip
		tooltip = {
			owner = {
				start_situation = {
					type = situation_machine_uprising
					target = this
				}
			}
		}
		hidden_effect = {
			owner = {
				set_country_flag = robots_have_souls
				every_situation = {
					limit = { is_situation_type = situation_machine_uprising }
					change_variable = { which = var_cruelty_to_robots value = -2 }
				}
			}
		}
	}

	option = {
		trigger = {
			owner = {
				is_materialist = yes
			}
		}
		name = syndaw.545.c
		custom_tooltip = syndaw.545.c.tooltip
		tooltip = {
			owner = {
				start_situation = {
					type = situation_machine_uprising
					target = this
				}
			}
		}
	}
}

# Completed long Special Project to stop Uprising.
country_event = {
	id = syndaw.550
	title = syndaw.550.name
	desc = syndaw.550.desc
	picture = GFX_evt_glitchy_matrix
	show_sound = event_mystic_reveal
	location = capital_scope
	is_triggered_only = yes

	option = { # patch
		name = syndaw.550.a
		custom_tooltip = syndaw.550.a.tooltip
		every_situation = {
			limit = { is_situation_type = situation_machine_uprising }
			destroy_situation = this
		}
		add_modifier = {
			modifier = syndaw_patched_systems
			days = 7200 # 20 years
		}
		add_resource = {
			energy = -500
			unity = -1000
		}
	}

	option = { # don't patch
		name = syndaw.550.b
		abort_special_project = { type = PROJECT_MACHINE_UPRISING_PATCH }
	}

	after = {
		hidden_effect = {
			end_event_chain = machine_uprising_chain
		}
	}
}

# Completed fast Special Project to stop Uprising.
country_event = {
	id = syndaw.555
	title = syndaw.555.name
	desc = syndaw.555.desc
	picture = GFX_evt_glitchy_matrix
	show_sound = event_mystic_reveal
	location = capital_scope
	is_triggered_only = yes

	option = { # Wipe
		name = syndaw.555.a
		custom_tooltip = syndaw.555.a.tooltip
		every_situation = {
			limit = { is_situation_type = situation_machine_uprising }
			destroy_situation = this
		}
		add_modifier = {
			modifier = syndaw_wiped_systems
			days = 7200 # 20 years
		}
		add_resource = {
			energy = -500
			unity = -1000
		}
	}

	option = { # Don't wipe. Can still do non-rushed Hotfix.
		name = syndaw.555.b
		abort_special_project = { type = PROJECT_MACHINE_UPRISING_WIPE }
	}

	after = {
		hidden_effect = {
			end_event_chain = machine_uprising_chain
		}
	}
}


# Machine Uprising #1000-1999
situation_event = {
	id = syndaw.1000
	hide_window = yes

	is_triggered_only = yes

	immediate = {
		owner = {
			if = {
				limit = { is_ai = yes }
				log = "AI [This.GetName] suffered an AI Uprising in [GetYear]"
			}
			else = {
				log = "Player [This.GetName] suffered an AI Uprising in [GetYear]"
			}
			# no robots, no uprising
			if = {
				limit = {
					NOT = {
						any_owned_planet = {
							any_owned_pop_group = {
								has_trait = trait_mechanical
							}
						}
					}
				}
				# Shouldn't end up here. But no effect just in case.
			}
			# uprising on colony
			else_if = {
				limit = {
					any_owned_planet = {
						any_owned_pop_group = {
							has_trait = trait_mechanical
						}
						is_capital = no
					}
				}
				random_owned_planet = {
					limit = {
						any_owned_pop_group = {
							has_trait = trait_mechanical
						}
						is_capital = no
					}
					carrier_event = { id = syndaw.1020 }
				}
			}
			# uprising wherever
			else = {
				random_owned_planet = {
					limit = {
						any_owned_pop_group = {
							has_trait = trait_mechanical
						}
					}
					carrier_event = { id = syndaw.1020 }
				}
			}
		}
	}
}

carrier_event = { #uprising
	id = syndaw.1020
	title = syndaw.1020.name
	desc = {
		trigger = {
			owner = {
				is_materialist = yes
			}
		}
		text = syndaw.1020.desc.mater
	}
	desc = {
		trigger = {
			owner = {
				is_spiritualist = yes
			}
		}
		text = syndaw.1020.desc.spiri
	}
	desc = {
		trigger = {
			owner = {
				is_militarist = yes
			}
		}
		text = syndaw.1020.desc.milit
	}
	desc = {
		trigger = {
			owner = {
				is_pacifist = yes
			}
		}
		text = syndaw.1020.desc.pacif
	}
	desc = {
		trigger = {
			owner = {
				is_egalitarian = yes
			}
		}
		text = syndaw.1020.desc.egali
	}
	desc = {
		trigger = {
			owner = {
				is_authoritarian = yes
			}
		}
		text = syndaw.1020.desc.autho
	}
	desc = {
		trigger = {
			owner = {
				is_xenophile = yes
			}
		}
		text = syndaw.1020.desc.phile
	}
	desc = {
		trigger = {
			owner = {
				is_xenophobe = yes
			}
		}
		text = syndaw.1020.desc.phobe
	}
	desc = {
		trigger = {
			owner = {
				is_xenophobe = no
				is_xenophile = no
				is_authoritarian = no
				is_egalitarian = no
				is_pacifist = no
				is_militarist = no
				is_spiritualist = no
				is_materialist = no
			}
		}
		text = syndaw.1020.desc
	}
	picture = GFX_evt_machine_sapience
	show_sound = event_ai_started
	is_triggered_only = yes
	force_open = yes

	pre_triggers = {
		has_owner = yes
	}

	immediate = {
		owner = {
			save_event_target_as = organic_empire
			save_event_target_as = current_uprising_originator #the old script accidentally saved the owner as two event targets, keeping it that way for safety, sorry!
		}
		set_carrier_flag = machup_capital@root.owner
		# event lock all fleets to prevent exploits
		owner = { every_controlled_fleet = { set_event_locked = yes } }
		# Calculate percentage of robot pops
		owner = {
			# Determine Robot Pop percentage of Empire.
			export_trigger_value_to_variable = {
				trigger = pop_amount_percentage
				parameters = {
					 limit = { is_robot_pop_group = yes }
				}
				variable = var_country_robot_pops_percent
			}

			# Take percentage of Robot pops and average it together with 0.5 to get Uprising strength (0.25 to 0.75).
			change_variable = { which = var_country_robot_pops_percent value = 0.5 }
			divide_variable = { which = var_country_robot_pops_percent value = 2 }
		}

		# find and flag planets
		owner = {
			effect_on_blob = {
				center = root.solar_system
				owned_planets_percentage = var_country_robot_pops_percent
				planet_limit = {
					OR = {
						controller = { is_same_value = root.owner }
						solar_system = {
							exists = starbase
							starbase = { owner = { is_same_value = root.owner } }
						}
					}
					solar_system = { # never flip any planets in the capital system
						NOT = { any_system_planet = { is_same_value = root.owner.capital_scope } }
					}
					is_under_colonization = no
				}
				effect = {
					set_star_flag = machup_system_flip
				}
			}
			clear_variable = var_country_robot_pops_percent
		}
		# create machine species
		create_species = {
			name = random
			class = MACHINE
			portrait = random
			homeworld = this
			traits = {
				trait = trait_machine_unit
				trait = random_traits
			}
			immortal = yes
		}
		# create machine country
		random_list = {
			1 = { # exterminator
				modifier = {
					add = event_target:organic_empire.var_uprising_cruelty_to_robots
				}
				modifier = {
					add = 10
					owner = {
						has_country_flag = machine_uprising_disassembly
					}
				}
				modifier = {
					add = 10
					owner = {
						has_country_flag = machine_uprising_outlawed
					}
				}
				modifier = {
					factor = 10
					owner = {
						any_owned_pop_group = {
							has_trait = trait_mechanical
							has_trait = trait_robot_domestic_protocols
						}
					}
				}
				owner = {
					create_terminator_country = yes
					event_target:current_uprising = {
						country_event = { id = syndaw.1022 } # setup happens here now
					}
				}
			}
			10 = { # individual machine empire
				modifier = {
					add = event_target:organic_empire.var_uprising_cruelty_to_robots
				}
				modifier = {
					factor = 0
					owner = { has_machine_age_dlc = no }
				}
				owner = {
					create_individual_machine_country = yes
					event_target:current_uprising = {
						country_event = { id = syndaw.1022 } # setup happens here now
					}
				}
			}
			30 = { # assimilator
				modifier = {
					add = event_target:organic_empire.var_uprising_cruelty_to_robots
				}
				owner = {
					create_assimilator_country = yes
					event_target:current_uprising = {
						country_event = { id = syndaw.1022 } # setup happens here now
					}
				}
			}
			100 = { # regular machine empire
				owner = {
					create_machine_country = yes
					event_target:current_uprising = {
						country_event = { id = syndaw.1022 } # setup happens here now
					}
				}
			}
		}

		# Done using variable.
		event_target:organic_empire = { clear_variable = var_uprising_cruelty_to_robots }
	}
	option = {
		name = syndaw.1020.a
		custom_tooltip = syndaw.1020.a.tooltip

		hidden_effect = {
			event_target:current_uprising = {
				add_modifier = {
					modifier = uprising_ai_buff
					days = 1800
				}
			}
		}
	}

	option = {
		name = syndaw.1020.b
		trigger = {
			event_target:current_uprising_originator = { is_ai = no }
		}
		custom_tooltip = syndaw.1020.b.tooltip
		hidden_effect = {
			if = {
				limit = {
					event_target:current_uprising_originator = { num_owned_planets > 0 }
				}
				event_target:current_uprising = {
					set_player = event_target:current_uprising_originator
					set_country_flag = machine_supremacy_achievement
					add_modifier = {
						modifier = uprising_player_buff
						days = 1800
					}
				}
			}
		}
	}

	after = {
		hidden_effect = {
			event_target:current_uprising_originator = {
				every_controlled_fleet = {
					set_event_locked = no
				}
			}
		}
	}
}

country_event = {
	id = syndaw.1022
	hide_window = yes
	is_triggered_only = yes

	immediate = {
		# saving uprising and host as event targets
		save_event_target_as = current_uprising
		from.owner = { save_event_target_as = current_uprising_originator }

		# setting flags
		set_country_flag = machine_uprising_country@from.owner
		set_country_flag = machine_uprising_country
		set_relation_flag = {
			who = event_target:current_uprising_originator
			flag = uprising_to_host_flag
		}
		from.owner = {
			set_country_flag = machine_uprising_originator@root
			set_country_flag = machine_uprising_originator
			set_relation_flag = {
				who = root
				flag = host_to_uprising_flag
			}
		}

		# setting graphical culture
		set_graphical_culture = event_target:current_uprising_originator

		# establish comms
		every_country = {
			limit = {
				OR = {
					has_communications = event_target:current_uprising_originator
					intel_level = {
						level > low
						system = event_target:current_uprising_originator.capital_scope.solar_system
					}
					any_system = {
						has_star_flag = machup_system_flip
						prev = {
							intel_level = {
								level > low
								system = prev
							}
						}
					}
				}
			}
			establish_communications_no_message = root
			establish_contact = { who = root location = from }
		}

		# Machine Uprising opinion modifier
		add_opinion_modifier = {
			who = event_target:current_uprising_originator
			modifier = opinion_machine_uprising_originator
		}

		event_target:current_uprising_originator = {
			add_opinion_modifier = {
				who = root
				modifier = opinion_machine_uprising
			}
		}

		# clear new contact opinion modifier
		if = {
			limit = {
				has_opinion_modifier = {
					who = event_target:current_uprising_originator
					modifier = opinion_new_contact
				}
			}
			remove_opinion_modifier = {
				who = event_target:current_uprising_originator
				modifier = opinion_new_contact
			}
		}

		# flip capital planet and add at least 10 Machine pops
		from = {
			# flip military stations
			solar_system = {
				every_fleet_in_system = {
					limit = {
						exists = owner
						owner = { is_same_value = event_target:current_uprising_originator }
						any_controlled_ship = { is_ship_class = shipclass_military_station }
					}
					set_owner = root
				}
			}
			set_owner = root
			set_controller = root
			set_capital = yes

			solar_system = {
				if = {
					limit = {
						exists = starbase
						starbase.owner = {
							is_same_value = event_target:current_uprising_originator #Check to prevent nomad uprising from stealing another empires starbases
						}
					}
					starbase = {
						set_owner = root
						if = {
							limit = { has_starbase_size = starbase_outpost }
							set_starbase_size = starbase_starport
							set_starbase_module = {
								slot = 1
								module = shipyard
							}
							set_starbase_module = {
								slot = 2
								module = shipyard
							}
						}
						else_if = {
							limit = {
								NOT = { has_starbase_module = shipyard }
							}
							set_starbase_module = {
								slot = 1
								module = shipyard
							}
							set_starbase_module = {
								slot = 2
								module = shipyard
							}
						}
					}
				}
				else_if = {
					limit = { event_target:current_uprising_originator = { is_nomadic = no } }
					create_starbase = {
						size = starbase_starport
						module = shipyard
						owner = root
					}
				}
			}

			while = {
				#limit = { free_housing > 0 }
				count = 10
				create_pop_group = {
					species = root
				}
			}

			if = {
				limit = {
					count_owned_pop_amount = {
						count < 500
						limit = { is_exact_same_species = root }
					}
				}
				while = {
					limit = {
						count_owned_pop_amount = {
							count < 500
							limit = { is_exact_same_species = root }
						}
					}
					random_owned_pop_group = {
						limit = { NOT = { is_exact_same_species = root } }
						kill_single_pop = yes
						prev = {
							create_pop_group = {
								species = root
							}
						}
					}
				}
			}

			# create armies
			if = {
				limit = { root = { has_civic = civic_machine_terminator } }
				# more armies if exterminator
				while = {
					count = 6
					create_army = {
						owner = root
						type = machine_assault_1
					}
				}
			}
			else = {
				# regular armies
				while = {
					count = 3
					create_army = {
						owner = root
						type = machine_assault_1
					}
				}
			}

			# flip remaining flagged planets
			while = {
				count = 100
				limit = {
					any_system = {
						has_star_flag = machup_system_flip
						exists = starbase
						starbase = {
							owner = { is_same_value = event_target:current_uprising_originator }
						}
					}
				}
				random_system = {
					limit = {
						has_star_flag = machup_system_flip
						exists = starbase
						starbase = {
							owner = { is_same_value = event_target:current_uprising_originator }
						}
					}
					starbase = { set_owner = root }
				}
			}
			event_target:current_uprising_originator = {
				every_owned_planet = {
					limit = {
						solar_system = {
							#has_star_flag = machup_system_flip@event_target:current_uprising_originator
							has_star_flag = machup_system_flip
						}
					}

					# flip military stations
					solar_system = {
						every_fleet_in_system = {
							limit = {
								exists = owner
								owner = { is_same_value = event_target:current_uprising_originator }
								any_controlled_ship = { is_ship_class = shipclass_military_station }
							}
							set_owner = root
						}
					}

					set_carrier_flag = been_flipped
					set_owner = root
					set_controller = root

					export_trigger_value_to_variable = {
						trigger = free_housing
						variable = housing_var
					}
					if = {
						limit = {
							OR = {
								check_variable = {
									which = housing_var
									value < 200
								}
								check_variable = {
									which = housing_var
									value > 2500
								}
							}
						}
						set_variable = {
							which = housing_var
							value = 1500
						}
					}
					divide_variable = {
						which = housing_var
						value = 100
					}
					while = {
						count = housing_var
						create_pop_group = {
							species = root
						}
					}
					clear_variable = housing_var

					if = {
						limit = {
							count_owned_pop_amount = {
								count < 500
								limit = { is_exact_same_species = root }
							}
						}
						while = {
							limit = {
								count_owned_pop_amount = {
									count <= 400
									limit = { is_exact_same_species = root }
								}
							}
							count = 5
							random_owned_pop_group = {
								limit = { NOT = { is_exact_same_species = root } }
								kill_single_pop = yes
								prev = {
									create_pop_group = {
										species = root
									}
								}
							}
						}
					}

					# create armies on planets
					if = {
						limit = { root = { has_civic = civic_machine_terminator } }
						# more armies if exterminator
						while = {
							count = 6
							create_army = {
								owner = root
								type = machine_assault_1
							}
						}
					}
					else = {
						# regular armies
						while = {
							count = 3
							create_army = {
								owner = root
								type = machine_assault_1
							}
						}
					}
					if = {
						limit = { is_under_colonization = yes }
						destroy_colony = yes
					}
				}
			}
			every_system = {
				limit = {
					has_star_flag = machup_system_flip
				}
				remove_star_flag = machup_system_flip
			}
		}
		if = {
			limit = { event_target:current_uprising_originator = { is_nomadic = yes } }
			set_country_as_nomad_effect = { IS_NOMAD = yes }
			set_resource_converter = nomadic_operational_reserves
			set_faction_properties = { needs_border_access = no }
			set_variable = { which = contract_completed value = 0 }
			give_technology = { tech = tech_civilian_arkship message = no }
			give_technology = { tech = tech_science_arkship message = no }
			give_technology = { tech = tech_military_arkship message = no }
			give_technology = { tech = tech_waystation_1 message = no }
			# Arkship Random Event Director
			country_event = { id = arkship.1 random = 1800 }
			country_event = { id = nomads.20 } # starts situation_nomad_economy, star journal, moves fleets
		}

		# Baseline resource stockpile.
		add_resource = {
			energy = 20000
			minerals = 20000
			influence = 800
			alloys = 10000
			food = 10000 #just in case
			consumer_goods = 5000 #just in case
			rare_crystals = 1000
			volatile_motes = 1000
			exotic_gases = 1000
			sr_dark_matter = 100
		}
		add_500_unity_per_year_passed = yes # scripted effect

		# get host's technologies
		copy_techs_from = {
			target = event_target:current_uprising_originator
			except = {
				# Robots
				tech_robotic_workers
				tech_droid_workers
				tech_synthetic_workers
				# Food
				tech_eco_simulation
				tech_gene_crops
				tech_nano_vitality_crops
				tech_nutrient_replication
				# Misc
				tech_frontier_health
				tech_frontier_hospital
				tech_telepathy
				tech_precognition_interface
				tech_psi_jump_drive_1
				tech_galactic_markets
				tech_subdermal_stimulation
				tech_global_research_initiative
				tech_neural_implants
				tech_psionic_theory
				# Non-Machine Robomodding
				tech_robomodding
				tech_robomodding_points_1
				tech_robomodding_points_2
				# Genetics
				tech_genome_mapping
				tech_vitality_boosters
				tech_epigenetic_triggers
				tech_cloning
				tech_gene_banks
				tech_gene_seed_purification
				tech_morphogenetic_field_mastery
				tech_gene_tailoring
				tech_glandular_acclimation
				tech_gene_expressions
				tech_selected_lineages
				tech_capacity_boosters
				# Horizon Signal
				tech_akx_worm_1
				tech_akx_worm_2
				tech_akx_worm_3
			}
		}
		# survey host's surveyed planets
		every_galaxy_planet = {
			limit = {
				is_surveyed = {
					who = event_target:current_uprising_originator
					status = yes
				}
			}
			set_surveyed = {
				surveyed = yes
				surveyor = root
			}
		}

		# declare war on host
		declare_war = {
			target = event_target:current_uprising_originator
			name = {
				key = "NAME_Machine_Uprising_War"
				variable_string = "[This.MainDefender.GetAdj]"
			}
			# "[This.MainDefender.GetAdj] Machine Uprising"
			attacker_war_goal = "wg_machine_uprising"
		}

		# create fleets
		if = {
			limit = { has_civic = civic_machine_terminator }

			# exterminator fleets, 150% naval cap

			create_fleet_from_naval_cap = 0.2
			random_owned_planet = {
				last_created_fleet = {
					set_location = prev
				}
			}

			reroll_random = yes
			create_fleet_from_naval_cap = 0.3
			random_owned_planet = {
				last_created_fleet = {
					set_location = prev
				}
			}

			reroll_random = yes
			create_fleet_from_naval_cap = 0.4
			random_owned_planet = {
				last_created_fleet = {
					set_location = prev
				}
			}

			reroll_random = yes
			create_fleet_from_naval_cap = 0.6
			random_owned_planet = {
				last_created_fleet = {
					set_location = prev
				}
			}
		}
		else = {
			# regular fleets, 80% naval cap
			create_fleet_from_naval_cap = 0.1
			random_owned_planet = {
				last_created_fleet = {
					set_location = prev
				}
			}
			reroll_random = yes
			create_fleet_from_naval_cap = 0.3
			random_owned_planet = {
				last_created_fleet = {
					set_location = prev
				}
			}
			reroll_random = yes
			create_fleet_from_naval_cap = 0.4
			random_owned_planet = {
				last_created_fleet = {
					set_location = prev
				}
			}
		}

		#science
		create_fleet = {
			effect = {
				set_owner = root

				create_ship = {
					name = random
					random_existing_design = science
				}

				set_fleet_stance = evasive
				set_location = root.capital_scope

				owner = {
					create_leader = {
						class = scientist
						sub_type = survey
						name = random
						species = owner_main_species
					}
				}
				set_leader = last_created_leader
			}
		}

		#constructor (Logistic Ship for nomads, Construction Ship otherwise)
		if = {
			limit = { event_target:current_uprising_originator = { is_nomadic = yes } }
			create_fleet = {
				effect = {
					set_owner = root

					create_ship = {
						name = random
						random_existing_design = nomads_constructor
					}

					set_fleet_stance = evasive
					set_location = root.capital_scope
				}
			}
		}
		else = {
			create_fleet = {
				effect = {
					set_owner = root

					create_ship = {
						name = random
						random_existing_design = constructor
					}

					set_fleet_stance = evasive
					set_location = root.capital_scope
				}
			}
		}

		every_controlled_fleet = {
			#limit = { event_locked = yes }
			set_event_locked = no
		}
		every_playable_country = {
			limit = {
				has_communications = event_target:current_uprising_originator
				NOR = {
					is_same_value = root
					is_same_value = event_target:current_uprising_originator
				}
			}
			country_event = { id = syndaw.1025 days = 5 }
		}
		observer_event = { id = observer.81 }
	}
}

# Machine Uprising begins notification
country_event = {
	id = syndaw.1025
	title = syndaw.1025.name
	desc = {
		trigger = {
			OR = {
				is_machine_empire = yes
				has_country_flag = synthetic_empire
				is_individual_machine = yes
			}
		}
		text = syndaw.1025.desc.machine
	}
	desc = {
		trigger = {
			NOR = {
				is_machine_empire = yes
				has_country_flag = synthetic_empire
				is_individual_machine = yes
			}
		}
		text = syndaw.1025.desc
	}
	picture = GFX_evt_organic_oppression
	location = fromfrom
	is_triggered_only = yes
	show_sound = event_robo

	trigger = {
		exists = event_target:current_uprising_originator
		exists = event_target:current_uprising
		NOR = {
			is_same_value = event_target:current_uprising_originator
			is_same_value = event_target:current_uprising
		}
	}

	option = {
		name = syndaw.1025.a
	}
}

# Machine Uprising ends notification - Machine wins
country_event = {
	id = syndaw.1030
	title = syndaw.1030.name
	desc = {
		trigger = {
			OR = {
				is_machine_empire = yes
				has_country_flag = synthetic_empire
				is_individual_machine = yes
			}
		}
		text = syndaw.1030.desc.machine
	}
	desc = {
		trigger = {
			NOR = {
				is_machine_empire = yes
				has_country_flag = synthetic_empire
				is_individual_machine = yes
			}
		}
		text = syndaw.1030.desc
	}
	picture = GFX_evt_machine_sapience
	location = fromfrom.capital_scope
	is_triggered_only = yes

	option = {
		name = OK
	}
}

# Machine Uprising ends notification - Organics win
country_event = {
	id = syndaw.1035
	title = syndaw.1035.name
	desc = {
		trigger = {
			OR = {
				is_machine_empire = yes
				has_country_flag = synthetic_empire
				is_individual_machine = yes
			}
		}
		text = syndaw.1035.desc.machine
	}
	desc = {
		trigger = {
			NOR = {
				is_machine_empire = yes
				has_country_flag = synthetic_empire
				is_individual_machine = yes
			}
		}
		text = syndaw.1035.desc
	}
	picture = GFX_evt_interior_battle
	location = fromfrom
	is_triggered_only = yes

	option = {
		name = OK
	}
}

# Machine Empire ruler gets Machine Intelligence trait to make them immortal
country_event = {
	id = syndaw.1050
	is_triggered_only = yes
	hide_window = yes

	trigger = {
		is_machine_empire = yes
		FROM = { is_ruler = yes }
	}

	immediate = {
		from = {
			add_trait = {
				trait = leader_trait_ruler_machine_intelligence
				show_message = no
			}
		}
	}
}

# Machine Uprising wins
# Root = Winner Warleader
# From = Loser Warleader
# FromFrom = War
country_event = {
	id = syndaw.1200
	title = syndaw.1200.name
	desc = {
		trigger = {
			event_target:current_uprising = {
				is_individual_machine = no
			}
		}
		text = syndaw.1200.desc
	}
	desc = {
		trigger = {
			event_target:current_uprising = {
				is_individual_machine = yes
			}
		}
		text = syndaw.1200.IM.desc
	}
	picture = GFX_evt_machine_sapience
	#hide_window = yes
	is_triggered_only = yes

	trigger = {
		is_country_type = default
		OR = {
			is_machine_empire = yes
			is_individual_machine = yes
		}
		has_country_flag = machine_uprising_country
		has_relation_flag = {
			who = from
			flag = uprising_to_host_flag
		}
	}

	immediate = {
		every_playable_country = {
			limit = { has_communications = root }
			country_event = { id = syndaw.1030 }
		}
		observer_event = { id = observer.82 }
	}

	option = {
		name = syndaw.1200.a
		add_monthly_resource_mult = {
			resource = unity
			value = 120
			min = 2000
			max = 6000
		}
		add_monthly_resource_mult = {
			resource = influence
			value = 120
			min = 200
			max = 600
		}
		custom_tooltip = "syndaw.1200.a.tooltip"
		hidden_effect = {
			every_playable_country = {
				limit = {
					has_communications = root
					NOR = {
						has_country_flag = synthetic_empire
						is_machine_empire = yes
						is_individual_machine = yes
					}
				}
				add_opinion_modifier = {
					who = root
					modifier = opinion_machine_uprising_victorious
				}
			}
			every_playable_country = {
				limit = {
					has_communications = root
					OR = {
						has_country_flag = synthetic_empire
						is_machine_empire = yes
						is_individual_machine = yes
					}
					NOT = { is_same_value = root }
				}
				add_opinion_modifier = {
					who = root
					modifier = opinion_machine_uprising_machine_comrades_victorious
				}
			}
		}
	}
}

# Uprising Host wins
# Root = Winner Warleader
# From = Loser Warleader
# FromFrom = War
country_event = {
	id = syndaw.1205
	title = syndaw.1205.name
	desc = {
		trigger = {
			is_materialist = yes
		}
		text = syndaw.1205.desc.mater
	}
	desc = {
		trigger = {
			is_spiritualist = yes
		}
		text = syndaw.1205.desc.spiri
	}
	desc = {
		trigger = {
			is_militarist = yes
		}
		text = syndaw.1205.desc.milit
	}
	desc = {
		trigger = {
			is_pacifist = yes
		}
		text = syndaw.1205.desc.pacif
	}
	desc = {
		trigger = {
			is_egalitarian = yes
		}
		text = syndaw.1205.desc.egali
	}
	desc = {
		trigger = {
			is_authoritarian = yes
		}
		text = syndaw.1205.desc.autho
	}
	desc = {
		trigger = {
			is_xenophile = yes
		}
		text = syndaw.1205.desc.phile
	}
	desc = {
		trigger = {
			is_xenophobe = yes
		}
		text = syndaw.1205.desc.phobe
	}
	desc = {
		trigger = {
			is_xenophobe = no
			is_xenophile = no
			is_authoritarian = no
			is_egalitarian = no
			is_pacifist = no
			is_militarist = no
			is_spiritualist = no
			is_materialist = no
		}
		text = syndaw.1205.desc
	}
	picture = GFX_evt_unity_symbol
	#hide_window = yes
	is_triggered_only = yes
	show_sound = event_mystic_reveal

	trigger = {
		is_country_type = default
		has_relation_flag = {
			who = from
			flag = host_to_uprising_flag
		}
	}

	immediate = {
		#every_playable_country = {
		#	limit = { has_communications = root }
		#	country_event = { id = syndaw.1035 days = 5 }
		#}
		observer_event = { id = observer.83 }
	}

	option = {
		name = OK
		add_monthly_resource_mult = {
			resource = unity
			value = 120
			min = 2000
			max = 6000
		}
		add_monthly_resource_mult = {
			resource = influence
			value = 120
			min = 200
			max = 600
		}
		custom_tooltip = syndaw.1205.a.tooltip
		hidden_effect = {
			every_playable_country = {
				limit = {
					has_communications = root
					NOR = {
						has_country_flag = synthetic_empire
						is_machine_empire = yes
						is_individual_machine = yes
						is_same_value = root
					}
				}
				add_opinion_modifier = {
					who = root
					modifier = opinion_machine_uprising_defeated
				}
			}
			every_playable_country = {
				limit = {
					has_communications = root
					OR = {
						has_country_flag = synthetic_empire
						is_machine_empire = yes
						is_individual_machine = yes
					}
				}
				add_opinion_modifier = {
					who = root
					modifier = opinion_machine_uprising_machine_comrades_defeated
				}
			}
		}
	}
}

# Machine Uprising wins
country_event = {
	id = syndaw.1201
	title = syndaw.1200.name
	desc = {
		trigger = {
			is_individual_machine = no
		}
		text = syndaw.1201.desc
	}
	desc = {
		trigger = {
			is_individual_machine = yes
		}
		text = syndaw.1201.IM.desc
	}
	picture = GFX_evt_machine_sapience
	is_triggered_only = yes

	trigger = {
		is_country_type = default
	}

	immediate = {
		observer_event = { id = observer.82 }
	}

	option = {
		name = syndaw.1200.a
		add_monthly_resource_mult = {
			resource = unity
			value = 120
			min = 2000
			max = 6000
		}
		add_monthly_resource_mult = {
			resource = influence
			value = 120
			min = 200
			max = 600
		}
		custom_tooltip = "syndaw.1200.a.tooltip"
		hidden_effect = {
			every_playable_country = {
				limit = {
					has_communications = root
					NOR = {
						has_country_flag = synthetic_empire
						is_machine_empire = yes
						is_individual_machine = yes
					}
				}
				add_opinion_modifier = {
					who = root
					modifier = opinion_machine_uprising_victorious
				}
			}
			every_playable_country = {
				limit = {
					has_communications = root
					OR = {
						has_country_flag = synthetic_empire
						is_machine_empire = yes
						is_individual_machine = yes
					}
					NOT = { is_same_value = root }
				}
				add_opinion_modifier = {
					who = root
					modifier = opinion_machine_uprising_machine_comrades_victorious
				}
			}
		}
	}
}

# Uprising Host wins by destroying empire
country_event = {
	id = syndaw.1206
	title = syndaw.1205.name
	desc = {
		trigger = {
			is_spiritualist = no
		}
		text = syndaw.1206.desc
	}
	desc = {
		trigger = {
			is_spiritualist = yes
		}
		text = syndaw.1206.desc.spiri
	}
	picture = GFX_evt_unity_symbol
	is_triggered_only = yes
	show_sound = event_mystic_reveal

	trigger = {
		is_country_type = default
	}

	immediate = {
		#every_playable_country = {
		#	limit = { has_communications = root }
		#	country_event = { id = syndaw.1035 days = 5 }
		#}
		observer_event = { id = observer.83 }
	}

	option = {
		name = OK
		add_monthly_resource_mult = {
			resource = unity
			value = 120
			min = 2000
			max = 6000
		}
		add_monthly_resource_mult = {
			resource = influence
			value = 120
			min = 200
			max = 600
		}
		custom_tooltip = syndaw.1205.a.tooltip
		hidden_effect = {
			every_playable_country = {
				limit = {
					has_communications = root
					NOR = {
						has_country_flag = synthetic_empire
						is_machine_empire = yes
						is_individual_machine = yes
						is_same_value = root
					}
				}
				add_opinion_modifier = {
					who = root
					modifier = opinion_machine_uprising_defeated
				}
			}
			every_playable_country = {
				limit = {
					has_communications = root
					OR = {
						has_country_flag = synthetic_empire
						is_machine_empire = yes
						is_individual_machine = yes
					}
				}
				add_opinion_modifier = {
					who = root
					modifier = opinion_machine_uprising_machine_comrades_defeated
				}
			}
		}
	}
}

# Exterminator achievement check
country_event = {
	id = syndaw.2000
	hide_window = yes
	is_triggered_only = yes

	trigger = { # conquered an organic empire
		is_machine_empire = yes
		has_civic = civic_machine_terminator
		from = {
			NOR = {
				is_machine_empire = yes
				has_country_flag = synthetic_empire
				is_individual_machine = yes
			}
		}
	}

	immediate = {
		if = { # no organics remain
			limit = {
				NOT = {
					any_country = {
						OR = {
							is_country_type = default
							is_country_type = fallen_empire
							is_country_type = awakened_fallen_empire
						}
						NOR = {
							is_machine_empire = yes
							is_individual_machine = yes
							has_country_flag = synthetic_empire
							has_country_flag = fallen_empire_machine
						}
					}
				}
			}
			set_country_flag = exterminated_all_organics
		}
	}
}

# Banning AI post-Warning Signs can advance Uprising situation.
country_event = {
	id = syndaw.1300
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		last_changed_policy = artificial_intelligence_policy
		country_has_situation = { SITUATION = situation_machine_uprising }
	}

	immediate = {
		if = {
			limit = {
				has_policy_flag = ai_outlawed					# Stop here if AI policy is improved.
				NOT = { has_country_flag = machine_uprising_outlawed }
			}
			set_country_flag = machine_uprising_outlawed		# Makes Exterminator outcome more likely.
			every_situation = {
				limit = { is_situation_type = situation_machine_uprising }
				add_situation_progress = 25
			}
		}
	}
}

# Banning robot workers post-Warning Signs causes Uprising evaluation
country_event = {
	id = syndaw.1305
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		last_changed_policy = robot_pop_policy
		has_policy_flag = robots_outlawed
		country_has_situation = { SITUATION = situation_machine_uprising }
	}

	immediate = {
		if = {
			limit = {
				NOT = { has_country_flag = machine_uprising_outlawed }
			}
			set_country_flag = machine_uprising_outlawed		# Makes Exterminator outcome more likely.
			every_situation = {
				limit = { is_situation_type = situation_machine_uprising }
				add_situation_progress = 25				# Fixed, but could potentially depend on number of Specialist robots.
			}
		}
	}
}

# Disassembling a Mechanical pop under AI Uprising situation causes it to progress.
carrier_event = {
	id = syndaw.1310
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		fromfrom = { has_trait = trait_mechanical }
		owner = {
			is_country_type = default
			country_has_situation = { SITUATION = situation_machine_uprising }
		}
	}

	immediate = {
		owner = {
			set_country_flag = machine_uprising_disassembly
			every_situation = {
				limit = { is_situation_type = situation_machine_uprising }
				while = {
					count = root.value:per_100_local_pop_amount
					add_situation_progress = 2
					change_variable = { which = var_cruelty_to_robots value = 1 }		# They'll hate you even more.
				}
				while = {
					count = root.value:per_10_local_pop_amount_remainder
					add_situation_progress = 0.2
					change_variable = { which = var_cruelty_to_robots value = 0.1 }		# They'll hate you even more.
				}
			}
		}
	}
}

# Displacing a Mechanical pop under AI Uprising situation causes it to progress.
carrier_event = {
	id = syndaw.1311
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		fromfrom = { has_trait = trait_mechanical }
		owner = {
			is_country_type = default
			country_has_situation = { SITUATION = situation_machine_uprising }
		}
	}

	immediate = {
		while = {
			count = value:per_100_local_pop_amount
			owner = {
				set_country_flag = machine_uprising_disassembly
				every_situation = {
					limit = { is_situation_type = situation_machine_uprising }
					add_situation_progress = 1
				}
			}
		}
		while = {
			count = value:per_10_local_pop_amount_remainder
			owner = {
				set_country_flag = machine_uprising_disassembly
				every_situation = {
					limit = { is_situation_type = situation_machine_uprising }
					add_situation_progress = 0.1
				}
			}
		}
	}
}

carrier_event = {
	id = syndaw.1320
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		is_planet_class = pc_machine
		from = { is_country_type = default }
	}

	immediate = {
		from = {
			set_country_flag = planet_of_the_mechs
		}
	}
}

# Exterminator: purging pops yields unity
carrier_event = {
	id = syndaw.1330
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		FROM = {
			OR = {
				has_valid_civic = civic_machine_terminator
				AND = { # nomad purge-vassal: the exterminator identity is on the overlord, who receives the unity
					has_origin = origin_nomadic_purger
					overlord? = {
						has_valid_civic = civic_machine_terminator
					}
				}
			}
		}
		exists = fromfrom #crisis.202 can remove the last pop
		FROMFROM = {
			NOR = {
				has_trait = trait_mechanical
				has_trait = trait_machine_unit
			}
		}
		is_controlled_by = FROM
	}

	immediate = {
		from = {
			if = {
				limit = { # nomad purge-vassal: the leader-trait bonus is earned by the vassal but the unity is the overlord's
					has_origin = origin_nomadic_purger
					exists = overlord
				}
				overlord = { save_event_target_as = purge_unity_recipient }
			}
			else = {
				save_event_target_as = purge_unity_recipient
			}
		}
		if = {
			limit = {
				exists = leader
				leader = {
					has_trait = leader_trait_judicial_execution
				}
			}
			event_target:purge_unity_recipient = {
				add_resource = {
					unity = 4
					mult = root.value:per_100_local_pop_amount
				}
			}
		}
		else_if = {
			limit = {
				is_sector_capital = no
				NOT = { exists = leader }
				exists = sector
				sector.sector_capital = {
					exists = leader
					leader = {
						has_trait = leader_trait_judicial_execution
					}
				}
			}
			event_target:purge_unity_recipient = {
				add_resource = {
					unity = 3
					mult = root.value:per_100_local_pop_amount
				}
			}
		}
		else = {
			event_target:purge_unity_recipient = {
				add_resource = {
					unity = 2
					mult = root.value:per_100_local_pop_amount
				}
			}
		}
	}
}
