﻿setup_agotma_rebel_title_effect = {
	create_landless_adventurer_title_effect = {
		REASON = flag:voluntary
		FLAVOR_CHAR = root
	}
}


agotma_start_war_effect = {
	if = {
		limit = { exists = scope:story }
		scope:story = { set_variable = agotma_ongoing_war }
	}
	add_character_flag = {
		flag = agotma_claim_cb_flag
		days = 1
	}
	scope:agotma_war_target_char = {
		add_character_flag = {
			flag = agotma_claim_cb_flag
			days = 1
		}
	}
	scope:agotma_instigated_char = {
		set_variable = {
			name = agotma_the_instigator
			value = scope:instigator
		}
	}

	# #TEST
	# if = {
	# 	limit = { exists = global_var:sum_of_instigation_wars }
	# 	change_global_variable = {
	# 		name = sum_of_instigation_wars
	# 		add = 1
	# 	}
	# }
	# else = {
	# 	set_global_variable = {
	# 		name = sum_of_instigation_wars
	# 		value = 1
	# 	}
	# }
	# #TEST

	if = {
		limit = { scope:agotma_war_target.title_capital_county.holder = scope:agotma_war_target.holder }
		scope:agotma_war_target.title_capital_county = { save_scope_as = agotma_war_target_capital }
	}

	if = {
		limit = { exists = scope:agotma_war_target_capital }
		start_war = {
			casus_belli = agotma_claim_cb
			target = scope:agotma_war_target_char
			target_title = scope:agotma_war_target
			target_title = scope:agotma_war_target_capital
			claimant = scope:agotma_instigated_char
		}
	}
	else = {
		start_war = {
			casus_belli = agotma_claim_cb
			target = scope:agotma_war_target_char
			target_title = scope:agotma_war_target
			claimant = scope:agotma_instigated_char
		}
	}

	if = {
		limit = {
			is_claimant = yes
			scope:agotma_war_target_char.primary_title.tier = tier_duchy
		}
		if = {
			limit = {
				OR = {
					has_trait = education_diplomacy_1
					has_trait = education_martial_1
					has_trait = education_stewardship_1
					has_trait = education_intrigue_1
					has_trait = education_learning_1
				}
			}
			spawn_army = {
				levies = 0
				men_at_arms = {
					type = armored_footmen
					stacks = 2
				}
				men_at_arms = {
					type = light_footmen
					stacks = 4
				}
				inheritable = no
				location = scope:agotma_instigated_char.location
				name = agotma_spawn_army_name
			}
		}
		else_if = {
			limit = {
				OR = {
					has_trait = education_diplomacy_2
					has_trait = education_martial_2
					has_trait = education_stewardship_2
					has_trait = education_intrigue_2
					has_trait = education_learning_2
				}
			}
			spawn_army = {
				levies = 0
				men_at_arms = {
					type = armored_footmen
					stacks = 3
				}
				men_at_arms = {
					type = light_footmen
					stacks = 6
				}
				men_at_arms = {
					type = bowmen
					stacks = 1
				}
				inheritable = no
				location = scope:agotma_instigated_char.location
				name = agotma_spawn_army_name
			}
		}
		else_if = {
			limit = {
				OR = {
					has_trait = education_diplomacy_3
					has_trait = education_martial_3
					has_trait = education_stewardship_3
					has_trait = education_intrigue_3
					has_trait = education_learning_3
				}
			}
			spawn_army = {
				levies = 0
				men_at_arms = {
					type = armored_footmen
					stacks = 6
				}
				men_at_arms = {
					type = light_footmen
					stacks = 12
				}
				men_at_arms = {
					type = bowmen
					stacks = 2
				}
				inheritable = no
				location = scope:agotma_instigated_char.location
				name = agotma_spawn_army_name
			}
		}
		else_if = {
			limit = {
				OR = {
					has_trait = education_diplomacy_4
					has_trait = education_martial_4
					has_trait = education_stewardship_4
					has_trait = education_intrigue_4
					has_trait = education_learning_4
				}
			}
			spawn_army = {
				levies = 0
				men_at_arms = {
					type = armored_footmen
					stacks = 9
				}
				men_at_arms = {
					type = light_footmen
					stacks = 18
				}
				men_at_arms = {
					type = bowmen
					stacks = 3
				}
				inheritable = no
				location = scope:agotma_instigated_char.location
				name = agotma_spawn_army_name
			}
		}
		else_if = {
			limit = {
				OR = {
					has_trait = education_diplomacy_5
					has_trait = education_martial_5
					has_trait = education_stewardship_5
					has_trait = education_intrigue_5
					has_trait = education_learning_5
				}
			}
			spawn_army = {
				levies = 0
				men_at_arms = {
					type = armored_footmen
					stacks = 12
				}
				men_at_arms = {
					type = light_footmen
					stacks = 24
				}
				men_at_arms = {
					type = bowmen
					stacks = 4
				}
				inheritable = no
				location = scope:agotma_instigated_char.location
				name = agotma_spawn_army_name
			}
		}
	}

	#ENEMY IS KING OR EMPIRE TIER

	if = {
		limit = {
			is_claimant = yes
			scope:agotma_war_target_char.primary_title.tier > tier_duchy
		}
		if = {
			limit = {
				OR = {
					has_trait = education_diplomacy_1
					has_trait = education_martial_1
					has_trait = education_stewardship_1
					has_trait = education_intrigue_1
					has_trait = education_learning_1
				}
			}
			spawn_army = {
				levies = 0
				men_at_arms = {
					type = armored_footmen
					stacks = 9
				}
				men_at_arms = {
					type = light_footmen
					stacks = 18
				}
				men_at_arms = {
					type = bowmen
					stacks = 3
				}
				inheritable = no
				location = scope:agotma_instigated_char.location
				name = agotma_spawn_army_name
			}
		}
		else_if = {
			limit = {
				OR = {
					has_trait = education_diplomacy_2
					has_trait = education_martial_2
					has_trait = education_stewardship_2
					has_trait = education_intrigue_2
					has_trait = education_learning_2
				}
			}
			spawn_army = {
				levies = 0
				men_at_arms = {
					type = armored_footmen
					stacks = 18
				}
				men_at_arms = {
					type = light_footmen
					stacks = 36
				}
				men_at_arms = {
					type = bowmen
					stacks = 6
				}
				inheritable = no
				location = scope:agotma_instigated_char.location
				name = agotma_spawn_army_name
			}
		}
		else_if = {
			limit = {
				OR = {
					has_trait = education_diplomacy_3
					has_trait = education_martial_3
					has_trait = education_stewardship_3
					has_trait = education_intrigue_3
					has_trait = education_learning_3
				}
			}
			spawn_army = {
				levies = 0
				men_at_arms = {
					type = armored_footmen
					stacks = 30
				}
				men_at_arms = {
					type = light_footmen
					stacks = 60
				}
				men_at_arms = {
					type = bowmen
					stacks = 10
				}
				inheritable = no
				location = scope:agotma_instigated_char.location
				name = agotma_spawn_army_name
			}
		}
		else_if = {
			limit = {
				OR = {
					has_trait = education_diplomacy_4
					has_trait = education_martial_4
					has_trait = education_stewardship_4
					has_trait = education_intrigue_4
					has_trait = education_learning_4
				}
			}
			spawn_army = {
				levies = 0
				men_at_arms = {
					type = armored_footmen
					stacks = 45
				}
				men_at_arms = {
					type = light_footmen
					stacks = 90
				}
				men_at_arms = {
					type = bowmen
					stacks = 15
				}
				inheritable = no
				location = scope:agotma_instigated_char.location
				name = agotma_spawn_army_name
			}
		}
		else_if = {
			limit = {
				OR = {
					has_trait = education_diplomacy_5
					has_trait = education_martial_5
					has_trait = education_stewardship_5
					has_trait = education_intrigue_5
					has_trait = education_learning_5
				}
			}
			spawn_army = {
				levies = 0
				men_at_arms = {
					type = armored_footmen
					stacks = 60
				}
				men_at_arms = {
					type = light_footmen
					stacks = 120
				}
				men_at_arms = {
					type = bowmen
					stacks = 20
				}
				inheritable = no
				location = scope:agotma_instigated_char.location
				name = agotma_spawn_army_name
			}
		}
	}
}

