spar_with_knight_interaction = {
	icon = icon_combat
	interface_priority = 30
	common_interaction = yes
	category = interaction_category_friendly
	ai_min_reply_days = 0
	ai_max_reply_days = 0
	popup_on_receive = yes
	pause_on_receive = yes
	desc = spar_with_knight_interaction_desc

	greeting = positive
	notification_text = REQUEST_SPARRING_TEXT

	is_shown = {
		scope:actor = {
			OR = {
				culture = {
					has_cultural_parameter = can_use_sparring_duel
				}
				top_participant_group:dynastic_cycle ?= {
					participant_group_type = expansion_movement
				}				
			}
		}
		scope:recipient = {
			is_knight_of = scope:actor
		}
		# Can't duel yourself.
		scope:actor != scope:recipient
	}

	is_valid_showing_failures_only = {
		scope:actor = { can_start_single_combat_trigger = yes }
		scope:actor = { can_be_combatant_based_on_gender_trigger = { ARMY_OWNER = scope:actor } }
		# Scope:recipient must be able to fight personally.
		scope:recipient = { can_start_single_combat_trigger = yes }
		# Scope:recipient must not be banned from single combat.
		scope:recipient = {
			custom_description = {
				text = fp1_tbc_recipient_banned_from_combat
				can_start_single_combat_banned_checks_trigger = no
			}
		}
		# Neither of the characters can be imprisoned.
		scope:actor = { is_imprisoned = no }
		scope:recipient = { is_imprisoned = no }
		
		scope:actor = { is_at_war = no }
		scope:recipient = { is_at_war = no }
	}

	can_send = {
		scope:actor = {
			custom_description = {
				text = "character_interactions_hostile_actions_disabled_delay"
				NOT = { has_character_flag = flag_hostile_actions_disabled_delay }
			}
		}
	}
	
	cost = { prestige = medium_prestige_value }
	cooldown = { years = 5 }
	cooldown_against_recipient = { years = 10 }

	on_send = {
		scope:actor = {
			add_character_flag = {
				flag = flag_hostile_actions_disabled_delay
				days = 10
			}
		}
	}

	on_accept = {
		# Set up the bout immediately.
		## First inform.
		scope:actor = {
			custom_tooltip = challenge_to_single_combat_interaction.enter_non_lethal_bout.tt
			custom_tooltip = spar_with_knight_interaction.effect.tt
			custom_tooltip = spar_with_knight_interaction.negative_effect.tt
		}
		scope:recipient = {
			custom_tooltip = spar_with_knight_interaction.effect.tt
			custom_tooltip = spar_with_knight_interaction.negative_effect.tt
		}
		## Finally, begin.
		configure_start_single_combat_effect = {
			SC_INITIATOR = scope:actor
			SC_ATTACKER = scope:actor
			SC_DEFENDER = scope:recipient
			FATALITY = no
			FIXED = no
			LOCALE = army_camp
			OUTPUT_EVENT = culture_tradition_events.0001
			INVALIDATION_EVENT = perk_interaction.0102
		}
	}

	ai_accept = {
		base = 10 # Try to make it 0 for most interactions
		
		modifier = {
			desc = CTSC_RELATIVE_PROWESS
			scope:recipient.prowess < scope:actor.prowess
			add = {
				value = scope:actor.prowess
				subtract = scope:recipient.prowess
				multiply = -2
			}
		}
		
		# Personality values
		ai_value_modifier = {
			who = scope:recipient
			ai_boldness = 0.5
			ai_honor = {
				if = {
					limit = {
						scope:recipient = {
							ai_honor > 0
						}
					}
					value = 0.25
				}
			}
		}
		modifier = {
			desc = AI_REFUSAL_WOUNDED
			scope:recipient = {
				OR = {
					has_trait = wounded 
					has_trait = maimed
				}
			}
			add = -200
		}
	}

	# AI
	ai_potential = {
		culture = {
			has_cultural_parameter = can_use_sparring_duel
		}
		NOT = { has_trait = craven }
		is_imprisoned = no
		can_start_single_combat_trigger = yes
		number_of_knights > 0
	}
	
	ai_target_quick_trigger = {
		adult = yes
	}
	ai_targets = {
		ai_recipients = vassals
		max = 10
	}
	ai_targets = {
		ai_recipients = courtiers
		max = 10
	}

	ai_frequency_by_tier = {
		barony = 0
		county = 72
		duchy = 72
		kingdom = 36
		empire = 36
		hegemony = 36
	}

	ai_will_do = {
		base = -25
		
		# Factor for personality (excluding realism).
		modifier = {
			add = {
				value = ai_boldness
				multiply = 1
			}
		}
		modifier = {
			add = {
				value = ai_energy
				multiply = 0.5
			}
		}
		modifier = {
			add = -50
			prowess <= low_skill_rating
		}
		modifier = {
			factor = 0
			OR = {
				has_trait = wounded 
				has_trait = maimed
			}
		}
	}
}


