
situation_planetary_revolt = { # Referenced in code - Do not change key
	picture = GFX_evt_alien_propaganda
	category = negative
	situation_log_category = urgent_matters

	desc = {
		trigger = {
			owner = { is_gestalt = no }
			NOT = { has_situation_flag = slave_revolt }
		}
		text = situation_planetary_revolt_desc_standard
	}
	desc = {
		trigger = {
			owner = { is_gestalt = no }
			has_situation_flag = slave_revolt
		}
		text = situation_planetary_revolt_desc_slave
	}
	desc = {
		trigger = {
			owner = { is_gestalt = yes }
			target = {
				any_owned_pop_group = {
					is_sapient = yes
					NOT = { has_ethic = ethic_gestalt_consciousness }
				}
			}
		}
		text = situation_planetary_revolt_desc_gestalt_standard
	}
	desc = {
		trigger = {
			owner = {
				is_gestalt = yes
				is_wilderness_empire = no
			}
			target = {
				count_owned_pop_amount = {
					limit = {
						is_sapient = yes
						NOT = { has_ethic = ethic_gestalt_consciousness }
					}
					count = 0
				}
			}
		}
		text = situation_planetary_revolt_desc_gestalt_breakaway_gestalt
	}
	desc = {
		trigger = {
			owner = { is_wilderness_empire = yes }
			target = {
				count_owned_pop_amount = {
					limit = {
						is_sapient = yes
						NOT = { has_ethic = ethic_gestalt_consciousness }
					}
					count = 0
				}
			}
		}
		text = situation_planetary_revolt_desc_gestalt_breakaway_wilderness
	}

	fail_icon = GFX_situation_outcome_meh
	fail_icon_frame = GFX_situation_outcome_frame_green
	complete_icon = GFX_situation_outcome_revolt
	complete_icon_frame = GFX_situation_outcome_frame_red

	on_start = {
		if = {
			limit = { years_passed < 3 }
			log_error = "Isn't it a bit early to have a revolt? Owner: [Owner.GetName] (Origin: [Owner.GetOriginName]), Target: [Target.GetName]."
		}
	}

	on_progress_complete = {
		target = {
			random_owned_species = {
				limit = { has_species_flag = rebellion_species@root }
				save_event_target_as = rebellion_species
			}
		}
		if = {
			limit = { NOT = { exists = event_target:rebellion_species } }
			deduce_planetary_separatism_species = yes
		}
		random_country = {
			limit = {
				has_country_flag = revolt_joiner_empire_@root
				separatism_joining_eligibility_trigger = {
					SPECIES = event_target:rebellion_species
					SITUATION = root
				}
			}
			custom_tooltip = situation_planetary_revolt_complete_joiner_tooltip
			save_event_target_as = new_owner_empire
		}
		if = {
			limit = { NOT = { exists = event_target:new_owner_empire } }
			custom_tooltip = situation_planetary_revolt_complete_tooltip
		}
		owner = {
			if = {
				limit = {
					any_owned_planet = {
						has_modifier = instability_join_revolt
						planet_stability < @[ stabilitylevel2 + 10 ]
						exists = controller
						controller = { is_same_value = root.owner }
						has_ground_combat = no
						has_orbital_bombardment = no
					}
				}
				custom_tooltip = planets_joining_revolt_tooltip
				owned_planet_list_tooltip = {
					limit = {
						has_modifier = instability_join_revolt
						planet_stability < @[ stabilitylevel2 + 10 ]
						exists = controller
						controller = { is_same_value = root.owner }
						has_ground_combat = no
						has_orbital_bombardment = no
					}
				}
			}
		}
		hidden_effect = {
			target = {
				set_timed_carrier_flag = {
					flag = recent_revolt_attempt
					days = 5400
				}
			}
			owner = {
				# Timeline Rebellion
				country_event = {
					id = timeline.7
					scopes = {
						from = from.target
					}
				}
				every_owned_planet = {
					limit = { has_modifier = instability_join_revolt }
					set_timed_carrier_flag = {
						flag = recent_revolt_attempt
						days = 5400
					}
				}
			}
			situation_event = { id = unrest.4200 }
		}
	}

	on_fail = {
		custom_tooltip = situation_planetary_revolt_fail_tooltip
		hidden_effect = {
			target = {
				if = {
					limit = {
						root = { has_situation_flag = slave_revolt }
					}
					carrier_event = { id = unrest.161 }
				}
				else = {
					carrier_event = { id = unrest.4300 }
				}
				set_timed_carrier_flag = {
					flag = recent_revolt_attempt
					days = 5400
				}
			}
			owner = {
				every_owned_planet = {
					limit = { has_modifier = instability_join_revolt }
					set_timed_carrier_flag = {
						flag = recent_revolt_attempt
						days = 5400
					}
				}
			}
			cleanup_separatism_situation = yes
			destroy_situation = this
		}
	}

	on_abort = {
		cleanup_separatism_situation = yes
	}

	abort_trigger = {
		NOT = {
			owner = { #otherwise, that event flips ownership, which immediately invalidates the Situation
				has_active_event = { unrest.4200 }
			}
		}
		OR = {
			NOT = { exists = target.owner }
			target.owner = { NOT = { is_same_value = root.owner } }
			target.controller = { NOT = { is_same_value = root.owner } }
		}
	}

	initial_progress = 0

	stages = {
		revolt_severity_1 = {
			icon = GFX_situation_stage_1
			icon_background = GFX_situation_stage_frame_red
			target_modifier = { planet_stability_add = -10 }
			end = 25
		}
		revolt_severity_2 = {
			icon = GFX_situation_stage_2
			icon_background = GFX_situation_stage_frame_red
			on_first_enter = {
				if = {
					limit = { has_situation_flag = slave_revolt }
					target = {
						#Sabotage
						carrier_event = { id = unrest.156 }
					}
				}
				else = {
					#Characteristics of the revolt (ethics)
					situation_event = { id = unrest.100 }
				}
			}
			target_modifier = { planet_stability_add = -10 }
			end = 50
		}
		revolt_severity_3 = {
			icon = GFX_situation_stage_3
			icon_background = GFX_situation_stage_frame_red
			on_first_enter = {
				#Characteristics of the revolt (species, other planets joining)
				situation_event = { id = unrest.4000 }
			}
			target_modifier = { planet_stability_add = -10 }
			end = 75
		}
		revolt_severity_4 = {
			icon = GFX_situation_stage_4
			icon_background = GFX_situation_stage_frame_red
			on_first_enter = {
				#Rebels will ask any other empires for support. If someone can be found, the player is notified.
				situation_event = { id = unrest.4050 }
			}
			target_modifier = { planet_stability_add = -10 }
			end = 100
		}
	}

	approach = {
		name = approach_revolt_do_nothing
		icon = GFX_situation_approach_this_is_fine
		icon_background = GFX_situation_approach_bg_yellow
		default = yes
		on_select = {
			custom_tooltip = deficit_approach_do_nothing_tooltip
		}
		ai_weight = {
			base = 2
		}
	}

	approach = {
		name = approach_revolt_distribute_amenities
		icon = GFX_situation_approach_amenities
		icon_background = GFX_situation_approach_bg_green
		triggered_target_modifier = {
			potential = {
				owner = { is_gestalt = no }
			}
			modifier = {
				planet_amenities_add = 2000
			}
		}
		triggered_target_modifier = {
			potential = {
				owner = { is_gestalt = yes }
			}
			modifier = {
				planet_amenities_no_happiness_add = 2000
			}
		}
		resources = {
			category = situations
			upkeep = {
				trigger = {
					owner = { is_gestalt = no }
				}
				consumer_goods = 20
			}
			upkeep = {
				trigger = {
					owner = { is_gestalt = yes }
				}
				unity = 30
			}
		}
		ai_weight = {
			base = 2
			modifier = {
				factor = 2
				OR = {
					current_situation_approach = approach_revolt_distribute_amenities
					target = { free_amenities < -5 }
				}
			}
		}
	}

	approach = {
		name = approach_revolt_crackdown
		icon = GFX_situation_approach_whip
		icon_background = GFX_situation_approach_bg_red
		target_modifier = {
			job_soldier_stability_add = 1.5
			planet_army_build_speed_mult = 0.5
			planet_building_stronghold_build_speed_mult = 0.5
			planet_soldiers_trade_upkeep_add = 1
		}
		resources = {
			category = situations
			upkeep = {
				unity = 30
			}
		}
		ai_weight = {
			base = 1
			modifier = {
				factor = 3
				owner = {
					OR = {
						is_militarist = yes
						is_authoritarian = yes
						is_machine_empire = yes
					}
				}
			}
			modifier = {
				factor = 3
				target = {
					OR = {
						num_assigned_jobs = {
							job = warrior_drone
							value > 300
						}
						num_assigned_jobs = {
							job = soldier
							value > 300
						}
					}
				}
			}
		}
	}

	on_monthly = {
		events = {
			unrest.163
		}
		random_events = {
			100 = 0
			3 = unrest.101
			3 = unrest.105
			3 = unrest.110
			3 = unrest.157
			3 = unrest.159
		}
	}

	#Note: if you alter this, you should consider whether to alter potential_revolt_situation_monthly_progress_estimation too
	monthly_progress = {
		base = 0
		modifier = {
			add = target.value:revolt_situation_low_stability_factor #0.2 for every point below 25 (so max 5)
			desc = STRING_LOW_STABILITY
			target = { planet_stability < @stabilitylevel2 }
		}
		modifier = {
			add = 1
			desc = STRING_UNEMPLOYMENT
			target = { num_unemployed >= 200 }
		}
		modifier = {
			add = 2
			desc = STRING_PURGING
			target = {
				has_current_purge = yes
				any_owned_pop_group = {
					is_being_purged = yes
					NOT = {
						has_purge_type = { type = purge_neutering }
					}
				}
			}
		}
		modifier = {
			add = 1
			desc = STRING_PURGING
			target = {
				has_current_purge = yes
				count_owned_species = {
					limit = {
						OR = {
							has_citizenship_type = { type = citizenship_purge country = root.owner }
							has_citizenship_type = { type = citizenship_purge_machine country = root.owner }
						}
						NOT = { has_purge_type = { type = purge_neutering country = root.owner } }
					}
					count = 0
				}
			}
		}
		modifier = {
			add = 1
			desc = STRING_ASSIMILATION
			target = {
				any_owned_species = {
					has_citizenship_type = { type = citizenship_assimilation country = root.owner }
				}
			}
		}
		modifier = {
			subtract = target.value:revolt_situation_high_stability_factor #0.5 for every point above 40
			desc = STRING_HIGH_STABILITY
			target = { planet_stability > @stabilitylevel3 }
		}
		modifier = {
			desc = STRING_STARBASE_CITADEL
			subtract = 1
			exists = target.solar_system.starbase
			target.solar_system.starbase = {
				has_starbase_size >= starbase_citadel
				NOT = { is_starbase_type = sfortress }
			}
		}
		modifier = {
			desc = STRING_STARBASE_BASTION
			subtract = 1
			exists = target.solar_system.starbase
			target.solar_system.starbase = {
				has_starbase_size > starbase_starport
				has_starbase_size < starbase_citadel
				is_starbase_type = sfortress
			}
		}
		modifier = {
			desc = STRING_STARBASE_BASTION
			subtract = 2
			exists = target.solar_system.starbase
			target.solar_system.starbase = {
				has_starbase_size >= starbase_citadel
				is_starbase_type = sfortress
			}
		}
		complex_trigger_modifier = {
			trigger = count_system_planet_colony
			trigger_scope = target.solar_system
			parameters = {
				limit = {
					NOT = { is_same_value = root.target }
					planet_stability >= @stabilitylevel3
					pop_amount > 500
				}
			}
			potential = {
				target.solar_system = {
					any_system_planet_colony = {
						NOT = { is_same_value = root.target }
						planet_stability >= @stabilitylevel3
						pop_amount > 500
					}
				}
			}
			desc = STRING_LOYAL_PLANETS_IN_SYSTEM
			mode = subtract
			mult = 2
		}
		complex_trigger_modifier = {
			trigger = count_system_planet_colony
			trigger_scope = target.solar_system
			parameters = {
				limit = {
					NOT = { is_same_value = root.target }
					planet_stability < @stabilitylevel2
					pop_amount > 500
				}
			}
			potential = {
				target.solar_system = {
					any_system_planet_colony = {
						NOT = { is_same_value = root.target }
						planet_stability < @stabilitylevel2
						pop_amount > 500
					}
				}
			}
			desc = STRING_REBELLIOUS_PLANETS_IN_SYSTEM
			mode = add
			mult = 2
		}
		modifier = {
			subtract = target.value:revolt_situation_garrison_factor #1 for every 200 over 200, max 5 (at 1200)
			desc = STRING_PLANETARY_GARRISON
			target = { planet_garrison_strength > 200 }
		}
		modifier = {
			subtract = 1
			desc = STRING_WEAK_PLANET
			target = { pop_amount < 3000 }
			NOT = {
				owner = {
					any_owned_planet = {
						has_modifier = instability_join_revolt
					}
				}
			}
		}
		modifier = {
			add = 1
			desc = STRING_STRONG_PLANET
			target = { pop_amount > 6000 }
		}
		modifier = {
			desc = STRING_REBELLIOUS_JOINER_PLANETS_IN_OWNER
			add = value:revolt_situation_rebellion_joiners
			owner = {
				any_owned_planet = {
					has_modifier = instability_join_revolt
					planet_stability < @[ stabilitylevel2 + 10 ]
					exists = controller
					controller = { is_same_value = root.owner }
				}
			}
		}
		modifier = {
			desc = STRING_OUTSIDE_SUPPORT
			add = 2
			any_country = { has_country_flag = revolt_joiner_empire_@root }
		}
		modifier = {
			desc = STRING_FOREIGN_OCCUPATION
			subtract = 20
			owner = { is_nomadic = no }
			target.solar_system = {
				exists = starbase
				starbase = {
					NOT = { is_owned_by = root.owner }
				}
			}
		}
		modifier = {
			desc = civilian_difficulty
			mult = 0.1
			situation_monthly_progress > 0
			is_difficulty = 0
			owner = {
				is_ai = no
			}
		}
		modifier = {
			desc = cadet_difficulty
			mult = 0.5
			situation_monthly_progress > 0
			is_difficulty = 1
			owner = {
				is_ai = no
			}
		}
	}
}

