# For all war-related effects
total_invasion_cb_rev_effect = {
	# Wealth, piety, prestige and bloodlines
	prestige = -1500
	piety = -750
	transfer_scaled_wealth = {
		to = event_target:target_defender
		value = 6.0
	}
	if = {
		limit = { NOT = { religion = event_target:target_defender } }
		religion_authority = {
			modifier = lost_total_invasion
			years = 20
		}
	}
	PREV = {
		prestige = 1500
		piety = 750
		hidden_tooltip = {
			if = {
				limit = {
					has_nickname = no
				}
				random_list = {
					20 = { give_nickname = nick_the_great }
					20 = { give_nickname = nick_the_hammer }
					20 = { give_nickname = nick_the_avenger }
					20 = { give_nickname = nick_the_lion }
					20 = { give_nickname = nick_the_bold }
				}
			}
		}
		if = {
			limit = { NOT = { religion = event_target:target_attacker } }
			religion_authority = {
				modifier = won_total_invasion
				years = 20
			}
		}
		participation_scaled_prestige = 1500
	}
	#Bloodlines
	if = {
		limit = {
			has_landed_title = e_scourge
			PREV = { NOT = { any_owned_bloodline = { has_bloodline_flag = end_of_scourge_bloodline_flag } } }
		}
		PREV = {
			if = {
				limit = { trait = creature_lothraxim }
				create_bloodline = {
					type = end_of_scourge
					inheritance = all_descendants
				}
			}
			else_if = {
				limit = { is_female = yes }
				create_bloodline = {
					type = end_of_scourge
					inheritance = matrilineal
				}
			}
			else = {
				create_bloodline = {
					type = end_of_scourge
				}
			}				
		}
	}
	else_if = {
		limit = {
			has_landed_title = e_burning_legion
			PREV = { NOT = { any_owned_bloodline = { has_bloodline_flag = end_of_legion_bloodline_flag } } }
		}
		PREV = {
			if = {
				limit = { 
					trait = creature_lothraxim
					religion_group = light_group
					has_game_rule = {
						name = lothraxim_bloodline
						value = on
					}
					
					NOR = { 
						any_owned_bloodline = { has_bloodline_flag = lothraxim_redeemer_bloodline_flag }
						has_character_flag = lothraxim_redeemer_flag 
					}
				}
				character_event = { id = WCRAC.1125 } 
			}
			else_if = {
				limit = { trait = creature_lothraxim }
				create_bloodline = {
					type = end_of_legion
					inheritance = all_descendants
				}
			}
			else_if = {
				limit = { is_female = yes }
				create_bloodline = {
					type = end_of_legion
					inheritance = matrilineal
				}
			}
			else = {
				create_bloodline = {
					type = end_of_legion
				}
			}
		}
	}
	else_if = {
		limit = {
			has_landed_title = e_burning_legion
			PREV = { any_owned_bloodline = { has_bloodline_flag = end_of_legion_bloodline_flag } } 
		}
		PREV = {
			if = {
				limit = { 
					trait = creature_lothraxim
					religion_group = light_group
					has_game_rule = {
						name = lothraxim_bloodline
						value = on
					}
					
					NOR = { 
						any_owned_bloodline = { has_bloodline_flag = lothraxim_redeemer_bloodline_flag }
						has_character_flag = lothraxim_redeemer_flag 
					}
				}
				character_event = { id = WCRAC.1125 } 
			}
		}
	}
	else_if = {
		limit = {
			has_landed_title = e_black_empire
			PREV = { NOT = { any_owned_bloodline = { has_bloodline_flag = end_of_black_empire_bloodline_flag } } }
		}
		PREV = {
			if = {
				limit = { trait = creature_lothraxim }
				create_bloodline = {
					type = end_of_black_empire
					inheritance = all_descendants
				}
			}
			else_if = {
				limit = { is_female = yes }
				create_bloodline = {
					type = end_of_black_empire
					inheritance = matrilineal
				}
			}
			else = {
				create_bloodline = {
					type = end_of_black_empire
				}
			}
		}
	}

	custom_tooltip = {
		text = total_invasion_cb_rev_tooltip
		hidden_tooltip = {
			any_realm_lord = {
				limit = { ai = no }
				set_defacto_liege = THIS	# Gives players a chance
			}
			any_realm_province = {
				limit = {
					county = {
						title_can_be_lost_trigger = yes
						holder_scope = {
							OR = {
								character = PREVPREVPREV
								is_liege_or_above = PREVPREVPREV
							}
						}
					}
				}
				county = { give_to_claimant_effect = yes }
			}

			# Stops invasions
			if = {
				limit = {
					has_landed_title = e_horde
					has_global_flag = orcish_invasion_flag
				}
				clr_global_flag = orcish_invasion_flag
			}

			if = {
				limit = { independent = yes }
				any_realm_lord = {
					target_defender_usurp_all_demesne_provinces_effect = yes
				}
			}
			target_defender_usurp_all_demesne_provinces_effect = yes
		}
	}

	if = {
		limit = {
			OR = {
				is_dark_being_trigger = yes
				has_invader_title_trigger = yes
			}
		}
		death = { death_reason = death_battle }
	}
}
total_invasion_cb_suc_effect = {
	custom_tooltip = {
		text = total_invasion_cb_suc_tooltip_01
		hidden_tooltip = {
			event_target:target_defender = {
				any_vassal = {
					limit = {
						OR = {
							religion = event_target:target_attacker
							true_religion = { target = event_target:target_attacker target_type = public }
							AND = {
								OR = {
									religion = orcish_fel
									religion = burning_legion_religion
									true_religion = orcish_fel
									true_religion = burning_legion_religion
								}
								event_target:target_attacker = {
									OR = {
										religion = orcish_fel
										religion = burning_legion_religion
									}
								}
							}
						}
					}
					convert_to_secret_religion = yes
					set_defacto_liege = event_target:target_attacker
				}
				any_realm_lord = {
					limit = { ai = no }
					set_defacto_liege = THIS	# Gives players a chance
				}
				# Usurp provinces (so it leaves baronies if county isn't controlled)
				any_realm_province = {
					limit = {
						county = {
							title_can_be_lost_trigger = yes
							holder_scope = {
								OR = {
									character = PREVPREVPREV
									is_liege_or_above = PREVPREVPREV
								}
							}
						}
					}
					county = {
						event_target:target_attacker = {
							gain_settlements_under_title = {
								title = PREV
								enemy = event_target:target_defender
								is_crusade = yes # Even if the title holder is not participating in the war, gain holdings occupied by all Crusade participants
								type = invasion
							}
							grant_title_no_opinion = { target = PREV type = invasion }
						}
					}
				}
			}
		}
	}
}