agotma_dragon_sickness_effect = {
	if = {
		limit = {
			NOT = { has_character_modifier = agotma_dragon_sickness_1 }
			NOT = { has_character_modifier = agotma_dragon_sickness_2 }
			NOT = { has_character_modifier = agotma_dragon_sickness_3 }
			NOT = { has_character_modifier = agotma_dragon_sickness_4 }
			NOT = { has_character_modifier = agotma_dragon_sickness_5 }
			NOT = { has_character_modifier = agotma_dragon_sickness_6 }
			NOT = { has_character_modifier = agotma_dragon_sickness_7 }
		}
		add_character_modifier = agotma_dragon_sickness_1
	}
	else_if = {
		limit = { has_character_modifier = agotma_dragon_sickness_1 }
		remove_character_modifier = agotma_dragon_sickness_1
		add_character_modifier = agotma_dragon_sickness_2
	}
	else_if = {
		limit = { has_character_modifier = agotma_dragon_sickness_2 }
		remove_character_modifier = agotma_dragon_sickness_2
		add_character_modifier = agotma_dragon_sickness_3
	}
	else_if = {
		limit = { has_character_modifier = agotma_dragon_sickness_3 }
		remove_character_modifier = agotma_dragon_sickness_3
		add_character_modifier = agotma_dragon_sickness_4
	}
	else_if = {
		limit = { has_character_modifier = agotma_dragon_sickness_4 }
		remove_character_modifier = agotma_dragon_sickness_4
		add_character_modifier = agotma_dragon_sickness_5
	}
	else_if = {
		limit = { has_character_modifier = agotma_dragon_sickness_5 }
		remove_character_modifier = agotma_dragon_sickness_5
		add_character_modifier = agotma_dragon_sickness_6
	}
	else_if = {
		limit = { has_character_modifier = agotma_dragon_sickness_6 }
		remove_character_modifier = agotma_dragon_sickness_6
		add_character_modifier = agotma_dragon_sickness_7
	}
}

