Skip to content

Commit b9347ea

Browse files
committed
runtime data for granary moved from building class
1 parent df8424f commit b9347ea

File tree

5 files changed

+57
-42
lines changed

5 files changed

+57
-42
lines changed

src/building/building.h

-3
Original file line numberDiff line numberDiff line change
@@ -212,9 +212,6 @@ class building {
212212
short formation_id;
213213
union impl_data_t {
214214
char data[512] = { 0 };
215-
struct granary_t {
216-
short resource_stored[16];
217-
} granary;
218215
struct {
219216
e_resource resource_id;
220217
} warehouse;

src/building/building_bazaar.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "grid/routing/routing.h"
1616
#include "graphics/image.h"
1717
#include "window/building/distribution.h"
18+
#include "building/building_granary.h"
1819
#include "graphics/graphics.h"
1920
#include "game/game.h"
2021
#include "widget/city/ornaments.h"
@@ -40,7 +41,7 @@ struct resource_data {
4041
return;
4142
}
4243

43-
if (b.data.granary.resource_stored[resource] < 100) {
44+
if (granary->runtime_data().resource_stored[resource] < 100) {
4445
return;
4546
}
4647

src/building/building_granary.cpp

+24-17
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ int building_granary::amount(e_resource resource) const {
6767
return 0;
6868
}
6969

70-
return data.granary.resource_stored[resource];
70+
return runtime_data().resource_stored[resource];
7171
}
7272

7373
bool building_granary::is_accepting(e_resource resource) {
@@ -105,7 +105,8 @@ int building_granary::add_resource(e_resource resource, bool is_produced, int am
105105
return -1;
106106
}
107107

108-
if (data.granary.resource_stored[RESOURCE_NONE] <= 0) {
108+
auto &d = runtime_data();
109+
if (d.resource_stored[RESOURCE_NONE] <= 0) {
109110
return -1; // no space
110111
}
111112

@@ -117,9 +118,9 @@ int building_granary::add_resource(e_resource resource, bool is_produced, int am
117118
city_resource_add_produced_to_granary(amount);
118119
}
119120

120-
int deliverable_amount = std::min<int>(data.granary.resource_stored[RESOURCE_NONE], amount);
121-
data.granary.resource_stored[resource] += deliverable_amount;
122-
data.granary.resource_stored[RESOURCE_NONE] -= deliverable_amount;
121+
int deliverable_amount = std::min<int>(d.resource_stored[RESOURCE_NONE], amount);
122+
d.resource_stored[resource] += deliverable_amount;
123+
d.resource_stored[RESOURCE_NONE] -= deliverable_amount;
123124
return amount - deliverable_amount;
124125
}
125126

@@ -133,19 +134,20 @@ int building_granary::total_stored() const {
133134
}
134135

135136
int building_granary::freespace() const {
136-
return data.granary.resource_stored[RESOURCE_NONE];
137+
return runtime_data().resource_stored[RESOURCE_NONE];
137138
}
138139

139140
int building_granary::remove_resource(e_resource resource, int amount) {
140141
if (amount <= 0) {
141142
return 0;
142143
}
143144

144-
int removed = std::min<int>(data.granary.resource_stored[resource], amount);
145+
auto &d = runtime_data();
146+
int removed = std::min<int>(d.resource_stored[resource], amount);
145147

146148
city_resource_remove_from_granary(resource, removed);
147-
data.granary.resource_stored[resource] -= removed;
148-
data.granary.resource_stored[RESOURCE_NONE] += removed;
149+
d.resource_stored[resource] -= removed;
150+
d.resource_stored[RESOURCE_NONE] += removed;
149151

150152
return amount - removed;
151153
}
@@ -252,7 +254,8 @@ int building_granary_for_storing(tile2i tile, e_resource resource, int distance_
252254
continue;
253255
}
254256

255-
if (granary->data.granary.resource_stored[RESOURCE_NONE] >= ONE_LOAD) {
257+
const auto &d = granary->runtime_data();
258+
if (d.resource_stored[RESOURCE_NONE] >= ONE_LOAD) {
256259
// there is room
257260
int dist = calc_distance_with_penalty(vec2i(granary->tilex() + 1, granary->tiley() + 1), tile, distance_from_entry, granary->distance_from_entry());
258261
if (dist < min_dist) {
@@ -295,7 +298,8 @@ int building_getting_granary_for_storing(tile2i tile, e_resource resource, int d
295298
if (granary->is_getting(resource) || granary->is_empty_all())
296299
continue;
297300

298-
if (granary->data.granary.resource_stored[RESOURCE_NONE] > ONE_LOAD) {
301+
const auto &d = granary->runtime_data();
302+
if (d.resource_stored[RESOURCE_NONE] > ONE_LOAD) {
299303
// there is room
300304
int dist = calc_distance_with_penalty(vec2i(granary->tilex() + 1, granary->tiley() + 1), tile, distance_from_entry, granary->distance_from_entry());
301305
if (dist < min_dist) {
@@ -481,21 +485,22 @@ void building_granary_draw_anim(building &b, vec2i point, tile2i tile, color mas
481485
}
482486

483487
void building_granary::on_create(int orientation) {
484-
data.granary.resource_stored[RESOURCE_NONE] = capacity_stored();
488+
runtime_data().resource_stored[RESOURCE_NONE] = capacity_stored();
485489
base.storage_id = building_storage_create(BUILDING_GRANARY);
486490
}
487491

488492
void building_granary::update_day() {
489493
building_impl::update_day();
490-
data.granary.resource_stored[RESOURCE_NONE] = capacity_stored() - total_stored();
494+
runtime_data().resource_stored[RESOURCE_NONE] = capacity_stored() - total_stored();
491495
}
492496

493497
void building_granary::bind_dynamic(io_buffer *iob, size_t version) {
494498
iob->bind____skip(2);
495499
iob->bind____skip(2);
496500

501+
auto &d = runtime_data();
497502
for (int i = 0; i < RESOURCES_MAX; i++) {
498-
iob->bind(BIND_SIGNATURE_INT16, &data.granary.resource_stored[i]);
503+
iob->bind(BIND_SIGNATURE_INT16, &d.resource_stored[i]);
499504
}
500505
}
501506

@@ -553,10 +558,12 @@ textid building_granary::get_tooltip() const {
553558
void building_granary::draw_stores(vec2i point, color color_mask, painter &ctx) {
554559
int last_spot_filled = 0;
555560
int resources_id = granary_m.anim["resources"].first_img();
561+
562+
const auto &d = runtime_data();
556563
for (int r = 1; r < 9; r++) {
557-
if (data.granary.resource_stored[r] > 0) {
558-
int spots_filled = ceil((float)(data.granary.resource_stored[r] - 199) / (float)400); // number of "spots" occupied by food
559-
if (spots_filled == 0 && data.granary.resource_stored[r] > 0)
564+
if (d.resource_stored[r] > 0) {
565+
int spots_filled = ceil((float)(d.resource_stored[r] - 199) / (float)400); // number of "spots" occupied by food
566+
if (spots_filled == 0 && d.resource_stored[r] > 0)
560567
spots_filled = 1;
561568

562569
for (int spot = last_spot_filled; spot < last_spot_filled + spots_filled; spot++) {

src/building/building_granary.h

+7
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ class building_granary : public building_storage {
2828
building_granary(building &b) : building_storage(b) {}
2929
virtual building_granary *dcast_granary() override { return this; }
3030

31+
struct runtime_data_t {
32+
short resource_stored[16];
33+
};
34+
3135
virtual void on_create(int orientation) override;
3236
virtual void spawn_figure() override;
3337
virtual e_sound_channel_city sound_channel() const override { return SOUND_CHANNEL_CITY_GRANARY; }
@@ -50,6 +54,9 @@ class building_granary : public building_storage {
5054
int capacity_stored() const { return 3200; }
5155
int allow_food_types() const { return 4; }
5256

57+
runtime_data_t &runtime_data() { return *(runtime_data_t *)data.data; }
58+
const runtime_data_t &runtime_data() const { return *(runtime_data_t *)data.data; }
59+
5360
void bless();
5461
granary_task_status determine_worker_task();
5562
void draw_stores(vec2i point, color color_mask, painter &ctx);

src/city/city_resource.cpp

+24-21
Original file line numberDiff line numberDiff line change
@@ -334,31 +334,34 @@ static void calculate_available_food() {
334334

335335
buildings_valid_do([] (building &b) {
336336
b.has_road_access = false;
337-
if (map_has_road_access(b.tile, b.size)) { // map_has_road_access_granary(b->tile.x(), b->tile.y(), 0)
338-
b.has_road_access = true;
339-
int pct_workers = calc_percentage<int>(b.num_workers, model_get_building(b.type)->laborers);
340-
if (pct_workers < 100) {
341-
city_data.resource.granaries.understaffed++;
342-
}
337+
if (!map_has_road_access(b.tile, b.size)) { // map_has_road_access_granary(b->tile.x(), b->tile.y(), 0)
338+
return;
339+
}
343340

344-
int amount_stored = 0;
345-
for (int r = RESOURCE_FOOD_MIN; r < RESOURCES_FOODS_MAX; r++) {
346-
amount_stored += b.data.granary.resource_stored[r];
347-
}
341+
b.has_road_access = true;
342+
int pct_workers = calc_percentage<int>(b.num_workers, model_get_building(b.type)->laborers);
343+
if (pct_workers < 100) {
344+
city_data.resource.granaries.understaffed++;
345+
}
348346

349-
if (pct_workers < 50) {
350-
city_data.resource.granaries.not_operating++;
351-
if (amount_stored > 0)
352-
city_data.resource.granaries.not_operating_with_food++;
347+
int amount_stored = 0;
348+
const auto &granary = b.dcast_granary()->runtime_data();
349+
for (int r = RESOURCE_FOOD_MIN; r < RESOURCES_FOODS_MAX; r++) {
350+
amount_stored += granary.resource_stored[r];
351+
}
353352

354-
} else {
355-
city_data.resource.granaries.operating++;
356-
for (int r = 0; r < RESOURCES_FOODS_MAX; r++)
357-
city_data.resource.granary_food_stored[r] += b.data.granary.resource_stored[r];
353+
if (pct_workers < 50) {
354+
city_data.resource.granaries.not_operating++;
355+
if (amount_stored > 0)
356+
city_data.resource.granaries.not_operating_with_food++;
358357

359-
if (amount_stored >= 100)
360-
tutorial_on_filled_granary(amount_stored);
361-
}
358+
} else {
359+
city_data.resource.granaries.operating++;
360+
for (int r = 0; r < RESOURCES_FOODS_MAX; r++)
361+
city_data.resource.granary_food_stored[r] += granary.resource_stored[r];
362+
363+
if (amount_stored >= 100)
364+
tutorial_on_filled_granary(amount_stored);
362365
}
363366
}, BUILDING_GRANARY);
364367

0 commit comments

Comments
 (0)