situation_machine_uprising = {		# To start, trigger event syndaw.545
	picture = GFX_evt_robot_assembly_plant
	fail_icon = GFX_situation_outcome_meh
	fail_icon_frame = GFX_situation_outcome_frame_green
	complete_icon = GFX_situation_outcome_revolt
	complete_icon_frame = GFX_situation_outcome_frame_red
	initial_progress = 20
	situation_log_category = urgent_matters

	on_start = {
		# Unlock special projects.
		owner = {
			begin_event_chain = {
				event_chain = "machine_uprising_chain"
				target = this
			}
			enable_special_project = {
				name = "PROJECT_MACHINE_UPRISING_PATCH"
				owner = this
			}
			enable_special_project = {
				name = "PROJECT_MACHINE_UPRISING_WIPE"
				owner = this
			}
		}

		# Did you let the AI run? Needs to check variable to prevent Error if it's unset.
		if = {
			limit = {
				owner = {
					check_variable = {
						which = var_syndaw_promote_ai
						value > 0
					}
				}
			}
			add_situation_progress = owner.var_syndaw_promote_ai
		}

		# Track how cruel the player is to the Robots. Affects if they become Exterminators etc.
		set_variable = { which = var_cruelty_to_robots value = 0 }

		# Block random events for a while to keep their pace decent.
		set_timed_situation_flag = { flag = flag_recent_event days = 45 }
	}
	on_monthly = {
		random_events = {
			# Note: Whenever an event is triggered, other events will be blocked for at least 75 days (@recent_event_pause).
			10 = 0
			1 = syndaw.505
			1 = syndaw.510
			1 = syndaw.515
			1 = syndaw.520
			1 = syndaw.525
			1 = syndaw.530
			1 = syndaw.535
			1 = syndaw.540
		}
	}
	monthly_progress = {
		base = -2
		modifier = {
			desc = situation_machine_uprising_pops_monthly_progress
			add = value:situation_machine_uprising_pops_monthly_progress
			check_variable_arithmetic = { which = value:situation_machine_uprising_pops_monthly_progress value > 0.5 }
		}
		modifier = {
			desc = situation_machine_uprising_violated
			add = 5
			check_variable = { which = var_cruelty_to_robots value > 20 }
		}
		modifier = {
			desc = situation_machine_uprising_ai_rights
			add = -5
			owner = {
				has_policy_flag = ai_full_rights
			}
		}
		modifier = {
			# If in a state where Situation won't finish.
			desc = situation_machine_uprising_evaded
			add = -20
			OR = {
				check_variable_arithmetic = { which = value:situation_machine_uprising_pops_monthly_progress value <= 0.5 }
				owner = {
					num_owned_planets < 3
				}
				AND = {
					# If AI rights, the Pops progress needs to be more than 7 to be a threat.
					check_variable_arithmetic = { which = value:situation_machine_uprising_pops_monthly_progress value < 7.2 }
					owner = {
						has_policy_flag = ai_full_rights
					}
				}
			}
		}
		modifier = {
			desc = civilian_difficulty
			mult = 0.1
			situation_monthly_progress > 0
			is_difficulty = 0
			owner = {
				is_ai = no
			}
		}
		modifier = {
			desc = cadet_difficulty
			mult = 0.5
			situation_monthly_progress > 0
			is_difficulty = 1
			owner = {
				is_ai = no
			}
		}
	}
	on_progress_complete = {
		custom_tooltip = situation_machine_uprising_finish_tooltip
		hidden_effect = {
			owner = {
				# Need to store this in the Country now, since Situation is destroyed.
				set_variable = { which = var_uprising_cruelty_to_robots value = root.var_cruelty_to_robots }
			}
			situation_event = { id = syndaw.1000 }
			owner = { country_cleanup_syndaw_situation = yes }
			destroy_situation = this
		}
	}
	on_fail = {
		custom_tooltip = situation_machine_uprising_fail_tooltip
		hidden_effect = {
			owner = { country_cleanup_syndaw_situation = yes }
			destroy_situation = this
		}
	}
	on_abort = {
		owner = { country_cleanup_syndaw_situation = yes }
	}
	abort_trigger = {
		OR = {
			NOT = { exists = target.owner }
			target.owner = { NOT = { is_same_value = root.owner } }
		}
	}
	stages = {
		stage_1 = {
			icon = GFX_situation_stage_1
			icon_background = GFX_situation_stage_frame_red
			end = 33
		}
		stage_2 = {
			icon = GFX_situation_stage_2
			icon_background = GFX_situation_stage_frame_red
			end = 67
			modifier = {
				pop_bonus_workforce_mult = -0.2
			}
		}
		stage_3 = {
			icon = GFX_situation_stage_3
			icon_background = GFX_situation_stage_frame_red
			end = 100
			modifier = {
				pop_bonus_workforce_mult = -0.5
			}
		}
	}
	approach = {
		name = approach_revolt_do_nothing
		allow = {
			custom_tooltip = {
				fail_text = too_late_to_change_approach
				NOT = { has_situation_flag = path_locked }
			}
		}
		icon = GFX_situation_approach_this_is_fine
		icon_background = GFX_situation_approach_bg_yellow
		default = yes
		on_select = {
			custom_tooltip = deficit_approach_do_nothing_tooltip
		}
	}
	approach = {
		name = approach_outlaw_robots
		icon = GFX_situation_approach_whip
		icon_background = GFX_situation_approach_bg_red
		allow = {
			custom_tooltip = {
				fail_text = already_have_this_policy
				owner = {
					NOT = { has_policy_flag = robots_outlawed }
				}
			}
			owner = {
				can_set_policy = {
					policy = robot_pop_policy
					option = robot_pops_outlawed
				}
			}
			custom_tooltip = {
				fail_text = too_late_to_change_approach
				NOT = { has_situation_flag = path_locked }
			}
		}
		on_select = {
			custom_tooltip = approach_outlaw_robots_tooltip
			hidden_effect = {
				owner = {
					set_policy = {
						policy = robot_pop_policy
						option = robot_pops_outlawed
						cooldown = yes
					}
				}
				set_situation_flag = path_locked
			}
		}
	}
	approach = {
		name = approach_give_robots_rights
		icon = GFX_situation_approach_scales
		icon_background = GFX_situation_approach_bg_green
		allow = {
			custom_tooltip = {
				fail_text = already_have_this_policy
				owner = {
					NOT = { has_policy_flag = ai_full_rights }
				}
			}
			owner = {
				can_set_policy = {
					policy = artificial_intelligence_policy
					option = ai_full_rights
				}
			}
			custom_tooltip = {
				fail_text = too_late_to_change_approach
				NOT = { has_situation_flag = path_locked }
			}
		}
		on_select = {
			custom_tooltip = approach_give_robots_rights_tooltip
			hidden_effect = {
				owner = {
					set_policy = {
						policy = artificial_intelligence_policy
						option = ai_full_rights
						cooldown = yes
					}
				}
				set_situation_flag = path_locked
			}
		}
	}
}

