﻿namespace = minor_decisions

#invite_knights_decision
# minor_decisions.0001 - invite the knights

#invite_claimants_decision
# minor_decisions.0002 - invite the claimants

#evict_holy_order_duel
# minor_decisions.0100 - evict_holy_order_duel

#Inviting knights for invite_knights_decision
minor_decisions.0001 = {
	hidden = yes

	trigger = {
		exists = var:number_of_invite_knights_countdown
	}

	immediate = {
		save_temporary_scope_as = host

		#Set up a list of provinces, one per duchy in the realm, find all relevant pool characters there
		every_realm_province = {
			if = {
				limit = {
					save_temporary_scope_as = potential_province
					NOT = {
						any_in_list = {
							list = pool_provinces
							county.duchy = scope:potential_province.county.duchy
						}
					}
				}
				add_to_temporary_list = pool_provinces
			}
		}

		every_in_list = {
			list = pool_provinces
			save_temporary_scope_as = pool_province

			every_pool_character = {
				province = scope:pool_province
				limit = {
					prowess >= invite_knights_decision_standard_value
					guest_knight_candidate_trigger = { HOST = root }
				}
				add_to_temporary_list = potential_knights
			}
		}

		#Randomize a good knight from the realm pool characters
		random_in_list = {
			list = potential_knights
			limit = { is_alive = yes }
			weight = {
				base = 1
				modifier = {
					add = {
						value = prowess
						subtract = invite_knights_decision_standard_value
						multiply = 5
					}
				}
			}
			save_scope_as = knight
		}

		#If no one was found, make one
		if = {
			limit = { NOT = { exists = scope:knight } }

			create_character = {
				location = root.capital_province
				template = pool_repopulate_prowess
				prowess = { invite_knights_decision_standard_value invite_knights_decision_upper_value }
				save_scope_as = knight
			}
		}
		
		#Cultural Traditions
		if = {
			limit = {
				root.culture = {
					has_cultural_parameter = better_knights_from_decision
				}
			}
			hidden_effect = {
				scope:knight = {
					blademaster_lifestyle_rank_up_effect = yes
					knight_increase_prowess_chance_effect = yes
					knight_increase_prowess_chance_effect = yes
				}
			}
		}

		#Add knight to court
		if = {
			limit = {
				scope:knight = {
					NOT = { has_trait = tourney_participant }
					is_lowborn = no
				}
			}
			random_list = {
				40 = {}
				60 = {
					trigger = { has_dlc_feature = tours_and_tournaments }
					scope:knight = {
						add_trait = tourney_participant
						add_random_tourney_participant_level_effect = yes
					}
				}
			}
		}
		add_visiting_courtier = scope:knight
		send_interface_message = {
			title = minor_decisions.0001.t
			desc = minor_decisions.0001.desc
			type = event_prowess_good_desc
			left_icon = scope:knight
		}


		#Count down the number of extra knights and either end the chain or resend this event
		change_variable = {
			name = number_of_invite_knights_countdown
			subtract = 1
		}

		if = {
			limit = { var:number_of_invite_knights_countdown = 0 }
			remove_variable = number_of_invite_knights_countdown
		}
		else = {
			clear_saved_scope = knight
			trigger_event = {
				id = minor_decisions.0001
				days = { 100 200 }
			}
		}
	}
}


