#########################################################
#
# Landless Character Adventures for The Old Gods DLC
# ( TOG.1200 to TOG.1499)
#
#
#########################################################

# Written by Henrik Fhraeus

namespace = TOG

# An ambitious landless claimant begins to plan an adventure for the title
character_event = {
	id = TOG.1200

	is_triggered_only = yes # on_yearly_pulse, random_events, oldgods_adventurers
	hide_window = yes # Only fires for unplayable landless characters

	has_dlc = "The Old Gods"
	min_age = 16
	max_age = 50
	only_capable = yes
	prisoner = no

	trigger = {
		is_ruler = no

		any_claim = {
			has_holder = yes

			OR = {
				real_tier = KING
				is_primary_holder_title_tier = yes
			}

			holder_scope = {
				independent = yes # Only do this vs independent rulers

				trigger_if = {
					limit = { is_nomadic = yes }

					NOR = {
						clan_title = {
							title = PREVPREV
						}

						primary_title = {
							title = PREVPREV
						}
					}
				}

				# Don't target my liege unless I am ambitious
				trigger_if = {
					limit = { is_liege_or_above_of = ROOT }
					ROOT = { trait = ambitious }
				}

				trigger_if = {
					limit = { is_merchant_republic = yes }

					capital_holding = {
						NOT = { de_jure_liege_or_above = PREVPREV }
					}
				}

				ROOT = {
					NOR = {
						obedient = PREV
						non_interference = PREV
					}
				}
			}
		}

		trigger_if = {
			limit = { is_female = yes }

			OR = {
				gender_equality_trigger = yes
				has_law = true_cognatic_succession
			}
		}

		diplomacy >= 7
		health >= 4

		NOR = {
			trait = content
			trait = craven
			has_character_modifier = planning_claimant_adventure

			has_game_rule = {
				name = adventurers
				value = none
			}

			any_spouse = {
				is_ruler = yes
			}

			# Somewhat clunky way of checking that the character does not stand to inherit any titles
			any_heir_title = {
				always = yes
			}
		}
	}

	weight_multiplier = {
		factor = 1

		mult_modifier = {
			factor = 5
			has_character_flag = flag_denied_title
		}

		mult_modifier = {
			factor = 5
			has_character_flag = demon_child_non_pagan
		}

		mult_modifier = {
			factor = 2
			trait = ambitious
		}

		mult_modifier = {
			factor = 2
			trait = envious
		}

		mult_modifier = {
			factor = 1.33
			diplomacy >= 12
		}

		mult_modifier = {
			factor = 1.33
			trait = brave
		}

		mult_modifier = {
			factor = 1.33
			trait = born_in_the_purple
		}

		mult_modifier = {
			factor = 1.2
			trait = proud
		}

		mult_modifier = {
			factor = 1.2
			trait = sayyid
		}

		mult_modifier = {
			factor = 0.67
			trait = slothful
		}

		mult_modifier = {
			factor = 0.67
			trait = humble
		}

		mult_modifier = {
			factor = 0.5
			trait = kind
		}

		mult_modifier = {
			factor = 0.02

			has_game_rule = {
				name = adventurers
				value = rare
			}
		}
		
		### ACR ###
		mult_modifier = {
			factor = 5
			trait = changeling
		}
	}

	immediate = {
		random_claim = {
			limit = {
				has_holder = yes

				OR = {
					real_tier = KING
					is_primary_holder_title_tier = yes
				}

				holder_scope = {
					independent = yes # Only do this vs independent rulers

					trigger_if = {
						limit = { is_nomadic = yes }

						NOR = {
							clan_title = {
								title = PREVPREV
							}

							primary_title = {
								title = PREVPREV
							}
						}
					}

					# Don't target my liege unless I am ambitious
					trigger_if = {
						limit = { is_liege_or_above_of = ROOT }
						ROOT = { trait = ambitious }
					}

					trigger_if = {
						limit = { is_merchant_republic = yes }

						capital_holding = {
							NOT = { de_jure_liege_or_above = PREVPREV }
						}
					}

					ROOT = {
						NOR = {
							obedient = PREV
							non_interference = PREV
						}
					}
				}
			}

			save_event_target_as = adventurer_target_title

			holder_scope = {
				save_event_target_as = adventurer_target

				reverse_opinion = {
					name = opinion_claimant_adventure_target
					who = ROOT
					years = 3
				}

				opinion = {
					name = opinion_targeted_by_adventurer
					who = ROOT
					years = 2
				}

				character_event = { id = TOG.1201 } # Warn the target
			}
		}

		# Can't prepare for the attack from within the realm
		if = {
			limit = { is_vassal_or_below_of = event_target:adventurer_target }
			banish = yes
		}

		add_character_modifier = {
			name = planning_claimant_adventure
			years = 2
			hidden = yes
		}

		character_event = {
			id = TOG.1202 # Fire the attack
			years = 2
		}

		end_inaccessibility_effect = yes # Removes in hiding or in_seclusion
		set_character_flag = do_not_disturb

		character_event = {
			id = TOG.1215 # Safety clearing of do_not_disturb
			years = 2
		}
	}
}

