﻿
provoked_independence_faction = {
	casus_belli = provoked_independence_faction_war
	
	short_effect_desc = independence_faction_short_effect_desc

	sort_order = 2

	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
		}
	}	
	
	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.0005
		}

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

		# Tell to the schemer demand is sent
		scope:faction.faction_target = {
			save_scope_as = target
			primary_title = {
				var:provoke_vassals_schemer = {
					trigger_event = sas_faction_demand.1015
				}
			}
		}

		scope:faction = {
			set_variable = {
				name = faction_sent_demand
				value = yes
			}
		}
	}

	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!
			}
		}

		modifier = {
			add = -1000
			faction_target = {
				involved_activity ?= {
					has_activity_type = activity_coronation
					activity_host = prev
				}
			}
		}
	}

	ai_create_score = {
		base = -150
		modifier = {
			has_character_modifier = provoked_independence_character_modifier
			add = 1000
		}
		modifier = {
			NOT = { has_character_modifier = provoked_independence_character_modifier }
			add = -10000
		}
		# Keep vanilla blockers; these would also prevent the scheme in the first place if sufficiently major
		common_create_faction_blockers = {
			FACTION_TARGET = scope:target
			FLAG = recent_independence_faction_war
		}
		independence_faction_create_blockers  = {
			FACTION_TARGET = scope:target
		}
	}

	ai_join_score = {
		base = -150

		modifier = {
			desc = "FACTION_REASON_POISONED_OPINION_MODIFIER"
			AND = {
				has_opinion_modifier = {
					target = scope:faction.faction_target
					modifier = scheme_poison_opinion_opinion
				}
				"opinion(scope:faction.faction_target)" < 0
			}
			add = 1000
		}

		modifier = {
			desc = "FACTION_REASON_PROVOKED_INDEPENDENCE_MODIFIER"
			has_character_modifier = provoked_independence_character_modifier
			add = 1000
		}
		############
		# BLOCKERS #

		common_join_faction_blockers = {
			FACTION_TARGET = scope:faction.faction_target
		}

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

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

		independence_faction_modifiers = {
			FACTION_TARGET = scope:faction.faction_target
		}

		# Small multiplier for my neighbors already in the Faction
		modifier = {
			desc = "FACTION_REASON_NEIGHBORS"

			scope:faction = {
				any_faction_member = { #suppress the description printout if factor is 1
					any_neighboring_realm_same_rank_owner = {
						this = root
					}
				}
			}

			factor = {
				value = 1
				scope:faction = {
					every_faction_member = {
						if = {
							limit = {
								any_neighboring_realm_same_rank_owner = {
									this = root
								}
							}
							add = 0.25
						}		
					}
				}
			}
		}

		modifier = {
			desc = "FACTION_REASON_POWERFUL_VASSAL"
			factor = 2
			is_powerful_vassal_of = scope:faction.faction_target
		}

		struggle_faction_modifiers = yes

		#House Hostility from Armenian tradition
		modifier = {
			desc = "FACTION_REASON_HOUSE_HOSTILITY"

			scope:faction.faction_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
				}
			}
		}
	}

	is_character_valid = {
		common_character_validity_trigger = {
			FACTION_TARGET = scope:faction.faction_target
		}
		has_valid_faction_members_trigger = yes
		trigger_if = {
			limit = {
				AND = {
					has_opinion_modifier = {
						target = scope:faction.faction_target
						modifier = scheme_poison_opinion_opinion
					}
					"opinion(scope:faction.faction_target)" < 0
				}
			}
		}
		trigger_if = {
			limit = {
				has_character_modifier = provoked_independence_character_modifier
			}
		}
	}

	can_character_create_ui = {
		common_can_character_create_trigger = {
			FACTION_TARGET = scope:target
		}
		liege = {
			is_independent_ruler = yes
		}
		has_character_modifier = provoked_independence_character_modifier
	}

	can_character_create = {
		common_can_character_create_trigger = {
			FACTION_TARGET = scope:target
		}
		liege = {
			is_independent_ruler = yes
		}
		has_character_modifier = provoked_independence_character_modifier
	}

	can_character_join = {
		trigger_if = {
			limit = {
				AND = {
					has_opinion_modifier = {
						target = scope:faction.faction_target
						modifier = scheme_poison_opinion_opinion
					}
					"opinion(scope:faction.faction_target)" < 0
				}
			}
		}
		common_can_character_join_trigger = {
		FACTION_TARGET = scope:faction.faction_target
		}
		
		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
				}
			}
		}
		
		trigger_if = {
			limit = {
				government_allows = administrative
				has_game_rule = admin_independence_disabled
			}
			custom_tooltip = {
				text = admin_high_tier_vassal_join_faction
				highest_held_title_tier >= tier_kingdom
			}
		}
		#PROPPA CONFEDERATION VASSALS
		trigger_if = {
			limit = {
				liege = {
					is_confederation_member = yes
				}
			}
			#Confederations block most faction types
			custom_tooltip = {
				text = confederaton_blocks_faction
				NOR = {
					government_has_flag = government_is_nomadic
					government_has_flag = government_is_tribal
				}
			}
		}
	}

	on_creation = {
		save_scope_as = faction
		set_variable = {
			name = faction_sent_demand
			value = no
		}
		if = {
			limit = {
				any_player = {
					is_landless_adventurer = yes
					is_within_diplo_range = { CHARACTER = scope:faction.faction_target }
					OR = {
						has_contact = scope:faction.faction_leader
						NOT = { has_contact = scope:faction.faction_target }
					}
					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
				}
			}
		}
	}

	character_leaves = {
		scope:faction_member = {
			if = {
				limit = { has_character_modifier = provoked_independence_character_modifier }
			}
			remove_character_modifier = provoked_independence_character_modifier
		}
	}

	on_destroy = {
		save_scope_as = faction
		every_faction_member = {
			if = {
				limit = { has_character_modifier = provoked_independence_character_modifier }
			}
			remove_character_modifier = provoked_independence_character_modifier
		}

		if = {
			limit = { var:faction_sent_demand = no }
			# Tell to the schemer the efforts failed
			scope:faction.faction_target = {
				save_scope_as = target
				primary_title = {
					var:provoke_vassals_schemer = {
						trigger_event = sas_faction_demand.1011
					}
				}
			}
		}
		remove_variable = faction_sent_demand
	}

	county_allow_join = no
	county_allow_create = no
}
