﻿##############
### common ###
##############
# Toggles the mode of the contubernalis supervisor between strict & relaxed
# management of the contubernalis.
#
# This is done by setting the variable `regula_contubernalis_supervisor_strict_mode`
# on the liege. When unset, tasks should use relaxed mode.
#
# scope = the liege of the councillor_contubernalis_supervisor
regula_contubernalis_supervisor_toggle_mode_effect = {
	if = {
		limit = {
			regula_contubernalis_supervisor_in_strict_mode_trigger = yes
		}
		custom_description_no_bullet = {
			text = regula_contubernalis_supervisor_toggle_mode_relaxed_effect
			remove_variable = regula_contubernalis_supervisor_strict_mode
		}
	}
	else = {
		custom_description_no_bullet = {
			text = regula_contubernalis_supervisor_toggle_mode_strict_effect
			set_variable = {
				name = regula_contubernalis_supervisor_strict_mode
				value = yes
			}
		}
	}
}

##############################
### task_sparring_partners ###
##############################
# Applies a random positive side effect for task_sparring_partners.
#
# scope:councillor = councillor_contubernalis_supervisor
# scope:councillor_liege = liege of the councillor_contubernalis_supervisor
# scope:contubernalis_participant = the contubernalis participating in the sparring
# scope:knight_participant = the knight participating in the sparring
regula_task_sparring_partners_positive_side_effect = {
	regula_task_sparring_partners_pick_positive_side_effect = {
		KNIGHT_MODIFIER_SCOPE = regula_tsp_knight_modifier
		KNIGHT_TRAIT_SCOPE = regula_tsp_knight_trait
		KNIGHT_TOURNEY_PARTICIPANT_SCOPE = regula_tsp_knight_tourney
		LIFESTYLE_XP_SCOPE = regula_tsp_knight_lifestyle_xp
		SKILL_IMPROVEMENT_SCOPE = regula_tsp_knight_skill_improvement
		SKILL_IMPROVEMENT_MAGNITUDE_SCOPE = regula_tsp_knight_skill_improvement_magnitude
	}

	scope:councillor_liege = {
		send_interface_message = {
			type = event_prowess_good_with_text
			title = regula_sparring_partners_side_effect_positive_title
			desc = regula_sparring_partners_side_effect_positive_desc
			left_icon = scope:knight_participant
			right_icon = scope:contubernalis_participant

			scope:knight_participant = {
				regula_task_sparring_partners_modifier_apply_modifier_effect = {
					MODIFIER_SCOPE = regula_tsp_knight_modifier
				}
				regula_task_sparring_partners_positive_trait_apply_trait_effect = {
					TRAIT_SCOPE = regula_tsp_knight_trait
				}
				regula_task_sparring_partners_positive_trait_apply_tourney_track_effect = {
					TOURNEY_SCOPE = regula_tsp_knight_tourney
				}
				regula_task_sparring_partners_apply_lifestyle_side_effect = {
					LIFESTYLE_XP_SCOPE = regula_tsp_knight_lifestyle_xp
				}
				regula_task_sparring_partners_apply_skill_enhancement_side_effect = {
					SKILL_IMPROVEMENT_SCOPE = regula_tsp_knight_skill_improvement
					SKILL_IMPROVEMENT_MAGNITUDE_SCOPE = regula_tsp_knight_skill_improvement_magnitude
				}
			}
		}
	}
}

