﻿### HOUSE RELATION TRIGGERS

house_relation_is_valid_to_start_trigger = {
	$HOUSE$ ?= { save_temporary_scope_as = house_1_temp }
	$OTHER_HOUSE$ ?= { save_temporary_scope_as = house_2_temp }
	trigger_if = {
		limit = {
			exists = scope:house_1_temp
			exists = scope:house_2_temp
		}
		scope:house_1_temp = {
			NOT = { this = scope:house_2_temp }
			# IF HOUSE RELATION DOES NOT ALREADY EXIST, ENSURE IT IS WORTHWHILE TO START
			trigger_if = {
				limit = {
					NOT = { has_house_relation_with = scope:house_2_temp }
				}
				# MINIMUM START CHECKS
				house_head ?= { is_playable_character = yes }
				scope:house_2_temp.house_head ?= { is_playable_character = yes }
				# EXTRA CHECKS FOR AI
				trigger_if = {
					limit = {
						NOR = {
							any_player = { house ?= scope:house_1_temp }
							any_player = { house ?= scope:house_2_temp }
						}
					}
					# DON'T CREATE LOTS OF RELATIONS FOR TINY AI HOUSES
					any_house_member = { count >= 5 }
					scope:house_2_temp = {
						any_house_member = { count >= 5 }
					}
				}
			}
			# GENERAL CHECKS
			OR = {
				# HOUSES IN SAME DYNASTY CAN ALWAYS MAKE RELATIONS
				house_dynasty = scope:house_2_temp.house_dynasty
				# TIER CHECKS IF BOTH INDEPENDENT
				trigger_if = {
					limit = {
						house_head = { is_independent_ruler = yes }
						scope:house_2_temp.house_head = { is_independent_ruler = yes }
					}
					house_head = {
						tier_difference = {
							target = scope:house_2_temp.house_head
							value < 2
						}
						tier_difference = {
							target = scope:house_2_temp.house_head
							value > -2
						}
					}
				}
				# SAME REALM CHECK IF NOT BOTH INDEPENDENT (SHARED TOP LIEGE)
				trigger_else = { house_head.top_liege = scope:house_2_temp.house_head.top_liege }
			}
			# DIPLO RANGE IS ALWAYS REQUIRED
			house_head = { in_diplomatic_range = scope:house_2_temp.house_head }
		}
	}
	# SCOPES ARE INVALID
	trigger_else = { always = no }
}

house_relation_is_valid_to_keep_trigger = {
	$HOUSE$ ?= { save_temporary_scope_as = house_1_temp }
	$OTHER_HOUSE$ ?= { save_temporary_scope_as = house_2_temp }
	trigger_if = {
		limit = {
			exists = scope:house_1_temp
			exists = scope:house_2_temp
		}
		scope:house_1_temp = {
			NOT = { this = scope:house_2_temp }
			# DIPLO RANGE IS ALWAYS REQUIRED
			house_head = { in_diplomatic_range = scope:house_2_temp.house_head }
		}
	}
	# SCOPES ARE INVALID
	trigger_else = { always = no }
}

has_better_house_relation_with_target_house_than_other_trigger = {
	save_temporary_scope_as = house_temp
	$TARGET$ = { save_temporary_scope_as = target_temp }
	$OTHER$ = { save_temporary_scope_as = other_temp }
	# If no relation with OTHER, TARGET relation must be positive
	trigger_if = {
		limit = {
			any_house_relation = {
				any_relation_house = { this = scope:target_temp }
				save_temporary_scope_as = target_relation_temp
			}
			NOT = {
				any_house_relation = {
					any_relation_house = { this = scope:other_temp }
				}			
			}
		}
		scope:target_relation_temp = {
			OR = {
				has_house_relation_level = cordial
				has_house_relation_level = friendly
				has_house_relation_level = amity
			}
		}
	}
	# If relation with both, TARGET relation must be better than OTHER relation
	trigger_else_if = {
		limit = {
			any_house_relation = {
				any_relation_house = { this = scope:target_temp }
				save_temporary_scope_as = target_relation_temp
			}
			any_house_relation = {
				any_relation_house = { this = scope:other_temp }
				save_temporary_scope_as = other_relation_temp	
			}
		}
		scope:target_relation_temp = {
			trigger_if = {
				limit = { has_house_relation_level = feud }
				scope:other_relation_temp = {
					NOT = { has_house_relation_level = feud }
				}
			}
			trigger_else_if = {
				limit = { has_house_relation_level = rivalry }
				scope:other_relation_temp = {
					NOR = {
						has_house_relation_level = rivalry
						has_house_relation_level = feud
					}
				}
			}
			trigger_else_if = {
				limit = { has_house_relation_level = quarrel }
				scope:other_relation_temp = {
					NOR = {
						has_house_relation_level = quarrel
						has_house_relation_level = rivalry
						has_house_relation_level = feud
					}
				}
			}
			trigger_else_if = {
				limit = { has_house_relation_level = neutral }
				scope:other_relation_temp = {
					NOR = {
						has_house_relation_level = neutral
						has_house_relation_level = quarrel
						has_house_relation_level = rivalry
						has_house_relation_level = feud
					}
				}
			}
			trigger_else_if = {
				limit = { has_house_relation_level = cordial }
				scope:other_relation_temp = {
					NOR = {
						has_house_relation_level = cordial
						has_house_relation_level = neutral
						has_house_relation_level = quarrel
						has_house_relation_level = rivalry
						has_house_relation_level = feud
					}
				}
			}
			trigger_else_if = {
				limit = { has_house_relation_level = friendly }
				scope:other_relation_temp = {
					NOR = {
						has_house_relation_level = friendly
						has_house_relation_level = cordial
						has_house_relation_level = neutral
						has_house_relation_level = quarrel
						has_house_relation_level = rivalry
						has_house_relation_level = feud
					}
				}
			}
			trigger_else_if = {
				limit = { has_house_relation_level = amity }
				scope:other_relation_temp = {
					NOR = {
						has_house_relation_level = amity
						has_house_relation_level = friendly
						has_house_relation_level = cordial
						has_house_relation_level = neutral
						has_house_relation_level = quarrel
						has_house_relation_level = rivalry
						has_house_relation_level = feud
					}
				}
			}
			trigger_else = { always = no }
		}
	}
	# Invalid
	trigger_else = { always = no }
}
