src/aircraft_cmd.cpp
author tron
Tue, 20 Feb 2007 06:39:09 +0000
changeset 6087 bfa159e740d9
parent 6037 71545e66b9c3
child 6099 d7d8e108ea2c
permissions -rw-r--r--
(svn r8822) -Fix

Variable scope, const
2186
db48cf29b983 (svn r2701) Insert Id tags into all source files
tron
parents: 2163
diff changeset
     1
/* $Id$ */
db48cf29b983 (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
862800791170 (svn r2397) - CodeChange: rename all "ttd" files to "openttd" files.
Darkvater
parents: 1877
diff changeset
     4
#include "openttd.h"
3963
6cab57074a9a (svn r5124) Add IsAircraftInHangar{Stopped,}(), which supersedes CheckStoppedInHangar()
tron
parents: 3955
diff changeset
     5
#include "aircraft.h"
1299
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents: 1282
diff changeset
     6
#include "debug.h"
2163
b17b313113a0 (svn r2673) Include functions.h directly, not globally via openttd.h
tron
parents: 2140
diff changeset
     7
#include "functions.h"
3315
c9d989783c55 (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
04b5403aaf6b (svn r815) Include strings.h only in the files which need it.
tron
parents: 458
diff changeset
     9
#include "table/strings.h"
679
04ca2cd69420 (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
2e00193652b2 (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
47963a0cfca3 (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
cbe0c766c947 (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"
5854
9eee280920f0 (svn r8428) -Codechange: Add proper names to aircraft subtypes instead of magic numbers and add a function IsNormalAircraft() which tells us whether the aircraft is in fact some flying device or a rotor/shadow.
Darkvater
parents: 5754
diff changeset
    20
#include "aircraft.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    21
#include "airport.h"
1752
d65cd19f7117 (svn r2256) - Fix: Trains cannot find a depot when they are in a tunnel. (glx)
matthijs
parents: 1614
diff changeset
    22
#include "vehicle_gui.h"
2517
6e3832ddd0f6 (svn r3046) Replace some numbers with sprite names and fix a typo in the sprite list
tron
parents: 2504
diff changeset
    23
#include "table/sprites.h"
2962
f0a49b646c48 (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
    24
#include "newgrf_engine.h"
3973
9d3cd1ed6ac0 (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
    25
#include "newgrf_callbacks.h"
4244
d40c73c55357 (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
    26
#include "newgrf_text.h"
4656
9c1d8c4d3e60 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4574
diff changeset
    27
#include "newgrf_sound.h"
4261
28670f743746 (svn r5887) -Cleanup: move date related functions, defines and variables to date.[ch]
rubidium
parents: 4259
diff changeset
    28
#include "date.h"
5972
59953719a3ff (svn r8661) -Fix: [depot windows] Enlarge the blocks in the depot window if a sprite is too big to fit (ships and aircraft only)
bjarni
parents: 5952
diff changeset
    29
#include "spritecache.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    30
5754
086187883012 (svn r8305) -Codechange: defining 'static const' in a header is a bit weird.
truelight
parents: 5668
diff changeset
    31
// this maps the terminal to its corresponding state and block flag
086187883012 (svn r8305) -Codechange: defining 'static const' in a header is a bit weird.
truelight
parents: 5668
diff changeset
    32
// currently set for 10 terms, 4 helipads
086187883012 (svn r8305) -Codechange: defining 'static const' in a header is a bit weird.
truelight
parents: 5668
diff changeset
    33
static const byte _airport_terminal_state[] = {2, 3, 4, 5, 6, 7, 19, 20, 0, 0, 8, 9, 21, 22};
086187883012 (svn r8305) -Codechange: defining 'static const' in a header is a bit weird.
truelight
parents: 5668
diff changeset
    34
static const byte _airport_terminal_flag[] =  {0, 1, 2, 3, 4, 5, 22, 23, 0, 0, 6, 7, 24, 25};
086187883012 (svn r8305) -Codechange: defining 'static const' in a header is a bit weird.
truelight
parents: 5668
diff changeset
    35
4842
79c4c9167d93 (svn r6768) -Cleanup: For airports change *Airport to *apc (variable naming coding style
Darkvater
parents: 4739
diff changeset
    36
static bool AirportMove(Vehicle *v, const AirportFTAClass *apc);
6087
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
    37
static bool AirportSetBlocks(Vehicle *v, const AirportFTA *current_pos, const AirportFTAClass *apc);
4843
4bef85baee3f (svn r6769) -Codechange: Minor syntax, const correctness, variable localization, coding
Darkvater
parents: 4842
diff changeset
    38
static bool AirportHasBlock(Vehicle *v, const AirportFTA *current_pos, const AirportFTAClass *apc);
4842
79c4c9167d93 (svn r6768) -Cleanup: For airports change *Airport to *apc (variable naming coding style
Darkvater
parents: 4739
diff changeset
    39
static bool AirportFindFreeTerminal(Vehicle *v, const AirportFTAClass *apc);
79c4c9167d93 (svn r6768) -Cleanup: For airports change *Airport to *apc (variable naming coding style
Darkvater
parents: 4739
diff changeset
    40
static bool AirportFindFreeHelipad(Vehicle *v, const AirportFTAClass *apc);
79c4c9167d93 (svn r6768) -Cleanup: For airports change *Airport to *apc (variable naming coding style
Darkvater
parents: 4739
diff changeset
    41
static void AirportGoToNextPosition(Vehicle *v, const AirportFTAClass *apc);
899
69db9cb6bc9d (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
    42
static void CrashAirplane(Vehicle *v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    43
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    44
static void AircraftNextAirportPos_and_Order(Vehicle *v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    45
static byte GetAircraftFlyingAltitude(const Vehicle *v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    46
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    47
static const SpriteID _aircraft_sprite[] = {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    48
	0x0EB5, 0x0EBD, 0x0EC5, 0x0ECD,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    49
	0x0ED5, 0x0EDD, 0x0E9D, 0x0EA5,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    50
	0x0EAD, 0x0EE5, 0x0F05, 0x0F0D,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    51
	0x0F15, 0x0F1D, 0x0F25, 0x0F2D,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    52
	0x0EED, 0x0EF5, 0x0EFD, 0x0F35,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    53
	0x0E9D, 0x0EA5, 0x0EAD, 0x0EB5,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    54
	0x0EBD, 0x0EC5
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    55
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    56
3710
44cf97e29c18 (svn r4653) - NewGRF: enable use of custom helicopter rotors in game (but not GUI windows yet) (mart3p)
peter1138
parents: 3701
diff changeset
    57
/* Helicopter rotor animation states */
44cf97e29c18 (svn r4653) - NewGRF: enable use of custom helicopter rotors in game (but not GUI windows yet) (mart3p)
peter1138
parents: 3701
diff changeset
    58
enum HelicopterRotorStates {
44cf97e29c18 (svn r4653) - NewGRF: enable use of custom helicopter rotors in game (but not GUI windows yet) (mart3p)
peter1138
parents: 3701
diff changeset
    59
	HRS_ROTOR_STOPPED,
44cf97e29c18 (svn r4653) - NewGRF: enable use of custom helicopter rotors in game (but not GUI windows yet) (mart3p)
peter1138
parents: 3701
diff changeset
    60
	HRS_ROTOR_MOVING_1,
44cf97e29c18 (svn r4653) - NewGRF: enable use of custom helicopter rotors in game (but not GUI windows yet) (mart3p)
peter1138
parents: 3701
diff changeset
    61
	HRS_ROTOR_MOVING_2,
44cf97e29c18 (svn r4653) - NewGRF: enable use of custom helicopter rotors in game (but not GUI windows yet) (mart3p)
peter1138
parents: 3701
diff changeset
    62
	HRS_ROTOR_MOVING_3,
44cf97e29c18 (svn r4653) - NewGRF: enable use of custom helicopter rotors in game (but not GUI windows yet) (mart3p)
peter1138
parents: 3701
diff changeset
    63
};
44cf97e29c18 (svn r4653) - NewGRF: enable use of custom helicopter rotors in game (but not GUI windows yet) (mart3p)
peter1138
parents: 3701
diff changeset
    64
1532
a69fdc89b5fc (svn r2036) Fix FindNearestHanger(), it never worked correctly because 0xFFFF != 65000 and a 16bit number has no 17th bit
tron
parents: 1530
diff changeset
    65
/* Find the nearest hangar to v
a69fdc89b5fc (svn r2036) Fix FindNearestHanger(), it never worked correctly because 0xFFFF != 65000 and a 16bit number has no 17th bit
tron
parents: 1530
diff changeset
    66
 * INVALID_STATION is returned, if the player does not have any suitable
a69fdc89b5fc (svn r2036) Fix FindNearestHanger(), it never worked correctly because 0xFFFF != 65000 and a 16bit number has no 17th bit
tron
parents: 1530
diff changeset
    67
 * airports (like helipads only)
a69fdc89b5fc (svn r2036) Fix FindNearestHanger(), it never worked correctly because 0xFFFF != 65000 and a 16bit number has no 17th bit
tron
parents: 1530
diff changeset
    68
 */
1793
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
    69
static StationID FindNearestHangar(const Vehicle *v)
1194
2b25b1477623 (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
    70
{
1532
a69fdc89b5fc (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
	const Station *st;
a69fdc89b5fc (svn r2036) Fix FindNearestHanger(), it never worked correctly because 0xFFFF != 65000 and a 16bit number has no 17th bit
tron
parents: 1530
diff changeset
    72
	uint best = 0;
1793
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
    73
	StationID index = INVALID_STATION;
4284
ea9579c54725 (svn r5914) -Fix [FS#235]: Bugfix for errors in FindNearestHangar function in aircraft_cmd.c (mart3p).
Darkvater
parents: 4261
diff changeset
    74
	TileIndex vtile = TileVirtXY(v->x_pos, v->y_pos);
1194
2b25b1477623 (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
    75
2b25b1477623 (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
    76
	FOR_ALL_STATIONS(st) {
6025
414b34699e87 (svn r8748) -Fix
tron
parents: 6024
diff changeset
    77
		if (st->owner != v->owner || !(st->facilities & FACIL_AIRPORT)) continue;
1204
747d1f630eda (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
    78
6032
fe6a454ac1ba (svn r8755) -Fix
tron
parents: 6027
diff changeset
    79
		const AirportFTAClass *afc = st->Airport();
6025
414b34699e87 (svn r8748) -Fix
tron
parents: 6024
diff changeset
    80
		if (afc->nof_depots == 0 || (
414b34699e87 (svn r8748) -Fix
tron
parents: 6024
diff changeset
    81
					/* don't crash the plane if we know it can't land at the airport */
414b34699e87 (svn r8748) -Fix
tron
parents: 6024
diff changeset
    82
					afc->flags & AirportFTAClass::SHORT_STRIP &&
414b34699e87 (svn r8748) -Fix
tron
parents: 6024
diff changeset
    83
					AircraftVehInfo(v->engine_type)->subtype & AIR_FAST &&
414b34699e87 (svn r8748) -Fix
tron
parents: 6024
diff changeset
    84
					!_cheats.no_jetcrash.value
414b34699e87 (svn r8748) -Fix
tron
parents: 6024
diff changeset
    85
				)) {
414b34699e87 (svn r8748) -Fix
tron
parents: 6024
diff changeset
    86
			continue;
414b34699e87 (svn r8748) -Fix
tron
parents: 6024
diff changeset
    87
		}
1532
a69fdc89b5fc (svn r2036) Fix FindNearestHanger(), it never worked correctly because 0xFFFF != 65000 and a 16bit number has no 17th bit
tron
parents: 1530
diff changeset
    88
6025
414b34699e87 (svn r8748) -Fix
tron
parents: 6024
diff changeset
    89
		// v->tile can't be used here, when aircraft is flying v->tile is set to 0
414b34699e87 (svn r8748) -Fix
tron
parents: 6024
diff changeset
    90
		uint distance = DistanceSquare(vtile, st->airport_tile);
414b34699e87 (svn r8748) -Fix
tron
parents: 6024
diff changeset
    91
		if (distance < best || index == INVALID_STATION) {
414b34699e87 (svn r8748) -Fix
tron
parents: 6024
diff changeset
    92
			best = distance;
414b34699e87 (svn r8748) -Fix
tron
parents: 6024
diff changeset
    93
			index = st->index;
1194
2b25b1477623 (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
		}
2b25b1477623 (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
	}
1532
a69fdc89b5fc (svn r2036) Fix FindNearestHanger(), it never worked correctly because 0xFFFF != 65000 and a 16bit number has no 17th bit
tron
parents: 1530
diff changeset
    96
	return index;
1194
2b25b1477623 (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
}
2b25b1477623 (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
1614
f6f2c918356d (svn r2118) - Fix: Fix compilation with network disabled, and comment out some of the warnings (unused function)
Darkvater
parents: 1533
diff changeset
    99
#if 0
1194
2b25b1477623 (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
   100
// returns true if vehicle v have an airport in the schedule, that has a hangar
2b25b1477623 (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
static bool HaveHangarInOrderList(Vehicle *v)
2b25b1477623 (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
{
2b25b1477623 (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
	const Order *order;
2b25b1477623 (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
2b25b1477623 (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
	FOR_VEHICLE_ORDERS(v, order) {
2b25b1477623 (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
		const Station *st = GetStation(order->station);
2b25b1477623 (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
   107
		if (st->owner == v->owner && st->facilities & FACIL_AIRPORT) {
4059
b1e1c1193f0a (svn r5346) - Feature: Add 4 new airports. 2 for aircraft, 2 for helicopters.
richk
parents: 4056
diff changeset
   108
			// If an airport doesn't have a hangar, skip it
6032
fe6a454ac1ba (svn r8755) -Fix
tron
parents: 6027
diff changeset
   109
			if (st->Airport()->nof_depots != 0)
1194
2b25b1477623 (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
   110
				return true;
2b25b1477623 (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
   111
		}
2b25b1477623 (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
   112
	}
2b25b1477623 (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
   113
2b25b1477623 (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
   114
	return false;
2b25b1477623 (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
   115
}
1614
f6f2c918356d (svn r2118) - Fix: Fix compilation with network disabled, and comment out some of the warnings (unused function)
Darkvater
parents: 1533
diff changeset
   116
#endif
1194
2b25b1477623 (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
   117
3157
3f35e2d9c8e3 (svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3140
diff changeset
   118
int GetAircraftImage(const Vehicle* v, Direction direction)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   119
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   120
	int spritenum = v->spritenum;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   121
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   122
	if (is_custom_sprite(spritenum)) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   123
		int sprite = GetCustomVehicleSprite(v, direction);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   124
2989
916f9443345f (svn r3564) Several smaller changes:
tron
parents: 2962
diff changeset
   125
		if (sprite != 0) return sprite;
2464
e3054f0f0627 (svn r2990) Remove _engine_original_sprites and replace all uses with references to our (new) original engine/vehicle tables.
peter1138
parents: 2423
diff changeset
   126
		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
   127
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   128
	return direction + _aircraft_sprite[spritenum];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   129
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   130
3710
44cf97e29c18 (svn r4653) - NewGRF: enable use of custom helicopter rotors in game (but not GUI windows yet) (mart3p)
peter1138
parents: 3701
diff changeset
   131
SpriteID GetRotorImage(const Vehicle *v)
44cf97e29c18 (svn r4653) - NewGRF: enable use of custom helicopter rotors in game (but not GUI windows yet) (mart3p)
peter1138
parents: 3701
diff changeset
   132
{
5854
9eee280920f0 (svn r8428) -Codechange: Add proper names to aircraft subtypes instead of magic numbers and add a function IsNormalAircraft() which tells us whether the aircraft is in fact some flying device or a rotor/shadow.
Darkvater
parents: 5754
diff changeset
   133
	assert(v->subtype == AIR_HELICOPTER);
3710
44cf97e29c18 (svn r4653) - NewGRF: enable use of custom helicopter rotors in game (but not GUI windows yet) (mart3p)
peter1138
parents: 3701
diff changeset
   134
6087
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
   135
	const Vehicle *w = v->next->next;
3710
44cf97e29c18 (svn r4653) - NewGRF: enable use of custom helicopter rotors in game (but not GUI windows yet) (mart3p)
peter1138
parents: 3701
diff changeset
   136
	if (is_custom_sprite(v->spritenum)) {
4056
7899dc230a12 (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
   137
		SpriteID spritenum = GetCustomRotorSprite(v, false);
3710
44cf97e29c18 (svn r4653) - NewGRF: enable use of custom helicopter rotors in game (but not GUI windows yet) (mart3p)
peter1138
parents: 3701
diff changeset
   138
		if (spritenum != 0) return spritenum;
44cf97e29c18 (svn r4653) - NewGRF: enable use of custom helicopter rotors in game (but not GUI windows yet) (mart3p)
peter1138
parents: 3701
diff changeset
   139
	}
44cf97e29c18 (svn r4653) - NewGRF: enable use of custom helicopter rotors in game (but not GUI windows yet) (mart3p)
peter1138
parents: 3701
diff changeset
   140
44cf97e29c18 (svn r4653) - NewGRF: enable use of custom helicopter rotors in game (but not GUI windows yet) (mart3p)
peter1138
parents: 3701
diff changeset
   141
	/* Return standard rotor sprites if there are no custom sprites for this helicopter */
44cf97e29c18 (svn r4653) - NewGRF: enable use of custom helicopter rotors in game (but not GUI windows yet) (mart3p)
peter1138
parents: 3701
diff changeset
   142
	return SPR_ROTOR_STOPPED + w->u.air.state;
44cf97e29c18 (svn r4653) - NewGRF: enable use of custom helicopter rotors in game (but not GUI windows yet) (mart3p)
peter1138
parents: 3701
diff changeset
   143
}
44cf97e29c18 (svn r4653) - NewGRF: enable use of custom helicopter rotors in game (but not GUI windows yet) (mart3p)
peter1138
parents: 3701
diff changeset
   144
5668
36b39f4a9032 (svn r8128) -Codechange: Split sprite and palette remap into separate 32 bit values.
peter1138
parents: 5651
diff changeset
   145
void DrawAircraftEngine(int x, int y, EngineID engine, SpriteID pal)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   146
{
4023
5d84c056a2b1 (svn r5262) Add symbolic names for the aircraft subtypes. not perfect, but better than raw numbers
tron
parents: 3988
diff changeset
   147
	const AircraftVehicleInfo* avi = AircraftVehInfo(engine);
5d84c056a2b1 (svn r5262) Add symbolic names for the aircraft subtypes. not perfect, but better than raw numbers
tron
parents: 3988
diff changeset
   148
	int spritenum = avi->image_index;
5668
36b39f4a9032 (svn r8128) -Codechange: Split sprite and palette remap into separate 32 bit values.
peter1138
parents: 5651
diff changeset
   149
	SpriteID sprite = (6 + _aircraft_sprite[spritenum]);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   150
381
527e47c8feec (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
	if (is_custom_sprite(spritenum)) {
3186
4317b5b295c4 (svn r3832) Replace some magic numbers by (Diag)Direction enums
tron
parents: 3183
diff changeset
   152
		sprite = GetCustomVehicleIcon(engine, DIR_W);
5012
d59cca2ac433 (svn r7032) - Fix (r1704): Sprite index wasn't updated with correct image number if
peter1138
parents: 4991
diff changeset
   153
		if (sprite == 0) {
2464
e3054f0f0627 (svn r2990) Remove _engine_original_sprites and replace all uses with references to our (new) original engine/vehicle tables.
peter1138
parents: 2423
diff changeset
   154
			spritenum = orig_aircraft_vehicle_info[engine - AIRCRAFT_ENGINES_INDEX].image_index;
5012
d59cca2ac433 (svn r7032) - Fix (r1704): Sprite index wasn't updated with correct image number if
peter1138
parents: 4991
diff changeset
   155
			sprite = (6 + _aircraft_sprite[spritenum]);
d59cca2ac433 (svn r7032) - Fix (r1704): Sprite index wasn't updated with correct image number if
peter1138
parents: 4991
diff changeset
   156
		}
381
527e47c8feec (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
   157
	}
527e47c8feec (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
   158
5668
36b39f4a9032 (svn r8128) -Codechange: Split sprite and palette remap into separate 32 bit values.
peter1138
parents: 5651
diff changeset
   159
	DrawSprite(sprite, pal, x, y);
381
527e47c8feec (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
   160
4023
5d84c056a2b1 (svn r5262) Add symbolic names for the aircraft subtypes. not perfect, but better than raw numbers
tron
parents: 3988
diff changeset
   161
	if (!(avi->subtype & AIR_CTOL)) {
3710
44cf97e29c18 (svn r4653) - NewGRF: enable use of custom helicopter rotors in game (but not GUI windows yet) (mart3p)
peter1138
parents: 3701
diff changeset
   162
		SpriteID rotor_sprite = GetCustomRotorIcon(engine);
44cf97e29c18 (svn r4653) - NewGRF: enable use of custom helicopter rotors in game (but not GUI windows yet) (mart3p)
peter1138
parents: 3701
diff changeset
   163
		if (rotor_sprite == 0) rotor_sprite = SPR_ROTOR_STOPPED;
5668
36b39f4a9032 (svn r8128) -Codechange: Split sprite and palette remap into separate 32 bit values.
peter1138
parents: 5651
diff changeset
   164
		DrawSprite(rotor_sprite, PAL_NONE, x, y - 5);
1200
d5ef134866de (svn r1704) - Fix: [newgrf] rotors of custom helicopters are displayed correctly in the hangar window
dominik
parents: 1197
diff changeset
   165
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   166
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   167
5972
59953719a3ff (svn r8661) -Fix: [depot windows] Enlarge the blocks in the depot window if a sprite is too big to fit (ships and aircraft only)
bjarni
parents: 5952
diff changeset
   168
/** Get the size of the sprite of an aircraft sprite heading west (used for lists)
59953719a3ff (svn r8661) -Fix: [depot windows] Enlarge the blocks in the depot window if a sprite is too big to fit (ships and aircraft only)
bjarni
parents: 5952
diff changeset
   169
 * @param engine The engine to get the sprite from
59953719a3ff (svn r8661) -Fix: [depot windows] Enlarge the blocks in the depot window if a sprite is too big to fit (ships and aircraft only)
bjarni
parents: 5952
diff changeset
   170
 * @param &width The width of the sprite
59953719a3ff (svn r8661) -Fix: [depot windows] Enlarge the blocks in the depot window if a sprite is too big to fit (ships and aircraft only)
bjarni
parents: 5952
diff changeset
   171
 * @param &height The height of the sprite
59953719a3ff (svn r8661) -Fix: [depot windows] Enlarge the blocks in the depot window if a sprite is too big to fit (ships and aircraft only)
bjarni
parents: 5952
diff changeset
   172
 */
59953719a3ff (svn r8661) -Fix: [depot windows] Enlarge the blocks in the depot window if a sprite is too big to fit (ships and aircraft only)
bjarni
parents: 5952
diff changeset
   173
void GetAircraftSpriteSize(EngineID engine, uint &width, uint &height)
59953719a3ff (svn r8661) -Fix: [depot windows] Enlarge the blocks in the depot window if a sprite is too big to fit (ships and aircraft only)
bjarni
parents: 5952
diff changeset
   174
{
59953719a3ff (svn r8661) -Fix: [depot windows] Enlarge the blocks in the depot window if a sprite is too big to fit (ships and aircraft only)
bjarni
parents: 5952
diff changeset
   175
	const AircraftVehicleInfo* avi = AircraftVehInfo(engine);
59953719a3ff (svn r8661) -Fix: [depot windows] Enlarge the blocks in the depot window if a sprite is too big to fit (ships and aircraft only)
bjarni
parents: 5952
diff changeset
   176
	int spritenum = avi->image_index;
59953719a3ff (svn r8661) -Fix: [depot windows] Enlarge the blocks in the depot window if a sprite is too big to fit (ships and aircraft only)
bjarni
parents: 5952
diff changeset
   177
	SpriteID sprite = (6 + _aircraft_sprite[spritenum]);
59953719a3ff (svn r8661) -Fix: [depot windows] Enlarge the blocks in the depot window if a sprite is too big to fit (ships and aircraft only)
bjarni
parents: 5952
diff changeset
   178
59953719a3ff (svn r8661) -Fix: [depot windows] Enlarge the blocks in the depot window if a sprite is too big to fit (ships and aircraft only)
bjarni
parents: 5952
diff changeset
   179
	if (is_custom_sprite(spritenum)) {
59953719a3ff (svn r8661) -Fix: [depot windows] Enlarge the blocks in the depot window if a sprite is too big to fit (ships and aircraft only)
bjarni
parents: 5952
diff changeset
   180
		sprite = GetCustomVehicleIcon(engine, DIR_W);
59953719a3ff (svn r8661) -Fix: [depot windows] Enlarge the blocks in the depot window if a sprite is too big to fit (ships and aircraft only)
bjarni
parents: 5952
diff changeset
   181
		if (sprite == 0) {
59953719a3ff (svn r8661) -Fix: [depot windows] Enlarge the blocks in the depot window if a sprite is too big to fit (ships and aircraft only)
bjarni
parents: 5952
diff changeset
   182
			spritenum = orig_aircraft_vehicle_info[engine - AIRCRAFT_ENGINES_INDEX].image_index;
59953719a3ff (svn r8661) -Fix: [depot windows] Enlarge the blocks in the depot window if a sprite is too big to fit (ships and aircraft only)
bjarni
parents: 5952
diff changeset
   183
			sprite = (6 + _aircraft_sprite[spritenum]);
59953719a3ff (svn r8661) -Fix: [depot windows] Enlarge the blocks in the depot window if a sprite is too big to fit (ships and aircraft only)
bjarni
parents: 5952
diff changeset
   184
		}
59953719a3ff (svn r8661) -Fix: [depot windows] Enlarge the blocks in the depot window if a sprite is too big to fit (ships and aircraft only)
bjarni
parents: 5952
diff changeset
   185
	}
59953719a3ff (svn r8661) -Fix: [depot windows] Enlarge the blocks in the depot window if a sprite is too big to fit (ships and aircraft only)
bjarni
parents: 5952
diff changeset
   186
59953719a3ff (svn r8661) -Fix: [depot windows] Enlarge the blocks in the depot window if a sprite is too big to fit (ships and aircraft only)
bjarni
parents: 5952
diff changeset
   187
	const Sprite *spr = GetSprite(sprite);
59953719a3ff (svn r8661) -Fix: [depot windows] Enlarge the blocks in the depot window if a sprite is too big to fit (ships and aircraft only)
bjarni
parents: 5952
diff changeset
   188
59953719a3ff (svn r8661) -Fix: [depot windows] Enlarge the blocks in the depot window if a sprite is too big to fit (ships and aircraft only)
bjarni
parents: 5952
diff changeset
   189
	width  = spr->width ;
59953719a3ff (svn r8661) -Fix: [depot windows] Enlarge the blocks in the depot window if a sprite is too big to fit (ships and aircraft only)
bjarni
parents: 5952
diff changeset
   190
	height = spr->height;
59953719a3ff (svn r8661) -Fix: [depot windows] Enlarge the blocks in the depot window if a sprite is too big to fit (ships and aircraft only)
bjarni
parents: 5952
diff changeset
   191
}
59953719a3ff (svn r8661) -Fix: [depot windows] Enlarge the blocks in the depot window if a sprite is too big to fit (ships and aircraft only)
bjarni
parents: 5952
diff changeset
   192
5920
11848ca714f4 (svn r8548) -Fix
tron
parents: 5919
diff changeset
   193
static int32 EstimateAircraftCost(const AircraftVehicleInfo *avi)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   194
{
5920
11848ca714f4 (svn r8548) -Fix
tron
parents: 5919
diff changeset
   195
	return avi->base_cost * (_price.aircraft_base >> 3) >> 5;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   196
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   197
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   198
3973
9d3cd1ed6ac0 (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
   199
/**
9d3cd1ed6ac0 (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
   200
 * Calculates cargo capacity based on an aircraft's passenger
9d3cd1ed6ac0 (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
   201
 * and mail capacities.
9d3cd1ed6ac0 (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
   202
 * @param cid Which cargo type to calculate a capacity for.
9d3cd1ed6ac0 (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
   203
 * @param engine Which engine to find a cargo capacity for.
9d3cd1ed6ac0 (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
   204
 * @return New cargo capacity value.
9d3cd1ed6ac0 (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
   205
 */
6087
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
   206
uint16 AircraftDefaultCargoCapacity(CargoID cid, const AircraftVehicleInfo *avi)
3973
9d3cd1ed6ac0 (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
   207
{
9d3cd1ed6ac0 (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
   208
	assert(cid != CT_INVALID);
9d3cd1ed6ac0 (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
   209
9d3cd1ed6ac0 (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
   210
	/* An aircraft can carry twice as much goods as normal cargo,
9d3cd1ed6ac0 (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
   211
	 * and four times as many passengers. */
9d3cd1ed6ac0 (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
   212
	switch (cid) {
9d3cd1ed6ac0 (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
   213
		case CT_PASSENGERS:
9d3cd1ed6ac0 (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
   214
			return avi->passenger_capacity;
9d3cd1ed6ac0 (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
   215
		case CT_MAIL:
9d3cd1ed6ac0 (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
   216
			return avi->passenger_capacity + avi->mail_capacity;
9d3cd1ed6ac0 (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
   217
		case CT_GOODS:
9d3cd1ed6ac0 (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
   218
			return (avi->passenger_capacity + avi->mail_capacity) / 2;
9d3cd1ed6ac0 (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
   219
		default:
9d3cd1ed6ac0 (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
   220
			return (avi->passenger_capacity + avi->mail_capacity) / 4;
9d3cd1ed6ac0 (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
   221
	}
9d3cd1ed6ac0 (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
   222
}
9d3cd1ed6ac0 (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
   223
9d3cd1ed6ac0 (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
   224
1793
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   225
/** Build an aircraft.
3491
35d747bb5e82 (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
   226
 * @param tile tile of depot where aircraft is built
1793
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   227
 * @param p1 aircraft type being built (engine)
3816
28ee8b8c2522 (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
   228
 * @param p2 bit 0 when set, the unitnumber will be 0, otherwise it will be a free number
1793
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   229
 */
3491
35d747bb5e82 (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
   230
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
   231
{
5215
b8fb685df4c5 (svn r7330) -Fix (r7304): Data invalidation doesn't always happen as the local
peter1138
parents: 5211
diff changeset
   232
	if (!IsEngineBuildable(p1, VEH_Aircraft, _current_player)) return_cmd_error(STR_ENGINE_NOT_BUILDABLE);
1196
67f7f3017d99 (svn r1700) - Fix: Hacked clients can no longer be used to build vehicles that are not available yet (Hackykid)
bjarni
parents: 1195
diff changeset
   233
5920
11848ca714f4 (svn r8548) -Fix
tron
parents: 5919
diff changeset
   234
	const AircraftVehicleInfo *avi = AircraftVehInfo(p1);
11848ca714f4 (svn r8548) -Fix
tron
parents: 5919
diff changeset
   235
	int32 value = EstimateAircraftCost(avi);
1226
f0cb2c427b84 (svn r1730) - Fix: Ships and Aircrafts have to be build in depots owned by _current_player (hacked client protection)
bjarni
parents: 1209
diff changeset
   236
2423
4266590b549d (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
   237
	// to just query the cost, it is not neccessary to have a valid tile (automation/AI)
4266590b549d (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
   238
	if (flags & DC_QUERY_COST) return value;
4266590b549d (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
   239
3338
7cff208f0446 (svn r4120) Use the new station functions where appropriate
tron
parents: 3315
diff changeset
   240
	if (!IsHangarTile(tile) || !IsTileOwner(tile, _current_player)) return CMD_ERROR;
1226
f0cb2c427b84 (svn r1730) - Fix: Ships and Aircrafts have to be build in depots owned by _current_player (hacked client protection)
bjarni
parents: 1209
diff changeset
   241
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   242
	SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   243
4161
60e38778d90b (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
   244
	// Prevent building aircraft types at places which can't handle them
5919
8eff39a70965 (svn r8547) -Fix
tron
parents: 5908
diff changeset
   245
	const Station* st = GetStationByTile(tile);
6032
fe6a454ac1ba (svn r8755) -Fix
tron
parents: 6027
diff changeset
   246
	const AirportFTAClass* apc = st->Airport();
6027
ff15b8c7c087 (svn r8750) -Fix (r8747): PLANES defined in wingdi.h caused compilation error under Win32. Changed to AIRPLANES
KUDr
parents: 6025
diff changeset
   247
	if (!(apc->flags & (avi->subtype & AIR_CTOL ? AirportFTAClass::AIRPLANES : AirportFTAClass::HELICOPTERS))) {
4161
60e38778d90b (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
   248
		return CMD_ERROR;
60e38778d90b (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
   249
	}
60e38778d90b (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
   250
5854
9eee280920f0 (svn r8428) -Codechange: Add proper names to aircraft subtypes instead of magic numbers and add a function IsNormalAircraft() which tells us whether the aircraft is in fact some flying device or a rotor/shadow.
Darkvater
parents: 5754
diff changeset
   251
	/* Allocate 2 or 3 vehicle structs, depending on type
9eee280920f0 (svn r8428) -Codechange: Add proper names to aircraft subtypes instead of magic numbers and add a function IsNormalAircraft() which tells us whether the aircraft is in fact some flying device or a rotor/shadow.
Darkvater
parents: 5754
diff changeset
   252
	 * vl[0] = aircraft, vl[1] = shadow, [vl[2] = rotor] */
6087
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
   253
	Vehicle *vl[3];
5922
caa040c9e25d (svn r8550) -Fix
tron
parents: 5921
diff changeset
   254
	if (!AllocateVehicles(vl, avi->subtype & AIR_CTOL ? 2 : 3)) {
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   255
		return_cmd_error(STR_00E1_TOO_MANY_VEHICLES_IN_GAME);
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   256
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   257
6087
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
   258
	UnitID 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
   259
	if (unit_num > _patches.max_aircraft)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   260
		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
   261
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   262
	if (flags & DC_EXEC) {
5854
9eee280920f0 (svn r8428) -Codechange: Add proper names to aircraft subtypes instead of magic numbers and add a function IsNormalAircraft() which tells us whether the aircraft is in fact some flying device or a rotor/shadow.
Darkvater
parents: 5754
diff changeset
   263
		Vehicle *v = vl[0]; // aircraft
9eee280920f0 (svn r8428) -Codechange: Add proper names to aircraft subtypes instead of magic numbers and add a function IsNormalAircraft() which tells us whether the aircraft is in fact some flying device or a rotor/shadow.
Darkvater
parents: 5754
diff changeset
   264
		Vehicle *u = vl[1]; // shadow
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   265
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   266
		v->unitnumber = unit_num;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   267
		v->type = u->type = VEH_Aircraft;
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5585
diff changeset
   268
		v->direction = DIR_SE;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   269
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   270
		v->owner = u->owner = _current_player;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   271
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   272
		v->tile = tile;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   273
//		u->tile = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   274
6087
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
   275
		uint x = TileX(tile) * TILE_SIZE + 5;
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
   276
		uint y = TileY(tile) * TILE_SIZE + 3;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   277
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   278
		v->x_pos = u->x_pos = x;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   279
		v->y_pos = u->y_pos = y;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   280
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   281
		u->z_pos = GetSlopeZ(x, y);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   282
		v->z_pos = u->z_pos + 1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   283
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   284
		v->x_offs = v->y_offs = -1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   285
//		u->delta_x = u->delta_y = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   286
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   287
		v->sprite_width = v->sprite_height = 2;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   288
		v->z_height = 5;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   289
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   290
		u->sprite_width = u->sprite_height = 2;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   291
		u->z_height = 1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   292
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   293
		v->vehstatus = VS_HIDDEN | VS_STOPPED | VS_DEFPAL;
4175
b79ec8a94d4e (svn r5613) Rename VS_DISASTER to VS_SHADOW, because this is what this flag actually means
tron
parents: 4161
diff changeset
   294
		u->vehstatus = VS_HIDDEN | VS_UNCLICKABLE | VS_SHADOW;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   295
538
24fdb517fbe5 (svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents: 534
diff changeset
   296
		v->spritenum = avi->image_index;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   297
//		v->cargo_count = u->number_of_pieces = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   298
922
10035216cbaf (svn r1410) Replaced all occurences of 'passanger' by 'passenger' in the code
celestar
parents: 919
diff changeset
   299
		v->cargo_cap = avi->passenger_capacity;
538
24fdb517fbe5 (svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents: 534
diff changeset
   300
		u->cargo_cap = avi->mail_capacity;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   301
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   302
		v->cargo_type = CT_PASSENGERS;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   303
		u->cargo_type = CT_MAIL;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   304
3870
d9ebc4ea750a (svn r4910) - NewGRF: add and initialize cargo subtype for vehicle visual variations
peter1138
parents: 3816
diff changeset
   305
		v->cargo_subtype = 0;
d9ebc4ea750a (svn r4910) - NewGRF: add and initialize cargo subtype for vehicle visual variations
peter1138
parents: 3816
diff changeset
   306
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   307
		v->string_id = STR_SV_AIRCRAFT_NAME;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   308
//		v->next_order_param = v->next_order = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   309
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   310
//		v->load_unload_time_rem = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   311
//		v->progress = 0;
1266
eccd576e322f (svn r1770) -Fix: Hopefully last pieces of code that are containing a station-id
truelight
parents: 1263
diff changeset
   312
		v->last_station_visited = INVALID_STATION;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   313
//		v->destination_coords = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   314
538
24fdb517fbe5 (svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents: 534
diff changeset
   315
		v->max_speed = avi->max_speed;
24fdb517fbe5 (svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents: 534
diff changeset
   316
		v->acceleration = avi->acceleration;
2477
225b2916fe2a (svn r3003) Change int, uint, uin16 and byte to EngineID where appropriate, plus some related changes (mostly casts)
tron
parents: 2475
diff changeset
   317
		v->engine_type = p1;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   318
5854
9eee280920f0 (svn r8428) -Codechange: Add proper names to aircraft subtypes instead of magic numbers and add a function IsNormalAircraft() which tells us whether the aircraft is in fact some flying device or a rotor/shadow.
Darkvater
parents: 5754
diff changeset
   319
		v->subtype = (avi->subtype & AIR_CTOL ? AIR_AIRCRAFT : AIR_HELICOPTER);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   320
		v->value = value;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   321
5854
9eee280920f0 (svn r8428) -Codechange: Add proper names to aircraft subtypes instead of magic numbers and add a function IsNormalAircraft() which tells us whether the aircraft is in fact some flying device or a rotor/shadow.
Darkvater
parents: 5754
diff changeset
   322
		u->subtype = AIR_SHADOW;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   323
3973
9d3cd1ed6ac0 (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
		/* Danger, Will Robinson!
9d3cd1ed6ac0 (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
		 * If the aircraft is refittable, but cannot be refitted to
9d3cd1ed6ac0 (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
		 * passengers, we select the cargo type from the refit mask.
9d3cd1ed6ac0 (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
		 * This is a fairly nasty hack to get around the fact that TTD
9d3cd1ed6ac0 (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
   328
		 * has no default cargo type specifier for planes... */
6087
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
   329
		CargoID cargo = FindFirstRefittableCargo(p1);
3973
9d3cd1ed6ac0 (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
   330
		if (cargo != CT_INVALID && cargo != CT_PASSENGERS) {
9d3cd1ed6ac0 (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
   331
			uint16 callback = CALLBACK_FAILED;
9d3cd1ed6ac0 (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
   332
9d3cd1ed6ac0 (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
   333
			v->cargo_type = cargo;
9d3cd1ed6ac0 (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
   334
9d3cd1ed6ac0 (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
   335
			if (HASBIT(EngInfo(p1)->callbackmask, CBM_REFIT_CAPACITY)) {
9d3cd1ed6ac0 (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
   336
				callback = GetVehicleCallback(CBID_VEHICLE_REFIT_CAPACITY, 0, 0, v->engine_type, v);
9d3cd1ed6ac0 (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
   337
			}
9d3cd1ed6ac0 (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
   338
9d3cd1ed6ac0 (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
   339
			if (callback == CALLBACK_FAILED) {
9d3cd1ed6ac0 (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
   340
				/* Callback failed, or not executed; use the default cargo capacity */
6087
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
   341
				v->cargo_cap = AircraftDefaultCargoCapacity(v->cargo_type, avi);
3973
9d3cd1ed6ac0 (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
   342
			} else {
9d3cd1ed6ac0 (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
   343
				v->cargo_cap = callback;
9d3cd1ed6ac0 (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
   344
			}
9d3cd1ed6ac0 (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
   345
9d3cd1ed6ac0 (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
   346
			/* Set the 'second compartent' capacity to none */
9d3cd1ed6ac0 (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
   347
			u->cargo_cap = 0;
9d3cd1ed6ac0 (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
   348
		}
9d3cd1ed6ac0 (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
   349
5854
9eee280920f0 (svn r8428) -Codechange: Add proper names to aircraft subtypes instead of magic numbers and add a function IsNormalAircraft() which tells us whether the aircraft is in fact some flying device or a rotor/shadow.
Darkvater
parents: 5754
diff changeset
   350
		const Engine *e = GetEngine(p1);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   351
		v->reliability = e->reliability;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   352
		v->reliability_spd_dec = e->reliability_spd_dec;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   353
		v->max_age = e->lifelength * 366;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   354
2564
24533c2db978 (svn r3101) -Codechange: added _new_vehicle_id
bjarni
parents: 2561
diff changeset
   355
		_new_vehicle_id = v->index;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   356
2423
4266590b549d (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
   357
		/* When we click on hangar we know the tile it is on. By that we know
4266590b549d (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
   358
		 * its position in the array of depots the airport has.....we can search
4266590b549d (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
   359
		 * layout for #th position of depot. Since layout must start with a listing
4266590b549d (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
   360
		 * of all depots, it is simple */
5921
7799711a9a23 (svn r8549) -Fix
tron
parents: 5920
diff changeset
   361
		for (uint i = 0;; i++) {
7799711a9a23 (svn r8549) -Fix
tron
parents: 5920
diff changeset
   362
			assert(i != apc->nof_depots);
7799711a9a23 (svn r8549) -Fix
tron
parents: 5920
diff changeset
   363
			if (st->airport_tile + ToTileIndexDiff(apc->airport_depots[i]) == tile) {
7799711a9a23 (svn r8549) -Fix
tron
parents: 5920
diff changeset
   364
				assert(apc->layout[i].heading == HANGAR);
7799711a9a23 (svn r8549) -Fix
tron
parents: 5920
diff changeset
   365
				v->u.air.pos = apc->layout[i].position;
7799711a9a23 (svn r8549) -Fix
tron
parents: 5920
diff changeset
   366
				break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   367
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   368
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   369
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   370
		v->u.air.state = HANGAR;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   371
		v->u.air.previous_pos = v->u.air.pos;
3315
c9d989783c55 (svn r4079) Add GetSation{Index,ByTile}() to get the station index resp. the station from a tile
tron
parents: 3186
diff changeset
   372
		v->u.air.targetairport = GetStationIndex(tile);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   373
		v->next = u;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   374
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   375
		v->service_interval = _patches.servint_aircraft;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   376
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   377
		v->date_of_last_service = _date;
4329
0e6e689f66e7 (svn r6002) -Cleanup: remove the now redundant BASE_YEAR constant.
rubidium
parents: 4293
diff changeset
   378
		v->build_year = u->build_year = _cur_year;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   379
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   380
		v->cur_image = u->cur_image = 0xEA0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   381
2804
ea4080819175 (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
   382
		v->random_bits = VehicleRandomBits();
ea4080819175 (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
   383
		u->random_bits = VehicleRandomBits();
ea4080819175 (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
   384
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   385
		VehiclePositionChanged(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   386
		VehiclePositionChanged(u);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   387
1793
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   388
		// Aircraft with 3 vehicles (chopper)?
5854
9eee280920f0 (svn r8428) -Codechange: Add proper names to aircraft subtypes instead of magic numbers and add a function IsNormalAircraft() which tells us whether the aircraft is in fact some flying device or a rotor/shadow.
Darkvater
parents: 5754
diff changeset
   389
		if (v->subtype == AIR_HELICOPTER) {
9eee280920f0 (svn r8428) -Codechange: Add proper names to aircraft subtypes instead of magic numbers and add a function IsNormalAircraft() which tells us whether the aircraft is in fact some flying device or a rotor/shadow.
Darkvater
parents: 5754
diff changeset
   390
			Vehicle *w = vl[2];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   391
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   392
			u->next = w;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   393
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   394
			w->type = VEH_Aircraft;
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5585
diff changeset
   395
			w->direction = DIR_N;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   396
			w->owner = _current_player;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   397
			w->x_pos = v->x_pos;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   398
			w->y_pos = v->y_pos;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   399
			w->z_pos = v->z_pos + 5;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   400
			w->x_offs = w->y_offs = -1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   401
			w->sprite_width = w->sprite_height = 2;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   402
			w->z_height = 1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   403
			w->vehstatus = VS_HIDDEN | VS_UNCLICKABLE;
3710
44cf97e29c18 (svn r4653) - NewGRF: enable use of custom helicopter rotors in game (but not GUI windows yet) (mart3p)
peter1138
parents: 3701
diff changeset
   404
			w->spritenum = 0xFF;
5854
9eee280920f0 (svn r8428) -Codechange: Add proper names to aircraft subtypes instead of magic numbers and add a function IsNormalAircraft() which tells us whether the aircraft is in fact some flying device or a rotor/shadow.
Darkvater
parents: 5754
diff changeset
   405
			w->subtype = AIR_ROTOR;
2517
6e3832ddd0f6 (svn r3046) Replace some numbers with sprite names and fix a typo in the sprite list
tron
parents: 2504
diff changeset
   406
			w->cur_image = SPR_ROTOR_STOPPED;
2804
ea4080819175 (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
   407
			w->random_bits = VehicleRandomBits();
3710
44cf97e29c18 (svn r4653) - NewGRF: enable use of custom helicopter rotors in game (but not GUI windows yet) (mart3p)
peter1138
parents: 3701
diff changeset
   408
			/* Use rotor's air.state to store the rotor animation frame */
44cf97e29c18 (svn r4653) - NewGRF: enable use of custom helicopter rotors in game (but not GUI windows yet) (mart3p)
peter1138
parents: 3701
diff changeset
   409
			w->u.air.state = HRS_ROTOR_STOPPED;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   410
			VehiclePositionChanged(w);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   411
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   412
4739
e626ef1b522e (svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents: 4732
diff changeset
   413
		InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
588
03521b270f62 (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);
2618
9691753519ac (svn r3156) -Fix: removed some cases where autoreplace windows were redrawn when nothing was changed
bjarni
parents: 2601
diff changeset
   416
		if (IsLocalPlayer())
5944
6d059cc8662b (svn r8610) -Codechange/Feature: rewrote the list handling in the autoreplace window
bjarni
parents: 5924
diff changeset
   417
			InvalidateAutoreplaceWindow(VEH_Aircraft); //updates the replace Aircraft window
6d059cc8662b (svn r8610) -Codechange/Feature: rewrote the list handling in the autoreplace window
bjarni
parents: 5924
diff changeset
   418
6d059cc8662b (svn r8610) -Codechange/Feature: rewrote the list handling in the autoreplace window
bjarni
parents: 5924
diff changeset
   419
		GetPlayer(_current_player)->num_engines[p1]++;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   420
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   421
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   422
	return value;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   423
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   424
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   425
410
0efd84450b01 (svn r607) -Patch: [ 985102 ] static cleanup
tron
parents: 381
diff changeset
   426
static void DoDeleteAircraft(Vehicle *v)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   427
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   428
	DeleteWindowById(WC_VEHICLE_VIEW, v->index);
588
03521b270f62 (svn r1009) -Feature: per-station vehicle lists
tron
parents: 578
diff changeset
   429
	RebuildVehicleLists();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   430
	InvalidateWindow(WC_COMPANY, v->owner);
5256
9056fd4b30f4 (svn r7386) -Codechange r7385: moved deletion of the vehicle highlight from DeleteVehicle to the sell commands as they are not called as often
bjarni
parents: 5215
diff changeset
   431
	DeleteDepotHighlightOfVehicle(v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   432
	DeleteVehicleChain(v);
1055
cc4f60cc9102 (svn r1556) -Fix: Vehicle list updates should now really work
Celestar
parents: 1053
diff changeset
   433
	InvalidateWindowClasses(WC_AIRCRAFT_LIST);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   434
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   435
1786
7cfd46c3fcc4 (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
   436
/** Sell an aircraft.
3491
35d747bb5e82 (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
   437
 * @param tile unused
1786
7cfd46c3fcc4 (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
   438
 * @param p1 vehicle ID to be sold
7cfd46c3fcc4 (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
   439
 * @param p2 unused
7cfd46c3fcc4 (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
   440
 */
3491
35d747bb5e82 (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
   441
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
   442
{
4352
6703cd8d39e0 (svn r6053) -Codechange: renamed all IsXXXIndex to IsValidXXXID
truelight
parents: 4351
diff changeset
   443
	if (!IsValidVehicleID(p1)) return CMD_ERROR;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   444
6087
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
   445
	Vehicle *v = GetVehicle(p1);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   446
3183
90c676e6a50d (svn r3829) Reduce the use of _error_message by directly returning error codes instead of using this global variable
tron
parents: 3160
diff changeset
   447
	if (v->type != VEH_Aircraft || !CheckOwnership(v->owner)) return CMD_ERROR;
3963
6cab57074a9a (svn r5124) Add IsAircraftInHangar{Stopped,}(), which supersedes CheckStoppedInHangar()
tron
parents: 3955
diff changeset
   448
	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
   449
1237
1c7a3f9c94b9 (svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents: 1235
diff changeset
   450
	SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES);
1c7a3f9c94b9 (svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents: 1235
diff changeset
   451
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   452
	if (flags & DC_EXEC) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   453
		// Invalidate depot
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   454
		InvalidateWindow(WC_VEHICLE_DEPOT, v->tile);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   455
		DoDeleteAircraft(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   456
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   457
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   458
	return -(int32)v->value;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   459
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   460
1786
7cfd46c3fcc4 (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
   461
/** Start/Stop an aircraft.
3491
35d747bb5e82 (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
   462
 * @param tile unused
1793
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   463
 * @param p1 aircraft ID to start/stop
1786
7cfd46c3fcc4 (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
   464
 * @param p2 unused
7cfd46c3fcc4 (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
   465
 */
3491
35d747bb5e82 (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
   466
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
   467
{
4352
6703cd8d39e0 (svn r6053) -Codechange: renamed all IsXXXIndex to IsValidXXXID
truelight
parents: 4351
diff changeset
   468
	if (!IsValidVehicleID(p1)) return CMD_ERROR;
1237
1c7a3f9c94b9 (svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents: 1235
diff changeset
   469
6087
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
   470
	Vehicle *v = GetVehicle(p1);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   471
1786
7cfd46c3fcc4 (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
   472
	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
   473
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   474
	// cannot stop airplane when in flight, or when taking off / landing
4059
b1e1c1193f0a (svn r5346) - Feature: Add 4 new airports. 2 for aircraft, 2 for helicopters.
richk
parents: 4056
diff changeset
   475
	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
   476
		return_cmd_error(STR_A017_AIRCRAFT_IS_IN_FLIGHT);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   477
4338
e079d33dc473 (svn r6039) -Fix: [newGRF] FS#287 Bugfix for error in NewGRF callback 31 code for aircraft (Mart3p)
bjarni
parents: 4329
diff changeset
   478
	/* Check if this aircraft can be started/stopped. The callback will fail or
e079d33dc473 (svn r6039) -Fix: [newGRF] FS#287 Bugfix for error in NewGRF callback 31 code for aircraft (Mart3p)
bjarni
parents: 4329
diff changeset
   479
	 * return 0xFF if it can. */
6087
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
   480
	uint16 callback = GetVehicleCallback(CBID_VEHICLE_START_STOP_CHECK, 0, 0, v->engine_type, v);
4338
e079d33dc473 (svn r6039) -Fix: [newGRF] FS#287 Bugfix for error in NewGRF callback 31 code for aircraft (Mart3p)
bjarni
parents: 4329
diff changeset
   481
	if (callback != CALLBACK_FAILED && callback != 0xFF) {
e079d33dc473 (svn r6039) -Fix: [newGRF] FS#287 Bugfix for error in NewGRF callback 31 code for aircraft (Mart3p)
bjarni
parents: 4329
diff changeset
   482
		StringID error = GetGRFStringID(GetEngineGRFID(v->engine_type), 0xD000 + callback);
e079d33dc473 (svn r6039) -Fix: [newGRF] FS#287 Bugfix for error in NewGRF callback 31 code for aircraft (Mart3p)
bjarni
parents: 4329
diff changeset
   483
		return_cmd_error(error);
e079d33dc473 (svn r6039) -Fix: [newGRF] FS#287 Bugfix for error in NewGRF callback 31 code for aircraft (Mart3p)
bjarni
parents: 4329
diff changeset
   484
	}
e079d33dc473 (svn r6039) -Fix: [newGRF] FS#287 Bugfix for error in NewGRF callback 31 code for aircraft (Mart3p)
bjarni
parents: 4329
diff changeset
   485
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   486
	if (flags & DC_EXEC) {
3963
6cab57074a9a (svn r5124) Add IsAircraftInHangar{Stopped,}(), which supersedes CheckStoppedInHangar()
tron
parents: 3955
diff changeset
   487
		if (IsAircraftInHangarStopped(v)) {
3139
b17abef09e15 (svn r3757) -Feature: Delete news items about vehicles, when they get stale
tron
parents: 3008
diff changeset
   488
			DeleteVehicleNews(p1, STR_A014_AIRCRAFT_IS_WAITING_IN);
b17abef09e15 (svn r3757) -Feature: Delete news items about vehicles, when they get stale
tron
parents: 3008
diff changeset
   489
		}
b17abef09e15 (svn r3757) -Feature: Delete news items about vehicles, when they get stale
tron
parents: 3008
diff changeset
   490
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   491
		v->vehstatus ^= VS_STOPPED;
755
80091de50044 (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
   492
		InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   493
		InvalidateWindow(WC_VEHICLE_DEPOT, v->tile);
1055
cc4f60cc9102 (svn r1556) -Fix: Vehicle list updates should now really work
Celestar
parents: 1053
diff changeset
   494
		InvalidateWindowClasses(WC_AIRCRAFT_LIST);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   495
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   496
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   497
	return 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   498
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   499
1793
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   500
/** Send an aircraft to the hangar.
3491
35d747bb5e82 (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
   501
 * @param tile unused
1793
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   502
 * @param p1 vehicle ID to send to the hangar
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   503
 * @param p2 various bitmasked elements
4506
e6a56518135c (svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents: 4463
diff changeset
   504
 * - p2 bit 0-3 - DEPOT_ flags (see vehicle.h)
e6a56518135c (svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents: 4463
diff changeset
   505
 * - p2 bit 8-10 - VLW flag (for mass goto depot)
1793
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   506
 */
3491
35d747bb5e82 (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
   507
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
   508
{
4506
e6a56518135c (svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents: 4463
diff changeset
   509
	if (p2 & DEPOT_MASS_SEND) {
e6a56518135c (svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents: 4463
diff changeset
   510
		/* Mass goto depot requested */
4546
507b7d9bd834 (svn r6378) -Codechange: Rename VLW_FLAGS to VLW_MASK as it is a mask
Darkvater
parents: 4544
diff changeset
   511
		if (!ValidVLWFlags(p2 & VLW_MASK)) return CMD_ERROR;
507b7d9bd834 (svn r6378) -Codechange: Rename VLW_FLAGS to VLW_MASK as it is a mask
Darkvater
parents: 4544
diff changeset
   512
		return SendAllVehiclesToDepot(VEH_Aircraft, flags, p2 & DEPOT_SERVICE, _current_player, (p2 & VLW_MASK), p1);
4463
4d67d3a36fc5 (svn r6246) -Feature: added the many times requested "send all vehicle to depot" button
bjarni
parents: 4451
diff changeset
   513
	}
4d67d3a36fc5 (svn r6246) -Feature: added the many times requested "send all vehicle to depot" button
bjarni
parents: 4451
diff changeset
   514
4506
e6a56518135c (svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents: 4463
diff changeset
   515
	if (!IsValidVehicleID(p1)) return CMD_ERROR;
1237
1c7a3f9c94b9 (svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents: 1235
diff changeset
   516
6087
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
   517
	Vehicle *v = GetVehicle(p1);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   518
4526
6d48f07e829e (svn r6352) -Fix: FS#322 Send to depot bug
bjarni
parents: 4519
diff changeset
   519
	if (v->type != VEH_Aircraft || !CheckOwnership(v->owner) || IsAircraftInHangar(v)) return CMD_ERROR;
4451
2e86da0081b5 (svn r6229) -Feature: Shared order lists now got a "goto depot" button
bjarni
parents: 4434
diff changeset
   520
4506
e6a56518135c (svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents: 4463
diff changeset
   521
	if (v->current_order.type == OT_GOTO_DEPOT && !(p2 & DEPOT_LOCATE_HANGAR)) {
4519
f53d829de680 (svn r6304) -Codechange r6295: Use !! instead of (bool) in commands to send vehicles to depots
bjarni
parents: 4510
diff changeset
   522
		if (!!(p2 & DEPOT_SERVICE) == HASBIT(v->current_order.flags, OFB_HALT_IN_DEPOT)) {
4510
54d30a2372c3 (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
			/* We called with a different DEPOT_SERVICE setting.
54d30a2372c3 (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
			 * Now we change the setting to apply the new one and let the vehicle head for the same hangar.
4549
106ed18a7675 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4546
diff changeset
   525
			 * Note: the if is (true for requesting service == true for ordered to stop in hangar) */
4510
54d30a2372c3 (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
   526
			if (flags & DC_EXEC) {
54d30a2372c3 (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
   527
				TOGGLEBIT(v->current_order.flags, OFB_HALT_IN_DEPOT);
54d30a2372c3 (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
   528
				InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
54d30a2372c3 (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
   529
			}
54d30a2372c3 (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
   530
			return 0;
54d30a2372c3 (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
   531
		}
54d30a2372c3 (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
   532
4506
e6a56518135c (svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents: 4463
diff changeset
   533
		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
   534
		if (flags & DC_EXEC) {
555
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
   535
			if (v->current_order.flags & OF_UNLOAD) v->cur_order_index++;
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
   536
			v->current_order.type = OT_DUMMY;
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
   537
			v->current_order.flags = 0;
755
80091de50044 (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
   538
			InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   539
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   540
	} else {
1262
e3473b1afda9 (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
   541
		bool next_airport_has_hangar = true;
4412
cae52239a576 (svn r6165) -Feature: control click Goto Depot will now make the vehicle service
bjarni
parents: 4389
diff changeset
   542
		StationID next_airport_index = v->u.air.targetairport;
1793
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   543
		const Station *st = GetStation(next_airport_index);
4111
221355fcade6 (svn r5454) -Fix: [ 1394231 ] Autorenew glitch on helicopters
bjarni
parents: 4095
diff changeset
   544
		/* If the station is not a valid airport or if it has no hangars */
6032
fe6a454ac1ba (svn r8755) -Fix
tron
parents: 6027
diff changeset
   545
		if (!st->IsValid() || st->airport_tile == 0 || st->Airport()->nof_depots == 0) {
1793
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   546
			// the aircraft has to search for a hangar on its own
6087
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
   547
			StationID station = FindNearestHangar(v);
1793
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   548
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   549
			next_airport_has_hangar = false;
4506
e6a56518135c (svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents: 4463
diff changeset
   550
			if (station == INVALID_STATION) return CMD_ERROR;
1793
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   551
			st = GetStation(station);
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   552
			next_airport_index = station;
1262
e3473b1afda9 (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
   553
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   554
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   555
		if (flags & DC_EXEC) {
555
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
   556
			v->current_order.type = OT_GOTO_DEPOT;
4412
cae52239a576 (svn r6165) -Feature: control click Goto Depot will now make the vehicle service
bjarni
parents: 4389
diff changeset
   557
			v->current_order.flags = OF_NON_STOP;
4991
a9f3eb46f329 (svn r6994) -Regression (r6291): When sending an aircrafts to a hangar the wrong bit gets tested to determine if a aircraft should stop there or just get serviced
tron
parents: 4845
diff changeset
   558
			if (!(p2 & DEPOT_SERVICE)) SETBIT(v->current_order.flags, OFB_HALT_IN_DEPOT);
5259
9d7de9a6b364 (svn r7389) -Fix: when forcing a vehicle to go to a depot, clear refit orders from current orders (caused refit error popup when not having no orders)
bjarni
parents: 5256
diff changeset
   559
			v->current_order.refit_cargo = CT_INVALID;
4527
fa30d19685c2 (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
   560
			v->current_order.dest = next_airport_index;
755
80091de50044 (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
   561
			InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
5438
b03687991037 (svn r7657) -Fix (r6291): 'Goto Depot' did not work for helicopters going to an airport without depot (mart3p).
rubidium
parents: 5380
diff changeset
   562
			if (v->u.air.state == FLYING && !next_airport_has_hangar) {
4506
e6a56518135c (svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents: 4463
diff changeset
   563
				/* The aircraft is now heading for a different hangar than the next in the orders */
1194
2b25b1477623 (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
   564
				AircraftNextAirportPos_and_Order(v);
2b25b1477623 (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
   565
			}
0
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
	}
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
	return 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   570
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   571
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   572
1802
da61740cc1e7 (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
/** Refits an aircraft to the specified cargo type.
3491
35d747bb5e82 (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
   574
 * @param tile unused
1802
da61740cc1e7 (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
   575
 * @param p1 vehicle ID of the aircraft to refit
da61740cc1e7 (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
   576
 * @param p2 various bitstuffed elements
2635
7ed07303448d (svn r3177) GB, CLRBIT, HASBIT, TOGGLEBIT
tron
parents: 2630
diff changeset
   577
 * - p2 = (bit 0-7) - the new cargo type to refit to
3954
7b51d0046efb (svn r5103) - Add cargo subtype parameter to refit commands (mart3p)
peter1138
parents: 3948
diff changeset
   578
 * - p2 = (bit 8-15) - the new cargo subtype to refit to
1802
da61740cc1e7 (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
   579
 */
3491
35d747bb5e82 (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
   580
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
   581
{
3954
7b51d0046efb (svn r5103) - Add cargo subtype parameter to refit commands (mart3p)
peter1138
parents: 3948
diff changeset
   582
	byte new_subtype = GB(p2, 8, 8);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   583
4352
6703cd8d39e0 (svn r6053) -Codechange: renamed all IsXXXIndex to IsValidXXXID
truelight
parents: 4351
diff changeset
   584
	if (!IsValidVehicleID(p1)) return CMD_ERROR;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   585
6087
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
   586
	Vehicle *v = GetVehicle(p1);
1237
1c7a3f9c94b9 (svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents: 1235
diff changeset
   587
1802
da61740cc1e7 (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
   588
	if (v->type != VEH_Aircraft || !CheckOwnership(v->owner)) return CMD_ERROR;
3963
6cab57074a9a (svn r5124) Add IsAircraftInHangar{Stopped,}(), which supersedes CheckStoppedInHangar()
tron
parents: 3955
diff changeset
   589
	if (!IsAircraftInHangarStopped(v)) return_cmd_error(STR_A01B_AIRCRAFT_MUST_BE_STOPPED);
1237
1c7a3f9c94b9 (svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents: 1235
diff changeset
   590
1802
da61740cc1e7 (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
   591
	/* Check cargo */
6087
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
   592
	CargoID new_cid = GB(p2, 0, 8);
2704
bdf6ae0cb27c (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
   593
	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
   594
1237
1c7a3f9c94b9 (svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents: 1235
diff changeset
   595
	SET_EXPENSES_TYPE(EXPENSES_AIRCRAFT_RUN);
1c7a3f9c94b9 (svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents: 1235
diff changeset
   596
3974
08b438ca3429 (svn r5147) - NewGRF: Use refit capacity callback when refitting an aircraft (mart3p)
peter1138
parents: 3973
diff changeset
   597
	/* Check the refit capacity callback */
6087
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
   598
	uint16 callback = CALLBACK_FAILED;
3974
08b438ca3429 (svn r5147) - NewGRF: Use refit capacity callback when refitting an aircraft (mart3p)
peter1138
parents: 3973
diff changeset
   599
	if (HASBIT(EngInfo(v->engine_type)->callbackmask, CBM_REFIT_CAPACITY)) {
08b438ca3429 (svn r5147) - NewGRF: Use refit capacity callback when refitting an aircraft (mart3p)
peter1138
parents: 3973
diff changeset
   600
		/* Back up the existing cargo type */
08b438ca3429 (svn r5147) - NewGRF: Use refit capacity callback when refitting an aircraft (mart3p)
peter1138
parents: 3973
diff changeset
   601
		CargoID temp_cid = v->cargo_type;
3988
fcb1549e4d4c (svn r5187) - NewGRF: temporarily change a vehicle's cargo subtype when calling the refit capacity callback.
peter1138
parents: 3987
diff changeset
   602
		byte temp_subtype = v->cargo_subtype;
3974
08b438ca3429 (svn r5147) - NewGRF: Use refit capacity callback when refitting an aircraft (mart3p)
peter1138
parents: 3973
diff changeset
   603
		v->cargo_type = new_cid;
3988
fcb1549e4d4c (svn r5187) - NewGRF: temporarily change a vehicle's cargo subtype when calling the refit capacity callback.
peter1138
parents: 3987
diff changeset
   604
		v->cargo_subtype = new_subtype;
3974
08b438ca3429 (svn r5147) - NewGRF: Use refit capacity callback when refitting an aircraft (mart3p)
peter1138
parents: 3973
diff changeset
   605
08b438ca3429 (svn r5147) - NewGRF: Use refit capacity callback when refitting an aircraft (mart3p)
peter1138
parents: 3973
diff changeset
   606
		callback = GetVehicleCallback(CBID_VEHICLE_REFIT_CAPACITY, 0, 0, v->engine_type, v);
08b438ca3429 (svn r5147) - NewGRF: Use refit capacity callback when refitting an aircraft (mart3p)
peter1138
parents: 3973
diff changeset
   607
08b438ca3429 (svn r5147) - NewGRF: Use refit capacity callback when refitting an aircraft (mart3p)
peter1138
parents: 3973
diff changeset
   608
		/* Restore the cargo type */
08b438ca3429 (svn r5147) - NewGRF: Use refit capacity callback when refitting an aircraft (mart3p)
peter1138
parents: 3973
diff changeset
   609
		v->cargo_type = temp_cid;
3988
fcb1549e4d4c (svn r5187) - NewGRF: temporarily change a vehicle's cargo subtype when calling the refit capacity callback.
peter1138
parents: 3987
diff changeset
   610
		v->cargo_subtype = temp_subtype;
3974
08b438ca3429 (svn r5147) - NewGRF: Use refit capacity callback when refitting an aircraft (mart3p)
peter1138
parents: 3973
diff changeset
   611
	}
08b438ca3429 (svn r5147) - NewGRF: Use refit capacity callback when refitting an aircraft (mart3p)
peter1138
parents: 3973
diff changeset
   612
6087
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
   613
	const AircraftVehicleInfo *avi = AircraftVehInfo(v->engine_type);
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
   614
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
   615
	uint pass;
3974
08b438ca3429 (svn r5147) - NewGRF: Use refit capacity callback when refitting an aircraft (mart3p)
peter1138
parents: 3973
diff changeset
   616
	if (callback == CALLBACK_FAILED) {
08b438ca3429 (svn r5147) - NewGRF: Use refit capacity callback when refitting an aircraft (mart3p)
peter1138
parents: 3973
diff changeset
   617
		/* If the callback failed, or wasn't executed, use the aircraft's
08b438ca3429 (svn r5147) - NewGRF: Use refit capacity callback when refitting an aircraft (mart3p)
peter1138
parents: 3973
diff changeset
   618
		 * default cargo capacity */
6087
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
   619
		pass = AircraftDefaultCargoCapacity(new_cid, avi);
3974
08b438ca3429 (svn r5147) - NewGRF: Use refit capacity callback when refitting an aircraft (mart3p)
peter1138
parents: 3973
diff changeset
   620
	} else {
08b438ca3429 (svn r5147) - NewGRF: Use refit capacity callback when refitting an aircraft (mart3p)
peter1138
parents: 3973
diff changeset
   621
		pass = callback;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   622
	}
3008
cef26520c91e (svn r3588) - Refit: Use only one global to return the refit capacity rather than one per vehicle type.
peter1138
parents: 3005
diff changeset
   623
	_returned_refit_capacity = pass;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   624
6087
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
   625
	int32 cost = 0;
4845
72bbb6e2694e (svn r6771) -Codechange: Replace two macros with functions. IS_HUMAN_PLAYER and
Darkvater
parents: 4843
diff changeset
   626
	if (IsHumanPlayer(v->owner) && new_cid != v->cargo_type) {
4544
69f1248a2d97 (svn r6376) -Codechange: [vehicle refit] moved all refit cost calculations into GetRefitCost()
bjarni
parents: 4529
diff changeset
   627
		cost = GetRefitCost(v->engine_type);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   628
	}
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
	if (flags & DC_EXEC) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   631
		v->cargo_cap = pass;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   632
6087
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
   633
		Vehicle *u = v->next;
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
   634
		uint 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
   635
		u->cargo_cap = mail;
3955
b96f96b31403 (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
   636
		if (v->cargo_type == new_cid) {
b96f96b31403 (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
   637
			v->cargo_count = min(pass, v->cargo_count);
b96f96b31403 (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
			u->cargo_count = min(mail, u->cargo_count);
b96f96b31403 (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
		} else {
b96f96b31403 (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
			v->cargo_count = 0;
b96f96b31403 (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
			u->cargo_count = 0;
b96f96b31403 (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
		}
1802
da61740cc1e7 (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
   643
		v->cargo_type = new_cid;
3954
7b51d0046efb (svn r5103) - Add cargo subtype parameter to refit commands (mart3p)
peter1138
parents: 3948
diff changeset
   644
		v->cargo_subtype = new_subtype;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   645
		InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
3722
0984901a2744 (svn r4693) - NewGRF: invalidate aircraft in viewports during loading stages so that graphic changes are visible. (mart3p)
peter1138
parents: 3710
diff changeset
   646
		InvalidateWindow(WC_VEHICLE_DEPOT, v->tile);
0984901a2744 (svn r4693) - NewGRF: invalidate aircraft in viewports during loading stages so that graphic changes are visible. (mart3p)
peter1138
parents: 3710
diff changeset
   647
		RebuildVehicleLists();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   648
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   649
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   650
	return cost;
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
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
static void CheckIfAircraftNeedsService(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   655
{
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   656
	if (_patches.servint_aircraft == 0) return;
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   657
	if (!VehicleNeedsService(v)) return;
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   658
	if (v->vehstatus & VS_STOPPED) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   659
555
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
   660
	if (v->current_order.type == OT_GOTO_DEPOT &&
1530
2b46869fc0d4 (svn r2034) -Codechange: Replaced the rather meaningless LOAD and UNLOAD flags for depots by something more meaningful
celestar
parents: 1520
diff changeset
   661
			v->current_order.flags & OF_HALT_IN_DEPOT)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   662
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   663
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   664
	if (_patches.gotodepot && VehicleHasDepotOrders(v)) return;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   665
4529
18bd5e7e35aa (svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents: 4527
diff changeset
   666
	if (IsAircraftInHangar(v)) {
18bd5e7e35aa (svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents: 4527
diff changeset
   667
		VehicleServiceInDepot(v);
18bd5e7e35aa (svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents: 4527
diff changeset
   668
		return;
18bd5e7e35aa (svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents: 4527
diff changeset
   669
	}
18bd5e7e35aa (svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents: 4527
diff changeset
   670
6087
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
   671
	const Station *st = GetStation(v->current_order.dest);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   672
	// only goto depot if the target airport has terminals (eg. it is airport)
6032
fe6a454ac1ba (svn r8755) -Fix
tron
parents: 6027
diff changeset
   673
	if (st->IsValid() && st->airport_tile != 0 && st->Airport()->terminals != NULL) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   674
//		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
   675
//		v->u.air.targetairport = st->index;
555
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
   676
		v->current_order.type = OT_GOTO_DEPOT;
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
   677
		v->current_order.flags = OF_NON_STOP;
755
80091de50044 (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
   678
		InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
555
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
   679
	} else if (v->current_order.type == OT_GOTO_DEPOT) {
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
   680
		v->current_order.type = OT_DUMMY;
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
   681
		v->current_order.flags = 0;
755
80091de50044 (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);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   683
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   684
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   685
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   686
void OnNewDay_Aircraft(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   687
{
5854
9eee280920f0 (svn r8428) -Codechange: Add proper names to aircraft subtypes instead of magic numbers and add a function IsNormalAircraft() which tells us whether the aircraft is in fact some flying device or a rotor/shadow.
Darkvater
parents: 5754
diff changeset
   688
	if (!IsNormalAircraft(v)) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   689
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   690
	if ((++v->day_counter & 7) == 0) DecreaseVehicleValue(v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   691
3140
5555cc95043a (svn r3758) Remove the news validation callback. It is superseded by r3757.
tron
parents: 3139
diff changeset
   692
	CheckOrders(v);
19
6080d2b6a959 (svn r20) Feature: warning when a vehicle has invalid orders (celestar)
dominik
parents: 11
diff changeset
   693
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   694
	CheckVehicleBreakdown(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   695
	AgeVehicle(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   696
	CheckIfAircraftNeedsService(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   697
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   698
	if (v->vehstatus & VS_STOPPED) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   699
6087
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
   700
	int32 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
   701
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   702
	v->profit_this_year -= cost >> 8;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   703
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   704
	SET_EXPENSES_TYPE(EXPENSES_AIRCRAFT_RUN);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   705
	SubtractMoneyFromPlayerFract(v->owner, cost);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   706
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   707
	InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
1055
cc4f60cc9102 (svn r1556) -Fix: Vehicle list updates should now really work
Celestar
parents: 1053
diff changeset
   708
	InvalidateWindowClasses(WC_AIRCRAFT_LIST);
0
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
1093
4fdc46eaf423 (svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents: 1064
diff changeset
   711
void AircraftYearlyLoop(void)
0
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
	Vehicle *v;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   714
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   715
	FOR_ALL_VEHICLES(v) {
5854
9eee280920f0 (svn r8428) -Codechange: Add proper names to aircraft subtypes instead of magic numbers and add a function IsNormalAircraft() which tells us whether the aircraft is in fact some flying device or a rotor/shadow.
Darkvater
parents: 5754
diff changeset
   716
		if (v->type == VEH_Aircraft && IsNormalAircraft(v)) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   717
			v->profit_last_year = v->profit_this_year;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   718
			v->profit_this_year = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   719
			InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
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
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   722
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   723
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   724
static void AgeAircraftCargo(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   725
{
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   726
	if (_age_cargo_skip_counter != 0) return;
0
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
	do {
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   729
		if (v->cargo_days != 0xFF) v->cargo_days++;
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   730
		v = v->next;
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   731
	} while (v != NULL);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   732
}
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
static void HelicopterTickHandler(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   735
{
6087
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
   736
	Vehicle *u = v->next->next;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   737
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   738
	if (u->vehstatus & VS_HIDDEN) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   739
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   740
	// 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
   741
	// loading/unloading at a terminal or stopped
555
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
   742
	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
   743
		if (u->cur_speed != 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   744
			u->cur_speed++;
3710
44cf97e29c18 (svn r4653) - NewGRF: enable use of custom helicopter rotors in game (but not GUI windows yet) (mart3p)
peter1138
parents: 3701
diff changeset
   745
			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
   746
				u->cur_speed = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   747
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   748
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   749
	} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   750
		if (u->cur_speed == 0)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   751
			u->cur_speed = 0x70;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   752
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   753
		if (u->cur_speed >= 0x50)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   754
			u->cur_speed--;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   755
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   756
6087
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
   757
	int tick = ++u->tick_counter;
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
   758
	int spd = u->cur_speed >> 4;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   759
6087
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
   760
	SpriteID img;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   761
	if (spd == 0) {
3710
44cf97e29c18 (svn r4653) - NewGRF: enable use of custom helicopter rotors in game (but not GUI windows yet) (mart3p)
peter1138
parents: 3701
diff changeset
   762
		u->u.air.state = HRS_ROTOR_STOPPED;
44cf97e29c18 (svn r4653) - NewGRF: enable use of custom helicopter rotors in game (but not GUI windows yet) (mart3p)
peter1138
parents: 3701
diff changeset
   763
		img = GetRotorImage(v);
2549
5587f9a38563 (svn r3078) Some more stuff, which piled up:
tron
parents: 2517
diff changeset
   764
		if (u->cur_image == img) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   765
	} else if (tick >= spd) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   766
		u->tick_counter = 0;
3710
44cf97e29c18 (svn r4653) - NewGRF: enable use of custom helicopter rotors in game (but not GUI windows yet) (mart3p)
peter1138
parents: 3701
diff changeset
   767
		u->u.air.state++;
44cf97e29c18 (svn r4653) - NewGRF: enable use of custom helicopter rotors in game (but not GUI windows yet) (mart3p)
peter1138
parents: 3701
diff changeset
   768
		if (u->u.air.state > HRS_ROTOR_MOVING_3) u->u.air.state = HRS_ROTOR_MOVING_1;
44cf97e29c18 (svn r4653) - NewGRF: enable use of custom helicopter rotors in game (but not GUI windows yet) (mart3p)
peter1138
parents: 3701
diff changeset
   769
		img = GetRotorImage(v);
2549
5587f9a38563 (svn r3078) Some more stuff, which piled up:
tron
parents: 2517
diff changeset
   770
	} else {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   771
		return;
2549
5587f9a38563 (svn r3078) Some more stuff, which piled up:
tron
parents: 2517
diff changeset
   772
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   773
2549
5587f9a38563 (svn r3078) Some more stuff, which piled up:
tron
parents: 2517
diff changeset
   774
	u->cur_image = img;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   775
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   776
	BeginVehicleMove(u);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   777
	VehiclePositionChanged(u);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   778
	EndVehicleMove(u);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   779
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   780
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   781
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
   782
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   783
	v->x_pos = x;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   784
	v->y_pos = y;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   785
	v->z_pos = z;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   786
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   787
	v->cur_image = GetAircraftImage(v, v->direction);
5854
9eee280920f0 (svn r8428) -Codechange: Add proper names to aircraft subtypes instead of magic numbers and add a function IsNormalAircraft() which tells us whether the aircraft is in fact some flying device or a rotor/shadow.
Darkvater
parents: 5754
diff changeset
   788
	if (v->subtype == AIR_HELICOPTER) v->next->next->cur_image = GetRotorImage(v);
0
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
	BeginVehicleMove(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   791
	VehiclePositionChanged(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   792
	EndVehicleMove(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   793
6087
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
   794
	Vehicle *u = v->next;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   795
6087
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
   796
	int safe_x = clamp(x, 0, MapMaxX() * TILE_SIZE);
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
   797
	int 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
   798
	u->x_pos = x;
4259
422724d8c66a (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
   799
	u->y_pos = y - ((v->z_pos-GetSlopeZ(safe_x, safe_y)) >> 3);;
422724d8c66a (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
   800
422724d8c66a (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
   801
	safe_y = clamp(u->y_pos, 0, MapMaxY() * TILE_SIZE);
422724d8c66a (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
   802
	u->z_pos = GetSlopeZ(safe_x, safe_y);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   803
	u->cur_image = v->cur_image;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   804
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   805
	BeginVehicleMove(u);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   806
	VehiclePositionChanged(u);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   807
	EndVehicleMove(u);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   808
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   809
	u = u->next;
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   810
	if (u != NULL) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   811
		u->x_pos = x;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   812
		u->y_pos = y;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   813
		u->z_pos = z + 5;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   814
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   815
		BeginVehicleMove(u);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   816
		VehiclePositionChanged(u);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   817
		EndVehicleMove(u);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   818
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   819
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   820
4725
40cccaaa042c (svn r6637) -Codechange: merged all (vehicle type)EnterDepot into VehicleEnterDepot()
bjarni
parents: 4712
diff changeset
   821
/** Handle Aircraft specific tasks when a an Aircraft enters a hangar
40cccaaa042c (svn r6637) -Codechange: merged all (vehicle type)EnterDepot into VehicleEnterDepot()
bjarni
parents: 4712
diff changeset
   822
 * @param *v Vehicle that enters the hangar
40cccaaa042c (svn r6637) -Codechange: merged all (vehicle type)EnterDepot into VehicleEnterDepot()
bjarni
parents: 4712
diff changeset
   823
 */
4732
4f1c405ac96e (svn r6644) -Fix(r6637): remove inline to allow MSVC compilation
belugas
parents: 4725
diff changeset
   824
void HandleAircraftEnterHangar(Vehicle *v)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   825
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   826
	v->subspeed = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   827
	v->progress = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   828
6087
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
   829
	Vehicle *u = v->next;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   830
	u->vehstatus |= VS_HIDDEN;
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   831
	u = u->next;
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   832
	if (u != NULL) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   833
		u->vehstatus |= VS_HIDDEN;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   834
		u->cur_speed = 0;
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
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   837
	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
   838
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   839
2549
5587f9a38563 (svn r3078) Some more stuff, which piled up:
tron
parents: 2517
diff changeset
   840
static void PlayAircraftSound(const Vehicle* v)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   841
{
4656
9c1d8c4d3e60 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4574
diff changeset
   842
	if (!PlayVehicleSound(v, VSE_START)) {
9c1d8c4d3e60 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4574
diff changeset
   843
		SndPlayVehicleFx(AircraftVehInfo(v->engine_type)->sfx, v);
9c1d8c4d3e60 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4574
diff changeset
   844
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   845
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   846
5945
fabc2fee32b3 (svn r8611) -Fix: Moved the aircraft speed limiting into UpdateAircraftSpeed and enummed some speed limits. This should give a more steady display in the status bar during taxiing
celestar
parents: 5944
diff changeset
   847
/** Special velocities for aircraft
fabc2fee32b3 (svn r8611) -Fix: Moved the aircraft speed limiting into UpdateAircraftSpeed and enummed some speed limits. This should give a more steady display in the status bar during taxiing
celestar
parents: 5944
diff changeset
   848
 */
fabc2fee32b3 (svn r8611) -Fix: Moved the aircraft speed limiting into UpdateAircraftSpeed and enummed some speed limits. This should give a more steady display in the status bar during taxiing
celestar
parents: 5944
diff changeset
   849
enum AircraftSpeedLimits {
fabc2fee32b3 (svn r8611) -Fix: Moved the aircraft speed limiting into UpdateAircraftSpeed and enummed some speed limits. This should give a more steady display in the status bar during taxiing
celestar
parents: 5944
diff changeset
   850
	SPEED_LIMIT_NONE   =  0,  ///< No environmental speed limit. Speed limit is type dependent
fabc2fee32b3 (svn r8611) -Fix: Moved the aircraft speed limiting into UpdateAircraftSpeed and enummed some speed limits. This should give a more steady display in the status bar during taxiing
celestar
parents: 5944
diff changeset
   851
	SPEED_LIMIT_TAXI   = 12,  ///< Maximum speed of an aircraft while taxiing
fabc2fee32b3 (svn r8611) -Fix: Moved the aircraft speed limiting into UpdateAircraftSpeed and enummed some speed limits. This should give a more steady display in the status bar during taxiing
celestar
parents: 5944
diff changeset
   852
	SPEED_LIMIT_BROKEN = 27,  ///< Maximum speed of an aircraft that is broken
fabc2fee32b3 (svn r8611) -Fix: Moved the aircraft speed limiting into UpdateAircraftSpeed and enummed some speed limits. This should give a more steady display in the status bar during taxiing
celestar
parents: 5944
diff changeset
   853
};
fabc2fee32b3 (svn r8611) -Fix: Moved the aircraft speed limiting into UpdateAircraftSpeed and enummed some speed limits. This should give a more steady display in the status bar during taxiing
celestar
parents: 5944
diff changeset
   854
fabc2fee32b3 (svn r8611) -Fix: Moved the aircraft speed limiting into UpdateAircraftSpeed and enummed some speed limits. This should give a more steady display in the status bar during taxiing
celestar
parents: 5944
diff changeset
   855
static bool UpdateAircraftSpeed(Vehicle *v, uint speed_limit)
0
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
	uint spd = v->acceleration * 2;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   858
	byte t;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   859
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   860
	v->subspeed = (t=v->subspeed) + (byte)spd;
5945
fabc2fee32b3 (svn r8611) -Fix: Moved the aircraft speed limiting into UpdateAircraftSpeed and enummed some speed limits. This should give a more steady display in the status bar during taxiing
celestar
parents: 5944
diff changeset
   861
	if (speed_limit == SPEED_LIMIT_NONE) speed_limit = v->max_speed;
fabc2fee32b3 (svn r8611) -Fix: Moved the aircraft speed limiting into UpdateAircraftSpeed and enummed some speed limits. This should give a more steady display in the status bar during taxiing
celestar
parents: 5944
diff changeset
   862
	spd = min(v->cur_speed + (spd >> 8) + (v->subspeed < t), speed_limit);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   863
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   864
	// adjust speed for broken vehicles
5945
fabc2fee32b3 (svn r8611) -Fix: Moved the aircraft speed limiting into UpdateAircraftSpeed and enummed some speed limits. This should give a more steady display in the status bar during taxiing
celestar
parents: 5944
diff changeset
   865
	if (v->vehstatus & VS_AIRCRAFT_BROKEN) spd = min(spd, SPEED_LIMIT_BROKEN);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   866
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   867
	//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
   868
	if (spd != v->cur_speed) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   869
		v->cur_speed = spd;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   870
		if (_patches.vehicle_speed)
755
80091de50044 (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
   871
			InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   872
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   873
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   874
	if (!(v->direction & 1)) spd = spd * 3 / 4;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   875
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   876
	if (spd == 0) return false;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   877
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   878
	if ((byte)++spd == 0) return true;
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
	v->progress = (t = v->progress) - (byte)spd;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   881
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   882
	return t < v->progress;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   883
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   884
5908
5ff4fe308285 (svn r8534) -Feature/Codechange: Provide aircraft with vertical separation depending on their altitude and velocity
celestar
parents: 5886
diff changeset
   885
/**
5ff4fe308285 (svn r8534) -Feature/Codechange: Provide aircraft with vertical separation depending on their altitude and velocity
celestar
parents: 5886
diff changeset
   886
 * Gets the cruise altitude of an aircraft.
5ff4fe308285 (svn r8534) -Feature/Codechange: Provide aircraft with vertical separation depending on their altitude and velocity
celestar
parents: 5886
diff changeset
   887
 * The cruise altitude is determined by the velocity of the vehicle
5ff4fe308285 (svn r8534) -Feature/Codechange: Provide aircraft with vertical separation depending on their altitude and velocity
celestar
parents: 5886
diff changeset
   888
 * and the direction it is moving
5ff4fe308285 (svn r8534) -Feature/Codechange: Provide aircraft with vertical separation depending on their altitude and velocity
celestar
parents: 5886
diff changeset
   889
 * @param v The vehicle. Should be an aircraft
5ff4fe308285 (svn r8534) -Feature/Codechange: Provide aircraft with vertical separation depending on their altitude and velocity
celestar
parents: 5886
diff changeset
   890
 * @returns Altitude in pixel units
5ff4fe308285 (svn r8534) -Feature/Codechange: Provide aircraft with vertical separation depending on their altitude and velocity
celestar
parents: 5886
diff changeset
   891
 */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   892
static byte GetAircraftFlyingAltitude(const Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   893
{
5908
5ff4fe308285 (svn r8534) -Feature/Codechange: Provide aircraft with vertical separation depending on their altitude and velocity
celestar
parents: 5886
diff changeset
   894
	/* Make sure Aircraft fly no lower so that they don't conduct
5ff4fe308285 (svn r8534) -Feature/Codechange: Provide aircraft with vertical separation depending on their altitude and velocity
celestar
parents: 5886
diff changeset
   895
	 * CFITs (controlled flight into terrain)
5ff4fe308285 (svn r8534) -Feature/Codechange: Provide aircraft with vertical separation depending on their altitude and velocity
celestar
parents: 5886
diff changeset
   896
	 */
5ff4fe308285 (svn r8534) -Feature/Codechange: Provide aircraft with vertical separation depending on their altitude and velocity
celestar
parents: 5886
diff changeset
   897
	byte base_altitude = 150;
5ff4fe308285 (svn r8534) -Feature/Codechange: Provide aircraft with vertical separation depending on their altitude and velocity
celestar
parents: 5886
diff changeset
   898
5ff4fe308285 (svn r8534) -Feature/Codechange: Provide aircraft with vertical separation depending on their altitude and velocity
celestar
parents: 5886
diff changeset
   899
	/* Make sure eastbound and westbound planes do not "crash" into each
5ff4fe308285 (svn r8534) -Feature/Codechange: Provide aircraft with vertical separation depending on their altitude and velocity
celestar
parents: 5886
diff changeset
   900
	 * other by providing them with vertical seperation
5ff4fe308285 (svn r8534) -Feature/Codechange: Provide aircraft with vertical separation depending on their altitude and velocity
celestar
parents: 5886
diff changeset
   901
	 */
5ff4fe308285 (svn r8534) -Feature/Codechange: Provide aircraft with vertical separation depending on their altitude and velocity
celestar
parents: 5886
diff changeset
   902
	switch (v->direction) {
6087
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
   903
		case DIR_N:
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
   904
		case DIR_NE:
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
   905
		case DIR_E:
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
   906
		case DIR_SE:
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
   907
			base_altitude += 15;
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
   908
			break;
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
   909
5908
5ff4fe308285 (svn r8534) -Feature/Codechange: Provide aircraft with vertical separation depending on their altitude and velocity
celestar
parents: 5886
diff changeset
   910
		default: break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   911
	}
5908
5ff4fe308285 (svn r8534) -Feature/Codechange: Provide aircraft with vertical separation depending on their altitude and velocity
celestar
parents: 5886
diff changeset
   912
5ff4fe308285 (svn r8534) -Feature/Codechange: Provide aircraft with vertical separation depending on their altitude and velocity
celestar
parents: 5886
diff changeset
   913
	/* Make faster planes fly higher so that they can overtake slower ones */
5ff4fe308285 (svn r8534) -Feature/Codechange: Provide aircraft with vertical separation depending on their altitude and velocity
celestar
parents: 5886
diff changeset
   914
	base_altitude += min(30 * (v->max_speed / 37), 90);
5ff4fe308285 (svn r8534) -Feature/Codechange: Provide aircraft with vertical separation depending on their altitude and velocity
celestar
parents: 5886
diff changeset
   915
5ff4fe308285 (svn r8534) -Feature/Codechange: Provide aircraft with vertical separation depending on their altitude and velocity
celestar
parents: 5886
diff changeset
   916
	return base_altitude;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   917
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   918
1401
44a7e0f04d28 (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
   919
static bool AircraftController(Vehicle *v)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   920
{
6087
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
   921
	const Station *st = GetStation(v->u.air.targetairport);
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
	// prevent going to 0,0 if airport is deleted.
6087
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
   924
	TileIndex tile = st->airport_tile;
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
   925
	if (tile == 0) tile = st->xy;
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
   926
	int x = TileX(tile) * TILE_SIZE;
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
   927
	int y = TileY(tile) * TILE_SIZE;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   928
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   929
	// get airport moving data
6032
fe6a454ac1ba (svn r8755) -Fix
tron
parents: 6027
diff changeset
   930
	const AirportFTAClass *afc = st->Airport();
6020
a7463b7eb894 (svn r8743) -Fix
tron
parents: 6011
diff changeset
   931
	const AirportMovingData *amd = afc->MovingData(v->u.air.pos);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   932
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   933
	// Helicopter raise
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   934
	if (amd->flag & AMED_HELI_RAISE) {
6087
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
   935
		Vehicle *u = v->next->next;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   936
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   937
		// Make sure the rotors don't rotate too fast
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   938
		if (u->cur_speed > 32) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   939
			v->cur_speed = 0;
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   940
			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
   941
		} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   942
			u->cur_speed = 32;
5945
fabc2fee32b3 (svn r8611) -Fix: Moved the aircraft speed limiting into UpdateAircraftSpeed and enummed some speed limits. This should give a more steady display in the status bar during taxiing
celestar
parents: 5944
diff changeset
   943
			if (UpdateAircraftSpeed(v, SPEED_LIMIT_NONE)) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   944
				v->tile = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   945
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   946
				// Reached altitude?
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   947
				if (v->z_pos >= 184) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   948
					v->cur_speed = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   949
					return true;
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
				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
   952
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   953
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   954
		return false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   955
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   956
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   957
	// Helicopter landing.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   958
	if (amd->flag & AMED_HELI_LOWER) {
5945
fabc2fee32b3 (svn r8611) -Fix: Moved the aircraft speed limiting into UpdateAircraftSpeed and enummed some speed limits. This should give a more steady display in the status bar during taxiing
celestar
parents: 5944
diff changeset
   959
		if (UpdateAircraftSpeed(v, SPEED_LIMIT_NONE)) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   960
			if (st->airport_tile == 0) {
1401
44a7e0f04d28 (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
   961
				// 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
   962
				// 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
   963
				// * 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
   964
				v->u.air.state = FLYING;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   965
				AircraftNextAirportPos_and_Order(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   966
				return false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   967
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   968
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   969
			// Vehicle is now at the airport.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   970
			v->tile = st->airport_tile;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   971
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   972
			// Find altitude of landing position.
6087
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
   973
			uint z = GetSlopeZ(x, y) + 1 + afc->delta_z;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   974
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   975
			if (z == v->z_pos) {
6087
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
   976
				Vehicle *u = v->next->next;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   977
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   978
				// Increase speed of rotors. When speed is 80, we've landed.
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   979
				if (u->cur_speed >= 80) return true;
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   980
				u->cur_speed += 4;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   981
			} else if (v->z_pos > z) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   982
				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
   983
			} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   984
				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
   985
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   986
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   987
		return false;
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
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   990
	// Get distance from destination pos to current pos.
6087
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
   991
	uint dist = myabs(x + amd->x - v->x_pos) +  myabs(y + amd->y - v->y_pos);
0
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
	// Need exact position?
2364
9685b222ab72 (svn r2890) Fix some signed/unsigned comparison warnings (tokai)
tron
parents: 2316
diff changeset
   994
	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
   995
		return true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   996
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   997
	// At final pos?
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   998
	if (dist == 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   999
		// Change direction smoothly to final direction.
6087
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
  1000
		DirDiff 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
696a6ca0bfa9 (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
5945
fabc2fee32b3 (svn r8611) -Fix: Moved the aircraft speed limiting into UpdateAircraftSpeed and enummed some speed limits. This should give a more steady display in the status bar during taxiing
celestar
parents: 5944
diff changeset
  1008
		if (!UpdateAircraftSpeed(v, SPEED_LIMIT_TAXI)) return false;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1009
3158
696a6ca0bfa9 (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
5945
fabc2fee32b3 (svn r8611) -Fix: Moved the aircraft speed limiting into UpdateAircraftSpeed and enummed some speed limits. This should give a more steady display in the status bar during taxiing
celestar
parents: 5944
diff changeset
  1017
	if (!UpdateAircraftSpeed(v, ((amd->flag & AMED_NOSPDCLAMP) == 0) ? SPEED_LIMIT_TAXI : SPEED_LIMIT_NONE)) return false;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1018
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1019
	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
  1020
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1021
	// Turn. Do it slowly if in the air.
6087
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
  1022
	Direction newdir = GetDirectionTowards(v, x + amd->x, y + amd->y);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1023
	if (newdir != v->direction) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1024
		if (amd->flag & AMED_SLOWTURN) {
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1025
			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
  1026
			v->direction = newdir;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1027
		} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1028
			v->cur_speed >>= 1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1029
			v->direction = newdir;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1030
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1031
	}
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
	// Move vehicle.
6087
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
  1034
	GetNewVehiclePosResult gp;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1035
	GetNewVehiclePos(v, &gp);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1036
	v->tile = gp.new_tile;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1037
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1038
	// If vehicle is in the air, use tile coordinate 0.
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1039
	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
  1040
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1041
	// Adjust Z for land or takeoff?
6087
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
  1042
	uint z = v->z_pos;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1043
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1044
	if (amd->flag & AMED_TAKEOFF) {
6087
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
  1045
		z = min(z + 2, GetAircraftFlyingAltitude(v));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1046
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1047
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1048
	if (amd->flag & AMED_LAND) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1049
		if (st->airport_tile == 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1050
			v->u.air.state = FLYING;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1051
			AircraftNextAirportPos_and_Order(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1052
			// get aircraft back on running altitude
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1053
			SetAircraftPosition(v, gp.x, gp.y, GetAircraftFlyingAltitude(v));
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1054
			return false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1055
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1056
6087
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
  1057
		uint curz = GetSlopeZ(x, y) + 1;
0
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
		if (curz > z) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1060
			z++;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1061
		} else {
5601
d58f82901b2f (svn r8055) -Codechange: Replace the different max, dmax, maxu whatever macros by a simple template function max(), that requires two arguments of the same type. While I'm at it change a variable called "max" to "maxval" in a function that calls max().
celestar
parents: 5587
diff changeset
  1062
			int t = max(1U, dist - 4);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1063
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1064
			z -= ((z - curz) + t - 1) / t;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1065
			if (z < curz) z = curz;
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
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1068
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1069
	// 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
  1070
	if (amd->flag & AMED_BRAKE) {
6087
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
  1071
		uint curz = GetSlopeZ(x, y) + 1;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1072
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1073
		if (z > curz) {
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1074
			z--;
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1075
		} else if (z < curz) {
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1076
			z++;
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1077
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1078
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1079
		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
  1080
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1081
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1082
	SetAircraftPosition(v, gp.x, gp.y, z);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1083
	return false;
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
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1086
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1087
static void HandleCrashedAircraft(Vehicle *v)
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
	v->u.air.crashed_counter++;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1090
6087
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
  1091
	Station *st = GetStation(v->u.air.targetairport);
899
69db9cb6bc9d (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
  1092
69db9cb6bc9d (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
  1093
	// make aircraft crash down to the ground
1150
7b5946a53372 (svn r1651) Fix: [ 1098696 ] Airport shadow remains after crashing
dominik
parents: 1139
diff changeset
  1094
	if (v->u.air.crashed_counter < 500 && st->airport_tile==0 && ((v->u.air.crashed_counter % 3) == 0) ) {
6087
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
  1095
		uint z = GetSlopeZ(v->x_pos, v->y_pos);
899
69db9cb6bc9d (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
  1096
		v->z_pos -= 1;
1150
7b5946a53372 (svn r1651) Fix: [ 1098696 ] Airport shadow remains after crashing
dominik
parents: 1139
diff changeset
  1097
		if (v->z_pos == z) {
7b5946a53372 (svn r1651) Fix: [ 1098696 ] Airport shadow remains after crashing
dominik
parents: 1139
diff changeset
  1098
			v->u.air.crashed_counter = 500;
7b5946a53372 (svn r1651) Fix: [ 1098696 ] Airport shadow remains after crashing
dominik
parents: 1139
diff changeset
  1099
			v->z_pos++;
7b5946a53372 (svn r1651) Fix: [ 1098696 ] Airport shadow remains after crashing
dominik
parents: 1139
diff changeset
  1100
		}
899
69db9cb6bc9d (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
  1101
	}
69db9cb6bc9d (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
  1102
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1103
	if (v->u.air.crashed_counter < 650) {
6087
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
  1104
		uint32 r;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1105
		if (CHANCE16R(1,32,r)) {
3160
14b27357d353 (svn r3786) More work for DirDiff
tron
parents: 3158
diff changeset
  1106
			static const DirDiff delta[] = {
14b27357d353 (svn r3786) More work for DirDiff
tron
parents: 3158
diff changeset
  1107
				DIRDIFF_45LEFT, DIRDIFF_SAME, DIRDIFF_SAME, DIRDIFF_45RIGHT
14b27357d353 (svn r3786) More work for DirDiff
tron
parents: 3158
diff changeset
  1108
			};
14b27357d353 (svn r3786) More work for DirDiff
tron
parents: 3158
diff changeset
  1109
14b27357d353 (svn r3786) More work for DirDiff
tron
parents: 3158
diff changeset
  1110
			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
  1111
			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
  1112
			r = Random();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1113
			CreateEffectVehicleRel(v,
2140
a04d0142ad65 (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
  1114
				GB(r, 0, 4) + 4,
a04d0142ad65 (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
  1115
				GB(r, 4, 4) + 4,
a04d0142ad65 (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
  1116
				GB(r, 8, 4),
1359
52782e5cf7c9 (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
  1117
				EV_EXPLOSION_SMALL);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1118
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1119
	} else if (v->u.air.crashed_counter >= 10000) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1120
		// remove rubble of crashed airplane
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1121
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1122
		// 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
  1123
		// 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
  1124
		// but they all share the same number
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1125
		CLRBITS(st->airport_flags, RUNWAY_IN_block);
4059
b1e1c1193f0a (svn r5346) - Feature: Add 4 new airports. 2 for aircraft, 2 for helicopters.
richk
parents: 4056
diff changeset
  1126
		CLRBITS(st->airport_flags, RUNWAY_IN_OUT_block); // commuter airport
b1e1c1193f0a (svn r5346) - Feature: Add 4 new airports. 2 for aircraft, 2 for helicopters.
richk
parents: 4056
diff changeset
  1127
		CLRBITS(st->airport_flags, RUNWAY_IN2_block); // intercontinental
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1128
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1129
		BeginVehicleMove(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1130
		EndVehicleMove(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1131
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1132
		DoDeleteAircraft(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1133
	}
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
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1136
static void HandleBrokenAircraft(Vehicle *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
	if (v->breakdown_ctr != 1) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1139
		v->breakdown_ctr = 1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1140
		v->vehstatus |= VS_AIRCRAFT_BROKEN;
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
		if (v->breakdowns_since_last_service != 255)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1143
			v->breakdowns_since_last_service++;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1144
		InvalidateWindow(WC_VEHICLE_VIEW, v->index);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1145
		InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1146
	}
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
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1149
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1150
static void HandleAircraftSmoke(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1151
{
2654
1370de8783d3 (svn r3196) Use structs instead of magic offsets into arrays
tron
parents: 2639
diff changeset
  1152
	static const struct {
1370de8783d3 (svn r3196) Use structs instead of magic offsets into arrays
tron
parents: 2639
diff changeset
  1153
		int8 x;
1370de8783d3 (svn r3196) Use structs instead of magic offsets into arrays
tron
parents: 2639
diff changeset
  1154
		int8 y;
1370de8783d3 (svn r3196) Use structs instead of magic offsets into arrays
tron
parents: 2639
diff changeset
  1155
	} smoke_pos[] = {
1370de8783d3 (svn r3196) Use structs instead of magic offsets into arrays
tron
parents: 2639
diff changeset
  1156
		{  5,  5 },
1370de8783d3 (svn r3196) Use structs instead of magic offsets into arrays
tron
parents: 2639
diff changeset
  1157
		{  6,  0 },
1370de8783d3 (svn r3196) Use structs instead of magic offsets into arrays
tron
parents: 2639
diff changeset
  1158
		{  5, -5 },
1370de8783d3 (svn r3196) Use structs instead of magic offsets into arrays
tron
parents: 2639
diff changeset
  1159
		{  0, -6 },
1370de8783d3 (svn r3196) Use structs instead of magic offsets into arrays
tron
parents: 2639
diff changeset
  1160
		{ -5, -5 },
1370de8783d3 (svn r3196) Use structs instead of magic offsets into arrays
tron
parents: 2639
diff changeset
  1161
		{ -6,  0 },
1370de8783d3 (svn r3196) Use structs instead of magic offsets into arrays
tron
parents: 2639
diff changeset
  1162
		{ -5,  5 },
1370de8783d3 (svn r3196) Use structs instead of magic offsets into arrays
tron
parents: 2639
diff changeset
  1163
		{  0,  6 }
1370de8783d3 (svn r3196) Use structs instead of magic offsets into arrays
tron
parents: 2639
diff changeset
  1164
	};
1370de8783d3 (svn r3196) Use structs instead of magic offsets into arrays
tron
parents: 2639
diff changeset
  1165
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1166
	if (!(v->vehstatus & VS_AIRCRAFT_BROKEN)) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1167
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1168
	if (v->cur_speed < 10) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1169
		v->vehstatus &= ~VS_AIRCRAFT_BROKEN;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1170
		v->breakdown_ctr = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1171
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1172
	}
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->tick_counter & 0x1F) == 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1175
		CreateEffectVehicleRel(v,
2654
1370de8783d3 (svn r3196) Use structs instead of magic offsets into arrays
tron
parents: 2639
diff changeset
  1176
			smoke_pos[v->direction].x,
1370de8783d3 (svn r3196) Use structs instead of magic offsets into arrays
tron
parents: 2639
diff changeset
  1177
			smoke_pos[v->direction].y,
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1178
			2,
1359
52782e5cf7c9 (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
  1179
			EV_SMOKE
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1180
		);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1181
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1182
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1183
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1184
static void ProcessAircraftOrder(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1185
{
3005
2590b9b4823b (svn r3585) Similar changes to the order handlers of the other vehicle types like r3584
tron
parents: 2989
diff changeset
  1186
	switch (v->current_order.type) {
2590b9b4823b (svn r3585) Similar changes to the order handlers of the other vehicle types like r3584
tron
parents: 2989
diff changeset
  1187
		case OT_GOTO_DEPOT:
2590b9b4823b (svn r3585) Similar changes to the order handlers of the other vehicle types like r3584
tron
parents: 2989
diff changeset
  1188
			if (!(v->current_order.flags & OF_PART_OF_ORDERS)) return;
2590b9b4823b (svn r3585) Similar changes to the order handlers of the other vehicle types like r3584
tron
parents: 2989
diff changeset
  1189
			if (v->current_order.flags & OF_SERVICE_IF_NEEDED &&
2590b9b4823b (svn r3585) Similar changes to the order handlers of the other vehicle types like r3584
tron
parents: 2989
diff changeset
  1190
					!VehicleNeedsService(v)) {
2590b9b4823b (svn r3585) Similar changes to the order handlers of the other vehicle types like r3584
tron
parents: 2989
diff changeset
  1191
				v->cur_order_index++;
2590b9b4823b (svn r3585) Similar changes to the order handlers of the other vehicle types like r3584
tron
parents: 2989
diff changeset
  1192
			}
2590b9b4823b (svn r3585) Similar changes to the order handlers of the other vehicle types like r3584
tron
parents: 2989
diff changeset
  1193
			break;
2590b9b4823b (svn r3585) Similar changes to the order handlers of the other vehicle types like r3584
tron
parents: 2989
diff changeset
  1194
2590b9b4823b (svn r3585) Similar changes to the order handlers of the other vehicle types like r3584
tron
parents: 2989
diff changeset
  1195
		case OT_LOADING: return;
4351
c9799dd53eec (svn r6052) -Codechange: change OrderType (order->type) in a typedef
truelight
parents: 4346
diff changeset
  1196
c9799dd53eec (svn r6052) -Codechange: change OrderType (order->type) in a typedef
truelight
parents: 4346
diff changeset
  1197
		default: break;
1151
06c115ce7b7a (svn r1652) Fix: train and road vehicle lists are now redrawn when vehicles enters/leaves a depot (Hackykid)
bjarni
parents: 1150
diff changeset
  1198
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
  1199
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1200
	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
  1201
6087
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
  1202
	const Order *order = GetVehicleOrder(v, v->cur_order_index);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1203
1024
5e446b5b3ec5 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1204
	if (order == NULL) {
555
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1205
		v->current_order.type = OT_NOTHING;
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1206
		v->current_order.flags = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1207
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1208
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1209
3005
2590b9b4823b (svn r3585) Similar changes to the order handlers of the other vehicle types like r3584
tron
parents: 2989
diff changeset
  1210
	if (order->type == OT_DUMMY && !CheckForValidOrders(v)) CrashAirplane(v);
901
0e42c4b151e3 (svn r1387) Fix: Airplanes now also crash when only one invalid order is in the schedule
dominik
parents: 899
diff changeset
  1211
4527
fa30d19685c2 (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
  1212
	if (order->type  == v->current_order.type  &&
fa30d19685c2 (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
  1213
			order->flags == v->current_order.flags &&
fa30d19685c2 (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
  1214
			order->dest  == v->current_order.dest)
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
1024
5e446b5b3ec5 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1217
	v->current_order = *order;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1218
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1219
	// orders are changed in flight, ensure going to the right station
1024
5e446b5b3ec5 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1220
	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
  1221
		AircraftNextAirportPos_and_Order(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1222
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1223
1024
5e446b5b3ec5 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1224
	InvalidateVehicleOrder(v);
5e446b5b3ec5 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1225
1055
cc4f60cc9102 (svn r1556) -Fix: Vehicle list updates should now really work
Celestar
parents: 1053
diff changeset
  1226
	InvalidateWindowClasses(WC_AIRCRAFT_LIST);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1227
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1228
3722
0984901a2744 (svn r4693) - NewGRF: invalidate aircraft in viewports during loading stages so that graphic changes are visible. (mart3p)
peter1138
parents: 3710
diff changeset
  1229
/** Mark all views dirty for an aircraft.
0984901a2744 (svn r4693) - NewGRF: invalidate aircraft in viewports during loading stages so that graphic changes are visible. (mart3p)
peter1138
parents: 3710
diff changeset
  1230
 * @param v vehicle to be redrawn.
0984901a2744 (svn r4693) - NewGRF: invalidate aircraft in viewports during loading stages so that graphic changes are visible. (mart3p)
peter1138
parents: 3710
diff changeset
  1231
 */
0984901a2744 (svn r4693) - NewGRF: invalidate aircraft in viewports during loading stages so that graphic changes are visible. (mart3p)
peter1138
parents: 3710
diff changeset
  1232
static void MarkAircraftDirty(Vehicle *v)
0984901a2744 (svn r4693) - NewGRF: invalidate aircraft in viewports during loading stages so that graphic changes are visible. (mart3p)
peter1138
parents: 3710
diff changeset
  1233
{
0984901a2744 (svn r4693) - NewGRF: invalidate aircraft in viewports during loading stages so that graphic changes are visible. (mart3p)
peter1138
parents: 3710
diff changeset
  1234
		v->cur_image = GetAircraftImage(v, v->direction);
5854
9eee280920f0 (svn r8428) -Codechange: Add proper names to aircraft subtypes instead of magic numbers and add a function IsNormalAircraft() which tells us whether the aircraft is in fact some flying device or a rotor/shadow.
Darkvater
parents: 5754
diff changeset
  1235
		if (v->subtype == AIR_HELICOPTER) v->next->next->cur_image = GetRotorImage(v);
3722
0984901a2744 (svn r4693) - NewGRF: invalidate aircraft in viewports during loading stages so that graphic changes are visible. (mart3p)
peter1138
parents: 3710
diff changeset
  1236
		MarkAllViewportsDirty(v->left_coord, v->top_coord, v->right_coord + 1, v->bottom_coord + 1);
0984901a2744 (svn r4693) - NewGRF: invalidate aircraft in viewports during loading stages so that graphic changes are visible. (mart3p)
peter1138
parents: 3710
diff changeset
  1237
}
0984901a2744 (svn r4693) - NewGRF: invalidate aircraft in viewports during loading stages so that graphic changes are visible. (mart3p)
peter1138
parents: 3710
diff changeset
  1238
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1239
static void HandleAircraftLoading(Vehicle *v, int mode)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1240
{
5856
c82e61d62cd3 (svn r8430) -Fix
tron
parents: 5854
diff changeset
  1241
	switch (v->current_order.type) {
5858
a48c5b18747a (svn r8434) -Fix (r8430): Missing braces caused compilation failure for some (most?) compilers...
peter1138
parents: 5856
diff changeset
  1242
		case OT_LOADING: {
5856
c82e61d62cd3 (svn r8430) -Fix
tron
parents: 5854
diff changeset
  1243
			if (mode != 0) return;
c82e61d62cd3 (svn r8430) -Fix
tron
parents: 5854
diff changeset
  1244
			if (--v->load_unload_time_rem != 0) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1245
5856
c82e61d62cd3 (svn r8430) -Fix
tron
parents: 5854
diff changeset
  1246
			if (CanFillVehicle(v) && (
c82e61d62cd3 (svn r8430) -Fix
tron
parents: 5854
diff changeset
  1247
						v->current_order.flags & OF_FULL_LOAD ||
c82e61d62cd3 (svn r8430) -Fix
tron
parents: 5854
diff changeset
  1248
						(_patches.gradual_loading && !HASBIT(v->load_status, LS_LOADING_FINISHED))
c82e61d62cd3 (svn r8430) -Fix
tron
parents: 5854
diff changeset
  1249
					)) {
c82e61d62cd3 (svn r8430) -Fix
tron
parents: 5854
diff changeset
  1250
				SET_EXPENSES_TYPE(EXPENSES_AIRCRAFT_INC);
c82e61d62cd3 (svn r8430) -Fix
tron
parents: 5854
diff changeset
  1251
				if (LoadUnloadVehicle(v, false)) {
c82e61d62cd3 (svn r8430) -Fix
tron
parents: 5854
diff changeset
  1252
					InvalidateWindow(WC_AIRCRAFT_LIST, v->owner);
c82e61d62cd3 (svn r8430) -Fix
tron
parents: 5854
diff changeset
  1253
					MarkAircraftDirty(v);
c82e61d62cd3 (svn r8430) -Fix
tron
parents: 5854
diff changeset
  1254
				}
c82e61d62cd3 (svn r8430) -Fix
tron
parents: 5854
diff changeset
  1255
				return;
3722
0984901a2744 (svn r4693) - NewGRF: invalidate aircraft in viewports during loading stages so that graphic changes are visible. (mart3p)
peter1138
parents: 3710
diff changeset
  1256
			}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1257
555
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1258
			Order b = v->current_order;
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1259
			v->current_order.type = OT_NOTHING;
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1260
			v->current_order.flags = 0;
3722
0984901a2744 (svn r4693) - NewGRF: invalidate aircraft in viewports during loading stages so that graphic changes are visible. (mart3p)
peter1138
parents: 3710
diff changeset
  1261
			MarkAircraftDirty(v);
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1262
			if (!(b.flags & OF_NON_STOP)) return;
5856
c82e61d62cd3 (svn r8430) -Fix
tron
parents: 5854
diff changeset
  1263
			break;
5858
a48c5b18747a (svn r8434) -Fix (r8430): Missing braces caused compilation failure for some (most?) compilers...
peter1138
parents: 5856
diff changeset
  1264
		}
5856
c82e61d62cd3 (svn r8430) -Fix
tron
parents: 5854
diff changeset
  1265
c82e61d62cd3 (svn r8430) -Fix
tron
parents: 5854
diff changeset
  1266
		case OT_DUMMY: break;
c82e61d62cd3 (svn r8430) -Fix
tron
parents: 5854
diff changeset
  1267
c82e61d62cd3 (svn r8430) -Fix
tron
parents: 5854
diff changeset
  1268
		default: return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1269
	}
5856
c82e61d62cd3 (svn r8430) -Fix
tron
parents: 5854
diff changeset
  1270
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1271
	v->cur_order_index++;
1024
5e446b5b3ec5 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 1020
diff changeset
  1272
	InvalidateVehicleOrder(v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1273
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1274
899
69db9cb6bc9d (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
  1275
static void CrashAirplane(Vehicle *v)
69db9cb6bc9d (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
  1276
{
69db9cb6bc9d (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
  1277
	v->vehstatus |= VS_CRASHED;
69db9cb6bc9d (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
  1278
	v->u.air.crashed_counter = 0;
69db9cb6bc9d (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
  1279
1359
52782e5cf7c9 (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
  1280
	CreateEffectVehicleRel(v, 4, 4, 8, EV_EXPLOSION_LARGE);
899
69db9cb6bc9d (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
69db9cb6bc9d (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
	InvalidateWindow(WC_VEHICLE_VIEW, v->index);
69db9cb6bc9d (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
6087
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
  1284
	uint amt = 2;
899
69db9cb6bc9d (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
	if (v->cargo_type == CT_PASSENGERS) amt += v->cargo_count;
69db9cb6bc9d (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
	SetDParam(0, amt);
69db9cb6bc9d (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
69db9cb6bc9d (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
	v->cargo_count = 0;
5951
f842b246bfd7 (svn r8617) -Fix
tron
parents: 5945
diff changeset
  1289
	v->next->cargo_count = 0;
6087
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
  1290
	const Station *st = GetStation(v->u.air.targetairport);
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
  1291
	StringID newsitem;
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1292
	if (st->airport_tile == 0) {
899
69db9cb6bc9d (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
		newsitem = STR_PLANE_CRASH_OUT_OF_FUEL;
69db9cb6bc9d (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
	} else {
69db9cb6bc9d (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(1, st->index);
69db9cb6bc9d (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
		newsitem = STR_A034_PLANE_CRASH_DIE_IN_FIREBALL;
69db9cb6bc9d (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
	}
69db9cb6bc9d (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
69db9cb6bc9d (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
  1299
	SetDParam(1, st->index);
69db9cb6bc9d (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
  1300
	AddNewsItem(newsitem,
69db9cb6bc9d (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
		NEWS_FLAGS(NM_THIN, NF_VIEWPORT|NF_VEHICLE, NT_ACCIDENT, 0),
69db9cb6bc9d (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
		v->index,
69db9cb6bc9d (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
		0);
69db9cb6bc9d (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
69db9cb6bc9d (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
	SndPlayVehicleFx(SND_12_EXPLOSION, v);
69db9cb6bc9d (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
}
69db9cb6bc9d (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
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1308
static void MaybeCrashAirplane(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1309
{
6087
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
  1310
	Station *st = GetStation(v->u.air.targetairport);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1311
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1312
	//FIXME -- MaybeCrashAirplane -> increase crashing chances of very modern airplanes on smaller than AT_METROPOLITAN airports
6087
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
  1313
	uint16 prob = 0x10000 / 1500;
6032
fe6a454ac1ba (svn r8755) -Fix
tron
parents: 6027
diff changeset
  1314
	if (st->Airport()->flags & AirportFTAClass::SHORT_STRIP &&
6025
414b34699e87 (svn r8748) -Fix
tron
parents: 6024
diff changeset
  1315
			AircraftVehInfo(v->engine_type)->subtype & AIR_FAST &&
414b34699e87 (svn r8748) -Fix
tron
parents: 6024
diff changeset
  1316
			!_cheats.no_jetcrash.value) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1317
		prob = 0x10000 / 20;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1318
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1319
2484
0e45d70ae908 (svn r3010) Get rid of quite some dubious casts, either by using GB(), proper types or just removing them
tron
parents: 2477
diff changeset
  1320
	if (GB(Random(), 0, 16) > prob) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1321
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1322
	// Crash the airplane. Remove all goods stored at the station.
6087
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
  1323
	for (uint i = 0; i != NUM_CARGO; i++) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1324
		st->goods[i].rating = 1;
2504
5ae89f643cdc (svn r3030) More work for GB/SB, this time concerning the waiting_acceptance attribute of stations
tron
parents: 2484
diff changeset
  1325
		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
  1326
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1327
899
69db9cb6bc9d (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
  1328
	CrashAirplane(v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1329
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1330
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1331
// we've landed and just arrived at a terminal
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1332
static void AircraftEntersTerminal(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1333
{
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1334
	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
  1335
6087
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
  1336
	Station *st = GetStation(v->u.air.targetairport);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1337
	v->last_station_visited = v->u.air.targetairport;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1338
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1339
	/* Check if station was ever visited before */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1340
	if (!(st->had_vehicle_of_type & HVOT_AIRCRAFT)) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1341
		uint32 flags;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1342
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1343
		st->had_vehicle_of_type |= HVOT_AIRCRAFT;
534
306bc86eb23e (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 507
diff changeset
  1344
		SetDParam(0, st->index);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1345
		// show newsitem of celebrating citizens
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1346
		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
  1347
		AddNewsItem(
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1348
			STR_A033_CITIZENS_CELEBRATE_FIRST,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1349
			flags,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1350
			v->index,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1351
			0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1352
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1353
6087
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
  1354
	Order old_order = v->current_order;
5651
79496e6d8a56 (svn r8110) -Codechange: direct Vehicle::current_order.type changes (to OT_LOADING and OT_LEAVESTATION) replaced by v->BeginLoading() and v->LeaveStation() calls. This should allow easy hooking of those state transitions in order to maintain vehicle loading queue.
KUDr
parents: 5601
diff changeset
  1355
	v->BeginLoading();
555
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1356
	v->current_order.flags = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1357
555
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1358
	if (old_order.type == OT_GOTO_STATION &&
4527
fa30d19685c2 (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
  1359
			v->current_order.dest == v->last_station_visited) {
555
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1360
		v->current_order.flags =
3872
a407dc67b041 (svn r4914) -Fix (FS#180) Aircraft can now serve as feeders
celestar
parents: 3870
diff changeset
  1361
			(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
  1362
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1363
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1364
	SET_EXPENSES_TYPE(EXPENSES_AIRCRAFT_INC);
5211
fb4dc0ca975d (svn r7326) -Feature: Add support for gradual (un)loading of vehicles (Maedhros)
peter1138
parents: 5198
diff changeset
  1365
	LoadUnloadVehicle(v, true);
3722
0984901a2744 (svn r4693) - NewGRF: invalidate aircraft in viewports during loading stages so that graphic changes are visible. (mart3p)
peter1138
parents: 3710
diff changeset
  1366
	MarkAircraftDirty(v);
755
80091de50044 (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
  1367
	InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
1055
cc4f60cc9102 (svn r1556) -Fix: Vehicle list updates should now really work
Celestar
parents: 1053
diff changeset
  1368
	InvalidateWindowClasses(WC_AIRCRAFT_LIST);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1369
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1370
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1371
static void AircraftLand(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1372
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1373
	v->sprite_width = v->sprite_height = 2;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1374
}
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
static void AircraftLandAirplane(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1377
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1378
	AircraftLand(v);
4656
9c1d8c4d3e60 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4574
diff changeset
  1379
	if (!PlayVehicleSound(v, VSE_TOUCHDOWN)) {
9c1d8c4d3e60 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4574
diff changeset
  1380
		SndPlayVehicleFx(SND_17_SKID_PLANE, v);
9c1d8c4d3e60 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4574
diff changeset
  1381
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1382
	MaybeCrashAirplane(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1383
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1384
6036
333637e97400 (svn r8759) -Feature: Allow airports to have up to 4 entry points depending on approach direction. Note: they're not yet used, but will be soon
celestar
parents: 6032
diff changeset
  1385
/**
333637e97400 (svn r8759) -Feature: Allow airports to have up to 4 entry points depending on approach direction. Note: they're not yet used, but will be soon
celestar
parents: 6032
diff changeset
  1386
 * Find the entry point to an airport depending on direction which
333637e97400 (svn r8759) -Feature: Allow airports to have up to 4 entry points depending on approach direction. Note: they're not yet used, but will be soon
celestar
parents: 6032
diff changeset
  1387
 * the airport is being approached from. Each airport can have up to
333637e97400 (svn r8759) -Feature: Allow airports to have up to 4 entry points depending on approach direction. Note: they're not yet used, but will be soon
celestar
parents: 6032
diff changeset
  1388
 * four entry points for its approach system so that approaching
333637e97400 (svn r8759) -Feature: Allow airports to have up to 4 entry points depending on approach direction. Note: they're not yet used, but will be soon
celestar
parents: 6032
diff changeset
  1389
 * aircraft do not fly through each other or are forced to do 180
333637e97400 (svn r8759) -Feature: Allow airports to have up to 4 entry points depending on approach direction. Note: they're not yet used, but will be soon
celestar
parents: 6032
diff changeset
  1390
 * degree turns during the approach. The arrivals are grouped into
333637e97400 (svn r8759) -Feature: Allow airports to have up to 4 entry points depending on approach direction. Note: they're not yet used, but will be soon
celestar
parents: 6032
diff changeset
  1391
 * four sectors dependent on the DiagDirection from which the airport
333637e97400 (svn r8759) -Feature: Allow airports to have up to 4 entry points depending on approach direction. Note: they're not yet used, but will be soon
celestar
parents: 6032
diff changeset
  1392
 * is approached.
333637e97400 (svn r8759) -Feature: Allow airports to have up to 4 entry points depending on approach direction. Note: they're not yet used, but will be soon
celestar
parents: 6032
diff changeset
  1393
 *
333637e97400 (svn r8759) -Feature: Allow airports to have up to 4 entry points depending on approach direction. Note: they're not yet used, but will be soon
celestar
parents: 6032
diff changeset
  1394
 * @param v   The vehicle that is approaching the airport
333637e97400 (svn r8759) -Feature: Allow airports to have up to 4 entry points depending on approach direction. Note: they're not yet used, but will be soon
celestar
parents: 6032
diff changeset
  1395
 * @param apc The Airport Class being approached.
333637e97400 (svn r8759) -Feature: Allow airports to have up to 4 entry points depending on approach direction. Note: they're not yet used, but will be soon
celestar
parents: 6032
diff changeset
  1396
 * @returns   The index of the entry point
333637e97400 (svn r8759) -Feature: Allow airports to have up to 4 entry points depending on approach direction. Note: they're not yet used, but will be soon
celestar
parents: 6032
diff changeset
  1397
 */
333637e97400 (svn r8759) -Feature: Allow airports to have up to 4 entry points depending on approach direction. Note: they're not yet used, but will be soon
celestar
parents: 6032
diff changeset
  1398
static byte AircraftGetEntryPoint(const Vehicle *v, const AirportFTAClass *apc)
333637e97400 (svn r8759) -Feature: Allow airports to have up to 4 entry points depending on approach direction. Note: they're not yet used, but will be soon
celestar
parents: 6032
diff changeset
  1399
{
333637e97400 (svn r8759) -Feature: Allow airports to have up to 4 entry points depending on approach direction. Note: they're not yet used, but will be soon
celestar
parents: 6032
diff changeset
  1400
	assert(v != NULL);
333637e97400 (svn r8759) -Feature: Allow airports to have up to 4 entry points depending on approach direction. Note: they're not yet used, but will be soon
celestar
parents: 6032
diff changeset
  1401
	assert(apc != NULL);
333637e97400 (svn r8759) -Feature: Allow airports to have up to 4 entry points depending on approach direction. Note: they're not yet used, but will be soon
celestar
parents: 6032
diff changeset
  1402
6037
71545e66b9c3 (svn r8763) -Fix
tron
parents: 6036
diff changeset
  1403
	const Station *st = GetStation(v->u.air.targetairport);
6036
333637e97400 (svn r8759) -Feature: Allow airports to have up to 4 entry points depending on approach direction. Note: they're not yet used, but will be soon
celestar
parents: 6032
diff changeset
  1404
	/* Make sure we don't go to 0,0 if the airport has been removed. */
6037
71545e66b9c3 (svn r8763) -Fix
tron
parents: 6036
diff changeset
  1405
	TileIndex tile = (st->airport_tile != 0) ? st->airport_tile : st->xy;
6036
333637e97400 (svn r8759) -Feature: Allow airports to have up to 4 entry points depending on approach direction. Note: they're not yet used, but will be soon
celestar
parents: 6032
diff changeset
  1406
6037
71545e66b9c3 (svn r8763) -Fix
tron
parents: 6036
diff changeset
  1407
	int delta_x = v->x_pos - TileX(tile) * TILE_SIZE;
71545e66b9c3 (svn r8763) -Fix
tron
parents: 6036
diff changeset
  1408
	int delta_y = v->y_pos - TileY(tile) * TILE_SIZE;
6036
333637e97400 (svn r8759) -Feature: Allow airports to have up to 4 entry points depending on approach direction. Note: they're not yet used, but will be soon
celestar
parents: 6032
diff changeset
  1409
6037
71545e66b9c3 (svn r8763) -Fix
tron
parents: 6036
diff changeset
  1410
	DiagDirection dir;
6036
333637e97400 (svn r8759) -Feature: Allow airports to have up to 4 entry points depending on approach direction. Note: they're not yet used, but will be soon
celestar
parents: 6032
diff changeset
  1411
	if (abs(delta_y) < abs(delta_x)) {
333637e97400 (svn r8759) -Feature: Allow airports to have up to 4 entry points depending on approach direction. Note: they're not yet used, but will be soon
celestar
parents: 6032
diff changeset
  1412
		/* We are northeast or southwest of the airport */
6037
71545e66b9c3 (svn r8763) -Fix
tron
parents: 6036
diff changeset
  1413
		dir = delta_x < 0 ? DIAGDIR_NE : DIAGDIR_SW;
71545e66b9c3 (svn r8763) -Fix
tron
parents: 6036
diff changeset
  1414
	} else {
71545e66b9c3 (svn r8763) -Fix
tron
parents: 6036
diff changeset
  1415
		/* We are northwest or southeast of the airport */
71545e66b9c3 (svn r8763) -Fix
tron
parents: 6036
diff changeset
  1416
		dir = delta_y < 0 ? DIAGDIR_NW : DIAGDIR_SE;
6036
333637e97400 (svn r8759) -Feature: Allow airports to have up to 4 entry points depending on approach direction. Note: they're not yet used, but will be soon
celestar
parents: 6032
diff changeset
  1417
	}
6037
71545e66b9c3 (svn r8763) -Fix
tron
parents: 6036
diff changeset
  1418
	return apc->entry_points[dir];
6036
333637e97400 (svn r8759) -Feature: Allow airports to have up to 4 entry points depending on approach direction. Note: they're not yet used, but will be soon
celestar
parents: 6032
diff changeset
  1419
}
333637e97400 (svn r8759) -Feature: Allow airports to have up to 4 entry points depending on approach direction. Note: they're not yet used, but will be soon
celestar
parents: 6032
diff changeset
  1420
333637e97400 (svn r8759) -Feature: Allow airports to have up to 4 entry points depending on approach direction. Note: they're not yet used, but will be soon
celestar
parents: 6032
diff changeset
  1421
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1422
// 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
  1423
static void AircraftNextAirportPos_and_Order(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1424
{
555
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1425
	if (v->current_order.type == OT_GOTO_STATION ||
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1426
			v->current_order.type == OT_GOTO_DEPOT)
4527
fa30d19685c2 (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
  1427
		v->u.air.targetairport = v->current_order.dest;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1428
6032
fe6a454ac1ba (svn r8755) -Fix
tron
parents: 6027
diff changeset
  1429
	const AirportFTAClass *apc = GetStation(v->u.air.targetairport)->Airport();
6036
333637e97400 (svn r8759) -Feature: Allow airports to have up to 4 entry points depending on approach direction. Note: they're not yet used, but will be soon
celestar
parents: 6032
diff changeset
  1430
	v->u.air.pos = v->u.air.previous_pos = AircraftGetEntryPoint(v, apc);
0
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
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1433
static void AircraftLeaveHangar(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1434
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1435
	v->cur_speed = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1436
	v->subspeed = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1437
	v->progress = 0;
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5585
diff changeset
  1438
	v->direction = DIR_SE;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1439
	v->vehstatus &= ~VS_HIDDEN;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1440
	{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1441
		Vehicle *u = v->next;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1442
		u->vehstatus &= ~VS_HIDDEN;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1443
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1444
		// Rotor blades
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1445
		u = u->next;
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1446
		if (u != NULL) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1447
			u->vehstatus &= ~VS_HIDDEN;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1448
			u->cur_speed = 80;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1449
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1450
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1451
578
1e66514eb621 (svn r998) now vehicles are serviced both when entering and when leaving depots to prevent that vehicles might need service when leaving after a long stay (ln--)
bjarni
parents: 555
diff changeset
  1452
	VehicleServiceInDepot(v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1453
	SetAircraftPosition(v, v->x_pos, v->y_pos, v->z_pos);
4739
e626ef1b522e (svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents: 4732
diff changeset
  1454
	InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
1055
cc4f60cc9102 (svn r1556) -Fix: Vehicle list updates should now really work
Celestar
parents: 1053
diff changeset
  1455
	InvalidateWindowClasses(WC_AIRCRAFT_LIST);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1456
}
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
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1459
////////////////////////////////////////////////////////////////////////////////
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1460
///////////////////   AIRCRAFT MOVEMENT SCHEME  ////////////////////////////////
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1461
////////////////////////////////////////////////////////////////////////////////
4842
79c4c9167d93 (svn r6768) -Cleanup: For airports change *Airport to *apc (variable naming coding style
Darkvater
parents: 4739
diff changeset
  1462
static void AircraftEventHandler_EnterTerminal(Vehicle *v, const AirportFTAClass *apc)
0
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
	AircraftEntersTerminal(v);
4842
79c4c9167d93 (svn r6768) -Cleanup: For airports change *Airport to *apc (variable naming coding style
Darkvater
parents: 4739
diff changeset
  1465
	v->u.air.state = apc->layout[v->u.air.pos].heading;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1466
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1467
4842
79c4c9167d93 (svn r6768) -Cleanup: For airports change *Airport to *apc (variable naming coding style
Darkvater
parents: 4739
diff changeset
  1468
static void AircraftEventHandler_EnterHangar(Vehicle *v, const AirportFTAClass *apc)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1469
{
4725
40cccaaa042c (svn r6637) -Codechange: merged all (vehicle type)EnterDepot into VehicleEnterDepot()
bjarni
parents: 4712
diff changeset
  1470
	VehicleEnterDepot(v);
4842
79c4c9167d93 (svn r6768) -Cleanup: For airports change *Airport to *apc (variable naming coding style
Darkvater
parents: 4739
diff changeset
  1471
	v->u.air.state = apc->layout[v->u.air.pos].heading;
0
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
// In an Airport Hangar
4842
79c4c9167d93 (svn r6768) -Cleanup: For airports change *Airport to *apc (variable naming coding style
Darkvater
parents: 4739
diff changeset
  1475
static void AircraftEventHandler_InHangar(Vehicle *v, const AirportFTAClass *apc)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1476
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1477
	// if we just arrived, execute EnterHangar first
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1478
	if (v->u.air.previous_pos != v->u.air.pos) {
4842
79c4c9167d93 (svn r6768) -Cleanup: For airports change *Airport to *apc (variable naming coding style
Darkvater
parents: 4739
diff changeset
  1479
		AircraftEventHandler_EnterHangar(v, apc);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1480
		return;
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
555
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1483
	// if we were sent to the depot, stay there
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1484
	if (v->current_order.type == OT_GOTO_DEPOT && (v->vehstatus & VS_STOPPED)) {
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1485
		v->current_order.type = OT_NOTHING;
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1486
		v->current_order.flags = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1487
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1488
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1489
555
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1490
	if (v->current_order.type != OT_GOTO_STATION &&
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1491
			v->current_order.type != OT_GOTO_DEPOT)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1492
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1493
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1494
	// if the block of the next position is busy, stay put
4842
79c4c9167d93 (svn r6768) -Cleanup: For airports change *Airport to *apc (variable naming coding style
Darkvater
parents: 4739
diff changeset
  1495
	if (AirportHasBlock(v, &apc->layout[v->u.air.pos], apc)) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1496
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1497
	// We are already at the target airport, we need to find a terminal
4527
fa30d19685c2 (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
  1498
	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
  1499
		// FindFreeTerminal:
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1500
		// 1. Find a free terminal, 2. Occupy it, 3. Set the vehicle's state to that terminal
5854
9eee280920f0 (svn r8428) -Codechange: Add proper names to aircraft subtypes instead of magic numbers and add a function IsNormalAircraft() which tells us whether the aircraft is in fact some flying device or a rotor/shadow.
Darkvater
parents: 5754
diff changeset
  1501
		if (v->subtype == AIR_HELICOPTER) {
9eee280920f0 (svn r8428) -Codechange: Add proper names to aircraft subtypes instead of magic numbers and add a function IsNormalAircraft() which tells us whether the aircraft is in fact some flying device or a rotor/shadow.
Darkvater
parents: 5754
diff changeset
  1502
			if (!AirportFindFreeHelipad(v, apc)) return; // helicopter
9eee280920f0 (svn r8428) -Codechange: Add proper names to aircraft subtypes instead of magic numbers and add a function IsNormalAircraft() which tells us whether the aircraft is in fact some flying device or a rotor/shadow.
Darkvater
parents: 5754
diff changeset
  1503
		} else {
4842
79c4c9167d93 (svn r6768) -Cleanup: For airports change *Airport to *apc (variable naming coding style
Darkvater
parents: 4739
diff changeset
  1504
			if (!AirportFindFreeTerminal(v, apc)) return; // airplane
2549
5587f9a38563 (svn r3078) Some more stuff, which piled up:
tron
parents: 2517
diff changeset
  1505
		}
5587f9a38563 (svn r3078) Some more stuff, which piled up:
tron
parents: 2517
diff changeset
  1506
	} else { // Else prepare for launch.
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1507
		// airplane goto state takeoff, helicopter to helitakeoff
5854
9eee280920f0 (svn r8428) -Codechange: Add proper names to aircraft subtypes instead of magic numbers and add a function IsNormalAircraft() which tells us whether the aircraft is in fact some flying device or a rotor/shadow.
Darkvater
parents: 5754
diff changeset
  1508
		v->u.air.state = (v->subtype == AIR_HELICOPTER) ? HELITAKEOFF : TAKEOFF;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1509
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1510
	AircraftLeaveHangar(v);
4842
79c4c9167d93 (svn r6768) -Cleanup: For airports change *Airport to *apc (variable naming coding style
Darkvater
parents: 4739
diff changeset
  1511
	AirportMove(v, apc);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1512
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1513
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1514
// At one of the Airport's Terminals
4842
79c4c9167d93 (svn r6768) -Cleanup: For airports change *Airport to *apc (variable naming coding style
Darkvater
parents: 4739
diff changeset
  1515
static void AircraftEventHandler_AtTerminal(Vehicle *v, const AirportFTAClass *apc)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1516
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1517
	// if we just arrived, execute EnterTerminal first
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1518
	if (v->u.air.previous_pos != v->u.air.pos) {
4842
79c4c9167d93 (svn r6768) -Cleanup: For airports change *Airport to *apc (variable naming coding style
Darkvater
parents: 4739
diff changeset
  1519
		AircraftEventHandler_EnterTerminal(v, apc);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1520
		// 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
  1521
		// and get serviced at the same time - patch setting
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1522
		if (_patches.serviceathelipad) {
5854
9eee280920f0 (svn r8428) -Codechange: Add proper names to aircraft subtypes instead of magic numbers and add a function IsNormalAircraft() which tells us whether the aircraft is in fact some flying device or a rotor/shadow.
Darkvater
parents: 5754
diff changeset
  1523
			if (v->subtype == AIR_HELICOPTER && apc->helipads != NULL) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1524
				// an exerpt of ServiceAircraft, without the invisibility stuff
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1525
				v->date_of_last_service = _date;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1526
				v->breakdowns_since_last_service = 0;
1926
530480d14685 (svn r2432) Use GetEngine() instead of DEREF_ENGINE() or even _engines[]
tron
parents: 1901
diff changeset
  1527
				v->reliability = GetEngine(v->engine_type)->reliability;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1528
				InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1529
			}
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
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1532
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1533
555
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1534
	if (v->current_order.type == OT_NOTHING) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1535
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1536
	// if the block of the next position is busy, stay put
4843
4bef85baee3f (svn r6769) -Codechange: Minor syntax, const correctness, variable localization, coding
Darkvater
parents: 4842
diff changeset
  1537
	if (AirportHasBlock(v, &apc->layout[v->u.air.pos], apc)) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1538
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1539
	// 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
  1540
	// ---> start moving
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1541
555
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1542
	switch (v->current_order.type) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1543
		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
  1544
			// airplane goto state takeoff, helicopter to helitakeoff
5854
9eee280920f0 (svn r8428) -Codechange: Add proper names to aircraft subtypes instead of magic numbers and add a function IsNormalAircraft() which tells us whether the aircraft is in fact some flying device or a rotor/shadow.
Darkvater
parents: 5754
diff changeset
  1545
			v->u.air.state = (v->subtype == AIR_HELICOPTER) ? HELITAKEOFF : TAKEOFF;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1546
			break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1547
		case OT_GOTO_DEPOT:   // visit hangar for serivicing, sale, etc.
4527
fa30d19685c2 (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
  1548
			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
  1549
				v->u.air.state = HANGAR;
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1550
			} else {
5854
9eee280920f0 (svn r8428) -Codechange: Add proper names to aircraft subtypes instead of magic numbers and add a function IsNormalAircraft() which tells us whether the aircraft is in fact some flying device or a rotor/shadow.
Darkvater
parents: 5754
diff changeset
  1551
				v->u.air.state = (v->subtype == AIR_HELICOPTER) ? HELITAKEOFF : TAKEOFF;
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1552
			}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1553
			break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1554
		default:  // orders have been deleted (no orders), goto depot and don't bother us
555
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1555
			v->current_order.type = OT_NOTHING;
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1556
			v->current_order.flags = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1557
			v->u.air.state = HANGAR;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1558
	}
4842
79c4c9167d93 (svn r6768) -Cleanup: For airports change *Airport to *apc (variable naming coding style
Darkvater
parents: 4739
diff changeset
  1559
	AirportMove(v, apc);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1560
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1561
4842
79c4c9167d93 (svn r6768) -Cleanup: For airports change *Airport to *apc (variable naming coding style
Darkvater
parents: 4739
diff changeset
  1562
static void AircraftEventHandler_General(Vehicle *v, const AirportFTAClass *apc)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1563
{
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5259
diff changeset
  1564
	assert("OK, you shouldn't be here, check your Airport Scheme!" && 0);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1565
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1566
6087
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
  1567
static void AircraftEventHandler_TakeOff(Vehicle *v, const AirportFTAClass *apc)
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
  1568
{
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1569
	PlayAircraftSound(v); // play takeoffsound for airplanes
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1570
	v->u.air.state = STARTTAKEOFF;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1571
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1572
4842
79c4c9167d93 (svn r6768) -Cleanup: For airports change *Airport to *apc (variable naming coding style
Darkvater
parents: 4739
diff changeset
  1573
static void AircraftEventHandler_StartTakeOff(Vehicle *v, const AirportFTAClass *apc)
0
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
	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
  1576
	v->u.air.state = ENDTAKEOFF;
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
4842
79c4c9167d93 (svn r6768) -Cleanup: For airports change *Airport to *apc (variable naming coding style
Darkvater
parents: 4739
diff changeset
  1579
static void AircraftEventHandler_EndTakeOff(Vehicle *v, const AirportFTAClass *apc)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1580
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1581
	v->u.air.state = FLYING;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1582
	// 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
  1583
	AircraftNextAirportPos_and_Order(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1584
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1585
4842
79c4c9167d93 (svn r6768) -Cleanup: For airports change *Airport to *apc (variable naming coding style
Darkvater
parents: 4739
diff changeset
  1586
static void AircraftEventHandler_HeliTakeOff(Vehicle *v, const AirportFTAClass *apc)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1587
{
2475
efdcc705cb61 (svn r3001) s/Player*/const Player*/
tron
parents: 2464
diff changeset
  1588
	const Player* p = GetPlayer(v->owner);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1589
	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
  1590
	v->u.air.state = FLYING;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1591
	// 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
  1592
	AircraftNextAirportPos_and_Order(v);
1135
6c3b6e855c98 (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
  1593
6c3b6e855c98 (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
  1594
	// check if the aircraft needs to be replaced or renewed and send it to a hangar if needed
4111
221355fcade6 (svn r5454) -Fix: [ 1394231 ] Autorenew glitch on helicopters
bjarni
parents: 4095
diff changeset
  1595
	// unless it is due for renewal but the engine is no longer available
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1596
	if (v->owner == _local_player && (
2848
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2819
diff changeset
  1597
				EngineHasReplacementForPlayer(p, v->engine_type) ||
4111
221355fcade6 (svn r5454) -Fix: [ 1394231 ] Autorenew glitch on helicopters
bjarni
parents: 4095
diff changeset
  1598
				((p->engine_renew && v->age - v->max_age > p->engine_renew_months * 30) &&
221355fcade6 (svn r5454) -Fix: [ 1394231 ] Autorenew glitch on helicopters
bjarni
parents: 4095
diff changeset
  1599
				HASBIT(GetEngine(v->engine_type)->player_avail, _local_player))
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1600
			)) {
1520
d88442095697 (svn r2024) -Fix: [autoreplace] reverted all changes involving v->set_for_replacement as they caused desyncs.
bjarni
parents: 1401
diff changeset
  1601
		_current_player = _local_player;
4506
e6a56518135c (svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents: 4463
diff changeset
  1602
		DoCommandP(v->tile, v->index, DEPOT_SERVICE | DEPOT_LOCATE_HANGAR, NULL, CMD_SEND_AIRCRAFT_TO_HANGAR | CMD_SHOW_NO_ERROR);
1520
d88442095697 (svn r2024) -Fix: [autoreplace] reverted all changes involving v->set_for_replacement as they caused desyncs.
bjarni
parents: 1401
diff changeset
  1603
		_current_player = OWNER_NONE;
1135
6c3b6e855c98 (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
  1604
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1605
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1606
4842
79c4c9167d93 (svn r6768) -Cleanup: For airports change *Airport to *apc (variable naming coding style
Darkvater
parents: 4739
diff changeset
  1607
static void AircraftEventHandler_Flying(Vehicle *v, const AirportFTAClass *apc)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1608
{
6087
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
  1609
	Station *st = GetStation(v->u.air.targetairport);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1610
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1611
	// runway busy or not allowed to use this airstation, circle
6027
ff15b8c7c087 (svn r8750) -Fix (r8747): PLANES defined in wingdi.h caused compilation error under Win32. Changed to AIRPLANES
KUDr
parents: 6025
diff changeset
  1612
	if (apc->flags & (v->subtype == AIR_HELICOPTER ? AirportFTAClass::HELICOPTERS : AirportFTAClass::AIRPLANES) &&
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1613
			st->airport_tile != 0 &&
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1614
			(st->owner == OWNER_NONE || st->owner == v->owner)) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1615
		// {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
  1616
		// 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
  1617
		// it is possible to choose from multiple landing runways, so loop until a free one is found
6087
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
  1618
		byte landingtype = (v->subtype == AIR_HELICOPTER) ? HELILANDING : LANDING;
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
  1619
		const AirportFTA *current = apc->layout[v->u.air.pos].next;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1620
		while (current != NULL) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1621
			if (current->heading == landingtype) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1622
				// 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
  1623
				// we don't want that for plane in air
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1624
				// hack for speed thingie
6087
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
  1625
				uint16 tcur_speed = v->cur_speed;
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
  1626
				uint16 tsubspeed = v->subspeed;
4842
79c4c9167d93 (svn r6768) -Cleanup: For airports change *Airport to *apc (variable naming coding style
Darkvater
parents: 4739
diff changeset
  1627
				if (!AirportHasBlock(v, current, apc)) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1628
					v->u.air.state = landingtype; // LANDING / HELILANDING
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1629
					// 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
  1630
					// 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
  1631
					// 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
  1632
					v->u.air.pos = current->next_position;
4842
79c4c9167d93 (svn r6768) -Cleanup: For airports change *Airport to *apc (variable naming coding style
Darkvater
parents: 4739
diff changeset
  1633
					SETBITS(st->airport_flags, apc->layout[v->u.air.pos].block);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1634
					return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1635
				}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1636
				v->cur_speed = tcur_speed;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1637
				v->subspeed = tsubspeed;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1638
			}
4842
79c4c9167d93 (svn r6768) -Cleanup: For airports change *Airport to *apc (variable naming coding style
Darkvater
parents: 4739
diff changeset
  1639
			current = current->next;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1640
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1641
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1642
	v->u.air.state = FLYING;
4842
79c4c9167d93 (svn r6768) -Cleanup: For airports change *Airport to *apc (variable naming coding style
Darkvater
parents: 4739
diff changeset
  1643
	v->u.air.pos = apc->layout[v->u.air.pos].next_position;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1644
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1645
4842
79c4c9167d93 (svn r6768) -Cleanup: For airports change *Airport to *apc (variable naming coding style
Darkvater
parents: 4739
diff changeset
  1646
static void AircraftEventHandler_Landing(Vehicle *v, const AirportFTAClass *apc)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1647
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1648
	AircraftLandAirplane(v);  // maybe crash airplane
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1649
	v->u.air.state = ENDLANDING;
1195
54d50ac3311a (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
  1650
	// check if the aircraft needs to be replaced or renewed and send it to a hangar if needed
54d50ac3311a (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
  1651
	if (v->current_order.type != OT_GOTO_DEPOT && v->owner == _local_player) {
54d50ac3311a (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
  1652
		// only the vehicle owner needs to calculate the rest (locally)
6087
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
  1653
		const Player* p = GetPlayer(v->owner);
2848
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2819
diff changeset
  1654
		if (EngineHasReplacementForPlayer(p, v->engine_type) ||
2293
b48192aec903 (svn r2817) -Codechange: [autoreplace]: moved autoreplace and autorenew to serverside
bjarni
parents: 2244
diff changeset
  1655
			(p->engine_renew && v->age - v->max_age > (p->engine_renew_months * 30))) {
4412
cae52239a576 (svn r6165) -Feature: control click Goto Depot will now make the vehicle service
bjarni
parents: 4389
diff changeset
  1656
			// send the aircraft to the hangar at next airport
1195
54d50ac3311a (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
  1657
			_current_player = _local_player;
4506
e6a56518135c (svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents: 4463
diff changeset
  1658
			DoCommandP(v->tile, v->index, DEPOT_SERVICE, NULL, CMD_SEND_AIRCRAFT_TO_HANGAR | CMD_SHOW_NO_ERROR);
1195
54d50ac3311a (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
  1659
			_current_player = OWNER_NONE;
54d50ac3311a (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
  1660
		}
54d50ac3311a (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
  1661
	}
0
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
4842
79c4c9167d93 (svn r6768) -Cleanup: For airports change *Airport to *apc (variable naming coding style
Darkvater
parents: 4739
diff changeset
  1664
static void AircraftEventHandler_HeliLanding(Vehicle *v, const AirportFTAClass *apc)
0
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
	AircraftLand(v); // helicopters don't crash
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1667
	v->u.air.state = HELIENDLANDING;
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
4842
79c4c9167d93 (svn r6768) -Cleanup: For airports change *Airport to *apc (variable naming coding style
Darkvater
parents: 4739
diff changeset
  1670
static void AircraftEventHandler_EndLanding(Vehicle *v, const AirportFTAClass *apc)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1671
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1672
	// next block busy, don't do a thing, just wait
4842
79c4c9167d93 (svn r6768) -Cleanup: For airports change *Airport to *apc (variable naming coding style
Darkvater
parents: 4739
diff changeset
  1673
	if (AirportHasBlock(v, &apc->layout[v->u.air.pos], apc)) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1674
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1675
	// if going to terminal (OT_GOTO_STATION) choose one
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1676
	// 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
  1677
	// 2. not going for terminal (but depot, no order),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1678
	// --> get out of the way to the hangar.
555
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1679
	if (v->current_order.type == OT_GOTO_STATION) {
4842
79c4c9167d93 (svn r6768) -Cleanup: For airports change *Airport to *apc (variable naming coding style
Darkvater
parents: 4739
diff changeset
  1680
		if (AirportFindFreeTerminal(v, apc)) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1681
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1682
	v->u.air.state = HANGAR;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1683
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1684
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1685
4842
79c4c9167d93 (svn r6768) -Cleanup: For airports change *Airport to *apc (variable naming coding style
Darkvater
parents: 4739
diff changeset
  1686
static void AircraftEventHandler_HeliEndLanding(Vehicle *v, const AirportFTAClass *apc)
0
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
	// next block busy, don't do a thing, just wait
4842
79c4c9167d93 (svn r6768) -Cleanup: For airports change *Airport to *apc (variable naming coding style
Darkvater
parents: 4739
diff changeset
  1689
	if (AirportHasBlock(v, &apc->layout[v->u.air.pos], apc)) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1690
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1691
	// 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
  1692
	// 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
  1693
	// 2. not going for terminal (but depot, no order),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1694
	// --> 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
  1695
	// --> else TAKEOFF
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1696
	// 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
  1697
	// must go to a hangar.
555
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1698
	if (v->current_order.type == OT_GOTO_STATION) {
4842
79c4c9167d93 (svn r6768) -Cleanup: For airports change *Airport to *apc (variable naming coding style
Darkvater
parents: 4739
diff changeset
  1699
		if (AirportFindFreeHelipad(v, apc)) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1700
	}
4842
79c4c9167d93 (svn r6768) -Cleanup: For airports change *Airport to *apc (variable naming coding style
Darkvater
parents: 4739
diff changeset
  1701
	v->u.air.state = (apc->nof_depots != 0) ? HANGAR : HELITAKEOFF;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1702
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1703
4842
79c4c9167d93 (svn r6768) -Cleanup: For airports change *Airport to *apc (variable naming coding style
Darkvater
parents: 4739
diff changeset
  1704
typedef void AircraftStateHandler(Vehicle *v, const AirportFTAClass *apc);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1705
static AircraftStateHandler * const _aircraft_state_handlers[] = {
4344
7e123fec5b0b (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
  1706
	AircraftEventHandler_General,        // TO_ALL         =  0
7e123fec5b0b (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
  1707
	AircraftEventHandler_InHangar,       // HANGAR         =  1
7e123fec5b0b (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
  1708
	AircraftEventHandler_AtTerminal,     // TERM1          =  2
7e123fec5b0b (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
  1709
	AircraftEventHandler_AtTerminal,     // TERM2          =  3
7e123fec5b0b (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
  1710
	AircraftEventHandler_AtTerminal,     // TERM3          =  4
7e123fec5b0b (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
  1711
	AircraftEventHandler_AtTerminal,     // TERM4          =  5
7e123fec5b0b (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
  1712
	AircraftEventHandler_AtTerminal,     // TERM5          =  6
7e123fec5b0b (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
  1713
	AircraftEventHandler_AtTerminal,     // TERM6          =  7
7e123fec5b0b (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
  1714
	AircraftEventHandler_AtTerminal,     // HELIPAD1       =  8
7e123fec5b0b (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
  1715
	AircraftEventHandler_AtTerminal,     // HELIPAD2       =  9
7e123fec5b0b (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
  1716
	AircraftEventHandler_TakeOff,        // TAKEOFF        = 10
7e123fec5b0b (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
  1717
	AircraftEventHandler_StartTakeOff,   // STARTTAKEOFF   = 11
7e123fec5b0b (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
  1718
	AircraftEventHandler_EndTakeOff,     // ENDTAKEOFF     = 12
7e123fec5b0b (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
  1719
	AircraftEventHandler_HeliTakeOff,    // HELITAKEOFF    = 13
7e123fec5b0b (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
  1720
	AircraftEventHandler_Flying,         // FLYING         = 14
7e123fec5b0b (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
  1721
	AircraftEventHandler_Landing,        // LANDING        = 15
7e123fec5b0b (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
  1722
	AircraftEventHandler_EndLanding,     // ENDLANDING     = 16
7e123fec5b0b (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
  1723
	AircraftEventHandler_HeliLanding,    // HELILANDING    = 17
7e123fec5b0b (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
  1724
	AircraftEventHandler_HeliEndLanding, // HELIENDLANDING = 18
7e123fec5b0b (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
  1725
	AircraftEventHandler_AtTerminal,     // TERM7          = 19
7e123fec5b0b (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
  1726
	AircraftEventHandler_AtTerminal,     // TERM8          = 20
7e123fec5b0b (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
  1727
	AircraftEventHandler_AtTerminal,     // HELIPAD3       = 21
7e123fec5b0b (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
  1728
	AircraftEventHandler_AtTerminal,     // HELIPAD4       = 22
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1729
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1730
4842
79c4c9167d93 (svn r6768) -Cleanup: For airports change *Airport to *apc (variable naming coding style
Darkvater
parents: 4739
diff changeset
  1731
static void AirportClearBlock(const Vehicle *v, const AirportFTAClass *apc)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1732
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1733
	// we have left the previous block, and entered the new one. Free the previous block
4842
79c4c9167d93 (svn r6768) -Cleanup: For airports change *Airport to *apc (variable naming coding style
Darkvater
parents: 4739
diff changeset
  1734
	if (apc->layout[v->u.air.previous_pos].block != apc->layout[v->u.air.pos].block) {
4344
7e123fec5b0b (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
		Station *st = GetStation(v->u.air.targetairport);
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1736
4842
79c4c9167d93 (svn r6768) -Cleanup: For airports change *Airport to *apc (variable naming coding style
Darkvater
parents: 4739
diff changeset
  1737
		CLRBITS(st->airport_flags, apc->layout[v->u.air.previous_pos].block);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1738
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1739
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1740
4842
79c4c9167d93 (svn r6768) -Cleanup: For airports change *Airport to *apc (variable naming coding style
Darkvater
parents: 4739
diff changeset
  1741
static void AirportGoToNextPosition(Vehicle *v, const AirportFTAClass *apc)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1742
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1743
	// if aircraft is not in position, wait until it is
2549
5587f9a38563 (svn r3078) Some more stuff, which piled up:
tron
parents: 2517
diff changeset
  1744
	if (!AircraftController(v)) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1745
4842
79c4c9167d93 (svn r6768) -Cleanup: For airports change *Airport to *apc (variable naming coding style
Darkvater
parents: 4739
diff changeset
  1746
	AirportClearBlock(v, apc);
79c4c9167d93 (svn r6768) -Cleanup: For airports change *Airport to *apc (variable naming coding style
Darkvater
parents: 4739
diff changeset
  1747
	AirportMove(v, apc); // move aircraft to next position
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1748
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1749
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1750
// gets pos from vehicle and next orders
4842
79c4c9167d93 (svn r6768) -Cleanup: For airports change *Airport to *apc (variable naming coding style
Darkvater
parents: 4739
diff changeset
  1751
static bool AirportMove(Vehicle *v, const AirportFTAClass *apc)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1752
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1753
	// error handling
4842
79c4c9167d93 (svn r6768) -Cleanup: For airports change *Airport to *apc (variable naming coding style
Darkvater
parents: 4739
diff changeset
  1754
	if (v->u.air.pos >= apc->nofelements) {
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5259
diff changeset
  1755
		DEBUG(misc, 0, "[Ap] position %d is not valid for current airport. Max position is %d", v->u.air.pos, apc->nofelements-1);
4842
79c4c9167d93 (svn r6768) -Cleanup: For airports change *Airport to *apc (variable naming coding style
Darkvater
parents: 4739
diff changeset
  1756
		assert(v->u.air.pos < apc->nofelements);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1757
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1758
6087
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
  1759
	const AirportFTA *current = &apc->layout[v->u.air.pos];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1760
	// 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
  1761
	if (current->heading == v->u.air.state) {
6087
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
  1762
		byte prev_pos = v->u.air.pos; // location could be changed in state, so save it before-hand
4842
79c4c9167d93 (svn r6768) -Cleanup: For airports change *Airport to *apc (variable naming coding style
Darkvater
parents: 4739
diff changeset
  1763
		_aircraft_state_handlers[v->u.air.state](v, apc);
2549
5587f9a38563 (svn r3078) Some more stuff, which piled up:
tron
parents: 2517
diff changeset
  1764
		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
  1765
		return true;
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
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1768
	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
  1769
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1770
	// there is only one choice to move to
4842
79c4c9167d93 (svn r6768) -Cleanup: For airports change *Airport to *apc (variable naming coding style
Darkvater
parents: 4739
diff changeset
  1771
	if (current->next == NULL) {
79c4c9167d93 (svn r6768) -Cleanup: For airports change *Airport to *apc (variable naming coding style
Darkvater
parents: 4739
diff changeset
  1772
		if (AirportSetBlocks(v, current, apc)) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1773
			v->u.air.pos = current->next_position;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1774
		} // move to next position
3579
4a55e8ce3087 (svn r4465) -Codechange (FS#60): Remove a variable no one really needs. (DaleStan)
celestar
parents: 3491
diff changeset
  1775
		return false;
0
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
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1778
	// 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
  1779
	// matches our heading
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1780
	do {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1781
		if (v->u.air.state == current->heading || current->heading == TO_ALL) {
4842
79c4c9167d93 (svn r6768) -Cleanup: For airports change *Airport to *apc (variable naming coding style
Darkvater
parents: 4739
diff changeset
  1782
			if (AirportSetBlocks(v, current, apc)) {
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1783
				v->u.air.pos = current->next_position;
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1784
			} // move to next position
3579
4a55e8ce3087 (svn r4465) -Codechange (FS#60): Remove a variable no one really needs. (DaleStan)
celestar
parents: 3491
diff changeset
  1785
			return false;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1786
		}
4842
79c4c9167d93 (svn r6768) -Cleanup: For airports change *Airport to *apc (variable naming coding style
Darkvater
parents: 4739
diff changeset
  1787
		current = current->next;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1788
	} while (current != NULL);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1789
6036
333637e97400 (svn r8759) -Feature: Allow airports to have up to 4 entry points depending on approach direction. Note: they're not yet used, but will be soon
celestar
parents: 6032
diff changeset
  1790
	DEBUG(misc, 0, "[Ap] cannot move further on Airport! (pos %d state %d) for vehicle %d", v->u.air.pos, v->u.air.state, v->index);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1791
	assert(0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1792
	return false;
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
// returns true if the road ahead is busy, eg. you must wait before proceeding
4843
4bef85baee3f (svn r6769) -Codechange: Minor syntax, const correctness, variable localization, coding
Darkvater
parents: 4842
diff changeset
  1796
static bool AirportHasBlock(Vehicle *v, const AirportFTA *current_pos, const AirportFTAClass *apc)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1797
{
4843
4bef85baee3f (svn r6769) -Codechange: Minor syntax, const correctness, variable localization, coding
Darkvater
parents: 4842
diff changeset
  1798
	const AirportFTA *reference = &apc->layout[v->u.air.pos];
4bef85baee3f (svn r6769) -Codechange: Minor syntax, const correctness, variable localization, coding
Darkvater
parents: 4842
diff changeset
  1799
	const AirportFTA *next = &apc->layout[current_pos->next_position];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1800
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1801
	// same block, then of course we can move
4842
79c4c9167d93 (svn r6768) -Cleanup: For airports change *Airport to *apc (variable naming coding style
Darkvater
parents: 4739
diff changeset
  1802
	if (apc->layout[current_pos->position].block != next->block) {
4843
4bef85baee3f (svn r6769) -Codechange: Minor syntax, const correctness, variable localization, coding
Darkvater
parents: 4842
diff changeset
  1803
		const Station *st = GetStation(v->u.air.targetairport);
6011
04cd7c7f1d64 (svn r8733) -Fix(r8705): Forgot to change some more places where airport blocks are stored
celestar
parents: 5996
diff changeset
  1804
		uint64 airport_flags = next->block;
2630
7206058a7e82 (svn r3172) static, const
tron
parents: 2618
diff changeset
  1805
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1806
		// check additional possible extra blocks
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1807
		if (current_pos != reference && current_pos->block != NOTHING_block) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1808
			airport_flags |= current_pos->block;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1809
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1810
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1811
		if (HASBITS(st->airport_flags, airport_flags)) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1812
			v->cur_speed = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1813
			v->subspeed = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1814
			return true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1815
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1816
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1817
	return false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1818
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1819
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1820
// returns true on success. Eg, next block was free and we have occupied it
6087
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
  1821
static bool AirportSetBlocks(Vehicle *v, const AirportFTA *current_pos, const AirportFTAClass *apc)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1822
{
6087
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
  1823
	const AirportFTA *next = &apc->layout[current_pos->next_position];
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
  1824
	const AirportFTA *reference = &apc->layout[v->u.air.pos];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1825
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1826
	// if the next position is in another block, check it and wait until it is free
4842
79c4c9167d93 (svn r6768) -Cleanup: For airports change *Airport to *apc (variable naming coding style
Darkvater
parents: 4739
diff changeset
  1827
	if ((apc->layout[current_pos->position].block & next->block) != next->block) {
6011
04cd7c7f1d64 (svn r8733) -Fix(r8705): Forgot to change some more places where airport blocks are stored
celestar
parents: 5996
diff changeset
  1828
		uint64 airport_flags = next->block;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1829
		//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
  1830
		// this means more blocks should be checked/set
6087
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
  1831
		const AirportFTA *current = current_pos;
4842
79c4c9167d93 (svn r6768) -Cleanup: For airports change *Airport to *apc (variable naming coding style
Darkvater
parents: 4739
diff changeset
  1832
		if (current == reference) current = current->next;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1833
		while (current != NULL) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1834
			if (current->heading == current_pos->heading && current->block != 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1835
				airport_flags |= current->block;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1836
				break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1837
			}
4842
79c4c9167d93 (svn r6768) -Cleanup: For airports change *Airport to *apc (variable naming coding style
Darkvater
parents: 4739
diff changeset
  1838
			current = current->next;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1839
		};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1840
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1841
		// 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
  1842
		// checking, because it has been set by the airplane before
2549
5587f9a38563 (svn r3078) Some more stuff, which piled up:
tron
parents: 2517
diff changeset
  1843
		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
  1844
6087
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
  1845
		Station* st = GetStation(v->u.air.targetairport);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1846
		if (HASBITS(st->airport_flags, airport_flags)) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1847
			v->cur_speed = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1848
			v->subspeed = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1849
			return false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1850
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1851
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1852
		if (next->block != NOTHING_block) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1853
			SETBITS(st->airport_flags, airport_flags); // occupy next block
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
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1856
	return true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1857
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1858
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1859
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
  1860
{
919
544f374ee392 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 909
diff changeset
  1861
	Station *st = GetStation(v->u.air.targetairport);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1862
	for (; i < last_terminal; i++) {
4059
b1e1c1193f0a (svn r5346) - Feature: Add 4 new airports. 2 for aircraft, 2 for helicopters.
richk
parents: 4056
diff changeset
  1863
		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
  1864
			// TERMINAL# HELIPAD#
4059
b1e1c1193f0a (svn r5346) - Feature: Add 4 new airports. 2 for aircraft, 2 for helicopters.
richk
parents: 4056
diff changeset
  1865
			v->u.air.state = _airport_terminal_state[i]; // start moving to that terminal/helipad
b1e1c1193f0a (svn r5346) - Feature: Add 4 new airports. 2 for aircraft, 2 for helicopters.
richk
parents: 4056
diff changeset
  1866
			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
  1867
			return true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1868
		}
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
	return false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1871
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1872
4842
79c4c9167d93 (svn r6768) -Cleanup: For airports change *Airport to *apc (variable naming coding style
Darkvater
parents: 4739
diff changeset
  1873
static uint GetNumTerminals(const AirportFTAClass *apc)
950
165341d74973 (svn r1440) -Feature: Allows more flexible airport layouts now, as the number of
celestar
parents: 926
diff changeset
  1874
{
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1875
	uint num = 0;
1019
6bae6c11e865 (svn r1520) Trim 134 (!) lines with trailing whitespace ):
tron
parents: 1018
diff changeset
  1876
6087
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
  1877
	for (uint i = apc->terminals[0]; i > 0; i--) num += apc->terminals[i];
1019
6bae6c11e865 (svn r1520) Trim 134 (!) lines with trailing whitespace ):
tron
parents: 1018
diff changeset
  1878
950
165341d74973 (svn r1440) -Feature: Allows more flexible airport layouts now, as the number of
celestar
parents: 926
diff changeset
  1879
	return num;
165341d74973 (svn r1440) -Feature: Allows more flexible airport layouts now, as the number of
celestar
parents: 926
diff changeset
  1880
}
165341d74973 (svn r1440) -Feature: Allows more flexible airport layouts now, as the number of
celestar
parents: 926
diff changeset
  1881
4842
79c4c9167d93 (svn r6768) -Cleanup: For airports change *Airport to *apc (variable naming coding style
Darkvater
parents: 4739
diff changeset
  1882
static bool AirportFindFreeTerminal(Vehicle *v, const AirportFTAClass *apc)
0
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
	/* example of more terminalgroups
4549
106ed18a7675 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4546
diff changeset
  1885
	 * {0,HANGAR,NOTHING_block,1}, {0,255,TERM_GROUP1_block,0}, {0,255,TERM_GROUP2_ENTER_block,1}, {0,0,N,1},
106ed18a7675 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4546
diff changeset
  1886
	 * Heading 255 denotes a group. We see 2 groups here:
106ed18a7675 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4546
diff changeset
  1887
	 * 1. group 0 -- TERM_GROUP1_block (check block)
106ed18a7675 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4546
diff changeset
  1888
	 * 2. group 1 -- TERM_GROUP2_ENTER_block (check block)
106ed18a7675 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4546
diff changeset
  1889
	 * First in line is checked first, group 0. If the block (TERM_GROUP1_block) is free, it
106ed18a7675 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4546
diff changeset
  1890
	 * looks at the corresponding terminals of that group. If no free ones are found, other
106ed18a7675 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4546
diff changeset
  1891
	 * possible groups are checked (in this case group 1, since that is after group 0). If that
106ed18a7675 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4546
diff changeset
  1892
	 * fails, then attempt fails and plane waits
106ed18a7675 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4546
diff changeset
  1893
	 */
4842
79c4c9167d93 (svn r6768) -Cleanup: For airports change *Airport to *apc (variable naming coding style
Darkvater
parents: 4739
diff changeset
  1894
	if (apc->terminals[0] > 1) {
6087
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
  1895
		const Station *st = GetStation(v->u.air.targetairport);
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
  1896
		const AirportFTA *temp = apc->layout[v->u.air.pos].next;
4843
4bef85baee3f (svn r6769) -Codechange: Minor syntax, const correctness, variable localization, coding
Darkvater
parents: 4842
diff changeset
  1897
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1898
		while (temp != NULL) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1899
			if (temp->heading == 255) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1900
				if (!HASBITS(st->airport_flags, temp->block)) {
950
165341d74973 (svn r1440) -Feature: Allows more flexible airport layouts now, as the number of
celestar
parents: 926
diff changeset
  1901
					//read which group do we want to go to?
165341d74973 (svn r1440) -Feature: Allows more flexible airport layouts now, as the number of
celestar
parents: 926
diff changeset
  1902
					//(the first free group)
6087
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
  1903
					uint target_group = temp->next_position + 1;
950
165341d74973 (svn r1440) -Feature: Allows more flexible airport layouts now, as the number of
celestar
parents: 926
diff changeset
  1904
165341d74973 (svn r1440) -Feature: Allows more flexible airport layouts now, as the number of
celestar
parents: 926
diff changeset
  1905
					//at what terminal does the group start?
165341d74973 (svn r1440) -Feature: Allows more flexible airport layouts now, as the number of
celestar
parents: 926
diff changeset
  1906
					//that means, sum up all terminals of
165341d74973 (svn r1440) -Feature: Allows more flexible airport layouts now, as the number of
celestar
parents: 926
diff changeset
  1907
					//groups with lower number
6087
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
  1908
					uint group_start = 0;
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
  1909
					for (uint i = 1; i < target_group; i++) {
4842
79c4c9167d93 (svn r6768) -Cleanup: For airports change *Airport to *apc (variable naming coding style
Darkvater
parents: 4739
diff changeset
  1910
						group_start += apc->terminals[i];
6087
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
  1911
					}
950
165341d74973 (svn r1440) -Feature: Allows more flexible airport layouts now, as the number of
celestar
parents: 926
diff changeset
  1912
6087
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
  1913
					uint group_end = group_start + apc->terminals[target_group];
2549
5587f9a38563 (svn r3078) Some more stuff, which piled up:
tron
parents: 2517
diff changeset
  1914
					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
  1915
				}
2549
5587f9a38563 (svn r3078) Some more stuff, which piled up:
tron
parents: 2517
diff changeset
  1916
			} else {
5587f9a38563 (svn r3078) Some more stuff, which piled up:
tron
parents: 2517
diff changeset
  1917
				/* once the heading isn't 255, we've exhausted the possible blocks.
5587f9a38563 (svn r3078) Some more stuff, which piled up:
tron
parents: 2517
diff changeset
  1918
				 * So we cannot move */
5587f9a38563 (svn r3078) Some more stuff, which piled up:
tron
parents: 2517
diff changeset
  1919
				return false;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1920
			}
4842
79c4c9167d93 (svn r6768) -Cleanup: For airports change *Airport to *apc (variable naming coding style
Darkvater
parents: 4739
diff changeset
  1921
			temp = temp->next;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1922
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1923
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1924
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1925
	// if there is only 1 terminalgroup, all terminals are checked (starting from 0 to max)
4842
79c4c9167d93 (svn r6768) -Cleanup: For airports change *Airport to *apc (variable naming coding style
Darkvater
parents: 4739
diff changeset
  1926
	return FreeTerminal(v, 0, GetNumTerminals(apc));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1927
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1928
4842
79c4c9167d93 (svn r6768) -Cleanup: For airports change *Airport to *apc (variable naming coding style
Darkvater
parents: 4739
diff changeset
  1929
static uint GetNumHelipads(const AirportFTAClass *apc)
950
165341d74973 (svn r1440) -Feature: Allows more flexible airport layouts now, as the number of
celestar
parents: 926
diff changeset
  1930
{
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1931
	uint num = 0;
1019
6bae6c11e865 (svn r1520) Trim 134 (!) lines with trailing whitespace ):
tron
parents: 1018
diff changeset
  1932
6087
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
  1933
	for (uint i = apc->helipads[0]; i > 0; i--) num += apc->helipads[i];
1019
6bae6c11e865 (svn r1520) Trim 134 (!) lines with trailing whitespace ):
tron
parents: 1018
diff changeset
  1934
950
165341d74973 (svn r1440) -Feature: Allows more flexible airport layouts now, as the number of
celestar
parents: 926
diff changeset
  1935
	return num;
165341d74973 (svn r1440) -Feature: Allows more flexible airport layouts now, as the number of
celestar
parents: 926
diff changeset
  1936
}
165341d74973 (svn r1440) -Feature: Allows more flexible airport layouts now, as the number of
celestar
parents: 926
diff changeset
  1937
165341d74973 (svn r1440) -Feature: Allows more flexible airport layouts now, as the number of
celestar
parents: 926
diff changeset
  1938
4842
79c4c9167d93 (svn r6768) -Cleanup: For airports change *Airport to *apc (variable naming coding style
Darkvater
parents: 4739
diff changeset
  1939
static bool AirportFindFreeHelipad(Vehicle *v, const AirportFTAClass *apc)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1940
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1941
	// if an airport doesn't have helipads, use terminals
4842
79c4c9167d93 (svn r6768) -Cleanup: For airports change *Airport to *apc (variable naming coding style
Darkvater
parents: 4739
diff changeset
  1942
	if (apc->helipads == NULL) return AirportFindFreeTerminal(v, apc);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1943
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1944
	// if there are more helicoptergroups, pick one, just as in AirportFindFreeTerminal()
4842
79c4c9167d93 (svn r6768) -Cleanup: For airports change *Airport to *apc (variable naming coding style
Darkvater
parents: 4739
diff changeset
  1945
	if (apc->helipads[0] > 1) {
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4072
diff changeset
  1946
		const Station* st = GetStation(v->u.air.targetairport);
4842
79c4c9167d93 (svn r6768) -Cleanup: For airports change *Airport to *apc (variable naming coding style
Darkvater
parents: 4739
diff changeset
  1947
		const AirportFTA* temp = apc->layout[v->u.air.pos].next;
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4072
diff changeset
  1948
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1949
		while (temp != NULL) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1950
			if (temp->heading == 255) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1951
				if (!HASBITS(st->airport_flags, temp->block)) {
950
165341d74973 (svn r1440) -Feature: Allows more flexible airport layouts now, as the number of
celestar
parents: 926
diff changeset
  1952
165341d74973 (svn r1440) -Feature: Allows more flexible airport layouts now, as the number of
celestar
parents: 926
diff changeset
  1953
					//read which group do we want to go to?
165341d74973 (svn r1440) -Feature: Allows more flexible airport layouts now, as the number of
celestar
parents: 926
diff changeset
  1954
					//(the first free group)
6087
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
  1955
					uint target_group = temp->next_position + 1;
950
165341d74973 (svn r1440) -Feature: Allows more flexible airport layouts now, as the number of
celestar
parents: 926
diff changeset
  1956
165341d74973 (svn r1440) -Feature: Allows more flexible airport layouts now, as the number of
celestar
parents: 926
diff changeset
  1957
					//at what terminal does the group start?
165341d74973 (svn r1440) -Feature: Allows more flexible airport layouts now, as the number of
celestar
parents: 926
diff changeset
  1958
					//that means, sum up all terminals of
165341d74973 (svn r1440) -Feature: Allows more flexible airport layouts now, as the number of
celestar
parents: 926
diff changeset
  1959
					//groups with lower number
6087
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
  1960
					uint group_start = 0;
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
  1961
					for (uint i = 1; i < target_group; i++) {
4842
79c4c9167d93 (svn r6768) -Cleanup: For airports change *Airport to *apc (variable naming coding style
Darkvater
parents: 4739
diff changeset
  1962
						group_start += apc->helipads[i];
6087
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
  1963
					}
950
165341d74973 (svn r1440) -Feature: Allows more flexible airport layouts now, as the number of
celestar
parents: 926
diff changeset
  1964
6087
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
  1965
					uint group_end = group_start + apc->helipads[target_group];
2549
5587f9a38563 (svn r3078) Some more stuff, which piled up:
tron
parents: 2517
diff changeset
  1966
					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
  1967
				}
2549
5587f9a38563 (svn r3078) Some more stuff, which piled up:
tron
parents: 2517
diff changeset
  1968
			} else {
5587f9a38563 (svn r3078) Some more stuff, which piled up:
tron
parents: 2517
diff changeset
  1969
				/* once the heading isn't 255, we've exhausted the possible blocks.
5587f9a38563 (svn r3078) Some more stuff, which piled up:
tron
parents: 2517
diff changeset
  1970
				 * So we cannot move */
5587f9a38563 (svn r3078) Some more stuff, which piled up:
tron
parents: 2517
diff changeset
  1971
				return false;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1972
			}
4842
79c4c9167d93 (svn r6768) -Cleanup: For airports change *Airport to *apc (variable naming coding style
Darkvater
parents: 4739
diff changeset
  1973
			temp = temp->next;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1974
		}
2549
5587f9a38563 (svn r3078) Some more stuff, which piled up:
tron
parents: 2517
diff changeset
  1975
	} else {
5587f9a38563 (svn r3078) Some more stuff, which piled up:
tron
parents: 2517
diff changeset
  1976
		// only 1 helicoptergroup, check all helipads
5587f9a38563 (svn r3078) Some more stuff, which piled up:
tron
parents: 2517
diff changeset
  1977
		// The blocks for helipads start after the last terminal (MAX_TERMINALS)
4842
79c4c9167d93 (svn r6768) -Cleanup: For airports change *Airport to *apc (variable naming coding style
Darkvater
parents: 4739
diff changeset
  1978
		return FreeTerminal(v, MAX_TERMINALS, GetNumHelipads(apc) + MAX_TERMINALS);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1979
	}
4434
a08cb4b5c179 (svn r6204) -Cleanup: replace non-indentation with spaces; like '}<TAB>else {' -> '} else {', tabs between code and comment, etc.
rubidium
parents: 4412
diff changeset
  1980
	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
  1981
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1982
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1983
static void AircraftEventHandler(Vehicle *v, int loop)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1984
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1985
	v->tick_counter++;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1986
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1987
	if (v->vehstatus & VS_CRASHED) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1988
		HandleCrashedAircraft(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1989
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1990
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1991
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1992
	if (v->vehstatus & VS_STOPPED) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1993
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1994
	/* aircraft is broken down? */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1995
	if (v->breakdown_ctr != 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1996
		if (v->breakdown_ctr <= 2) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1997
			HandleBrokenAircraft(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1998
		} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1999
			v->breakdown_ctr--;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2000
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2001
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2002
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2003
	HandleAircraftSmoke(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2004
	ProcessAircraftOrder(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2005
	HandleAircraftLoading(v, loop);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2006
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  2007
	if (v->current_order.type >= OT_LOADING) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2008
6032
fe6a454ac1ba (svn r8755) -Fix
tron
parents: 6027
diff changeset
  2009
	AirportGoToNextPosition(v, GetStation(v->u.air.targetairport)->Airport());
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
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2012
void Aircraft_Tick(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2013
{
5854
9eee280920f0 (svn r8428) -Codechange: Add proper names to aircraft subtypes instead of magic numbers and add a function IsNormalAircraft() which tells us whether the aircraft is in fact some flying device or a rotor/shadow.
Darkvater
parents: 5754
diff changeset
  2014
	if (!IsNormalAircraft(v)) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2015
5854
9eee280920f0 (svn r8428) -Codechange: Add proper names to aircraft subtypes instead of magic numbers and add a function IsNormalAircraft() which tells us whether the aircraft is in fact some flying device or a rotor/shadow.
Darkvater
parents: 5754
diff changeset
  2016
	if (v->subtype == AIR_HELICOPTER) HelicopterTickHandler(v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2017
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2018
	AgeAircraftCargo(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2019
6087
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
  2020
	for (uint i = 0; i != 6; i++) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2021
		AircraftEventHandler(v, i);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2022
		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
  2023
			break;
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
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2026
950
165341d74973 (svn r1440) -Feature: Allows more flexible airport layouts now, as the number of
celestar
parents: 926
diff changeset
  2027
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2028
// need to be called to load aircraft from old version
1093
4fdc46eaf423 (svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents: 1064
diff changeset
  2029
void UpdateOldAircraft(void)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2030
{
6087
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
  2031
	// set airport_flags to 0 for all airports just to be sure
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2032
	Station *st;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2033
	FOR_ALL_STATIONS(st) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2034
		st->airport_flags = 0; // reset airport
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2035
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2036
6087
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
  2037
	Vehicle *v_oldstyle;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2038
	FOR_ALL_VEHICLES(v_oldstyle) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2039
	// 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
  2040
	// skip those
5854
9eee280920f0 (svn r8428) -Codechange: Add proper names to aircraft subtypes instead of magic numbers and add a function IsNormalAircraft() which tells us whether the aircraft is in fact some flying device or a rotor/shadow.
Darkvater
parents: 5754
diff changeset
  2041
		if (v_oldstyle->type == VEH_Aircraft && IsNormalAircraft(v_oldstyle)) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2042
			// airplane in terminal stopped doesn't hurt anyone, so goto next
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  2043
			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
  2044
				v_oldstyle->u.air.state = HANGAR;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2045
				continue;
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
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2048
			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
  2049
			v_oldstyle->vehstatus &= ~VS_STOPPED; // make airplane moving
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2050
			v_oldstyle->u.air.state = FLYING;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2051
			AircraftNextAirportPos_and_Order(v_oldstyle); // move it to the entry point of the airport
6087
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
  2052
			GetNewVehiclePosResult gp;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2053
			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
  2054
			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
  2055
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2056
			// correct speed of helicopter-rotors
5854
9eee280920f0 (svn r8428) -Codechange: Add proper names to aircraft subtypes instead of magic numbers and add a function IsNormalAircraft() which tells us whether the aircraft is in fact some flying device or a rotor/shadow.
Darkvater
parents: 5754
diff changeset
  2057
			if (v_oldstyle->subtype == AIR_HELICOPTER) v_oldstyle->next->next->cur_speed = 32;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2058
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2059
			// set new position x,y,z
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2060
			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
  2061
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2062
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2063
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2064
6087
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
  2065
void UpdateAirplanesOnNewStation(const Station *st)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2066
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2067
	// only 1 station is updated per function call, so it is enough to get entry_point once
6032
fe6a454ac1ba (svn r8755) -Fix
tron
parents: 6027
diff changeset
  2068
	const AirportFTAClass *ap = st->Airport();
6087
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
  2069
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
  2070
	Vehicle *v;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2071
	FOR_ALL_VEHICLES(v) {
5854
9eee280920f0 (svn r8428) -Codechange: Add proper names to aircraft subtypes instead of magic numbers and add a function IsNormalAircraft() which tells us whether the aircraft is in fact some flying device or a rotor/shadow.
Darkvater
parents: 5754
diff changeset
  2072
		if (v->type == VEH_Aircraft && IsNormalAircraft(v)) {
4434
a08cb4b5c179 (svn r6204) -Cleanup: replace non-indentation with spaces; like '}<TAB>else {' -> '} else {', tabs between code and comment, etc.
rubidium
parents: 4412
diff changeset
  2073
			if (v->u.air.targetairport == st->index) { // if heading to this airport
a08cb4b5c179 (svn r6204) -Cleanup: replace non-indentation with spaces; like '}<TAB>else {' -> '} else {', tabs between code and comment, etc.
rubidium
parents: 4412
diff changeset
  2074
				/* update position of airplane. If plane is not flying, landing, or taking off
a08cb4b5c179 (svn r6204) -Cleanup: replace non-indentation with spaces; like '}<TAB>else {' -> '} else {', tabs between code and comment, etc.
rubidium
parents: 4412
diff changeset
  2075
				 *you cannot delete airport, so it doesn't matter
a08cb4b5c179 (svn r6204) -Cleanup: replace non-indentation with spaces; like '}<TAB>else {' -> '} else {', tabs between code and comment, etc.
rubidium
parents: 4412
diff changeset
  2076
				 */
a08cb4b5c179 (svn r6204) -Cleanup: replace non-indentation with spaces; like '}<TAB>else {' -> '} else {', tabs between code and comment, etc.
rubidium
parents: 4412
diff changeset
  2077
				if (v->u.air.state >= FLYING) { // circle around
6036
333637e97400 (svn r8759) -Feature: Allow airports to have up to 4 entry points depending on approach direction. Note: they're not yet used, but will be soon
celestar
parents: 6032
diff changeset
  2078
					v->u.air.pos = v->u.air.previous_pos = AircraftGetEntryPoint(v, ap);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2079
					v->u.air.state = FLYING;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2080
					// landing plane needs to be reset to flying height (only if in pause mode upgrade,
1401
44a7e0f04d28 (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
  2081
					// in normal mode, plane is reset in AircraftController. It doesn't hurt for FLYING
6087
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
  2082
					GetNewVehiclePosResult gp;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2083
					GetNewVehiclePos(v, &gp);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2084
					// set new position x,y,z
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2085
					SetAircraftPosition(v, gp.x, gp.y, GetAircraftFlyingAltitude(v));
2549
5587f9a38563 (svn r3078) Some more stuff, which piled up:
tron
parents: 2517
diff changeset
  2086
				} else {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2087
					assert(v->u.air.state == ENDTAKEOFF || v->u.air.state == HELITAKEOFF);
6087
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
  2088
					byte takeofftype = (v->subtype == AIR_HELICOPTER) ? HELITAKEOFF : ENDTAKEOFF;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2089
					// search in airportdata for that heading
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2090
					// easiest to do, since this doesn't happen a lot
6087
bfa159e740d9 (svn r8822) -Fix
tron
parents: 6037
diff changeset
  2091
					for (uint cnt = 0; cnt < ap->nofelements; cnt++) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2092
						if (ap->layout[cnt].heading == takeofftype) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2093
							v->u.air.pos = ap->layout[cnt].position;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2094
							break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2095
						}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2096
					}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2097
				}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2098
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2099
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2100
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2101
}