﻿#Events related to the Marshal's tasks
#EK NOTE: added cap to prowess gain from an event
namespace = marshal_task


####
#0001-0999: Task outcome events
# by Petter Vilberg
####
# 0301 - New Commander
# 0302 - Commander learns Marshal's Commander Trait
# 0303 - Knight becomes Blademaster or gain more Prowess

####
#1000-1999: Side effects from Organize the Levies
# by Petter Vilberg
####
# 1001 - Levy Desertions
# 1002 - Unused Farmland
# 1003 - Insufficient Guards

# 1101 - Organization of Service
# 1102 - Increased Military Presence
# 1103 - Acclaimed Knight levy captaincy by Jason Cantalini

####
#2000-2999: Side effects from Train Commanders
# by Petter Vilberg
####
# 2001 - Wounded Commander
# 2002 - Severely Harmed Commander
# 2003 - Commander Killed

# 2101 - Commander Teaches Other Commander a Trait
# 2102 - Knight Improved
# 2103 - Acclaimed Knight inspires Martial Study by Jason Cantalini

####
#3000-3999: Side effects from Increase Control
# by Petter Vilberg
####
# 3001 - Reduce County Opinion
# 3002 - Loss of Control
# 3003 - Baron Opinion Loss

# 3101 - Increased County Opinion
# 3102 - Baron Opinion Gain
# 3103 - Acclaimed Knight Subdues Peasantry by Jason Cantalini

####
#4000-4999: Side effects from Manage Royal Guards
# by Claudia Baldassi
####
# 4001 - Knight is Wounded
# 4002 - Disorganized Royal Guards

# 4101 - Knight Improved
# 4102 - Hostile Scheme Disrupted



##################
# Task Outcome Events
# 0001-0999
##################

########
# Train Commander Events
# 0301-0399
########

#New Commander
# by petter Vilberg
marshal_task.0301 = {
	type = character_event
	title = marshal_task.0301.t
	desc = marshal_task.0301.desc

	theme = martial
	left_portrait = {
		character = scope:commander
		animation = ecstasy
	}
	right_portrait = {
		character = cp:councillor_marshal
		animation = personality_rational
	}

	trigger = {
		is_ai = no
		exists = cp:councillor_marshal
		scope:councillor = cp:councillor_marshal
		NOT = {
			has_variable = had_new_commander_event
		}
		OR = {
			exists = capital_province
			exists = location
		}

		OR = {
			number_of_knights < max_number_of_knights
			any_knight = {
				prowess < 8
			}
		}
	}

	weight_multiplier = {
		base = 1
	}

	immediate = {
		set_variable = {
			name = had_new_commander_event
			days = 730
		}
		random_sub_realm_county ?= {
			save_scope_as = origin
		}
		hidden_effect = {
			if = {
				limit = {
					exists = capital_province
				}
				capital_province = { save_scope_as = target_location }
			}
			else = {
				location = { save_scope_as = target_location }
			}
			if = { #making sure it works for landless
				limit = {
					NOT = { exists = scope:origin }
				}
				scope:target_location = {
					save_scope_as = origin
				}
			}
			create_character = {
				gender_female_chance = root_soldier_female_chance
				location = scope:target_location
				template = new_warrior_character
				faith = scope:origin.faith
				culture = scope:origin.culture
				save_scope_as = commander
			}
			add_visiting_courtier = scope:commander
		}
	}

	option = {
		name = marshal_task.0301.a
		add_courtier = scope:commander
		ai_chance = {
			base = 100
		}
	}

	option = {
		name = marshal_task.0301.b
		scope:commander = {
			select_and_move_to_pool_effect = yes
		}
		ai_chance = {
			base = 10
		}
	}
}


# Teach a Commander Marshal's Commander Trait
# by Petter Vilberg
scripted_trigger marshal_task_0302_commander_trigger = {
	number_of_commander_traits < commander_trait_limit
	NOT = { this = root.cp:councillor_marshal }
	NOT = { has_variable = commander_trait_being_taught }
}

marshal_task.0302 = {
	hidden = yes

	trigger = {
		exists = cp:councillor_marshal
		scope:councillor = cp:councillor_marshal
		cp:councillor_marshal = {
			councillor_positive_event_opinion_trigger = yes
			martial > mediocre_skill_rating
		}
		any_knight = {
			can_be_trained_as_knight_now_light_trigger = yes
			marshal_task_0302_commander_trigger = yes
		}
	}

	weight_multiplier = {
		base = 1
		compare_modifier = {
			target = scope:councillor
			value = martial
			multiplier = 0.2
			offset = inverted_mediocre_skill_rating
		}
	}

	immediate = {
		cp:councillor_marshal = {
			save_scope_as = active_councillor
			save_scope_as = teacher
		}
		every_knight = {
			limit = {
				can_be_trained_as_knight_now_light_trigger = yes
				marshal_task_0302_commander_trigger = yes
			}
			add_to_temporary_list = possible_commander
		}
		random_in_list = {
			list = possible_commander
			limit = {
				character_has_commander_trait_scope_does_not = scope:teacher
				has_education_martial_trigger = yes
			}
			alternative_limit = {
				has_education_martial_trigger = yes
			}
			alternative_limit = {
				always = yes
			}

			weight = {
				base = 5

				compare_modifier = {
					value = martial
				}
				compare_modifier = {
					value = prowess
					multiplier = 0.5
				}

				modifier = {
					add = 5
					number_of_commander_traits < 2
				}
				modifier = {
					add = 5
					number_of_commander_traits < 1
				}
			}
			save_scope_as = commander
		}

		random_list = {
			75 = {
				trigger = {
					teacher_has_trait_that_student_does_not_trigger = { 
						TEACHER = scope:teacher
						STUDENT = scope:commander
					}
				}
				select_ct_for_teacher_to_teach_student_effect = {
					TEACHER = scope:teacher
					STUDENT = scope:commander
				}
			}
			25 = {
				scope:commander = {
					discover_new_commander_trait_to_teach = yes
				}
			}
		}

		random_list = {
			10 = {
				compare_modifier = {
					value = scope:teacher.martial
					multiplier = 2
				}
				compare_modifier = {
					value = scope:commander.learning
					multiplier = 2
				}
				send_interface_message = {
					type = msg_marshal_task_good
					title = marshal_task.0302.notification.a
					desc = {
						triggered_desc = {
							trigger = {
								scope:active_councillor ?= { martial <= average_skill_level }
							}
							desc = task_martial_good_unskilled_notification_tooltip
						}
						triggered_desc = {
							trigger = {
								scope:active_councillor ?= { martial > average_skill_level }
							}
							desc = task_martial_good_skilled_notification_tooltip
						}
					}
					tooltip = task_train_commanders_notification_tooltip

					left_icon = scope:commander
					right_icon = scope:teacher

					scope:commander = {
						learn_commander_trait_success_effect = yes
						add_martial_skill = 1
					}
				}
			}
			90 = {
				send_interface_message = {
					type = msg_marshal_task_good
					title = marshal_task.0302.notification.b
					desc = {
						triggered_desc = {
							trigger = {
								scope:active_councillor ?= { martial <= average_skill_level }
							}
							desc = task_martial_good_unskilled_notification_tooltip
						}
						triggered_desc = {
							trigger = {
								scope:active_councillor ?= { martial > average_skill_level }
							}
							desc = task_martial_good_skilled_notification_tooltip
						}
					}
					tooltip = task_train_commanders_notification_tooltip

					left_icon = scope:commander
					right_icon = scope:active_councillor

					scope:commander = {
						# Did not succeed to learn the skill
						add_martial_skill = 1
					}
				}
			}
		}
		
		scope:commander = {
			if = {
				limit = {
					exists = var:commander_trait_being_taught
				}
				remove_variable = commander_trait_being_taught
			}
			if = {
				limit = {
					has_character_flag = being_taught_commander_trait
				}
				remove_character_flag = being_taught_commander_trait
			}
		}
	}
}

