d_set_host = {
	ROOT = {
		set_country_flag = wia_host
	}
	log = "Gave host permissions to this country"
}

d_set_mp = {
	set_global_flag = wia_mp
	log = "Multiplayer flag has activated"
}

d_unset_mp = {
	clr_global_flag = wia_mp
	log = "Multiplayer flag has activated"
}

d_give_money = {
	add_to_variable = {
		var = money_var
		value = 10000
	}
}

d_money_switch = {
	if = {
		limit = {
			has_global_flag = wia_money_gflag
		}

		clr_global_flag = wia_money_gflag

		else_if = {
			limit = {
				NOT = {
					has_global_flag = wia_money_gflag
				}
			}
			set_global_flag = wia_money_gflag
		}
	}
}

d_bm_sp_all_equipment = {
	FROM = {
		set_technology = {
			bm_sp_unlock_all_equipment = 1
			popup = no
		}
		log = "Unlocked all equipment added by Better Mechanics : Extended Projects"
	}
}
d_bm_sp_all_modules = {
	FROM = {
		set_technology = {
			bm_sp_unlock_all_modules = 1
			popup = no
		}
		log = "Unlocked all modules added by Better Mechanics : Extended Projects"
	}
}
d_bm_sp_all_units = {
	FROM = {
		set_technology = {
			bm_sp_unlock_all_units = 1
			popup = no
		}
		log = "Unlocked all units added by Better Mechanics : Extended Projects"
	}
}
d_bm_sp_all = {
	FROM = {
		set_technology = {
			bm_sp_unlock_all_equipment = 1
			popup = no
		}
		set_technology = {
			bm_sp_unlock_all_modules = 1
			popup = no
		}
		set_technology = {
			bm_sp_unlock_all_units = 1
			popup = no
		}
		log = "Unlocked all equipment, modules and units added by Better Mechanics : Extended Projects"
	}
}

more_mapmodes_update_custom_map_modes_effects = {
	force_update_map_mode = {
		mapmode = more_mapmodes_core_map_mode
	}
	force_update_map_mode = {
		mapmode = more_mapmodes_majors_map_mode
	}
	force_update_map_mode = {
		mapmode = more_mapmodes_world_war_map_mode
	}
	force_update_map_mode = {
		mapmode = more_mapmodes_spymasters_map_mode
	}
	force_update_map_mode = {
		mapmode = more_mapmodes_guarantees_map_mode
	}
	force_update_map_mode = {
		mapmode = more_mapmodes_subjects_map_mode
	}
}

increase_mobilisation = {
	if = {
		limit = {
			has_idea = war_economy
		}
		swap_ideas = {
			remove_idea = war_economy
			add_idea = tot_economic_mobilisation
		}
	}
	if = {
		limit = {
			has_idea = partial_economic_mobilisation
		}
		swap_ideas = {
			remove_idea = partial_economic_mobilisation
			add_idea = war_economy
		}
	}
	if = {
		limit = {
			has_idea = low_economic_mobilisation
		}
		swap_ideas = {
			remove_idea = low_economic_mobilisation
			add_idea = partial_economic_mobilisation
		}
	}
	if = {
		limit = {
			has_idea = civilian_economy
		}
		swap_ideas = {
			remove_idea = civilian_economy
			add_idea = low_economic_mobilisation
		}
	}
	if = {
		limit = {
			has_idea = isolation
		}
		swap_ideas = {
			remove_idea = isolation
			add_idea = civilian_economy
		}
	}
	if = {
		limit = {
			has_idea = undisturbed_isolation
		}
		swap_ideas = {
			remove_idea = undisturbed_isolation
			add_idea = isolation
		}
	}
}

decrease_mobilisation = {
	if = {
		limit = {
			has_idea = low_economic_mobilisation
		}
		swap_ideas = {
			remove_idea = low_economic_mobilisation
			add_idea = civilian_economy
		}
	}
	if = {
		limit = {
			has_idea = partial_economic_mobilisation
		}
		swap_ideas = {
			remove_idea = partial_economic_mobilisation
			add_idea = low_economic_mobilisation
		}
	}
	if = {
		limit = {
			has_idea = war_economy
		}
		swap_ideas = {
			remove_idea = war_economy
			add_idea = partial_economic_mobilisation
		}
	}
	if = {
		limit = {
			has_idea = tot_economic_mobilisation
		}
		swap_ideas = {
			remove_idea = tot_economic_mobilisation
			add_idea = war_economy
		}
	}
}