agotma_dragon_decline_period_effect = {
	title:b_ruins.holder = {
		if = {
			limit = { has_game_rule = agot_dragon_culling_high }
			set_variable = {
				name = agotma_dragon_decline_period
				years = { 80 160 }
			}
		}
		else_if = {
			limit = { has_game_rule = agot_dragon_culling_normal }
			set_variable = {
				name = agotma_dragon_decline_period
				years = { 65 130 }
			}
		}
		else_if = {
			limit = { has_game_rule = agot_dragon_culling_canon_minimal }
			set_variable = {
				name = agotma_dragon_decline_period
				years = { 50 100 }
			}
		}
		else_if = {
			limit = { has_game_rule = agot_dragon_culling_low }
			set_variable = {
				name = agotma_dragon_decline_period
				years = { 25 50 }
			}
		}
		else = {
			set_variable = {
				name = agotma_dragon_decline_period
				years = { 150 300 }
			}
		}
	}
}

agotma_dragon_decline_period_perma_effect = {
	title:b_ruins.holder = {
		set_variable = agotma_dragon_decline_period
	}
}

agotma_dragon_decline_period_remvove_effect = {
	title:b_ruins.holder = {
		remove_variable = agotma_dragon_decline_period
	}
}

# INSTIGATOR

agotma_instigator_story_end_effect = {
	hidden_effect = {
		if = {
			limit = { scope:instigator.top_liege.primary_title ?= { has_variable = agotma_instigator_started } }
			scope:instigator.top_liege.primary_title = { remove_variable = agotma_instigator_started }
		}
		if = {
			limit = { scope:instigator.top_liege.primary_title ?= { has_variable = agotma_instigator_started_ai } }
			scope:instigator.top_liege.primary_title = {
				remove_variable = agotma_instigator_started_ai
				set_variable = {
					name = agotma_instigator_started_ai
					years = 1
				}
			}
		}
		scope:agotma_instigated_char = {
			if = {
				limit = { is_alive = yes }
				add_character_flag = {
					flag = already_instigated
					years = 5
				}
			}
		}
		if = {
			limit = { exists = scope:story }
			scope:story = { end_story = yes }
		}
	}
}