# Knight becomes Blademaster or gain more Prowess
marshal_task.0303 = {
	hidden = yes

	trigger = {
		exists = cp:councillor_marshal
		scope:councillor = cp:councillor_marshal
		cp:councillor_marshal = {
			councillor_positive_event_opinion_trigger = yes
			martial > mediocre_skill_rating
		}
		any_knight = {
			can_be_trained_as_knight_now_light_trigger = yes
			NOT = { this = root.cp:councillor_marshal }
			#EK EDIT: added cap to prowess gain for commanders - if they're already gained 4 prowess from this, they won't be picked again
			NAND = {
				has_variable = knight_training_prowess_cap
				var:knight_training_prowess_cap = 4
			}
			#EK EDIT END
		}
	}

	weight_multiplier = {
		base = 1
		compare_modifier = {
			target = scope:councillor
			value = martial
			multiplier = 0.2
			offset = inverted_mediocre_skill_rating
		}
	}

	immediate = {
		cp:councillor_marshal = {
			save_scope_as = active_councillor
		}
		every_knight = {
			limit = {
				can_be_trained_as_knight_now_light_trigger = yes
				NOT = { this = root.cp:councillor_marshal }
				#EK EDIT: added cap to prowess gain for commanders - if they're already gained 4 prowess from this, they won't be picked again
				NAND = {
					has_variable = knight_training_prowess_cap
					var:knight_training_prowess_cap = 4
				}
				#EK EDIT END
			}
			add_to_temporary_list = possible_knight
		}
		random_in_list = {
			list = possible_knight
			save_scope_as = commander
		}

		random_list = {
			2 = {
				trigger = {
					scope:commander = {
						NAND = {
							has_trait = lifestyle_blademaster
							has_trait_xp = {
								trait = lifestyle_blademaster
								value >= 100
							}
						}
					}
				}
				compare_modifier = {
					value = scope:active_councillor.martial
					multiplier = 2
				}
				compare_modifier = {
					value = scope:commander.learning
					multiplier = 2
				}
				modifier = {
					add = 2
					scope:active_councillor.prowess >= average_skill_level
				}
				modifier = {
					add = 3
					scope:active_councillor.prowess >= good_skill_level
				}
				send_interface_message = {
					type = msg_marshal_task_good
					title = marshal_task.0303.notification.c
					desc = {
						triggered_desc = {
							trigger = {
								scope:active_councillor ?= { martial <= average_skill_level }
							}
							desc = task_martial_good_unskilled_notification_tooltip
						}
						triggered_desc = {
							trigger = {
								scope:active_councillor ?= { martial > average_skill_level }
							}
							desc = task_martial_good_skilled_notification_tooltip
						}
					}
					tooltip = task_train_commanders_notification_tooltip

					left_icon = scope:commander
					right_icon = scope:active_councillor

					scope:commander = {
						blademaster_lifestyle_rank_up_effect = yes
					}
				}
			}
			10 = {
				compare_modifier = {
					value = scope:active_councillor.martial
					multiplier = 2
				}
				compare_modifier = {
					value = scope:commander.learning
					multiplier = 2
				}
				send_interface_message = {
					type = msg_marshal_task_good
					title = marshal_task.0303.notification.a
					desc = {
						triggered_desc = {
							trigger = {
								scope:active_councillor ?= { martial <= average_skill_level }
							}
							desc = task_martial_good_unskilled_notification_tooltip
						}
						triggered_desc = {
							trigger = {
								scope:active_councillor ?= { martial > average_skill_level }
							}
							desc = task_martial_good_skilled_notification_tooltip
						}
					}
					tooltip = task_train_commanders_notification_tooltip

					left_icon = scope:commander
					right_icon = scope:active_councillor

					scope:commander = {
						add_prowess_skill = 2
						#EK EDIT: capping prowess gain for commanders - based upon the confidants perk effect
						if = {
							limit = { NOT = { has_variable = knight_training_prowess_cap }} #check if they're triggered this effect before
							set_variable = {
								name = knight_training_prowess_cap
								value = 0
							}
						}	
						if = {
							limit = { exists = var:knight_training_prowess_cap }
							if = {
								limit = {
									exists = var:knight_training_prowess_cap
									var:knight_training_prowess_cap < 4 #capping it at 4 seems reasonable
								}
								change_variable = {
									name = knight_training_prowess_cap
									add = 1
								}
							}
						}
						#EK EDIT END
					}
				}
			}
			90 = {
				send_interface_message = {
					type = msg_marshal_task_good
					title = marshal_task.2102.notification
					desc = {
						triggered_desc = {
							trigger = {
								scope:active_councillor ?= { martial <= average_skill_level }
							}
							desc = task_martial_good_unskilled_notification_tooltip
						}
						triggered_desc = {
							trigger = {
								scope:active_councillor ?= { martial > average_skill_level }
							}
							desc = task_martial_good_skilled_notification_tooltip
						}
					}
					tooltip = task_train_commanders_notification_tooltip

					left_icon = scope:commander
					right_icon = scope:active_councillor

					scope:commander = {
						add_prowess_skill = 1
						#EK EDIT: capping prowess gain for commanders - based upon the confidants perk effect
						if = {
							limit = { NOT = { has_variable = knight_training_prowess_cap }} #check if they're triggered this effect before
							set_variable = {
								name = knight_training_prowess_cap
								value = 0
							}
						}	
						if = {
							limit = { exists = var:knight_training_prowess_cap }
							if = {
								limit = {
									exists = var:knight_training_prowess_cap
									var:knight_training_prowess_cap < 4 #capping it at 4 seems reasonable
								}
								change_variable = {
									name = knight_training_prowess_cap
									add = 1
								}
							}
						}
						#EK EDIT END
					}
				}
			}
		}
	}
}

# Is this character bad enough at martial/prowess that making them a commander would be a mistake?
scripted_trigger marshal_task_0304_is_valid_beneficiary_of_nepotism = {
	is_physically_able_ai_adult = yes
	prowess < mediocre_skill_rating
	martial <= mediocre_skill_rating
	NOR = {
		is_knight_of = scope:councillor_liege
	}
	# Only peeps who are your vassal or who aren't employed in an important court position elsewhere are valid targets
	OR = {
		is_vassal_of = scope:councillor_liege
		AND = {
			is_landed = no
			has_any_court_position = no
		}
	}
	can_be_knight_trigger = { ARMY_OWNER = scope:councillor_liege }
}

# Will this character ask you to employ their incompetent child as a knight?
scripted_trigger marshal_task_0304_is_valid_advocate_of_nepotism = {
	NOT = { this = root }
	is_physically_able_ai_adult = yes
	is_available = yes
	NOR = {
		this = root
		has_relation_rival = scope:councillor_liege
		has_relation_nemesis = scope:councillor_liege
	}
	ai_honor < high_positive_ai_value
	ai_energy >= low_negative_ai_value
}

scripted_trigger marshal_task_0304_is_corrupt_parent_of_weak_child = {
	marshal_task_0304_is_valid_advocate_of_nepotism = yes
	any_child = {
		marshal_task_0304_is_valid_beneficiary_of_nepotism = yes
	}
}

