forked from Keriew/augustus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobject.c
374 lines (348 loc) · 11.2 KB
/
object.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
#include "object.h"
#include "core/calc.h"
#include "core/image.h"
#include "empire/city.h"
#include "empire/trade_route.h"
#include "empire/type.h"
#include "game/animation.h"
#include "scenario/empire.h"
#define MAX_OBJECTS 200
typedef struct {
int in_use;
int city_type;
int city_name_id;
int trade_route_open;
int trade_route_cost;
int city_sells_resource[10];
int city_buys_resource[8];
int trade40;
int trade25;
int trade15;
empire_object obj;
} full_empire_object;
static full_empire_object objects[MAX_OBJECTS];
static int get_trade_amount_code(int index, int resource);
static int is_sea_trade_route(int route_id);
static void fix_image_ids(void)
{
int image_id = 0;
for (int i = 0; i < MAX_OBJECTS; i++) {
if (objects[i].in_use
&& objects[i].obj.type == EMPIRE_OBJECT_CITY
&& objects[i].city_type == EMPIRE_CITY_OURS) {
image_id = objects[i].obj.image_id;
break;
}
}
if (image_id > 0 && image_id != image_group(GROUP_EMPIRE_CITY)) {
// empire map uses old version of graphics: increase every graphic id
int offset = image_group(GROUP_EMPIRE_CITY) - image_id;
for (int i = 0; i < MAX_OBJECTS; i++) {
if (!objects[i].in_use) {
continue;
}
if (objects[i].obj.image_id) {
objects[i].obj.image_id += offset;
if (objects[i].obj.expanded.image_id) {
objects[i].obj.expanded.image_id += offset;
}
}
}
}
}
void empire_object_load(buffer *buf)
{
for (int i = 0; i < MAX_OBJECTS; i++) {
full_empire_object *full = &objects[i];
empire_object *obj = &full->obj;
obj->id = i;
obj->type = buffer_read_u8(buf);
full->in_use = buffer_read_u8(buf);
obj->animation_index = buffer_read_u8(buf);
buffer_skip(buf, 1);
obj->x = buffer_read_i16(buf);
obj->y = buffer_read_i16(buf);
obj->width = buffer_read_i16(buf);
obj->height = buffer_read_i16(buf);
obj->image_id = buffer_read_i16(buf);
obj->expanded.image_id = buffer_read_i16(buf);
buffer_skip(buf, 1);
obj->distant_battle_travel_months = buffer_read_u8(buf);
buffer_skip(buf, 2);
obj->expanded.x = buffer_read_i16(buf);
obj->expanded.y = buffer_read_i16(buf);
full->city_type = buffer_read_u8(buf);
full->city_name_id = buffer_read_u8(buf);
obj->trade_route_id = buffer_read_u8(buf);
full->trade_route_open = buffer_read_u8(buf);
full->trade_route_cost = buffer_read_i16(buf);
for (int r = 0; r < 10; r++) {
full->city_sells_resource[r] = buffer_read_u8(buf);
}
buffer_skip(buf, 2);
for (int r = 0; r < 8; r++) {
full->city_buys_resource[r] = buffer_read_u8(buf);
}
obj->invasion_path_id = buffer_read_u8(buf);
obj->invasion_years = buffer_read_u8(buf);
full->trade40 = buffer_read_u16(buf);
full->trade25 = buffer_read_u16(buf);
full->trade15 = buffer_read_u16(buf);
buffer_skip(buf, 6);
}
fix_image_ids();
}
void empire_object_init_cities(void)
{
empire_city_clear_all();
int route_index = 1;
for (int i = 0; i < MAX_OBJECTS; i++) {
if (!objects[i].in_use || objects[i].obj.type != EMPIRE_OBJECT_CITY) {
continue;
}
full_empire_object *obj = &objects[i];
empire_city *city = empire_city_get(route_index++);
city->in_use = 1;
city->type = obj->city_type;
city->name_id = obj->city_name_id;
if (obj->obj.trade_route_id < 0) {
obj->obj.trade_route_id = 0;
}
if (obj->obj.trade_route_id >= 20) {
obj->obj.trade_route_id = 19;
}
city->route_id = obj->obj.trade_route_id;
city->is_open = obj->trade_route_open;
city->cost_to_open = obj->trade_route_cost;
city->is_sea_trade = is_sea_trade_route(obj->obj.trade_route_id);
for (int resource = RESOURCE_MIN; resource < RESOURCE_MAX; resource++) {
city->sells_resource[resource] = 0;
city->buys_resource[resource] = 0;
if (city->type == EMPIRE_CITY_DISTANT_ROMAN
|| city->type == EMPIRE_CITY_DISTANT_FOREIGN
|| city->type == EMPIRE_CITY_VULNERABLE_ROMAN
|| city->type == EMPIRE_CITY_FUTURE_ROMAN) {
continue;
}
if (empire_object_city_sells_resource(i, resource)) {
city->sells_resource[resource] = 1;
}
if (empire_object_city_buys_resource(i, resource)) {
city->buys_resource[resource] = 1;
}
int amount;
switch (get_trade_amount_code(i, resource)) {
case 1: amount = 15; break;
case 2: amount = 25; break;
case 3: amount = 40; break;
default: amount = 0; break;
}
trade_route_init(city->route_id, resource, amount);
}
city->trader_entry_delay = 4;
city->trader_figure_ids[0] = 0;
city->trader_figure_ids[1] = 0;
city->trader_figure_ids[2] = 0;
city->empire_object_id = i;
}
}
int empire_object_init_distant_battle_travel_months(int object_type)
{
int month = 0;
for (int i = 0; i < MAX_OBJECTS; i++) {
if (objects[i].in_use && objects[i].obj.type == object_type) {
month++;
objects[i].obj.distant_battle_travel_months = month;
}
}
return month;
}
const empire_object *empire_object_get(int object_id)
{
return &objects[object_id].obj;
}
const empire_object *empire_object_get_our_city(void)
{
for (int i = 0; i < MAX_OBJECTS; i++) {
if (objects[i].in_use) {
const empire_object *obj = &objects[i].obj;
if (obj->type == EMPIRE_OBJECT_CITY && objects[i].city_type == EMPIRE_CITY_OURS) {
return obj;
}
}
}
return 0;
}
void empire_object_foreach(void (*callback)(const empire_object *))
{
for (int i = 0; i < MAX_OBJECTS; i++) {
if (objects[i].in_use) {
callback(&objects[i].obj);
}
}
}
const empire_object *empire_object_get_battle_icon(int path_id, int year)
{
for (int i = 0; i < MAX_OBJECTS; i++) {
if (objects[i].in_use) {
empire_object *obj = &objects[i].obj;
if (obj->type == EMPIRE_OBJECT_BATTLE_ICON &&
obj->invasion_path_id == path_id && obj->invasion_years == year) {
return obj;
}
}
}
return 0;
}
int empire_object_get_max_invasion_path(void)
{
int max_path = 0;
for (int i = 0; i < MAX_OBJECTS; i++) {
if (objects[i].in_use && objects[i].obj.type == EMPIRE_OBJECT_BATTLE_ICON) {
if (objects[i].obj.invasion_path_id > max_path) {
max_path = objects[i].obj.invasion_path_id;
}
}
}
return max_path;
}
int empire_object_get_closest(int x, int y)
{
int min_dist = 10000;
int min_obj_id = 0;
for (int i = 0; i < MAX_OBJECTS && objects[i].in_use; i++) {
const empire_object *obj = &objects[i].obj;
int obj_x, obj_y;
if (scenario_empire_is_expanded()) {
obj_x = obj->expanded.x;
obj_y = obj->expanded.y;
} else {
obj_x = obj->x;
obj_y = obj->y;
}
if (obj_x - 8 > x || obj_x + obj->width + 8 <= x) {
continue;
}
if (obj_y - 8 > y || obj_y + obj->height + 8 <= y) {
continue;
}
int dist = calc_maximum_distance(x, y, obj_x + obj->width / 2, obj_y + obj->height / 2);
if (dist < min_dist) {
min_dist = dist;
min_obj_id = i + 1;
}
}
return min_obj_id;
}
void empire_object_set_expanded(int object_id, int new_city_type)
{
objects[object_id].city_type = new_city_type;
if (new_city_type == EMPIRE_CITY_TRADE) {
objects[object_id].obj.expanded.image_id = image_group(GROUP_EMPIRE_CITY_TRADE);
} else if (new_city_type == EMPIRE_CITY_DISTANT_ROMAN) {
objects[object_id].obj.expanded.image_id = image_group(GROUP_EMPIRE_CITY_DISTANT_ROMAN);
}
}
int empire_object_city_buys_resource(int object_id, int resource)
{
const full_empire_object *object = &objects[object_id];
for (int i = 0; i < 8; i++) {
if (object->city_buys_resource[i] == resource) {
return 1;
}
}
return 0;
}
int empire_object_city_sells_resource(int object_id, int resource)
{
const full_empire_object *object = &objects[object_id];
for (int i = 0; i < 10; i++) {
if (object->city_sells_resource[i] == resource) {
return 1;
}
}
return 0;
}
static int is_trade_city(int index)
{
if (objects[index].obj.type != EMPIRE_OBJECT_CITY) {
return 0;
}
return objects[index].city_type > EMPIRE_CITY_OURS && objects[index].city_type < EMPIRE_CITY_FUTURE_ROMAN;
}
static int get_trade_amount_code(int index, int resource)
{
if (!is_trade_city(index)) {
return 0;
}
int resource_flag = 1 << resource;
if (objects[index].trade40 & resource_flag) {
return 3;
}
if (objects[index].trade25 & resource_flag) {
return 2;
}
if (objects[index].trade15 & resource_flag) {
return 1;
}
return 0;
}
static int is_sea_trade_route(int route_id)
{
for (int i = 0; i < MAX_OBJECTS; i++) {
if (objects[i].in_use && objects[i].obj.trade_route_id == route_id) {
if (objects[i].obj.type == EMPIRE_OBJECT_SEA_TRADE_ROUTE) {
return 1;
}
if (objects[i].obj.type == EMPIRE_OBJECT_LAND_TRADE_ROUTE) {
return 0;
}
}
}
return 0;
}
static int get_animation_offset(int image_id, int current_index)
{
if (current_index <= 0) {
current_index = 1;
}
const image *img = image_get(image_id);
int animation_speed = img->animation_speed_id;
if (!game_animation_should_advance(animation_speed)) {
return current_index;
}
if (img->animation_can_reverse) {
int is_reverse = 0;
if (current_index & 0x80) {
is_reverse = 1;
}
int current_sprite = current_index & 0x7f;
if (is_reverse) {
current_index = current_sprite - 1;
if (current_index < 1) {
current_index = 1;
is_reverse = 0;
}
} else {
current_index = current_sprite + 1;
if (current_index > img->num_animation_sprites) {
current_index = img->num_animation_sprites;
is_reverse = 1;
}
}
if (is_reverse) {
current_index = current_index | 0x80;
}
} else {
// Absolutely normal case
current_index++;
if (current_index > img->num_animation_sprites) {
current_index = 1;
}
}
return current_index;
}
int empire_object_update_animation(const empire_object *obj, int image_id)
{
return objects[obj->id].obj.animation_index = get_animation_offset(image_id, obj->animation_index);
}