﻿namespace = el_landed_contract_con_nei

#choice of adventurer
el_landed_contract_con_nei.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_con_nei.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 = { learning }
			value = very_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_scholars }
				el_landed_education_bonus_more_modifier = { SKILL = learning }
				el_landed_intelect_bonus_more_modifier = { SCOPE = el_adventurer_fin }
				el_landed_con_nei_fervor_bonus_more_modifier = yes
				el_landed_con_nei_reformed_bonus_more_modifier = yes
				el_landed_con_nei_cynical_bonus_more_modifier = yes
				el_landed_con_nei_theologian_bonus_more_modifier = yes
				el_landed_contrat_con_nei_succeed_effect = yes
				scope:el_landed_con_nei_fin = {
					faith = { save_scope_as = el_landed_faith }
				}
				trigger_event = {
					id = el_landed_contract_con_nei.0007
					days = 19
				}
				scope:el_employer = {
					trigger_event =  {
						id = el_landed_contract_con_nei.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_scholars }
				el_landed_education_bonus_less_modifier = { SKILL = learning }
				el_landed_intelect_bonus_less_modifier = { SCOPE = el_adventurer_fin }
				el_landed_con_nei_fervor_bonus_less_modifier = yes
				el_landed_con_nei_reformed_bonus_less_modifier = yes
				el_landed_con_nei_cynical_bonus_less_modifier = yes
				el_landed_con_nei_theologian_bonus_less_modifier = yes
				trigger_event = {
					id = el_landed_contract_con_nei.0007
					days = 19
				}
				scope:el_employer = {
					trigger_event =  {
					id = el_landed_contract_con_nei.0003
					days = { 10 16 }
					}
				}				
			}
		}
		ai_chance = {
				base = 100
		}
	}
}

#tutor succeed
el_landed_contract_con_nei.0002 = {
	type = character_event
	theme = landless_adventurer
	title = el_landed_contract_con_nei.0002.t
	desc = el_landed_contract_con_nei.0002.desc
	override_background = { reference = holy_site_generic }
	window = big_event_window
	#success
	left_portrait = {
		character = scope:el_adventurer_fin
		animation = storyteller  
		camera = camera_event_scheme_center_look_semi_right
	}
	center_portrait = {
		character = scope:el_landed_con_nei_fin
		animation = prayer  
		camera = camera_event_very_right
	}
	#success
	right_portrait = {
		character = scope:el_merchant_1
		animation = prayer  
		camera = camera_event_very_right
	}
	trigger = {
		scope:el_employer = { is_alive = yes }
		scope:el_adventurer_fin = { is_alive = yes }
	}
	on_trigger_fail = {
		trigger_event = el_landed_contract_con_nei.0006
	}	
	immediate = {
		scope:el_landed_con_nei_fin = {
			if = {
				limit = { is_alive = yes }
				add_character_flag = {
					flag = el_landed_con_nei_cant_be_conv
					days = 1825
				}
			}
		}
		scope:el_landed_con_nei_fin = {
			if = {
				limit = { NOT = { has_trait = zealous } }
				set_character_faith = scope:el_employer.faith
				hidden_effect = {
					every_close_family_member = {
						limit = {
							is_courtier_of = scope:el_landed_con_nei_fin
							is_ruler = no
						}
						set_character_faith = scope:el_employer.faith
					}		
				}
			}
		}				
		scope:el_land_con_nei_location = {
			county = {
				if = {
					limit = { exists =  scope:el_adventurer_fin.var:el_con_nei_ruler }
					random_neighboring_county = {
						limit = {
							faith = scope:el_landed_faith
							NOT = { faith = scope:el_employer.faith }
						}
						set_county_faith = scope:el_employer.faith
						add_county_modifier = {
							modifier = el_landed_con_nei_county_success_modifier
							days = 1825
						}
					}					
				}
				set_county_faith = scope:el_employer.faith
				add_county_modifier = {
					modifier = el_landed_con_nei_county_success_modifier
					days = 1825
				}
			}
		}
		create_character = {
			template = el_merchant_template
			gender_female_chance = 30
			location = scope:el_adventurer_fin.location
			faith = scope:el_adventurer_fin.location.faith
			culture = scope:el_adventurer_fin.location.culture
			save_scope_as = el_merchant_1
		}	
	}
	option = {
		name = el_landed_contract_con_nei.0002.a
		scope:el_adventurer_fin = {
			start_travel_plan = {
				destination = scope:el_employer.capital_province
				on_arrival_event = el_landed_contract_con_nei.0024
			}
		}		
	}
	after = {
		if = {
			limit = { scope:el_employer = { is_alive = no } }
			scope:el_adventurer_fin = {
				remove_variable = el_con_nei_ruler
				current_travel_plan ?= { resume_travel_plan = yes }
			}
		}
		hidden_effect = {
			if = {
				limit = { exists = scope:el_merchant_1 }
				scope:el_merchant_1 = {
					if = {
						limit = { is_alive = yes }
						death = {
							death_reason = death_vanished 
						}
					}
				}
			}
		}
	}			
}

