## 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)

religious_liberation = {
	name = CB_RELIGIOUS_LIBERATION
	war_name = WAR_RELIGIOUS_LIBERATION
	sprite = 9
	truce_days = 1825
	is_permanent = yes
	can_call_allies = yes # Can either side call allies
	can_attack_vassals = no
	can_ask_to_join_war = yes
	is_holy_war = yes

	can_use = {
		has_dlc = "Charlemagne"		
		FROM = {
			NOT = { 
				religion = ROOT 
			}
			NOT = { 
				same_realm = ROOT 
			}
		}
		OR = {
			ROOT = {
				religion_head = {
					liege = { character = FROM }
					NOT = { religion = FROM }
				}
			}
			AND = {
				FROM = {
					OR = {
						any_realm_province = {
							count = 1
							county = { holder_scope = { NOT = { same_realm = ROOT } } }
							religion = ROOT
						}
						any_realm_lord = {
							count = 1
							religion = ROOT
						}
					}
					OR = {
						any_realm_province = {
							count = 6
							religion = ROOT
							county = { holder_scope = { NOT = { same_realm = ROOT } } }
						}
						any_realm_lord = {
							count = 6
							higher_tier_than = baron
							religion = ROOT
						}
						any_realm_lord = {
							count = 2
							higher_tier_than = count
							religion = ROOT
						}
						ROOT = { ai = no }
					}
					any_realm_province = {
						any_neighbor_province = {
							owner = {
								OR = {
									character = ROOT
									any_liege = { character = ROOT }
								}
							}
						}
					}
				}
			}
		}
	}
	
	is_valid = {
		FROM = {
			NOT = { religion = ROOT }
		#	independent = yes
			OR = {
				any_realm_province = {
					count = 1
					religion = ROOT
				}
				ROOT = { 
					religion_head = { 
						liege = { 
							character = FROM 
						}
					}
				}
			}
		}
	}
		
	on_success = { #provinces which follow your religion become independent.

		FROM = {
			any_realm_lord = {
				limit = { 
					liege = { 
						NOT = { 
							religion = ROOT 
						}
					}
					NOT = {
						any_liege = {
							religion = ROOT 
						}
					}
					religion = ROOT
				}
				set_defacto_liege = THIS
				set_character_flag = religiously_liberated
				set_character_flag = already
			}
			any_realm_province = {
				limit = {
					religion = ROOT
					county = { holder_scope = { NOT = { same_realm = ROOT } } }
				}
				county = {
				#	if = {
				#		limit = { FROM = { completely_controls = PREV } }
				#	}
					create_random_soldier = {
						random_traits = yes
						dynasty = random
						religion = THIS
						culture = THIS
						female = no
						age = 23
						trait = brilliant_strategist
						trait = zealous
					}
					new_character = {
						usurp_title_plus_barony_if_unlanded = PREV
						set_defacto_liege = THIS
						opinion = {
							modifier = Liberated
							who = ROOT
						}
						set_character_flag = religiously_liberated

					}
					ROOT = {
						piety = 25
					}
				}
			}
		}
		any_playable_ruler = {
			limit = {
				religion = ROOT
				has_character_flag = religiously_liberated
			}
			any_playable_ruler = {
				limit = {
					religion = ROOT
					has_character_flag = religiously_liberated
				}
			
				opinion = {
					modifier = religious_ally
					who = PREV
				}	
			}
		}
		any_playable_ruler = {
			limit = { 
				has_character_flag = religiously_liberated
				independent = yes
				NOT = { has_character_flag = already }
			}
			primary_title = {
				kingdom = {
					create_title = {
						tier = DUKE
						landless = no
						temporary = no
						custom_created = yes
						culture = PREVPREV
						holder = PREVPREV
						base_title = THIS
						copy_title_laws = yes
						name = RELIGIOUS_ALLIANCE
					}
					set_title_flag = culture_kingdom
				}
			}
			any_playable_ruler = {
				limit = { 
					NOT = { has_character_flag = already }
					has_character_flag = religiously_liberated
					culture = PREV
					capital_scope = { 
						kingdom = {
							has_title_flag = culture_kingdom
						}
					}
				}
				set_defacto_liege = PREV
			}
			capital_scope = {
				kingdom = {
					clr_title_flag = culture_kingdom
				}
			}
		}
		any_playable_ruler = {
			limit = { 
				has_character_flag = religiously_liberated
			}
			clr_character_flag = religiously_liberated
			clr_character_flag = already
		}
	}
	on_fail = {
		ROOT = {
			piety = -100
		}
		FROM = {
			piety = 100
		}
		any_defender = {
			limit = { character = FROM }
			participation_scaled_prestige = 50
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 50 }
		}
	}

	on_reverse_demand = {
		ROOT = {
			piety = -200
		}
		FROM = {
			piety = 200
		}
		ROOT = {
			prestige = -200
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
		}
	}
	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 = 2 #high desire
	}	
	
}