#Inviting claimants for invite_claimants_decision
minor_decisions.0002 = {
	hidden = yes

	trigger = {
		exists = var:number_of_invite_claimants_countdown
	}

	immediate = {
		save_scope_as = host #Must be permanent for courtier_guest_management.1001


		#Set up a list of provinces, one per duchy in the realm, find all relevant pool characters there
		every_realm_province = {
			if = {
				limit = {
					save_temporary_scope_as = potential_province
					NOT = {
						any_in_list = {
							list = pool_provinces
							county.duchy = scope:potential_province.county.duchy
						}
					}
				}
				add_to_temporary_list = pool_provinces
			}
		}

		every_in_list = {
			list = pool_provinces
			save_temporary_scope_as = pool_province

			every_pool_character = {
				province = scope:pool_province
				limit = {
					any_claim = { useful_courtier_or_guest_claim_trigger = { RULER = root } }
				}
				add_to_temporary_list = potential_claimants
			}
		}

		#Randomize a good claimant from the realm pool characters
		random_in_list = {
			list = potential_claimants
			weight = {
				base = 0
				modifier = {
					add = {
						if = {
							limit = {
								any_claim = {
									useful_courtier_or_guest_claim_trigger = { RULER = scope:host }
									tier >= tier_kingdom
								}
							}
							add = 20
						}
						else_if = {
							limit = {
								any_claim = {
									useful_courtier_or_guest_claim_trigger = { RULER = scope:host }
									tier >= tier_duchy
								}
							}
							add = 5
						}
						else = {
							add = 1
						}
					}
				}
			}
			save_temporary_scope_as = claimant
		}

		#Does claimant exist? Send it my way and count down the variable
		if = {
			limit = { exists = scope:claimant }

			add_visiting_courtier = scope:claimant

			scope:claimant = {
				save_scope_as = guest #For courtier_guest_management.1001
				trigger_event = courtier_guest_management.1001 #For message
			}
			change_variable = {
				name = number_of_invite_claimants_countdown
				subtract = 1
			}
		}

		#Either end the chain or resend this event
		if = {
			limit = { var:number_of_invite_claimants_countdown = 0 }
			remove_variable = number_of_invite_claimants_countdown
		}
		else = {
			trigger_event = {
				id = minor_decisions.0002
				days = 25
			}
		}
	}
}