character_event = {
	id = TOG.1201
	desc = EVTDESC_TOG_1201
	picture = GFX_evt_crusaders
	border = GFX_event_normal_frame_war

	is_triggered_only = yes

	ai = no

	option = {
		name = EVTOPTA_TOG_1201
	}
}

# An ambitious landless claimant starts an adventure for the title
character_event = {
	id = TOG.1202

	hide_window = yes # Only fires for unplayable landless characters
	is_triggered_only = yes

	only_capable = yes
	prisoner = no

	trigger = {
		is_ruler = no
		has_claim = event_target:adventurer_target_title

		event_target:adventurer_target_title = {
			holder = event_target:adventurer_target

			holder_scope = {
				NOT = {
					defending_against_claimant = {
						character = ROOT
						title = PREV
					}
				}

				independent = yes # Only do this vs independent rulers

				trigger_if = {
					limit = { is_nomadic = yes }

					NOR = {
						clan_title = {
							title = PREVPREV
						}

						primary_title = {
							title = PREVPREV
						}
					}
				}

				# Title still held by the same ruler
				reverse_has_opinion_modifier = {
					name = opinion_claimant_adventure_target
					who = ROOT
				}

				# Don't target my liege unless I am ambitious
				trigger_if = {
					limit = { is_liege_or_above_of = ROOT }
					ROOT = { trait = ambitious }
				}

				trigger_if = {
					limit = { is_merchant_republic = yes }

					capital_holding = {
						NOT = { de_jure_liege_or_above = PREVPREV }
					}
				}

				ROOT = {
					NOR = {
						obedient = PREV
						non_interference = PREV
					}
				}
			}

			OR = {
				real_tier = KING
				is_primary_holder_title_tier = yes
			}
		}

		NOR = {
			any_spouse = {
				is_ruler = yes
			}

			# Somewhat clunky way of checking that the character does not stand to inherit any titles
			any_heir_title = {
				always = yes
			}
		}
	}

	immediate = {
		liege = {
			character_event = {
				id = TOG.1203
				days = 1
			}

			capital_scope = {
				save_event_target_as = liege_capital
			}
		}

		wealth = 500
		clr_character_flag = do_not_disturb
		set_defacto_liege = ROOT

		create_title = {
			tier = DUKE
			landless = yes
			temporary = yes
			adventurer = yes
			culture = ROOT
			name = CLAIMANT_ADVENTURE
			holder = ROOT
		}

		set_character_flag = claimant_adventurer

		random_list = {
			1 = {
				while = {
					count = 6

					spawn_unit = {
						province = event_target:liege_capital
						home = event_target:liege_capital
						owner = ROOT
						match_character = event_target:adventurer_target
						match_mult = 0.125
						match_min = 300
						match_max = 5000
						attrition = 0.25
						earmark = claimant_adventure
					}
				}
			}

			1 = {
				while = {
					count = 6

					spawn_unit = {
						province = event_target:liege_capital
						home = event_target:liege_capital
						owner = ROOT
						match_character = event_target:adventurer_target
						match_mult = 0.1
						match_min = 300
						match_max = 5000
						attrition = 0.25
						earmark = claimant_adventure
					}
				}
			}

			1 = {
				while = {
					count = 6

					spawn_unit = {
						province = event_target:liege_capital
						home = event_target:liege_capital
						owner = ROOT
						match_character = event_target:adventurer_target
						match_mult = 0.075
						match_min = 300
						match_max = 5000
						attrition = 0.25
						earmark = claimant_adventure
					}
				}
			}
		}

		# Create some decent commander characters
		while = {
			count = 4

			create_random_soldier = {
				random_traits = yes
				dynasty = none
				religion = ROOT
				culture = ROOT
				female = no
				min_age = 25
				max_age = 33
			}
		}

		# Create a fleet, just to be sure
		spawn_fleet = {
			province = closest # closest sea zone
			owner = ROOT
			disband_on_peace = yes
			earmark = claimant_adventure

			troops = {
				galleys = { 150 150 }
			}
		}

		remove_opinion = {
			name = opinion_claimant_adventure_target
			who = event_target:adventurer_target
		}

		log = "TOG.1202: [Root.GetBestName] starts claim adventure for [adventurer_target_title.GetName]"

		unsafe_war = {
			target = event_target:adventurer_target
			casus_belli = claim
			thirdparty_title = event_target:adventurer_target_title
		}
	}
}

