Skip to content

Commit c0e4ae2

Browse files
committed
refactor: house_population moved from building common logic
1 parent d4ce775 commit c0e4ae2

33 files changed

+174
-132
lines changed

src/building/building.h

-1
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,6 @@ class building {
172172
//unsigned short creation_sequence_index;
173173
short houses_covered;
174174
short percentage_houses_covered;
175-
short house_population;
176175
short distance_from_entry;
177176
tile2i road_access;
178177
short figure_spawn_delay;

src/building/building_debug_properties.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ void game_debug_show_properties_object(pcstr prefix, building *b) {
2828
game_debug_show_property("road_network_id", b->road_network_id);
2929
game_debug_show_property("houses_covered", b->houses_covered);
3030
game_debug_show_property("percentage_houses_covered", b->percentage_houses_covered);
31-
game_debug_show_property("house_population", b->house_population);
31+
//game_debug_show_property("house_population", b->house_population);
3232
//game_debug_show_property("house_population_room", b->house_population_room);
3333
game_debug_show_property("distance_from_entry", b->distance_from_entry);
3434
//game_debug_show_property("house_highest_population", b->house_highest_population);

src/building/building_health.cpp

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include "building_health.h"
22

3-
#include "building/building.h"
3+
#include "building/building_house.h"
44
#include "building/count.h"
55
#include "city/object_info.h"
66
#include "game/resource.h"
@@ -28,9 +28,12 @@ declare_console_command_p(plague_no, game_cheat_noplague);
2828

2929
void game_cheat_noplague(std::istream &is, std::ostream &os) {
3030
buildings_valid_do([&] (building &b) {
31-
if (!b.house_size || b.house_population <= 0) {
31+
auto house = b.dcast_house();
32+
33+
if (!house || house->house_population() <= 0) {
3234
return;
3335
}
36+
3437
building *main = b.main();
3538
main->disease_days = 0;
3639
main->has_plague = false;
@@ -43,10 +46,12 @@ void game_cheat_start_plague(std::istream &is, std::ostream &os) {
4346

4447
int total_population = 0;
4548
buildings_valid_do([&] (building &b) {
46-
if (!b.house_size || b.house_population <= 0) {
49+
auto house = b.dcast_house();
50+
51+
if (!house || house->house_population() <= 0) {
4752
return;
4853
}
49-
total_population += b.house_population;
54+
total_population += house->house_population();
5055
});
5156
g_city.health.start_disease(total_population, true, plague_people);
5257
}

src/building/building_house.cpp

+30-24
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ void building_house::bind_dynamic(io_buffer *iob, size_t version) {
198198
iob->bind(BIND_SIGNATURE_UINT8, &d.is_merged);
199199
iob->bind(BIND_SIGNATURE_UINT8, &d.criminal_active);
200200
iob->bind(BIND_SIGNATURE_INT16, &d.highest_population);
201+
iob->bind(BIND_SIGNATURE_INT16, &d.population);
201202
}
202203

203204
int building_house::get_fire_risk(int value) const {
@@ -217,7 +218,7 @@ bvariant building_house::get_property(const xstring &domain, const xstring &name
217218

218219
void building_house::create_vacant_lot(tile2i tile, int image_id) {
219220
building* b = building_create(BUILDING_HOUSE_VACANT_LOT, tile, 0);
220-
b->house_population = 0;
221+
221222
b->distance_from_entry = 0;
222223
map_building_tiles_add(b->id, b->tile, 1, image_id, TERRAIN_BUILDING);
223224
}
@@ -270,7 +271,7 @@ void building_house::add_population(int num_people) {
270271
}
271272

272273
auto &d = runtime_data();
273-
base.house_population += num_people;
274+
d.population += num_people;
274275
city_population_add(num_people);
275276
base.remove_figure(2);
276277
}
@@ -313,10 +314,10 @@ int16_t building_house::population_room() const {
313314
}
314315

315316
void building_house::change_to_vacant_lot() {
316-
base.house_population = 0;
317+
auto &d = runtime_data();
317318
base.type = BUILDING_HOUSE_VACANT_LOT;
318319

319-
auto &d = runtime_data();
320+
d.population = 0;
320321
d.level = (e_house_level)(base.type - BUILDING_HOUSE_VACANT_LOT);
321322
const house_model &model = static_cast<const house_model&>(params());
322323
int vacant_lot_id = model.anim["house"].first_img();
@@ -336,7 +337,7 @@ void building_house::change_to_vacant_lot() {
336337
}
337338

338339
bool building_house::is_vacant_lot() const {
339-
return base.house_population == 0;
340+
return house_population() == 0;
340341
}
341342

342343
static void prepare_for_merge(int building_id, int num_tiles) {
@@ -354,10 +355,10 @@ static void prepare_for_merge(int building_id, int num_tiles) {
354355
if (house && house->id() != building_id && house->base.house_size) {
355356
g_merge_data.population += house->house_population();
356357
for (int inv = 0; inv < INVENTORY_MAX; inv++) {
357-
auto &d = house->runtime_data();
358-
g_merge_data.inventory[inv] += d.inventory[inv];
359-
g_merge_data.foods[inv] += d.foods[inv];
360-
house->base.house_population = 0;
358+
auto &housed = house->runtime_data();
359+
g_merge_data.inventory[inv] += housed.inventory[inv];
360+
g_merge_data.foods[inv] += housed.foods[inv];
361+
housed.population = 0;
361362
house->base.state = BUILDING_STATE_DELETED_BY_GAME;
362363
}
363364
}
@@ -368,10 +369,10 @@ static void prepare_for_merge(int building_id, int num_tiles) {
368369
void building_house::merge_impl() {
369370
prepare_for_merge(id(), 4);
370371

372+
auto &d = runtime_data();
371373
base.size = base.house_size = 2;
372-
base.house_population += g_merge_data.population;
374+
d.population += g_merge_data.population;
373375

374-
auto &d = runtime_data();
375376
for (int i = 0; i < INVENTORY_MAX; i++) {
376377
d.foods[i] += g_merge_data.foods[i];
377378
d.inventory[i] += g_merge_data.inventory[i];
@@ -673,9 +674,9 @@ e_house_progress building_house::check_evolve_desirability() {
673674

674675
static void create_house_tile(e_building_type type, tile2i tile, int image_id, int population, custom_span<int> inventory, custom_span<int> foods) {
675676
auto house = building_create(type, tile, 0)->dcast_house();
676-
house->base.house_population = population;
677677

678678
auto &housed = house->runtime_data();
679+
housed.population = population;
679680
for (int i = 0; i < INVENTORY_MAX; i++) {
680681
housed.foods[i] = foods[i];
681682
housed.inventory[i] = inventory[i];
@@ -698,8 +699,8 @@ static void split_size2(building* b, e_building_type new_type) {
698699
foods_remainder[i] = housed.foods[i] % 4;
699700
}
700701

701-
int population_per_tile = b->house_population / 4;
702-
int population_remainder = b->house_population % 4;
702+
int population_per_tile = housed.population / 4;
703+
int population_remainder = housed.population % 4;
703704

704705
map_building_tiles_remove(b->id, b->tile);
705706

@@ -708,7 +709,7 @@ static void split_size2(building* b, e_building_type new_type) {
708709
housed.level = (e_house_level)(b->type - BUILDING_HOUSE_VACANT_LOT);
709710
b->size = b->house_size = 1;
710711
housed.is_merged = false;
711-
b->house_population = population_per_tile + population_remainder;
712+
housed.population = population_per_tile + population_remainder;
712713
for (int i = 0; i < INVENTORY_MAX; i++) {
713714
housed.inventory[i] = inventory_per_tile[i] + inventory_remainder[i];
714715
housed.foods[i] = foods_per_tile[i] + foods_remainder[i];
@@ -738,8 +739,8 @@ static void split_size3(building* b) {
738739
foods_remainder[i] = housed.foods[i] % 9;
739740
}
740741

741-
int population_per_tile = b->house_population / 9;
742-
int population_remainder = b->house_population % 9;
742+
int population_per_tile = housed.population / 9;
743+
int population_remainder = housed.population % 9;
743744

744745
map_building_tiles_remove(b->id, b->tile);
745746

@@ -748,7 +749,7 @@ static void split_size3(building* b) {
748749
housed.level = (e_house_level)(b->type - BUILDING_HOUSE_VACANT_LOT);
749750
b->size = b->house_size = 1;
750751
housed.is_merged = false;
751-
b->house_population = population_per_tile + population_remainder;
752+
housed.population = population_per_tile + population_remainder;
752753
for (int i = 0; i < INVENTORY_MAX; i++) {
753754
housed.inventory[i] = inventory_per_tile[i] + inventory_remainder[i];
754755
housed.foods[i] = foods_per_tile[i] + foods_remainder[i];
@@ -811,7 +812,7 @@ void building_house_common_manor::devolve_to_fancy_residence() {
811812
housed.level = (e_house_level)(base.type - BUILDING_HOUSE_VACANT_LOT);
812813
base.size = base.house_size = 2;
813814
housed.is_merged = false;
814-
base.house_population = population_per_tile + population_remainder;
815+
housed.population = population_per_tile + population_remainder;
815816
for (int i = 0; i < INVENTORY_MAX; i++) {
816817
housed.inventory[i] = inventory_per_tile[i] + inventory_remainder[i];
817818
housed.foods[i] = foods_per_tile[i] + foods_remainder[i];
@@ -854,7 +855,7 @@ void building_house_modest_estate::devolve_to_statel_manor() {
854855
housed.level = (e_house_level)(base.type - BUILDING_HOUSE_VACANT_LOT);
855856
base.size = base.house_size = 3;
856857
housed.is_merged = false;
857-
base.house_population = population_per_tile + population_remainder;
858+
housed.population = population_per_tile + population_remainder;
858859
for (int i = 0; i < INVENTORY_MAX; i++) {
859860
housed.inventory[i] = inventory_per_tile[i] + inventory_remainder[i];
860861
}
@@ -910,6 +911,10 @@ void building_house::on_create(int orientation) {
910911
d.health = 100;
911912
d.house_happiness = 50;
912913
d.level = (e_house_level)(type() - BUILDING_HOUSE_VACANT_LOT);
914+
915+
if (d.level == 0) {
916+
d.population = 0;
917+
}
913918
}
914919

915920
void building_house::on_place_checks() {
@@ -1050,7 +1055,7 @@ void building_house_spacious_apartment::expand_to_common_residence() {
10501055
base.type = BUILDING_HOUSE_COMMON_RESIDENCE;
10511056
housed.level = HOUSE_COMMON_RESIDENCE;
10521057
base.size = base.house_size = 2;
1053-
base.house_population += g_merge_data.population;
1058+
housed.population += g_merge_data.population;
10541059
for (int i = 0; i < INVENTORY_MAX; i++) {
10551060
housed.foods[i] += g_merge_data.foods[i];
10561061
housed.inventory[i] += g_merge_data.inventory[i];
@@ -1122,7 +1127,7 @@ void building_house_fancy_residence::expand_to_common_manor() {
11221127
housed.level = HOUSE_COMMON_MANOR;
11231128
base.size = 3;
11241129
base.house_size = 3;
1125-
base.house_population += g_merge_data.population;
1130+
housed.population += g_merge_data.population;
11261131

11271132
for (int i = 0; i < INVENTORY_MAX; i++) {
11281133
housed.foods[i] += g_merge_data.foods[i];
@@ -1193,8 +1198,9 @@ void building_house_stately_manor::expand_to_modest_estate() {
11931198

11941199
base.type = BUILDING_HOUSE_MODEST_ESTATE;
11951200
housed.level = HOUSE_MODEST_ESTATE;
1196-
base.size = base.house_size = 4;
1197-
base.house_population += g_merge_data.population;
1201+
base.size = 4;
1202+
base.house_size = 4;
1203+
housed.population += g_merge_data.population;
11981204
for (int i = 0; i < INVENTORY_MAX; i++) {
11991205
housed.foods[i] += g_merge_data.foods[i];
12001206
housed.inventory[i] += g_merge_data.inventory[i];

src/building/building_house.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class building_house : public building_impl {
2222
uint16_t inventory[8];
2323
uint16_t highest_population;
2424
uint16_t unreachable_ticks;
25+
uint16_t population;
2526
uint8_t is_merged;
2627
uint8_t booth_juggler;
2728
uint8_t bandstand_juggler;
@@ -68,8 +69,8 @@ class building_house : public building_impl {
6869
virtual int get_fire_risk(int value) const override;
6970
virtual bvariant get_property(const xstring &domain, const xstring &name) const override;
7071

71-
inline short house_population() const { return base.house_population; }
72-
inline void change_population(short delta) { base.house_population += delta; }
72+
inline short house_population() const { return runtime_data().population; }
73+
inline void change_population(short delta) { runtime_data().population += delta; }
7374
inline e_house_level house_level() const { return runtime_data().level; }
7475
int16_t population_room() const;
7576
void change_to_vacant_lot();

src/building/construction/clear.cpp

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include "clear.h"
22

3-
#include "building/building.h"
43
#include "building/industry.h"
4+
#include "building/building_house.h"
55
#include "building/building_farm.h"
66
#include "city/city.h"
77
#include "city/warnings.h"
@@ -99,9 +99,10 @@ static int clear_land_confirmed(bool measure_only, clear_confirm_t confirm) {
9999
}
100100
}
101101

102-
if (b->house_size > 0 && b->house_population > 0 && !measure_only) {
103-
figure_create_homeless(b->tile, b->house_population);
104-
b->house_population = 0;
102+
auto house = b->dcast_house();
103+
if (house && house->house_population() > 0 && !measure_only) {
104+
figure_create_homeless(b->tile, house->house_population());
105+
house->runtime_data().population = 0;
105106
}
106107

107108
if (building_is_floodplain_farm(*b) && config_get(CONFIG_GP_CH_SOIL_DEPLETION)) {

src/building/destruction.cpp

+7-3
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,16 @@ static void destroy_on_fire(building* b, bool plagued) {
2525
b->fire_risk = 0;
2626
b->damage_risk = 0;
2727

28-
if (b->house_size && b->house_population > 0) {
29-
city_population_remove_home_removed(b->house_population);
28+
auto house = b->dcast_house();
29+
if (house) {
30+
auto &housed = house->runtime_data();
31+
if (housed.population > 0) {
32+
city_population_remove_home_removed(housed.population);
33+
housed.population = 0;
34+
}
3035
}
3136

3237
//int was_tent = b->house_size && b->data.house.level <= HOUSE_STURDY_HUT;
33-
b->house_population = 0;
3438
b->house_size = 0;
3539
b->state = BUILDING_STATE_DELETED_BY_GAME;
3640
b->output_resource_first_id = RESOURCE_NONE;

src/building/maintenance.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,9 @@ void building_maintenance_check_fire_collapse(void) {
155155
b.fire_risk += model->fire_risk;
156156
int expected_fire_risk = 0;
157157
auto house = b.dcast_house();
158-
if (!b.house_size) {
158+
if (!house) {
159159
expected_fire_risk += 50;
160-
} else if (b.house_population <= 0) {
160+
} else if (house && house->house_population() <= 0) {
161161
expected_fire_risk = 0;
162162
} else if (house && house->house_level() <= HOUSE_COMMON_SHANTY) {
163163
expected_fire_risk += 100;
@@ -204,9 +204,9 @@ void building_maintenance_check_kingdome_access() {
204204
b.distance_from_entry = 0;
205205
housed.unreachable_ticks++;
206206
if (housed.unreachable_ticks > 4) {
207-
if (b.house_population > 0) {
208-
figure_create_homeless(b.tile, b.house_population);
209-
b.house_population = 0;
207+
if (housed.population > 0) {
208+
figure_create_homeless(b.tile, housed.population);
209+
housed.population = 0;
210210
housed.unreachable_ticks = 0;
211211
}
212212
b.state = BUILDING_STATE_UNDO;

src/city/city_buildings.cpp

+5-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "grid/canals.h"
1111
#include "grid/building_tiles.h"
1212
#include "grid/routing/routing_terrain.h"
13+
#include "building/building_house.h"
1314
#include "io/io_buffer.h"
1415

1516
building g_all_buildings[5000];
@@ -188,8 +189,9 @@ void building_update_state(void) {
188189
lands_recalc = true;
189190
building_delete_UNSAFE(b);
190191
} else if (b->state == BUILDING_STATE_RUBBLE) {
191-
if (b->house_size > 0) {
192-
city_population_remove_home_removed(b->house_population);
192+
auto house = b->dcast_house();
193+
if (house && house->base.house_size > 0) {
194+
city_population_remove_home_removed(house->house_population());
193195
}
194196

195197
building_delete_UNSAFE(b);
@@ -248,7 +250,7 @@ io_buffer *iob_buildings = new io_buffer([] (io_buffer *iob, size_t version) {
248250
iob->bind(BIND_SIGNATURE_INT16, &b->houses_covered);
249251
iob->bind(BIND_SIGNATURE_INT16, &b->percentage_houses_covered);
250252

251-
iob->bind(BIND_SIGNATURE_INT16, &b->house_population);
253+
iob->bind____skip(2);
252254
iob->bind____skip(2);
253255
iob->bind(BIND_SIGNATURE_INT16, &b->distance_from_entry);
254256
iob->bind____skip(2);

0 commit comments

Comments
 (0)