Skip to content

Commit 70b7c34

Browse files
committed
refactor: moved dock/wharf runtime data from building class
1 parent f11d1ca commit 70b7c34

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+381
-254
lines changed

src/building/building.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,9 @@ void building::new_fill_in_data_for_type(e_building_type _tp, tile2i _tl, int or
9595

9696
// subtype
9797
if (is_house()) {
98-
subtype.house_level = (e_house_level)(type - BUILDING_HOUSE_VACANT_LOT);
98+
data.house.level = (e_house_level)(type - BUILDING_HOUSE_VACANT_LOT);
9999
} else {
100-
subtype.house_level = HOUSE_CRUDE_HUT;
100+
data.house.level = HOUSE_CRUDE_HUT;
101101
}
102102

103103
// unique data
@@ -995,7 +995,7 @@ bvariant building_impl::get_property(const xstring &domain, const xstring &name)
995995
return bvariant();
996996
}
997997

998-
void building_impl::destroy_by_poof(bool clouds) {
998+
void building_impl::destroy_by_poof(bool clouds) {
999999
building_destroy_by_poof(&base, clouds);
10001000
}
10011001

src/building/building.h

+5-18
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ struct tooltip_context;
8787
struct object_info;
8888
struct painter;
8989
struct mouse;
90+
struct water_access_tiles;
9091
class build_planner;
9192

9293
constexpr uint32_t MAX_BUILDINGS = 4000;
@@ -166,10 +167,6 @@ class building {
166167
tile2i tile;
167168
uint8_t orientation;
168169
e_building_type type;
169-
union {
170-
e_house_level house_level;
171-
uint16_t data;
172-
} subtype;
173170
short native_meeting_center_id;
174171
unsigned short road_network_id;
175172
//unsigned short creation_sequence_index;
@@ -214,19 +211,7 @@ class building {
214211
unsigned short tax_collector_id;
215212
short formation_id;
216213
union impl_data_t {
217-
struct dock_t {
218-
short queued_docker_id;
219-
int dock_tiles[2];
220-
sbitarray64 trading_goods;
221-
uint8_t num_ships;
222-
short docker_ids[3];
223-
short trade_ship_id;
224-
uint8_t docker_anim_frame;
225-
e_figure_type process_type;
226-
bool reparing;
227-
short progress;
228-
bool has_fish;
229-
} dock;
214+
char data[512] = { 0 };
230215
struct water_lift_t {
231216
int input_tiles[2];
232217
int output_tiles[2];
@@ -304,6 +289,7 @@ class building {
304289
} entertainment;
305290

306291
struct {
292+
e_house_level level;
307293
uint16_t foods[8];
308294
uint16_t inventory[8];
309295
uint8_t booth_juggler;
@@ -635,6 +621,7 @@ class building_impl {
635621
virtual bool add_resource(e_resource resource, int amount) { return false; }
636622
virtual int get_orientation() const { return base.orientation; }
637623
virtual void on_config_reload() {}
624+
virtual void set_water_access_tiles(const water_access_tiles &tiles) {}
638625

639626
virtual building_farm *dcast_farm() { return nullptr; }
640627
virtual building_brewery *dcast_brewery() { return nullptr; }
@@ -735,7 +722,7 @@ class building_impl {
735722
inline short distance_from_entry() const { return base.distance_from_entry; }
736723
inline int road_network() const { return base.road_network_id; }
737724
inline const animation_t &anim(const xstring &key) const { return params().anim[key]; }
738-
725+
739726
virtual bool is_workshop() const { return false; }
740727
virtual bool is_administration() const { return false; }
741728
virtual bool is_unique_building() const { return false; }

src/building/building_dock.cpp

+47-35
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ void building_dock::static_params::planer_setup_preview_graphics(build_planner &
4444

4545
void building_dock::on_create(int orientation) {
4646
base.orientation = orientation;
47-
data.dock.trading_goods.one();
47+
runtime_data().trading_goods.one();
4848
}
4949

5050
void building_dock::on_place(int orientation, int variant) {
@@ -58,7 +58,7 @@ void building_dock::on_destroy() {
5858
}
5959

6060
bool building_dock::can_play_animation() const {
61-
if (data.dock.num_ships > 0) {
61+
if (runtime_data().num_ships > 0) {
6262
return true;
6363
}
6464

@@ -108,21 +108,22 @@ void building_dock::spawn_figure() {
108108
}
109109
// count existing dockers
110110
int existing_dockers = 0;
111+
auto &d = runtime_data();
111112
for (int i = 0; i < 3; i++) {
112-
if (data.dock.docker_ids[i]) {
113-
if (figure_get(data.dock.docker_ids[i])->type == FIGURE_DOCKER) {
113+
if (d.docker_ids[i]) {
114+
if (figure_get(d.docker_ids[i])->type == FIGURE_DOCKER) {
114115
existing_dockers++;
115116
} else {
116-
data.dock.docker_ids[i] = 0;
117+
d.docker_ids[i] = 0;
117118
}
118119
}
119120
}
120121

121122
if (existing_dockers > max_dockers) {
122123
// too many dockers, poof one of them
123124
for (int i = 2; i >= 0; i--) {
124-
if (data.dock.docker_ids[i]) {
125-
figure_get(data.dock.docker_ids[i])->poof();
125+
if (d.docker_ids[i]) {
126+
figure_get(d.docker_ids[i])->poof();
126127
return;
127128
}
128129
}
@@ -133,8 +134,8 @@ void building_dock::spawn_figure() {
133134
f->action_state = FIGURE_ACTION_132_DOCKER_IDLING;
134135
f->set_home(&base);
135136
for (int i = 0; i < 3; i++) {
136-
if (!data.dock.docker_ids[i]) {
137-
data.dock.docker_ids[i] = f->id;
137+
if (!d.docker_ids[i]) {
138+
d.docker_ids[i] = f->id;
138139
return;
139140
}
140141
}
@@ -143,9 +144,10 @@ void building_dock::spawn_figure() {
143144

144145
void building_dock::on_tick(bool refresh_only) {
145146
auto &anim_wharf = base.anim;
147+
auto &d = runtime_data();
146148
if (anim_wharf.valid()) {
147-
data.dock.docker_anim_frame++;
148-
data.dock.docker_anim_frame %= (anim_wharf.max_frames * anim_wharf.frame_duration);
149+
d.docker_anim_frame++;
150+
d.docker_anim_frame %= (anim_wharf.max_frames * anim_wharf.frame_duration);
149151
}
150152
}
151153

@@ -169,37 +171,39 @@ void building_dock::update_graphic() {
169171

170172
bool building_dock::draw_ornaments_and_animations_height(painter &ctx, vec2i point, tile2i t, color color_mask) {
171173
auto &anim_dockers = base.anim;
172-
174+
auto &d = runtime_data();
173175
if (anim_dockers.valid()) {
174-
int img_id = anim_dockers.start() + (data.dock.docker_anim_frame / anim_dockers.frame_duration) * 4;
176+
int img_id = anim_dockers.start() + (d.docker_anim_frame / anim_dockers.frame_duration) * 4;
175177
ImageDraw::img_generic(ctx, img_id, point + anim_dockers.pos, color_mask, 1.f, ImgFlag_InternalOffset);
176178
}
177179
return false;
178180
}
179181

180182
void building_dock::bind_dynamic(io_buffer *iob, size_t version) {
181-
iob->bind(BIND_SIGNATURE_INT16, &data.dock.queued_docker_id);
182-
iob->bind(BIND_SIGNATURE_INT32, &data.dock.dock_tiles[0]);
183-
iob->bind(BIND_SIGNATURE_INT32, &data.dock.dock_tiles[1]);
184-
iob->bind(BIND_SIGNATURE_UINT64, data.dock.trading_goods.data_ptr());
183+
auto &d = runtime_data();
184+
iob->bind(BIND_SIGNATURE_INT16, &d.queued_docker_id);
185+
iob->bind(BIND_SIGNATURE_INT32, &d.dock_tiles[0]);
186+
iob->bind(BIND_SIGNATURE_INT32, &d.dock_tiles[1]);
187+
iob->bind(BIND_SIGNATURE_UINT64, d.trading_goods.data_ptr());
185188
iob->bind____skip(9);
186-
iob->bind(BIND_SIGNATURE_UINT8, &data.dock.num_ships);
189+
iob->bind(BIND_SIGNATURE_UINT8, &d.num_ships);
187190
iob->bind____skip(2);
188191
iob->bind(BIND_SIGNATURE_INT8, &base.orientation);
189192
iob->bind____skip(3);
190193

191-
iob->bind(BIND_SIGNATURE_INT16, &data.dock.docker_ids[0]);
192-
iob->bind(BIND_SIGNATURE_INT16, &data.dock.docker_ids[1]);
193-
iob->bind(BIND_SIGNATURE_INT16, &data.dock.docker_ids[2]);
194-
195-
iob->bind(BIND_SIGNATURE_INT16, &data.dock.trade_ship_id);
194+
iob->bind(BIND_SIGNATURE_INT16, &d.docker_ids[0]);
195+
iob->bind(BIND_SIGNATURE_INT16, &d.docker_ids[1]);
196+
iob->bind(BIND_SIGNATURE_INT16, &d.docker_ids[2]);
197+
198+
iob->bind(BIND_SIGNATURE_INT16, &d.trade_ship_id);
196199
}
197200

198201
int building_dock::count_idle_dockers() const {
199202
int num_idle = 0;
203+
auto &d = runtime_data();
200204
for (int i = 0; i < 3; i++) {
201-
if (data.dock.docker_ids[i]) {
202-
figure* f = figure_get(data.dock.docker_ids[i]);
205+
if (d.docker_ids[i]) {
206+
figure* f = figure_get(d.docker_ids[i]);
203207
if (f->action_state == FIGURE_ACTION_132_DOCKER_IDLING
204208
|| f->action_state == FIGURE_ACTION_133_DOCKER_IMPORT_QUEUE) {
205209
num_idle++;
@@ -216,25 +220,26 @@ bool map_tile_is_connected_to_open_water(tile2i tile) {
216220
}
217221

218222
void building_dock::unaccept_all_goods() {
219-
data.dock.trading_goods.zeroes(64);
223+
runtime_data().trading_goods.zeroes(64);
220224
}
221225

222226
int building_dock::trader_id() {
223-
return figure_get(data.dock.trade_ship_id)->trader_id;
227+
return figure_get(runtime_data().trade_ship_id)->trader_id;
224228
}
225229

226230
int building_dock::trader_city_id() {
227-
return data.dock.trade_ship_id
228-
? figure_get(data.dock.trade_ship_id)->empire_city_id
231+
auto &d = runtime_data();
232+
return d.trade_ship_id
233+
? figure_get(d.trade_ship_id)->empire_city_id
229234
: 0;
230235
}
231236

232237
bool building_dock::is_trade_accepted(e_resource r) {
233-
return data.dock.trading_goods.is_set(r);
238+
return runtime_data().trading_goods.is_set(r);
234239
}
235240

236241
void building_dock::toggle_good_accepted(e_resource r) {
237-
data.dock.trading_goods.flip(r);
242+
runtime_data().trading_goods.flip(r);
238243
}
239244

240245
bool building_dock::accepts_ship(int ship_id) {
@@ -255,10 +260,16 @@ bool building_dock::accepts_ship(int ship_id) {
255260
void building_dock::highlight_waypoints() {
256261
building_impl::highlight_waypoints();
257262

258-
map_highlight_set(data.dock.dock_tiles[0], ehighligth_green);
259-
map_highlight_set(data.dock.dock_tiles[1], ehighligth_green);
263+
auto &d = runtime_data();
264+
map_highlight_set(d.dock_tiles[0], ehighligth_green);
265+
map_highlight_set(d.dock_tiles[1], ehighligth_green);
260266
}
261267

268+
void building_dock::set_water_access_tiles(const water_access_tiles &tiles) {
269+
auto &d = runtime_data();
270+
d.dock_tiles[0] = tiles.point_a.grid_offset();
271+
d.dock_tiles[1] = tiles.point_b.grid_offset();
272+
}
262273

263274
tile2i building_dock::moor_tile() const {
264275
vec2i offset;
@@ -315,7 +326,8 @@ building_dest map_get_free_destination_dock(int ship_id) {
315326
}
316327

317328
better_dock = dock;
318-
if (!dock->data.dock.trade_ship_id || dock->data.dock.trade_ship_id == ship_id) {
329+
auto &d = dock->runtime_data();
330+
if (!d.trade_ship_id || d.trade_ship_id == ship_id) {
319331
break;
320332
}
321333
}
@@ -326,7 +338,7 @@ building_dest map_get_free_destination_dock(int ship_id) {
326338
}
327339

328340
tile2i moor_tile = better_dock->moor_tile();
329-
better_dock->data.dock.trade_ship_id = ship_id;
341+
better_dock->runtime_data().trade_ship_id = ship_id;
330342
return {better_dock->id(), moor_tile };
331343
}
332344

src/building/building_dock.h

+18
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,20 @@ class building_dock : public building_impl {
66
public:
77
BUILDING_METAINFO(BUILDING_DOCK, building_dock)
88

9+
struct runtime_data_t {
10+
short queued_docker_id;
11+
int dock_tiles[2];
12+
sbitarray64 trading_goods;
13+
uint8_t num_ships;
14+
short docker_ids[3];
15+
short trade_ship_id;
16+
uint8_t docker_anim_frame;
17+
e_figure_type process_type;
18+
bool reparing;
19+
short progress;
20+
bool has_fish;
21+
};
22+
923
building_dock(building &b) : building_impl(b) {}
1024
virtual building_dock *dcast_dock() override { return this; }
1125

@@ -30,6 +44,10 @@ class building_dock : public building_impl {
3044
virtual bool draw_ornaments_and_animations_height(painter &ctx, vec2i point, tile2i tile, color color_mask) override;
3145
virtual void bind_dynamic(io_buffer *iob, size_t version) override;
3246
virtual void highlight_waypoints() override;
47+
virtual void set_water_access_tiles(const water_access_tiles &tiles);
48+
49+
runtime_data_t &runtime_data() { return *(runtime_data_t *)data.data; }
50+
const runtime_data_t &runtime_data() const { return *(runtime_data_t *)data.data; }
3351

3452
void unaccept_all_goods();
3553
int trader_id();

src/building/building_ferry.cpp

+10-2
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,19 @@ void building_ferry::highlight_waypoints() {
6969
map_highlight_set(fpoints.point_b, ehighligth_green);
7070
}
7171

72+
void building_ferry::set_water_access_tiles(const water_access_tiles &tiles) {
73+
auto &d = runtime_data();
74+
d.dock_tiles[0] = tiles.point_a.grid_offset();
75+
d.dock_tiles[1] = tiles.point_b.grid_offset();
76+
}
77+
7278
void building_ferry::bind_dynamic(io_buffer *iob, size_t verrsion) {
79+
auto &d = runtime_data();
80+
7381
iob->bind____skip(88);
7482
iob->bind(BIND_SIGNATURE_UINT8, &base.orientation);
75-
iob->bind(BIND_SIGNATURE_INT32, &data.dock.dock_tiles[0]);
76-
iob->bind(BIND_SIGNATURE_INT32, &data.dock.dock_tiles[1]);
83+
iob->bind(BIND_SIGNATURE_INT32, &d.dock_tiles[0]);
84+
iob->bind(BIND_SIGNATURE_INT32, &d.dock_tiles[1]);
7785
}
7886

7987
bool info_window_ferry::check(object_info &c) {

src/building/building_ferry.h

+8
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ class building_ferry : public building_routeblock {
1212
virtual int planer_construction_update(build_planner &planer, tile2i start, tile2i end) const override;
1313
};
1414

15+
struct runtime_data_t {
16+
int dock_tiles[2];
17+
};
18+
1519
virtual building_ferry *dcast_ferry() override { return this; }
1620
virtual building_routeblock *dcast_routeblock() override { return this; }
1721

@@ -23,6 +27,10 @@ class building_ferry : public building_routeblock {
2327
virtual bool force_draw_top_tile(painter &ctx, tile2i tile, vec2i pixel, color mask) override;
2428
virtual void highlight_waypoints() override;
2529
virtual void bind_dynamic(io_buffer *iob, size_t verrsion) override;
30+
virtual void set_water_access_tiles(const water_access_tiles &tiles) override;
31+
32+
runtime_data_t &runtime_data() { return *(runtime_data_t *)data.data; }
33+
const runtime_data_t &runtime_data() const { return *(runtime_data_t *)data.data; }
2634

2735
virtual bool get_permission(e_permission p) override { return false; }
2836
};

0 commit comments

Comments
 (0)