1 #include "trade.h"
2 
3 #include "building/caravanserai.h"
4 #include "building/count.h"
5 #include "building/lighthouse.h"
6 #include "building/monument.h"
7 #include "city/constants.h"
8 #include "core/config.h"
9 #include "city/data_private.h"
10 #include "empire/city.h"
11 #include "figure/figure.h"
12 
city_trade_update(void)13 void city_trade_update(void)
14 {
15     city_data.trade.num_sea_routes = 0;
16     city_data.trade.num_land_routes = 0;
17     // Wine types
18     city_data.resource.wine_types_available = building_count_industry_total(RESOURCE_WINE) > 0 ? 1 : 0;
19     if (building_monument_gt_module_is_active(VENUS_MODULE_1_DISTRIBUTE_WINE)) {
20         city_data.resource.wine_types_available += 1;
21     }
22 
23     city_data.resource.wine_types_available += empire_city_count_wine_sources();
24 
25     // Update trade problems
26     if (city_data.trade.land_trade_problem_duration > 0) {
27         city_data.trade.land_trade_problem_duration--;
28         if (building_caravanserai_is_fully_functional()) {
29             city_data.trade.land_trade_problem_duration--;
30         }
31     } else {
32         city_data.trade.land_trade_problem_duration = 0;
33     }
34     if (city_data.trade.sea_trade_problem_duration > 0) {
35         city_data.trade.sea_trade_problem_duration--;
36         if (building_lighthouse_is_fully_functional()) {
37             city_data.trade.sea_trade_problem_duration--;
38         }
39     }
40     if (city_data.trade.sea_trade_problem_duration <= 0) {
41         city_data.trade.sea_trade_problem_duration = 0;
42     }
43     if (city_data.trade.land_trade_problem_duration <= 0) {
44         city_data.trade.land_trade_problem_duration = 0;
45     }
46 
47 
48     empire_city_generate_trader();
49 }
50 
city_trade_add_land_trade_route(void)51 void city_trade_add_land_trade_route(void)
52 {
53     city_data.trade.num_land_routes++;
54 }
55 
city_trade_add_sea_trade_route(void)56 void city_trade_add_sea_trade_route(void)
57 {
58     city_data.trade.num_sea_routes++;
59 }
60 
city_trade_has_land_trade_route(void)61 int city_trade_has_land_trade_route(void)
62 {
63     return city_data.trade.num_land_routes > 0;
64 }
65 
city_trade_has_sea_trade_route(void)66 int city_trade_has_sea_trade_route(void)
67 {
68     return city_data.trade.num_sea_routes > 0;
69 }
70 
city_trade_start_land_trade_problems(int duration)71 void city_trade_start_land_trade_problems(int duration)
72 {
73     city_data.trade.land_trade_problem_duration = duration;
74 }
75 
city_trade_start_sea_trade_problems(int duration)76 void city_trade_start_sea_trade_problems(int duration)
77 {
78     city_data.trade.sea_trade_problem_duration = duration;
79 }
80 
city_trade_has_land_trade_problems(void)81 int city_trade_has_land_trade_problems(void)
82 {
83     return city_data.trade.land_trade_problem_duration > 0;
84 }
85 
city_trade_has_sea_trade_problems(void)86 int city_trade_has_sea_trade_problems(void)
87 {
88     return city_data.trade.sea_trade_problem_duration > 0;
89 }
90 
city_trade_current_caravan_import_resource(void)91 int city_trade_current_caravan_import_resource(void)
92 {
93     return city_data.trade.caravan_import_resource;
94 }
95 
city_trade_next_caravan_import_resource(void)96 int city_trade_next_caravan_import_resource(void)
97 {
98     city_data.trade.caravan_import_resource++;
99     if (city_data.trade.caravan_import_resource >= RESOURCE_MAX) {
100         city_data.trade.caravan_import_resource = RESOURCE_MIN;
101     }
102     return city_data.trade.caravan_import_resource;
103 }
104 
city_trade_next_caravan_backup_import_resource(void)105 int city_trade_next_caravan_backup_import_resource(void)
106 {
107     city_data.trade.caravan_backup_import_resource++;
108     if (city_data.trade.caravan_backup_import_resource >= RESOURCE_MAX) {
109         city_data.trade.caravan_backup_import_resource = RESOURCE_MIN;
110     }
111     return city_data.trade.caravan_backup_import_resource;
112 }
113 
city_trade_next_docker_import_resource(void)114 int city_trade_next_docker_import_resource(void)
115 {
116     city_data.trade.docker_import_resource++;
117     if (city_data.trade.docker_import_resource >= RESOURCE_MAX) {
118         city_data.trade.docker_import_resource = RESOURCE_MIN;
119     }
120     return city_data.trade.docker_import_resource;
121 }
122 
city_trade_next_docker_export_resource(void)123 int city_trade_next_docker_export_resource(void)
124 {
125     city_data.trade.docker_export_resource++;
126     if (city_data.trade.docker_export_resource >= RESOURCE_MAX) {
127         city_data.trade.docker_export_resource = RESOURCE_MIN;
128     }
129     return city_data.trade.docker_export_resource;
130 }
131 
trade_caravan_count(void)132 int trade_caravan_count(void)
133 {
134     int count = 0;
135     for (int i = 1; i < figure_count(); i++) {
136         figure *f = figure_get(i);
137         if (f->type == FIGURE_TRADE_CARAVAN || f->type == FIGURE_TRADE_CARAVAN_DONKEY || f->type == FIGURE_NATIVE_TRADER) {
138             count++;
139         }
140     }
141     return count;
142 }
143