minor_decisions.0003 = {
	title = minor_decisions.0003.t
	left_portrait = {
		character = root
		animation = steward
	}
	right_portrait = {
		character = scope:barrel_character
		animation = scheme
	}
	desc = {
		desc = minor_decisions.0003.desc.opening
		first_valid = { # In what way is this character unusual
			triggered_desc = {
				trigger = { 
					scope:barrel_character = {
						has_character_flag = diseased_flag
					}
				}
				desc = minor_decisions.0003.desc.diseased
			}
			triggered_desc = {
				trigger = { 
					scope:barrel_character = {
						has_character_flag = physical_flag
					}
				}
				desc = minor_decisions.0003.desc.physical
			}
			triggered_desc = {
				trigger = { 
					scope:barrel_character = {
						has_character_flag = maimed_flag
					}
				}
				desc = minor_decisions.0003.desc.maimed
			}
			triggered_desc = {
				trigger = { 
					scope:barrel_character = {
						has_character_flag = criminal_flag
					}
				}
				desc = minor_decisions.0003.desc.criminal
			}
			triggered_desc = {
				trigger = { 
					scope:barrel_character = {
						has_character_flag = ewan_flag
					}
				}
				desc = minor_decisions.0003.desc.ewan
			}
		}
		first_valid = { # Criminal or weird
			triggered_desc = {
				trigger = { 
					scope:barrel_character = {
						has_character_flag = criminal_flag
					}
				}
				desc = minor_decisions.0003.desc.criminal_bridge
			}
			triggered_desc = {
				trigger = { 
					scope:barrel_character = {
						NOT = { has_character_flag = criminal_flag }
					}
				}
				desc = minor_decisions.0003.desc.bridge_1
			}
		}
		desc = minor_decisions.0003.desc.bride_2
		first_valid = { # What is this character good at
			triggered_desc = {
				trigger = { 
					scope:barrel_character = {
						has_trait = education_learning
					}
				}
				desc = minor_decisions.0003.desc.educated
			}
			triggered_desc = {
				trigger = { 
					scope:barrel_character = {
						has_trait = education_intrigue
					}
				}
				desc = minor_decisions.0003.desc.cunning
			}
			triggered_desc = {
				trigger = { 
					scope:barrel_character = {
						has_trait = education_diplomacy
					}
				}
				desc = minor_decisions.0003.desc.charismatic
			}
			triggered_desc = {
				trigger = { 
					scope:barrel_character = {
						has_trait = education_stewardship
					}
				}
				desc = minor_decisions.0003.desc.organised
			}
			triggered_desc = {
				trigger = { 
					scope:barrel_character = {
						has_trait = education_martial
					}
				}
				desc = minor_decisions.0003.desc.strong
			}
			desc = minor_decisions.0003.desc.outro
		}
	}

	theme = court

	immediate = {
		hidden_effect = {
			random_list = {
				# New character has a disease
				30 = {
					create_character = {
						location = root.capital_province
						template = random_barrel_character
						random_traits_list = {
							count = { 1 3 }
							consumption = {}
							typhus = {}
							lovers_pox = {}
							great_pox = {}
							pneumonic = {}
						}
						health = 6
						save_scope_as = barrel_character
					}
					scope:barrel_character = {
						add_character_flag = diseased_flag
					}
				}
				# New character has a genetic condition considered "bad"
				30 = {
					create_character = {
						location = root.capital_province
						template = random_barrel_character
						random_traits_list = {
							count = { 1 3 }
							dwarf = {}
							giant = {}
							hunchbacked = {}
							inbred = {}
							weak = {}
							dull = {}
							clubfooted = {}
							lisping = {}
							stuttering = {}
							wheezing = {}
							albino = {}
							scaly = {}
							spindly = {}
						}
						health = 3
						save_scope_as = barrel_character
					}
					scope:barrel_character = {
						add_character_flag = physical_flag
					}
				}
				# New character is disfigured in some way
				30 = {
					create_character = {
						location = root.capital_province
						template = random_barrel_character
						random_traits_list = {
							count = { 1 3 }
							maimed = {}
							one_eyed = {}
							one_legged = {}
							disfigured = {}
							blind = {}
							eunuch_1 = {
								trigger = {
									is_female = no
								}
							}
						}
						health = 4
						save_scope_as = barrel_character
					}
					scope:barrel_character = {
						add_character_flag = maimed_flag
					}
				}
				# New character is a criminal
				9 = {
					create_character = {
						location = root.capital_province
						template = random_barrel_character
						random_traits_list = {
							count = 1
							cannibal = {
								trigger = {
									root.faith = {
										NOT = {
											has_doctrine_parameter = cannibalism_legal
										}
									}
								}
							}
							murderer = {}
							fornicator = {
								trigger = {
									root.faith = {
										NOR = {
											has_doctrine_parameter = adultery_male_crime
											has_doctrine_parameter = adultery_female_crime
										}
									}
								}
							}
							adulterer = {
								trigger = {
									root.faith = {
										NOR = {
											has_doctrine_parameter = adultery_male_crime
											has_doctrine_parameter = adultery_female_crime
										}
									}
								}
							}
							incestuous = {
								trigger = {
									root.faith = {
										NOT = {
											has_doctrine = doctrine_consanguinity_unrestricted
										}
									}
								}
							}
							sodomite = {
								trigger = {
									root.faith = {
										has_doctrine = doctrine_homosexuality_crime
									}
								}
							}
							deviant = {
								trigger = {
									root.faith = {
										has_doctrine_parameter = deviancy_illegal
									}
								}
							}
						}
						save_scope_as = barrel_character
					}
					scope:barrel_character = {
						add_character_flag = criminal_flag
					}
				}
				# New character is a shambling corpse of a person
				1 = {
					create_character = {
						location = root.capital_province
						template = random_barrel_character
						random_traits_list = {
							count = { 3 5 }
							consumption = {}
							cancer = {}
							typhus = {}
							smallpox = {}
							leper = {}
							lovers_pox = {}
							great_pox = {}
							pneumonic = {}
						}

						random_traits_list = {
							count = { 2 4 }
							lunatic_genetic = {}
							possessed_genetic = {}
							dwarf = {}
							giant = {}
							inbred = {}
							weak = {}
							dull = {}
							clubfooted = {}
							hunchbacked = {}
							lisping = {}
							stuttering = {}
							wheezing = {}
							albino = {}
							scaly = {}
							spindly = {}
						}

						random_traits_list = {
							count = { 2 3 }
							maimed = {}
							one_eyed = {}
							one_legged = {}
							disfigured = {}
							blind = {}
							eunuch_1 = {}
						}

						health = 8

						save_scope_as = barrel_character
					}
					scope:barrel_character = {
						add_character_flag = ewan_flag
					}
				}
			}
			add_courtier = scope:barrel_character
			# Determine which court position they will be offered
			decide_court_position_to_give_effect = {
				CANDIDATE = scope:barrel_character
				EMPLOYER = root
			}
		}
	}

	# Grant them the requested court position
	option = { 
		name = minor_decisions.0003.a
		ai_chance = { # The AI should basically always accept if they took the decision in the first place
			base = 100
		}
		distribute_determined_court_position_effect = {
			CANDIDATE = scope:barrel_character
			EMPLOYER = root
		}
		change_current_court_grandeur = medium_court_grandeur_loss
	}
	# Make them an executioner; it was common to pluck random lowborns off the street to become executioner
	option = { 
		name =  minor_decisions.0003.b
		trigger = {
			can_appoint_char_to_court_position = {
				CHAR = scope:barrel_character
				COURT_POS = cupbearer_court_position
			}
			scope:barrel_character = { NOT = { has_character_flag = executioner_flag } }
		}
		change_current_court_grandeur = minor_court_grandeur_gain
		court_position_grant_effect = {
			EMPLOYER = root
			POS = executioner
			CANDIDATE = scope:barrel_character
		}
	}
	# I don't like this person, get me another
	option = { 
		name = minor_decisions.0003.c
		trigger = {
			NOT = { has_character_flag = already_rejected_a_barrel_char_flag }
		}
		scope:barrel_character = {
			silent_disappearance_effect = yes
		}
		trigger_event = minor_decisions.0003
		hidden_effect = {
			add_character_flag = {
				flag = already_rejected_a_barrel_char_flag
				years = 1
			}
		}
	}
	# Forget this
	option = { 
		name = minor_decisions.0003.d
		add_prestige_no_experience = 150
		scope:barrel_character = {
			silent_disappearance_effect = yes
		}
	}
}

