﻿namespace = ach_maintenance_events

ach_maintenance_events.0001 = { # Clean up of Coronation realm law, will happen 1 tick after you gain the title (due to on_actions)
	type = character_event
	hidden = yes

	trigger  = {
		coronation_trigger = yes
	}

	immediate = {
		if = {
			limit = {
				NOT = {
					has_game_rule = coronation_laws_off
				}
			}
			if = {
				limit = {
					has_variable = crowned_king_var
					NOT = { has_realm_law = crowned_king }
				}
				add_realm_law_skip_effects = crowned_king
			}
			else_if = {
				limit = {
					has_variable = crowned_emperor_var
					NOT = { has_realm_law = crowned_emperor }
				}
				add_realm_law_skip_effects = crowned_emperor
			}
			else_if = {
				limit = {
					NOR = {
						has_variable = crowned_king_var
						has_variable = crowned_emperor_var
					}
					NOT = { has_realm_law = uncrowned }
				}
				add_realm_law_skip_effects = uncrowned
			}
		}
	}
}

ach_maintenance_events.0002 = { # ACH Oath synergy culture rewards
	type = character_event
	hidden = yes

	trigger = {
		has_variable = mend_the_fracture_oath_target_culture
		culture ?= {
			has_cultural_tradition = oath_mend_the_fracture_tradition
		}
		top_liege = root
		var:mend_the_fracture_oath_target_culture = {
			has_cultural_tradition = oath_mend_the_fracture_tradition
			NOT = { this = root.culture }
			any_culture_county = {
				count >= 3
				top_liege = root.top_liege
			}
		}
	}

	immediate = {
		# calculate the chances of a positive thing happening
		set_variable = {
			name = synergy_cultures_chance
			value = 0.1
		}
		set_variable = {
			name = synergy_cultures_amount
			value = 0
		}
		var:mend_the_fracture_oath_target_culture = {
			if = {
				limit = {
					cultural_acceptance = {
						target = root.culture
						value >= 99
					}
				}
				root = {
					change_variable = {
						name = synergy_cultures_chance
						add = 0.4
					}
				}
			}
			else_if = {
				limit = {
					cultural_acceptance = {
						target = root.culture
						value >= 80
					}
				}
				root = {
					change_variable = {
						name = synergy_cultures_chance
						add = 0.3
					}
				}
			}
			else_if = {
				limit = {
					cultural_acceptance = {
						target = root.culture
						value >= 60
					}
				}
				root = {
					change_variable = {
						name = synergy_cultures_chance
						add = 0.2
					}
				}
			}
			else_if = {
				limit = {
					cultural_acceptance = {
						target = root.culture
						value >= 40
					}
				}
				root = {
					change_variable = {
						name = synergy_cultures_chance
						add = 0.1
					}
				}
			}
			if = {
				limit = {
					culture_head ?= {
						opinion = {
							target = root
							value >= 80
						}
					}
				}
				root = {
					change_variable = {
						name = synergy_cultures_chance
						add = 0.3
					}
				}
			}
			else_if = {
				limit = {
					culture_head ?= {
						opinion = {
							target = root
							value >= 40
						}
					}
				}
				root = {
					change_variable = {
						name = synergy_cultures_chance
						add = 0.2
					}
				}
			}
			else_if = {
				limit = {
					culture_head ?= {
						opinion = {
							target = root
							value >= 20
						}
					}
				}
				root = {
					change_variable = {
						name = synergy_cultures_chance
						add = 0.1
					}
				}
			}
		}
		if = {
			limit = {
				exists = var:mend_the_fracture_oath_target_culture.culture_head
			}
			if = {
				limit = {
					opinion = {
						target = var:mend_the_fracture_oath_target_culture.culture_head
						value >= 80
					}
				}
				root = {
					change_variable = {
						name = synergy_cultures_chance
						add = 0.3
					}
				}
			}
			else_if = {
				limit = {
					opinion = {
						target = var:mend_the_fracture_oath_target_culture.culture_head
						value >= 40
					}
				}
				root = {
					change_variable = {
						name = synergy_cultures_chance
						add = 0.2
					}
				}
			}
			else_if = {
				limit = {
					opinion = {
						target = var:mend_the_fracture_oath_target_culture.culture_head
						value >= 20
					}
				}
				root = {
					change_variable = {
						name = synergy_cultures_chance
						add = 0.1
					}
				}
			}
		}
		
		if = {
			limit = {
				prestige_level = 5
			}
			change_variable = {
				name = synergy_cultures_chance
				add = 0.2
			}
		}
		else_if = {
			limit = {
				prestige_level = 4
			}
			change_variable = {
				name = synergy_cultures_chance
				add = 0.15
			}
		}
		else_if = {
			limit = {
				prestige_level = 3
			}
			change_variable = {
				name = synergy_cultures_chance
				add = 0.1
			}
		}
		else_if = {
			limit = {
				prestige_level = 2
			}
			change_variable = {
				name = synergy_cultures_chance
				add = 0.05
			}
		}
		else_if = {
			limit = {
				prestige_level = 1
			}
			change_variable = {
				name = synergy_cultures_chance
				add = 0.02
			}
		}


		if = {
			limit = {
				var:synergy_cultures_chance >= 1
			}
			change_variable = {
				name = synergy_cultures_amount
				add = var:synergy_cultures_chance
				multiply = 2
			}
			set_variable = {
				name = synergy_cultures_chance
				value = 1
			}
		}

		# calculate the amount of a positive things happening
		var:mend_the_fracture_oath_target_culture = {
			every_culture_county = {
				limit = {
					top_liege = root.top_liege
				}
				change_variable = {
					name = synergy_cultures_amount
					add = 1
				}
			}
		}
		change_variable = {
			name = synergy_cultures_amount
			multiply = 0.2
			multiply = var:synergy_cultures_chance
			min = 1
		}

		while = {
			limit = {
				var:synergy_cultures_amount > 0
			}
			random_list = {
				1 = {
					culture ?= {
						random_culture_county = {
							limit = {
								NOT = {
									has_county_modifier = oath_mend_the_fracture_county
								}
								holder = root
							}
							alternative_limit = {
								NOT = {
									has_county_modifier = oath_mend_the_fracture_county
								}
								top_liege = root.top_liege
							}
							alternative_limit = {
								NOT = {
									has_county_modifier = oath_mend_the_fracture_county
								}
							}
							add_county_modifier = {
								modifier = oath_mend_the_fracture_county
								years = 2
							}
							add_to_list = synergy_culture_counties
						}
					}
					var:mend_the_fracture_oath_target_culture = {
						random_culture_county = {
							limit = {
								NOT = {
									has_county_modifier = oath_mend_the_fracture_county
								}
								holder = root
							}
							alternative_limit = {
								NOT = {
									has_county_modifier = oath_mend_the_fracture_county
								}
								top_liege = root.top_liege
							}
							alternative_limit = {
								NOT = {
									has_county_modifier = oath_mend_the_fracture_county
								}
							}
							add_county_modifier = {
								modifier = oath_mend_the_fracture_county
								years = 3
							}
							add_to_list = synergy_culture_counties
						}
					}
					save_scope_as = county_reward
				}
				1 = {
					modifier = {
						NOT = {
							root.culture.culture_head ?= root
						}
						factor = 0
					}
					spawn_army = {
						name = ach_culture_synergy_troops
						men_at_arms = {
							type = accolade_maa_archers
							stacks = 1
						}
						men_at_arms = {
							type = accolade_maa_skirmishers
							stacks = 1
						}
						location = root.capital_province
						uses_supply = no
						inheritable = yes
					}
					var:mend_the_fracture_oath_target_culture = {
						if = {
							limit = {
								NOT = {
									culture_head ?= root
								}
							}
						}
						culture_head ?= {
							spawn_army = {
								name = ach_culture_synergy_troops
								men_at_arms = {
									type = accolade_maa_skirmishers
									stacks = 1
								}
								location = root.capital_province
								uses_supply = no
								inheritable = yes
							}
						}
					}
					save_scope_as = army_reward
				}
				1 = {
					modifier = {
						NOT = {
							root.culture.culture_head ?= root
							var:mend_the_fracture_oath_target_culture.culture_head != root
						}
						factor = 0
					}
					modifier = {
						opinion = {
							target = var:mend_the_fracture_oath_target_culture.culture_head
							value = 100
						}
						factor = 0
					}
					add_opinion = {
						target = var:mend_the_fracture_oath_target_culture.culture_head
						opinion = 20
						modifier = ach_synergy_culture_opinion
					}
					save_scope_as = opinion_reward
				}
			}
			
			change_variable = {
				name = synergy_cultures_amount
				subtract = 1
			}
		}
		
		# inform the player about what happened
		send_interface_message = {
			title = ach_maintenance_events.0002.title
			if = {
				limit = {
					exists = scope:army_reward
				}
				custom_tooltip = ach_maintenance_events.0002.army_reward
			}
			if = {
				limit = {
					exists = scope:opinion_reward
				}
				custom_tooltip = ach_maintenance_events.0002.opinion_reward
			}
			if = {
				limit = {
					exists = scope:county_reward
				}
				every_in_list = {
					list = synergy_culture_counties
					show_as_tooltip = {
						add_county_modifier = {
							modifier = oath_mend_the_fracture_county
							years = 2
						}
					}	
				}
			}
		}

		if = {
			limit = {
				var:mend_the_fracture_oath_target_culture = {
					any_culture_county = {
						holder = {
							culture = var:mend_the_fracture_oath_target_culture
							is_ai = no
						}
					}
				}
			}
			send_interface_message = {
				title = ach_maintenance_events.0002.title
				if = {
					limit = {
						exists = scope:army_reward
					}
					custom_tooltip = ach_maintenance_events.0002.army_reward
				}
				if = {
					limit = {
						exists = scope:opinion_reward
					}
					custom_tooltip = ach_maintenance_events.0002.opinion_reward
				}
				if = {
					limit = {
						exists = scope:county_reward
					}
					every_in_list = {
						list = synergy_culture_counties
						show_as_tooltip = {
							add_county_modifier = {
								modifier = oath_mend_the_fracture_county
								years = 2
							}
						}	
					}
				}
			}
		}
		remove_variable = synergy_cultures_chance
		remove_variable = synergy_cultures_amount
	}
}

