﻿namespace = regula_faction_demand

# 0001-0100 - Servitude Faction Events
# 0101-0200 - Heresy Faction Events

############################
# Servitude Faction Events #
############################

# Servitude Faction Demand
regula_faction_demand.0001 = {
	type = letter_event
	sender = scope:faction_leader
	opening = {
		desc = "REGULA_FACTION_DEMAND_SERVITUDE"
	}
	desc = "REGULA_FACTION_DEMAND_SERVITUDE_DESC"

	trigger = {
		exists = scope:faction # May have ceased to exist by the time this triggers
	}

	option = {
		name = "FACTION_DEMAND_ACCEPT"

		root = {
			save_scope_as = faction_target
			add_dread = medium_dread_loss
		}

		regula_faction_servitude_faction_success_effect = {
			FACTION = scope:faction
			LEADER = scope:faction_leader
			TARGET = scope:faction_target
		}

		scope:faction = {
			every_faction_member = {
				trigger_event = regula_faction_demand.0002
			}
		}

		add_prestige = {
			value = medium_prestige_value
			multiply = -1
		}

		ai_chance = {
			base = 1
			modifier = {
				add = 99
				scope:faction = { faction_power >= 100 }
			}
			modifier = {
				add = 50
				scope:faction = { faction_power >= 125 }
			}
			modifier = {
				add = 50
				scope:faction = { faction_power >= 150 }
			}
			modifier = {
				add = 50
				scope:faction = { faction_power >= 200 }
			}
			modifier = {
				factor = 0.1
				any_ally = {
					NOR = {
						target_is_liege_or_above = root
						target_is_vassal_or_below = root
					}
					max_military_strength > root.max_military_strength
				}
			}
			compare_modifier = {
				value = debt_level
				multiplier = 25
			}
		}
	}

	option = {
		name = "FACTION_DEMAND_REFUSE"

		show_as_tooltip = {
			scope:faction = {
				faction_start_war = {}
			}
		}

		root = {
			save_scope_as = faction_target
		}

		scope:faction_leader = {
			trigger_event = regula_faction_demand.0003
		}

		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_boldness = 4.0
			}
			modifier = {
				add = 50
				scope:faction = { faction_power < 80 }
			}
			modifier = {
				add = 100
				scope:faction = { faction_power < 60 }
			}
			modifier = {
				add = 1000
				scope:faction = { faction_power < 40 }
			}
			compare_modifier = {
				value = debt_level
				multiplier = -25
			}
		}
	}
}

# Independence Faction Demand Accepted
regula_faction_demand.0002 = {
	type = letter_event
	sender = scope:faction_target
	opening = {
		desc = "FACTION_DEMAND_INDEPENDENCE_ACCEPTED"
	}
	desc = "FACTION_DEMAND_INDEPENDENCE_ACCEPTED_DESC"

	option = {
		name = "FACTION_DEMAND_ACCEPTED_OPT"
		custom_tooltip = faction_demand.0002.tt
	}
}

# Independence Faction Demand Refused
regula_faction_demand.0003 = {
	type = letter_event
	sender = scope:faction_target
	opening = {
		desc = "FACTION_DEMAND_INDEPENDENCE_REFUSED"
	}
	desc = "FACTION_DEMAND_INDEPENDENCE_REFUSED_DESC"

	option = {
		name = "FACTION_DEMAND_REFUSED_OPT"
		scope:faction = {
			faction_start_war = {}

			every_faction_member = {
				limit = { NOT = { this = scope:faction.faction_leader } }
				trigger_event = regula_faction_demand.0004
			}
		}
	}
}

# Independence Faction Demand Refused Member Notice
regula_faction_demand.0004 = {
	type = letter_event
	sender = scope:faction_target
	opening = {
		desc = "FACTION_DEMAND_INDEPENDENCE_REFUSED"
	}
	desc = "FACTION_DEMAND_INDEPENDENCE_REFUSED_DESC"

	option = {
		name = "FACTION_DEMAND_REFUSED_OPT"
	}
}

# Independence Faction Demand Send Notice to Magister
regula_faction_demand.0006 = {
	type = letter_event
	sender = {
		character = scope:faction_leader
		animation = happiness
	}
	opening = {
		desc = "REGULA_FACTION_DEMAND_SERVITUDE_INFORM_MAGISTER_NOTIFICATION"
	}
	desc = "REGULA_FACTION_DEMAND_SERVITUDE_INFORM_MAGISTER_DESC"

	trigger = {
		exists = scope:faction # May have ceased to exist by the time this triggers
	}

	option = {
		name = "REGULA_FACTION_DEMAND_SERVITUDE_INFORM_MAGISTER_ACCEPT"

		# Issue the demand to the faction target soon.
		scope:faction_target = {
			trigger_event = {
				id = regula_faction_demand.0001
				days = 5
			}
		}
	}
	option = {
		name = "REGULA_FACTION_DEMAND_SERVITUDE_INFORM_MAGISTER_DELAY"

		# Ask the magister again in a few months.
		trigger_event = {
			id = regula_faction_demand.0006
			months = 2
		}
	}
	option = {
		name = "REGULA_FACTION_DEMAND_SERVITUDE_INFORM_MAGISTER_REFUSE"
		scope:faction = {
			faction_remove_war = yes

			# Make sure that the target won't immediately be re-targeted.
			scope:faction_target = {
				add_character_flag = {
					flag = recent_regula_servitude_faction_war
					years = faction_war_defeat_cooldown
				}
			}

			destroy_faction = yes
			#TODO: notify participants?
		}
	}
}

