﻿# This effect requires you to input which convert to convert to with GOVERNMENT_TO_ADOPT
# If you want it to be dynamic, simply use "flag:dynamic"
# Otherwise, set the government you want to adopt
convert_to_administrative_from_feudalism_effect = {
	save_scope_as = administrative_liege
	
	switch = {
		trigger = $GOVERNMENT_TO_ADOPT$
		flag:dynamic = { # If we want the selection to be dynamic, save and clear the scope to prevent errors
			save_scope_value_as = {
				name = has_selected_a_government
				value = flag:dynamic
			}
			clear_saved_scope = has_selected_a_government
		}
		flag:freehold = { # freehold_government / Administrative
			save_scope_value_as = {
				name = has_selected_a_government
				value = flag:freehold
			}
		}
		flag:administrative = { # administrative_government / Administrative
			save_scope_value_as = {
				name = has_selected_a_government
				value = flag:administrative
			}
		}
		flag:celestial = { # celestial_government / Celestial
			save_scope_value_as = {
				name = has_selected_a_government
				value = flag:celestial
			}
		}
		flag:meritocratic = { # meritocratic_government / Meritocratic
			save_scope_value_as = {
				name = has_selected_a_government
				value = flag:meritocratic
			}
		}
		flag:japan_administrative = { # japan_administrative_government / Ritsuryo
			save_scope_value_as = {
				name = has_selected_a_government
				value = flag:japan_administrative
			}
		}
	}

	change_to_administrative_effect = yes

	#Direct Vassals who fulfill the ep3_vassal_will_become_admin criteria are converted. Vassals above 25 opinion always accept
	#Vassals are converted as a hierarchy. If a king is converted we convert all who are valid below that king (so his dukes, their counts, and their barons) are all converted.
	#If a vassal does not convert (because of not accepting or because they are outside of the de jure empire, etc) then the vassals below them are _not_ converted either.

	#Human vassals are given a choice to convert or not in the ping event, even if powerful or having very good relation.

	hidden_effect = { 
		save_scope_as = actor
		every_powerful_vassal = { #All valid powerful vassals auto convert - their support was required to take the decision.
			limit = {
				ep3_vassal_will_become_admin = yes
				is_ai = yes
			}
			save_scope_as = recipient
			change_to_administrative_effect = yes	
			every_vassal = { #Everyone below also converts
				limit = {
					ep3_vassal_will_become_admin = yes #We still don't want mayors and bishops
					is_ai = yes
				}
				save_scope_as = recipient	
				change_to_administrative_effect = yes
				every_vassal = { #Everyone below also converts (could be Counts, could be Barons)
					limit = {
						ep3_vassal_will_become_admin = yes #We still don't want mayors and bishops
						is_ai = yes
					}
					save_scope_as = recipient	
					change_to_administrative_effect = yes
					every_vassal = { #Everyone below also converts could be Barons)
						limit = {
							ep3_vassal_will_become_admin = yes #We still don't want mayors and bishops
							is_ai = yes
						}
						save_scope_as = recipient
						change_to_administrative_effect = yes
					}
				}
			}
		}
		every_vassal = {
			limit = {
				ep3_vassal_will_become_admin = yes
				trigger_if = { #Dukes and above can say no
					limit = {
						primary_title.tier >= tier_duchy
						is_ai = yes
					}
					opinion = {
						target = scope:administrative_liege
						value > 25
					}
				}
			}
			save_scope_as = recipient
			change_to_administrative_effect = yes
			every_vassal = { #Everyone below also converts (could be Counts, could be Dukes)
				limit = {
					ep3_vassal_will_become_admin = yes #We still don't want mayors and bishops
					is_ai = yes
				}
				save_scope_as = recipient	
				change_to_administrative_effect = yes
				every_vassal = { #Everyone below also converts (Could be Counts, could be Barons)
					limit = {
						ep3_vassal_will_become_admin = yes #We still don't want mayors and bishops
						is_ai = yes
					}
					save_scope_as = recipient	
					change_to_administrative_effect = yes
					every_vassal = { #Everyone below also converts (could be Barons)
						limit = {
							ep3_vassal_will_become_admin = yes #We still don't want mayors and bishops
							is_ai = yes
						}
						save_scope_as = recipient
						change_to_administrative_effect = yes
					}
				}
			}
		}
		every_vassal = {
			limit = {
				ep3_vassal_will_become_admin = yes
				trigger_if = { #Dukes and above can say no
					limit = {
						primary_title.tier >= tier_duchy
						is_ai = yes
					}
					opinion = {
						target = scope:administrative_liege
						value > 0
					}
				}
			}
			random_list = {
				10 = { } #10% of the vassals that like us less than 25 but more than 0 still convert.
				90 = {
					save_scope_as = recipient
					change_to_administrative_effect = yes
					every_vassal = { #Everyone below also converts (could be Counts, could be Dukes)
						limit = {
							ep3_vassal_will_become_admin = yes #We still don't want mayors and bishops
							is_ai = yes
						}
						save_scope_as = recipient	
						change_to_administrative_effect = yes
						every_vassal = { #Everyone below also converts (Could be Counts, could be Barons)
							limit = {
								ep3_vassal_will_become_admin = yes #We still don't want mayors and bishops
								is_ai = yes
							}
							save_scope_as = recipient	
							change_to_administrative_effect = yes
							every_vassal = { #Everyone below also converts (could be Barons)
								limit = {
									ep3_vassal_will_become_admin = yes #We still don't want mayors and bishops
									is_ai = yes
								}
								save_scope_as = recipient	
								change_to_administrative_effect = yes
							}
						}
					}
				}
			}
		}
		every_vassal = {
			limit = {
				highest_held_title_tier >= tier_duchy
				house.house_head ?= this
				government_has_flag = government_is_administrative
				NOT = {
					any_held_title = { is_noble_family_title = yes }
				}
			}
			create_noble_family_effect = { GOVERNMENT_GIVER = this }
		}
	}

	if = {
		limit = {
			#AGOT Disabled
			# NOT = {
			# 	has_character_flag = latin_emp_force_admin_flag
			# }
			top_liege = this
		}
		add_character_modifier = {
			modifier = first_admin_emperor
		}
	}
	
	change_influence = admin_convert_influence_value

	custom_description_no_bullet = {
		text = vassals_can_become_administrative
	}
	custom_tooltip = powerful_vassals_become_administrative
	custom_tooltip = vassals_become_administrative
	custom_tooltip = vassals_who_may_switch_to_administrative
}

