aircraft_cmd.c
author bjarni
Sun, 03 Sep 2006 11:49:38 +0000
changeset 4529 2bfac24af02a
parent 4527 b18634a31a4a
child 4544 b2d5ad597e4d
permissions -rw-r--r--
(svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
Now all vehicles are serviced when it's time for service and they are in a depot
This will avoid the goto depot order from ever showing up when in a depot
2186
461a2aff3486 (svn r2701) Insert Id tags into all source files
tron
parents: 2163
diff changeset
     1
/* $Id$ */
461a2aff3486 (svn r2701) Insert Id tags into all source files
tron
parents: 2163
diff changeset
     2
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
     3
#include "stdafx.h"
1891
92a3b0aa0946 (svn r2397) - CodeChange: rename all "ttd" files to "openttd" files.
Darkvater
parents: 1877
diff changeset
     4
#include "openttd.h"
3963
d7d284c2f1f7 (svn r5124) Add IsAircraftInHangar{Stopped,}(), which supersedes CheckStoppedInHangar()
tron
parents: 3955
diff changeset
     5
#include "aircraft.h"
1299
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents: 1282
diff changeset
     6
#include "debug.h"
2163
637ec3c361f5 (svn r2673) Include functions.h directly, not globally via openttd.h
tron
parents: 2140
diff changeset
     7
#include "functions.h"
3315
1f65f8260092 (svn r4079) Add GetSation{Index,ByTile}() to get the station index resp. the station from a tile
tron
parents: 3186
diff changeset
     8
#include "station_map.h"
507
8aa8100b0b22 (svn r815) Include strings.h only in the files which need it.
tron
parents: 458
diff changeset
     9
#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
    10
#include "map.h"
1209
a1ac96655b79 (svn r1713) Split off several functions which query/set information about a single tile from map.h and put them into a seperate file tile.h
tron
parents: 1206
diff changeset
    11
#include "tile.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    12
#include "vehicle.h"
1790
4afb4b4e4278 (svn r2294) - CodeChange: check the service interval settings when changing of all vehicle-types. To simplify things introduce GetServiceIntervalClamped() that returns the same or clamped value of the new service interval. There were some inconsistencies in the gui files so I had to change those, and const correctness kicked in, so it's a bit messy at certain points.
Darkvater
parents: 1786
diff changeset
    13
#include "depot.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    14
#include "engine.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    15
#include "command.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    16
#include "station.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    17
#include "news.h"
337
66647f97e7c0 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
    18
#include "sound.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    19
#include "player.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    20
#include "airport.h"
1752
cdbfb2f23e72 (svn r2256) - Fix: Trains cannot find a depot when they are in a tunnel. (glx)
matthijs
parents: 1614
diff changeset
    21
#include "vehicle_gui.h"
2517
b90693227193 (svn r3046) Replace some numbers with sprite names and fix a typo in the sprite list
tron
parents: 2504
diff changeset
    22
#include "table/sprites.h"
2962
dbd168a4703a (svn r3524) - Split newgrf features from engine.[ch] into newgrf_engine.[ch], and add the new files to project files.
peter1138
parents: 2952
diff changeset
    23
#include "newgrf_engine.h"
3973
7624a250577b (svn r5146) - NewGRF: Support selection of aircraft default cargo type by checking its refit mask. If aircraft can be refitted to passengers, no change happens, else the first refittable type is chosen. Also use refit capacity to determine the default capacity.
peter1138
parents: 3963
diff changeset
    24
#include "newgrf_callbacks.h"
4244
44dcec623504 (svn r5822) - NewGRF: add support for callback 31, vehicle start/stop check. This allows a set to disallow a vehicle from being started, i.e. to not be able to leave the depot. This is almost a partner to callback 1D.
peter1138
parents: 4242
diff changeset
    25
#include "newgrf_text.h"
4261
2ec8f5a9747b (svn r5887) -Cleanup: move date related functions, defines and variables to date.[ch]
rubidium
parents: 4259
diff changeset
    26
#include "date.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    27
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    28
static bool AirportMove(Vehicle *v, const AirportFTAClass *Airport);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    29
static bool AirportSetBlocks(Vehicle *v, AirportFTA *current_pos, const AirportFTAClass *Airport);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    30
static bool AirportHasBlock(Vehicle *v, AirportFTA *current_pos, const AirportFTAClass *Airport);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    31
static bool AirportFindFreeTerminal(Vehicle *v, const AirportFTAClass *Airport);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    32
static bool AirportFindFreeHelipad(Vehicle *v, const AirportFTAClass *Airport);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    33
static void AirportGoToNextPosition(Vehicle *v, const AirportFTAClass *Airport);
899
93b0626ddfce (svn r1385) Fix: [ 1095020 ] When all stations in an aircraft's order list are demolished, the plane eventually crashes (running out of fuel)
dominik
parents: 842
diff changeset
    34
static void CrashAirplane(Vehicle *v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    35
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    36
static void AircraftNextAirportPos_and_Order(Vehicle *v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    37
static byte GetAircraftFlyingAltitude(const Vehicle *v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    38
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    39
static const SpriteID _aircraft_sprite[] = {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    40
	0x0EB5, 0x0EBD, 0x0EC5, 0x0ECD,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    41
	0x0ED5, 0x0EDD, 0x0E9D, 0x0EA5,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    42
	0x0EAD, 0x0EE5, 0x0F05, 0x0F0D,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    43
	0x0F15, 0x0F1D, 0x0F25, 0x0F2D,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    44
	0x0EED, 0x0EF5, 0x0EFD, 0x0F35,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    45
	0x0E9D, 0x0EA5, 0x0EAD, 0x0EB5,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    46
	0x0EBD, 0x0EC5
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    47
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    48
3710
a3db4dff570f (svn r4653) - NewGRF: enable use of custom helicopter rotors in game (but not GUI windows yet) (mart3p)
peter1138
parents: 3701
diff changeset
    49
/* Helicopter rotor animation states */
a3db4dff570f (svn r4653) - NewGRF: enable use of custom helicopter rotors in game (but not GUI windows yet) (mart3p)
peter1138
parents: 3701
diff changeset
    50
enum HelicopterRotorStates {
a3db4dff570f (svn r4653) - NewGRF: enable use of custom helicopter rotors in game (but not GUI windows yet) (mart3p)
peter1138
parents: 3701
diff changeset
    51
	HRS_ROTOR_STOPPED,
a3db4dff570f (svn r4653) - NewGRF: enable use of custom helicopter rotors in game (but not GUI windows yet) (mart3p)
peter1138
parents: 3701
diff changeset
    52
	HRS_ROTOR_MOVING_1,
a3db4dff570f (svn r4653) - NewGRF: enable use of custom helicopter rotors in game (but not GUI windows yet) (mart3p)
peter1138
parents: 3701
diff changeset
    53
	HRS_ROTOR_MOVING_2,
a3db4dff570f (svn r4653) - NewGRF: enable use of custom helicopter rotors in game (but not GUI windows yet) (mart3p)
peter1138
parents: 3701
diff changeset
    54
	HRS_ROTOR_MOVING_3,
a3db4dff570f (svn r4653) - NewGRF: enable use of custom helicopter rotors in game (but not GUI windows yet) (mart3p)
peter1138
parents: 3701
diff changeset
    55
};
a3db4dff570f (svn r4653) - NewGRF: enable use of custom helicopter rotors in game (but not GUI windows yet) (mart3p)
peter1138
parents: 3701
diff changeset
    56
1532
83719caec8d1 (svn r2036) Fix FindNearestHanger(), it never worked correctly because 0xFFFF != 65000 and a 16bit number has no 17th bit
tron
parents: 1530
diff changeset
    57
/* Find the nearest hangar to v
83719caec8d1 (svn r2036) Fix FindNearestHanger(), it never worked correctly because 0xFFFF != 65000 and a 16bit number has no 17th bit
tron
parents: 1530
diff changeset
    58
 * INVALID_STATION is returned, if the player does not have any suitable
83719caec8d1 (svn r2036) Fix FindNearestHanger(), it never worked correctly because 0xFFFF != 65000 and a 16bit number has no 17th bit
tron
parents: 1530
diff changeset
    59
 * airports (like helipads only)
83719caec8d1 (svn r2036) Fix FindNearestHanger(), it never worked correctly because 0xFFFF != 65000 and a 16bit number has no 17th bit
tron
parents: 1530
diff changeset
    60
 */
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
    61
static StationID FindNearestHangar(const Vehicle *v)
1194
616dddfc77f3 (svn r1698) - Feature [autoreplace] helicopters will now go to the nearest hangar if they are set to autoreplace and don't have any airport with a hangar in
bjarni
parents: 1151
diff changeset
    62
{
1532
83719caec8d1 (svn r2036) Fix FindNearestHanger(), it never worked correctly because 0xFFFF != 65000 and a 16bit number has no 17th bit
tron
parents: 1530
diff changeset
    63
	const Station *st;
83719caec8d1 (svn r2036) Fix FindNearestHanger(), it never worked correctly because 0xFFFF != 65000 and a 16bit number has no 17th bit
tron
parents: 1530
diff changeset
    64
	uint best = 0;
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
    65
	StationID index = INVALID_STATION;
4284
adbca0e65aaa (svn r5914) -Fix [FS#235]: Bugfix for errors in FindNearestHangar function in aircraft_cmd.c (mart3p).
Darkvater
parents: 4261
diff changeset
    66
	TileIndex vtile = TileVirtXY(v->x_pos, v->y_pos);
1194
616dddfc77f3 (svn r1698) - Feature [autoreplace] helicopters will now go to the nearest hangar if they are set to autoreplace and don't have any airport with a hangar in
bjarni
parents: 1151
diff changeset
    67
616dddfc77f3 (svn r1698) - Feature [autoreplace] helicopters will now go to the nearest hangar if they are set to autoreplace and don't have any airport with a hangar in
bjarni
parents: 1151
diff changeset
    68
	FOR_ALL_STATIONS(st) {
1532
83719caec8d1 (svn r2036) Fix FindNearestHanger(), it never worked correctly because 0xFFFF != 65000 and a 16bit number has no 17th bit
tron
parents: 1530
diff changeset
    69
		if (st->owner == v->owner && st->facilities & FACIL_AIRPORT &&
83719caec8d1 (svn r2036) Fix FindNearestHanger(), it never worked correctly because 0xFFFF != 65000 and a 16bit number has no 17th bit
tron
parents: 1530
diff changeset
    70
				GetAirport(st->airport_type)->nof_depots > 0) {
83719caec8d1 (svn r2036) Fix FindNearestHanger(), it never worked correctly because 0xFFFF != 65000 and a 16bit number has no 17th bit
tron
parents: 1530
diff changeset
    71
			uint distance;
1204
94bc5d4587c1 (svn r1708) - Fix: FindNearestHangar() will no longer return a hangar where a jet will crash if it is a jet, that is searching
bjarni
parents: 1200
diff changeset
    72
1532
83719caec8d1 (svn r2036) Fix FindNearestHanger(), it never worked correctly because 0xFFFF != 65000 and a 16bit number has no 17th bit
tron
parents: 1530
diff changeset
    73
			// don't crash the plane if we know it can't land at the airport
4284
adbca0e65aaa (svn r5914) -Fix [FS#235]: Bugfix for errors in FindNearestHangar function in aircraft_cmd.c (mart3p).
Darkvater
parents: 4261
diff changeset
    74
			if ((AircraftVehInfo(v->engine_type)->subtype & AIR_FAST) &&
adbca0e65aaa (svn r5914) -Fix [FS#235]: Bugfix for errors in FindNearestHangar function in aircraft_cmd.c (mart3p).
Darkvater
parents: 4261
diff changeset
    75
					(st->airport_type == AT_SMALL || st->airport_type == AT_COMMUTER) &&
1532
83719caec8d1 (svn r2036) Fix FindNearestHanger(), it never worked correctly because 0xFFFF != 65000 and a 16bit number has no 17th bit
tron
parents: 1530
diff changeset
    76
					!_cheats.no_jetcrash.value)
83719caec8d1 (svn r2036) Fix FindNearestHanger(), it never worked correctly because 0xFFFF != 65000 and a 16bit number has no 17th bit
tron
parents: 1530
diff changeset
    77
				continue;
83719caec8d1 (svn r2036) Fix FindNearestHanger(), it never worked correctly because 0xFFFF != 65000 and a 16bit number has no 17th bit
tron
parents: 1530
diff changeset
    78
4284
adbca0e65aaa (svn r5914) -Fix [FS#235]: Bugfix for errors in FindNearestHangar function in aircraft_cmd.c (mart3p).
Darkvater
parents: 4261
diff changeset
    79
			// v->tile can't be used here, when aircraft is flying v->tile is set to 0
adbca0e65aaa (svn r5914) -Fix [FS#235]: Bugfix for errors in FindNearestHangar function in aircraft_cmd.c (mart3p).
Darkvater
parents: 4261
diff changeset
    80
			distance = DistanceSquare(vtile, st->airport_tile);
1532
83719caec8d1 (svn r2036) Fix FindNearestHanger(), it never worked correctly because 0xFFFF != 65000 and a 16bit number has no 17th bit
tron
parents: 1530
diff changeset
    81
			if (distance < best || index == INVALID_STATION) {
83719caec8d1 (svn r2036) Fix FindNearestHanger(), it never worked correctly because 0xFFFF != 65000 and a 16bit number has no 17th bit
tron
parents: 1530
diff changeset
    82
				best = distance;
83719caec8d1 (svn r2036) Fix FindNearestHanger(), it never worked correctly because 0xFFFF != 65000 and a 16bit number has no 17th bit
tron
parents: 1530
diff changeset
    83
				index = st->index;
1194
616dddfc77f3 (svn r1698) - Feature [autoreplace] helicopters will now go to the nearest hangar if they are set to autoreplace and don't have any airport with a hangar in
bjarni
parents: 1151
diff changeset
    84
			}
616dddfc77f3 (svn r1698) - Feature [autoreplace] helicopters will now go to the nearest hangar if they are set to autoreplace and don't have any airport with a hangar in
bjarni
parents: 1151
diff changeset
    85
		}
616dddfc77f3 (svn r1698) - Feature [autoreplace] helicopters will now go to the nearest hangar if they are set to autoreplace and don't have any airport with a hangar in
bjarni
parents: 1151
diff changeset
    86
	}
1532
83719caec8d1 (svn r2036) Fix FindNearestHanger(), it never worked correctly because 0xFFFF != 65000 and a 16bit number has no 17th bit
tron
parents: 1530
diff changeset
    87
	return index;
1194
616dddfc77f3 (svn r1698) - Feature [autoreplace] helicopters will now go to the nearest hangar if they are set to autoreplace and don't have any airport with a hangar in
bjarni
parents: 1151
diff changeset
    88
}
616dddfc77f3 (svn r1698) - Feature [autoreplace] helicopters will now go to the nearest hangar if they are set to autoreplace and don't have any airport with a hangar in
bjarni
parents: 1151
diff changeset
    89
1614
4ee38e74afd1 (svn r2118) - Fix: Fix compilation with network disabled, and comment out some of the warnings (unused function)
Darkvater
parents: 1533
diff changeset
    90
#if 0
1194
616dddfc77f3 (svn r1698) - Feature [autoreplace] helicopters will now go to the nearest hangar if they are set to autoreplace and don't have any airport with a hangar in
bjarni
parents: 1151
diff changeset
    91
// returns true if vehicle v have an airport in the schedule, that has a hangar
616dddfc77f3 (svn r1698) - Feature [autoreplace] helicopters will now go to the nearest hangar if they are set to autoreplace and don't have any airport with a hangar in
bjarni
parents: 1151
diff changeset
    92
static bool HaveHangarInOrderList(Vehicle *v)
616dddfc77f3 (svn r1698) - Feature [autoreplace] helicopters will now go to the nearest hangar if they are set to autoreplace and don't have any airport with a hangar in
bjarni
parents: 1151
diff changeset
    93
{
616dddfc77f3 (svn r1698) - Feature [autoreplace] helicopters will now go to the nearest hangar if they are set to autoreplace and don't have any airport with a hangar in
bjarni
parents: 1151
diff changeset
    94
	const Order *order;
616dddfc77f3 (svn r1698) - Feature [autoreplace] helicopters will now go to the nearest hangar if they are set to autoreplace and don't have any airport with a hangar in
bjarni
parents: 1151
diff changeset
    95
616dddfc77f3 (svn r1698) - Feature [autoreplace] helicopters will now go to the nearest hangar if they are set to autoreplace and don't have any airport with a hangar in
bjarni
parents: 1151
diff changeset
    96
	FOR_VEHICLE_ORDERS(v, order) {
616dddfc77f3 (svn r1698) - Feature [autoreplace] helicopters will now go to the nearest hangar if they are set to autoreplace and don't have any airport with a hangar in
bjarni
parents: 1151
diff changeset
    97
		const Station *st = GetStation(order->station);
616dddfc77f3 (svn r1698) - Feature [autoreplace] helicopters will now go to the nearest hangar if they are set to autoreplace and don't have any airport with a hangar in
bjarni
parents: 1151
diff changeset
    98
		if (st->owner == v->owner && st->facilities & FACIL_AIRPORT) {
4059
f1442dfe1c8a (svn r5346) - Feature: Add 4 new airports. 2 for aircraft, 2 for helicopters.
richk
parents: 4056
diff changeset
    99
			// If an airport doesn't have a hangar, skip it
f1442dfe1c8a (svn r5346) - Feature: Add 4 new airports. 2 for aircraft, 2 for helicopters.
richk
parents: 4056
diff changeset
   100
			if (GetAirport(st->airport_type)->nof_depots != 0)
1194
616dddfc77f3 (svn r1698) - Feature [autoreplace] helicopters will now go to the nearest hangar if they are set to autoreplace and don't have any airport with a hangar in
bjarni
parents: 1151
diff changeset
   101
				return true;
616dddfc77f3 (svn r1698) - Feature [autoreplace] helicopters will now go to the nearest hangar if they are set to autoreplace and don't have any airport with a hangar in
bjarni
parents: 1151
diff changeset
   102
		}
616dddfc77f3 (svn r1698) - Feature [autoreplace] helicopters will now go to the nearest hangar if they are set to autoreplace and don't have any airport with a hangar in
bjarni
parents: 1151
diff changeset
   103
	}
616dddfc77f3 (svn r1698) - Feature [autoreplace] helicopters will now go to the nearest hangar if they are set to autoreplace and don't have any airport with a hangar in
bjarni
parents: 1151
diff changeset
   104
616dddfc77f3 (svn r1698) - Feature [autoreplace] helicopters will now go to the nearest hangar if they are set to autoreplace and don't have any airport with a hangar in
bjarni
parents: 1151
diff changeset
   105
	return false;
616dddfc77f3 (svn r1698) - Feature [autoreplace] helicopters will now go to the nearest hangar if they are set to autoreplace and don't have any airport with a hangar in
bjarni
parents: 1151
diff changeset
   106
}
1614
4ee38e74afd1 (svn r2118) - Fix: Fix compilation with network disabled, and comment out some of the warnings (unused function)
Darkvater
parents: 1533
diff changeset
   107
#endif
1194
616dddfc77f3 (svn r1698) - Feature [autoreplace] helicopters will now go to the nearest hangar if they are set to autoreplace and don't have any airport with a hangar in
bjarni
parents: 1151
diff changeset
   108
3157
40de8616c04c (svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3140
diff changeset
   109
int GetAircraftImage(const Vehicle* v, Direction direction)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   110
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   111
	int spritenum = v->spritenum;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   112
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   113
	if (is_custom_sprite(spritenum)) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   114
		int sprite = GetCustomVehicleSprite(v, direction);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   115
2989
99c95a3ebcaa (svn r3564) Several smaller changes:
tron
parents: 2962
diff changeset
   116
		if (sprite != 0) return sprite;
2464
3dee59d241c0 (svn r2990) Remove _engine_original_sprites and replace all uses with references to our (new) original engine/vehicle tables.
peter1138
parents: 2423
diff changeset
   117
		spritenum = orig_aircraft_vehicle_info[v->engine_type - AIRCRAFT_ENGINES_INDEX].image_index;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   118
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   119
	return direction + _aircraft_sprite[spritenum];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   120
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   121
3710
a3db4dff570f (svn r4653) - NewGRF: enable use of custom helicopter rotors in game (but not GUI windows yet) (mart3p)
peter1138
parents: 3701
diff changeset
   122
SpriteID GetRotorImage(const Vehicle *v)
a3db4dff570f (svn r4653) - NewGRF: enable use of custom helicopter rotors in game (but not GUI windows yet) (mart3p)
peter1138
parents: 3701
diff changeset
   123
{
a3db4dff570f (svn r4653) - NewGRF: enable use of custom helicopter rotors in game (but not GUI windows yet) (mart3p)
peter1138
parents: 3701
diff changeset
   124
	const Vehicle *w;
a3db4dff570f (svn r4653) - NewGRF: enable use of custom helicopter rotors in game (but not GUI windows yet) (mart3p)
peter1138
parents: 3701
diff changeset
   125
a3db4dff570f (svn r4653) - NewGRF: enable use of custom helicopter rotors in game (but not GUI windows yet) (mart3p)
peter1138
parents: 3701
diff changeset
   126
	assert((v->subtype & 1) == 0);
a3db4dff570f (svn r4653) - NewGRF: enable use of custom helicopter rotors in game (but not GUI windows yet) (mart3p)
peter1138
parents: 3701
diff changeset
   127
a3db4dff570f (svn r4653) - NewGRF: enable use of custom helicopter rotors in game (but not GUI windows yet) (mart3p)
peter1138
parents: 3701
diff changeset
   128
	w = v->next->next;
a3db4dff570f (svn r4653) - NewGRF: enable use of custom helicopter rotors in game (but not GUI windows yet) (mart3p)
peter1138
parents: 3701
diff changeset
   129
	if (is_custom_sprite(v->spritenum)) {
4056
30ceffc75059 (svn r5336) - NewGRF: draw custom helicopter rotor sprites in vehicle info window, ensuring the correct direction is used (inspiration from mart3p)
peter1138
parents: 4023
diff changeset
   130
		SpriteID spritenum = GetCustomRotorSprite(v, false);
3710
a3db4dff570f (svn r4653) - NewGRF: enable use of custom helicopter rotors in game (but not GUI windows yet) (mart3p)
peter1138
parents: 3701
diff changeset
   131
		if (spritenum != 0) return spritenum;
a3db4dff570f (svn r4653) - NewGRF: enable use of custom helicopter rotors in game (but not GUI windows yet) (mart3p)
peter1138
parents: 3701
diff changeset
   132
	}
a3db4dff570f (svn r4653) - NewGRF: enable use of custom helicopter rotors in game (but not GUI windows yet) (mart3p)
peter1138
parents: 3701
diff changeset
   133
a3db4dff570f (svn r4653) - NewGRF: enable use of custom helicopter rotors in game (but not GUI windows yet) (mart3p)
peter1138
parents: 3701
diff changeset
   134
	/* Return standard rotor sprites if there are no custom sprites for this helicopter */
a3db4dff570f (svn r4653) - NewGRF: enable use of custom helicopter rotors in game (but not GUI windows yet) (mart3p)
peter1138
parents: 3701
diff changeset
   135
	return SPR_ROTOR_STOPPED + w->u.air.state;
a3db4dff570f (svn r4653) - NewGRF: enable use of custom helicopter rotors in game (but not GUI windows yet) (mart3p)
peter1138
parents: 3701
diff changeset
   136
}
a3db4dff570f (svn r4653) - NewGRF: enable use of custom helicopter rotors in game (but not GUI windows yet) (mart3p)
peter1138
parents: 3701
diff changeset
   137
2477
d454d5152386 (svn r3003) Change int, uint, uin16 and byte to EngineID where appropriate, plus some related changes (mostly casts)
tron
parents: 2475
diff changeset
   138
void DrawAircraftEngine(int x, int y, EngineID engine, uint32 image_ormod)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   139
{
4023
b65af90447d2 (svn r5262) Add symbolic names for the aircraft subtypes. not perfect, but better than raw numbers
tron
parents: 3988
diff changeset
   140
	const AircraftVehicleInfo* avi = AircraftVehInfo(engine);
b65af90447d2 (svn r5262) Add symbolic names for the aircraft subtypes. not perfect, but better than raw numbers
tron
parents: 3988
diff changeset
   141
	int spritenum = avi->image_index;
1200
a2036275549e (svn r1704) - Fix: [newgrf] rotors of custom helicopters are displayed correctly in the hangar window
dominik
parents: 1197
diff changeset
   142
	int sprite = (6 + _aircraft_sprite[spritenum]);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   143
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
   144
	if (is_custom_sprite(spritenum)) {
3186
5798593c2d95 (svn r3832) Replace some magic numbers by (Diag)Direction enums
tron
parents: 3183
diff changeset
   145
		sprite = GetCustomVehicleIcon(engine, DIR_W);
1200
a2036275549e (svn r1704) - Fix: [newgrf] rotors of custom helicopters are displayed correctly in the hangar window
dominik
parents: 1197
diff changeset
   146
		if (!sprite)
2464
3dee59d241c0 (svn r2990) Remove _engine_original_sprites and replace all uses with references to our (new) original engine/vehicle tables.
peter1138
parents: 2423
diff changeset
   147
			spritenum = orig_aircraft_vehicle_info[engine - AIRCRAFT_ENGINES_INDEX].image_index;
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
   148
	}
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
   149
1200
a2036275549e (svn r1704) - Fix: [newgrf] rotors of custom helicopters are displayed correctly in the hangar window
dominik
parents: 1197
diff changeset
   150
	DrawSprite(sprite | image_ormod, x, y);
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
   151
4023
b65af90447d2 (svn r5262) Add symbolic names for the aircraft subtypes. not perfect, but better than raw numbers
tron
parents: 3988
diff changeset
   152
	if (!(avi->subtype & AIR_CTOL)) {
3710
a3db4dff570f (svn r4653) - NewGRF: enable use of custom helicopter rotors in game (but not GUI windows yet) (mart3p)
peter1138
parents: 3701
diff changeset
   153
		SpriteID rotor_sprite = GetCustomRotorIcon(engine);
a3db4dff570f (svn r4653) - NewGRF: enable use of custom helicopter rotors in game (but not GUI windows yet) (mart3p)
peter1138
parents: 3701
diff changeset
   154
		if (rotor_sprite == 0) rotor_sprite = SPR_ROTOR_STOPPED;
a3db4dff570f (svn r4653) - NewGRF: enable use of custom helicopter rotors in game (but not GUI windows yet) (mart3p)
peter1138
parents: 3701
diff changeset
   155
		DrawSprite(rotor_sprite, x, y - 5);
1200
a2036275549e (svn r1704) - Fix: [newgrf] rotors of custom helicopters are displayed correctly in the hangar window
dominik
parents: 1197
diff changeset
   156
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   157
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   158
2752
b5fe5a7e6282 (svn r3297) Staticise
tron
parents: 2704
diff changeset
   159
static int32 EstimateAircraftCost(EngineID engine_type)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   160
{
538
5dd99f3b7eca (svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents: 534
diff changeset
   161
	return AircraftVehInfo(engine_type)->base_cost * (_price.aircraft_base>>3)>>5;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   162
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   163
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   164
3973
7624a250577b (svn r5146) - NewGRF: Support selection of aircraft default cargo type by checking its refit mask. If aircraft can be refitted to passengers, no change happens, else the first refittable type is chosen. Also use refit capacity to determine the default capacity.
peter1138
parents: 3963
diff changeset
   165
/**
7624a250577b (svn r5146) - NewGRF: Support selection of aircraft default cargo type by checking its refit mask. If aircraft can be refitted to passengers, no change happens, else the first refittable type is chosen. Also use refit capacity to determine the default capacity.
peter1138
parents: 3963
diff changeset
   166
 * Calculates cargo capacity based on an aircraft's passenger
7624a250577b (svn r5146) - NewGRF: Support selection of aircraft default cargo type by checking its refit mask. If aircraft can be refitted to passengers, no change happens, else the first refittable type is chosen. Also use refit capacity to determine the default capacity.
peter1138
parents: 3963
diff changeset
   167
 * and mail capacities.
7624a250577b (svn r5146) - NewGRF: Support selection of aircraft default cargo type by checking its refit mask. If aircraft can be refitted to passengers, no change happens, else the first refittable type is chosen. Also use refit capacity to determine the default capacity.
peter1138
parents: 3963
diff changeset
   168
 * @param cid Which cargo type to calculate a capacity for.
7624a250577b (svn r5146) - NewGRF: Support selection of aircraft default cargo type by checking its refit mask. If aircraft can be refitted to passengers, no change happens, else the first refittable type is chosen. Also use refit capacity to determine the default capacity.
peter1138
parents: 3963
diff changeset
   169
 * @param engine Which engine to find a cargo capacity for.
7624a250577b (svn r5146) - NewGRF: Support selection of aircraft default cargo type by checking its refit mask. If aircraft can be refitted to passengers, no change happens, else the first refittable type is chosen. Also use refit capacity to determine the default capacity.
peter1138
parents: 3963
diff changeset
   170
 * @return New cargo capacity value.
7624a250577b (svn r5146) - NewGRF: Support selection of aircraft default cargo type by checking its refit mask. If aircraft can be refitted to passengers, no change happens, else the first refittable type is chosen. Also use refit capacity to determine the default capacity.
peter1138
parents: 3963
diff changeset
   171
 */
3987
96b119a9fb94 (svn r5186) - NewGRF: show default aircraft cargo type and the purchase list (mart3p)
peter1138
parents: 3974
diff changeset
   172
uint16 AircraftDefaultCargoCapacity(CargoID cid, EngineID engine_type)
3973
7624a250577b (svn r5146) - NewGRF: Support selection of aircraft default cargo type by checking its refit mask. If aircraft can be refitted to passengers, no change happens, else the first refittable type is chosen. Also use refit capacity to determine the default capacity.
peter1138
parents: 3963
diff changeset
   173
{
7624a250577b (svn r5146) - NewGRF: Support selection of aircraft default cargo type by checking its refit mask. If aircraft can be refitted to passengers, no change happens, else the first refittable type is chosen. Also use refit capacity to determine the default capacity.
peter1138
parents: 3963
diff changeset
   174
	const AircraftVehicleInfo *avi = AircraftVehInfo(engine_type);
7624a250577b (svn r5146) - NewGRF: Support selection of aircraft default cargo type by checking its refit mask. If aircraft can be refitted to passengers, no change happens, else the first refittable type is chosen. Also use refit capacity to determine the default capacity.
peter1138
parents: 3963
diff changeset
   175
7624a250577b (svn r5146) - NewGRF: Support selection of aircraft default cargo type by checking its refit mask. If aircraft can be refitted to passengers, no change happens, else the first refittable type is chosen. Also use refit capacity to determine the default capacity.
peter1138
parents: 3963
diff changeset
   176
	assert(cid != CT_INVALID);
7624a250577b (svn r5146) - NewGRF: Support selection of aircraft default cargo type by checking its refit mask. If aircraft can be refitted to passengers, no change happens, else the first refittable type is chosen. Also use refit capacity to determine the default capacity.
peter1138
parents: 3963
diff changeset
   177
7624a250577b (svn r5146) - NewGRF: Support selection of aircraft default cargo type by checking its refit mask. If aircraft can be refitted to passengers, no change happens, else the first refittable type is chosen. Also use refit capacity to determine the default capacity.
peter1138
parents: 3963
diff changeset
   178
	/* An aircraft can carry twice as much goods as normal cargo,
7624a250577b (svn r5146) - NewGRF: Support selection of aircraft default cargo type by checking its refit mask. If aircraft can be refitted to passengers, no change happens, else the first refittable type is chosen. Also use refit capacity to determine the default capacity.
peter1138
parents: 3963
diff changeset
   179
	 * and four times as many passengers. */
7624a250577b (svn r5146) - NewGRF: Support selection of aircraft default cargo type by checking its refit mask. If aircraft can be refitted to passengers, no change happens, else the first refittable type is chosen. Also use refit capacity to determine the default capacity.
peter1138
parents: 3963
diff changeset
   180
	switch (cid) {
7624a250577b (svn r5146) - NewGRF: Support selection of aircraft default cargo type by checking its refit mask. If aircraft can be refitted to passengers, no change happens, else the first refittable type is chosen. Also use refit capacity to determine the default capacity.
peter1138
parents: 3963
diff changeset
   181
		case CT_PASSENGERS:
7624a250577b (svn r5146) - NewGRF: Support selection of aircraft default cargo type by checking its refit mask. If aircraft can be refitted to passengers, no change happens, else the first refittable type is chosen. Also use refit capacity to determine the default capacity.
peter1138
parents: 3963
diff changeset
   182
			return avi->passenger_capacity;
7624a250577b (svn r5146) - NewGRF: Support selection of aircraft default cargo type by checking its refit mask. If aircraft can be refitted to passengers, no change happens, else the first refittable type is chosen. Also use refit capacity to determine the default capacity.
peter1138
parents: 3963
diff changeset
   183
		case CT_MAIL:
7624a250577b (svn r5146) - NewGRF: Support selection of aircraft default cargo type by checking its refit mask. If aircraft can be refitted to passengers, no change happens, else the first refittable type is chosen. Also use refit capacity to determine the default capacity.
peter1138
parents: 3963
diff changeset
   184
			return avi->passenger_capacity + avi->mail_capacity;
7624a250577b (svn r5146) - NewGRF: Support selection of aircraft default cargo type by checking its refit mask. If aircraft can be refitted to passengers, no change happens, else the first refittable type is chosen. Also use refit capacity to determine the default capacity.
peter1138
parents: 3963
diff changeset
   185
		case CT_GOODS:
7624a250577b (svn r5146) - NewGRF: Support selection of aircraft default cargo type by checking its refit mask. If aircraft can be refitted to passengers, no change happens, else the first refittable type is chosen. Also use refit capacity to determine the default capacity.
peter1138
parents: 3963
diff changeset
   186
			return (avi->passenger_capacity + avi->mail_capacity) / 2;
7624a250577b (svn r5146) - NewGRF: Support selection of aircraft default cargo type by checking its refit mask. If aircraft can be refitted to passengers, no change happens, else the first refittable type is chosen. Also use refit capacity to determine the default capacity.
peter1138
parents: 3963
diff changeset
   187
		default:
7624a250577b (svn r5146) - NewGRF: Support selection of aircraft default cargo type by checking its refit mask. If aircraft can be refitted to passengers, no change happens, else the first refittable type is chosen. Also use refit capacity to determine the default capacity.
peter1138
parents: 3963
diff changeset
   188
			return (avi->passenger_capacity + avi->mail_capacity) / 4;
7624a250577b (svn r5146) - NewGRF: Support selection of aircraft default cargo type by checking its refit mask. If aircraft can be refitted to passengers, no change happens, else the first refittable type is chosen. Also use refit capacity to determine the default capacity.
peter1138
parents: 3963
diff changeset
   189
	}
7624a250577b (svn r5146) - NewGRF: Support selection of aircraft default cargo type by checking its refit mask. If aircraft can be refitted to passengers, no change happens, else the first refittable type is chosen. Also use refit capacity to determine the default capacity.
peter1138
parents: 3963
diff changeset
   190
}
7624a250577b (svn r5146) - NewGRF: Support selection of aircraft default cargo type by checking its refit mask. If aircraft can be refitted to passengers, no change happens, else the first refittable type is chosen. Also use refit capacity to determine the default capacity.
peter1138
parents: 3963
diff changeset
   191
7624a250577b (svn r5146) - NewGRF: Support selection of aircraft default cargo type by checking its refit mask. If aircraft can be refitted to passengers, no change happens, else the first refittable type is chosen. Also use refit capacity to determine the default capacity.
peter1138
parents: 3963
diff changeset
   192
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   193
/** Build an aircraft.
3491
4c8427796c64 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3421
diff changeset
   194
 * @param tile tile of depot where aircraft is built
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   195
 * @param p1 aircraft type being built (engine)
3816
2cdb29b57c1f (svn r4826) -Fix: [autoreplace] fixed possible problem when autoreplacing and was number of vehicles (of a type, not total) was reached
bjarni
parents: 3722
diff changeset
   196
 * @param p2 bit 0 when set, the unitnumber will be 0, otherwise it will be a free number
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   197
 */
3491
4c8427796c64 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3421
diff changeset
   198
int32 CmdBuildAircraft(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   199
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   200
	int32 value;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   201
	Vehicle *vl[3], *v, *u, *w;
1282
e7a73ee62d2f (svn r1786) -Fix: unitnumber is increased to 16bit, so now you can have up to 5000
truelight
parents: 1266
diff changeset
   202
	UnitID unit_num;
1877
e7318705dd04 (svn r2383) - Fix: Monkey-testing turned up some command crashes.
Darkvater
parents: 1874
diff changeset
   203
	const AircraftVehicleInfo *avi;
4161
7b08131239f7 (svn r5594) Be more strict what's allowed to be built in a hangar: if the airport can't handle planes/helicopters don't present and disallow building planes/helicopters - the latter case wasn't covered yet.
tron
parents: 4111
diff changeset
   204
	const AirportFTAClass* ap;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   205
	Engine *e;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   206
2854
6b85a5f4b65f (svn r3402) -Fix: [building/cloning] added a string telling if build failed due to not being able to build a vehicle. Triggered when cloning a retired design
bjarni
parents: 2848
diff changeset
   207
	if (!IsEngineBuildable(p1, VEH_Aircraft)) return_cmd_error(STR_ENGINE_NOT_BUILDABLE);
1196
115f46e3807d (svn r1700) - Fix: Hacked clients can no longer be used to build vehicles that are not available yet (Hackykid)
bjarni
parents: 1195
diff changeset
   208
2423
84222db5a01a (svn r2949) The AI no longer needs to 'cheat' to build aircraft; eg it builds them now from a hangar. Also, to query the price of a new aircraft tile information is not needed
Darkvater
parents: 2422
diff changeset
   209
	value = EstimateAircraftCost(p1);
1226
cbcaba80c31c (svn r1730) - Fix: Ships and Aircrafts have to be build in depots owned by _current_player (hacked client protection)
bjarni
parents: 1209
diff changeset
   210
2423
84222db5a01a (svn r2949) The AI no longer needs to 'cheat' to build aircraft; eg it builds them now from a hangar. Also, to query the price of a new aircraft tile information is not needed
Darkvater
parents: 2422
diff changeset
   211
	// to just query the cost, it is not neccessary to have a valid tile (automation/AI)
84222db5a01a (svn r2949) The AI no longer needs to 'cheat' to build aircraft; eg it builds them now from a hangar. Also, to query the price of a new aircraft tile information is not needed
Darkvater
parents: 2422
diff changeset
   212
	if (flags & DC_QUERY_COST) return value;
84222db5a01a (svn r2949) The AI no longer needs to 'cheat' to build aircraft; eg it builds them now from a hangar. Also, to query the price of a new aircraft tile information is not needed
Darkvater
parents: 2422
diff changeset
   213
3338
2c60ed0dcce3 (svn r4120) Use the new station functions where appropriate
tron
parents: 3315
diff changeset
   214
	if (!IsHangarTile(tile) || !IsTileOwner(tile, _current_player)) return CMD_ERROR;
1226
cbcaba80c31c (svn r1730) - Fix: Ships and Aircrafts have to be build in depots owned by _current_player (hacked client protection)
bjarni
parents: 1209
diff changeset
   215
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   216
	SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   217
1877
e7318705dd04 (svn r2383) - Fix: Monkey-testing turned up some command crashes.
Darkvater
parents: 1874
diff changeset
   218
	avi = AircraftVehInfo(p1);
4161
7b08131239f7 (svn r5594) Be more strict what's allowed to be built in a hangar: if the airport can't handle planes/helicopters don't present and disallow building planes/helicopters - the latter case wasn't covered yet.
tron
parents: 4111
diff changeset
   219
7b08131239f7 (svn r5594) Be more strict what's allowed to be built in a hangar: if the airport can't handle planes/helicopters don't present and disallow building planes/helicopters - the latter case wasn't covered yet.
tron
parents: 4111
diff changeset
   220
	// Prevent building aircraft types at places which can't handle them
7b08131239f7 (svn r5594) Be more strict what's allowed to be built in a hangar: if the airport can't handle planes/helicopters don't present and disallow building planes/helicopters - the latter case wasn't covered yet.
tron
parents: 4111
diff changeset
   221
	ap = GetAirport(GetStationByTile(tile)->airport_type);
7b08131239f7 (svn r5594) Be more strict what's allowed to be built in a hangar: if the airport can't handle planes/helicopters don't present and disallow building planes/helicopters - the latter case wasn't covered yet.
tron
parents: 4111
diff changeset
   222
	if ((avi->subtype & AIR_CTOL ? HELICOPTERS_ONLY : AIRCRAFT_ONLY) == ap->acc_planes) {
7b08131239f7 (svn r5594) Be more strict what's allowed to be built in a hangar: if the airport can't handle planes/helicopters don't present and disallow building planes/helicopters - the latter case wasn't covered yet.
tron
parents: 4111
diff changeset
   223
		return CMD_ERROR;
7b08131239f7 (svn r5594) Be more strict what's allowed to be built in a hangar: if the airport can't handle planes/helicopters don't present and disallow building planes/helicopters - the latter case wasn't covered yet.
tron
parents: 4111
diff changeset
   224
	}
7b08131239f7 (svn r5594) Be more strict what's allowed to be built in a hangar: if the airport can't handle planes/helicopters don't present and disallow building planes/helicopters - the latter case wasn't covered yet.
tron
parents: 4111
diff changeset
   225
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   226
	// allocate 2 or 3 vehicle structs, depending on type
4023
b65af90447d2 (svn r5262) Add symbolic names for the aircraft subtypes. not perfect, but better than raw numbers
tron
parents: 3988
diff changeset
   227
	if (!AllocateVehicles(vl, avi->subtype & AIR_CTOL ? 2 : 3) ||
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   228
			IsOrderPoolFull()) {
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   229
		return_cmd_error(STR_00E1_TOO_MANY_VEHICLES_IN_GAME);
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   230
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   231
4095
55a5642143f1 (svn r5427) - Codechange: testing for "== true" is redundant.
peter1138
parents: 4077
diff changeset
   232
	unit_num = HASBIT(p2, 0) ? 0 : GetFreeUnitNumber(VEH_Aircraft);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   233
	if (unit_num > _patches.max_aircraft)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   234
		return_cmd_error(STR_00E1_TOO_MANY_VEHICLES_IN_GAME);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   235
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   236
	if (flags & DC_EXEC) {
3973
7624a250577b (svn r5146) - NewGRF: Support selection of aircraft default cargo type by checking its refit mask. If aircraft can be refitted to passengers, no change happens, else the first refittable type is chosen. Also use refit capacity to determine the default capacity.
peter1138
parents: 3963
diff changeset
   237
		CargoID cargo;
3491
4c8427796c64 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3421
diff changeset
   238
		uint x;
4c8427796c64 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3421
diff changeset
   239
		uint y;
4c8427796c64 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3421
diff changeset
   240
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   241
		v = vl[0];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   242
		u = vl[1];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   243
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   244
		v->unitnumber = unit_num;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   245
		v->type = u->type = VEH_Aircraft;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   246
		v->direction = 3;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   247
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   248
		v->owner = u->owner = _current_player;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   249
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   250
		v->tile = tile;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   251
//		u->tile = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   252
3421
8ab76c47c72c (svn r4246) -Codechange. Replaced about 100 occurences of '16' by TILE_SIZE
celestar
parents: 3338
diff changeset
   253
		x = TileX(tile) * TILE_SIZE + 5;
8ab76c47c72c (svn r4246) -Codechange. Replaced about 100 occurences of '16' by TILE_SIZE
celestar
parents: 3338
diff changeset
   254
		y = TileY(tile) * TILE_SIZE + 3;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   255
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   256
		v->x_pos = u->x_pos = x;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   257
		v->y_pos = u->y_pos = y;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   258
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   259
		u->z_pos = GetSlopeZ(x, y);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   260
		v->z_pos = u->z_pos + 1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   261
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   262
		v->x_offs = v->y_offs = -1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   263
//		u->delta_x = u->delta_y = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   264
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   265
		v->sprite_width = v->sprite_height = 2;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   266
		v->z_height = 5;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   267
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   268
		u->sprite_width = u->sprite_height = 2;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   269
		u->z_height = 1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   270
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   271
		v->vehstatus = VS_HIDDEN | VS_STOPPED | VS_DEFPAL;
4175
c0bcc65f36d4 (svn r5613) Rename VS_DISASTER to VS_SHADOW, because this is what this flag actually means
tron
parents: 4161
diff changeset
   272
		u->vehstatus = VS_HIDDEN | VS_UNCLICKABLE | VS_SHADOW;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   273
538
5dd99f3b7eca (svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents: 534
diff changeset
   274
		v->spritenum = avi->image_index;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   275
//		v->cargo_count = u->number_of_pieces = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   276
922
fc040662bd43 (svn r1410) Replaced all occurences of 'passanger' by 'passenger' in the code
celestar
parents: 919
diff changeset
   277
		v->cargo_cap = avi->passenger_capacity;
538
5dd99f3b7eca (svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents: 534
diff changeset
   278
		u->cargo_cap = avi->mail_capacity;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   279
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   280
		v->cargo_type = CT_PASSENGERS;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   281
		u->cargo_type = CT_MAIL;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   282
3870
766d7bd6c299 (svn r4910) - NewGRF: add and initialize cargo subtype for vehicle visual variations
peter1138
parents: 3816
diff changeset
   283
		v->cargo_subtype = 0;
766d7bd6c299 (svn r4910) - NewGRF: add and initialize cargo subtype for vehicle visual variations
peter1138
parents: 3816
diff changeset
   284
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   285
		v->string_id = STR_SV_AIRCRAFT_NAME;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   286
//		v->next_order_param = v->next_order = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   287
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   288
//		v->load_unload_time_rem = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   289
//		v->progress = 0;
1266
9dc5638fe8cc (svn r1770) -Fix: Hopefully last pieces of code that are containing a station-id
truelight
parents: 1263
diff changeset
   290
		v->last_station_visited = INVALID_STATION;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   291
//		v->destination_coords = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   292
538
5dd99f3b7eca (svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents: 534
diff changeset
   293
		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
   294
		v->acceleration = avi->acceleration;
2477
d454d5152386 (svn r3003) Change int, uint, uin16 and byte to EngineID where appropriate, plus some related changes (mostly casts)
tron
parents: 2475
diff changeset
   295
		v->engine_type = p1;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   296
4023
b65af90447d2 (svn r5262) Add symbolic names for the aircraft subtypes. not perfect, but better than raw numbers
tron
parents: 3988
diff changeset
   297
		v->subtype = (avi->subtype & AIR_CTOL ? 2 : 0);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   298
		v->value = value;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   299
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   300
		u->subtype = 4;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   301
3973
7624a250577b (svn r5146) - NewGRF: Support selection of aircraft default cargo type by checking its refit mask. If aircraft can be refitted to passengers, no change happens, else the first refittable type is chosen. Also use refit capacity to determine the default capacity.
peter1138
parents: 3963
diff changeset
   302
		/* Danger, Will Robinson!
7624a250577b (svn r5146) - NewGRF: Support selection of aircraft default cargo type by checking its refit mask. If aircraft can be refitted to passengers, no change happens, else the first refittable type is chosen. Also use refit capacity to determine the default capacity.
peter1138
parents: 3963
diff changeset
   303
		 * If the aircraft is refittable, but cannot be refitted to
7624a250577b (svn r5146) - NewGRF: Support selection of aircraft default cargo type by checking its refit mask. If aircraft can be refitted to passengers, no change happens, else the first refittable type is chosen. Also use refit capacity to determine the default capacity.
peter1138
parents: 3963
diff changeset
   304
		 * passengers, we select the cargo type from the refit mask.
7624a250577b (svn r5146) - NewGRF: Support selection of aircraft default cargo type by checking its refit mask. If aircraft can be refitted to passengers, no change happens, else the first refittable type is chosen. Also use refit capacity to determine the default capacity.
peter1138
parents: 3963
diff changeset
   305
		 * This is a fairly nasty hack to get around the fact that TTD
7624a250577b (svn r5146) - NewGRF: Support selection of aircraft default cargo type by checking its refit mask. If aircraft can be refitted to passengers, no change happens, else the first refittable type is chosen. Also use refit capacity to determine the default capacity.
peter1138
parents: 3963
diff changeset
   306
		 * has no default cargo type specifier for planes... */
7624a250577b (svn r5146) - NewGRF: Support selection of aircraft default cargo type by checking its refit mask. If aircraft can be refitted to passengers, no change happens, else the first refittable type is chosen. Also use refit capacity to determine the default capacity.
peter1138
parents: 3963
diff changeset
   307
		cargo = FindFirstRefittableCargo(p1);
7624a250577b (svn r5146) - NewGRF: Support selection of aircraft default cargo type by checking its refit mask. If aircraft can be refitted to passengers, no change happens, else the first refittable type is chosen. Also use refit capacity to determine the default capacity.
peter1138
parents: 3963
diff changeset
   308
		if (cargo != CT_INVALID && cargo != CT_PASSENGERS) {
7624a250577b (svn r5146) - NewGRF: Support selection of aircraft default cargo type by checking its refit mask. If aircraft can be refitted to passengers, no change happens, else the first refittable type is chosen. Also use refit capacity to determine the default capacity.
peter1138
parents: 3963
diff changeset
   309
			uint16 callback = CALLBACK_FAILED;
7624a250577b (svn r5146) - NewGRF: Support selection of aircraft default cargo type by checking its refit mask. If aircraft can be refitted to passengers, no change happens, else the first refittable type is chosen. Also use refit capacity to determine the default capacity.
peter1138
parents: 3963
diff changeset
   310
7624a250577b (svn r5146) - NewGRF: Support selection of aircraft default cargo type by checking its refit mask. If aircraft can be refitted to passengers, no change happens, else the first refittable type is chosen. Also use refit capacity to determine the default capacity.
peter1138
parents: 3963
diff changeset
   311
			v->cargo_type = cargo;
7624a250577b (svn r5146) - NewGRF: Support selection of aircraft default cargo type by checking its refit mask. If aircraft can be refitted to passengers, no change happens, else the first refittable type is chosen. Also use refit capacity to determine the default capacity.
peter1138
parents: 3963
diff changeset
   312
7624a250577b (svn r5146) - NewGRF: Support selection of aircraft default cargo type by checking its refit mask. If aircraft can be refitted to passengers, no change happens, else the first refittable type is chosen. Also use refit capacity to determine the default capacity.
peter1138
parents: 3963
diff changeset
   313
			if (HASBIT(EngInfo(p1)->callbackmask, CBM_REFIT_CAPACITY)) {
7624a250577b (svn r5146) - NewGRF: Support selection of aircraft default cargo type by checking its refit mask. If aircraft can be refitted to passengers, no change happens, else the first refittable type is chosen. Also use refit capacity to determine the default capacity.
peter1138
parents: 3963
diff changeset
   314
				callback = GetVehicleCallback(CBID_VEHICLE_REFIT_CAPACITY, 0, 0, v->engine_type, v);
7624a250577b (svn r5146) - NewGRF: Support selection of aircraft default cargo type by checking its refit mask. If aircraft can be refitted to passengers, no change happens, else the first refittable type is chosen. Also use refit capacity to determine the default capacity.
peter1138
parents: 3963
diff changeset
   315
			}
7624a250577b (svn r5146) - NewGRF: Support selection of aircraft default cargo type by checking its refit mask. If aircraft can be refitted to passengers, no change happens, else the first refittable type is chosen. Also use refit capacity to determine the default capacity.
peter1138
parents: 3963
diff changeset
   316
7624a250577b (svn r5146) - NewGRF: Support selection of aircraft default cargo type by checking its refit mask. If aircraft can be refitted to passengers, no change happens, else the first refittable type is chosen. Also use refit capacity to determine the default capacity.
peter1138
parents: 3963
diff changeset
   317
			if (callback == CALLBACK_FAILED) {
7624a250577b (svn r5146) - NewGRF: Support selection of aircraft default cargo type by checking its refit mask. If aircraft can be refitted to passengers, no change happens, else the first refittable type is chosen. Also use refit capacity to determine the default capacity.
peter1138
parents: 3963
diff changeset
   318
				/* Callback failed, or not executed; use the default cargo capacity */
7624a250577b (svn r5146) - NewGRF: Support selection of aircraft default cargo type by checking its refit mask. If aircraft can be refitted to passengers, no change happens, else the first refittable type is chosen. Also use refit capacity to determine the default capacity.
peter1138
parents: 3963
diff changeset
   319
				v->cargo_cap = AircraftDefaultCargoCapacity(v->cargo_type, v->engine_type);
7624a250577b (svn r5146) - NewGRF: Support selection of aircraft default cargo type by checking its refit mask. If aircraft can be refitted to passengers, no change happens, else the first refittable type is chosen. Also use refit capacity to determine the default capacity.
peter1138
parents: 3963
diff changeset
   320
			} else {
7624a250577b (svn r5146) - NewGRF: Support selection of aircraft default cargo type by checking its refit mask. If aircraft can be refitted to passengers, no change happens, else the first refittable type is chosen. Also use refit capacity to determine the default capacity.
peter1138
parents: 3963
diff changeset
   321
				v->cargo_cap = callback;
7624a250577b (svn r5146) - NewGRF: Support selection of aircraft default cargo type by checking its refit mask. If aircraft can be refitted to passengers, no change happens, else the first refittable type is chosen. Also use refit capacity to determine the default capacity.
peter1138
parents: 3963
diff changeset
   322
			}
7624a250577b (svn r5146) - NewGRF: Support selection of aircraft default cargo type by checking its refit mask. If aircraft can be refitted to passengers, no change happens, else the first refittable type is chosen. Also use refit capacity to determine the default capacity.
peter1138
parents: 3963
diff changeset
   323
7624a250577b (svn r5146) - NewGRF: Support selection of aircraft default cargo type by checking its refit mask. If aircraft can be refitted to passengers, no change happens, else the first refittable type is chosen. Also use refit capacity to determine the default capacity.
peter1138
parents: 3963
diff changeset
   324
			/* Set the 'second compartent' capacity to none */
7624a250577b (svn r5146) - NewGRF: Support selection of aircraft default cargo type by checking its refit mask. If aircraft can be refitted to passengers, no change happens, else the first refittable type is chosen. Also use refit capacity to determine the default capacity.
peter1138
parents: 3963
diff changeset
   325
			u->cargo_cap = 0;
7624a250577b (svn r5146) - NewGRF: Support selection of aircraft default cargo type by checking its refit mask. If aircraft can be refitted to passengers, no change happens, else the first refittable type is chosen. Also use refit capacity to determine the default capacity.
peter1138
parents: 3963
diff changeset
   326
		}
7624a250577b (svn r5146) - NewGRF: Support selection of aircraft default cargo type by checking its refit mask. If aircraft can be refitted to passengers, no change happens, else the first refittable type is chosen. Also use refit capacity to determine the default capacity.
peter1138
parents: 3963
diff changeset
   327
1926
68d60188a22f (svn r2432) Use GetEngine() instead of DEREF_ENGINE() or even _engines[]
tron
parents: 1901
diff changeset
   328
		e = GetEngine(p1);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   329
		v->reliability = e->reliability;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   330
		v->reliability_spd_dec = e->reliability_spd_dec;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   331
		v->max_age = e->lifelength * 366;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   332
2564
371e0fa97e1e (svn r3101) -Codechange: added _new_vehicle_id
bjarni
parents: 2561
diff changeset
   333
		_new_vehicle_id = v->index;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   334
2423
84222db5a01a (svn r2949) The AI no longer needs to 'cheat' to build aircraft; eg it builds them now from a hangar. Also, to query the price of a new aircraft tile information is not needed
Darkvater
parents: 2422
diff changeset
   335
		v->u.air.pos = MAX_ELEMENTS;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   336
2423
84222db5a01a (svn r2949) The AI no longer needs to 'cheat' to build aircraft; eg it builds them now from a hangar. Also, to query the price of a new aircraft tile information is not needed
Darkvater
parents: 2422
diff changeset
   337
		/* When we click on hangar we know the tile it is on. By that we know
84222db5a01a (svn r2949) The AI no longer needs to 'cheat' to build aircraft; eg it builds them now from a hangar. Also, to query the price of a new aircraft tile information is not needed
Darkvater
parents: 2422
diff changeset
   338
		 * its position in the array of depots the airport has.....we can search
84222db5a01a (svn r2949) The AI no longer needs to 'cheat' to build aircraft; eg it builds them now from a hangar. Also, to query the price of a new aircraft tile information is not needed
Darkvater
parents: 2422
diff changeset
   339
		 * layout for #th position of depot. Since layout must start with a listing
84222db5a01a (svn r2949) The AI no longer needs to 'cheat' to build aircraft; eg it builds them now from a hangar. Also, to query the price of a new aircraft tile information is not needed
Darkvater
parents: 2422
diff changeset
   340
		 * of all depots, it is simple */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   341
		{
3315
1f65f8260092 (svn r4079) Add GetSation{Index,ByTile}() to get the station index resp. the station from a tile
tron
parents: 3186
diff changeset
   342
			const Station* st = GetStationByTile(tile);
2423
84222db5a01a (svn r2949) The AI no longer needs to 'cheat' to build aircraft; eg it builds them now from a hangar. Also, to query the price of a new aircraft tile information is not needed
Darkvater
parents: 2422
diff changeset
   343
			const AirportFTAClass* apc = GetAirport(st->airport_type);
1533
f735e49c8deb (svn r2037) uint -> TileIndex, remove pointless casts, const, misc.
tron
parents: 1532
diff changeset
   344
			uint i;
f735e49c8deb (svn r2037) uint -> TileIndex, remove pointless casts, const, misc.
tron
parents: 1532
diff changeset
   345
2423
84222db5a01a (svn r2949) The AI no longer needs to 'cheat' to build aircraft; eg it builds them now from a hangar. Also, to query the price of a new aircraft tile information is not needed
Darkvater
parents: 2422
diff changeset
   346
			for (i = 0; i < apc->nof_depots; i++) {
84222db5a01a (svn r2949) The AI no longer needs to 'cheat' to build aircraft; eg it builds them now from a hangar. Also, to query the price of a new aircraft tile information is not needed
Darkvater
parents: 2422
diff changeset
   347
				if (st->airport_tile + ToTileIndexDiff(apc->airport_depots[i]) == tile) {
84222db5a01a (svn r2949) The AI no longer needs to 'cheat' to build aircraft; eg it builds them now from a hangar. Also, to query the price of a new aircraft tile information is not needed
Darkvater
parents: 2422
diff changeset
   348
					assert(apc->layout[i].heading == HANGAR);
84222db5a01a (svn r2949) The AI no longer needs to 'cheat' to build aircraft; eg it builds them now from a hangar. Also, to query the price of a new aircraft tile information is not needed
Darkvater
parents: 2422
diff changeset
   349
					v->u.air.pos = apc->layout[i].position;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   350
					break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   351
				}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   352
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   353
			// to ensure v->u.air.pos has been given a value
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   354
			assert(v->u.air.pos != MAX_ELEMENTS);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   355
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   356
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   357
		v->u.air.state = HANGAR;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   358
		v->u.air.previous_pos = v->u.air.pos;
3315
1f65f8260092 (svn r4079) Add GetSation{Index,ByTile}() to get the station index resp. the station from a tile
tron
parents: 3186
diff changeset
   359
		v->u.air.targetairport = GetStationIndex(tile);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   360
		v->next = u;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   361
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   362
		v->service_interval = _patches.servint_aircraft;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   363
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   364
		v->date_of_last_service = _date;
4329
9759d5c52010 (svn r6002) -Cleanup: remove the now redundant BASE_YEAR constant.
rubidium
parents: 4293
diff changeset
   365
		v->build_year = u->build_year = _cur_year;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   366
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   367
		v->cur_image = u->cur_image = 0xEA0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   368
2804
b932fe148d36 (svn r3352) - NewGRF: Move initialization of vehicle random_bits to DC_EXEC blocks to allow use of Random() instead of InteractiveRandom(), which will alleviate some possible network desyncs.
peter1138
parents: 2752
diff changeset
   369
		v->random_bits = VehicleRandomBits();
b932fe148d36 (svn r3352) - NewGRF: Move initialization of vehicle random_bits to DC_EXEC blocks to allow use of Random() instead of InteractiveRandom(), which will alleviate some possible network desyncs.
peter1138
parents: 2752
diff changeset
   370
		u->random_bits = VehicleRandomBits();
b932fe148d36 (svn r3352) - NewGRF: Move initialization of vehicle random_bits to DC_EXEC blocks to allow use of Random() instead of InteractiveRandom(), which will alleviate some possible network desyncs.
peter1138
parents: 2752
diff changeset
   371
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   372
		VehiclePositionChanged(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   373
		VehiclePositionChanged(u);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   374
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   375
		// Aircraft with 3 vehicles (chopper)?
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   376
		if (v->subtype == 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   377
			w = vl[2];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   378
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   379
			u->next = w;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   380
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   381
			w->type = VEH_Aircraft;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   382
			w->direction = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   383
			w->owner = _current_player;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   384
			w->x_pos = v->x_pos;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   385
			w->y_pos = v->y_pos;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   386
			w->z_pos = v->z_pos + 5;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   387
			w->x_offs = w->y_offs = -1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   388
			w->sprite_width = w->sprite_height = 2;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   389
			w->z_height = 1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   390
			w->vehstatus = VS_HIDDEN | VS_UNCLICKABLE;
3710
a3db4dff570f (svn r4653) - NewGRF: enable use of custom helicopter rotors in game (but not GUI windows yet) (mart3p)
peter1138
parents: 3701
diff changeset
   391
			w->spritenum = 0xFF;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   392
			w->subtype = 6;
2517
b90693227193 (svn r3046) Replace some numbers with sprite names and fix a typo in the sprite list
tron
parents: 2504
diff changeset
   393
			w->cur_image = SPR_ROTOR_STOPPED;
2804
b932fe148d36 (svn r3352) - NewGRF: Move initialization of vehicle random_bits to DC_EXEC blocks to allow use of Random() instead of InteractiveRandom(), which will alleviate some possible network desyncs.
peter1138
parents: 2752
diff changeset
   394
			w->random_bits = VehicleRandomBits();
3710
a3db4dff570f (svn r4653) - NewGRF: enable use of custom helicopter rotors in game (but not GUI windows yet) (mart3p)
peter1138
parents: 3701
diff changeset
   395
			/* Use rotor's air.state to store the rotor animation frame */
a3db4dff570f (svn r4653) - NewGRF: enable use of custom helicopter rotors in game (but not GUI windows yet) (mart3p)
peter1138
parents: 3701
diff changeset
   396
			w->u.air.state = HRS_ROTOR_STOPPED;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   397
			VehiclePositionChanged(w);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   398
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   399
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   400
		InvalidateWindow(WC_VEHICLE_DEPOT, v->tile);
588
1b60458bdc29 (svn r1009) -Feature: per-station vehicle lists
tron
parents: 578
diff changeset
   401
		RebuildVehicleLists();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   402
		InvalidateWindow(WC_COMPANY, v->owner);
2618
7546b838230b (svn r3156) -Fix: removed some cases where autoreplace windows were redrawn when nothing was changed
bjarni
parents: 2601
diff changeset
   403
		if (IsLocalPlayer())
7546b838230b (svn r3156) -Fix: removed some cases where autoreplace windows were redrawn when nothing was changed
bjarni
parents: 2601
diff changeset
   404
			InvalidateWindow(WC_REPLACE_VEHICLE, VEH_Aircraft); //updates the replace Aircraft window
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   405
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   406
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   407
	return value;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   408
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   409
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   410
410
8de2aaf20800 (svn r607) -Patch: [ 985102 ] static cleanup
tron
parents: 381
diff changeset
   411
static void DoDeleteAircraft(Vehicle *v)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   412
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   413
	DeleteWindowById(WC_VEHICLE_VIEW, v->index);
588
1b60458bdc29 (svn r1009) -Feature: per-station vehicle lists
tron
parents: 578
diff changeset
   414
	RebuildVehicleLists();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   415
	InvalidateWindow(WC_COMPANY, v->owner);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   416
	DeleteVehicleChain(v);
1055
3aae1d6f12aa (svn r1556) -Fix: Vehicle list updates should now really work
Celestar
parents: 1053
diff changeset
   417
	InvalidateWindowClasses(WC_AIRCRAFT_LIST);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   418
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   419
1786
a54634efeb98 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1752
diff changeset
   420
/** Sell an aircraft.
3491
4c8427796c64 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3421
diff changeset
   421
 * @param tile unused
1786
a54634efeb98 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1752
diff changeset
   422
 * @param p1 vehicle ID to be sold
a54634efeb98 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1752
diff changeset
   423
 * @param p2 unused
a54634efeb98 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1752
diff changeset
   424
 */
3491
4c8427796c64 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3421
diff changeset
   425
int32 CmdSellAircraft(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   426
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   427
	Vehicle *v;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   428
4352
8ddb01bc6075 (svn r6053) -Codechange: renamed all IsXXXIndex to IsValidXXXID
truelight
parents: 4351
diff changeset
   429
	if (!IsValidVehicleID(p1)) return CMD_ERROR;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   430
919
b0d6c7642f99 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 909
diff changeset
   431
	v = GetVehicle(p1);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   432
3183
cf71bd234ebd (svn r3829) Reduce the use of _error_message by directly returning error codes instead of using this global variable
tron
parents: 3160
diff changeset
   433
	if (v->type != VEH_Aircraft || !CheckOwnership(v->owner)) return CMD_ERROR;
3963
d7d284c2f1f7 (svn r5124) Add IsAircraftInHangar{Stopped,}(), which supersedes CheckStoppedInHangar()
tron
parents: 3955
diff changeset
   434
	if (!IsAircraftInHangarStopped(v)) return_cmd_error(STR_A01B_AIRCRAFT_MUST_BE_STOPPED);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   435
1237
0a1ce05c3d45 (svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents: 1235
diff changeset
   436
	SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES);
0a1ce05c3d45 (svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents: 1235
diff changeset
   437
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   438
	if (flags & DC_EXEC) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   439
		// Invalidate depot
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   440
		InvalidateWindow(WC_VEHICLE_DEPOT, v->tile);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   441
		DoDeleteAircraft(v);
2618
7546b838230b (svn r3156) -Fix: removed some cases where autoreplace windows were redrawn when nothing was changed
bjarni
parents: 2601
diff changeset
   442
		if (IsLocalPlayer())
7546b838230b (svn r3156) -Fix: removed some cases where autoreplace windows were redrawn when nothing was changed
bjarni
parents: 2601
diff changeset
   443
			InvalidateWindow(WC_REPLACE_VEHICLE, VEH_Aircraft); // updates the replace Aircraft window
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   444
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   445
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   446
	return -(int32)v->value;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   447
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   448
1786
a54634efeb98 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1752
diff changeset
   449
/** Start/Stop an aircraft.
3491
4c8427796c64 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3421
diff changeset
   450
 * @param tile unused
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   451
 * @param p1 aircraft ID to start/stop
1786
a54634efeb98 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1752
diff changeset
   452
 * @param p2 unused
a54634efeb98 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1752
diff changeset
   453
 */
3491
4c8427796c64 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3421
diff changeset
   454
int32 CmdStartStopAircraft(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   455
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   456
	Vehicle *v;
4338
cc4648e8e529 (svn r6039) -Fix: [newGRF] FS#287 Bugfix for error in NewGRF callback 31 code for aircraft (Mart3p)
bjarni
parents: 4329
diff changeset
   457
	uint16 callback;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   458
4352
8ddb01bc6075 (svn r6053) -Codechange: renamed all IsXXXIndex to IsValidXXXID
truelight
parents: 4351
diff changeset
   459
	if (!IsValidVehicleID(p1)) return CMD_ERROR;
1237
0a1ce05c3d45 (svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents: 1235
diff changeset
   460
919
b0d6c7642f99 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 909
diff changeset
   461
	v = GetVehicle(p1);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   462
1786
a54634efeb98 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1752
diff changeset
   463
	if (v->type != VEH_Aircraft || !CheckOwnership(v->owner)) return CMD_ERROR;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   464
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   465
	// cannot stop airplane when in flight, or when taking off / landing
4059
f1442dfe1c8a (svn r5346) - Feature: Add 4 new airports. 2 for aircraft, 2 for helicopters.
richk
parents: 4056
diff changeset
   466
	if (v->u.air.state >= STARTTAKEOFF && v->u.air.state < TERM7)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   467
		return_cmd_error(STR_A017_AIRCRAFT_IS_IN_FLIGHT);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   468
4338
cc4648e8e529 (svn r6039) -Fix: [newGRF] FS#287 Bugfix for error in NewGRF callback 31 code for aircraft (Mart3p)
bjarni
parents: 4329
diff changeset
   469
	/* Check if this aircraft can be started/stopped. The callback will fail or
cc4648e8e529 (svn r6039) -Fix: [newGRF] FS#287 Bugfix for error in NewGRF callback 31 code for aircraft (Mart3p)
bjarni
parents: 4329
diff changeset
   470
	 * return 0xFF if it can. */
cc4648e8e529 (svn r6039) -Fix: [newGRF] FS#287 Bugfix for error in NewGRF callback 31 code for aircraft (Mart3p)
bjarni
parents: 4329
diff changeset
   471
	callback = GetVehicleCallback(CBID_VEHICLE_START_STOP_CHECK, 0, 0, v->engine_type, v);
cc4648e8e529 (svn r6039) -Fix: [newGRF] FS#287 Bugfix for error in NewGRF callback 31 code for aircraft (Mart3p)
bjarni
parents: 4329
diff changeset
   472
	if (callback != CALLBACK_FAILED && callback != 0xFF) {
cc4648e8e529 (svn r6039) -Fix: [newGRF] FS#287 Bugfix for error in NewGRF callback 31 code for aircraft (Mart3p)
bjarni
parents: 4329
diff changeset
   473
		StringID error = GetGRFStringID(GetEngineGRFID(v->engine_type), 0xD000 + callback);
cc4648e8e529 (svn r6039) -Fix: [newGRF] FS#287 Bugfix for error in NewGRF callback 31 code for aircraft (Mart3p)
bjarni
parents: 4329
diff changeset
   474
		return_cmd_error(error);
cc4648e8e529 (svn r6039) -Fix: [newGRF] FS#287 Bugfix for error in NewGRF callback 31 code for aircraft (Mart3p)
bjarni
parents: 4329
diff changeset
   475
	}
cc4648e8e529 (svn r6039) -Fix: [newGRF] FS#287 Bugfix for error in NewGRF callback 31 code for aircraft (Mart3p)
bjarni
parents: 4329
diff changeset
   476
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   477
	if (flags & DC_EXEC) {
3963
d7d284c2f1f7 (svn r5124) Add IsAircraftInHangar{Stopped,}(), which supersedes CheckStoppedInHangar()
tron
parents: 3955
diff changeset
   478
		if (IsAircraftInHangarStopped(v)) {
3139
4c950c7ec5c9 (svn r3757) -Feature: Delete news items about vehicles, when they get stale
tron
parents: 3008
diff changeset
   479
			DeleteVehicleNews(p1, STR_A014_AIRCRAFT_IS_WAITING_IN);
4c950c7ec5c9 (svn r3757) -Feature: Delete news items about vehicles, when they get stale
tron
parents: 3008
diff changeset
   480
		}
4c950c7ec5c9 (svn r3757) -Feature: Delete news items about vehicles, when they get stale
tron
parents: 3008
diff changeset
   481
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   482
		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
   483
		InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   484
		InvalidateWindow(WC_VEHICLE_DEPOT, v->tile);
1055
3aae1d6f12aa (svn r1556) -Fix: Vehicle list updates should now really work
Celestar
parents: 1053
diff changeset
   485
		InvalidateWindowClasses(WC_AIRCRAFT_LIST);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   486
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   487
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   488
	return 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   489
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   490
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   491
/** Send an aircraft to the hangar.
3491
4c8427796c64 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3421
diff changeset
   492
 * @param tile unused
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   493
 * @param p1 vehicle ID to send to the hangar
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   494
 * @param p2 various bitmasked elements
4506
0d8fcc0a4e49 (svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents: 4463
diff changeset
   495
 * - p2 bit 0-3 - DEPOT_ flags (see vehicle.h)
0d8fcc0a4e49 (svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents: 4463
diff changeset
   496
 * - p2 bit 8-10 - VLW flag (for mass goto depot)
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   497
 */
3491
4c8427796c64 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3421
diff changeset
   498
int32 CmdSendAircraftToHangar(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   499
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   500
	Vehicle *v;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   501
4506
0d8fcc0a4e49 (svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents: 4463
diff changeset
   502
	if (p2 & DEPOT_MASS_SEND) {
0d8fcc0a4e49 (svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents: 4463
diff changeset
   503
		/* Mass goto depot requested */
0d8fcc0a4e49 (svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents: 4463
diff changeset
   504
		if (!ValidVLWFlags(p2 & VLW_FLAGS)) return CMD_ERROR;
0d8fcc0a4e49 (svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents: 4463
diff changeset
   505
		return SendAllVehiclesToDepot(VEH_Aircraft, flags, p2 & DEPOT_SERVICE, _current_player, (p2 & VLW_FLAGS), p1);
4463
3a70624c40eb (svn r6246) -Feature: added the many times requested "send all vehicle to depot" button
bjarni
parents: 4451
diff changeset
   506
	}
3a70624c40eb (svn r6246) -Feature: added the many times requested "send all vehicle to depot" button
bjarni
parents: 4451
diff changeset
   507
4506
0d8fcc0a4e49 (svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents: 4463
diff changeset
   508
	if (!IsValidVehicleID(p1)) return CMD_ERROR;
1237
0a1ce05c3d45 (svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents: 1235
diff changeset
   509
919
b0d6c7642f99 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 909
diff changeset
   510
	v = GetVehicle(p1);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   511
4526
77ef74d28e76 (svn r6352) -Fix: FS#322 Send to depot bug
bjarni
parents: 4519
diff changeset
   512
	if (v->type != VEH_Aircraft || !CheckOwnership(v->owner) || IsAircraftInHangar(v)) return CMD_ERROR;
4451
66603f0f732e (svn r6229) -Feature: Shared order lists now got a "goto depot" button
bjarni
parents: 4434
diff changeset
   513
4506
0d8fcc0a4e49 (svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents: 4463
diff changeset
   514
	if (v->current_order.type == OT_GOTO_DEPOT && !(p2 & DEPOT_LOCATE_HANGAR)) {
4519
1437b1e7416c (svn r6304) -Codechange r6295: Use !! instead of (bool) in commands to send vehicles to depots
bjarni
parents: 4510
diff changeset
   515
		if (!!(p2 & DEPOT_SERVICE) == HASBIT(v->current_order.flags, OFB_HALT_IN_DEPOT)) {
4510
071bca98c577 (svn r6295) -Feature: using goto depot with a different control selection will now alter the service/stopping in depot flag instead of cancelling the goto depot order
bjarni
parents: 4506
diff changeset
   516
			/* We called with a different DEPOT_SERVICE setting.
071bca98c577 (svn r6295) -Feature: using goto depot with a different control selection will now alter the service/stopping in depot flag instead of cancelling the goto depot order
bjarni
parents: 4506
diff changeset
   517
			 * Now we change the setting to apply the new one and let the vehicle head for the same hangar.
071bca98c577 (svn r6295) -Feature: using goto depot with a different control selection will now alter the service/stopping in depot flag instead of cancelling the goto depot order
bjarni
parents: 4506
diff changeset
   518
			 * Note: the if is (true for requesting service == true for ordered to stop in hangar)          */
071bca98c577 (svn r6295) -Feature: using goto depot with a different control selection will now alter the service/stopping in depot flag instead of cancelling the goto depot order
bjarni
parents: 4506
diff changeset
   519
			if (flags & DC_EXEC) {
071bca98c577 (svn r6295) -Feature: using goto depot with a different control selection will now alter the service/stopping in depot flag instead of cancelling the goto depot order
bjarni
parents: 4506
diff changeset
   520
				TOGGLEBIT(v->current_order.flags, OFB_HALT_IN_DEPOT);
071bca98c577 (svn r6295) -Feature: using goto depot with a different control selection will now alter the service/stopping in depot flag instead of cancelling the goto depot order
bjarni
parents: 4506
diff changeset
   521
				InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
071bca98c577 (svn r6295) -Feature: using goto depot with a different control selection will now alter the service/stopping in depot flag instead of cancelling the goto depot order
bjarni
parents: 4506
diff changeset
   522
			}
071bca98c577 (svn r6295) -Feature: using goto depot with a different control selection will now alter the service/stopping in depot flag instead of cancelling the goto depot order
bjarni
parents: 4506
diff changeset
   523
			return 0;
071bca98c577 (svn r6295) -Feature: using goto depot with a different control selection will now alter the service/stopping in depot flag instead of cancelling the goto depot order
bjarni
parents: 4506
diff changeset
   524
		}
071bca98c577 (svn r6295) -Feature: using goto depot with a different control selection will now alter the service/stopping in depot flag instead of cancelling the goto depot order
bjarni
parents: 4506
diff changeset
   525
4506
0d8fcc0a4e49 (svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents: 4463
diff changeset
   526
		if (p2 & DEPOT_DONT_CANCEL) return CMD_ERROR; // Requested no cancelation of hangar orders
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   527
		if (flags & DC_EXEC) {
555
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
   528
			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
   529
			v->current_order.type = OT_DUMMY;
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
   530
			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
   531
			InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   532
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   533
	} else {
1262
b232a05f37e4 (svn r1766) - Feature: Aircrafts will now go to the nearest hangar if the next airport in the orders do not have one (helipads)
bjarni
parents: 1245
diff changeset
   534
		bool next_airport_has_hangar = true;
4111
8efd85ca4b76 (svn r5454) -Fix: [ 1394231 ] Autorenew glitch on helicopters
bjarni
parents: 4095
diff changeset
   535
		/* If bit 17 is set, next airport is specified by low word of p2, otherwise it's the target airport */
4412
945e8a9bb89a (svn r6165) -Feature: control click Goto Depot will now make the vehicle service
bjarni
parents: 4389
diff changeset
   536
		StationID next_airport_index = v->u.air.targetairport;
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   537
		const Station *st = GetStation(next_airport_index);
4111
8efd85ca4b76 (svn r5454) -Fix: [ 1394231 ] Autorenew glitch on helicopters
bjarni
parents: 4095
diff changeset
   538
		/* If the station is not a valid airport or if it has no hangars */
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   539
		if (!IsValidStation(st) || st->airport_tile == 0 || GetAirport(st->airport_type)->nof_depots == 0) {
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   540
			StationID station;
1532
83719caec8d1 (svn r2036) Fix FindNearestHanger(), it never worked correctly because 0xFFFF != 65000 and a 16bit number has no 17th bit
tron
parents: 1530
diff changeset
   541
4506
0d8fcc0a4e49 (svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents: 4463
diff changeset
   542
			if (!(p2 & DEPOT_LOCATE_HANGAR)) return CMD_ERROR;
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   543
			// the aircraft has to search for a hangar on its own
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   544
			station = FindNearestHangar(v);
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   545
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   546
			next_airport_has_hangar = false;
4506
0d8fcc0a4e49 (svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents: 4463
diff changeset
   547
			if (station == INVALID_STATION) return CMD_ERROR;
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   548
			st = GetStation(station);
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   549
			next_airport_index = station;
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   550
1262
b232a05f37e4 (svn r1766) - Feature: Aircrafts will now go to the nearest hangar if the next airport in the orders do not have one (helipads)
bjarni
parents: 1245
diff changeset
   551
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   552
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   553
		if (flags & DC_EXEC) {
555
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
   554
			v->current_order.type = OT_GOTO_DEPOT;
4412
945e8a9bb89a (svn r6165) -Feature: control click Goto Depot will now make the vehicle service
bjarni
parents: 4389
diff changeset
   555
			v->current_order.flags = OF_NON_STOP;
4506
0d8fcc0a4e49 (svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents: 4463
diff changeset
   556
			if (!(p1 & DEPOT_SERVICE)) SETBIT(v->current_order.flags, OFB_HALT_IN_DEPOT);
4527
b18634a31a4a (svn r6353) -Codechange: Make DestinationID a typedef of uin16, which is as large as any type of destinataion (StationID, DepotID, WaypointID) it can hold
tron
parents: 4526
diff changeset
   557
			v->current_order.dest = next_airport_index;
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
   558
			InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
4506
0d8fcc0a4e49 (svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents: 4463
diff changeset
   559
			if (p2 & DEPOT_LOCATE_HANGAR || (p2 & DEPOT_SERVICE && v->u.air.state == FLYING && !next_airport_has_hangar)) {
0d8fcc0a4e49 (svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents: 4463
diff changeset
   560
				/* The aircraft is now heading for a different hangar than the next in the orders */
1194
616dddfc77f3 (svn r1698) - Feature [autoreplace] helicopters will now go to the nearest hangar if they are set to autoreplace and don't have any airport with a hangar in
bjarni
parents: 1151
diff changeset
   561
				AircraftNextAirportPos_and_Order(v);
616dddfc77f3 (svn r1698) - Feature [autoreplace] helicopters will now go to the nearest hangar if they are set to autoreplace and don't have any airport with a hangar in
bjarni
parents: 1151
diff changeset
   562
				v->u.air.targetairport = next_airport_index;
616dddfc77f3 (svn r1698) - Feature [autoreplace] helicopters will now go to the nearest hangar if they are set to autoreplace and don't have any airport with a hangar in
bjarni
parents: 1151
diff changeset
   563
			}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   564
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   565
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   566
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   567
	return 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   568
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   569
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   570
1802
448f187042d3 (svn r2306) - CodeChange: Check the last commands; refits. This needed an extensive rewrite and global/local-cargo ID juggling and bitmasking. However with this done it looks better as well and is compatible with newgrf handling. Big thanks to HackyKid for doing most of the work. This also closes patch "[ 1199277 ] Command checks"
Darkvater
parents: 1793
diff changeset
   571
/** Refits an aircraft to the specified cargo type.
3491
4c8427796c64 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3421
diff changeset
   572
 * @param tile unused
1802
448f187042d3 (svn r2306) - CodeChange: Check the last commands; refits. This needed an extensive rewrite and global/local-cargo ID juggling and bitmasking. However with this done it looks better as well and is compatible with newgrf handling. Big thanks to HackyKid for doing most of the work. This also closes patch "[ 1199277 ] Command checks"
Darkvater
parents: 1793
diff changeset
   573
 * @param p1 vehicle ID of the aircraft to refit
448f187042d3 (svn r2306) - CodeChange: Check the last commands; refits. This needed an extensive rewrite and global/local-cargo ID juggling and bitmasking. However with this done it looks better as well and is compatible with newgrf handling. Big thanks to HackyKid for doing most of the work. This also closes patch "[ 1199277 ] Command checks"
Darkvater
parents: 1793
diff changeset
   574
 * @param p2 various bitstuffed elements
2635
88b8b74c01ac (svn r3177) GB, CLRBIT, HASBIT, TOGGLEBIT
tron
parents: 2630
diff changeset
   575
 * - p2 = (bit 0-7) - the new cargo type to refit to
3954
634cd5e35803 (svn r5103) - Add cargo subtype parameter to refit commands (mart3p)
peter1138
parents: 3948
diff changeset
   576
 * - p2 = (bit 8-15) - the new cargo subtype to refit to
1802
448f187042d3 (svn r2306) - CodeChange: Check the last commands; refits. This needed an extensive rewrite and global/local-cargo ID juggling and bitmasking. However with this done it looks better as well and is compatible with newgrf handling. Big thanks to HackyKid for doing most of the work. This also closes patch "[ 1199277 ] Command checks"
Darkvater
parents: 1793
diff changeset
   577
 */
3491
4c8427796c64 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3421
diff changeset
   578
int32 CmdRefitAircraft(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   579
{
1802
448f187042d3 (svn r2306) - CodeChange: Check the last commands; refits. This needed an extensive rewrite and global/local-cargo ID juggling and bitmasking. However with this done it looks better as well and is compatible with newgrf handling. Big thanks to HackyKid for doing most of the work. This also closes patch "[ 1199277 ] Command checks"
Darkvater
parents: 1793
diff changeset
   580
	Vehicle *v;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   581
	int pass, mail;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   582
	int32 cost;
2635
88b8b74c01ac (svn r3177) GB, CLRBIT, HASBIT, TOGGLEBIT
tron
parents: 2630
diff changeset
   583
	CargoID new_cid = GB(p2, 0, 8);
3954
634cd5e35803 (svn r5103) - Add cargo subtype parameter to refit commands (mart3p)
peter1138
parents: 3948
diff changeset
   584
	byte new_subtype = GB(p2, 8, 8);
1802
448f187042d3 (svn r2306) - CodeChange: Check the last commands; refits. This needed an extensive rewrite and global/local-cargo ID juggling and bitmasking. However with this done it looks better as well and is compatible with newgrf handling. Big thanks to HackyKid for doing most of the work. This also closes patch "[ 1199277 ] Command checks"
Darkvater
parents: 1793
diff changeset
   585
	const AircraftVehicleInfo *avi;
3974
ee12e0c0336c (svn r5147) - NewGRF: Use refit capacity callback when refitting an aircraft (mart3p)
peter1138
parents: 3973
diff changeset
   586
	uint16 callback = CALLBACK_FAILED;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   587
4352
8ddb01bc6075 (svn r6053) -Codechange: renamed all IsXXXIndex to IsValidXXXID
truelight
parents: 4351
diff changeset
   588
	if (!IsValidVehicleID(p1)) return CMD_ERROR;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   589
919
b0d6c7642f99 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 909
diff changeset
   590
	v = GetVehicle(p1);
1237
0a1ce05c3d45 (svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents: 1235
diff changeset
   591
1802
448f187042d3 (svn r2306) - CodeChange: Check the last commands; refits. This needed an extensive rewrite and global/local-cargo ID juggling and bitmasking. However with this done it looks better as well and is compatible with newgrf handling. Big thanks to HackyKid for doing most of the work. This also closes patch "[ 1199277 ] Command checks"
Darkvater
parents: 1793
diff changeset
   592
	if (v->type != VEH_Aircraft || !CheckOwnership(v->owner)) return CMD_ERROR;
3963
d7d284c2f1f7 (svn r5124) Add IsAircraftInHangar{Stopped,}(), which supersedes CheckStoppedInHangar()
tron
parents: 3955
diff changeset
   593
	if (!IsAircraftInHangarStopped(v)) return_cmd_error(STR_A01B_AIRCRAFT_MUST_BE_STOPPED);
1237
0a1ce05c3d45 (svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents: 1235
diff changeset
   594
924
433faaf547c1 (svn r1412) -Feature/Fix: Aircraft refit options have been restricted to "sane"
celestar
parents: 922
diff changeset
   595
	avi = AircraftVehInfo(v->engine_type);
1237
0a1ce05c3d45 (svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents: 1235
diff changeset
   596
1802
448f187042d3 (svn r2306) - CodeChange: Check the last commands; refits. This needed an extensive rewrite and global/local-cargo ID juggling and bitmasking. However with this done it looks better as well and is compatible with newgrf handling. Big thanks to HackyKid for doing most of the work. This also closes patch "[ 1199277 ] Command checks"
Darkvater
parents: 1793
diff changeset
   597
	/* Check cargo */
2704
dc9ae68dfd74 (svn r3248) - Codechange: Change interface of CanRefitTo() to supply the engine type directly instead of getting it from a vehicle. This allows the function to be used before vehicles are involved.
peter1138
parents: 2697
diff changeset
   598
	if (new_cid > NUM_CARGO || !CanRefitTo(v->engine_type, new_cid)) return CMD_ERROR;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   599
1237
0a1ce05c3d45 (svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents: 1235
diff changeset
   600
	SET_EXPENSES_TYPE(EXPENSES_AIRCRAFT_RUN);
0a1ce05c3d45 (svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents: 1235
diff changeset
   601
3974
ee12e0c0336c (svn r5147) - NewGRF: Use refit capacity callback when refitting an aircraft (mart3p)
peter1138
parents: 3973
diff changeset
   602
	/* Check the refit capacity callback */
ee12e0c0336c (svn r5147) - NewGRF: Use refit capacity callback when refitting an aircraft (mart3p)
peter1138
parents: 3973
diff changeset
   603
	if (HASBIT(EngInfo(v->engine_type)->callbackmask, CBM_REFIT_CAPACITY)) {
ee12e0c0336c (svn r5147) - NewGRF: Use refit capacity callback when refitting an aircraft (mart3p)
peter1138
parents: 3973
diff changeset
   604
		/* Back up the existing cargo type */
ee12e0c0336c (svn r5147) - NewGRF: Use refit capacity callback when refitting an aircraft (mart3p)
peter1138
parents: 3973
diff changeset
   605
		CargoID temp_cid = v->cargo_type;
3988
b76291f6ce29 (svn r5187) - NewGRF: temporarily change a vehicle's cargo subtype when calling the refit capacity callback.
peter1138
parents: 3987
diff changeset
   606
		byte temp_subtype = v->cargo_subtype;
3974
ee12e0c0336c (svn r5147) - NewGRF: Use refit capacity callback when refitting an aircraft (mart3p)
peter1138
parents: 3973
diff changeset
   607
		v->cargo_type = new_cid;
3988
b76291f6ce29 (svn r5187) - NewGRF: temporarily change a vehicle's cargo subtype when calling the refit capacity callback.
peter1138
parents: 3987
diff changeset
   608
		v->cargo_subtype = new_subtype;
3974
ee12e0c0336c (svn r5147) - NewGRF: Use refit capacity callback when refitting an aircraft (mart3p)
peter1138
parents: 3973
diff changeset
   609
ee12e0c0336c (svn r5147) - NewGRF: Use refit capacity callback when refitting an aircraft (mart3p)
peter1138
parents: 3973
diff changeset
   610
		callback = GetVehicleCallback(CBID_VEHICLE_REFIT_CAPACITY, 0, 0, v->engine_type, v);
ee12e0c0336c (svn r5147) - NewGRF: Use refit capacity callback when refitting an aircraft (mart3p)
peter1138
parents: 3973
diff changeset
   611
ee12e0c0336c (svn r5147) - NewGRF: Use refit capacity callback when refitting an aircraft (mart3p)
peter1138
parents: 3973
diff changeset
   612
		/* Restore the cargo type */
ee12e0c0336c (svn r5147) - NewGRF: Use refit capacity callback when refitting an aircraft (mart3p)
peter1138
parents: 3973
diff changeset
   613
		v->cargo_type = temp_cid;
3988
b76291f6ce29 (svn r5187) - NewGRF: temporarily change a vehicle's cargo subtype when calling the refit capacity callback.
peter1138
parents: 3987
diff changeset
   614
		v->cargo_subtype = temp_subtype;
3974
ee12e0c0336c (svn r5147) - NewGRF: Use refit capacity callback when refitting an aircraft (mart3p)
peter1138
parents: 3973
diff changeset
   615
	}
ee12e0c0336c (svn r5147) - NewGRF: Use refit capacity callback when refitting an aircraft (mart3p)
peter1138
parents: 3973
diff changeset
   616
ee12e0c0336c (svn r5147) - NewGRF: Use refit capacity callback when refitting an aircraft (mart3p)
peter1138
parents: 3973
diff changeset
   617
	if (callback == CALLBACK_FAILED) {
ee12e0c0336c (svn r5147) - NewGRF: Use refit capacity callback when refitting an aircraft (mart3p)
peter1138
parents: 3973
diff changeset
   618
		/* If the callback failed, or wasn't executed, use the aircraft's
ee12e0c0336c (svn r5147) - NewGRF: Use refit capacity callback when refitting an aircraft (mart3p)
peter1138
parents: 3973
diff changeset
   619
		 * default cargo capacity */
ee12e0c0336c (svn r5147) - NewGRF: Use refit capacity callback when refitting an aircraft (mart3p)
peter1138
parents: 3973
diff changeset
   620
		pass = AircraftDefaultCargoCapacity(new_cid, v->engine_type);
ee12e0c0336c (svn r5147) - NewGRF: Use refit capacity callback when refitting an aircraft (mart3p)
peter1138
parents: 3973
diff changeset
   621
	} else {
ee12e0c0336c (svn r5147) - NewGRF: Use refit capacity callback when refitting an aircraft (mart3p)
peter1138
parents: 3973
diff changeset
   622
		pass = callback;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   623
	}
3008
7b519db78ce2 (svn r3588) - Refit: Use only one global to return the refit capacity rather than one per vehicle type.
peter1138
parents: 3005
diff changeset
   624
	_returned_refit_capacity = pass;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   625
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   626
	cost = 0;
1802
448f187042d3 (svn r2306) - CodeChange: Check the last commands; refits. This needed an extensive rewrite and global/local-cargo ID juggling and bitmasking. However with this done it looks better as well and is compatible with newgrf handling. Big thanks to HackyKid for doing most of the work. This also closes patch "[ 1199277 ] Command checks"
Darkvater
parents: 1793
diff changeset
   627
	if (IS_HUMAN_PLAYER(v->owner) && new_cid != v->cargo_type) {
4242
e6baba95f369 (svn r5819) - NewGRF: add support for refit costs specified in NewGRF. This may affect the default refit costs for the default rail vehicles.
peter1138
parents: 4175
diff changeset
   628
		cost = (EngInfo(v->engine_type)->refit_cost * _price.aircraft_base) >> 10;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   629
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   630
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   631
	if (flags & DC_EXEC) {
1802
448f187042d3 (svn r2306) - CodeChange: Check the last commands; refits. This needed an extensive rewrite and global/local-cargo ID juggling and bitmasking. However with this done it looks better as well and is compatible with newgrf handling. Big thanks to HackyKid for doing most of the work. This also closes patch "[ 1199277 ] Command checks"
Darkvater
parents: 1793
diff changeset
   632
		Vehicle *u;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   633
		v->cargo_cap = pass;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   634
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   635
		u = v->next;
1802
448f187042d3 (svn r2306) - CodeChange: Check the last commands; refits. This needed an extensive rewrite and global/local-cargo ID juggling and bitmasking. However with this done it looks better as well and is compatible with newgrf handling. Big thanks to HackyKid for doing most of the work. This also closes patch "[ 1199277 ] Command checks"
Darkvater
parents: 1793
diff changeset
   636
		mail = (new_cid != CT_PASSENGERS) ? 0 : avi->mail_capacity;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   637
		u->cargo_cap = mail;
3955
81e7e25e4599 (svn r5104) - When refitting a vehicle to its existing cargo type, don't lose the cargo onboard (useful when adding wagons to a train)
peter1138
parents: 3954
diff changeset
   638
		if (v->cargo_type == new_cid) {
81e7e25e4599 (svn r5104) - When refitting a vehicle to its existing cargo type, don't lose the cargo onboard (useful when adding wagons to a train)
peter1138
parents: 3954
diff changeset
   639
			v->cargo_count = min(pass, v->cargo_count);
81e7e25e4599 (svn r5104) - When refitting a vehicle to its existing cargo type, don't lose the cargo onboard (useful when adding wagons to a train)
peter1138
parents: 3954
diff changeset
   640
			u->cargo_count = min(mail, u->cargo_count);
81e7e25e4599 (svn r5104) - When refitting a vehicle to its existing cargo type, don't lose the cargo onboard (useful when adding wagons to a train)
peter1138
parents: 3954
diff changeset
   641
		} else {
81e7e25e4599 (svn r5104) - When refitting a vehicle to its existing cargo type, don't lose the cargo onboard (useful when adding wagons to a train)
peter1138
parents: 3954
diff changeset
   642
			v->cargo_count = 0;
81e7e25e4599 (svn r5104) - When refitting a vehicle to its existing cargo type, don't lose the cargo onboard (useful when adding wagons to a train)
peter1138
parents: 3954
diff changeset
   643
			u->cargo_count = 0;
81e7e25e4599 (svn r5104) - When refitting a vehicle to its existing cargo type, don't lose the cargo onboard (useful when adding wagons to a train)
peter1138
parents: 3954
diff changeset
   644
		}
1802
448f187042d3 (svn r2306) - CodeChange: Check the last commands; refits. This needed an extensive rewrite and global/local-cargo ID juggling and bitmasking. However with this done it looks better as well and is compatible with newgrf handling. Big thanks to HackyKid for doing most of the work. This also closes patch "[ 1199277 ] Command checks"
Darkvater
parents: 1793
diff changeset
   645
		v->cargo_type = new_cid;
3954
634cd5e35803 (svn r5103) - Add cargo subtype parameter to refit commands (mart3p)
peter1138
parents: 3948
diff changeset
   646
		v->cargo_subtype = new_subtype;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   647
		InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
3722
00921cc83d70 (svn r4693) - NewGRF: invalidate aircraft in viewports during loading stages so that graphic changes are visible. (mart3p)
peter1138
parents: 3710
diff changeset
   648
		InvalidateWindow(WC_VEHICLE_DEPOT, v->tile);
00921cc83d70 (svn r4693) - NewGRF: invalidate aircraft in viewports during loading stages so that graphic changes are visible. (mart3p)
peter1138
parents: 3710
diff changeset
   649
		RebuildVehicleLists();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   650
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   651
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   652
	return cost;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   653
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   654
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   655
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   656
static void CheckIfAircraftNeedsService(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   657
{
2630
35249d2ded3e (svn r3172) static, const
tron
parents: 2618
diff changeset
   658
	const Station* st;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   659
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   660
	if (_patches.servint_aircraft == 0) return;
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   661
	if (!VehicleNeedsService(v)) return;
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   662
	if (v->vehstatus & VS_STOPPED) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   663
555
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
   664
	if (v->current_order.type == OT_GOTO_DEPOT &&
1530
caa16c506a22 (svn r2034) -Codechange: Replaced the rather meaningless LOAD and UNLOAD flags for depots by something more meaningful
celestar
parents: 1520
diff changeset
   665
			v->current_order.flags & OF_HALT_IN_DEPOT)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   666
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   667
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   668
	if (_patches.gotodepot && VehicleHasDepotOrders(v)) return;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   669
4529
2bfac24af02a (svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents: 4527
diff changeset
   670
	if (IsAircraftInHangar(v)) {
2bfac24af02a (svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents: 4527
diff changeset
   671
		VehicleServiceInDepot(v);
2bfac24af02a (svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents: 4527
diff changeset
   672
		return;
2bfac24af02a (svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents: 4527
diff changeset
   673
	}
2bfac24af02a (svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents: 4527
diff changeset
   674
4527
b18634a31a4a (svn r6353) -Codechange: Make DestinationID a typedef of uin16, which is as large as any type of destinataion (StationID, DepotID, WaypointID) it can hold
tron
parents: 4526
diff changeset
   675
	st = GetStation(v->current_order.dest);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   676
	// only goto depot if the target airport has terminals (eg. it is airport)
4346
3f00094f2670 (svn r6047) -Codechange: FOR_ALL now _only_ loops valid items, and skips invalid ones
truelight
parents: 4344
diff changeset
   677
	if (IsValidStation(st) && st->airport_tile != 0 && GetAirport(st->airport_type)->terminals != NULL) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   678
//		printf("targetairport = %d, st->index = %d\n", v->u.air.targetairport, st->index);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   679
//		v->u.air.targetairport = st->index;
555
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
   680
		v->current_order.type = OT_GOTO_DEPOT;
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
   681
		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
   682
		InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
555
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
   683
	} else if (v->current_order.type == OT_GOTO_DEPOT) {
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
   684
		v->current_order.type = OT_DUMMY;
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
   685
		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
   686
		InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   687
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   688
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   689
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   690
void OnNewDay_Aircraft(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   691
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   692
	int32 cost;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   693
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   694
	if (v->subtype > 2) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   695
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   696
	if ((++v->day_counter & 7) == 0) DecreaseVehicleValue(v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   697
3140
69cb681c6d86 (svn r3758) Remove the news validation callback. It is superseded by r3757.
tron
parents: 3139
diff changeset
   698
	CheckOrders(v);
19
6080d2b6a959 (svn r20) Feature: warning when a vehicle has invalid orders (celestar)
dominik
parents: 11
diff changeset
   699
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   700
	CheckVehicleBreakdown(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   701
	AgeVehicle(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   702
	CheckIfAircraftNeedsService(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   703
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   704
	if (v->vehstatus & VS_STOPPED) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   705
538
5dd99f3b7eca (svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents: 534
diff changeset
   706
	cost = AircraftVehInfo(v->engine_type)->running_cost * _price.aircraft_running / 364;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   707
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   708
	v->profit_this_year -= cost >> 8;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   709
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   710
	SET_EXPENSES_TYPE(EXPENSES_AIRCRAFT_RUN);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   711
	SubtractMoneyFromPlayerFract(v->owner, cost);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   712
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   713
	InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
1055
3aae1d6f12aa (svn r1556) -Fix: Vehicle list updates should now really work
Celestar
parents: 1053
diff changeset
   714
	InvalidateWindowClasses(WC_AIRCRAFT_LIST);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   715
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   716
1093
e8d26c7dc42f (svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents: 1064
diff changeset
   717
void AircraftYearlyLoop(void)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   718
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   719
	Vehicle *v;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   720
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   721
	FOR_ALL_VEHICLES(v) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   722
		if (v->type == VEH_Aircraft && v->subtype <= 2) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   723
			v->profit_last_year = v->profit_this_year;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   724
			v->profit_this_year = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   725
			InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   726
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   727
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   728
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   729
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   730
static void AgeAircraftCargo(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   731
{
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   732
	if (_age_cargo_skip_counter != 0) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   733
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   734
	do {
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   735
		if (v->cargo_days != 0xFF) v->cargo_days++;
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   736
		v = v->next;
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   737
	} while (v != NULL);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   738
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   739
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   740
static void HelicopterTickHandler(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   741
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   742
	Vehicle *u;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   743
	int tick,spd;
3710
a3db4dff570f (svn r4653) - NewGRF: enable use of custom helicopter rotors in game (but not GUI windows yet) (mart3p)
peter1138
parents: 3701
diff changeset
   744
	SpriteID img;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   745
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   746
	u = v->next->next;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   747
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   748
	if (u->vehstatus & VS_HIDDEN) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   749
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   750
	// if true, helicopter rotors do not rotate. This should only be the case if a helicopter is
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   751
	// loading/unloading at a terminal or stopped
555
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
   752
	if (v->current_order.type == OT_LOADING || (v->vehstatus & VS_STOPPED)) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   753
		if (u->cur_speed != 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   754
			u->cur_speed++;
3710
a3db4dff570f (svn r4653) - NewGRF: enable use of custom helicopter rotors in game (but not GUI windows yet) (mart3p)
peter1138
parents: 3701
diff changeset
   755
			if (u->cur_speed >= 0x80 && u->u.air.state == HRS_ROTOR_MOVING_3) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   756
				u->cur_speed = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   757
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   758
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   759
	} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   760
		if (u->cur_speed == 0)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   761
			u->cur_speed = 0x70;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   762
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   763
		if (u->cur_speed >= 0x50)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   764
			u->cur_speed--;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   765
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   766
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   767
	tick = ++u->tick_counter;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   768
	spd = u->cur_speed >> 4;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   769
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   770
	if (spd == 0) {
3710
a3db4dff570f (svn r4653) - NewGRF: enable use of custom helicopter rotors in game (but not GUI windows yet) (mart3p)
peter1138
parents: 3701
diff changeset
   771
		u->u.air.state = HRS_ROTOR_STOPPED;
a3db4dff570f (svn r4653) - NewGRF: enable use of custom helicopter rotors in game (but not GUI windows yet) (mart3p)
peter1138
parents: 3701
diff changeset
   772
		img = GetRotorImage(v);
2549
f1d3b383d557 (svn r3078) Some more stuff, which piled up:
tron
parents: 2517
diff changeset
   773
		if (u->cur_image == img) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   774
	} else if (tick >= spd) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   775
		u->tick_counter = 0;
3710
a3db4dff570f (svn r4653) - NewGRF: enable use of custom helicopter rotors in game (but not GUI windows yet) (mart3p)
peter1138
parents: 3701
diff changeset
   776
		u->u.air.state++;
a3db4dff570f (svn r4653) - NewGRF: enable use of custom helicopter rotors in game (but not GUI windows yet) (mart3p)
peter1138
parents: 3701
diff changeset
   777
		if (u->u.air.state > HRS_ROTOR_MOVING_3) u->u.air.state = HRS_ROTOR_MOVING_1;
a3db4dff570f (svn r4653) - NewGRF: enable use of custom helicopter rotors in game (but not GUI windows yet) (mart3p)
peter1138
parents: 3701
diff changeset
   778
		img = GetRotorImage(v);
2549
f1d3b383d557 (svn r3078) Some more stuff, which piled up:
tron
parents: 2517
diff changeset
   779
	} else {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   780
		return;
2549
f1d3b383d557 (svn r3078) Some more stuff, which piled up:
tron
parents: 2517
diff changeset
   781
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   782
2549
f1d3b383d557 (svn r3078) Some more stuff, which piled up:
tron
parents: 2517
diff changeset
   783
	u->cur_image = img;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   784
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   785
	BeginVehicleMove(u);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   786
	VehiclePositionChanged(u);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   787
	EndVehicleMove(u);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   788
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   789
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   790
static void SetAircraftPosition(Vehicle *v, int x, int y, int z)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   791
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   792
	Vehicle *u;
4259
71b8dba85ce1 (svn r5883) -Fix [FS#272]: use the height of the edge of the map for shadows of aircrafts that are outside the map; similar to r5841, caused by r5794.
rubidium
parents: 4244
diff changeset
   793
	int safe_x;
71b8dba85ce1 (svn r5883) -Fix [FS#272]: use the height of the edge of the map for shadows of aircrafts that are outside the map; similar to r5841, caused by r5794.
rubidium
parents: 4244
diff changeset
   794
	int safe_y;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   795
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   796
	v->x_pos = x;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   797
	v->y_pos = y;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   798
	v->z_pos = z;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   799
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   800
	v->cur_image = GetAircraftImage(v, v->direction);
3710
a3db4dff570f (svn r4653) - NewGRF: enable use of custom helicopter rotors in game (but not GUI windows yet) (mart3p)
peter1138
parents: 3701
diff changeset
   801
	if (v->subtype == 0) v->next->next->cur_image = GetRotorImage(v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   802
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   803
	BeginVehicleMove(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   804
	VehiclePositionChanged(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   805
	EndVehicleMove(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   806
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   807
	u = v->next;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   808
4259
71b8dba85ce1 (svn r5883) -Fix [FS#272]: use the height of the edge of the map for shadows of aircrafts that are outside the map; similar to r5841, caused by r5794.
rubidium
parents: 4244
diff changeset
   809
	safe_x = clamp(x, 0, MapMaxX() * TILE_SIZE);
71b8dba85ce1 (svn r5883) -Fix [FS#272]: use the height of the edge of the map for shadows of aircrafts that are outside the map; similar to r5841, caused by r5794.
rubidium
parents: 4244
diff changeset
   810
	safe_y = clamp(y - 1, 0, MapMaxY() * TILE_SIZE);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   811
	u->x_pos = x;
4259
71b8dba85ce1 (svn r5883) -Fix [FS#272]: use the height of the edge of the map for shadows of aircrafts that are outside the map; similar to r5841, caused by r5794.
rubidium
parents: 4244
diff changeset
   812
	u->y_pos = y - ((v->z_pos-GetSlopeZ(safe_x, safe_y)) >> 3);;
71b8dba85ce1 (svn r5883) -Fix [FS#272]: use the height of the edge of the map for shadows of aircrafts that are outside the map; similar to r5841, caused by r5794.
rubidium
parents: 4244
diff changeset
   813
71b8dba85ce1 (svn r5883) -Fix [FS#272]: use the height of the edge of the map for shadows of aircrafts that are outside the map; similar to r5841, caused by r5794.
rubidium
parents: 4244
diff changeset
   814
	safe_y = clamp(u->y_pos, 0, MapMaxY() * TILE_SIZE);
71b8dba85ce1 (svn r5883) -Fix [FS#272]: use the height of the edge of the map for shadows of aircrafts that are outside the map; similar to r5841, caused by r5794.
rubidium
parents: 4244
diff changeset
   815
	u->z_pos = GetSlopeZ(safe_x, safe_y);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   816
	u->cur_image = v->cur_image;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   817
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   818
	BeginVehicleMove(u);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   819
	VehiclePositionChanged(u);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   820
	EndVehicleMove(u);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   821
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   822
	u = u->next;
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   823
	if (u != NULL) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   824
		u->x_pos = x;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   825
		u->y_pos = y;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   826
		u->z_pos = z + 5;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   827
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   828
		BeginVehicleMove(u);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   829
		VehiclePositionChanged(u);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   830
		EndVehicleMove(u);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   831
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   832
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   833
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   834
static void ServiceAircraft(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   835
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   836
	Vehicle *u;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   837
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   838
	v->cur_speed = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   839
	v->subspeed = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   840
	v->progress = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   841
	v->vehstatus |= VS_HIDDEN;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   842
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   843
	u = v->next;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   844
	u->vehstatus |= VS_HIDDEN;
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   845
	u = u->next;
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   846
	if (u != NULL) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   847
		u->vehstatus |= VS_HIDDEN;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   848
		u->cur_speed = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   849
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   850
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   851
	SetAircraftPosition(v, v->x_pos, v->y_pos, v->z_pos);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   852
	InvalidateWindow(WC_VEHICLE_DEPOT, v->tile);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   853
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
   854
	VehicleServiceInDepot(v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   855
	InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   856
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   857
2549
f1d3b383d557 (svn r3078) Some more stuff, which piled up:
tron
parents: 2517
diff changeset
   858
static void PlayAircraftSound(const Vehicle* v)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   859
{
538
5dd99f3b7eca (svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents: 534
diff changeset
   860
	SndPlayVehicleFx(AircraftVehInfo(v->engine_type)->sfx, v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   861
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   862
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   863
static bool UpdateAircraftSpeed(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   864
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   865
	uint spd = v->acceleration * 2;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   866
	byte t;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   867
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   868
	v->subspeed = (t=v->subspeed) + (byte)spd;
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   869
	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
   870
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   871
	// adjust speed for broken vehicles
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   872
	if (v->vehstatus & VS_AIRCRAFT_BROKEN) spd = min(spd, 27);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   873
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   874
	//updates statusbar only if speed have changed to save CPU time
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   875
	if (spd != v->cur_speed) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   876
		v->cur_speed = spd;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   877
		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
   878
			InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   879
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   880
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   881
	if (!(v->direction & 1)) spd = spd * 3 / 4;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   882
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   883
	if (spd == 0) return false;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   884
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   885
	if ((byte)++spd == 0) return true;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   886
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   887
	v->progress = (t = v->progress) - (byte)spd;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   888
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   889
	return t < v->progress;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   890
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   891
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   892
// get Aircraft running altitude
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   893
static byte GetAircraftFlyingAltitude(const Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   894
{
1897
98b62e167a43 (svn r2403) if cascade -> switch
tron
parents: 1891
diff changeset
   895
	switch (v->max_speed) {
98b62e167a43 (svn r2403) if cascade -> switch
tron
parents: 1891
diff changeset
   896
		case 37: return 162;
98b62e167a43 (svn r2403) if cascade -> switch
tron
parents: 1891
diff changeset
   897
		case 74: return 171;
98b62e167a43 (svn r2403) if cascade -> switch
tron
parents: 1891
diff changeset
   898
		default: return 180;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   899
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   900
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   901
1401
56d372a75b74 (svn r1905) - Fix: [ 1118810 ] openttd: ship_cmd.c:642 ... Assertion failed. Mapwrap fixed in ship_cmd.c (was implicitely ok before biggermaps).
Darkvater
parents: 1359
diff changeset
   902
static bool AircraftController(Vehicle *v)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   903
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   904
	Station *st;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   905
	const AirportMovingData *amd;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   906
	Vehicle *u;
3158
a821c8fec829 (svn r3784) Add a type and functions to handle direction changes
tron
parents: 3157
diff changeset
   907
	byte z,newdir,maxz,curz;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   908
	GetNewVehiclePosResult gp;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   909
	uint dist;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   910
	int x,y;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   911
919
b0d6c7642f99 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 909
diff changeset
   912
	st = GetStation(v->u.air.targetairport);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   913
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   914
	// prevent going to 0,0 if airport is deleted.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   915
	{
1977
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1926
diff changeset
   916
		TileIndex tile = st->airport_tile;
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1926
diff changeset
   917
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   918
		if (tile == 0) tile = st->xy;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   919
		// xy of destination
3421
8ab76c47c72c (svn r4246) -Codechange. Replaced about 100 occurences of '16' by TILE_SIZE
celestar
parents: 3338
diff changeset
   920
		x = TileX(tile) * TILE_SIZE;
8ab76c47c72c (svn r4246) -Codechange. Replaced about 100 occurences of '16' by TILE_SIZE
celestar
parents: 3338
diff changeset
   921
		y = TileY(tile) * TILE_SIZE;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   922
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   923
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   924
	// get airport moving data
3701
13911c5df3e7 (svn r4642) - Codechange: reorganise airport.h and airport_movement.h to avoid having 8 copies of the airport FTAs, and make the enums used available elsewhere.
peter1138
parents: 3579
diff changeset
   925
	amd = GetAirportMovingData(st->airport_type, v->u.air.pos);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   926
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   927
	// Helicopter raise
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   928
	if (amd->flag & AMED_HELI_RAISE) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   929
		u = v->next->next;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   930
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   931
		// Make sure the rotors don't rotate too fast
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   932
		if (u->cur_speed > 32) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   933
			v->cur_speed = 0;
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   934
			if (--u->cur_speed == 32) SndPlayVehicleFx(SND_18_HELICOPTER, v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   935
		} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   936
			u->cur_speed = 32;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   937
			if (UpdateAircraftSpeed(v)) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   938
				v->tile = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   939
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   940
				// Reached altitude?
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   941
				if (v->z_pos >= 184) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   942
					v->cur_speed = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   943
					return true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   944
				}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   945
				SetAircraftPosition(v, v->x_pos, v->y_pos, v->z_pos+1);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   946
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   947
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   948
		return false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   949
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   950
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   951
	// Helicopter landing.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   952
	if (amd->flag & AMED_HELI_LOWER) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   953
		if (UpdateAircraftSpeed(v)) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   954
			if (st->airport_tile == 0) {
1401
56d372a75b74 (svn r1905) - Fix: [ 1118810 ] openttd: ship_cmd.c:642 ... Assertion failed. Mapwrap fixed in ship_cmd.c (was implicitely ok before biggermaps).
Darkvater
parents: 1359
diff changeset
   955
				// FIXME - AircraftController -> if station no longer exists, do not land
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   956
				// helicopter will circle until sign disappears, then go to next order
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   957
				// * what to do when it is the only order left, right now it just stays in 1 place
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   958
				v->u.air.state = FLYING;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   959
				AircraftNextAirportPos_and_Order(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   960
				return false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   961
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   962
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   963
			// Vehicle is now at the airport.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   964
			v->tile = st->airport_tile;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   965
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   966
			// Find altitude of landing position.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   967
			z = GetSlopeZ(x, y) + 1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   968
			if (st->airport_type == AT_OILRIG) z += 54;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   969
			if (st->airport_type == AT_HELIPORT) z += 60;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   970
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   971
			if (z == v->z_pos) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   972
				u = v->next->next;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   973
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   974
				// Increase speed of rotors. When speed is 80, we've landed.
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   975
				if (u->cur_speed >= 80) return true;
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   976
				u->cur_speed += 4;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   977
			} else if (v->z_pos > z) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   978
				SetAircraftPosition(v, v->x_pos, v->y_pos, v->z_pos-1);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   979
			} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   980
				SetAircraftPosition(v, v->x_pos, v->y_pos, v->z_pos+1);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   981
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   982
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   983
		return false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   984
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   985
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   986
	// Get distance from destination pos to current pos.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   987
	dist = myabs(x + amd->x - v->x_pos) +  myabs(y + amd->y - v->y_pos);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   988
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   989
	// Need exact position?
2364
5ac6c8d6e031 (svn r2890) Fix some signed/unsigned comparison warnings (tokai)
tron
parents: 2316
diff changeset
   990
	if (!(amd->flag & AMED_EXACTPOS) && dist <= (amd->flag & AMED_SLOWTURN ? 8U : 4U))
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   991
		return true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   992
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   993
	// At final pos?
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   994
	if (dist == 0) {
3158
a821c8fec829 (svn r3784) Add a type and functions to handle direction changes
tron
parents: 3157
diff changeset
   995
		DirDiff dirdiff;
a821c8fec829 (svn r3784) Add a type and functions to handle direction changes
tron
parents: 3157
diff changeset
   996
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   997
		if (v->cur_speed > 12) v->cur_speed = 12;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   998
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   999
		// Change direction smoothly to final direction.
3158
a821c8fec829 (svn r3784) Add a type and functions to handle direction changes
tron
parents: 3157
diff changeset
  1000
		dirdiff = DirDifference(amd->direction, v->direction);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
  1001
		// if distance is 0, and plane points in right direction, no point in calling
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1002
		// UpdateAircraftSpeed(). So do it only afterwards
3158
a821c8fec829 (svn r3784) Add a type and functions to handle direction changes
tron
parents: 3157
diff changeset
  1003
		if (dirdiff == DIRDIFF_SAME) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1004
			v->cur_speed = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1005
			return true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1006
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1007
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1008
		if (!UpdateAircraftSpeed(v)) return false;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1009
3158
a821c8fec829 (svn r3784) Add a type and functions to handle direction changes
tron
parents: 3157
diff changeset
  1010
		v->direction = ChangeDir(v->direction, dirdiff > DIRDIFF_REVERSE ? DIRDIFF_45LEFT : DIRDIFF_45RIGHT);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1011
		v->cur_speed >>= 1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1012
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1013
		SetAircraftPosition(v, v->x_pos, v->y_pos, v->z_pos);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1014
		return false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1015
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1016
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1017
	if (!(amd->flag & AMED_NOSPDCLAMP) && v->cur_speed > 12) v->cur_speed = 12;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1018
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1019
	if (!UpdateAircraftSpeed(v)) return false;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1020
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1021
	if (v->load_unload_time_rem != 0) v->load_unload_time_rem--;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1022
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1023
	// Turn. Do it slowly if in the air.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1024
	newdir = GetDirectionTowards(v, x + amd->x, y + amd->y);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1025
	if (newdir != v->direction) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1026
		if (amd->flag & AMED_SLOWTURN) {
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1027
			if (v->load_unload_time_rem == 0) v->load_unload_time_rem = 8;
99
66c0918845af (svn r100) Fix: [ 1012086 ] Drunk pilot (Celestar)
dominik
parents: 84
diff changeset
  1028
			v->direction = newdir;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1029
		} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1030
			v->cur_speed >>= 1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1031
			v->direction = newdir;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1032
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1033
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1034
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1035
	// Move vehicle.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1036
	GetNewVehiclePos(v, &gp);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1037
	v->tile = gp.new_tile;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1038
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1039
	// If vehicle is in the air, use tile coordinate 0.
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1040
	if (amd->flag & (AMED_TAKEOFF | AMED_SLOWTURN | AMED_LAND)) v->tile = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1041
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1042
	// Adjust Z for land or takeoff?
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1043
	z = v->z_pos;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1044
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1045
	if (amd->flag & AMED_TAKEOFF) {
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1046
		z += 2;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1047
		maxz = GetAircraftFlyingAltitude(v);
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1048
		if (z > maxz) z = maxz;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1049
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1050
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1051
	if (amd->flag & AMED_LAND) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1052
		if (st->airport_tile == 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1053
			v->u.air.state = FLYING;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1054
			AircraftNextAirportPos_and_Order(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1055
			// get aircraft back on running altitude
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1056
			SetAircraftPosition(v, gp.x, gp.y, GetAircraftFlyingAltitude(v));
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1057
			return false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1058
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1059
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1060
		curz = GetSlopeZ(x, y) + 1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1061
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1062
		if (curz > z) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1063
			z++;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1064
		} else {
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1065
			int t = max(1, dist - 4);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1066
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1067
			z -= ((z - curz) + t - 1) / t;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1068
			if (z < curz) z = curz;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1069
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1070
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1071
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1072
	// We've landed. Decrase speed when we're reaching end of runway.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1073
	if (amd->flag & AMED_BRAKE) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1074
		curz = GetSlopeZ(x, y) + 1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1075
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1076
		if (z > curz) {
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1077
			z--;
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1078
		} else if (z < curz) {
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1079
			z++;
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1080
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1081
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1082
		if (dist < 64 && v->cur_speed > 12) v->cur_speed -= 4;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1083
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1084
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1085
	SetAircraftPosition(v, gp.x, gp.y, z);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1086
	return false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1087
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1088
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1089
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1090
static void HandleCrashedAircraft(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1091
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1092
	uint32 r;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1093
	Station *st;
899
93b0626ddfce (svn r1385) Fix: [ 1095020 ] When all stations in an aircraft's order list are demolished, the plane eventually crashes (running out of fuel)
dominik
parents: 842
diff changeset
  1094
	int z;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1095
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1096
	v->u.air.crashed_counter++;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1097
919
b0d6c7642f99 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 909
diff changeset
  1098
	st = GetStation(v->u.air.targetairport);
899
93b0626ddfce (svn r1385) Fix: [ 1095020 ] When all stations in an aircraft's order list are demolished, the plane eventually crashes (running out of fuel)
dominik
parents: 842
diff changeset
  1099
93b0626ddfce (svn r1385) Fix: [ 1095020 ] When all stations in an aircraft's order list are demolished, the plane eventually crashes (running out of fuel)
dominik
parents: 842
diff changeset
  1100
	// make aircraft crash down to the ground
1150
9ff458b2e574 (svn r1651) Fix: [ 1098696 ] Airport shadow remains after crashing
dominik
parents: 1139
diff changeset
  1101
	if (v->u.air.crashed_counter < 500 && st->airport_tile==0 && ((v->u.air.crashed_counter % 3) == 0) ) {
9ff458b2e574 (svn r1651) Fix: [ 1098696 ] Airport shadow remains after crashing
dominik
parents: 1139
diff changeset
  1102
		z = GetSlopeZ(v->x_pos, v->y_pos);
899
93b0626ddfce (svn r1385) Fix: [ 1095020 ] When all stations in an aircraft's order list are demolished, the plane eventually crashes (running out of fuel)
dominik
parents: 842
diff changeset
  1103
		v->z_pos -= 1;
1150
9ff458b2e574 (svn r1651) Fix: [ 1098696 ] Airport shadow remains after crashing
dominik
parents: 1139
diff changeset
  1104
		if (v->z_pos == z) {
9ff458b2e574 (svn r1651) Fix: [ 1098696 ] Airport shadow remains after crashing
dominik
parents: 1139
diff changeset
  1105
			v->u.air.crashed_counter = 500;
9ff458b2e574 (svn r1651) Fix: [ 1098696 ] Airport shadow remains after crashing
dominik
parents: 1139
diff changeset
  1106
			v->z_pos++;
9ff458b2e574 (svn r1651) Fix: [ 1098696 ] Airport shadow remains after crashing
dominik
parents: 1139
diff changeset
  1107
		}
899
93b0626ddfce (svn r1385) Fix: [ 1095020 ] When all stations in an aircraft's order list are demolished, the plane eventually crashes (running out of fuel)
dominik
parents: 842
diff changeset
  1108
	}
93b0626ddfce (svn r1385) Fix: [ 1095020 ] When all stations in an aircraft's order list are demolished, the plane eventually crashes (running out of fuel)
dominik
parents: 842
diff changeset
  1109
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1110
	if (v->u.air.crashed_counter < 650) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1111
		if (CHANCE16R(1,32,r)) {
3160
9201d3f304e0 (svn r3786) More work for DirDiff
tron
parents: 3158
diff changeset
  1112
			static const DirDiff delta[] = {
9201d3f304e0 (svn r3786) More work for DirDiff
tron
parents: 3158
diff changeset
  1113
				DIRDIFF_45LEFT, DIRDIFF_SAME, DIRDIFF_SAME, DIRDIFF_45RIGHT
9201d3f304e0 (svn r3786) More work for DirDiff
tron
parents: 3158
diff changeset
  1114
			};
9201d3f304e0 (svn r3786) More work for DirDiff
tron
parents: 3158
diff changeset
  1115
9201d3f304e0 (svn r3786) More work for DirDiff
tron
parents: 3158
diff changeset
  1116
			v->direction = ChangeDir(v->direction, delta[GB(r, 16, 2)]);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1117
			SetAircraftPosition(v, v->x_pos, v->y_pos, v->z_pos);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1118
			r = Random();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1119
			CreateEffectVehicleRel(v,
2140
d708eb80ab8b (svn r2650) Convert many explicit shifts+ands to extract bits to invocations of GB - should be a bit nicer to read
tron
parents: 2133
diff changeset
  1120
				GB(r, 0, 4) + 4,
d708eb80ab8b (svn r2650) Convert many explicit shifts+ands to extract bits to invocations of GB - should be a bit nicer to read
tron
parents: 2133
diff changeset
  1121
				GB(r, 4, 4) + 4,
d708eb80ab8b (svn r2650) Convert many explicit shifts+ands to extract bits to invocations of GB - should be a bit nicer to read
tron
parents: 2133
diff changeset
  1122
				GB(r, 8, 4),
1359
8ba976aed634 (svn r1863) Give the effect vehicle type enums more descriptive names and use the enum as parameter type for CreateEffectVehicle*()
tron
parents: 1299
diff changeset
  1123
				EV_EXPLOSION_SMALL);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1124
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1125
	} else if (v->u.air.crashed_counter >= 10000) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1126
		// remove rubble of crashed airplane
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1127
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1128
		// clear runway-in on all airports, set by crashing plane
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1129
		// small airports use AIRPORT_BUSY, city airports use RUNWAY_IN_OUT_block, etc.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1130
		// but they all share the same number
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1131
		CLRBITS(st->airport_flags, RUNWAY_IN_block);
4059
f1442dfe1c8a (svn r5346) - Feature: Add 4 new airports. 2 for aircraft, 2 for helicopters.
richk
parents: 4056
diff changeset
  1132
		CLRBITS(st->airport_flags, RUNWAY_IN_OUT_block); // commuter airport
f1442dfe1c8a (svn r5346) - Feature: Add 4 new airports. 2 for aircraft, 2 for helicopters.
richk
parents: 4056
diff changeset
  1133
		CLRBITS(st->airport_flags, RUNWAY_IN2_block); // intercontinental
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1134
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1135
		BeginVehicleMove(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1136
		EndVehicleMove(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1137
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1138
		DoDeleteAircraft(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1139
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1140
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1141
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1142
static void HandleBrokenAircraft(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1143
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1144
	if (v->breakdown_ctr != 1) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1145
		v->breakdown_ctr = 1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1146
		v->vehstatus |= VS_AIRCRAFT_BROKEN;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1147
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1148
		if (v->breakdowns_since_last_service != 255)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1149
			v->breakdowns_since_last_service++;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1150
		InvalidateWindow(WC_VEHICLE_VIEW, v->index);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1151
		InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1152
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1153
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1154
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1155
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1156
static void HandleAircraftSmoke(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1157
{
2654
df351c3ddd59 (svn r3196) Use structs instead of magic offsets into arrays
tron
parents: 2639
diff changeset
  1158
	static const struct {
df351c3ddd59 (svn r3196) Use structs instead of magic offsets into arrays
tron
parents: 2639
diff changeset
  1159
		int8 x;
df351c3ddd59 (svn r3196) Use structs instead of magic offsets into arrays
tron
parents: 2639
diff changeset
  1160
		int8 y;
df351c3ddd59 (svn r3196) Use structs instead of magic offsets into arrays
tron
parents: 2639
diff changeset
  1161
	} smoke_pos[] = {
df351c3ddd59 (svn r3196) Use structs instead of magic offsets into arrays
tron
parents: 2639
diff changeset
  1162
		{  5,  5 },
df351c3ddd59 (svn r3196) Use structs instead of magic offsets into arrays
tron
parents: 2639
diff changeset
  1163
		{  6,  0 },
df351c3ddd59 (svn r3196) Use structs instead of magic offsets into arrays
tron
parents: 2639
diff changeset
  1164
		{  5, -5 },
df351c3ddd59 (svn r3196) Use structs instead of magic offsets into arrays
tron
parents: 2639
diff changeset
  1165
		{  0, -6 },
df351c3ddd59 (svn r3196) Use structs instead of magic offsets into arrays
tron
parents: 2639
diff changeset
  1166
		{ -5, -5 },
df351c3ddd59 (svn r3196) Use structs instead of magic offsets into arrays
tron
parents: 2639
diff changeset
  1167
		{ -6,  0 },
df351c3ddd59 (svn r3196) Use structs instead of magic offsets into arrays
tron
parents: 2639
diff changeset
  1168
		{ -5,  5 },
df351c3ddd59 (svn r3196) Use structs instead of magic offsets into arrays
tron
parents: 2639
diff changeset
  1169
		{  0,  6 }
df351c3ddd59 (svn r3196) Use structs instead of magic offsets into arrays
tron
parents: 2639
diff changeset
  1170
	};
df351c3ddd59 (svn r3196) Use structs instead of magic offsets into arrays
tron
parents: 2639
diff changeset
  1171
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1172
	if (!(v->vehstatus & VS_AIRCRAFT_BROKEN)) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1173
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1174
	if (v->cur_speed < 10) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1175
		v->vehstatus &= ~VS_AIRCRAFT_BROKEN;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1176
		v->breakdown_ctr = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1177
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1178
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1179
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1180
	if ((v->tick_counter & 0x1F) == 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1181
		CreateEffectVehicleRel(v,
2654
df351c3ddd59 (svn r3196) Use structs instead of magic offsets into arrays
tron
parents: 2639
diff changeset
  1182
			smoke_pos[v->direction].x,
df351c3ddd59 (svn r3196) Use structs instead of magic offsets into arrays
tron
parents: 2639
diff changeset
  1183
			smoke_pos[v->direction].y,
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1184
			2,
1359
8ba976aed634 (svn r1863) Give the effect vehicle type enums more descriptive names and use the enum as parameter type for CreateEffectVehicle*()
tron
parents: 1299
diff changeset
  1185
			EV_SMOKE
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1186
		);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1187
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1188
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1189
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1190
static void ProcessAircraftOrder(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1191
{
1043
123072ba6ced (svn r1544) -Fix: SwapOrder did not use AssignOrder, which caused the saveroutine to
truelight
parents: 1035
diff changeset
  1192
	const Order *order;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1193
3005
db4007baa0ab (svn r3585) Similar changes to the order handlers of the other vehicle types like r3584
tron
parents: 2989
diff changeset
  1194
	switch (v->current_order.type) {
db4007baa0ab (svn r3585) Similar changes to the order handlers of the other vehicle types like r3584
tron
parents: 2989
diff changeset
  1195
		case OT_GOTO_DEPOT:
db4007baa0ab (svn r3585) Similar changes to the order handlers of the other vehicle types like r3584
tron
parents: 2989
diff changeset
  1196
			if (!(v->current_order.flags & OF_PART_OF_ORDERS)) return;
db4007baa0ab (svn r3585) Similar changes to the order handlers of the other vehicle types like r3584
tron
parents: 2989
diff changeset
  1197
			if (v->current_order.flags & OF_SERVICE_IF_NEEDED &&
db4007baa0ab (svn r3585) Similar changes to the order handlers of the other vehicle types like r3584
tron
parents: 2989
diff changeset
  1198
					!VehicleNeedsService(v)) {
db4007baa0ab (svn r3585) Similar changes to the order handlers of the other vehicle types like r3584
tron
parents: 2989
diff changeset
  1199
				v->cur_order_index++;
db4007baa0ab (svn r3585) Similar changes to the order handlers of the other vehicle types like r3584
tron
parents: 2989
diff changeset
  1200
			}
db4007baa0ab (svn r3585) Similar changes to the order handlers of the other vehicle types like r3584
tron
parents: 2989
diff changeset
  1201
			break;
db4007baa0ab (svn r3585) Similar changes to the order handlers of the other vehicle types like r3584
tron
parents: 2989
diff changeset
  1202
db4007baa0ab (svn r3585) Similar changes to the order handlers of the other vehicle types like r3584
tron
parents: 2989
diff changeset
  1203
		case OT_LOADING: return;
4351
63ae31104f07 (svn r6052) -Codechange: change OrderType (order->type) in a typedef
truelight
parents: 4346
diff changeset
  1204
63ae31104f07 (svn r6052) -Codechange: change OrderType (order->type) in a typedef
truelight
parents: 4346
diff changeset
  1205
		default: break;
1151
614cbcb5b9fe (svn r1652) Fix: train and road vehicle lists are now redrawn when vehicles enters/leaves a depot (Hackykid)
bjarni
parents: 1150
diff changeset
  1206
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
  1207
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1208
	if (v->cur_order_index >= v->num_orders) v->cur_order_index = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1209
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1210
	order = GetVehicleOrder(v, v->cur_order_index);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1211
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1212
	if (order == NULL) {
555
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1213
		v->current_order.type = OT_NOTHING;
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1214
		v->current_order.flags = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1215
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1216
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1217
3005
db4007baa0ab (svn r3585) Similar changes to the order handlers of the other vehicle types like r3584
tron
parents: 2989
diff changeset
  1218
	if (order->type == OT_DUMMY && !CheckForValidOrders(v)) CrashAirplane(v);
901
c92d5da1acb3 (svn r1387) Fix: Airplanes now also crash when only one invalid order is in the schedule
dominik
parents: 899
diff changeset
  1219
4527
b18634a31a4a (svn r6353) -Codechange: Make DestinationID a typedef of uin16, which is as large as any type of destinataion (StationID, DepotID, WaypointID) it can hold
tron
parents: 4526
diff changeset
  1220
	if (order->type  == v->current_order.type  &&
b18634a31a4a (svn r6353) -Codechange: Make DestinationID a typedef of uin16, which is as large as any type of destinataion (StationID, DepotID, WaypointID) it can hold
tron
parents: 4526
diff changeset
  1221
			order->flags == v->current_order.flags &&
b18634a31a4a (svn r6353) -Codechange: Make DestinationID a typedef of uin16, which is as large as any type of destinataion (StationID, DepotID, WaypointID) it can hold
tron
parents: 4526
diff changeset
  1222
			order->dest  == v->current_order.dest)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1223
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1224
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1225
	v->current_order = *order;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1226
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1227
	// orders are changed in flight, ensure going to the right station
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1228
	if (order->type == OT_GOTO_STATION && v->u.air.state == FLYING) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1229
		AircraftNextAirportPos_and_Order(v);
4527
b18634a31a4a (svn r6353) -Codechange: Make DestinationID a typedef of uin16, which is as large as any type of destinataion (StationID, DepotID, WaypointID) it can hold
tron
parents: 4526
diff changeset
  1230
		v->u.air.targetairport = order->dest;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1231
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1232
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1233
	InvalidateVehicleOrder(v);
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1234
1055
3aae1d6f12aa (svn r1556) -Fix: Vehicle list updates should now really work
Celestar
parents: 1053
diff changeset
  1235
	InvalidateWindowClasses(WC_AIRCRAFT_LIST);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1236
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1237
3722
00921cc83d70 (svn r4693) - NewGRF: invalidate aircraft in viewports during loading stages so that graphic changes are visible. (mart3p)
peter1138
parents: 3710
diff changeset
  1238
/** Mark all views dirty for an aircraft.
00921cc83d70 (svn r4693) - NewGRF: invalidate aircraft in viewports during loading stages so that graphic changes are visible. (mart3p)
peter1138
parents: 3710
diff changeset
  1239
 * @param v vehicle to be redrawn.
00921cc83d70 (svn r4693) - NewGRF: invalidate aircraft in viewports during loading stages so that graphic changes are visible. (mart3p)
peter1138
parents: 3710
diff changeset
  1240
 */
00921cc83d70 (svn r4693) - NewGRF: invalidate aircraft in viewports during loading stages so that graphic changes are visible. (mart3p)
peter1138
parents: 3710
diff changeset
  1241
static void MarkAircraftDirty(Vehicle *v)
00921cc83d70 (svn r4693) - NewGRF: invalidate aircraft in viewports during loading stages so that graphic changes are visible. (mart3p)
peter1138
parents: 3710
diff changeset
  1242
{
00921cc83d70 (svn r4693) - NewGRF: invalidate aircraft in viewports during loading stages so that graphic changes are visible. (mart3p)
peter1138
parents: 3710
diff changeset
  1243
		v->cur_image = GetAircraftImage(v, v->direction);
00921cc83d70 (svn r4693) - NewGRF: invalidate aircraft in viewports during loading stages so that graphic changes are visible. (mart3p)
peter1138
parents: 3710
diff changeset
  1244
		if (v->subtype == 0) {
00921cc83d70 (svn r4693) - NewGRF: invalidate aircraft in viewports during loading stages so that graphic changes are visible. (mart3p)
peter1138
parents: 3710
diff changeset
  1245
			v->next->next->cur_image = GetRotorImage(v);
00921cc83d70 (svn r4693) - NewGRF: invalidate aircraft in viewports during loading stages so that graphic changes are visible. (mart3p)
peter1138
parents: 3710
diff changeset
  1246
		}
00921cc83d70 (svn r4693) - NewGRF: invalidate aircraft in viewports during loading stages so that graphic changes are visible. (mart3p)
peter1138
parents: 3710
diff changeset
  1247
		MarkAllViewportsDirty(v->left_coord, v->top_coord, v->right_coord + 1, v->bottom_coord + 1);
00921cc83d70 (svn r4693) - NewGRF: invalidate aircraft in viewports during loading stages so that graphic changes are visible. (mart3p)
peter1138
parents: 3710
diff changeset
  1248
}
00921cc83d70 (svn r4693) - NewGRF: invalidate aircraft in viewports during loading stages so that graphic changes are visible. (mart3p)
peter1138
parents: 3710
diff changeset
  1249
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1250
static void HandleAircraftLoading(Vehicle *v, int mode)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1251
{
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1252
	if (v->current_order.type == OT_NOTHING) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1253
555
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1254
	if (v->current_order.type != OT_DUMMY) {
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1255
		if (v->current_order.type != OT_LOADING) return;
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1256
		if (mode != 0) return;
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1257
		if (--v->load_unload_time_rem != 0) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1258
555
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1259
		if (v->current_order.flags & OF_FULL_LOAD && CanFillVehicle(v)) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1260
			SET_EXPENSES_TYPE(EXPENSES_AIRCRAFT_INC);
3722
00921cc83d70 (svn r4693) - NewGRF: invalidate aircraft in viewports during loading stages so that graphic changes are visible. (mart3p)
peter1138
parents: 3710
diff changeset
  1261
			if (LoadUnloadVehicle(v)) {
00921cc83d70 (svn r4693) - NewGRF: invalidate aircraft in viewports during loading stages so that graphic changes are visible. (mart3p)
peter1138
parents: 3710
diff changeset
  1262
				InvalidateWindow(WC_AIRCRAFT_LIST, v->owner);
00921cc83d70 (svn r4693) - NewGRF: invalidate aircraft in viewports during loading stages so that graphic changes are visible. (mart3p)
peter1138
parents: 3710
diff changeset
  1263
				MarkAircraftDirty(v);
00921cc83d70 (svn r4693) - NewGRF: invalidate aircraft in viewports during loading stages so that graphic changes are visible. (mart3p)
peter1138
parents: 3710
diff changeset
  1264
			}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1265
			return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1266
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1267
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1268
		{
555
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1269
			Order b = v->current_order;
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1270
			v->current_order.type = OT_NOTHING;
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1271
			v->current_order.flags = 0;
3722
00921cc83d70 (svn r4693) - NewGRF: invalidate aircraft in viewports during loading stages so that graphic changes are visible. (mart3p)
peter1138
parents: 3710
diff changeset
  1272
			MarkAircraftDirty(v);
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1273
			if (!(b.flags & OF_NON_STOP)) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1274
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1275
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1276
	v->cur_order_index++;
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1277
	InvalidateVehicleOrder(v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1278
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1279
899
93b0626ddfce (svn r1385) Fix: [ 1095020 ] When all stations in an aircraft's order list are demolished, the plane eventually crashes (running out of fuel)
dominik
parents: 842
diff changeset
  1280
static void CrashAirplane(Vehicle *v)
93b0626ddfce (svn r1385) Fix: [ 1095020 ] When all stations in an aircraft's order list are demolished, the plane eventually crashes (running out of fuel)
dominik
parents: 842
diff changeset
  1281
{
93b0626ddfce (svn r1385) Fix: [ 1095020 ] When all stations in an aircraft's order list are demolished, the plane eventually crashes (running out of fuel)
dominik
parents: 842
diff changeset
  1282
	uint16 amt;
93b0626ddfce (svn r1385) Fix: [ 1095020 ] When all stations in an aircraft's order list are demolished, the plane eventually crashes (running out of fuel)
dominik
parents: 842
diff changeset
  1283
	Station *st;
93b0626ddfce (svn r1385) Fix: [ 1095020 ] When all stations in an aircraft's order list are demolished, the plane eventually crashes (running out of fuel)
dominik
parents: 842
diff changeset
  1284
	StringID newsitem;
93b0626ddfce (svn r1385) Fix: [ 1095020 ] When all stations in an aircraft's order list are demolished, the plane eventually crashes (running out of fuel)
dominik
parents: 842
diff changeset
  1285
93b0626ddfce (svn r1385) Fix: [ 1095020 ] When all stations in an aircraft's order list are demolished, the plane eventually crashes (running out of fuel)
dominik
parents: 842
diff changeset
  1286
	v->vehstatus |= VS_CRASHED;
93b0626ddfce (svn r1385) Fix: [ 1095020 ] When all stations in an aircraft's order list are demolished, the plane eventually crashes (running out of fuel)
dominik
parents: 842
diff changeset
  1287
	v->u.air.crashed_counter = 0;
93b0626ddfce (svn r1385) Fix: [ 1095020 ] When all stations in an aircraft's order list are demolished, the plane eventually crashes (running out of fuel)
dominik
parents: 842
diff changeset
  1288
1359
8ba976aed634 (svn r1863) Give the effect vehicle type enums more descriptive names and use the enum as parameter type for CreateEffectVehicle*()
tron
parents: 1299
diff changeset
  1289
	CreateEffectVehicleRel(v, 4, 4, 8, EV_EXPLOSION_LARGE);
899
93b0626ddfce (svn r1385) Fix: [ 1095020 ] When all stations in an aircraft's order list are demolished, the plane eventually crashes (running out of fuel)
dominik
parents: 842
diff changeset
  1290
93b0626ddfce (svn r1385) Fix: [ 1095020 ] When all stations in an aircraft's order list are demolished, the plane eventually crashes (running out of fuel)
dominik
parents: 842
diff changeset
  1291
	InvalidateWindow(WC_VEHICLE_VIEW, v->index);
93b0626ddfce (svn r1385) Fix: [ 1095020 ] When all stations in an aircraft's order list are demolished, the plane eventually crashes (running out of fuel)
dominik
parents: 842
diff changeset
  1292
93b0626ddfce (svn r1385) Fix: [ 1095020 ] When all stations in an aircraft's order list are demolished, the plane eventually crashes (running out of fuel)
dominik
parents: 842
diff changeset
  1293
	amt = 2;
93b0626ddfce (svn r1385) Fix: [ 1095020 ] When all stations in an aircraft's order list are demolished, the plane eventually crashes (running out of fuel)
dominik
parents: 842
diff changeset
  1294
	if (v->cargo_type == CT_PASSENGERS) amt += v->cargo_count;
93b0626ddfce (svn r1385) Fix: [ 1095020 ] When all stations in an aircraft's order list are demolished, the plane eventually crashes (running out of fuel)
dominik
parents: 842
diff changeset
  1295
	SetDParam(0, amt);
93b0626ddfce (svn r1385) Fix: [ 1095020 ] When all stations in an aircraft's order list are demolished, the plane eventually crashes (running out of fuel)
dominik
parents: 842
diff changeset
  1296
93b0626ddfce (svn r1385) Fix: [ 1095020 ] When all stations in an aircraft's order list are demolished, the plane eventually crashes (running out of fuel)
dominik
parents: 842
diff changeset
  1297
	v->cargo_count = 0;
93b0626ddfce (svn r1385) Fix: [ 1095020 ] When all stations in an aircraft's order list are demolished, the plane eventually crashes (running out of fuel)
dominik
parents: 842
diff changeset
  1298
	v->next->cargo_count = 0,
919
b0d6c7642f99 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 909
diff changeset
  1299
	st = GetStation(v->u.air.targetairport);
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1300
	if (st->airport_tile == 0) {
899
93b0626ddfce (svn r1385) Fix: [ 1095020 ] When all stations in an aircraft's order list are demolished, the plane eventually crashes (running out of fuel)
dominik
parents: 842
diff changeset
  1301
		newsitem = STR_PLANE_CRASH_OUT_OF_FUEL;
93b0626ddfce (svn r1385) Fix: [ 1095020 ] When all stations in an aircraft's order list are demolished, the plane eventually crashes (running out of fuel)
dominik
parents: 842
diff changeset
  1302
	} else {
93b0626ddfce (svn r1385) Fix: [ 1095020 ] When all stations in an aircraft's order list are demolished, the plane eventually crashes (running out of fuel)
dominik
parents: 842
diff changeset
  1303
		SetDParam(1, st->index);
93b0626ddfce (svn r1385) Fix: [ 1095020 ] When all stations in an aircraft's order list are demolished, the plane eventually crashes (running out of fuel)
dominik
parents: 842
diff changeset
  1304
		newsitem = STR_A034_PLANE_CRASH_DIE_IN_FIREBALL;
93b0626ddfce (svn r1385) Fix: [ 1095020 ] When all stations in an aircraft's order list are demolished, the plane eventually crashes (running out of fuel)
dominik
parents: 842
diff changeset
  1305
	}
93b0626ddfce (svn r1385) Fix: [ 1095020 ] When all stations in an aircraft's order list are demolished, the plane eventually crashes (running out of fuel)
dominik
parents: 842
diff changeset
  1306
93b0626ddfce (svn r1385) Fix: [ 1095020 ] When all stations in an aircraft's order list are demolished, the plane eventually crashes (running out of fuel)
dominik
parents: 842
diff changeset
  1307
	SetDParam(1, st->index);
93b0626ddfce (svn r1385) Fix: [ 1095020 ] When all stations in an aircraft's order list are demolished, the plane eventually crashes (running out of fuel)
dominik
parents: 842
diff changeset
  1308
	AddNewsItem(newsitem,
93b0626ddfce (svn r1385) Fix: [ 1095020 ] When all stations in an aircraft's order list are demolished, the plane eventually crashes (running out of fuel)
dominik
parents: 842
diff changeset
  1309
		NEWS_FLAGS(NM_THIN, NF_VIEWPORT|NF_VEHICLE, NT_ACCIDENT, 0),
93b0626ddfce (svn r1385) Fix: [ 1095020 ] When all stations in an aircraft's order list are demolished, the plane eventually crashes (running out of fuel)
dominik
parents: 842
diff changeset
  1310
		v->index,
93b0626ddfce (svn r1385) Fix: [ 1095020 ] When all stations in an aircraft's order list are demolished, the plane eventually crashes (running out of fuel)
dominik
parents: 842
diff changeset
  1311
		0);
93b0626ddfce (svn r1385) Fix: [ 1095020 ] When all stations in an aircraft's order list are demolished, the plane eventually crashes (running out of fuel)
dominik
parents: 842
diff changeset
  1312
93b0626ddfce (svn r1385) Fix: [ 1095020 ] When all stations in an aircraft's order list are demolished, the plane eventually crashes (running out of fuel)
dominik
parents: 842
diff changeset
  1313
	SndPlayVehicleFx(SND_12_EXPLOSION, v);
93b0626ddfce (svn r1385) Fix: [ 1095020 ] When all stations in an aircraft's order list are demolished, the plane eventually crashes (running out of fuel)
dominik
parents: 842
diff changeset
  1314
}
93b0626ddfce (svn r1385) Fix: [ 1095020 ] When all stations in an aircraft's order list are demolished, the plane eventually crashes (running out of fuel)
dominik
parents: 842
diff changeset
  1315
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1316
static void MaybeCrashAirplane(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1317
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1318
	Station *st;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1319
	uint16 prob;
2549
f1d3b383d557 (svn r3078) Some more stuff, which piled up:
tron
parents: 2517
diff changeset
  1320
	uint i;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1321
919
b0d6c7642f99 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 909
diff changeset
  1322
	st = GetStation(v->u.air.targetairport);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1323
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1324
	//FIXME -- MaybeCrashAirplane -> increase crashing chances of very modern airplanes on smaller than AT_METROPOLITAN airports
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1325
	prob = 0x10000 / 1500;
4059
f1442dfe1c8a (svn r5346) - Feature: Add 4 new airports. 2 for aircraft, 2 for helicopters.
richk
parents: 4056
diff changeset
  1326
	if (((st->airport_type == AT_SMALL) || (st->airport_type == AT_COMMUTER)) && (AircraftVehInfo(v->engine_type)->subtype & AIR_FAST) && !_cheats.no_jetcrash.value) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1327
		prob = 0x10000 / 20;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1328
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1329
2484
8e0c88a833fb (svn r3010) Get rid of quite some dubious casts, either by using GB(), proper types or just removing them
tron
parents: 2477
diff changeset
  1330
	if (GB(Random(), 0, 16) > prob) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1331
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1332
	// Crash the airplane. Remove all goods stored at the station.
2549
f1d3b383d557 (svn r3078) Some more stuff, which piled up:
tron
parents: 2517
diff changeset
  1333
	for (i = 0; i != NUM_CARGO; i++) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1334
		st->goods[i].rating = 1;
2504
7c27fc230cda (svn r3030) More work for GB/SB, this time concerning the waiting_acceptance attribute of stations
tron
parents: 2484
diff changeset
  1335
		SB(st->goods[i].waiting_acceptance, 0, 12, 0);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1336
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1337
899
93b0626ddfce (svn r1385) Fix: [ 1095020 ] When all stations in an aircraft's order list are demolished, the plane eventually crashes (running out of fuel)
dominik
parents: 842
diff changeset
  1338
	CrashAirplane(v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1339
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1340
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1341
// we've landed and just arrived at a terminal
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1342
static void AircraftEntersTerminal(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1343
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1344
	Station *st;
555
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1345
	Order old_order;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1346
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1347
	if (v->current_order.type == OT_GOTO_DEPOT) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1348
919
b0d6c7642f99 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 909
diff changeset
  1349
	st = GetStation(v->u.air.targetairport);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1350
	v->last_station_visited = v->u.air.targetairport;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1351
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1352
	/* Check if station was ever visited before */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1353
	if (!(st->had_vehicle_of_type & HVOT_AIRCRAFT)) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1354
		uint32 flags;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1355
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1356
		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
  1357
		SetDParam(0, st->index);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1358
		// show newsitem of celebrating citizens
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1359
		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);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1360
		AddNewsItem(
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1361
			STR_A033_CITIZENS_CELEBRATE_FIRST,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1362
			flags,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1363
			v->index,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1364
			0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1365
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1366
555
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1367
	old_order = v->current_order;
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1368
	v->current_order.type = OT_LOADING;
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1369
	v->current_order.flags = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1370
555
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1371
	if (old_order.type == OT_GOTO_STATION &&
4527
b18634a31a4a (svn r6353) -Codechange: Make DestinationID a typedef of uin16, which is as large as any type of destinataion (StationID, DepotID, WaypointID) it can hold
tron
parents: 4526
diff changeset
  1372
			v->current_order.dest == v->last_station_visited) {
555
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1373
		v->current_order.flags =
3872
2c1a79141c34 (svn r4914) -Fix (FS#180) Aircraft can now serve as feeders
celestar
parents: 3870
diff changeset
  1374
			(old_order.flags & (OF_FULL_LOAD | OF_UNLOAD | OF_TRANSFER)) | OF_NON_STOP;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1375
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1376
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1377
	SET_EXPENSES_TYPE(EXPENSES_AIRCRAFT_INC);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1378
	LoadUnloadVehicle(v);
3722
00921cc83d70 (svn r4693) - NewGRF: invalidate aircraft in viewports during loading stages so that graphic changes are visible. (mart3p)
peter1138
parents: 3710
diff changeset
  1379
	MarkAircraftDirty(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
  1380
	InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
1055
3aae1d6f12aa (svn r1556) -Fix: Vehicle list updates should now really work
Celestar
parents: 1053
diff changeset
  1381
	InvalidateWindowClasses(WC_AIRCRAFT_LIST);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1382
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1383
1064
1b6564cec6be (svn r1565) -Fix: [ 1104969 ] Aircraft in hangar messages are now revalidated before
celestar
parents: 1055
diff changeset
  1384
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1385
static void AircraftEnterHangar(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1386
{
555
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1387
	Order old_order;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1388
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1389
	ServiceAircraft(v);
1055
3aae1d6f12aa (svn r1556) -Fix: Vehicle list updates should now really work
Celestar
parents: 1053
diff changeset
  1390
	InvalidateWindowClasses(WC_AIRCRAFT_LIST);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
  1391
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
  1392
	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
  1393
555
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1394
	if (v->current_order.type == OT_GOTO_DEPOT) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1395
		InvalidateWindow(WC_VEHICLE_VIEW, v->index);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1396
555
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1397
		old_order = v->current_order;
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1398
		v->current_order.type = OT_NOTHING;
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1399
		v->current_order.flags = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1400
1530
caa16c506a22 (svn r2034) -Codechange: Replaced the rather meaningless LOAD and UNLOAD flags for depots by something more meaningful
celestar
parents: 1520
diff changeset
  1401
		if (HASBIT(old_order.flags, OFB_PART_OF_ORDERS)) {
555
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1402
			v->cur_order_index++;
1530
caa16c506a22 (svn r2034) -Codechange: Replaced the rather meaningless LOAD and UNLOAD flags for depots by something more meaningful
celestar
parents: 1520
diff changeset
  1403
		} else if (HASBIT(old_order.flags, OFB_HALT_IN_DEPOT)) { // force depot visit
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1404
			v->vehstatus |= VS_STOPPED;
1055
3aae1d6f12aa (svn r1556) -Fix: Vehicle list updates should now really work
Celestar
parents: 1053
diff changeset
  1405
			InvalidateWindowClasses(WC_AIRCRAFT_LIST);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1406
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1407
			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
  1408
				SetDParam(0, v->unitnumber);
3140
69cb681c6d86 (svn r3758) Remove the news validation callback. It is superseded by r3757.
tron
parents: 3139
diff changeset
  1409
				AddNewsItem(
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1410
					STR_A014_AIRCRAFT_IS_WAITING_IN,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1411
					NEWS_FLAGS(NM_SMALL, NF_VIEWPORT|NF_VEHICLE, NT_ADVICE, 0),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1412
					v->index,
3140
69cb681c6d86 (svn r3758) Remove the news validation callback. It is superseded by r3757.
tron
parents: 3139
diff changeset
  1413
					0
69cb681c6d86 (svn r3758) Remove the news validation callback. It is superseded by r3757.
tron
parents: 3139
diff changeset
  1414
				);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1415
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1416
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1417
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1418
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1419
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1420
static void AircraftLand(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1421
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1422
	v->sprite_width = v->sprite_height = 2;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1423
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1424
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1425
static void AircraftLandAirplane(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1426
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1427
	AircraftLand(v);
541
e1cd34389f79 (svn r925) Use sound enums
tron
parents: 538
diff changeset
  1428
	SndPlayVehicleFx(SND_17_SKID_PLANE, v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1429
	MaybeCrashAirplane(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1430
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1431
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1432
// set the right pos when heading to other airports after takeoff
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1433
static void AircraftNextAirportPos_and_Order(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1434
{
2630
35249d2ded3e (svn r3172) static, const
tron
parents: 2618
diff changeset
  1435
	const Station* st;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1436
	const AirportFTAClass *Airport;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1437
555
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1438
	if (v->current_order.type == OT_GOTO_STATION ||
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1439
			v->current_order.type == OT_GOTO_DEPOT)
4527
b18634a31a4a (svn r6353) -Codechange: Make DestinationID a typedef of uin16, which is as large as any type of destinataion (StationID, DepotID, WaypointID) it can hold
tron
parents: 4526
diff changeset
  1440
		v->u.air.targetairport = v->current_order.dest;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1441
919
b0d6c7642f99 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 909
diff changeset
  1442
	st = GetStation(v->u.air.targetairport);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1443
	Airport = GetAirport(st->airport_type);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1444
	v->u.air.pos = v->u.air.previous_pos = Airport->entry_point;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1445
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1446
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1447
static void AircraftLeaveHangar(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1448
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1449
	v->cur_speed = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1450
	v->subspeed = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1451
	v->progress = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1452
	v->direction = 3;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1453
	v->vehstatus &= ~VS_HIDDEN;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1454
	{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1455
		Vehicle *u = v->next;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1456
		u->vehstatus &= ~VS_HIDDEN;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1457
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1458
		// Rotor blades
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1459
		u = u->next;
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1460
		if (u != NULL) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1461
			u->vehstatus &= ~VS_HIDDEN;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1462
			u->cur_speed = 80;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1463
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1464
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1465
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
  1466
	VehicleServiceInDepot(v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1467
	SetAircraftPosition(v, v->x_pos, v->y_pos, v->z_pos);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1468
	InvalidateWindow(WC_VEHICLE_DEPOT, v->tile);
1055
3aae1d6f12aa (svn r1556) -Fix: Vehicle list updates should now really work
Celestar
parents: 1053
diff changeset
  1469
	InvalidateWindowClasses(WC_AIRCRAFT_LIST);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1470
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1471
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1472
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1473
////////////////////////////////////////////////////////////////////////////////
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1474
///////////////////   AIRCRAFT MOVEMENT SCHEME  ////////////////////////////////
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1475
////////////////////////////////////////////////////////////////////////////////
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1476
static void AircraftEventHandler_EnterTerminal(Vehicle *v, const AirportFTAClass *Airport)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1477
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1478
	AircraftEntersTerminal(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1479
	v->u.air.state = Airport->layout[v->u.air.pos].heading;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1480
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1481
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1482
static void AircraftEventHandler_EnterHangar(Vehicle *v, const AirportFTAClass *Airport)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1483
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1484
	AircraftEnterHangar(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1485
	v->u.air.state = Airport->layout[v->u.air.pos].heading;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1486
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1487
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1488
// In an Airport Hangar
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1489
static void AircraftEventHandler_InHangar(Vehicle *v, const AirportFTAClass *Airport)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1490
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1491
	// if we just arrived, execute EnterHangar first
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1492
	if (v->u.air.previous_pos != v->u.air.pos) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1493
		AircraftEventHandler_EnterHangar(v, Airport);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1494
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1495
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1496
555
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1497
	// if we were sent to the depot, stay there
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1498
	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
  1499
		v->current_order.type = OT_NOTHING;
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1500
		v->current_order.flags = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1501
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1502
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1503
555
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1504
	if (v->current_order.type != OT_GOTO_STATION &&
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1505
			v->current_order.type != OT_GOTO_DEPOT)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1506
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1507
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1508
	// if the block of the next position is busy, stay put
2549
f1d3b383d557 (svn r3078) Some more stuff, which piled up:
tron
parents: 2517
diff changeset
  1509
	if (AirportHasBlock(v, &Airport->layout[v->u.air.pos], Airport)) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1510
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1511
	// We are already at the target airport, we need to find a terminal
4527
b18634a31a4a (svn r6353) -Codechange: Make DestinationID a typedef of uin16, which is as large as any type of destinataion (StationID, DepotID, WaypointID) it can hold
tron
parents: 4526
diff changeset
  1512
	if (v->current_order.dest == v->u.air.targetairport) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1513
		// FindFreeTerminal:
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1514
		// 1. Find a free terminal, 2. Occupy it, 3. Set the vehicle's state to that terminal
2549
f1d3b383d557 (svn r3078) Some more stuff, which piled up:
tron
parents: 2517
diff changeset
  1515
		if (v->subtype != 0) {
f1d3b383d557 (svn r3078) Some more stuff, which piled up:
tron
parents: 2517
diff changeset
  1516
			if (!AirportFindFreeTerminal(v, Airport)) return; // airplane
f1d3b383d557 (svn r3078) Some more stuff, which piled up:
tron
parents: 2517
diff changeset
  1517
		} else {
f1d3b383d557 (svn r3078) Some more stuff, which piled up:
tron
parents: 2517
diff changeset
  1518
			if (!AirportFindFreeHelipad(v, Airport)) return; // helicopter
f1d3b383d557 (svn r3078) Some more stuff, which piled up:
tron
parents: 2517
diff changeset
  1519
		}
f1d3b383d557 (svn r3078) Some more stuff, which piled up:
tron
parents: 2517
diff changeset
  1520
	} else { // Else prepare for launch.
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1521
		// airplane goto state takeoff, helicopter to helitakeoff
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1522
		v->u.air.state = (v->subtype != 0) ? TAKEOFF : HELITAKEOFF;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1523
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1524
	AircraftLeaveHangar(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1525
	AirportMove(v, Airport);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1526
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1527
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1528
// At one of the Airport's Terminals
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1529
static void AircraftEventHandler_AtTerminal(Vehicle *v, const AirportFTAClass *Airport)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1530
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1531
	// if we just arrived, execute EnterTerminal first
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1532
	if (v->u.air.previous_pos != v->u.air.pos) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1533
		AircraftEventHandler_EnterTerminal(v, Airport);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1534
		// on an airport with helipads, a helicopter will always land there
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1535
		// and get serviced at the same time - patch setting
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1536
		if (_patches.serviceathelipad) {
950
1f6546ea35a3 (svn r1440) -Feature: Allows more flexible airport layouts now, as the number of
celestar
parents: 926
diff changeset
  1537
			if (v->subtype == 0 && Airport->helipads != NULL) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1538
				// an exerpt of ServiceAircraft, without the invisibility stuff
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1539
				v->date_of_last_service = _date;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1540
				v->breakdowns_since_last_service = 0;
1926
68d60188a22f (svn r2432) Use GetEngine() instead of DEREF_ENGINE() or even _engines[]
tron
parents: 1901
diff changeset
  1541
				v->reliability = GetEngine(v->engine_type)->reliability;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1542
				InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1543
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1544
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1545
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1546
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1547
555
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1548
	if (v->current_order.type == OT_NOTHING) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1549
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1550
	// if the block of the next position is busy, stay put
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1551
	if (AirportHasBlock(v, &Airport->layout[v->u.air.pos], Airport)) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1552
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1553
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1554
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1555
	// airport-road is free. We either have to go to another airport, or to the hangar
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1556
	// ---> start moving
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1557
555
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1558
	switch (v->current_order.type) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1559
		case OT_GOTO_STATION: // ready to fly to another airport
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1560
			// airplane goto state takeoff, helicopter to helitakeoff
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1561
			v->u.air.state = (v->subtype != 0) ? TAKEOFF : HELITAKEOFF;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1562
			break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1563
		case OT_GOTO_DEPOT:   // visit hangar for serivicing, sale, etc.
4527
b18634a31a4a (svn r6353) -Codechange: Make DestinationID a typedef of uin16, which is as large as any type of destinataion (StationID, DepotID, WaypointID) it can hold
tron
parents: 4526
diff changeset
  1564
			if (v->current_order.dest == v->u.air.targetairport) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1565
				v->u.air.state = HANGAR;
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1566
			} else {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1567
				v->u.air.state = (v->subtype != 0) ? TAKEOFF : HELITAKEOFF;
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1568
			}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1569
			break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1570
		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
  1571
			v->current_order.type = OT_NOTHING;
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1572
			v->current_order.flags = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1573
			v->u.air.state = HANGAR;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1574
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1575
	AirportMove(v, Airport);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1576
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1577
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1578
static void AircraftEventHandler_General(Vehicle *v, const AirportFTAClass *Airport)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1579
{
70
60216d94f7c1 (svn r71) -Cheat: [976127] no extra frequent jet crash on small airports (truesatan)
darkvater
parents: 19
diff changeset
  1580
	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
  1581
	assert(0);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1582
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1583
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1584
static void AircraftEventHandler_TakeOff(Vehicle *v, const AirportFTAClass *Airport) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1585
	PlayAircraftSound(v); // play takeoffsound for airplanes
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1586
	v->u.air.state = STARTTAKEOFF;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1587
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1588
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1589
static void AircraftEventHandler_StartTakeOff(Vehicle *v, const AirportFTAClass *Airport)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1590
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1591
	v->sprite_width = v->sprite_height = 24; // ??? no idea what this is
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1592
	v->u.air.state = ENDTAKEOFF;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1593
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1594
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1595
static void AircraftEventHandler_EndTakeOff(Vehicle *v, const AirportFTAClass *Airport)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1596
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1597
	v->u.air.state = FLYING;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1598
	// get the next position to go to, differs per airport
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1599
	AircraftNextAirportPos_and_Order(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1600
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1601
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1602
static void AircraftEventHandler_HeliTakeOff(Vehicle *v, const AirportFTAClass *Airport)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1603
{
2475
8443e1eefe1b (svn r3001) s/Player*/const Player*/
tron
parents: 2464
diff changeset
  1604
	const Player* p = GetPlayer(v->owner);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1605
	v->sprite_width = v->sprite_height = 24; // ??? no idea what this is
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1606
	v->u.air.state = FLYING;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1607
	// get the next position to go to, differs per airport
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1608
	AircraftNextAirportPos_and_Order(v);
1135
b939a3c018eb (svn r1636) fix: helicopters now go to a hangar if they are set for replacement or needs to be renewed
bjarni
parents: 1128
diff changeset
  1609
b939a3c018eb (svn r1636) fix: helicopters now go to a hangar if they are set for replacement or needs to be renewed
bjarni
parents: 1128
diff changeset
  1610
	// check if the aircraft needs to be replaced or renewed and send it to a hangar if needed
4111
8efd85ca4b76 (svn r5454) -Fix: [ 1394231 ] Autorenew glitch on helicopters
bjarni
parents: 4095
diff changeset
  1611
	// unless it is due for renewal but the engine is no longer available
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1612
	if (v->owner == _local_player && (
2848
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2819
diff changeset
  1613
				EngineHasReplacementForPlayer(p, v->engine_type) ||
4111
8efd85ca4b76 (svn r5454) -Fix: [ 1394231 ] Autorenew glitch on helicopters
bjarni
parents: 4095
diff changeset
  1614
				((p->engine_renew && v->age - v->max_age > p->engine_renew_months * 30) &&
8efd85ca4b76 (svn r5454) -Fix: [ 1394231 ] Autorenew glitch on helicopters
bjarni
parents: 4095
diff changeset
  1615
				HASBIT(GetEngine(v->engine_type)->player_avail, _local_player))
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1616
			)) {
1520
c85dc79795e0 (svn r2024) -Fix: [autoreplace] reverted all changes involving v->set_for_replacement as they caused desyncs.
bjarni
parents: 1401
diff changeset
  1617
		_current_player = _local_player;
4506
0d8fcc0a4e49 (svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents: 4463
diff changeset
  1618
		DoCommandP(v->tile, v->index, DEPOT_SERVICE | DEPOT_LOCATE_HANGAR, NULL, CMD_SEND_AIRCRAFT_TO_HANGAR | CMD_SHOW_NO_ERROR);
1520
c85dc79795e0 (svn r2024) -Fix: [autoreplace] reverted all changes involving v->set_for_replacement as they caused desyncs.
bjarni
parents: 1401
diff changeset
  1619
		_current_player = OWNER_NONE;
1135
b939a3c018eb (svn r1636) fix: helicopters now go to a hangar if they are set for replacement or needs to be renewed
bjarni
parents: 1128
diff changeset
  1620
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1621
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1622
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1623
static void AircraftEventHandler_Flying(Vehicle *v, const AirportFTAClass *Airport)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1624
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1625
	Station *st;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1626
	byte landingtype;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1627
	AirportFTA *current;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1628
	uint16 tcur_speed, tsubspeed;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1629
919
b0d6c7642f99 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 909
diff changeset
  1630
	st = GetStation(v->u.air.targetairport);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1631
	// flying device is accepted at this station
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1632
	// small airport --> no helicopters (AIRCRAFT_ONLY)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1633
	// all other airports --> all types of flying devices (ALL)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1634
	// heliport/oilrig, etc --> no airplanes (HELICOPTERS_ONLY)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1635
	// runway busy or not allowed to use this airstation, circle
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1636
	if (v->subtype != Airport->acc_planes &&
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1637
			st->airport_tile != 0 &&
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1638
			(st->owner == OWNER_NONE || st->owner == v->owner)) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1639
		// {32,FLYING,NOTHING_block,37}, {32,LANDING,N,33}, {32,HELILANDING,N,41},
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1640
		// if it is an airplane, look for LANDING, for helicopter HELILANDING
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1641
		// it is possible to choose from multiple landing runways, so loop until a free one is found
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1642
		landingtype = (v->subtype != 0) ? LANDING : HELILANDING;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1643
		current = Airport->layout[v->u.air.pos].next_in_chain;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1644
		while (current != NULL) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1645
			if (current->heading == landingtype) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1646
				// save speed before, since if AirportHasBlock is false, it resets them to 0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1647
				// we don't want that for plane in air
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1648
				// hack for speed thingie
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1649
				tcur_speed = v->cur_speed;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1650
				tsubspeed = v->subspeed;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1651
				if (!AirportHasBlock(v, current, Airport)) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1652
					v->u.air.state = landingtype; // LANDING / HELILANDING
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1653
					// it's a bit dirty, but I need to set position to next position, otherwise
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1654
					// if there are multiple runways, plane won't know which one it took (because
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1655
					// they all have heading LANDING). And also occupy that block!
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1656
					v->u.air.pos = current->next_position;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1657
					SETBITS(st->airport_flags, Airport->layout[v->u.air.pos].block);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1658
					return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1659
				}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1660
				v->cur_speed = tcur_speed;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1661
				v->subspeed = tsubspeed;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1662
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1663
			current = current->next_in_chain;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1664
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1665
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1666
	v->u.air.state = FLYING;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1667
	v->u.air.pos = Airport->layout[v->u.air.pos].next_position;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1668
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1669
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1670
static void AircraftEventHandler_Landing(Vehicle *v, const AirportFTAClass *Airport)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1671
{
2475
8443e1eefe1b (svn r3001) s/Player*/const Player*/
tron
parents: 2464
diff changeset
  1672
	const Player* p = GetPlayer(v->owner);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1673
	AircraftLandAirplane(v);  // maybe crash airplane
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1674
	v->u.air.state = ENDLANDING;
1195
95bbeb74ca13 (svn r1699) - Feature [autoreplace] a plane will now go to the hangar right after landing if it needs to be replaced
bjarni
parents: 1194
diff changeset
  1675
	// check if the aircraft needs to be replaced or renewed and send it to a hangar if needed
95bbeb74ca13 (svn r1699) - Feature [autoreplace] a plane will now go to the hangar right after landing if it needs to be replaced
bjarni
parents: 1194
diff changeset
  1676
	if (v->current_order.type != OT_GOTO_DEPOT && v->owner == _local_player) {
95bbeb74ca13 (svn r1699) - Feature [autoreplace] a plane will now go to the hangar right after landing if it needs to be replaced
bjarni
parents: 1194
diff changeset
  1677
		// only the vehicle owner needs to calculate the rest (locally)
2848
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2819
diff changeset
  1678
		if (EngineHasReplacementForPlayer(p, v->engine_type) ||
2293
a71c210076be (svn r2817) -Codechange: [autoreplace]: moved autoreplace and autorenew to serverside
bjarni
parents: 2244
diff changeset
  1679
			(p->engine_renew && v->age - v->max_age > (p->engine_renew_months * 30))) {
4412
945e8a9bb89a (svn r6165) -Feature: control click Goto Depot will now make the vehicle service
bjarni
parents: 4389
diff changeset
  1680
			// send the aircraft to the hangar at next airport
1195
95bbeb74ca13 (svn r1699) - Feature [autoreplace] a plane will now go to the hangar right after landing if it needs to be replaced
bjarni
parents: 1194
diff changeset
  1681
			_current_player = _local_player;
4506
0d8fcc0a4e49 (svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents: 4463
diff changeset
  1682
			DoCommandP(v->tile, v->index, DEPOT_SERVICE, NULL, CMD_SEND_AIRCRAFT_TO_HANGAR | CMD_SHOW_NO_ERROR);
1195
95bbeb74ca13 (svn r1699) - Feature [autoreplace] a plane will now go to the hangar right after landing if it needs to be replaced
bjarni
parents: 1194
diff changeset
  1683
			_current_player = OWNER_NONE;
95bbeb74ca13 (svn r1699) - Feature [autoreplace] a plane will now go to the hangar right after landing if it needs to be replaced
bjarni
parents: 1194
diff changeset
  1684
		}
95bbeb74ca13 (svn r1699) - Feature [autoreplace] a plane will now go to the hangar right after landing if it needs to be replaced
bjarni
parents: 1194
diff changeset
  1685
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1686
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1687
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1688
static void AircraftEventHandler_HeliLanding(Vehicle *v, const AirportFTAClass *Airport)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1689
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1690
	AircraftLand(v); // helicopters don't crash
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1691
	v->u.air.state = HELIENDLANDING;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1692
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1693
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1694
static void AircraftEventHandler_EndLanding(Vehicle *v, const AirportFTAClass *Airport)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1695
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1696
	// next block busy, don't do a thing, just wait
2549
f1d3b383d557 (svn r3078) Some more stuff, which piled up:
tron
parents: 2517
diff changeset
  1697
	if (AirportHasBlock(v, &Airport->layout[v->u.air.pos], Airport)) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1698
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1699
	// if going to terminal (OT_GOTO_STATION) choose one
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1700
	// 1. in case all terminals are busy AirportFindFreeTerminal() returns false or
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1701
	// 2. not going for terminal (but depot, no order),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1702
	// --> get out of the way to the hangar.
555
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1703
	if (v->current_order.type == OT_GOTO_STATION) {
2549
f1d3b383d557 (svn r3078) Some more stuff, which piled up:
tron
parents: 2517
diff changeset
  1704
		if (AirportFindFreeTerminal(v, Airport)) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1705
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1706
	v->u.air.state = HANGAR;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1707
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1708
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1709
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1710
static void AircraftEventHandler_HeliEndLanding(Vehicle *v, const AirportFTAClass *Airport)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1711
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1712
	// next block busy, don't do a thing, just wait
2549
f1d3b383d557 (svn r3078) Some more stuff, which piled up:
tron
parents: 2517
diff changeset
  1713
	if (AirportHasBlock(v, &Airport->layout[v->u.air.pos], Airport)) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1714
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1715
	// if going to helipad (OT_GOTO_STATION) choose one. If airport doesn't have helipads, choose terminal
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1716
	// 1. in case all terminals/helipads are busy (AirportFindFreeHelipad() returns false) or
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1717
	// 2. not going for terminal (but depot, no order),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1718
	// --> get out of the way to the hangar IF there are terminals on the airport.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1719
	// --> else TAKEOFF
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1720
	// the reason behind this is that if an airport has a terminal, it also has a hangar. Airplanes
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1721
	// must go to a hangar.
555
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1722
	if (v->current_order.type == OT_GOTO_STATION) {
2549
f1d3b383d557 (svn r3078) Some more stuff, which piled up:
tron
parents: 2517
diff changeset
  1723
		if (AirportFindFreeHelipad(v, Airport)) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1724
	}
4059
f1442dfe1c8a (svn r5346) - Feature: Add 4 new airports. 2 for aircraft, 2 for helicopters.
richk
parents: 4056
diff changeset
  1725
	v->u.air.state = (Airport->nof_depots != 0) ? HANGAR : HELITAKEOFF;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1726
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1727
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1728
typedef void AircraftStateHandler(Vehicle *v, const AirportFTAClass *Airport);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1729
static AircraftStateHandler * const _aircraft_state_handlers[] = {
4344
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4338
diff changeset
  1730
	AircraftEventHandler_General,        // TO_ALL         =  0
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4338
diff changeset
  1731
	AircraftEventHandler_InHangar,       // HANGAR         =  1
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4338
diff changeset
  1732
	AircraftEventHandler_AtTerminal,     // TERM1          =  2
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4338
diff changeset
  1733
	AircraftEventHandler_AtTerminal,     // TERM2          =  3
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4338
diff changeset
  1734
	AircraftEventHandler_AtTerminal,     // TERM3          =  4
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4338
diff changeset
  1735
	AircraftEventHandler_AtTerminal,     // TERM4          =  5
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4338
diff changeset
  1736
	AircraftEventHandler_AtTerminal,     // TERM5          =  6
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4338
diff changeset
  1737
	AircraftEventHandler_AtTerminal,     // TERM6          =  7
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4338
diff changeset
  1738
	AircraftEventHandler_AtTerminal,     // HELIPAD1       =  8
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4338
diff changeset
  1739
	AircraftEventHandler_AtTerminal,     // HELIPAD2       =  9
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4338
diff changeset
  1740
	AircraftEventHandler_TakeOff,        // TAKEOFF        = 10
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4338
diff changeset
  1741
	AircraftEventHandler_StartTakeOff,   // STARTTAKEOFF   = 11
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4338
diff changeset
  1742
	AircraftEventHandler_EndTakeOff,     // ENDTAKEOFF     = 12
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4338
diff changeset
  1743
	AircraftEventHandler_HeliTakeOff,    // HELITAKEOFF    = 13
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4338
diff changeset
  1744
	AircraftEventHandler_Flying,         // FLYING         = 14
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4338
diff changeset
  1745
	AircraftEventHandler_Landing,        // LANDING        = 15
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4338
diff changeset
  1746
	AircraftEventHandler_EndLanding,     // ENDLANDING     = 16
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4338
diff changeset
  1747
	AircraftEventHandler_HeliLanding,    // HELILANDING    = 17
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4338
diff changeset
  1748
	AircraftEventHandler_HeliEndLanding, // HELIENDLANDING = 18
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4338
diff changeset
  1749
	AircraftEventHandler_AtTerminal,     // TERM7          = 19
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4338
diff changeset
  1750
	AircraftEventHandler_AtTerminal,     // TERM8          = 20
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4338
diff changeset
  1751
	AircraftEventHandler_AtTerminal,     // HELIPAD3       = 21
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4338
diff changeset
  1752
	AircraftEventHandler_AtTerminal,     // HELIPAD4       = 22
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1753
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1754
4344
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4338
diff changeset
  1755
static void AirportClearBlock(const Vehicle *v, const AirportFTAClass *Airport)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1756
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1757
	// we have left the previous block, and entered the new one. Free the previous block
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1758
	if (Airport->layout[v->u.air.previous_pos].block != Airport->layout[v->u.air.pos].block) {
4344
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4338
diff changeset
  1759
		Station *st = GetStation(v->u.air.targetairport);
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1760
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1761
		CLRBITS(st->airport_flags, Airport->layout[v->u.air.previous_pos].block);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1762
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1763
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1764
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1765
static void AirportGoToNextPosition(Vehicle *v, const AirportFTAClass *Airport)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1766
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1767
	// if aircraft is not in position, wait until it is
2549
f1d3b383d557 (svn r3078) Some more stuff, which piled up:
tron
parents: 2517
diff changeset
  1768
	if (!AircraftController(v)) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1769
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1770
	AirportClearBlock(v, Airport);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1771
	AirportMove(v, Airport); // move aircraft to next position
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1772
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1773
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1774
// gets pos from vehicle and next orders
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1775
static bool AirportMove(Vehicle *v, const AirportFTAClass *Airport)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1776
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1777
	AirportFTA *current;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1778
	byte prev_pos;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1779
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1780
	// error handling
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1781
	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
  1782
		DEBUG(misc, 0) ("position %d is not valid for current airport. Max position is %d", v->u.air.pos, Airport->nofelements-1);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1783
		assert(v->u.air.pos < Airport->nofelements);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1784
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1785
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1786
	current = &Airport->layout[v->u.air.pos];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1787
	// we have arrived in an important state (eg terminal, hangar, etc.)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1788
	if (current->heading == v->u.air.state) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1789
		prev_pos = v->u.air.pos; // location could be changed in state, so save it before-hand
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1790
		_aircraft_state_handlers[v->u.air.state](v, Airport);
2549
f1d3b383d557 (svn r3078) Some more stuff, which piled up:
tron
parents: 2517
diff changeset
  1791
		if (v->u.air.state != FLYING) v->u.air.previous_pos = prev_pos;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1792
		return true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1793
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1794
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1795
	v->u.air.previous_pos = v->u.air.pos; // save previous location
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1796
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1797
	// there is only one choice to move to
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1798
	if (current->next_in_chain == NULL) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1799
		if (AirportSetBlocks(v, current, Airport)) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1800
			v->u.air.pos = current->next_position;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1801
		} // move to next position
3579
8885d65a2ba7 (svn r4465) -Codechange (FS#60): Remove a variable no one really needs. (DaleStan)
celestar
parents: 3491
diff changeset
  1802
		return false;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1803
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1804
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1805
	// there are more choices to choose from, choose the one that
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1806
	// matches our heading
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1807
	do {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1808
		if (v->u.air.state == current->heading || current->heading == TO_ALL) {
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1809
			if (AirportSetBlocks(v, current, Airport)) {
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1810
				v->u.air.pos = current->next_position;
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1811
			} // move to next position
3579
8885d65a2ba7 (svn r4465) -Codechange (FS#60): Remove a variable no one really needs. (DaleStan)
celestar
parents: 3491
diff changeset
  1812
			return false;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1813
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1814
		current = current->next_in_chain;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1815
	} while (current != NULL);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1816
70
60216d94f7c1 (svn r71) -Cheat: [976127] no extra frequent jet crash on small airports (truesatan)
darkvater
parents: 19
diff changeset
  1817
	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
  1818
	DEBUG(misc, 0) ("Airport entry point: %d, Vehicle: %d", Airport->entry_point, v->index);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1819
	assert(0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1820
	return false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1821
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1822
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1823
// returns true if the road ahead is busy, eg. you must wait before proceeding
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1824
static bool AirportHasBlock(Vehicle *v, AirportFTA *current_pos, const AirportFTAClass *Airport)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1825
{
2630
35249d2ded3e (svn r3172) static, const
tron
parents: 2618
diff changeset
  1826
	const AirportFTA* reference = &Airport->layout[v->u.air.pos];
35249d2ded3e (svn r3172) static, const
tron
parents: 2618
diff changeset
  1827
	const AirportFTA* next = &Airport->layout[current_pos->next_position];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1828
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1829
	// same block, then of course we can move
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1830
	if (Airport->layout[current_pos->position].block != next->block) {
2630
35249d2ded3e (svn r3172) static, const
tron
parents: 2618
diff changeset
  1831
		const Station* st = GetStation(v->u.air.targetairport);
35249d2ded3e (svn r3172) static, const
tron
parents: 2618
diff changeset
  1832
		uint32 airport_flags = next->block;
35249d2ded3e (svn r3172) static, const
tron
parents: 2618
diff changeset
  1833
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1834
		// check additional possible extra blocks
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1835
		if (current_pos != reference && current_pos->block != NOTHING_block) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1836
			airport_flags |= current_pos->block;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1837
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1838
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1839
		if (HASBITS(st->airport_flags, airport_flags)) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1840
			v->cur_speed = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1841
			v->subspeed = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1842
			return true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1843
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1844
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1845
	return false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1846
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1847
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1848
// returns true on success. Eg, next block was free and we have occupied it
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1849
static bool AirportSetBlocks(Vehicle *v, AirportFTA *current_pos, const AirportFTAClass *Airport)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1850
{
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1851
	AirportFTA* next = &Airport->layout[current_pos->next_position];
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1852
	AirportFTA* reference = &Airport->layout[v->u.air.pos];
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1853
	AirportFTA* current;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1854
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1855
	// if the next position is in another block, check it and wait until it is free
4059
f1442dfe1c8a (svn r5346) - Feature: Add 4 new airports. 2 for aircraft, 2 for helicopters.
richk
parents: 4056
diff changeset
  1856
	if ((Airport->layout[current_pos->position].block & next->block) != next->block) {
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1857
		uint32 airport_flags = next->block;
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1858
		Station* st = GetStation(v->u.air.targetairport);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1859
		//search for all all elements in the list with the same state, and blocks != N
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1860
		// this means more blocks should be checked/set
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1861
		current = current_pos;
2549
f1d3b383d557 (svn r3078) Some more stuff, which piled up:
tron
parents: 2517
diff changeset
  1862
		if (current == reference) current = current->next_in_chain;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1863
		while (current != NULL) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1864
			if (current->heading == current_pos->heading && current->block != 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1865
				airport_flags |= current->block;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1866
				break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1867
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1868
			current = current->next_in_chain;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1869
		};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1870
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1871
		// if the block to be checked is in the next position, then exclude that from
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1872
		// checking, because it has been set by the airplane before
2549
f1d3b383d557 (svn r3078) Some more stuff, which piled up:
tron
parents: 2517
diff changeset
  1873
		if (current_pos->block == next->block) airport_flags ^= next->block;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1874
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1875
		if (HASBITS(st->airport_flags, airport_flags)) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1876
			v->cur_speed = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1877
			v->subspeed = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1878
			return false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1879
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1880
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1881
		if (next->block != NOTHING_block) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1882
			SETBITS(st->airport_flags, airport_flags); // occupy next block
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1883
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1884
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1885
	return true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1886
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1887
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1888
static bool FreeTerminal(Vehicle *v, byte i, byte last_terminal)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1889
{
919
b0d6c7642f99 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 909
diff changeset
  1890
	Station *st = GetStation(v->u.air.targetairport);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1891
	for (; i < last_terminal; i++) {
4059
f1442dfe1c8a (svn r5346) - Feature: Add 4 new airports. 2 for aircraft, 2 for helicopters.
richk
parents: 4056
diff changeset
  1892
		if (!HASBIT(st->airport_flags, _airport_terminal_flag[i])) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1893
			// TERMINAL# HELIPAD#
4059
f1442dfe1c8a (svn r5346) - Feature: Add 4 new airports. 2 for aircraft, 2 for helicopters.
richk
parents: 4056
diff changeset
  1894
			v->u.air.state = _airport_terminal_state[i]; // start moving to that terminal/helipad
f1442dfe1c8a (svn r5346) - Feature: Add 4 new airports. 2 for aircraft, 2 for helicopters.
richk
parents: 4056
diff changeset
  1895
			SETBIT(st->airport_flags, _airport_terminal_flag[i]); // occupy terminal/helipad
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1896
			return true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1897
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1898
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1899
	return false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1900
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1901
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1902
static uint GetNumTerminals(const AirportFTAClass *Airport)
950
1f6546ea35a3 (svn r1440) -Feature: Allows more flexible airport layouts now, as the number of
celestar
parents: 926
diff changeset
  1903
{
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1904
	uint num = 0;
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1905
	uint i;
1019
6363b8a4273e (svn r1520) Trim 134 (!) lines with trailing whitespace ):
tron
parents: 1018
diff changeset
  1906
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1907
	for (i = Airport->terminals[0]; i > 0; i--) num += Airport->terminals[i];
1019
6363b8a4273e (svn r1520) Trim 134 (!) lines with trailing whitespace ):
tron
parents: 1018
diff changeset
  1908
950
1f6546ea35a3 (svn r1440) -Feature: Allows more flexible airport layouts now, as the number of
celestar
parents: 926
diff changeset
  1909
	return num;
1f6546ea35a3 (svn r1440) -Feature: Allows more flexible airport layouts now, as the number of
celestar
parents: 926
diff changeset
  1910
}
1f6546ea35a3 (svn r1440) -Feature: Allows more flexible airport layouts now, as the number of
celestar
parents: 926
diff changeset
  1911
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
  1912
static bool AirportFindFreeTerminal(Vehicle *v, const AirportFTAClass *Airport)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1913
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1914
	AirportFTA *temp;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1915
	Station *st;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1916
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1917
	/* example of more terminalgroups
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1918
		{0,HANGAR,NOTHING_block,1}, {0,255,TERM_GROUP1_block,0}, {0,255,TERM_GROUP2_ENTER_block,1}, {0,0,N,1},
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1919
		Heading 255 denotes a group. We see 2 groups here:
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1920
		1. group 0 -- TERM_GROUP1_block (check block)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1921
		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
  1922
		First in line is checked first, group 0. If the block (TERM_GROUP1_block) is free, it
4434
4175805666a5 (svn r6204) -Cleanup: replace non-indentation with spaces; like '}<TAB>else {' -> '} else {', tabs between code and comment, etc.
rubidium
parents: 4412
diff changeset
  1923
		looks at the corresponding terminals of that group. If no free ones are found, other
4175805666a5 (svn r6204) -Cleanup: replace non-indentation with spaces; like '}<TAB>else {' -> '} else {', tabs between code and comment, etc.
rubidium
parents: 4412
diff changeset
  1924
		possible groups are checked (in this case group 1, since that is after group 0). If that
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1925
		fails, then attempt fails and plane waits
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1926
	*/
950
1f6546ea35a3 (svn r1440) -Feature: Allows more flexible airport layouts now, as the number of
celestar
parents: 926
diff changeset
  1927
	if (Airport->terminals[0] > 1) {
919
b0d6c7642f99 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 909
diff changeset
  1928
		st = GetStation(v->u.air.targetairport);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1929
		temp = Airport->layout[v->u.air.pos].next_in_chain;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1930
		while (temp != NULL) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1931
			if (temp->heading == 255) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1932
				if (!HASBITS(st->airport_flags, temp->block)) {
950
1f6546ea35a3 (svn r1440) -Feature: Allows more flexible airport layouts now, as the number of
celestar
parents: 926
diff changeset
  1933
					int target_group;
1f6546ea35a3 (svn r1440) -Feature: Allows more flexible airport layouts now, as the number of
celestar
parents: 926
diff changeset
  1934
					int i;
1f6546ea35a3 (svn r1440) -Feature: Allows more flexible airport layouts now, as the number of
celestar
parents: 926
diff changeset
  1935
					int group_start = 0;
1f6546ea35a3 (svn r1440) -Feature: Allows more flexible airport layouts now, as the number of
celestar
parents: 926
diff changeset
  1936
					int group_end;
1f6546ea35a3 (svn r1440) -Feature: Allows more flexible airport layouts now, as the number of
celestar
parents: 926
diff changeset
  1937
1f6546ea35a3 (svn r1440) -Feature: Allows more flexible airport layouts now, as the number of
celestar
parents: 926
diff changeset
  1938
					//read which group do we want to go to?
1f6546ea35a3 (svn r1440) -Feature: Allows more flexible airport layouts now, as the number of
celestar
parents: 926
diff changeset
  1939
					//(the first free group)
1f6546ea35a3 (svn r1440) -Feature: Allows more flexible airport layouts now, as the number of
celestar
parents: 926
diff changeset
  1940
					target_group = temp->next_position + 1;
1f6546ea35a3 (svn r1440) -Feature: Allows more flexible airport layouts now, as the number of
celestar
parents: 926
diff changeset
  1941
1f6546ea35a3 (svn r1440) -Feature: Allows more flexible airport layouts now, as the number of
celestar
parents: 926
diff changeset
  1942
					//at what terminal does the group start?
1f6546ea35a3 (svn r1440) -Feature: Allows more flexible airport layouts now, as the number of
celestar
parents: 926
diff changeset
  1943
					//that means, sum up all terminals of
1f6546ea35a3 (svn r1440) -Feature: Allows more flexible airport layouts now, as the number of
celestar
parents: 926
diff changeset
  1944
					//groups with lower number
2549
f1d3b383d557 (svn r3078) Some more stuff, which piled up:
tron
parents: 2517
diff changeset
  1945
					for (i = 1; i < target_group; i++)
950
1f6546ea35a3 (svn r1440) -Feature: Allows more flexible airport layouts now, as the number of
celestar
parents: 926
diff changeset
  1946
						group_start += Airport->terminals[i];
1f6546ea35a3 (svn r1440) -Feature: Allows more flexible airport layouts now, as the number of
celestar
parents: 926
diff changeset
  1947
1f6546ea35a3 (svn r1440) -Feature: Allows more flexible airport layouts now, as the number of
celestar
parents: 926
diff changeset
  1948
					group_end = group_start + Airport->terminals[target_group];
2549
f1d3b383d557 (svn r3078) Some more stuff, which piled up:
tron
parents: 2517
diff changeset
  1949
					if (FreeTerminal(v, group_start, group_end)) return true;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1950
				}
2549
f1d3b383d557 (svn r3078) Some more stuff, which piled up:
tron
parents: 2517
diff changeset
  1951
			} else {
f1d3b383d557 (svn r3078) Some more stuff, which piled up:
tron
parents: 2517
diff changeset
  1952
				/* once the heading isn't 255, we've exhausted the possible blocks.
f1d3b383d557 (svn r3078) Some more stuff, which piled up:
tron
parents: 2517
diff changeset
  1953
				 * So we cannot move */
f1d3b383d557 (svn r3078) Some more stuff, which piled up:
tron
parents: 2517
diff changeset
  1954
				return false;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1955
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1956
			temp = temp->next_in_chain;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1957
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1958
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1959
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1960
	// if there is only 1 terminalgroup, all terminals are checked (starting from 0 to max)
950
1f6546ea35a3 (svn r1440) -Feature: Allows more flexible airport layouts now, as the number of
celestar
parents: 926
diff changeset
  1961
	return FreeTerminal(v, 0, GetNumTerminals(Airport));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1962
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1963
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1964
static uint GetNumHelipads(const AirportFTAClass *Airport)
950
1f6546ea35a3 (svn r1440) -Feature: Allows more flexible airport layouts now, as the number of
celestar
parents: 926
diff changeset
  1965
{
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1966
	uint num = 0;
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1967
	uint i;
1019
6363b8a4273e (svn r1520) Trim 134 (!) lines with trailing whitespace ):
tron
parents: 1018
diff changeset
  1968
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1969
	for (i = Airport->helipads[0]; i > 0; i--) num += Airport->helipads[i];
1019
6363b8a4273e (svn r1520) Trim 134 (!) lines with trailing whitespace ):
tron
parents: 1018
diff changeset
  1970
950
1f6546ea35a3 (svn r1440) -Feature: Allows more flexible airport layouts now, as the number of
celestar
parents: 926
diff changeset
  1971
	return num;
1f6546ea35a3 (svn r1440) -Feature: Allows more flexible airport layouts now, as the number of
celestar
parents: 926
diff changeset
  1972
}
1f6546ea35a3 (svn r1440) -Feature: Allows more flexible airport layouts now, as the number of
celestar
parents: 926
diff changeset
  1973
1f6546ea35a3 (svn r1440) -Feature: Allows more flexible airport layouts now, as the number of
celestar
parents: 926
diff changeset
  1974
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
  1975
static bool AirportFindFreeHelipad(Vehicle *v, const AirportFTAClass *Airport)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1976
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1977
	// if an airport doesn't have helipads, use terminals
2549
f1d3b383d557 (svn r3078) Some more stuff, which piled up:
tron
parents: 2517
diff changeset
  1978
	if (Airport->helipads == NULL) return AirportFindFreeTerminal(v, Airport);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1979
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1980
	// if there are more helicoptergroups, pick one, just as in AirportFindFreeTerminal()
950
1f6546ea35a3 (svn r1440) -Feature: Allows more flexible airport layouts now, as the number of
celestar
parents: 926
diff changeset
  1981
	if (Airport->helipads[0] > 1) {
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4072
diff changeset
  1982
		const Station* st = GetStation(v->u.air.targetairport);
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4072
diff changeset
  1983
		const AirportFTA* temp = Airport->layout[v->u.air.pos].next_in_chain;
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4072
diff changeset
  1984
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1985
		while (temp != NULL) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1986
			if (temp->heading == 255) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1987
				if (!HASBITS(st->airport_flags, temp->block)) {
950
1f6546ea35a3 (svn r1440) -Feature: Allows more flexible airport layouts now, as the number of
celestar
parents: 926
diff changeset
  1988
					int target_group;
1f6546ea35a3 (svn r1440) -Feature: Allows more flexible airport layouts now, as the number of
celestar
parents: 926
diff changeset
  1989
					int i;
1f6546ea35a3 (svn r1440) -Feature: Allows more flexible airport layouts now, as the number of
celestar
parents: 926
diff changeset
  1990
					int group_start = 0;
1f6546ea35a3 (svn r1440) -Feature: Allows more flexible airport layouts now, as the number of
celestar
parents: 926
diff changeset
  1991
					int group_end;
1f6546ea35a3 (svn r1440) -Feature: Allows more flexible airport layouts now, as the number of
celestar
parents: 926
diff changeset
  1992
1f6546ea35a3 (svn r1440) -Feature: Allows more flexible airport layouts now, as the number of
celestar
parents: 926
diff changeset
  1993
					//read which group do we want to go to?
1f6546ea35a3 (svn r1440) -Feature: Allows more flexible airport layouts now, as the number of
celestar
parents: 926
diff changeset
  1994
					//(the first free group)
1f6546ea35a3 (svn r1440) -Feature: Allows more flexible airport layouts now, as the number of
celestar
parents: 926
diff changeset
  1995
					target_group = temp->next_position + 1;
1f6546ea35a3 (svn r1440) -Feature: Allows more flexible airport layouts now, as the number of
celestar
parents: 926
diff changeset
  1996
1f6546ea35a3 (svn r1440) -Feature: Allows more flexible airport layouts now, as the number of
celestar
parents: 926
diff changeset
  1997
					//at what terminal does the group start?
1f6546ea35a3 (svn r1440) -Feature: Allows more flexible airport layouts now, as the number of
celestar
parents: 926
diff changeset
  1998
					//that means, sum up all terminals of
1f6546ea35a3 (svn r1440) -Feature: Allows more flexible airport layouts now, as the number of
celestar
parents: 926
diff changeset
  1999
					//groups with lower number
2952
6a26eeda9679 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2854
diff changeset
  2000
					for (i = 1; i < target_group; i++)
950
1f6546ea35a3 (svn r1440) -Feature: Allows more flexible airport layouts now, as the number of
celestar
parents: 926
diff changeset
  2001
						group_start += Airport->helipads[i];
1f6546ea35a3 (svn r1440) -Feature: Allows more flexible airport layouts now, as the number of
celestar
parents: 926
diff changeset
  2002
1f6546ea35a3 (svn r1440) -Feature: Allows more flexible airport layouts now, as the number of
celestar
parents: 926
diff changeset
  2003
					group_end = group_start + Airport->helipads[target_group];
2549
f1d3b383d557 (svn r3078) Some more stuff, which piled up:
tron
parents: 2517
diff changeset
  2004
					if (FreeTerminal(v, group_start, group_end)) return true;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2005
				}
2549
f1d3b383d557 (svn r3078) Some more stuff, which piled up:
tron
parents: 2517
diff changeset
  2006
			} else {
f1d3b383d557 (svn r3078) Some more stuff, which piled up:
tron
parents: 2517
diff changeset
  2007
				/* once the heading isn't 255, we've exhausted the possible blocks.
f1d3b383d557 (svn r3078) Some more stuff, which piled up:
tron
parents: 2517
diff changeset
  2008
				 * So we cannot move */
f1d3b383d557 (svn r3078) Some more stuff, which piled up:
tron
parents: 2517
diff changeset
  2009
				return false;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2010
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2011
			temp = temp->next_in_chain;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2012
		}
2549
f1d3b383d557 (svn r3078) Some more stuff, which piled up:
tron
parents: 2517
diff changeset
  2013
	} else {
f1d3b383d557 (svn r3078) Some more stuff, which piled up:
tron
parents: 2517
diff changeset
  2014
		// only 1 helicoptergroup, check all helipads
f1d3b383d557 (svn r3078) Some more stuff, which piled up:
tron
parents: 2517
diff changeset
  2015
		// The blocks for helipads start after the last terminal (MAX_TERMINALS)
f1d3b383d557 (svn r3078) Some more stuff, which piled up:
tron
parents: 2517
diff changeset
  2016
		return FreeTerminal(v, MAX_TERMINALS, GetNumHelipads(Airport) + MAX_TERMINALS);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2017
	}
4434
4175805666a5 (svn r6204) -Cleanup: replace non-indentation with spaces; like '}<TAB>else {' -> '} else {', tabs between code and comment, etc.
rubidium
parents: 4412
diff changeset
  2018
	return false; // it shouldn't get here anytime, but just to be sure
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2019
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2020
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2021
static void AircraftEventHandler(Vehicle *v, int loop)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2022
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2023
	v->tick_counter++;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2024
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2025
	if (v->vehstatus & VS_CRASHED) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2026
		HandleCrashedAircraft(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2027
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2028
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2029
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  2030
	if (v->vehstatus & VS_STOPPED) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2031
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2032
	/* aircraft is broken down? */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2033
	if (v->breakdown_ctr != 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2034
		if (v->breakdown_ctr <= 2) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2035
			HandleBrokenAircraft(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2036
		} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2037
			v->breakdown_ctr--;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2038
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2039
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2040
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2041
	HandleAircraftSmoke(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2042
	ProcessAircraftOrder(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2043
	HandleAircraftLoading(v, loop);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2044
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  2045
	if (v->current_order.type >= OT_LOADING) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2046
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2047
	// pass the right airport structure to the functions
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2048
	// DEREF_STATION gets target airport (Station *st), its type is passed to GetAirport
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2049
	// that returns the correct layout depending on type
919
b0d6c7642f99 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 909
diff changeset
  2050
	AirportGoToNextPosition(v, GetAirport(GetStation(v->u.air.targetairport)->airport_type));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2051
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2052
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2053
void Aircraft_Tick(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2054
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2055
	int i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2056
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  2057
	if (v->subtype > 2) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2058
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  2059
	if (v->subtype == 0) HelicopterTickHandler(v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2060
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2061
	AgeAircraftCargo(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2062
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  2063
	for (i = 0; i != 6; i++) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2064
		AircraftEventHandler(v, i);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2065
		if (v->type != VEH_Aircraft) // In case it was deleted
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2066
			break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2067
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2068
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2069
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  2070
void UpdateOilRig(void)
950
1f6546ea35a3 (svn r1440) -Feature: Allows more flexible airport layouts now, as the number of
celestar
parents: 926
diff changeset
  2071
{
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  2072
	Station* st;
1019
6363b8a4273e (svn r1520) Trim 134 (!) lines with trailing whitespace ):
tron
parents: 1018
diff changeset
  2073
950
1f6546ea35a3 (svn r1440) -Feature: Allows more flexible airport layouts now, as the number of
celestar
parents: 926
diff changeset
  2074
	FOR_ALL_STATIONS(st) {
1f6546ea35a3 (svn r1440) -Feature: Allows more flexible airport layouts now, as the number of
celestar
parents: 926
diff changeset
  2075
		if (st->airport_type == 5) st->airport_type = AT_OILRIG;
1f6546ea35a3 (svn r1440) -Feature: Allows more flexible airport layouts now, as the number of
celestar
parents: 926
diff changeset
  2076
	}
1f6546ea35a3 (svn r1440) -Feature: Allows more flexible airport layouts now, as the number of
celestar
parents: 926
diff changeset
  2077
}
1f6546ea35a3 (svn r1440) -Feature: Allows more flexible airport layouts now, as the number of
celestar
parents: 926
diff changeset
  2078
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2079
// need to be called to load aircraft from old version
1093
e8d26c7dc42f (svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents: 1064
diff changeset
  2080
void UpdateOldAircraft(void)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2081
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2082
	Station *st;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2083
	Vehicle *v_oldstyle;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2084
	GetNewVehiclePosResult gp;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2085
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2086
	// set airport_flags to 0 for all airports just to be sure
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2087
	FOR_ALL_STATIONS(st) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2088
		st->airport_flags = 0; // reset airport
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2089
		// type of oilrig has been moved, update it (3-5)
2549
f1d3b383d557 (svn r3078) Some more stuff, which piled up:
tron
parents: 2517
diff changeset
  2090
		if (st->airport_type == 3) st->airport_type = AT_OILRIG;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2091
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2092
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2093
	FOR_ALL_VEHICLES(v_oldstyle) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2094
	// airplane has another vehicle with subtype 4 (shadow), helicopter also has 3 (rotor)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2095
	// skip those
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2096
		if (v_oldstyle->type == VEH_Aircraft && v_oldstyle->subtype <= 2) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2097
			// airplane in terminal stopped doesn't hurt anyone, so goto next
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  2098
			if (v_oldstyle->vehstatus & VS_STOPPED && v_oldstyle->u.air.state == 0) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2099
				v_oldstyle->u.air.state = HANGAR;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2100
				continue;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2101
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2102
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2103
			AircraftLeaveHangar(v_oldstyle); // make airplane visible if it was in a depot for example
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2104
			v_oldstyle->vehstatus &= ~VS_STOPPED; // make airplane moving
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2105
			v_oldstyle->u.air.state = FLYING;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2106
			AircraftNextAirportPos_and_Order(v_oldstyle); // move it to the entry point of the airport
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2107
			GetNewVehiclePos(v_oldstyle, &gp); // get the position of the plane (to be used for setting)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2108
			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
  2109
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2110
			// correct speed of helicopter-rotors
2549
f1d3b383d557 (svn r3078) Some more stuff, which piled up:
tron
parents: 2517
diff changeset
  2111
			if (v_oldstyle->subtype == 0) v_oldstyle->next->next->cur_speed = 32;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2112
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2113
			// set new position x,y,z
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2114
			SetAircraftPosition(v_oldstyle, gp.x, gp.y, GetAircraftFlyingAltitude(v_oldstyle));
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2115
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2116
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2117
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2118
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
  2119
void UpdateAirplanesOnNewStation(Station *st)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2120
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2121
	GetNewVehiclePosResult gp;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2122
	Vehicle *v;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2123
	byte takeofftype;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2124
	uint16 cnt;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2125
	// only 1 station is updated per function call, so it is enough to get entry_point once
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2126
	const AirportFTAClass *ap = GetAirport(st->airport_type);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2127
	FOR_ALL_VEHICLES(v) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2128
		if (v->type == VEH_Aircraft && v->subtype <= 2) {
4434
4175805666a5 (svn r6204) -Cleanup: replace non-indentation with spaces; like '}<TAB>else {' -> '} else {', tabs between code and comment, etc.
rubidium
parents: 4412
diff changeset
  2129
			if (v->u.air.targetairport == st->index) { // if heading to this airport
4175805666a5 (svn r6204) -Cleanup: replace non-indentation with spaces; like '}<TAB>else {' -> '} else {', tabs between code and comment, etc.
rubidium
parents: 4412
diff changeset
  2130
				/* update position of airplane. If plane is not flying, landing, or taking off
4175805666a5 (svn r6204) -Cleanup: replace non-indentation with spaces; like '}<TAB>else {' -> '} else {', tabs between code and comment, etc.
rubidium
parents: 4412
diff changeset
  2131
				 *you cannot delete airport, so it doesn't matter
4175805666a5 (svn r6204) -Cleanup: replace non-indentation with spaces; like '}<TAB>else {' -> '} else {', tabs between code and comment, etc.
rubidium
parents: 4412
diff changeset
  2132
				 */
4175805666a5 (svn r6204) -Cleanup: replace non-indentation with spaces; like '}<TAB>else {' -> '} else {', tabs between code and comment, etc.
rubidium
parents: 4412
diff changeset
  2133
				if (v->u.air.state >= FLYING) { // circle around
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2134
					v->u.air.pos = v->u.air.previous_pos = ap->entry_point;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2135
					v->u.air.state = FLYING;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2136
					// landing plane needs to be reset to flying height (only if in pause mode upgrade,
1401
56d372a75b74 (svn r1905) - Fix: [ 1118810 ] openttd: ship_cmd.c:642 ... Assertion failed. Mapwrap fixed in ship_cmd.c (was implicitely ok before biggermaps).
Darkvater
parents: 1359
diff changeset
  2137
					// in normal mode, plane is reset in AircraftController. It doesn't hurt for FLYING
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2138
					GetNewVehiclePos(v, &gp);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2139
					// set new position x,y,z
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2140
					SetAircraftPosition(v, gp.x, gp.y, GetAircraftFlyingAltitude(v));
2549
f1d3b383d557 (svn r3078) Some more stuff, which piled up:
tron
parents: 2517
diff changeset
  2141
				} else {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2142
					assert(v->u.air.state == ENDTAKEOFF || v->u.air.state == HELITAKEOFF);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2143
					takeofftype = (v->subtype == 0) ? HELITAKEOFF : ENDTAKEOFF;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2144
					// search in airportdata for that heading
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2145
					// easiest to do, since this doesn't happen a lot
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2146
					for (cnt = 0; cnt < ap->nofelements; cnt++) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2147
						if (ap->layout[cnt].heading == takeofftype) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2148
							v->u.air.pos = ap->layout[cnt].position;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2149
							break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2150
						}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2151
					}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2152
				}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2153
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2154
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2155
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2156
}