#Save the children for marriage
agotma_find_random_suitable_child_for_marriage = {
	$OFFERING_CHARACTER$ = { save_scope_as = offering_character }
	$RECEIVING_CHARACTER$ = { save_scope_as = receiving_character }
	$MARRIAGE_WILL_FAVOR_HOUSE$ = {
		add_character_flag = {
			flag = incoming_marriage_offer_favors_my_house
			days = 50
		}
	}

	scope:offering_character = {
		set_variable = {
			name = marriage_rule
			value = flag:$ALLOW_HEIR$
			days = 1
		}

		#Find child from offering side
		if = {
			limit = {
				any_child = {
					can_marry_common_trigger = yes
					is_playable_character = no
					agot_can_be_ruler = yes
					is_imprisoned = no
					is_incapable = no
					trigger_if = {
						limit = {
							exists = scope:offering_character.var:marriage_rule
							scope:offering_character.var:marriage_rule = flag:no
							scope:offering_character = { NOT = { has_character_flag = incoming_marriage_offer_favors_my_house } }
						}
						NOT = { is_primary_heir_of = scope:offering_character }
					}
				}
			}
			random_child = {
				limit = {
					can_marry_common_trigger = yes
					is_playable_character = no
					agot_can_be_ruler = yes
					is_imprisoned = no
					is_incapable = no
					trigger_if = {
						limit = {
							exists = scope:offering_character.var:marriage_rule
							scope:offering_character.var:marriage_rule = flag:no
							scope:offering_character = { NOT = { has_character_flag = incoming_marriage_offer_favors_my_house } }
						}
						NOT = { is_primary_heir_of = scope:offering_character }
					}
				}
				save_scope_as = t_$CHILD_OF_OFFERING_CHARACTER_SCOPE_NAME$
			}
			#Find child from receiving side
			if = {
				limit = {
					exists = scope:t_$CHILD_OF_OFFERING_CHARACTER_SCOPE_NAME$
					scope:receiving_character = {
						any_child = {
							can_marry_character_trigger = { CHARACTER = scope:t_$CHILD_OF_OFFERING_CHARACTER_SCOPE_NAME$ }
							is_playable_character = no
							agot_can_be_ruler = yes
							is_imprisoned = no
							is_incapable = no
							trigger_if = {
								limit = {
									exists = scope:offering_character.var:marriage_rule
									scope:offering_character.var:marriage_rule = flag:no
									scope:receiving_character = { NOT = { has_character_flag = incoming_marriage_offer_favors_my_house } }
								}
								NOT = { is_primary_heir_of = scope:receiving_character }
							}
							is_courtier_of = scope:receiving_character
						}
					}
				}
				scope:receiving_character = {
					random_child = {
						limit = {
							can_marry_character_trigger = { CHARACTER = scope:t_$CHILD_OF_OFFERING_CHARACTER_SCOPE_NAME$ }
							is_playable_character = no
							agot_can_be_ruler = yes
							is_imprisoned = no
							is_incapable = no
							trigger_if = {
								limit = {
									exists = scope:offering_character.var:marriage_rule
									scope:offering_character.var:marriage_rule = flag:no
									scope:receiving_character = { NOT = { has_character_flag = incoming_marriage_offer_favors_my_house } }
								}
								NOT = { is_primary_heir_of = scope:receiving_character }
							}
							is_courtier_of = scope:receiving_character
						}
						save_scope_as = $CHILD_OF_RECEIVING_CHARACTER_SCOPE_NAME$
					}
				}
			}
		}
		#Save final scopes
		if = {
			limit = {
				exists = scope:t_$CHILD_OF_OFFERING_CHARACTER_SCOPE_NAME$
				exists = scope:$CHILD_OF_RECEIVING_CHARACTER_SCOPE_NAME$
			}
			scope:t_$CHILD_OF_OFFERING_CHARACTER_SCOPE_NAME$ = { save_scope_as = $CHILD_OF_OFFERING_CHARACTER_SCOPE_NAME$ }
		}
		#Fallback
		else_if = {
			limit = {
				exists = scope:t_$CHILD_OF_OFFERING_CHARACTER_SCOPE_NAME$
				NOT = { exists = scope:$CHILD_OF_RECEIVING_CHARACTER_SCOPE_NAME$ }
			}
			#Find child from offering side - opposite sex of previous check
			if = {
				limit = {
					any_child = {
						can_marry_common_trigger = yes
						is_playable_character = no
						agot_can_be_ruler = yes
						is_imprisoned = no
						is_incapable = no
						sex_opposite_of = scope:t_$CHILD_OF_OFFERING_CHARACTER_SCOPE_NAME$
						trigger_if = {
							limit = {
								exists = scope:offering_character.var:marriage_rule
								scope:offering_character.var:marriage_rule = flag:no
								scope:offering_character = { NOT = { has_character_flag = incoming_marriage_offer_favors_my_house } }
							}
							NOT = { is_primary_heir_of = scope:offering_character }
						}
					}
				}
				random_child = {
					limit = {
						can_marry_common_trigger = yes
						is_playable_character = no
						agot_can_be_ruler = yes
						is_imprisoned = no
						is_incapable = no
						sex_opposite_of = scope:t_$CHILD_OF_OFFERING_CHARACTER_SCOPE_NAME$
						trigger_if = {
							limit = {
								exists = scope:offering_character.var:marriage_rule
								scope:offering_character.var:marriage_rule = flag:no
								scope:offering_character = { NOT = { has_character_flag = incoming_marriage_offer_favors_my_house } }
							}
							NOT = { is_primary_heir_of = scope:offering_character }
						}
					}
					save_scope_as = t_t_$CHILD_OF_OFFERING_CHARACTER_SCOPE_NAME$
				}
				#Find child from receiving side
				if = {
					limit = {
						exists = scope:t_t_$CHILD_OF_OFFERING_CHARACTER_SCOPE_NAME$
						scope:receiving_character = {
							any_child = {
								can_marry_character_trigger = { CHARACTER = scope:t_t_$CHILD_OF_OFFERING_CHARACTER_SCOPE_NAME$ }
								is_playable_character = no
								agot_can_be_ruler = yes
								is_imprisoned = no
								is_incapable = no
								trigger_if = {
									limit = {
										exists = scope:offering_character.var:marriage_rule
										scope:offering_character.var:marriage_rule = flag:no
										scope:receiving_character = { NOT = { has_character_flag = incoming_marriage_offer_favors_my_house } }
									}
									NOT = { is_primary_heir_of = scope:receiving_character }
								}
								is_courtier_of = scope:receiving_character
							}
						}
					}
					scope:receiving_character = {
						random_child = {
							limit = {
								can_marry_character_trigger = { CHARACTER = scope:t_t_$CHILD_OF_OFFERING_CHARACTER_SCOPE_NAME$ }
								is_playable_character = no
								agot_can_be_ruler = yes
								is_imprisoned = no
								is_incapable = no
								trigger_if = {
									limit = {
										exists = scope:offering_character.var:marriage_rule
										scope:offering_character.var:marriage_rule = flag:no
										scope:receiving_character = { NOT = { has_character_flag = incoming_marriage_offer_favors_my_house } }
									}
									NOT = { is_primary_heir_of = scope:receiving_character }
								}
								is_courtier_of = scope:receiving_character
							}
							save_scope_as = $CHILD_OF_RECEIVING_CHARACTER_SCOPE_NAME$
						}
					}
				}
			}
			#Save final scopes
			if = {
				limit = {
					exists = scope:t_t_$CHILD_OF_OFFERING_CHARACTER_SCOPE_NAME$
					exists = scope:$CHILD_OF_RECEIVING_CHARACTER_SCOPE_NAME$
				}
				scope:t_t_$CHILD_OF_OFFERING_CHARACTER_SCOPE_NAME$ = { save_scope_as = $CHILD_OF_OFFERING_CHARACTER_SCOPE_NAME$ }
			}
		}
	}
}

