#############################################
#
# Bankruptcy events
#
# Event ID 38000-38099 is reserved
#
#############################################

# Patch 2.00: disabled
# Ruler is in the red. Go to the moneylenders.
# character_event = {
#	id = 38000
#	desc = EVTDESC38000
#	picture = GFX_evt_council
#
#	only_playable = yes
#
#	trigger = {
#		wealth < -50
#
#		trigger_if = {
#			limit = { has_character_flag = loan_refused }
#
#			had_character_flag = {
#				flag = loan_refused
#				years >= 1
#			}
#		}
#
#		NOT = { has_character_flag = loan_taken }
#	}
#
#	mean_time_to_happen = {
#		months = 1
#	}
#
#	option = {
#		name = EVTOPTA38000 # Go to the moneylenders - Favorable terms
#		tooltip_info = stewardship
#
#		trigger = { stewardship >= 8 }
#
#		wealth = 200
#
#		character_event = {
#			id = 38001
#			years = 5
#			tooltip = EVTTOOLTIP38001
#		}
#
#		set_character_flag = loan_taken
#		clr_character_flag = loan_refused
#	}
#
#	option = {
#		name = EVTOPTB38000 # Go to the moneylenders - Unfavorable terms
#
#		trigger = { stewardship < 8 }
#
#		wealth = 200
#
#		character_event = {
#			id = 38002
#			years = 5
#			tooltip = EVTTOOLTIP38002
#		}
#
#		set_character_flag = loan_taken
#		clr_character_flag = loan_refused
#	}
#
#	option = {
#		name = EVTOPTC38000 # Do not go to the moneylenders
#
#		set_character_flag = loan_refused
#	}
# }

# Follow-up to favorable loan terms
# character_event = {
#	id = 38001
#	desc = EVTDESC38001
#	picture = GFX_evt_council
#
#	is_triggered_only = yes
#
#	option = {
#		name = EVTOPTA38001 # Return the money
#
#		trigger = { wealth >= 250 }
#
#		wealth = -250
#		clr_character_flag = loan_taken
#	}
#
#	option = {
#		name = EVTOPTB38001 # Cannot afford it
#
#		trigger = { wealth < 250 }
#
#		character_event = {
#			id = 38002
#			years = 1
#			tooltip = EVTTOOLTIP38002B
#		}
#	}
#
# }

# # Follow-up to unfavorable loan terms
# character_event = {
# 	id = 38002
# 	desc = EVTDESC38002
# 	picture = GFX_evt_council
#
# 	is_triggered_only = yes
#
# 	option = {
# 		name = EVTOPTA38002 # Return the money
#
# 		trigger = { wealth >= 300 }
#
# 		wealth = -300
# 		clr_character_flag = loan_taken
# 	}
#
# 	option = {
# 		name = EVTOPTB38002 # Cannot afford it
#
# 		trigger = { wealth < 300 }
#
# 		prestige = -25
#
# 		character_event = {
# 			id = 38002
# 			years = 1
# 			tooltip = EVTTOOLTIP38002B
# 		}
# 	}
#
# 	option = {
# 		name = EVTOPTC38002 # Get rid of them!
#
# 		trigger = {
# 			OR = {
# 				trait = wroth
# 				trait = lunatic
# 				trait = possessed
# 			}
# 		}
#
# 		add_character_modifier = {
# 			name = offended_moneylenders
# 			years = 5
# 		}
#
# 		clr_character_flag = loan_taken
# 	}
# }

