author | darkvater |
Tue, 21 Dec 2004 23:27:58 +0000 | |
changeset 755 | 823358e83abf |
parent 699 | 02b8b9c9194f |
child 812 | 79c99885ad00 |
permissions | -rw-r--r-- |
0 | 1 |
#include "stdafx.h" |
2 |
#include "ttd.h" |
|
507
8aa8100b0b22
(svn r815) Include strings.h only in the files which need it.
tron
parents:
458
diff
changeset
|
3 |
#include "table/strings.h" |
679
e959706a3e4d
(svn r1117) Move map arrays and some related macros into their own files map.c and map.h
tron
parents:
593
diff
changeset
|
4 |
#include "map.h" |
0 | 5 |
#include "vehicle.h" |
6 |
#include "engine.h" |
|
7 |
#include "command.h" |
|
8 |
#include "station.h" |
|
9 |
#include "news.h" |
|
10 |
#include "gfx.h" |
|
337
66647f97e7c0
(svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents:
193
diff
changeset
|
11 |
#include "sound.h" |
0 | 12 |
#include "player.h" |
13 |
#include "airport.h" |
|
14 |
||
15 |
static bool AirportMove(Vehicle *v, const AirportFTAClass *Airport); |
|
16 |
static bool AirportSetBlocks(Vehicle *v, AirportFTA *current_pos, const AirportFTAClass *Airport); |
|
17 |
static bool AirportHasBlock(Vehicle *v, AirportFTA *current_pos, const AirportFTAClass *Airport); |
|
18 |
static bool AirportFindFreeTerminal(Vehicle *v, const AirportFTAClass *Airport); |
|
19 |
static bool AirportFindFreeHelipad(Vehicle *v, const AirportFTAClass *Airport); |
|
20 |
static void AirportGoToNextPosition(Vehicle *v, const AirportFTAClass *Airport); |
|
21 |
||
22 |
static void AircraftNextAirportPos_and_Order(Vehicle *v); |
|
23 |
static byte GetAircraftFlyingAltitude(const Vehicle *v); |
|
24 |
||
25 |
static const SpriteID _aircraft_sprite[] = { |
|
26 |
0x0EB5, 0x0EBD, 0x0EC5, 0x0ECD, |
|
27 |
0x0ED5, 0x0EDD, 0x0E9D, 0x0EA5, |
|
28 |
0x0EAD, 0x0EE5, 0x0F05, 0x0F0D, |
|
29 |
0x0F15, 0x0F1D, 0x0F25, 0x0F2D, |
|
30 |
0x0EED, 0x0EF5, 0x0EFD, 0x0F35, |
|
31 |
0x0E9D, 0x0EA5, 0x0EAD, 0x0EB5, |
|
32 |
0x0EBD, 0x0EC5 |
|
33 |
}; |
|
34 |
||
35 |
int GetAircraftImage(Vehicle *v, byte direction) |
|
36 |
{ |
|
37 |
int spritenum = v->spritenum; |
|
38 |
||
39 |
if (is_custom_sprite(spritenum)) { |
|
40 |
int sprite = GetCustomVehicleSprite(v, direction); |
|
41 |
||
42 |
if (sprite) return sprite; |
|
43 |
spritenum = _engine_original_sprites[v->engine_type]; |
|
44 |
} |
|
45 |
return direction + _aircraft_sprite[spritenum]; |
|
46 |
} |
|
47 |
||
48 |
void DrawAircraftEngine(int x, int y, int engine, uint32 image_ormod) |
|
49 |
{ |
|
538
5dd99f3b7eca
(svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents:
534
diff
changeset
|
50 |
int spritenum = AircraftVehInfo(engine)->image_index; |
0 | 51 |
|
381
94c4794d6bd4
(svn r570) -newgrf: Support for custom aircrafts via GRF files. Planeset seems to work :). Also use aircraft_vehinfo() instead of the old tables (pasky).
darkvater
parents:
337
diff
changeset
|
52 |
if (is_custom_sprite(spritenum)) { |
94c4794d6bd4
(svn r570) -newgrf: Support for custom aircrafts via GRF files. Planeset seems to work :). Also use aircraft_vehinfo() instead of the old tables (pasky).
darkvater
parents:
337
diff
changeset
|
53 |
int sprite = GetCustomVehicleIcon(engine, 6); |
94c4794d6bd4
(svn r570) -newgrf: Support for custom aircrafts via GRF files. Planeset seems to work :). Also use aircraft_vehinfo() instead of the old tables (pasky).
darkvater
parents:
337
diff
changeset
|
54 |
|
94c4794d6bd4
(svn r570) -newgrf: Support for custom aircrafts via GRF files. Planeset seems to work :). Also use aircraft_vehinfo() instead of the old tables (pasky).
darkvater
parents:
337
diff
changeset
|
55 |
if (sprite) { |
94c4794d6bd4
(svn r570) -newgrf: Support for custom aircrafts via GRF files. Planeset seems to work :). Also use aircraft_vehinfo() instead of the old tables (pasky).
darkvater
parents:
337
diff
changeset
|
56 |
DrawSprite(sprite | image_ormod, x, y); |
94c4794d6bd4
(svn r570) -newgrf: Support for custom aircrafts via GRF files. Planeset seems to work :). Also use aircraft_vehinfo() instead of the old tables (pasky).
darkvater
parents:
337
diff
changeset
|
57 |
return; |
94c4794d6bd4
(svn r570) -newgrf: Support for custom aircrafts via GRF files. Planeset seems to work :). Also use aircraft_vehinfo() instead of the old tables (pasky).
darkvater
parents:
337
diff
changeset
|
58 |
} |
94c4794d6bd4
(svn r570) -newgrf: Support for custom aircrafts via GRF files. Planeset seems to work :). Also use aircraft_vehinfo() instead of the old tables (pasky).
darkvater
parents:
337
diff
changeset
|
59 |
spritenum = _engine_original_sprites[engine]; |
94c4794d6bd4
(svn r570) -newgrf: Support for custom aircrafts via GRF files. Planeset seems to work :). Also use aircraft_vehinfo() instead of the old tables (pasky).
darkvater
parents:
337
diff
changeset
|
60 |
} |
94c4794d6bd4
(svn r570) -newgrf: Support for custom aircrafts via GRF files. Planeset seems to work :). Also use aircraft_vehinfo() instead of the old tables (pasky).
darkvater
parents:
337
diff
changeset
|
61 |
|
94c4794d6bd4
(svn r570) -newgrf: Support for custom aircrafts via GRF files. Planeset seems to work :). Also use aircraft_vehinfo() instead of the old tables (pasky).
darkvater
parents:
337
diff
changeset
|
62 |
DrawSprite((6 + _aircraft_sprite[spritenum]) | image_ormod, x, y); |
94c4794d6bd4
(svn r570) -newgrf: Support for custom aircrafts via GRF files. Planeset seems to work :). Also use aircraft_vehinfo() instead of the old tables (pasky).
darkvater
parents:
337
diff
changeset
|
63 |
|
538
5dd99f3b7eca
(svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents:
534
diff
changeset
|
64 |
if ((AircraftVehInfo(engine)->subtype & 1) == 0) |
0 | 65 |
DrawSprite(0xF3D, x, y-5); |
66 |
} |
|
67 |
||
68 |
void DrawAircraftEngineInfo(int engine, int x, int y, int maxw) |
|
69 |
{ |
|
538
5dd99f3b7eca
(svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents:
534
diff
changeset
|
70 |
const AircraftVehicleInfo *avi = AircraftVehInfo(engine); |
5dd99f3b7eca
(svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents:
534
diff
changeset
|
71 |
SetDParam(0, ((_price.aircraft_base >> 3) * avi->base_cost) >> 5); |
5dd99f3b7eca
(svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents:
534
diff
changeset
|
72 |
SetDParam(1, avi->max_speed << 3); |
5dd99f3b7eca
(svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents:
534
diff
changeset
|
73 |
SetDParam(2, avi->passanger_capacity); |
5dd99f3b7eca
(svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents:
534
diff
changeset
|
74 |
SetDParam(3, avi->mail_capacity); |
5dd99f3b7eca
(svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents:
534
diff
changeset
|
75 |
SetDParam(4, avi->running_cost * _price.aircraft_running >> 8); |
0 | 76 |
|
77 |
DrawStringMultiCenter(x, y, STR_A02E_COST_MAX_SPEED_CAPACITY, maxw); |
|
78 |
} |
|
79 |
||
80 |
/* Allocate many vehicles */ |
|
410 | 81 |
static bool AllocateVehicles(Vehicle **vl, int num) |
0 | 82 |
{ |
83 |
int i; |
|
84 |
Vehicle *v; |
|
85 |
bool success = true; |
|
86 |
||
87 |
for(i=0; i!=num; i++) { |
|
88 |
vl[i] = v = AllocateVehicle(); |
|
89 |
if (v == NULL) { |
|
90 |
success = false; |
|
91 |
break; |
|
92 |
} |
|
93 |
v->type = 1; |
|
94 |
} |
|
95 |
||
96 |
while (--i >= 0) { |
|
97 |
vl[i]->type = 0; |
|
98 |
} |
|
99 |
||
100 |
return success; |
|
101 |
} |
|
102 |
||
103 |
static int32 EstimateAircraftCost(uint16 engine_type) |
|
104 |
{ |
|
538
5dd99f3b7eca
(svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents:
534
diff
changeset
|
105 |
return AircraftVehInfo(engine_type)->base_cost * (_price.aircraft_base>>3)>>5; |
0 | 106 |
} |
107 |
||
108 |
||
109 |
/* p1 = engine */ |
|
110 |
int32 CmdBuildAircraft(int x, int y, uint32 flags, uint32 p1, uint32 p2) |
|
111 |
{ |
|
112 |
int32 value; |
|
113 |
Vehicle *vl[3], *v, *u, *w; |
|
114 |
byte unit_num; |
|
115 |
uint tile = TILE_FROM_XY(x,y); |
|
538
5dd99f3b7eca
(svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents:
534
diff
changeset
|
116 |
const AircraftVehicleInfo *avi = AircraftVehInfo(p1); |
0 | 117 |
Engine *e; |
118 |
||
119 |
SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES); |
|
120 |
||
121 |
value = EstimateAircraftCost(p1); |
|
122 |
||
123 |
if (flags & DC_QUERY_COST) |
|
124 |
return value; |
|
125 |
||
126 |
// allocate 2 or 3 vehicle structs, depending on type |
|
538
5dd99f3b7eca
(svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents:
534
diff
changeset
|
127 |
if (!AllocateVehicles(vl, (avi->subtype & 1) == 0 ? 3 : 2) || |
0 | 128 |
_ptr_to_next_order >= endof(_order_array)) |
129 |
return_cmd_error(STR_00E1_TOO_MANY_VEHICLES_IN_GAME); |
|
130 |
||
131 |
unit_num = GetFreeUnitNumber(VEH_Aircraft); |
|
132 |
if (unit_num > _patches.max_aircraft) |
|
133 |
return_cmd_error(STR_00E1_TOO_MANY_VEHICLES_IN_GAME); |
|
134 |
||
135 |
if (flags & DC_EXEC) { |
|
136 |
v = vl[0]; |
|
137 |
u = vl[1]; |
|
138 |
||
139 |
v->unitnumber = unit_num; |
|
140 |
v->type = u->type = VEH_Aircraft; |
|
141 |
v->direction = 3; |
|
142 |
||
143 |
v->owner = u->owner = _current_player; |
|
144 |
||
145 |
v->tile = tile; |
|
146 |
// u->tile = 0; |
|
147 |
||
148 |
x = GET_TILE_X(tile)*16 + 5; |
|
149 |
y = GET_TILE_Y(tile)*16 + 3; |
|
150 |
||
151 |
v->x_pos = u->x_pos = x; |
|
152 |
v->y_pos = u->y_pos = y; |
|
153 |
||
154 |
u->z_pos = GetSlopeZ(x, y); |
|
155 |
v->z_pos = u->z_pos + 1; |
|
156 |
||
157 |
v->x_offs = v->y_offs = -1; |
|
158 |
// u->delta_x = u->delta_y = 0; |
|
159 |
||
160 |
v->sprite_width = v->sprite_height = 2; |
|
161 |
v->z_height = 5; |
|
162 |
||
163 |
u->sprite_width = u->sprite_height = 2; |
|
164 |
u->z_height = 1; |
|
165 |
||
166 |
v->vehstatus = VS_HIDDEN | VS_STOPPED | VS_DEFPAL; |
|
167 |
u->vehstatus = VS_HIDDEN | VS_UNCLICKABLE | VS_DISASTER; |
|
168 |
||
538
5dd99f3b7eca
(svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents:
534
diff
changeset
|
169 |
v->spritenum = avi->image_index; |
0 | 170 |
// v->cargo_count = u->number_of_pieces = 0; |
171 |
||
538
5dd99f3b7eca
(svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents:
534
diff
changeset
|
172 |
v->cargo_cap = avi->passanger_capacity; |
5dd99f3b7eca
(svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents:
534
diff
changeset
|
173 |
u->cargo_cap = avi->mail_capacity; |
0 | 174 |
|
175 |
v->cargo_type = CT_PASSENGERS; |
|
176 |
u->cargo_type = CT_MAIL; |
|
177 |
||
178 |
v->string_id = STR_SV_AIRCRAFT_NAME; |
|
179 |
// v->next_order_param = v->next_order = 0; |
|
180 |
||
181 |
// v->load_unload_time_rem = 0; |
|
182 |
// v->progress = 0; |
|
183 |
v->last_station_visited = 0xFF; |
|
184 |
// v->destination_coords = 0; |
|
185 |
||
538
5dd99f3b7eca
(svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents:
534
diff
changeset
|
186 |
v->max_speed = avi->max_speed; |
5dd99f3b7eca
(svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents:
534
diff
changeset
|
187 |
v->acceleration = avi->acceleration; |
0 | 188 |
v->engine_type = (byte)p1; |
189 |
||
538
5dd99f3b7eca
(svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents:
534
diff
changeset
|
190 |
v->subtype = (avi->subtype & 1) == 0 ? 0 : 2; |
0 | 191 |
v->value = value; |
192 |
||
193 |
u->subtype = 4; |
|
194 |
||
195 |
e = &_engines[p1]; |
|
196 |
v->reliability = e->reliability; |
|
197 |
v->reliability_spd_dec = e->reliability_spd_dec; |
|
198 |
v->max_age = e->lifelength * 366; |
|
199 |
||
200 |
_new_aircraft_id = v->index; |
|
201 |
||
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
202 |
_ptr_to_next_order->type = OT_NOTHING; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
203 |
_ptr_to_next_order->flags = 0; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
204 |
v->schedule_ptr = _ptr_to_next_order++; |
0 | 205 |
// the AI doesn't click on a tile to build airplanes, so the below code will |
206 |
// never work. Therefore just assume the AI's planes always come from Hangar0 |
|
84
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
76
diff
changeset
|
207 |
// On hold for NewAI |
1e0721c29bad
(svn r85) -Add: initial commit of new AI (enable in Patch menu)
truelight
parents:
76
diff
changeset
|
208 |
v->u.air.pos = (!_patches.ainew_active && _is_ai_player) ? 0:MAX_ELEMENTS; |
0 | 209 |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
210 |
/* When we click on hangar we know the tile (it is in var 'tile')it is on. By that we know |
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
211 |
its position in the array of depots the airport has.....we can search |
0 | 212 |
->layout for #th position of depot. Since layout must start with depots, it is simple |
213 |
*/ |
|
214 |
{ |
|
215 |
Station *st; |
|
216 |
const AirportFTAClass *Airport; |
|
217 |
const uint16 *cur_depot; |
|
218 |
byte i = 0; |
|
219 |
st = DEREF_STATION(_map2[tile]); |
|
220 |
Airport = GetAirport(st->airport_type); |
|
699
02b8b9c9194f
(svn r1149) Fix hack which abuses first TileIndex of airport depot array as number of depots (similar change as in map branch)
tron
parents:
679
diff
changeset
|
221 |
for (cur_depot = Airport->airport_depots; i != Airport->nof_depots; cur_depot++) { |
0 | 222 |
if ((uint)(st->airport_tile + *cur_depot) == tile) { |
223 |
assert(Airport->layout[i].heading == HANGAR); |
|
224 |
v->u.air.pos = Airport->layout[i].position; |
|
225 |
break; |
|
226 |
} |
|
227 |
i++; |
|
228 |
} |
|
229 |
// to ensure v->u.air.pos has been given a value |
|
230 |
assert(v->u.air.pos != MAX_ELEMENTS); |
|
231 |
} |
|
232 |
||
233 |
v->u.air.state = HANGAR; |
|
234 |
v->u.air.previous_pos = v->u.air.pos; |
|
235 |
v->u.air.targetairport = _map2[tile]; |
|
236 |
v->next = u; |
|
237 |
||
238 |
v->service_interval = _patches.servint_aircraft; |
|
239 |
||
240 |
v->date_of_last_service = _date; |
|
241 |
v->build_year = _cur_year; |
|
242 |
||
243 |
v->cur_image = u->cur_image = 0xEA0; |
|
244 |
||
245 |
VehiclePositionChanged(v); |
|
246 |
VehiclePositionChanged(u); |
|
247 |
||
248 |
// Aircraft with 3 vehicles? |
|
249 |
if (v->subtype == 0) { |
|
250 |
w = vl[2]; |
|
251 |
||
252 |
u->next = w; |
|
253 |
||
254 |
w->type = VEH_Aircraft; |
|
255 |
w->direction = 0; |
|
256 |
w->owner = _current_player; |
|
257 |
w->x_pos = v->x_pos; |
|
258 |
w->y_pos = v->y_pos; |
|
259 |
w->z_pos = v->z_pos + 5; |
|
260 |
w->x_offs = w->y_offs = -1; |
|
261 |
w->sprite_width = w->sprite_height = 2; |
|
262 |
w->z_height = 1; |
|
263 |
w->vehstatus = VS_HIDDEN | VS_UNCLICKABLE; |
|
264 |
w->subtype = 6; |
|
265 |
w->cur_image = 0xF3D; |
|
266 |
VehiclePositionChanged(w); |
|
267 |
} |
|
268 |
||
269 |
InvalidateWindow(WC_VEHICLE_DEPOT, v->tile); |
|
588 | 270 |
RebuildVehicleLists(); |
0 | 271 |
InvalidateWindow(WC_COMPANY, v->owner); |
272 |
} |
|
273 |
||
274 |
return value; |
|
275 |
} |
|
276 |
||
277 |
bool IsAircraftHangarTile(TileIndex tile) |
|
278 |
{ |
|
279 |
// 0x56 - hangar facing other way international airport (86) |
|
280 |
// 0x20 - hangar large airport (32) |
|
281 |
// 0x41 - hangar small airport (65) |
|
282 |
return IS_TILETYPE(tile, MP_STATION) && |
|
283 |
(_map5[tile] == 32 || _map5[tile] == 65 || _map5[tile] == 86); |
|
284 |
} |
|
285 |
||
286 |
static bool CheckStoppedInHangar(Vehicle *v) |
|
287 |
{ |
|
288 |
if (!(v->vehstatus&VS_STOPPED) || |
|
289 |
!IsAircraftHangarTile(v->tile)) { |
|
290 |
_error_message = STR_A01B_AIRCRAFT_MUST_BE_STOPPED; |
|
291 |
return false; |
|
292 |
} |
|
293 |
||
294 |
return true; |
|
295 |
} |
|
296 |
||
297 |
||
410 | 298 |
static void DoDeleteAircraft(Vehicle *v) |
0 | 299 |
{ |
300 |
DeleteWindowById(WC_VEHICLE_VIEW, v->index); |
|
588 | 301 |
RebuildVehicleLists(); |
0 | 302 |
InvalidateWindow(WC_COMPANY, v->owner); |
303 |
DeleteVehicleChain(v); |
|
304 |
} |
|
305 |
||
306 |
// p1 = vehicle |
|
307 |
int32 CmdSellAircraft(int x, int y, uint32 flags, uint32 p1, uint32 p2) |
|
308 |
{ |
|
309 |
Vehicle *v; |
|
310 |
||
311 |
SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES); |
|
312 |
||
313 |
v = &_vehicles[p1]; |
|
314 |
||
315 |
if (!CheckOwnership(v->owner) || !CheckStoppedInHangar(v)) |
|
316 |
return CMD_ERROR; |
|
317 |
||
318 |
if (flags & DC_EXEC) { |
|
319 |
// Invalidate depot |
|
320 |
InvalidateWindow(WC_VEHICLE_DEPOT, v->tile); |
|
321 |
||
322 |
DoDeleteAircraft(v); |
|
323 |
||
324 |
} |
|
325 |
||
326 |
return -(int32)v->value; |
|
327 |
} |
|
328 |
||
329 |
// p1 = vehicle |
|
330 |
int32 CmdStartStopAircraft(int x, int y, uint32 flags, uint32 p1, uint32 p2) |
|
331 |
{ |
|
332 |
Vehicle *v; |
|
333 |
||
334 |
v = &_vehicles[p1]; |
|
335 |
||
336 |
if (!CheckOwnership(v->owner)) |
|
337 |
return CMD_ERROR; |
|
338 |
||
339 |
// cannot stop airplane when in flight, or when taking off / landing |
|
340 |
if (v->u.air.state >= STARTTAKEOFF) { |
|
341 |
return_cmd_error(STR_A017_AIRCRAFT_IS_IN_FLIGHT); |
|
342 |
} |
|
343 |
||
344 |
if (flags & DC_EXEC) { |
|
345 |
v->vehstatus ^= VS_STOPPED; |
|
755
823358e83abf
(svn r1211) -Feature: Introduce sticky windows to all vehicle windows...Just make sure you do not sticky more than 23 at a time. Also a lot of places in the code invalidated the 'status bar' of the vehicle, referring to it as widget 4. This is now widget 5 and has been #definitized
darkvater
parents:
699
diff
changeset
|
346 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); |
0 | 347 |
InvalidateWindow(WC_VEHICLE_DEPOT, v->tile); |
348 |
} |
|
349 |
||
350 |
return 0; |
|
351 |
} |
|
352 |
||
353 |
// p1 = vehicle |
|
354 |
int32 CmdSendAircraftToHangar(int x, int y, uint32 flags, uint32 p1, uint32 p2) |
|
355 |
{ |
|
356 |
Vehicle *v; |
|
357 |
Station *st; |
|
358 |
||
359 |
v = &_vehicles[p1]; |
|
360 |
||
361 |
if (!CheckOwnership(v->owner)) |
|
362 |
return CMD_ERROR; |
|
363 |
||
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
364 |
if (v->current_order.type == OT_GOTO_DEPOT) { |
0 | 365 |
if (flags & DC_EXEC) { |
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
366 |
if (v->current_order.flags & OF_UNLOAD) v->cur_order_index++; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
367 |
v->current_order.type = OT_DUMMY; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
368 |
v->current_order.flags = 0; |
755
823358e83abf
(svn r1211) -Feature: Introduce sticky windows to all vehicle windows...Just make sure you do not sticky more than 23 at a time. Also a lot of places in the code invalidated the 'status bar' of the vehicle, referring to it as widget 4. This is now widget 5 and has been #definitized
darkvater
parents:
699
diff
changeset
|
369 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); |
0 | 370 |
} |
371 |
} else { |
|
372 |
st = DEREF_STATION(v->u.air.targetairport); |
|
373 |
// If an airport doesn't have terminals (so no landing space for airports), |
|
374 |
// it surely doesn't have any hangars |
|
375 |
if (st->xy == 0 || st->airport_tile == 0 || GetAirport(st->airport_type)->nofterminals == 0) |
|
376 |
return CMD_ERROR; |
|
377 |
||
378 |
if (flags & DC_EXEC) { |
|
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
379 |
v->current_order.type = OT_GOTO_DEPOT; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
380 |
v->current_order.flags = OF_NON_STOP | OF_FULL_LOAD; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
381 |
v->current_order.station = v->u.air.targetairport; |
755
823358e83abf
(svn r1211) -Feature: Introduce sticky windows to all vehicle windows...Just make sure you do not sticky more than 23 at a time. Also a lot of places in the code invalidated the 'status bar' of the vehicle, referring to it as widget 4. This is now widget 5 and has been #definitized
darkvater
parents:
699
diff
changeset
|
382 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); |
0 | 383 |
} |
384 |
} |
|
385 |
||
386 |
return 0; |
|
387 |
} |
|
388 |
||
389 |
// p1 = vehicle |
|
390 |
// p2 = new service int |
|
391 |
int32 CmdChangeAircraftServiceInt(int x, int y, uint32 flags, uint32 p1, uint32 p2) |
|
392 |
{ |
|
393 |
Vehicle *v; |
|
394 |
||
395 |
v = &_vehicles[p1]; |
|
396 |
||
397 |
if (!CheckOwnership(v->owner)) |
|
398 |
return CMD_ERROR; |
|
399 |
||
400 |
if (flags & DC_EXEC) { |
|
401 |
v->service_interval = (uint16)p2; |
|
402 |
InvalidateWindowWidget(WC_VEHICLE_DETAILS, v->index, 7); |
|
403 |
} |
|
404 |
||
405 |
return 0; |
|
406 |
} |
|
407 |
||
408 |
// p1 = vehicle |
|
409 |
// p2 = new cargo type |
|
410 |
int32 CmdRefitAircraft(int x, int y, uint32 flags, uint32 p1, uint32 p2) |
|
411 |
{ |
|
412 |
Vehicle *v,*u; |
|
413 |
int pass, mail; |
|
414 |
int32 cost; |
|
415 |
||
416 |
SET_EXPENSES_TYPE(EXPENSES_AIRCRAFT_RUN); |
|
417 |
||
418 |
v = &_vehicles[p1]; |
|
419 |
if (!CheckOwnership(v->owner) || !CheckStoppedInHangar(v)) |
|
420 |
return CMD_ERROR; |
|
421 |
||
538
5dd99f3b7eca
(svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents:
534
diff
changeset
|
422 |
pass = AircraftVehInfo(v->engine_type)->passanger_capacity; |
0 | 423 |
if (p2 != 0) { |
424 |
pass >>= 1; |
|
425 |
if (p2 != 5) |
|
426 |
pass >>= 1; |
|
427 |
} |
|
428 |
_aircraft_refit_capacity = pass; |
|
429 |
||
430 |
cost = 0; |
|
431 |
if (IS_HUMAN_PLAYER(v->owner) && (byte)p2 != v->cargo_type) { |
|
432 |
cost = _price.aircraft_base >> 7; |
|
433 |
} |
|
434 |
||
435 |
if (flags & DC_EXEC) { |
|
436 |
v->cargo_cap = pass; |
|
437 |
||
438 |
u = v->next; |
|
538
5dd99f3b7eca
(svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents:
534
diff
changeset
|
439 |
mail = AircraftVehInfo(v->engine_type)->mail_capacity; |
0 | 440 |
if (p2 != 0) { |
441 |
mail = 0; |
|
442 |
} |
|
443 |
u->cargo_cap = mail; |
|
444 |
v->cargo_count = u->cargo_count = 0; |
|
445 |
v->cargo_type = (byte)p2; |
|
446 |
InvalidateWindow(WC_VEHICLE_DETAILS, v->index); |
|
447 |
} |
|
448 |
||
449 |
return cost; |
|
450 |
} |
|
451 |
||
452 |
void HandleClickOnAircraft(Vehicle *v) |
|
453 |
{ |
|
454 |
ShowAircraftViewWindow(v); |
|
455 |
} |
|
456 |
||
457 |
static void CheckIfAircraftNeedsService(Vehicle *v) |
|
458 |
{ |
|
459 |
Station *st; |
|
460 |
||
76
30511cbc5188
(svn r77) -Fix: [1010788] AI service interval bug (tnx truesatan)
truelight
parents:
70
diff
changeset
|
461 |
if (_patches.servint_aircraft == 0) |
11
836bc4b37b5b
(svn r12) Change: removed patch no_train_service. Instead you can set the default service interval for any vehicle type to 'disabled'.
dominik
parents:
0
diff
changeset
|
462 |
return; |
836bc4b37b5b
(svn r12) Change: removed patch no_train_service. Instead you can set the default service interval for any vehicle type to 'disabled'.
dominik
parents:
0
diff
changeset
|
463 |
|
593 | 464 |
if (!VehicleNeedsService(v)) |
0 | 465 |
return; |
466 |
||
467 |
if (v->vehstatus & VS_STOPPED) |
|
468 |
return; |
|
469 |
||
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
470 |
if (v->current_order.type == OT_GOTO_DEPOT && |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
471 |
v->current_order.flags & OF_FULL_LOAD) |
0 | 472 |
return; |
473 |
||
76
30511cbc5188
(svn r77) -Fix: [1010788] AI service interval bug (tnx truesatan)
truelight
parents:
70
diff
changeset
|
474 |
if (_patches.gotodepot && ScheduleHasDepotOrders(v->schedule_ptr)) |
0 | 475 |
return; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
476 |
|
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
477 |
st = DEREF_STATION(v->current_order.station); |
0 | 478 |
// only goto depot if the target airport has terminals (eg. it is airport) |
479 |
if (st->xy != 0 && st->airport_tile != 0 && GetAirport(st->airport_type)->nofterminals != 0) { |
|
480 |
// printf("targetairport = %d, st->index = %d\n", v->u.air.targetairport, st->index); |
|
481 |
// v->u.air.targetairport = st->index; |
|
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
482 |
v->current_order.type = OT_GOTO_DEPOT; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
483 |
v->current_order.flags = OF_NON_STOP; |
755
823358e83abf
(svn r1211) -Feature: Introduce sticky windows to all vehicle windows...Just make sure you do not sticky more than 23 at a time. Also a lot of places in the code invalidated the 'status bar' of the vehicle, referring to it as widget 4. This is now widget 5 and has been #definitized
darkvater
parents:
699
diff
changeset
|
484 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); |
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
485 |
} else if (v->current_order.type == OT_GOTO_DEPOT) { |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
486 |
v->current_order.type = OT_DUMMY; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
487 |
v->current_order.flags = 0; |
755
823358e83abf
(svn r1211) -Feature: Introduce sticky windows to all vehicle windows...Just make sure you do not sticky more than 23 at a time. Also a lot of places in the code invalidated the 'status bar' of the vehicle, referring to it as widget 4. This is now widget 5 and has been #definitized
darkvater
parents:
699
diff
changeset
|
488 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); |
0 | 489 |
} |
490 |
} |
|
491 |
||
492 |
void OnNewDay_Aircraft(Vehicle *v) |
|
493 |
{ |
|
494 |
int32 cost; |
|
495 |
||
496 |
if (v->subtype > 2) |
|
497 |
return; |
|
498 |
||
499 |
if ((++v->day_counter & 7) == 0) |
|
500 |
DecreaseVehicleValue(v); |
|
501 |
||
19
6080d2b6a959
(svn r20) Feature: warning when a vehicle has invalid orders (celestar)
dominik
parents:
11
diff
changeset
|
502 |
CheckOrders(v); |
6080d2b6a959
(svn r20) Feature: warning when a vehicle has invalid orders (celestar)
dominik
parents:
11
diff
changeset
|
503 |
|
0 | 504 |
CheckVehicleBreakdown(v); |
505 |
AgeVehicle(v); |
|
506 |
CheckIfAircraftNeedsService(v); |
|
507 |
||
508 |
if (v->vehstatus & VS_STOPPED) |
|
509 |
return; |
|
510 |
||
538
5dd99f3b7eca
(svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents:
534
diff
changeset
|
511 |
cost = AircraftVehInfo(v->engine_type)->running_cost * _price.aircraft_running / 364; |
0 | 512 |
|
513 |
v->profit_this_year -= cost >> 8; |
|
514 |
||
515 |
SET_EXPENSES_TYPE(EXPENSES_AIRCRAFT_RUN); |
|
516 |
SubtractMoneyFromPlayerFract(v->owner, cost); |
|
517 |
||
518 |
InvalidateWindow(WC_VEHICLE_DETAILS, v->index); |
|
519 |
InvalidateWindow(WC_AIRCRAFT_LIST, v->owner); |
|
520 |
} |
|
521 |
||
522 |
void AircraftYearlyLoop() |
|
523 |
{ |
|
524 |
Vehicle *v; |
|
525 |
||
526 |
FOR_ALL_VEHICLES(v) { |
|
527 |
if (v->type == VEH_Aircraft && v->subtype <= 2) { |
|
528 |
v->profit_last_year = v->profit_this_year; |
|
529 |
v->profit_this_year = 0; |
|
530 |
InvalidateWindow(WC_VEHICLE_DETAILS, v->index); |
|
531 |
} |
|
532 |
} |
|
533 |
} |
|
534 |
||
535 |
static void AgeAircraftCargo(Vehicle *v) |
|
536 |
{ |
|
537 |
if (_age_cargo_skip_counter != 0) |
|
538 |
return; |
|
539 |
||
540 |
do { |
|
541 |
if (v->cargo_days != 0xFF) |
|
542 |
v->cargo_days++; |
|
543 |
} while ( (v=v->next) != NULL ); |
|
544 |
} |
|
545 |
||
546 |
static void HelicopterTickHandler(Vehicle *v) |
|
547 |
{ |
|
548 |
Vehicle *u; |
|
549 |
int tick,spd; |
|
550 |
uint16 img; |
|
551 |
||
552 |
u = v->next->next; |
|
553 |
||
554 |
if (u->vehstatus & VS_HIDDEN) |
|
555 |
return; |
|
556 |
||
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
557 |
// if true, helicopter rotors do not rotate. This should only be the case if a helicopter is |
0 | 558 |
// loading/unloading at a terminal or stopped |
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
559 |
if (v->current_order.type == OT_LOADING || (v->vehstatus & VS_STOPPED)) { |
0 | 560 |
if (u->cur_speed != 0) { |
561 |
u->cur_speed++; |
|
562 |
if (u->cur_speed >= 0x80 && u->cur_image == 0xF40) { |
|
563 |
u->cur_speed = 0; |
|
564 |
} |
|
565 |
} |
|
566 |
} else { |
|
567 |
if (u->cur_speed == 0) |
|
568 |
u->cur_speed = 0x70; |
|
569 |
||
570 |
if (u->cur_speed >= 0x50) |
|
571 |
u->cur_speed--; |
|
572 |
} |
|
573 |
||
574 |
tick = ++u->tick_counter; |
|
575 |
spd = u->cur_speed >> 4; |
|
576 |
||
577 |
if (spd == 0) { |
|
578 |
img = 0xF3D; |
|
579 |
if (u->cur_image == img) |
|
580 |
return; |
|
581 |
} else if (tick >= spd) { |
|
582 |
u->tick_counter = 0; |
|
583 |
img = u->cur_image + 1; |
|
584 |
if (img > 0xF40) |
|
585 |
img = 0xF3E; |
|
586 |
} else |
|
587 |
return; |
|
588 |
||
589 |
u->cur_image=img; |
|
590 |
||
591 |
BeginVehicleMove(u); |
|
592 |
VehiclePositionChanged(u); |
|
593 |
EndVehicleMove(u); |
|
594 |
} |
|
595 |
||
596 |
static void SetAircraftPosition(Vehicle *v, int x, int y, int z) |
|
597 |
{ |
|
598 |
Vehicle *u; |
|
599 |
int yt; |
|
600 |
||
601 |
v->x_pos = x; |
|
602 |
v->y_pos = y; |
|
603 |
v->z_pos = z; |
|
604 |
||
605 |
v->cur_image = GetAircraftImage(v, v->direction); |
|
606 |
||
607 |
BeginVehicleMove(v); |
|
608 |
VehiclePositionChanged(v); |
|
609 |
EndVehicleMove(v); |
|
610 |
||
611 |
u = v->next; |
|
612 |
||
613 |
yt = y - ((v->z_pos-GetSlopeZ(x, y-1)) >> 3); |
|
614 |
u->x_pos = x; |
|
615 |
u->y_pos = yt; |
|
616 |
u->z_pos = GetSlopeZ(x,yt); |
|
617 |
u->cur_image = v->cur_image; |
|
618 |
||
619 |
BeginVehicleMove(u); |
|
620 |
VehiclePositionChanged(u); |
|
621 |
EndVehicleMove(u); |
|
622 |
||
623 |
if ((u=u->next) != NULL) { |
|
624 |
u->x_pos = x; |
|
625 |
u->y_pos = y; |
|
626 |
u->z_pos = z + 5; |
|
627 |
||
628 |
BeginVehicleMove(u); |
|
629 |
VehiclePositionChanged(u); |
|
630 |
EndVehicleMove(u); |
|
631 |
} |
|
632 |
} |
|
633 |
||
634 |
static void ServiceAircraft(Vehicle *v) |
|
635 |
{ |
|
636 |
Vehicle *u; |
|
637 |
||
638 |
v->cur_speed = 0; |
|
639 |
v->subspeed = 0; |
|
640 |
v->progress = 0; |
|
641 |
v->vehstatus |= VS_HIDDEN; |
|
642 |
||
643 |
u = v->next; |
|
644 |
u->vehstatus |= VS_HIDDEN; |
|
645 |
if ((u=u->next) != NULL) { |
|
646 |
u->vehstatus |= VS_HIDDEN; |
|
647 |
u->cur_speed = 0; |
|
648 |
} |
|
649 |
||
650 |
SetAircraftPosition(v, v->x_pos, v->y_pos, v->z_pos); |
|
651 |
InvalidateWindow(WC_VEHICLE_DEPOT, v->tile); |
|
652 |
||
578
86e352980acd
(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:
555
diff
changeset
|
653 |
VehicleServiceInDepot(v); |
0 | 654 |
InvalidateWindow(WC_VEHICLE_DETAILS, v->index); |
655 |
} |
|
656 |
||
657 |
static void PlayAircraftSound(Vehicle *v) |
|
658 |
{ |
|
538
5dd99f3b7eca
(svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents:
534
diff
changeset
|
659 |
SndPlayVehicleFx(AircraftVehInfo(v->engine_type)->sfx, v); |
0 | 660 |
} |
661 |
||
662 |
static bool UpdateAircraftSpeed(Vehicle *v) |
|
663 |
{ |
|
664 |
uint spd = v->acceleration * 2; |
|
665 |
byte t; |
|
666 |
||
667 |
v->subspeed = (t=v->subspeed) + (byte)spd; |
|
668 |
spd = min( v->cur_speed + (spd >> 8) + (v->subspeed < t), v->max_speed); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
669 |
|
0 | 670 |
// adjust speed for broken vehicles |
671 |
if(v->vehstatus&VS_AIRCRAFT_BROKEN) spd = min(spd, 27); |
|
672 |
||
673 |
//updates statusbar only if speed have changed to save CPU time |
|
674 |
if (spd != v->cur_speed) { |
|
675 |
v->cur_speed = spd; |
|
676 |
if (_patches.vehicle_speed) |
|
755
823358e83abf
(svn r1211) -Feature: Introduce sticky windows to all vehicle windows...Just make sure you do not sticky more than 23 at a time. Also a lot of places in the code invalidated the 'status bar' of the vehicle, referring to it as widget 4. This is now widget 5 and has been #definitized
darkvater
parents:
699
diff
changeset
|
677 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); |
0 | 678 |
} |
679 |
||
680 |
if (!(v->direction & 1)) { |
|
681 |
spd = spd * 3 >> 2; |
|
682 |
} |
|
683 |
||
684 |
if (spd == 0) |
|
685 |
return false; |
|
686 |
||
687 |
if ((byte)++spd == 0) |
|
688 |
return true; |
|
689 |
||
690 |
v->progress = (t = v->progress) - (byte)spd; |
|
691 |
||
692 |
return (t < v->progress); |
|
693 |
} |
|
694 |
||
695 |
// get Aircraft running altitude |
|
696 |
static byte GetAircraftFlyingAltitude(const Vehicle *v) |
|
697 |
{ |
|
698 |
byte maxz = 162; |
|
699 |
if (v->max_speed != 37) { |
|
700 |
maxz = 171; |
|
701 |
if (v->max_speed != 74) {maxz = 180;} |
|
702 |
} |
|
703 |
return maxz; |
|
704 |
} |
|
705 |
||
706 |
static bool Aircraft_5(Vehicle *v) |
|
707 |
{ |
|
708 |
Station *st; |
|
709 |
const AirportMovingData *amd; |
|
710 |
Vehicle *u; |
|
711 |
byte z,dirdiff,newdir,maxz,curz; |
|
712 |
GetNewVehiclePosResult gp; |
|
713 |
uint dist; |
|
714 |
int x,y; |
|
715 |
||
716 |
st = DEREF_STATION(v->u.air.targetairport); |
|
717 |
||
718 |
// prevent going to 0,0 if airport is deleted. |
|
719 |
{ |
|
720 |
uint tile = st->airport_tile; |
|
721 |
if (tile == 0) tile = st->xy; |
|
722 |
// xy of destination |
|
723 |
x = GET_TILE_X(tile)*16; |
|
724 |
y = GET_TILE_Y(tile)*16; |
|
725 |
} |
|
726 |
||
727 |
// get airport moving data |
|
728 |
assert(v->u.air.pos < GetAirport(st->airport_type)->nofelements); |
|
729 |
amd = &_airport_moving_datas[st->airport_type][v->u.air.pos]; |
|
730 |
||
731 |
// Helicopter raise |
|
732 |
if (amd->flag & AMED_HELI_RAISE) { |
|
733 |
u = v->next->next; |
|
734 |
||
735 |
// Make sure the rotors don't rotate too fast |
|
736 |
if (u->cur_speed > 32) { |
|
737 |
v->cur_speed = 0; |
|
738 |
if (--u->cur_speed == 32) { |
|
541 | 739 |
SndPlayVehicleFx(SND_18_HELICOPTER, v); |
0 | 740 |
} |
741 |
} else { |
|
742 |
u->cur_speed = 32; |
|
743 |
if (UpdateAircraftSpeed(v)) { |
|
744 |
v->tile = 0; |
|
745 |
||
746 |
// Reached altitude? |
|
747 |
if (v->z_pos >= 184) { |
|
748 |
v->cur_speed = 0; |
|
749 |
return true; |
|
750 |
} |
|
751 |
SetAircraftPosition(v, v->x_pos, v->y_pos, v->z_pos+1); |
|
752 |
} |
|
753 |
} |
|
754 |
return false; |
|
755 |
} |
|
756 |
||
757 |
// Helicopter landing. |
|
758 |
if (amd->flag & AMED_HELI_LOWER) { |
|
759 |
if (UpdateAircraftSpeed(v)) { |
|
760 |
if (st->airport_tile == 0) { |
|
761 |
// FIXME - Aircraft_5 -> if station no longer exists, do not land |
|
762 |
// helicopter will circle until sign disappears, then go to next order |
|
763 |
// * what to do when it is the only order left, right now it just stays in 1 place |
|
764 |
v->u.air.state = FLYING; |
|
765 |
AircraftNextAirportPos_and_Order(v); |
|
766 |
return false; |
|
767 |
} |
|
768 |
||
769 |
// Vehicle is now at the airport. |
|
770 |
v->tile = st->airport_tile; |
|
771 |
||
772 |
// Find altitude of landing position. |
|
773 |
z = GetSlopeZ(x, y) + 1; |
|
774 |
if (st->airport_type == AT_OILRIG) z += 54; |
|
775 |
if (st->airport_type == AT_HELIPORT) z += 60; |
|
776 |
||
777 |
if (z == v->z_pos) { |
|
778 |
u = v->next->next; |
|
779 |
||
780 |
// Increase speed of rotors. When speed is 80, we've landed. |
|
781 |
if (u->cur_speed >= 80) |
|
782 |
return true; |
|
783 |
u->cur_speed+=4; |
|
784 |
} else if (v->z_pos > z) { |
|
785 |
SetAircraftPosition(v, v->x_pos, v->y_pos, v->z_pos-1); |
|
786 |
} else { |
|
787 |
SetAircraftPosition(v, v->x_pos, v->y_pos, v->z_pos+1); |
|
788 |
} |
|
789 |
} |
|
790 |
return false; |
|
791 |
} |
|
792 |
||
793 |
// Get distance from destination pos to current pos. |
|
794 |
dist = myabs(x + amd->x - v->x_pos) + myabs(y + amd->y - v->y_pos); |
|
795 |
||
796 |
// Need exact position? |
|
797 |
if (!(amd->flag & AMED_EXACTPOS) && dist <= (uint)((amd->flag&AMED_SLOWTURN)?8:4)) |
|
798 |
return true; |
|
799 |
||
800 |
// At final pos? |
|
801 |
if (dist == 0) { |
|
802 |
||
803 |
// Clamp speed to 12. |
|
804 |
if (v->cur_speed > 12) |
|
805 |
v->cur_speed = 12; |
|
806 |
||
807 |
// Change direction smoothly to final direction. |
|
808 |
dirdiff = amd->direction - v->direction; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
809 |
// if distance is 0, and plane points in right direction, no point in calling |
0 | 810 |
// UpdateAircraftSpeed(). So do it only afterwards |
811 |
if (dirdiff == 0) { |
|
812 |
v->cur_speed = 0; |
|
813 |
return true; |
|
814 |
} |
|
815 |
||
816 |
if (!UpdateAircraftSpeed(v)) |
|
817 |
return false; |
|
818 |
||
819 |
v->direction = (v->direction+((dirdiff&7)<5?1:-1)) & 7; |
|
820 |
v->cur_speed >>= 1; |
|
821 |
||
822 |
SetAircraftPosition(v, v->x_pos, v->y_pos, v->z_pos); |
|
823 |
return false; |
|
824 |
} |
|
825 |
||
826 |
// Clamp speed? |
|
827 |
if (!(amd->flag & AMED_NOSPDCLAMP) && v->cur_speed > 12) |
|
828 |
v->cur_speed = 12; |
|
829 |
||
830 |
if (!UpdateAircraftSpeed(v)) |
|
831 |
return false; |
|
832 |
||
833 |
// Decrease animation counter. |
|
834 |
if (v->load_unload_time_rem != 0) |
|
835 |
v->load_unload_time_rem--; |
|
836 |
||
837 |
// Turn. Do it slowly if in the air. |
|
838 |
newdir = GetDirectionTowards(v, x + amd->x, y + amd->y); |
|
839 |
if (newdir != v->direction) { |
|
840 |
if (amd->flag & AMED_SLOWTURN) { |
|
841 |
if (v->load_unload_time_rem == 0) { |
|
842 |
v->load_unload_time_rem = 8; |
|
843 |
} |
|
99 | 844 |
v->direction = newdir; |
0 | 845 |
} else { |
846 |
v->cur_speed >>= 1; |
|
847 |
v->direction = newdir; |
|
848 |
} |
|
849 |
} |
|
850 |
||
851 |
// Move vehicle. |
|
852 |
GetNewVehiclePos(v, &gp); |
|
853 |
v->tile = gp.new_tile; |
|
854 |
||
855 |
// If vehicle is in the air, use tile coordinate 0. |
|
856 |
if (amd->flag & (AMED_TAKEOFF | AMED_SLOWTURN | AMED_LAND)) { |
|
857 |
v->tile = 0; |
|
858 |
} |
|
859 |
||
860 |
// Adjust Z for land or takeoff? |
|
861 |
z = v->z_pos; |
|
862 |
||
863 |
if (amd->flag & AMED_TAKEOFF) { |
|
864 |
z+=2; |
|
865 |
// Determine running altitude |
|
866 |
maxz = GetAircraftFlyingAltitude(v); |
|
867 |
if (z > maxz) |
|
868 |
z = maxz; |
|
869 |
} |
|
870 |
||
871 |
if (amd->flag & AMED_LAND) { |
|
872 |
if (st->airport_tile == 0) { |
|
873 |
//FIXME -- FIXED - Aircraft_5 -> if station no longer exists, do not land |
|
874 |
// * what to do when it is the only order left, right now it just stays in 1 place? |
|
875 |
v->u.air.state = FLYING; |
|
876 |
AircraftNextAirportPos_and_Order(v); |
|
877 |
// get aircraft back on running altitude |
|
878 |
SetAircraftPosition(v, gp.x, gp.y, GetAircraftFlyingAltitude(v)); |
|
879 |
return false; |
|
880 |
} |
|
881 |
||
882 |
curz = GetSlopeZ(x, y) + 1; |
|
883 |
||
884 |
if (curz > z) { |
|
885 |
z++; |
|
886 |
} else { |
|
887 |
int t = max(1, dist-4); |
|
888 |
||
889 |
z -= ((z - curz) + t - 1) / t; |
|
890 |
if (z < curz) z = curz; |
|
891 |
} |
|
892 |
} |
|
893 |
||
894 |
// We've landed. Decrase speed when we're reaching end of runway. |
|
895 |
if (amd->flag & AMED_BRAKE) { |
|
896 |
curz = GetSlopeZ(x, y) + 1; |
|
897 |
||
898 |
if (z > curz) z--; |
|
899 |
else if (z < curz) z++; |
|
900 |
||
901 |
if (dist < 64 && v->cur_speed > 12) |
|
902 |
v->cur_speed -= 4; |
|
903 |
} |
|
904 |
||
905 |
SetAircraftPosition(v, gp.x, gp.y, z); |
|
906 |
return false; |
|
907 |
} |
|
908 |
||
909 |
static const int8 _crashed_aircraft_moddir[4] = { |
|
910 |
-1,0,0,1 |
|
911 |
}; |
|
912 |
||
913 |
static void HandleCrashedAircraft(Vehicle *v) |
|
914 |
{ |
|
915 |
uint32 r; |
|
916 |
Station *st; |
|
917 |
||
918 |
v->u.air.crashed_counter++; |
|
919 |
||
920 |
if (v->u.air.crashed_counter < 650) { |
|
921 |
if (CHANCE16R(1,32,r)) { |
|
922 |
v->direction = (v->direction+_crashed_aircraft_moddir[(r >> 16)&3]) & 7; |
|
923 |
SetAircraftPosition(v, v->x_pos, v->y_pos, v->z_pos); |
|
924 |
r = Random(); |
|
925 |
CreateEffectVehicleRel(v, |
|
926 |
4 + (r&0xF), |
|
927 |
4 + ((r>>4)&0xF), |
|
928 |
((r>>8)&0xF), |
|
929 |
EV_DEMOLISH); |
|
930 |
} |
|
931 |
} else if (v->u.air.crashed_counter >= 10000) { |
|
932 |
st = DEREF_STATION(v->u.air.targetairport); |
|
933 |
// remove rubble of crashed airplane |
|
934 |
||
935 |
// clear runway-in on all airports, set by crashing plane |
|
936 |
// small airports use AIRPORT_BUSY, city airports use RUNWAY_IN_OUT_block, etc. |
|
937 |
// but they all share the same number |
|
938 |
CLRBITS(st->airport_flags, RUNWAY_IN_block); |
|
939 |
||
940 |
BeginVehicleMove(v); |
|
941 |
EndVehicleMove(v); |
|
942 |
||
943 |
DoDeleteAircraft(v); |
|
944 |
} |
|
945 |
} |
|
946 |
||
947 |
static void HandleBrokenAircraft(Vehicle *v) |
|
948 |
{ |
|
949 |
if (v->breakdown_ctr != 1) { |
|
950 |
v->breakdown_ctr = 1; |
|
951 |
v->vehstatus |= VS_AIRCRAFT_BROKEN; |
|
952 |
||
953 |
if (v->breakdowns_since_last_service != 255) |
|
954 |
v->breakdowns_since_last_service++; |
|
955 |
InvalidateWindow(WC_VEHICLE_VIEW, v->index); |
|
956 |
InvalidateWindow(WC_VEHICLE_DETAILS, v->index); |
|
957 |
} |
|
958 |
} |
|
959 |
||
960 |
static const int8 _aircraft_smoke_xy[16] = { |
|
961 |
5,6,5,0,-5,-6,-5,0, /* x coordinates */ |
|
962 |
5,0,-5,-6,-5,0,5,6, /* y coordinate */ |
|
963 |
}; |
|
964 |
||
965 |
static void HandleAircraftSmoke(Vehicle *v) |
|
966 |
{ |
|
967 |
if (!(v->vehstatus&VS_AIRCRAFT_BROKEN)) |
|
968 |
return; |
|
969 |
||
970 |
if (v->cur_speed < 10) { |
|
971 |
v->vehstatus &= ~VS_AIRCRAFT_BROKEN; |
|
972 |
v->breakdown_ctr = 0; |
|
973 |
return; |
|
974 |
} |
|
975 |
||
976 |
if ((v->tick_counter & 0x1F) == 0) { |
|
977 |
CreateEffectVehicleRel(v, |
|
978 |
_aircraft_smoke_xy[v->direction], |
|
979 |
_aircraft_smoke_xy[v->direction + 8], |
|
980 |
2, |
|
981 |
EV_SMOKE_3 |
|
982 |
); |
|
983 |
} |
|
984 |
} |
|
985 |
||
986 |
static void ProcessAircraftOrder(Vehicle *v) |
|
987 |
{ |
|
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
988 |
Order order; |
0 | 989 |
|
990 |
// OT_GOTO_DEPOT, OT_LOADING |
|
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
991 |
if (v->current_order.type >= OT_GOTO_DEPOT && |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
992 |
v->current_order.type <= OT_LOADING) { |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
993 |
if (v->current_order.type != OT_GOTO_DEPOT || |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
994 |
!(v->current_order.flags & OF_UNLOAD)) |
0 | 995 |
return; |
996 |
} |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
997 |
|
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
998 |
if (v->current_order.type == OT_GOTO_DEPOT && |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
999 |
(v->current_order.flags & (OF_UNLOAD | OF_FULL_LOAD)) == (OF_UNLOAD | OF_FULL_LOAD) && |
593 | 1000 |
!VehicleNeedsService(v)) { |
0 | 1001 |
v->cur_order_index++; |
1002 |
} |
|
1003 |
||
1004 |
if (v->cur_order_index >= v->num_orders) |
|
1005 |
v->cur_order_index = 0; |
|
1006 |
||
1007 |
order = v->schedule_ptr[v->cur_order_index]; |
|
1008 |
||
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1009 |
if (order.type == OT_NOTHING) { |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1010 |
v->current_order.type = OT_NOTHING; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1011 |
v->current_order.flags = 0; |
0 | 1012 |
return; |
1013 |
} |
|
1014 |
||
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1015 |
if (order.type == v->current_order.type && |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1016 |
order.flags == v->current_order.flags && |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1017 |
order.station == v->current_order.station) |
0 | 1018 |
return; |
1019 |
||
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1020 |
v->current_order = order; |
0 | 1021 |
|
1022 |
// orders are changed in flight, ensure going to the right station |
|
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1023 |
if (order.type == OT_GOTO_STATION && v->u.air.state == FLYING) { |
0 | 1024 |
AircraftNextAirportPos_and_Order(v); |
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1025 |
v->u.air.targetairport = order.station; |
0 | 1026 |
} |
1027 |
||
1028 |
InvalidateVehicleOrderWidget(v); |
|
1029 |
} |
|
1030 |
||
1031 |
static void HandleAircraftLoading(Vehicle *v, int mode) |
|
1032 |
{ |
|
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1033 |
if (v->current_order.type == OT_NOTHING) |
0 | 1034 |
return; |
1035 |
||
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1036 |
if (v->current_order.type != OT_DUMMY) { |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1037 |
if (v->current_order.type != OT_LOADING) |
0 | 1038 |
return; |
1039 |
||
1040 |
if (mode != 0) |
|
1041 |
return; |
|
1042 |
||
1043 |
if (--v->load_unload_time_rem) |
|
1044 |
return; |
|
1045 |
||
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1046 |
if (v->current_order.flags & OF_FULL_LOAD && CanFillVehicle(v)) { |
0 | 1047 |
SET_EXPENSES_TYPE(EXPENSES_AIRCRAFT_INC); |
1048 |
LoadUnloadVehicle(v); |
|
1049 |
return; |
|
1050 |
} |
|
1051 |
||
1052 |
{ |
|
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1053 |
Order b = v->current_order; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1054 |
v->current_order.type = OT_NOTHING; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1055 |
v->current_order.flags = 0; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1056 |
if (!(b.flags & OF_NON_STOP)) |
0 | 1057 |
return; |
1058 |
} |
|
1059 |
} |
|
1060 |
v->cur_order_index++; |
|
1061 |
InvalidateVehicleOrderWidget(v); |
|
1062 |
} |
|
1063 |
||
1064 |
static void MaybeCrashAirplane(Vehicle *v) |
|
1065 |
{ |
|
1066 |
Station *st; |
|
1067 |
uint16 prob; |
|
1068 |
int i; |
|
1069 |
uint16 amt; |
|
1070 |
||
1071 |
st = DEREF_STATION(v->u.air.targetairport); |
|
1072 |
||
1073 |
//FIXME -- MaybeCrashAirplane -> increase crashing chances of very modern airplanes on smaller than AT_METROPOLITAN airports |
|
1074 |
prob = 0x10000 / 1500; |
|
538
5dd99f3b7eca
(svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents:
534
diff
changeset
|
1075 |
if (st->airport_type == AT_SMALL && (AircraftVehInfo(v->engine_type)->subtype & 2) && !_cheats.no_jetcrash.value) { |
0 | 1076 |
prob = 0x10000 / 20; |
1077 |
} |
|
1078 |
||
1079 |
if ((uint16)Random() > prob) |
|
1080 |
return; |
|
1081 |
||
1082 |
// Crash the airplane. Remove all goods stored at the station. |
|
1083 |
for(i=0; i!=NUM_CARGO; i++) { |
|
1084 |
st->goods[i].rating = 1; |
|
1085 |
st->goods[i].waiting_acceptance &= ~0xFFF; |
|
1086 |
} |
|
1087 |
||
1088 |
v->vehstatus |= VS_CRASHED; |
|
1089 |
v->u.air.crashed_counter = 0; |
|
1090 |
||
1091 |
CreateEffectVehicleRel(v, 4, 4, 8, EV_CRASHED_SMOKE); |
|
1092 |
||
1093 |
InvalidateWindow(WC_VEHICLE_VIEW, v->index); |
|
1094 |
||
1095 |
amt = 2; |
|
1096 |
if (v->cargo_type == CT_PASSENGERS) amt += v->cargo_count; |
|
534
17ab2f22ff74
(svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents:
507
diff
changeset
|
1097 |
SetDParam(0, amt); |
0 | 1098 |
|
1099 |
v->cargo_count = 0; |
|
1100 |
v->next->cargo_count = 0, |
|
1101 |
||
534
17ab2f22ff74
(svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents:
507
diff
changeset
|
1102 |
SetDParam(1, st->index); |
0 | 1103 |
AddNewsItem(STR_A034_PLANE_CRASH_DIE_IN_FIREBALL, |
1104 |
NEWS_FLAGS(NM_THIN, NF_VIEWPORT|NF_VEHICLE, NT_ACCIDENT, 0), |
|
1105 |
v->index, |
|
1106 |
0); |
|
1107 |
||
1108 |
ModifyStationRatingAround(TILE_FROM_XY(v->x_pos, v->y_pos), v->owner, -160, 30); |
|
541 | 1109 |
SndPlayVehicleFx(SND_12_EXPLOSION, v); |
0 | 1110 |
} |
1111 |
||
1112 |
// we've landed and just arrived at a terminal |
|
1113 |
static void AircraftEntersTerminal(Vehicle *v) |
|
1114 |
{ |
|
1115 |
Station *st; |
|
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1116 |
Order old_order; |
0 | 1117 |
|
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1118 |
if (v->current_order.type == OT_GOTO_DEPOT) |
0 | 1119 |
return; |
1120 |
||
1121 |
st = DEREF_STATION(v->u.air.targetairport); |
|
1122 |
v->last_station_visited = v->u.air.targetairport; |
|
1123 |
||
1124 |
/* Check if station was ever visited before */ |
|
1125 |
if (!(st->had_vehicle_of_type & HVOT_AIRCRAFT)) { |
|
1126 |
uint32 flags; |
|
1127 |
||
1128 |
st->had_vehicle_of_type |= HVOT_AIRCRAFT; |
|
534
17ab2f22ff74
(svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents:
507
diff
changeset
|
1129 |
SetDParam(0, st->index); |
0 | 1130 |
// show newsitem of celebrating citizens |
1131 |
flags = (v->owner == _local_player) ? NEWS_FLAGS(NM_THIN, NF_VIEWPORT|NF_VEHICLE, NT_ARRIVAL_PLAYER, 0) : NEWS_FLAGS(NM_THIN, NF_VIEWPORT|NF_VEHICLE, NT_ARRIVAL_OTHER, 0); |
|
1132 |
AddNewsItem( |
|
1133 |
STR_A033_CITIZENS_CELEBRATE_FIRST, |
|
1134 |
flags, |
|
1135 |
v->index, |
|
1136 |
0); |
|
1137 |
} |
|
1138 |
||
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1139 |
old_order = v->current_order; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1140 |
v->current_order.type = OT_LOADING; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1141 |
v->current_order.flags = 0; |
0 | 1142 |
|
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1143 |
if (old_order.type == OT_GOTO_STATION && |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1144 |
v->current_order.station == v->last_station_visited) { |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1145 |
v->current_order.flags = |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1146 |
(old_order.flags & (OF_FULL_LOAD | OF_UNLOAD)) | OF_NON_STOP; |
0 | 1147 |
} |
1148 |
||
1149 |
SET_EXPENSES_TYPE(EXPENSES_AIRCRAFT_INC); |
|
1150 |
LoadUnloadVehicle(v); |
|
755
823358e83abf
(svn r1211) -Feature: Introduce sticky windows to all vehicle windows...Just make sure you do not sticky more than 23 at a time. Also a lot of places in the code invalidated the 'status bar' of the vehicle, referring to it as widget 4. This is now widget 5 and has been #definitized
darkvater
parents:
699
diff
changeset
|
1151 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); |
0 | 1152 |
} |
1153 |
||
1154 |
static void AircraftEnterHangar(Vehicle *v) |
|
1155 |
{ |
|
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1156 |
Order old_order; |
0 | 1157 |
|
1158 |
ServiceAircraft(v); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
1159 |
|
0 | 1160 |
MaybeRenewVehicle(v, EstimateAircraftCost(v->engine_type)); |
1161 |
||
445
0e3fa3da3899
(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
|
1162 |
TriggerVehicle(v, VEHICLE_TRIGGER_DEPOT); |
0e3fa3da3899
(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
|
1163 |
|
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1164 |
if (v->current_order.type == OT_GOTO_DEPOT) { |
0 | 1165 |
InvalidateWindow(WC_VEHICLE_VIEW, v->index); |
1166 |
||
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1167 |
old_order = v->current_order; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1168 |
v->current_order.type = OT_NOTHING; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1169 |
v->current_order.flags = 0; |
0 | 1170 |
|
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1171 |
if (old_order.flags & OF_UNLOAD) { |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1172 |
v->cur_order_index++; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1173 |
} else if (old_order.flags & OF_FULL_LOAD) { // force depot visit |
0 | 1174 |
v->vehstatus |= VS_STOPPED; |
1175 |
||
1176 |
if (v->owner == _local_player) { |
|
534
17ab2f22ff74
(svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents:
507
diff
changeset
|
1177 |
SetDParam(0, v->unitnumber); |
0 | 1178 |
AddNewsItem( |
1179 |
STR_A014_AIRCRAFT_IS_WAITING_IN, |
|
1180 |
NEWS_FLAGS(NM_SMALL, NF_VIEWPORT|NF_VEHICLE, NT_ADVICE, 0), |
|
1181 |
v->index, |
|
1182 |
0); |
|
1183 |
} |
|
1184 |
} |
|
1185 |
} |
|
1186 |
} |
|
1187 |
||
1188 |
static void AircraftLand(Vehicle *v) |
|
1189 |
{ |
|
1190 |
v->sprite_width = v->sprite_height = 2; |
|
1191 |
} |
|
1192 |
||
1193 |
static void AircraftLandAirplane(Vehicle *v) |
|
1194 |
{ |
|
1195 |
AircraftLand(v); |
|
541 | 1196 |
SndPlayVehicleFx(SND_17_SKID_PLANE, v); |
0 | 1197 |
MaybeCrashAirplane(v); |
1198 |
} |
|
1199 |
||
1200 |
// set the right pos when heading to other airports after takeoff |
|
1201 |
static void AircraftNextAirportPos_and_Order(Vehicle *v) |
|
1202 |
{ |
|
1203 |
Station *st; |
|
1204 |
const AirportFTAClass *Airport; |
|
1205 |
||
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1206 |
if (v->current_order.type == OT_GOTO_STATION || |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1207 |
v->current_order.type == OT_GOTO_DEPOT) |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1208 |
v->u.air.targetairport = v->current_order.station; |
0 | 1209 |
|
1210 |
st = DEREF_STATION(v->u.air.targetairport); |
|
1211 |
Airport = GetAirport(st->airport_type); |
|
1212 |
v->u.air.pos = v->u.air.previous_pos = Airport->entry_point; |
|
1213 |
} |
|
1214 |
||
1215 |
static void AircraftLeaveHangar(Vehicle *v) |
|
1216 |
{ |
|
1217 |
v->cur_speed = 0; |
|
1218 |
v->subspeed = 0; |
|
1219 |
v->progress = 0; |
|
1220 |
v->direction = 3; |
|
1221 |
v->vehstatus &= ~VS_HIDDEN; |
|
1222 |
{ |
|
1223 |
Vehicle *u = v->next; |
|
1224 |
u->vehstatus &= ~VS_HIDDEN; |
|
1225 |
||
1226 |
// Rotor blades |
|
1227 |
if ((u=u->next) != NULL) { |
|
1228 |
u->vehstatus &= ~VS_HIDDEN; |
|
1229 |
u->cur_speed = 80; |
|
1230 |
} |
|
1231 |
} |
|
1232 |
||
578
86e352980acd
(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:
555
diff
changeset
|
1233 |
VehicleServiceInDepot(v); |
0 | 1234 |
SetAircraftPosition(v, v->x_pos, v->y_pos, v->z_pos); |
1235 |
InvalidateWindow(WC_VEHICLE_DEPOT, v->tile); |
|
1236 |
} |
|
1237 |
||
1238 |
||
1239 |
//////////////////////////////////////////////////////////////////////////////// |
|
1240 |
/////////////////// AIRCRAFT MOVEMENT SCHEME //////////////////////////////// |
|
1241 |
//////////////////////////////////////////////////////////////////////////////// |
|
1242 |
static void AircraftEventHandler_EnterTerminal(Vehicle *v, const AirportFTAClass *Airport) |
|
1243 |
{ |
|
1244 |
AircraftEntersTerminal(v); |
|
1245 |
v->u.air.state = Airport->layout[v->u.air.pos].heading; |
|
1246 |
} |
|
1247 |
||
1248 |
static void AircraftEventHandler_EnterHangar(Vehicle *v, const AirportFTAClass *Airport) |
|
1249 |
{ |
|
1250 |
AircraftEnterHangar(v); |
|
1251 |
v->u.air.state = Airport->layout[v->u.air.pos].heading; |
|
1252 |
} |
|
1253 |
||
1254 |
// In an Airport Hangar |
|
1255 |
static void AircraftEventHandler_InHangar(Vehicle *v, const AirportFTAClass *Airport) |
|
1256 |
{ |
|
1257 |
// if we just arrived, execute EnterHangar first |
|
1258 |
if (v->u.air.previous_pos != v->u.air.pos) { |
|
1259 |
AircraftEventHandler_EnterHangar(v, Airport); |
|
1260 |
return; |
|
1261 |
} |
|
1262 |
||
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1263 |
// if we were sent to the depot, stay there |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1264 |
if (v->current_order.type == OT_GOTO_DEPOT && (v->vehstatus & VS_STOPPED)) { |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1265 |
v->current_order.type = OT_NOTHING; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1266 |
v->current_order.flags = 0; |
0 | 1267 |
return; |
1268 |
} |
|
1269 |
||
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1270 |
if (v->current_order.type != OT_GOTO_STATION && |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1271 |
v->current_order.type != OT_GOTO_DEPOT) |
0 | 1272 |
return; |
1273 |
||
1274 |
// if the block of the next position is busy, stay put |
|
1275 |
if (AirportHasBlock(v, &Airport->layout[v->u.air.pos], Airport)) {return;} |
|
1276 |
||
1277 |
// We are already at the target airport, we need to find a terminal |
|
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1278 |
if (v->current_order.station == v->u.air.targetairport) { |
0 | 1279 |
// FindFreeTerminal: |
1280 |
// 1. Find a free terminal, 2. Occupy it, 3. Set the vehicle's state to that terminal |
|
1281 |
if (v->subtype != 0) {if(!AirportFindFreeTerminal(v, Airport)) {return;}} // airplane |
|
1282 |
else {if(!AirportFindFreeHelipad(v, Airport)) {return;}} // helicopter |
|
1283 |
} |
|
1284 |
else { // Else prepare for launch. |
|
1285 |
// airplane goto state takeoff, helicopter to helitakeoff |
|
1286 |
v->u.air.state = (v->subtype != 0) ? TAKEOFF : HELITAKEOFF; |
|
1287 |
} |
|
1288 |
AircraftLeaveHangar(v); |
|
1289 |
AirportMove(v, Airport); |
|
1290 |
} |
|
1291 |
||
1292 |
// At one of the Airport's Terminals |
|
1293 |
static void AircraftEventHandler_AtTerminal(Vehicle *v, const AirportFTAClass *Airport) |
|
1294 |
{ |
|
1295 |
// if we just arrived, execute EnterTerminal first |
|
1296 |
if (v->u.air.previous_pos != v->u.air.pos) { |
|
1297 |
AircraftEventHandler_EnterTerminal(v, Airport); |
|
1298 |
// on an airport with helipads, a helicopter will always land there |
|
1299 |
// and get serviced at the same time - patch setting |
|
1300 |
if (_patches.serviceathelipad) { |
|
1301 |
if (v->subtype == 0 && Airport->nofhelipads > 0) { |
|
1302 |
// an exerpt of ServiceAircraft, without the invisibility stuff |
|
1303 |
v->date_of_last_service = _date; |
|
1304 |
v->breakdowns_since_last_service = 0; |
|
1305 |
v->reliability = _engines[v->engine_type].reliability; |
|
1306 |
InvalidateWindow(WC_VEHICLE_DETAILS, v->index); |
|
1307 |
} |
|
1308 |
} |
|
1309 |
return; |
|
1310 |
} |
|
1311 |
||
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1312 |
if (v->current_order.type == OT_NOTHING) return; |
0 | 1313 |
|
1314 |
// if the block of the next position is busy, stay put |
|
1315 |
if (AirportHasBlock(v, &Airport->layout[v->u.air.pos], Airport)) { |
|
1316 |
return; |
|
1317 |
} |
|
1318 |
||
1319 |
// airport-road is free. We either have to go to another airport, or to the hangar |
|
1320 |
// ---> start moving |
|
1321 |
||
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1322 |
switch (v->current_order.type) { |
0 | 1323 |
case OT_GOTO_STATION: // ready to fly to another airport |
1324 |
// airplane goto state takeoff, helicopter to helitakeoff |
|
1325 |
v->u.air.state = (v->subtype != 0) ? TAKEOFF : HELITAKEOFF; |
|
1326 |
break; |
|
1327 |
case OT_GOTO_DEPOT: // visit hangar for serivicing, sale, etc. |
|
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1328 |
if (v->current_order.station == v->u.air.targetairport) |
0 | 1329 |
v->u.air.state = HANGAR; |
1330 |
else |
|
1331 |
v->u.air.state = (v->subtype != 0) ? TAKEOFF : HELITAKEOFF; |
|
1332 |
break; |
|
1333 |
default: // orders have been deleted (no orders), goto depot and don't bother us |
|
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1334 |
v->current_order.type = OT_NOTHING; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1335 |
v->current_order.flags = 0; |
0 | 1336 |
v->u.air.state = HANGAR; |
1337 |
} |
|
1338 |
AirportMove(v, Airport); |
|
1339 |
} |
|
1340 |
||
1341 |
static void AircraftEventHandler_General(Vehicle *v, const AirportFTAClass *Airport) |
|
1342 |
{ |
|
70
60216d94f7c1
(svn r71) -Cheat: [976127] no extra frequent jet crash on small airports (truesatan)
darkvater
parents:
19
diff
changeset
|
1343 |
DEBUG(misc, 0) ("OK, you shouldn't be here, check your Airport Scheme!"); |
60216d94f7c1
(svn r71) -Cheat: [976127] no extra frequent jet crash on small airports (truesatan)
darkvater
parents:
19
diff
changeset
|
1344 |
assert(0); |
0 | 1345 |
} |
1346 |
||
1347 |
static void AircraftEventHandler_TakeOff(Vehicle *v, const AirportFTAClass *Airport) { |
|
1348 |
PlayAircraftSound(v); // play takeoffsound for airplanes |
|
1349 |
v->u.air.state = STARTTAKEOFF; |
|
1350 |
} |
|
1351 |
||
1352 |
static void AircraftEventHandler_StartTakeOff(Vehicle *v, const AirportFTAClass *Airport) |
|
1353 |
{ |
|
1354 |
v->sprite_width = v->sprite_height = 24; // ??? no idea what this is |
|
1355 |
v->u.air.state = ENDTAKEOFF; |
|
1356 |
} |
|
1357 |
||
1358 |
static void AircraftEventHandler_EndTakeOff(Vehicle *v, const AirportFTAClass *Airport) |
|
1359 |
{ |
|
1360 |
v->u.air.state = FLYING; |
|
1361 |
// get the next position to go to, differs per airport |
|
1362 |
AircraftNextAirportPos_and_Order(v); |
|
1363 |
} |
|
1364 |
||
1365 |
static void AircraftEventHandler_HeliTakeOff(Vehicle *v, const AirportFTAClass *Airport) |
|
1366 |
{ |
|
1367 |
v->sprite_width = v->sprite_height = 24; // ??? no idea what this is |
|
1368 |
v->u.air.state = FLYING; |
|
1369 |
// get the next position to go to, differs per airport |
|
1370 |
AircraftNextAirportPos_and_Order(v); |
|
1371 |
} |
|
1372 |
||
1373 |
static void AircraftEventHandler_Flying(Vehicle *v, const AirportFTAClass *Airport) |
|
1374 |
{ |
|
1375 |
Station *st; |
|
1376 |
byte landingtype; |
|
1377 |
AirportFTA *current; |
|
1378 |
uint16 tcur_speed, tsubspeed; |
|
1379 |
||
1380 |
st = DEREF_STATION(v->u.air.targetairport); |
|
1381 |
// flying device is accepted at this station |
|
1382 |
// small airport --> no helicopters (AIRCRAFT_ONLY) |
|
1383 |
// all other airports --> all types of flying devices (ALL) |
|
1384 |
// heliport/oilrig, etc --> no airplanes (HELICOPTERS_ONLY) |
|
1385 |
// runway busy or not allowed to use this airstation, circle |
|
1386 |
if (! (v->subtype == Airport->acc_planes || |
|
1387 |
st->airport_tile == 0 || (st->owner != OWNER_NONE && st->owner != v->owner) )) { |
|
1388 |
||
1389 |
// {32,FLYING,NOTHING_block,37}, {32,LANDING,N,33}, {32,HELILANDING,N,41}, |
|
1390 |
// if it is an airplane, look for LANDING, for helicopter HELILANDING |
|
1391 |
// it is possible to choose from multiple landing runways, so loop until a free one is found |
|
1392 |
landingtype = (v->subtype != 0) ? LANDING : HELILANDING; |
|
1393 |
current = Airport->layout[v->u.air.pos].next_in_chain; |
|
1394 |
while (current != NULL) { |
|
1395 |
if (current->heading == landingtype) { |
|
1396 |
// save speed before, since if AirportHasBlock is false, it resets them to 0 |
|
1397 |
// we don't want that for plane in air |
|
1398 |
// hack for speed thingie |
|
1399 |
tcur_speed = v->cur_speed; |
|
1400 |
tsubspeed = v->subspeed; |
|
1401 |
if (!AirportHasBlock(v, current, Airport)) { |
|
1402 |
v->u.air.state = landingtype; // LANDING / HELILANDING |
|
1403 |
// it's a bit dirty, but I need to set position to next position, otherwise |
|
1404 |
// if there are multiple runways, plane won't know which one it took (because |
|
1405 |
// they all have heading LANDING). And also occupy that block! |
|
1406 |
v->u.air.pos = current->next_position; |
|
1407 |
SETBITS(st->airport_flags, Airport->layout[v->u.air.pos].block); |
|
1408 |
return; |
|
1409 |
} |
|
1410 |
v->cur_speed = tcur_speed; |
|
1411 |
v->subspeed = tsubspeed; |
|
1412 |
} |
|
1413 |
current = current->next_in_chain; |
|
1414 |
} |
|
1415 |
} |
|
1416 |
v->u.air.state = FLYING; |
|
1417 |
v->u.air.pos = Airport->layout[v->u.air.pos].next_position; |
|
1418 |
} |
|
1419 |
||
1420 |
static void AircraftEventHandler_Landing(Vehicle *v, const AirportFTAClass *Airport) |
|
1421 |
{ |
|
1422 |
AircraftLandAirplane(v); // maybe crash airplane |
|
1423 |
v->u.air.state = ENDLANDING; |
|
1424 |
} |
|
1425 |
||
1426 |
static void AircraftEventHandler_HeliLanding(Vehicle *v, const AirportFTAClass *Airport) |
|
1427 |
{ |
|
1428 |
AircraftLand(v); // helicopters don't crash |
|
1429 |
v->u.air.state = HELIENDLANDING; |
|
1430 |
} |
|
1431 |
||
1432 |
static void AircraftEventHandler_EndLanding(Vehicle *v, const AirportFTAClass *Airport) |
|
1433 |
{ |
|
1434 |
// next block busy, don't do a thing, just wait |
|
1435 |
if(AirportHasBlock(v, &Airport->layout[v->u.air.pos], Airport)) {return;} |
|
1436 |
||
1437 |
// if going to terminal (OT_GOTO_STATION) choose one |
|
1438 |
// 1. in case all terminals are busy AirportFindFreeTerminal() returns false or |
|
1439 |
// 2. not going for terminal (but depot, no order), |
|
1440 |
// --> get out of the way to the hangar. |
|
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1441 |
if (v->current_order.type == OT_GOTO_STATION) { |
0 | 1442 |
if (AirportFindFreeTerminal(v, Airport)) {return;} |
1443 |
} |
|
1444 |
v->u.air.state = HANGAR; |
|
1445 |
||
1446 |
} |
|
1447 |
||
1448 |
static void AircraftEventHandler_HeliEndLanding(Vehicle *v, const AirportFTAClass *Airport) |
|
1449 |
{ |
|
1450 |
// next block busy, don't do a thing, just wait |
|
1451 |
if(AirportHasBlock(v, &Airport->layout[v->u.air.pos], Airport)) {return;} |
|
1452 |
||
1453 |
// if going to helipad (OT_GOTO_STATION) choose one. If airport doesn't have helipads, choose terminal |
|
1454 |
// 1. in case all terminals/helipads are busy (AirportFindFreeHelipad() returns false) or |
|
1455 |
// 2. not going for terminal (but depot, no order), |
|
1456 |
// --> get out of the way to the hangar IF there are terminals on the airport. |
|
1457 |
// --> else TAKEOFF |
|
1458 |
// the reason behind this is that if an airport has a terminal, it also has a hangar. Airplanes |
|
1459 |
// must go to a hangar. |
|
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1460 |
if (v->current_order.type == OT_GOTO_STATION) { |
0 | 1461 |
if (AirportFindFreeHelipad(v, Airport)) {return;} |
1462 |
} |
|
1463 |
v->u.air.state = (Airport->nofterminals != 0) ? HANGAR : HELITAKEOFF; |
|
1464 |
} |
|
1465 |
||
1466 |
typedef void AircraftStateHandler(Vehicle *v, const AirportFTAClass *Airport); |
|
1467 |
static AircraftStateHandler * const _aircraft_state_handlers[] = { |
|
1468 |
AircraftEventHandler_General, // TO_ALL = 0 |
|
1469 |
AircraftEventHandler_InHangar, // HANGAR = 1 |
|
1470 |
AircraftEventHandler_AtTerminal, // TERM1 = 2 |
|
1471 |
AircraftEventHandler_AtTerminal, // TERM2 = 3 |
|
1472 |
AircraftEventHandler_AtTerminal, // TERM3 = 4 |
|
1473 |
AircraftEventHandler_AtTerminal, // TERM4 = 5 |
|
1474 |
AircraftEventHandler_AtTerminal, // TERM5 = 6 |
|
1475 |
AircraftEventHandler_AtTerminal, // TERM6 = 7 |
|
1476 |
AircraftEventHandler_AtTerminal, // HELIPAD1 = 8 |
|
1477 |
AircraftEventHandler_AtTerminal, // HELIPAD2 = 9 |
|
1478 |
AircraftEventHandler_TakeOff, // TAKEOFF = 10 |
|
1479 |
AircraftEventHandler_StartTakeOff, // STARTTAKEOFF = 11 |
|
1480 |
AircraftEventHandler_EndTakeOff, // ENDTAKEOFF = 12 |
|
1481 |
AircraftEventHandler_HeliTakeOff, // HELITAKEOFF = 13 |
|
1482 |
AircraftEventHandler_Flying, // FLYING = 14 |
|
1483 |
AircraftEventHandler_Landing, // LANDING = 15 |
|
1484 |
AircraftEventHandler_EndLanding, // ENDLANDING = 16 |
|
1485 |
AircraftEventHandler_HeliLanding, // HELILANDING = 17 |
|
1486 |
AircraftEventHandler_HeliEndLanding,// HELIENDLANDING = 18 |
|
1487 |
}; |
|
1488 |
||
1489 |
static void AirportClearBlock(Vehicle *v, const AirportFTAClass *Airport) |
|
1490 |
{ |
|
1491 |
Station *st; |
|
1492 |
// we have left the previous block, and entered the new one. Free the previous block |
|
1493 |
if (Airport->layout[v->u.air.previous_pos].block != Airport->layout[v->u.air.pos].block) { |
|
1494 |
st = DEREF_STATION(v->u.air.targetairport); |
|
1495 |
CLRBITS(st->airport_flags, Airport->layout[v->u.air.previous_pos].block); |
|
1496 |
} |
|
1497 |
} |
|
1498 |
||
1499 |
static void AirportGoToNextPosition(Vehicle *v, const AirportFTAClass *Airport) |
|
1500 |
{ |
|
1501 |
// if aircraft is not in position, wait until it is |
|
1502 |
if (!Aircraft_5(v)) {return;} |
|
1503 |
||
1504 |
AirportClearBlock(v, Airport); |
|
1505 |
AirportMove(v, Airport); // move aircraft to next position |
|
1506 |
} |
|
1507 |
||
1508 |
// gets pos from vehicle and next orders |
|
1509 |
static bool AirportMove(Vehicle *v, const AirportFTAClass *Airport) |
|
1510 |
{ |
|
1511 |
AirportFTA *current; |
|
1512 |
byte prev_pos; |
|
1513 |
bool retval = false; |
|
1514 |
||
1515 |
// error handling |
|
1516 |
if (v->u.air.pos >= Airport->nofelements) { |
|
70
60216d94f7c1
(svn r71) -Cheat: [976127] no extra frequent jet crash on small airports (truesatan)
darkvater
parents:
19
diff
changeset
|
1517 |
DEBUG(misc, 0) ("position %d is not valid for current airport. Max position is %d", v->u.air.pos, Airport->nofelements-1); |
0 | 1518 |
assert(v->u.air.pos < Airport->nofelements); |
1519 |
} |
|
1520 |
||
1521 |
current = &Airport->layout[v->u.air.pos]; |
|
1522 |
// we have arrived in an important state (eg terminal, hangar, etc.) |
|
1523 |
if (current->heading == v->u.air.state) { |
|
1524 |
prev_pos = v->u.air.pos; // location could be changed in state, so save it before-hand |
|
1525 |
_aircraft_state_handlers[v->u.air.state](v, Airport); |
|
1526 |
if (v->u.air.state != FLYING) {v->u.air.previous_pos = prev_pos;} |
|
1527 |
return true; |
|
1528 |
} |
|
1529 |
||
1530 |
v->u.air.previous_pos = v->u.air.pos; // save previous location |
|
1531 |
||
1532 |
// there is only one choice to move to |
|
1533 |
if (current->next_in_chain == NULL) { |
|
1534 |
if (AirportSetBlocks(v, current, Airport)) { |
|
1535 |
v->u.air.pos = current->next_position; |
|
1536 |
} // move to next position |
|
1537 |
return retval; |
|
1538 |
} |
|
1539 |
||
1540 |
// there are more choices to choose from, choose the one that |
|
1541 |
// matches our heading |
|
1542 |
do { |
|
1543 |
if (v->u.air.state == current->heading || current->heading == TO_ALL) { |
|
1544 |
if (AirportSetBlocks(v, current, Airport)) { |
|
1545 |
v->u.air.pos = current->next_position; |
|
1546 |
} // move to next position |
|
1547 |
return retval; |
|
1548 |
} |
|
1549 |
current = current->next_in_chain; |
|
1550 |
} while (current != NULL); |
|
1551 |
||
70
60216d94f7c1
(svn r71) -Cheat: [976127] no extra frequent jet crash on small airports (truesatan)
darkvater
parents:
19
diff
changeset
|
1552 |
DEBUG(misc, 0) ("Cannot move further on Airport...! pos:%d state:%d", v->u.air.pos, v->u.air.state); |
60216d94f7c1
(svn r71) -Cheat: [976127] no extra frequent jet crash on small airports (truesatan)
darkvater
parents:
19
diff
changeset
|
1553 |
DEBUG(misc, 0) ("Airport entry point: %d, Vehicle: %d", Airport->entry_point, v->index); |
0 | 1554 |
assert(0); |
1555 |
return false; |
|
1556 |
} |
|
1557 |
||
1558 |
// returns true if the road ahead is busy, eg. you must wait before proceeding |
|
1559 |
static bool AirportHasBlock(Vehicle *v, AirportFTA *current_pos, const AirportFTAClass *Airport) |
|
1560 |
{ |
|
1561 |
Station *st; |
|
1562 |
uint32 airport_flags; |
|
1563 |
AirportFTA *next, *reference; |
|
1564 |
reference = &Airport->layout[v->u.air.pos]; |
|
1565 |
next = &Airport->layout[current_pos->next_position]; |
|
1566 |
||
1567 |
// same block, then of course we can move |
|
1568 |
if (Airport->layout[current_pos->position].block != next->block) { |
|
1569 |
airport_flags = next->block; |
|
1570 |
st = DEREF_STATION(v->u.air.targetairport); |
|
1571 |
// check additional possible extra blocks |
|
1572 |
if (current_pos != reference && current_pos->block != NOTHING_block) { |
|
1573 |
airport_flags |= current_pos->block; |
|
1574 |
} |
|
1575 |
||
1576 |
if (HASBITS(st->airport_flags, airport_flags)) { |
|
1577 |
v->cur_speed = 0; |
|
1578 |
v->subspeed = 0; |
|
1579 |
return true; |
|
1580 |
} |
|
1581 |
} |
|
1582 |
return false; |
|
1583 |
} |
|
1584 |
||
1585 |
// returns true on success. Eg, next block was free and we have occupied it |
|
1586 |
static bool AirportSetBlocks(Vehicle *v, AirportFTA *current_pos, const AirportFTAClass *Airport) |
|
1587 |
{ |
|
1588 |
Station *st; |
|
1589 |
uint32 airport_flags; |
|
1590 |
AirportFTA *current, *reference, *next; |
|
1591 |
next = &Airport->layout[current_pos->next_position]; |
|
1592 |
reference = &Airport->layout[v->u.air.pos]; |
|
1593 |
||
1594 |
// if the next position is in another block, check it and wait until it is free |
|
1595 |
if (Airport->layout[current_pos->position].block != next->block) { |
|
1596 |
airport_flags = next->block; |
|
1597 |
st = DEREF_STATION(v->u.air.targetairport); |
|
1598 |
//search for all all elements in the list with the same state, and blocks != N |
|
1599 |
// this means more blocks should be checked/set |
|
1600 |
current = current_pos; |
|
1601 |
if (current == reference) { current = current->next_in_chain;} |
|
1602 |
while (current != NULL) { |
|
1603 |
if (current->heading == current_pos->heading && current->block != 0) { |
|
1604 |
airport_flags |= current->block; |
|
1605 |
break; |
|
1606 |
} |
|
1607 |
current = current->next_in_chain; |
|
1608 |
}; |
|
1609 |
||
1610 |
// if the block to be checked is in the next position, then exclude that from |
|
1611 |
// checking, because it has been set by the airplane before |
|
1612 |
if (current_pos->block == next->block) {airport_flags ^= next->block;} |
|
1613 |
||
1614 |
if (HASBITS(st->airport_flags, airport_flags)) { |
|
1615 |
v->cur_speed = 0; |
|
1616 |
v->subspeed = 0; |
|
1617 |
return false; |
|
1618 |
} |
|
1619 |
||
1620 |
if (next->block != NOTHING_block) { |
|
1621 |
SETBITS(st->airport_flags, airport_flags); // occupy next block |
|
1622 |
} |
|
1623 |
} |
|
1624 |
return true; |
|
1625 |
} |
|
1626 |
||
1627 |
static bool FreeTerminal(Vehicle *v, byte i, byte last_terminal) |
|
1628 |
{ |
|
1629 |
Station *st = DEREF_STATION(v->u.air.targetairport); |
|
1630 |
for (; i < last_terminal; i++) { |
|
1631 |
if (!HASBIT(st->airport_flags, i)) { |
|
1632 |
// TERMINAL# HELIPAD# |
|
1633 |
v->u.air.state = i + TERM1; // start moving to that terminal/helipad |
|
1634 |
SETBIT(st->airport_flags, i); // occupy terminal/helipad |
|
1635 |
return true; |
|
1636 |
} |
|
1637 |
} |
|
1638 |
return false; |
|
1639 |
} |
|
1640 |
||
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
1641 |
static bool AirportFindFreeTerminal(Vehicle *v, const AirportFTAClass *Airport) |
0 | 1642 |
{ |
1643 |
byte nofterminalspergroup, i; |
|
1644 |
AirportFTA *temp; |
|
1645 |
Station *st; |
|
1646 |
||
1647 |
/* example of more terminalgroups |
|
1648 |
{0,HANGAR,NOTHING_block,1}, {0,255,TERM_GROUP1_block,0}, {0,255,TERM_GROUP2_ENTER_block,1}, {0,0,N,1}, |
|
1649 |
Heading 255 denotes a group. We see 2 groups here: |
|
1650 |
1. group 0 -- TERM_GROUP1_block (check block) |
|
1651 |
2. group 1 -- TERM_GROUP2_ENTER_block (check block) |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
1652 |
First in line is checked first, group 0. If the block (TERM_GROUP1_block) is free, it |
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
1653 |
looks at the corresponding terminals of that group. If no free ones are found, other |
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
1654 |
possible groups are checked (in this case group 1, since that is after group 0). If that |
0 | 1655 |
fails, then attempt fails and plane waits |
1656 |
*/ |
|
1657 |
if (Airport->nofterminalgroups > 1) { |
|
1658 |
st = DEREF_STATION(v->u.air.targetairport); |
|
1659 |
nofterminalspergroup = Airport->nofterminals / Airport->nofterminalgroups; |
|
1660 |
temp = Airport->layout[v->u.air.pos].next_in_chain; |
|
1661 |
while (temp != NULL) { |
|
1662 |
if (temp->heading == 255) { |
|
1663 |
if (!HASBITS(st->airport_flags, temp->block)) { |
|
1664 |
i = temp->next_position * nofterminalspergroup; // next_position denotes the group to check |
|
1665 |
// only that group will be checked (eg 6 terms, 2 groups) |
|
1666 |
// with i = 0 terms 1 - 3 and |
|
1667 |
// with i = 1 terms 4 - 6 |
|
1668 |
if (FreeTerminal(v, i, i + nofterminalspergroup)) {return true;} |
|
1669 |
} |
|
1670 |
} |
|
1671 |
else {return false;} // once the heading isn't 255, we've exhausted the possible blocks. So we cannot move |
|
1672 |
temp = temp->next_in_chain; |
|
1673 |
} |
|
1674 |
} |
|
1675 |
||
1676 |
// if there is only 1 terminalgroup, all terminals are checked (starting from 0 to max) |
|
1677 |
return FreeTerminal(v, 0, Airport->nofterminals); |
|
1678 |
} |
|
1679 |
||
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
1680 |
static bool AirportFindFreeHelipad(Vehicle *v, const AirportFTAClass *Airport) |
0 | 1681 |
{ |
1682 |
Station *st; |
|
1683 |
byte nofhelipadspergroup, i; |
|
1684 |
AirportFTA *temp; |
|
1685 |
||
1686 |
// if an airport doesn't have helipads, use terminals |
|
1687 |
if (Airport->nofhelipads == 0) {return AirportFindFreeTerminal(v, Airport);} |
|
1688 |
||
1689 |
// if there are more helicoptergroups, pick one, just as in AirportFindFreeTerminal() |
|
1690 |
if (Airport->nofhelipadgroups > 1) { |
|
1691 |
st = DEREF_STATION(v->u.air.targetairport); |
|
1692 |
nofhelipadspergroup = Airport->nofhelipads / Airport->nofhelipadgroups; |
|
1693 |
temp = Airport->layout[v->u.air.pos].next_in_chain; |
|
1694 |
while (temp != NULL) { |
|
1695 |
if (temp->heading == 255) { |
|
1696 |
if (!HASBITS(st->airport_flags, temp->block)) { |
|
1697 |
i = temp->next_position * nofhelipadspergroup; // next position is the group to check |
|
1698 |
// heliports start from after TERMINALS, so MAX_TERMINALS needs to be added |
|
1699 |
if (FreeTerminal(v, i + MAX_TERMINALS, i + MAX_TERMINALS + nofhelipadspergroup)) {return true;} |
|
1700 |
} |
|
1701 |
} |
|
1702 |
else {return false;} // once the heading isn't 255, we've exhausted the possible blocks. So we cannot move |
|
1703 |
temp = temp->next_in_chain; |
|
1704 |
} |
|
1705 |
} |
|
1706 |
// only 1 helicoptergroup, check all helipads |
|
1707 |
// The blocks for helipads start after the last terminal (MAX_TERMINALS) |
|
1708 |
else {return FreeTerminal(v, MAX_TERMINALS, Airport->nofhelipads + MAX_TERMINALS);} |
|
1709 |
return false; // it shouldn't get here anytime, but just to be sure |
|
1710 |
} |
|
1711 |
||
1712 |
static void AircraftEventHandler(Vehicle *v, int loop) |
|
1713 |
{ |
|
1714 |
v->tick_counter++; |
|
1715 |
||
1716 |
if (v->vehstatus & VS_CRASHED) { |
|
1717 |
HandleCrashedAircraft(v); |
|
1718 |
return; |
|
1719 |
} |
|
1720 |
||
1721 |
/* exit if aircraft is stopped */ |
|
1722 |
if (v->vehstatus & VS_STOPPED) |
|
1723 |
return; |
|
1724 |
||
1725 |
/* aircraft is broken down? */ |
|
1726 |
if (v->breakdown_ctr != 0) { |
|
1727 |
if (v->breakdown_ctr <= 2) { |
|
1728 |
HandleBrokenAircraft(v); |
|
1729 |
} else { |
|
1730 |
v->breakdown_ctr--; |
|
1731 |
} |
|
1732 |
} |
|
1733 |
||
1734 |
HandleAircraftSmoke(v); |
|
1735 |
ProcessAircraftOrder(v); |
|
1736 |
HandleAircraftLoading(v, loop); |
|
1737 |
||
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1738 |
if (v->current_order.type >= OT_LOADING) |
0 | 1739 |
return; |
1740 |
||
1741 |
// pass the right airport structure to the functions |
|
1742 |
// DEREF_STATION gets target airport (Station *st), its type is passed to GetAirport |
|
1743 |
// that returns the correct layout depending on type |
|
1744 |
AirportGoToNextPosition(v, GetAirport(DEREF_STATION(v->u.air.targetairport)->airport_type)); |
|
1745 |
} |
|
1746 |
||
1747 |
void Aircraft_Tick(Vehicle *v) |
|
1748 |
{ |
|
1749 |
int i; |
|
1750 |
||
1751 |
if (v->subtype > 2) |
|
1752 |
return; |
|
1753 |
||
1754 |
if (v->subtype == 0) |
|
1755 |
HelicopterTickHandler(v); |
|
1756 |
||
1757 |
AgeAircraftCargo(v); |
|
1758 |
||
1759 |
for(i=0; i!=6; i++) { |
|
1760 |
AircraftEventHandler(v, i); |
|
1761 |
if (v->type != VEH_Aircraft) // In case it was deleted |
|
1762 |
break; |
|
1763 |
} |
|
1764 |
} |
|
1765 |
||
1766 |
// need to be called to load aircraft from old version |
|
1767 |
void UpdateOldAircraft() |
|
1768 |
{ |
|
1769 |
Station *st; |
|
1770 |
Vehicle *v_oldstyle; |
|
1771 |
GetNewVehiclePosResult gp; |
|
1772 |
||
1773 |
// set airport_flags to 0 for all airports just to be sure |
|
1774 |
FOR_ALL_STATIONS(st) { |
|
1775 |
st->airport_flags = 0; // reset airport |
|
1776 |
// type of oilrig has been moved, update it (3-5) |
|
1777 |
if (st->airport_type == 3) {st->airport_type = AT_OILRIG;} |
|
1778 |
} |
|
1779 |
||
1780 |
FOR_ALL_VEHICLES(v_oldstyle) { |
|
1781 |
// airplane has another vehicle with subtype 4 (shadow), helicopter also has 3 (rotor) |
|
1782 |
// skip those |
|
1783 |
if (v_oldstyle->type == VEH_Aircraft && v_oldstyle->subtype <= 2) { |
|
1784 |
// airplane in terminal stopped doesn't hurt anyone, so goto next |
|
1785 |
if ((v_oldstyle->vehstatus & VS_STOPPED) && (v_oldstyle->u.air.state == 0)) { |
|
1786 |
v_oldstyle->u.air.state = HANGAR; |
|
1787 |
continue; |
|
1788 |
} |
|
1789 |
||
1790 |
AircraftLeaveHangar(v_oldstyle); // make airplane visible if it was in a depot for example |
|
1791 |
v_oldstyle->vehstatus &= ~VS_STOPPED; // make airplane moving |
|
1792 |
v_oldstyle->u.air.state = FLYING; |
|
1793 |
AircraftNextAirportPos_and_Order(v_oldstyle); // move it to the entry point of the airport |
|
1794 |
GetNewVehiclePos(v_oldstyle, &gp); // get the position of the plane (to be used for setting) |
|
1795 |
v_oldstyle->tile = 0; // aircraft in air is tile=0 |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
1796 |
|
0 | 1797 |
// correct speed of helicopter-rotors |
1798 |
if (v_oldstyle->subtype == 0) {v_oldstyle->next->next->cur_speed = 32;} |
|
1799 |
||
1800 |
// set new position x,y,z |
|
1801 |
SetAircraftPosition(v_oldstyle, gp.x, gp.y, GetAircraftFlyingAltitude(v_oldstyle)); |
|
1802 |
} |
|
1803 |
} |
|
1804 |
} |
|
1805 |
||
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
164
diff
changeset
|
1806 |
void UpdateAirplanesOnNewStation(Station *st) |
0 | 1807 |
{ |
1808 |
GetNewVehiclePosResult gp; |
|
1809 |
Vehicle *v; |
|
1810 |
byte takeofftype; |
|
1811 |
uint16 cnt; |
|
1812 |
// only 1 station is updated per function call, so it is enough to get entry_point once |
|
1813 |
const AirportFTAClass *ap = GetAirport(st->airport_type); |
|
1814 |
FOR_ALL_VEHICLES(v) { |
|
1815 |
if (v->type == VEH_Aircraft && v->subtype <= 2) { |
|
1816 |
if (v->u.air.targetairport == st->index) { // if heading to this airport |
|
1817 |
/* update position of airplane. If plane is not flying, landing, or taking off |
|
1818 |
you cannot delete airport, so it doesn't matter |
|
1819 |
*/ |
|
1820 |
if (v->u.air.state >= FLYING) { // circle around |
|
1821 |
v->u.air.pos = v->u.air.previous_pos = ap->entry_point; |
|
1822 |
v->u.air.state = FLYING; |
|
1823 |
// landing plane needs to be reset to flying height (only if in pause mode upgrade, |
|
1824 |
// in normal mode, plane is reset in Aircraft_5. It doesn't hurt for FLYING |
|
1825 |
GetNewVehiclePos(v, &gp); |
|
1826 |
// set new position x,y,z |
|
1827 |
SetAircraftPosition(v, gp.x, gp.y, GetAircraftFlyingAltitude(v)); |
|
1828 |
} |
|
1829 |
else { |
|
1830 |
assert(v->u.air.state == ENDTAKEOFF || v->u.air.state == HELITAKEOFF); |
|
1831 |
takeofftype = (v->subtype == 0) ? HELITAKEOFF : ENDTAKEOFF; |
|
1832 |
// search in airportdata for that heading |
|
1833 |
// easiest to do, since this doesn't happen a lot |
|
1834 |
for (cnt = 0; cnt < ap->nofelements; cnt++) { |
|
1835 |
if (ap->layout[cnt].heading == takeofftype) { |
|
1836 |
v->u.air.pos = ap->layout[cnt].position; |
|
1837 |
break; |
|
1838 |
} |
|
1839 |
} |
|
1840 |
} |
|
1841 |
} |
|
1842 |
} |
|
1843 |
} |
|
1844 |
} |