﻿### EK NOTE: Removed references to real world religions
nation_fracturing_faction = {
	casus_belli = nation_fracturing_faction_war
	
	short_effect_desc = nation_fracturing_faction_short_effect_desc

	sort_order = 0

	discontent_progress = {
		base = 0

		common_discontent_progress_modifier = yes
	}

	power_threshold = {
		base = 80

		modifier = {
			add = 20
			faction_target = {
				has_perk = hard_rule_perk
			}
			desc = "FACTION_POWER_HARD_RULE"
		}

		dynamic_power_threshold_scripted_modifier = {
			FACTION_TYPE1 = claimant_faction
			FACTION_TYPE2 = liberty_faction
			FACTION_TYPE3 = populist_faction
			FACTION_TYPE4 = ek_crisis_faction # EK EDIT: account for crisis
		}
	}

	is_character_valid = {
		common_character_validity_trigger = {
			FACTION_TARGET = scope:faction.faction_target
		}
		has_valid_faction_members_trigger = yes
	}
	
	demand = {
		save_scope_as = faction

		faction_leader = {
			save_scope_as = faction_leader
		}

		faction_target = {
			save_scope_as = faction_target
		}

		# Let the human players in the faction know that the demand will be sent
		every_faction_member = {
			limit = {
				is_ai = no
				NOT = { this = scope:faction.faction_leader }
			}
			trigger_event = faction_demand.1005
		}

		# Send the actual demand in 5 days
		faction_target = {
			trigger_event = {
				id = faction_demand.1000
				days = 5
			}
		}
	}

	can_character_create_ui = {
		base_faction_trigger = { FACTION_TYPE = nation_fracturing_faction }

		common_can_character_create_trigger = {
			FACTION_TARGET = scope:target
		}

		liege = {
			is_independent_ruler = yes
			NOT = { government_has_flag = government_is_theocracy }
			#EK DISABLED
			#custom_tooltip = {
			#	text = nation_fracturing_faction.can_character_create_ui.blocked_by_struggle
			#	# We could use a parameter but this is just infinitely more performant.
			#	NOT = {
			#		any_held_title = { has_variable = struggle_block_dissolution_faction }
			#	}
			#}
		}

		# Only powerfull vassals can create such factions and they need to have either another culture or faith
		OR = {
			is_powerful_vassal = yes
			government_has_flag = government_is_nomadic
			is_ai = no
		}
		
        #EK DISABLED: No Islam in TES
		#trigger_if = {
		#	limit = {
		#		faith.religion = religion:islam_religion
		#		liege = { has_title = title:d_sunni }
		#		title:d_sunni = {
		#			has_variable = dar_al_islam
		#		}
		#	}
		#	liege = {
		#		NOT = {
		#			has_title = title:d_sunni
		#		}
		#	}
		#}
		
		# Faction is not allowed against administrative rulers
		#trigger_if = {
		#	limit = {
		#		scope:target = { government_allows = administrative }
		#	}
		#	scope:target = { save_temporary_scope_as = recipient }
		#	custom_tooltip = {
		#		text = has_admin_gov
		#		NOT = { scope:target = { government_allows = administrative } }
		#	}
		#}
	}

	can_character_create = {
		common_can_character_create_trigger = {
			FACTION_TARGET = scope:target
		}
		
		liege = {
			is_independent_ruler = yes
			NOT = { government_has_flag = government_is_theocracy }
			#EK DISABLED
			#custom_tooltip = {
			#	text = nation_fracturing_faction.can_character_create_ui.blocked_by_struggle
			#	# We could use a parameter but this is just infinitely more performant.
			#	NOT = {
			#		any_held_title = { has_variable = struggle_block_dissolution_faction }
			#	}
			#}
		}
		
		# Only powerfull vassals can create such factions and they need to have either another culture or faith
		OR = {
			is_powerful_vassal = yes
			is_ai = no
		}
		
		# EK DISABLED: No Islam in TES
		#trigger_if = {
		#	limit = {
		#		faith.religion = religion:islam_religion
		#		liege = { has_title = title:d_sunni }
		#		title:d_sunni = {
		#			has_variable = dar_al_islam
		#		}
		#	}
		#	liege = {
		#		NOT = {
		#			has_title = title:d_sunni
		#		}
		#	}
		#}
		
		# Faction is not allowed against administrative rulers
		#trigger_if = {
		#	limit = {
		#		scope:target = { government_allows = administrative }
		#	}
		#	scope:target = { save_temporary_scope_as = recipient }
		#	custom_tooltip = {
		#		text = has_admin_gov
		#		NOT = { scope:target = { government_allows = administrative } }
		#	}
		#}
	}

	ai_create_score = {
		base = -200 # Base reluctance value we must overcome to start an Independence Faction.

		############
		# BLOCKERS #
		common_create_faction_blockers = {
			FACTION_TARGET = scope:target
			FLAG = recent_nation_fracturing_faction_war
		}

		nation_fracturing_faction_blockers = {
			FACTION_TARGET = scope:target
		}

		#######################
		# Standard AI Weights #

		common_faction_modifiers = {
			FACTION_TARGET = scope:target
			OPINION_MULTIPLIER = -2
			MAX_OPINION = 200
			POWER = 0
			THRESHOLD = 80
		}

		nation_fracturing_faction_modifiers = {
			FACTION_TARGET = scope:target
		}
		#House Hostility from Armenian tradition
	    modifier = {
			desc = "FACTION_REASON_HOUSE_HOSTILITY"

			scope:target.house ?= {
				NOT = { this = root.house }
			}
			culture = { has_cultural_parameter = house_hostility_more_common }

			factor = 1.25
	    }
	}

	can_character_join = {
		common_can_character_join_trigger = {
			FACTION_TARGET = scope:faction.faction_target
		}
		
		# EK DISABLED: No Islam in TES
		#trigger_if = {
		#	limit = {
		#		faith.religion = religion:islam_religion
		#		liege = { has_title = title:d_sunni }
		#		title:d_sunni = {
		#			has_variable = dar_al_islam
		#		}
		#	}
		#	liege = {
		#		NOT = {
		#			has_title = title:d_sunni
		#		}
		#	}
		#}
		
		# Faction is not allowed against administrative rulers
		#trigger_if = {
		#	limit = {
		#		scope:faction.faction_target = { government_allows = administrative }
		#	}
		#	scope:faction.faction_target = { save_temporary_scope_as = recipient }
		#	custom_tooltip = {
		#		text = has_admin_gov
		#		NOT = { scope:faction.faction_target = { government_allows = administrative } }
		#	}
		#}
	}

	ai_join_score = {
		base = -150 # Base reluctance value we must overcome to join an Independence Faction.

		############
		# BLOCKERS #

		common_join_faction_blockers = {
			FACTION_TARGET = scope:faction.faction_target
		}

		nation_fracturing_faction_blockers = {
			FACTION_TARGET = scope:faction.faction_target
		}

		#######################
		# Standard AI Weights #

		common_faction_modifiers = {
			FACTION_TARGET = scope:faction.faction_target
			OPINION_MULTIPLIER = -2
			MAX_OPINION = 100
			POWER = scope:faction.faction_power
			THRESHOLD = scope:faction.faction_power_threshold
		}

		nation_fracturing_faction_modifiers = {
			FACTION_TARGET = scope:faction.faction_target
		}

		modifier = { # CE1 Divine Mandate perk
			desc = "FACTION_REASON_LEGITIMACY_LEGACY_4"
			liege.dynasty = { has_dynasty_perk = ce1_legitimacy_legacy_4 }
			add = -35
		}
		#House Hostility from Armenian tradition
	    modifier = {
			desc = "FACTION_REASON_HOUSE_HOSTILITY"

			scope:target.house ?= {
				NOT = { this = root.house }
			}
			culture = { has_cultural_parameter = house_hostility_more_common }

			factor = 1.25
	    }
		#EP3 admin legitimacy event
		modifier = {
			desc = "FACTION_REASON_LOW_LEGITIMACY_CHALLENGER"
			add = 25
			scope:faction = {
				any_faction_member = {
					has_character_flag = low_legitimacy_admin_factions_flag
				}
			}
		}
	}

	ai_demand_chance = {
		base = 0

		# 40% base chance at minimum power (80%), increasing linearly
		compare_modifier = {
			value = faction_power
			multiplier = 0.5
		}

		# Once the faction has a good chance to win (10% stronger than liege) demand chance increases much more rapidly.
		compare_modifier = {
			trigger = {	faction_power > 110 }
			value = faction_power
			multiplier = 1
		}

		modifier = {
			add = 100
			faction_target = {
				is_at_war = yes # Independence Factions are opportunistic bastards!
			}
		}
	}


	county_join_score =  {
		base = 0
		compare_modifier = {
			desc = "FACTION_REASON_COUNTY_OPINION"
			value = county_opinion
			multiplier = -1.0
		}
	}

	is_county_valid = {
		holder = scope:faction.faction_target
		OR = {
			NOT = { culture = scope:faction.faction_target.culture } 
			NOT = { faith = scope:faction.faction_target.faith } 
		}
	}

	on_creation = {
		save_scope_as = faction
		if = {
			limit = {
				any_player = {
					is_landless_adventurer = yes
					is_within_diplo_range = { CHARACTER = scope:faction.faction_target }
					NOT = { has_contact = scope:faction.faction_target }
					OR = {
						faith = scope:faction.faction_target.faith
						culture = scope:faction.faction_target.culture
					}
					any_character_task_contract = {
						has_task_contract_type = laamp_join_faction_contract
						count <= 3
					}
					can_create_task_contract = {
						type_name = laamp_join_faction_contract
						employer = scope:faction.faction_leader
					}
					save_temporary_scope_as = player_laamp
				}
			}
			scope:player_laamp = {
				create_task_contract = {
					task_contract_type = laamp_join_faction_contract
					task_contract_tier = scope:faction.faction_leader.task_contract_tier_value
					location = scope:faction.faction_leader.primary_title.title_province
					task_contract_employer = scope:faction.faction_leader
					target = scope:faction.faction_target
				}
			}
		}
	}

	county_allow_join = yes
	county_allow_create = no
}
