1 #include "mess_hall.h"
2 
3 #include "building/distribution.h"
4 #include "game/resource.h"
5 
6 #define MAX_DISTANCE 40
7 #define MAX_FOOD 1200
8 
get_needed_food(building * mess_hall)9 static int get_needed_food(building *mess_hall)
10 {
11     int food_needed = INVENTORY_FLAG_NONE;
12 
13     if (building_distribution_is_good_accepted(INVENTORY_WHEAT, mess_hall)) {
14         inventory_set(&food_needed, INVENTORY_WHEAT);
15     }
16     if (building_distribution_is_good_accepted(INVENTORY_VEGETABLES, mess_hall)) {
17         inventory_set(&food_needed, INVENTORY_VEGETABLES);
18     }
19     if (building_distribution_is_good_accepted(INVENTORY_FRUIT, mess_hall)) {
20         inventory_set(&food_needed, INVENTORY_FRUIT);
21     }
22     if (building_distribution_is_good_accepted(INVENTORY_MEAT, mess_hall)) {
23         inventory_set(&food_needed, INVENTORY_MEAT);
24     }
25 
26     return food_needed;
27 }
28 
building_mess_hall_get_storage_destination(building * mess_hall)29 int building_mess_hall_get_storage_destination(building *mess_hall)
30 {
31     int food_needed = get_needed_food(mess_hall);
32 
33     if (food_needed == INVENTORY_FLAG_NONE) {
34         return 0;
35     }
36     inventory_storage_info data[INVENTORY_MAX];
37     if (!building_distribution_get_inventory_storages(data, BUILDING_MESS_HALL,
38             mess_hall->road_network_id, mess_hall->road_access_x, mess_hall->road_access_y, MAX_DISTANCE)) {
39         return 0;
40     }
41     // Prefer whichever food we don't have
42     int fetch_inventory = building_distribution_fetch(mess_hall, data, 0, 1, food_needed);
43     if (fetch_inventory != INVENTORY_NONE) {
44         mess_hall->data.market.fetch_inventory_id = fetch_inventory;
45         return data[fetch_inventory].building_id;
46     }
47     // Then prefer smallest stock below baseline stock
48     fetch_inventory = building_distribution_fetch(mess_hall, data, BASELINE_STOCK, 0, food_needed);
49     if (fetch_inventory != INVENTORY_NONE) {
50         mess_hall->data.market.fetch_inventory_id = fetch_inventory;
51         return data[fetch_inventory].building_id;
52     }
53     // All items well stocked: pick food below threshold
54     fetch_inventory = building_distribution_fetch(mess_hall, data, MAX_FOOD, 0, food_needed);
55     if (fetch_inventory != INVENTORY_NONE) {
56         mess_hall->data.market.fetch_inventory_id = fetch_inventory;
57         return data[fetch_inventory].building_id;
58     }
59     return 0;
60 }
61