agotma_marry_random_suitable_child = {
	$OFFERING_CHARACTER$ = { save_scope_as = offering_character }
	$RECEIVING_CHARACTER$ = { save_scope_as = receiving_character }
	$OFFERING_CHARACTER_CHILD$ = { save_scope_as = offering_character_child }
	$RECEIVING_CHARACTER_CHILD$ = { save_scope_as = receiving_character_child }

	if = {
		limit = {
			scope:offering_character_child = { is_adult = yes }
			scope:receiving_character_child = { is_adult = yes }
		}
		if = {
			limit = { scope:offering_character_child = { is_male = yes } }
			if = {
				limit = { scope:offering_character = { has_character_flag = incoming_marriage_offer_favors_my_house } }
				scope:offering_character_child = { marry = scope:receiving_character_child }
			}
			else = { scope:offering_character_child = { marry_matrilineal = scope:receiving_character_child } }
		}
		else = {
			if = {
				limit = { scope:offering_character = { has_character_flag = incoming_marriage_offer_favors_my_house } }
				scope:offering_character_child = { marry_matrilineal = scope:receiving_character_child }
			}
			else = { scope:offering_character_child = { marry = scope:receiving_character_child } }
		}

		scope:offering_character = {
			create_alliance = { # By marriage
				target = scope:receiving_character
				allied_through_owner = scope:offering_character_child
				allied_through_target = scope:receiving_character_child
			}
		}
	}
	else = {
		if = {
			limit = { scope:offering_character_child = { is_male = yes } }
			if = {
				limit = { scope:offering_character = { has_character_flag = incoming_marriage_offer_favors_my_house } }
				scope:offering_character_child = { create_betrothal = scope:receiving_character_child }
			}
			else = { scope:offering_character_child = { create_betrothal_matrilineal = scope:receiving_character_child } }
		}
		else = {
			if = {
				limit = { scope:offering_character = { has_character_flag = incoming_marriage_offer_favors_my_house } }
				scope:offering_character_child = { create_betrothal_matrilineal = scope:receiving_character_child }
			}
			else = { scope:offering_character_child = { create_betrothal = scope:receiving_character_child } }
		}

		scope:offering_character = {
			create_alliance = { # By marriage
				target = scope:receiving_character
				allied_through_owner = scope:offering_character_child
				allied_through_target = scope:receiving_character_child
			}
		}
	}
}

