# Dynamic Research: RP modifier logic (war, peace, laws, alliances)
#
# High-level structure:
# - dr_apply_rp_modifier_logic: main entry point, resets the aggregate modifier
#   and calls the three helper effects below.
# - dr_apply_war_rp_logic: builds a war/peace modifier based on wars, war
#   length, stability and ruling party support and stores it in
#   total_rp_modifier.
# - dr_apply_law_rp_logic: adds law-based RP modifiers (trade/economy/
#   conscription) into total_rp_modifier.
# - dr_apply_alliance_rp_logic: computes the alliance RP bonus, respecting
#   the game rule cap, and adds it to total_rp_modifier.

dr_apply_rp_modifier_logic = {
	# Reset aggregated modifier before recomputing
	set_variable = { total_rp_modifier = 0 }

	# Split into smaller, modular effects
	dr_apply_war_rp_logic = yes
	dr_apply_law_rp_logic = yes
	dr_apply_alliance_rp_logic = yes
}

# Calculate war penalty factors (war support, stability, ruling party)
# Sets: war_penalty_factor_ws, war_penalty_factor_stab, war_penalty_factor_party
dr_calculate_war_penalty_factors = {
	set_variable = { war_penalty_factor_ws = 0 }
	set_variable = { war_penalty_factor_stab = 0 }
	set_variable = { war_penalty_factor_party = 0 }

	# War support factor (0.0-0.4), 5 grobe Stufen statt 1%-Schritte
	set_variable = { war_penalty_factor_ws = 0.40 }
	if = { limit = { has_war_support > 0.20 } set_variable = { war_penalty_factor_ws = 0.30 } }
	if = { limit = { has_war_support > 0.40 } set_variable = { war_penalty_factor_ws = 0.20 } }
	if = { limit = { has_war_support > 0.60 } set_variable = { war_penalty_factor_ws = 0.10 } }
	if = { limit = { has_war_support > 0.80 } set_variable = { war_penalty_factor_ws = 0.00 } }

	# Stability factor (0.0-0.3), 5 grobe Stufen statt 1%-Schritte
	set_variable = { war_penalty_factor_stab = 0.30 }
	if = { limit = { has_stability > 0.20 } set_variable = { war_penalty_factor_stab = 0.24 } }
	if = { limit = { has_stability > 0.40 } set_variable = { war_penalty_factor_stab = 0.18 } }
	if = { limit = { has_stability > 0.60 } set_variable = { war_penalty_factor_stab = 0.12 } }
	if = { limit = { has_stability > 0.80 } set_variable = { war_penalty_factor_stab = 0.00 } }

	# Ruling party factor (0.0-0.4), based on ruling ideology popularity.
	set_variable = { war_penalty_factor_party = 0.4 }

	# Calculate government support factor using helper effects
	dr_set_government_popularity = yes
	dr_get_government_support_factor = yes
	set_variable = { war_penalty_factor_party = government_support_factor }
}

# Helper effect: Set temp_government_popularity based on current government type
# Sets: temp_government_popularity (democratic, fascism, communism, or neutrality)
dr_set_government_popularity = {
	if = {
		limit = { has_government = democratic }
		set_variable = { temp_government_popularity = democratic }
	}
	if = {
		limit = { has_government = fascism }
		set_variable = { temp_government_popularity = fascism }
	}
	if = {
		limit = { has_government = communism }
		set_variable = { temp_government_popularity = communism }
	}
	if = {
		limit = { has_government = neutrality }
		set_variable = { temp_government_popularity = neutrality }
	}
}

