﻿namespace = el_landed_contract_sup_fac

#choice of adventurer
el_landed_contract_sup_fac.0001 = {
	hidden = yes
	type = character_event
	trigger = {
		scope:el_employer = { is_alive = yes }
		scope:el_adventurer_fin = { is_alive = yes }
	}
	on_trigger_fail = {
		trigger_event = el_landed_contract_sup_fac.0006 
	}
	immediate = {
		scope:el_adventurer_fin = {
			if = {
				limit = { scope:el_employer = { is_alive = yes } }
				current_travel_plan ?= { pause_travel_plan = yes }
			}
			else_if = {
				limit = { scope:el_employer = { is_alive = no } }
				start_travel_plan = {
					destination = scope:el_adventurer_fin.domicile.domicile_location
					return_trip = no
				}
			}
		}
		scope:el_employer = {
			if = { 
				limit = { is_alive = yes }
				add_character_flag = {
					flag = el_is_employing_adventurer
					days = 365
				}
			}				
		}
	}
	option = {
		duel = {
			skills = { martial }
			value = high_skill_rating
			#succed
			50 = {
				compare_modifier = {
					value = scope:duel_value
					multiplier = 3.5
				}
				el_landed_camp_purpose_bonus_more_modifier = { CAMP_PURPOSE = camp_purpose_mercenaries }
				el_landed_education_bonus_more_modifier = { SKILL = martial }
				el_landed_intelect_bonus_more_modifier = { SCOPE = el_adventurer_fin }
				el_landed_sup_fac_military_strenght_bonus_more_modifier = yes
				trigger_event = {
					id = el_landed_contract_sup_fac.0007
					days = 19
				}
				scope:el_employer = {
					trigger_event =  {
						id = el_landed_contract_sup_fac.0002
						days = { 10 16 }
					}
				}
			}
			#fail
			50 = {
				compare_modifier = {
					value = scope:duel_value
					multiplier = -3.5
				}
				el_landed_camp_purpose_bonus_less_modifier = { CAMP_PURPOSE = camp_purpose_mercenaries }
				el_landed_education_bonus_less_modifier = { SKILL = martial }
				el_landed_intelect_bonus_less_modifier = { SCOPE = el_adventurer_fin }
				el_landed_sup_fac_military_strenght_bonus_less_modifier = yes
				trigger_event = {
					id = el_landed_contract_sup_fac.0007
					days = 19
				}
				scope:el_employer = {
					trigger_event =  {
						id = el_landed_contract_sup_fac.0003
						days = { 10 16 }
					}
				}				
			}
		}
		ai_chance = {
				base = 100
		}
	}
}

#tutor succeed
el_landed_contract_sup_fac.0002 = {
	type = character_event
	theme = landless_adventurer
	title = el_landed_contract_sup_fac.0002.t
	desc = el_landed_contract_sup_fac.0002.desc
	override_background = { reference = army_camp }
	window = big_event_window
	center_portrait = {
		character = scope:el_adventurer_fin
		animation =  marshal 
		camera = camera_event_scheme_vs_center_positioned_left	
	}
	#success
	right_portrait = {
		character = scope:el_landed_sup_fac_leader
		animation = interested  
	}
	trigger = {
		scope:el_employer = { is_alive = yes }
		scope:el_adventurer_fin = { is_alive = yes }
	}
	on_trigger_fail = {
		trigger_event = el_landed_contract_sup_fac.0006
	}	
	immediate = {
		scope:el_landed_sup_fac_fin = {
			if = {
				limit = { is_alive = yes }
				add_character_flag = {
					flag = el_landed_sup_fac_cant_be_sup_fac
					days = 1825
				}
			}
		}
		scope:el_landed_sup_fac_faction_fin = {
			add_faction_discontent = el_landed_sup_fac_levie_discont_value
		}
		scope:el_adventurer_fin = {
			add_character_flag = need_military_outfit
			if = {
				limit = { can_join_faction = scope:el_landed_sup_fac_faction_fin }
				join_faction_forced = {
					faction = scope:el_landed_sup_fac_faction_fin
					forced_by = scope:el_employer
					years = 2
				}
			}
			else_if = {
				limit = { NOT = { can_join_faction = scope:el_landed_sup_fac_faction_fin } }
				scope:el_landed_sup_fac_leader = {
					el_landed_contrat_sup_fac_spawn_army_effect = yes	
				}
			}			
		}	
	}
	option = {
		name = el_landed_contract_sup_fac.0002.a
		scope:el_adventurer_fin = {
			start_travel_plan = {
				destination = scope:el_employer.capital_province
				on_arrival_event = el_landed_contract_sup_fac.0024
			}
		}		
	}
	after = {
		scope:el_adventurer_fin = {
			remove_character_flag = need_military_outfit
		}
		if = {
			limit = { scope:el_employer = { is_alive = no } }
			scope:el_adventurer_fin = {
				current_travel_plan ?= { resume_travel_plan = yes }
			}
		}
	}			
}

