Skip to content

Commit dfd8abc

Browse files
committed
imperial: correct handle request with foods
1 parent 710f65e commit dfd8abc

24 files changed

+111
-175
lines changed

src/building/building_barracks.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ void building_recruiter::on_place_checks() {
176176
return;
177177
}
178178

179-
if (city_resource_count(RESOURCE_WEAPONS) <= 0) {
179+
if (city_resource_warehouse_stored(RESOURCE_WEAPONS) <= 0) {
180180
building_construction_warning_show(WARNING_WEAPONS_NEEDED);
181181
}
182182
}

src/building/building_brewery.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ void building_brewery::on_place_checks() {
5252
return;
5353
}
5454

55-
if (city_resource_count(RESOURCE_BEER) > 0 || city_resource_count(RESOURCE_BARLEY) > 0) {
55+
if (city_resource_warehouse_stored(RESOURCE_BEER) > 0 || city_resource_warehouse_stored(RESOURCE_BARLEY) > 0) {
5656
return;
5757
}
5858

src/building/building_jewels_workshop.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ void building_jewels_workshop::on_place_checks() {
2626
return;
2727
}
2828

29-
if (city_resource_count(RESOURCE_LUXURY_GOODS) > 0 || city_resource_count(RESOURCE_GEMS) > 0) {
29+
if (city_resource_warehouse_stored(RESOURCE_LUXURY_GOODS) > 0 || city_resource_warehouse_stored(RESOURCE_GEMS) > 0) {
3030
return;
3131
}
3232

src/building/building_pottery.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ void building_pottery::on_place_checks() {
3737
return;
3838
}
3939

40-
if (city_resource_count(RESOURCE_POTTERY) > 0 || city_resource_count(RESOURCE_CLAY) > 0) {
40+
if (city_resource_warehouse_stored(RESOURCE_POTTERY) > 0 || city_resource_warehouse_stored(RESOURCE_CLAY) > 0) {
4141
return;
4242
}
4343

src/building/building_scribal_school.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ void building_scribal_school::on_place_checks() {
5252
return;
5353
}
5454

55-
if (city_resource_count(RESOURCE_PAPYRUS) > 0) {
55+
if (city_resource_warehouse_stored(RESOURCE_PAPYRUS) > 0) {
5656
return;
5757
}
5858

src/building/building_storage_yard.cpp

+1-35
Original file line numberDiff line numberDiff line change
@@ -298,40 +298,6 @@ bool building_storage_yard::draw_ornaments_and_animations_height(painter &ctx, v
298298
return true;
299299
}
300300

301-
int building_storageyards_remove_resource(e_resource resource, int amount) {
302-
int amount_left = amount;
303-
int building_id = city_resource_last_used_storageyard();
304-
// first go for non-getting warehouses
305-
for (int i = 1; i < MAX_BUILDINGS && amount_left > 0; i++) {
306-
building_id++;
307-
if (building_id >= MAX_BUILDINGS) {
308-
building_id = 1;
309-
}
310-
311-
building_storage_yard *warehouse = building_get(building_id)->dcast_storage_yard();
312-
if (warehouse && warehouse->is_valid()) {
313-
if (!warehouse->is_getting(resource)) {
314-
city_resource_set_last_used_storageyard(building_id);
315-
amount_left = warehouse->remove_resource(resource, amount_left);
316-
}
317-
}
318-
}
319-
// if that doesn't work, take it anyway
320-
for (int i = 1; i < MAX_BUILDINGS && amount_left > 0; i++) {
321-
building_id++;
322-
if (building_id >= MAX_BUILDINGS) {
323-
building_id = 1;
324-
}
325-
326-
building_storage_yard* warehouse = building_get(building_id)->dcast_storage_yard();
327-
if (warehouse && warehouse->is_valid()) {
328-
city_resource_set_last_used_storageyard(building_id);
329-
amount_left = warehouse->remove_resource(resource, amount_left);
330-
}
331-
}
332-
return amount - amount_left;
333-
}
334-
335301
int building_storage_yard_for_storing(tile2i tile, e_resource resource, int distance_from_entry, int road_network_id, int* understaffed, tile2i &dst) {
336302
int min_dist = 10000;
337303
int min_building_id = 0;
@@ -506,7 +472,7 @@ storage_worker_task building_storage_yard_determine_getting_up_resources(buildin
506472
int lacking = requesting - total_stored;
507473

508474
// determine if there's enough room for more to accept, depending on "get up to..." settings!
509-
if (room >= 0 && lacking > 0 && city_resource_count(check_resource) - total_stored > 0) {
475+
if (room >= 0 && lacking > 0 && city_resource_warehouse_stored(check_resource) - total_stored > 0) {
510476
if (!warehouse->for_getting(check_resource, 0)) { // any other place contain this resource..?
511477
continue;
512478
}

src/building/building_storage_yard.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -74,5 +74,4 @@ building_storage_yard *storage_yard_cast(building *b);
7474

7575
int building_storage_yard_for_storing(tile2i tile, e_resource resource, int distance_from_entry, int road_network_id, int *understaffed, tile2i &dst);
7676
void building_storageyard_remove_resource_curse(building* warehouse, int amount);
77-
void building_storageyards_add_resource(e_resource resource, int amount);
78-
int building_storageyards_remove_resource(e_resource resource, int amount);
77+
void building_storageyards_add_resource(e_resource resource, int amount);

src/building/building_weaponsmith.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ void building_weaponsmith::on_place_checks() {
3535
return;
3636
}
3737

38-
if (city_resource_count(RESOURCE_WEAPONS) <= 0 && city_resource_count(RESOURCE_COPPER) <= 0) {
38+
if (city_resource_warehouse_stored(RESOURCE_WEAPONS) <= 0 && city_resource_warehouse_stored(RESOURCE_COPPER) <= 0) {
3939
building_construction_warning_show(WARNING_IRON_NEEDED);
4040

4141
if (g_city.can_produce_resource(RESOURCE_COPPER))

src/building/building_weaver.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ void building_weaver::on_place_checks() {
2626
return;
2727
}
2828

29-
if (city_resource_count(RESOURCE_FLAX) > 0) {
29+
if (city_resource_warehouse_stored(RESOURCE_FLAX) > 0) {
3030
return;
3131
}
3232

src/building/building_workshop_papyrus.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ void building_papyrus_maker::on_place_checks() {
5151
return;
5252
}
5353

54-
if (city_resource_count(RESOURCE_REEDS) > 0) {
54+
if (city_resource_warehouse_stored(RESOURCE_REEDS) > 0) {
5555
return;
5656
}
5757

src/building/construction/build_planner.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -934,7 +934,7 @@ void BuildPlanner::update_requirements_check() {
934934
/////// special requirements
935935
//
936936
if (special_flags & PlannerFlags::Resources) {
937-
if (city_resource_count((e_resource)additional_req_param1) < additional_req_param2) {
937+
if (city_resource_warehouse_stored((e_resource)additional_req_param1) < additional_req_param2) {
938938
immediate_warning_id = additional_req_param3;
939939
can_place = CAN_NOT_BUT_GREEN;
940940
}
@@ -1470,7 +1470,7 @@ void BuildPlanner::construction_finalize() { // confirm final placement
14701470

14711471
// consume resources for specific buildings (e.g. marble, granite)
14721472
if (special_flags & PlannerFlags::Resources) {
1473-
building_storageyards_remove_resource((e_resource)additional_req_param1, additional_req_param2);
1473+
city_storageyards_remove_resource((e_resource)additional_req_param1, additional_req_param2);
14741474
}
14751475

14761476
// finally, go over the rest of the stuff for all building types

src/city/city_festival.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ void city_festival_t::schedule() {
6767
city_finance_process_requests_and_festivals(cost);
6868

6969
if (selected.size == FESTIVAL_GRAND) {
70-
building_storageyards_remove_resource(RESOURCE_BEER, grand_alcohol);
70+
city_storageyards_remove_resource(RESOURCE_BEER, grand_alcohol);
7171
}
7272
}
7373

src/city/city_resource.cpp

+59-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "building/industry.h"
55
#include "building/model.h"
66
#include "building/building_storage_yard.h"
7+
#include "building/building_granary.h"
78
#include "city/city.h"
89
#include "city/warning.h"
910
#include "graphics/window.h"
@@ -23,10 +24,22 @@ struct available_data_t {
2324
available_data_t g_available_data;
2425

2526
static auto &city_data = g_city;
26-
int city_resource_count(e_resource resource) {
27+
int city_resource_warehouse_stored(e_resource resource) {
2728
return city_data.resource.stored_in_warehouses[resource];
2829
}
2930

31+
int city_resource_granary_stored(e_resource resource) {
32+
if (!resource_is_food(resource)) {
33+
return 0;
34+
}
35+
return city_data.resource.granary_food_stored[resource];
36+
}
37+
38+
int city_resource_storages_stored(e_resource resource) {
39+
return city_resource_warehouse_stored(resource)
40+
+ city_resource_granary_stored(resource);
41+
}
42+
3043
const resource_list &city_resource_get_available() {
3144
return g_available_data.resources;
3245
}
@@ -183,15 +196,60 @@ void city_resource_add_produced_to_granary(int amount) {
183196
void city_resource_remove_from_granary(int food, int amount) {
184197
city_data.resource.granary_food_stored[food] -= amount;
185198
}
199+
186200
void city_resource_add_to_storageyard(e_resource resource, int amount) {
187201
city_data.resource.space_in_warehouses[resource] -= amount;
188202
city_data.resource.stored_in_warehouses[resource] += amount;
189203
}
204+
190205
void city_resource_remove_from_storageyard(e_resource resource, int amount) {
191206
city_data.resource.space_in_warehouses[resource] += amount;
192207
city_data.resource.stored_in_warehouses[resource] -= amount;
193208
}
194209

210+
int city_storageyards_remove_resource(e_resource resource, int amount) {
211+
int amount_left = amount;
212+
213+
// first go for non-getting warehouses
214+
buildings_valid_do([&] (building &b) {
215+
building_storage_yard *warehouse = b.dcast_storage_yard();
216+
if (warehouse && warehouse->is_valid() && !warehouse->is_getting(resource)) {
217+
amount_left = warehouse->remove_resource(resource, amount_left);
218+
}
219+
});
220+
// if that doesn't work, take it anyway
221+
buildings_valid_do([&] (building &b) {
222+
building_storage_yard *warehouse = b.dcast_storage_yard();
223+
if (warehouse && warehouse->is_valid()) {
224+
amount_left = warehouse->remove_resource(resource, amount_left);
225+
}
226+
});
227+
228+
return amount - amount_left;
229+
}
230+
231+
int city_granaries_remove_resource(e_resource resource, int amount) {
232+
int amount_left = amount;
233+
234+
// first go for non-getting warehouses
235+
buildings_valid_do([&] (building &b) {
236+
building_granary *granary = b.dcast_granary();
237+
if (granary && granary->is_valid() && !granary->is_getting(resource)) {
238+
amount_left = granary->remove_resource(resource, amount_left);
239+
}
240+
});
241+
// if that doesn't work, take it anyway
242+
buildings_valid_do([&] (building &b) {
243+
building_granary *granary = b.dcast_granary();
244+
if (granary && granary->is_valid()) {
245+
amount_left = granary->remove_resource(resource, amount_left);
246+
}
247+
});
248+
249+
return amount - amount_left;
250+
}
251+
252+
195253
void city_resource_calculate_storageyard_stocks() {
196254
OZZY_PROFILER_SECTION("Game/Run/Tick/Warehouse Stocks Update");
197255
for (int i = 0; i < RESOURCES_MAX; i++) {

src/city/city_resource.h

+6-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
#include <iosfwd>
99
#include <string>
1010

11-
int city_resource_count(e_resource resource);
11+
int city_resource_warehouse_stored(e_resource resource);
12+
int city_resource_granary_stored(e_resource resource);
13+
int city_resource_storages_stored(e_resource resource);
1214

1315
const resource_list &city_resource_get_available();
1416
const resource_list &city_resource_get_available_foods();
@@ -45,6 +47,9 @@ void city_resource_consume_food();
4547
void city_resource_add_items(e_resource res, int amount);
4648
void city_resource_was_added_warning(e_resource res);
4749

50+
int city_storageyards_remove_resource(e_resource resource, int amount);
51+
int city_granaries_remove_resource(e_resource resource, int amount);
52+
4853
template<e_resource R>
4954
void game_cheat_add_resource(std::istream &is, std::ostream &os) {
5055
std::string args; is >> args;

src/empire/empire.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ bool empire_t::can_export_resource_to_city(int city_id, e_resource resource) {
227227
return false;
228228
}
229229

230-
if (city_resource_count(resource) <= city_resource_trading_amount(resource)) {
230+
if (city_resource_warehouse_stored(resource) <= city_resource_trading_amount(resource)) {
231231
// stocks too low
232232
return false;
233233
}
@@ -265,7 +265,7 @@ bool empire_t::can_import_resource_from_city(int city_id, e_resource resource) {
265265
return false;
266266
}
267267

268-
int in_stock = city_resource_count(resource);
268+
int in_stock = city_resource_warehouse_stored(resource);
269269
int max_in_stock = 0;
270270

271271
if (status == TRADE_STATUS_IMPORT_AS_NEEDED) {

src/game/resource.cpp

-6
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,6 @@ const resource_list resource_list::foods = resource_list(RESOURCE_GRAIN, RESOURC
6666
const resource_list resource_list::all = resource_list(RESOURCE_GRAIN, RESOURCE_MARBLE);
6767
const resource_list resource_list::values = resource_list(RESOURCE_NONE, RESOURCE_MARBLE);
6868

69-
int resource_is_food(int resource) {
70-
return resource == RESOURCE_GRAIN || resource == RESOURCE_MEAT || resource == RESOURCE_LETTUCE
71-
|| resource == RESOURCE_CHICKPEAS || resource == RESOURCE_POMEGRANATES || resource == RESOURCE_FIGS
72-
|| resource == RESOURCE_FISH || resource == RESOURCE_GAMEMEAT;
73-
}
74-
7569
e_resource get_raw_resource(e_resource resource) {
7670
switch (resource) {
7771
case RESOURCE_STRAW: return RESOURCE_GRAIN;

src/game/resource.h

+9-1
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,14 @@ bool resource_type_any_of(e_resource type, Args ... args) {
172172
return (std::find(std::begin(types), std::end(types), type) != std::end(types));
173173
}
174174

175-
int resource_is_food(int resource);
175+
inline bool resource_is_food(e_resource resource) {
176+
return resource_type_any_of
177+
(
178+
resource,
179+
RESOURCE_GRAIN, RESOURCE_MEAT, RESOURCE_LETTUCE,
180+
RESOURCE_CHICKPEAS, RESOURCE_POMEGRANATES, RESOURCE_FIGS,
181+
RESOURCE_FISH, RESOURCE_GAMEMEAT
182+
);
183+
}
176184

177185
e_resource get_raw_resource(e_resource resource);

src/game/tutorial.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -346,24 +346,24 @@ void tutorial_on_filled_granary(int quantity) {
346346
}
347347

348348
void tutorial_check_resources_on_storageyard() {
349-
if (!g_tutorials_flags.tutorial_3.pottery_made && city_resource_count(RESOURCE_POTTERY) >= 1) {
349+
if (!g_tutorials_flags.tutorial_3.pottery_made && city_resource_warehouse_stored(RESOURCE_POTTERY) >= 1) {
350350
g_tutorials_flags.tutorial_3.pottery_made = true;
351351
g_tutorials_flags.tutorial_3.pottery_made_year = gametime().year;
352352
//building_menu_update(BUILDSET_NORMAL);
353353
post_message(MESSAGE_TUTORIAL_TRADE);
354-
} else if (!g_tutorials_flags.tutorial_3.pottery_made && city_resource_count(RESOURCE_POTTERY) >= 2) {
354+
} else if (!g_tutorials_flags.tutorial_3.pottery_made && city_resource_warehouse_stored(RESOURCE_POTTERY) >= 2) {
355355
g_tutorials_flags.tutorial_3.pottery_made = true;
356356
building_menu_update(tutorial_stage.tutorial_gardens);
357357
post_message(MESSAGE_TUTORIAL_MUNICIPAL_STRUCTURES);
358-
} else if (!g_tutorials_flags.tutorial_4.beer_made && city_resource_count(RESOURCE_BEER) >= 3) {
358+
} else if (!g_tutorials_flags.tutorial_4.beer_made && city_resource_warehouse_stored(RESOURCE_BEER) >= 3) {
359359
g_tutorials_flags.tutorial_4.beer_made = true;
360360
building_menu_update(tutorial_stage.tutorial_finance);
361361
post_message(MESSAGE_TUTORIAL_FINANCES);
362-
} if (!g_tutorials_flags.tutorial_5.papyrus_made && city_resource_count(RESOURCE_PAPYRUS) >= 1) {
362+
} if (!g_tutorials_flags.tutorial_5.papyrus_made && city_resource_warehouse_stored(RESOURCE_PAPYRUS) >= 1) {
363363
g_tutorials_flags.tutorial_5.papyrus_made = 1;
364364
building_menu_update(tutorial_stage.tutorial_trading);
365365
post_message(MESSAGE_TUTORIAL_TRADE_WITH_OTHER_CITIES);
366-
} if (!g_tutorials_flags.tutorial_5.bricks_bought && city_resource_count(RESOURCE_BRICKS) >= 1) {
366+
} if (!g_tutorials_flags.tutorial_5.bricks_bought && city_resource_warehouse_stored(RESOURCE_BRICKS) >= 1) {
367367
g_tutorials_flags.tutorial_5.bricks_bought = 1;
368368
building_menu_update(tutorial_stage.tutorial_monuments);
369369
post_message(MESSAGE_TUTORIAL_MONUMENTS);

src/platform/renderer_filters.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ void config_load_filter_properties(bool header) {
154154
void platform_render_init_filters() {}
155155
bool platform_render_support_filters() { return false; }
156156
bool platform_render_any_filter_active() { return false; }
157-
void platform_render_proceed_filter(int w, int h, int format, const std::vector<uint8_t> &pixels, SDL_Texture *filter_texture) {}
157+
void platform_render_proceed_filter(int w, int h, int format, const std::vector<uint8_t> &pixels, SDL_Texture *filter_texture, SDL_Texture *source_texture) {}
158158
void platform_render_init_filter_context() {}
159159
void config_load_filter_properties(bool) {}
160160

src/scenario/request.cpp

+6-3
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ void scenario_request_handle(event_ph_t &event, int caller_event_id, e_event_act
8080
break;
8181

8282
case e_event_state_in_progress:
83-
if (!event.can_comply_dialog_shown && city_resource_count(request.resource) >= request.amount) {
83+
if (!event.can_comply_dialog_shown && city_resource_storages_stored(request.resource) >= request.amount) {
8484
event.can_comply_dialog_shown = true;
8585
city_message &message = city_message_post(true, MESSAGE_REQUEST_CAN_COMPLY, event.event_id, 0);
8686
message.req_amount = request.resource_amount();
@@ -155,10 +155,13 @@ void scenario_request_dispatch(int id) {
155155
city_finance_process_requests_and_festivals(request.amount);
156156
} else if (request.resource == RESOURCE_TROOPS) {
157157
city_population_remove_for_troop_request(request.amount);
158-
building_storageyards_remove_resource(RESOURCE_WEAPONS, request.amount);
158+
city_storageyards_remove_resource(RESOURCE_WEAPONS, request.amount);
159159
} else {
160160
int amount = request.resource_amount();
161-
building_storageyards_remove_resource(request.resource, amount);
161+
amount -= city_storageyards_remove_resource(request.resource, amount);
162+
if (amount > 0) {
163+
city_granaries_remove_resource(request.resource, amount);
164+
}
162165
}
163166
}
164167

0 commit comments

Comments
 (0)