forked from Keriew/augustus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwarehouse.c
605 lines (575 loc) · 20.6 KB
/
warehouse.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
#include "warehouse.h"
#include "building/count.h"
#include "building/granary.h"
#include "building/model.h"
#include "building/storage.h"
#include "city/buildings.h"
#include "city/finance.h"
#include "city/military.h"
#include "city/resource.h"
#include "core/calc.h"
#include "core/image.h"
#include "empire/trade_prices.h"
#include "game/tutorial.h"
#include "map/image.h"
#include "map/road_access.h"
#include "scenario/property.h"
int building_warehouse_get_space_info(building *warehouse)
{
int total_loads = 0;
int empty_spaces = 0;
building *space = warehouse;
for (int i = 0; i < 8; i++) {
space = building_next(space);
if (space->id <= 0) {
return 0;
}
if (space->subtype.warehouse_resource_id) {
total_loads += space->loads_stored;
} else {
empty_spaces++;
}
}
if (empty_spaces > 0) {
return WAREHOUSE_ROOM;
} else if (total_loads < 32) {
return WAREHOUSE_SOME_ROOM;
} else {
return WAREHOUSE_FULL;
}
}
int building_warehouse_get_amount(building *warehouse, int resource)
{
int loads = 0;
building *space = warehouse;
for (int i = 0; i < 8; i++) {
space = building_next(space);
if (space->id <= 0) {
return 0;
}
if (space->subtype.warehouse_resource_id && space->subtype.warehouse_resource_id == resource) {
loads += space->loads_stored;
}
}
return loads;
}
int building_warehouse_add_resource(building *b, int resource)
{
if (b->id <= 0) {
return 0;
}
building *main = building_main(b);
if (building_warehouse_is_not_accepting(resource,main)) {
return 0;
}
// check building itself
int find_space = 0;
if (b->subtype.warehouse_resource_id && b->subtype.warehouse_resource_id != resource) {
find_space = 1;
} else if (b->loads_stored >= 4) {
find_space = 1;
} else if (b->type == BUILDING_WAREHOUSE) {
find_space = 1;
}
if (find_space) {
int space_found = 0;
building *space = building_main(b);
for (int i = 0; i < 8; i++) {
space = building_next(space);
if (!space->id) {
return 0;
}
if (!space->subtype.warehouse_resource_id || space->subtype.warehouse_resource_id == resource) {
if (space->loads_stored < 4) {
space_found = 1;
b = space;
break;
}
}
}
if (!space_found) {
return 0;
}
}
city_resource_add_to_warehouse(resource, 1);
b->subtype.warehouse_resource_id = resource;
b->loads_stored++;
tutorial_on_add_to_warehouse();
building_warehouse_space_set_image(b, resource);
return 1;
}
int building_warehouse_remove_resource(building *warehouse, int resource, int amount)
{
// returns amount still needing removal
if (warehouse->type != BUILDING_WAREHOUSE) {
return amount;
}
building *space = warehouse;
for (int i = 0; i < 8; i++) {
if (amount <= 0) {
return 0;
}
space = building_next(space);
if (space->id <= 0) {
continue;
}
if (space->subtype.warehouse_resource_id != resource || space->loads_stored <= 0) {
continue;
}
if (space->loads_stored > amount) {
city_resource_remove_from_warehouse(resource, amount);
space->loads_stored -= amount;
amount = 0;
} else {
city_resource_remove_from_warehouse(resource, space->loads_stored);
amount -= space->loads_stored;
space->loads_stored = 0;
space->subtype.warehouse_resource_id = RESOURCE_NONE;
}
building_warehouse_space_set_image(space, resource);
}
return amount;
}
void building_warehouse_remove_resource_curse(building *warehouse, int amount)
{
if (warehouse->type != BUILDING_WAREHOUSE) {
return;
}
building *space = warehouse;
for (int i = 0; i < 8 && amount > 0; i++) {
space = building_next(space);
if (space->id <= 0 || space->loads_stored <= 0) {
continue;
}
int resource = space->subtype.warehouse_resource_id;
if (space->loads_stored > amount) {
city_resource_remove_from_warehouse(resource, amount);
space->loads_stored -= amount;
amount = 0;
} else {
city_resource_remove_from_warehouse(resource, space->loads_stored);
amount -= space->loads_stored;
space->loads_stored = 0;
space->subtype.warehouse_resource_id = RESOURCE_NONE;
}
building_warehouse_space_set_image(space, resource);
}
}
void building_warehouse_space_set_image(building *space, int resource)
{
int image_id;
if (space->loads_stored <= 0) {
image_id = image_group(GROUP_BUILDING_WAREHOUSE_STORAGE_EMPTY);
} else {
image_id = image_group(GROUP_BUILDING_WAREHOUSE_STORAGE_FILLED) +
4 * (resource - 1) + resource_image_offset(resource, RESOURCE_IMAGE_STORAGE) +
space->loads_stored - 1;
}
map_image_set(space->grid_offset, image_id);
}
void building_warehouse_space_add_import(building *space, int resource)
{
city_resource_add_to_warehouse(resource, 1);
space->loads_stored++;
space->subtype.warehouse_resource_id = resource;
int price = trade_price_buy(resource);
city_finance_process_import(price);
building_warehouse_space_set_image(space, resource);
}
void building_warehouse_space_remove_export(building *space, int resource)
{
city_resource_remove_from_warehouse(resource, 1);
space->loads_stored--;
if (space->loads_stored <= 0) {
space->subtype.warehouse_resource_id = RESOURCE_NONE;
}
int price = trade_price_sell(resource);
city_finance_process_export(price);
building_warehouse_space_set_image(space, resource);
}
void building_warehouses_add_resource(int resource, int amount)
{
int building_id = city_resource_last_used_warehouse();
for (int i = 1; i < MAX_BUILDINGS && amount > 0; i++) {
building_id++;
if (building_id >= MAX_BUILDINGS) {
building_id = 1;
}
building *b = building_get(building_id);
if (b->state == BUILDING_STATE_IN_USE && b->type == BUILDING_WAREHOUSE) {
city_resource_set_last_used_warehouse(building_id);
while (amount && building_warehouse_add_resource(b, resource)) {
amount--;
}
}
}
}
int HALF_WAREHOUSE = 16;
int QUARTER_WAREHOUSE = 8;
int building_warehouse_is_accepting(int resource, building *b)
{
const building_storage *s = building_storage_get(b->storage_id);
int amount = building_warehouse_get_amount(b, resource);
if ((s->resource_state[resource] == BUILDING_STORAGE_STATE_ACCEPTING) ||
(s->resource_state[resource] == BUILDING_STORAGE_STATE_ACCEPTING_HALF && amount < HALF_WAREHOUSE) ||
(s->resource_state[resource] == BUILDING_STORAGE_STATE_ACCEPTING_QUARTER && amount < QUARTER_WAREHOUSE)) {
return 1;
} else {
return 0;
}
}
int building_warehouse_is_getting(int resource, building *b)
{
const building_storage *s = building_storage_get(b->storage_id);
int amount = building_warehouse_get_amount(b, resource);
if ((s->resource_state[resource] == BUILDING_STORAGE_STATE_GETTING) ||
(s->resource_state[resource] == BUILDING_STORAGE_STATE_GETTING_HALF && amount < HALF_WAREHOUSE) ||
(s->resource_state[resource] == BUILDING_STORAGE_STATE_GETTING_QUARTER && amount < QUARTER_WAREHOUSE)) {
return 1;
} else {
return 0;
}
}
int building_warehouse_is_gettable(int resource, building *b)
{
const building_storage *s = building_storage_get(b->storage_id);
if ((s->resource_state[resource] == BUILDING_STORAGE_STATE_GETTING) ||
(s->resource_state[resource] == BUILDING_STORAGE_STATE_GETTING_HALF) ||
(s->resource_state[resource] == BUILDING_STORAGE_STATE_GETTING_QUARTER)) {
return 1;
} else {
return 0;
}
}
int building_warehouse_is_not_accepting(int resource, building *b)
{
return !((building_warehouse_is_accepting(resource,b) || building_warehouse_is_getting(resource,b)));
}
int building_warehouse_get_acceptable_quantity(int resource, building* b)
{
const building_storage* s = building_storage_get(b->storage_id);
switch (s->resource_state[resource]) {
case BUILDING_STORAGE_STATE_ACCEPTING:
case BUILDING_STORAGE_STATE_GETTING:
return 32;
break;
case BUILDING_STORAGE_STATE_ACCEPTING_HALF:
case BUILDING_STORAGE_STATE_GETTING_HALF:
return HALF_WAREHOUSE;
break;
case BUILDING_STORAGE_STATE_ACCEPTING_QUARTER:
case BUILDING_STORAGE_STATE_GETTING_QUARTER:
return QUARTER_WAREHOUSE;
break;
default:
return 0;
}
}
int building_warehouses_remove_resource(int resource, int amount)
{
int amount_left = amount;
int building_id = city_resource_last_used_warehouse();
// first go for non-getting warehouses
for (int i = 1; i < MAX_BUILDINGS && amount_left > 0; i++) {
building_id++;
if (building_id >= MAX_BUILDINGS) {
building_id = 1;
}
building *b = building_get(building_id);
if (b->state == BUILDING_STATE_IN_USE && b->type == BUILDING_WAREHOUSE) {
if (!building_warehouse_is_getting(resource,b)) {
city_resource_set_last_used_warehouse(building_id);
amount_left = building_warehouse_remove_resource(b, resource, amount_left);
}
}
}
// if that doesn't work, take it anyway
for (int i = 1; i < MAX_BUILDINGS && amount_left > 0; i++) {
building_id++;
if (building_id >= MAX_BUILDINGS) {
building_id = 1;
}
building *b = building_get(building_id);
if (b->state == BUILDING_STATE_IN_USE && b->type == BUILDING_WAREHOUSE) {
city_resource_set_last_used_warehouse(building_id);
amount_left = building_warehouse_remove_resource(b, resource, amount_left);
}
}
return amount - amount_left;
}
int building_warehouse_for_storing(int src_building_id, int x, int y, int resource,
int distance_from_entry, int road_network_id, int *understaffed,
map_point *dst)
{
int min_dist = 10000;
int min_building_id = 0;
for (int i = 1; i < MAX_BUILDINGS; i++) {
building *b = building_get(i);
if (b->state != BUILDING_STATE_IN_USE || b->type != BUILDING_WAREHOUSE_SPACE) {
continue;
}
if (!b->has_road_access || b->distance_from_entry <= 0 || b->road_network_id != road_network_id) {
continue;
}
building *building_dst = building_main(b);
if (src_building_id == building_dst->id) {
continue;
}
const building_storage *s = building_storage_get(building_dst->storage_id);
if (building_warehouse_is_not_accepting(resource,building_dst) || s->empty_all) {
continue;
}
int pct_workers = calc_percentage(building_dst->num_workers, model_get_building(building_dst->type)->laborers);
if (pct_workers < 100) {
if (understaffed) {
*understaffed += 1;
}
continue;
}
int dist;
if (b->subtype.warehouse_resource_id == RESOURCE_NONE) { // empty warehouse space
dist = calc_distance_with_penalty(b->x, b->y, x, y, distance_from_entry, b->distance_from_entry);
} else if (b->subtype.warehouse_resource_id == resource && b->loads_stored < 4) {
dist = calc_distance_with_penalty(b->x, b->y, x, y, distance_from_entry, b->distance_from_entry);
} else {
dist = 0;
}
if (dist > 0 && dist < min_dist) {
min_dist = dist;
min_building_id = i;
}
}
building *b = building_main(building_get(min_building_id));
if (b->has_road_access == 1) {
map_point_store_result(b->x, b->y, dst);
} else if (!map_has_road_access(b->x, b->y, 3, dst)) {
return 0;
}
return min_building_id;
}
int building_warehouse_for_getting(building *src, int resource, map_point *dst)
{
int min_dist = 10000;
building *min_building = 0;
for (int i = 1; i < MAX_BUILDINGS; i++) {
building *b = building_get(i);
if (b->state != BUILDING_STATE_IN_USE || b->type != BUILDING_WAREHOUSE) {
continue;
}
if (i == src->id) {
continue;
}
int loads_stored = 0;
building *space = b;
const building_storage *s = building_storage_get(b->storage_id);
for (int t = 0; t < 8; t++) {
space = building_next(space);
if (space->id > 0 && space->loads_stored > 0) {
if (space->subtype.warehouse_resource_id == resource) {
loads_stored += space->loads_stored;
}
}
}
if (loads_stored > 0 && !building_warehouse_is_gettable(resource,b)) {
int dist = calc_distance_with_penalty(b->x, b->y, src->x, src->y,
src->distance_from_entry, b->distance_from_entry);
dist -= 4 * loads_stored;
if (dist < min_dist) {
min_dist = dist;
min_building = b;
}
}
}
if (min_building) {
map_point_store_result(min_building->road_access_x, min_building->road_access_y, dst);
return min_building->id;
} else {
return 0;
}
}
static int determine_granary_accept_foods(int resources[RESOURCE_MAX_FOOD])
{
if (scenario_property_rome_supplies_wheat()) {
return 0;
}
for (int i = 0; i < RESOURCE_MAX_FOOD; i++) {
resources[i] = 0;
}
int can_accept = 0;
for (int i = 1; i < MAX_BUILDINGS; i++) {
building *b = building_get(i);
if (b->state != BUILDING_STATE_IN_USE || b->type != BUILDING_GRANARY || !b->has_road_access) {
continue;
}
int pct_workers = calc_percentage(b->num_workers, model_get_building(b->type)->laborers);
if (pct_workers >= 100 && b->data.granary.resource_stored[RESOURCE_NONE] >= 1200) {
const building_storage *s = building_storage_get(b->storage_id);
if (!s->empty_all) {
for (int r = 0; r < RESOURCE_MAX_FOOD; r++) {
if (!building_warehouse_is_not_accepting(r,b)) {
resources[r]++;
can_accept = 1;
}
}
}
}
}
return can_accept;
}
static int determine_granary_get_foods(int resources[RESOURCE_MAX_FOOD])
{
if (scenario_property_rome_supplies_wheat()) {
return 0;
}
for (int i = 0; i < RESOURCE_MAX_FOOD; i++) {
resources[i] = 0;
}
int can_get = 0;
for (int i = 1; i < MAX_BUILDINGS; i++) {
building *b = building_get(i);
if (b->state != BUILDING_STATE_IN_USE || b->type != BUILDING_GRANARY || !b->has_road_access) {
continue;
}
int pct_workers = calc_percentage(b->num_workers, model_get_building(b->type)->laborers);
if (pct_workers >= 100 && b->data.granary.resource_stored[RESOURCE_NONE] > 100) {
const building_storage *s = building_storage_get(b->storage_id);
if (!s->empty_all) {
for (int r = 0; r < RESOURCE_MAX_FOOD; r++) {
if (building_granary_is_getting(r,b)) {
resources[r]++;
can_get = 1;
}
}
}
}
}
return can_get;
}
static int contains_non_stockpiled_food(building *space, const int *resources)
{
if (space->id <= 0) {
return 0;
}
if (space->loads_stored <= 0) {
return 0;
}
int resource = space->subtype.warehouse_resource_id;
if (city_resource_is_stockpiled(resource)) {
return 0;
}
if (resource == RESOURCE_WHEAT || resource == RESOURCE_VEGETABLES ||
resource == RESOURCE_FRUIT || resource == RESOURCE_MEAT) {
if (resources[resource] > 0) {
return 1;
}
}
return 0;
}
int building_warehouse_determine_worker_task(building *warehouse, int *resource)
{
int pct_workers = calc_percentage(warehouse->num_workers, model_get_building(warehouse->type)->laborers);
if (pct_workers < 50) {
return WAREHOUSE_TASK_NONE;
}
const building_storage *s = building_storage_get(warehouse->storage_id);
building *space;
// get resources
for (int r = RESOURCE_MIN; r < RESOURCE_MAX; r++) {
if (!building_warehouse_is_getting(r,warehouse) || city_resource_is_stockpiled(r)) {
continue;
}
int loads_stored = 0;
space = warehouse;
for (int i = 0; i < 8; i++) {
space = building_next(space);
if (space->id > 0 && space->loads_stored > 0) {
if (space->subtype.warehouse_resource_id == r) {
loads_stored += space->loads_stored;
}
}
}
int room = 0;
space = warehouse;
for (int i = 0; i < 8; i++) {
space = building_next(space);
if (space->id > 0) {
if (space->loads_stored <= 0) {
room += 4;
}
if (space->subtype.warehouse_resource_id == r) {
room += 4 - space->loads_stored;
}
}
}
if (room >= 8 && (loads_stored <= 4 || ((building_warehouse_get_acceptable_quantity(r,space) - loads_stored) >= 4)) && city_resource_count(r) - loads_stored >= 4) {
*resource = r;
return WAREHOUSE_TASK_GETTING;
}
}
// deliver weapons to barracks
if (building_count_active(BUILDING_BARRACKS) > 0 && city_military_has_legionary_legions() &&
!city_resource_is_stockpiled(RESOURCE_WEAPONS)) {
building *barracks = building_get(city_buildings_get_barracks());
if (barracks->loads_stored < 4 &&
warehouse->road_network_id == barracks->road_network_id) {
space = warehouse;
for (int i = 0; i < 8; i++) {
space = building_next(space);
if (space->id > 0 && space->loads_stored > 0 &&
space->subtype.warehouse_resource_id == RESOURCE_WEAPONS) {
*resource = RESOURCE_WEAPONS;
return WAREHOUSE_TASK_DELIVERING;
}
}
}
}
// deliver raw materials to workshops
space = warehouse;
for (int i = 0; i < 8; i++) {
space = building_next(space);
if (space->id > 0 && space->loads_stored > 0) {
if (!city_resource_is_stockpiled(space->subtype.warehouse_resource_id)) {
int workshop_type = resource_to_workshop_type(space->subtype.warehouse_resource_id);
if (workshop_type != WORKSHOP_NONE && city_resource_has_workshop_with_room(workshop_type)) {
*resource = space->subtype.warehouse_resource_id;
return WAREHOUSE_TASK_DELIVERING;
}
}
}
}
// deliver food to getting granary
int granary_resources[RESOURCE_MAX_FOOD];
if (determine_granary_get_foods(granary_resources)) {
space = warehouse;
for (int i = 0; i < 8; i++) {
space = building_next(space);
if (contains_non_stockpiled_food(space, granary_resources)) {
*resource = space->subtype.warehouse_resource_id;
return WAREHOUSE_TASK_DELIVERING;
}
}
}
// deliver food to accepting granary
if (determine_granary_accept_foods(granary_resources) && !scenario_property_rome_supplies_wheat()) {
space = warehouse;
for (int i = 0; i < 8; i++) {
space = building_next(space);
if (contains_non_stockpiled_food(space, granary_resources)) {
*resource = space->subtype.warehouse_resource_id;
return WAREHOUSE_TASK_DELIVERING;
}
}
}
// move goods to other warehouses
if (s->empty_all) {
space = warehouse;
for (int i = 0; i < 8; i++) {
space = building_next(space);
if (space->id > 0 && space->loads_stored > 0) {
*resource = space->subtype.warehouse_resource_id;
return WAREHOUSE_TASK_DELIVERING;
}
}
}
return WAREHOUSE_TASK_NONE;
}