# Promote an incompetent house member's child to knighthood
marshal_task.0304 = {
	type = character_event
	title = marshal_task.0304.t
	desc = marshal_task.0304.desc
	theme = martial
	left_portrait = {
		character = scope:beneficiary_of_nepotism
		animation = marshal_random_weapon
	}
	right_portrait = {
		character = cp:councillor_marshal
		animation = interested_left
	}
	lower_left_portrait = scope:advocate_of_nepotism

	trigger = {
		exists = cp:councillor_marshal
		scope:councillor = cp:councillor_marshal
		NOT = {
			has_variable = had_new_commander_event
		}
		# Either you have a harmonious clan government (so you care about unity effects) or the parent is a vassal (so you care about their opinion)
		OR = {
			AND = {
				government_has_flag = government_is_clan
				exists = house
				house = {
					OR = {
						has_house_unity_stage = friendly
						has_house_unity_stage = harmonious
					}
					any_house_member = {
						marshal_task_0304_is_corrupt_parent_of_weak_child = yes
					}
				}
			}
			any_vassal = {
				marshal_task_0304_is_corrupt_parent_of_weak_child = yes
			}
		}
	}

	# Ambitious parents are more likely to try this scheme
	weight_multiplier = {
		base = 1
		modifier = {
			add = 0.5
			OR = {
				AND = {
					exists = house
					house = {
						any_house_member = {
							marshal_task_0304_is_corrupt_parent_of_weak_child = yes
							has_trait = ambitious
						}
					}
				}
				any_vassal = {
					marshal_task_0304_is_corrupt_parent_of_weak_child = yes
					has_trait = ambitious
				}
			}
		}
	}

	immediate = {
		set_variable = {
			name = had_new_commander_event
			days = 730
		}
		# Select who will appear in the event
		if = {
			limit = {
				exists = house
				government_has_flag = government_is_clan
				house = {
					OR = {
						has_house_unity_stage = friendly
						has_house_unity_stage = harmonious
					}
					any_house_member = {
						marshal_task_0304_is_corrupt_parent_of_weak_child = yes
					}
				}
			}
			house = {
				random_house_member = {
					limit = { 
						marshal_task_0304_is_corrupt_parent_of_weak_child = yes
					}
					weight = {
						base = 1
						modifier = {
							add = 0.5
							has_trait = ambitious
						}
						# Weight mothers more strongly
						modifier = {
							add = 0.5
							is_female = yes
						}
					}
					save_scope_as = advocate_of_nepotism
					random_child = {
						limit = {
							marshal_task_0304_is_valid_beneficiary_of_nepotism = yes
						}
						save_scope_as = beneficiary_of_nepotism
						get_quirk_character_effect = yes
					}
				}
			}
		}
		else = {
			random_vassal = {
				limit = {
					marshal_task_0304_is_corrupt_parent_of_weak_child = yes
				}
				weight = {
					base = 1
					modifier = {
						add = 0.5
						has_trait = ambitious
					}
					# Weight mothers more strongly
					modifier = {
						add = 0.5
						is_female = yes
					}
				}
				save_scope_as = advocate_of_nepotism
				random_child = {
					limit = {
						marshal_task_0304_is_valid_beneficiary_of_nepotism = yes
					}
					save_scope_as = beneficiary_of_nepotism
					get_quirk_character_effect = yes
				}
			}
		}
		# Add the incompetent potential commander as a guest if they aren't already at your court or landed
		if = {
			limit = {
				scope:beneficiary_of_nepotism = {
					NOR = {
						is_courtier_of = root
						location = root.capital_province
						is_landed = yes
					}
				}
			}
			add_visiting_courtier = scope:beneficiary_of_nepotism
		}
	}

	option = { # Accept the nepotism
		name = {
			trigger = { house = scope:beneficiary_of_nepotism.house }
			text = marshal_task.0304.a_clan
		}
		name = {
			trigger = { NOT = { house = scope:beneficiary_of_nepotism.house } }
			text = marshal_task.0304.a_neutral
		}
		if = {
			limit = {
				scope:beneficiary_of_nepotism = {
					is_landed = no
					NOT = { is_courtier_of = root }
				}
			}
			add_courtier = scope:beneficiary_of_nepotism
		}
		scope:beneficiary_of_nepotism = {
			hidden_effect = { set_knight_status = force }
			add_character_flag = {
				flag = marshal_task_0304_forced_knighthood
				years = 20
			}
		}
		custom_description_no_bullet = {
			text = marshal_task_0304_a_tt
			subject = scope:advocate_of_nepotism
			object = scope:beneficiary_of_nepotism
		}
		reverse_add_opinion = {
			target = scope:advocate_of_nepotism
			modifier = marshal_task_train_commanders_supported_my_child_opinion
		}
		# For the next 20 years, we'll check monthly to see if the child is still employed as a knight
		# But only if the councillor liege is a player (for performance reasons)
		if = {
			limit = { is_ai = no }
			trigger_event = {
				id = marshal_task.0305
				days = 14
			}
		}
		if = {
			limit = {
				government_has_flag = government_is_clan
				# Only add/lose unity if the parent is a part of your house
				house = scope:advocate_of_nepotism.house
			}
			add_clan_unity_interaction_effect = {
				CHARACTER = scope:councillor_liege
				TARGET = scope:advocate_of_nepotism
				VALUE = minor_unity_gain
				DESC = clan_unity_yes_to_nepotism.desc
				REVERSE_NON_HOUSE_TARGET = no
			}
		}
		ai_chance = {
			base = 50
			ai_value_modifier = {
				ai_honor = -0.25
			}
		}
		stress_impact = {
			arrogant = minor_stress_impact_gain
			paranoid = minor_stress_impact_gain
		}
	}

	option = { # Offer for their child to be your courtier instead
		name = marshal_task.0304.b
		trigger = {
			scope:beneficiary_of_nepotism = {
				NOR = {
					is_courtier_of = scope:councillor_liege
					is_landed = yes
				}
			}
		}
		duel = {
			skill = diplomacy
			target = scope:advocate_of_nepotism
			6 = {
				compare_modifier = {
					value = scope:duel_value
					multiplier = 0.5
					min = -5
				}
				desc = marshal_task.0304.b.success
				send_interface_toast = {
					type = event_toast_effect_good
					title = marshal_task.0304.b.success
					left_icon = scope:advocate_of_nepotism
					right_icon = scope:beneficiary_of_nepotism
					add_courtier = scope:beneficiary_of_nepotism
				}
			}
			4 = {
				compare_modifier = {
					value = scope:duel_value
					multiplier = -0.5
					min = -5
				}
				desc = marshal_task.0304.b.failure
				send_interface_toast = {
					type = event_toast_effect_bad
					title = marshal_task.0304.b.failure
					left_icon = scope:advocate_of_nepotism				
					reverse_add_opinion = {
						target = scope:advocate_of_nepotism
						modifier = marshal_task_train_commanders_rebuffed_my_child_opinion
						opinion = -20
					}
					if = {
						limit = {
							government_has_flag = government_is_clan
							house = scope:advocate_of_nepotism.house
						}
						add_clan_unity_interaction_effect = {
							CHARACTER = scope:councillor_liege
							TARGET = scope:advocate_of_nepotism
							VALUE = minor_unity_loss
							DESC = clan_unity_refused_nepotism.desc
							REVERSE_NON_HOUSE_TARGET = no
						}
					}
				}
			}
		}
		ai_chance = {
			base = 10
			compare_modifier = {
				value = diplomacy
				offset = scope:advocate_of_nepotism.diplomacy
				multiplier = 2.5
			}
		}
	}

	option = { # Deny the nepotism
		name = marshal_task.0304.c
		ai_chance = {
			base = 10
		}
		reverse_add_opinion = {
			target = scope:advocate_of_nepotism
			modifier = marshal_task_train_commanders_rebuffed_my_child_opinion
		}
		if = {
			limit = {
				government_has_flag = government_is_clan
				house = scope:advocate_of_nepotism.house 
			}
			add_clan_unity_interaction_effect = {
				CHARACTER = scope:councillor_liege
				TARGET = scope:advocate_of_nepotism
				VALUE = minor_unity_loss
				DESC = clan_unity_refused_nepotism.desc
				REVERSE_NON_HOUSE_TARGET = no
			}
		}
	}
}

marshal_task.0305 = {
	hidden = yes

	trigger = {
		scope:councillor_liege = {
			is_alive = yes
		}
		scope:advocate_of_nepotism = {
			is_alive = yes
		}
		scope:beneficiary_of_nepotism = {
			is_alive = yes
			is_healthy = yes
			has_character_flag = marshal_task_0304_forced_knighthood
			NOT = {
				has_opinion_modifier = {
					modifier = kicked_me_from_court
					target = scope:councillor_liege
				}
			}
		}
	}

	immediate = {
		if = {
			limit = {
				scope:beneficiary_of_nepotism = { is_knight_of = scope:councillor_liege }
			}
			trigger_event = {
				id = marshal_task.0305
				days = 30
			}
		}
		else = {
			trigger_event = marshal_task.306
		}
	}
}

# The worthless house member you promised to keep as a knight was removed from their post
marshal_task.0306 = {
	type = letter_event
	sender = scope:advocate_of_nepotism
	opening = marshal_task.0306.opening
	desc = marshal_task.0306.desc

	option = {
		name = marshal_task.0306.a
		stress_impact = {
			shy = medium_stress_impact_gain
		}
		scope:advocate_of_nepotism = {
			if = {
				limit = {
					has_opinion_modifier = {
						target = root
						modifier = marshal_task_train_commanders_supported_my_child_opinion	
					}
				}
				remove_opinion = {
					target = root
					modifier = marshal_task_train_commanders_supported_my_child_opinion
				}
			}
			add_opinion = {
				target = root
				modifier = weak_excuses
				opinion = -25
				years = 15
			}
		}
		scope:beneficiary_of_nepotism = {
			remove_character_flag = marshal_task_0304_forced_knighthood
		}
		if = {
			limit = {
				government_has_flag = government_is_clan
				house = scope:advocate_of_nepotism.house
			}
			add_clan_unity_interaction_effect = {
				CHARACTER = scope:councillor_liege
				TARGET = scope:advocate_of_nepotism
				VALUE = medium_unity_loss
				DESC = clan_unity_nepotism_removed_from_post.desc
				REVERSE_NON_HOUSE_TARGET = no
			}
		}
	}
}

##########
# END Task Outcome Events
##########


##################
# Side Effects from Organize the Levies
# 1001-1999
# by Petter Vilberg
##################

###
# Bad Side Effects
###

#Levy Desertions
# by Petter Vilberg
marshal_task.1001 = {
	hidden = yes

	trigger = {
		exists = cp:councillor_marshal
		scope:councillor = cp:councillor_marshal
		NOT = {
			has_character_modifier = marshal_task_levy_desertions_modifier
		}
		cp:councillor_marshal = {
			martial < high_skill_rating
		}
	}

	weight_multiplier = {
		base = 1
		compare_modifier = {
			target = scope:councillor
			value = martial
			multiplier = -0.2
			offset = inverted_high_skill_rating
		}
	}

	immediate = {
		cp:councillor_marshal = {
			save_scope_as = active_councillor
		}
		set_variable = {
			name = had_marshal_task_side_effect
			value = yes
			days = marshal_task_side_effect_cooldown
		}
		send_interface_message = {
			type = msg_marshal_task_bad
			title = marshal_task.1001.notification
			desc = {
				triggered_desc = {
					trigger = {
						scope:active_councillor ?= { martial <= average_skill_level }
					}
					desc = task_martial_bad_unskilled_notification_tooltip
				}
				triggered_desc = {
					trigger = {
						scope:active_councillor ?= { martial > average_skill_level }
					}
					desc = task_martial_bad_skilled_notification_tooltip
				}
			}
			tooltip = task_organize_levies_notification_tooltip

			left_icon = scope:councillor

			add_character_modifier = {
				modifier = marshal_task_levy_desertions_modifier
				days = marshal_task_modifier_duration
			}
		}
	}
}


#Unused Farmland
# by Petter Vilberg

marshal_task.1002 = {
	hidden = yes

	trigger = {
		exists = cp:councillor_marshal
		scope:councillor = cp:councillor_marshal
		any_sub_realm_county ?= {
			NOT = {
				has_county_modifier = marshal_task_unused_farmland_modifier
			}
		}
		cp:councillor_marshal = {
			martial < high_skill_rating
		}
	}

	weight_multiplier = {
		base = 1
		compare_modifier = {
			target = scope:councillor
			value = martial
			multiplier = -0.2
			offset = inverted_high_skill_rating
		}
	}

	immediate = {
		cp:councillor_marshal = {
			save_scope_as = active_councillor
		}
		set_variable = {
			name = had_marshal_task_side_effect
			value = yes
			days = marshal_task_side_effect_cooldown
		}
		random_sub_realm_county ?= {
			limit = {
				NOT = {
					has_county_modifier = marshal_task_unused_farmland_modifier
				}
			}
			save_scope_as = county
		}

		send_interface_message = {
			type = msg_marshal_task_bad
			title = marshal_task.1002.notification
			desc = {
				triggered_desc = {
					trigger = {
						scope:active_councillor ?= { martial <= average_skill_level }
					}
					desc = task_martial_bad_unskilled_notification_tooltip
				}
				triggered_desc = {
					trigger = {
						scope:active_councillor ?= { martial > average_skill_level }
					}
					desc = task_martial_bad_skilled_notification_tooltip
				}
			}
			tooltip = task_organize_levies_notification_tooltip

			left_icon = scope:councillor
			right_icon = scope:county

			scope:county ?= {
				add_county_modifier = {
					modifier = marshal_task_unused_farmland_modifier
					days = marshal_task_modifier_duration
				}
			}
		}
	}
}