situation_terravore_consume_planet = {
	complete_icon_frame = GFX_situation_outcome_frame_green
	complete_icon = GFX_situation_exploding_planet
	situation_log_category = empire_concerns
	inline_script = "situations/consume_world_planet_pictures"

	stages = {
		stage_1 = {
			icon = GFX_situation_stage_1
			icon_background = GFX_situation_stage_frame_green
			end = 1000
			custom_tooltip = stage_feed_feed_feed
		}
	}

	category = positive
	initial_progress = 0

	on_progress_complete = {
		custom_tooltip = terravore_consume_finish_tooltip
		hidden_effect = {
			situation_event = { id = colony.185 }
		}
	}
	on_monthly = {
		events = {
			colony.190 #Adds blockers and grants reward
		}
	}
	on_abort = {
		target = {
			remove_planet_flag = being_devoured
			remove_modifier = being_devoured_modifier
		}
	}
	approach = {#Devour
		name = approach_devour
		on_select = {
			custom_tooltip = terravore_devour_effect
		}
		default = yes
		icon = GFX_situation_approach_amenities
		icon_background = GFX_situation_approach_bg_green
		ai_weight = {
			base = 10
		}
	}
	approach = {#Restraint
		name = approach_restraint
		on_select = {
			custom_tooltip = terravore_restraint_effect
			owner = {
				add_resource = { unity = -1000 }
			}
			abort_situation = this
		}
		icon = GFX_situation_approach_influence
		icon_background = GFX_situation_approach_bg_yellow
		ai_weight = {
			base = 10
		}
	}
	monthly_progress = {
		base = 0
		modifier = {
			add = value:terravore_progress
			desc = string_terravore_progress
		}
	}

	abort_trigger = {
		OR = {
			NOT = { exists = target.owner }
			target.owner = { NOT = { is_same_value = root.owner } }
		}
	}
}