#train fail
el_landed_contract_sup_fac.0003 = {
	type = character_event
	theme = landless_adventurer
	title = el_landed_contract_sup_fac.0003.t
	desc = el_landed_contract_sup_fac.0003.desc
	override_background = { reference = army_camp }
	window = big_event_window
	center_portrait = {
		character = scope:el_adventurer_fin
		animation =  shame 
		camera = camera_event_scheme_vs_center_positioned_left	
	}
	#success
	right_portrait = {
		character = scope:el_landed_sup_fac_leader
		animation = disapproval   
	}
	trigger = {
		scope:el_employer = { is_alive = yes }
		scope:el_adventurer_fin = { is_alive = yes }
	}
	on_trigger_fail = {
		trigger_event = el_landed_contract_sup_fac.0006
	}	
	immediate = {
		scope:el_landed_sup_fac_fin = {
			if = {
				limit = { is_alive = yes }
				add_character_flag = {
					flag = el_landed_sup_fac_cant_be_sup_fac
					days = 1825
				}
			}
		}
		scope:el_adventurer_fin = {
			add_character_flag = need_military_outfit
		}			
	}		
	option = {
		name = el_landed_contract_sup_fac.0003.a
		scope:el_adventurer_fin = {
			start_travel_plan = {
				destination = scope:el_employer.capital_province
				on_arrival_event = el_landed_contract_sup_fac.0025
			}
		}
	}
	after = {
		scope:el_adventurer_fin = {
			remove_character_flag = need_military_outfit
		}
		if = {
			limit = { scope:el_employer = { is_alive = no } }
			scope:el_adventurer_fin = {
				current_travel_plan ?= { resume_travel_plan = yes }
			}
		}
	}	
}

#succeed contract
el_landed_contract_sup_fac.0004 = {
	type = character_event
	theme = landless_adventurer
	title = el_landed_contract_sup_fac.0004.t
	desc = el_landed_contract_sup_fac.0004.desc
	#window = big_event_window
	override_background = { reference = throne_room }
	left_portrait = {
		character = scope:el_employer
		animation = admiration 
		}
	right_portrait = {
		character = scope:el_adventurer_fin
		animation = idle 
		}
		
	immediate = {
		set_variable = {
			name = el_landed_cant_use_sup_fac
			value = yes
			days = 730
		}
	}		
	option = {
		name = el_landed_contract_sup_fac.0004.a
		pay_short_term_gold = {
			target = scope:el_adventurer_fin
			gold = medium_gold_value
		}
		scope:el_adventurer_fin = {
			#add_contact = scope:el_employer
			if = {
				limit = { has_variable = el_gallowsbait }
				add_trait = gallowsbait
			}
			add_prestige = medium_prestige_gain
			domicile = { change_provisions = minor_provisions_value }
		}

	}
	option = {
		name = el_landed_contract_sup_fac.0004.b
		add_prestige = medium_prestige_loss
		scope:el_adventurer_fin = {
			if = {
				limit = { has_variable = el_gallowsbait }
				add_trait = gallowsbait
			}
			add_prestige = medium_prestige_gain		
			add_opinion = {
				target = scope:el_employer
				modifier = el_landed_refuse_payment
			}
			if = {
			limit = {
				NOT = { has_relation_rival = scope:el_employer }
			}
			set_relation_rival = {
				target = scope:el_employer
				reason = el_landed_refuse_payment_rival
			}
			}
			else_if = {
				limit = {
					has_relation_rival = scope:el_employer
					NOT = { has_relation_nemesis = scope:el_employer }
				}
				set_relation_nemesis = {
					target = scope:el_employer 
					reason = el_landed_refuse_payment_rival
				}
			}
		}
	}
	after = {
		scope:el_adventurer_fin = {
			remove_character_flag = is_in_task_contract_event_chain
			start_travel_plan = {
				destination = scope:el_adventurer_fin.domicile.domicile_location
				return_trip = no
			}
		}
		scope:el_employer = {
			remove_character_flag = el_is_employing_adventurer
		}
	}		
}