character_event = {
	id = TOG.1203
	desc = EVTDESC_TOG_1203
	picture = GFX_evt_crusaders
	border = GFX_event_normal_frame_war

	is_triggered_only = yes

	trigger = {
		FROM = { is_ruler = yes } # The DoW worked and the adventurer has a temporary title
	}

	immediate = {
		FROM = {
			add_trait = adventurer
		}

		any_liege = {
			character_event = {
				id = TOG.1204
			}
		}
	}

	option = {
		name = EVTOPTA_TOG_1203
	}
}

character_event = {
	id = TOG.1204
	desc = EVTDESC_TOG_1204
	picture = GFX_evt_crusaders
	border = GFX_event_normal_frame_war

	is_triggered_only = yes
	show_from_from = yes

	ai = no

	option = {
		name = EVTOPTA_TOG_1203
	}
}

# An ambitious character with no inheritance begins to plan an adventure for a foreign title
character_event = {
	id = TOG.1210

	is_triggered_only = yes # on_yearly_pulse, random_events, oldgods_adventurers
	hide_window = yes # Only fires for unplayable landless characters

	has_dlc = "The Old Gods"
	min_age = 16
	max_age = 50
	only_capable = yes
	prisoner = no

	trigger = {
		is_ruler = no

		trigger_if = {
			limit = { is_female = yes }

			OR = {
				gender_equality_trigger = yes
				has_law = true_cognatic_succession
			}
		}

		diplomacy >= 7
		martial >= 9
		health >= 4

		OR = { # Only these for now
			culture_group = north_germanic
			culture_group = west_germanic
			culture_group = altaic
			culture = norman
			culture = french
			culture = breton
			culture = catalan

			trigger_if = {
				limit = { culture = hungarian }
				year < 1066
			}

			has_character_flag = demon_child_non_pagan
			### ACR ###
			trait = changeling
		}

		OR = {
			father_even_if_dead = {
				OR = {
					higher_real_tier_than = BARON

					father_even_if_dead = {
						higher_real_tier_than = BARON
					}

					mother_even_if_dead = {
						higher_real_tier_than = BARON
					}
				}
			}

			mother_even_if_dead = {
				OR = {
					higher_real_tier_than = BARON

					father_even_if_dead = {
						higher_real_tier_than = BARON
					}

					mother_even_if_dead = {
						higher_real_tier_than = BARON
					}
				}
			}
		}

		NOR = {
			has_game_rule = {
				name = adventurers
				value = none
			}

			trait = content
			trait = craven
			has_character_modifier = planning_duchy_adventure

			any_spouse = {
				is_ruler = yes
			}

			# Somewhat clunky way of checking that the character does not stand to inherit any titles
			any_heir_title = {
				always = yes
			}

			# Handled by event TOG.1200
			any_claim = {
				has_holder = yes

				OR = {
					real_tier = KING
					is_primary_holder_title_tier = yes
				}

				holder_scope = {
					independent = yes

					trigger_if = {
						limit = { is_merchant_republic = yes }

						capital_holding = {
							NOT = { de_jure_liege_or_above = PREVPREV }
						}
					}
				}

				ROOT = {
					NOR = {
						obedient = PREV
						non_interference = PREV
					}
				}
			}
		}
	}

	weight_multiplier = {
		factor = 1

		mult_modifier = {
			factor = 0.25

			is_allowed_to_loot = yes
			has_dlc = "Horse Lords"
		}

		mult_modifier = {
			factor = 2
			trait = ambitious
		}

		mult_modifier = {
			factor = 2

			trigger_if = {
				limit = { culture = norman }
				year < 1250
			}
			trigger_else = {
				culture_group = north_germanic
				year < 1066
			}
		}

		mult_modifier = {
			factor = 5
			has_character_flag = flag_denied_title
		}

		mult_modifier = {
			factor = 5
			has_character_flag = demon_child_non_pagan
		}

		mult_modifier = {
			factor = 1.33
			diplomacy >= 12
		}

		mult_modifier = {
			factor = 1.33
			martial >= 12
		}

		mult_modifier = {
			factor = 1.33
			trait = brave
		}

		mult_modifier = {
			factor = 1.2
			trait = proud
		}

		mult_modifier = {
			factor = 0.33
			trait = slothful
		}

		mult_modifier = {
			factor = 0.33
			trait = humble
		}

		mult_modifier = {
			factor = 0.33
			trait = kind
		}

		mult_modifier = {
			factor = 0.33
			any_liege = { ai = no }
		}

		mult_modifier = {
			factor = 0.33
			top_liege = { real_tier = EMPEROR }
		}

		mult_modifier = {
			factor = 0.02

			has_game_rule = {
				name = adventurers
				value = rare
			}
		}
		
		### ACR ###
		mult_modifier = {
			factor = 5
			trait = changeling
		}
	}

	immediate = {
		random_independent_ruler = {
			limit = {
				is_landed = yes

				NOR = {
					culture = ROOT
					is_liege_or_above_of = ROOT
				}

				ROOT = {
					NOR = {
						obedient = PREV
						non_interference = PREV
					}
				}

				any_realm_province = {
					trigger_if = {
						limit = { is_coastal = yes }

						ROOT = {
							NOR = {
								culture_group = altaic
								culture = hungarian
							}
						}

						distance = {
							who = ROOT
							value < 800
						}
					}
					trigger_else = {
						any_neighbor_province = {
							owner = {
								same_realm = ROOT
							}
						}
					}

					NOR = {
						region = world_europe_north
						de_jure_liege_or_above = d_bjarmia
					}
				}
			}

			save_event_target_as = adventurer_target
			character_event = { id = TOG.1211 } # Warn the target

			opinion = {
				name = opinion_targeted_by_adventurer
				who = ROOT
				years = 2
			}
		}

		opinion = {
			name = opinion_duchy_adventure_target
			who = event_target:adventurer_target
			years = 3
		}

		add_character_modifier = {
			name = planning_duchy_adventure
			years = 2
			hidden = yes
		}

		character_event = {
			id = TOG.1212 # Fire the attack
			years = 2
		}

		end_inaccessibility_effect = yes # Removes in hiding or in_seclusion
		set_character_flag = do_not_disturb

		character_event = {
			id = TOG.1215
			days = 730
		}
	}
}