# Helper effect: Calculate government support factor based on popularity
# Expects: temp_government_popularity (variable containing popularity value, e.g., democratic, fascism, communism, neutrality)
# Sets: government_support_factor (0.0, 0.1, 0.2, 0.3, or 0.4) based on config thresholds
# Uses config variables: government_support_threshold_very_low, government_support_threshold_low, government_support_threshold_medium, government_support_threshold_high, government_support_threshold_very_high
dr_get_government_support_factor = {
	set_variable = { government_support_factor = 0.4 }
	
	# Check against thresholds (from highest to lowest for better performance)
	# Very high support (> very_high): 0.0 (best)
	if = {
		limit = {
			check_variable = {
				var = temp_government_popularity
				value = government_support_threshold_very_high
				compare = greater_than
			}
		}
		set_variable = { government_support_factor = 0.0 }
	}
	# High support (high - very_high): 0.1
	if = {
		limit = {
			check_variable = {
				var = temp_government_popularity
				value = government_support_threshold_very_high
				compare = less_than_or_equals
			}
			check_variable = {
				var = temp_government_popularity
				value = government_support_threshold_high
				compare = greater_than
			}
		}
		set_variable = { government_support_factor = 0.1 }
	}
	# Medium support (medium - high): 0.1 (unchanged for compatibility)
	if = {
		limit = {
			check_variable = {
				var = temp_government_popularity
				value = government_support_threshold_high
				compare = less_than_or_equals
			}
			check_variable = {
				var = temp_government_popularity
				value = government_support_threshold_medium
				compare = greater_than
			}
		}
		set_variable = { government_support_factor = 0.1 }
	}
	# Low support (low - medium): 0.2
	if = {
		limit = {
			check_variable = {
				var = temp_government_popularity
				value = government_support_threshold_medium
				compare = less_than_or_equals
			}
			check_variable = {
				var = temp_government_popularity
				value = government_support_threshold_low
				compare = greater_than
			}
		}
		set_variable = { government_support_factor = 0.2 }
	}
	# Very low support (very_low - low): 0.3
	if = {
		limit = {
			check_variable = {
				var = temp_government_popularity
				value = government_support_threshold_low
				compare = less_than_or_equals
			}
			check_variable = {
				var = temp_government_popularity
				value = government_support_threshold_very_low
				compare = greater_than
			}
		}
		set_variable = { government_support_factor = 0.3 }
	}
	# Below very_low threshold remains 0.4 (worst, default)
}

# Helper effect: Map government_support_factor to peace bonus
# Expects: government_support_factor (0.0, 0.1, 0.2, 0.3, or 0.4)
# Modifies: peace_rp_bonus (adds 0.03 for 0.0, 0.02 for 0.1, 0.0 for 0.2+)
dr_map_government_support_to_peace_bonus = {
	if = {
		limit = { check_variable = { var = government_support_factor value = 0.0 compare = equals } }
		add_to_variable = { peace_rp_bonus = 0.03 }
	}
	if = {
		limit = { check_variable = { var = government_support_factor value = 0.1 compare = equals } }
		add_to_variable = { peace_rp_bonus = 0.02 }
	}
}

# Calculate war phase factor based on war duration and type
# Sets: war_phase_factor, war_defense_factor
dr_calculate_war_phase_factor = {
	set_variable = { war_phase_factor = 0.0 }
	set_variable = { war_defense_factor = 1.0 }

	# Defensive vs. offensive war
	if = {
		limit = {
			has_defensive_war = yes
			has_offensive_war = no
		}
		set_variable = { war_defense_factor = 0.5 }
	}

	# Transition phase: offensive war ~3 months, defensive war ~6 months

	# Offensive war (at least one offensive war)
	if = {
		limit = { has_offensive_war = yes }

		if = {
			limit = {
				check_variable = {
					var = dr_days_in_war
					value = war_phase_offensive_threshold_1
					compare = greater_than_or_equals
				}
			}
			set_variable = { war_phase_factor = 0.33 }
		}
		if = {
			limit = {
				check_variable = {
					var = dr_days_in_war
					value = war_phase_offensive_threshold_2
					compare = greater_than_or_equals
				}
			}
			set_variable = { war_phase_factor = 0.66 }
		}
		if = {
			limit = {
				check_variable = {
					var = dr_days_in_war
					value = war_phase_offensive_threshold_3
					compare = greater_than_or_equals
				}
			}
			set_variable = { war_phase_factor = 1.0 }
		}
	}

	# Pure defensive war
	if = {
		limit = {
			has_defensive_war = yes
			has_offensive_war = no
		}

		if = {
			limit = {
				check_variable = {
					var = dr_days_in_war
					value = war_phase_defensive_threshold_1
					compare = greater_than_or_equals
				}
			}
			set_variable = { war_phase_factor = 0.33 }
		}
		if = {
			limit = {
				check_variable = {
					var = dr_days_in_war
					value = war_phase_defensive_threshold_2
					compare = greater_than_or_equals
				}
			}
			set_variable = { war_phase_factor = 0.66 }
		}
		if = {
			limit = {
				check_variable = {
					var = dr_days_in_war
					value = war_phase_defensive_threshold_3
					compare = greater_than_or_equals
				}
			}
			set_variable = { war_phase_factor = 1.0 }
		}
	}
}

