Skip to content

Commit 904456b

Browse files
committed
refactor: warehouse resource id moved to special section
1 parent f228641 commit 904456b

8 files changed

+58
-50
lines changed

src/building/building.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,6 @@ class building {
169169
union {
170170
e_house_level house_level;
171171
uint16_t data;
172-
e_resource warehouse_resource_id;
173172
// short workshop_type;
174173
short native_meeting_center_id;
175174
} subtype;
@@ -245,6 +244,9 @@ class building {
245244
struct granary_t {
246245
short resource_stored[16];
247246
} granary;
247+
struct {
248+
e_resource resource_id;
249+
} warehouse;
248250
struct guild_t {
249251
uint8_t max_workers;
250252
} guild;

src/building/building_storage_room.cpp

+8-4
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ void building_storage_room::window_info_foreground(object_info &ctx) {
2525
yard()->window_info_foreground(ctx);
2626
}
2727

28+
void building_storage_room::bind_dynamic(io_buffer *iob, size_t version) {
29+
iob->bind(BIND_SIGNATURE_UINT8, &data.warehouse.resource_id);
30+
}
31+
2832
const storage_t *building_storage_room::storage() {
2933
return building_storage_get(base.storage_id);
3034
}
@@ -44,7 +48,7 @@ void building_storage_room::set_image(e_resource resource) {
4448
void building_storage_room::add_import(e_resource resource) {
4549
city_resource_add_to_storageyard(resource, 100);
4650
base.stored_amount_first += 100;
47-
base.subtype.warehouse_resource_id = resource;
51+
data.warehouse.resource_id = resource;
4852

4953
int price = trade_price_buy(resource);
5054
city_finance_process_import(price);
@@ -56,7 +60,7 @@ void building_storage_room::remove_export(e_resource resource) {
5660
city_resource_remove_from_storageyard(resource, 100);
5761
base.stored_amount_first -= 100;
5862
if (base.stored_amount_first <= 0) {
59-
base.subtype.warehouse_resource_id = RESOURCE_NONE;
63+
data.warehouse.resource_id = RESOURCE_NONE;
6064
}
6165

6266
int price = trade_price_sell(resource);
@@ -74,12 +78,12 @@ int building_storage_room::distance_with_penalty(tile2i src, e_resource resource
7478
}
7579

7680
// check for spaces that already has some of the resource, first
77-
if (base.subtype.warehouse_resource_id == resource && base.stored_amount_first < 400) {
81+
if (data.warehouse.resource_id == resource && base.stored_amount_first < 400) {
7882
return calc_distance_with_penalty(tile(), src, distance_from_entry, base.distance_from_entry);
7983
}
8084

8185
// second pass, return the first
82-
if (base.subtype.warehouse_resource_id == RESOURCE_NONE) { // empty warehouse space
86+
if (data.warehouse.resource_id == RESOURCE_NONE) { // empty warehouse space
8387
return calc_distance_with_penalty(tile(), src, distance_from_entry, base.distance_from_entry);
8488
}
8589

src/building/building_storage_room.h

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class building_storage_room : public building_impl {
1919
virtual e_sound_channel_city sound_channel() const override { return SOUND_CHANNEL_CITY_STORAGE_YARD; }
2020
virtual bool can_play_animation() const override { return false; }
2121
virtual int get_fire_risk(int value) const override { return 0; }
22+
virtual void bind_dynamic(io_buffer *iob, size_t version) override;
2223

2324
const storage_t *storage();
2425
building_storage_yard *yard() { return main()->dcast_storage_yard(); }

src/building/building_storage_yard.cpp

+26-26
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ int building_storage_yard::get_space_info() const {
8383
return 0;
8484
}
8585

86-
if (space->subtype.warehouse_resource_id) {
86+
if (space->data.warehouse.resource_id) {
8787
total_amounts += space->stored_amount_first;
8888
} else {
8989
empty_spaces++;
@@ -106,7 +106,7 @@ int building_storage_yard::amount(e_resource resource) const {
106106
if (space->id <= 0)
107107
return 0;
108108

109-
if (space->subtype.warehouse_resource_id && space->subtype.warehouse_resource_id == resource) {
109+
if (space->data.warehouse.resource_id && space->data.warehouse.resource_id == resource) {
110110
total += space->stored_amount_first;
111111
}
112112
}
@@ -126,9 +126,9 @@ int building_storage_yard::freespace(e_resource resource) {
126126
int freespace = 0;
127127
building_storage_room* space = room();
128128
while (space) {
129-
if (!space->base.subtype.warehouse_resource_id) {
129+
if (!space->data.warehouse.resource_id) {
130130
freespace += 400;
131-
} else if(space->base.subtype.warehouse_resource_id == resource) {
131+
} else if(space->data.warehouse.resource_id == resource) {
132132
freespace += (400 - space->base.stored_amount_first);
133133
}
134134
space = space->next_room();
@@ -140,7 +140,7 @@ int building_storage_yard::freespace() const {
140140
int freespace = 0;
141141
const building_storage_room* space = room();
142142
while (space) {
143-
if (!space->base.subtype.warehouse_resource_id) {
143+
if (!space->data.warehouse.resource_id) {
144144
freespace += 400;
145145
}
146146
space = space->next_room();
@@ -158,7 +158,7 @@ int building_storage_yard::add_resource(e_resource resource, bool is_produced, i
158158

159159
// check the initial provided space itself, first
160160
bool look_for_space = false;
161-
if (base.subtype.warehouse_resource_id && base.subtype.warehouse_resource_id != resource) {
161+
if (data.warehouse.resource_id && data.warehouse.resource_id != resource) {
162162
look_for_space = true;
163163
} else if (base.stored_amount_first >= 400) {
164164
look_for_space = true;
@@ -174,7 +174,7 @@ int building_storage_yard::add_resource(e_resource resource, bool is_produced, i
174174
if (look_for_space) {
175175
bool space_found = false;
176176
while (space) {
177-
if (!space->base.subtype.warehouse_resource_id || space->base.subtype.warehouse_resource_id == resource) {
177+
if (!space->data.warehouse.resource_id || space->data.warehouse.resource_id == resource) {
178178
if (space->base.stored_amount_first < 400) {
179179
space_found = true;
180180
break;
@@ -189,7 +189,7 @@ int building_storage_yard::add_resource(e_resource resource, bool is_produced, i
189189
}
190190

191191
city_resource_add_to_storageyard(resource, 1);
192-
space->base.subtype.warehouse_resource_id = resource;
192+
space->data.warehouse.resource_id = resource;
193193
int space_on_tile = 400 - space->base.stored_amount_first;
194194
int unloading_amount = std::min<int>(space_on_tile, amount_left);
195195
space->base.stored_amount_first += unloading_amount;
@@ -214,7 +214,7 @@ int building_storage_yard::remove_resource(e_resource resource, int amount) {
214214
if (amount <= 0)
215215
return 0;
216216

217-
if (space->base.subtype.warehouse_resource_id != resource || space->base.stored_amount_first <= 0) {
217+
if (space->data.warehouse.resource_id != resource || space->base.stored_amount_first <= 0) {
218218
space = space->next_room();
219219
continue;
220220
}
@@ -228,7 +228,7 @@ int building_storage_yard::remove_resource(e_resource resource, int amount) {
228228
city_resource_remove_from_storageyard(resource, space->base.stored_amount_first);
229229
amount -= space->base.stored_amount_first;
230230
space->base.stored_amount_first = 0;
231-
space->base.subtype.warehouse_resource_id = RESOURCE_NONE;
231+
space->data.warehouse.resource_id = RESOURCE_NONE;
232232
}
233233
space->set_image(resource);
234234
space = space->next_room();
@@ -248,7 +248,7 @@ void building_storageyard_remove_resource_curse(building* b, int amount) {
248248
continue;
249249
}
250250

251-
e_resource resource = space->base.subtype.warehouse_resource_id;
251+
e_resource resource = space->data.warehouse.resource_id;
252252
if (space->base.stored_amount_first > amount) {
253253
city_resource_remove_from_storageyard(resource, amount);
254254
space->base.stored_amount_first -= amount;
@@ -257,7 +257,7 @@ void building_storageyard_remove_resource_curse(building* b, int amount) {
257257
city_resource_remove_from_storageyard(resource, space->base.stored_amount_first);
258258
amount -= space->base.stored_amount_first;
259259
space->base.stored_amount_first = 0;
260-
space->base.subtype.warehouse_resource_id = RESOURCE_NONE;
260+
space->data.warehouse.resource_id = RESOURCE_NONE;
261261
}
262262
space->set_image(resource);
263263
space = space->next_room();
@@ -392,7 +392,7 @@ int building_storage_yard::for_getting(e_resource resource, tile2i* dst) {
392392
const storage_t* s = space->storage();
393393
while (space) {
394394
if (space->base.stored_amount_first > 0) {
395-
if (space->base.subtype.warehouse_resource_id == resource)
395+
if (space->data.warehouse.resource_id == resource)
396396
amounts_stored += space->base.stored_amount_first;
397397
}
398398
space = space->next_room();
@@ -463,7 +463,7 @@ static bool contains_non_stockpiled_food(building* space, const resource_list &f
463463
return false;
464464
}
465465

466-
e_resource resource = space->subtype.warehouse_resource_id;
466+
e_resource resource = space->data.warehouse.resource_id;
467467
if (city_resource_is_stockpiled(resource)) {
468468
return false;
469469
}
@@ -490,7 +490,7 @@ storage_worker_task building_storage_yard_determine_getting_up_resources(buildin
490490
room += 4;
491491
}
492492

493-
if (space->base.subtype.warehouse_resource_id == check_resource) { // found a space (tile) with resource on it!
493+
if (space->data.warehouse.resource_id == check_resource) { // found a space (tile) with resource on it!
494494
total_stored += space->base.stored_amount_first; // add loads to total, if any!
495495
room += 400 - space->base.stored_amount_first; // add room to total, if any!
496496
}
@@ -533,7 +533,7 @@ storage_worker_task building_storageyard_deliver_weapons(building *warehouse) {
533533
for (int i = 0; i < 8; i++) {
534534
space = space->next();
535535
if (space->id > 0 && space->stored_amount_first > 0
536-
&& space->subtype.warehouse_resource_id == RESOURCE_WEAPONS) {
536+
&& space->data.warehouse.resource_id == RESOURCE_WEAPONS) {
537537
available += space->stored_amount_first;
538538
}
539539
}
@@ -564,7 +564,7 @@ storage_worker_task building_storageyard_deliver_timber_to_shipyard_school(build
564564
for (int i = 0; i < 8; i++) {
565565
space = space->next();
566566
if (space->id > 0 && space->stored_amount_first > 0
567-
&& space->subtype.warehouse_resource_id == RESOURCE_TIMBER) {
567+
&& space->data.warehouse.resource_id == RESOURCE_TIMBER) {
568568
available += space->stored_amount_first;
569569
}
570570
}
@@ -594,7 +594,7 @@ storage_worker_task building_storageyard_deliver_papyrus_to_scribal_school(build
594594
for (int i = 0; i < 8; i++) {
595595
space = space->next();
596596
if (space->id > 0 && space->stored_amount_first > 0
597-
&& space->subtype.warehouse_resource_id == RESOURCE_PAPYRUS) {
597+
&& space->data.warehouse.resource_id == RESOURCE_PAPYRUS) {
598598
available += space->stored_amount_first;
599599
}
600600
}
@@ -616,17 +616,17 @@ storage_worker_task building_storageyard_deliver_resource_to_workshop(building *
616616
continue;
617617
}
618618

619-
e_resource check_resource = space->subtype.warehouse_resource_id;
619+
e_resource check_resource = space->data.warehouse.resource_id;
620620
if (city_resource_is_stockpiled(check_resource)) {
621621
continue;
622622
}
623623

624624
storage_worker_task task = {STORAGEYARD_TASK_NONE};
625625
buildings_workshop_do([&] (building &b) {
626-
if (!b.need_resource(space->subtype.warehouse_resource_id) || b.need_resource_amount(check_resource) < 100) {
626+
if (!b.need_resource(space->data.warehouse.resource_id) || b.need_resource_amount(check_resource) < 100) {
627627
return;
628628
}
629-
task = {STORAGEYARD_TASK_DELIVERING, space, 100, space->subtype.warehouse_resource_id};
629+
task = {STORAGEYARD_TASK_DELIVERING, space, 100, space->data.warehouse.resource_id};
630630
});
631631

632632
if (task.result == STORAGEYARD_TASK_DELIVERING) {
@@ -648,7 +648,7 @@ storage_worker_task building_storage_yard::deliver_food_to_gettingup_granary(bui
648648
space = space->next();
649649
if (contains_non_stockpiled_food(space, granary_resources)) {
650650
// always one load only for granaries?
651-
return {STORAGEYARD_TASK_DELIVERING, space, 100, space->subtype.warehouse_resource_id};
651+
return {STORAGEYARD_TASK_DELIVERING, space, 100, space->data.warehouse.resource_id};
652652
}
653653
}
654654

@@ -666,7 +666,7 @@ storage_worker_task building_storageyard_deliver_food_to_accepting_granary(build
666666
space = space->next();
667667
if (contains_non_stockpiled_food(space, granary_resources)) {
668668
// always one load only for granaries?
669-
return {STORAGEYARD_TASK_DELIVERING, space, 100, space->subtype.warehouse_resource_id};
669+
return {STORAGEYARD_TASK_DELIVERING, space, 100, space->data.warehouse.resource_id};
670670
}
671671
}
672672
}
@@ -691,11 +691,11 @@ storage_worker_task building_storageyard_deliver_to_monuments(building *warehous
691691
for (int i = 0; i < 8; i++) {
692692
space = space->next();
693693
int available = space->stored_amount_first;
694-
if (space->id <= 0 || !space->subtype.warehouse_resource_id || available <= 0) {
694+
if (space->id <= 0 || !space->data.warehouse.resource_id || available <= 0) {
695695
continue;
696696
}
697697

698-
e_resource resource = space->subtype.warehouse_resource_id;
698+
e_resource resource = space->data.warehouse.resource_id;
699699
if (city_resource_is_stockpiled(resource)) {
700700
continue;
701701
}
@@ -764,7 +764,7 @@ storage_worker_task building_storage_yard::determine_worker_task() {
764764
for (int i = 0; i < 8; i++) {
765765
space = space->next();
766766
if (space->id > 0 && space->stored_amount_first > 0) {
767-
return {STORAGEYARD_TASK_DELIVERING, space, space->stored_amount_first, space->subtype.warehouse_resource_id};
767+
return {STORAGEYARD_TASK_DELIVERING, space, space->stored_amount_first, space->data.warehouse.resource_id};
768768
}
769769
}
770770
}

src/city/city_resource.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -273,9 +273,9 @@ void city_resource_calculate_storageyard_stocks() {
273273
building* warehouse = b->main();
274274
if (warehouse->has_road_access) {
275275
b->has_road_access = warehouse->has_road_access;
276-
if (b->subtype.warehouse_resource_id) {
276+
if (b->data.warehouse.resource_id) {
277277
int amounts = b->stored_amount_first;
278-
int resource = b->subtype.warehouse_resource_id;
278+
int resource = b->data.warehouse.resource_id;
279279
city_data.resource.stored_in_warehouses[resource] += amounts;
280280
city_data.resource.space_in_warehouses[resource] += 400 - amounts;
281281
} else {

src/figure/trader.cpp

+6-5
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,14 @@ e_resource trader_get_buy_resource(building* b, int city_id, int amount) {
9898

9999
building_storage_room* space = warehouse->room();
100100
while (space) {
101-
e_resource resource = space->base.subtype.warehouse_resource_id;
101+
e_resource resource = space->data.warehouse.resource_id;
102102
if (space->base.stored_amount_first >= amount && g_empire.can_export_resource_to_city(city_id, resource)) {
103103
// update stocks
104104
city_resource_remove_from_storageyard(resource, amount);
105105
space->base.stored_amount_first -= amount;
106-
if (space->base.stored_amount_first <= 0)
107-
space->base.subtype.warehouse_resource_id = RESOURCE_NONE;
106+
if (space->base.stored_amount_first <= 0) {
107+
space->data.warehouse.resource_id = RESOURCE_NONE;
108+
}
108109

109110
// update finances
110111
city_finance_process_export(trade_price_sell(resource));
@@ -139,7 +140,7 @@ e_resource trader_get_sell_resource(building* b, int city_id) {
139140
building_storage_room* space = warehouse->room();
140141
while (space) {
141142
if (space->base.stored_amount_first > 0 && space->base.stored_amount_first < 400
142-
&& space->base.subtype.warehouse_resource_id == resource_to_import) {
143+
&& space->data.warehouse.resource_id == resource_to_import) {
143144
space->add_import(resource_to_import);
144145
city_trade_next_caravan_import_resource();
145146
return resource_to_import;
@@ -163,7 +164,7 @@ e_resource trader_get_sell_resource(building* b, int city_id) {
163164
space = warehouse->room();
164165
while (space) {
165166
if (space->base.stored_amount_first < 400
166-
&& space->base.subtype.warehouse_resource_id == resource_to_import) {
167+
&& space->data.warehouse.resource_id == resource_to_import) {
167168
space->add_import(resource_to_import);
168169
return resource_to_import;
169170
}

src/figuretype/figure_docker.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ bool figure_docker::try_import_resource(building* b, e_resource resource, int ci
4444
// try existing storage bay with the same resource
4545
building_storage_room* space = warehouse->room();
4646
while (space) {
47-
if (space->base.stored_amount_first > 0 && space->base.stored_amount_first < 400 && space->base.subtype.warehouse_resource_id == resource) {
47+
if (space->base.stored_amount_first > 0 && space->base.stored_amount_first < 400 && space->data.warehouse.resource_id == resource) {
4848
trade_route.increase_traded(resource, 100);
4949
space->add_import(resource);
5050
return true;
@@ -54,7 +54,7 @@ bool figure_docker::try_import_resource(building* b, e_resource resource, int ci
5454
// try unused storage bay
5555
space = warehouse->room();
5656
while (space) {
57-
if (space->base.subtype.warehouse_resource_id == RESOURCE_NONE) {
57+
if (space->data.warehouse.resource_id == RESOURCE_NONE) {
5858
trade_route.increase_traded(resource, 100);
5959
space->add_import(resource);
6060
return true;
@@ -76,7 +76,7 @@ int figure_docker::try_export_resource(building* b, e_resource resource, int cit
7676

7777
building_storage_room* space = warehouse->room();
7878
while (space) {
79-
if (space->base.stored_amount_first && space->base.subtype.warehouse_resource_id == resource) {
79+
if (space->base.stored_amount_first && space->data.warehouse.resource_id == resource) {
8080
auto &trade_route = g_empire.city(city_id)->get_route();
8181
trade_route.increase_traded(resource, 100);
8282
space->remove_export(resource);
@@ -130,11 +130,11 @@ int figure_docker::get_closest_warehouse_for_import(tile2i pos, int city_id, int
130130
int distance_penalty = 32;
131131
building_storage_room *space = warehouse->room();
132132
while (space) {
133-
if (space->base.subtype.warehouse_resource_id == RESOURCE_NONE) {
133+
if (space->data.warehouse.resource_id == RESOURCE_NONE) {
134134
distance_penalty -= 8;
135135
}
136136

137-
if (space->base.subtype.warehouse_resource_id == resource && space->base.stored_amount_first < 400) {
137+
if (space->data.warehouse.resource_id == resource && space->base.stored_amount_first < 400) {
138138
distance_penalty -= 4;
139139
}
140140

@@ -204,7 +204,7 @@ int figure_docker::get_closest_warehouse_for_export(tile2i pos, int city_id, int
204204
building* space = b;
205205
for (int s = 0; s < 8; s++) {
206206
space = space->next();
207-
if (space->id && space->subtype.warehouse_resource_id == resource && space->stored_amount_first > 0)
207+
if (space->id && space->data.warehouse.resource_id == resource && space->stored_amount_first > 0)
208208
distance_penalty--;
209209
}
210210

0 commit comments

Comments
 (0)