# Gives holdings under title even if it's occupated unlike gain_settlements_under_title
give_holdings_under_title_effect = {
	event_target:target_title = {
		any_direct_de_jure_vassal_title = {
			limit = {
				holder_scope = {
					OR = {
						character = event_target:target_attacker 
						is_liege_or_above = event_target:target_attacker 
					}
				}
			}
			grant_title_no_opinion = { target = event_target:target_new_ruler type = claim }
		}
	}
}
# Effects to give county to claimant. If there's no claimant, creates it
give_to_claimant_effect = {
	if = {
		limit = {
			kingdom = { NOT = { title = k_black_morass } }	# Lands around the portal always go away to defender
		}
		save_event_target_as = target_title
		location = { save_event_target_as = target_location }

		# Kingdom
		if = {
			limit = {
				kingdom = {
					OR = {
						has_holder_claimant_trigger = yes
						has_landless_claimant_trigger = yes
					}
				}
			}
			kingdom = {
				save_event_target_as = target_new_realm
				
				if = {
					limit = {
						has_holder_claimant_trigger = yes
					}
					give_to_holder_claimant_effect = yes
				}
				else = {
					give_to_landless_claimant_effect = yes
				}
			}
		}
		# Duchy
		else_if = {
			limit = {
				OR = {
					dejure_liege_title = { has_holder_claimant_trigger = yes }
					has_spawned_claimant_trigger = yes
				}
			}
			dejure_liege_title = {
				save_event_target_as = target_new_realm
				
				if = {
					limit = {
						has_holder_claimant_trigger = yes
					}
					give_to_holder_claimant_effect = yes
				}
				else = {
					give_to_spawned_claimant_effect = yes
				}
			}
		}
	}
}
give_to_holder_claimant_effect = {
	holder_scope = {
		log = "(Great War) [This.GetBestName] was chosen as new king of [target_title.GetFullBaseName]"
		save_event_target_as = target_new_ruler
		
		give_holdings_under_title_effect = yes
		grant_title_no_opinion = { target = event_target:target_title type = claim }
		
		if = {
			limit = { primary_title = { capital_scope = { province = event_target:target_location } } }
			capital = event_target:target_title
		}
		
		spawn_target_new_ruler_troops_effect = yes

		# set_defacto_liege = THIS
	}
}
give_to_landless_claimant_effect = {
	any_claimant = {
		limit = {
			is_landless_claimant_trigger = yes
		}
		if = {
			limit = {
				# To make sure
				event_target:target_title = {
					holder_scope = {
						OR = {
							character = event_target:target_attacker
							same_realm = event_target:target_attacker
						}
					}
				}
			}
			log = "(Great War) [This.GetBestName] was chosen as new king of [target_title.GetFullBaseName]"
			save_event_target_as = target_new_ruler
			save_event_target_as = target_settler

			if = {
				limit = {
					is_ruler = yes
					is_feudal = no
					is_tribal = no
				}
				abdicate = yes
			}
		
			# target_settler build new holding or usurp existing one
			event_target:target_title = { location = { build_or_give_holding_to_target_settler_effect = yes } }

			give_holdings_under_title_effect = yes
			grant_title_no_opinion = { target = event_target:target_title type = claim }

			event_target:target_new_realm = {
				if = {
					limit = {
						has_holder = yes
						holder_scope = { NOT = { character = event_target:target_new_ruler } }
					}
					destroy_landed_title = THIS
				}
				grant_title_no_opinion = { target = event_target:target_new_ruler type = claim }
			}

			spawn_target_new_ruler_troops_effect = yes

			set_defacto_liege = THIS
		}
	}
}
give_to_spawned_claimant_effect = {
	event_target:target_location = {
		create_character = {
			female = no
			dynasty = none
			religion = THIS
			culture = THIS
			age = 30
			trait = ambitious trait = zealous
			random_traits = yes
			flag = spawned_character_flag
		}
		new_character = {
			log = "(Great War) [This.GetBestName] was chosen as new king of [target_title.GetFullBaseName]"
			save_event_target_as = target_new_ruler
			save_event_target_as = target_settler
			
			if = {
				limit = { evil_public_religion_trigger = yes }
				random = {
					chance = 66
					
					switch_to_random_non_evil_religion_effect = yes
				}
			}
			
			# target_settler build new holding or usurp existing one
			event_target:target_title = { location = { build_or_give_holding_to_target_settler_effect = yes } }

			give_holdings_under_title_effect = yes
			grant_title_no_opinion = { target = event_target:target_title type = claim }

			event_target:target_new_realm = {
				if = {
					limit = {
						has_holder = yes
						holder_scope = { NOT = { character = event_target:target_new_ruler } }
					}
					destroy_landed_title = THIS
				}
				grant_title_no_opinion = { target = event_target:target_new_ruler type = claim }
			}

			spawn_target_new_ruler_troops_effect = yes

			set_defacto_liege = THIS
		}
	}
}
spawn_target_new_ruler_troops_effect = {
	spawn_unit = {
		owner = event_target:target_new_ruler
		province = event_target:target_location
		home = event_target:target_location
		troops = {
			archers = { 65 65 }
			heavy_infantry = { 175 175 }
			light_cavalry = { 15 15 }
			light_infantry = { 25 25 }
		}
		attrition = 1.0
		maintenance = no
	}
}

