Skip to content

Commit

Permalink
refactor: house_size option moved from building common class
Browse files Browse the repository at this point in the history
  • Loading branch information
dalerank committed Mar 3, 2025
1 parent de601d9 commit f8092a8
Show file tree
Hide file tree
Showing 50 changed files with 160 additions and 126 deletions.
22 changes: 1 addition & 21 deletions src/building/building.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,18 +80,6 @@ void building::new_fill_in_data_for_type(e_building_type _tp, tile2i _tl, int or
damage_proof = props.damage_proof;
is_adjacent_to_water = map_terrain_is_adjacent_to_water(tile, size);

// house size
house_size = 0;
if (type >= BUILDING_HOUSE_CRUDE_HUT && type <= BUILDING_HOUSE_SPACIOUS_APARTMENT) {
house_size = 1;
} else if (type >= BUILDING_HOUSE_COMMON_RESIDENCE && type <= BUILDING_HOUSE_FANCY_RESIDENCE) {
house_size = 2;
} else if (type >= BUILDING_HOUSE_COMMON_MANOR && type <= BUILDING_HOUSE_STATELY_MANOR) {
house_size = 3;
} else if (type >= BUILDING_HOUSE_MODEST_ESTATE && type <= BUILDING_HOUSE_PALATIAL_ESTATE) {
house_size = 4;
}

// unique data
output_resource_first_id = RESOURCE_NONE;
dcast()->on_create(orientation);
Expand Down Expand Up @@ -864,11 +852,7 @@ bool building_impl::can_play_animation() const {
}

void building_impl::update_count() const {
if (!base.house_size) {
building_increase_type_count(base.type, base.num_workers > 0);
} else {
building_increase_type_count(base.type, base.house_size > 0);
}
building_increase_type_count(base.type, base.num_workers > 0);
}

void building_impl::draw_normal_anim(painter &ctx, vec2i pixel, tile2i tile, color mask) {
Expand Down Expand Up @@ -947,10 +931,6 @@ void building_impl::highlight_waypoints() { // highlight the 4 routing tiles for
map_highlight_set(base.road_access, ehighligth_red);
}

if (base.house_size) { // building doesn't send roamers
return;
}

int hx, hy;
hx = tilex();
hy = tiley() - 8;
Expand Down
1 change: 0 additions & 1 deletion src/building/building.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,6 @@ class building {
e_building_state state;
uint8_t reserved_id;
uint8_t size;
uint8_t house_size;
tile2i tile;
uint8_t orientation;
short native_meeting_center_id;
Expand Down
2 changes: 1 addition & 1 deletion src/building/building_debug_properties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ void game_debug_show_properties_object(pcstr prefix, building *b) {
game_debug_show_property("state", token::find_name(e_building_state_tokens, b->state));
game_debug_show_property("size", b->size);
//game_debug_show_property("house_is_merged", b->house_is_merged);
game_debug_show_property("house_size", b->house_size);
//game_debug_show_property("house_size", b->house_size);
game_debug_show_property("tile", b->tile);
bstring256 type_name; type_name.printf("%s [%d]", token::find_name(e_building_type_tokens, b->type), b->type);
game_debug_show_property("type", type_name);
Expand Down
67 changes: 47 additions & 20 deletions src/building/building_house.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "building_house.h"

#include "building/building.h"
#include "building/count.h"
#include "city/city.h"
#include "city/warnings.h"
#include "city/population.h"
Expand Down Expand Up @@ -146,6 +146,10 @@ void building_house::on_undo() {
/*nothing*/
}

void building_house::update_count() const {
building_increase_type_count(type(), runtime_data().hsize > 0);
}

void building_house::bind_dynamic(io_buffer *iob, size_t version) {
auto &d = runtime_data();

Expand Down Expand Up @@ -203,12 +207,17 @@ void building_house::bind_dynamic(io_buffer *iob, size_t version) {
iob->bind(BIND_SIGNATURE_UINT16, &d.tax_collector_id);
iob->bind(BIND_SIGNATURE_INT16, &d.tax_income_or_storage);
iob->bind(BIND_SIGNATURE_UINT8, &d.days_without_food);
iob->bind(BIND_SIGNATURE_UINT8, &d.hsize);
}

int building_house::get_fire_risk(int value) const {
return (house_level() == BUILDING_HOUSE_VACANT_LOT && is_vacant_lot()) ? value : 0;
}

void building_house::highlight_waypoints() {
// nothing
}

bvariant building_house::get_property(const xstring &domain, const xstring &name) const {
auto &d = runtime_data();
if (domain == tags().house && name == tags().level_name) {
Expand Down Expand Up @@ -332,7 +341,8 @@ void building_house::change_to_vacant_lot() {
if (is_merged()) {
map_building_tiles_remove(base.id, base.tile);
d.is_merged = 0;
base.size = base.house_size = 1;
d.hsize = 1;
base.size = 1;
map_building_tiles_add(base.id, base.tile, 1, vacant_lot_id, TERRAIN_BUILDING);

building_house::create_vacant_lot(base.tile.shifted(1, 0), vacant_lot_id);
Expand All @@ -359,7 +369,7 @@ static void prepare_for_merge(int building_id, int num_tiles) {
int house_offset = grid_offset + house_tile_offsets(i);
if (map_terrain_is(house_offset, TERRAIN_BUILDING)) {
auto house = building_at(house_offset)->dcast_house();
if (house && house->id() != building_id && house->base.house_size) {
if (house && house->id() != building_id && house->runtime_data().hsize) {
g_merge_data.population += house->house_population();
for (int inv = 0; inv < INVENTORY_MAX; inv++) {
auto &housed = house->runtime_data();
Expand All @@ -377,7 +387,8 @@ void building_house::merge_impl() {
prepare_for_merge(id(), 4);

auto &d = runtime_data();
base.size = base.house_size = 2;
base.size = 2;
d.hsize = 2;
d.population += g_merge_data.population;

for (int i = 0; i < INVENTORY_MAX; i++) {
Expand Down Expand Up @@ -415,7 +426,7 @@ void building_house::merge() {
auto other_house = building_at(tile_offset)->dcast_house();
if (other_house->id() == base.id) {
num_house_tiles++;
} else if (other_house->state() == BUILDING_STATE_VALID && other_house->base.house_size
} else if (other_house->state() == BUILDING_STATE_VALID && other_house->runtime_data().hsize
&& (other_house->house_level() == house_level())
&& !other_house->is_merged()) {
num_house_tiles++;
Expand Down Expand Up @@ -592,7 +603,7 @@ bool building_house::can_expand(int num_tiles) {
auto other_house = building_at(tile_offset)->dcast_house();
if (other_house->id() == id()) {
ok_tiles++;
} else if (other_house->state() == BUILDING_STATE_VALID && other_house->base.house_size) {
} else if (other_house->state() == BUILDING_STATE_VALID && other_house->runtime_data().hsize) {
if (other_house->house_level() <= house_level()) {
ok_tiles++;
}
Expand All @@ -616,7 +627,7 @@ bool building_house::can_expand(int num_tiles) {
auto other_house = building_at(tile_offset)->dcast_house();
if (other_house->id() == id())
ok_tiles++;
else if (other_house->state() == BUILDING_STATE_VALID && other_house->base.house_size) {
else if (other_house->state() == BUILDING_STATE_VALID && other_house->runtime_data().hsize) {
if (other_house->house_level() <= house_level())
ok_tiles++;
}
Expand All @@ -639,7 +650,7 @@ bool building_house::can_expand(int num_tiles) {
auto other_house = building_at(tile_offset)->dcast_house();
if (other_house->id() == id()) {
ok_tiles++;
} else if (other_house->state() == BUILDING_STATE_VALID && other_house->base.house_size) {
} else if (other_house->state() == BUILDING_STATE_VALID && other_house->runtime_data().hsize) {
if (other_house->house_level() <= house_level())
ok_tiles++;
}
Expand Down Expand Up @@ -714,7 +725,8 @@ static void split_size2(building* b, e_building_type new_type) {
// main tile
b->type = new_type;
housed.level = (e_house_level)(b->type - BUILDING_HOUSE_VACANT_LOT);
b->size = b->house_size = 1;
b->size = 1;
housed.hsize = 1;
housed.is_merged = false;
housed.population = population_per_tile + population_remainder;
for (int i = 0; i < INVENTORY_MAX; i++) {
Expand Down Expand Up @@ -754,7 +766,8 @@ static void split_size3(building* b) {
// main tile
b->type = BUILDING_HOUSE_SPACIOUS_APARTMENT;
housed.level = (e_house_level)(b->type - BUILDING_HOUSE_VACANT_LOT);
b->size = b->house_size = 1;
b->size = 1;
housed.hsize = 1;
housed.is_merged = false;
housed.population = population_per_tile + population_remainder;
for (int i = 0; i < INVENTORY_MAX; i++) {
Expand All @@ -781,12 +794,12 @@ void building_house::split(int num_tiles) {
int tile_offset = grid_offset + house_tile_offsets(i);
if (map_terrain_is(tile_offset, TERRAIN_BUILDING)) {
auto other_house = building_at(tile_offset)->dcast_house();
if (other_house->id() != id() && other_house->base.house_size) {
if (other_house->id() != id() && other_house->runtime_data().hsize) {
if (other_house->is_merged()) {
split_size2(&other_house->base, other_house->type());
} else if (other_house->base.house_size == 2) {
} else if (other_house->runtime_data().hsize == 2) {
split_size2(&other_house->base, BUILDING_HOUSE_SPACIOUS_APARTMENT);
} else if (other_house->base.house_size == 3) {
} else if (other_house->runtime_data().hsize == 3) {
split_size3(&other_house->base);
}
}
Expand Down Expand Up @@ -816,8 +829,9 @@ void building_house_common_manor::devolve_to_fancy_residence() {

// main tile
base.type = BUILDING_HOUSE_FANCY_RESIDENCE;
base.size = 2;
housed.level = (e_house_level)(base.type - BUILDING_HOUSE_VACANT_LOT);
base.size = base.house_size = 2;
housed.hsize = 2;
housed.is_merged = false;
housed.population = population_per_tile + population_remainder;
for (int i = 0; i < INVENTORY_MAX; i++) {
Expand Down Expand Up @@ -859,8 +873,9 @@ void building_house_modest_estate::devolve_to_statel_manor() {

// main tile
base.type = BUILDING_HOUSE_STATELY_MANOR;
base.size = 3;
housed.level = (e_house_level)(base.type - BUILDING_HOUSE_VACANT_LOT);
base.size = base.house_size = 3;
housed.hsize = 3;
housed.is_merged = false;
housed.population = population_per_tile + population_remainder;
for (int i = 0; i < INVENTORY_MAX; i++) {
Expand Down Expand Up @@ -922,6 +937,17 @@ void building_house::on_create(int orientation) {
if (d.level == 0) {
d.population = 0;
}

d.hsize = 0;
if (type() >= BUILDING_HOUSE_CRUDE_HUT && type() <= BUILDING_HOUSE_SPACIOUS_APARTMENT) {
d.hsize = 1;
} else if (type() >= BUILDING_HOUSE_COMMON_RESIDENCE && type() <= BUILDING_HOUSE_FANCY_RESIDENCE) {
d.hsize = 2;
} else if (type() >= BUILDING_HOUSE_COMMON_MANOR && type() <= BUILDING_HOUSE_STATELY_MANOR) {
d.hsize = 3;
} else if (type() >= BUILDING_HOUSE_MODEST_ESTATE && type() <= BUILDING_HOUSE_PALATIAL_ESTATE) {
d.hsize = 4;
}
}

void building_house::on_place_checks() {
Expand Down Expand Up @@ -1060,8 +1086,9 @@ void building_house_spacious_apartment::expand_to_common_residence() {
auto &housed = runtime_data();

base.type = BUILDING_HOUSE_COMMON_RESIDENCE;
base.size = 2;
housed.level = HOUSE_COMMON_RESIDENCE;
base.size = base.house_size = 2;
housed.hsize = 2;
housed.population += g_merge_data.population;
for (int i = 0; i < INVENTORY_MAX; i++) {
housed.foods[i] += g_merge_data.foods[i];
Expand Down Expand Up @@ -1131,9 +1158,9 @@ void building_house_fancy_residence::expand_to_common_manor() {

auto &housed = runtime_data();
base.type = BUILDING_HOUSE_COMMON_MANOR;
housed.level = HOUSE_COMMON_MANOR;
base.size = 3;
base.house_size = 3;
housed.level = HOUSE_COMMON_MANOR;
housed.hsize = 3;
housed.population += g_merge_data.population;

for (int i = 0; i < INVENTORY_MAX; i++) {
Expand Down Expand Up @@ -1204,9 +1231,9 @@ void building_house_stately_manor::expand_to_modest_estate() {
auto &housed = runtime_data();

base.type = BUILDING_HOUSE_MODEST_ESTATE;
housed.level = HOUSE_MODEST_ESTATE;
base.size = 4;
base.house_size = 4;
housed.level = HOUSE_MODEST_ESTATE;
housed.hsize = 4;
housed.population += g_merge_data.population;
for (int i = 0; i < INVENTORY_MAX; i++) {
housed.foods[i] += g_merge_data.foods[i];
Expand Down
4 changes: 4 additions & 0 deletions src/building/building_house.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,21 +61,25 @@ class building_house : public building_impl {
uint8_t criminal_active;
uint8_t tax_coverage;
uint8_t days_without_food;
uint8_t hsize;
};

virtual void on_create(int orientation) override;
//virtual void on_place(int orientation, int variant) override;
virtual void on_place_checks() override;
virtual e_sound_channel_city sound_channel() const override { return SOUND_CHANNEL_CITY_STATUE; }
virtual void on_undo() override;
virtual void update_count() const override;
virtual void bind_dynamic(io_buffer *iob, size_t version) override;
virtual bool evolve(house_demands* demands) = 0;
virtual int get_fire_risk(int value) const override;
virtual void highlight_waypoints() override;
virtual bvariant get_property(const xstring &domain, const xstring &name) const override;

inline short house_population() const { return runtime_data().population; }
inline void change_population(short delta) { runtime_data().population += delta; }
inline e_house_level house_level() const { return runtime_data().level; }
inline uint8_t hsize() const { return runtime_data().hsize; }
int16_t population_room() const;
void change_to_vacant_lot();
bool is_vacant_lot() const;
Expand Down
2 changes: 1 addition & 1 deletion src/building/destruction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ static void destroy_on_fire(building* b, bool plagued) {
if (housed.population > 0) {
city_population_remove_home_removed(housed.population);
housed.population = 0;
housed.hsize = 0;
}
}

//int was_tent = b->house_size && b->data.house.level <= HOUSE_STURDY_HUT;
b->house_size = 0;
b->state = BUILDING_STATE_DELETED_BY_GAME;
b->output_resource_first_id = RESOURCE_NONE;
b->output_resource_second_id = RESOURCE_NONE;
Expand Down
14 changes: 8 additions & 6 deletions src/building/house_evolution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -310,28 +310,30 @@ void building_house_determine_evolve_text(building* b, int worst_desirability_bu
}
}

int building_house_determine_worst_desirability_building(building* house) {
int building_house_determine_worst_desirability_building(building* asker) {
int lowest_desirability = 0;
int lowest_building_id = 0;
grid_area area = map_grid_get_area(house->tile, 1, 6);
grid_area area = map_grid_get_area(asker->tile, 1, 6);

auto asker_house = asker->dcast_house();
for (int y = area.tmin.y(), endy = area.tmax.y(); y <= endy; y++) {
for (int x = area.tmin.x(), endx = area.tmax.x(); x <= endx; x++) {
int building_id = map_building_at(MAP_OFFSET(x, y));
if (building_id <= 0)
continue;

building* b = building_get(building_id);
if (b->state != BUILDING_STATE_VALID || building_id == house->id)
auto b = building_get(building_id);
if (b->state != BUILDING_STATE_VALID || building_id == b->id) {
continue;
}

if (!b->house_size || b->type < house->type) {
if (!asker_house->house_level() || b->type < asker->type) {
int des = model_get_building(b->type)->desirability_value;
if (des < 0) {
// simplified desirability calculation
int step_size = model_get_building(b->type)->desirability_step_size;
int range = model_get_building(b->type)->desirability_range;
int dist = calc_maximum_distance(vec2i(x, y), house->tile);
int dist = calc_maximum_distance(vec2i(x, y), asker->tile);
if (dist <= range) {
while (--dist > 1) {
des += step_size;
Expand Down
3 changes: 2 additions & 1 deletion src/building/maintenance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,8 @@ void building_maintenance_check_kingdome_access() {
map_routing_calculate_distances(entry_point);
int problem_grid_offset = 0;
buildings_valid_do( [&problem_grid_offset] (building &b) {
if (b.house_size) {
auto house = b.dcast_house();
if (house && house->hsize() > 0) {
OZZY_PROFILER_SECTION("Game/Run/Tick/Check Road Access/House");
tile2i road_tile = map_closest_road_within_radius(b, 2);
auto &housed = b.dcast_house()->runtime_data();
Expand Down
12 changes: 6 additions & 6 deletions src/city/buildings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,11 +270,11 @@ void city_buildings_t::update_religion_supply_houses() {

for (int yy = area.tmin.y(), endy = area.tmax.y(); yy <= endy; yy++) {
for (int xx = area.tmin.x(), endx = area.tmax.x(); xx <= endx; xx++) {
int building_id = map_building_at(MAP_OFFSET(xx, yy));
int building_id = map_building_at(tile2i(xx, yy));

building *b = building_get(building_id);
if (b->house_size) {
b->dcast_house()->runtime_data().shrine_access = true;
auto house = building_get(building_id)->dcast_house();
if (house) {
house->runtime_data().shrine_access = true;
}
}
}
Expand All @@ -286,8 +286,8 @@ void city_buildings_t::update_religion_supply_houses() {

if (b.is_shrine()) {
shrines.push_back(&b);
} else if (b.house_size) {
b.dcast_house()->runtime_data().shrine_access = false;
} else if (auto house = b.dcast_house(); !!house) {
house->runtime_data().shrine_access = false;
}
}

Expand Down
Loading

0 comments on commit f8092a8

Please sign in to comment.