Skip to content

Commit 91bf8a5

Browse files
committed
buildings: redesigned tracking building id system
1 parent a4c8f43 commit 91bf8a5

11 files changed

+142
-171
lines changed

src/building/building_dock.cpp

+80-99
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "window/building/distribution.h"
2222
#include "graphics/elements/lang_text.h"
2323
#include "city/labor.h"
24+
#include "city/city.h"
2425
#include "js/js_game.h"
2526

2627
building_dock::static_params dock_m;
@@ -32,8 +33,6 @@ void building_dock::static_params::load(archive arch) {
3233
void building_dock::on_create(int orientation) {
3334
data.dock.orientation = orientation;
3435
data.dock.trading_goods.one();
35-
36-
city_buildings_add_dock();
3736
}
3837

3938
void building_dock::on_place(int orientation, int variant) {
@@ -44,7 +43,6 @@ void building_dock::on_place(int orientation, int variant) {
4443
}
4544

4645
void building_dock::on_destroy() {
47-
city_buildings_remove_dock();
4846
}
4947

5048
bool building_dock::can_play_animation() const {
@@ -57,9 +55,8 @@ bool building_dock::can_play_animation() const {
5755
}
5856

5957
void building_dock::update_count() const {
60-
if (num_workers() > 0 && base.has_open_water_access) {
61-
city_buildings_add_working_dock(id());
62-
}
58+
const bool is_active = num_workers() > 0 && base.has_open_water_access;
59+
g_city.buildings.track_building(type(), id(), is_active);
6360
}
6461

6562
void building_dock::update_map_orientation(int orientation) {
@@ -215,147 +212,131 @@ void building_dock::toggle_good_accepted(e_resource r) {
215212
data.dock.trading_goods.flip(r);
216213
}
217214

218-
bool building_dock_accepts_ship(int ship_id, int dock_id) {
219-
building_dock* dock = building_get(dock_id)->dcast_dock();
220-
if (!dock) {
221-
return false;
222-
}
223-
215+
bool building_dock::accepts_ship(int ship_id) {
224216
figure* f = figure_get(ship_id);
225217

226218
empire_city* city = g_empire.city(f->empire_city_id);
227219
const resource_list resources = city_resource_get_available();
228220
int any_acceptance = 0;
229221
for (auto r: resources) {
230222
if (city->sells_resource[r.type] || city->buys_resource[r.type]) {
231-
any_acceptance += dock->is_trade_accepted(r.type) ? 1 : 0;
223+
any_acceptance += is_trade_accepted(r.type) ? 1 : 0;
232224
}
233225
}
234226

235227
return (any_acceptance > 0);
236228
}
237229

230+
tile2i building_dock::moor_tile() const {
231+
vec2i offset;
232+
switch (data.dock.orientation) {
233+
case 0: offset = { 1, -1 }; break;
234+
case 1: offset = { 3, 1 }; break;
235+
case 2: offset = { 1, 3 }; break;
236+
default: offset = { -1, 1 }; break;
237+
}
238+
239+
return tile().shifted(offset.x, offset.y);
240+
}
241+
242+
tile2i building_dock::wait_tile() const {
243+
vec2i offset;
244+
switch (data.dock.orientation) {
245+
case 0: offset = { 2, -2 }; break;
246+
case 1: offset = { 4, 2 }; break;
247+
case 2: offset = { 2, 4 }; break;
248+
default: offset = { -2, 2 }; break;
249+
}
250+
251+
return tile().shifted(offset.x, offset.y);
252+
}
253+
254+
tile2i building_dock::reid_tile() const {
255+
vec2i offset;
256+
switch (data.dock.orientation) {
257+
case 0: offset = { 2, -3 }; break;
258+
case 1: offset = { 5, 2 }; break;
259+
case 2: offset = { 2, 5 }; break;
260+
default: offset = { -3, 2 }; break;
261+
}
262+
263+
return tile().shifted(offset.x, offset.y);
264+
}
265+
238266
building_dest map_get_free_destination_dock(int ship_id) {
239-
if (!city_buildings_has_working_dock())
240-
return {0, tile2i::invalid};
267+
if (!g_city.buildings.has_working_dock()) {
268+
return { 0, tile2i::invalid };
269+
}
241270

242-
int dock_id = 0;
243-
for (int i = 0; i < 10; i++) {
244-
dock_id = city_buildings_get_working_dock(i);
245-
if (!dock_id) {
271+
const auto &docks = g_city.buildings.track_buildings(BUILDING_DOCK);
272+
building_dock* better_dock = nullptr;
273+
for (const auto &bid: docks) {
274+
building_dock *dock = ::building_get(bid)->dcast_dock();
275+
if (!dock || !dock->num_workers()) {
246276
continue;
247277
}
248278

249-
if (!building_dock_accepts_ship(ship_id, dock_id)) {
250-
dock_id = 0;
279+
if (!dock->accepts_ship(ship_id)) {
280+
better_dock = nullptr;
251281
continue;
252282
}
253283

254-
building* dock = building_get(dock_id);
284+
better_dock = dock;
255285
if (!dock->data.dock.trade_ship_id || dock->data.dock.trade_ship_id == ship_id) {
256286
break;
257287
}
258288
}
259289

260290
// BUG: when 10 docks in city, always takes last one... regardless of whether it is free
261-
if (dock_id <= 0)
262-
return {0, tile2i::invalid};
263-
264-
building* dock = building_get(dock_id);
265-
vec2i offset;
266-
switch (dock->data.dock.orientation) {
267-
case 0: offset = {1, -1}; break;
268-
case 1: offset = {3, 1}; break;
269-
case 2: offset = {1, 3}; break;
270-
default: offset = {-1, 1}; break;
291+
if (!better_dock) {
292+
return { 0, tile2i::invalid };
271293
}
272-
tile2i dock_tile = dock->tile.shifted(offset.x, offset.y);
273-
tile2i dest_tile;
274-
map_point_store_result(dock_tile, dest_tile);
275-
dock->data.dock.trade_ship_id = ship_id;
276-
return {dock_id, dest_tile};
294+
295+
tile2i moor_tile = better_dock->moor_tile();
296+
better_dock->data.dock.trade_ship_id = ship_id;
297+
return {better_dock->id(), moor_tile };
277298
}
278299

279300
building_dest map_get_queue_destination_dock(int ship_id) {
280-
if (!city_buildings_has_working_dock())
281-
return {0, tile2i::invalid};
301+
if (!g_city.buildings.has_working_dock()) {
302+
return { 0, tile2i::invalid };
303+
}
282304

283305
// first queue position
284-
for (int i = 0; i < 10; i++) {
285-
int dock_id = city_buildings_get_working_dock(i);
286-
if (!dock_id)
306+
const auto &docks = g_city.buildings.track_buildings(BUILDING_DOCK);
307+
for (const auto &bid : docks) {
308+
building_dock *dock = ::building_get(bid)->dcast_dock();
309+
if (!dock) {
287310
continue;
311+
}
288312

289-
if (!building_dock_accepts_ship(ship_id, dock_id)) {
290-
dock_id = 0;
313+
if (!dock->accepts_ship(ship_id)) {
291314
continue;
292315
}
293-
building* dock = building_get(dock_id);
294-
int dx, dy;
295-
switch (dock->data.dock.orientation) {
296-
case 0:
297-
dx = 2;
298-
dy = -2;
299-
break;
300-
case 1:
301-
dx = 4;
302-
dy = 2;
303-
break;
304-
case 2:
305-
dx = 2;
306-
dy = 4;
307-
break;
308-
default:
309-
dx = -2;
310-
dy = 2;
311-
break;
312-
}
313-
tile2i dock_tile = dock->tile.shifted(dx, dy);
314-
tile2i dest_dock;
315-
map_point_store_result(dock_tile, dest_dock);
316316

317-
if (!map_has_figure_at(dest_dock)) {
318-
return {dock_id, dest_dock};
317+
tile2i wait_tile = dock->wait_tile();
318+
if (!map_has_figure_at(wait_tile)) {
319+
return {dock->id(), wait_tile};
319320
}
320321
}
321322

322323
// second queue position
323-
for (int i = 0; i < 10; i++) {
324-
int dock_id = city_buildings_get_working_dock(i);
325-
if (!dock_id)
324+
building_dock *better_dock = nullptr;
325+
for (const auto &bid : docks) {
326+
building_dock *dock = ::building_get(bid)->dcast_dock();
327+
if (!dock) {
326328
continue;
329+
}
327330

328-
if (!building_dock_accepts_ship(ship_id, dock_id)) {
329-
dock_id = 0;
331+
if (!dock->accepts_ship(ship_id)) {
330332
continue;
331333
}
332-
building* dock = building_get(dock_id);
333-
int dx, dy;
334-
switch (dock->data.dock.orientation) {
335-
case 0:
336-
dx = 2;
337-
dy = -3;
338-
break;
339-
case 1:
340-
dx = 5;
341-
dy = 2;
342-
break;
343-
case 2:
344-
dx = 2;
345-
dy = 5;
346-
break;
347-
default:
348-
dx = -3;
349-
dy = 2;
350-
break;
351-
}
352334

353-
tile2i dock_tile = dock->tile.shifted(dx, dy);
354-
tile2i dest_dock;
355-
map_point_store_result(dock_tile, dest_dock);
356-
if (!map_has_figure_at(dest_dock)) {
357-
return {dock_id, dest_dock};
335+
tile2i reid_tile = dock->reid_tile();
336+
if (!map_has_figure_at(reid_tile)) {
337+
return {dock->id(), reid_tile};
358338
}
359339
}
340+
360341
return {0, tile2i::invalid};
361342
}

src/building/building_dock.h

+4
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ class building_dock : public building_impl {
3232
bool is_trade_accepted(e_resource r);
3333
void toggle_good_accepted(e_resource r);
3434
int count_idle_dockers() const;
35+
bool accepts_ship(int ship_id);
36+
tile2i moor_tile() const;
37+
tile2i wait_tile() const;
38+
tile2i reid_tile() const;
3539

3640
static const static_params &current_params() { return (const static_params &)params(TYPE); }
3741
};

src/building/building_fishing_wharf.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ void building_fishing_wharf::on_place_update_tiles(int orientation, int variant)
5454
void building_fishing_wharf::update_count() const {
5555
if (num_workers() > 0 && base.has_open_water_access) {
5656
const figure *boat = get_figure(BUILDING_SLOT_BOAT);
57-
city_buildings_add_working_wharf(boat->is_valid());
57+
if (!boat->is_valid()) {
58+
g_city.buildings.request_fishing_boat();
59+
}
5860
}
5961

6062
building_increase_industry_count(RESOURCE_FISH, num_workers() > 0);

src/building/building_shipyard.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ void building_shipyard::spawn_figure() {
6868
if (has_figure_of_type(BUILDING_SLOT_BOAT, FIGURE_FISHING_BOAT)) {
6969
return;
7070
}
71+
7172
int pct_workers = worker_percentage();
7273
if (pct_workers >= 100)
7374
data.industry.progress += 10;
@@ -127,7 +128,6 @@ void building_shipyard::on_place_update_tiles(int orientation, int variant) {
127128
}
128129

129130
void building_shipyard::update_count() const {
130-
if (num_workers() > 0 && base.has_open_water_access) {
131-
city_buildings_add_working_shipyard(id());
132-
}
131+
const bool is_active = (num_workers() > 0 && base.has_open_water_access);
132+
g_city.buildings.track_building(type(), id(), is_active);
133133
}

src/building/building_type.h

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include "core/tokenum.h"
44

5+
using building_id = uint16_t;
56
enum e_building_type {
67
BUILDING_NONE = 0,
78
BUILDING_RESERVED_1 = 1,

src/building/count.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ void building_clear_counters() {
2525
memset(&g_count_data, 0, sizeof(count_data_t));
2626
}
2727

28-
void building_increase_type_count(int type, bool active) {
28+
void building_increase_type_count(e_building_type type, bool active) {
2929
g_count_data.buildings[type].total++;
3030
g_count_data.buildings[type].active += (active ? 1 : 0);
3131
}

src/building/count.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ int building_count_active(e_building_type type);
88
int building_count_total(e_building_type type);
99
int building_count_industry_active(e_resource resource);
1010
int building_count_industry_total(e_resource resource);
11-
void building_increase_type_count(int type, bool active);
11+
void building_increase_type_count(e_building_type type, bool active);
1212
void building_increase_industry_count(int resource, bool active);

0 commit comments

Comments
 (0)