# Ruler in red, lean on vassals
character_event = {
	id = 38010
	desc = EVTDESC38010
	picture = GFX_evt_council

	is_triggered_only = yes # on_yearly_pulse, random_events, bankruptcy_events

	only_playable = yes
	prisoner = no

	trigger = {
		wealth < -50

		any_vassal = {
			opinion = {
				who = ROOT
				value >= 0
			}

			wealth >= 10

			check_variable = {
				which = loaned_to_liege
				value == 0
			}
		}

		# This is mostly here for modders. As-written, this value should always be 0 once the event chain is concluded
		check_variable = {
			which = loan_size
			value == 0
		}
	}

	immediate = {
		random_vassal = {
			limit = {
				wealth >= 250

				opinion = {
					who = ROOT
					value >= 0
				}

				check_variable = {
					which = loaned_to_liege
					value == 0
				}
			}

			save_event_target_as = rich_vassal
		}

		random_vassal = {
			limit = {
				wealth >= 50

				opinion = {
					who = ROOT
					value = 0
				}

				check_variable = {
					which = loaned_to_liege
					value == 0
				}
			}

			preferred_limit = {
				NOT = { character =  event_target:rich_vassal }
			}

			save_event_target_as = middle_vassal
		}

		random_vassal = {
			limit = {
				wealth >= 10

				opinion = {
					who = ROOT
					value = 0
				}

				check_variable = {
					which = loaned_to_liege
					value == 0
				}
			}

			preferred_limit = {
				NOR = {
					character =  event_target:rich_vassal
					character =  event_target:middle_vassal
				}
			}

			save_event_target_as = poor_vassal
		}
	}

	option = {
		name = EVTOPTA38010 # Ask one vassal for large

		trigger = {
			event_target:rich_vassal = { always = yes }
		}

		event_target:rich_vassal = {
			show_scope_change = no

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

			hidden_effect = {
				set_variable = {
					which = loaned_to_liege
					value = -250
				}

				ROOT = {
					set_variable = {
						which = loan_size
						value = 250
					}
				}
			}

			character_event = {
				id = 38011
				days = 1
				tooltip = EVTTOOLTIP38013
			}
		}

		ai_chance = { factor = 10 }
	}

	option = {
		name = EVTOPTC38010 # ask one vassal for medium

		trigger = {
			event_target:middle_vassal = { always = yes }
		}

		event_target:middle_vassal = {
			show_scope_change = no

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

			hidden_effect = {
				set_variable = {
					which = loaned_to_liege
					value = -50
				}

				ROOT = {
					set_variable = {
						which = loan_size
						value = 50
					}
				}
			}

			character_event = {
				id = 38011
				days = 1
				tooltip = EVTTOOLTIP38012
			}
		}

		ai_chance = {
			factor = 1

			mult_modifier = {
				factor = 10
				wealth >= -100
			}
		}
	}

	option = {
		name = EVTOPTB38010 # ask one vassal for small

		event_target:poor_vassal = {
			show_scope_change = no

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

			hidden_effect = {
				set_variable = {
					which = loaned_to_liege
					value = -10
				}

				ROOT = {
					set_variable = {
						which = loan_size
						value = 10
					}
				}
			}

			character_event = {
				id = 38011
				days = 1
				tooltip = EVTTOOLTIP38011
			}
		}
	}
}

# Request
character_event = {
	id = 38011
	desc = EVTDESC38011
	picture = GFX_evt_council

	is_triggered_only = yes

	option = {
		name = EVTOPTA_38011 # Give the money in exchange for a favor

		trigger = {
			has_dlc = "Conclave"
			NOT = { holds_favor_on = FROM }

			check_variable = {
				which = loaned_to_liege
				value == -250
			}
		}

		custom_tooltip = {
			text = EVTOPTA_38011_TOOLTIP

			add_favor = FROM
			wealth = loaned_to_liege

			FROM = {
				character_event = {
					id = 38016 # Payment accepted, for favor
					days = 1
				}
			}
		}

		ai_chance = {
			factor = 5

			trigger = {
				opinion = {
					who = FROM
					value >= 0
				}
			}

			mult_modifier = {
				factor = 1.5

				opinion = {
					who = FROM
					value >= 25
				}
			}

			mult_modifier = {
				factor = 2

				opinion = {
					who = FROM
					value >= 50
				}
			}

			mult_modifier = {
				factor = 3

				opinion = {
					who = FROM
					value >= 75
				}
			}
		}
	}

	option = {
		name = ACCEPT # Accept

		wealth = loaned_to_liege

		FROM = {
			character_event = {
				id = 38015 # Payment accepted
				days = 1
			}
		}

		ai_chance = {
			factor = 1

			trigger = {
				opinion = {
					who = FROM
					value = 0
				}
			}

			mult_modifier = {
				factor = 1.5

				opinion = {
					who = FROM
					value >= 25
				}
			}

			mult_modifier = {
				factor = 2

				opinion = {
					who = FROM
					value >= 50
				}
			}

			mult_modifier = {
				factor = 3

				opinion = {
					who = FROM
					value >= 75
				}
			}
		}
	}

	option = {
		name = DECLINE

		FROM = {
			show_scope_change = no
			opinion = {
			name = refused_money_request
				who = ROOT
			years = 5
		}

			character_event = {
				id = 38014 # Refusal to pay
				days = 1
			}
		}

		ai_chance = {
			factor = 1

			trigger = {
				opinion = {
					who = FROM
					value < 90
				}
			}

			mult_modifier = {
				factor = 2

				opinion = {
					who = FROM
					value < 10
				}
			}
		}
	}
}

