﻿### Constants
@max_battles_to_win_state = 10
@min_battles_to_win_state = 1
@min_victory_score = 50
@hazardous_terrain_factor = 0.03
@forested_terrain_factor = 0.02
@elevated_terrain_factor = 0.01

concentration_of_defensive_mobilized_battalions = {
	add = num_mobilized_units_in_theater
	divide = num_provinces_in_theater
}

provinces_with_hazardous_terrain = {
	every_province = {
		if = { 
			limit = { has_label = label_hazardous }
			add = 1
		}
	}
}

provinces_with_forested_terrain = {
	every_province = {
		if = { 
			limit = { has_label = label_forested }
			add = 1
		}
	}
}

provinces_with_elevated_terrain = {
	every_province = {
		if = { 
			limit = { has_label = label_elevated }
			add = 1
		}
	}
}

# root = state
# is_advancing_side = true if the state is being occupied
occupation_cost = {
	# population of the state sets the base cost
	add = {
		add = state_population
		divide = 5000
		max = 1000
		min = 1
		desc = "OCCUPATION_COST_FROM_POPULATION"
	}
	
	save_temporary_value_as = base_cost
	# cost increases for every province with difficult terrain, up to a limit set by infrastructure used in the state
	if = {
		limit = { provinces_with_hazardous_terrain > 0 }
		add = {
			add = scope:base_cost
			multiply = {
				add = {
					add = provinces_with_hazardous_terrain
					multiply = @[hazardous_terrain_factor]
				}
			}
			desc = "OCCUPATION_COST_FROM_NUM_HAZARDOUS_PROVINCES"
		}
	}
	
	if = {
		limit = { provinces_with_forested_terrain > 0 }
		add = {
			add = scope:base_cost
			multiply = {
				add = provinces_with_forested_terrain
				multiply = @[forested_terrain_factor]
			}
			desc = "OCCUPATION_COST_FROM_NUM_FORESTED_PROVINCES"
		}
	}
	if = {
		limit = { provinces_with_elevated_terrain > 0 }
		add = {
			add = scope:base_cost
			multiply = {
				add = provinces_with_elevated_terrain
				multiply = @[hazardous_terrain_factor]
			}
			desc = "OCCUPATION_COST_FROM_NUM_ELEVATED_PROVINCES"
		}
	}
	# for every 20 points of infrastructure usage in the state, 1x multiple of difficult terrain bonus is negated
	#if = {
	#	limit = { infrastructure_usage > 1 }
	#	divide = {
	##		add = 1
	#		add = {
	#			add = {
	##				add = provinces_with_hazardous_terrain
	#				multiply = @[hazardous_terrain_factor]
	#			}
	#			add = {
		#			add = provinces_with_forested_terrain
	#				multiply = @[forested_terrain_factor]
	#			}
	#			add = {
	#				add = provinces_with_elevated_terrain
	#				multiply = @[elevated_terrain_factor]
	#			}
	#			save_temporary_value_as = total_difficult_terrain_malus
	#			
	#			max = {
		#			add = infrastructure_usage
	#				divide = 20
	#				max = scope:total_difficult_terrain_malus
	#			}
	#		}
	#		desc = "OCCUPATION_COST_FROM_INFRASTRUCTURE_USAGE"
	#	}
	#}
	
	# cost increases for the occupier if there are more mobilized enemy battalions in the theater than there are provinces
	#theater = {
	#	if = {
	#		limit = { 
	#			scope:is_advancing_side = yes
	#			concentration_of_defensive_mobilized_battalions > 1
	#		}
	#		multiply = {
	#			add = concentration_of_defensive_mobilized_battalions
	#			max = 10
	#		}
	#		desc = "OCCUPATION_COST_FROM_CONCENTRATION_OF_BATTALIONS"
	#	}
	#}
	
	save_temporary_value_as = pre_clamped_cost
	if = {
		limit = { scope:pre_clamped_cost > @[min_victory_score*max_battles_to_win_state] }
		max = @[min_victory_score*max_battles_to_win_state]
		desc = "OCCUPATION_COST_MAX_VALUE_REACHED"
	}
	if = {
		limit = { scope:pre_clamped_cost < @[min_victory_score*min_battles_to_win_state] }
		min = @[min_victory_score*min_battles_to_win_state]
		desc = "OCCUPATION_COST_MIN_VALUE_REACHED"
	}	
}

# root = battleside
battle_victory_score = {
	add = {
		add = 100
		desc = "BATTLE_VICTORY_BASE"
	}

	multiply = {
			add = 1
			add = modifier:unit_occupation_mult
			min = 0
			desc = "BATTLE_VICTORY_UNIT_OCCUPATION_MULT"
		}
	
	save_temporary_value_as = pre_min_amount_cap
	if = {
		limit = { scope:pre_min_amount_cap < @[min_victory_score] }
		min = @[min_victory_score]
		desc = "BATTLE_VICTORY_MIN_VALUE_REACHED"
	}
	
	# Starting / Final Battalions on winning / losing side
	# Starting / Final Morale on winning / losing side
	# General scope of winning / losing side (for modifier access)
}