#Insufficient Guards
# by Petter Vilberg
marshal_task.1003 = {
	hidden = yes

	trigger = {
		exists = cp:councillor_marshal
		scope:councillor = cp:councillor_marshal
		any_sub_realm_county ?= {
			marshal_task_1003_county_trigger = yes
			NOR = {
				title_province = { has_holding_type = nomad_holding }
				title_province = { has_holding_type = herder_holding }
			}
		}
		cp:councillor_marshal = {
			martial < high_skill_rating
		}
	}

	weight_multiplier = {
		base = 1
		compare_modifier = {
			target = scope:councillor
			value = martial
			multiplier = -0.2
			offset = inverted_high_skill_rating
		}
	}

	immediate = {
		cp:councillor_marshal = {
			save_scope_as = active_councillor
		}
		set_variable = {
			name = had_marshal_task_side_effect
			value = yes
			days = marshal_task_side_effect_cooldown
		}
		random_sub_realm_county ?= {
			limit = {
				marshal_task_1003_county_trigger = yes
			}
			save_scope_as = county
		}

		send_interface_message = {
			type = msg_marshal_task_bad
			title = marshal_task.1003.notification
			desc = {
				triggered_desc = {
					trigger = {
						scope:active_councillor ?= { martial <= average_skill_level }
					}
					desc = task_martial_bad_unskilled_notification_tooltip
				}
				triggered_desc = {
					trigger = {
						scope:active_councillor ?= { martial > average_skill_level }
					}
					desc = task_martial_bad_skilled_notification_tooltip
				}
			}
			tooltip = task_organize_levies_notification_tooltip

			left_icon = scope:councillor
			right_icon = scope:county

			scope:county ?= {
				random_list = {
					1 = {
						trigger = {
							NOT = { has_county_modifier = county_corruption_unsafe_highways_modifier }
						}
						add_county_modifier = {
							modifier = county_corruption_unsafe_highways_modifier
							years = 5
						}
					}
					1 = {
						trigger = {
							NOT = { has_county_modifier = county_corruption_thieves_guild_modifier }
						}
						add_county_modifier = {
							modifier = county_corruption_thieves_guild_modifier
							years = 5
						}
					}
					1 = {
						trigger = {
							NOT = { has_county_modifier = county_corruption_bandits_rampant_modifier }
						}
						add_county_modifier = {
							modifier = county_corruption_bandits_rampant_modifier
							years = 5
						}
					}
					#EK ADDITION: added our own crisis modifers
					1 = {
						ek_add_crisis_modifier_effect = yes
					}
					#EK ADDITION END
				}
			}
		}
	}
}




####
# Good Side Effects
####

#Organization of Service
# by Petter Vilberg
marshal_task.1101 = {
	hidden = yes

	trigger = {
		exists = cp:councillor_marshal
		scope:councillor = cp:councillor_marshal
		any_sub_realm_county ?= {
			marshal_task_1101_county_trigger = yes
		}
		cp:councillor_marshal = {
			councillor_positive_event_opinion_trigger = yes
			martial > mediocre_skill_rating
		}
	}

	weight_multiplier = {
		base = 1
		compare_modifier = {
			target = scope:councillor
			value = martial
			multiplier = 0.2
			offset = inverted_mediocre_skill_rating
		}
	}

	immediate = {
		cp:councillor_marshal = {
			save_scope_as = active_councillor
		}
		set_variable = {
			name = had_marshal_task_side_effect
			value = yes
			days = marshal_task_side_effect_cooldown
		}
		random_sub_realm_county ?= {
			limit = {
				marshal_task_1101_county_trigger = yes
			}
			save_scope_as = county
		}

		send_interface_message = {
			type = msg_marshal_task_good
			title = marshal_task.1101.notification
			desc = {
				triggered_desc = {
					trigger = {
						scope:active_councillor ?= { martial <= average_skill_level }
					}
					desc = task_martial_good_unskilled_notification_tooltip
				}
				triggered_desc = {
					trigger = {
						scope:active_councillor ?= { martial > average_skill_level }
					}
					desc = task_martial_good_skilled_notification_tooltip
				}
			}
			tooltip = task_organize_levies_notification_tooltip

			left_icon = scope:active_councillor
			right_icon = scope:county

			scope:county ?= {
				add_county_modifier = {
					modifier = marshal_task_organized_service_modifier
					days = marshal_task_modifier_duration
				}
			}
		}
	}
}

#Increased Military Presence
# by Petter Vilberg
marshal_task.1102 = {
	hidden = yes

	trigger = {
		exists = cp:councillor_marshal
		scope:councillor = cp:councillor_marshal
		any_sub_realm_county ?= {
			marshal_task_1102_county_trigger = yes
		}
		cp:councillor_marshal = {
			councillor_positive_event_opinion_trigger = yes
			martial > mediocre_skill_rating
		}
	}

	weight_multiplier = {
		base = 1
		compare_modifier = {
			target = scope:councillor
			value = martial
			multiplier = 0.2
			offset = inverted_mediocre_skill_rating
		}
	}

	immediate = {
		cp:councillor_marshal = {
			save_scope_as = active_councillor
		}
		set_variable = {
			name = had_marshal_task_side_effect
			value = yes
			days = marshal_task_side_effect_cooldown
		}
		random_sub_realm_county ?= {
			limit = {
				marshal_task_1102_county_trigger = yes
			}
			save_scope_as = county
		}

		send_interface_message = {
			type = msg_marshal_task_good
			title = marshal_task.1102.notification
			desc = {
				triggered_desc = {
					trigger = {
						scope:active_councillor ?= { martial <= average_skill_level }
					}
					desc = task_martial_good_unskilled_notification_tooltip
				}
				triggered_desc = {
					trigger = {
						scope:active_councillor ?= { martial > average_skill_level }
					}
					desc = task_martial_good_skilled_notification_tooltip
				}
			}
			tooltip = task_organize_levies_notification_tooltip

			left_icon = scope:active_councillor
			right_icon = scope:county

			scope:county ?= {
				add_county_modifier = {
					modifier = marshal_task_military_presence_modifier
					days = marshal_task_modifier_duration
				}
			}
		}
	}
}

#Acclaimed Knight Levy Captaincy
# by Jason Cantalini
marshal_task.1103 = {
	hidden = yes

	cooldown = { months = 12 }

	trigger = {
		exists = cp:councillor_marshal
		scope:councillor = cp:councillor_marshal
		any_active_accolade = {
			has_accolade_parameter = accolade_organize_army_bonus
		}
		NOT = {
			has_character_modifier = levy_captaincy_modifier
		}
	}

	weight_multiplier = {
		base = 1
		compare_modifier = {
			target = scope:acclaimed_tactician
			value = martial
			multiplier = 0.2
			offset = inverted_mediocre_skill_rating
		}
	}

	immediate = {
		random_knight = {
			limit = {
				is_acclaimed = yes
				accolade = {
					has_accolade_parameter = accolade_organize_army_bonus
				}
			}
			save_scope_as = acclaimed_tactician
		}
		set_variable = {
			name = had_marshal_task_side_effect
			value = yes
			days = marshal_task_side_effect_cooldown
		}

		send_interface_message = {
			type = msg_marshal_task_good
			title = marshal_task.1103.notification
			desc = task_martial_good_acclaimed_knight_notification_tooltip
			tooltip = task_organize_levies_notification_tooltip

			right_icon = scope:acclaimed_tactician

			add_character_modifier = {
				modifier = levy_captaincy_modifier
				years = 5
			}
		}
	}
}

###############
# Side effects from Train Commanders
# 2000-2999
###############

####
# Bad Side Effects
####

#Commander is Wounded
# by Petter Vilberg
scripted_trigger marshal_task_2001_commander_trigger = {
	NOT = { this = root.cp:councillor_marshal }
	NOT = { has_trait = wounded }
}

