# This script added a number of rural districts of each type set by a variable
# Flags are used to determine if the district type is uncapped or not.
# Otherwise planet class is used in some special cases.
# Finally the variables and flags are cleared.

# Cap district counts for Arkship colonies
if = {
	limit = {
		is_planet_class = pc_ark
	}
	if = {
		limit = {
			check_variable = {
				which = energy_districts
				value > ark_energy_district_cap
			}
		}
		set_variable = {
			which = energy_districts
			value = ark_energy_district_cap
		}
	}
}

# Generators
if = {
	limit = {
		check_variable = {
			which = energy_districts
			value >= 1
		}
	}
	if = {
		limit = {
			NOR = {
				has_carrier_flag = uncapped_energy
				is_planet_class = pc_machine
				is_planet_class = pc_habitat
				is_planet_class = pc_ark
			}
			check_modifier_value = {
				modifier = district_generator_max_add
				value < energy_districts
			}
		}
		export_modifier_to_variable = {
			modifier = district_generator_max_add
			variable = energy_districts
		}
	}
	if = {
		limit = {
			has_carrier_flag = wilderness_planet
		}
		add_district_and_planet_size_if_needed_effect_amount = {
			amount = energy_districts
			district = district_photosynthesis_fields
		}
	}
	else_if = {
		limit = {
			has_carrier_flag = uncapped_energy
		}
		add_district_and_planet_size_if_needed_effect_amount = {
			amount = energy_districts
			district = district_generator
		}
	}
	else_if = {
		limit = {
			is_planet_class = pc_machine
		}
		while = {
			count = energy_districts
			add_district = district_nexus_1
		}
		add_zone = {
			district = district_nexus_1
			zone = zone_energy_nexus
		}
	}
	else_if = {
		limit = {
			is_planet_class = pc_habitat
		}
		add_district_and_planet_size_if_needed_effect_amount = {
			amount = energy_districts
			district = district_hab_energy
		}
	}
	else_if = {
		limit = {
			is_planet_class = pc_ark
		}
		add_district_and_planet_size_if_needed_effect_amount = {
			amount = energy_districts
			district = district_ark_generator
		}
	}
	else = {
		add_district_and_planet_size_if_needed_effect_amount = {
			amount = energy_districts
			district = district_generator
		}
	}
}

# Mining
if = {
	limit = {
		check_variable = {
			which = mining_districts
			value >= 1
		}
	}
	if = {
		limit = {
			NOR = {
				has_carrier_flag = uncapped_mining
				is_planet_class = pc_machine
				is_planet_class = pc_habitat
				is_planet_class = pc_ark
			}
			check_modifier_value = {
				modifier = district_mining_max_add
				value < mining_districts
			}
		}
		export_modifier_to_variable = {
			modifier = district_mining_max_add
			variable = mining_districts
		}
	}
	if = {
		limit = {
			has_carrier_flag = wilderness_planet
		}
		add_district_and_planet_size_if_needed_effect_amount = {
			amount = mining_districts
			district = district_hollow_mountains
		}
	}
	else_if = {
		limit = {
			has_carrier_flag = uncapped_mining
		}
		add_district_and_planet_size_if_needed_effect_amount = {
			amount = mining_districts
			district = district_mining
		}
	}
	else_if = {
		limit = {
			is_planet_class = pc_machine
		}
		while = {
			count = mining_districts
			add_district = district_nexus_2
		}
		add_zone = {
			district = district_nexus_2
			zone = zone_minerals_nexus
		}
	}
	else_if = {
		limit = {
			is_planet_class = pc_habitat
		}
		add_district_and_planet_size_if_needed_effect_amount = {
			amount = mining_districts
			district = district_hab_mining
		}
	}
	else_if = {
		limit = {
			is_planet_class = pc_ark
		}
		# Do nothing
		# Arks have no mining districts
	}
	else = {
		add_district_and_planet_size_if_needed_effect_amount = {
			amount = mining_districts
			district = district_mining
		}
	}
}

# Arkship Decks
if = {
	limit = {
		is_planet_class = pc_ark
	}
	add_district_and_planet_size_if_needed_effect_amount = {
		amount = ark_basic_districts
		district = district_ark_basic
	}
	if = {
		limit = {
			check_variable = {
				which = farming_districts
				value >= 1
			}
		}
		add_zone = {
			district = district_ark_basic
			zone = zone_ark_food
		}
	}
	else = {
		add_zone = {
			district = district_ark_basic
			zone = zone_ark_harvest
		}
	}
}

# Agriculture
if = {
	limit = {
		check_variable = {
			which = farming_districts
			value >= 1
		}
	}
	if = {
		limit = {
			NOT = {
				has_carrier_flag = uncapped_farming
				is_planet_class = pc_ark
			}
			check_modifier_value = {
				modifier = district_farming_max_add
				value < farming_districts
			}
		}
		export_modifier_to_variable = {
			modifier = district_farming_max_add
			variable = farming_districts
		}
	}
	if = {
		limit = {
			has_carrier_flag = wilderness_planet
		}
		add_district_and_planet_size_if_needed_effect_amount = {
			amount = farming_districts
			district = district_orchard_forests
		}
	}
	else_if = {
		limit = {
			has_carrier_flag = uncapped_farming
		}
		add_district_and_planet_size_if_needed_effect_amount = {
			amount = farming_districts
			district = district_farming
		}
	}
	else_if = {
		limit = {
			is_planet_class = pc_machine
		}
		# Do nothing
		# Included to prevent the final else statement from trying to add districts
	}
	else_if = {
		limit = {
			is_planet_class = pc_habitat
		}
		add_zone = {
			district = district_hab_energy
			zone = zone_habitat_hydroponics
		}
		add_district_and_planet_size_if_needed_effect_amount = {
			amount = farming_districts
			district = district_hab_energy
		}
		add_building = {
			zone = zone_habitat_hydroponics
			building = building_hydroponics_farm
		}
	}
	else_if = {
		limit = {
			is_planet_class = pc_ark
		}
		# Do nothing
		# Handled in the ark-specific block above
	}
	else = {
		add_district_and_planet_size_if_needed_effect_amount = {
			amount = farming_districts
			district = district_farming
		}
	}
}

# Clear variables and flags
clear_variable = energy_districts
clear_variable = mining_districts
clear_variable = farming_districts
clear_variable = ark_energy_district_cap
clear_variable = ark_basic_districts
remove_carrier_flag = uncapped_energy
remove_carrier_flag = uncapped_mining
remove_carrier_flag = uncapped_farming
remove_carrier_flag = wilderness_planet