#fail contract
el_landed_contract_sup_fac.0005 = {
	type = character_event
	theme = landless_adventurer
	title = el_landed_contract_sup_fac.0005.t
	desc = el_landed_contract_sup_fac.0005.desc
	#window = big_event_window
	override_background = { reference = throne_room }
	left_portrait = {
		character = scope:el_employer
		animation = disapproval 
		}
	right_portrait = {
		character = scope:el_adventurer_fin
		animation = disbelief 
		}
		
	immediate = {
		set_variable = {
			name = el_landed_cant_use_sup_fac
			value = yes
			days = 730
		}
	}		
	option = {
		name = el_landed_contract_sup_fac.0005.a
		scope:el_adventurer_fin = {
			if = {
				limit = { has_personality_dominant_trigger = yes }
				add_stress = medium_stress_impact_gain
			}	
		}
		scope:el_employer = {
			if = {
				limit = { has_personality_dominant_trigger = yes }
				add_stress = medium_stress_impact_gain
			}	
		}
	}
	
	after = {
		scope:el_adventurer_fin = {
			remove_character_flag = is_in_task_contract_event_chain
			start_travel_plan = {
				destination = scope:el_adventurer_fin.domicile.domicile_location
				return_trip = no
			}
		}
		scope:el_employer = {
			remove_character_flag = el_is_employing_adventurer
		}
	}
}

#clear if dead
el_landed_contract_sup_fac.0006 = {
	hidden = yes
		immediate = {		
		scope:el_employer = {
			remove_character_flag = el_is_employing_adventurer
		}
		scope:el_adventurer_fin = {
			if = {
				limit = { is_alive = yes }
				remove_character_flag = is_in_task_contract_event_chain
				if = {
					limit = { current_travel_plan ?= { is_paused  = yes } }
					current_travel_plan ?= { resume_travel_plan = yes }
				}
			}
		}
	}
}

#clear if employer dead
el_landed_contract_sup_fac.0007 = {
	hidden = yes
	immediate = {
		if = {
			limit = { scope:el_employer = { is_alive = no }}
			scope:el_employer = {
			}
			scope:el_adventurer_fin = {
				if = {
					limit = { is_alive = yes }
					remove_character_flag = is_in_task_contract_event_chain
					if = {
						limit = { current_travel_plan ?= { is_paused  = yes } }
						current_travel_plan ?= { resume_travel_plan = yes }
					}
				}
			}
		}
	}
}