# Servitude faction success liege change event for peasant leader.
# Done as a separate event because the peasant leader needs to go
# through clean up before we actually change their liege.
#
# scope = the character that will be swearing fealty to the Magister.
regula_faction_demand.0007 = {
	type = character_event
	hidden = yes

	immediate = {
		create_title_and_vassal_change = {
			type = swear_fealty
			save_scope_as = change
			add_claim_on_loss = no
		}
		change_liege = {
			liege = global_var:magister_character
			change = scope:change
		}
		resolve_title_and_vassal_change = scope:change

		# Add a small amount of gold to the newly landed vassal.
		add_gold = 25

		# Update government to match original government.
		if = { # If we started tribal, don't become feudal.
			limit = {
				has_variable = change_government_tribal
			}

			change_government = tribal_government

			# Also update authority laws since tribal uses a different authority system.
			add_realm_law = tribal_authority_0
			if = { limit = { has_realm_law = crown_authority_0 } remove_realm_law = crown_authority_0 }
			if = { limit = { has_realm_law = crown_authority_1 } remove_realm_law = crown_authority_1 }
			if = { limit = { has_realm_law = crown_authority_2 } remove_realm_law = crown_authority_2 }
			if = { limit = { has_realm_law = crown_authority_3 } remove_realm_law = crown_authority_3 }

			remove_variable = change_government_tribal
		}
		else_if = { # If we started clan, stay clan.
			limit = {
				has_variable = change_government_clan
			}

			change_government = clan_government

			remove_variable = change_government_clan
		}
	}
}

#########################
# Heresy Faction Events #
#########################

# Heresy Faction Demand Sent
#
# scope = the faction target (magister)
# scope:faction = the faction
# scope:faction_leader = the faction leader
# scope:faction_target = the faction target (magister)
regula_faction_demand.0101 = {
	type = letter_event
	sender = scope:faction_leader
	opening = {
		desc = "REGULA_FACTION_DEMAND_HERESY"
	}
	desc = "REGULA_FACTION_DEMAND_HERESY_DESC"

	trigger = {
		exists = scope:faction # May have ceased to exist by the time this triggers
	}

	option = {
		name = "FACTION_DEMAND_ACCEPT"

		root = {
			save_scope_as = faction_target
			add_dread = medium_dread_loss
		}

		regula_faction_heresy_faction_success_effect = {
			FACTION = scope:faction
			LEADER = scope:faction_leader
			TARGET = scope:faction_target
		}

		scope:faction = {
			every_faction_member = {
				trigger_event = regula_faction_demand.0002
			}
		}

		add_prestige = medium_prestige_loss

		ai_chance = {
			# Chance currently unused because this will always be sent to the
			# player.
			base = 1
			modifier = {
				add = 99
				scope:faction = { faction_power >= 100 }
			}
			modifier = {
				add = 50
				scope:faction = { faction_power >= 125 }
			}
			modifier = {
				add = 50
				scope:faction = { faction_power >= 150 }
			}
			modifier = {
				add = 50
				scope:faction = { faction_power >= 200 }
			}
			modifier = {
				factor = 0.1
				any_ally = {
					NOR = {
						target_is_liege_or_above = root
						target_is_vassal_or_below = root
					}
					max_military_strength > root.max_military_strength
				}
			}
			compare_modifier = {
				value = debt_level
				multiplier = 25
			}
		}
	}

	option = {
		name = "FACTION_DEMAND_REFUSE"

		show_as_tooltip = {
			scope:faction = {
				faction_start_war = {}
			}
		}

		root = {
			save_scope_as = faction_target
		}

		scope:faction_leader = {
			trigger_event = regula_faction_demand.0003
		}

		ai_chance = {
			# Chance currently unused because this will always be sent to the
			# player.
			base = 100
			ai_value_modifier = {
				ai_boldness = 4.0
			}
			modifier = {
				add = 50
				scope:faction = { faction_power < 80 }
			}
			modifier = {
				add = 100
				scope:faction = { faction_power < 60 }
			}
			modifier = {
				add = 1000
				scope:faction = { faction_power < 40 }
			}
			compare_modifier = {
				value = debt_level
				multiplier = -25
			}
		}
	}
}

# Heresy faction success independency event for peasant leader.
# Done as a separate event because the peasant leader needs to go
# through clean up before we actually make them independent.
#
# scope = the character that will be becoming independent.
regula_faction_demand.0110 = {
	type = character_event
	hidden = yes

	immediate = {
		create_title_and_vassal_change = {
			type = independency
			save_scope_as = change
			add_claim_on_loss = no
		}
		becomes_independent = {
			change = scope:change
		}
		resolve_title_and_vassal_change = scope:change

		# Add a small amount of gold to the newly independent ruler.
		add_gold = 25

		# Update government to match original government.
		if = { # If we started tribal, don't become feudal.
			limit = {
				has_variable = change_government_tribal
			}

			change_government = tribal_government

			# Also update authority laws since tribal uses a different authority system.
			add_realm_law = tribal_authority_0
			if = { limit = { has_realm_law = crown_authority_0 } remove_realm_law = crown_authority_0 }
			if = { limit = { has_realm_law = crown_authority_1 } remove_realm_law = crown_authority_1 }
			if = { limit = { has_realm_law = crown_authority_2 } remove_realm_law = crown_authority_2 }
			if = { limit = { has_realm_law = crown_authority_3 } remove_realm_law = crown_authority_3 }

			remove_variable = change_government_tribal
		}
		else_if = { # If we started clan, stay clan.
			limit = {
				has_variable = change_government_clan
			}

			change_government = clan_government

			remove_variable = change_government_clan
		}
	}
}