#Inviting poets for invite_poets_decision
minor_decisions.0004 = {
	hidden = yes

	trigger = {
		exists = var:number_of_invite_poets_countdown
	}

	immediate = {
		save_temporary_scope_as = host

		#Set up a list of provinces, one per duchy in the realm, find all relevant pool characters there
		every_realm_province = {
			if = {
				limit = {
					save_temporary_scope_as = potential_province
					NOT = {
						any_in_list = {
							list = pool_provinces
							county.duchy = scope:potential_province.county.duchy
						}
					}
				}
				add_to_temporary_list = pool_provinces
			}
		}

		every_in_list = {
			list = pool_provinces
			save_temporary_scope_as = pool_province

			every_pool_character = {
				province = scope:pool_province
				limit = {
					diplomacy >= invite_poets_decision_standard_value
					guest_poet_candidate_trigger = yes
				}
				add_to_temporary_list = potential_poets
			}
		}

		#Randomize a good knight from the realm pool characters
		random_in_list = {
			list = potential_poets
			limit = { is_alive = yes }
			weight = {
				base = 1
				modifier = {
					add = {
						value = diplomacy
						subtract = invite_poets_decision_standard_value
						multiply = 5
					}
				}
			}
			save_scope_as = poet
		}

		#If no one was found, make one
		if = {
			limit = { NOT = { exists = scope:poet } }

			create_character = {
				location = root.capital_province
				template = ep3_poet_template
				diplomacy = { invite_poets_decision_standard_value invite_poets_decision_upper_value }
				save_scope_as = poet
			}
		}

		add_courtier = scope:poet
		send_interface_message = {
			title = minor_decisions.0004.t
			desc = minor_decisions.0004.desc
			type = event_diplomacy_good
			left_icon = scope:poet
		}


		#Count down the number of extra poets and either end the chain or resend this event
		change_variable = {
			name = number_of_invite_poets_countdown
			subtract = 1
		}

		if = {
			limit = { var:number_of_invite_poets_countdown = 0 }
			remove_variable = number_of_invite_poets_countdown
		}
		else = {
			clear_saved_scope = poet
			trigger_event = {
				id = minor_decisions.0004
				days = { 100 200 }
			}
		}
	}
}