ach_maintenance_events.0100 = { #Coronation host is ded, notify guests
	type = character_event
	title = ach_maintenance_events.0100.t
	desc = ach_maintenance_events.0100.desc
	theme = realm

	left_portrait = {
		character = root
		animation = shock
	}

	right_portrait = {
		character = scope:host
		animation = dead
	}

	immediate = {

	}

	option = {
		name = ach_maintenance_events.0100.a
	}

}

ach_maintenance_events.0101 = { #Coronation host has been imprisoned, notify guests
	type = character_event
	title = ach_maintenance_events.0101.t
	desc = ach_maintenance_events.0101.desc
	theme = realm

	left_portrait = {
		character = root
		animation = shock
	}

	right_portrait = {
		character = scope:host
		animation = prisonhouse
	}

	lower_left_portrait = {
		character = scope:gaoler
	}

	immediate = {
		scope:host.imprisoner = {
			save_scope_as = gaoler
		}
	}

	trigger = {
		NOT = { #you know what you did
			this = scope:host.imprisoner
		}
	}

	option = {
		name = ach_maintenance_events.0101.a
	}

}

ach_maintenance_events.0102 = { #Coronation host has been imprisoned, notify host and cancellation
	type = character_event
	title = ach_maintenance_events.0102.t
	desc = ach_maintenance_events.0102.desc
	theme = realm

	left_portrait = {
		character = root
		animation = prisonhouse
	}

	right_portrait = {
		character = scope:gaoler
		animation = manic
	}

	immediate = {
		scope:host.imprisoner = {
			save_scope_as = gaoler
		}
	}

	option = {
		name = ach_maintenance_events.0102.a
	}
}