character_event = {
	id = TOG.1211
	desc = EVTDESC_TOG_1211
	picture = GFX_evt_crusaders
	border = GFX_event_normal_frame_war

	is_triggered_only = yes

	ai = no

	option = {
		name = EVTOPTA_TOG_1211
	}
}

# An ambitious character with no inheritance starts an adventure for a foreign title
# (Temporary title created and a war declared)
character_event = {
	id = TOG.1212

	is_triggered_only = yes
	hide_window = yes # Only fires for unplayable landless characters

	has_dlc = "The Old Gods"
	prisoner = no
	only_capable = yes

	trigger = {
		is_ruler = no

		NOR = {
			any_spouse = {
				is_ruler = yes
			}

			any_heir_title = {
				always = yes
			}
		}

		event_target:adventurer_target = {
			is_landed = yes

			# Previously targeted ruler is still an independent ruler
			reverse_has_opinion_modifier = {
				name = opinion_duchy_adventure_target
				who = ROOT
			}

			NOR = {
				culture = ROOT
				is_liege_or_above_of = ROOT
			}

			any_realm_province = {
				trigger_if = {
					limit = { is_coastal = yes }

					ROOT = {
						NOR = {
							culture_group = altaic
							culture = hungarian
						}
					}

					distance = {
						who = ROOT
						value < 800
					}
				}
				trigger_else = {
					any_neighbor_province = {
						owner = {
							same_realm = ROOT
						}
					}
				}

				NOR = {
					region = world_europe_north
					de_jure_liege_or_above = d_bjarmia
				}
			}

			ROOT = {
				NOR = {
					obedient = PREV
					non_interference = PREV
				}
			}
		}
	}

	immediate = {
		wealth = 500
		clr_character_flag = do_not_disturb

		liege = {
			character_event = {
				id = TOG.1213
				days = 1
			}

			capital_scope = {
				save_event_target_as = liege_capital
			}
		}

		event_target:adventurer_target = {
			random_realm_province = {
				limit = {
					trigger_if = {
						limit = { is_coastal = yes }

						ROOT = {
							NOR = {
								culture_group = altaic
								culture = hungarian
							}
						}

						distance = {
							who = ROOT
							value < 800
						}
					}
					trigger_else = {
						any_neighbor_province = {
							owner = {
								same_realm = ROOT
							}
						}
					}

					NOR = {
						region = world_europe_north
						de_jure_liege_or_above = d_bjarmia
					}
				}

				county = { save_event_target_as = adventurer_target_title }
			}
		}

		set_defacto_liege = ROOT

		create_title = {
			tier = DUKE
			landless = yes
			temporary = yes
			adventurer = yes
			culture = ROOT
			name = CLAIMANT_ADVENTURE
			holder = ROOT
		}

		set_character_flag = duchy_adventurer

		while = {
			count = 3

			spawn_unit = {
				province = event_target:liege_capital
				home = event_target:liege_capital
				owner = THIS
				match_character = event_target:adventurer_target
				match_mult = 0.125
				match_min = 500
				match_max = 10000
				attrition = 0.25
			}
		}

		# Create some decent commander characters
		while = {
			count = 2

			create_random_soldier = {
				random_traits = yes
				dynasty = none
				religion = ROOT
				culture = ROOT
				female = no
				min_age = 25
				max_age = 30
			}
		}

		unsafe_war = {
			target = event_target:adventurer_target
			casus_belli = duchy_adventure
			thirdparty_title = event_target:adventurer_target_title
			tier = DUKE
		}

		log = "TOG.1212: [Root.GetBestName] starts adventure for [adventurer_target_title.Duchy.GetName]"

		remove_opinion = {
			name = opinion_duchy_adventure_target
			who = event_target:adventurer_target
		}

		if = {
			limit = {
				NOR = {
					culture_group = altaic
					culture = hungarian
				}
			}

			# Create a fleet, just to be sure
			spawn_fleet = {
				province = closest # closest sea zone
				owner = ROOT
				disband_on_peace = yes

				troops = {
					galleys = { 200 200 }
				}
			}
		}
	}
}

character_event = {
	id = TOG.1213
	desc = EVTDESC_TOG_1213
	picture = GFX_evt_crusaders
	border = GFX_event_normal_frame_war

	is_triggered_only = yes

	trigger = {
		FROM = { is_ruler = yes } # The DoW worked and the adventurer has a temporary title
	}

	immediate = {
		FROM = {
			add_trait = adventurer
		}

		any_liege = {
			character_event = { id = TOG.1214 }
		}
	}

	option = {
		name = EVTOPTA_TOG_1203
	}
}

character_event = {
	id = TOG.1214
	desc = EVTDESC_TOG_1214
	picture = GFX_evt_crusaders
	border = GFX_event_normal_frame_war

	is_triggered_only = yes
	show_from_from = yes

	ai = no

	option = {
		name = EVTOPTA_TOG_1203
	}
}

#safety clear of DND
character_event = {
	id = TOG.1215

	hide_window = yes
	is_triggered_only = yes

	immediate = {
		clr_character_flag = do_not_disturb
	}
}