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