﻿
military_faction = {
	casus_belli = military_faction_war

	short_effect_desc = military_faction_short_effect_desc

	sort_order = 1
	
	can_character_create = {	
		OR = {
			has_government = military_command
			AND = {
				has_government = patrician_government
				domain_size > 0
			}
		}
		scope:target = { NOT = { has_government = military_command } }
		custom_description = {
			text = TOOLTIPmilitary_faction_BYZANTINE
			NAND = {
				scope:target = { has_culture_group = culture_group:byzantine_group }
				byzantine_elective_deformed_candidate_trigger = yes
			}
		}	
		
		is_adult = yes
		NOT = { is_allied_to = scope:target }
		scope:target = {
			NOT = { has_strong_hook = root }
		}

		OR = {
			is_ai = no
			NOR = {
				has_relation_lover = scope:target
				has_relation_friend = scope:target
			}
		}
		custom_description = {
			text = character_blocked_from_joining
			NOT = {
				has_character_flag = joining_faction_block
			}
		}

		scope:target.highest_held_title_tier > tier_county
		highest_held_title_tier > tier_barony

		####
		# BLOCKERS
		####
		# General Faction immunity
		custom_description = {
			text = character_is_immune_to_factions
			subject = scope:target
			NOT = { scope:target = { immune_to_factions_trigger = yes } }
		}

		custom_description = {
			text = character_has_faction_disabling_modifier
			character_has_faction_disabling_modifier_trigger = yes
		}
	}
	
	can_character_join = {
		OR = {
			has_government = military_command
			AND = {
				has_government = patrician_government
				domain_size > 0
			}
		}
		scope:faction.faction_target = { NOT = { has_government = military_command } }
		
		is_adult = yes
		NOT = { is_allied_to = scope:faction.faction_target }
		scope:faction.faction_target = {
			NOT = { has_strong_hook = root }
		}

		OR = {
			is_ai = no
			NOR = {
				has_relation_lover = scope:faction.faction_target
				has_relation_friend = scope:faction.faction_target
			}
		}

		scope:faction.faction_target.highest_held_title_tier > tier_county

		custom_description = {
			text = character_has_faction_disabling_modifier
			character_has_faction_disabling_modifier_trigger = yes
		}
	}

	
	# can_character_become_leader = {
	# }

	is_character_valid = {
		OR = {
			has_government = military_command
			AND = {
				has_government = patrician_government
				domain_size > 0
			}
		}	

		scope:faction.faction_target = liege
		highest_held_title_tier > tier_barony

		#Not blocked through events
		custom_description = {
			text = character_blocked_from_joining
			NOT = {
				has_character_flag = joining_faction_block
			}
		}
	}
	
	# can_character_become_leader = {
		# NOT = {
			# scope:faction = {
				# any_faction_member = {
					# OR = {
						# tier > scope:character.tier
						# military_strength_greater_than_ratio_trigger = { TARGET = scope:character RATIO = 1.5 }
					# }
				# }
			# }
		# }
	# }
	
	county_allow_join = yes
	county_allow_create = no
	
	can_county_join = {
	}

	is_county_valid = {
		scope:faction.faction_target = holder
	}

	county_join_score =  {
		base = -5
		
		modifier = {
			add = 20
			county_opinion < 1
			scope:faction = { #Encourage to join if faction is powerful and discontent
				faction_discontent > faction_power_threshold 
				faction_discontent >= 80
			}
		}
		
		modifier = { #Capital reluctance
			add = -5
			this = scope:faction.faction_target.capital_county
		}
		
		#Losing war
		modifier = {
			add = 10	
			scope:faction.faction_target = { 
				any_character_war = {
					OR = {
						AND = {
							scope:faction.faction_target = { is_attacker_in_war = prev }
							attacker_war_score <= -25
						}	
						AND = {
							scope:faction.faction_target = { is_defender_in_war = prev }
							defender_war_score <= -25
						}
					}	
				}
			}
		}
		#Long offensive war
		modifier = {
			add = 10	
			scope:faction.faction_target = { 
				is_at_war = yes
				is_at_war_as_attacker = yes
				days_of_continuous_war = 548
			}
		}
		
		modifier = {
			add = -5
			county_opinion >= 20
		}
		modifier = {
			add = -5
			county_opinion >= 10
		}
		modifier = {
			add = 5
			county_opinion < 0
		}
		modifier = {
			add = 5
			county_opinion < -5
		}
		modifier = {
			add = 5
			county_opinion < -10
		}
		modifier = {
			add = 5
			county_opinion < -15
		}
		modifier = {
			add = 5
			scope:faction = { #Unless faction is powerful and discontent, prefer other factions
				faction_discontent > faction_power_threshold 
				faction_discontent >= 80
			}	
			county_opinion < -20
		}
		modifier = {
			add = 5
			scope:faction = { #Unless faction is powerful and discontent, prefer other factions
				faction_discontent > faction_power_threshold 
				faction_discontent >= 80
			}	
			county_opinion < -25
		}
		modifier = {
			add = 5
			scope:faction = { #Unless faction is powerful and discontent, prefer other factions
				faction_discontent > faction_power_threshold 
				faction_discontent >= 80
			}	
			county_opinion < -30
		}

		# Incapable to rule
		modifier = {
			add = 5
			scope:faction = { #Unless faction is powerful and discontent, prefer other factions
				faction_discontent > faction_power_threshold 
				faction_discontent >= 80
			}	
			exists = scope:faction.faction_target
			scope:faction.faction_target = { 
				OR = {
					is_adult = no 
					has_trait = incapable
					has_trait = infirm
				}	
			}
		}
		#Tyranny
		modifier = {
			add = 10
			scope:faction = { #Unless faction is powerful and discontent, prefer other factions
				faction_discontent > faction_power_threshold 
				faction_discontent >= 80
			}	
			scope:faction.faction_target = { tyranny >= low_tyranny }
		}
		
		
		# Dread
		modifier = {
			add = {
				add = scope:faction.faction_target.dread
				multiply = -0.24
			}
		}
	}

	discontent_progress = {
		base = 0

		# Discontent always slowly decays in military faction
		modifier = {
			add = -1
			desc = "FACTION_DISCONTENT_MILITARY"
			faction_target = { NOT = { has_character_flag = military_faction_coup_attempt } }
		}
		modifier = {
			add = -10
			desc = "FACTION_DISCONTENT_MILITARY_RECENT_COUP_ATTEMPT"
			faction_target = { has_character_flag = military_faction_coup_attempt }
		}
		
		
		modifier = { #Losing war
			add = 4
			desc = "FACTION_DISCONTENT_MILITARY_LOSING_WAR"
			faction_target = {		
				save_temporary_scope_as = the_faction_target
				any_character_war = {
					OR = {
						AND = {
							scope:the_faction_target = { is_attacker_in_war = prev }
							attacker_war_score <= -25
						}	
						AND = {
							scope:the_faction_target = { is_defender_in_war = prev }
							defender_war_score <= -25
						}
					}	
				}
			}				
		}
		
		modifier = { #Long offensive war
			add = 3
			desc = "FACTION_DISCONTENT_MILITARY_OFFENSIVE_WAR"
			faction_target = {	
				is_at_war = yes
				is_at_war_as_attacker = yes
				days_of_continuous_war = 548
			}				
		}
		
		modifier = { #Has been ignoring military candidate
			add = {
				value = faction_target.var:military_candidate_ignored_count
				multiply = 0.5
				round = yes
				max = 4
			}
			desc = "FACTION_DISCONTENT_MILITARY_IGNORING_CANDIDATE"
			faction_target = {	
				exists = var:military_candidate_ignored_count
				var:military_candidate_ignored_count >= 3
			}				
		}
		
		modifier = { #Liege's domain is large
			add = 1
			desc = "FACTION_DISCONTENT_LIEGE_DOMAIN_LARGE"
			faction_target = {	
				domain_size >= 4
				domain_limit_percentage >= 0.75
			}				
		}
		
		modifier = { #Byzantine elective succession is traditional
			add = 2
			desc = "FACTION_DISCONTENT_MILITARY_NO_ELECTIVE"
			faction_target = {	
				culture_group = culture_group:byzantine_group
				primary_title = {
					NOR = {
						has_title_law = imperial_elective_succession_law
						has_title_law = feudal_elective_succession_law
						has_title_law = princely_elective_succession_law
						has_title_law = saxon_elective_succession_law
						has_title_law = scandinavian_elective_succession_law
					}
				}
			}				
		}
		
		# modifier = { #Liege is new
			# add = 2
			# desc = "FACTION_DISCONTENT_NEW_RULER"
			#no trigger for this???				
		# }
		modifier = { #Liege is a child
			add = 1
			desc = "FACTION_DISCONTENT_LIEGE_CHILD"
			faction_target = { is_adult = no }				
		}
		modifier = { #Liege is unfit to rule
			add = 1
			desc = "FACTION_DISCONTENT_LIEGE_UNFIT"
			faction_target = { 
				OR = {
					has_trait = incapable
					has_trait = infirm
				}
			}				
		}
		modifier = { #Liege is a tyrant
			add = {
				value = 1
				if = {
					limit = { faction_target = { tyranny >= medium_tyranny } }
					add = 1
				}
				if = {
					limit = { faction_target = { tyranny >= high_tyranny } }
					add = 2
				}
			}
			desc = "FACTION_DISCONTENT_LIEGE_TYRANT"
			faction_target = { 
				tyranny >= low_tyranny
			}	
		}
		modifier = { #Faction leader opinion
			add = {
				value = 1
				if = {
					limit = { 
						faction_leader = {
							opinion = { target = scope:the_faction_target value < -50 }
						}
					}
					add = 1
				}
			}
			desc = "FACTION_DISCONTENT_LEADER_OPINION"
			faction_target = { 
				save_temporary_scope_as = the_faction_target
			}	
			faction_leader = {
				save_temporary_scope_as = the_faction_leader
				opinion = { target = scope:the_faction_target value < -20 }
			}		
		}
		modifier = { #Liege is bankrupt
			add = 1
			desc = "FACTION_DISCONTENT_LIEGE_BANKRUPT"
			faction_target = { 
				gold < 0
			}	
		}
	}

	power_threshold = {
		base = 80

		modifier = {
			add = 20
			faction_target = {
				has_perk = hard_rule_perk
			}
			desc = "FACTION_POWER_HARD_RULE"
		}
	}
	
	demand = {
		if = { #prevent player from sending demands without enough discontent
			limit = { 
				faction_discontent > faction_power_threshold 
				faction_discontent >= 80
			}
			save_scope_as = faction
			
			if = { #First demand elective monarchy before trying a coup
				limit = {
					faction_target = {
						NOT = { has_character_flag = refused_military_faction_elective_monarchy }
						primary_title = {
							NOR = {
								has_title_law = imperial_elective_succession_law
								has_title_law = feudal_elective_succession_law
								has_title_law = princely_elective_succession_law
								has_title_law = saxon_elective_succession_law
								has_title_law = scandinavian_elective_succession_law
							}
						}
					}
				}
				faction_target = {
					save_scope_as = faction_target
					primary_title = {
						save_scope_as = faction_targeted_title
					}
				}

				faction_leader = {
					save_scope_as = faction_leader
				}

				# 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 = imperial.0029
				}

				# Send the actual demand in 5 days
				faction_target = {
					trigger_event = {
						id = imperial.0025
						days = 5
					}
				}
			}
			else = { #Coup
				faction_target = {
					save_scope_as = faction_target
					add_character_flag = { flag = military_faction_coup_attempt years = 5 }
					primary_title = {
						save_scope_as = faction_targeted_title
					}
				}

				faction_leader = {
					save_scope_as = faction_leader					
				}
				random_faction_member = {
					limit = { 
						is_adult = yes
						is_physically_able_adult = yes
						NOT = { has_trait = infirm }
						NOT = { has_trait = blind }
						NOT = { has_trait = eunuch }
					}
					weight = {
						base = 1
						modifier = {
							add = {
								value = tier
								multiply = 1000
							}	
						}
						modifier = {
							add = max_military_strength
						}
						modifier = {
							add = prestige
						}
					}
					save_scope_as = faction_claimant
				}

				# 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 = imperial.0024
				}

				# Send the actual demand in 5 days
				faction_target = {
					trigger_event = {
						id = imperial.0020
						days = 5
					}
				}
			}	
		}	
		else_if = { #Inform player demand is not yet valid
			limit = { faction_leader = { is_ai = no } }
			faction_leader = {
				send_interface_toast = {
					title = military_faction_DEMAND_NOT_VALID_TITLE
					custom_tooltip = military_faction_DEMAND_NOT_VALID_DESC
					right_icon = this
				}
			}
		}
	}
	
	special_character_title = "FACTIONS_WINDOW_MILITARY_CANIDATE"
	update_effect = {	
		save_scope_as = faction_update
		
		##Imperial vassal government check is hooked in here as code does not check it often enough
		faction_target = { 
			trigger_event = imperial.0004 		
		}
		##
		
		#check existing preferred commander candidate
		if = {
			limit = { 
				special_character = { 
					OR = {
						is_ruler = no
						AND = {
							has_government = patrician_government
							domain_size = 0	
						}	
						military_faction_duke_promotion_candidate_trigger = { FACTION_TARGET = scope:faction_update.faction_target }
					}	
					eligible_for_military_appointment_trigger = { APPOINTEE = scope:faction_update.faction_target }
					any_liege_or_above = { this = scope:faction_update.faction_target }
					is_imprisoned = no
				}
			}	
			#Prompt AI emperor to grant a title to the canididate
			if = {
				limit = {
					faction_target = { 
						is_ai = yes
						NOT = { has_character_flag = military_title_grant_check }
						is_character_interaction_valid = {
							recipient = scope:faction_update.special_character
							interaction = grant_titles_interaction
						}
						OR = {
							AND = {
								ai_greed < 50
								opinion = { target = scope:faction_update.special_character value > -20 }
								reverse_opinion = { target = scope:faction_update.special_character value > -20 }
							}	
							scope:faction_update = { #faction is very discontent
								faction_discontent >= 80
								faction_power >= 75
							}
						}	
						trigger_if = {
							limit = { #candidate is for duchy promotion
								scope:faction_update.special_character = {
									is_ruler = yes
									military_faction_duke_promotion_candidate_trigger = { FACTION_TARGET = scope:faction_update.faction_target }
								}	
							}
							is_at_war = no
							short_term_gold >= 250 #has enough gold to create title
							OR = { #Motives
								opinion = { target = scope:faction_update.special_character value > 40 } #likes candidate
								dynasty = scope:faction_update.special_character.dynasty #related
								is_close_family_of = scope:faction_update.special_character
								scope:faction_update = { #faction is very discontent
									faction_discontent >= 80
								}
							}
						}	
						trigger_else = { #candidate is for county
							domain_size > 4
							domain_limit_percentage > 0.75					
							OR = { #Motives
								domain_limit_available < 0 #over domain limit
								opinion = { target = scope:faction_update.special_character value > 40 } #likes candidate
								dynasty = scope:faction_update.special_character.dynasty #related
								is_close_family_of = scope:faction_update.special_character
								scope:faction_update = { #faction is discontent
									faction_discontent >= 60
								}
							}	
						}	
					}
				}
				faction_target = { 
					add_character_flag = { flag = military_title_grant_check days = 100 }
					trigger_event = imperial.0030 
				}
			}				
		}
		else = { #Find new candidate
			remove_special_character = yes
			remove_special_character = special_character
			if = {
				limit = { 
					NOT = { faction_is_at_war = yes } 
				}
				
				#random = { #try and find another
					#chance = 50
					faction_target = {
						every_courtier = {
							limit = {
								is_ruler = no
								eligible_for_military_appointment_trigger = { APPOINTEE = scope:faction_update.faction_target }
							}
							add_to_temporary_list = eligible_candidates
						}
						every_vassal = {
							limit = {
								OR = {
									has_government = military_command
									has_government = patrician_government
								}	
							}
							if = { #eligible for promotion to duke
								limit = {
									eligible_for_military_appointment_trigger = { APPOINTEE = scope:faction_update.faction_target }
									military_faction_duke_promotion_candidate_trigger = { FACTION_TARGET = scope:faction_update.faction_target }
								}
								add_to_temporary_list = eligible_candidates
							}
							every_courtier = {
								limit = {
									is_ruler = no
									eligible_for_military_appointment_trigger = { APPOINTEE = scope:faction_update.faction_target }
								}
								add_to_temporary_list = eligible_candidates
							}
						}
						every_vassal_or_below = {
							limit = { 
								has_government = patrician_government 
								domain_size = 0	
								eligible_for_military_appointment_trigger = { APPOINTEE = scope:faction_update.faction_target }
							}
							add_to_temporary_list = eligible_candidates
						}
						
						#find pool characters
						every_directly_owned_province = {
							save_temporary_scope_as = pool_province

							every_pool_character = {
								province = scope:pool_province
								limit = {
									is_ruler = no
									eligible_for_military_appointment_trigger = { APPOINTEE = scope:faction_update.faction_target }
								}
								add_to_temporary_list = eligible_candidates
							}
						}

						random_in_list = {
							list = eligible_candidates
							limit = { #further filters
								NOR = { #check gender
									AND = {
										scope:faction_update.faction_target = {
											OR = {
												has_realm_law = male_only_law
												has_realm_law = male_preference_law
											}
										}	
										is_female = yes
									}
									AND = {
										scope:faction_update.faction_target = {
											OR = {
												has_realm_law = female_only_law
												has_realm_law = female_preference_law
											}
										}		
										is_female = no
									}
								}
								OR = {
									martial >= 20
									AND = {
										prestige >= 100
										martial >= 16
									}	
									prestige >= 250
									highest_held_title_tier >= tier_county
									AND = {
										is_ruler = yes
										has_government = patrician_government
									}	
									liege = {
										highest_held_title_tier >= tier_duchy
										dynasty = prev.dynasty
									}
								}
								NAND = {
									is_lowborn = yes
									prestige < 500
									martial < 16
								}
							}
							alternative_limit = {
								NOR = { #check gender
									AND = {
										scope:faction_update.faction_target = {
											OR = {
												has_realm_law = male_only_law
												has_realm_law = male_preference_law
											}
										}	
										is_female = yes
									}
									AND = {
										scope:faction_update.faction_target = {
											OR = {
												has_realm_law = female_only_law
												has_realm_law = female_preference_law
											}
										}		
										is_female = no
									}
								}
							}
							weight = {
								base = 1
								modifier = { #Avoid liege's family
									dynasty = scope:faction_update.faction_target.dynasty
									add = -100
								}
								modifier = { #Part of a noble family
									NOT = { dynasty = scope:faction_update.faction_target.dynasty }
									has_dynasty = yes
									OR = {
										dynasty = liege.dynasty
										AND = {
											is_ruler = yes
											has_government = patrician_government
										}	
										dynasty = { dynasty_prestige_level >= 1 }
									}	
									add = 80
								}
								modifier = { #Lowborn
									is_lowborn = yes
									add = -25
								}
								modifier = { #Lowborn nobody
									is_lowborn = yes
									prestige < 100
									martial < 14
									add = -2000
								}
								modifier = { #Culture
									culture = scope:faction_update.faction_target.culture
									add = 40
								}
								modifier = { #Religion
									religion = scope:faction_update.faction_target.religion
									add = 40
								}
								modifier = { #Military command promotion
									has_government = military_command
									highest_held_title_tier >= tier_county
									add = 50
								}
								modifier = { #Patrician
									is_ruler = yes
									has_government = patrician_government
									add = 50
								}
								modifier = { #Liege tier
									is_ruler = no
									add = {
										if = {
											limit = { liege.tier = tier_duchy }
											value = 15
										}
										else_if = {
											limit = { liege.tier = tier_kingdom }
											value = 20
										}
										else_if = {
											limit = { liege.tier = tier_empire }
											value = 25
										}
										else = {
											value = 0
										}
									}	
								}
								#Martial
								modifier = { 
									martial >= 6
									add = 60							
								}
								modifier = { 
									martial >= 8
									add = 20						
								}
								modifier = { 
									martial >= 10
									add = 20						
								}
								modifier = { 
									martial >= 12
									add = 20							
								}
								modifier = { 
									martial >= 14
									add = 20						
								}
								modifier = { 
									martial >= 16
									add = 20						
								}
								modifier = { 
									martial >= 18
									add = 20							
								}
								modifier = { 
									martial >= 20
									add = 20							
								}
								#Diplomacy
								modifier = { 
									diplomacy >= 6
									add = 30								
								}
								modifier = { 
									diplomacy >= 8
									add = 10							
								}
								modifier = { 
									diplomacy >= 10
									add = 10							
								}
								modifier = { 
									diplomacy >= 12
									add = 10							
								}
								modifier = { 
									diplomacy >= 14
									add = 10							
								}
								modifier = { 
									diplomacy >= 16
									add = 10							
								}
								modifier = { 
									diplomacy >= 18
									add = 10							
								}
								modifier = { 
									diplomacy >= 20
									add = 10							
								}
								
								#Age
								modifier = { 
									age >= 30
									add = 25							
								}
								modifier = { 
									age >= 40
									add = 25							
								}
								modifier = { 
									age >= 50
									age < 60
									add = 25							
								}
								
								#Prestige
								modifier = { 
									prestige >= 100
									add = 40								
								}
								modifier = { 
									prestige >= 200
									add = 40								
								}
								modifier = { 
									prestige >= 300
									add = 40								
								}
								modifier = { 
									prestige >= 400
									add = 40								
								}
								modifier = { 
									prestige >= 500
									add = 40								
								}
								modifier = { 
									prestige >= 750
									add = 40							
								}
								modifier = { 
									prestige >= 1000
									add = 40							
								}
								modifier = { 
									prestige >= 1500
									add = 40							
								}
								modifier = { 
									prestige >= 2000
									add = 40							
								}
								# #AI personality
								# modifier = { 
									# add = { 
										# value = ai_greed
										# multiply = 0.5
										# min = -50
										# max = 50
									# }
								# }
								# modifier = { 
									# add = { 
										# value = ai_energy
										# multiply = 0.5
										# min = -50
										# max = 50
									# }
								# }
							} 						
							save_scope_as = new_military_candidate	
							send_interface_toast = {
								title = military_faction_BECAME_CANDIDATE_TITLE
								left_icon = scope:new_military_candidate 
								
								custom_tooltip = military_faction_BECAME_CANDIDATE_DESC
							}	
							if = {
								limit = { 
									NOT = { any_liege_or_above = { this = scope:faction_update.faction_target } } 
									is_ruler = no
								}
								scope:faction_update.faction_target = {
									recruit_courtier = prev
								}
							}
						}					
						scope:faction_update = { 
							set_special_character = scope:new_military_candidate 
							every_faction_member = {
								send_interface_toast = {
									title = military_faction_NEW_CANDIDATE_TITLE
									left_icon = scope:new_military_candidate 
									
									custom_tooltip = military_faction_NEW_CANDIDATE_DESC
								}
							}
						}
						send_interface_toast = {
							title = military_faction_NEW_CANDIDATE_TITLE
							left_icon = scope:new_military_candidate 
							
							custom_tooltip = military_faction_NEW_CANDIDATE_DESC
						}					
					}
				#}
			}	
		}
	}

	ai_join_score = {
		base = 20

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

		# Do not join a new faction if I am already at war.
		modifier = {
			add = -100
			AND = {
				is_at_war = yes
				trigger_if = {
					limit = {
						exists = joined_faction
					}
					NOT = { joined_faction = scope:faction }
				}
			}
		}

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

		modifier = {
			add = -100 # AI won't join at +50 Opinion
			opinion = { target = scope:faction.faction_target value >= 50 }
		}
		
		# +50 join weight at -100 opinion
		opinion_modifier = {
			who = root
			opinion_target = scope:faction.faction_target
			multiplier = -0.5
			min = 0
			max = 50
		}

		# Dread
		intimidated_from_faction_modifier = {
			TARGET = scope:faction.faction_target
		}

		# Difficulty Settings
		modifier = { # Easy
			add = -10
			has_game_rule = easy_difficulty
			scope:faction.faction_target = {
				is_ai = no
			}
		}
		modifier = { # Very Easy
			add = -20
			has_game_rule = very_easy_difficulty
			scope:faction.faction_target = {
				is_ai = no
			}
		}

		##########################
		# Faction 'Stacking' Factors, attempts to cluster AI rulers into several powerful factions instead of many weak ones.
		modifier = {
			scope:faction.faction_power >= scope:faction.faction_power_halfway_threshold
			factor = faction_weight_factor_power_halfway_threshold
		}
		modifier = {
			scope:faction.faction_power >= scope:faction.faction_power_pushing_threshold
			factor = faction_weight_factor_power_pushing_threshold
		}
		modifier = {
			scope:faction.faction_power >= scope:faction.faction_power_threshold
			factor = faction_weight_factor_power_exceeds_threshold
		}
	}

	ai_create_score = {
		base = 15

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


		# Do not join a new faction if I am already at war.
		modifier = {
			add = -100
			is_at_war = yes
		}

		#######################
		# Standard AI Weights #
		#
		
		modifier = {
			add = -100 # AI won't join at +50 Opinion
			opinion = { target = scope:target value >= 50 }
		}
		
		# +50 join weight at -100 opinion
		opinion_modifier = {
			who = root
			opinion_target = scope:target
			multiplier = -0.5
			min = 0
			max = 50
		}

		# Dread
		intimidated_from_faction_modifier = {
			TARGET = scope:target
		}
		
		# Difficulty Settings
		modifier = { # Easy
			add = -25
			has_game_rule = easy_difficulty
			scope:target = {
				is_ai = no
			}
		}
		modifier = { # Very Easy
			add = -75
			has_game_rule = very_easy_difficulty
			scope:target = {
				is_ai = no
			}
		}
	}

	ai_demand_chance = {
		base = 0

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

		# 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 = 0.75
		}

		# Less likely if already in a war with an external ruler - the Liberty Faction is pretty sporting
		modifier = {
			add = -75
			faction_target = {
				any_war_enemy = {
					NOT = {
						target_is_liege_or_above = root.faction_target
					}
				}
			}
		}
	}
}