disband_twenty_percent_units = {
	save_event_target_as = disband_country
	XXA = {
		set_temp_variable = {
			old_manpower = manpower_k
		}
	}
	random_state = {
		limit = {
			impassable = yes
			is_fully_controlled_by = owner
		}
		owner = { save_event_target_as = disband_state_owner }
		save_event_target_as = disband_state
		set_state_owner_to = XXA
	}
	transfer_units_fraction = {
		target = XXA
		size = 0
		army_ratio = 0.2
		keep_unit_leaders_trigger = {
			always = yes
		}
	}
	XXA = {
		delete_unit = {
			disband = yes
		}
		set_temp_variable = {
			manpower_diff = manpower_k
		}
		subtract_from_temp_variable = {
			manpower_diff = old_manpower
		}
		while_loop_effect = { #avoid overflows
			limit = { check_variable = { manpower_diff > 1 } }
			subtract_from_temp_variable = { manpower_diff = 1 }
			event_target:disband_country = { add_manpower = 1000 }
			add_manpower = -1000
		}
		multiply_temp_variable = {
			manpower_diff = 1000
		}
		event_target:disband_country = { add_manpower = PREV.manpower_diff }
		multiply_temp_variable = {
			manpower_diff = -1
		}
		add_manpower = manpower_diff
		send_equipment_fraction = { target = event_target:disband_country value = 1 }
	}
	event_target:disband_state = { set_state_owner_to = event_target:disband_state_owner }
	annex_country = {
		target = XXA
		transfer_troops = no
	}
}

disband_twenty_percent_units_delete_equipment = { #used for disarming countries in peace events
	save_event_target_as = disband_country
	XXA = {
		set_temp_variable = {
			old_manpower = manpower_k
		}
	}
	random_state = {
		limit = {
			impassable = yes
			is_fully_controlled_by = owner
		}
		owner = { save_event_target_as = disband_state_owner }
		save_event_target_as = disband_state
		set_state_owner_to = XXA
	}
	transfer_units_fraction = {
		target = XXA
		size = 0
		army_ratio = 0.2
		keep_unit_leaders_trigger = {
			always = yes
		}
	}
	XXA = {
		delete_unit = {
			disband = yes
		}
		set_temp_variable = {
			manpower_diff = manpower_k
		}
		subtract_from_temp_variable = {
			manpower_diff = old_manpower
		}
		while_loop_effect = { #avoid overflows
			limit = { check_variable = { manpower_diff > 1 } }
			subtract_from_temp_variable = { manpower_diff = 1 }
			event_target:disband_country = { add_manpower = 1000 }
			add_manpower = -1000
		}
		multiply_temp_variable = {
			manpower_diff = 1000
		}
		event_target:disband_country = { add_manpower = PREV.manpower_diff }
		multiply_temp_variable = {
			manpower_diff = -1
		}
		add_manpower = manpower_diff
		set_equipment_fraction = 0
	}
	event_target:disband_state = { set_state_owner_to = event_target:disband_state_owner }
	annex_country = {
		target = XXA
		transfer_troops = no
	}
}

disband_fifty_percent_units = {
	save_event_target_as = disband_country
	XXA = {
		set_temp_variable = {
			old_manpower = manpower_k
		}
	}
	random_state = {
		limit = {
			impassable = yes
			is_fully_controlled_by = owner
		}
		owner = { save_event_target_as = disband_state_owner }
		save_event_target_as = disband_state
		set_state_owner_to = XXA
	}
	transfer_units_fraction = {
		target = XXA
		size = 0
		army_ratio = 0.5
		keep_unit_leaders_trigger = {
			always = yes
		}
	}
	XXA = {
		delete_unit = {
			disband = yes
		}
		set_temp_variable = {
			manpower_diff = manpower_k
		}
		subtract_from_temp_variable = {
			manpower_diff = old_manpower
		}
		while_loop_effect = { #avoid overflows
			limit = { check_variable = { manpower_diff > 1 } }
			subtract_from_temp_variable = { manpower_diff = 1 }
			event_target:disband_country = { add_manpower = 1000 }
			add_manpower = -1000
		}
		multiply_temp_variable = {
			manpower_diff = 1000
		}
		event_target:disband_country = { add_manpower = PREV.manpower_diff }
		multiply_temp_variable = {
			manpower_diff = -1
		}
		add_manpower = manpower_diff
		send_equipment_fraction = { target = event_target:disband_country value = 1 }
	}
	event_target:disband_state = { set_state_owner_to = event_target:disband_state_owner }
	annex_country = {
		target = XXA
		transfer_troops = no
	}
}

