###############################
# Mandate Events
# by Maximilian Olbers
###############################

namespace = mandate

@tier1materialreward = 6
@tier1materialmin = 100
@tier1materialmax = 500
@tier2materialreward = 12
@tier2materialmin = 150
@tier2materialmax = 1000
@tier3materialreward = 18
@tier3materialmin = 250
@tier3materialmax = 1500
@tier4materialreward = 24
@tier4materialmin = 350
@tier4materialmax = 2000

@tier1influencereward = 24
@tier1influencemin = 40
@tier1influencemax = 100
@tier2influencereward = 36
@tier2influencemin = 80
@tier2influencemax = 175
@tier3influencereward = 48
@tier3influencemin = 125
@tier3influencemax = 250
@tier4influencereward = 60
@tier4influencemin = 150
@tier4influencemax = 300

@tier1researchreward = 6
@tier1researchmin = 60
@tier1researchmax = 150
@tier2researchreward = 12
@tier2researchmin = 90
@tier2researchmax = 250
@tier3researchreward = 18
@tier3researchmin = 120
@tier3researchmax = 350
@tier4researchreward = 24
@tier4researchmin = 150
@tier4researchmax = 500

#########################
# General events
# id 000
#########################

# Mandate added
country_event = {
	id = mandate.1
	title = mandate.1.title
	desc = mandate.1.desc
	picture = GFX_evt_throne_room
	show_sound = event_default

	is_triggered_only = yes

	# "ruler X elected, check sitch log"

	option = {
		name = OK
	}
}

# Mandate failed/expired
country_event = {
	id = mandate.2
	title = mandate.2.title
	desc = mandate.2.desc
	picture = GFX_evt_galactic_senate
	show_sound = event_default

	is_triggered_only = yes

	trigger = {
		OR = {
			has_event_chain = mandate_separatist_chain
			has_event_chain = mandate_emancipation_chain
			has_event_chain = mandate_emancipation2_chain
			has_event_chain = mandate_orbresearch_chain
			has_event_chain = mandate_miner_chain
			has_event_chain = mandate_shipwright_chain
			has_event_chain = mandate_peacemaker_chain
			has_event_chain = mandate_slaver_chain
			has_event_chain = mandate_xenopurge_chain
			has_event_chain = mandate_nationalist_chain
		}
	}

	immediate = {
		switch = {
			trigger = has_event_chain
			
			mandate_separatist_chain = {
				end_event_chain = mandate_separatist_chain
			}
			
			mandate_emancipation_chain = {
				end_event_chain = mandate_emancipation_chain
			}
			
			mandate_emancipation2_chain = {
				end_event_chain = mandate_emancipation2_chain
			}
			
			mandate_orbresearch_chain = {
				end_event_chain = mandate_orbresearch_chain
			}
			
			mandate_miner_chain = {
				end_event_chain = mandate_miner_chain
			}
			
			mandate_shipwright_chain = {
				end_event_chain = mandate_shipwright_chain
			}
			
			mandate_peacemaker_chain = {
				end_event_chain = mandate_peacemaker_chain
			}
			
			mandate_slaver_chain = {
				end_event_chain = mandate_slaver_chain
			}
			
			mandate_xenopurge_chain = {
				end_event_chain = mandate_xenopurge_chain
				random_owned_planet = {
					limit = { has_planet_flag = purge_planet }
					remove_planet_flag = purge_planet
				}
			}
			
			mandate_nationalist_chain = {
				end_event_chain = mandate_nationalist_chain
			}
		}
	}

	option = {
		name = OK
	}
}

# On leader death
country_event = {
	id = mandate.21
	hide_window = yes

	is_triggered_only = yes

	trigger = {
		from = {
			is_same_value = root.ruler
		}
	}

	immediate = {
		country_event = { id = mandate.2 }
	}
}

