# Scripted effects for adventurer events

# ROOT = adventurer
plus_find_adventurer_target_effect = {
	if = {
		limit = { NOT = { event_target:adventurer_target = { always = yes } } }

		random_opinion_modifier_target = {
			limit = {
				reverse_has_opinion_modifier = {
					who = PREV
					name = opinion_claimant_adventure_target
				}
			}

			save_event_target_as = adventurer_target
		}
	}
}

# ROOT = adventurer
plus_clear_adventurer_location_effect = {
	any_province = {
		ROOT = { clr_character_flag = adventurer_location_@PREV }
	}
}

# ROOT = adventurer
plus_find_adventurer_location_effect = {
	random_province = {
		limit = { ROOT = { has_character_flag = adventurer_location_@PREV } }
		save_event_target_as = adventurer_location
	}
}

# ROOT = adventurer
plus_find_adventurer_origin_effect = {
	random_province = {
		limit = { ROOT = { has_character_flag = adventurer_origin_@PREV } }
		save_event_target_as = adventurer_origin
	}
}

# ROOT = adventurer
plus_find_adventurer_target_title_effect = {
	if = {
		limit = { NOT = { event_target:adventurer_target_title = { always = yes } } }

		random_landed_title = {
			limit = { ROOT = { has_character_flag = adventurer_target_title_@PREV } }
			save_event_target_as = adventurer_target_title
		}
	}
}

# ROOT = adventurer, THIS = ruler
plus_travel_to_ruler_effect = {
	plus_find_adventurer_target_title_effect = yes
	plus_clear_adventurer_location_effect = yes

	capital_scope = {
		ROOT = { set_character_flag = adventurer_location_@PREV }
	}

	if = {
		limit = { plus_closest_to_adventurer_trigger = yes }
		ROOT = { add_character_modifier = { name = do_not_disturb days = 5 hidden = yes } }
		character_event = { id = adventurerPlus.115 days = 5 }
	}
	else_if = {
		limit = { plus_close_to_adventurer_trigger = yes }
		ROOT = { add_character_modifier = { name = do_not_disturb days = 10 hidden = yes } }
		character_event = { id = adventurerPlus.115 days = 10 }
	}
	else_if = {
		limit = { plus_not_close_to_adventurer_trigger = yes }
		ROOT = { add_character_modifier = { name = do_not_disturb days = 15 hidden = yes } }
		character_event = { id = adventurerPlus.115 days = 15 }
	}
	else_if = {
		limit = { plus_far_from_adventurer_trigger = yes }
		ROOT = { add_character_modifier = { name = do_not_disturb days = 20 hidden = yes } }
		character_event = { id = adventurerPlus.115 days = 30 }
	}
	else_if = {
		limit = { plus_furthest_from_adventurer_trigger = yes }
		ROOT = { add_character_modifier = { name = do_not_disturb days = 25 hidden = yes } }
		character_event = { id = adventurerPlus.115 days = 60 }
	}
	else = {
		ROOT = { add_character_modifier = { name = do_not_disturb days = 30 hidden = yes } }
		character_event = { id = adventurerPlus.115 days = 90 }
	}
}

# ROOT = approached character, FROM = adventurer
plus_donate_to_adventurer_effect = {
	if = {
		limit = { wealth >= 300 }
		transfer_scaled_wealth = { to = FROM value = 1 max = 150 min = 150 }
	}
	else_if = {
		limit = { wealth >= 200 }
		transfer_scaled_wealth = { to = FROM value = 1 max = 100 min = 100 }
	}
	else_if = {
		limit = { wealth >= 160 }
		transfer_scaled_wealth = { to = FROM value = 1 max = 80 min = 80 }
	}
	else_if = {
		limit = { wealth >= 120 }
		transfer_scaled_wealth = { to = FROM value = 1 max = 60 min = 60 }
	}
	else_if = {
		limit = { wealth >= 80 }
		transfer_scaled_wealth = { to = FROM value = 1 max = 40 min = 40 }
	}
	else_if = {
		limit = { wealth >= 40 }
		transfer_scaled_wealth = { to = FROM value = 1 max = 20 min = 20 }
	}
	else_if = {
		limit = { wealth >= 1 }
		transfer_scaled_wealth = { to = FROM value = 1 max = 10 min = 10 }
	}
}

