0
|
1 |
#include "stdafx.h"
|
|
2 |
#include "ttd.h"
|
|
3 |
#include "vehicle.h"
|
|
4 |
#include "gfx.h"
|
|
5 |
//#include "station.h"
|
|
6 |
#include "viewport.h"
|
|
7 |
#include "news.h"
|
|
8 |
#include "command.h"
|
|
9 |
#include "saveload.h"
|
|
10 |
#include "player.h"
|
|
11 |
#include "engine.h"
|
|
12 |
|
|
13 |
#define INVALID_COORD (-0x8000)
|
|
14 |
#define GEN_HASH(x,y) (((x & 0x1F80)>>7) + ((y & 0xFC0)))
|
|
15 |
|
|
16 |
|
|
17 |
void VehicleInTheWayErrMsg(Vehicle *v)
|
|
18 |
{
|
|
19 |
StringID id;
|
|
20 |
|
|
21 |
(id = STR_8803_TRAIN_IN_THE_WAY,v->type == VEH_Train) ||
|
|
22 |
(id = STR_9000_ROAD_VEHICLE_IN_THE_WAY,v->type == VEH_Road) ||
|
|
23 |
(id = STR_A015_AIRCRAFT_IN_THE_WAY,v->type == VEH_Aircraft) ||
|
|
24 |
(id = STR_980E_SHIP_IN_THE_WAY, true);
|
|
25 |
|
|
26 |
_error_message = id;
|
|
27 |
}
|
|
28 |
|
|
29 |
static void *EnsureNoVehicleProc(Vehicle *v, void *data)
|
|
30 |
{
|
|
31 |
if (v->tile != (TileIndex)(int)data || v->type == VEH_Disaster)
|
|
32 |
return NULL;
|
|
33 |
|
|
34 |
VehicleInTheWayErrMsg(v);
|
|
35 |
return (void*)1;
|
|
36 |
}
|
|
37 |
|
|
38 |
bool EnsureNoVehicle(TileIndex tile)
|
|
39 |
{
|
|
40 |
return VehicleFromPos(tile, (void*)(int)tile, (VehicleFromPosProc*)EnsureNoVehicleProc) == NULL;
|
|
41 |
}
|
|
42 |
|
|
43 |
static void *EnsureNoVehicleProcZ(Vehicle *v, void *data)
|
|
44 |
{
|
|
45 |
uint32 d = (uint32)data; // max mapsize 4,096*4,096 if shifted 8 bits for byte
|
|
46 |
// with uint64 max mapsize is 2,6843,5456*2,6843,5456 :D
|
|
47 |
if (v->tile != (TileIndex)(d >> (sizeof(byte)*8)) || v->z_pos != (byte)d || v->type == VEH_Disaster)
|
|
48 |
return NULL;
|
|
49 |
|
|
50 |
VehicleInTheWayErrMsg(v);
|
|
51 |
return (void*)1;
|
|
52 |
}
|
|
53 |
|
|
54 |
bool EnsureNoVehicleZ(TileIndex tile, byte z)
|
|
55 |
{
|
|
56 |
TileInfo ti;
|
|
57 |
FindLandscapeHeightByTile(&ti, tile);
|
|
58 |
// needs z correction for slope-type graphics that have the NORTHERN tile lowered
|
|
59 |
// 1, 2, 3, 4, 5, 6 and 7
|
|
60 |
if (CORRECT_Z(ti.tileh))
|
|
61 |
z += 8;
|
|
62 |
|
|
63 |
return VehicleFromPos(tile, (void*)((TileIndex)tile << (sizeof(byte)*8) | (byte)z), (VehicleFromPosProc*)EnsureNoVehicleProcZ) == NULL;
|
|
64 |
}
|
|
65 |
|
|
66 |
Vehicle *FindVehicleBetween(TileIndex from, TileIndex to, byte z)
|
|
67 |
{
|
|
68 |
int x1 = GET_TILE_X(from);
|
|
69 |
int y1 = GET_TILE_Y(from);
|
|
70 |
int x2 = GET_TILE_X(to);
|
|
71 |
int y2 = GET_TILE_Y(to);
|
|
72 |
Vehicle *veh;
|
|
73 |
uint count;
|
|
74 |
|
|
75 |
/* Make sure x1 < x2 or y1 < y2 */
|
|
76 |
if (x1 > x2 || y1 > y2) {
|
|
77 |
intswap(x1,x2);
|
|
78 |
intswap(y1,y2);
|
|
79 |
}
|
|
80 |
for(veh = _vehicles, count=lengthof(_vehicles); count != 0; count--, veh++) {
|
|
81 |
if ((veh->type == VEH_Train || veh->type == VEH_Road) && (z==0xFF || veh->z_pos == z)) {
|
|
82 |
if ((veh->x_pos>>4) >= x1 && (veh->x_pos>>4) <= x2 &&
|
|
83 |
(veh->y_pos>>4) >= y1 && (veh->y_pos>>4) <= y2) {
|
|
84 |
return veh;
|
|
85 |
}
|
|
86 |
}
|
|
87 |
}
|
|
88 |
return NULL;
|
|
89 |
}
|
|
90 |
|
|
91 |
void VehiclePositionChanged(Vehicle *v)
|
|
92 |
{
|
|
93 |
int img = v->cur_image;
|
|
94 |
const SpriteDimension *sd;
|
|
95 |
Point pt = RemapCoords(v->x_pos + v->x_offs, v->y_pos + v->y_offs, v->z_pos);
|
|
96 |
|
|
97 |
sd = GetSpriteDimension(img);
|
|
98 |
|
|
99 |
pt.x += sd->xoffs;
|
|
100 |
pt.y += sd->yoffs;
|
|
101 |
|
|
102 |
UpdateVehiclePosHash(v, pt.x, pt.y);
|
|
103 |
|
|
104 |
v->left_coord = pt.x;
|
|
105 |
v->top_coord = pt.y;
|
|
106 |
v->right_coord = pt.x + sd->xsize + 2;
|
|
107 |
v->bottom_coord = pt.y + sd->ysize + 2;
|
|
108 |
}
|
|
109 |
|
|
110 |
void UpdateCheckpointSign(Checkpoint *cp)
|
|
111 |
{
|
|
112 |
Point pt = RemapCoords2(GET_TILE_X(cp->xy)*16, GET_TILE_Y(cp->xy)*16);
|
|
113 |
SET_DPARAM16(0, cp - _checkpoints);
|
|
114 |
UpdateViewportSignPos(&cp->sign, pt.x, pt.y - 0x20, STR_CHECKPOINT_VIEWPORT);
|
|
115 |
}
|
|
116 |
|
|
117 |
void RedrawCheckpointSign(Checkpoint *cp)
|
|
118 |
{
|
|
119 |
MarkAllViewportsDirty(
|
|
120 |
cp->sign.left - 6,
|
|
121 |
cp->sign.top,
|
|
122 |
cp->sign.left + (cp->sign.width_1 << 2) + 12,
|
|
123 |
cp->sign.top + 48);
|
|
124 |
}
|
|
125 |
|
|
126 |
// Called after load to update coordinates
|
|
127 |
void AfterLoadVehicles()
|
|
128 |
{
|
|
129 |
Vehicle *v;
|
|
130 |
Checkpoint *cp;
|
|
131 |
|
|
132 |
FOR_ALL_VEHICLES(v) {
|
|
133 |
if (v->type != 0) {
|
|
134 |
v->left_coord = INVALID_COORD;
|
|
135 |
VehiclePositionChanged(v);
|
|
136 |
|
|
137 |
if (v->type == VEH_Train) {
|
|
138 |
if (v->subtype == 0)
|
|
139 |
UpdateTrainAcceleration(v);
|
|
140 |
}
|
|
141 |
|
|
142 |
#if defined(_DEBUG)
|
|
143 |
if (!(v->schedule_ptr == NULL || (v->schedule_ptr >= _order_array && v->schedule_ptr < _ptr_to_next_order)))
|
|
144 |
v->schedule_ptr = NULL;
|
|
145 |
#endif
|
|
146 |
}
|
|
147 |
}
|
|
148 |
|
|
149 |
// update checkpoint signs
|
|
150 |
for(cp=_checkpoints; cp != endof(_checkpoints); cp++) if (cp->xy) UpdateCheckpointSign(cp);
|
|
151 |
}
|
|
152 |
|
|
153 |
|
|
154 |
static Vehicle *InitializeVehicle(Vehicle *v)
|
|
155 |
{
|
|
156 |
VehicleID index = v->index;
|
|
157 |
memset(v, 0, sizeof(Vehicle));
|
|
158 |
v->index = index;
|
|
159 |
|
|
160 |
assert(v->schedule_ptr == NULL);
|
|
161 |
|
|
162 |
v->left_coord = INVALID_COORD;
|
|
163 |
v->next = NULL;
|
|
164 |
v->next_hash = 0xffff;
|
|
165 |
v->string_id = 0;
|
|
166 |
return v;
|
|
167 |
}
|
|
168 |
|
|
169 |
Vehicle *ForceAllocateSpecialVehicle()
|
|
170 |
{
|
|
171 |
Vehicle *v;
|
|
172 |
for(v=_vehicles + NUM_NORMAL_VEHICLES;
|
|
173 |
v!=&_vehicles[NUM_NORMAL_VEHICLES+NUM_SPECIAL_VEHICLES]; v++) {
|
|
174 |
if (v->type == 0)
|
|
175 |
return InitializeVehicle(v);
|
|
176 |
}
|
|
177 |
return NULL;
|
|
178 |
|
|
179 |
}
|
|
180 |
|
|
181 |
Vehicle *ForceAllocateVehicle()
|
|
182 |
{
|
|
183 |
Vehicle *v;
|
|
184 |
for(v=_vehicles; v!=&_vehicles[NUM_NORMAL_VEHICLES]; v++) {
|
|
185 |
if (v->type == 0)
|
|
186 |
return InitializeVehicle(v);
|
|
187 |
}
|
|
188 |
return NULL;
|
|
189 |
}
|
|
190 |
|
|
191 |
Vehicle *AllocateVehicle()
|
|
192 |
{
|
|
193 |
Vehicle *v;
|
|
194 |
int num;
|
|
195 |
|
|
196 |
if (IS_HUMAN_PLAYER(_current_player)) {
|
|
197 |
num = 0;
|
|
198 |
|
|
199 |
for(v=_vehicles; v!=&_vehicles[NUM_NORMAL_VEHICLES]; v++) {
|
|
200 |
if (v->type == 0)
|
|
201 |
num++;
|
|
202 |
}
|
|
203 |
|
|
204 |
if (num <= 90)
|
|
205 |
return NULL;
|
|
206 |
}
|
|
207 |
|
|
208 |
return ForceAllocateVehicle();
|
|
209 |
}
|
|
210 |
|
|
211 |
void *VehicleFromPos(TileIndex tile, void *data, VehicleFromPosProc *proc)
|
|
212 |
{
|
|
213 |
int x,y,x2,y2;
|
|
214 |
VehicleID veh;
|
|
215 |
Point pt = RemapCoords(GET_TILE_X(tile) * 16, GET_TILE_Y(tile) * 16, 0);
|
|
216 |
|
|
217 |
x2 = ((pt.x + 104) & 0x1F80) >> 7;
|
|
218 |
x = ((pt.x - 174) & 0x1F80) >> 7;
|
|
219 |
|
|
220 |
y2 = ((pt.y + 56) & 0xFC0);
|
|
221 |
y = ((pt.y - 294) & 0xFC0);
|
|
222 |
|
|
223 |
for(;;) {
|
|
224 |
int xb = x;
|
|
225 |
for(;;) {
|
|
226 |
veh = _vehicle_position_hash[ (x+y)&0xFFFF ];
|
|
227 |
while (veh != INVALID_VEHICLE) {
|
|
228 |
Vehicle *v = &_vehicles[veh];
|
|
229 |
void *a;
|
|
230 |
|
|
231 |
if ((a = proc(v, data)) != NULL)
|
|
232 |
return a;
|
|
233 |
veh = v->next_hash;
|
|
234 |
}
|
|
235 |
|
|
236 |
if (x == x2)
|
|
237 |
break;
|
|
238 |
|
|
239 |
x = (x + 1) & 0x3F;
|
|
240 |
}
|
|
241 |
x = xb;
|
|
242 |
|
|
243 |
if (y == y2)
|
|
244 |
break;
|
|
245 |
|
|
246 |
y = (y + 0x40) & ((0x3F) << 6);
|
|
247 |
}
|
|
248 |
return NULL;
|
|
249 |
}
|
|
250 |
|
|
251 |
|
|
252 |
|
|
253 |
void UpdateVehiclePosHash(Vehicle *v, int x, int y)
|
|
254 |
{
|
|
255 |
VehicleID *old_hash, *new_hash;
|
|
256 |
int old_x = v->left_coord;
|
|
257 |
int old_y = v->top_coord;
|
|
258 |
Vehicle *u;
|
|
259 |
|
|
260 |
new_hash = (x == INVALID_COORD) ? NULL : &_vehicle_position_hash[GEN_HASH(x,y)];
|
|
261 |
old_hash = (old_x == INVALID_COORD) ? NULL : &_vehicle_position_hash[GEN_HASH(old_x, old_y)];
|
|
262 |
|
|
263 |
if (old_hash == new_hash)
|
|
264 |
return;
|
|
265 |
|
|
266 |
/* remove from hash table? */
|
|
267 |
if (old_hash != NULL) {
|
|
268 |
Vehicle *last = NULL;
|
|
269 |
int idx = *old_hash;
|
|
270 |
while((u = &_vehicles[idx]) != v) {
|
|
271 |
idx = u->next_hash;
|
|
272 |
assert(idx != INVALID_VEHICLE);
|
|
273 |
last = u;
|
|
274 |
}
|
|
275 |
|
|
276 |
if (last == NULL)
|
|
277 |
*old_hash = v->next_hash;
|
|
278 |
else
|
|
279 |
last->next_hash = v->next_hash;
|
|
280 |
}
|
|
281 |
|
|
282 |
/* insert into hash table? */
|
|
283 |
if (new_hash != NULL) {
|
|
284 |
v->next_hash = *new_hash;
|
|
285 |
*new_hash = v->index;
|
|
286 |
}
|
|
287 |
}
|
|
288 |
|
|
289 |
void InitializeVehicles()
|
|
290 |
{
|
|
291 |
Vehicle *v;
|
|
292 |
int i;
|
|
293 |
|
|
294 |
// clear it...
|
|
295 |
memset(&_vehicles, 0, sizeof(_vehicles));
|
|
296 |
memset(&_checkpoints, 0, sizeof(_checkpoints));
|
|
297 |
memset(&_depots, 0, sizeof(_depots));
|
|
298 |
|
|
299 |
// setup indexes..
|
|
300 |
i = 0;
|
|
301 |
FOR_ALL_VEHICLES(v)
|
|
302 |
v->index = i++;
|
|
303 |
|
|
304 |
memset(_vehicle_position_hash, -1, sizeof(_vehicle_position_hash));
|
|
305 |
|
|
306 |
_ptr_to_next_order = _order_array;
|
|
307 |
}
|
|
308 |
|
|
309 |
Vehicle *GetLastVehicleInChain(Vehicle *v)
|
|
310 |
{
|
|
311 |
while (v->next != NULL) v = v->next;
|
|
312 |
return v;
|
|
313 |
}
|
|
314 |
|
|
315 |
Vehicle *GetPrevVehicleInChain(Vehicle *v)
|
|
316 |
{
|
|
317 |
Vehicle *org = v;
|
|
318 |
|
|
319 |
v = _vehicles;
|
|
320 |
while (v->type != VEH_Train || org != v->next) {
|
|
321 |
if (++v == endof(_vehicles))
|
|
322 |
return NULL;
|
|
323 |
}
|
|
324 |
|
|
325 |
return v;
|
|
326 |
}
|
|
327 |
|
|
328 |
Vehicle *GetFirstVehicleInChain(Vehicle *v)
|
|
329 |
{
|
|
330 |
for(;;) {
|
|
331 |
Vehicle *u = v;
|
|
332 |
v = _vehicles;
|
|
333 |
while (v->type != VEH_Train || u != v->next) {
|
|
334 |
if (++v == endof(_vehicles))
|
|
335 |
return u;
|
|
336 |
}
|
|
337 |
}
|
|
338 |
}
|
|
339 |
|
|
340 |
int CountVehiclesInChain(Vehicle *v)
|
|
341 |
{
|
|
342 |
int count = 0;
|
|
343 |
do count++; while ( (v=v->next) != NULL);
|
|
344 |
return count;
|
|
345 |
}
|
|
346 |
|
|
347 |
|
|
348 |
Depot *AllocateDepot()
|
|
349 |
{
|
|
350 |
Depot *dep, *free_dep = NULL;
|
|
351 |
int num_free = 0;
|
|
352 |
|
|
353 |
for(dep = _depots; dep != endof(_depots); dep++) {
|
|
354 |
if (dep->xy == 0) {
|
|
355 |
num_free++;
|
|
356 |
if (free_dep==NULL)
|
|
357 |
free_dep = dep;
|
|
358 |
}
|
|
359 |
}
|
|
360 |
|
|
361 |
if (free_dep == NULL ||
|
|
362 |
(num_free < 30 && IS_HUMAN_PLAYER(_current_player))) {
|
|
363 |
_error_message = STR_1009_TOO_MANY_DEPOTS;
|
|
364 |
return NULL;
|
|
365 |
}
|
|
366 |
|
|
367 |
return free_dep;
|
|
368 |
}
|
|
369 |
|
|
370 |
Checkpoint *AllocateCheckpoint()
|
|
371 |
{
|
|
372 |
Checkpoint *cp;
|
|
373 |
|
|
374 |
for(cp = _checkpoints; cp != endof(_checkpoints); cp++) {
|
|
375 |
if (cp->xy == 0)
|
|
376 |
return cp;
|
|
377 |
}
|
|
378 |
|
|
379 |
return NULL;
|
|
380 |
}
|
|
381 |
|
|
382 |
uint GetCheckpointByTile(uint tile)
|
|
383 |
{
|
|
384 |
Checkpoint *cp;
|
|
385 |
int i=0;
|
|
386 |
for(cp=_checkpoints; cp->xy != (TileIndex)tile; cp++) { i++; }
|
|
387 |
return i;
|
|
388 |
}
|
|
389 |
|
|
390 |
|
|
391 |
Vehicle *IsScheduleShared(Vehicle *u)
|
|
392 |
{
|
|
393 |
uint16 *sched = u->schedule_ptr;
|
|
394 |
Vehicle *v;
|
|
395 |
|
|
396 |
FOR_ALL_VEHICLES(v) {
|
|
397 |
if (v->schedule_ptr == sched && u != v && v->type != 0)
|
|
398 |
return v;
|
|
399 |
}
|
|
400 |
return NULL;
|
|
401 |
}
|
|
402 |
|
|
403 |
void DeleteVehicleSchedule(Vehicle *v)
|
|
404 |
{
|
|
405 |
uint16 *sched, *cur;
|
|
406 |
int num;
|
|
407 |
Vehicle *u;
|
|
408 |
|
|
409 |
// if the schedule is shared, don't delete it.
|
|
410 |
if ((u = IsScheduleShared(v)) != NULL) {
|
|
411 |
v->schedule_ptr = NULL;
|
|
412 |
InvalidateWindow(WC_VEHICLE_ORDERS, u->index);
|
|
413 |
return;
|
|
414 |
}
|
|
415 |
|
|
416 |
sched = v->schedule_ptr;
|
|
417 |
v->schedule_ptr = NULL;
|
|
418 |
|
|
419 |
num = v->num_orders + 1;
|
|
420 |
|
|
421 |
_ptr_to_next_order -= num;
|
|
422 |
|
|
423 |
cur = sched;
|
|
424 |
while (cur != _ptr_to_next_order) {
|
|
425 |
assert(cur < _ptr_to_next_order);
|
|
426 |
cur[0] = cur[num];
|
|
427 |
cur++;
|
|
428 |
}
|
|
429 |
|
|
430 |
FOR_ALL_VEHICLES(v) {
|
|
431 |
if (v->schedule_ptr != NULL && sched < v->schedule_ptr) {
|
|
432 |
v->schedule_ptr -= num;
|
|
433 |
}
|
|
434 |
}
|
|
435 |
}
|
|
436 |
|
|
437 |
void DeleteCommandFromVehicleSchedule(uint cmd)
|
|
438 |
{
|
|
439 |
Vehicle *v;
|
|
440 |
uint16 *sched;
|
|
441 |
uint order;
|
|
442 |
bool need_invalidate;
|
|
443 |
|
|
444 |
FOR_ALL_VEHICLES(v) {
|
|
445 |
if (v->type != 0 && v->schedule_ptr != NULL) {
|
|
446 |
|
|
447 |
// clear last station visited
|
|
448 |
if (v->last_station_visited == (cmd>>8) && (cmd & OT_MASK) == OT_GOTO_STATION)
|
|
449 |
v->last_station_visited = 0xFF;
|
|
450 |
|
|
451 |
// check the next order
|
|
452 |
if ( (uint)((v->next_order&OT_MASK) | (v->next_order_param<<8)) == cmd) {
|
|
453 |
v->next_order = OT_DUMMY;
|
|
454 |
InvalidateWindow(WC_VEHICLE_VIEW, v->index);
|
|
455 |
}
|
|
456 |
|
|
457 |
// clear the order list
|
|
458 |
need_invalidate = false;
|
|
459 |
sched = v->schedule_ptr;
|
|
460 |
while ((order=*sched++) != 0) {
|
|
461 |
if ( (order & (OT_MASK|0xFF00)) == cmd) {
|
|
462 |
sched[-1] = OT_DUMMY;
|
|
463 |
need_invalidate = true;
|
|
464 |
}
|
|
465 |
}
|
|
466 |
|
|
467 |
if (need_invalidate)
|
|
468 |
InvalidateWindow(WC_VEHICLE_ORDERS, v->index);
|
|
469 |
}
|
|
470 |
}
|
|
471 |
}
|
|
472 |
|
|
473 |
void DoDeleteDepot(uint tile)
|
|
474 |
{
|
|
475 |
byte dep_index;
|
|
476 |
Depot *d;
|
|
477 |
|
|
478 |
// Clear it
|
|
479 |
DoClearSquare(tile);
|
|
480 |
|
|
481 |
// Nullify the depot struct
|
|
482 |
for(d=_depots,dep_index=0; d->xy != (TileIndex)tile; d++) {dep_index++;}
|
|
483 |
d->xy = 0;
|
|
484 |
|
|
485 |
DeleteCommandFromVehicleSchedule((dep_index << 8) + OT_GOTO_DEPOT);
|
|
486 |
|
|
487 |
// Delete the depot
|
|
488 |
DeleteWindowById(WC_VEHICLE_DEPOT, tile);
|
|
489 |
}
|
|
490 |
|
|
491 |
void DeleteVehicle(Vehicle *v)
|
|
492 |
{
|
|
493 |
DeleteName(v->string_id);
|
|
494 |
v->type = 0;
|
|
495 |
UpdateVehiclePosHash(v, INVALID_COORD, 0);
|
|
496 |
v->next_hash = 0xffff;
|
|
497 |
|
|
498 |
if (v->schedule_ptr != NULL)
|
|
499 |
DeleteVehicleSchedule(v);
|
|
500 |
}
|
|
501 |
|
|
502 |
void DeleteVehicleChain(Vehicle *v)
|
|
503 |
{
|
|
504 |
do {
|
|
505 |
Vehicle *u = v;
|
|
506 |
v = v->next;
|
|
507 |
DeleteVehicle(u);
|
|
508 |
} while (v);
|
|
509 |
}
|
|
510 |
|
|
511 |
|
|
512 |
void Aircraft_Tick(Vehicle *v);
|
|
513 |
void RoadVeh_Tick(Vehicle *v);
|
|
514 |
void Ship_Tick(Vehicle *v);
|
|
515 |
void Train_Tick(Vehicle *v);
|
|
516 |
void EffectVehicle_Tick(Vehicle *v);
|
|
517 |
void DisasterVehicle_Tick(Vehicle *v);
|
|
518 |
|
|
519 |
VehicleTickProc *_vehicle_tick_procs[] = {
|
|
520 |
Train_Tick,
|
|
521 |
RoadVeh_Tick,
|
|
522 |
Ship_Tick,
|
|
523 |
Aircraft_Tick,
|
|
524 |
EffectVehicle_Tick,
|
|
525 |
DisasterVehicle_Tick,
|
|
526 |
};
|
|
527 |
|
|
528 |
void CallVehicleTicks()
|
|
529 |
{
|
|
530 |
Vehicle *v;
|
|
531 |
|
|
532 |
FOR_ALL_VEHICLES(v) {
|
|
533 |
if (v->type != 0)
|
|
534 |
_vehicle_tick_procs[v->type - 0x10](v);
|
|
535 |
}
|
|
536 |
}
|
|
537 |
|
|
538 |
static bool CanFillVehicle_FullLoadAny(Vehicle *v)
|
|
539 |
{
|
|
540 |
uint32 full = 0, not_full = 0;
|
|
541 |
|
|
542 |
// patch should return "true" to continue loading, i.e. when there is no cargo type that is fully loaded.
|
|
543 |
do {
|
|
544 |
//Should never happen, but just in case future additions change this
|
|
545 |
assert(v->cargo_type<32);
|
|
546 |
|
|
547 |
if (v->cargo_cap != 0) {
|
|
548 |
uint32 mask = 1 << v->cargo_type;
|
|
549 |
if (v->cargo_cap == v->cargo_count) full |= mask; else not_full |= mask;
|
|
550 |
}
|
|
551 |
} while ( (v=v->next) != NULL);
|
|
552 |
|
|
553 |
// continue loading if there is a non full cargo type and no cargo type that is full
|
|
554 |
return not_full && (full & ~not_full) == 0;
|
|
555 |
}
|
|
556 |
|
|
557 |
bool CanFillVehicle(Vehicle *v)
|
|
558 |
{
|
|
559 |
byte *t = &_map_type_and_height[v->tile];
|
|
560 |
|
|
561 |
if (t[0] >> 4 == MP_STATION ||
|
|
562 |
(v->type == VEH_Ship &&
|
|
563 |
(t[TILE_XY(1,0)] >> 4 == MP_STATION ||
|
|
564 |
t[TILE_XY(-1,0)] >> 4 == MP_STATION ||
|
|
565 |
t[TILE_XY(0,1)] >> 4 == MP_STATION ||
|
|
566 |
t[TILE_XY(0,-1)] >> 4 == MP_STATION ||
|
|
567 |
t[TILE_XY(-2,0)] >> 4 == MP_STATION))) {
|
|
568 |
|
|
569 |
// If patch is active, use alternative CanFillVehicle-function
|
|
570 |
if (_patches.full_load_any)
|
|
571 |
return CanFillVehicle_FullLoadAny(v);
|
|
572 |
|
|
573 |
do {
|
|
574 |
if (v->cargo_count != v->cargo_cap)
|
|
575 |
return true;
|
|
576 |
} while ( (v=v->next) != NULL);
|
|
577 |
}
|
|
578 |
return false;
|
|
579 |
}
|
|
580 |
|
|
581 |
static void DoDrawVehicle(Vehicle *v)
|
|
582 |
{
|
|
583 |
uint32 image = v->cur_image;
|
|
584 |
|
|
585 |
if (v->vehstatus & VS_DISASTER) {
|
|
586 |
image |= 0x3224000;
|
|
587 |
} else if (v->vehstatus & VS_DEFPAL) {
|
|
588 |
image |= (v->vehstatus & VS_CRASHED) ? 0x3248000 : SPRITE_PALETTE(PLAYER_SPRITE_COLOR(v->owner));
|
|
589 |
}
|
|
590 |
|
|
591 |
AddSortableSpriteToDraw(image, v->x_pos + v->x_offs, v->y_pos + v->y_offs,
|
|
592 |
v->sprite_width, v->sprite_height, v->z_height, v->z_pos);
|
|
593 |
}
|
|
594 |
|
|
595 |
void ViewportAddVehicles(DrawPixelInfo *dpi)
|
|
596 |
{
|
|
597 |
int x,xb, y, x2, y2;
|
|
598 |
VehicleID veh;
|
|
599 |
Vehicle *v;
|
|
600 |
|
|
601 |
x = ((dpi->left - 70) & 0x1F80) >> 7;
|
|
602 |
x2 = ((dpi->left + dpi->width) & 0x1F80) >> 7;
|
|
603 |
|
|
604 |
y = ((dpi->top - 70) & 0xFC0);
|
|
605 |
y2 = ((dpi->top + dpi->height) & 0xFC0);
|
|
606 |
|
|
607 |
for(;;) {
|
|
608 |
xb = x;
|
|
609 |
for(;;) {
|
|
610 |
veh = _vehicle_position_hash[ (x+y)&0xFFFF ];
|
|
611 |
while (veh != INVALID_VEHICLE) {
|
|
612 |
v = &_vehicles[veh];
|
|
613 |
|
|
614 |
if (!(v->vehstatus & VS_HIDDEN) &&
|
|
615 |
dpi->left <= v->right_coord &&
|
|
616 |
dpi->top <= v->bottom_coord &&
|
|
617 |
dpi->left + dpi->width >= v->left_coord &&
|
|
618 |
dpi->top + dpi->height >= v->top_coord) {
|
|
619 |
DoDrawVehicle(v);
|
|
620 |
}
|
|
621 |
veh = v->next_hash;
|
|
622 |
}
|
|
623 |
|
|
624 |
if (x == x2)
|
|
625 |
break;
|
|
626 |
x = (x + 1) & 0x3F;
|
|
627 |
}
|
|
628 |
x = xb;
|
|
629 |
|
|
630 |
if (y == y2)
|
|
631 |
break;
|
|
632 |
y = (y + 0x40) & ((0x3F) << 6);
|
|
633 |
}
|
|
634 |
}
|
|
635 |
|
|
636 |
static void EffectInit_0(Vehicle *v)
|
|
637 |
{
|
|
638 |
uint32 r = Random();
|
|
639 |
v->cur_image = (uint16)((r & 7) + 3701);
|
|
640 |
v->progress = (byte)((r >> 16)&7);
|
|
641 |
}
|
|
642 |
|
|
643 |
static void EffectTick_0(Vehicle *v)
|
|
644 |
{
|
|
645 |
uint tile;
|
|
646 |
uint img;
|
|
647 |
|
|
648 |
if (--v->progress & 0x80) {
|
|
649 |
BeginVehicleMove(v);
|
|
650 |
|
|
651 |
tile = TILE_FROM_XY(v->x_pos, v->y_pos);
|
|
652 |
if (!IS_TILETYPE(tile, MP_INDUSTRY)) {
|
|
653 |
EndVehicleMove(v);
|
|
654 |
DeleteVehicle(v);
|
|
655 |
return;
|
|
656 |
}
|
|
657 |
|
|
658 |
img = v->cur_image + 1;
|
|
659 |
if (img > 3708) img = 3701;
|
|
660 |
v->cur_image = img;
|
|
661 |
v->progress = 7;
|
|
662 |
VehiclePositionChanged(v);
|
|
663 |
EndVehicleMove(v);
|
|
664 |
}
|
|
665 |
}
|
|
666 |
|
|
667 |
static void EffectInit_1(Vehicle *v)
|
|
668 |
{
|
|
669 |
v->cur_image = 3079;
|
|
670 |
v->progress = 12;
|
|
671 |
}
|
|
672 |
|
|
673 |
static void EffectTick_1(Vehicle *v)
|
|
674 |
{
|
|
675 |
bool moved;
|
|
676 |
|
|
677 |
BeginVehicleMove(v);
|
|
678 |
|
|
679 |
moved = false;
|
|
680 |
|
|
681 |
if ((++v->progress & 7) == 0) {
|
|
682 |
v->z_pos++;
|
|
683 |
moved = true;
|
|
684 |
}
|
|
685 |
|
|
686 |
if ((v->progress & 0xF)==4) {
|
|
687 |
if (++v->cur_image > 3083) {
|
|
688 |
EndVehicleMove(v);
|
|
689 |
DeleteVehicle(v);
|
|
690 |
return;
|
|
691 |
}
|
|
692 |
moved = true;
|
|
693 |
}
|
|
694 |
|
|
695 |
if (moved) {
|
|
696 |
VehiclePositionChanged(v);
|
|
697 |
EndVehicleMove(v);
|
|
698 |
}
|
|
699 |
}
|
|
700 |
|
|
701 |
static void EffectInit_2(Vehicle *v)
|
|
702 |
{
|
|
703 |
v->cur_image = 3073;
|
|
704 |
v->progress = 0;
|
|
705 |
}
|
|
706 |
|
|
707 |
static void EffectTick_2(Vehicle *v)
|
|
708 |
{
|
|
709 |
if ((++v->progress & 3) == 0) {
|
|
710 |
BeginVehicleMove(v);
|
|
711 |
v->z_pos++;
|
|
712 |
VehiclePositionChanged(v);
|
|
713 |
EndVehicleMove(v);
|
|
714 |
} else if ((v->progress & 7) == 1) {
|
|
715 |
BeginVehicleMove(v);
|
|
716 |
if (++v->cur_image > 3078) {
|
|
717 |
EndVehicleMove(v);
|
|
718 |
DeleteVehicle(v);
|
|
719 |
} else {
|
|
720 |
VehiclePositionChanged(v);
|
|
721 |
EndVehicleMove(v);
|
|
722 |
}
|
|
723 |
}
|
|
724 |
}
|
|
725 |
|
|
726 |
static void EffectInit_3(Vehicle *v)
|
|
727 |
{
|
|
728 |
v->cur_image = 3084;
|
|
729 |
v->progress = 1;
|
|
730 |
}
|
|
731 |
|
|
732 |
static void EffectTick_3(Vehicle *v)
|
|
733 |
{
|
|
734 |
if (++v->progress > 2) {
|
|
735 |
v->progress = 0;
|
|
736 |
BeginVehicleMove(v);
|
|
737 |
if (++v->cur_image > 3089) {
|
|
738 |
EndVehicleMove(v);
|
|
739 |
DeleteVehicle(v);
|
|
740 |
} else {
|
|
741 |
VehiclePositionChanged(v);
|
|
742 |
EndVehicleMove(v);
|
|
743 |
}
|
|
744 |
}
|
|
745 |
}
|
|
746 |
|
|
747 |
static void EffectInit_4(Vehicle *v)
|
|
748 |
{
|
|
749 |
v->cur_image = 2040;
|
|
750 |
v->progress = 12;
|
|
751 |
}
|
|
752 |
|
|
753 |
static void EffectTick_4(Vehicle *v)
|
|
754 |
{
|
|
755 |
bool moved;
|
|
756 |
|
|
757 |
BeginVehicleMove(v);
|
|
758 |
|
|
759 |
moved = false;
|
|
760 |
|
|
761 |
if ((++v->progress & 3) == 0) {
|
|
762 |
v->z_pos++;
|
|
763 |
moved = true;
|
|
764 |
}
|
|
765 |
|
|
766 |
if ((v->progress & 0xF)==4) {
|
|
767 |
if (++v->cur_image > 2044) {
|
|
768 |
EndVehicleMove(v);
|
|
769 |
DeleteVehicle(v);
|
|
770 |
return;
|
|
771 |
}
|
|
772 |
moved = true;
|
|
773 |
}
|
|
774 |
|
|
775 |
if (moved) {
|
|
776 |
VehiclePositionChanged(v);
|
|
777 |
EndVehicleMove(v);
|
|
778 |
}
|
|
779 |
}
|
|
780 |
|
|
781 |
static void EffectInit_5(Vehicle *v)
|
|
782 |
{
|
|
783 |
v->cur_image = 3709;
|
|
784 |
v->progress = 0;
|
|
785 |
}
|
|
786 |
|
|
787 |
static void EffectTick_5(Vehicle *v)
|
|
788 |
{
|
|
789 |
if (!(++v->progress & 3)) {
|
|
790 |
BeginVehicleMove(v);
|
|
791 |
if (++v->cur_image > 3724) {
|
|
792 |
EndVehicleMove(v);
|
|
793 |
DeleteVehicle(v);
|
|
794 |
} else {
|
|
795 |
VehiclePositionChanged(v);
|
|
796 |
EndVehicleMove(v);
|
|
797 |
}
|
|
798 |
}
|
|
799 |
}
|
|
800 |
|
|
801 |
static void EffectInit_6(Vehicle *v)
|
|
802 |
{
|
|
803 |
v->cur_image = 3737;
|
|
804 |
v->progress = 0;
|
|
805 |
}
|
|
806 |
|
|
807 |
static void EffectTick_6(Vehicle *v)
|
|
808 |
{
|
|
809 |
if (!(++v->progress & 7)) {
|
|
810 |
BeginVehicleMove(v);
|
|
811 |
if (++v->cur_image > 3740) v->cur_image = 3737;
|
|
812 |
VehiclePositionChanged(v);
|
|
813 |
EndVehicleMove(v);
|
|
814 |
}
|
|
815 |
|
|
816 |
if (!--v->u.special.unk0) {
|
|
817 |
BeginVehicleMove(v);
|
|
818 |
EndVehicleMove(v);
|
|
819 |
DeleteVehicle(v);
|
|
820 |
}
|
|
821 |
}
|
|
822 |
|
|
823 |
static void EffectInit_7(Vehicle *v)
|
|
824 |
{
|
|
825 |
v->cur_image = 3725;
|
|
826 |
v->progress = 0;
|
|
827 |
}
|
|
828 |
|
|
829 |
static void EffectTick_7(Vehicle *v)
|
|
830 |
{
|
|
831 |
if (!(++v->progress & 3)) {
|
|
832 |
BeginVehicleMove(v);
|
|
833 |
if (++v->cur_image > 3736) {
|
|
834 |
EndVehicleMove(v);
|
|
835 |
DeleteVehicle(v);
|
|
836 |
} else {
|
|
837 |
VehiclePositionChanged(v);
|
|
838 |
EndVehicleMove(v);
|
|
839 |
}
|
|
840 |
}
|
|
841 |
}
|
|
842 |
|
|
843 |
static void EffectInit_8(Vehicle *v)
|
|
844 |
{
|
|
845 |
v->cur_image = 1416;
|
|
846 |
v->progress = 0;
|
|
847 |
v->u.special.unk0 = 0;
|
|
848 |
v->u.special.unk2 = 0;
|
|
849 |
}
|
|
850 |
|
|
851 |
#define MK(imag,dir,dur) (imag<<6)+(dir<<4)+dur
|
|
852 |
static const byte _effecttick8_data[] = {
|
|
853 |
MK(0,0,4),
|
|
854 |
MK(3,3,4),
|
|
855 |
MK(2,2,7),
|
|
856 |
MK(0,2,7),
|
|
857 |
MK(1,1,3),
|
|
858 |
MK(2,2,7),
|
|
859 |
MK(0,2,7),
|
|
860 |
MK(1,1,3),
|
|
861 |
MK(2,2,7),
|
|
862 |
MK(0,2,7),
|
|
863 |
MK(3,3,6),
|
|
864 |
MK(2,2,6),
|
|
865 |
MK(1,1,7),
|
|
866 |
MK(3,1,7),
|
|
867 |
MK(0,0,3),
|
|
868 |
MK(1,1,7),
|
|
869 |
MK(3,1,7),
|
|
870 |
MK(0,0,3),
|
|
871 |
MK(1,1,7),
|
|
872 |
MK(3,1,7),
|
|
873 |
255
|
|
874 |
};
|
|
875 |
#undef MK
|
|
876 |
|
|
877 |
static const int8 _xy_inc_by_dir[5] = {
|
|
878 |
-1, 0, 1, 0, -1,
|
|
879 |
};
|
|
880 |
|
|
881 |
#define GET_X_INC_BY_DIR(d) _xy_inc_by_dir[d]
|
|
882 |
#define GET_Y_INC_BY_DIR(d) _xy_inc_by_dir[(d)+1]
|
|
883 |
|
|
884 |
static void EffectTick_8(Vehicle *v)
|
|
885 |
{
|
|
886 |
byte b;
|
|
887 |
|
|
888 |
if (!(++v->progress & 7)) {
|
|
889 |
v->u.special.unk2++;
|
|
890 |
BeginVehicleMove(v);
|
|
891 |
|
|
892 |
b = _effecttick8_data[v->u.special.unk0];
|
|
893 |
|
|
894 |
v->cur_image = 0x588 + (b>>6);
|
|
895 |
|
|
896 |
v->x_pos += GET_X_INC_BY_DIR((b>>4)&3);
|
|
897 |
v->y_pos += GET_X_INC_BY_DIR((b>>4)&3);
|
|
898 |
|
|
899 |
if (v->u.special.unk2 < (b & 7)) {
|
|
900 |
v->u.special.unk2 = 0;
|
|
901 |
v->u.special.unk0++;
|
|
902 |
if (_effecttick8_data[v->u.special.unk0] == 0xFF) {
|
|
903 |
EndVehicleMove(v);
|
|
904 |
DeleteVehicle(v);
|
|
905 |
return;
|
|
906 |
}
|
|
907 |
}
|
|
908 |
VehiclePositionChanged(v);
|
|
909 |
EndVehicleMove(v);
|
|
910 |
}
|
|
911 |
}
|
|
912 |
|
|
913 |
static void EffectInit_9(Vehicle *v)
|
|
914 |
{
|
|
915 |
v->cur_image = 4751;
|
|
916 |
v->spritenum = 0;
|
|
917 |
v->progress = 0;
|
|
918 |
}
|
|
919 |
|
|
920 |
#define MK(x,y,z,i) (x+4)+(y+4)*16,(z+4)+i*16
|
|
921 |
|
|
922 |
/* -1,0,1,2 = 2*/
|
|
923 |
/* -1,0,1 = 2*/
|
|
924 |
/* */
|
|
925 |
static const byte _effecttick9_data1[] = {
|
|
926 |
MK(0,0,1,0),
|
|
927 |
MK(1,0,1,1),
|
|
928 |
MK(0,0,1,0),
|
|
929 |
MK(1,0,1,2),
|
|
930 |
0x81,
|
|
931 |
};
|
|
932 |
|
|
933 |
|
|
934 |
static const byte _effecttick9_data2[] = {
|
|
935 |
MK(0,0,1,0),
|
|
936 |
MK(-1,0,1,1),
|
|
937 |
MK(0,0,1,0),
|
|
938 |
MK(-1,0,1,2),
|
|
939 |
0x81,
|
|
940 |
};
|
|
941 |
|
|
942 |
static const byte _effecttick9_data3[] = {
|
|
943 |
MK(0,0,1,0),
|
|
944 |
MK(0,1,1,1),
|
|
945 |
MK(0,0,1,0),
|
|
946 |
MK(0,1,1,2),
|
|
947 |
0x81,
|
|
948 |
};
|
|
949 |
|
|
950 |
static const byte _effecttick9_data4[] = {
|
|
951 |
MK(0,0,1,0),
|
|
952 |
MK(0,-1,1,1),
|
|
953 |
MK(0,0,1,0),
|
|
954 |
MK(0,-1,1,2),
|
|
955 |
0x81,
|
|
956 |
};
|
|
957 |
|
|
958 |
static const byte _effecttick9_data5[] = {
|
|
959 |
MK(0,0,1,2),
|
|
960 |
MK(0,0,1,7),
|
|
961 |
MK(0,0,1,8),
|
|
962 |
MK(0,0,1,9),
|
|
963 |
0x80,
|
|
964 |
};
|
|
965 |
|
|
966 |
static const byte _effecttick9_data6[] = {
|
|
967 |
MK(0,0,1,0),
|
|
968 |
MK(0,0,1,1),
|
|
969 |
MK(0,0,1,0),
|
|
970 |
MK(0,0,1,2),
|
|
971 |
MK(0,0,1,0),
|
|
972 |
MK(0,0,1,1),
|
|
973 |
MK(0,0,1,0),
|
|
974 |
MK(0,0,1,2),
|
|
975 |
MK(0,0,1,0),
|
|
976 |
MK(0,0,1,1),
|
|
977 |
MK(0,0,1,0),
|
|
978 |
MK(0,0,1,2),
|
|
979 |
MK(0,0,1,0),
|
|
980 |
MK(0,0,1,1),
|
|
981 |
MK(0,0,1,0),
|
|
982 |
MK(0,0,1,2),
|
|
983 |
MK(0,0,1,0),
|
|
984 |
MK(0,0,1,1),
|
|
985 |
MK(0,0,1,0),
|
|
986 |
MK(0,0,1,2),
|
|
987 |
MK(0,0,1,0),
|
|
988 |
MK(0,0,1,1),
|
|
989 |
MK(0,0,1,0),
|
|
990 |
MK(0,0,1,2),
|
|
991 |
MK(0,0,1,0),
|
|
992 |
MK(0,0,1,1),
|
|
993 |
MK(0,0,1,0),
|
|
994 |
MK(0,0,1,2),
|
|
995 |
MK(0,0,1,0),
|
|
996 |
MK(0,0,1,1),
|
|
997 |
MK(0,0,1,0),
|
|
998 |
MK(0,0,1,2),
|
|
999 |
MK(0,0,1,0),
|
|
1000 |
MK(0,0,1,1),
|
|
1001 |
MK(0,0,1,0),
|
|
1002 |
MK(0,0,1,2),
|
|
1003 |
MK(0,0,1,0),
|
|
1004 |
MK(0,0,1,1),
|
|
1005 |
MK(0,0,1,0),
|
|
1006 |
MK(0,0,1,2),
|
|
1007 |
MK(0,0,1,0),
|
|
1008 |
MK(0,0,1,1),
|
|
1009 |
MK(0,0,1,0),
|
|
1010 |
MK(0,0,1,2),
|
|
1011 |
MK(0,0,1,0),
|
|
1012 |
MK(0,0,1,1),
|
|
1013 |
MK(0,0,1,0),
|
|
1014 |
MK(0,0,1,2),
|
|
1015 |
MK(0,0,1,0),
|
|
1016 |
MK(0,0,1,1),
|
|
1017 |
MK(0,0,1,0),
|
|
1018 |
MK(0,0,1,2),
|
|
1019 |
MK(0,0,1,0),
|
|
1020 |
MK(0,0,1,1),
|
|
1021 |
MK(0,0,1,0),
|
|
1022 |
MK(0,0,1,2),
|
|
1023 |
MK(0,0,1,0),
|
|
1024 |
MK(0,0,1,1),
|
|
1025 |
MK(0,0,1,0),
|
|
1026 |
MK(0,0,1,2),
|
|
1027 |
MK(0,0,1,0),
|
|
1028 |
MK(0,0,1,1),
|
|
1029 |
MK(2,1,3,0),
|
|
1030 |
MK(1,1,3,1),
|
|
1031 |
MK(2,1,3,0),
|
|
1032 |
MK(1,1,3,2),
|
|
1033 |
MK(2,1,3,0),
|
|
1034 |
MK(1,1,3,1),
|
|
1035 |
MK(2,1,3,0),
|
|
1036 |
MK(1,0,1,2),
|
|
1037 |
MK(0,0,1,0),
|
|
1038 |
MK(1,0,1,1),
|
|
1039 |
MK(0,0,1,0),
|
|
1040 |
MK(1,0,1,2),
|
|
1041 |
MK(0,0,1,0),
|
|
1042 |
MK(1,0,1,1),
|
|
1043 |
MK(0,0,1,0),
|
|
1044 |
MK(1,0,1,2),
|
|
1045 |
0x82,0,
|
|
1046 |
MK(0,0,0,0xA),
|
|
1047 |
MK(0,0,0,0xB),
|
|
1048 |
MK(0,0,0,0xC),
|
|
1049 |
MK(0,0,0,0xD),
|
|
1050 |
MK(0,0,0,0xE),
|
|
1051 |
0x80
|
|
1052 |
};
|
|
1053 |
#undef MK
|
|
1054 |
|
|
1055 |
static const byte * const _effecttick9_data[6] = {
|
|
1056 |
_effecttick9_data1,
|
|
1057 |
_effecttick9_data2,
|
|
1058 |
_effecttick9_data3,
|
|
1059 |
_effecttick9_data4,
|
|
1060 |
_effecttick9_data5,
|
|
1061 |
_effecttick9_data6,
|
|
1062 |
};
|
|
1063 |
|
|
1064 |
static void EffectTick_9(Vehicle *v)
|
|
1065 |
{
|
|
1066 |
int et;
|
|
1067 |
const byte *b;
|
|
1068 |
|
|
1069 |
if (((++v->progress)&3) != 0)
|
|
1070 |
return;
|
|
1071 |
|
|
1072 |
BeginVehicleMove(v);
|
|
1073 |
|
|
1074 |
et = v->engine_type + 1;
|
|
1075 |
|
|
1076 |
if (v->spritenum == 0) {
|
|
1077 |
if (++v->cur_image < 4754) {
|
|
1078 |
VehiclePositionChanged(v);
|
|
1079 |
EndVehicleMove(v);
|
|
1080 |
return;
|
|
1081 |
}
|
|
1082 |
if (v->u.special.unk2 != 0) {
|
|
1083 |
v->spritenum = (byte)((Random()&3)+1);
|
|
1084 |
} else {
|
|
1085 |
v->spritenum = 6;
|
|
1086 |
}
|
|
1087 |
et = 0;
|
|
1088 |
}
|
|
1089 |
|
|
1090 |
again:
|
|
1091 |
v->engine_type = et;
|
|
1092 |
b = &_effecttick9_data[v->spritenum - 1][et*2];
|
|
1093 |
|
|
1094 |
if (*b == 0x80) {
|
|
1095 |
EndVehicleMove(v);
|
|
1096 |
DeleteVehicle(v);
|
|
1097 |
return;
|
|
1098 |
}
|
|
1099 |
|
|
1100 |
if (*b == 0x81) {
|
|
1101 |
if (v->z_pos > 180 || CHANCE16(1,96)) {
|
|
1102 |
v->spritenum = 5;
|
|
1103 |
SndPlayVehicleFx(0x2F, v);
|
|
1104 |
}
|
|
1105 |
et = 0;
|
|
1106 |
goto again;
|
|
1107 |
}
|
|
1108 |
|
|
1109 |
if (*b == 0x82) {
|
|
1110 |
uint tile;
|
|
1111 |
|
|
1112 |
et++;
|
|
1113 |
SndPlayVehicleFx(0x31, v);
|
|
1114 |
|
|
1115 |
tile = TILE_FROM_XY(v->x_pos, v->y_pos);
|
|
1116 |
if (IS_TILETYPE(tile, MP_INDUSTRY) &&
|
|
1117 |
_map5[tile]==0xA2) {
|
|
1118 |
AddAnimatedTile(tile);
|
|
1119 |
}
|
|
1120 |
goto again;
|
|
1121 |
}
|
|
1122 |
|
|
1123 |
v->x_pos += (b[0]&0xF) - 4;
|
|
1124 |
v->y_pos += (b[0]>>4) - 4;
|
|
1125 |
v->z_pos += (b[1]&0xF) - 4;
|
|
1126 |
v->cur_image = 4748 + (b[1] >> 4);
|
|
1127 |
|
|
1128 |
VehiclePositionChanged(v);
|
|
1129 |
EndVehicleMove(v);
|
|
1130 |
}
|
|
1131 |
|
|
1132 |
|
|
1133 |
typedef void EffectInitProc(Vehicle *v);
|
|
1134 |
typedef void EffectTickProc(Vehicle *v);
|
|
1135 |
|
|
1136 |
static EffectInitProc * const _effect_init_procs[] = {
|
|
1137 |
EffectInit_0,
|
|
1138 |
EffectInit_1,
|
|
1139 |
EffectInit_2,
|
|
1140 |
EffectInit_3,
|
|
1141 |
EffectInit_4,
|
|
1142 |
EffectInit_5,
|
|
1143 |
EffectInit_6,
|
|
1144 |
EffectInit_7,
|
|
1145 |
EffectInit_8,
|
|
1146 |
EffectInit_9,
|
|
1147 |
};
|
|
1148 |
|
|
1149 |
static EffectTickProc * const _effect_tick_procs[] = {
|
|
1150 |
EffectTick_0,
|
|
1151 |
EffectTick_1,
|
|
1152 |
EffectTick_2,
|
|
1153 |
EffectTick_3,
|
|
1154 |
EffectTick_4,
|
|
1155 |
EffectTick_5,
|
|
1156 |
EffectTick_6,
|
|
1157 |
EffectTick_7,
|
|
1158 |
EffectTick_8,
|
|
1159 |
EffectTick_9,
|
|
1160 |
};
|
|
1161 |
|
|
1162 |
|
|
1163 |
Vehicle *CreateEffectVehicle(int x, int y, int z, int type)
|
|
1164 |
{
|
|
1165 |
Vehicle *v;
|
|
1166 |
|
|
1167 |
v = ForceAllocateSpecialVehicle();
|
|
1168 |
if (v != NULL) {
|
|
1169 |
v->type = VEH_Special;
|
|
1170 |
v->subtype = type;
|
|
1171 |
v->x_pos = x;
|
|
1172 |
v->y_pos = y;
|
|
1173 |
v->z_pos = z;
|
|
1174 |
v->z_height = v->sprite_width = v->sprite_height = 1;
|
|
1175 |
v->x_offs = v->y_offs = 0;
|
|
1176 |
v->tile = 0;
|
|
1177 |
v->vehstatus = VS_UNCLICKABLE;
|
|
1178 |
|
|
1179 |
_effect_init_procs[type](v);
|
|
1180 |
|
|
1181 |
VehiclePositionChanged(v);
|
|
1182 |
BeginVehicleMove(v);
|
|
1183 |
EndVehicleMove(v);
|
|
1184 |
}
|
|
1185 |
return v;
|
|
1186 |
}
|
|
1187 |
|
|
1188 |
Vehicle *CreateEffectVehicleAbove(int x, int y, int z, int type)
|
|
1189 |
{
|
|
1190 |
return CreateEffectVehicle(x, y, GetSlopeZ(x, y) + z, type);
|
|
1191 |
}
|
|
1192 |
|
|
1193 |
Vehicle *CreateEffectVehicleRel(Vehicle *v, int x, int y, int z, int type)
|
|
1194 |
{
|
|
1195 |
return CreateEffectVehicle(v->x_pos + x, v->y_pos + y, v->z_pos + z, type);
|
|
1196 |
}
|
|
1197 |
|
|
1198 |
void EffectVehicle_Tick(Vehicle *v)
|
|
1199 |
{
|
|
1200 |
_effect_tick_procs[v->subtype](v);
|
|
1201 |
}
|
|
1202 |
|
|
1203 |
Vehicle *CheckClickOnVehicle(ViewPort *vp, int x, int y)
|
|
1204 |
{
|
|
1205 |
Vehicle *found = NULL, *v;
|
|
1206 |
uint dist, best_dist = (uint)-1;
|
|
1207 |
|
|
1208 |
if ( (uint)(x -= vp->left) >= (uint)vp->width ||
|
|
1209 |
(uint)(y -= vp->top) >= (uint)vp->height)
|
|
1210 |
return NULL;
|
|
1211 |
|
|
1212 |
x = (x << vp->zoom) + vp->virtual_left;
|
|
1213 |
y = (y << vp->zoom) + vp->virtual_top;
|
|
1214 |
|
|
1215 |
FOR_ALL_VEHICLES(v) {
|
|
1216 |
if (v->type != 0 && (v->vehstatus & (VS_HIDDEN|VS_UNCLICKABLE)) == 0 &&
|
|
1217 |
x >= v->left_coord && x <= v->right_coord &&
|
|
1218 |
y >= v->top_coord && y <= v->bottom_coord) {
|
|
1219 |
|
|
1220 |
dist = max(
|
|
1221 |
myabs( ((v->left_coord + v->right_coord)>>1) - x ),
|
|
1222 |
myabs( ((v->top_coord + v->bottom_coord)>>1) - y )
|
|
1223 |
);
|
|
1224 |
|
|
1225 |
if (dist < best_dist) {
|
|
1226 |
found = v;
|
|
1227 |
best_dist = dist;
|
|
1228 |
}
|
|
1229 |
}
|
|
1230 |
}
|
|
1231 |
|
|
1232 |
return found;
|
|
1233 |
}
|
|
1234 |
|
|
1235 |
|
|
1236 |
void DecreaseVehicleValue(Vehicle *v)
|
|
1237 |
{
|
|
1238 |
v->value -= v->value >> 8;
|
|
1239 |
InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
|
|
1240 |
}
|
|
1241 |
|
|
1242 |
static const byte _breakdown_chance[64] = {
|
|
1243 |
3, 3, 3, 3, 3, 3, 3, 3,
|
|
1244 |
4, 4, 5, 5, 6, 6, 7, 7,
|
|
1245 |
8, 8, 9, 9, 10, 10, 11, 11,
|
|
1246 |
12, 13, 13, 13, 13, 14, 15, 16,
|
|
1247 |
17, 19, 21, 25, 28, 31, 34, 37,
|
|
1248 |
40, 44, 48, 52, 56, 60, 64, 68,
|
|
1249 |
72, 80, 90, 100, 110, 120, 130, 140,
|
|
1250 |
150, 170, 190, 210, 230, 250, 250, 250,
|
|
1251 |
};
|
|
1252 |
|
|
1253 |
void CheckVehicleBreakdown(Vehicle *v)
|
|
1254 |
{
|
|
1255 |
int rel, rel_old;
|
|
1256 |
uint32 r;
|
|
1257 |
int chance;
|
|
1258 |
|
|
1259 |
/* decrease reliability */
|
|
1260 |
v->reliability = rel = max((rel_old = v->reliability) - v->reliability_spd_dec, 0);
|
|
1261 |
if ((rel_old >> 8) != (rel >> 8))
|
|
1262 |
InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
|
|
1263 |
|
|
1264 |
if (v->breakdown_ctr != 0 || (v->vehstatus & VS_STOPPED) != 0 ||
|
|
1265 |
v->cur_speed < 5 || _game_mode == GM_MENU)
|
|
1266 |
return;
|
|
1267 |
|
|
1268 |
r = Random();
|
|
1269 |
|
|
1270 |
/* increase chance of failure */
|
|
1271 |
chance = v->breakdown_chance + 1;
|
|
1272 |
if (CHANCE16I(1,25,r)) chance += 25;
|
|
1273 |
v->breakdown_chance = min(255, chance);
|
|
1274 |
|
|
1275 |
/* calculate reliability value to use in comparison */
|
|
1276 |
rel = v->reliability;
|
|
1277 |
if (v->type == VEH_Ship) rel += 0x6666;
|
|
1278 |
|
|
1279 |
/* disabled breakdowns? */
|
|
1280 |
if (_opt.diff.vehicle_breakdowns < 1)
|
|
1281 |
return;
|
|
1282 |
|
|
1283 |
/* reduced breakdowns? */
|
|
1284 |
if (_opt.diff.vehicle_breakdowns == 1) rel += 0x6666;
|
|
1285 |
|
|
1286 |
/* check if to break down */
|
|
1287 |
if (_breakdown_chance[(uint)min(rel, 0xffff) >> 10] <= v->breakdown_chance) {
|
|
1288 |
v->breakdown_ctr = (byte)(((r >> 16) & 0x3F) + 0x3F);
|
|
1289 |
v->breakdown_delay = (byte)(((r >> 24) & 0x7F) | 0x80);
|
|
1290 |
v->breakdown_chance = 0;
|
|
1291 |
}
|
|
1292 |
}
|
|
1293 |
|
|
1294 |
static const StringID _vehicle_type_names[4] = {
|
|
1295 |
STR_019F_TRAIN,
|
|
1296 |
STR_019C_ROAD_VEHICLE,
|
|
1297 |
STR_019E_SHIP,
|
|
1298 |
STR_019D_AIRCRAFT,
|
|
1299 |
};
|
|
1300 |
|
|
1301 |
static void ShowVehicleGettingOld(Vehicle *v, StringID msg)
|
|
1302 |
{
|
|
1303 |
if (v->owner != _local_player)
|
|
1304 |
return;
|
26
|
1305 |
// Do not show getting-old message if autorenew is active
|
|
1306 |
if (_patches.autorenew)
|
|
1307 |
return;
|
0
|
1308 |
|
|
1309 |
SET_DPARAM16(0, _vehicle_type_names[v->type - 0x10]);
|
|
1310 |
SET_DPARAM16(1, v->unitnumber);
|
|
1311 |
AddNewsItem(msg, NEWS_FLAGS(NM_SMALL, NF_VIEWPORT|NF_VEHICLE, NT_ADVICE, 0), v->index, 0);
|
|
1312 |
}
|
|
1313 |
|
|
1314 |
void AgeVehicle(Vehicle *v)
|
|
1315 |
{
|
|
1316 |
int age;
|
|
1317 |
|
|
1318 |
if (v->age < 65535)
|
|
1319 |
v->age++;
|
|
1320 |
|
|
1321 |
age = v->age - v->max_age;
|
|
1322 |
if (age == 366*0 || age == 366*1 || age == 366*2 || age == 366*3 || age == 366*4)
|
|
1323 |
v->reliability_spd_dec <<= 1;
|
|
1324 |
|
|
1325 |
InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
|
|
1326 |
|
|
1327 |
if (age == -366) {
|
|
1328 |
ShowVehicleGettingOld(v, STR_01A0_IS_GETTING_OLD);
|
|
1329 |
} else if (age == 0) {
|
|
1330 |
ShowVehicleGettingOld(v, STR_01A1_IS_GETTING_VERY_OLD);
|
|
1331 |
} else if (age == 366*1 || age == 366*2 || age == 366*3 || age == 366*4 || age == 366*5) {
|
|
1332 |
ShowVehicleGettingOld(v, STR_01A2_IS_GETTING_VERY_OLD_AND);
|
|
1333 |
}
|
|
1334 |
}
|
|
1335 |
|
|
1336 |
void MaybeRenewVehicle(Vehicle *v, int32 build_cost)
|
|
1337 |
{
|
|
1338 |
Engine *e;
|
|
1339 |
|
26
|
1340 |
// A vehicle is autorenewed when it it gets the amount of months
|
|
1341 |
// give by _patches.autorenew_months away for his max age.
|
|
1342 |
// Standard is -6, meaning 6 months before his max age
|
|
1343 |
// It can be any value between -12 and 12.
|
|
1344 |
if (!_patches.autorenew || v->age - v->max_age < (_patches.autorenew_months * 30))
|
0
|
1345 |
return;
|
26
|
1346 |
|
|
1347 |
if (DEREF_PLAYER(v->owner)->money64 < _patches.autorenew_money + build_cost - v->value) {
|
|
1348 |
if (v->owner == _local_player) {
|
|
1349 |
int message;
|
|
1350 |
SET_DPARAM16(0, v->unitnumber);
|
|
1351 |
switch (v->type) {
|
|
1352 |
case VEH_Train: message = STR_TRAIN_AUTORENEW_FAILED; break;
|
|
1353 |
case VEH_Road: message = STR_ROADVEHICLE_AUTORENEW_FAILED; break;
|
|
1354 |
case VEH_Ship: message = STR_SHIP_AUTORENEW_FAILED; break;
|
|
1355 |
case VEH_Aircraft: message = STR_AIRCRAFT_AUTORENEW_FAILED; break;
|
|
1356 |
// This should never happen
|
|
1357 |
default: message = 0; break;
|
|
1358 |
}
|
|
1359 |
|
|
1360 |
AddNewsItem(message, NEWS_FLAGS(NM_SMALL, NF_VIEWPORT|NF_VEHICLE, NT_ADVICE, 0), v->index, 0);
|
|
1361 |
}
|
|
1362 |
return;
|
|
1363 |
}
|
|
1364 |
|
|
1365 |
// Withdraw the money from the right player ;)
|
|
1366 |
_current_player = v->owner;
|
0
|
1367 |
|
|
1368 |
e = &_engines[v->engine_type];
|
|
1369 |
v->reliability = e->reliability;
|
|
1370 |
v->reliability_spd_dec = e->reliability_spd_dec;
|
|
1371 |
v->age = 0;
|
|
1372 |
|
|
1373 |
v->date_of_last_service = _date;
|
|
1374 |
v->build_year = _cur_year;
|
|
1375 |
|
|
1376 |
SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES);
|
|
1377 |
SubtractMoneyFromPlayer(build_cost - v->value);
|
|
1378 |
v->value = build_cost;
|
|
1379 |
|
|
1380 |
InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
|
|
1381 |
}
|
|
1382 |
|
|
1383 |
|
|
1384 |
int32 CmdNameVehicle(int x, int y, uint32 flags, uint32 p1, uint32 p2)
|
|
1385 |
{
|
|
1386 |
Vehicle *v;
|
|
1387 |
StringID str;
|
|
1388 |
|
|
1389 |
v = &_vehicles[p1];
|
|
1390 |
|
|
1391 |
if (!CheckOwnership(v->owner))
|
|
1392 |
return CMD_ERROR;
|
|
1393 |
|
|
1394 |
str = AllocateName((byte*)_decode_parameters, 2);
|
|
1395 |
if (str == 0)
|
|
1396 |
return CMD_ERROR;
|
|
1397 |
|
|
1398 |
if (flags & DC_EXEC) {
|
|
1399 |
StringID old_str = v->string_id;
|
|
1400 |
v->string_id = str;
|
|
1401 |
DeleteName(old_str);
|
|
1402 |
MarkWholeScreenDirty();
|
|
1403 |
} else {
|
|
1404 |
DeleteName(str);
|
|
1405 |
}
|
|
1406 |
|
|
1407 |
return 0;
|
|
1408 |
}
|
|
1409 |
|
|
1410 |
|
|
1411 |
|
|
1412 |
static Rect _old_vehicle_coords;
|
|
1413 |
|
|
1414 |
void BeginVehicleMove(Vehicle *v) {
|
|
1415 |
_old_vehicle_coords.left = v->left_coord;
|
|
1416 |
_old_vehicle_coords.top = v->top_coord;
|
|
1417 |
_old_vehicle_coords.right = v->right_coord;
|
|
1418 |
_old_vehicle_coords.bottom = v->bottom_coord;
|
|
1419 |
}
|
|
1420 |
|
|
1421 |
void EndVehicleMove(Vehicle *v)
|
|
1422 |
{
|
|
1423 |
MarkAllViewportsDirty(
|
|
1424 |
min(_old_vehicle_coords.left,v->left_coord),
|
|
1425 |
min(_old_vehicle_coords.top,v->top_coord),
|
|
1426 |
max(_old_vehicle_coords.right,v->right_coord)+1,
|
|
1427 |
max(_old_vehicle_coords.bottom,v->bottom_coord)+1
|
|
1428 |
);
|
|
1429 |
}
|
|
1430 |
|
|
1431 |
void InvalidateVehicleOrderWidget(Vehicle *v)
|
|
1432 |
{
|
|
1433 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, 4);
|
|
1434 |
InvalidateWindowWidget(WC_VEHICLE_ORDERS, v->index, 2);
|
|
1435 |
}
|
|
1436 |
|
|
1437 |
/* returns true if staying in the same tile */
|
|
1438 |
bool GetNewVehiclePos(Vehicle *v, GetNewVehiclePosResult *gp)
|
|
1439 |
{
|
|
1440 |
static const int8 _delta_coord[16] = {
|
|
1441 |
-1,-1,-1, 0, 1, 1, 1, 0, /* x */
|
|
1442 |
-1, 0, 1, 1, 1, 0,-1,-1, /* y */
|
|
1443 |
};
|
|
1444 |
|
|
1445 |
int x = v->x_pos + _delta_coord[v->direction];
|
|
1446 |
int y = v->y_pos + _delta_coord[v->direction + 8];
|
|
1447 |
|
|
1448 |
gp->x = x;
|
|
1449 |
gp->y = y;
|
|
1450 |
gp->old_tile = v->tile;
|
|
1451 |
gp->new_tile = TILE_FROM_XY(x,y);
|
|
1452 |
return gp->old_tile == gp->new_tile;
|
|
1453 |
}
|
|
1454 |
|
|
1455 |
static const byte _new_direction_table[9] = {
|
|
1456 |
0, 7, 6,
|
|
1457 |
1, 3, 5,
|
|
1458 |
2, 3, 4,
|
|
1459 |
};
|
|
1460 |
|
|
1461 |
byte GetDirectionTowards(Vehicle *v, int x, int y)
|
|
1462 |
{
|
|
1463 |
byte dirdiff, dir;
|
|
1464 |
int i = 0;
|
|
1465 |
|
|
1466 |
if (y >= v->y_pos) {
|
|
1467 |
if (y != v->y_pos) i+=3;
|
|
1468 |
i+=3;
|
|
1469 |
}
|
|
1470 |
|
|
1471 |
if (x >= v->x_pos) {
|
|
1472 |
if (x != v->x_pos) i++;
|
|
1473 |
i++;
|
|
1474 |
}
|
|
1475 |
|
|
1476 |
dir = v->direction;
|
|
1477 |
|
|
1478 |
dirdiff = _new_direction_table[i] - dir;
|
|
1479 |
if (dirdiff == 0)
|
|
1480 |
return dir;
|
|
1481 |
return (dir+((dirdiff&7)<5?1:-1)) & 7;
|
|
1482 |
}
|
|
1483 |
|
22
|
1484 |
/* Return value has bit 0x2 set, when the vehicle enters a station. Then,
|
|
1485 |
* result << 8 contains the id of the station entered. If the return value has
|
|
1486 |
* bit 0x8 set, the vehicle could not and did not enter the tile. Are there
|
|
1487 |
* other bits that can be set? */
|
0
|
1488 |
uint32 VehicleEnterTile(Vehicle *v, uint tile, int x, int y)
|
|
1489 |
{
|
|
1490 |
uint old_tile = v->tile;
|
|
1491 |
uint32 result = _tile_type_procs[GET_TILETYPE(tile)]->vehicle_enter_tile_proc(v, tile, x, y);
|
|
1492 |
|
22
|
1493 |
/* When vehicle_enter_tile_proc returns 8, that apparently means that
|
|
1494 |
* we cannot enter the tile at all. In that case, don't call
|
|
1495 |
* leave_tile. */
|
0
|
1496 |
if (!(result & 8) && old_tile != tile) {
|
|
1497 |
VehicleLeaveTileProc *proc = _tile_type_procs[GET_TILETYPE(old_tile)]->vehicle_leave_tile_proc;
|
|
1498 |
if (proc != NULL)
|
|
1499 |
proc(v, old_tile, x, y);
|
|
1500 |
}
|
|
1501 |
return result;
|
|
1502 |
}
|
|
1503 |
|
|
1504 |
uint GetFreeUnitNumber(byte type)
|
|
1505 |
{
|
|
1506 |
uint unit_num = 0;
|
|
1507 |
Vehicle *u;
|
|
1508 |
|
|
1509 |
restart:
|
|
1510 |
unit_num++;
|
|
1511 |
FOR_ALL_VEHICLES(u) {
|
|
1512 |
if (u->type == type && u->owner == _current_player &&
|
|
1513 |
unit_num == u->unitnumber)
|
|
1514 |
goto restart;
|
|
1515 |
}
|
|
1516 |
return unit_num;
|
|
1517 |
}
|
|
1518 |
|
|
1519 |
|
|
1520 |
// Save and load of vehicles
|
|
1521 |
const byte _common_veh_desc[] = {
|
|
1522 |
SLE_VAR(Vehicle,subtype, SLE_UINT8),
|
|
1523 |
|
|
1524 |
SLE_VAR(Vehicle,next_in_chain_old, SLE_UINT16),
|
|
1525 |
SLE_VAR(Vehicle,string_id, SLE_STRINGID),
|
|
1526 |
SLE_VAR(Vehicle,unitnumber, SLE_UINT8),
|
|
1527 |
SLE_VAR(Vehicle,owner, SLE_UINT8),
|
|
1528 |
SLE_VAR(Vehicle,tile, SLE_UINT16),
|
|
1529 |
SLE_VAR(Vehicle,dest_tile, SLE_UINT16),
|
|
1530 |
|
|
1531 |
SLE_VAR(Vehicle,x_pos, SLE_INT16),
|
|
1532 |
SLE_VAR(Vehicle,y_pos, SLE_INT16),
|
|
1533 |
SLE_VAR(Vehicle,z_pos, SLE_UINT8),
|
|
1534 |
SLE_VAR(Vehicle,direction, SLE_UINT8),
|
|
1535 |
|
|
1536 |
SLE_VAR(Vehicle,cur_image, SLE_UINT16),
|
|
1537 |
SLE_VAR(Vehicle,spritenum, SLE_UINT8),
|
|
1538 |
SLE_VAR(Vehicle,sprite_width, SLE_UINT8),
|
|
1539 |
SLE_VAR(Vehicle,sprite_height, SLE_UINT8),
|
|
1540 |
SLE_VAR(Vehicle,z_height, SLE_UINT8),
|
|
1541 |
SLE_VAR(Vehicle,x_offs, SLE_INT8),
|
|
1542 |
SLE_VAR(Vehicle,y_offs, SLE_INT8),
|
|
1543 |
SLE_VAR(Vehicle,engine_type, SLE_UINT16),
|
|
1544 |
SLE_VAR(Vehicle,max_speed, SLE_UINT16),
|
|
1545 |
SLE_VAR(Vehicle,cur_speed, SLE_UINT16),
|
|
1546 |
SLE_VAR(Vehicle,subspeed, SLE_UINT8),
|
|
1547 |
SLE_VAR(Vehicle,acceleration, SLE_UINT8),
|
|
1548 |
SLE_VAR(Vehicle,progress, SLE_UINT8),
|
|
1549 |
|
|
1550 |
SLE_VAR(Vehicle,vehstatus, SLE_UINT8),
|
|
1551 |
SLE_VAR(Vehicle,last_station_visited,SLE_UINT8),
|
|
1552 |
|
|
1553 |
SLE_VAR(Vehicle,cargo_type, SLE_UINT8),
|
|
1554 |
SLE_VAR(Vehicle,cargo_days, SLE_UINT8),
|
|
1555 |
SLE_VAR(Vehicle,cargo_source, SLE_UINT8),
|
|
1556 |
SLE_VAR(Vehicle,cargo_cap, SLE_UINT16),
|
|
1557 |
SLE_VAR(Vehicle,cargo_count, SLE_UINT16),
|
|
1558 |
|
|
1559 |
SLE_VAR(Vehicle,day_counter, SLE_UINT8),
|
|
1560 |
SLE_VAR(Vehicle,tick_counter, SLE_UINT8),
|
|
1561 |
|
|
1562 |
SLE_VAR(Vehicle,cur_order_index, SLE_UINT8),
|
|
1563 |
SLE_VAR(Vehicle,num_orders, SLE_UINT8),
|
|
1564 |
SLE_VAR(Vehicle,next_order, SLE_UINT8),
|
|
1565 |
SLE_VAR(Vehicle,next_order_param, SLE_UINT8),
|
|
1566 |
SLE_REF(Vehicle,schedule_ptr, REF_SCHEDULE),
|
|
1567 |
|
|
1568 |
SLE_VAR(Vehicle,age, SLE_UINT16),
|
|
1569 |
SLE_VAR(Vehicle,max_age, SLE_UINT16),
|
|
1570 |
SLE_VAR(Vehicle,date_of_last_service,SLE_UINT16),
|
|
1571 |
SLE_VAR(Vehicle,service_interval, SLE_UINT16),
|
|
1572 |
SLE_VAR(Vehicle,reliability, SLE_UINT16),
|
|
1573 |
SLE_VAR(Vehicle,reliability_spd_dec,SLE_UINT16),
|
|
1574 |
SLE_VAR(Vehicle,breakdown_ctr, SLE_UINT8),
|
|
1575 |
SLE_VAR(Vehicle,breakdown_delay, SLE_UINT8),
|
|
1576 |
SLE_VAR(Vehicle,breakdowns_since_last_service, SLE_UINT8),
|
|
1577 |
SLE_VAR(Vehicle,breakdown_chance, SLE_UINT8),
|
|
1578 |
SLE_VAR(Vehicle,build_year, SLE_UINT8),
|
|
1579 |
|
|
1580 |
SLE_VAR(Vehicle,load_unload_time_rem, SLE_UINT16),
|
|
1581 |
|
|
1582 |
SLE_VAR(Vehicle,profit_this_year, SLE_INT32),
|
|
1583 |
SLE_VAR(Vehicle,profit_last_year, SLE_INT32),
|
|
1584 |
SLE_VAR(Vehicle,value, SLE_UINT32),
|
|
1585 |
|
|
1586 |
// reserve extra space in savegame here. (currently 16 bytes)
|
|
1587 |
SLE_CONDARR(NullStruct,null,SLE_FILE_U64 | SLE_VAR_NULL, 2, 2, 255),
|
|
1588 |
|
|
1589 |
SLE_END()
|
|
1590 |
};
|
|
1591 |
|
|
1592 |
|
|
1593 |
static const byte _train_desc[] = {
|
|
1594 |
SLE_WRITEBYTE(Vehicle,type,VEH_Train, 0), // Train type. VEH_Train in mem, 0 in file.
|
|
1595 |
SLE_INCLUDEX(0, INC_VEHICLE_COMMON),
|
|
1596 |
SLE_VARX(offsetof(Vehicle,u)+offsetof(VehicleRail,crash_anim_pos), SLE_UINT16),
|
|
1597 |
SLE_VARX(offsetof(Vehicle,u)+offsetof(VehicleRail,force_proceed), SLE_UINT8),
|
|
1598 |
SLE_VARX(offsetof(Vehicle,u)+offsetof(VehicleRail,railtype), SLE_UINT8),
|
|
1599 |
SLE_VARX(offsetof(Vehicle,u)+offsetof(VehicleRail,track), SLE_UINT8),
|
|
1600 |
|
|
1601 |
SLE_CONDVARX(offsetof(Vehicle,u)+offsetof(VehicleRail,flags), SLE_UINT8, 2, 255),
|
|
1602 |
SLE_CONDVARX(offsetof(Vehicle,u)+offsetof(VehicleRail,days_since_order_progr), SLE_UINT16, 2, 255),
|
|
1603 |
|
|
1604 |
// reserve extra space in savegame here. (currently 13 bytes)
|
|
1605 |
SLE_CONDARR(NullStruct,null,SLE_FILE_U8 | SLE_VAR_NULL, 13, 2, 255),
|
|
1606 |
|
|
1607 |
SLE_END()
|
|
1608 |
};
|
|
1609 |
|
|
1610 |
static const byte _roadveh_desc[] = {
|
|
1611 |
SLE_WRITEBYTE(Vehicle,type,VEH_Road, 1), // Road type. VEH_Road in mem, 1 in file.
|
|
1612 |
SLE_INCLUDEX(0, INC_VEHICLE_COMMON),
|
|
1613 |
SLE_VARX(offsetof(Vehicle,u)+offsetof(VehicleRoad,state), SLE_UINT8),
|
|
1614 |
SLE_VARX(offsetof(Vehicle,u)+offsetof(VehicleRoad,frame), SLE_UINT8),
|
|
1615 |
SLE_VARX(offsetof(Vehicle,u)+offsetof(VehicleRoad,unk2), SLE_UINT16),
|
|
1616 |
SLE_VARX(offsetof(Vehicle,u)+offsetof(VehicleRoad,overtaking), SLE_UINT8),
|
|
1617 |
SLE_VARX(offsetof(Vehicle,u)+offsetof(VehicleRoad,overtaking_ctr),SLE_UINT8),
|
|
1618 |
SLE_VARX(offsetof(Vehicle,u)+offsetof(VehicleRoad,crashed_ctr), SLE_UINT16),
|
|
1619 |
SLE_VARX(offsetof(Vehicle,u)+offsetof(VehicleRoad,reverse_ctr), SLE_UINT8),
|
|
1620 |
|
|
1621 |
// reserve extra space in savegame here. (currently 16 bytes)
|
|
1622 |
SLE_CONDARR(NullStruct,null,SLE_FILE_U64 | SLE_VAR_NULL, 2, 2, 255),
|
|
1623 |
|
|
1624 |
SLE_END()
|
|
1625 |
};
|
|
1626 |
|
|
1627 |
static const byte _ship_desc[] = {
|
|
1628 |
SLE_WRITEBYTE(Vehicle,type,VEH_Ship, 2), // Ship type. VEH_Ship in mem, 2 in file.
|
|
1629 |
SLE_INCLUDEX(0, INC_VEHICLE_COMMON),
|
|
1630 |
SLE_VARX(offsetof(Vehicle,u)+offsetof(VehicleShip,state), SLE_UINT8),
|
|
1631 |
|
|
1632 |
// reserve extra space in savegame here. (currently 16 bytes)
|
|
1633 |
SLE_CONDARR(NullStruct,null,SLE_FILE_U64 | SLE_VAR_NULL, 2, 2, 255),
|
|
1634 |
|
|
1635 |
SLE_END()
|
|
1636 |
};
|
|
1637 |
|
|
1638 |
static const byte _aircraft_desc[] = {
|
|
1639 |
SLE_WRITEBYTE(Vehicle,type,VEH_Aircraft, 3), // Aircraft type. VEH_Aircraft in mem, 3 in file.
|
|
1640 |
SLE_INCLUDEX(0, INC_VEHICLE_COMMON),
|
|
1641 |
SLE_VARX(offsetof(Vehicle,u)+offsetof(VehicleAir,crashed_counter), SLE_UINT16),
|
|
1642 |
SLE_VARX(offsetof(Vehicle,u)+offsetof(VehicleAir,pos), SLE_UINT8),
|
|
1643 |
SLE_VARX(offsetof(Vehicle,u)+offsetof(VehicleAir,targetairport), SLE_UINT8),
|
|
1644 |
SLE_VARX(offsetof(Vehicle,u)+offsetof(VehicleAir,state), SLE_UINT8),
|
|
1645 |
|
|
1646 |
SLE_CONDVARX(offsetof(Vehicle,u)+offsetof(VehicleAir,previous_pos), SLE_UINT8, 2, 255),
|
|
1647 |
|
|
1648 |
// reserve extra space in savegame here. (currently 15 bytes)
|
|
1649 |
SLE_CONDARR(NullStruct,null,SLE_FILE_U8 | SLE_VAR_NULL, 15, 2, 255),
|
|
1650 |
|
|
1651 |
SLE_END()
|
|
1652 |
};
|
|
1653 |
|
|
1654 |
static const byte _special_desc[] = {
|
|
1655 |
SLE_WRITEBYTE(Vehicle,type,VEH_Special, 4),
|
|
1656 |
|
|
1657 |
SLE_VAR(Vehicle,subtype, SLE_UINT8),
|
|
1658 |
|
|
1659 |
SLE_VAR(Vehicle,tile, SLE_UINT16),
|
|
1660 |
|
|
1661 |
SLE_VAR(Vehicle,x_pos, SLE_INT16),
|
|
1662 |
SLE_VAR(Vehicle,y_pos, SLE_INT16),
|
|
1663 |
SLE_VAR(Vehicle,z_pos, SLE_UINT8),
|
|
1664 |
|
|
1665 |
SLE_VAR(Vehicle,cur_image, SLE_UINT16),
|
|
1666 |
SLE_VAR(Vehicle,sprite_width, SLE_UINT8),
|
|
1667 |
SLE_VAR(Vehicle,sprite_height, SLE_UINT8),
|
|
1668 |
SLE_VAR(Vehicle,z_height, SLE_UINT8),
|
|
1669 |
SLE_VAR(Vehicle,x_offs, SLE_INT8),
|
|
1670 |
SLE_VAR(Vehicle,y_offs, SLE_INT8),
|
|
1671 |
SLE_VAR(Vehicle,progress, SLE_UINT8),
|
|
1672 |
SLE_VAR(Vehicle,vehstatus, SLE_UINT8),
|
|
1673 |
|
|
1674 |
SLE_VARX(offsetof(Vehicle,u)+offsetof(VehicleSpecial,unk0), SLE_UINT16),
|
|
1675 |
SLE_VARX(offsetof(Vehicle,u)+offsetof(VehicleSpecial,unk2), SLE_UINT8),
|
|
1676 |
|
|
1677 |
// reserve extra space in savegame here. (currently 16 bytes)
|
|
1678 |
SLE_CONDARR(NullStruct,null,SLE_FILE_U64 | SLE_VAR_NULL, 2, 2, 255),
|
|
1679 |
|
|
1680 |
SLE_END()
|
|
1681 |
};
|
|
1682 |
|
|
1683 |
static const byte _disaster_desc[] = {
|
|
1684 |
SLE_WRITEBYTE(Vehicle,type,VEH_Disaster, 5),
|
|
1685 |
|
|
1686 |
SLE_VAR(Vehicle,next_in_chain_old,SLE_UINT16),
|
|
1687 |
|
|
1688 |
SLE_VAR(Vehicle,subtype, SLE_UINT8),
|
|
1689 |
SLE_VAR(Vehicle,tile, SLE_UINT16),
|
|
1690 |
SLE_VAR(Vehicle,dest_tile, SLE_UINT16),
|
|
1691 |
|
|
1692 |
SLE_VAR(Vehicle,x_pos, SLE_INT16),
|
|
1693 |
SLE_VAR(Vehicle,y_pos, SLE_INT16),
|
|
1694 |
SLE_VAR(Vehicle,z_pos, SLE_UINT8),
|
|
1695 |
SLE_VAR(Vehicle,direction, SLE_UINT8),
|
|
1696 |
|
|
1697 |
SLE_VAR(Vehicle,x_offs, SLE_INT8),
|
|
1698 |
SLE_VAR(Vehicle,y_offs, SLE_INT8),
|
|
1699 |
SLE_VAR(Vehicle,sprite_width, SLE_UINT8),
|
|
1700 |
SLE_VAR(Vehicle,sprite_height, SLE_UINT8),
|
|
1701 |
SLE_VAR(Vehicle,z_height, SLE_UINT8),
|
|
1702 |
SLE_VAR(Vehicle,owner, SLE_UINT8),
|
|
1703 |
SLE_VAR(Vehicle,vehstatus, SLE_UINT8),
|
|
1704 |
SLE_VAR(Vehicle,next_order, SLE_UINT8),
|
|
1705 |
|
|
1706 |
SLE_VAR(Vehicle,cur_image, SLE_UINT16),
|
|
1707 |
SLE_VAR(Vehicle,age, SLE_UINT16),
|
|
1708 |
|
|
1709 |
SLE_VAR(Vehicle,tick_counter, SLE_UINT8),
|
|
1710 |
|
|
1711 |
SLE_VARX(offsetof(Vehicle,u)+offsetof(VehicleDisaster,image_override), SLE_UINT16),
|
|
1712 |
SLE_VARX(offsetof(Vehicle,u)+offsetof(VehicleDisaster,unk2), SLE_UINT16),
|
|
1713 |
|
|
1714 |
// reserve extra space in savegame here. (currently 16 bytes)
|
|
1715 |
SLE_CONDARR(NullStruct,null,SLE_FILE_U64 | SLE_VAR_NULL, 2, 2, 255),
|
|
1716 |
|
|
1717 |
SLE_END()
|
|
1718 |
};
|
|
1719 |
|
|
1720 |
|
|
1721 |
static const void *_veh_descs[] = {
|
|
1722 |
_train_desc,
|
|
1723 |
_roadveh_desc,
|
|
1724 |
_ship_desc,
|
|
1725 |
_aircraft_desc,
|
|
1726 |
_special_desc,
|
|
1727 |
_disaster_desc,
|
|
1728 |
};
|
|
1729 |
|
|
1730 |
// Will be called when the vehicles need to be saved.
|
|
1731 |
static void Save_VEHS()
|
|
1732 |
{
|
|
1733 |
Vehicle *v;
|
|
1734 |
// Write the vehicles
|
|
1735 |
FOR_ALL_VEHICLES(v) {
|
|
1736 |
if (v->type != 0) {
|
|
1737 |
SlSetArrayIndex(v->index);
|
|
1738 |
v->next_in_chain_old = v->next ? v->next->index : INVALID_VEHICLE;
|
|
1739 |
SlObject(v, _veh_descs[v->type - 0x10]);
|
|
1740 |
}
|
|
1741 |
}
|
|
1742 |
}
|
|
1743 |
|
|
1744 |
// Will be called when vehicles need to be loaded.
|
|
1745 |
static void Load_VEHS()
|
|
1746 |
{
|
|
1747 |
int index;
|
|
1748 |
Vehicle *v;
|
|
1749 |
|
|
1750 |
while ((index = SlIterateArray()) != -1) {
|
|
1751 |
Vehicle *v = &_vehicles[index];
|
|
1752 |
v->next_in_chain_old = INVALID_VEHICLE;
|
|
1753 |
SlObject(v, _veh_descs[SlReadByte()]);
|
|
1754 |
v->next = v->next_in_chain_old == INVALID_VEHICLE ? NULL : &_vehicles[v->next_in_chain_old];
|
|
1755 |
if (v->type == VEH_Train)
|
|
1756 |
v->u.rail.first_engine = 0xffff;
|
|
1757 |
}
|
|
1758 |
|
|
1759 |
// Iterate through trains and set first_engine appropriately.
|
|
1760 |
FOR_ALL_VEHICLES(v) {
|
|
1761 |
Vehicle *w;
|
|
1762 |
|
|
1763 |
if (v->type != VEH_Train || v->subtype != 0)
|
|
1764 |
continue;
|
|
1765 |
|
|
1766 |
for (w = v->next; w; w = w->next)
|
|
1767 |
w->u.rail.first_engine = v->engine_type;
|
|
1768 |
}
|
|
1769 |
}
|
|
1770 |
|
|
1771 |
static const byte _depot_desc[] = {
|
|
1772 |
SLE_VAR(Depot,xy, SLE_UINT16),
|
|
1773 |
SLE_VAR(Depot,town_index, SLE_UINT16),
|
|
1774 |
SLE_END()
|
|
1775 |
};
|
|
1776 |
|
|
1777 |
static void Save_DEPT()
|
|
1778 |
{
|
|
1779 |
Depot *d;
|
|
1780 |
int i;
|
|
1781 |
for(i=0,d=_depots; i!=lengthof(_depots); i++,d++) {
|
|
1782 |
if (d->xy != 0) {
|
|
1783 |
SlSetArrayIndex(i);
|
|
1784 |
SlObject(d, _depot_desc);
|
|
1785 |
}
|
|
1786 |
}
|
|
1787 |
}
|
|
1788 |
|
|
1789 |
static void Load_DEPT()
|
|
1790 |
{
|
|
1791 |
int index;
|
|
1792 |
while ((index = SlIterateArray()) != -1) {
|
|
1793 |
SlObject(&_depots[index], _depot_desc);
|
|
1794 |
}
|
|
1795 |
}
|
|
1796 |
|
|
1797 |
static const byte _checkpoint_desc[] = {
|
|
1798 |
SLE_VAR(Checkpoint,xy, SLE_UINT16),
|
|
1799 |
SLE_VAR(Checkpoint,town_or_string, SLE_UINT16),
|
|
1800 |
SLE_VAR(Checkpoint,deleted, SLE_UINT8),
|
|
1801 |
|
|
1802 |
SLE_CONDVAR(Checkpoint, build_date, SLE_UINT16, 3, 255),
|
|
1803 |
SLE_CONDVAR(Checkpoint, stat_id, SLE_UINT8, 3, 255),
|
|
1804 |
|
|
1805 |
SLE_END()
|
|
1806 |
};
|
|
1807 |
|
|
1808 |
static void Save_CHKP()
|
|
1809 |
{
|
|
1810 |
Checkpoint *cp;
|
|
1811 |
int i;
|
|
1812 |
for(i=0,cp=_checkpoints; i!=lengthof(_checkpoints); i++,cp++) {
|
|
1813 |
if (cp->xy != 0) {
|
|
1814 |
SlSetArrayIndex(i);
|
|
1815 |
SlObject(cp, _checkpoint_desc);
|
|
1816 |
}
|
|
1817 |
}
|
|
1818 |
}
|
|
1819 |
|
|
1820 |
static void Load_CHKP()
|
|
1821 |
{
|
|
1822 |
int index;
|
|
1823 |
while ((index = SlIterateArray()) != -1) {
|
|
1824 |
SlObject(&_checkpoints[index], _checkpoint_desc);
|
|
1825 |
}
|
|
1826 |
}
|
|
1827 |
|
|
1828 |
|
|
1829 |
static void Save_ORDR()
|
|
1830 |
{
|
|
1831 |
uint len = _ptr_to_next_order - _order_array;
|
|
1832 |
SlArray(_order_array, len, SLE_UINT16);
|
|
1833 |
}
|
|
1834 |
|
|
1835 |
static void Load_ORDR()
|
|
1836 |
{
|
|
1837 |
uint len = SlGetFieldLength() >> 1;
|
|
1838 |
_ptr_to_next_order = _order_array + len;
|
|
1839 |
SlArray(_order_array, len, SLE_UINT16);
|
|
1840 |
}
|
|
1841 |
|
|
1842 |
const ChunkHandler _veh_chunk_handlers[] = {
|
|
1843 |
{ 'VEHS', Save_VEHS, Load_VEHS, CH_SPARSE_ARRAY},
|
|
1844 |
{ 'ORDR', Save_ORDR, Load_ORDR, CH_RIFF},
|
|
1845 |
{ 'DEPT', Save_DEPT, Load_DEPT, CH_ARRAY},
|
|
1846 |
{ 'CHKP', Save_CHKP, Load_CHKP, CH_ARRAY | CH_LAST},
|
|
1847 |
};
|
|
1848 |
|
|
1849 |
|