marshal_task.2001 = {
	hidden = yes

	trigger = {
		exists = cp:councillor_marshal
		scope:councillor = cp:councillor_marshal
		cp:councillor_marshal = {
			martial < high_skill_rating
		}
		any_knight = {
			marshal_task_2001_commander_trigger = yes
			can_be_trained_as_knight_now_light_trigger = yes
		}
	}

	weight_multiplier = {
		base = 1
		compare_modifier = {
			target = scope:councillor
			value = martial
			multiplier = -0.2
			offset = inverted_high_skill_rating
		}
	}

	immediate = {
		cp:councillor_marshal = {
			save_scope_as = active_councillor
		}
		set_variable = {
			name = had_marshal_task_side_effect
			value = yes
			days = marshal_task_side_effect_cooldown
		}
		every_knight = {
			limit = {
				can_be_trained_as_knight_now_light_trigger = yes
			}
			add_to_list = valid_commander_list
		}
		random_in_list = {
			list = valid_commander_list
			limit = {
				marshal_task_2001_commander_trigger = yes
			}
			weight = {
				base = 2
				compare_modifier = {
					value = prowess
					multiplier = -0.1
				}
			}
			save_scope_as = commander
		}

		send_interface_message = {
			type = msg_marshal_task_bad
			title = marshal_task.2001.notification
			desc = {
				triggered_desc = {
					trigger = {
						scope:active_councillor ?= { martial <= average_skill_level }
					}
					desc = task_martial_bad_unskilled_notification_tooltip
				}
				triggered_desc = {
					trigger = {
						scope:active_councillor ?= { martial > average_skill_level }
					}
					desc = task_martial_bad_skilled_notification_tooltip
				}
			}
			tooltip = task_train_commanders_notification_tooltip

			left_icon = scope:commander
			right_icon = scope:active_councillor

			scope:commander = {
				increase_wounds_effect = {
					REASON = duel
				}
			}
		}
	}
}

#Commander is Severely Harmed
# by Petter Vilberg
scripted_trigger marshal_task_2002_commander_trigger = {
	OR = {
		is_landed = no
		highest_held_title_tier = tier_barony
	}
	NOT = { this = root.cp:councillor_marshal }
	NOR = {
		has_trait = wounded
		has_trait = maimed
	}
}

marshal_task.2002 = {
	hidden = yes

	trigger = {
		exists = cp:councillor_marshal
		scope:councillor = cp:councillor_marshal
		cp:councillor_marshal = {
			martial < high_skill_rating
		}
		OR = {
			any_knight = {
				marshal_task_2002_commander_trigger = yes
				can_be_trained_as_knight_now_light_trigger = yes
			}
		}
	}

	weight_multiplier = {
		base = 1
		compare_modifier = {
			target = scope:councillor
			value = martial
			multiplier = -0.2
			offset = inverted_high_skill_rating
		}
	}

	immediate = {
		cp:councillor_marshal = {
			save_scope_as = active_councillor
		}
		set_variable = {
			name = had_marshal_task_side_effect
			value = yes
			days = marshal_task_side_effect_cooldown
		}
		every_knight = {
			limit = {
				can_be_trained_as_knight_now_light_trigger = yes
			}
			add_to_list = valid_commander_list
		}
		random_in_list = {
			list = valid_commander_list
			limit = {
				marshal_task_2002_commander_trigger = yes
			}
			weight = {
				base = 2
				compare_modifier = {
					value = prowess
					multiplier = -0.1
				}
			}
			save_scope_as = commander
		}
		random_list = {
			1 = {
				send_interface_message = {
					type = msg_marshal_task_bad
					title = marshal_task.2002.generic_title
					desc = {
						triggered_desc = {
							trigger = {
								scope:active_councillor ?= { martial <= average_skill_level }
							}
							desc = task_martial_bad_unskilled_notification_tooltip
						}
						triggered_desc = {
							trigger = {
								scope:active_councillor ?= { martial > average_skill_level }
							}
							desc = task_martial_bad_skilled_notification_tooltip
						}
					}
					tooltip = task_train_commanders_notification_tooltip

					right_icon = scope:commander
					left_icon = scope:active_councillor

					scope:commander = {
						increase_wounds_effect = {
							REASON = duel
						}
						add_trait = one_eyed
					}
				}
				#save_scope_value_as = {
				#	name = injury
				#	value = flag:eye
				#}
			}
			3 = {
				send_interface_message = {
					type = msg_marshal_task_bad
					title = marshal_task.2002.generic_title
					desc = {
						triggered_desc = {
							trigger = {
								scope:active_councillor ?= { martial <= average_skill_level }
							}
							desc = task_martial_bad_unskilled_notification_tooltip
						}
						triggered_desc = {
							trigger = {
								scope:active_councillor ?= { martial > average_skill_level }
							}
							desc = task_martial_bad_skilled_notification_tooltip
						}
					}
					tooltip = task_train_commanders_notification_tooltip

					right_icon = scope:commander
					left_icon = scope:active_councillor

					scope:commander = {
						increase_wounds_effect = {
							REASON = duel
						}
						add_trait = one_legged
					}
				}
				#save_scope_value_as = {
				#	name = injury
				#	value = flag:leg
				#}
			}
			3 = {
				send_interface_message = {
					type = msg_marshal_task_bad
					title = marshal_task.2002.generic_title
					desc = {
						triggered_desc = {
							trigger = {
								scope:active_councillor ?= { martial <= average_skill_level }
							}
							desc = task_martial_bad_unskilled_notification_tooltip
						}
						triggered_desc = {
							trigger = {
								scope:active_councillor ?= { martial > average_skill_level }
							}
							desc = task_martial_bad_skilled_notification_tooltip
						}
					}
					tooltip = task_train_commanders_notification_tooltip

					right_icon = scope:commander
					left_icon = scope:active_councillor

					scope:commander = {
						increase_wounds_effect = {
							REASON = duel
						}
						apply_maimed_trait_and_modifier_effect = yes
					}
				}
				#save_scope_value_as = {
				#	name = injury
				#	value = flag:hand
				#}
			}
		}
	}

}

#Commander is Killed
# by Petter Vilberg
scripted_trigger marshal_task_2003_commander_trigger = {
	OR = {
		is_landed = no
		highest_held_title_tier = tier_barony
	}
	NOT = { this = root.cp:councillor_marshal }
	NOT = { has_trait = wounded }
}

marshal_task.2003 = {
	type = character_event
	title = marshal_task.2003.t
	desc = {
		desc = marshal_task.2001.opening
		desc = marshal_task.2003.ending
	}
	left_portrait = {
		character = cp:councillor_marshal
		animation = shock
	}
	right_portrait = {
		character = scope:commander
		animation = beg
	}
	theme = martial

	trigger = {
		exists = cp:councillor_marshal
		scope:councillor = cp:councillor_marshal
		cp:councillor_marshal = {
			martial < high_skill_rating
		}
		OR = {
			any_knight = {
				marshal_task_2003_commander_trigger = yes
				can_be_trained_as_knight_now_light_trigger = yes
			}
		}
	}

	weight_multiplier = {
		base = 1
		compare_modifier = {
			target = scope:councillor
			value = martial
			multiplier = -0.2
			offset = inverted_high_skill_rating
		}
	}

	immediate = {
		cp:councillor_marshal = {
			save_scope_as = active_councillor
		}
		set_variable = {
			name = had_marshal_task_side_effect
			value = yes
			days = marshal_task_side_effect_cooldown
		}
		every_knight = {
			limit = {
				can_be_trained_as_knight_now_light_trigger = yes
			}
			add_to_list = valid_commander_list
		}
		random_in_list = {
			list = valid_commander_list
			limit = {
				marshal_task_2003_commander_trigger = yes
			}
			weight = {
				base = 2
				compare_modifier = {
					value = prowess
					multiplier = -0.1
				}
			}
			save_scope_as = commander
		}
	}

	option = {
		name = marshal_task.2003.a
		scope:commander = {
			death = {
				death_reason = death_duel
			}
		}
	}
}


####
# Good Side Effects
####

#Commander may teach another Commander a trait
# by Petter Vilberg

scripted_trigger marshal_task_2101_teacher_trigger = {
	number_of_commander_traits > 0
	NOT = { this = root.cp:councillor_marshal }
	NOT = { has_variable = commander_trait_being_taught }
}

scripted_trigger marshal_task_2101_commander_trigger = {
	number_of_commander_traits < commander_trait_limit
	NOT = { this = root.cp:councillor_marshal }
	NOT = { has_variable = commander_trait_being_taught }
	character_has_commander_trait_scope_does_not = scope:teacher
}

marshal_task.2101 = {
	hidden = yes

	trigger = {
		exists = cp:councillor_marshal
		scope:councillor = cp:councillor_marshal
		cp:councillor_marshal = {
			councillor_positive_event_opinion_trigger = yes
			martial > mediocre_skill_rating
		}

		#First, add every potential commander to a trigger list
		any_knight = {
			can_be_trained_as_knight_now_light_trigger = yes
			marshal_task_2101_teacher_trigger = yes
			count >= 2
			add_to_temporary_list = possible_commanders
		}

		#Find two candidates
		any_in_list = {
			list = possible_commanders
			save_temporary_scope_as = teacher
		}
		any_in_list = {
			list = possible_commanders
			marshal_task_2101_commander_trigger = yes #This should ensure there is another possible candidate that is not also the teacher
		}
	}

	weight_multiplier = {
		base = 1
		compare_modifier = {
			target = scope:councillor
			value = martial
			multiplier = 0.2
			offset = inverted_mediocre_skill_rating
		}
	}

	immediate = {
		cp:councillor_marshal = {
			save_scope_as = active_councillor
		}
		set_variable = {
			name = had_marshal_task_side_effect
			value = yes
			days = marshal_task_side_effect_cooldown
		}

		#Build a list of possible characters involved
		every_knight = {
			limit = {
				can_be_trained_as_knight_now_light_trigger = yes
			}
			add_to_list = possible_commanders
		}

		#Pick a teacher
		random_in_list = {
			list = possible_commanders
			limit = {
				marshal_task_2101_teacher_trigger = yes
				#Check that there is still a valid commander to learn from teacher:
				save_temporary_scope_as = teacher
				any_in_list = {
					list = possible_commanders				
					NOT = { this = scope:teacher }
					marshal_task_2101_commander_trigger = yes
				}
			}
			save_scope_as = teacher
		}
		#Find a commander to learn
		random_in_list = {
			list = possible_commanders
			limit = {
				marshal_task_2101_commander_trigger = yes
			}
			save_scope_as = commander
		}

		#Select a trait to be taught
		select_ct_for_teacher_to_teach_student_effect = {
			TEACHER = scope:teacher
			STUDENT = scope:commander
		}

		send_interface_message = {
			type = msg_marshal_task_good
			title = marshal_task.2101.notification
			desc = {
				triggered_desc = {
					trigger = {
						scope:active_councillor ?= { martial <= average_skill_level }
					}
					desc = task_martial_good_unskilled_notification_tooltip
				}
				triggered_desc = {
					trigger = {
						scope:active_councillor ?= { martial > average_skill_level }
					}
					desc = task_martial_good_skilled_notification_tooltip
				}
			}
			tooltip = task_train_commanders_notification_tooltip

			left_icon = scope:commander
			right_icon = scope:teacher

			scope:commander = {
				learn_commander_trait_success_effect = yes
			}
		}
		
		scope:commander = {
			if = {
				limit = {
					exists = var:commander_trait_being_taught
				}
				remove_variable = commander_trait_being_taught
			}
			if = {
				limit = {
					has_character_flag = being_taught_commander_trait
				}
				remove_character_flag = being_taught_commander_trait
			}
		}
	}
}