#train fail
el_landed_contract_con_nei.0003 = {
	type = character_event
	theme = landless_adventurer
	title = el_landed_contract_con_nei.0003.t
	desc = el_landed_contract_con_nei.0003.desc
	override_background = { reference = holy_site_generic }
	window = big_event_window
	left_portrait = {
		character = scope:el_merchant_1
		animation = aggressive_unarmed
		camera = camera_event_scheme_center_look_semi_right	
	}
	center_portrait = {
		character = scope:el_merchant_2
		animation = rage  
		camera = camera_event_scheme_center_look_semi_right	
	}
	right_portrait = {
		character = scope:el_adventurer_fin
		animation = shock
	}
	trigger = {
		scope:el_employer = { is_alive = yes }
		scope:el_adventurer_fin = { is_alive = yes }
	}
	on_trigger_fail = {
		trigger_event = el_landed_contract_con_nei.0006
	}	
	immediate = {
		scope:el_landed_con_nei_fin = {
			if = {
				limit = { is_alive = yes }
				add_character_flag = {
					flag = el_landed_con_nei_cant_be_conv
					days = 1825
				}
			}
		}
		scope:el_adventurer_fin = {
			increase_wounds_no_death_effect = { REASON = fight }
		}
		create_character = {
			template = el_merchant_template
			gender_female_chance = 30
			location = scope:el_adventurer_fin.location
			faith = scope:el_adventurer_fin.location.faith
			culture = scope:el_adventurer_fin.location.culture
			save_scope_as = el_merchant_1
		}
		create_character = {
			template = el_merchant_template
			gender_female_chance = 30
			location = scope:el_adventurer_fin.location
			faith = scope:el_adventurer_fin.location.faith
			culture = scope:el_adventurer_fin.location.culture
			save_scope_as = el_merchant_2
		}		
	}		
	option = {
		name = el_landed_contract_con_nei.0003.a
		scope:el_adventurer_fin = {
			start_travel_plan = {
				destination = scope:el_employer.capital_province
				on_arrival_event = el_landed_contract_con_nei.0025
			}
		}

	}
	after = {
		hidden_effect = {
			if = {
				limit = { exists = scope:el_merchant_1 }
				scope:el_merchant_1 = {
					if = {
						limit = { is_alive = yes }
						death = {
							death_reason = death_vanished 
						}
					}
				}
			}
			if = {
				limit = { exists = scope:el_merchant_2 }
				scope:el_merchant_2 = {
					if = {
						limit = { is_alive = yes }
						death = {
							death_reason = death_vanished 
						}
					}
				}
			}
		}		
	}		
}