# Refusal to pay
character_event = {
	id = 38014
	desc = EVTDESC38014
	picture = GFX_evt_council

	is_triggered_only = yes

	option = {
		name = OK

		prestige = -1

		hidden_effect = {
			set_variable = {
				which = loan_size
				value = 0
			}
		}
	}
}

# Payment accepted
character_event = {
	id = 38015
	desc = EVTDESC38015
	picture = GFX_evt_council

	is_triggered_only = yes

	option = {
		name = OK

		wealth = loan_size
		prestige = 1

		hidden_effect = {
			set_variable = {
				which = loan_size
				value = 0
			}
		}
	}

}

# Payment accepted, for a favor
character_event = {
	id = 38016
	desc = EVTDESC38016
	picture = GFX_evt_council

	is_triggered_only = yes

	option = {
		name = OK

		custom_tooltip = {
			text = EVTOPT_38016_TOOLTIP
			wealth = 250
		}

		hidden_effect = {
			set_variable = {
				which = loan_size
				value = 0
			}
		}
	}
}

# Ruler is in the red. Seize Church Properties
character_event = {
	id = 38020
	desc = EVTDESC38020
	picture = GFX_evt_pope_feast

	is_triggered_only = yes # on_yearly_pulse, random_events, bankruptcy_events

	only_playable = yes
	religion_group = christian
	prisoner = no

	trigger = {
		is_catholic_branch = yes
		is_heretic = no
		wealth < -50

		any_vassal = {
			is_theocracy = yes
			prisoner = no
		}

		NOR = {
			trait = zealous
			has_character_flag = stolen_church_properties
		}
	}

	immediate = {
		set_character_flag = stolen_church_properties
	}

	option = {
		name = EVTOPTA38020 # There are fat bishops, seize them !

		trigger = {
			any_vassal = {
				is_theocracy = yes

				any_demesne_title = {
					tier = BARON
					num_of_buildings >= 1
				}
			}
		}

		random_vassal = {
			show_scope_change = no

			limit = {
				is_theocracy = yes

				any_demesne_title = {
					tier = BARON
					num_of_buildings >= 1
				}
			}

			character_event = {
				id = 38021
				days = 1
				tooltip = EVTTOOLTIP38021
			}

			imprison = ROOT
		}

		wealth = 300
		add_trait_fully_silently_cruel_effect = yes
		pf_tradition_minus8_effect = yes

		ai_chance = {
			factor = 1

			trigger = { trait = cynical }
		}
	}

	option = {
		name = EVTOPTB38020 # Times are harsh, force them to share the costs!

		wealth = 100

		random = {
			chance = 25

			random_vassal = {
				show_scope_change = no

				limit = { is_theocracy = yes }

				character_event = {
					id = 38021
					days = 1
					tooltip = EVTTOOLTIP38021
				}
			}
		}


		pf_tradition_minus4_effect = yes
	}

	option = {
		name = EVTOPTC38020 # Compromise with them to find money!

		wealth = 50
		piety = -25
		prestige = -50
	}

	option = {
		name = EVTOPTD38020 # Never! I will save my Christian soul!

		piety = 50

		if = {
			limit = {
				NOT = { trait = zealous }
			}

			change_stewardship = -1
			add_trait_fully_silently_zealous_effect = yes
		}

		pf_tradition_plus1_effect = yes
	}
}


