﻿namespace = vassals_to_arms

# Top liege calls all subjects to protect against an attacker, if they are affected by this war (would lose land)
vassals_to_arms.0001 = {

	type = character_event

	title = vassals_to_arms.0001.t
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = { exists = scope:war_target_title }
				desc = vassals_to_arms.0001.desc.a
			}
			desc = vassals_to_arms.0001.desc.b
		}
	}

	theme = war

	trigger = {
		is_at_war_with_liege = no
		NOR = {
			is_defender_in_war = scope:war
			is_attacker_in_war = scope:war
		}
		OR = {
			has_game_rule = vta_rule_defender
			has_game_rule = vta_rule_both
		}
		vta_should_event_trigger = {
			WAR = scope:war
			TITLE = scope:war_target_title
			TRIGGER = using_cb
		}
	}

	override_background = {
		reference = battlefield
	}

	left_portrait = {
		character = scope:defender
		animation = worry
	}

	right_portrait = {
		character = ROOT
		animation = war_over_win
	}

	immediate = {
		save_scope_as = actor

		scope:war = {
			# Title loss custom description
			every_in_list = {
				variable = vta_lose_land
				save_temporary_scope_as = target_character
				if = {
					limit = {
						OR = {
							this = scope:actor
							is_vassal_or_below_of = scope:actor
						}
					}
					every_held_title = {
						limit = {
							tier >= tier_county
							save_temporary_scope_as = this_title
							OR = {
								# Every other war
								AND = {
									exists = scope:war_target_title
									OR = {
										this = scope:war_target_title
										target_is_de_jure_liege_or_above = scope:war_target_title
									}
								}
								# De Jure War
								# County Struggle
								AND = {
									NOT = { exists = scope:war_target_title }
									scope:war = {
										OR = {
											using_cb = de_jure_cb
											using_cb = county_struggle_cb
										}
									}
									scope:war = {
										any_in_list = {
											variable = vta_de_jure_titles
											save_temporary_scope_as = list_title
											scope:this_title = { target_is_de_jure_liege_or_above = scope:list_title }
										}
									}
								}
							}
							OR = {
								AND = {
									scope:war = { using_cb = claim_cb }
									exists = scope:war_target_title
									tier >= tier_duchy
									this = scope:war_target_title
								}
								AND = {
									tier = tier_county
									OR = {
										scope:war_target_title.tier = tier_county
										NOT = { scope:war = { using_cb = claim_cb } }
										holder = {
											any_sub_realm_county = {
												NOT = { target_is_de_jure_liege_or_above = scope:war_target_title }
											}
										}
									}
								}
								
							}
						}
						save_temporary_scope_as = target_title
						if = {
							limit = {
								OR = {
									this.holder = scope:actor
									holder = { is_vassal_of = scope:actor }
								}
							}
							custom_description = {
								text = vta_title_loss_localization
								subject = PREV
								object = this
							}
						}
						else = {
							custom_description = {
								text = vta_indirect_title_loss_localization
								subject = PREV
								object = this
							}
						}
					}
				}
			}
			# New liege custom description
			every_in_list = {
				variable = vta_change_liege
				save_temporary_scope_as = target_character
				if = {
					limit = { this = scope:actor }
					custom_description = {
						text = vta_new_liege_localization
						subject = scope:actor
						object = this
					}
				}
				else_if = {
					limit = { is_vassal_or_below_of = scope:actor }
					custom_description = {
						text = vta_vassal_loss_localization
						subject = this.liege
						object = this
					}
				}
			}
		}
	}

	# Join war
	option = {
		name = vassals_to_arms.0001.a
		trigger = {
			OR = {
				AND = {
					has_game_rule = vta_rule_land_subject
					scope:war = {
						is_target_in_variable_list = {
							name = vta_change_liege
							target = scope:actor
						}
					}
				}
				any_vassal_or_below = {
					save_temporary_scope_as = vassal
					scope:war = {
						is_target_in_variable_list = {
							name = vta_lose_land
							target = scope:vassal
						}
					}
				}
				scope:war = {
					is_target_in_variable_list = {
						name = vta_lose_land
						target = scope:actor
					}
				}
				#is_ai = no
			}
		}

		scope:war = { add_defender = ROOT }

		ai_chance = {
			base = 0

			modifier = {
				add = vta_join_liege
				always = yes
			}
		}
	}

	# Don't join war
	option = {
		name = vassals_to_arms.0001.b

		ai_chance = {
			base = 0

			modifier = {
				add = vta_dont_join_liege
				always = yes
			}
			
		}
	}
}

