﻿agotma_buybetrothal = {
	# Basic Setup
	skill = intrigue
	success_desc = "AGOTMA_BUYBETROTHAL_SUCCESS_DESC"
	icon = icon_scheme_agotma_buybetrothal
	illustration = "gfx/interface/illustrations/event_scenes/corridor.dds"
	category = personal
	target_type = character
	is_secret = no
	is_basic = yes
	uses_resistance = no
	cooldown = { years = 5 }

	# Parameters
	speed_per_skill_point = -2.5
	spymaster_speed_per_skill_point = 0	
	uses_resistance = no
	base_progress_goal = 365
	base_maximum_success = 95
	minimum_success = 5

	odds_prediction = {
		add = base_odds_prediction_target_is_char_value
		add = odds_skill_contribution_diplomacy_value
		add = odds_befriend_scheme_misc_value
		min = 0
	}

	allow = {
		scope:actor = {
			NOT = {
				has_trait = incapable
				has_character_modifier = agotma_buybetrothal_final_modifier
			}
			any_child = { count >= 1 }
			is_landed = yes
			highest_held_title_tier >= tier_duchy
			is_adult = yes
		}
		scope:recipient = {
			is_betrothed = yes
			OR = {
				AND = {
					is_landed = no
					is_child_of = scope:recipient.top_liege
				}
				AND = {
					is_landed = yes
					scope:recipient = scope:recipient.top_liege
				}
			}
		}

		scope:recipient.top_liege = {
			is_landed = yes
			highest_held_title_tier = tier_hegemony
		}
		scope:recipient.betrothed.house.house_head = { NOT = { is_at_war_with = scope:recipient.top_liege } }
		scope:actor = { NOT = { has_variable = i_completed_agotma_buybetrothal } }

		OR = {
			scope:secondary_recipient.father = {
				is_landed = yes
				highest_held_title_tier >= tier_duchy
			}
			scope:secondary_recipient.mother = {
				is_landed = yes
				highest_held_title_tier >= tier_duchy
			}
			scope:secondary_recipient = {
				is_landed = yes
				highest_held_title_tier >= tier_duchy
			}
		}
		NOT = {
			scope:recipient = scope:actor
			scope:secondary_recipient.father = scope:recipient.top_liege
			scope:secondary_recipient.mother = scope:recipient.top_liege
			scope:secondary_recipient.house.house_head = scope:recipient.top_liege.house.house_head
		}
		scope:recipient.father = {
			NOT = { has_character_modifier = agotma_buybetrothal_betrothed_modifier }
		}
		scope:recipient.mother = {
			NOT = { has_character_modifier = agotma_buybetrothal_betrothed_modifier }
		}
		scope:recipient.religion = scope:actor.religion
		scope:recipient.top_liege = { is_ai = yes }
		scope:recipient = { is_ai = yes }
		OR = {
			AND = {
				scope:actor = { is_ai = yes }
				has_game_rule = agotma_buy_betrothal_enabled
			}
			scope:actor = { is_ai = no }
		}
		OR = {
			AND = {
				scope:actor = { is_ai = yes }
				scope:recipient.top_liege.primary_title = { NOT = { has_variable = agotma_buybetrothal_started } }
			}
			scope:actor = { is_ai = no }
		}
	}

	valid = {
		is_imprisoned = no
		# You're too far away
		scope:target = {
			OR = {
				exists = location
				in_diplomatic_range = scope:owner
			}
		}

		OR = {
			AND = {
				is_ai = yes
				scope:target.top_liege.primary_title ?= { NOT = { has_variable = agotma_buybetrothal_started } }
			}
			AND = {
				is_ai = no
				scope:target = { NOT = { has_character_flag = agotma_boughtbetrothal } }
			}
		}

		scope:target = {
			OR = {
				is_at_war = no
				has_variable = pre_war_liege
			}
		}
	}


	base_success_chance = {
		base = 100
	}

	on_phase_completed = {
		scheme_target_character = {
			save_scope_as = target
		}

		scheme_owner = {
			save_scope_as = owner
			#Do I want to proceed to a roll?
			if = {
				limit = {
					is_alive = yes
					scope:target.top_liege = { is_ai = yes }
					scope:target = { is_ai = yes }
				}
				if = {
					limit = {
						scope:target.betrothed.house.house_head = { is_at_war_with = scope:target.top_liege }
					}
					trigger_event = agotma_buybetrothal_outcome.1003 #try again later
				}
				else = {
					if = {
						limit = { scope:owner = { is_ai = no } }
						trigger_event = agotma_buybetrothal_outcome.0001
						scope:target = { add_character_flag = agotma_boughtbetrothal }
					}
					else = {
						random_list = {
							50 = {
								trigger_event = agotma_buybetrothal_outcome.0001
								scope:target = { add_character_flag = agotma_boughtbetrothal }
							}
							50 = { trigger_event = agotma_buybetrothal_outcome.9999 }
						}
					}
				}
			}
			else = {
				scope:scheme = {
					end_scheme = yes
				}
			}
		}

		scope:target.top_liege.primary_title = {
			if = {
				limit = { NOT = { has_variable = agotma_buybetrothal_started } }
				set_variable = {
					name = agotma_buybetrothal_started
					years = 15
				}
			}
		}
	}

	on_invalidated = {
		scheme_target_character = {
			save_scope_as = target
		}
		scheme_owner = {
			save_scope_as = owner
		}

		if = {
			limit = {
				scope:target.top_liege.primary_title ?= { has_variable = agotma_buybetrothal_started }
				scope:owner = { is_ai = yes }
			}
			scope:owner = {
				trigger_event = agotma_buybetrothal_outcome.9999
			}
		}

		if = {
			limit = {
				scope:target = { is_alive = no }
			}
			scope:owner = {
				trigger_event = agotma_buybetrothal_outcome.0006
			}
		}

		if = {
			limit = {
				exists = scope:scheme
			}
			scope:owner = {
				trigger_event = agotma_buybetrothal_outcome.0008
			}
		}
	}

	on_start = {
		save_scope_as = scheme
		scheme_owner = { save_scope_as = owner }
		scheme_target_character = { save_scope_as = target }

		if = {
			limit = {
				scope:target = {is_female = yes}
				OR = {
					scope:owner = {
						any_child = {
							is_married = no
							is_betrothed = no
							is_male = yes
							age < 40
							can_marry_trigger = yes
						}
					}
					scope:owner = {
						any_sibling = {
							is_married = no
							is_betrothed = no
							is_male = yes
							age < 40
							can_marry_trigger = yes
						}
					}
					scope:owner = {
						is_married = no
						is_betrothed = no
						is_male = yes
						age < 40
						can_marry_trigger = yes
					}
				}

			}

#			scope:owner = {	trigger_event = agotma_buybetrothal_outcome.1001 }


			scope:owner = {
				trigger_event = {
					id = agotma_buybetrothal_outcome.1001
					days = { 1 2 }
				}
			}

		}

		else_if = {
			limit = {
				scope:target = {is_male = yes}
				OR = {
					scope:owner = {
						any_child = {
							is_married = no
							is_betrothed = no
							is_female = yes
							age < 40
							can_marry_trigger = yes
						}
					}
					scope:owner = {
						any_sibling = {
							is_married = no
							is_betrothed = no
							is_female = yes
							age < 40
							can_marry_trigger = yes
						}
					}
					scope:owner = {
						is_married = no
						is_betrothed = no
						is_female = yes
						age < 40
						can_marry_trigger = yes
					}
				}

			}

#			scope:owner = {	trigger_event = agotma_buybetrothal_outcome.1001 }


			scope:owner = {
				trigger_event = {
					id = agotma_buybetrothal_outcome.1001
					days = { 1 2 }
				}
			}

		}

		else_if = {
			limit = {
				scope:owner = {
					is_alive = yes
				}
			}
#			scope:owner = {	trigger_event = agotma_buybetrothal_outcome.1002 }

			scope:owner = {
				trigger_event = {
					id = agotma_buybetrothal_outcome.1002
					days = { 1 2 }
				}
			}
		}
	}

	on_monthly = {
		save_scope_as = scheme
		scheme_owner = { save_scope_as = owner }
		scheme_target_character = { save_scope_as = target }

		#already revealed!
		if = {
			limit = {
				NOT = { scheme_progress = 10 }
				scope:target = {
					NOT = { is_betrothed = yes }
				}
			}
			scope:owner = {
				trigger_event = agotma_buybetrothal_outcome.0007
			}
		}

		if = {
			limit = {
				scope:target.betrothed.house.house_head = { is_at_war_with = scope:target.top_liege }
			}
			scope:owner = { trigger_event = agotma_buybetrothal_outcome.1003 } #try again later
		}
	}
}