# Calculate peacetime bonus based on stability and ruling party support
# Sets: peace_rp_bonus
dr_calculate_peace_bonus = {
	set_variable = { peace_rp_bonus = 0 }
	if = {
		limit = { has_war = no }

		# Stability contribution
		if = {
			limit = { has_stability > 0.7 }
			add_to_variable = { peace_rp_bonus = 0.02 }
		}
		if = {
			limit = { has_stability > 0.85 }
			add_to_variable = { peace_rp_bonus = 0.01 }
		}

		# Ruling party contribution (using helper effects for consistency)
		# Calculate government support factor and map to peace bonus
		dr_set_government_popularity = yes
		dr_get_government_support_factor = yes
		# Map government_support_factor to peace bonus: 0.0 -> 0.03, 0.1 -> 0.02, 0.2+ -> 0.0
		dr_map_government_support_to_peace_bonus = yes

		# Cap at +5%
		if = {
			limit = {
				check_variable = {
					var = peace_rp_bonus
					value = 0.05
					compare = greater_than
				}
			}
			set_variable = { peace_rp_bonus = 0.05 }
		}

		add_to_variable = { total_rp_modifier = peace_rp_bonus }
	}
}

dr_apply_war_rp_logic = {
	# War-time RP modifier (global, applied to total RP)
	set_variable = { war_rp_base_penalty = 0 }
	set_variable = { war_penalty_factor_total = 0 }
	set_variable = { war_rp_penalty_effective = 0 }

	if = {
		limit = { has_war = yes }

		# Maximaler Malus aus Game Rule
		if = {
			limit = { has_game_rule = { rule = DR_WAR_RP_RULE option = DR_WAR_RP_MINUS_5 } }
			set_variable = { war_rp_base_penalty = 0.05 }
		}
		if = {
			limit = { has_game_rule = { rule = DR_WAR_RP_RULE option = DR_WAR_RP_MINUS_10 } }
			set_variable = { war_rp_base_penalty = 0.10 }
		}
		if = {
			limit = { has_game_rule = { rule = DR_WAR_RP_RULE option = DR_WAR_RP_MINUS_15 } }
			set_variable = { war_rp_base_penalty = 0.15 }
		}
		if = {
			limit = { has_game_rule = { rule = DR_WAR_RP_RULE option = DR_WAR_RP_MINUS_20 } }
			set_variable = { war_rp_base_penalty = 0.20 }
		}
		if = {
			limit = { has_game_rule = { rule = DR_WAR_RP_RULE option = DR_WAR_RP_MINUS_25 } }
			set_variable = { war_rp_base_penalty = 0.25 }
		}
		if = {
			limit = { has_game_rule = { rule = DR_WAR_RP_RULE option = DR_WAR_RP_MINUS_30 } }
			set_variable = { war_rp_base_penalty = 0.30 }
		}

		# Calculate penalty factors (war support, stability, ruling party)
		dr_calculate_war_penalty_factors = yes

		# Calculate phase factor (war duration and type)
		dr_calculate_war_phase_factor = yes

		# Combined war penalty factor
		set_variable = { war_penalty_factor_total = 0 }
		add_to_variable = { war_penalty_factor_total = war_penalty_factor_ws }
		add_to_variable = { war_penalty_factor_total = war_penalty_factor_stab }
		add_to_variable = { war_penalty_factor_total = war_penalty_factor_party }

		multiply_variable = { war_penalty_factor_total = war_phase_factor }
		multiply_variable = { war_penalty_factor_total = war_defense_factor }

		set_variable = { war_rp_penalty_effective = war_rp_base_penalty }
		multiply_variable = { war_rp_penalty_effective = war_penalty_factor_total }
		multiply_variable = { war_rp_penalty_effective = -1 }
		add_to_variable = { total_rp_modifier = war_rp_penalty_effective }
	}

	# Calculate peacetime bonus
	dr_calculate_peace_bonus = yes
}

