## normal scopes(including posttitle scopes) ##
# ROOT = receiver
# FROM = giver
# <no scope change> = attacker or receiver
## title scopes ##
# ROOT = receiver
# FROM = giver
# <no scope change> = thirdparty landed title	
#
# the following effects/triggers exists (example execution order: on_success->on_success_title->on_success_posttitle):
# is_valid, is_valid_title, on_add, on_add_title, on_add_posttitle, on_success, on_success_title, on_success_posttitle, on_fail, on_fail_title, on_fail_posttitle, on_reverse_demand, on_reverse_demand_title, on_reverse_demand_posttitle
#
# Added on_attacker_leader_death, on_defender_leader_death and on_thirdparty_death, which all trigger when corresponding character dies
# These three all have war scopes, which currently has the following scope changes: 
# attacker, defender, thirdparty(only valid if thirdparty character is involved), thirdparty_title(only valid if thirdparty title is involved)
#
# ai_will_do: modifies value AI places on the CB compared to other CBs (default: 1)
# can_use_gui: If otherwise valid, the CB is listed in the Diplo View, but you can't declare war unless 'can_use_gui' is also valid (also shows a trigger tooltip.)

# TODO catholic rulers should risk getting excommunicated by the pope
# Random chance it not firing

horde_subjugation = {
	name = SM_HORDE_SUBJUGATION
	war_name = WAR_NAME_HORDE_SUBJUGATION
	sprite = 8
	truce_days = 600
	
	is_permanent = yes
	can_ask_to_join_war = yes
	hostile_against_others = yes
	check_de_jure_tier = KING # this scans all de jure kingdoms for the counties which are held by or vassals(or below) of selected character. Only valid if is_permanent = yes
	
	can_use = {
		ROOT = {
				hidden_tooltip = {has_character_flag = hordling}
				OR = {
					AND = {
						has_dlc = "Horse Lords"
						is_nomadic = no
					}
					AND = {
						NOT = { has_dlc = "Horse Lords" }
						has_horde_culture = no 
					}
				}
				NOR = {
					pays_tribute_to = FROM
					any_liege = {
						OR = {
							pays_tribute_to = FROM
							FROM = {
								pays_tribute_to = PREV
							}
						}
					}
				}
				NOT = { same_realm = FROM }
				NOT = { is_liege_or_above = FROM }
				OR = {
					religion = FROM
					AND = {
						religion_group = pagan_group
						is_reformed_religion = no
					}
				}
		}
		FROM = {
			is_landed = yes
		}
	}
	
	is_valid = {
		always = yes
	}
	
	is_valid_title = {
		FROM = {
			any_realm_title = {
				de_jure_liege_or_above = PREVPREV
			}
		}
	}
	
	on_add = {
		# Chance catholic rulers get excommunicated if they break natural order to go horde!!
		if = {
			limit = {
				ROOT = {
					religion = catholic
				}				
			}
			random = {
				chance = 10
				modifier = {
					factor = 2.5
					religion_head = {
						NOT = {personal_opinion = {who = ROOT value = 25}}
						personal_opinion = {who = ROOT value = -25}
					}
				}
				modifier = {
					factor = 5
					religion_head = {
						NOT = {personal_opinion = {who = ROOT value = -25}}
					}
				}
			}
			excommunicate = yes 
		}
	}
	
	on_success_title = {
		# Usurp the target kingdom itself, if the enemy holds it
		if = {
			limit = {
				holder_scope = {
					character = FROM
				}
				NOT = { ROOT = { tier = EMPEROR } }
			}
			ROOT = { usurp_title = { target = PREV type = invasion } }
		}
		
		if = {
			# Take the enemy's duchies within the target kingdom, if we are the same or lower tier
			limit = {
				ROOT = {
					NOT = { higher_tier_than = FROM }
				}
			}
			FROM = {
				any_demesne_title = {
					limit = {
						tier = DUKE
						OR = {
							title = PREVPREV
							de_jure_liege_or_above = PREVPREV
						}
					}
					ROOT = { usurp_title = { target = PREV type = invasion } }
				}
			}
		}
		
		# Have I become the vassal of the loser due to taking a duchy or kingdom?
		if = {
			limit = {
				ROOT = {
					is_liege_or_above = FROM	
				}
			}
			ROOT = {
				set_defacto_liege = ROOT
			}
		}
		
		# Vassalize the rest
		custom_tooltip = {
			text = pagan_subjugation_tip
			hidden_tooltip = {
				ROOT = {
					subjugate_or_take_under_title = { # If the target only has territory within the kingdom, he is simply vassalized
						title = PREV
						enemy = FROM
					}
				}
			}
		}
	}
	
	on_success = {
		hidden_tooltip = {
			ROOT = {
				if = {
					limit = {
						has_nickname = no
					}
					random_list = {
					  10 = {
						give_nickname = nick_the_conqueror
					  }
					  10 = {
						give_nickname = nick_the_victorious
					  }
					  10 = {
						give_nickname = nick_the_ruthless
					  }
					  10 = {
						give_nickname = nick_the_glorious
					  }
					  10 = {
						give_nickname = nick_the_avenger
					  }
					  10 = {
						give_nickname = nick_the_brave
					  }
					  30 = {
						# chance of nothing
					  }
					}
				}
			}
		}
		ROOT = {
			prestige = 100
		}
		if = {
			any_attacker = {
				limit = { not = { character = ROOT } }
				hidden_tooltip = { participation_scaled_prestige = 100 }
			}
		}
		
		FROM = {
			piety = -100
			prestige = -100
		}
	}
	
	on_fail = {
		FROM = {
			prestige = 100
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
		ROOT = {
			if = {
				limit = { ROOT = { tier = count } }
				prestige = -100
			}
			if = {
				limit = { ROOT = { tier = duke } }
				prestige = -150
			}
			if = {
				limit = { ROOT = { tier = king } }
				prestige = -200
			}
		}
	}
	
	on_reverse_demand = {
		
		ROOT = {
			if = {
				limit = { ROOT = { tier = count } }
				prestige = -200
			}
			if = {
				limit = { ROOT = { tier = duke } }
				prestige = -300
			}
			if = {
				limit = { ROOT = { tier = king } }
				prestige = -400
			}
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
		}
		FROM = {
			prestige = 200
		}
		if = {
			limit = { FROM = { tier = count } }
			any_defender = {
				limit = { not = { character = FROM } }
				hidden_tooltip = { participation_scaled_prestige = 100 }
			}
		}
		if = {
			limit = { FROM = { tier = duke } }
			any_defender = {
				limit = { not = { character = FROM } }
				hidden_tooltip = { participation_scaled_prestige = 150 }
			}
		}
		if = {
			limit = { FROM = { tier = king } }
			any_defender = {
				limit = { not = { character = FROM } }
				hidden_tooltip = { participation_scaled_prestige = 200 }
			}
		}
	}
	
	attacker_ai_victory_worth = {
		factor = -1
	}
	
	attacker_ai_defeat_worth = {
		factor = 100
	}

	defender_ai_victory_worth = {
		factor = -1 # always accept
	}
	
	defender_ai_defeat_worth = {
		factor = 100
	}
	
	ai_will_do = {
		factor = 1.5
	}
}