# 2101 - Knight Improved
marshal_task.2102 = {
	hidden = yes

	trigger = {
		exists = cp:councillor_marshal
		scope:councillor = cp:councillor_marshal
		cp:councillor_marshal = {
			councillor_positive_event_opinion_trigger = yes
			martial > mediocre_skill_rating
		}

		any_knight = {
			can_be_trained_as_knight_now_light_trigger = yes
			#EK EDIT: added cap to prowess gain for commanders - if they're already gained 4 prowess from this, they won't be picked again
			NAND = {
				has_variable = knight_training_prowess_cap
				var:knight_training_prowess_cap = 4
			}
			#EK EDIT END
			add_to_temporary_list = possible_commanders
		}

		any_in_list = {
			list = possible_commanders
			NOT = { this = root.cp:councillor_marshal }
		}
	}

	weight_multiplier = {
		base = 1
		compare_modifier = {
			target = scope:councillor
			value = martial
			multiplier = 0.2
			offset = inverted_mediocre_skill_rating
		}
	}

	immediate = {
		cp:councillor_marshal = {
			save_scope_as = active_councillor
		}
		set_variable = {
			name = had_marshal_task_side_effect
			value = yes
			days = marshal_task_side_effect_cooldown
		}

		#Build a list of possible characters involved
		every_knight = {
			limit = {
				can_be_trained_as_knight_now_light_trigger = yes
				#EK EDIT: added cap to prowess gain for commanders - if they're already gained 4 prowess from this, they won't be picked again
				NAND = {
					has_variable = knight_training_prowess_cap
					var:knight_training_prowess_cap = 4
				}
				#EK EDIT END
			}
			add_to_list = possible_knights
		}

		#Find a knight to improve
		random_in_list = {
			list = possible_knights
			limit = {
				NOT = { this = root.cp:councillor_marshal }
			}
			save_scope_as = commander
		}

		send_interface_message = {
			type = msg_marshal_task_good
			title = marshal_task.2102.notification
			desc = {
				triggered_desc = {
					trigger = {
						scope:active_councillor ?= { martial <= average_skill_level }
					}
					desc = task_martial_good_unskilled_notification_tooltip
				}
				triggered_desc = {
					trigger = {
						scope:active_councillor ?= { martial > average_skill_level }
					}
					desc = task_martial_good_skilled_notification_tooltip
				}
			}
			tooltip = task_train_commanders_notification_tooltip

			left_icon = scope:commander
			right_icon = scope:active_councillor

			scope:commander = {
				add_prowess_skill = 1
				#EK EDIT: capping prowess gain for commanders - based upon the confidants perk effect
				if = {
					limit = { NOT = { has_variable = knight_training_prowess_cap }} #check if they're triggered this effect before
					set_variable = {
						name = knight_training_prowess_cap
						value = 0
					}
				}	
				if = {
					limit = { exists = var:knight_training_prowess_cap }
					if = {
						limit = {
							exists = var:knight_training_prowess_cap
							var:knight_training_prowess_cap < 4 #capping it at 4 seems reasonable
						}
						change_variable = {
							name = knight_training_prowess_cap
							add = 1
						}
					}
				}
				#EK EDIT END
			}
		}
	}
}

# 2103 - Acclaimed Knight drills elites in tourney skills
# By Jason Cantalini
scripted_trigger marshal_task_2103_accolade_trigger = {
	is_acclaimed = yes
	accolade = {
		OR = {
			has_accolade_parameter = accolade_train_commanders_bonus
			has_accolade_parameter = accolade_train_commanders_bonus_high
		}
	}
}

marshal_task.2103 = {
	hidden = yes

	cooldown = { months = 12 }

	trigger = {
		exists = cp:councillor_marshal
		scope:councillor = cp:councillor_marshal
		# check acclaimed knight is good to go
		any_knight = {
			marshal_task_2103_accolade_trigger = yes
			save_temporary_scope_as = mentor
		}

		# check for a knight to train
		any_knight = {
			NOT = { scope:mentor ?= this }
			can_be_knight_now_light_trigger = yes
		}
	}

	weight_multiplier = {
		base = 1
		compare_modifier = {
			target = scope:councillor
			value = martial
			multiplier = 0.2
			offset = inverted_mediocre_skill_rating
		}
	}

	immediate = {
		#save accolade knight
		random_knight = {
			limit = {
				marshal_task_2103_accolade_trigger = yes
			}
			save_scope_as = acclaimed_mentor
		}
		set_variable = {
			name = had_marshal_task_side_effect
			value = yes
			days = marshal_task_side_effect_cooldown
		}

		#Build a list of possible trained knights
		every_knight = {
			limit = {
				can_be_knight_now_light_trigger = yes
				NOT = { this = scope:acclaimed_mentor }
			}
			add_to_temporary_list = possible_trained_knights
		}

		#Choose someone to train
		random_in_list = {
			list = possible_trained_knights
			save_scope_as = trained_knight
		}

		#Choose the track
		random_list = {
			10 = {
				modifier = { 
					add = 20
					scope:trained_knight = {
						has_trait_xp = {
							trait = tourney_participant
							track = foot
							value >= 30
						}
					}
				}
				save_scope_value_as = { name = track value = flag:foot }
			}
			10 = {
				modifier = { 
					add = 20
					scope:trained_knight = {
						has_trait_xp = {
							trait = tourney_participant
							track = horse
							value >= 30
						}
					}
				}
				save_scope_value_as = { name = track value = flag:horse }
			}
			10 = {
				modifier = { 
					add = 20
					scope:trained_knight = {
						has_trait_xp = {
							trait = tourney_participant
							track = bow
							value >= 30
						}
					}
				}
				save_scope_value_as = { name = track value = flag:bow }
			}
			10 = {
				modifier = { 
					add = 20
					scope:trained_knight = {
						has_trait_xp = {
							trait = tourney_participant
							track = wit
							value >= 30
						}
					}
				}
				save_scope_value_as = { name = track value = flag:wit }
			}
		}

		send_interface_message = {
			type = msg_marshal_task_good
			title = marshal_task.2103.notification
			desc = task_martial_good_acclaimed_knight_notification_tooltip
			tooltip = task_train_commanders_notification_tooltip
		
			left_icon = scope:trained_knight
			right_icon = scope:acclaimed_mentor
			scope:trained_knight = {
				if = {
					limit = {
						NOT = { has_trait = tourney_participant }
					}
					add_trait = tourney_participant
				}
				if = {
					limit = {
						scope:acclaimed_mentor = {
							accolade = {
								has_accolade_parameter = accolade_train_commanders_bonus
							}
						}
					}
					scope:trained_knight = {
						switch = {
							trigger = scope:track
							flag:foot = {
								add_trait_xp = {
									trait = tourney_participant
									track = foot
									value = tournament_hastiludes_xp_gain_medium_value
								}
							}
							flag:bow = {
								add_trait_xp = {
									trait = tourney_participant
									track = bow
									value = tournament_hastiludes_xp_gain_medium_value
								}
							}
							flag:wit = {
								add_trait_xp = {
									trait = tourney_participant
									track = wit
									value = tournament_hastiludes_xp_gain_medium_value
								}
							}
							flag:horse = {
								add_trait_xp = {
									trait = tourney_participant
									track = horse
									value = tournament_hastiludes_xp_gain_medium_value
								}
							}
						}
					}
				}
				else_if = {
					limit = {
						scope:acclaimed_mentor = {
							accolade = {
								has_accolade_parameter = accolade_train_commanders_bonus_high
							}
						}
					}
					scope:trained_knight = {
						switch = {
							trigger = scope:track
							flag:foot = {
								add_trait_xp = {
									trait = tourney_participant
									track = foot
									value = tournament_hastiludes_xp_gain_medium_value
								}
							}
							flag:bow = {
								add_trait_xp = {
									trait = tourney_participant
									track = bow
									value = tournament_hastiludes_xp_gain_medium_value
								}
							}
							flag:wit = {
								add_trait_xp = {
									trait = tourney_participant
									track = wit
									value = tournament_hastiludes_xp_gain_medium_value
								}
							}
							flag:horse = {
								add_trait_xp = {
									trait = tourney_participant
									track = horse
									value = tournament_hastiludes_xp_gain_medium_value
								}
							}
						}
					}
				}
			}
		}
	}
}