# Vassal join attacking liege, because some of his/her titles are pressed
vassals_to_arms.0002 = {

	type = character_event

	title = vassals_to_arms.0002.t
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = { exists = scope:war_target_title }
				desc = vassals_to_arms.0002.desc.a
			}
			desc = vassals_to_arms.0002.desc.b
		}
	}

	theme = war

	trigger = {
		is_at_war_with_liege = no
		NOR = {
			is_defender_in_war = scope:war
			is_attacker_in_war = scope:war
		}
		OR = {
			has_game_rule = vta_rule_attacker
			has_game_rule = vta_rule_both
		}
		vta_should_event_trigger = {
			WAR = scope:war
			TITLE = scope:war_target_title
			TRIGGER = using_cb
		}
	}

	left_portrait = {
		character = scope:defender
		animation = worry
	}

	right_portrait = {
		character = ROOT
		animation = war_over_win
	}

	immediate = {
		if = {
			limit = {
				any_sub_realm_title = {
					tier >= tier_duchy
					save_temporary_scope_as = this_title
					scope:war = { is_target_in_variable_list = { name = vta_de_jure_titles target = scope:this_title } }
				}
			}
			random_sub_realm_title = {
				limit = {
					tier >= tier_duchy
					save_temporary_scope_as = this_title
					scope:war = { is_target_in_variable_list = { name = vta_de_jure_titles target = scope:this_title } }
				}
				save_scope_as = dejure_target
			}
		}
	}

	# Join war as claimant
	option = {
		name = vassals_to_arms.0002.a

		trigger = { exists = scope:war_target_title }

		ai_chance = {
			base = 30

			modifier = {
				factor = 2
				has_trait = ambitious
			}

			modifier = {
				factor = 0.5
				has_trait = content
			}
		}

		scope:war = { add_attacker = ROOT }
	}

	# Join war as De Jure owner
	option = {
		name = vassals_to_arms.0002.c

		trigger = {
			NOT = { exists = scope:war_target_title }
			exists = scope:dejure_target
		}

		ai_chance = {
			base = 30

			modifier = {
				factor = 2
				has_trait = ambitious
			}

			modifier = {
				factor = 0.5
				has_trait = content
			}
		}

		scope:war = { add_attacker = ROOT }
	}

	# Don't join war
	option = {
		name = vassals_to_arms.0002.b

		ai_chance = {
			base = 0

			modifier = {
				add = 15
				is_at_war_as_defender = yes
			}

			modifier = {
				add = 15
				OR = {
					has_trait = content
					has_trait = craven
					has_trait = lazy
				}
			}

			modifier = {
				factor = 0.25
				OR = {
					has_trait = ambitious
					has_trait = brave
					has_trait = diligent
				}
			}

		}
	}
}

#vassals_to_arms.0003 = {
#
#	type = character_event
#
#	title = vassals_to_arms.0002.t
#	desc = vassals_to_arms.0002.desc
#
#	theme = war
#
#	left_portrait = {
#		character = ROOT
#		animation = worry
#	}
#
#	immediate = {
#		every_vassal = {
#			set_variable = {
#				name = military_strength
#				value = 0
#			}
#			every_held_title = {
#				limit = { tier = tier_county }
#				save_temporary_scope_as = holding
#				holder = {
#					change_variable = {
#						name = military_strength
#						add = scope:holding.county_levies_to_raise
#					}
#				}
#			}
#		}
#	}
#
#	option = {
#		name = "Test124"
#	}
#}

vassals_to_arms.0004 = {

	type = character_event

	title = vassals_to_arms.0004.t
	desc = vassals_to_arms.0004.desc

	theme = diplomacy

	trigger = {
		OR = {
			NOT = { exists = global_var:vta_settings_open }
			global_var:vta_settings_open = 0
		}
	}

	immediate = {
		set_global_variable = {
			name = vta_settings_open
			value = 1
		}
	}

	left_portrait = {
		character = root
		animation = personality_cynical
	}

	widget = {
		gui = "event_window_widget_vta"
		container = "custom_widgets_container"
	}

	option = {
		name = vassals_to_arms.0004.a
	}

	after = {
		set_global_variable = {
			name = vta_settings_open
			value = 0
		}
	}
}