# Removing progress to mandate if you disband a related station
country_event = {
	id = mandate.3
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		OR = {
			has_event_chain = mandate_miner_chain
			has_event_chain = mandate_orbresearch_chain
		}
	}

	immediate = {
		if = {
			limit = {
				OR = {
					from = {
						is_ship_size = mining_station
					}
					AND = {
						from = {
							is_ship_size = outpost_station
							orbit = {
								OR = {
									has_resource = {
										type = energy
										amount > 0
									}
									has_resource = {
										type = minerals
										amount > 0
									}
								}
							}
						}
					}
				}
				has_event_chain = mandate_miner_chain
			}
			add_event_chain_counter = { 
				event_chain = mandate_miner_chain 
				counter = mining_stations_built
				amount = -1
			}
		}
		if = {
			limit = {
				OR = {
					from = {
						is_ship_size = research_station
					}
					AND = {
						from = {
							is_ship_size = outpost_station
							orbit = {
								OR = {
									has_resource = {
										type = physics_research
										amount > 0
									}
									has_resource = {
										type = society_research
										amount > 0
									}
									has_resource = {
										type = engineering_research
										amount > 0
									}
								}
							}
						}
					}
				}
				has_event_chain = mandate_orbresearch_chain
			}
			add_event_chain_counter = { 
				event_chain = mandate_orbresearch_chain 
				counter = orbresearch_built
				amount = -1
			}
		} 
	}
}

#########################
# Planet Separatist Events
# id 100
#########################

# Mandate fulfilled, country released
country_event = {
	id = mandate.100
	title = mandate.100.title
	desc = mandate.100.desc
	picture = GFX_evt_throne_room
	show_sound = event_default
	location = event_target:released_planet

	is_triggered_only = yes

	trigger = {
		has_event_chain = mandate_separatist_chain
	}

	immediate = {
		end_event_chain = mandate_separatist_chain
		from = {
			remove_pop_faction_flag = mandated_release
		}
	}

	option = {
		name = OK
		add_monthly_resource_mult = {
			resource = influence
			value = @tier3influencereward
			min = @tier3influencemin
			max = @tier3influencemax
		}
	}
}


#########################
# Miner events
# id 110
#########################
# This = construction ship
# From = planet it is built on
# Increase Mandated Mining Station counter when built
ship_event = {
	id = mandate.110
	hide_window = yes

	is_triggered_only = yes

	trigger = {
		owner = {
			has_event_chain = mandate_miner_chain
		}
	}

	immediate = {
		owner = {
			if = {
				limit = {
					from = { #Check if you built an orbital station and if it doesn't qualify to mine the right resources
						has_mining_station = no
						NOT = {
							has_resource = {
								type = energy
								amount > 0
							}
							has_resource = {
								type = minerals
								amount > 0
							}
						}
					}
				}
				###DO NOTHING
				else = {
					add_event_chain_counter = { 
						event_chain = mandate_miner_chain 
						counter = mining_stations_built
						amount = 1
					}
					if = {
						limit = {
							has_completed_event_chain_counter = {
								event_chain = mandate_miner_chain
								counter = mining_stations_built
							}
						}
						country_event = { id = mandate.111 }
					}
				}
			}
		}
	}
}

# Mandate fulfilled
country_event = {
	id = mandate.111
	title = mandate.111.title
	desc = mandate.111.desc
	picture = GFX_evt_mining_station
	show_sound = event_default

	is_triggered_only = yes

	immediate = {
		end_event_chain = mandate_miner_chain
	}

	option = {
		name = OK
		add_monthly_resource_mult = {
			resource = influence
			value = @tier2influencereward
			min = @tier2influencemin
			max = @tier2influencemax
		}
	}
}

