﻿dynamic_power_threshold_scripted_modifier = {
	#Lower the threshold depending on the state of other factions
	#Check if the factions types passed in parameter are:
	#Is at war: -20
	#Exists and is above its power theshold: -10
	#Exists and is below its power theshold: -5
	modifier = {
		add = {
			value = 0
			
			if = {
				limit = {
					faction_target = {
						any_targeting_faction = {
							faction_is_type = $FACTION_TYPE1$
							faction_is_at_war = yes 
						}
					}
				}
				add = -20
			}
			else_if = {
				limit = {
					faction_target = {
						any_targeting_faction = {
							faction_is_type = $FACTION_TYPE1$
							faction_is_at_war = no
							faction_power >= faction_power_threshold
						}
					}
				}
				add = -10
			}
			else_if = {
				limit = {
					faction_target = {
						any_targeting_faction = {
							faction_is_type = $FACTION_TYPE1$
							faction_is_at_war = no
						}
					}
				}
				add = -5
			}

			if = {
				limit = {
					faction_target = {
						any_targeting_faction = {
							faction_is_type = $FACTION_TYPE2$
							faction_is_at_war = yes 
						}
					}
				}
				add = -20
			}
			else_if = {
				limit = {
					faction_target = {
						any_targeting_faction = {
							faction_is_type = $FACTION_TYPE2$
							faction_is_at_war = no
							faction_power >= faction_power_threshold
						}
					}
				}
				add = -10
			}
			else_if = {
				limit = {
					faction_target = {
						any_targeting_faction = {
							faction_is_type = $FACTION_TYPE2$
							faction_is_at_war = no
						}
					}
				}
				add = -5
			}

			if = {
				limit = {
					faction_target = {
						any_targeting_faction = {
							faction_is_type = $FACTION_TYPE3$
							faction_is_at_war = yes 
						}
					}
				}
				add = -20
			}
			else_if = {
				limit = {
					faction_target = {
						any_targeting_faction = {
							faction_is_type = $FACTION_TYPE3$
							faction_is_at_war = no
							faction_power >= faction_power_threshold
						}
					}
				}
				add = -10
			}
			else_if = {
				limit = {
					faction_target = {
						any_targeting_faction = {
							faction_is_type = $FACTION_TYPE3$
							faction_is_at_war = no
						}
					}
				}
				add = -5
			}
			#security to never reach a power threshold equals to 0
			min = -70
		}
		desc = "FACTION_POWER_INTERNAL_ISSUES"
	}
	LTAF_loyalist_faction_power_threshold_modifier = yes
}

common_faction_modifiers = {
	modifier = {
		add = { subtract = LTAF_loyalty }
	}
	# +/- 40 join weight at -/+100 opinion
	opinion_modifier = {
		who = root
		opinion_target = $FACTION_TARGET$ 
		multiplier = $OPINION_MULTIPLIER$
		max = $MAX_OPINION$
	}

	# for each character.religion != factionTarget.religion add X join-score
	pluralism_fundamentalism_modifier = {
		ASTRAY_BASE_VALUE = 25
		HOSTILE_BASE_VALUE = 50
		EVIL_BASE_VALUE = 100
		BASE_FAITH = faith
		TARGET_FAITH = $FACTION_TARGET$.faith
	}

	# +25 join chance per sin, -25 join chance per virtue
	legalism_virtue_and_sin_modifier = {
		TARGET = $FACTION_TARGET$
		SCORE_PER_TRAIT = 25
	}

	# -100 join chance from intimidation, -1000 join chance from terrified. This modifier doesn't apply if a faction is above the power threshold
	intimidated_from_faction_modifier = {
		TARGET = $FACTION_TARGET$
		POWER =  $POWER$
		THRESHOLD = $THRESHOLD$
	}

	# Debt: +10 to +60 join chance depending on how far in debt the top liege is
	liege_debt_modifier = {
		TARGET = $FACTION_TARGET$
		SCORE_PER_DEBT_LEVEL = 10
	}

	# Difficulty Settings
	modifier = { # Easy
		add = -50
		has_game_rule = easy_difficulty
		$FACTION_TARGET$ = {
			is_ai = no
		}
	}
	modifier = { # Very Easy
		add = -150
		has_game_rule = very_easy_difficulty
		$FACTION_TARGET$ = {
			is_ai = no
		}
	}

	# Realm Stability Settings
	modifier = {
		add = 50
		has_game_rule = lesser_realm_stability
	}
	modifier = {
		add = -50
		has_game_rule = higher_realm_stability
	}
	modifier = {
		add = -100
		has_game_rule = extreme_realm_stability
	}

	#safe guard to avoid Factions being created when the game starts
	modifier = {
		add = -10000
		years_from_game_start < 1
		$FACTION_TARGET$.primary_title.tier = tier_duchy

		# The modifier should not be applied for the Iberian struggle
		NOT = {
			$FACTION_TARGET$ = {
				any_character_struggle = {
					involvement = involved
				}
			}
		}
	}

	modifier = {
		add = -10000
		years_from_game_start < 5
		$FACTION_TARGET$.primary_title.tier = tier_kingdom
	}

	modifier = {
		add = -10000
		years_from_game_start < 10
		$FACTION_TARGET$.primary_title.tier = tier_empire

		# The modifier should not be applied for the Iberian struggle
		NOT = {
			$FACTION_TARGET$ = {
				any_character_struggle = {
					involvement = involved
				}
			}
		}
	}
}