# Audax Validator EnableCommentMetadata

# Equivalent to "liberate_religion" ("# New6") in 01_cb_types
# Only can_use and can_use_gui have been changed from the vanilla version
# DAlso, deleted the duplicate is_valid_title block that's present in vanilla

expd_t_religious_liberation_cb = {
	name = CB_NAME_LIBERATE_RELIGION
	war_name = WAR_NAME_LIBERATE_RELIGION
	sprite = 35
	truce_days = 3650
	is_permanent = yes
	is_holy_war = yes
	check_de_jure_tier = KING
	apply_short_occ_mod = no # Do not apply the 'recently_conquered' modifier to Holdings

	allowed_to_target_tributaries = no
	
	sort_priority = 4000
	
	on_add = {
		hidden_tooltip = {
			if = { 
				limit = {
					NOT = {
						ROOT = {
							any_owned_bloodline = {  
								has_bloodline_flag = bloodline_religious_liberation
								bloodline_is_active_for = PREV
							}
						}
					}
				}
				ROOT = {
					set_character_flag = used_liberate_opportunity
				}
			}
		}
		hidden_tooltip = {
			# Audax Validator "." Ignore_NEXT
			fire_haruspicy_event_effect = yes
		}
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				# Audax Validator "." Ignore_NEXT
				detract_grace_super_huge_effect = yes 
			}
		}
	}

	can_use = {
		OR = {
			has_dlc = "Holy Fury" 
			has_dlc = "Jade Dragon"
		}
		ROOT = {
			OR = {
				any_owned_bloodline = {  
					has_bloodline_flag = bloodline_religious_liberation
					bloodline_is_active_for = PREV
				}
				NOT = {
					has_game_rule = {
						name = jade_dragon_cbs
						value = off
					}
				}
			}
			independent = yes
			OR = {
				any_owned_bloodline = {  
					has_bloodline_flag = bloodline_religious_liberation
					bloodline_is_active_for = PREV
				}
				AND = {
					has_character_flag = had_liberate_opportunity
					NOT = { has_character_flag = used_liberate_opportunity }
				}
			}
			is_nomadic = no
			mercenary = no
			NOT = { pacifist = yes }
			NOT = { same_realm = FROM }
			NOT = { religion = FROM }
			top_liege = { religion = ROOT } # Seems unnecessary (since ROOT nust be independent); retained for consistency with the vanilla CB
		}
	}
	
	can_use_title = {
		
		# The kingdom being targeted must contain a county of the attacker's religion.
		# NB: This means that the county's religion is different to its top liege's religion, due to can_use_title.
		any_de_jure_vassal_title = {
			tier = COUNT
			holder_scope = {
				OR = {
					character = FROM
					is_liege_or_above = FROM
				}
			}
			location = {
				religion = ROOT
			}
		}
		
		# The vanilla adjacency condition must NOT be true (because you should use the vanilla CB)... 
		# --- Vanilla adjacency description: The attacker needs at least one county in the target kingdom, or a border, or be only two sea zones away from one of your counties
		NOT = {
			any_de_jure_vassal_title = {
				OR = {
					holder_scope = {
						OR = {
							character = ROOT
							is_liege_or_above = ROOT
						}
					}
					location = {
						any_neighbor_province = {
							OR = {
								AND = {
									has_owner = yes
									owner = {
										OR = {
											character = ROOT
											is_liege_or_above = ROOT
										}
									}
								}
								AND = {
									is_land = no
									OR = {
										ROOT = { religion_group = muslim }
										FROM = { religion_group = muslim }
										AND = {
											ROOT = { religion_group = pagan_group }
											has_global_flag = pagan_ghws_unlocked
										}
										AND = {
											ROOT = { religion_group = christian }
											has_global_flag = christian_crusades_unlocked
										}
										AND = {
											ROOT = { religion_group = muslim }
											has_global_flag = muslim_jihads_unlocked
										}
									}
									any_neighbor_province = {
										OR = {
											AND = {
												has_owner = yes
												owner = {
													OR = {
														character = ROOT
														is_liege_or_above = ROOT
													}
												}
											}
											AND = {
												is_land = no
												any_neighbor_province = {
													owner = {
														OR = {
															character = ROOT
															is_liege_or_above = ROOT
														}
													}
												}
											}
										}
									}
								}
							}
						}
					}
				}
			}
		}
		
		# ... But this adjusted border condition MUST be true
		expd_t_from_borders_root_or_roots_same_religion_tributaries = yes
		expd_t_this_de_jure_kingdom_borders_root_or_roots_same_religion_tributaries = yes
		
	}

	is_valid = {
		ROOT = {
			NOT = { pacifist = yes }
			NOT = { same_realm = FROM }
			NOT = { religion = FROM }
			top_liege = {
				NOT = { religion = FROM }
			}
		}
	}
	
	is_valid_title = {
		FROM = {
			any_realm_province = {
				de_jure_liege_or_above = PREVPREV
			}
		}
	}
	
	on_success = {
		hidden_tooltip = { 
			any_attacker = {
				limit = { NOT = { character = ROOT } }
				
				participation_scaled_piety = 100
				participation_scaled_prestige = 50
				if = {
					limit = {
						uses_decadence = yes
					}
					participation_scaled_decadence = -10
				}
				if = {
					limit = {
						has_ambition = obj_strengthen_religion
					}
					change_variable = { which = strengthen_religion value = 1 }
				}
			}
		}
		
		ROOT = {
			show_scope_change = no
			participation_scaled_piety = 500
			participation_scaled_prestige = 250
			if = {
				limit = {
					uses_decadence = yes
				}
				participation_scaled_decadence = -10
			}
			religion_authority = {
				modifier = won_holy_war
				years = 20
			}
		}
		FROM = { show_scope_change = no piety = -100 }
		FROM = { show_scope_change = no prestige = -50 }
	}

	on_success_title = {
		custom_tooltip = {
			text = religious_liberation_cb_succ_tip
			hidden_tooltip = {
				FROM = {
					if = {
						limit = {
							any_realm_title = {
								tier = COUNT
								kingdom = { title = PREVPREVPREV }
								location = {
									religion = ROOT
								}
							}
						}
						random_realm_title = {
							limit = {
								tier = COUNT
								kingdom = { title = PREVPREVPREV }
								location = {
									religion = ROOT
								}
							}
							location = {
								create_character = {
									religion = ROOT
									culture = THIS
									dynasty = random
									random_traits = yes
									female = no
									age = 20
									health = 7
									fertility = 0.7
									attributes = {
										martial = 10
										diplomacy = 10
										stewardship = 10
									}
									flag = is_religious_liberation_ruler
								}
								new_character = {
									remove_trait = cynical
									add_trait = zealous
									wealth = 500
									prestige = 500
									piety = 500
									save_event_target_as = new_ruler
									usurp_title = { target = PREVPREV type = invasion }
								}
							}
						}
					}
					if = {
						limit = {
							NOR = {
								event_target:new_ruler = { is_alive = yes }
								any_realm_title = {
									tier = COUNT
									kingdom = { title = PREVPREVPREV }
									location = {
										religion = ROOT
									}
								}
							}
						}
						random_realm_title = {
							limit = {
								tier = COUNT
								kingdom = { title = PREVPREVPREV }
							}
							location = {
								create_character = {
									religion = ROOT
									culture = THIS
									dynasty = random
									random_traits = yes
									female = no
									age = 20
									health = 7
									fertility = 0.7
									attributes = {
										martial = 10
										diplomacy = 10
										stewardship = 10
									}
									flag = is_religious_liberation_ruler
								}
								new_character = {
									remove_trait = cynical
									add_trait = zealous
									wealth = 500
									prestige = 500
									piety = 500
									save_event_target_as = new_ruler
									usurp_title = { target = PREVPREV type = invasion }
								}
							}
						}
					}
				}
				if = {
					limit = {
						has_holder = no
					}
					usurp_title = { target = event_target:new_ruler type = invasion }
				}
				else = {
					event_target:new_ruler = {
						create_title = {
							tier = KING
							landless = no
							temporary = no
							custom_created = yes
							culture = event_target:new_ruler
							holder = event_target:new_ruler
							name = "LIBERATED_KINGDOM"
							base_title = PREV
						}
					}
				}
				
				event_target:new_ruler = {
					vassalize_or_take_under_title = {
						title = PREV
						enemy = FROM
						same_religion = yes # Only vassalize rulers of my religion
						is_religious = yes
						type = holy_war
						as_if_allied_to = ROOT
					}
					set_defacto_liege = THIS
				}
				
				if = {
					limit = {
						has_holder = yes
						holder_scope = {
							OR = {
								character = FROM
								is_liege_or_above = FROM
							}
						}
					}
					destroy_landed_title = THIS
					usurp_title_only = { target = event_target:new_ruler type = invasion }
					make_primary_title = yes
					event_target:new_ruler = {
						any_demesne_title = {
							limit = {
								tier = KING
								is_primary_holder_title = no
							}
							destroy_landed_title = THIS
						}
					}
				}
				
				ROOT = {
					hidden_tooltip = {
						if = {
							limit = {
								has_ambition = obj_strengthen_religion
							}
							change_variable = { which = strengthen_religion value = 1 }
						}
					}
					make_tributary = { who = event_target:new_ruler tributary_type = permanent }
				}
			}
		}

		hidden_tooltip = {
			check_if_reconquista_finished_effect = yes
			FROM = {
				# Audax Validator "." Ignore_NEXT
				landless_pope_effect = yes
			}
		}
	}

	on_fail = {
		ROOT = { 
			show_scope_change = no
			piety = -125 
			prestige = -250
		}
	}
	
	on_invalidation = {
		ROOT = {
			clr_character_flag = used_liberate_opportunity
		}
	}

	on_reverse_demand = {
		ROOT = {
			show_scope_change = no
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
			piety = -250
			prestige = -500
		}
		FROM = {
			show_scope_change = no
			participation_scaled_piety = 200
			participation_scaled_prestige = 100
			if = {
				limit = {
					uses_decadence = yes
				}
				participation_scaled_decadence = -10
			}
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { 
				participation_scaled_piety = 200
				participation_scaled_prestige = 100
				if = {
					limit = {
						uses_decadence = yes
					}
					participation_scaled_decadence = -10
				}
				hidden_tooltip = {
					if = {
						limit = {
							has_ambition = obj_strengthen_religion
						}
						change_variable = { which = strengthen_religion value = 1 }
					}
				}
			}
		}
		any_attacker = {
			limit = {
				character = ROOT
				uses_decadence = yes
			}
			participation_scaled_decadence = 20
		}
		any_attacker = {
			limit = {
				NOT = { character = ROOT }
				uses_decadence = yes
			}
			hidden_tooltip = { 
				participation_scaled_decadence = 20
			}
		}
		ROOT = { 
			religion_authority = {
				modifier = lost_holy_war
				years = 20
			}
		}
		FROM = { 
			religion_authority = {
				modifier = won_holy_war
				years = 20
			}
			hidden_tooltip = {
				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
		modifier = { #Focus on Coronation first.
			factor = 0.1
			has_dlc = "Holy Fury"
			ROOT = {
				is_feudal = yes
				OR = {
					religion = catholic
					religion = fraticelli
				}
				NOR = {
					trait = crowned_by_priest
					trait = crowned_by_bishop
					trait = crowned_by_pope
					trait = crowned_by_myself
				}
			}
		}
	}
}