##################
# Side Effects from Increase Control
# 3001-3999
##################

###
#Bad Side Effects
###

#Reduce County Opinion
# by Petter Vilberg
marshal_task.3001 = {
	hidden = yes

	trigger = {
		scope:councillor = {
			OR = {
				has_council_position = councillor_marshal
				is_kurultai_trigger = yes
			}
			martial < high_skill_rating
		}
		scope:county ?= {
			NOR = {
				has_county_modifier = marshal_task_increase_control_decreased_opinion_modifier
				has_county_modifier = marshal_task_increase_control_increased_opinion_modifier
			}
		}
	}

	weight_multiplier = {
		base = 1
		compare_modifier = {
			target = scope:councillor
			value = martial
			multiplier = -0.2
			offset = inverted_high_skill_rating
		}
	}

	immediate = {
		scope:councillor ?= {
			save_scope_as = active_councillor
		}
		set_variable = {
			name = had_marshal_task_side_effect
			value = yes
			days = marshal_task_side_effect_cooldown
		}
		send_interface_message = {
			type = msg_marshal_task_bad
			title = marshal_task.3001.notification
			desc = {
				triggered_desc = {
					trigger = {
						scope:active_councillor ?= { martial <= average_skill_level }
					}
					desc = task_martial_bad_unskilled_notification_tooltip
				}
				triggered_desc = {
					trigger = {
						scope:active_councillor ?= { martial > average_skill_level }
					}
					desc = task_martial_bad_skilled_notification_tooltip
				}
			}
			tooltip = task_increase_control_notification_tooltip

			left_icon = scope:active_councillor
			right_icon = scope:county

			scope:county ?= {
				add_county_modifier = {
					modifier = marshal_task_increase_control_decreased_opinion_modifier
					years = 5
				}
			}
		}
	}

}


#Loss of Control
# by Petter Vilberg
marshal_task.3002 = {
	hidden = yes

	trigger = {
		scope:councillor = {
			OR = {
				has_council_position = councillor_marshal
				is_kurultai_trigger = yes
			}
			martial < high_skill_rating
		}
		scope:county ?= {
			county_control > medium_county_control
		}
	}

	weight_multiplier = {
		base = 1
		compare_modifier = {
			target = scope:councillor
			value = martial
			multiplier = -0.2
			offset = inverted_high_skill_rating
		}
	}

	immediate = {
		scope:councillor ?= {
			save_scope_as = active_councillor
		}
		set_variable = {
			name = had_marshal_task_side_effect
			value = yes
			days = marshal_task_side_effect_cooldown
		}

		send_interface_message = {
			type = msg_marshal_task_bad
			title = marshal_task.3002.notification
			desc = {
				triggered_desc = {
					trigger = {
						scope:active_councillor ?= { martial <= average_skill_level }
					}
					desc = task_martial_bad_unskilled_notification_tooltip
				}
				triggered_desc = {
					trigger = {
						scope:active_councillor ?= { martial > average_skill_level }
					}
					desc = task_martial_bad_skilled_notification_tooltip
				}
			}
			tooltip = task_increase_control_notification_tooltip

			left_icon = scope:active_councillor
			right_icon = scope:county

			scope:county ?= {
				change_county_control = medium_county_control_loss
			}
		}
	}
}


#Baron opinion loss
# by Petter Vilberg

scripted_trigger marshal_task_3003_baron_trigger = {
	NOR = {
		has_opinion_modifier = {
			target = root
			modifier = marshal_task_increase_control_baron_increase_opinion
		}
		has_opinion_modifier = {
			target = root
			modifier = marshal_task_increase_control_baron_decrease_opinion
		}
	}
	NOT = {
		this = scope:councillor
	}
}

scripted_trigger marshal_task_3003_county_trigger = {
	any_in_de_facto_hierarchy = {
		tier = tier_barony
		exists = holder
		NOT = { holder = ROOT }
		holder = {
			marshal_task_3003_baron_trigger = yes
		}
	}
}

marshal_task.3003 = {
	hidden = yes

	trigger = {
		scope:councillor = {
			OR = {
				has_council_position = councillor_marshal
				is_kurultai_trigger = yes
			}
			martial < high_skill_rating
		}
		scope:county ?= {
			marshal_task_3003_county_trigger = yes
		}
	}

	weight_multiplier = {
		base = 1
		compare_modifier = {
			target = scope:councillor
			value = martial
			multiplier = -0.2
			offset = inverted_high_skill_rating
		}
	}

	immediate = {
		cp:councillor_marshal = {
			save_scope_as = active_councillor
		}
		set_variable = {
			name = had_marshal_task_side_effect
			value = yes
			days = marshal_task_side_effect_cooldown
		}
		scope:county ?= {
			random_in_de_facto_hierarchy = {
				limit = {
					tier = tier_barony
					exists = holder
					NOT = { holder = ROOT }
					holder = {
						marshal_task_3003_baron_trigger = yes
					}
				}
				holder = {
					save_scope_as = baron
				}
			}
		}

		send_interface_message = {
			type = msg_marshal_task_bad
			title = marshal_task.3003.notification
			desc = {
				triggered_desc = {
					trigger = {
						scope:active_councillor ?= { martial <= average_skill_level }
					}
					desc = task_martial_good_unskilled_notification_tooltip
				}
				triggered_desc = {
					trigger = {
						scope:active_councillor ?= { martial > average_skill_level }
					}
					desc = task_martial_good_skilled_notification_tooltip
				}
			}
			tooltip = task_increase_control_notification_tooltip

			left_icon = scope:active_councillor
			right_icon = scope:baron

			add_prestige = minor_prestige_loss
			scope:baron = {
				add_opinion = {
					target = root
					modifier = marshal_task_increase_control_baron_decrease_opinion
					days = marshal_task_modifier_duration
				}
			}
		}
	}

}


####
# Good Side Effects
####

#Increased County Opinion
# by Petter Vilberg
marshal_task.3101 = {
	hidden = yes

	trigger = {
		scope:councillor = {
			OR = {
				has_council_position = councillor_marshal
				is_kurultai_trigger = yes
			}
			councillor_positive_event_opinion_trigger = yes
			martial > mediocre_skill_rating
		}
		scope:county ?= {
			NOR = {
				has_county_modifier = marshal_task_increase_control_decreased_opinion_modifier
				has_county_modifier = marshal_task_increase_control_increased_opinion_modifier
			}
		}
	}

	weight_multiplier = {
		base = 1
		compare_modifier = {
			target = scope:councillor
			value = martial
			multiplier = 0.2
			offset = inverted_mediocre_skill_rating
		}
	}

	immediate = {
		scope:councillor ?= {
			save_scope_as = active_councillor
		}
		set_variable = {
			name = had_marshal_task_side_effect
			value = yes
			days = marshal_task_side_effect_cooldown
		}

		send_interface_message = {
			type = msg_marshal_task_good
			title = marshal_task.3101.notification
			desc = {
				triggered_desc = {
					trigger = {
						scope:active_councillor ?= { martial <= average_skill_level }
					}
					desc = task_martial_good_unskilled_notification_tooltip
				}
				triggered_desc = {
					trigger = {
						scope:active_councillor ?= { martial > average_skill_level }
					}
					desc = task_martial_good_skilled_notification_tooltip
				}
			}
			tooltip = task_increase_control_notification_tooltip

			left_icon = scope:active_councillor
			right_icon = scope:county

			scope:county ?= {
				add_county_modifier = {
					modifier = marshal_task_increase_control_increased_opinion_modifier
					years = 5
				}
			}
		}
	}
}

#Baron opinion gain
# by Petter Vilberg

marshal_task.3102 = {
	hidden = yes

	trigger = {
		scope:councillor = {
			OR = {
				has_council_position = councillor_marshal
				is_kurultai_trigger = yes
			}
			councillor_positive_event_opinion_trigger = yes
			martial > mediocre_skill_rating
		}
		scope:county ?= {
			marshal_task_3003_county_trigger = yes
		}
	}

	weight_multiplier = {
		base = 1
		compare_modifier = {
			target = scope:councillor
			value = martial
			multiplier = 0.2
			offset = inverted_mediocre_skill_rating
		}
	}

	immediate = {
		scope:councillor ?= {
			save_scope_as = active_councillor
		}
		set_variable = {
			name = had_marshal_task_side_effect
			value = yes
			days = marshal_task_side_effect_cooldown
		}
		scope:county ?= {
			random_in_de_facto_hierarchy = {
				limit = {
					tier = tier_barony
					exists = holder
					NOT = { holder = ROOT }
					holder = {
						marshal_task_3003_baron_trigger = yes
					}
				}
				holder = {
					save_scope_as = baron
				}
			}
		}

		send_interface_message = {
			type = msg_marshal_task_good
			title = marshal_task.3102.notification
			desc = {
				triggered_desc = {
					trigger = {
						scope:active_councillor ?= { martial <= average_skill_level }
					}
					desc = task_martial_good_unskilled_notification_tooltip
				}
				triggered_desc = {
					trigger = {
						scope:active_councillor ?= { martial > average_skill_level }
					}
					desc = task_martial_good_skilled_notification_tooltip
				}
			}
			tooltip = task_increase_control_notification_tooltip

			left_icon = scope:active_councillor
			right_icon = scope:baron

			scope:baron = {
				add_opinion = {
					target = root
					modifier = marshal_task_increase_control_baron_increase_opinion
					days = marshal_task_modifier_duration
				}
			}
			add_prestige = minor_prestige_gain
		}
	}
}