# Picks the positive effect to be granted for side effects of
# task_sparring_partners.
#
# KNIGHT_MODIFIER_SCOPE = scope to which positive modifier effects to the knight participant will be stored
# KNIGHT_TRAIT_SCOPE = scope to which any positive trait effects on the knight will be stored
# KNIGHT_TOURNEY_PARTICIPANT_SCOPE = scope to which any tourney participant specific improvements will be stored
# LIFESTYLE_XP_SCOPE = scope to which lifestyle xp change effects will be stored
# SKILL_IMPROVEMENT_SCOPE = scope indicating which skill to improve
# SKILL_IMPROVEMENT_MAGNITUDE_SCOPE = scope indicating how much to improve skills by
#
# scope:contubernalis_participant = the contubernalis participating in the sparring
# scope:knight_participant = the knight participating in the sparring
regula_task_sparring_partners_pick_positive_side_effect = {
	random_list = {
		50 = { # Positive Modifier
			trigger = {
				scope:knight_participant = {
					regula_task_sparring_partners_can_gain_positive_sparring_modifier_trigger = yes
				}
			}
			scope:knight_participant = {
				regula_task_sparring_partners_positive_modifier_pick_modifier_effect = {
					SAVE_AS = $KNIGHT_MODIFIER_SCOPE$
				}
			}
		}
		30 = { # Trait & Trait XP Changes
			trigger = {
				scope:knight_participant = {
					regula_task_sparring_can_gain_positive_trait_modifification_trigger = yes
				}
			}
			scope:knight_participant = {
				regula_task_sparring_partners_positive_pick_trait_side_effects = {
					KNIGHT_TRAIT_SCOPE = $KNIGHT_TRAIT_SCOPE$
					KNIGHT_TOURNEY_PARTICIPANT_SCOPE = $KNIGHT_TOURNEY_PARTICIPANT_SCOPE$
				}
			}
		}
		10 = { # Martial Lifestyle XP
			trigger = {
				scope:knight_participant = {
					highest_held_title_tier >= tier_county
					martial_lifestyle_perk_points < max_perk_amount_per_lifestyle
				}
			}
			scope:knight_participant = {
				regula_task_sparring_partners_positive_pick_lifestyle_side_effect = {
					LIFESTYLE_XP_SCOPE = $LIFESTYLE_XP_SCOPE$
				}
			}
		}
		10 = { # Skill Enchancement
			scope:knight_participant = {
				regula_task_sparring_partners_positive_pick_skill_enhancement_side_effect = {
					SKILL_IMPROVEMENT_SCOPE = $SKILL_IMPROVEMENT_SCOPE$
					SKILL_IMPROVEMENT_MAGNITUDE_SCOPE = $SKILL_IMPROVEMENT_MAGNITUDE_SCOPE$
				}
			}
		}
	}
}

# Applies a random negative side effect for task_sparring_partners.
#
# Note: This does not include more serious negative side effects such as death
# or more permanent negative impacts.
#
# scope:councillor = councillor_contubernalis_supervisor
# scope:councillor_liege = liege of the councillor_contubernalis_supervisor
# scope:contubernalis_participant = the contubernalis participating in the sparring
# scope:knight_participant = the knight participating in the sparring
regula_task_sparring_partners_negative_side_effect = {
	regula_task_sparring_partners_pick_negative_side_effect = {
		KNIGHT_MODIFIER_SCOPE = regula_tsp_knight_modifier
		MINOR_WOUND_CONTUBERNALIS_SCOPE = regula_tsp_minor_wound_contubernalis
		MINOR_WOUND_KNIGHT_SCOPE = regula_tsp_minor_wound_knight
		CONTUBERNALIS_MAIMED = regula_tsp_contubernalis_maimed
		CONTUBERNALIS_KILLED = regula_tsp_contubernalis_killed
	}
	
	scope:councillor_liege = {
		send_interface_message = {
			type = event_prowess_bad_with_text
			title = regula_sparring_partners_side_effect_negative_title
			desc = regula_sparring_partners_side_effect_negative_desc
			left_icon = scope:knight_participant
			right_icon = scope:contubernalis_participant

			scope:contubernalis_participant = {
				regula_contubernalis_supervisor_apply_wound = {
					BOOL_SCOPE = regula_tsp_minor_wound_contubernalis
					REASON = training_accident
				}
				regula_contubernalis_supervisor_apply_maim_trait_effect = {
					TRAIT_SCOPE = regula_tsp_contubernalis_maimed
					REASON = training_accident
				}
				regula_contubernalis_supervisor_death_effect = {
					BOOL_SCOPE = regula_tsp_contubernalis_killed
					DEATH_REASON = death_training_accident
				}
			}
			scope:knight_participant = {
				regula_task_sparring_partners_modifier_apply_modifier_effect = {
					MODIFIER_SCOPE = regula_tsp_knight_modifier
				}
				regula_contubernalis_supervisor_apply_wound = {
					BOOL_SCOPE = regula_tsp_minor_wound_knight
					REASON = training_accident
				}
			}
		}
	}
}