dr_apply_law_rp_logic = {
	# Law-based RP effects (trade / economy / conscription)
	# Can be toggled via DR_LAW_RP_RULE (default: ON).
	if = {
		limit = { has_game_rule = { rule = DR_LAW_RP_RULE option = DR_LAW_RP_ON } }

		# Trade law RP effects
		if = {
			limit = { has_idea = free_trade }
			add_to_variable = { total_rp_modifier = 0.02 }
		}
		if = {
			limit = { has_idea = export_focus }
			add_to_variable = { total_rp_modifier = 0.01 }
		}
		if = {
			limit = { has_idea = closed_economy }
			add_to_variable = { total_rp_modifier = -0.01 }
		}

		# Economy law RP effects
		if = {
			limit = { has_idea = civilian_economy }
			add_to_variable = { total_rp_modifier = 0.02 }
		}
		if = {
			limit = { has_idea = low_economic_mobilisation }
			add_to_variable = { total_rp_modifier = 0.01 }
		}
		if = {
			limit = { has_idea = war_economy }
			add_to_variable = { total_rp_modifier = -0.01 }
		}
		if = {
			limit = { has_idea = tot_economic_mobilisation }
			add_to_variable = { total_rp_modifier = -0.02 }
		}

		# Conscription law RP penalties
		if = {
			limit = { has_idea = extensive_conscription }
			add_to_variable = { total_rp_modifier = -0.02 }
		}
		if = {
			limit = { has_idea = service_by_requirement }
			add_to_variable = { total_rp_modifier = -0.04 }
		}
		if = {
			limit = { has_idea = all_adults_serve }
			add_to_variable = { total_rp_modifier = -0.07 }
		}
		if = {
			limit = { has_idea = scraping_the_barrel }
			add_to_variable = { total_rp_modifier = -0.10 }
		}
	}
}