character_event = {
	id = 38021
	desc = EVTDESC38021
	picture = GFX_evt_lunatic

	is_triggered_only = yes

	immediate = {
		random_demesne_title = {
			limit = { tier = BARON }

			destroy_random_building = yes
		}

		wealth = -100
	}

	option = {
		name = EVTOPTA38021 # They are cursed in the eyes of God!

		FROM = {
			show_scope_change = no

			character_event = {
				id = 38022 # Excommunication
				days = 63
				tooltip = EVTTOOLTIP38022
			}
		}

		ai_chance = {
			factor = 1

			mult_modifier = {
				factor = 10

				opinion = {
					who = FROM
					value < 50
				}
			}

			mult_modifier = {
				factor = 10
				trait = zealous
			}

			mult_modifier = {
				factor = 100
				prisoner = yes
			}
		}
	}

	option = {
		name = EVTOPTB38021 # Jesus said to forgive them for their sins.

		opinion = {
			name = grudge_from_sin
			who = ROOT
			years = 5
		}

		ai_chance = {
			factor = 1

			trigger = { prisoner = no }

			mult_modifier = {
				factor = 2
				trait = kind
			}

			mult_modifier = {
				factor = 1.5
				trait = humble
			}
		}
	}
}

# We have been judged in the eyes of God
character_event = {
	id = 38022
	desc = EVTDESC38022
	picture = GFX_evt_bad_news

	is_triggered_only = yes
	hide_from = yes

	option = {
		name = EVTOPTA38022

		piety = -50

		if = {
			limit = { can_excommunicate_trigger = yes }
			add_trait = excommunicated
		}
	}
}

# On-Action: Mercs go on a rampage
letter_event = {
	id = 38050
	desc = EVTDESC38050

	is_triggered_only = yes # on_merc_rampage

	ai = no

	option = {
		name = EVTOPTA38050
	}
}

# On-Action: Mercs leave in disgust
letter_event = {
	id = 38051
	desc = EVTDESC38051

	is_triggered_only = yes # on_merc_leave

	ai = no

	option = {
		name = EVTOPTA38051
	}
}

# On-Action: Mercs switch side FROM you
letter_event = {
	id = 38052
	desc = EVTDESC38052

	is_triggered_only = yes # on_merc_turn_coat_from

	ai = no

	option = {
		name = EVTOPTA38052
	}
}

# On-Action: Mercs switch side TO you
letter_event = {
	id = 38053
	desc = EVTDESC38053

	is_triggered_only = yes # on_merc_turn_coat_to

	ai = no

	option = {
		name = EVTOPTA38053
	}
}

# On-Action: Holy Order leaves your service
letter_event = {
	id = 38054
	desc = EVTDESC38054
	border = GFX_event_letter_frame_war

	is_triggered_only = yes # on_holy_order_leave

	ai = no

	option = {
		name = EVTOPTA38054
	}
}