# Selects a negative side effect to occur for task_sparring_partners.
#
# KNIGHT_MODIFIER_SCOPE = scope indicating a modifier to be granted to the knight
# MINOR_WOUND_CONTUBERNALIS_SCOPE = boolean scope indicating that the contubernalis should be wounded
# MINOR_WOUND_KNIGHT_SCOPE = boolean scope indicating that the knight should be wounded
# CONTUBERNALIS_MAIMED = scope indicating maiming effect to be applied to the contubernalis
# CONTUBERNALIS_KILLED = boolean scope which indicates that the contubernalis should be killed as a side effect
#
# scope:contubernalis_participant = the contubernalis participating in the sparring
# scope:knight_participant = the knight participating in the sparring
regula_task_sparring_partners_pick_negative_side_effect = {
	random_list = {
		50 = { # Negative Modifier
			trigger = {
				scope:knight_participant = {
					regula_task_sparring_partners_can_gain_negative_sparring_modifier_trigger = yes
				}
			}
			scope:knight_participant = {
				regula_task_sparring_partners_negative_modifier_pick_modifier_effect = {
					SAVE_AS = $KNIGHT_MODIFIER_SCOPE$
				}
			}
		}
		40 = { # Non-fatal Contubernalis Wound
			trigger = {
				scope:contubernalis_participant = {
					has_trait_rank = {
						trait = wounded
						rank < 3
					}
				}
			}
			save_temporary_scope_value_as = {
				name = $MINOR_WOUND_CONTUBERNALIS_SCOPE$
				value = yes
			}
		}
		10 = { # Non-fatal Knight Wound
			trigger = {
				scope:knight_participant = {
					has_trait_rank = {
						trait = wounded
						rank < 3
					}
				}
			}
			save_temporary_scope_value_as = {
				name = $MINOR_WOUND_KNIGHT_SCOPE$
				value = yes
			}
		}
		10 = { # Contubernalis Maimed
			trigger = {
				scope:councillor_liege = {
					regula_contubernalis_supervisor_in_strict_mode_trigger = yes
				}
				scope:contubernalis_participant = {
					regula_contubernalis_supervisor_can_gain_maim_side_effect_trigger = yes
				}
			}
			
			scope:contubernalis_participant = {
				regula_contubernalis_supervisor_pick_maim_trait_effect = {
					SAVE_AS = $CONTUBERNALIS_MAIMED$
				}
			}
		}
		5 = { # Contubernalis Death
			trigger = {
				scope:councillor_liege = {
					regula_contubernalis_supervisor_in_strict_mode_trigger = yes
				}
			}
			save_temporary_scope_value_as = {
				name = $CONTUBERNALIS_KILLED$
				value = yes
			}
		}
	}
}

#############################
### task_beasts_of_burden ###
#############################
# Applies a random positive side effect for task_beasts_of_burden.
#
# scope:councillor = councillor_contubernalis_supervisor
# scope:councillor_liege = liege of the councillor_contubernalis_supervisor
# scope:contubernalis_participant = the contubernalis doing labor
# scope:county_participant = the county gaining free labor
regula_task_beasts_of_burden_positive_side_effect = {
	regula_task_beasts_of_burden_pick_positive_side_effect = {
		COUNTY_MODIFIER_SCOPE = regula_bob_county_modifier
		LIEGE_EFFECT_SCOPE = regula_bob_liege_effect
	}

	scope:councillor_liege = {
		send_interface_message = {
			type = event_prowess_good_with_text
			title = regula_beasts_of_burden_side_effect_positive_title
			desc = regula_beasts_of_burden_side_effect_positive_desc
			left_icon = scope:county_participant
			right_icon = scope:contubernalis_participant

			scope:councillor_liege = {
				regula_task_beasts_of_burden_apply_liege_side_effect = {
					FLAG_SCOPE = regula_bob_liege_effect
				}
			}
			scope:county_participant = {
				regula_task_beasts_of_burden_apply_county_modifier_side_effect = {
					MODIFIER_SCOPE = regula_bob_county_modifier
				}
			}
		}
	}
}

# Picks the positive effect to be granted for side effects of
# task_beasts_of_burden.
#
# COUNTY_MODIFIER_SCOPE = scope to which any selected county modifiers will be stored
# LIEGE_EFFECT_SCOPE = scope to which any liege side effects will be stored
#
# scope:councillor_liege = liege of the councillor_contubernalis_supervisor
# scope:contubernalis_participant = the contubernalis doing labor
# scope:county_participant = the county gaining free labor
regula_task_beasts_of_burden_pick_positive_side_effect = {
	random_list = {
		90 = { # Positive County Modifier
			trigger = {
				scope:county_participant = {
					regula_task_beasts_of_burden_can_gain_positive_county_modifier_trigger = yes
				}
			}
			scope:county_participant = {
				regula_task_beasts_of_burden_pick_positive_county_modifier_side_effect = {
					SAVE_AS = $COUNTY_MODIFIER_SCOPE$
				}
			}
		}
		10 = { # Immediate Liege Changes
			scope:councillor_liege = {
				regula_task_beasts_of_burden_pick_positive_liege_side_effect = {
					SAVE_AS = $LIEGE_EFFECT_SCOPE$
				}
			}
		}
	}
}

