# buddhist_holy_war
# indian_subjugation

# 51 - Disabled in CK2Plus - see 'holy_war'
buddhist_holy_war = {
	name = CB_NAME_RELIGIOUS_MINOR
	war_name = WAR_NAME_RELIGIOUS
	sprite = 9
	truce_days = 1825
	sort_priority = 800

	hostile_against_others = yes
	is_permanent = no # Disable in CK2Plus
	check_all_titles = yes # if permanent, setting this to true will check against all of someones titles, including vassal held titles
	can_ask_to_join_war = no
	allowed_to_target_tributaries = no

	can_use = {
		always = no # Disabled in CK2Plus

		# Attacker
		ROOT = {
			religion = buddhist
			NOT = { same_realm = FROM }

			trigger_if = {
				limit = { religion_group = FROM }

				OR = {
					FROM = { religion = taoist }

					# Can always attack known devil worshippers of the same religion
					AND = {
						FROM = { has_character_modifier = known_satanist }
						NOT = { has_character_modifier = known_satanist }
					}
				}
			}

			top_liege = {
				religion_group = ROOT

				OR = {
					NOT = { religion = FROM }
					FROM = { religion = taoist }

					# Can always attack known devil worshippers of the same religion
					trigger_if = {
						limit = { religion_group = FROM }

						FROM = { has_character_modifier = known_satanist }
						NOT = { has_character_modifier = known_satanist }

						ROOT = {
							NOT = { has_character_modifier = known_satanist }
						}
					}
				}
			}

			is_nomadic = no
			mercenary = no
		}
	}

	can_use_title = {
		real_tier = COUNT

		# The attacker needs a border or be at the most two sea zones away
		location = {
			any_neighbor_province = {
				OR = {
					trigger_if = {
						limit = { has_owner = yes }

						owner = {
							OR = {
								character = ROOT
								is_vassal_or_below_of = ROOT
							}
						}
					}
					trigger_else = {
						is_land = no # first sea zone

						any_neighbor_province = {
							OR = {
								trigger_if = {
									limit = { has_owner = yes }

									owner = {
										OR = {
											character = ROOT
											is_vassal_or_below_of = ROOT
										}
									}
								}
								trigger_else = {
									is_land = no # second sea zone

									any_neighbor_province = {
										owner = {
											OR = {
												character = ROOT
												is_vassal_or_below_of = ROOT
											}
										}
									}
								}
							}
						}
					}
				}
			}
		}
	}

	is_valid = {
		ROOT = {
			religion = buddhist
			NOT = { same_realm = FROM }

			trigger_if = {
				limit = { religion_group = FROM }

				OR = {
					FROM = { religion = taoist }

					# Can always attack known devil worshippers of the same religion
					AND = {
						FROM = { has_character_modifier = known_satanist }
						NOT = { has_character_modifier = known_satanist }
					}
				}
			}

			top_liege = {
				OR = {
					NOT = { religion = FROM }
					FROM = { religion = taoist }

					AND = { # Can always attack known devil worshippers of the same religion
						religion_group = FROM
						FROM = { has_character_modifier = known_satanist }
						NOT = { has_character_modifier = known_satanist }

						ROOT = {
							NOT = { has_character_modifier = known_satanist }
						}
					}
				}
			}
		}
	}

	on_add = {
		fire_haruspicy_event_effect = yes
		anger_china_effect = yes
	}

	on_success = {
		hidden_effect = {
			any_attacker = {
				if = {
					limit = { has_ambition = obj_strengthen_religion }

					change_variable = {
						which = strengthen_religion
						value = 1
					}
				}
			}
		}
	}

	on_success_title = {
		if = { # Vassalise one county counts
			limit = {
				holder_scope = {
					real_tier = COUNT
					num_of_count_titles == 1
					lower_real_tier_than = attacker
					is_patrician = no
				}
			}

			holder_scope = {
				set_defacto_liege = attacker
			}
		}

		else = { # Usurp title in all other circumstances
			usurp_title_plus_barony_if_unlanded = {
				target = attacker
				type = holy_war
			}

			any_de_jure_vassal_title = { # take all baronies under the one we're fighting for
				limit = {
					has_holder = yes
					NOT = { de_facto_liege = PREV }

					holder_scope = {
						OR = {
							character = defender
							is_vassal_or_below_of = defender
						}
					}
				}

				usurp_title_plus_barony_if_unlanded = {
					target = attacker
					type = holy_war
				}
			}
		}

		attacker = {
			show_scope_change = no

			participation_scaled_prestige = 100
		}

		any_attacker = {
			limit = { NOT = { character = attacker } }
			hidden_effect = { participation_scaled_prestige = 100 }
		}
	}

	on_fail_title = {
		attacker = {
			show_scope_change = no

			prestige = -100
		}

		defender = {
			show_scope_change = no

			participation_scaled_prestige = 50
		}

		any_defender = {
			limit = { NOT = { character = defender } }
			hidden_effect = { participation_scaled_prestige = 50 }
		}
	}

	on_reverse_demand = {
		attacker = {
			show_scope_change = no

			prestige = -200

			transfer_scaled_wealth = {
				to = defender
				value = 4.0
			}
		}

		defender = {
			show_scope_change = no

			participation_scaled_prestige = 100

			hidden_effect = {
				if = {
					limit = { has_ambition = obj_strengthen_religion }

					change_variable = {
						which = strengthen_religion
						value = 1
					}
				}
			}
		}

		any_defender = {
			limit = { NOT = { character = defender } }

			hidden_effect = {
				participation_scaled_prestige = 100

				if = {
					limit = { has_ambition = obj_strengthen_religion }

					change_variable = {
						which = strengthen_religion
						value = 1
					}
				}
			}
		}
	}

	attacker_ai_victory_worth = {
		factor = -1 # always accept
	}

	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

		trigger = {
			trigger_if = {
				limit = {
					ROOT = {
						is_tribal = yes
					}

					FROM = {
						is_nomadic = yes
					}
				}

				any_direct_de_jure_vassal_title = {
					any_direct_de_jure_vassal_title = {
						OR = {
							holding_type = castle
							holding_type = city
							holding_type = tribal
						}
					}
				}
			}
			trigger_else_if = {
				limit = {
					ROOT = {
						is_nomadic = no
						is_tribal = no
					}

					FROM = {
						is_nomadic = yes
					}

				}

				any_direct_de_jure_vassal_title = {
					any_direct_de_jure_vassal_title = {
						OR = {
							holding_type = castle
							holding_type = city
						}
					}
				}
			}
		}
	}
}