# Offer to buy a title from indebted liege
character_event = {
	id = 38060
	desc = EVTDESC38060
	picture = GFX_evt_council

	is_triggered_only = yes # on_yearly_pulse, random_events, bankruptcy_events

	only_playable = yes
	min_age = 16
	prisoner = no
	only_capable = yes

	trigger = {
		wealth >= 250
		independent = no
		NOT = { has_character_flag = offered_to_buy_liege_titles }

		liege = {
			wealth < -50

			any_demesne_title = {
				lower_real_tier_than = PREV
				has_de_jure_pretension = ROOT
				can_be_given_away = yes

				trigger_if = {
					limit = { tier = COUNT }

					location = {
						is_capital = no
					}
				}
			}
		}
	}

	weight_multiplier = {
		factor = 1

		mult_modifier = {
			factor = 1.33
			liege = { wealth < -75 }
		}

		mult_modifier = {
			factor = 1.33
			liege = { wealth < -100 }
		}

		mult_modifier = {
			factor = 1.33
			liege = { wealth < -125 }
		}

		mult_modifier = {
			factor = 1.33
			liege = { wealth < -150 }
		}

		mult_modifier = {
			factor = 1.33
			wealth >= 275
		}

		mult_modifier = {
			factor = 1.33
			wealth >= 300
		}

		mult_modifier = {
			factor = 1.33
			wealth >= 325
		}

		mult_modifier = {
			factor = 1.33
			wealth >= 350
		}
	}

	immediate = {
		set_character_flag = offered_to_buy_liege_titles

		liege = {
			random_demesne_title = {
				limit = {
					lower_real_tier_than = PREV
					has_de_jure_pretension = ROOT
					can_be_given_away = yes

					trigger_if = {
						limit = { tier = COUNT }

						location = {
							is_capital = no
						}
					}
				}

				save_event_target_as = title_for_sale
			}
		}
	}

	option = {
		name = EVTOPTC38060

		trigger = {
			has_dlc = "Conclave"
			NOT = { holds_favor_on = liege }
		}

		liege = {
			show_scope_change = no

			character_event = {
				id = 38062
				days = 1
				tooltip = EVTTOOLTIP38061
			}
		}

		tooltip = {
			grant_title = event_target:title_for_sale
			add_favor = liege

			transfer_scaled_wealth = {
				to = liege
				value = 1
				min = 300
				max = 300
			}
		}

		ai_chance = { factor = 100 }
	}

	option = {
		name = EVTOPTA38060

		ai_chance = { factor = 100 }

		liege = {
			show_scope_change = no

			character_event = {
				id = 38061
				days = 1
				tooltip = EVTTOOLTIP38061
			}
		}

		tooltip = {
			grant_title = event_target:title_for_sale

			transfer_scaled_wealth = {
				to = liege
				value = 1
				min = 200
				max = 200
			}
		}
	}

	option = {
		name = EVTOPTB38060

		ai_chance = { factor = 0 }
	}
}

# Liege receives offer to sell land
character_event = {
	id = 38061
	desc = EVTDESC38061
	picture = GFX_evt_council

	is_triggered_only = yes

	option = {
		name = EVTOPTA38061

		if = {
			# If the title has been given away in the meantime, or lost
			limit = { NOT = { has_landed_title = event_target:title_for_sale } }

			random_demesne_title = {
				limit = {
					lower_real_tier_than = ROOT

					trigger_if = {
						limit = { tier = COUNT }

						location = {
							is_capital = no
						}
					}
				}

				preferred_limit = {
					has_de_jure_pretension = FROM
					can_be_given_away = yes
				}

				preferred_limit = {
					can_be_given_away = yes
				}

				save_event_target_as = title_for_sale
			}
		}

		transfer_scaled_wealth = {
			to = FROM
			value = 1
			min = 200
			max = 200
		}

		FROM = {
			show_scope_change = no

			grant_title = event_target:title_for_sale
		}

		ai_chance = { factor = 100 }
	}

	option = {
		name = EVTOPTB38061

		ai_chance = { factor = 0 }
	}
}

# Liege receives offer to sell land for favor
character_event = {
	id = 38062
	desc = EVTDESC38062
	picture = GFX_evt_council

	is_triggered_only = yes

	option = {
		name = EVTOPTA38062

		if = {
			# If the title has been given away in the meantime, or lost
			limit = { NOT = { has_landed_title = event_target:title_for_sale } }

			random_demesne_title = {
				limit = {
					lower_real_tier_than = ROOT

					trigger_if = {
						limit = { tier = COUNT }

						location = {
							is_capital = no
						}
					}
				}

				preferred_limit = {
					has_de_jure_pretension = FROM
					can_be_given_away = yes
				}

				preferred_limit = {
					can_be_given_away = yes
				}

				save_event_target_as = title_for_sale
			}
		}

		transfer_scaled_wealth = {
			from = FROM
			value = 1
			min = 300
			max = 300
		}

		FROM = {
			show_scope_change = no

			grant_title = event_target:title_for_sale
			add_favor = ROOT
		}

		ai_chance = { factor = 100 }
	}

	option = {
		name = EVTOPTB38062

		ai_chance = { factor = 0 }
	}
}