# Helper effect: Calculate opinion factor for an ally (0.0 to 1.0 in 0.1 steps)
# Uses reverse checking (from highest to lowest) for better average performance
# Sets: alliance_rel_factor
dr_get_opinion_factor_for_ally = {
	set_variable = { alliance_rel_factor = 0.0 }
	
	# Check from highest to lowest (reverse order for better average performance)
	if = {
		limit = { has_opinion = { target = ROOT value > 79 } } # 80+
		set_variable = { alliance_rel_factor = 1.0 }
	}
	if = {
		limit = {
			has_opinion = { target = ROOT value > 71 } # 72-79
			NOT = { has_opinion = { target = ROOT value > 79 } }
		}
		set_variable = { alliance_rel_factor = 0.9 }
	}
	if = {
		limit = {
			has_opinion = { target = ROOT value > 63 } # 64-71
			NOT = { has_opinion = { target = ROOT value > 71 } }
		}
		set_variable = { alliance_rel_factor = 0.8 }
	}
	if = {
		limit = {
			has_opinion = { target = ROOT value > 55 } # 56-63
			NOT = { has_opinion = { target = ROOT value > 63 } }
		}
		set_variable = { alliance_rel_factor = 0.7 }
	}
	if = {
		limit = {
			has_opinion = { target = ROOT value > 47 } # 48-55
			NOT = { has_opinion = { target = ROOT value > 55 } }
		}
		set_variable = { alliance_rel_factor = 0.6 }
	}
	if = {
		limit = {
			has_opinion = { target = ROOT value > 39 } # 40-47
			NOT = { has_opinion = { target = ROOT value > 47 } }
		}
		set_variable = { alliance_rel_factor = 0.5 }
	}
	if = {
		limit = {
			has_opinion = { target = ROOT value > 31 } # 32-39
			NOT = { has_opinion = { target = ROOT value > 39 } }
		}
		set_variable = { alliance_rel_factor = 0.4 }
	}
	if = {
		limit = {
			has_opinion = { target = ROOT value > 23 } # 24-31
			NOT = { has_opinion = { target = ROOT value > 31 } }
		}
		set_variable = { alliance_rel_factor = 0.3 }
	}
	if = {
		limit = {
			has_opinion = { target = ROOT value > 15 } # 16-23
			NOT = { has_opinion = { target = ROOT value > 23 } }
		}
		set_variable = { alliance_rel_factor = 0.2 }
	}
	if = {
		limit = {
			has_opinion = { target = ROOT value > 7 } # 8-15
			NOT = { has_opinion = { target = ROOT value > 15 } }
		}
		set_variable = { alliance_rel_factor = 0.1 }
	}
	# 0-7 remains 0.0 (default)
}

# Helper effect: Calculate opinion factor FROM ROOT to PREV (Ally)
# Checks ROOT's opinion of the Ally (PREV).
# Sets: alliance_rel_factor
dr_get_opinion_factor_from_root = {
	set_variable = { alliance_rel_factor = 0.0 }
	
	# Check from highest to lowest
	if = {
		limit = { has_opinion = { target = PREV value > 79 } }
		set_variable = { alliance_rel_factor = 1.0 }
	}
	if = {
		limit = {
			has_opinion = { target = PREV value > 71 }
			NOT = { has_opinion = { target = PREV value > 79 } }
		}
		set_variable = { alliance_rel_factor = 0.9 }
	}
	if = {
		limit = {
			has_opinion = { target = PREV value > 63 }
			NOT = { has_opinion = { target = PREV value > 71 } }
		}
		set_variable = { alliance_rel_factor = 0.8 }
	}
	if = {
		limit = {
			has_opinion = { target = PREV value > 55 }
			NOT = { has_opinion = { target = PREV value > 63 } }
		}
		set_variable = { alliance_rel_factor = 0.7 }
	}
	if = {
		limit = {
			has_opinion = { target = PREV value > 47 }
			NOT = { has_opinion = { target = PREV value > 55 } }
		}
		set_variable = { alliance_rel_factor = 0.6 }
	}
	if = {
		limit = {
			has_opinion = { target = PREV value > 39 }
			NOT = { has_opinion = { target = PREV value > 47 } }
		}
		set_variable = { alliance_rel_factor = 0.5 }
	}
	if = {
		limit = {
			has_opinion = { target = PREV value > 31 }
			NOT = { has_opinion = { target = PREV value > 39 } }
		}
		set_variable = { alliance_rel_factor = 0.4 }
	}
	if = {
		limit = {
			has_opinion = { target = PREV value > 23 }
			NOT = { has_opinion = { target = PREV value > 31 } }
		}
		set_variable = { alliance_rel_factor = 0.3 }
	}
	if = {
		limit = {
			has_opinion = { target = PREV value > 15 }
			NOT = { has_opinion = { target = PREV value > 23 } }
		}
		set_variable = { alliance_rel_factor = 0.2 }
	}
	if = {
		limit = {
			has_opinion = { target = PREV value > 7 }
			NOT = { has_opinion = { target = PREV value > 15 } }
		}
		set_variable = { alliance_rel_factor = 0.1 }
	}
}