#Appointing a Podesta over a Commune - Turning a one county holder into a Republican Vassal
#Local culture must have the parameter, as does the granting character.
#Recipient does not need to have local culture.
appoint_podesta_interaction = {
	icon = grant_titles_interaction
	interface_priority = 30
	common_interaction = yes
	category = interaction_category_vassal
	ai_min_reply_days = 0
	ai_max_reply_days = 0
	popup_on_receive = yes
	pause_on_receive = yes
	desc = appoint_podesta_desc

	greeting = positive
	notification_text = REQUEST_REPUBLIC_TEXT

	is_shown = {
		scope:actor != scope:recipient # Can't make yourself a Podesta.
		scope:actor = {
			culture = {
				has_cultural_parameter = can_make_republican_vassals
			}
			any_held_title = {
				count > 1
			}
		}
		scope:recipient = {
			is_ai = yes
			is_landed = yes
			target_is_liege_or_above = scope:actor
			NOR = { 
				government_has_flag = government_is_republic 
				government_has_flag = government_is_prepublic 
				government_has_flag = government_is_urepublic 
			}
			
		}
	}

	is_valid_showing_failures_only = {
		scope:recipient = { can_be_granted_titles_by = { RULER = scope:actor } }
		NOT = { scope:actor = { is_at_war_with = scope:recipient } }

		custom_description = {
			text = too_many_republics
			scope:actor = {
				any_vassal = {
					count < 4 #Use Script Value
					highest_held_title_tier >= tier_county
					government_has_flag = government_is_republic
				}
			}
		}

		
		scope:recipient = {
			target_is_liege_or_above = scope:actor
			NOR = { 
				government_has_flag = government_is_republic 
				government_has_flag = government_is_prepublic 
				government_has_flag = government_is_urepublic 
			}
			is_claimant = no

			custom_description = {
				text = must_be_one_county_count
				any_held_title = {
					count = 1
					title_tier = county
				}
			}

			custom_description = {
				text = must_not_have_landed_relatives
				NOT = {
					any_close_or_extended_family_member = {
						is_landed_or_landless_administrative = yes
					}
				}
			}
			
			custom_description = {
				text = must_not_be_heir_to_any_title
				scope:recipient = {
					any_heir_title = {
						count < 1
						tier >= tier_county
					}
				}
			}

			custom_description = {	#The county must be developed or have at least one city
				text = county_must_have_city
				any_held_title = {
					count = 1
					title_tier = county
					OR = {
						any_county_province = {
							has_holding_type = city_holding
						}
						development_level > 20
					}
				}
			}
			custom_description = { #County must be of a culture that has the tradition parameter
				text = county_must_have_culture
				any_held_title = {
					count = 1
					title_tier = county
					OR = {
						any_county_province = {
							culture = {
								has_cultural_parameter = can_make_republican_vassals
							}
						}
						OR = { 
							government_has_flag = government_is_prepublic 
							government_has_flag = government_is_urepublic 
						}
					}
				}
			}			
		}

		trigger_if = {
			limit = {
				scope:recipient = {
					is_ruler = no
				}
			}
			scope:recipient = {
				is_imprisoned = no
			}
		}
	}

	cost = { prestige = minor_prestige_value }
	cooldown = { years = 5 }
	cooldown_against_recipient = { years = 10 }

	on_accept = {
		scope:actor = {
			hidden_effect = {
				send_interface_toast = {
					type = event_toast_effect_neutral
					title = appoint_podesta_interaction_notification

					left_icon = scope:actor					
					right_icon = scope:recipient
					if = {
						limit = {
							scope:actor	= { government_has_flag = government_is_urepublic }
						}
						scope:recipient = {
							show_as_tooltip = {
								change_government = urbanrepublic_government
								create_title_and_vassal_change = {
									type = returned
									save_scope_as = change
									add_claim_on_loss = no
								}
								primary_title = { 
									add_to_global_variable_list = {
										name = urepublic_list
										target = prev
									}
								}
								every_vassal = {
									limit = {
										government_has_flag = government_is_republic
									}
									every_held_title = { 
										change_title_holder = {
											holder = scope:recipient
											change = scope:change
											take_baronies = yes
										}
									}
								}
								resolve_title_and_vassal_change = scope:change
							}
						}
					}
					else_if = {
						limit = {
							scope:actor	= { government_has_flag = government_is_prepublic }
						}
						scope:recipient = {
							show_as_tooltip = {
								change_government = peasantrepublic_government
								create_title_and_vassal_change = {
									type = returned
									save_scope_as = change
									add_claim_on_loss = no
								}
								primary_title = { 
									add_to_global_variable_list = {
										name = prepublic_list
										target = prev
									}
								}
								every_vassal = {
									limit = {
										government_has_flag = government_is_republic
									}
									every_held_title = { 
										change_title_holder = {
											holder = scope:recipient
											change = scope:change
											take_baronies = yes
										}
									}
								}
								resolve_title_and_vassal_change = scope:change
							}
						}
					}
					else = {
						scope:recipient = {
							show_as_tooltip = {
								change_government = republic_government
								create_title_and_vassal_change = {
									type = returned
									save_scope_as = change
									add_claim_on_loss = no
								}
								every_vassal = {
									limit = {
										government_has_flag = government_is_republic
									}
									every_held_title = { 
										change_title_holder = {
											holder = scope:recipient
											change = scope:change
											take_baronies = yes
										}
									}
								}
								resolve_title_and_vassal_change = scope:change
							}
						}
					}
				}
			}
		}
		scope:recipient = {
			change_government = republic_government
			create_title_and_vassal_change = {
				type = returned
				save_scope_as = change
				add_claim_on_loss = no
			}
			every_vassal = {
				limit = {
					government_has_flag = government_is_republic
				}
				every_held_title = { 
					change_title_holder = {
						holder = scope:recipient
						change = scope:change
						take_baronies = yes
					}
				}
			}
			resolve_title_and_vassal_change = scope:change
		}

		# If we're a clan this interaction affects unity
		add_clan_unity_interaction_effect = {
			CHARACTER = scope:actor
			TARGET = scope:recipient
			VALUE = minor_unity_gain
			DESC = clan_unity_podesta.desc
			REVERSE_NON_HOUSE_TARGET = no
		}
	}

	auto_accept = yes

	# AI
	ai_potential = {
		culture = {
			has_cultural_parameter = can_make_republican_vassals
			has_cultural_era_or_later = culture_era_high_medieval
		}
		primary_title.tier > tier_county
	}
	
	ai_target_quick_trigger = {
		adult = yes
	}
	ai_targets = {
		ai_recipients = vassals
	}

	ai_frequency_by_tier = {
		barony = 0
		county = 0
		duchy = 240
		kingdom = 60
		empire = 60
		hegemony = 60
	}

	ai_will_do = {
		base = 100
	}
}