#choice of target
el_landed_contract_sup_fac.0010 = {
	type = character_event
	theme = landless_adventurer
	title = el_landed_contract_sup_fac.0010.t
	desc = el_landed_contract_sup_fac.0010.desc
	#window = big_event_window
	override_background = { reference = throne_room }
	left_portrait = {
		character = scope:el_employer 
		animation = thinking
		}
	right_portrait = {
		character = scope:el_adventurer_fin
		animation = idle
		}
	trigger = {
		scope:el_employer = { is_alive = yes }
		scope:el_adventurer_fin = { is_alive = yes }
	}
	on_trigger_fail = {
		trigger_event = el_landed_contract_sup_fac.0006 
	}
	widget = {
		gui = "event_window_el_landed_character_ruler"
		container = "custom_widgets_container"
	}
	immediate = {
		add_character_flag = {
				flag = el_is_employing_adventurer
				days = 365
		}
		every_neighboring_and_across_water_top_liege_realm_owner = {
			limit = {
				NOT = { is_tributary_of = scope:el_employer }
				capital_province = { squared_distance = { target = scope:el_employer.capital_province value < el_squared_distance_max_travel } }
				has_targeting_faction = yes
				NOT = { has_character_flag = el_landed_sup_fac_cant_be_sup_fac }
				any_targeting_faction = {
					faction_is_at_war = no
					any_faction_member = {
						count > 0
						NOT = { this = scope:el_employer }
						#is_landed = yes
					}
				}
			}
			root = {
				add_to_variable_list = {
					name = el_landed_target_list
					target = prev
				}
			}
		}
		every_neighboring_and_across_water_realm_same_rank_owner = {
			limit = {
				NOT = { is_tributary_of = scope:el_employer }
				capital_province = { squared_distance = { target = scope:el_employer.capital_province value < el_squared_distance_max_travel } }
				has_targeting_faction = yes
				NOT = { has_character_flag = el_landed_sup_fac_cant_be_sup_fac }
				any_targeting_faction = {
					faction_is_at_war = no
					any_faction_member = {
						count > 0
						NOT = { this = scope:el_employer }
						#is_landed = yes
					}
				}
			}
			root = {
				add_to_variable_list = {
					name = el_landed_target_list
					target = prev
				}
			}
		}
		every_land_neighboring_realm_with_tributaries_owner = {
			limit = {
				NOT = { is_tributary_of = scope:el_employer }
				capital_province = { squared_distance = { target = scope:el_employer.capital_province value < el_squared_distance_max_travel } }
				has_targeting_faction = yes
				NOT = { has_character_flag = el_landed_sup_fac_cant_be_sup_fac }
				any_targeting_faction = {
					faction_is_at_war = no
					any_faction_member = {
						count > 0
						NOT = { this = scope:el_employer }
						#is_landed = yes
					}					
				}
			}
			root = {
				add_to_variable_list = {
					name = el_landed_target_list
					target = prev
				}
			}
		}
		top_liege ?= {
			if = {
				limit = {
					any_sub_realm_county = {
						count > 0
						holder = {
							NOT = { this = scope:el_employer }
							NOT = { is_tributary_of = scope:el_employer }
							NOT = { is_vassal_or_below_of = scope:el_employer }
							has_targeting_faction = yes
							NOT = { has_character_flag = el_landed_sup_fac_cant_be_sup_fac }
							capital_province = {
								squared_distance = { target = scope:el_employer.capital_province value < el_squared_distance_max_travel } 
							}
							any_targeting_faction = {
								faction_is_at_war = no
								any_faction_member = {
									count > 0
									NOT = { this = scope:el_employer }
									#is_landed = yes
								}
							}
						}
					}
				}
				root = {
					add_to_variable_list = {
						name = el_landed_target_list
						target = prev
					}
				}
			}
		}		
		random_in_list = {
			variable = el_landed_target_list
			save_scope_as = el_landed_sup_fac_fin
		}		
	}
	option = {
		name = el_landed_contract_sup_fac.0010.a
		custom_tooltip = el_landed_trigger.0001.a.t.gen
		custom_tooltip = el_landed_trigger.0001.a.tt.gen
		if = {
			limit = { has_variable = el_landed_target  }
			var:el_landed_target = {save_scope_as = el_landed_sup_fac_fin }
		}
		scope:el_landed_sup_fac_fin = {
			capital_province = {
				save_scope_as = el_land_sup_fac_location
			}
		}
		clear_variable_list = el_landed_target_list 
		remove_variable = el_landed_target
		scope:el_employer = {
			trigger_event = el_landed_contract_sup_fac.0011
		}
	}
}