convert_to_administrative_from_feudalism_game_start_effect = {
	if = {
		limit = { has_dlc_feature = roads_to_power }
		# Because we run this twice, since folks can change in the lobby.
		if = {
			limit = {
				NOT = { government_has_flag = government_is_administrative }
			}
			change_government = administrative_government
			create_noble_family_effect = { GOVERNMENT_GIVER = this }
			add_to_list = estatified_list
		}

		every_vassal_or_below = {
			limit = {
				highest_held_title_tier >= tier_county
				OR = {
					government_has_flag = government_is_feudal
					government_has_flag = government_is_clan
					government_has_flag = government_is_tribal
				}
				NOR = {
					faith.religious_head ?= this
					liege = {
						faith.religious_head ?= this
						top_liege != this
					}
					#AGOT Disabled
					#AND = {
					#	exists = scope:ignore_culture
					#	culture = scope:ignore_culture
					#}
				}
			}
			change_to_administrative_effect = yes
		}
	}
}

#AGOT Disabled
# apply_historic_administrative_game_rule_effect = {
# 	# We'll be doing some excessive triggers below in a bid to lightly future-proof + error-spam mods less, don't worry about it.
# 	## We also do player-specific realms separately — we need to know what their realm is so we do it after the lobby exclusively.
# 	# Egypt.
# 	if = {
# 		limit = {
# 			NOT = { has_game_rule = historicity_extra_admin_egypt_gameplay_not_administrative }
# 			exists = title:k_egypt
# 		}
# 		if = {
# 			limit = { has_game_rule = historicity_extra_admin_egypt_historic_always }
# 			title:k_egypt.holder ?= { convert_to_administrative_from_feudalism_game_start_effect = yes }
# 		}
# 	}
# 	# Arabia.
# 	if = {
# 		limit = {
# 			NOT = { has_game_rule = historicity_extra_admin_arabia_gameplay_not_administrative }
# 			exists = title:e_arabia
# 		}
# 		if = {
# 			limit = { has_game_rule = historicity_extra_admin_arabia_historic_867 }
# 			# Give the Armenians a fighting chance.
# 			culture:armenian = { save_scope_as = ignore_culture }
# 			title:e_arabia.holder ?= { convert_to_administrative_from_feudalism_game_start_effect = yes }
# 		}
# 	}
# 	# Ghana.
# 	if = {
# 		limit = {
# 			NOT = { has_game_rule = historicity_extra_admin_ghana_gameplay_not_administrative }
# 			exists = title:k_ghana
# 		}
# 		if = {
# 			limit = { has_game_rule = historicity_extra_admin_ghana_historic_always }
# 			title:k_ghana.holder ?= { convert_to_administrative_from_feudalism_game_start_effect = yes }
# 		}
# 	}
# 	# Kabulistan/the Ghaznivids.
# 	if = {
# 		limit = {
# 			NOT = { has_game_rule = historicity_extra_admin_kabulistan_gameplay_not_administrative }
# 			exists = title:k_kabulistan
# 		}
# 		if = {
# 			limit = { has_game_rule = historicity_extra_admin_kabulistan_historic_always }
# 			title:k_kabulistan.holder ?= { convert_to_administrative_from_feudalism_game_start_effect = yes }
# 		}
# 	}
# 	# Persia.
# 	if = {
# 		limit = {
# 			NOT = { has_game_rule = historicity_extra_admin_persia_gameplay_not_administrative }
# 			exists = title:e_persia
# 			exists = title:k_persia
# 		}
# 		if = {
# 			limit = {
# 				game_start_date = 1178.10.1
# 				has_game_rule = historicity_extra_admin_persia_historic_1178
# 			}
# 			title:k_persia.holder ?= { convert_to_administrative_from_feudalism_game_start_effect = yes }
# 		}
# 		else_if = {
# 			limit = { has_game_rule = historicity_extra_admin_persia_alt_historic_always }
# 			# Independent k_persia.
# 			if = {
# 				limit = {
# 					title:k_persia.holder ?= { top_liege = this }
# 				}
# 				title:k_persia.holder ?= { convert_to_administrative_from_feudalism_game_start_effect = yes }
# 			}
# 			# Else, the empire.
# 			else = {
# 				title:e_persia.holder ?= { convert_to_administrative_from_feudalism_game_start_effect = yes }
# 			}
# 		}
# 	}
# 	# Maghreb.
# 	if = {
# 		limit = {
# 			NOT = { has_game_rule = historicity_extra_admin_maghreb_gameplay_not_administrative }
# 			exists = title:k_maghreb
# 		}
# 		if = {
# 			limit = {
# 				game_start_date >= 1066.9.15
# 				has_game_rule = historicity_extra_admin_maghreb_historic_post_1066
# 			}
# 			title:k_maghreb.holder ?= { convert_to_administrative_from_feudalism_game_start_effect = yes }
# 		}
# 		else_if = {
# 			limit = { has_game_rule = historicity_extra_admin_maghreb_alt_historic_always }
# 			title:k_maghreb.holder ?= { convert_to_administrative_from_feudalism_game_start_effect = yes }
# 		}
# 	}
# 	# Tamilakam/the Cholas.
# 	if = {
# 		limit = {
# 			NOT = { has_game_rule = historicity_extra_admin_tamilakam_gameplay_not_administrative }
# 			exists = title:k_tamilakam
# 		}
# 		if = {
# 			limit = {
# 				game_start_date = 1066.9.15
# 				has_game_rule = historicity_extra_admin_tamilakam_historic_1066
# 			}
# 			title:k_tamilakam.holder ?= { convert_to_administrative_from_feudalism_game_start_effect = yes }
# 		}
# 		else_if = {
# 			limit = { has_game_rule = historicity_extra_admin_tamilakam_alt_historic_always }
# 			title:k_tamilakam.holder ?= { convert_to_administrative_from_feudalism_game_start_effect = yes }
# 		}
# 	}
# 	# The Carolingians.
# 	if = {
# 		limit = {
# 			NOT = { has_game_rule = historicity_extra_admin_carolingians_gameplay_not_administrative }
# 			game_start_date = 867.1.1
# 			exists = title:k_france
# 			exists = title:k_lotharingia
# 			exists = title:k_east_francia
# 			exists = title:k_italy
# 		}
# 		if = {
# 			limit = { has_game_rule = historicity_extra_admin_carolingians_alt_historic_all_administrative }
# 			title:k_france.holder ?= { convert_to_administrative_from_feudalism_game_start_effect = yes }
# 			title:k_lotharingia.holder ?= { convert_to_administrative_from_feudalism_game_start_effect = yes }
# 			title:k_east_francia.holder ?= { convert_to_administrative_from_feudalism_game_start_effect = yes }
# 			title:k_italy.holder ?= { convert_to_administrative_from_feudalism_game_start_effect = yes }
# 		}
# 		if = {
# 			limit = { has_game_rule = historicity_extra_admin_carolingians_alt_historic_west_francia_administrative }
# 			title:k_france.holder ?= { convert_to_administrative_from_feudalism_game_start_effect = yes }
# 		}
# 		if = {
# 			limit = { has_game_rule = historicity_extra_admin_carolingians_alt_historic_lotharingia_administrative }
# 			title:k_lotharingia.holder ?= { convert_to_administrative_from_feudalism_game_start_effect = yes }
# 		}
# 		if = {
# 			limit = { has_game_rule = historicity_extra_admin_carolingians_alt_historic_east_francia_administrative }
# 			title:k_east_francia.holder ?= { convert_to_administrative_from_feudalism_game_start_effect = yes }
# 		}
# 		if = {
# 			limit = { has_game_rule = historicity_extra_admin_carolingians_alt_historic_italy_administrative }
# 			title:k_italy.holder ?= { convert_to_administrative_from_feudalism_game_start_effect = yes }
# 		}
# 	}
# }