agotma_force_alliance = {
	$ALLY_A$ = { save_scope_as = ally_a }
	$ALLY_B$ = { save_scope_as = ally_b }
	if = {
		limit = { agotma_valid_force_alliance = { ALLY_A = scope:ally_a ALLY_B = scope:ally_b } }
		scope:ally_a = {
			add_opinion = {
				target = scope:ally_b
				modifier = event_negotiated_alliance_opinion
			}
		}
		scope:ally_b = {
			add_opinion = {
				target = scope:ally_a
				modifier = event_negotiated_alliance_opinion
			}
		}
		scope:ally_a = {
			create_alliance = {
				target = scope:ally_b
				allied_through_owner = scope:ally_a
				allied_through_target = scope:ally_b
			}
		}
	}
}

asdfaq = {
	save_scope_as = me
	scope:me = {
		every_child = {
			limit = {
				NOT = { house = scope:me.house }
			}
			set_house = scope:me.house
		}
	}
}

asder = {
	add_trait = agotma_ill_dragon
	create_story = agotma_dragon_sickness_story
}

# asder = {
# 	random_courtier = {
# 		limit = {
# 			has_trait = one_eyed
# 		}
# 		save_scope_as = thisguys
# 	}
# 	add_secret = {
# 		type = secret_agot_disputed_heritage
# 		target = scope:thisguys
# 	}
# }

asdx = {
	random_courtier = {
		limit = {
			has_trait = one_eyed
		}
		save_scope_as = thisguy
	}

	scope:thisguy = {
		every_child = {
			set_house = scope:thisguy.house
		}
	}
}