# 52: The Indian religions can subjugate each other
# Disabled and rolled into 'pagan_subjugation'
indian_subjugation = {
	name = CB_NAME_PAGAN_SUBJUGATION
	war_name = WAR_NAME_PAGAN_SUBJUGATION
	sprite = 8
	truce_days = 365
	sort_priority = 1000

	hostile_against_others = yes
	is_permanent = no # Disabled in CK2Plus
	can_ask_to_join_war = 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
	allowed_to_target_tributaries = no

	can_use_gui = {
		# Attacker
		ROOT = {
			piety >= 500
		}
	}

	can_use = {
		always = no # Disabled in CK2Plus, see above
		# Attacker
		ROOT = {
			has_dharmic_religion_trigger = yes
			culture_group = FROM
			NOT = { same_realm = FROM }
			has_horde_culture = no # Should use tribal invasion instead
			is_nomadic = no
			mercenary = no
		}

		# Defender
		FROM = {
			has_dharmic_religion_trigger = yes

			NOR = {
				is_offmap_governor = yes

				any_war = {
					defender = { character = PREVPREV }
					using_cb = indian_subjugation
				}
			}
		}
	}

	can_use_title = {
		# Attacker
		ROOT = {
			trigger_if = { # can only do it once, except in de jure kingdom of current capital with the right ambition
				limit = { has_character_modifier = launched_subjugation }

				has_ambition = obj_become_king

				capital_scope = {
					kingdom = {
						title = PREVPREVPREV
					}
				}
			}
		}

		# Defender
		FROM = {
			num_of_realm_counties = {
				value >= 1
				title = PREV
			}
		}
	}

	is_valid_title = {
		# Defender
		FROM = {
			num_of_realm_counties = {
				value >= 1
				title = PREV
			}
		}
	}

	on_add = {
		attacker = {
			show_scope_change = no

			piety = -500
		}

		fire_haruspicy_event_effect = yes
		anger_china_effect = yes
	}

	on_add_title = {
		attacker = {
			show_scope_change = no

			if = {
				limit = {
					trigger_if = {
						limit = { has_ambition = obj_become_king }

						capital_scope = {
							kingdom = {
								NOT = { title = PREVPREVPREV }
							}
						}
					}
				}

				add_character_modifier = {
					name = launched_subjugation
					years = 1000
					hidden = yes
				}
			}
		}
	}

	on_success = {
		attacker = {
			show_scope_change = no

			participation_scaled_prestige = 200
		}

		any_attacker = {
			limit = { NOT = { character = attacker } }
			hidden_effect = { participation_scaled_prestige = 200 }
		}

		defender = {
			show_scope_change = no

			prestige = -200
		}
	}

	on_success_title = {
		# Usurp the target kingdom itself, if the enemy holds it
		if = {
			limit = {
				holder = defender

				attacker = {
					lower_real_tier_than = EMPEROR
				}
			}

			attacker = {
				show_scope_change = no

				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 = {
				attacker = {
					NOT = { higher_real_tier_than = defender }
				}
			}

			defender = {
				show_scope_change = no

				any_demesne_title = {
					limit = {
						real_tier = DUKE

						OR = {
							title = PREVPREV
							de_jure_liege_or_above = PREVPREV
						}
					}

					attacker = {
						show_scope_change = no

						usurp_title = {
							target = PREV
							type = invasion
						}
					}
				}
			}
		}

		# Have I become the vassal of the loser due to taking a duchy or kingdom?
		# TODO: Ensure vassals using this CB can't become independent!
		if = {
			limit = {
				attacker = {
					is_vassal_or_below_of = defender
				}
			}

			attacker = {
				set_defacto_liege = THIS
			}
		}

		# Vassalize the rest
		custom_tooltip = {
			text = pagan_subjugation_tip

			attacker = {
				subjugate_or_take_under_title = { # If the target only has territory within the kingdom, he is simply vassalized
					title = PREV
					enemy = defender
				}
			}
		}
	}

	on_fail = {
		attacker = {
			show_scope_change = no

			prestige = -100
		}

		defender = {
			show_scope_change = no

			participation_scaled_prestige = 100
		}

		any_defender = {
			limit = { NOT = { character = defender } }
			hidden_effect = { participation_scaled_prestige = 100 }
		}
	}

	on_reverse_demand = {
		attacker = {
			show_scope_change = no

			prestige = -200

			transfer_scaled_wealth = {
				to = defender
				value = 2.0
			}
		}

		defender = {
			show_scope_change = no

			participation_scaled_prestige = 200
		}

		any_defender = {
			limit = { NOT = { character = defender } }
			hidden_effect = { participation_scaled_prestige = 200 }
		}
	}

	on_invalidation = {
		attacker = {
			show_scope_change = no

			remove_character_modifier = launched_subjugation
			piety = 500
		}
	}

	attacker_ai_victory_worth = {
		factor = -1 # always accept
	}

	attacker_ai_defeat_worth = {
		factor = 100
	}

	defender_ai_victory_worth = {
		factor = -1 # always accept
	}

	defender_ai_defeat_worth = {
		factor = 100
	}

	ai_will_do = {
		factor = 0.9 # Prefer using CB's without on_add costs

		trigger = {
			trigger_if = {
				limit = {
					ROOT = {
						is_tribal = yes
					}

					FROM = {
						is_nomadic = yes
					}
				}

				any_direct_de_jure_vassal_title = {
					any_direct_de_jure_vassal_title = {
						OR = {
							holding_type = castle
							holding_type = city
							holding_type = tribal
						}
					}
				}
			}
			trigger_else_if = {
				limit = {
					ROOT = {
						is_nomadic = no
						is_tribal = no
					}

					FROM = {
						is_nomadic = yes
					}

				}

				any_direct_de_jure_vassal_title = {
					any_direct_de_jure_vassal_title = {
						OR = {
							holding_type = castle
							holding_type = city
						}
					}
				}
			}

			trigger_if = {
				limit = {
					ROOT = {
						has_character_flag = is_temujin
						has_landed_title = e_mongol_empire
						is_landed = yes
					}
				}

				FROM = {
					NOT = { has_landed_title = e_china_west_governor }
				}
			}
		}
	}
}