############################
# Orbital Researcher events
# id 120
############################
# Increase Mandated Research Station counter when built
ship_event = {
	id = mandate.120
	hide_window = yes

	is_triggered_only = yes

	trigger = {
		owner = {
			has_event_chain = mandate_orbresearch_chain
		}
	}

	immediate = {
		owner = {
			if = {
				limit = {
					from = { #Check if you built an orbital station and if it doesn't qualify to mine the right resources
						has_research_station = no
						NOT = {
							has_resource = {
								type = physics_research
								amount > 0
							}
							has_resource = {
								type = society_research
								amount > 0
							}
							has_resource = {
								type = engineering_research
								amount > 0
							}
						}
					}
				}
				###DO NOTHING
				else = {
					add_event_chain_counter = { 
						event_chain = mandate_orbresearch_chain 
						counter = orbresearch_built
						amount = 1
					}
					if = {
						limit = {
							has_completed_event_chain_counter = {
								event_chain = mandate_orbresearch_chain
								counter = orbresearch_built
							}
						}
						country_event = { id = mandate.121 }
					}
				}
			}
		}
	}
}

# Mandate fulfilled
country_event = {
	id = mandate.121
	title = mandate.121.title
	desc = mandate.121.desc
	picture = GFX_evt_satellite_in_orbit
	show_sound = event_default

	is_triggered_only = yes

	immediate = {
		end_event_chain = mandate_orbresearch_chain
	}

	option = {
		name = OK
		add_monthly_resource_mult = {
			resource = influence
			value = @tier2influencereward
			min = @tier2influencemin
			max = @tier2influencemax
		}
	}
}

############################
# Xenopurger events - CURRENTLY UNUSED
# id 130
############################
# Determine purge target
#country_event = {
#	id = mandate.130
#	hide_window = yes
#
#	is_triggered_only = yes
#
#	immediate = {
#		random_owned_planet = {
#			limit = {
#				any_pop = {
#					NOT = { is_same_species = owner_main_species }
#				}
#			}
#			save_event_target_as = purge_planet
#			set_planet_flag = purge_planet
#		}
#		begin_event_chain = {
#			event_chain = "mandate_xenopurge_chain"
#			target = this
#		}
#	}
#}

# # Check when pop purged
# # This = Planet scope
# # From = Country
# planet_event = {
	# id = mandate.131
	# hide_window = yes

	# is_triggered_only = yes

	# trigger = {
		# owner = {
			# has_event_chain = mandate_xenopurge_chain
		# }
		# has_planet_flag = purge_planet
	# }

	# immediate = {
		# if = {
			# limit = {
				# any_pop = {
					# NOT = { is_same_species = owner_main_species }
				# }
			# }
			# break = yes
		# }
		# owner = {
			# add_event_chain_counter = {
				# event_chain = mandate_xenopurge_chain
				# counter = world_purged
				# amount = 1
			# }
			# country_event = { id = mandate.132 }
		# }
	# }
# }

# # Mandate fulfilled
# country_event = {
	# id = mandate.132
	# title = mandate.132.title
	# desc = mandate.132.desc
	# picture = GFX_evt_burning_settlement
	# show_sound = event_default
	# location = from

	# is_triggered_only = yes

	# immediate = {
		# end_event_chain = mandate_xenopurge_chain
		# from = { remove_planet_flag = purge_planet }
	# }

	# option = {
		# name = OK
		# add_monthly_resource_mult = {
			# resource = influence
			# value = @tier2influencereward
			# min = @tier2influencemin
			# max = @tier2influencemax
		# }
		# hidden_effect = {
			# every_owned_pop = {
				# add_modifier = {
					# modifier = mandate_fulfilled_mod
					# days = 360
				# }
			# }
		# }
	# }
# }

############################
# Shipwright events
# id 140
############################
country_event = {
	id = mandate.140
	hide_window = yes

	is_triggered_only = yes

	trigger = { has_event_chain = mandate_shipwright_chain }

	immediate = {
		if = {
			limit = {
				used_naval_capacity_percent > 0.749
			}
			end_event_chain = mandate_shipwright_chain
			country_event = { id = mandate.141 }
			break = yes
		}
		end_event_chain = mandate_shipwright_chain
		country_event = { id = mandate.142 }
	}
}