convert_to_feudalism_from_administrative_effect = {
	#Gain some gold for selling off your estates
	if = {
		limit = {
			any_vassal = {
				any_held_title = { is_noble_family_title = yes }
				exists = domicile
			}
		}
		custom_tooltip = estates_get_bought
		every_vassal = {
			limit = { 
				any_held_title = { is_noble_family_title = yes }
				exists = domicile
			}
			hidden_effect = {
				add_gold = {
					value = {
						value = major_gold_value
						switch = {
							trigger = domicile.num_domicile_buildings
							7 = { multiply = 7 }
							6 = { multiply = 6 }
							5 = { multiply = 5 }
							4 = { multiply = 4 }
							3 = { multiply = 3 }
							2 = { multiply = 2 }
						}
					}
				}
			}	
		}
	}

	#Then change government
	if = {
		limit = { ep3_is_clan_inclined_trigger = yes }
		change_government = clan_government

		#Duchies automatically transfer, need to poke the kingdoms
		if = {
			limit = {
				any_vassal = {
					primary_title.tier >= tier_kingdom
					NOR = {
						government_has_flag = government_is_clan
						government_has_flag = government_is_theocracy
					}
				}
			}
			custom_tooltip = kings_become_clan
			every_vassal = {
				limit = {
					primary_title.tier >= tier_kingdom
					NOR = {
						government_has_flag = government_is_clan
						government_has_flag = government_is_theocracy
					}
				}
				hidden_effect = { change_government = clan_government }
			}
		}
	}
	else = {
		#AGOT Modified
		#change_government = feudal_government
		if = {
			limit = {
				agot_is_valid_for_lp_feudal_govt_when_converting_from_admin = yes		
			}
			change_government = lp_feudal_government
		}
		else = {
			change_government = feudal_government
		}

		#Duchies automatically transfer, need to poke the kingdoms
		if = {
			limit = {
				any_vassal = {
					primary_title.tier >= tier_kingdom
					NOR = {
						government_has_flag = government_is_feudal
						government_has_flag = government_is_theocracy
					}
				}
			}
			#AGOT Modified
			#custom_tooltip = kings_become_feudal
			if = {
				limit = {
					agot_is_valid_for_lp_feudal_govt_when_converting_from_admin = yes			
				}
				custom_tooltip = agot_converting_to_admin_kings_become_lp_feudal
			}
			else = {
				custom_tooltip = kings_become_feudal
			}
			#AGOT Modified
			#every_vassal = {
			#	limit = { 
			#		primary_title.tier >= tier_kingdom
			#		NOR = {
			#			government_has_flag = government_is_feudal
			#			government_has_flag = government_is_theocracy
			#		}
			#	}
			#	hidden_effect = { change_government = feudal_government }
			#}	
			every_vassal_or_below = {
				limit = { 
					primary_title.tier > tier_barony
					NOR = {
						government_has_flag = government_is_feudal
						government_has_flag = government_is_theocracy
					}
				}
				hidden_effect = {
					if = {
						limit = {
							agot_is_valid_for_lp_feudal_govt_when_converting_from_admin = yes
						}
						change_government = lp_feudal_government
					}
					else_if = {
						limit = {
							government_has_flag = government_is_administrative
						}
						change_government = feudal_government
					}
				}
			}
		}
	}
	
	#Governors become Dukes tooltip
	if = {
		limit = {
			any_vassal = {
				primary_title.tier = tier_duchy
				NOT = { government_has_flag = government_is_clan }
			}
		}
		custom_tooltip = governors_become_dukes
	}
}