#choice of target
el_landed_contract_sup_fac.0011 = {
	type = character_event
	theme = landless_adventurer
	title = el_landed_contract_sup_fac.0011.t
	desc = el_landed_contract_sup_fac.0011.desc
	#window = big_event_window
	override_background = { reference = throne_room }
	left_portrait = {
		character = scope:el_employer 
		animation = thinking
		}
	right_portrait = {
		character = scope:el_adventurer_fin
		animation = idle
		}
	trigger = {
		scope:el_employer = { is_alive = yes }
		scope:el_adventurer_fin = { is_alive = yes }
	}
	on_trigger_fail = {
		trigger_event = el_landed_contract_sup_fac.0006 
	}
	widget = {
		gui = "event_window_el_landed_character_faction"
		container = "custom_widgets_container"
	}
	immediate = {
		scope:el_landed_sup_fac_fin = {
			every_targeting_faction = {
				limit = {
					faction_is_at_war = no
					any_faction_member = {
						count > 0
						NOT = { this = scope:el_employer }
						#is_landed = yes
					}
				}
				root = {
					add_to_variable_list = {
						name = el_landed_target_list
						target = prev
					}
				}
			}
		}
		random_in_list = {
			variable = el_landed_target_list
			save_scope_as = el_landed_sup_fac_faction_fin
		}		
	}
	option = {
		name = el_landed_contract_sup_fac.0011.a
		custom_tooltip = el_landed_trigger.0001.a.t.gen
		custom_tooltip = el_landed_trigger.0001.a.tt.gen
		if = {
			limit = { has_variable = el_landed_target  }
			var:el_landed_target = {save_scope_as = el_landed_sup_fac_faction_fin }
		}
		scope:el_landed_sup_fac_faction_fin = {
			ordered_faction_member = {
				limit = { 
					exists = this 
					NOT = { this = scope:el_employer }
					#is_landed = yes
				}
				order_by = max_military_strength
				max = 1
				save_scope_as = el_landed_sup_fac_leader
			}
		}
		scope:el_landed_sup_fac_leader = {
			capital_province ?= {
				save_scope_as = el_land_sup_fac_location_2
			}
		}
		clear_variable_list = el_landed_target_list 
		remove_variable = el_landed_target
		if =  {
			limit = {
				scope:el_land_sup_fac_location_2 = {
					exists = this
				}
			}
			scope:el_adventurer_fin = {
				start_travel_plan = {
					destination = scope:el_land_sup_fac_location_2
					on_arrival_event =  el_landed_contract_sup_fac.0001
				}
			}
		}
		else = {
			scope:el_adventurer_fin = {
				start_travel_plan = {
					destination = scope:el_land_sup_fac_location
					on_arrival_event =  el_landed_contract_sup_fac.0001
				}
			}
		}
	}
}


el_landed_contract_sup_fac.0020 = {
	hidden = yes
	trigger = {
		scope:el_employer = { is_alive = yes }
		scope:el_adventurer_fin = { is_alive = yes }
	}
	on_trigger_fail = {
		trigger_event = el_landed_contract_sup_fac.0007
	}
	immediate = {
		scope:el_employer = {
			trigger_event = el_landed_contract_sup_fac.0010
		}
	}
}

el_landed_contract_sup_fac.0024 = {
	hidden = yes
	immediate = {
		if = {
			limit = { scope:el_employer = { is_alive = no } }
			start_travel_plan = {
				destination = scope:el_adventurer_fin.domicile.domicile_location
				return_trip = no
			}
		}
		else_if = {
			limit = { scope:el_employer = { is_alive = yes } }
			scope:el_employer = {
				trigger_event = el_landed_contract_sup_fac.0004
			}
		}
	}
}

el_landed_contract_sup_fac.0025 = {
	hidden = yes
	immediate = {
		if = {
			limit = { scope:el_employer = { is_alive = no } }
			start_travel_plan = {
				destination = scope:el_adventurer_fin.domicile.domicile_location
				return_trip = no
			}
		}
		else_if = {
			limit = { scope:el_employer = { is_alive = yes } }
			scope:el_employer = {
				trigger_event = el_landed_contract_sup_fac.0005
			}
		}
	}
}