# ROOT = ruler, FROM = adventurer
adventurerPlus_recruits_demesne = {
	# Approach the ruler's non-landed courtiers/vassals
	any_courtier = {
		limit = {
			is_ruler = no
			adventurerPlus_can_approach_vassal_trigger = yes
		}

		letter_event = {
			id = adventurerPlus.118
			days = 1
		}
	}

	any_vassal = {
		limit = {
			is_playable = no
			adventurerPlus_can_approach_vassal_trigger = yes
		}

		letter_event = {
			id = adventurerPlus.118
			days = 1
		}
	}

	# Recruit from the ruler's demesne
	any_realm_province = {
		limit = {
			NOT = { has_province_modifier = adventurer_recruited }
			county = { is_occupied = no }
			held_under_PREV = yes
		}

		province_event = {
			id = adventurerPlus.119
			days = 1
		}
	}

	# Adventurer moves on after a time
	if = {
		limit = { num_of_count_titles < 2 }

		FROM = {
			add_character_modifier = { name = do_not_disturb days = 5 hidden = yes }
			character_event = { id = adventurerPlus.110 days = 5 }
		}
	}
	else_if = {
		limit = { num_of_count_titles < 3 }

		FROM = {
			add_character_modifier = { name = do_not_disturb days = 10 hidden = yes }
			character_event = { id = adventurerPlus.110 days = 10 }
		}
	}
	else_if = {
		limit = { num_of_count_titles < 4 }

		FROM = {
			add_character_modifier = { name = do_not_disturb days = 15 hidden = yes }
			character_event = { id = adventurerPlus.110 days = 15 }
		}
	}
	else_if = {
		limit = { num_of_count_titles < 5 }

		FROM = {
			add_character_modifier = { name = do_not_disturb days = 20 hidden = yes }
			character_event = { id = adventurerPlus.110 days = 20 }
		}
	}
	else = {
		FROM = {
			add_character_modifier = { name = do_not_disturb days = 30 hidden = yes }
			character_event = { id = adventurerPlus.110 days = 30 }
		}
	}
}

# THIS = adventurer, must be used inside the adventurer's scope
plus_abandon_adventure_effect = {
	# Free all prisoners
	any_courtier = {
		limit = { prisoner = yes }
		imprison = no
	}

	# Move all courtiers to the new court
	if = {
		limit = { event_target:new_court_home = { always = yes } }
		any_courtier = { move_character = event_target:new_court_home }
	}

	# Remove all adventurer statuses and flags
	remove_character_modifier = planning_claimant_adventure
	remove_do_not_disturb_effect = yes
	clr_character_flag = planning_claimant_adventure
	clr_character_flag = warned_adventurer_target
	clr_character_flag = not_interested_in_movements
	clr_character_flag = found_home

	any_province = {
		PREV = {
			clr_character_flag = adventurer_location_@PREV
			clr_character_flag = adventurer_origin_@PREV
		}
	}

	any_landed_title = {
		PREV = {
			clr_character_flag = adventurer_target_title_@PREV
		}
	}

	any_opinion_modifier_target = {
		limit = {
			has_opinion_modifier = {
				name = opinion_targeted_by_adventurer
				who = PREV
			}
		}

		remove_opinion = {
			name = opinion_targeted_by_adventurer
			who = PREV
		}

		reverse_remove_opinion = {
			name = opinion_claimant_adventure_target
			who = PREV
		}
	}

	any_demesne_title = {
		limit = { adventurer = yes }
		unsafe_destroy_landed_title = THIS
	}

	if = {
		limit = { event_target:new_court_home = { always = yes } }
		move_character = event_target:new_court_home
	}

	clear_event_target = new_court_home
}

# ROOT = adventurer
plus_find_friendly_court_effect = {
	# If I have a non-adventurer title, my most friendly court is my own court
	if = {
		limit = { any_demesne_title = { adventurer = no } }
		save_event_target_as = new_court_home
	}
	else = {
		# Otherwise...
		# Find a court in my home realm that supported me first
		random_opinion_modifier_target = {
			limit = {
				is_playable = yes
				is_landed = yes
			}

			preferred_limit = {
				same_realm = event_target:adventurer_target

				reverse_has_opinion_modifier = {
					name = backed_adventurer
					who = ROOT
				}
			}

			# Failing that, find a court elsewhere that supported me
			preferred_limit = {
				reverse_has_opinion_modifier = {
					name = backed_adventurer
					who = ROOT
				}
			}

			# Failing that, find a court that let me stay
			preferred_limit = {
				reverse_has_opinion_modifier = {
					name = let_adventurer_stay
					who = ROOT
				}
			}
		}

		# Failing all that, my current location will have to do
		if = {
			limit = { NOT = { event_target:new_court_home = { always = yes } } }

			event_target:adventurer_location = {
				owner = { save_event_target_as = new_court_home }
			}
		}
	}
}

# THIS = adventurer
adventurerPlus_buy_troops_effect = {
	while = {
		limit = { wealth >= 50 }
		count = 25

		wealth = -50

		spawn_unit = {
			province = event_target:attack_court
			home = event_target:attack_court
			owner = ROOT
			match_character = event_target:adventurer_target
			match_mult = 0.02
			match_min = 250
			match_max = 1000
			attrition = 0.25
			earmark = claimant_adventure
			cannot_inherit = yes
			disband_on_peace = yes
			merge = yes
		}

		spawn_fleet = {
			province = closest # closest sea zone
			owner = THIS
			disband_on_peace = yes
			merge = yes
			earmark = claimant_adventure

			troops = {
				galleys = { 10 10 }
			}
		}
	}
}