# Submod hook: override opinion factor calculation for alliance members
# If this hook sets alliance_rel_factor and sets the dr_opinion_factor_custom_set flag,
# the default calculation is skipped. This allows submods to explicitly set 0.0 if needed.
# Example:
#   set_variable = { alliance_rel_factor = 0.5 }
#   set_country_flag = dr_opinion_factor_custom_set
dr_get_opinion_factor_for_ally_submods = { }

dr_apply_alliance_rp_logic = {
	# Alliance / faction RP modifier
	# 5% per ally, scaled by relations and capped by game rule.

	# If alliance bonuses are disabled or we are not in a faction,
	# clear any previous bonus and return early.
	if = {
		limit = {
			OR = {
				is_in_faction = no
				has_game_rule = { rule = DR_ALLIANCE_RP_RULE option = DR_ALLIANCE_RP_OFF }
			}
		}
		set_variable = { alliance_bonus_pct = 0 }
	}

	if = {
		limit = {
			is_in_faction = yes
			OR = {
				has_game_rule = { rule = DR_ALLIANCE_RP_RULE option = DR_ALLIANCE_RP_PLUS_5 }
				has_game_rule = { rule = DR_ALLIANCE_RP_RULE option = DR_ALLIANCE_RP_PLUS_10 }
				has_game_rule = { rule = DR_ALLIANCE_RP_RULE option = DR_ALLIANCE_RP_PLUS_15 }
				has_game_rule = { rule = DR_ALLIANCE_RP_RULE option = DR_ALLIANCE_RP_PLUS_20 }
				has_game_rule = { rule = DR_ALLIANCE_RP_RULE option = DR_ALLIANCE_RP_PLUS_25 }
				has_game_rule = { rule = DR_ALLIANCE_RP_RULE option = DR_ALLIANCE_RP_PLUS_30 }
			}
		}

		# Initialize update timer if not present.
		if = {
			limit = { NOT = { has_variable = dr_days_until_alliance_update } }
			set_variable = { dr_days_until_alliance_update = 0 }
		}

		# Countdown and only recalculate when timer expires.
		subtract_from_variable = { dr_days_until_alliance_update = 1 }

		if = {
			limit = {
				check_variable = {
					var = dr_days_until_alliance_update
					value = 0
					compare = less_than_or_equals
				}
			}

			# Rebuild alliance bonus from scratch.
			set_variable = { alliance_member_count = 0 }
			set_variable = { alliance_rel_factor_sum = 0 }
			set_variable = { alliance_bonus_cap = 0 }

			every_other_country = {
				limit = { is_in_faction_with = ROOT }

				ROOT = { add_to_variable = { alliance_member_count = 1 } }

				# Calculate opinion factor (0.0 to 1.0 in 0.1 steps)
				# Relation factor g_rel in 10 steps between 0 and 80 opinion:
				# 0-7  -> 0.0
				# ...
				# 80+   -> 1.0

				# 1. Calculate Ally's opinion of US (They like Us)
				# Run in ALLY scope
				set_variable = { alliance_rel_factor = 0.0 }
				clr_country_flag = dr_opinion_factor_custom_set
				
				# Allow submods to override opinion calculation (in Ally scope)
				dr_get_opinion_factor_for_ally_submods = yes
				
				# Default calculation
				if = {
					limit = { NOT = { has_country_flag = dr_opinion_factor_custom_set } }
					dr_get_opinion_factor_for_ally = yes
				}
				set_variable = { factor_they_like_us = alliance_rel_factor }

				# 2. Calculate OUR opinion of THEM (We like Them)
				# Run in ROOT scope
				ROOT = {
					set_variable = { alliance_rel_factor = 0.0 }
					
					# Use the new helper to check opinion of PREV (Ally)
					dr_get_opinion_factor_from_root = yes
					set_variable = { factor_we_like_them = alliance_rel_factor }
					
					# 3. Average the two factors
					add_to_variable = { factor_we_like_them = PREV:factor_they_like_us }
					divide_variable = { factor_we_like_them = 2 }
					
					# Add to sum
					add_to_variable = { alliance_rel_factor_sum = factor_we_like_them }
				}
			}

			# 5% base bonus per ally, capped by game rule and scaled with average relation.
			if = {
				limit = {
					check_variable = {
						var = alliance_member_count
						value = 0
						compare = greater_than
					}
				}

				# Base cap from number of allies: 5% per member.
				set_variable = { alliance_bonus_cap = alliance_member_count }
				multiply_variable = { alliance_bonus_cap = 0.05 }

				# Global cap from game rule (min(alliance_bonus_cap, rule value)).
				if = {
					limit = { has_game_rule = { rule = DR_ALLIANCE_RP_RULE option = DR_ALLIANCE_RP_PLUS_5 } }
					if = {
						limit = {
							check_variable = {
								var = alliance_bonus_cap
								value = 0.05
								compare = greater_than
							}
						}
						set_variable = { alliance_bonus_cap = 0.05 }
					}
				}
				if = {
					limit = { has_game_rule = { rule = DR_ALLIANCE_RP_RULE option = DR_ALLIANCE_RP_PLUS_10 } }
					if = {
						limit = {
							check_variable = {
								var = alliance_bonus_cap
								value = 0.10
								compare = greater_than
							}
						}
						set_variable = { alliance_bonus_cap = 0.10 }
					}
				}
				if = {
					limit = { has_game_rule = { rule = DR_ALLIANCE_RP_RULE option = DR_ALLIANCE_RP_PLUS_15 } }
					if = {
						limit = {
							check_variable = {
								var = alliance_bonus_cap
								value = 0.15
								compare = greater_than
							}
						}
						set_variable = { alliance_bonus_cap = 0.15 }
					}
				}
				if = {
					limit = { has_game_rule = { rule = DR_ALLIANCE_RP_RULE option = DR_ALLIANCE_RP_PLUS_20 } }
					if = {
						limit = {
							check_variable = {
								var = alliance_bonus_cap
								value = 0.20
								compare = greater_than
							}
						}
						set_variable = { alliance_bonus_cap = 0.20 }
					}
				}
				if = {
					limit = { has_game_rule = { rule = DR_ALLIANCE_RP_RULE option = DR_ALLIANCE_RP_PLUS_25 } }
					if = {
						limit = {
							check_variable = {
								var = alliance_bonus_cap
								value = 0.25
								compare = greater_than
							}
						}
						set_variable = { alliance_bonus_cap = 0.25 }
					}
				}
				if = {
					limit = { has_game_rule = { rule = DR_ALLIANCE_RP_RULE option = DR_ALLIANCE_RP_PLUS_30 } }
					if = {
						limit = {
							check_variable = {
								var = alliance_bonus_cap
								value = 0.30
								compare = greater_than
							}
						}
						set_variable = { alliance_bonus_cap = 0.30 }
					}
				}

				# Average relation factor (0-1) across all allies.
				set_variable = { alliance_avg_rel_factor = alliance_rel_factor_sum }
				divide_variable = { alliance_avg_rel_factor = alliance_member_count }

				# Effective alliance bonus: cap * average relation factor.
				set_variable = { alliance_bonus_pct = alliance_bonus_cap }
				multiply_variable = { alliance_bonus_pct = alliance_avg_rel_factor }
			}

			# Re-run alliance update roughly once per week.
			set_variable = { dr_days_until_alliance_update = 7 }
		}
	}

	# Apply the last computed alliance bonus (0 if none).
	if = {
		limit = { has_variable = alliance_bonus_pct }
		add_to_variable = { total_rp_modifier = alliance_bonus_pct }
	}
}