#succeed contract
el_landed_contract_con_nei.0004 = {
	type = character_event
	theme = landless_adventurer
	title = el_landed_contract_con_nei.0004.t
	desc = el_landed_contract_con_nei.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_con_nei
			value = yes
			days = 730
		}
	}		
	option = {
		name = el_landed_contract_con_nei.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_con_nei.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_con_nei.0005 = {
	type = character_event
	theme = landless_adventurer
	title = el_landed_contract_con_nei.0005.t
	desc = el_landed_contract_con_nei.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_con_nei
			value = yes
			days = 730
		}
		scope:el_landed_con_nei_fin = {
			if = {
				limit = { is_alive = yes }
				add_character_flag = {
					flag = el_landed_con_nei_cant_be_conv
					days = 1825
				}
			}
		}
	}		
	option = {
		name = el_landed_contract_con_nei.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_con_nei.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_con_nei.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_con_nei.0010 = {
	type = character_event
	theme = landless_adventurer
	title = el_landed_contract_con_nei.0010.t
	desc = el_landed_contract_con_nei.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_con_nei.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 }
				exists = capital_province
				NOT = { faith =   scope:el_employer.faith  }
				NOT = { has_character_flag = el_landed_con_nei_cant_be_conv }
				highest_held_title_tier  < tier_kingdom
				capital_province = { squared_distance = { target = scope:el_employer.capital_province value < el_squared_distance_max_travel } }
				NOT = { capital_county.faith = scope:el_employer.faith }
				NOT = {
					any_held_title = {
						is_head_of_faith = 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 }
				exists = capital_province
				NOT = { faith =   scope:el_employer.faith  }
				NOT = { has_character_flag = el_landed_con_nei_cant_be_conv }
				highest_held_title_tier  < tier_kingdom
				capital_province = { squared_distance = { target = scope:el_employer.capital_province value < el_squared_distance_max_travel } }
				NOT = { capital_county.faith = scope:el_employer.faith }
				NOT = {
					any_held_title = {
						is_head_of_faith = yes
					}
				}
			}
			root = {
				add_to_variable_list = {
					name = el_landed_target_list
					target = prev
				}
			}
		}
		every_neighboring_and_across_water_top_liege_realm_owner = {
			limit = { NOT = { is_tributary_of = scope:el_employer } }
			every_sub_realm_county = {
				limit = {
					holder = {
						NOT = { faith =  scope:el_employer.faith  }
						NOT = { has_character_flag = el_landed_con_nei_cant_be_conv }
						highest_held_title_tier  < tier_kingdom
						NOT = {
							any_held_title = {
								is_head_of_faith = yes
							}
						}
						capital_province = {
							squared_distance = { target = scope:el_employer.capital_province value < el_squared_distance_max_travel } 
						}
					}
					NOT = { faith = scope:el_employer.faith }
				}
				holder = { 
					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 } }
			every_sub_realm_county = {
				limit = {
					holder = {
						NOT = { faith =  scope:el_employer.faith  }
						NOT = { has_character_flag = el_landed_con_nei_cant_be_conv }
						highest_held_title_tier  < tier_kingdom
						NOT = {
							any_held_title = {
								is_head_of_faith = yes
							}
						}
						capital_province = {
							squared_distance = { target = scope:el_employer.capital_province value < el_squared_distance_max_travel } 
						}
					}
					NOT = { faith = scope:el_employer.faith }
				}
				holder = { 
					root = {
						add_to_variable_list = {
							name = el_landed_target_list
							target = prev
						}
					}
				}
			}
		}
		top_liege ?= {
			every_sub_realm_county = {
				limit = {
					holder = {
						NOT = { this = scope:el_employer }
						NOT = { this = scope:el_employer.top_liege }
						NOT = { is_vassal_or_below_of = scope:el_employer }
						NOT = { faith =  scope:el_employer.faith  }
						NOT = { has_character_flag = el_landed_con_nei_cant_be_conv }
						highest_held_title_tier  < tier_kingdom
						NOT = {
							any_held_title = {
								is_head_of_faith = yes
							}
						}
						capital_province = {
							squared_distance = { target = scope:el_employer.capital_province value < el_squared_distance_max_travel } 
						}
					}
					NOT = { faith = scope:el_employer.faith }
				}
				holder = { 
					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_con_nei_fin
		}		
	}
	option = {
		name = el_landed_contract_con_nei.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_con_nei_fin }
		}
		scope:el_landed_con_nei_fin = {
			capital_province = {
				save_scope_as = el_land_con_nei_location
			}
		}
		clear_variable_list = el_landed_target_list 
		remove_variable = el_landed_target
		scope:el_adventurer_fin = {
			start_travel_plan = {
				destination = scope:el_land_con_nei_location
				on_arrival_event =  el_landed_contract_con_nei.0001
			}
		}
	}
}

el_landed_contract_con_nei.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_con_nei.0007
	}
	immediate = {
		scope:el_employer = {
			trigger_event = el_landed_contract_con_nei.0010
		}
	}
}

el_landed_contract_con_nei.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_con_nei.0004
			}
		}
	}
}

el_landed_contract_con_nei.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_con_nei.0005
			}
		}
	}
}