ach_maintenance_events.0103 = { #No one shows up, notify host
	type = character_event
	title = ach_maintenance_events.0103.t
	desc = ach_maintenance_events.0103.desc
	theme = realm

	left_portrait = {
		character = root
		animation = worry
	}


	immediate = {

	}

	option = {
		name = ach_maintenance_events.0103.a
	}

}

ach_maintenance_events.0104 = { #Officiator ded, host cancelled the coronation, notify guests
	type = character_event
	title = ach_maintenance_events.0104.t
	desc = ach_maintenance_events.0104.desc
	theme = realm

	left_portrait = {
		character = root
		animation = stunned
	}

	right_portrait = {
		character = scope:host
		animation = wailing
	}

	lower_right_portrait = {
		character = scope:dead_officiator
	}

	immediate = {
		scope:activity.var:officiator ?= { save_scope_as = dead_officiator }
	}

	option = {
		name = ach_maintenance_events.0104.a
	}

}

ach_maintenance_events.0105 = { #Host excommunicated, notify guests
	type = character_event
	title = ach_maintenance_events.0105.t
	desc = ach_maintenance_events.0105.desc
	theme = realm

	left_portrait = {
		character = root
		animation = worry
	}

	center_portrait = {
		character = scope:host
		animation = shame
	}

	right_portrait = {
		character = scope:hof
		animation = go_to_your_room
	}

	immediate = {
		faith.religious_head = { save_scope_as = hof }
	}

	option = {
		name = ach_maintenance_events.0105.a
	}

}

ach_maintenance_events.0106 = { #Host excommunicated, notify host
	type = character_event
	title = ach_maintenance_events.0106.t
	desc = ach_maintenance_events.0106.desc
	theme = realm

	left_portrait = {
		character = root
		animation = shame
	}

	right_portrait = {
		character = scope:hof
		animation = go_to_your_room
	}

	immediate = {
		faith.religious_head = { save_scope_as = hof }
	}

	option = {
		name = ach_maintenance_events.0106.a
	}

}
