Skip to content

Adjust urban sprawl to work better with different map sizes #1973

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/economy/economy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -745,7 +745,7 @@ void initialize(sys::state& state) {
province::for_each_land_province(state, [&](dcon::province_id p) {
auto fp = fatten(state.world, p);
//max size of exploitable land:
auto max_rgo_size = std::ceil(4000.f * state.map_state.map_data.province_area[province::to_map_id(p)]);
auto max_rgo_size = std::ceil(state.map_state.map_data.province_area_km2[province::to_map_id(p)]);
// currently exploited land
float pop_amount = 0.0f;
for(auto pt : state.world.in_pop_type) {
Expand Down
3 changes: 3 additions & 0 deletions src/gamestate/serialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ uint8_t const* read_scenario_section(uint8_t const* ptr_in, uint8_t const* secti
ptr_in = deserialize(ptr_in, state.map_state.map_data.terrain_id_map);
ptr_in = deserialize(ptr_in, state.map_state.map_data.province_id_map);
ptr_in = deserialize(ptr_in, state.map_state.map_data.province_area);
ptr_in = deserialize(ptr_in, state.map_state.map_data.province_area_km2);
ptr_in = deserialize(ptr_in, state.map_state.map_data.diagonal_borders);
}
{
Expand Down Expand Up @@ -331,6 +332,7 @@ uint8_t* write_scenario_section(uint8_t* ptr_in, sys::state& state) {
ptr_in = serialize(ptr_in, state.map_state.map_data.terrain_id_map);
ptr_in = serialize(ptr_in, state.map_state.map_data.province_id_map);
ptr_in = serialize(ptr_in, state.map_state.map_data.province_area);
ptr_in = serialize(ptr_in, state.map_state.map_data.province_area_km2);
ptr_in = serialize(ptr_in, state.map_state.map_data.diagonal_borders);
}
{
Expand Down Expand Up @@ -512,6 +514,7 @@ scenario_size sizeof_scenario_section(sys::state& state) {
sz += serialize_size(state.map_state.map_data.terrain_id_map);
sz += serialize_size(state.map_state.map_data.province_id_map);
sz += serialize_size(state.map_state.map_data.province_area);
sz += serialize_size(state.map_state.map_data.province_area_km2);
sz += serialize_size(state.map_state.map_data.diagonal_borders);
}
{ sz += sizeof(parsing::defines); }
Expand Down
49 changes: 33 additions & 16 deletions src/map/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2342,13 +2342,16 @@ bool get_provinces_part_of_rr_path(sys::state& state, std::vector<bool>& visited
return true;
}

glm::vec2 get_node(sys::state& state, glm::vec2 center, int i, int j) {
glm::vec2 get_node(sys::state& state, glm::vec2 center, int i, int j, int size_x, int size_y) {
const auto rpx = rng::get_random(state, j ^ i ^ (uint32_t)center.x, i);
const float rx = (float(rng::reduce(uint32_t(rpx), 8192)) / (8192.f)) - 0.5f;
const auto rpy = rng::get_random(state, j ^ i ^ (uint32_t)center.y ^ 5653, j);
const float ry = (float(rng::reduce(uint32_t(rpy), 8192)) / (8192.f)) - 0.5f;

auto base_shift = glm::vec2{ (float)i + rx, (float)j + ry } * 0.4f / sqrt(sqrt((float) (i * i) + (float) (j * j) + 1));
auto scale_x = (float)size_x / 5600.f;
auto scale_y = (float)size_y / 2160.f;

auto base_shift = glm::vec2{ ((float)i + rx) * scale_x, ((float)j + ry) * scale_y} * 0.4f / sqrt(sqrt((float) (i * i) + (float) (j * j) + 1));
return center + base_shift;
}

Expand All @@ -2362,25 +2365,29 @@ void display_data::update_sprawl(sys::state& state) {
std::vector<std::vector<glm::vec2>> connectors{};
connectors.resize(state.world.province_size());

auto minimal_population_per_visible_settlement = 2500.f;

// Populate paths with railroads - only account provinces that have been visited
// but not the adjacencies
for(const auto p : state.world.in_province) {

auto rural_population = 0.f;
for(auto wt : state.culture_definitions.rgo_workers) {
rural_population += state.world.province_get_demographics(p, demographics::to_key(state, wt));
for(auto pt : state.world.in_pop_type) {
if(pt.get_is_paid_rgo_worker())
rural_population += state.world.province_get_demographics(p, demographics::to_key(state, pt));
}
rural_population += state.world.province_get_demographics(p, demographics::to_employment_key(state, state.culture_definitions.slaves));

rural_population += state.world.province_get_demographics(p, demographics::to_key(state, state.culture_definitions.slaves));
rural_population += state.world.province_get_demographics(p, demographics::to_key(state, state.culture_definitions.clergy));

auto urban_pop = p.get_demographics(demographics::total) - rural_population;

if(urban_pop < 1000.f) {
if(urban_pop < minimal_population_per_visible_settlement) {
continue;
}

auto province_size = state.map_state.map_data.province_area[province::to_map_id(p)];
if(province_size < 1) {
auto province_size = state.map_state.map_data.province_area_km2[province::to_map_id(p)];
auto province_size_pixels = state.map_state.map_data.province_area[province::to_map_id(p)];
if(province_size_pixels < 1) {
continue;
}

Expand All @@ -2404,7 +2411,17 @@ void display_data::update_sprawl(sys::state& state) {

std::vector<std::pair<glm::vec2, float>> weighted_settlements;

int potential_settlement_slots = std::min((unsigned)7, province_size / 200);
auto km2_per_potential_settlement = 2000.f;


int potential_settlement_slots = std::min(
(int)7, std::min(
(int)(province_size / km2_per_potential_settlement),
(int)(urban_pop / minimal_population_per_visible_settlement)
)
);
potential_settlement_slots = std::max(1, potential_settlement_slots);

int settlement_slots = potential_settlement_slots;

if(p.get_port_to()) {
Expand All @@ -2421,7 +2438,7 @@ void display_data::update_sprawl(sys::state& state) {
}
}

auto side = sqrt(province_size);
auto side = sqrt(province_size_pixels);

// try to spawn random settlements in this area

Expand All @@ -2446,7 +2463,7 @@ void display_data::update_sprawl(sys::state& state) {
settlement_slots -= 1;
weighted_settlements.push_back({ pos, 0.5f / (potential_settlement_slots + 1) });
roads.push_back({ pos, central_settlement });
if(settlement_slots < 0) {
if(settlement_slots <= 0) {
break;
}
}
Expand Down Expand Up @@ -2527,10 +2544,10 @@ void display_data::update_sprawl(sys::state& state) {
continue;
}

auto node_1 = get_node(state, settlement.first, i, k);
auto node_2 = get_node(state, settlement.first, i + 1, k);
auto node_3 = get_node(state, settlement.first, i, k + 1);
auto node_4 = get_node(state, settlement.first, i + 1, k + 1);
auto node_1 = get_node(state, settlement.first, i, k, size_x, size_y);
auto node_2 = get_node(state, settlement.first, i + 1, k, size_x, size_y);
auto node_3 = get_node(state, settlement.first, i, k + 1, size_x, size_y);
auto node_4 = get_node(state, settlement.first, i + 1, k + 1, size_x, size_y);

auto node_center = (node_1 + node_2 + node_3 + node_4) / 4.f;

Expand Down
1 change: 1 addition & 0 deletions src/map/map.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ class display_data {
std::vector<uint8_t> terrain_id_map;
std::vector<uint8_t> median_terrain_type;
std::vector<uint32_t> province_area;
std::vector<float> province_area_km2;
Copy link
Owner

@schombert schombert Jun 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it might be worthwhile to think about moving some of these values into dcon if they have to be serialized anyways. I believe that when Erik originally wrote this section he was unfamiliar with dcon and so avoided putting anything into it for that reason, not out of some principled choice.

std::vector<uint8_t> diagonal_borders;

// map pixel -> province id
Expand Down
13 changes: 13 additions & 0 deletions src/map/map_data_loading.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "system_state.hpp"
#include "parsers_declarations.hpp"
#include "opengl_wrapper.hpp"
#include "math_fns.hpp"

#ifdef _WIN64

Expand Down Expand Up @@ -474,12 +475,24 @@ void display_data::load_terrain_data(parsers::scenario_building_context& context
void display_data::load_median_terrain_type(parsers::scenario_building_context& context) {
median_terrain_type.resize(context.state.world.province_size() + 1);
province_area.resize(context.state.world.province_size() + 1);
province_area_km2.resize(context.state.world.province_size() + 1);

float R = context.state.defines.alice_globe_mean_radius_km;

std::vector<std::array<int, 64>> terrain_histogram(context.state.world.province_size() + 1, std::array<int, 64>{});
for(int i = size_x * size_y - 1; i-- > 0;) {
auto prov_id = province_id_map[i];
auto terrain_id = terrain_id_map[i];
if(terrain_id < 64)
terrain_histogram[prov_id][terrain_id] += 1;
auto x = i % size_x;
auto y = i / size_x;
// 0.5f is added to shift us to the center of the pixel;
// float s = (((float)x + 0.5f) / (float) size_x) * 2 * math::pi;
float t = (((float)y + 0.5f) / (float) size_y - 0.5f) * math::pi;
auto area_form = R * R * math::cos(t);
auto pixel_size = area_form * (1.f / (float)size_y * math::pi) * (1.f / (float)size_x * 2 * math::pi);
province_area_km2[prov_id] += pixel_size;
}

for(int i = context.state.world.province_size(); i-- > 1;) { // map-id province 0 == the invalid province; we don't need to collect data for it
Expand Down
2 changes: 2 additions & 0 deletions src/parsing/defines.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,8 @@
LUA_DEFINES_LIST_ELEMENT(alice_naval_base_to_colonial_distance_factor, 0.04) \
LUA_DEFINES_LIST_ELEMENT(alice_allow_factories_in_colonies, 0.0) \
LUA_DEFINES_LIST_ELEMENT(alice_always_available_cbs_zero_infamy, 1.0) \
LUA_DEFINES_LIST_ELEMENT(alice_globe_mean_radius_km, 6371.0) \


// scales the needs values so that they are needs per this many pops
// this value was arrived at by looking at farmers: 40'000 farmers produces enough grain to satisfy about 2/3
Expand Down