# Succesful check, number of military vessels at set date
country_event = {
	id = mandate.141
	title = mandate.141.title
	desc = mandate.141.desc
	picture = GFX_evt_federation_fleet
	show_sound = event_default

	is_triggered_only = yes

	option = {
		name = OK
		add_monthly_resource_mult = {
			resource = influence
			value = @tier2influencereward
			min = @tier2influencemin
			max = @tier2influencemax
		}
	}
}

# Unsuccesful check, number of military vessels at set date
country_event = {
	id = mandate.142
	title = mandate.142.title
	desc = mandate.142.desc
	picture = GFX_evt_wormhole
	show_sound = event_default

	is_triggered_only = yes

	option = {
		name = OK
	}
}

#Check if the chain completion token should be on or off
country_event = {
	id = mandate.143
	hide_window = yes

	is_triggered_only = yes

	trigger = { has_event_chain = mandate_shipwright_chain }

	immediate = {
		if = {
			limit = {
				used_naval_capacity_percent > 0.75
				NOT = {
					has_completed_event_chain_counter = {
						event_chain = mandate_shipwright_chain
						counter = "military_presence_maintained"
					}
				}
			}
			add_event_chain_counter = {
				event_chain = mandate_shipwright_chain
				counter = military_presence_maintained
				amount = 1
			}
			else = {
				if = {
					limit = {
						has_completed_event_chain_counter = {
							event_chain = mandate_shipwright_chain
							counter = "military_presence_maintained"
						}
					}
					add_event_chain_counter = {
						event_chain = "mandate_shipwright_chain"
						counter = "military_presence_maintained"
						amount = -1
					}
				}
			}
		}
	}
}

ship_event = {
	id = mandate.144
	hide_window = yes

	is_triggered_only = yes

	trigger = { owner = { has_event_chain = mandate_shipwright_chain } }

	immediate = {
		owner = {
			country_event = { id = mandate.143 }
		}
	}
}

############################
# Emancipator events
# id 150
############################
# Emancipator 1 Mandate fulfilled
# on_policy_changed, slavery policy
country_event = {
	id = mandate.150
	title = mandate.150.title
	desc = mandate.150.desc
	picture = GFX_evt_smugglers_in_bar
	show_sound = event_default

	is_triggered_only = yes

	trigger = {
		last_changed_policy = slavery
		NOT = { has_policy_flag = slavery_allowed_all }
		has_event_chain = mandate_emancipation_chain
	}

	immediate = {
		end_event_chain = mandate_emancipation_chain
	}

	option = {
		name = OK
		add_monthly_resource_mult = {
			resource = influence
			value = @tier3influencereward
			min = @tier3influencemin
			max = @tier3influencemax
		}
		hidden_effect = {
			every_owned_pop = {
				add_modifier = {
					modifier = mandate_fulfilled_mod
					days = 360
				}
			}
		}
	}
}

##################################

country_event = {
	id = mandate.154
	hide_window = yes

	is_triggered_only = yes

	immediate = {
		random_owned_planet = {
			limit = { any_pop = { is_enslaved = yes } }
			set_planet_flag = free_planet
			save_event_target_as = free_planet
		}
		begin_event_chain = {
			event_chain = "mandate_emancipation2_chain"
			target = this
		}
	}
}

# Emancipator 2
# Pop is released from slavery
# This = Pop scope
# owner_species = { (species)
# owner = { owner_species = { (empire main species)
# owner = { (empire)
pop_event = {
	id = mandate.155
	hide_window = yes

	is_triggered_only = yes

	trigger = {
		owner = {
			has_event_chain = mandate_emancipation2_chain
		}
		planet = {
			has_planet_flag = free_planet
		}
	}

	immediate = {
		planet = {
			if = {
				limit = {
					any_pop = {
						is_enslaved = yes
					}
				}
				break = yes
			}
			save_event_target_as = free_planet
		}
		owner = {
			add_event_chain_counter = {
				event_chain = mandate_emancipation2_chain
				counter = planet_freed
				amount = 1
			}
			country_event = { id = mandate.156 }
		}
	}
}