prev_subjugate_this_destroy_titles_effect = {
	# If has titles of same tier or higher, takes it
	if = {
		limit = { NOT = { lower_tier_than = PREV } }
		# If these titles can't be conquered, just destroys it and takes all direct vassals
		if = {
			limit = {
				any_demesne_title = {
					NOT = { lower_tier_than = PREVPREV }
					title_can_be_lost_trigger = no
				}
			}
			hidden_effect = { any_vassal = { set_defacto_liege = PREVPREV } }
			any_demesne_title = {
				limit = { NOT = { lower_tier_than = PREVPREV } }
				if = {
					limit = { is_landless_type_title = yes }
					activate_title = { title = THIS status = no }
				}
				destroy_landed_title = THIS
			}
		}
		# If these titles can be conqured, takes it to transfer vassals propertly and destroys it
		else = {
			any_demesne_title = {
				limit = { NOT = { lower_tier_than = PREVPREV } }
				hidden_effect = { grant_title_no_opinion = { target = PREVPREV type = claim } }
				destroy_landed_title = THIS
			}
		}
	}
	set_defacto_liege = PREV
}

# Used in the Frozen Throne bookmark
this_kill_from_balnazzar_effect = {
	if = {
		limit = {
			start_date = 605.6.6
			OR = {
				has_character_flag = is_sylvanas
				has_character_flag = is_garithos
			}
			FROM = { has_character_flag = is_balnazzar }
		}
		FROM = {
			show_scope_change = no
			
			death = { death_reason = death_twisting_nether }
		}
	}
}

religious_cb_succ_effect = {
	custom_tooltip = {
		text = religious_cb_succ_tip
		hidden_tooltip = {
			ROOT = {
				vassalize_or_take_under_title = {
					title = PREV
					enemy = FROM
					same_religion = yes # Only vassalize rulers of my religion
					is_religious = yes
					type = holy_war
				}
			}
		}
	}
}

root_usurp_from_realm_titles_plus_this_title = {
	ROOT = { save_event_target_as = target_defender }
	
	FROM = {
		create_temporary_landless_title_effect = yes
		if = {
			limit = { independent = yes }
			any_realm_lord = {
				target_defender_usurp_all_demesne_provinces_effect = yes
			}
		}
		target_defender_usurp_all_demesne_provinces_effect = yes
	}
	usurp_title = { target = ROOT type = holy_war }
	FROM = { destroy_temporary_landless_title_effect = yes }
}

target_defender_usurp_all_demesne_provinces_effect = {
	any_demesne_title = {
		limit = {
			title_can_be_lost_trigger = yes
			lower_tier_than = DUKE
		}
		grant_title_no_opinion = { target = event_target:target_defender type = invasion }
	}
}
prev_usurp_all_demesne_provinces_effect = {
	any_demesne_title = {
		limit = {
			title_can_be_lost_trigger = yes
			lower_tier_than = DUKE
		}
		grant_title_no_opinion = { target = PREVPREV type = invasion }
	}
}