# Applies a random negative side effect for task_beasts_of_burden.
#
# scope:councillor = councillor_contubernalis_supervisor
# scope:councillor_liege = liege of the councillor_contubernalis_supervisor
# scope:contubernalis_participant = the contubernalis doing labor
# scope:county_participant = the county gaining free labor
regula_task_beasts_of_burden_negative_side_effect = {
	regula_task_beasts_of_burden_pick_negative_side_effect = {
		COUNTY_MODIFIER_SCOPE = regula_bob_county_modifier
		MINOR_WOUND_CONTUBERNALIS_SCOPE = regula_bob_minor_wound_contubernalis
		CONTUBERNALIS_MAIMED = regula_bob_contubernalis_maimed
		CONTUBERNALIS_KILLED = regula_bob_contubernalis_killed
	}

	scope:councillor_liege = {
		send_interface_message = {
			type = event_prowess_bad_with_text
			title = regula_beasts_of_burden_side_effect_negative_title
			desc = regula_beasts_of_burden_side_effect_negative_title
			left_icon = scope:county_participant
			right_icon = scope:contubernalis_participant

			scope:contubernalis_participant = {
				regula_contubernalis_supervisor_apply_wound = {
					BOOL_SCOPE = regula_bob_minor_wound_contubernalis
					REASON = accident
				}
				regula_contubernalis_supervisor_apply_maim_trait_effect = {
					TRAIT_SCOPE = regula_bob_contubernalis_maimed
					REASON = accident
				}
				regula_contubernalis_supervisor_death_effect = {
					BOOL_SCOPE = regula_bob_contubernalis_killed
					DEATH_REASON = death_accident
				}
			}
			scope:county_participant = {
				regula_task_beasts_of_burden_apply_county_modifier_side_effect = {
					MODIFIER_SCOPE = regula_bob_county_modifier
				}
			}
		}
	}
}

# Selects a negative side effect to occur for task_sparring_partners.
#
# COUNTY_MODIFIER_SCOPE = scope to which any selected county modifiers will be stored
# MINOR_WOUND_CONTUBERNALIS_SCOPE = scope to which any wound effects for contubernalis will be stored
# CONTUBERNALIS_MAIMED = scope indicating maiming effect to be applied to the contubernalis
# CONTUBERNALIS_KILLED = boolean scope which indicates that the contubernalis should be killed as a side effect
#
# scope:contubernalis_participant = the contubernalis doing labor
# scope:county_participant = the county gaining free labor
regula_task_beasts_of_burden_pick_negative_side_effect = {
	random_list = {
		80 = { # Negative County Modifier
			trigger = {
				scope:county_participant = {
					regula_task_beasts_of_burden_can_gain_negative_county_modifier_trigger = yes
				}
			}
			scope:county_participant = {
				regula_task_beasts_of_burden_pick_negative_county_modifier_side_effect = {
					SAVE_AS = $COUNTY_MODIFIER_SCOPE$
				}
			}
		}
		20 = { # Contubernalis Wound
			trigger = {
				scope:contubernalis_participant = {
					has_trait_rank = {
						trait = wounded
						rank < 3
					}
				}
			}
			save_temporary_scope_value_as = {
				name = $MINOR_WOUND_CONTUBERNALIS_SCOPE$
				value = yes
			}
		}
		5 = { # Contubernalis Maimed
			trigger = {
				scope:councillor_liege = {
					regula_contubernalis_supervisor_in_strict_mode_trigger = yes
				}
				scope:contubernalis_participant = {
					regula_contubernalis_supervisor_can_gain_maim_side_effect_trigger = yes
				}
			}
			
			scope:contubernalis_participant = {
				regula_contubernalis_supervisor_pick_maim_trait_effect = {
					SAVE_AS = $CONTUBERNALIS_MAIMED$
				}
			}
		}
		1 = { # Contubernalis Death
			trigger = {
				scope:councillor_liege = {
					regula_contubernalis_supervisor_in_strict_mode_trigger = yes
				}
			}
			save_temporary_scope_value_as = {
				name = $CONTUBERNALIS_KILLED$
				value = yes
			}
		}
	}
}