calculate_maximum_divisions = {
	### calculate base values - population and total factory count
	clear_variable = non_core_base
	set_variable = { base_units = 0 }
	set_variable = { base_states = num_owned_states }
	set_temp_variable = { factory_points = 0 }
	set_temp_variable = { mils = 0 }
	every_owned_state = {
		if = {
			limit = { is_core_of = ROOT }
			add_to_variable = { ROOT.base_units = state_population_k }
		}
		else = {
			add_to_variable = { ROOT.non_core_base = state_population_k }
		}
		add_to_temp_variable = { factory_points = industrial_complex_level }
		add_to_temp_variable = { factory_points = building_level@dockyard }
		add_to_temp_variable = { mils = arms_factory_level }
	}
	multiply_temp_variable = { mils = 4 } #mils are worth 1.5x as much as civs/navs
	add_to_temp_variable = { factory_points = mils }

	# non-cores only give 10% of the population
	if = {
		limit = { has_variable = non_core_base }
		divide_variable = { non_core_base = 10 }
		add_to_variable = { base_units = non_core_base }
		divide_variable = { non_core_base = 100 } #for the tooltip
	}

	# base unit count = population / 1000
	divide_variable = { base_units = 100 }
	set_variable = { base_population = base_units } #for the tooltip
	multiply_variable = { base_states = 20 }
	add_to_variable = { base_units = base_states }

	# calculate the modifier -- 1 + [ ( industry - population ) / ( industry + population ) ]
	set_variable = { divisor = factory_points }
	add_to_variable = { divisor = base_units }

	set_variable = { industry_factor = factory_points }
	subtract_from_variable = { industry_factor = base_units }
	divide_variable = { industry_factor = divisor }
	add_to_variable = { industry_factor = 1 }
	
	multiply_variable = { industry_factor = 4.25 }
	
	# minimum modifier -- x0.5
	clamp_variable = { var = industry_factor min = 0.15 }

	set_variable = { max_divisions = base_units }
	multiply_variable = { max_divisions = industry_factor } 

	# +15% for faction leaders
	if = {
		limit = { is_faction_leader = yes }
		multiply_variable = { max_divisions = 1.5 }
	}

	# +25% for has war #add by ken
	if = {
		limit = { has_war = yes }
		multiply_variable = { max_divisions = 1.5 }
	}

	# +10% per economy law level above civilian economy
	set_temp_variable = { economy_mult = 1 }
	add_to_temp_variable = { economy_mult = modifier@unit_limit_factory_bonus }
	multiply_variable = { max_divisions = economy_mult }

	# the lower limit is the number of owned states or 5, whichever is higher
	set_variable = { lower_limit = num_owned_states }
	clamp_variable = {
		var = lower_limit
		min = 100
		max = 7500
	}

	# apply the lower limit
	set_variable = { net_max_divisions = max_divisions }
	round_variable = net_max_divisions
	clamp_variable = {
		var = max_divisions
		min = lower_limit
		max = 7500
	}

	# add any specific bonuses to the limit
	set_variable = { additional_unit_limit = modifier@unit_limit_bonus }
	add_to_variable = { max_divisions = additional_unit_limit }

	round_variable = max_divisions
}

calculate_mobilisation_penalty = {
	set_variable = { mobilisation_penalty_var = num_battalions }
	divide_variable = { mobilisation_penalty_var = max_divisions }
	subtract_from_variable = { mobilisation_penalty_var = 1 }

	divide_variable = { mobilisation_penalty_var = 4 }
	clamp_variable = {
		var = mobilisation_penalty_var
		min = -0.3
		max = 0.3
	}

	set_variable = { mobilisation_penalty_var_positive = mobilisation_penalty_var }

	#for negative effects
	multiply_variable = { mobilisation_penalty_var = -1 }
}