#Acclaimed Knight Subdues Peasantry
# by Jason Cantalini
marshal_task.3103 = {
	hidden = yes

	cooldown = { months = 12 }

	trigger = {
		scope:councillor = {
			OR = {
				has_council_position = councillor_marshal
				is_kurultai_trigger = yes
			}
			martial < high_skill_rating
		}
		any_active_accolade = {
			has_accolade_parameter = accolade_increase_control_bonus
		}
		scope:county ?= {
			county_control < 95
		}
	}

	weight_multiplier = {
		base = 1
	}

	immediate = {
		scope:councillor ?= {
			save_scope_as = active_councillor
		}
		random_knight = {
			limit = {
				is_acclaimed = yes
				accolade = {
					has_accolade_parameter = accolade_increase_control_bonus
				}
			}
			save_scope_as = acclaimed_reeve
		}
		set_variable = {
			name = had_marshal_task_side_effect
			value = yes
			days = marshal_task_side_effect_cooldown
		}

		send_interface_message = {
			type = msg_marshal_task_good
			title = marshal_task.3103.notification
			desc = task_martial_good_acclaimed_knight_notification_tooltip
			tooltip = task_increase_control_notification_tooltip

			left_icon = scope:acclaimed_reeve
			right_icon = scope:county

			scope:county ?= {
				change_county_control = accolade_county_control_gain_value
			}
		}
	}
}


##################
# Side Effects from Manage Royal Guard
# 4001-4999
##################

####
# Bad Side Effects
####

# Knight is Wounded
# by Claudia Baldassi
scripted_trigger marshal_task_4001_knight_trigger = {
	NOT = { this = root.cp:councillor_marshal }
	NOT = { has_trait = wounded }
}

marshal_task.4001 = {
	hidden = yes

	trigger = {
		exists = cp:councillor_marshal
		scope:councillor = cp:councillor_marshal
		cp:councillor_marshal = {
			martial < high_skill_rating
		}
		any_knight = {
			can_be_trained_as_knight_now_light_trigger = yes
			marshal_task_4001_knight_trigger = yes
			NOT = { is_vassal_of = root }
		}
	}

	weight_multiplier = {
		base = 1
		compare_modifier = {
			target = scope:councillor
			value = martial
			multiplier = -0.2
			offset = inverted_high_skill_rating
		}
	}

	immediate = {
		cp:councillor_marshal = {
			save_scope_as = active_councillor
		}
		set_variable = {
			name = had_marshal_task_side_effect
			value = yes
			days = marshal_task_side_effect_cooldown
		}
		random_knight = {
			limit = {
				can_be_trained_as_knight_now_light_trigger = yes
				marshal_task_4001_knight_trigger = yes
				NOT = { is_vassal_of = root }
			}
			weight = {
				base = 2
				compare_modifier = {
					value = prowess
					multiplier = -0.1
				}
			}
			save_scope_as = knight
		}

		send_interface_message = {
			type = msg_marshal_task_bad
			title = marshal_task.2001.notification
			desc = {
				triggered_desc = {
					trigger = {
						scope:active_councillor ?= { martial <= average_skill_level }
					}
					desc = task_martial_bad_unskilled_notification_tooltip
				}
				triggered_desc = {
					trigger = {
						scope:active_councillor ?= { martial > average_skill_level }
					}
					desc = task_martial_bad_skilled_notification_tooltip
				}
			}
			tooltip = task_manage_guards_notification_tooltip

			left_icon = scope:knight
			right_icon = scope:active_councillor

			scope:knight = {
				increase_wounds_effect = {
					REASON = duel
				}
			}
		}
	}
}

#Disorganized Royal Guards
# by Claudia Baldassi

marshal_task.4002 = {
	hidden = yes

	trigger = {
		exists = cp:councillor_marshal
		scope:councillor = cp:councillor_marshal
		scope:councillor ?= {
			martial < high_skill_rating
		}
	}

	weight_multiplier = {
		base = 1
		compare_modifier = {
			target = scope:councillor
			value = martial
			multiplier = -0.2
			offset = inverted_high_skill_rating
		}
	}

	immediate = {
		cp:councillor_marshal = {
			save_scope_as = active_councillor
		}
		set_variable = {
			name = had_marshal_task_side_effect
			value = yes
			days = marshal_task_side_effect_cooldown
		}

		send_interface_message = {
			type = msg_marshal_task_bad
			title = marshal_task.4002.notification
			desc = {
				triggered_desc = {
					trigger = {
						scope:active_councillor ?= { martial <= average_skill_level }
					}
					desc = task_martial_bad_unskilled_notification_tooltip
				}
				triggered_desc = {
					trigger = {
						scope:active_councillor ?= { martial > average_skill_level }
					}
					desc = task_martial_bad_skilled_notification_tooltip
				}
			}
			tooltip = task_manage_guards_notification_tooltip

			left_icon = scope:active_councillor

			scope:councillor_liege = {
				add_character_modifier = {
					modifier = task_manage_guards_disorganized_modifier
					months = 6
				}
			}
		}
	}
}

####
# Good Side Effects
####

#Knight Improved
# by Claudia Baldassi
marshal_task.4101 = {
	hidden = yes

	trigger = {
		exists = cp:councillor_marshal
		scope:councillor = cp:councillor_marshal
		cp:councillor_marshal = {
			councillor_positive_event_opinion_trigger = yes
			martial > mediocre_skill_rating
		}

		any_knight = {
			can_be_trained_as_knight_now_light_trigger = yes
			add_to_temporary_list = possible_commanders
		}

		any_in_list = {
			list = possible_commanders
			NOT = { this = root.cp:councillor_marshal }
		}
	}

	weight_multiplier = {
		base = 1
		compare_modifier = {
			target = scope:councillor
			value = martial
			multiplier = 0.2
			offset = inverted_mediocre_skill_rating
		}
	}

	immediate = {
		cp:councillor_marshal = {
			save_scope_as = active_councillor
		}
		set_variable = {
			name = had_marshal_task_side_effect
			value = yes
			days = marshal_task_side_effect_cooldown
		}

		#Build a list of possible characters involved
		every_knight = {
			limit = {
				can_be_trained_as_knight_now_light_trigger = yes
			}
			add_to_list = possible_knights
		}

		#Find a knight to improve
		random_in_list = {
			list = possible_knights
			limit = {
				NOT = { this = root.cp:councillor_marshal }
			}
			save_scope_as = commander
		}

		send_interface_message = {
			type = msg_marshal_task_good
			title = marshal_task.2102.notification
			desc = {
				triggered_desc = {
					trigger = {
						scope:active_councillor ?= { martial <= average_skill_level }
					}
					desc = task_martial_good_unskilled_notification_tooltip
				}
				triggered_desc = {
					trigger = {
						scope:active_councillor ?= { martial > average_skill_level }
					}
					desc = task_martial_good_skilled_notification_tooltip
				}
			}
			tooltip = task_manage_guards_notification_tooltip

			left_icon = scope:commander
			right_icon = scope:active_councillor

			scope:commander = {
				add_prowess_skill = 1
			}
		}
	}
}

# Hostile Scheme Disrupted
#by Claudia Baldassi
scripted_trigger marshal_task_4102_scheme_trigger = {
	hostile_scheme_trigger = yes
	scheme_owner = { is_ai = yes }
	save_temporary_scope_as = scheme
	scheme_progress >= scheme_progress_33
}

marshal_task.4102 = {
	type = letter_event
	opening = {
		desc = spymaster_task.0399.opening
	}
	desc = {
		desc = spymaster_task.1101.desc_opening
		first_valid = {
			triggered_desc = {
				trigger = { scope:scheme = { scheme_type = murder } }
				desc = spymaster_task.1101.murder
			}
			triggered_desc = {
				trigger = { scope:scheme = { scheme_type = abduct } }
				desc = spymaster_task.1101.abduct
			}
			desc = spymaster_task.1101.fallback
		}
		desc = spymaster_task.1101.ending
	}
	sender = scope:councillor

	trigger = {
		exists = cp:councillor_marshal
		scope:councillor = cp:councillor_marshal
		any_targeting_scheme = {
			marshal_task_4102_scheme_trigger = yes
		}
		scope:councillor ?= {
			councillor_positive_event_opinion_trigger = yes
			martial >= medium_skill_rating
		}
	}

	weight_multiplier = {
		base = 1
		compare_modifier = {
			target = scope:councillor
			value = martial
			multiplier = 0.2
			offset = inverted_mediocre_skill_rating
		}
	}

	immediate = {
		random_targeting_scheme = {
			limit = {
				marshal_task_4102_scheme_trigger = yes
			}
			save_scope_as = scheme
			scheme_owner = {
				save_scope_as = owner
			}
			if = {
				limit = {
					is_scheme_exposed = no
				}
				hidden_effect = {
					expose_scheme = yes
				}
				custom_tooltip = spymaster_task.1101.exposure_tt
			}
		}
	}

	option = {
		name = spymaster_task.1101.a
		scope:scheme = {
			end_scheme = yes
		}
	}
}