country_event = {
	id = mandate.156
	title = mandate.156.title
	desc = mandate.156.desc
	picture = GFX_evt_smugglers_in_bar
	show_sound = event_default

	is_triggered_only = yes

	trigger = {
		has_event_chain = mandate_emancipation2_chain
	}

	immediate = {
		end_event_chain = mandate_emancipation2_chain
	}

	option = {
		name = OK
		add_monthly_resource_mult = {
			resource = influence
			value = @tier3influencereward
			min = @tier3influencemin
			max = @tier3influencemax
		}
		hidden_effect = {
			every_owned_pop = {
				add_modifier = {
					modifier = mandate_fulfilled_mod
					days = 360
				}
			}
		}
	}
}

############################
# Peacemaker events
# id 160
############################
# Check if at peace
country_event = {
	id = mandate.160
	hide_window = yes

	is_triggered_only = yes

	trigger = {
		has_event_chain = mandate_peacemaker_chain
	}

	immediate = {
		if = {
			limit = { is_at_war = yes }
			country_event = { id = mandate.162 }
		}
		if = {
			limit = { is_at_war = no }
			country_event = { id = mandate.161 }
		}
	}
}

# achieved peace
country_event = {
	id = mandate.161
	title = mandate.161.title
	desc = mandate.161.desc
	picture = GFX_evt_throne_room
	show_sound = event_default

	is_triggered_only = yes

	trigger = {
		has_event_chain = mandate_peacemaker_chain
	}

	immediate = {
		end_event_chain = mandate_peacemaker_chain
	}

	option = {
		name = OK
		add_monthly_resource_mult = {
			resource = influence
			value = @tier2influencereward
			min = @tier2influencemin
			max = @tier2influencemax
		}
	}
}

# still at war
country_event = {
	id = mandate.162
	title = mandate.162.title
	desc = mandate.162.desc
	picture = GFX_evt_throne_room
	show_sound = event_default

	is_triggered_only = yes

	trigger = {
		has_event_chain = mandate_peacemaker_chain
	}

	immediate = {
		end_event_chain = mandate_peacemaker_chain
	}

	option = {
		name = OK
	}
}

############################
# Slaver events
# id 170
############################
# Pop enslaved
pop_event = {
	id = mandate.170
	hide_window = yes

	is_triggered_only = yes

	trigger = {
		owner = { has_event_chain = mandate_slaver_chain }
		NOT = { has_modifier = pop_was_enslaved }
	}

	immediate = {
		owner = {
			add_event_chain_counter = { 
				event_chain = mandate_slaver_chain 
				counter = pops_enslaved
				amount = 1 
			}
			if = {
				limit = {
					has_completed_event_chain_counter = {
						event_chain = mandate_slaver_chain
						counter = pops_enslaved
					}
				}
				country_event = { id = mandate.171 }
			}
		}
	}
}

# Mandate fulfilled
country_event = {
	id = mandate.171
	title = mandate.171.title
	desc = mandate.171.desc
	picture = GFX_evt_smugglers_in_bar
	show_sound = event_default

	is_triggered_only = yes

	immediate = {
		end_event_chain = mandate_slaver_chain
	}

	option = {
		name = OK
		add_monthly_resource_mult = {
			resource = influence
			value = @tier2influencereward
			min = @tier2influencemin
			max = @tier2influencemax
		}
	}
}

############################
# Nationalist events
# id 180
############################
# Mandate fulfilled
country_event = {
	id = mandate.180
	title = mandate.180.title
	desc = mandate.180.desc
	picture = GFX_evt_galactic_senate
	show_sound = event_default

	is_triggered_only = yes

	immediate = {
		end_event_chain = mandate_nationalist_chain
	}

	option = {
		name = OK
		add_monthly_resource_mult = {
			resource = influence
			value = @tier3influencereward
			min = @tier3influencemin
			max = @tier3influencemax
		}
	}
}