humiliate_stooge_counter_effect = {
	if = {
		limit = {
			NOT = { has_variable = humiliated_stooge_counter_var }
		}
		set_variable = {
			name = humiliated_stooge_counter_var
			value = 1
		}
	}
	else = {
		change_variable = {
			name = humiliated_stooge_counter_var
			add = 1
		}
	}
}

humiliate_stooge_reward_effect = {
	hidden_effect = {
		scope:stooge ?= {
			add_opinion = {
				target = root
				opinion = -25
				modifier = humiliated_opinion
			}
		}
		if = {
			limit = {
				scope:stooge.var:humiliated_stooge_counter_var ?= 1
			}
			# First time, it's fine
			send_interface_toast = {
				type = event_toast_effect_good
				title = humiliate_the_stooge_decision_tt
				left_icon = root
				right_icon = scope:stooge
				add_stress = major_stress_impact_loss
				add_dread = minor_dread_gain
				add_character_modifier = {
					modifier = stooge_humiliated_modifier
					years = 2
				}
			}
		}
		else_if = {	
			limit = {
				scope:stooge.var:humiliated_stooge_counter_var ?= 2
			}
			send_interface_toast = {
				type = event_toast_effect_good
				title = humiliate_the_stooge_decision_2_tt
				left_icon = root
				right_icon = scope:stooge
				add_stress = medium_stress_impact_loss
				add_dread = medium_dread_gain
				add_character_modifier = {
					modifier = stooge_humiliated_modifier
					years = 2
				}
			}
		}
		else_if = {	
			limit = {
				scope:stooge.var:humiliated_stooge_counter_var ?= 3
			}
			# Third time, crying
			send_interface_toast = {
				type = event_toast_effect_good
				title = humiliate_the_stooge_decision_3_tt
				left_icon = root
				right_icon = scope:stooge
				add_stress = minor_stress_impact_loss
				add_dread = major_dread_gain
				add_character_modifier = {
					modifier = stooge_humiliated_modifier
					years = 2
				}
			}
		}
		else_if = {
			limit = {
				scope:stooge.var:humiliated_stooge_counter_var ?= 4
			}
			# Fourth time, it's over
			send_interface_toast = {
				type = event_toast_effect_neutral
				title = humiliate_the_stooge_decision_4_tt
				left_icon = root
				right_icon = scope:stooge
				add_dread = major_dread_gain
				add_character_modifier = {
					modifier = stooge_humiliated_fourth_time_modifier
					years = 2
				}
				remove_courtier_or_guest = scope:stooge
				scope:stooge = { # They won't join your court
					set_variable = {
						name = non_recruitable_var
						value = root 
					}
				}
			}
		}
	}
}

admin_confirmation_vassal_reward_guts_effect = {
	dynasty ?= { add_dynasty_prestige = minor_dynasty_prestige_gain }
	if = {
		limit = {
			scope:confirmation_liege = { is_ai = yes }
		}
		reverse_add_opinion = {
			target = scope:confirmation_liege
			modifier = respect_opinion
			opinion = 20
		}
	}
	add_prestige = major_prestige_gain
	change_influence = major_influence_gain
}
