2005-07-24 14:12:37 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2007-02-23 01:48:53 +00:00
|
|
|
/** @file aircraft_cmd.cpp
|
|
|
|
* This file deals with aircraft and airport movements functionalities */
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
#include "stdafx.h"
|
2005-06-02 19:30:21 +00:00
|
|
|
#include "openttd.h"
|
2006-06-05 12:43:41 +00:00
|
|
|
#include "aircraft.h"
|
2005-02-05 15:58:59 +00:00
|
|
|
#include "debug.h"
|
2005-07-22 07:02:20 +00:00
|
|
|
#include "functions.h"
|
2007-04-12 13:07:15 +00:00
|
|
|
#include "landscape.h"
|
2006-03-24 08:55:08 +00:00
|
|
|
#include "station_map.h"
|
2007-07-16 09:16:58 +00:00
|
|
|
#include "strings.h"
|
2004-11-25 10:47:30 +00:00
|
|
|
#include "table/strings.h"
|
2004-12-15 22:18:54 +00:00
|
|
|
#include "map.h"
|
2005-01-29 12:19:05 +00:00
|
|
|
#include "tile.h"
|
2004-08-09 17:04:08 +00:00
|
|
|
#include "vehicle.h"
|
2007-06-20 19:17:22 +00:00
|
|
|
#include "timetable.h"
|
2005-05-11 16:17:03 +00:00
|
|
|
#include "depot.h"
|
2004-08-09 17:04:08 +00:00
|
|
|
#include "engine.h"
|
|
|
|
#include "command.h"
|
|
|
|
#include "station.h"
|
|
|
|
#include "news.h"
|
2004-11-05 23:12:33 +00:00
|
|
|
#include "sound.h"
|
2004-08-09 17:04:08 +00:00
|
|
|
#include "player.h"
|
2007-01-27 12:29:55 +00:00
|
|
|
#include "aircraft.h"
|
2004-08-09 17:04:08 +00:00
|
|
|
#include "airport.h"
|
2005-05-02 23:59:11 +00:00
|
|
|
#include "vehicle_gui.h"
|
2005-10-15 11:06:54 +00:00
|
|
|
#include "table/sprites.h"
|
2006-02-03 12:55:21 +00:00
|
|
|
#include "newgrf_engine.h"
|
2006-06-07 07:20:28 +00:00
|
|
|
#include "newgrf_callbacks.h"
|
2006-08-09 21:11:45 +00:00
|
|
|
#include "newgrf_text.h"
|
2006-09-27 18:17:01 +00:00
|
|
|
#include "newgrf_sound.h"
|
2006-08-14 14:21:15 +00:00
|
|
|
#include "date.h"
|
2007-02-10 13:37:32 +00:00
|
|
|
#include "spritecache.h"
|
2007-03-19 20:54:13 +00:00
|
|
|
#include "cargotype.h"
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-05-01 16:35:14 +00:00
|
|
|
void Aircraft::UpdateDeltaXY(Direction direction)
|
|
|
|
{
|
|
|
|
uint32 x;
|
|
|
|
#define MKIT(a, b, c, d) ((a & 0xFF) << 24) | ((b & 0xFF) << 16) | ((c & 0xFF) << 8) | ((d & 0xFF) << 0)
|
|
|
|
switch (this->subtype) {
|
|
|
|
default: NOT_REACHED();
|
|
|
|
case AIR_AIRCRAFT:
|
|
|
|
case AIR_HELICOPTER:
|
|
|
|
switch (this->u.air.state) {
|
|
|
|
case ENDTAKEOFF:
|
|
|
|
case LANDING:
|
|
|
|
case HELILANDING:
|
|
|
|
case FLYING: x = MKIT(24, 24, -1, -1); break;
|
|
|
|
default: x = MKIT( 2, 2, -1, -1); break;
|
|
|
|
}
|
|
|
|
this->z_height = 5;
|
|
|
|
break;
|
|
|
|
case AIR_SHADOW: this->z_height = 1; x = MKIT(2, 2, 0, 0); break;
|
|
|
|
case AIR_ROTOR: this->z_height = 1; x = MKIT(2, 2, -1, -1); break;
|
|
|
|
}
|
|
|
|
#undef MKIT
|
|
|
|
|
|
|
|
this->x_offs = GB(x, 0, 8);
|
|
|
|
this->y_offs = GB(x, 8, 8);
|
|
|
|
this->sprite_width = GB(x, 16, 8);
|
|
|
|
this->sprite_height = GB(x, 24, 8);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-02-23 11:50:43 +00:00
|
|
|
/** this maps the terminal to its corresponding state and block flag
|
|
|
|
* currently set for 10 terms, 4 helipads */
|
2007-01-21 14:15:38 +00:00
|
|
|
static const byte _airport_terminal_state[] = {2, 3, 4, 5, 6, 7, 19, 20, 0, 0, 8, 9, 21, 22};
|
|
|
|
static const byte _airport_terminal_flag[] = {0, 1, 2, 3, 4, 5, 22, 23, 0, 0, 6, 7, 24, 25};
|
|
|
|
|
2006-10-13 23:08:55 +00:00
|
|
|
static bool AirportMove(Vehicle *v, const AirportFTAClass *apc);
|
2007-02-20 06:39:09 +00:00
|
|
|
static bool AirportSetBlocks(Vehicle *v, const AirportFTA *current_pos, const AirportFTAClass *apc);
|
2006-10-14 09:51:04 +00:00
|
|
|
static bool AirportHasBlock(Vehicle *v, const AirportFTA *current_pos, const AirportFTAClass *apc);
|
2006-10-13 23:08:55 +00:00
|
|
|
static bool AirportFindFreeTerminal(Vehicle *v, const AirportFTAClass *apc);
|
|
|
|
static bool AirportFindFreeHelipad(Vehicle *v, const AirportFTAClass *apc);
|
2005-01-05 13:15:27 +00:00
|
|
|
static void CrashAirplane(Vehicle *v);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
static void AircraftNextAirportPos_and_Order(Vehicle *v);
|
|
|
|
static byte GetAircraftFlyingAltitude(const Vehicle *v);
|
|
|
|
|
|
|
|
static const SpriteID _aircraft_sprite[] = {
|
|
|
|
0x0EB5, 0x0EBD, 0x0EC5, 0x0ECD,
|
|
|
|
0x0ED5, 0x0EDD, 0x0E9D, 0x0EA5,
|
|
|
|
0x0EAD, 0x0EE5, 0x0F05, 0x0F0D,
|
|
|
|
0x0F15, 0x0F1D, 0x0F25, 0x0F2D,
|
|
|
|
0x0EED, 0x0EF5, 0x0EFD, 0x0F35,
|
|
|
|
0x0E9D, 0x0EA5, 0x0EAD, 0x0EB5,
|
|
|
|
0x0EBD, 0x0EC5
|
|
|
|
};
|
|
|
|
|
2007-02-23 11:50:43 +00:00
|
|
|
/** Helicopter rotor animation states */
|
2006-05-01 20:05:03 +00:00
|
|
|
enum HelicopterRotorStates {
|
|
|
|
HRS_ROTOR_STOPPED,
|
|
|
|
HRS_ROTOR_MOVING_1,
|
|
|
|
HRS_ROTOR_MOVING_2,
|
|
|
|
HRS_ROTOR_MOVING_3,
|
|
|
|
};
|
|
|
|
|
2007-02-23 11:50:43 +00:00
|
|
|
/** Find the nearest hangar to v
|
2005-03-23 11:12:17 +00:00
|
|
|
* INVALID_STATION is returned, if the player does not have any suitable
|
|
|
|
* airports (like helipads only)
|
2007-02-23 11:50:43 +00:00
|
|
|
* @param v vehicle looking for a hangar
|
|
|
|
* @return the StationID if one is found, otherwise, INVALID_STATION
|
2005-03-23 11:12:17 +00:00
|
|
|
*/
|
2005-05-12 00:11:37 +00:00
|
|
|
static StationID FindNearestHangar(const Vehicle *v)
|
2005-01-27 15:43:44 +00:00
|
|
|
{
|
2005-03-23 11:12:17 +00:00
|
|
|
const Station *st;
|
|
|
|
uint best = 0;
|
2005-05-12 00:11:37 +00:00
|
|
|
StationID index = INVALID_STATION;
|
2006-08-15 14:38:43 +00:00
|
|
|
TileIndex vtile = TileVirtXY(v->x_pos, v->y_pos);
|
2005-01-27 15:43:44 +00:00
|
|
|
|
|
|
|
FOR_ALL_STATIONS(st) {
|
2007-02-15 20:35:45 +00:00
|
|
|
if (st->owner != v->owner || !(st->facilities & FACIL_AIRPORT)) continue;
|
|
|
|
|
2007-02-16 09:38:43 +00:00
|
|
|
const AirportFTAClass *afc = st->Airport();
|
2007-02-15 20:35:45 +00:00
|
|
|
if (afc->nof_depots == 0 || (
|
|
|
|
/* don't crash the plane if we know it can't land at the airport */
|
|
|
|
afc->flags & AirportFTAClass::SHORT_STRIP &&
|
|
|
|
AircraftVehInfo(v->engine_type)->subtype & AIR_FAST &&
|
|
|
|
!_cheats.no_jetcrash.value
|
|
|
|
)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2007-02-23 11:50:43 +00:00
|
|
|
/* v->tile can't be used here, when aircraft is flying v->tile is set to 0 */
|
2007-02-15 20:35:45 +00:00
|
|
|
uint distance = DistanceSquare(vtile, st->airport_tile);
|
|
|
|
if (distance < best || index == INVALID_STATION) {
|
|
|
|
best = distance;
|
|
|
|
index = st->index;
|
2005-01-27 15:43:44 +00:00
|
|
|
}
|
|
|
|
}
|
2005-03-23 11:12:17 +00:00
|
|
|
return index;
|
2005-01-27 15:43:44 +00:00
|
|
|
}
|
|
|
|
|
2005-04-02 00:17:16 +00:00
|
|
|
#if 0
|
2007-02-23 11:50:43 +00:00
|
|
|
/** Check if given vehicle has a goto hangar in his orders
|
|
|
|
* @param v vehicle to inquiry
|
|
|
|
* @return true if vehicle v has an airport in the schedule, that has a hangar */
|
2005-01-27 15:43:44 +00:00
|
|
|
static bool HaveHangarInOrderList(Vehicle *v)
|
|
|
|
{
|
|
|
|
const Order *order;
|
|
|
|
|
|
|
|
FOR_VEHICLE_ORDERS(v, order) {
|
|
|
|
const Station *st = GetStation(order->station);
|
|
|
|
if (st->owner == v->owner && st->facilities & FACIL_AIRPORT) {
|
2007-02-23 11:50:43 +00:00
|
|
|
/* If an airport doesn't have a hangar, skip it */
|
2007-02-16 09:38:43 +00:00
|
|
|
if (st->Airport()->nof_depots != 0)
|
2005-01-27 15:43:44 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2005-04-02 00:17:16 +00:00
|
|
|
#endif
|
2005-01-27 15:43:44 +00:00
|
|
|
|
2007-07-01 19:11:47 +00:00
|
|
|
int Aircraft::GetImage(Direction direction) const
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2007-07-01 19:11:47 +00:00
|
|
|
int spritenum = this->spritenum;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
if (is_custom_sprite(spritenum)) {
|
2007-07-01 19:11:47 +00:00
|
|
|
int sprite = GetCustomVehicleSprite(this, direction);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-02-06 09:18:04 +00:00
|
|
|
if (sprite != 0) return sprite;
|
2007-07-01 19:11:47 +00:00
|
|
|
spritenum = orig_aircraft_vehicle_info[this->engine_type - AIRCRAFT_ENGINES_INDEX].image_index;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
return direction + _aircraft_sprite[spritenum];
|
|
|
|
}
|
|
|
|
|
2006-05-01 20:05:03 +00:00
|
|
|
SpriteID GetRotorImage(const Vehicle *v)
|
|
|
|
{
|
2007-01-27 12:29:55 +00:00
|
|
|
assert(v->subtype == AIR_HELICOPTER);
|
2006-05-01 20:05:03 +00:00
|
|
|
|
2007-08-30 13:03:56 +00:00
|
|
|
const Vehicle *w = v->Next()->Next();
|
2006-05-01 20:05:03 +00:00
|
|
|
if (is_custom_sprite(v->spritenum)) {
|
2006-06-22 21:15:27 +00:00
|
|
|
SpriteID spritenum = GetCustomRotorSprite(v, false);
|
2006-05-01 20:05:03 +00:00
|
|
|
if (spritenum != 0) return spritenum;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return standard rotor sprites if there are no custom sprites for this helicopter */
|
|
|
|
return SPR_ROTOR_STOPPED + w->u.air.state;
|
|
|
|
}
|
|
|
|
|
2007-01-14 19:57:49 +00:00
|
|
|
void DrawAircraftEngine(int x, int y, EngineID engine, SpriteID pal)
|
2004-11-13 20:37:57 +00:00
|
|
|
{
|
2006-06-14 11:05:30 +00:00
|
|
|
const AircraftVehicleInfo* avi = AircraftVehInfo(engine);
|
|
|
|
int spritenum = avi->image_index;
|
2007-01-14 19:57:49 +00:00
|
|
|
SpriteID sprite = (6 + _aircraft_sprite[spritenum]);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2004-11-13 20:37:57 +00:00
|
|
|
if (is_custom_sprite(spritenum)) {
|
2006-03-12 17:42:04 +00:00
|
|
|
sprite = GetCustomVehicleIcon(engine, DIR_W);
|
2006-11-01 22:27:16 +00:00
|
|
|
if (sprite == 0) {
|
2005-09-26 19:01:49 +00:00
|
|
|
spritenum = orig_aircraft_vehicle_info[engine - AIRCRAFT_ENGINES_INDEX].image_index;
|
2006-11-01 22:27:16 +00:00
|
|
|
sprite = (6 + _aircraft_sprite[spritenum]);
|
|
|
|
}
|
2004-11-13 20:37:57 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-01-14 19:57:49 +00:00
|
|
|
DrawSprite(sprite, pal, x, y);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-06-14 11:05:30 +00:00
|
|
|
if (!(avi->subtype & AIR_CTOL)) {
|
2006-05-01 20:05:03 +00:00
|
|
|
SpriteID rotor_sprite = GetCustomRotorIcon(engine);
|
|
|
|
if (rotor_sprite == 0) rotor_sprite = SPR_ROTOR_STOPPED;
|
2007-01-14 19:57:49 +00:00
|
|
|
DrawSprite(rotor_sprite, PAL_NONE, x, y - 5);
|
2005-01-28 12:20:06 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-02-10 13:37:32 +00:00
|
|
|
/** Get the size of the sprite of an aircraft sprite heading west (used for lists)
|
|
|
|
* @param engine The engine to get the sprite from
|
2007-04-02 14:20:31 +00:00
|
|
|
* @param width The width of the sprite
|
|
|
|
* @param height The height of the sprite
|
2007-02-10 13:37:32 +00:00
|
|
|
*/
|
|
|
|
void GetAircraftSpriteSize(EngineID engine, uint &width, uint &height)
|
|
|
|
{
|
|
|
|
const AircraftVehicleInfo* avi = AircraftVehInfo(engine);
|
|
|
|
int spritenum = avi->image_index;
|
|
|
|
SpriteID sprite = (6 + _aircraft_sprite[spritenum]);
|
|
|
|
|
|
|
|
if (is_custom_sprite(spritenum)) {
|
|
|
|
sprite = GetCustomVehicleIcon(engine, DIR_W);
|
|
|
|
if (sprite == 0) {
|
|
|
|
spritenum = orig_aircraft_vehicle_info[engine - AIRCRAFT_ENGINES_INDEX].image_index;
|
|
|
|
sprite = (6 + _aircraft_sprite[spritenum]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const Sprite *spr = GetSprite(sprite);
|
|
|
|
|
|
|
|
width = spr->width ;
|
|
|
|
height = spr->height;
|
|
|
|
}
|
|
|
|
|
2007-06-18 10:48:15 +00:00
|
|
|
static CommandCost EstimateAircraftCost(EngineID engine, const AircraftVehicleInfo *avi)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2007-06-18 19:53:50 +00:00
|
|
|
return CommandCost(GetEngineProperty(engine, 0x0B, avi->base_cost) * (_price.aircraft_base >> 3) >> 5);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-06-07 07:20:28 +00:00
|
|
|
/**
|
|
|
|
* Calculates cargo capacity based on an aircraft's passenger
|
|
|
|
* and mail capacities.
|
|
|
|
* @param cid Which cargo type to calculate a capacity for.
|
2007-04-02 14:20:31 +00:00
|
|
|
* @param avi Which engine to find a cargo capacity for.
|
2006-06-07 07:20:28 +00:00
|
|
|
* @return New cargo capacity value.
|
|
|
|
*/
|
2007-02-20 06:39:09 +00:00
|
|
|
uint16 AircraftDefaultCargoCapacity(CargoID cid, const AircraftVehicleInfo *avi)
|
2006-06-07 07:20:28 +00:00
|
|
|
{
|
|
|
|
assert(cid != CT_INVALID);
|
|
|
|
|
|
|
|
/* An aircraft can carry twice as much goods as normal cargo,
|
|
|
|
* and four times as many passengers. */
|
|
|
|
switch (cid) {
|
|
|
|
case CT_PASSENGERS:
|
|
|
|
return avi->passenger_capacity;
|
|
|
|
case CT_MAIL:
|
|
|
|
return avi->passenger_capacity + avi->mail_capacity;
|
|
|
|
case CT_GOODS:
|
|
|
|
return (avi->passenger_capacity + avi->mail_capacity) / 2;
|
|
|
|
default:
|
|
|
|
return (avi->passenger_capacity + avi->mail_capacity) / 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-05-12 00:11:37 +00:00
|
|
|
/** Build an aircraft.
|
2006-04-10 07:15:58 +00:00
|
|
|
* @param tile tile of depot where aircraft is built
|
2007-02-23 11:50:43 +00:00
|
|
|
* @param flags for command
|
2005-05-12 00:11:37 +00:00
|
|
|
* @param p1 aircraft type being built (engine)
|
2006-05-11 13:31:14 +00:00
|
|
|
* @param p2 bit 0 when set, the unitnumber will be 0, otherwise it will be a free number
|
2007-02-23 11:50:43 +00:00
|
|
|
* return result of operation. Could be cost, error
|
2005-05-12 00:11:37 +00:00
|
|
|
*/
|
2007-06-18 10:48:15 +00:00
|
|
|
CommandCost CmdBuildAircraft(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2007-04-02 11:35:08 +00:00
|
|
|
if (!IsEngineBuildable(p1, VEH_AIRCRAFT, _current_player)) return_cmd_error(STR_AIRCRAFT_NOT_AVAILABLE);
|
2005-01-27 21:00:05 +00:00
|
|
|
|
2007-02-03 07:54:13 +00:00
|
|
|
const AircraftVehicleInfo *avi = AircraftVehInfo(p1);
|
2007-06-18 10:48:15 +00:00
|
|
|
CommandCost value = EstimateAircraftCost(p1, avi);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-02-23 11:50:43 +00:00
|
|
|
/* to just query the cost, it is not neccessary to have a valid tile (automation/AI) */
|
2005-05-12 00:11:37 +00:00
|
|
|
if (flags & DC_QUERY_COST) return value;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-03-26 19:20:15 +00:00
|
|
|
if (!IsHangarTile(tile) || !IsTileOwner(tile, _current_player)) return CMD_ERROR;
|
2005-09-13 13:30:18 +00:00
|
|
|
|
|
|
|
SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES);
|
|
|
|
|
2007-02-23 11:50:43 +00:00
|
|
|
/* Prevent building aircraft types at places which can't handle them */
|
2007-03-06 22:11:58 +00:00
|
|
|
if (!IsAircraftBuildableAtStation(p1, tile)) return CMD_ERROR;
|
2006-07-22 14:43:53 +00:00
|
|
|
|
2007-01-27 12:29:55 +00:00
|
|
|
/* Allocate 2 or 3 vehicle structs, depending on type
|
|
|
|
* vl[0] = aircraft, vl[1] = shadow, [vl[2] = rotor] */
|
2007-02-20 06:39:09 +00:00
|
|
|
Vehicle *vl[3];
|
2007-08-03 19:36:00 +00:00
|
|
|
if (!Vehicle::AllocateList(vl, avi->subtype & AIR_CTOL ? 2 : 3)) {
|
2005-11-14 19:48:04 +00:00
|
|
|
return_cmd_error(STR_00E1_TOO_MANY_VEHICLES_IN_GAME);
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-03-08 16:27:54 +00:00
|
|
|
UnitID unit_num = HASBIT(p2, 0) ? 0 : GetFreeUnitNumber(VEH_AIRCRAFT);
|
2004-08-09 17:04:08 +00:00
|
|
|
if (unit_num > _patches.max_aircraft)
|
|
|
|
return_cmd_error(STR_00E1_TOO_MANY_VEHICLES_IN_GAME);
|
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
2007-01-27 12:29:55 +00:00
|
|
|
Vehicle *v = vl[0]; // aircraft
|
|
|
|
Vehicle *u = vl[1]; // shadow
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
v->unitnumber = unit_num;
|
2007-04-29 21:24:08 +00:00
|
|
|
v = new (v) Aircraft();
|
|
|
|
u = new (u) Aircraft();
|
2007-01-10 18:56:51 +00:00
|
|
|
v->direction = DIR_SE;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
v->owner = u->owner = _current_player;
|
|
|
|
|
|
|
|
v->tile = tile;
|
|
|
|
// u->tile = 0;
|
|
|
|
|
2007-02-20 06:39:09 +00:00
|
|
|
uint x = TileX(tile) * TILE_SIZE + 5;
|
|
|
|
uint y = TileY(tile) * TILE_SIZE + 3;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
v->x_pos = u->x_pos = x;
|
|
|
|
v->y_pos = u->y_pos = y;
|
|
|
|
|
|
|
|
u->z_pos = GetSlopeZ(x, y);
|
|
|
|
v->z_pos = u->z_pos + 1;
|
|
|
|
|
|
|
|
// u->delta_x = u->delta_y = 0;
|
|
|
|
|
|
|
|
v->vehstatus = VS_HIDDEN | VS_STOPPED | VS_DEFPAL;
|
2006-07-26 08:32:20 +00:00
|
|
|
u->vehstatus = VS_HIDDEN | VS_UNCLICKABLE | VS_SHADOW;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2004-12-03 21:57:05 +00:00
|
|
|
v->spritenum = avi->image_index;
|
2004-08-09 17:04:08 +00:00
|
|
|
// v->cargo_count = u->number_of_pieces = 0;
|
|
|
|
|
2005-01-07 08:38:27 +00:00
|
|
|
v->cargo_cap = avi->passenger_capacity;
|
2004-12-03 21:57:05 +00:00
|
|
|
u->cargo_cap = avi->mail_capacity;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
v->cargo_type = CT_PASSENGERS;
|
|
|
|
u->cargo_type = CT_MAIL;
|
|
|
|
|
2006-05-19 10:04:03 +00:00
|
|
|
v->cargo_subtype = 0;
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
v->string_id = STR_SV_AIRCRAFT_NAME;
|
|
|
|
// v->next_order_param = v->next_order = 0;
|
|
|
|
|
|
|
|
// v->load_unload_time_rem = 0;
|
|
|
|
// v->progress = 0;
|
2005-02-02 16:16:43 +00:00
|
|
|
v->last_station_visited = INVALID_STATION;
|
2004-08-09 17:04:08 +00:00
|
|
|
// v->destination_coords = 0;
|
|
|
|
|
2004-12-03 21:57:05 +00:00
|
|
|
v->max_speed = avi->max_speed;
|
|
|
|
v->acceleration = avi->acceleration;
|
2005-10-01 12:43:34 +00:00
|
|
|
v->engine_type = p1;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-01-27 12:29:55 +00:00
|
|
|
v->subtype = (avi->subtype & AIR_CTOL ? AIR_AIRCRAFT : AIR_HELICOPTER);
|
2007-05-01 16:35:14 +00:00
|
|
|
v->UpdateDeltaXY(INVALID_DIR);
|
2007-06-21 14:32:27 +00:00
|
|
|
v->value = value.GetCost();
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-01-27 12:29:55 +00:00
|
|
|
u->subtype = AIR_SHADOW;
|
2007-05-01 16:35:14 +00:00
|
|
|
u->UpdateDeltaXY(INVALID_DIR);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-06-07 07:20:28 +00:00
|
|
|
/* Danger, Will Robinson!
|
|
|
|
* If the aircraft is refittable, but cannot be refitted to
|
|
|
|
* passengers, we select the cargo type from the refit mask.
|
|
|
|
* This is a fairly nasty hack to get around the fact that TTD
|
|
|
|
* has no default cargo type specifier for planes... */
|
2007-02-20 06:39:09 +00:00
|
|
|
CargoID cargo = FindFirstRefittableCargo(p1);
|
2006-06-07 07:20:28 +00:00
|
|
|
if (cargo != CT_INVALID && cargo != CT_PASSENGERS) {
|
|
|
|
uint16 callback = CALLBACK_FAILED;
|
|
|
|
|
|
|
|
v->cargo_type = cargo;
|
|
|
|
|
|
|
|
if (HASBIT(EngInfo(p1)->callbackmask, CBM_REFIT_CAPACITY)) {
|
|
|
|
callback = GetVehicleCallback(CBID_VEHICLE_REFIT_CAPACITY, 0, 0, v->engine_type, v);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (callback == CALLBACK_FAILED) {
|
|
|
|
/* Callback failed, or not executed; use the default cargo capacity */
|
2007-02-20 06:39:09 +00:00
|
|
|
v->cargo_cap = AircraftDefaultCargoCapacity(v->cargo_type, avi);
|
2006-06-07 07:20:28 +00:00
|
|
|
} else {
|
|
|
|
v->cargo_cap = callback;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the 'second compartent' capacity to none */
|
|
|
|
u->cargo_cap = 0;
|
|
|
|
}
|
|
|
|
|
2007-01-27 12:29:55 +00:00
|
|
|
const Engine *e = GetEngine(p1);
|
2004-08-09 17:04:08 +00:00
|
|
|
v->reliability = e->reliability;
|
|
|
|
v->reliability_spd_dec = e->reliability_spd_dec;
|
|
|
|
v->max_age = e->lifelength * 366;
|
|
|
|
|
2005-10-29 21:54:28 +00:00
|
|
|
_new_vehicle_id = v->index;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-09-13 13:30:18 +00:00
|
|
|
/* When we click on hangar we know the tile it is on. By that we know
|
|
|
|
* its position in the array of depots the airport has.....we can search
|
|
|
|
* layout for #th position of depot. Since layout must start with a listing
|
|
|
|
* of all depots, it is simple */
|
2007-02-03 08:30:22 +00:00
|
|
|
for (uint i = 0;; i++) {
|
2007-03-06 22:11:58 +00:00
|
|
|
const Station *st = GetStationByTile(tile);
|
|
|
|
const AirportFTAClass *apc = st->Airport();
|
|
|
|
|
2007-02-03 08:30:22 +00:00
|
|
|
assert(i != apc->nof_depots);
|
|
|
|
if (st->airport_tile + ToTileIndexDiff(apc->airport_depots[i]) == tile) {
|
|
|
|
assert(apc->layout[i].heading == HANGAR);
|
|
|
|
v->u.air.pos = apc->layout[i].position;
|
|
|
|
break;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
v->u.air.state = HANGAR;
|
|
|
|
v->u.air.previous_pos = v->u.air.pos;
|
2006-03-24 08:55:08 +00:00
|
|
|
v->u.air.targetairport = GetStationIndex(tile);
|
2004-08-09 17:04:08 +00:00
|
|
|
v->next = u;
|
|
|
|
|
|
|
|
v->service_interval = _patches.servint_aircraft;
|
|
|
|
|
|
|
|
v->date_of_last_service = _date;
|
2006-08-20 19:05:28 +00:00
|
|
|
v->build_year = u->build_year = _cur_year;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
v->cur_image = u->cur_image = 0xEA0;
|
|
|
|
|
2005-12-28 22:29:59 +00:00
|
|
|
v->random_bits = VehicleRandomBits();
|
|
|
|
u->random_bits = VehicleRandomBits();
|
|
|
|
|
2007-02-28 17:59:05 +00:00
|
|
|
v->vehicle_flags = 0;
|
|
|
|
if (e->flags & ENGINE_EXCLUSIVE_PREVIEW) SETBIT(v->vehicle_flags, VF_BUILT_AS_PROTOTYPE);
|
|
|
|
|
2007-04-18 18:37:40 +00:00
|
|
|
UpdateAircraftCache(v);
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
VehiclePositionChanged(v);
|
|
|
|
VehiclePositionChanged(u);
|
|
|
|
|
2007-02-23 11:50:43 +00:00
|
|
|
/* Aircraft with 3 vehicles (chopper)? */
|
2007-01-27 12:29:55 +00:00
|
|
|
if (v->subtype == AIR_HELICOPTER) {
|
|
|
|
Vehicle *w = vl[2];
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
u->next = w;
|
|
|
|
|
2007-04-29 21:24:08 +00:00
|
|
|
w = new (w) Aircraft();
|
2007-01-10 18:56:51 +00:00
|
|
|
w->direction = DIR_N;
|
2004-08-09 17:04:08 +00:00
|
|
|
w->owner = _current_player;
|
|
|
|
w->x_pos = v->x_pos;
|
|
|
|
w->y_pos = v->y_pos;
|
|
|
|
w->z_pos = v->z_pos + 5;
|
|
|
|
w->vehstatus = VS_HIDDEN | VS_UNCLICKABLE;
|
2006-05-01 20:05:03 +00:00
|
|
|
w->spritenum = 0xFF;
|
2007-01-27 12:29:55 +00:00
|
|
|
w->subtype = AIR_ROTOR;
|
2005-10-15 11:06:54 +00:00
|
|
|
w->cur_image = SPR_ROTOR_STOPPED;
|
2005-12-28 22:29:59 +00:00
|
|
|
w->random_bits = VehicleRandomBits();
|
2006-05-01 20:05:03 +00:00
|
|
|
/* Use rotor's air.state to store the rotor animation frame */
|
|
|
|
w->u.air.state = HRS_ROTOR_STOPPED;
|
2007-05-01 16:35:14 +00:00
|
|
|
w->UpdateDeltaXY(INVALID_DIR);
|
2004-08-09 17:04:08 +00:00
|
|
|
VehiclePositionChanged(w);
|
|
|
|
}
|
|
|
|
|
2006-10-05 12:59:28 +00:00
|
|
|
InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
|
2004-12-10 18:16:08 +00:00
|
|
|
RebuildVehicleLists();
|
2004-08-09 17:04:08 +00:00
|
|
|
InvalidateWindow(WC_COMPANY, v->owner);
|
2005-11-08 23:18:09 +00:00
|
|
|
if (IsLocalPlayer())
|
2007-08-07 23:07:10 +00:00
|
|
|
InvalidateAutoreplaceWindow(v->engine_type, v->group_id); //updates the replace Aircraft window
|
2007-02-06 11:11:12 +00:00
|
|
|
|
|
|
|
GetPlayer(_current_player)->num_engines[p1]++;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-11-14 19:44:06 +00:00
|
|
|
static void DoDeleteAircraft(Vehicle *v)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
DeleteWindowById(WC_VEHICLE_VIEW, v->index);
|
2004-12-10 18:16:08 +00:00
|
|
|
RebuildVehicleLists();
|
2004-08-09 17:04:08 +00:00
|
|
|
InvalidateWindow(WC_COMPANY, v->owner);
|
2006-12-05 22:59:42 +00:00
|
|
|
DeleteDepotHighlightOfVehicle(v);
|
2004-08-09 17:04:08 +00:00
|
|
|
DeleteVehicleChain(v);
|
2005-01-18 09:35:31 +00:00
|
|
|
InvalidateWindowClasses(WC_AIRCRAFT_LIST);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2005-05-11 00:00:27 +00:00
|
|
|
/** Sell an aircraft.
|
2006-04-10 07:15:58 +00:00
|
|
|
* @param tile unused
|
2007-02-23 11:50:43 +00:00
|
|
|
* @param flags for command type
|
2005-05-11 00:00:27 +00:00
|
|
|
* @param p1 vehicle ID to be sold
|
|
|
|
* @param p2 unused
|
2007-02-23 11:50:43 +00:00
|
|
|
* @return result of operation. Error or sold value
|
2005-05-11 00:00:27 +00:00
|
|
|
*/
|
2007-06-18 10:48:15 +00:00
|
|
|
CommandCost CmdSellAircraft(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2006-08-22 18:15:17 +00:00
|
|
|
if (!IsValidVehicleID(p1)) return CMD_ERROR;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-02-20 06:39:09 +00:00
|
|
|
Vehicle *v = GetVehicle(p1);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-03-08 16:27:54 +00:00
|
|
|
if (v->type != VEH_AIRCRAFT || !CheckOwnership(v->owner)) return CMD_ERROR;
|
2007-08-29 21:49:08 +00:00
|
|
|
if (!v->IsStoppedInDepot()) return_cmd_error(STR_A01B_AIRCRAFT_MUST_BE_STOPPED);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-01-30 20:50:06 +00:00
|
|
|
SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES);
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
if (flags & DC_EXEC) {
|
|
|
|
// Invalidate depot
|
|
|
|
InvalidateWindow(WC_VEHICLE_DEPOT, v->tile);
|
|
|
|
DoDeleteAircraft(v);
|
|
|
|
}
|
|
|
|
|
2007-06-18 21:44:47 +00:00
|
|
|
return CommandCost(-v->value);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2005-05-11 00:00:27 +00:00
|
|
|
/** Start/Stop an aircraft.
|
2006-04-10 07:15:58 +00:00
|
|
|
* @param tile unused
|
2007-02-23 11:50:43 +00:00
|
|
|
* @param flags for command type
|
2005-05-12 00:11:37 +00:00
|
|
|
* @param p1 aircraft ID to start/stop
|
2005-05-11 00:00:27 +00:00
|
|
|
* @param p2 unused
|
2007-02-23 11:50:43 +00:00
|
|
|
* @return result of operation. Nothing if everything went well
|
2005-05-11 00:00:27 +00:00
|
|
|
*/
|
2007-06-18 10:48:15 +00:00
|
|
|
CommandCost CmdStartStopAircraft(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2006-08-22 18:15:17 +00:00
|
|
|
if (!IsValidVehicleID(p1)) return CMD_ERROR;
|
2005-01-30 20:50:06 +00:00
|
|
|
|
2007-02-20 06:39:09 +00:00
|
|
|
Vehicle *v = GetVehicle(p1);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-03-08 16:27:54 +00:00
|
|
|
if (v->type != VEH_AIRCRAFT || !CheckOwnership(v->owner)) return CMD_ERROR;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-02-23 11:50:43 +00:00
|
|
|
/* cannot stop airplane when in flight, or when taking off / landing */
|
2006-06-23 22:05:40 +00:00
|
|
|
if (v->u.air.state >= STARTTAKEOFF && v->u.air.state < TERM7)
|
2004-08-09 17:04:08 +00:00
|
|
|
return_cmd_error(STR_A017_AIRCRAFT_IS_IN_FLIGHT);
|
|
|
|
|
2006-08-21 16:35:47 +00:00
|
|
|
/* Check if this aircraft can be started/stopped. The callback will fail or
|
|
|
|
* return 0xFF if it can. */
|
2007-02-20 06:39:09 +00:00
|
|
|
uint16 callback = GetVehicleCallback(CBID_VEHICLE_START_STOP_CHECK, 0, 0, v->engine_type, v);
|
2006-08-21 16:35:47 +00:00
|
|
|
if (callback != CALLBACK_FAILED && callback != 0xFF) {
|
|
|
|
StringID error = GetGRFStringID(GetEngineGRFID(v->engine_type), 0xD000 + callback);
|
|
|
|
return_cmd_error(error);
|
|
|
|
}
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
if (flags & DC_EXEC) {
|
2007-08-29 21:49:08 +00:00
|
|
|
if (v->IsStoppedInDepot()) {
|
2006-03-04 11:01:35 +00:00
|
|
|
DeleteVehicleNews(p1, STR_A014_AIRCRAFT_IS_WAITING_IN);
|
|
|
|
}
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
v->vehstatus ^= VS_STOPPED;
|
2004-12-21 23:27:58 +00:00
|
|
|
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
|
2004-08-09 17:04:08 +00:00
|
|
|
InvalidateWindow(WC_VEHICLE_DEPOT, v->tile);
|
2005-01-18 09:35:31 +00:00
|
|
|
InvalidateWindowClasses(WC_AIRCRAFT_LIST);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-06-18 19:53:50 +00:00
|
|
|
return CommandCost();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2005-05-12 00:11:37 +00:00
|
|
|
/** Send an aircraft to the hangar.
|
2006-04-10 07:15:58 +00:00
|
|
|
* @param tile unused
|
2007-02-23 11:50:43 +00:00
|
|
|
* @param flags for command type
|
2005-05-12 00:11:37 +00:00
|
|
|
* @param p1 vehicle ID to send to the hangar
|
|
|
|
* @param p2 various bitmasked elements
|
2006-09-01 10:24:15 +00:00
|
|
|
* - p2 bit 0-3 - DEPOT_ flags (see vehicle.h)
|
|
|
|
* - p2 bit 8-10 - VLW flag (for mass goto depot)
|
2007-02-23 11:50:43 +00:00
|
|
|
* @return o if everything went well
|
2005-05-12 00:11:37 +00:00
|
|
|
*/
|
2007-06-18 10:48:15 +00:00
|
|
|
CommandCost CmdSendAircraftToHangar(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2006-09-01 10:24:15 +00:00
|
|
|
if (p2 & DEPOT_MASS_SEND) {
|
|
|
|
/* Mass goto depot requested */
|
2006-09-04 15:16:58 +00:00
|
|
|
if (!ValidVLWFlags(p2 & VLW_MASK)) return CMD_ERROR;
|
2007-03-08 16:27:54 +00:00
|
|
|
return SendAllVehiclesToDepot(VEH_AIRCRAFT, flags, p2 & DEPOT_SERVICE, _current_player, (p2 & VLW_MASK), p1);
|
2006-08-30 21:39:01 +00:00
|
|
|
}
|
|
|
|
|
2006-09-01 10:24:15 +00:00
|
|
|
if (!IsValidVehicleID(p1)) return CMD_ERROR;
|
2005-01-30 20:50:06 +00:00
|
|
|
|
2007-02-20 06:39:09 +00:00
|
|
|
Vehicle *v = GetVehicle(p1);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-08-29 21:49:08 +00:00
|
|
|
if (v->type != VEH_AIRCRAFT || !CheckOwnership(v->owner) || v->IsInDepot()) return CMD_ERROR;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-09-01 10:24:15 +00:00
|
|
|
if (v->current_order.type == OT_GOTO_DEPOT && !(p2 & DEPOT_LOCATE_HANGAR)) {
|
2006-09-02 09:35:03 +00:00
|
|
|
if (!!(p2 & DEPOT_SERVICE) == HASBIT(v->current_order.flags, OFB_HALT_IN_DEPOT)) {
|
2006-09-01 12:37:03 +00:00
|
|
|
/* We called with a different DEPOT_SERVICE setting.
|
|
|
|
* Now we change the setting to apply the new one and let the vehicle head for the same hangar.
|
2006-09-04 20:40:33 +00:00
|
|
|
* Note: the if is (true for requesting service == true for ordered to stop in hangar) */
|
2006-09-01 12:37:03 +00:00
|
|
|
if (flags & DC_EXEC) {
|
|
|
|
TOGGLEBIT(v->current_order.flags, OFB_HALT_IN_DEPOT);
|
|
|
|
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
|
|
|
|
}
|
2007-06-18 19:53:50 +00:00
|
|
|
return CommandCost();
|
2006-09-01 12:37:03 +00:00
|
|
|
}
|
|
|
|
|
2006-09-01 10:24:15 +00:00
|
|
|
if (p2 & DEPOT_DONT_CANCEL) return CMD_ERROR; // Requested no cancelation of hangar orders
|
2004-08-09 17:04:08 +00:00
|
|
|
if (flags & DC_EXEC) {
|
2004-12-05 12:43:04 +00:00
|
|
|
if (v->current_order.flags & OF_UNLOAD) v->cur_order_index++;
|
|
|
|
v->current_order.type = OT_DUMMY;
|
|
|
|
v->current_order.flags = 0;
|
2004-12-21 23:27:58 +00:00
|
|
|
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
} else {
|
2005-02-01 22:04:53 +00:00
|
|
|
bool next_airport_has_hangar = true;
|
2006-08-27 09:28:52 +00:00
|
|
|
StationID next_airport_index = v->u.air.targetairport;
|
2005-05-12 00:11:37 +00:00
|
|
|
const Station *st = GetStation(next_airport_index);
|
2006-07-03 21:14:21 +00:00
|
|
|
/* If the station is not a valid airport or if it has no hangars */
|
2007-02-16 09:38:43 +00:00
|
|
|
if (!st->IsValid() || st->airport_tile == 0 || st->Airport()->nof_depots == 0) {
|
2007-02-23 11:50:43 +00:00
|
|
|
/* the aircraft has to search for a hangar on its own */
|
2007-02-20 06:39:09 +00:00
|
|
|
StationID station = FindNearestHangar(v);
|
2005-05-12 00:11:37 +00:00
|
|
|
|
|
|
|
next_airport_has_hangar = false;
|
2006-09-01 10:24:15 +00:00
|
|
|
if (station == INVALID_STATION) return CMD_ERROR;
|
2005-05-12 00:11:37 +00:00
|
|
|
next_airport_index = station;
|
2005-02-01 22:04:53 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
2007-04-20 11:39:01 +00:00
|
|
|
if (v->current_order.type == OT_LOADING) v->LeaveStation();
|
|
|
|
|
2004-12-05 12:43:04 +00:00
|
|
|
v->current_order.type = OT_GOTO_DEPOT;
|
2006-08-27 09:28:52 +00:00
|
|
|
v->current_order.flags = OF_NON_STOP;
|
2006-10-28 14:37:33 +00:00
|
|
|
if (!(p2 & DEPOT_SERVICE)) SETBIT(v->current_order.flags, OFB_HALT_IN_DEPOT);
|
2006-12-05 23:57:04 +00:00
|
|
|
v->current_order.refit_cargo = CT_INVALID;
|
2006-09-03 08:25:27 +00:00
|
|
|
v->current_order.dest = next_airport_index;
|
2004-12-21 23:27:58 +00:00
|
|
|
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
|
2006-12-30 21:41:15 +00:00
|
|
|
if (v->u.air.state == FLYING && !next_airport_has_hangar) {
|
2006-09-01 10:24:15 +00:00
|
|
|
/* The aircraft is now heading for a different hangar than the next in the orders */
|
2005-01-27 15:43:44 +00:00
|
|
|
AircraftNextAirportPos_and_Order(v);
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-18 19:53:50 +00:00
|
|
|
return CommandCost();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-05-14 12:36:16 +00:00
|
|
|
/** Refits an aircraft to the specified cargo type.
|
2006-04-10 07:15:58 +00:00
|
|
|
* @param tile unused
|
2007-02-23 11:50:43 +00:00
|
|
|
* @param flags for command type
|
2005-05-14 12:36:16 +00:00
|
|
|
* @param p1 vehicle ID of the aircraft to refit
|
|
|
|
* @param p2 various bitstuffed elements
|
2005-11-14 08:09:57 +00:00
|
|
|
* - p2 = (bit 0-7) - the new cargo type to refit to
|
2006-06-04 17:38:48 +00:00
|
|
|
* - p2 = (bit 8-15) - the new cargo subtype to refit to
|
2007-04-29 08:43:00 +00:00
|
|
|
* - p2 = (bit 16) - refit only this vehicle (ignored)
|
2007-02-23 11:50:43 +00:00
|
|
|
* @return cost of refit or error
|
2005-05-14 12:36:16 +00:00
|
|
|
*/
|
2007-06-18 10:48:15 +00:00
|
|
|
CommandCost CmdRefitAircraft(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2006-06-04 17:38:48 +00:00
|
|
|
byte new_subtype = GB(p2, 8, 8);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-08-22 18:15:17 +00:00
|
|
|
if (!IsValidVehicleID(p1)) return CMD_ERROR;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-02-20 06:39:09 +00:00
|
|
|
Vehicle *v = GetVehicle(p1);
|
2005-01-30 20:50:06 +00:00
|
|
|
|
2007-03-08 16:27:54 +00:00
|
|
|
if (v->type != VEH_AIRCRAFT || !CheckOwnership(v->owner)) return CMD_ERROR;
|
2007-08-29 21:49:08 +00:00
|
|
|
if (!v->IsStoppedInDepot()) return_cmd_error(STR_A01B_AIRCRAFT_MUST_BE_STOPPED);
|
2005-01-30 20:50:06 +00:00
|
|
|
|
2005-05-14 12:36:16 +00:00
|
|
|
/* Check cargo */
|
2007-02-20 06:39:09 +00:00
|
|
|
CargoID new_cid = GB(p2, 0, 8);
|
2007-03-16 21:42:11 +00:00
|
|
|
if (new_cid >= NUM_CARGO || !CanRefitTo(v->engine_type, new_cid)) return CMD_ERROR;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-01-30 20:50:06 +00:00
|
|
|
SET_EXPENSES_TYPE(EXPENSES_AIRCRAFT_RUN);
|
|
|
|
|
2006-06-07 07:33:56 +00:00
|
|
|
/* Check the refit capacity callback */
|
2007-02-20 06:39:09 +00:00
|
|
|
uint16 callback = CALLBACK_FAILED;
|
2006-06-07 07:33:56 +00:00
|
|
|
if (HASBIT(EngInfo(v->engine_type)->callbackmask, CBM_REFIT_CAPACITY)) {
|
|
|
|
/* Back up the existing cargo type */
|
|
|
|
CargoID temp_cid = v->cargo_type;
|
2006-06-09 07:03:53 +00:00
|
|
|
byte temp_subtype = v->cargo_subtype;
|
2006-06-07 07:33:56 +00:00
|
|
|
v->cargo_type = new_cid;
|
2006-06-09 07:03:53 +00:00
|
|
|
v->cargo_subtype = new_subtype;
|
2006-06-07 07:33:56 +00:00
|
|
|
|
|
|
|
callback = GetVehicleCallback(CBID_VEHICLE_REFIT_CAPACITY, 0, 0, v->engine_type, v);
|
|
|
|
|
|
|
|
/* Restore the cargo type */
|
|
|
|
v->cargo_type = temp_cid;
|
2006-06-09 07:03:53 +00:00
|
|
|
v->cargo_subtype = temp_subtype;
|
2006-06-07 07:33:56 +00:00
|
|
|
}
|
|
|
|
|
2007-02-20 06:39:09 +00:00
|
|
|
const AircraftVehicleInfo *avi = AircraftVehInfo(v->engine_type);
|
|
|
|
|
|
|
|
uint pass;
|
2006-06-07 07:33:56 +00:00
|
|
|
if (callback == CALLBACK_FAILED) {
|
|
|
|
/* If the callback failed, or wasn't executed, use the aircraft's
|
|
|
|
* default cargo capacity */
|
2007-02-20 06:39:09 +00:00
|
|
|
pass = AircraftDefaultCargoCapacity(new_cid, avi);
|
2006-06-07 07:33:56 +00:00
|
|
|
} else {
|
|
|
|
pass = callback;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2006-02-11 09:24:51 +00:00
|
|
|
_returned_refit_capacity = pass;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-06-18 19:53:50 +00:00
|
|
|
CommandCost cost;
|
2006-10-14 15:15:56 +00:00
|
|
|
if (IsHumanPlayer(v->owner) && new_cid != v->cargo_type) {
|
2006-09-04 09:07:52 +00:00
|
|
|
cost = GetRefitCost(v->engine_type);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
|
|
|
v->cargo_cap = pass;
|
|
|
|
|
2007-08-30 13:03:56 +00:00
|
|
|
Vehicle *u = v->Next();
|
2007-03-19 20:54:13 +00:00
|
|
|
uint mail = IsCargoInClass(new_cid, CC_PASSENGERS) ? avi->mail_capacity : 0;
|
2004-08-09 17:04:08 +00:00
|
|
|
u->cargo_cap = mail;
|
2007-06-22 11:58:59 +00:00
|
|
|
v->cargo.Truncate(v->cargo_type == new_cid ? pass : 0);
|
|
|
|
u->cargo.Truncate(v->cargo_type == new_cid ? mail : 0);
|
2005-05-14 12:36:16 +00:00
|
|
|
v->cargo_type = new_cid;
|
2006-06-04 17:38:48 +00:00
|
|
|
v->cargo_subtype = new_subtype;
|
2004-08-09 17:04:08 +00:00
|
|
|
InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
|
2006-05-02 19:24:02 +00:00
|
|
|
InvalidateWindow(WC_VEHICLE_DEPOT, v->tile);
|
|
|
|
RebuildVehicleLists();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return cost;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void CheckIfAircraftNeedsService(Vehicle *v)
|
|
|
|
{
|
2005-11-14 19:48:04 +00:00
|
|
|
if (_patches.servint_aircraft == 0) return;
|
|
|
|
if (!VehicleNeedsService(v)) return;
|
|
|
|
if (v->vehstatus & VS_STOPPED) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2004-12-05 12:43:04 +00:00
|
|
|
if (v->current_order.type == OT_GOTO_DEPOT &&
|
2005-03-20 08:43:29 +00:00
|
|
|
v->current_order.flags & OF_HALT_IN_DEPOT)
|
2004-08-09 17:04:08 +00:00
|
|
|
return;
|
|
|
|
|
2005-11-14 19:48:04 +00:00
|
|
|
if (_patches.gotodepot && VehicleHasDepotOrders(v)) return;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2007-08-29 21:49:08 +00:00
|
|
|
if (v->IsInDepot()) {
|
2006-09-03 11:49:38 +00:00
|
|
|
VehicleServiceInDepot(v);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-02-20 06:39:09 +00:00
|
|
|
const Station *st = GetStation(v->current_order.dest);
|
2007-02-23 11:50:43 +00:00
|
|
|
/* only goto depot if the target airport has terminals (eg. it is airport) */
|
2007-02-16 09:38:43 +00:00
|
|
|
if (st->IsValid() && st->airport_tile != 0 && st->Airport()->terminals != NULL) {
|
2004-08-09 17:04:08 +00:00
|
|
|
// printf("targetairport = %d, st->index = %d\n", v->u.air.targetairport, st->index);
|
|
|
|
// v->u.air.targetairport = st->index;
|
2007-05-14 08:07:20 +00:00
|
|
|
if (v->current_order.type == OT_LOADING) v->LeaveStation();
|
2004-12-05 12:43:04 +00:00
|
|
|
v->current_order.type = OT_GOTO_DEPOT;
|
|
|
|
v->current_order.flags = OF_NON_STOP;
|
2004-12-21 23:27:58 +00:00
|
|
|
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
|
2004-12-05 12:43:04 +00:00
|
|
|
} else if (v->current_order.type == OT_GOTO_DEPOT) {
|
|
|
|
v->current_order.type = OT_DUMMY;
|
|
|
|
v->current_order.flags = 0;
|
2004-12-21 23:27:58 +00:00
|
|
|
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void OnNewDay_Aircraft(Vehicle *v)
|
|
|
|
{
|
2007-01-27 12:29:55 +00:00
|
|
|
if (!IsNormalAircraft(v)) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-11-14 19:48:04 +00:00
|
|
|
if ((++v->day_counter & 7) == 0) DecreaseVehicleValue(v);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-03-04 11:15:44 +00:00
|
|
|
CheckOrders(v);
|
2004-08-11 10:15:38 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
CheckVehicleBreakdown(v);
|
|
|
|
AgeVehicle(v);
|
|
|
|
CheckIfAircraftNeedsService(v);
|
|
|
|
|
2005-11-14 19:48:04 +00:00
|
|
|
if (v->vehstatus & VS_STOPPED) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-06-18 19:53:50 +00:00
|
|
|
CommandCost cost = CommandCost(GetVehicleProperty(v, 0x0E, AircraftVehInfo(v->engine_type)->running_cost) * _price.aircraft_running / 364);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-06-18 19:53:50 +00:00
|
|
|
v->profit_this_year -= cost.GetCost() >> 8;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
SET_EXPENSES_TYPE(EXPENSES_AIRCRAFT_RUN);
|
|
|
|
SubtractMoneyFromPlayerFract(v->owner, cost);
|
|
|
|
|
|
|
|
InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
|
2005-01-18 09:35:31 +00:00
|
|
|
InvalidateWindowClasses(WC_AIRCRAFT_LIST);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
void AircraftYearlyLoop()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
Vehicle *v;
|
|
|
|
|
|
|
|
FOR_ALL_VEHICLES(v) {
|
2007-03-08 16:27:54 +00:00
|
|
|
if (v->type == VEH_AIRCRAFT && IsNormalAircraft(v)) {
|
2004-08-09 17:04:08 +00:00
|
|
|
v->profit_last_year = v->profit_this_year;
|
|
|
|
v->profit_this_year = 0;
|
|
|
|
InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void AgeAircraftCargo(Vehicle *v)
|
|
|
|
{
|
2005-11-14 19:48:04 +00:00
|
|
|
if (_age_cargo_skip_counter != 0) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
do {
|
2007-06-22 11:58:59 +00:00
|
|
|
v->cargo.AgeCargo();
|
2007-08-30 13:03:56 +00:00
|
|
|
v = v->Next();
|
2005-11-14 19:48:04 +00:00
|
|
|
} while (v != NULL);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void HelicopterTickHandler(Vehicle *v)
|
|
|
|
{
|
2007-08-30 13:03:56 +00:00
|
|
|
Vehicle *u = v->Next()->Next();
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-11-14 19:48:04 +00:00
|
|
|
if (u->vehstatus & VS_HIDDEN) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-02-23 11:50:43 +00:00
|
|
|
/* if true, helicopter rotors do not rotate. This should only be the case if a helicopter is
|
|
|
|
* loading/unloading at a terminal or stopped */
|
2004-12-05 12:43:04 +00:00
|
|
|
if (v->current_order.type == OT_LOADING || (v->vehstatus & VS_STOPPED)) {
|
2004-08-09 17:04:08 +00:00
|
|
|
if (u->cur_speed != 0) {
|
|
|
|
u->cur_speed++;
|
2006-05-01 20:05:03 +00:00
|
|
|
if (u->cur_speed >= 0x80 && u->u.air.state == HRS_ROTOR_MOVING_3) {
|
2004-08-09 17:04:08 +00:00
|
|
|
u->cur_speed = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (u->cur_speed == 0)
|
|
|
|
u->cur_speed = 0x70;
|
|
|
|
|
|
|
|
if (u->cur_speed >= 0x50)
|
|
|
|
u->cur_speed--;
|
|
|
|
}
|
|
|
|
|
2007-02-20 06:39:09 +00:00
|
|
|
int tick = ++u->tick_counter;
|
|
|
|
int spd = u->cur_speed >> 4;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-02-20 06:39:09 +00:00
|
|
|
SpriteID img;
|
2004-08-09 17:04:08 +00:00
|
|
|
if (spd == 0) {
|
2006-05-01 20:05:03 +00:00
|
|
|
u->u.air.state = HRS_ROTOR_STOPPED;
|
|
|
|
img = GetRotorImage(v);
|
2005-10-23 13:04:44 +00:00
|
|
|
if (u->cur_image == img) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
} else if (tick >= spd) {
|
|
|
|
u->tick_counter = 0;
|
2006-05-01 20:05:03 +00:00
|
|
|
u->u.air.state++;
|
|
|
|
if (u->u.air.state > HRS_ROTOR_MOVING_3) u->u.air.state = HRS_ROTOR_MOVING_1;
|
|
|
|
img = GetRotorImage(v);
|
2005-10-23 13:04:44 +00:00
|
|
|
} else {
|
2004-08-09 17:04:08 +00:00
|
|
|
return;
|
2005-10-23 13:04:44 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-10-23 13:04:44 +00:00
|
|
|
u->cur_image = img;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
BeginVehicleMove(u);
|
|
|
|
VehiclePositionChanged(u);
|
|
|
|
EndVehicleMove(u);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void SetAircraftPosition(Vehicle *v, int x, int y, int z)
|
|
|
|
{
|
|
|
|
v->x_pos = x;
|
|
|
|
v->y_pos = y;
|
|
|
|
v->z_pos = z;
|
|
|
|
|
2007-07-01 19:11:47 +00:00
|
|
|
v->cur_image = v->GetImage(v->direction);
|
2007-08-30 13:03:56 +00:00
|
|
|
if (v->subtype == AIR_HELICOPTER) v->Next()->Next()->cur_image = GetRotorImage(v);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
BeginVehicleMove(v);
|
|
|
|
VehiclePositionChanged(v);
|
|
|
|
EndVehicleMove(v);
|
|
|
|
|
2007-08-30 13:03:56 +00:00
|
|
|
Vehicle *u = v->Next();
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-02-20 06:39:09 +00:00
|
|
|
int safe_x = clamp(x, 0, MapMaxX() * TILE_SIZE);
|
|
|
|
int safe_y = clamp(y - 1, 0, MapMaxY() * TILE_SIZE);
|
2004-08-09 17:04:08 +00:00
|
|
|
u->x_pos = x;
|
2006-08-13 14:46:16 +00:00
|
|
|
u->y_pos = y - ((v->z_pos-GetSlopeZ(safe_x, safe_y)) >> 3);;
|
|
|
|
|
|
|
|
safe_y = clamp(u->y_pos, 0, MapMaxY() * TILE_SIZE);
|
|
|
|
u->z_pos = GetSlopeZ(safe_x, safe_y);
|
2004-08-09 17:04:08 +00:00
|
|
|
u->cur_image = v->cur_image;
|
|
|
|
|
|
|
|
BeginVehicleMove(u);
|
|
|
|
VehiclePositionChanged(u);
|
|
|
|
EndVehicleMove(u);
|
|
|
|
|
2007-08-30 13:03:56 +00:00
|
|
|
u = u->Next();
|
2005-11-14 19:48:04 +00:00
|
|
|
if (u != NULL) {
|
2004-08-09 17:04:08 +00:00
|
|
|
u->x_pos = x;
|
|
|
|
u->y_pos = y;
|
|
|
|
u->z_pos = z + 5;
|
|
|
|
|
|
|
|
BeginVehicleMove(u);
|
|
|
|
VehiclePositionChanged(u);
|
|
|
|
EndVehicleMove(u);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-10-04 12:01:59 +00:00
|
|
|
/** Handle Aircraft specific tasks when a an Aircraft enters a hangar
|
|
|
|
* @param *v Vehicle that enters the hangar
|
|
|
|
*/
|
2006-10-05 02:26:27 +00:00
|
|
|
void HandleAircraftEnterHangar(Vehicle *v)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
v->subspeed = 0;
|
|
|
|
v->progress = 0;
|
|
|
|
|
2007-08-30 13:03:56 +00:00
|
|
|
Vehicle *u = v->Next();
|
2004-08-09 17:04:08 +00:00
|
|
|
u->vehstatus |= VS_HIDDEN;
|
2007-08-30 13:03:56 +00:00
|
|
|
u = u->Next();
|
2005-11-14 19:48:04 +00:00
|
|
|
if (u != NULL) {
|
2004-08-09 17:04:08 +00:00
|
|
|
u->vehstatus |= VS_HIDDEN;
|
|
|
|
u->cur_speed = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
SetAircraftPosition(v, v->x_pos, v->y_pos, v->z_pos);
|
|
|
|
}
|
|
|
|
|
2005-10-23 13:04:44 +00:00
|
|
|
static void PlayAircraftSound(const Vehicle* v)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2006-09-27 18:17:01 +00:00
|
|
|
if (!PlayVehicleSound(v, VSE_START)) {
|
|
|
|
SndPlayVehicleFx(AircraftVehInfo(v->engine_type)->sfx, v);
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-04-18 18:37:40 +00:00
|
|
|
|
|
|
|
void UpdateAircraftCache(Vehicle *v)
|
|
|
|
{
|
|
|
|
uint max_speed = GetVehicleProperty(v, 0x0C, 0);
|
|
|
|
if (max_speed != 0) {
|
|
|
|
/* Convert from original units to (approx) km/h */
|
|
|
|
max_speed = (max_speed * 129) / 10;
|
|
|
|
|
|
|
|
v->u.air.cached_max_speed = max_speed;
|
|
|
|
} else {
|
|
|
|
v->u.air.cached_max_speed = 0xFFFF;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-03-02 12:01:24 +00:00
|
|
|
/**
|
|
|
|
* Special velocities for aircraft
|
2007-02-06 15:38:23 +00:00
|
|
|
*/
|
|
|
|
enum AircraftSpeedLimits {
|
2007-03-02 12:01:24 +00:00
|
|
|
SPEED_LIMIT_TAXI = 50, ///< Maximum speed of an aircraft while taxiing
|
|
|
|
SPEED_LIMIT_APPROACH = 230, ///< Maximum speed of an aircraft on finals
|
|
|
|
SPEED_LIMIT_BROKEN = 320, ///< Maximum speed of an aircraft that is broken
|
|
|
|
SPEED_LIMIT_HOLD = 425, ///< Maximum speed of an aircraft that flies the holding pattern
|
|
|
|
SPEED_LIMIT_NONE = 0xFFFF ///< No environmental speed limit. Speed limit is type dependent
|
2007-02-06 15:38:23 +00:00
|
|
|
};
|
|
|
|
|
2007-03-02 12:01:24 +00:00
|
|
|
/**
|
|
|
|
* Sets the new speed for an aircraft
|
|
|
|
* @param v The vehicle for which the speed should be obtained
|
|
|
|
* @param speed_limit The maximum speed the vehicle may have.
|
|
|
|
* @param hard_limit If true, the limit is directly enforced, otherwise the plane is slowed down gradually
|
|
|
|
* @return The number of position updates needed within the tick
|
|
|
|
*/
|
|
|
|
static int UpdateAircraftSpeed(Vehicle *v, uint speed_limit = SPEED_LIMIT_NONE, bool hard_limit = true)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2007-03-02 12:01:24 +00:00
|
|
|
uint spd = v->acceleration * 16;
|
2004-08-09 17:04:08 +00:00
|
|
|
byte t;
|
|
|
|
|
2007-04-18 18:37:40 +00:00
|
|
|
if (v->u.air.cached_max_speed < speed_limit) {
|
|
|
|
if (v->cur_speed < speed_limit) hard_limit = false;
|
|
|
|
speed_limit = v->u.air.cached_max_speed;
|
|
|
|
}
|
|
|
|
|
2007-03-02 12:01:24 +00:00
|
|
|
speed_limit = min(speed_limit, v->max_speed);
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
v->subspeed = (t=v->subspeed) + (byte)spd;
|
2007-03-02 12:01:24 +00:00
|
|
|
|
|
|
|
if (!hard_limit && v->cur_speed > speed_limit) speed_limit = v->cur_speed - (v->cur_speed / 48);
|
|
|
|
|
2007-02-06 15:38:23 +00:00
|
|
|
spd = min(v->cur_speed + (spd >> 8) + (v->subspeed < t), speed_limit);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2007-02-23 11:50:43 +00:00
|
|
|
/* adjust speed for broken vehicles */
|
2007-02-06 15:38:23 +00:00
|
|
|
if (v->vehstatus & VS_AIRCRAFT_BROKEN) spd = min(spd, SPEED_LIMIT_BROKEN);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-02-23 11:50:43 +00:00
|
|
|
/* updates statusbar only if speed have changed to save CPU time */
|
2004-08-09 17:04:08 +00:00
|
|
|
if (spd != v->cur_speed) {
|
|
|
|
v->cur_speed = spd;
|
|
|
|
if (_patches.vehicle_speed)
|
2004-12-21 23:27:58 +00:00
|
|
|
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2005-11-14 19:48:04 +00:00
|
|
|
if (!(v->direction & 1)) spd = spd * 3 / 4;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-03-02 12:01:24 +00:00
|
|
|
spd += v->progress;
|
|
|
|
v->progress = (byte)spd;
|
|
|
|
return spd >> 8;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-02-02 15:04:59 +00:00
|
|
|
/**
|
|
|
|
* Gets the cruise altitude of an aircraft.
|
|
|
|
* The cruise altitude is determined by the velocity of the vehicle
|
|
|
|
* and the direction it is moving
|
|
|
|
* @param v The vehicle. Should be an aircraft
|
|
|
|
* @returns Altitude in pixel units
|
|
|
|
*/
|
2004-08-09 17:04:08 +00:00
|
|
|
static byte GetAircraftFlyingAltitude(const Vehicle *v)
|
|
|
|
{
|
2007-02-02 15:04:59 +00:00
|
|
|
/* Make sure Aircraft fly no lower so that they don't conduct
|
|
|
|
* CFITs (controlled flight into terrain)
|
|
|
|
*/
|
|
|
|
byte base_altitude = 150;
|
|
|
|
|
|
|
|
/* Make sure eastbound and westbound planes do not "crash" into each
|
|
|
|
* other by providing them with vertical seperation
|
|
|
|
*/
|
|
|
|
switch (v->direction) {
|
2007-02-20 06:39:09 +00:00
|
|
|
case DIR_N:
|
|
|
|
case DIR_NE:
|
|
|
|
case DIR_E:
|
|
|
|
case DIR_SE:
|
2007-03-02 12:01:24 +00:00
|
|
|
base_altitude += 10;
|
2007-02-20 06:39:09 +00:00
|
|
|
break;
|
|
|
|
|
2007-02-02 15:04:59 +00:00
|
|
|
default: break;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2007-02-02 15:04:59 +00:00
|
|
|
|
|
|
|
/* Make faster planes fly higher so that they can overtake slower ones */
|
2007-03-02 12:01:24 +00:00
|
|
|
base_altitude += min(20 * (v->max_speed / 200), 90);
|
2007-02-02 15:04:59 +00:00
|
|
|
|
|
|
|
return base_altitude;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-03-29 13:52:34 +00:00
|
|
|
/**
|
|
|
|
* Find the entry point to an airport depending on direction which
|
|
|
|
* the airport is being approached from. Each airport can have up to
|
|
|
|
* four entry points for its approach system so that approaching
|
|
|
|
* aircraft do not fly through each other or are forced to do 180
|
|
|
|
* degree turns during the approach. The arrivals are grouped into
|
|
|
|
* four sectors dependent on the DiagDirection from which the airport
|
|
|
|
* is approached.
|
|
|
|
*
|
|
|
|
* @param v The vehicle that is approaching the airport
|
|
|
|
* @param apc The Airport Class being approached.
|
|
|
|
* @returns The index of the entry point
|
|
|
|
*/
|
|
|
|
static byte AircraftGetEntryPoint(const Vehicle *v, const AirportFTAClass *apc)
|
|
|
|
{
|
|
|
|
assert(v != NULL);
|
|
|
|
assert(apc != NULL);
|
|
|
|
|
|
|
|
const Station *st = GetStation(v->u.air.targetairport);
|
|
|
|
/* Make sure we don't go to 0,0 if the airport has been removed. */
|
|
|
|
TileIndex tile = (st->airport_tile != 0) ? st->airport_tile : st->xy;
|
|
|
|
|
|
|
|
int delta_x = v->x_pos - TileX(tile) * TILE_SIZE;
|
|
|
|
int delta_y = v->y_pos - TileY(tile) * TILE_SIZE;
|
|
|
|
|
|
|
|
DiagDirection dir;
|
|
|
|
if (abs(delta_y) < abs(delta_x)) {
|
|
|
|
/* We are northeast or southwest of the airport */
|
|
|
|
dir = delta_x < 0 ? DIAGDIR_NE : DIAGDIR_SW;
|
|
|
|
} else {
|
|
|
|
/* We are northwest or southeast of the airport */
|
|
|
|
dir = delta_y < 0 ? DIAGDIR_NW : DIAGDIR_SE;
|
|
|
|
}
|
|
|
|
return apc->entry_points[dir];
|
|
|
|
}
|
|
|
|
|
2007-03-02 12:01:24 +00:00
|
|
|
/**
|
|
|
|
* Controls the movement of an aircraft. This function actually moves the vehicle
|
|
|
|
* on the map and takes care of minor things like sound playback.
|
|
|
|
* @todo De-mystify the cur_speed values for helicopter rotors.
|
|
|
|
* @param v The vehicle that is moved. Must be the first vehicle of the chain
|
|
|
|
* @return Whether the position requested by the State Machine has been reached
|
|
|
|
*/
|
2005-02-22 19:38:44 +00:00
|
|
|
static bool AircraftController(Vehicle *v)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2007-03-02 12:01:24 +00:00
|
|
|
int count;
|
2007-02-20 06:39:09 +00:00
|
|
|
const Station *st = GetStation(v->u.air.targetairport);
|
2007-03-29 13:52:34 +00:00
|
|
|
const AirportFTAClass *afc = st->Airport();
|
|
|
|
const AirportMovingData *amd;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-02-23 11:50:43 +00:00
|
|
|
/* prevent going to 0,0 if airport is deleted. */
|
2007-02-20 06:39:09 +00:00
|
|
|
TileIndex tile = st->airport_tile;
|
2007-03-29 13:52:34 +00:00
|
|
|
if (tile == 0) {
|
|
|
|
tile = st->xy;
|
|
|
|
|
|
|
|
/* Jump into our "holding pattern" state machine if possible */
|
|
|
|
if (v->u.air.pos >= afc->nofelements) v->u.air.pos = v->u.air.previous_pos = AircraftGetEntryPoint(v, afc);
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-02-23 11:50:43 +00:00
|
|
|
/* get airport moving data */
|
2007-03-29 13:52:34 +00:00
|
|
|
amd = afc->MovingData(v->u.air.pos);
|
|
|
|
|
|
|
|
int x = TileX(tile) * TILE_SIZE;
|
|
|
|
int y = TileY(tile) * TILE_SIZE;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-02-23 11:50:43 +00:00
|
|
|
/* Helicopter raise */
|
2004-08-09 17:04:08 +00:00
|
|
|
if (amd->flag & AMED_HELI_RAISE) {
|
2007-08-30 13:03:56 +00:00
|
|
|
Vehicle *u = v->Next()->Next();
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-02-23 11:50:43 +00:00
|
|
|
/* Make sure the rotors don't rotate too fast */
|
2004-08-09 17:04:08 +00:00
|
|
|
if (u->cur_speed > 32) {
|
|
|
|
v->cur_speed = 0;
|
2005-11-14 19:48:04 +00:00
|
|
|
if (--u->cur_speed == 32) SndPlayVehicleFx(SND_18_HELICOPTER, v);
|
2004-08-09 17:04:08 +00:00
|
|
|
} else {
|
|
|
|
u->cur_speed = 32;
|
2007-03-02 12:01:24 +00:00
|
|
|
count = UpdateAircraftSpeed(v);
|
|
|
|
if (count > 0) {
|
2004-08-09 17:04:08 +00:00
|
|
|
v->tile = 0;
|
|
|
|
|
2007-02-23 11:50:43 +00:00
|
|
|
/* Reached altitude? */
|
2004-08-09 17:04:08 +00:00
|
|
|
if (v->z_pos >= 184) {
|
|
|
|
v->cur_speed = 0;
|
|
|
|
return true;
|
|
|
|
}
|
2007-03-02 12:01:24 +00:00
|
|
|
SetAircraftPosition(v, v->x_pos, v->y_pos, min(v->z_pos + count, 184));
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-02-23 11:50:43 +00:00
|
|
|
/* Helicopter landing. */
|
2004-08-09 17:04:08 +00:00
|
|
|
if (amd->flag & AMED_HELI_LOWER) {
|
2007-03-02 12:01:24 +00:00
|
|
|
count = UpdateAircraftSpeed(v);
|
|
|
|
if (count > 0) {
|
2004-08-09 17:04:08 +00:00
|
|
|
if (st->airport_tile == 0) {
|
2007-02-23 11:50:43 +00:00
|
|
|
/* FIXME - AircraftController -> if station no longer exists, do not land
|
|
|
|
* helicopter will circle until sign disappears, then go to next order
|
|
|
|
* what to do when it is the only order left, right now it just stays in 1 place */
|
2004-08-09 17:04:08 +00:00
|
|
|
v->u.air.state = FLYING;
|
2007-05-06 21:10:49 +00:00
|
|
|
UpdateAircraftCache(v);
|
2004-08-09 17:04:08 +00:00
|
|
|
AircraftNextAirportPos_and_Order(v);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-02-23 11:50:43 +00:00
|
|
|
/* Vehicle is now at the airport. */
|
2004-08-09 17:04:08 +00:00
|
|
|
v->tile = st->airport_tile;
|
|
|
|
|
2007-02-23 11:50:43 +00:00
|
|
|
/* Find altitude of landing position. */
|
2007-03-02 12:01:24 +00:00
|
|
|
int z = GetSlopeZ(x, y) + 1 + afc->delta_z;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
if (z == v->z_pos) {
|
2007-08-30 13:03:56 +00:00
|
|
|
Vehicle *u = v->Next()->Next();
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-02-23 11:50:43 +00:00
|
|
|
/* Increase speed of rotors. When speed is 80, we've landed. */
|
2005-11-14 19:48:04 +00:00
|
|
|
if (u->cur_speed >= 80) return true;
|
|
|
|
u->cur_speed += 4;
|
2004-08-09 17:04:08 +00:00
|
|
|
} else if (v->z_pos > z) {
|
2007-03-02 12:01:24 +00:00
|
|
|
SetAircraftPosition(v, v->x_pos, v->y_pos, max(v->z_pos - count, z));
|
2004-08-09 17:04:08 +00:00
|
|
|
} else {
|
2007-03-02 12:01:24 +00:00
|
|
|
SetAircraftPosition(v, v->x_pos, v->y_pos, min(v->z_pos + count, z));
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-02-23 11:50:43 +00:00
|
|
|
/* Get distance from destination pos to current pos. */
|
2007-02-20 06:39:09 +00:00
|
|
|
uint dist = myabs(x + amd->x - v->x_pos) + myabs(y + amd->y - v->y_pos);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-02-23 11:50:43 +00:00
|
|
|
/* Need exact position? */
|
2007-03-02 12:01:24 +00:00
|
|
|
if (!(amd->flag & AMED_EXACTPOS) && dist <= (amd->flag & AMED_SLOWTURN ? 8U : 4U)) return true;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-02-23 11:50:43 +00:00
|
|
|
/* At final pos? */
|
2004-08-09 17:04:08 +00:00
|
|
|
if (dist == 0) {
|
2007-02-23 11:50:43 +00:00
|
|
|
/* Change direction smoothly to final direction. */
|
2007-02-20 06:39:09 +00:00
|
|
|
DirDiff dirdiff = DirDifference(amd->direction, v->direction);
|
2007-02-23 11:50:43 +00:00
|
|
|
/* if distance is 0, and plane points in right direction, no point in calling
|
|
|
|
* UpdateAircraftSpeed(). So do it only afterwards */
|
2006-03-08 07:48:56 +00:00
|
|
|
if (dirdiff == DIRDIFF_SAME) {
|
2004-08-09 17:04:08 +00:00
|
|
|
v->cur_speed = 0;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-02-06 15:38:23 +00:00
|
|
|
if (!UpdateAircraftSpeed(v, SPEED_LIMIT_TAXI)) return false;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-03-08 07:48:56 +00:00
|
|
|
v->direction = ChangeDir(v->direction, dirdiff > DIRDIFF_REVERSE ? DIRDIFF_45LEFT : DIRDIFF_45RIGHT);
|
2004-08-09 17:04:08 +00:00
|
|
|
v->cur_speed >>= 1;
|
|
|
|
|
|
|
|
SetAircraftPosition(v, v->x_pos, v->y_pos, v->z_pos);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-03-02 12:01:24 +00:00
|
|
|
uint speed_limit = SPEED_LIMIT_TAXI;
|
|
|
|
bool hard_limit = true;
|
|
|
|
|
|
|
|
if (amd->flag & AMED_NOSPDCLAMP) speed_limit = SPEED_LIMIT_NONE;
|
|
|
|
if (amd->flag & AMED_HOLD) { speed_limit = SPEED_LIMIT_HOLD; hard_limit = false; }
|
|
|
|
if (amd->flag & AMED_LAND) { speed_limit = SPEED_LIMIT_APPROACH; hard_limit = false; }
|
|
|
|
if (amd->flag & AMED_BRAKE) { speed_limit = SPEED_LIMIT_TAXI; hard_limit = false; }
|
|
|
|
|
|
|
|
count = UpdateAircraftSpeed(v, speed_limit, hard_limit);
|
|
|
|
if (count == 0) return false;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-11-14 19:48:04 +00:00
|
|
|
if (v->load_unload_time_rem != 0) v->load_unload_time_rem--;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-03-02 12:01:24 +00:00
|
|
|
do {
|
2007-03-18 21:44:00 +00:00
|
|
|
|
|
|
|
GetNewVehiclePosResult gp;
|
|
|
|
|
|
|
|
if (dist < 4) {
|
|
|
|
/* move vehicle one pixel towards target */
|
|
|
|
gp.x = (v->x_pos != (x + amd->x)) ?
|
|
|
|
v->x_pos + ((x + amd->x > v->x_pos) ? 1 : -1) :
|
|
|
|
v->x_pos;
|
|
|
|
gp.y = (v->y_pos != (y + amd->y)) ?
|
|
|
|
v->y_pos + ((y + amd->y > v->y_pos) ? 1 : -1) :
|
|
|
|
v->y_pos;
|
|
|
|
|
|
|
|
/* Oilrigs must keep v->tile as st->airport_tile, since the landing pad is in a non-airport tile */
|
|
|
|
gp.new_tile = (st->airport_type == AT_OILRIG) ? st->airport_tile : TileVirtXY(gp.x, gp.y);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
/* Turn. Do it slowly if in the air. */
|
|
|
|
Direction newdir = GetDirectionTowards(v, x + amd->x, y + amd->y);
|
|
|
|
if (newdir != v->direction) {
|
|
|
|
v->direction = newdir;
|
|
|
|
if (amd->flag & AMED_SLOWTURN) {
|
|
|
|
if (v->load_unload_time_rem == 0) v->load_unload_time_rem = 8;
|
|
|
|
} else {
|
|
|
|
v->cur_speed >>= 1;
|
|
|
|
}
|
2007-03-02 12:01:24 +00:00
|
|
|
}
|
2007-03-18 21:44:00 +00:00
|
|
|
|
|
|
|
/* Move vehicle. */
|
|
|
|
gp = GetNewVehiclePos(v);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-03-02 12:01:24 +00:00
|
|
|
v->tile = gp.new_tile;
|
|
|
|
/* If vehicle is in the air, use tile coordinate 0. */
|
|
|
|
// if (amd->flag & (AMED_TAKEOFF | AMED_SLOWTURN | AMED_LAND)) v->tile = 0;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-03-02 12:01:24 +00:00
|
|
|
/* Adjust Z for land or takeoff? */
|
|
|
|
uint z = v->z_pos;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-03-02 12:01:24 +00:00
|
|
|
if (amd->flag & AMED_TAKEOFF) {
|
|
|
|
z = min(z + 2, GetAircraftFlyingAltitude(v));
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-03-02 12:01:24 +00:00
|
|
|
if ((amd->flag & AMED_HOLD) && (z > 150)) z--;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-03-02 12:01:24 +00:00
|
|
|
if (amd->flag & AMED_LAND) {
|
|
|
|
if (st->airport_tile == 0) {
|
|
|
|
/* Airport has been removed, abort the landing procedure */
|
|
|
|
v->u.air.state = FLYING;
|
2007-05-06 21:10:49 +00:00
|
|
|
UpdateAircraftCache(v);
|
2007-03-02 12:01:24 +00:00
|
|
|
AircraftNextAirportPos_and_Order(v);
|
|
|
|
/* get aircraft back on running altitude */
|
|
|
|
SetAircraftPosition(v, gp.x, gp.y, GetAircraftFlyingAltitude(v));
|
|
|
|
continue;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-03-02 12:01:24 +00:00
|
|
|
uint curz = GetSlopeZ(x, y) + 1;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-03-02 12:01:24 +00:00
|
|
|
if (curz > z) {
|
|
|
|
z++;
|
|
|
|
} else {
|
|
|
|
int t = max(1U, dist - 4);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-03-02 12:01:24 +00:00
|
|
|
z -= ((z - curz) + t - 1) / t;
|
|
|
|
if (z < curz) z = curz;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-03-02 12:01:24 +00:00
|
|
|
/* We've landed. Decrase speed when we're reaching end of runway. */
|
|
|
|
if (amd->flag & AMED_BRAKE) {
|
|
|
|
uint curz = GetSlopeZ(x, y) + 1;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-03-02 12:01:24 +00:00
|
|
|
if (z > curz) {
|
|
|
|
z--;
|
|
|
|
} else if (z < curz) {
|
|
|
|
z++;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-03-02 12:01:24 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-03-02 12:01:24 +00:00
|
|
|
SetAircraftPosition(v, gp.x, gp.y, z);
|
|
|
|
} while (--count != 0);
|
2004-08-09 17:04:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void HandleCrashedAircraft(Vehicle *v)
|
|
|
|
{
|
|
|
|
v->u.air.crashed_counter++;
|
|
|
|
|
2007-02-20 06:39:09 +00:00
|
|
|
Station *st = GetStation(v->u.air.targetairport);
|
2005-01-05 13:15:27 +00:00
|
|
|
|
2007-02-23 11:50:43 +00:00
|
|
|
/* make aircraft crash down to the ground */
|
2005-01-24 22:09:20 +00:00
|
|
|
if (v->u.air.crashed_counter < 500 && st->airport_tile==0 && ((v->u.air.crashed_counter % 3) == 0) ) {
|
2007-02-20 06:39:09 +00:00
|
|
|
uint z = GetSlopeZ(v->x_pos, v->y_pos);
|
2005-01-05 13:15:27 +00:00
|
|
|
v->z_pos -= 1;
|
2005-01-24 22:09:20 +00:00
|
|
|
if (v->z_pos == z) {
|
|
|
|
v->u.air.crashed_counter = 500;
|
|
|
|
v->z_pos++;
|
|
|
|
}
|
2005-01-05 13:15:27 +00:00
|
|
|
}
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
if (v->u.air.crashed_counter < 650) {
|
2007-02-20 06:39:09 +00:00
|
|
|
uint32 r;
|
2004-08-09 17:04:08 +00:00
|
|
|
if (CHANCE16R(1,32,r)) {
|
2006-03-08 08:16:31 +00:00
|
|
|
static const DirDiff delta[] = {
|
|
|
|
DIRDIFF_45LEFT, DIRDIFF_SAME, DIRDIFF_SAME, DIRDIFF_45RIGHT
|
|
|
|
};
|
|
|
|
|
|
|
|
v->direction = ChangeDir(v->direction, delta[GB(r, 16, 2)]);
|
2004-08-09 17:04:08 +00:00
|
|
|
SetAircraftPosition(v, v->x_pos, v->y_pos, v->z_pos);
|
|
|
|
r = Random();
|
|
|
|
CreateEffectVehicleRel(v,
|
2005-07-20 15:29:28 +00:00
|
|
|
GB(r, 0, 4) + 4,
|
|
|
|
GB(r, 4, 4) + 4,
|
|
|
|
GB(r, 8, 4),
|
2005-02-12 15:53:32 +00:00
|
|
|
EV_EXPLOSION_SMALL);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
} else if (v->u.air.crashed_counter >= 10000) {
|
2007-02-23 11:50:43 +00:00
|
|
|
/* remove rubble of crashed airplane */
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-02-23 11:50:43 +00:00
|
|
|
/* clear runway-in on all airports, set by crashing plane
|
|
|
|
* small airports use AIRPORT_BUSY, city airports use RUNWAY_IN_OUT_block, etc.
|
|
|
|
* but they all share the same number */
|
2004-08-09 17:04:08 +00:00
|
|
|
CLRBITS(st->airport_flags, RUNWAY_IN_block);
|
2006-06-23 22:05:40 +00:00
|
|
|
CLRBITS(st->airport_flags, RUNWAY_IN_OUT_block); // commuter airport
|
2007-02-23 11:50:43 +00:00
|
|
|
CLRBITS(st->airport_flags, RUNWAY_IN2_block); // intercontinental
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
BeginVehicleMove(v);
|
|
|
|
EndVehicleMove(v);
|
|
|
|
|
|
|
|
DoDeleteAircraft(v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void HandleBrokenAircraft(Vehicle *v)
|
|
|
|
{
|
|
|
|
if (v->breakdown_ctr != 1) {
|
|
|
|
v->breakdown_ctr = 1;
|
|
|
|
v->vehstatus |= VS_AIRCRAFT_BROKEN;
|
|
|
|
|
|
|
|
if (v->breakdowns_since_last_service != 255)
|
|
|
|
v->breakdowns_since_last_service++;
|
|
|
|
InvalidateWindow(WC_VEHICLE_VIEW, v->index);
|
|
|
|
InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void HandleAircraftSmoke(Vehicle *v)
|
|
|
|
{
|
2005-11-16 12:01:46 +00:00
|
|
|
static const struct {
|
|
|
|
int8 x;
|
|
|
|
int8 y;
|
|
|
|
} smoke_pos[] = {
|
|
|
|
{ 5, 5 },
|
|
|
|
{ 6, 0 },
|
|
|
|
{ 5, -5 },
|
|
|
|
{ 0, -6 },
|
|
|
|
{ -5, -5 },
|
|
|
|
{ -6, 0 },
|
|
|
|
{ -5, 5 },
|
|
|
|
{ 0, 6 }
|
|
|
|
};
|
|
|
|
|
2005-11-14 19:48:04 +00:00
|
|
|
if (!(v->vehstatus & VS_AIRCRAFT_BROKEN)) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
if (v->cur_speed < 10) {
|
|
|
|
v->vehstatus &= ~VS_AIRCRAFT_BROKEN;
|
|
|
|
v->breakdown_ctr = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((v->tick_counter & 0x1F) == 0) {
|
|
|
|
CreateEffectVehicleRel(v,
|
2005-11-16 12:01:46 +00:00
|
|
|
smoke_pos[v->direction].x,
|
|
|
|
smoke_pos[v->direction].y,
|
2004-08-09 17:04:08 +00:00
|
|
|
2,
|
2005-02-12 15:53:32 +00:00
|
|
|
EV_SMOKE
|
2004-08-09 17:04:08 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ProcessAircraftOrder(Vehicle *v)
|
|
|
|
{
|
2006-02-10 06:33:56 +00:00
|
|
|
switch (v->current_order.type) {
|
|
|
|
case OT_GOTO_DEPOT:
|
|
|
|
if (!(v->current_order.flags & OF_PART_OF_ORDERS)) return;
|
|
|
|
if (v->current_order.flags & OF_SERVICE_IF_NEEDED &&
|
|
|
|
!VehicleNeedsService(v)) {
|
2007-06-20 19:17:22 +00:00
|
|
|
UpdateVehicleTimetable(v, true);
|
2007-06-25 23:14:13 +00:00
|
|
|
v->cur_order_index++;
|
2006-02-10 06:33:56 +00:00
|
|
|
}
|
|
|
|
break;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2006-02-10 06:33:56 +00:00
|
|
|
case OT_LOADING: return;
|
2006-08-22 17:13:49 +00:00
|
|
|
|
|
|
|
default: break;
|
2006-02-10 06:33:56 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-11-14 19:48:04 +00:00
|
|
|
if (v->cur_order_index >= v->num_orders) v->cur_order_index = 0;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-02-20 06:39:09 +00:00
|
|
|
const Order *order = GetVehicleOrder(v, v->cur_order_index);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-02-27 16:18:31 +00:00
|
|
|
if (order == NULL|| (order->type == OT_DUMMY && !CheckForValidOrders(v))) {
|
|
|
|
/*
|
|
|
|
* We do not have an order. This can be divided into two cases:
|
|
|
|
* 1) we are heading to an invalid station. In this case we must
|
|
|
|
* find another airport to go to. If there is nowhere to go,
|
|
|
|
* we will destroy the aircraft as it otherwise will enter
|
|
|
|
* the holding pattern for the first airport, which can cause
|
|
|
|
* the plane to go into an undefined state when building an
|
|
|
|
* airport with the same StationID.
|
|
|
|
* 2) we are (still) heading to a (still) valid airport, then we
|
|
|
|
* can continue going there. This can happen when you are
|
|
|
|
* changing the aircraft's orders while in-flight or in for
|
|
|
|
* example a depot. However, when we have a current order to
|
|
|
|
* go to a depot, we have to keep that order so the aircraft
|
|
|
|
* actually stops.
|
|
|
|
*/
|
|
|
|
const Station *st = GetStation(v->u.air.targetairport);
|
|
|
|
if (!st->IsValid() || st->airport_tile == 0) {
|
2007-06-18 10:48:15 +00:00
|
|
|
CommandCost ret;
|
2007-02-27 16:18:31 +00:00
|
|
|
PlayerID old_player = _current_player;
|
|
|
|
|
|
|
|
_current_player = v->owner;
|
|
|
|
ret = DoCommand(v->tile, v->index, 0, DC_EXEC, CMD_SEND_AIRCRAFT_TO_HANGAR);
|
|
|
|
_current_player = old_player;
|
|
|
|
|
|
|
|
if (CmdFailed(ret)) CrashAirplane(v);
|
|
|
|
} else if (v->current_order.type != OT_GOTO_DEPOT) {
|
2007-03-08 21:39:34 +00:00
|
|
|
v->current_order.Free();
|
2007-02-27 16:18:31 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2006-09-03 08:25:27 +00:00
|
|
|
if (order->type == v->current_order.type &&
|
|
|
|
order->flags == v->current_order.flags &&
|
|
|
|
order->dest == v->current_order.dest)
|
2004-08-09 17:04:08 +00:00
|
|
|
return;
|
|
|
|
|
2005-01-15 19:06:22 +00:00
|
|
|
v->current_order = *order;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-02-23 11:50:43 +00:00
|
|
|
/* orders are changed in flight, ensure going to the right station */
|
2005-01-15 19:06:22 +00:00
|
|
|
if (order->type == OT_GOTO_STATION && v->u.air.state == FLYING) {
|
2004-08-09 17:04:08 +00:00
|
|
|
AircraftNextAirportPos_and_Order(v);
|
|
|
|
}
|
|
|
|
|
2005-01-15 19:06:22 +00:00
|
|
|
InvalidateVehicleOrder(v);
|
|
|
|
|
2005-01-18 09:35:31 +00:00
|
|
|
InvalidateWindowClasses(WC_AIRCRAFT_LIST);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-04-29 22:33:51 +00:00
|
|
|
void Aircraft::MarkDirty()
|
2006-05-02 19:24:02 +00:00
|
|
|
{
|
2007-07-01 19:11:47 +00:00
|
|
|
this->cur_image = this->GetImage(this->direction);
|
2007-08-30 13:03:56 +00:00
|
|
|
if (this->subtype == AIR_HELICOPTER) this->Next()->Next()->cur_image = GetRotorImage(this);
|
2007-04-29 22:33:51 +00:00
|
|
|
MarkAllViewportsDirty(this->left_coord, this->top_coord, this->right_coord + 1, this->bottom_coord + 1);
|
2006-05-02 19:24:02 +00:00
|
|
|
}
|
|
|
|
|
2005-01-05 13:15:27 +00:00
|
|
|
static void CrashAirplane(Vehicle *v)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
v->vehstatus |= VS_CRASHED;
|
|
|
|
v->u.air.crashed_counter = 0;
|
|
|
|
|
2005-02-12 15:53:32 +00:00
|
|
|
CreateEffectVehicleRel(v, 4, 4, 8, EV_EXPLOSION_LARGE);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
InvalidateWindow(WC_VEHICLE_VIEW, v->index);
|
|
|
|
|
2007-02-20 06:39:09 +00:00
|
|
|
uint amt = 2;
|
2007-06-22 11:58:59 +00:00
|
|
|
if (IsCargoInClass(v->cargo_type, CC_PASSENGERS)) amt += v->cargo.Count();
|
2004-12-02 22:53:07 +00:00
|
|
|
SetDParam(0, amt);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-06-22 11:58:59 +00:00
|
|
|
v->cargo.Truncate(0);
|
2007-08-30 13:03:56 +00:00
|
|
|
v->Next()->cargo.Truncate(0);
|
2007-02-20 06:39:09 +00:00
|
|
|
const Station *st = GetStation(v->u.air.targetairport);
|
|
|
|
StringID newsitem;
|
2005-11-14 19:48:04 +00:00
|
|
|
if (st->airport_tile == 0) {
|
2005-01-05 13:15:27 +00:00
|
|
|
newsitem = STR_PLANE_CRASH_OUT_OF_FUEL;
|
|
|
|
} else {
|
|
|
|
SetDParam(1, st->index);
|
|
|
|
newsitem = STR_A034_PLANE_CRASH_DIE_IN_FIREBALL;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2004-12-02 22:53:07 +00:00
|
|
|
SetDParam(1, st->index);
|
2005-01-05 13:15:27 +00:00
|
|
|
AddNewsItem(newsitem,
|
2004-08-09 17:04:08 +00:00
|
|
|
NEWS_FLAGS(NM_THIN, NF_VIEWPORT|NF_VEHICLE, NT_ACCIDENT, 0),
|
|
|
|
v->index,
|
|
|
|
0);
|
|
|
|
|
2004-12-04 09:26:39 +00:00
|
|
|
SndPlayVehicleFx(SND_12_EXPLOSION, v);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2005-01-05 13:15:27 +00:00
|
|
|
static void MaybeCrashAirplane(Vehicle *v)
|
|
|
|
{
|
2007-02-20 06:39:09 +00:00
|
|
|
Station *st = GetStation(v->u.air.targetairport);
|
2005-01-05 13:15:27 +00:00
|
|
|
|
2007-02-23 11:50:43 +00:00
|
|
|
/* FIXME -- MaybeCrashAirplane -> increase crashing chances of very modern airplanes on smaller than AT_METROPOLITAN airports */
|
2007-02-20 06:39:09 +00:00
|
|
|
uint16 prob = 0x10000 / 1500;
|
2007-02-16 09:38:43 +00:00
|
|
|
if (st->Airport()->flags & AirportFTAClass::SHORT_STRIP &&
|
2007-02-15 20:35:45 +00:00
|
|
|
AircraftVehInfo(v->engine_type)->subtype & AIR_FAST &&
|
|
|
|
!_cheats.no_jetcrash.value) {
|
2005-01-05 13:15:27 +00:00
|
|
|
prob = 0x10000 / 20;
|
|
|
|
}
|
|
|
|
|
2005-10-03 21:20:01 +00:00
|
|
|
if (GB(Random(), 0, 16) > prob) return;
|
2005-01-05 13:15:27 +00:00
|
|
|
|
2007-02-23 11:50:43 +00:00
|
|
|
/* Crash the airplane. Remove all goods stored at the station. */
|
2007-03-21 13:19:01 +00:00
|
|
|
for (CargoID i = 0; i < NUM_CARGO; i++) {
|
2005-01-05 13:15:27 +00:00
|
|
|
st->goods[i].rating = 1;
|
2007-06-22 11:58:59 +00:00
|
|
|
st->goods[i].cargo.Truncate(0);
|
2005-01-05 13:15:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CrashAirplane(v);
|
|
|
|
}
|
|
|
|
|
2007-02-23 11:50:43 +00:00
|
|
|
/** we've landed and just arrived at a terminal */
|
2004-08-09 17:04:08 +00:00
|
|
|
static void AircraftEntersTerminal(Vehicle *v)
|
|
|
|
{
|
2005-11-14 19:48:04 +00:00
|
|
|
if (v->current_order.type == OT_GOTO_DEPOT) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-02-20 06:39:09 +00:00
|
|
|
Station *st = GetStation(v->u.air.targetairport);
|
2004-08-09 17:04:08 +00:00
|
|
|
v->last_station_visited = v->u.air.targetairport;
|
|
|
|
|
|
|
|
/* Check if station was ever visited before */
|
|
|
|
if (!(st->had_vehicle_of_type & HVOT_AIRCRAFT)) {
|
|
|
|
uint32 flags;
|
|
|
|
|
|
|
|
st->had_vehicle_of_type |= HVOT_AIRCRAFT;
|
2004-12-02 22:53:07 +00:00
|
|
|
SetDParam(0, st->index);
|
2007-02-23 11:50:43 +00:00
|
|
|
/* show newsitem of celebrating citizens */
|
2004-08-09 17:04:08 +00:00
|
|
|
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);
|
|
|
|
AddNewsItem(
|
|
|
|
STR_A033_CITIZENS_CELEBRATE_FIRST,
|
|
|
|
flags,
|
|
|
|
v->index,
|
|
|
|
0);
|
|
|
|
}
|
|
|
|
|
2007-01-13 18:55:54 +00:00
|
|
|
v->BeginLoading();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void AircraftLandAirplane(Vehicle *v)
|
|
|
|
{
|
2007-05-01 16:35:14 +00:00
|
|
|
v->UpdateDeltaXY(INVALID_DIR);
|
|
|
|
|
2006-09-27 18:17:01 +00:00
|
|
|
if (!PlayVehicleSound(v, VSE_TOUCHDOWN)) {
|
|
|
|
SndPlayVehicleFx(SND_17_SKID_PLANE, v);
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
MaybeCrashAirplane(v);
|
|
|
|
}
|
|
|
|
|
2007-02-16 12:10:19 +00:00
|
|
|
|
2007-02-23 11:50:43 +00:00
|
|
|
/** set the right pos when heading to other airports after takeoff */
|
2004-08-09 17:04:08 +00:00
|
|
|
static void AircraftNextAirportPos_and_Order(Vehicle *v)
|
|
|
|
{
|
2004-12-05 12:43:04 +00:00
|
|
|
if (v->current_order.type == OT_GOTO_STATION ||
|
|
|
|
v->current_order.type == OT_GOTO_DEPOT)
|
2006-09-03 08:25:27 +00:00
|
|
|
v->u.air.targetairport = v->current_order.dest;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-02-16 09:38:43 +00:00
|
|
|
const AirportFTAClass *apc = GetStation(v->u.air.targetairport)->Airport();
|
2007-02-16 12:10:19 +00:00
|
|
|
v->u.air.pos = v->u.air.previous_pos = AircraftGetEntryPoint(v, apc);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void AircraftLeaveHangar(Vehicle *v)
|
|
|
|
{
|
|
|
|
v->cur_speed = 0;
|
|
|
|
v->subspeed = 0;
|
|
|
|
v->progress = 0;
|
2007-01-10 18:56:51 +00:00
|
|
|
v->direction = DIR_SE;
|
2004-08-09 17:04:08 +00:00
|
|
|
v->vehstatus &= ~VS_HIDDEN;
|
|
|
|
{
|
2007-08-30 13:03:56 +00:00
|
|
|
Vehicle *u = v->Next();
|
2004-08-09 17:04:08 +00:00
|
|
|
u->vehstatus &= ~VS_HIDDEN;
|
|
|
|
|
2007-02-23 11:50:43 +00:00
|
|
|
/* Rotor blades */
|
2007-08-30 13:03:56 +00:00
|
|
|
u = u->Next();
|
2005-11-14 19:48:04 +00:00
|
|
|
if (u != NULL) {
|
2004-08-09 17:04:08 +00:00
|
|
|
u->vehstatus &= ~VS_HIDDEN;
|
|
|
|
u->cur_speed = 80;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-12-09 21:46:56 +00:00
|
|
|
VehicleServiceInDepot(v);
|
2004-08-09 17:04:08 +00:00
|
|
|
SetAircraftPosition(v, v->x_pos, v->y_pos, v->z_pos);
|
2006-10-05 12:59:28 +00:00
|
|
|
InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
|
2005-01-18 09:35:31 +00:00
|
|
|
InvalidateWindowClasses(WC_AIRCRAFT_LIST);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
/////////////////// AIRCRAFT MOVEMENT SCHEME ////////////////////////////////
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2006-10-13 23:08:55 +00:00
|
|
|
static void AircraftEventHandler_EnterTerminal(Vehicle *v, const AirportFTAClass *apc)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
AircraftEntersTerminal(v);
|
2006-10-13 23:08:55 +00:00
|
|
|
v->u.air.state = apc->layout[v->u.air.pos].heading;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2006-10-13 23:08:55 +00:00
|
|
|
static void AircraftEventHandler_EnterHangar(Vehicle *v, const AirportFTAClass *apc)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2006-10-04 12:01:59 +00:00
|
|
|
VehicleEnterDepot(v);
|
2006-10-13 23:08:55 +00:00
|
|
|
v->u.air.state = apc->layout[v->u.air.pos].heading;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-02-23 11:50:43 +00:00
|
|
|
/** In an Airport Hangar */
|
2006-10-13 23:08:55 +00:00
|
|
|
static void AircraftEventHandler_InHangar(Vehicle *v, const AirportFTAClass *apc)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2007-02-23 11:50:43 +00:00
|
|
|
/* if we just arrived, execute EnterHangar first */
|
2004-08-09 17:04:08 +00:00
|
|
|
if (v->u.air.previous_pos != v->u.air.pos) {
|
2006-10-13 23:08:55 +00:00
|
|
|
AircraftEventHandler_EnterHangar(v, apc);
|
2004-08-09 17:04:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-02-23 11:50:43 +00:00
|
|
|
/* if we were sent to the depot, stay there */
|
2004-12-05 12:43:04 +00:00
|
|
|
if (v->current_order.type == OT_GOTO_DEPOT && (v->vehstatus & VS_STOPPED)) {
|
2007-03-08 21:39:34 +00:00
|
|
|
v->current_order.Free();
|
2004-08-09 17:04:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2004-12-05 12:43:04 +00:00
|
|
|
if (v->current_order.type != OT_GOTO_STATION &&
|
|
|
|
v->current_order.type != OT_GOTO_DEPOT)
|
2004-08-09 17:04:08 +00:00
|
|
|
return;
|
|
|
|
|
2007-02-23 11:50:43 +00:00
|
|
|
/* if the block of the next position is busy, stay put */
|
2006-10-13 23:08:55 +00:00
|
|
|
if (AirportHasBlock(v, &apc->layout[v->u.air.pos], apc)) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-02-23 11:50:43 +00:00
|
|
|
/* We are already at the target airport, we need to find a terminal */
|
2006-09-03 08:25:27 +00:00
|
|
|
if (v->current_order.dest == v->u.air.targetairport) {
|
2007-02-23 11:50:43 +00:00
|
|
|
/* FindFreeTerminal:
|
|
|
|
* 1. Find a free terminal, 2. Occupy it, 3. Set the vehicle's state to that terminal */
|
2007-01-27 12:29:55 +00:00
|
|
|
if (v->subtype == AIR_HELICOPTER) {
|
2006-10-13 23:08:55 +00:00
|
|
|
if (!AirportFindFreeHelipad(v, apc)) return; // helicopter
|
2007-01-27 12:29:55 +00:00
|
|
|
} else {
|
|
|
|
if (!AirportFindFreeTerminal(v, apc)) return; // airplane
|
2005-10-23 13:04:44 +00:00
|
|
|
}
|
|
|
|
} else { // Else prepare for launch.
|
2007-02-23 11:50:43 +00:00
|
|
|
/* airplane goto state takeoff, helicopter to helitakeoff */
|
2007-01-27 12:29:55 +00:00
|
|
|
v->u.air.state = (v->subtype == AIR_HELICOPTER) ? HELITAKEOFF : TAKEOFF;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
AircraftLeaveHangar(v);
|
2006-10-13 23:08:55 +00:00
|
|
|
AirportMove(v, apc);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-02-23 11:50:43 +00:00
|
|
|
/** At one of the Airport's Terminals */
|
2006-10-13 23:08:55 +00:00
|
|
|
static void AircraftEventHandler_AtTerminal(Vehicle *v, const AirportFTAClass *apc)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2007-02-23 11:50:43 +00:00
|
|
|
/* if we just arrived, execute EnterTerminal first */
|
2004-08-09 17:04:08 +00:00
|
|
|
if (v->u.air.previous_pos != v->u.air.pos) {
|
2006-10-13 23:08:55 +00:00
|
|
|
AircraftEventHandler_EnterTerminal(v, apc);
|
2007-02-23 11:50:43 +00:00
|
|
|
/* on an airport with helipads, a helicopter will always land there
|
|
|
|
* and get serviced at the same time - patch setting */
|
2004-08-09 17:04:08 +00:00
|
|
|
if (_patches.serviceathelipad) {
|
2007-01-27 12:29:55 +00:00
|
|
|
if (v->subtype == AIR_HELICOPTER && apc->helipads != NULL) {
|
2007-02-23 11:50:43 +00:00
|
|
|
/* an exerpt of ServiceAircraft, without the invisibility stuff */
|
2004-08-09 17:04:08 +00:00
|
|
|
v->date_of_last_service = _date;
|
|
|
|
v->breakdowns_since_last_service = 0;
|
2005-06-07 18:13:49 +00:00
|
|
|
v->reliability = GetEngine(v->engine_type)->reliability;
|
2004-08-09 17:04:08 +00:00
|
|
|
InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-03-08 21:39:34 +00:00
|
|
|
if (!v->current_order.IsValid()) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-02-23 11:50:43 +00:00
|
|
|
/* if the block of the next position is busy, stay put */
|
2006-10-14 09:51:04 +00:00
|
|
|
if (AirportHasBlock(v, &apc->layout[v->u.air.pos], apc)) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-02-23 11:50:43 +00:00
|
|
|
/* airport-road is free. We either have to go to another airport, or to the hangar
|
|
|
|
* ---> start moving */
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2004-12-05 12:43:04 +00:00
|
|
|
switch (v->current_order.type) {
|
2004-08-09 17:04:08 +00:00
|
|
|
case OT_GOTO_STATION: // ready to fly to another airport
|
2007-02-23 11:50:43 +00:00
|
|
|
/* airplane goto state takeoff, helicopter to helitakeoff */
|
2007-01-27 12:29:55 +00:00
|
|
|
v->u.air.state = (v->subtype == AIR_HELICOPTER) ? HELITAKEOFF : TAKEOFF;
|
2004-08-09 17:04:08 +00:00
|
|
|
break;
|
|
|
|
case OT_GOTO_DEPOT: // visit hangar for serivicing, sale, etc.
|
2006-09-03 08:25:27 +00:00
|
|
|
if (v->current_order.dest == v->u.air.targetairport) {
|
2004-08-09 17:04:08 +00:00
|
|
|
v->u.air.state = HANGAR;
|
2005-11-14 19:48:04 +00:00
|
|
|
} else {
|
2007-01-27 12:29:55 +00:00
|
|
|
v->u.air.state = (v->subtype == AIR_HELICOPTER) ? HELITAKEOFF : TAKEOFF;
|
2005-11-14 19:48:04 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
break;
|
|
|
|
default: // orders have been deleted (no orders), goto depot and don't bother us
|
2007-03-08 21:39:34 +00:00
|
|
|
v->current_order.Free();
|
2004-08-09 17:04:08 +00:00
|
|
|
v->u.air.state = HANGAR;
|
|
|
|
}
|
2006-10-13 23:08:55 +00:00
|
|
|
AirportMove(v, apc);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2006-10-13 23:08:55 +00:00
|
|
|
static void AircraftEventHandler_General(Vehicle *v, const AirportFTAClass *apc)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2006-12-26 17:36:18 +00:00
|
|
|
assert("OK, you shouldn't be here, check your Airport Scheme!" && 0);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-02-20 06:39:09 +00:00
|
|
|
static void AircraftEventHandler_TakeOff(Vehicle *v, const AirportFTAClass *apc)
|
|
|
|
{
|
2004-08-09 17:04:08 +00:00
|
|
|
PlayAircraftSound(v); // play takeoffsound for airplanes
|
|
|
|
v->u.air.state = STARTTAKEOFF;
|
|
|
|
}
|
|
|
|
|
2006-10-13 23:08:55 +00:00
|
|
|
static void AircraftEventHandler_StartTakeOff(Vehicle *v, const AirportFTAClass *apc)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
v->u.air.state = ENDTAKEOFF;
|
2007-05-01 16:35:14 +00:00
|
|
|
v->UpdateDeltaXY(INVALID_DIR);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2006-10-13 23:08:55 +00:00
|
|
|
static void AircraftEventHandler_EndTakeOff(Vehicle *v, const AirportFTAClass *apc)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
v->u.air.state = FLYING;
|
2007-02-23 11:50:43 +00:00
|
|
|
/* get the next position to go to, differs per airport */
|
2004-08-09 17:04:08 +00:00
|
|
|
AircraftNextAirportPos_and_Order(v);
|
|
|
|
}
|
|
|
|
|
2006-10-13 23:08:55 +00:00
|
|
|
static void AircraftEventHandler_HeliTakeOff(Vehicle *v, const AirportFTAClass *apc)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2005-09-30 20:37:25 +00:00
|
|
|
const Player* p = GetPlayer(v->owner);
|
2004-08-09 17:04:08 +00:00
|
|
|
v->u.air.state = FLYING;
|
2007-05-01 16:35:14 +00:00
|
|
|
v->UpdateDeltaXY(INVALID_DIR);
|
|
|
|
|
2007-02-23 11:50:43 +00:00
|
|
|
/* get the next position to go to, differs per airport */
|
2004-08-09 17:04:08 +00:00
|
|
|
AircraftNextAirportPos_and_Order(v);
|
2005-01-24 10:50:57 +00:00
|
|
|
|
2007-02-23 11:50:43 +00:00
|
|
|
/* check if the aircraft needs to be replaced or renewed and send it to a hangar if needed
|
|
|
|
* unless it is due for renewal but the engine is no longer available */
|
2005-11-14 19:48:04 +00:00
|
|
|
if (v->owner == _local_player && (
|
2007-05-19 09:40:18 +00:00
|
|
|
EngineHasReplacementForPlayer(p, v->engine_type, v->group_id) ||
|
2006-07-03 21:14:21 +00:00
|
|
|
((p->engine_renew && v->age - v->max_age > p->engine_renew_months * 30) &&
|
|
|
|
HASBIT(GetEngine(v->engine_type)->player_avail, _local_player))
|
2005-11-14 19:48:04 +00:00
|
|
|
)) {
|
2005-03-19 21:16:22 +00:00
|
|
|
_current_player = _local_player;
|
2006-09-01 10:24:15 +00:00
|
|
|
DoCommandP(v->tile, v->index, DEPOT_SERVICE | DEPOT_LOCATE_HANGAR, NULL, CMD_SEND_AIRCRAFT_TO_HANGAR | CMD_SHOW_NO_ERROR);
|
2005-03-19 21:16:22 +00:00
|
|
|
_current_player = OWNER_NONE;
|
2005-01-24 10:50:57 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2006-10-13 23:08:55 +00:00
|
|
|
static void AircraftEventHandler_Flying(Vehicle *v, const AirportFTAClass *apc)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2007-02-20 06:39:09 +00:00
|
|
|
Station *st = GetStation(v->u.air.targetairport);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-02-23 11:50:43 +00:00
|
|
|
/* runway busy or not allowed to use this airstation, circle */
|
2007-02-15 22:00:04 +00:00
|
|
|
if (apc->flags & (v->subtype == AIR_HELICOPTER ? AirportFTAClass::HELICOPTERS : AirportFTAClass::AIRPLANES) &&
|
2005-11-14 19:48:04 +00:00
|
|
|
st->airport_tile != 0 &&
|
|
|
|
(st->owner == OWNER_NONE || st->owner == v->owner)) {
|
2004-08-09 17:04:08 +00:00
|
|
|
// {32,FLYING,NOTHING_block,37}, {32,LANDING,N,33}, {32,HELILANDING,N,41},
|
|
|
|
// if it is an airplane, look for LANDING, for helicopter HELILANDING
|
|
|
|
// it is possible to choose from multiple landing runways, so loop until a free one is found
|
2007-02-20 06:39:09 +00:00
|
|
|
byte landingtype = (v->subtype == AIR_HELICOPTER) ? HELILANDING : LANDING;
|
|
|
|
const AirportFTA *current = apc->layout[v->u.air.pos].next;
|
2004-08-09 17:04:08 +00:00
|
|
|
while (current != NULL) {
|
|
|
|
if (current->heading == landingtype) {
|
2007-02-23 11:50:43 +00:00
|
|
|
/* save speed before, since if AirportHasBlock is false, it resets them to 0
|
|
|
|
* we don't want that for plane in air
|
|
|
|
* hack for speed thingie */
|
2007-02-20 06:39:09 +00:00
|
|
|
uint16 tcur_speed = v->cur_speed;
|
|
|
|
uint16 tsubspeed = v->subspeed;
|
2006-10-13 23:08:55 +00:00
|
|
|
if (!AirportHasBlock(v, current, apc)) {
|
2004-08-09 17:04:08 +00:00
|
|
|
v->u.air.state = landingtype; // LANDING / HELILANDING
|
2007-02-23 11:50:43 +00:00
|
|
|
/* it's a bit dirty, but I need to set position to next position, otherwise
|
|
|
|
* if there are multiple runways, plane won't know which one it took (because
|
|
|
|
* they all have heading LANDING). And also occupy that block! */
|
2004-08-09 17:04:08 +00:00
|
|
|
v->u.air.pos = current->next_position;
|
2006-10-13 23:08:55 +00:00
|
|
|
SETBITS(st->airport_flags, apc->layout[v->u.air.pos].block);
|
2004-08-09 17:04:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
v->cur_speed = tcur_speed;
|
|
|
|
v->subspeed = tsubspeed;
|
|
|
|
}
|
2006-10-13 23:08:55 +00:00
|
|
|
current = current->next;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
v->u.air.state = FLYING;
|
2006-10-13 23:08:55 +00:00
|
|
|
v->u.air.pos = apc->layout[v->u.air.pos].next_position;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2006-10-13 23:08:55 +00:00
|
|
|
static void AircraftEventHandler_Landing(Vehicle *v, const AirportFTAClass *apc)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
v->u.air.state = ENDLANDING;
|
2007-05-01 16:35:14 +00:00
|
|
|
AircraftLandAirplane(v); // maybe crash airplane
|
|
|
|
|
2007-02-23 11:50:43 +00:00
|
|
|
/* check if the aircraft needs to be replaced or renewed and send it to a hangar if needed */
|
2005-01-27 20:38:19 +00:00
|
|
|
if (v->current_order.type != OT_GOTO_DEPOT && v->owner == _local_player) {
|
2007-02-23 11:50:43 +00:00
|
|
|
/* only the vehicle owner needs to calculate the rest (locally) */
|
2007-02-20 06:39:09 +00:00
|
|
|
const Player* p = GetPlayer(v->owner);
|
2007-05-19 09:40:18 +00:00
|
|
|
if (EngineHasReplacementForPlayer(p, v->engine_type, v->group_id) ||
|
2005-08-06 16:07:22 +00:00
|
|
|
(p->engine_renew && v->age - v->max_age > (p->engine_renew_months * 30))) {
|
2007-02-23 11:50:43 +00:00
|
|
|
/* send the aircraft to the hangar at next airport */
|
2005-01-27 20:38:19 +00:00
|
|
|
_current_player = _local_player;
|
2006-09-01 10:24:15 +00:00
|
|
|
DoCommandP(v->tile, v->index, DEPOT_SERVICE, NULL, CMD_SEND_AIRCRAFT_TO_HANGAR | CMD_SHOW_NO_ERROR);
|
2005-01-27 20:38:19 +00:00
|
|
|
_current_player = OWNER_NONE;
|
|
|
|
}
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2006-10-13 23:08:55 +00:00
|
|
|
static void AircraftEventHandler_HeliLanding(Vehicle *v, const AirportFTAClass *apc)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
v->u.air.state = HELIENDLANDING;
|
2007-05-01 16:35:14 +00:00
|
|
|
v->UpdateDeltaXY(INVALID_DIR);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2006-10-13 23:08:55 +00:00
|
|
|
static void AircraftEventHandler_EndLanding(Vehicle *v, const AirportFTAClass *apc)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2007-02-23 11:50:43 +00:00
|
|
|
/* next block busy, don't do a thing, just wait */
|
2006-10-13 23:08:55 +00:00
|
|
|
if (AirportHasBlock(v, &apc->layout[v->u.air.pos], apc)) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-02-23 11:50:43 +00:00
|
|
|
/* if going to terminal (OT_GOTO_STATION) choose one
|
|
|
|
* 1. in case all terminals are busy AirportFindFreeTerminal() returns false or
|
|
|
|
* 2. not going for terminal (but depot, no order),
|
|
|
|
* --> get out of the way to the hangar. */
|
2004-12-05 12:43:04 +00:00
|
|
|
if (v->current_order.type == OT_GOTO_STATION) {
|
2006-10-13 23:08:55 +00:00
|
|
|
if (AirportFindFreeTerminal(v, apc)) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
v->u.air.state = HANGAR;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2006-10-13 23:08:55 +00:00
|
|
|
static void AircraftEventHandler_HeliEndLanding(Vehicle *v, const AirportFTAClass *apc)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2007-02-23 11:50:43 +00:00
|
|
|
/* next block busy, don't do a thing, just wait */
|
2006-10-13 23:08:55 +00:00
|
|
|
if (AirportHasBlock(v, &apc->layout[v->u.air.pos], apc)) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-02-23 11:50:43 +00:00
|
|
|
/* if going to helipad (OT_GOTO_STATION) choose one. If airport doesn't have helipads, choose terminal
|
|
|
|
* 1. in case all terminals/helipads are busy (AirportFindFreeHelipad() returns false) or
|
|
|
|
* 2. not going for terminal (but depot, no order),
|
|
|
|
* --> get out of the way to the hangar IF there are terminals on the airport.
|
|
|
|
* --> else TAKEOFF
|
|
|
|
* the reason behind this is that if an airport has a terminal, it also has a hangar. Airplanes
|
|
|
|
* must go to a hangar. */
|
2004-12-05 12:43:04 +00:00
|
|
|
if (v->current_order.type == OT_GOTO_STATION) {
|
2006-10-13 23:08:55 +00:00
|
|
|
if (AirportFindFreeHelipad(v, apc)) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2006-10-13 23:08:55 +00:00
|
|
|
v->u.air.state = (apc->nof_depots != 0) ? HANGAR : HELITAKEOFF;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2006-10-13 23:08:55 +00:00
|
|
|
typedef void AircraftStateHandler(Vehicle *v, const AirportFTAClass *apc);
|
2004-08-09 17:04:08 +00:00
|
|
|
static AircraftStateHandler * const _aircraft_state_handlers[] = {
|
2006-08-22 14:38:37 +00:00
|
|
|
AircraftEventHandler_General, // TO_ALL = 0
|
|
|
|
AircraftEventHandler_InHangar, // HANGAR = 1
|
|
|
|
AircraftEventHandler_AtTerminal, // TERM1 = 2
|
|
|
|
AircraftEventHandler_AtTerminal, // TERM2 = 3
|
|
|
|
AircraftEventHandler_AtTerminal, // TERM3 = 4
|
|
|
|
AircraftEventHandler_AtTerminal, // TERM4 = 5
|
|
|
|
AircraftEventHandler_AtTerminal, // TERM5 = 6
|
|
|
|
AircraftEventHandler_AtTerminal, // TERM6 = 7
|
|
|
|
AircraftEventHandler_AtTerminal, // HELIPAD1 = 8
|
|
|
|
AircraftEventHandler_AtTerminal, // HELIPAD2 = 9
|
|
|
|
AircraftEventHandler_TakeOff, // TAKEOFF = 10
|
|
|
|
AircraftEventHandler_StartTakeOff, // STARTTAKEOFF = 11
|
|
|
|
AircraftEventHandler_EndTakeOff, // ENDTAKEOFF = 12
|
|
|
|
AircraftEventHandler_HeliTakeOff, // HELITAKEOFF = 13
|
|
|
|
AircraftEventHandler_Flying, // FLYING = 14
|
|
|
|
AircraftEventHandler_Landing, // LANDING = 15
|
|
|
|
AircraftEventHandler_EndLanding, // ENDLANDING = 16
|
|
|
|
AircraftEventHandler_HeliLanding, // HELILANDING = 17
|
|
|
|
AircraftEventHandler_HeliEndLanding, // HELIENDLANDING = 18
|
|
|
|
AircraftEventHandler_AtTerminal, // TERM7 = 19
|
|
|
|
AircraftEventHandler_AtTerminal, // TERM8 = 20
|
|
|
|
AircraftEventHandler_AtTerminal, // HELIPAD3 = 21
|
|
|
|
AircraftEventHandler_AtTerminal, // HELIPAD4 = 22
|
2004-08-09 17:04:08 +00:00
|
|
|
};
|
|
|
|
|
2006-10-13 23:08:55 +00:00
|
|
|
static void AirportClearBlock(const Vehicle *v, const AirportFTAClass *apc)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2007-02-23 11:50:43 +00:00
|
|
|
/* we have left the previous block, and entered the new one. Free the previous block */
|
2006-10-13 23:08:55 +00:00
|
|
|
if (apc->layout[v->u.air.previous_pos].block != apc->layout[v->u.air.pos].block) {
|
2006-08-22 14:38:37 +00:00
|
|
|
Station *st = GetStation(v->u.air.targetairport);
|
2005-11-14 19:48:04 +00:00
|
|
|
|
2006-10-13 23:08:55 +00:00
|
|
|
CLRBITS(st->airport_flags, apc->layout[v->u.air.previous_pos].block);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-02-21 19:49:18 +00:00
|
|
|
static void AirportGoToNextPosition(Vehicle *v)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2007-02-23 11:50:43 +00:00
|
|
|
/* if aircraft is not in position, wait until it is */
|
2005-10-23 13:04:44 +00:00
|
|
|
if (!AircraftController(v)) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-02-21 19:49:18 +00:00
|
|
|
const AirportFTAClass *apc = GetStation(v->u.air.targetairport)->Airport();
|
|
|
|
|
2006-10-13 23:08:55 +00:00
|
|
|
AirportClearBlock(v, apc);
|
|
|
|
AirportMove(v, apc); // move aircraft to next position
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-02-23 11:50:43 +00:00
|
|
|
/* gets pos from vehicle and next orders */
|
2006-10-13 23:08:55 +00:00
|
|
|
static bool AirportMove(Vehicle *v, const AirportFTAClass *apc)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2007-02-23 11:50:43 +00:00
|
|
|
/* error handling */
|
2006-10-13 23:08:55 +00:00
|
|
|
if (v->u.air.pos >= apc->nofelements) {
|
2006-12-26 17:36:18 +00:00
|
|
|
DEBUG(misc, 0, "[Ap] position %d is not valid for current airport. Max position is %d", v->u.air.pos, apc->nofelements-1);
|
2006-10-13 23:08:55 +00:00
|
|
|
assert(v->u.air.pos < apc->nofelements);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-02-20 06:39:09 +00:00
|
|
|
const AirportFTA *current = &apc->layout[v->u.air.pos];
|
2007-02-23 11:50:43 +00:00
|
|
|
/* we have arrived in an important state (eg terminal, hangar, etc.) */
|
2004-08-09 17:04:08 +00:00
|
|
|
if (current->heading == v->u.air.state) {
|
2007-02-20 06:39:09 +00:00
|
|
|
byte prev_pos = v->u.air.pos; // location could be changed in state, so save it before-hand
|
2007-04-18 18:37:40 +00:00
|
|
|
byte prev_state = v->u.air.state;
|
2006-10-13 23:08:55 +00:00
|
|
|
_aircraft_state_handlers[v->u.air.state](v, apc);
|
2005-10-23 13:04:44 +00:00
|
|
|
if (v->u.air.state != FLYING) v->u.air.previous_pos = prev_pos;
|
2007-05-06 21:10:49 +00:00
|
|
|
if (v->u.air.state != prev_state || v->u.air.pos != prev_pos) UpdateAircraftCache(v);
|
2004-08-09 17:04:08 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
v->u.air.previous_pos = v->u.air.pos; // save previous location
|
|
|
|
|
2007-02-23 11:50:43 +00:00
|
|
|
/* there is only one choice to move to */
|
2006-10-13 23:08:55 +00:00
|
|
|
if (current->next == NULL) {
|
|
|
|
if (AirportSetBlocks(v, current, apc)) {
|
2004-08-09 17:04:08 +00:00
|
|
|
v->u.air.pos = current->next_position;
|
2007-05-06 21:10:49 +00:00
|
|
|
UpdateAircraftCache(v);
|
2004-08-09 17:04:08 +00:00
|
|
|
} // move to next position
|
2006-04-18 07:20:37 +00:00
|
|
|
return false;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-02-23 11:50:43 +00:00
|
|
|
/* there are more choices to choose from, choose the one that
|
|
|
|
* matches our heading */
|
2004-08-09 17:04:08 +00:00
|
|
|
do {
|
|
|
|
if (v->u.air.state == current->heading || current->heading == TO_ALL) {
|
2006-10-13 23:08:55 +00:00
|
|
|
if (AirportSetBlocks(v, current, apc)) {
|
2005-11-14 19:48:04 +00:00
|
|
|
v->u.air.pos = current->next_position;
|
2007-05-06 21:10:49 +00:00
|
|
|
UpdateAircraftCache(v);
|
2005-11-14 19:48:04 +00:00
|
|
|
} // move to next position
|
2006-04-18 07:20:37 +00:00
|
|
|
return false;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2006-10-13 23:08:55 +00:00
|
|
|
current = current->next;
|
2004-08-09 17:04:08 +00:00
|
|
|
} while (current != NULL);
|
|
|
|
|
2007-02-16 12:10:19 +00:00
|
|
|
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);
|
2004-08-09 17:04:08 +00:00
|
|
|
assert(0);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-02-23 11:50:43 +00:00
|
|
|
/* returns true if the road ahead is busy, eg. you must wait before proceeding */
|
2006-10-14 09:51:04 +00:00
|
|
|
static bool AirportHasBlock(Vehicle *v, const AirportFTA *current_pos, const AirportFTAClass *apc)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2006-10-14 09:51:04 +00:00
|
|
|
const AirportFTA *reference = &apc->layout[v->u.air.pos];
|
|
|
|
const AirportFTA *next = &apc->layout[current_pos->next_position];
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-02-23 11:50:43 +00:00
|
|
|
/* same block, then of course we can move */
|
2006-10-13 23:08:55 +00:00
|
|
|
if (apc->layout[current_pos->position].block != next->block) {
|
2006-10-14 09:51:04 +00:00
|
|
|
const Station *st = GetStation(v->u.air.targetairport);
|
2007-02-14 12:00:43 +00:00
|
|
|
uint64 airport_flags = next->block;
|
2005-11-13 13:43:55 +00:00
|
|
|
|
2007-02-23 11:50:43 +00:00
|
|
|
/* check additional possible extra blocks */
|
2004-08-09 17:04:08 +00:00
|
|
|
if (current_pos != reference && current_pos->block != NOTHING_block) {
|
|
|
|
airport_flags |= current_pos->block;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (HASBITS(st->airport_flags, airport_flags)) {
|
|
|
|
v->cur_speed = 0;
|
|
|
|
v->subspeed = 0;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-02-23 11:50:43 +00:00
|
|
|
/**
|
2007-04-02 14:20:31 +00:00
|
|
|
* "reserve" a block for the plane
|
2007-02-23 11:50:43 +00:00
|
|
|
* @param v airplane that requires the operation
|
2007-04-02 14:20:31 +00:00
|
|
|
* @param current_pos of the vehicle in the list of blocks
|
2007-02-23 11:50:43 +00:00
|
|
|
* @param apc airport on which block is requsted to be set
|
|
|
|
* @returns true on success. Eg, next block was free and we have occupied it
|
|
|
|
*/
|
2007-02-20 06:39:09 +00:00
|
|
|
static bool AirportSetBlocks(Vehicle *v, const AirportFTA *current_pos, const AirportFTAClass *apc)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2007-02-20 06:39:09 +00:00
|
|
|
const AirportFTA *next = &apc->layout[current_pos->next_position];
|
|
|
|
const AirportFTA *reference = &apc->layout[v->u.air.pos];
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-02-23 11:50:43 +00:00
|
|
|
/* if the next position is in another block, check it and wait until it is free */
|
2006-10-13 23:08:55 +00:00
|
|
|
if ((apc->layout[current_pos->position].block & next->block) != next->block) {
|
2007-02-14 12:00:43 +00:00
|
|
|
uint64 airport_flags = next->block;
|
2007-02-23 11:50:43 +00:00
|
|
|
/* search for all all elements in the list with the same state, and blocks != N
|
|
|
|
* this means more blocks should be checked/set */
|
2007-02-20 06:39:09 +00:00
|
|
|
const AirportFTA *current = current_pos;
|
2006-10-13 23:08:55 +00:00
|
|
|
if (current == reference) current = current->next;
|
2004-08-09 17:04:08 +00:00
|
|
|
while (current != NULL) {
|
|
|
|
if (current->heading == current_pos->heading && current->block != 0) {
|
|
|
|
airport_flags |= current->block;
|
|
|
|
break;
|
|
|
|
}
|
2006-10-13 23:08:55 +00:00
|
|
|
current = current->next;
|
2004-08-09 17:04:08 +00:00
|
|
|
};
|
|
|
|
|
2007-02-23 11:50:43 +00:00
|
|
|
/* if the block to be checked is in the next position, then exclude that from
|
|
|
|
* checking, because it has been set by the airplane before */
|
2005-10-23 13:04:44 +00:00
|
|
|
if (current_pos->block == next->block) airport_flags ^= next->block;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-02-20 06:39:09 +00:00
|
|
|
Station* st = GetStation(v->u.air.targetairport);
|
2004-08-09 17:04:08 +00:00
|
|
|
if (HASBITS(st->airport_flags, airport_flags)) {
|
|
|
|
v->cur_speed = 0;
|
|
|
|
v->subspeed = 0;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (next->block != NOTHING_block) {
|
|
|
|
SETBITS(st->airport_flags, airport_flags); // occupy next block
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool FreeTerminal(Vehicle *v, byte i, byte last_terminal)
|
|
|
|
{
|
2005-01-06 22:31:58 +00:00
|
|
|
Station *st = GetStation(v->u.air.targetairport);
|
2004-08-09 17:04:08 +00:00
|
|
|
for (; i < last_terminal; i++) {
|
2006-06-23 22:05:40 +00:00
|
|
|
if (!HASBIT(st->airport_flags, _airport_terminal_flag[i])) {
|
2007-02-23 11:50:43 +00:00
|
|
|
/* TERMINAL# HELIPAD# */
|
2006-06-23 22:05:40 +00:00
|
|
|
v->u.air.state = _airport_terminal_state[i]; // start moving to that terminal/helipad
|
|
|
|
SETBIT(st->airport_flags, _airport_terminal_flag[i]); // occupy terminal/helipad
|
2004-08-09 17:04:08 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2006-10-13 23:08:55 +00:00
|
|
|
static uint GetNumTerminals(const AirportFTAClass *apc)
|
2005-01-09 08:49:40 +00:00
|
|
|
{
|
2005-11-14 19:48:04 +00:00
|
|
|
uint num = 0;
|
2005-01-15 08:58:31 +00:00
|
|
|
|
2007-02-20 06:39:09 +00:00
|
|
|
for (uint i = apc->terminals[0]; i > 0; i--) num += apc->terminals[i];
|
2005-01-15 08:58:31 +00:00
|
|
|
|
2005-01-09 08:49:40 +00:00
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
2006-10-13 23:08:55 +00:00
|
|
|
static bool AirportFindFreeTerminal(Vehicle *v, const AirportFTAClass *apc)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
/* example of more terminalgroups
|
2006-09-04 20:40:33 +00:00
|
|
|
* {0,HANGAR,NOTHING_block,1}, {0,255,TERM_GROUP1_block,0}, {0,255,TERM_GROUP2_ENTER_block,1}, {0,0,N,1},
|
|
|
|
* Heading 255 denotes a group. We see 2 groups here:
|
|
|
|
* 1. group 0 -- TERM_GROUP1_block (check block)
|
|
|
|
* 2. group 1 -- TERM_GROUP2_ENTER_block (check block)
|
|
|
|
* First in line is checked first, group 0. If the block (TERM_GROUP1_block) is free, it
|
|
|
|
* looks at the corresponding terminals of that group. If no free ones are found, other
|
|
|
|
* possible groups are checked (in this case group 1, since that is after group 0). If that
|
|
|
|
* fails, then attempt fails and plane waits
|
|
|
|
*/
|
2006-10-13 23:08:55 +00:00
|
|
|
if (apc->terminals[0] > 1) {
|
2007-02-20 06:39:09 +00:00
|
|
|
const Station *st = GetStation(v->u.air.targetairport);
|
|
|
|
const AirportFTA *temp = apc->layout[v->u.air.pos].next;
|
2006-10-14 09:51:04 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
while (temp != NULL) {
|
|
|
|
if (temp->heading == 255) {
|
|
|
|
if (!HASBITS(st->airport_flags, temp->block)) {
|
2007-02-23 11:50:43 +00:00
|
|
|
/* read which group do we want to go to?
|
|
|
|
* (the first free group) */
|
2007-02-20 06:39:09 +00:00
|
|
|
uint target_group = temp->next_position + 1;
|
2005-01-09 08:49:40 +00:00
|
|
|
|
2007-02-23 11:50:43 +00:00
|
|
|
/* at what terminal does the group start?
|
|
|
|
* that means, sum up all terminals of
|
|
|
|
* groups with lower number */
|
2007-02-20 06:39:09 +00:00
|
|
|
uint group_start = 0;
|
|
|
|
for (uint i = 1; i < target_group; i++) {
|
2006-10-13 23:08:55 +00:00
|
|
|
group_start += apc->terminals[i];
|
2007-02-20 06:39:09 +00:00
|
|
|
}
|
2005-01-09 08:49:40 +00:00
|
|
|
|
2007-02-20 06:39:09 +00:00
|
|
|
uint group_end = group_start + apc->terminals[target_group];
|
2005-10-23 13:04:44 +00:00
|
|
|
if (FreeTerminal(v, group_start, group_end)) return true;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2005-10-23 13:04:44 +00:00
|
|
|
} else {
|
|
|
|
/* once the heading isn't 255, we've exhausted the possible blocks.
|
|
|
|
* So we cannot move */
|
|
|
|
return false;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2006-10-13 23:08:55 +00:00
|
|
|
temp = temp->next;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-02-23 11:50:43 +00:00
|
|
|
/* if there is only 1 terminalgroup, all terminals are checked (starting from 0 to max) */
|
2006-10-13 23:08:55 +00:00
|
|
|
return FreeTerminal(v, 0, GetNumTerminals(apc));
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2006-10-13 23:08:55 +00:00
|
|
|
static uint GetNumHelipads(const AirportFTAClass *apc)
|
2005-01-09 08:49:40 +00:00
|
|
|
{
|
2005-11-14 19:48:04 +00:00
|
|
|
uint num = 0;
|
2005-01-15 08:58:31 +00:00
|
|
|
|
2007-02-20 06:39:09 +00:00
|
|
|
for (uint i = apc->helipads[0]; i > 0; i--) num += apc->helipads[i];
|
2005-01-15 08:58:31 +00:00
|
|
|
|
2005-01-09 08:49:40 +00:00
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-10-13 23:08:55 +00:00
|
|
|
static bool AirportFindFreeHelipad(Vehicle *v, const AirportFTAClass *apc)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2007-02-23 11:50:43 +00:00
|
|
|
/* if an airport doesn't have helipads, use terminals */
|
2006-10-13 23:08:55 +00:00
|
|
|
if (apc->helipads == NULL) return AirportFindFreeTerminal(v, apc);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-02-23 11:50:43 +00:00
|
|
|
/* if there are more helicoptergroups, pick one, just as in AirportFindFreeTerminal() */
|
2006-10-13 23:08:55 +00:00
|
|
|
if (apc->helipads[0] > 1) {
|
2006-06-27 21:25:53 +00:00
|
|
|
const Station* st = GetStation(v->u.air.targetairport);
|
2006-10-13 23:08:55 +00:00
|
|
|
const AirportFTA* temp = apc->layout[v->u.air.pos].next;
|
2006-06-27 21:25:53 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
while (temp != NULL) {
|
|
|
|
if (temp->heading == 255) {
|
|
|
|
if (!HASBITS(st->airport_flags, temp->block)) {
|
2005-01-09 08:49:40 +00:00
|
|
|
|
2007-02-23 11:50:43 +00:00
|
|
|
/* read which group do we want to go to?
|
|
|
|
* (the first free group) */
|
2007-02-20 06:39:09 +00:00
|
|
|
uint target_group = temp->next_position + 1;
|
2005-01-09 08:49:40 +00:00
|
|
|
|
2007-02-23 11:50:43 +00:00
|
|
|
/* at what terminal does the group start?
|
|
|
|
* that means, sum up all terminals of
|
|
|
|
* groups with lower number */
|
2007-02-20 06:39:09 +00:00
|
|
|
uint group_start = 0;
|
|
|
|
for (uint i = 1; i < target_group; i++) {
|
2006-10-13 23:08:55 +00:00
|
|
|
group_start += apc->helipads[i];
|
2007-02-20 06:39:09 +00:00
|
|
|
}
|
2005-01-09 08:49:40 +00:00
|
|
|
|
2007-02-20 06:39:09 +00:00
|
|
|
uint group_end = group_start + apc->helipads[target_group];
|
2005-10-23 13:04:44 +00:00
|
|
|
if (FreeTerminal(v, group_start, group_end)) return true;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2005-10-23 13:04:44 +00:00
|
|
|
} else {
|
|
|
|
/* once the heading isn't 255, we've exhausted the possible blocks.
|
|
|
|
* So we cannot move */
|
|
|
|
return false;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2006-10-13 23:08:55 +00:00
|
|
|
temp = temp->next;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2005-10-23 13:04:44 +00:00
|
|
|
} else {
|
2007-02-23 11:50:43 +00:00
|
|
|
/* only 1 helicoptergroup, check all helipads
|
|
|
|
* The blocks for helipads start after the last terminal (MAX_TERMINALS) */
|
2006-10-13 23:08:55 +00:00
|
|
|
return FreeTerminal(v, MAX_TERMINALS, GetNumHelipads(apc) + MAX_TERMINALS);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2006-08-28 18:53:03 +00:00
|
|
|
return false; // it shouldn't get here anytime, but just to be sure
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void AircraftEventHandler(Vehicle *v, int loop)
|
|
|
|
{
|
|
|
|
v->tick_counter++;
|
2007-06-20 19:17:22 +00:00
|
|
|
v->current_order_time++;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
if (v->vehstatus & VS_CRASHED) {
|
|
|
|
HandleCrashedAircraft(v);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2005-11-14 19:48:04 +00:00
|
|
|
if (v->vehstatus & VS_STOPPED) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
/* aircraft is broken down? */
|
|
|
|
if (v->breakdown_ctr != 0) {
|
|
|
|
if (v->breakdown_ctr <= 2) {
|
|
|
|
HandleBrokenAircraft(v);
|
|
|
|
} else {
|
|
|
|
v->breakdown_ctr--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
HandleAircraftSmoke(v);
|
|
|
|
ProcessAircraftOrder(v);
|
2007-05-07 16:21:34 +00:00
|
|
|
v->HandleLoading(loop != 0);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-11-14 19:48:04 +00:00
|
|
|
if (v->current_order.type >= OT_LOADING) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-02-21 19:49:18 +00:00
|
|
|
AirportGoToNextPosition(v);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-07-01 19:24:54 +00:00
|
|
|
void Aircraft::Tick()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2007-07-01 19:24:54 +00:00
|
|
|
if (!IsNormalAircraft(this)) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-07-01 19:24:54 +00:00
|
|
|
if (this->subtype == AIR_HELICOPTER) HelicopterTickHandler(this);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-07-01 19:24:54 +00:00
|
|
|
AgeAircraftCargo(this);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-03-02 12:01:24 +00:00
|
|
|
for (uint i = 0; i != 2; i++) {
|
2007-07-01 19:24:54 +00:00
|
|
|
AircraftEventHandler(this, i);
|
|
|
|
if (this->type != VEH_AIRCRAFT) // In case it was deleted
|
2004-08-09 17:04:08 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-01-09 08:49:40 +00:00
|
|
|
|
2007-02-23 11:50:43 +00:00
|
|
|
/** need to be called to load aircraft from old version */
|
2007-03-07 11:47:46 +00:00
|
|
|
void UpdateOldAircraft()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2007-02-23 11:50:43 +00:00
|
|
|
/* set airport_flags to 0 for all airports just to be sure */
|
2007-02-20 06:39:09 +00:00
|
|
|
Station *st;
|
2004-08-09 17:04:08 +00:00
|
|
|
FOR_ALL_STATIONS(st) {
|
|
|
|
st->airport_flags = 0; // reset airport
|
|
|
|
}
|
|
|
|
|
2007-02-20 06:39:09 +00:00
|
|
|
Vehicle *v_oldstyle;
|
2004-08-09 17:04:08 +00:00
|
|
|
FOR_ALL_VEHICLES(v_oldstyle) {
|
2007-02-23 11:50:43 +00:00
|
|
|
/* airplane has another vehicle with subtype 4 (shadow), helicopter also has 3 (rotor)
|
|
|
|
* skip those */
|
2007-03-08 16:27:54 +00:00
|
|
|
if (v_oldstyle->type == VEH_AIRCRAFT && IsNormalAircraft(v_oldstyle)) {
|
2007-02-23 11:50:43 +00:00
|
|
|
/* airplane in terminal stopped doesn't hurt anyone, so goto next */
|
2005-11-14 19:48:04 +00:00
|
|
|
if (v_oldstyle->vehstatus & VS_STOPPED && v_oldstyle->u.air.state == 0) {
|
2004-08-09 17:04:08 +00:00
|
|
|
v_oldstyle->u.air.state = HANGAR;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
AircraftLeaveHangar(v_oldstyle); // make airplane visible if it was in a depot for example
|
|
|
|
v_oldstyle->vehstatus &= ~VS_STOPPED; // make airplane moving
|
|
|
|
v_oldstyle->u.air.state = FLYING;
|
|
|
|
AircraftNextAirportPos_and_Order(v_oldstyle); // move it to the entry point of the airport
|
2007-02-25 10:49:13 +00:00
|
|
|
GetNewVehiclePosResult gp = GetNewVehiclePos(v_oldstyle);
|
2004-08-09 17:04:08 +00:00
|
|
|
v_oldstyle->tile = 0; // aircraft in air is tile=0
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2007-02-23 11:50:43 +00:00
|
|
|
/* correct speed of helicopter-rotors */
|
2007-08-30 13:03:56 +00:00
|
|
|
if (v_oldstyle->subtype == AIR_HELICOPTER) v_oldstyle->Next()->Next()->cur_speed = 32;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-02-23 11:50:43 +00:00
|
|
|
/* set new position x,y,z */
|
2004-08-09 17:04:08 +00:00
|
|
|
SetAircraftPosition(v_oldstyle, gp.x, gp.y, GetAircraftFlyingAltitude(v_oldstyle));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-04-03 16:12:28 +00:00
|
|
|
/**
|
|
|
|
* Updates the status of the Aircraft heading or in the station
|
|
|
|
* @param st Station been updated
|
|
|
|
*/
|
2007-02-20 06:39:09 +00:00
|
|
|
void UpdateAirplanesOnNewStation(const Station *st)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2007-02-23 11:50:43 +00:00
|
|
|
/* only 1 station is updated per function call, so it is enough to get entry_point once */
|
2007-02-16 09:38:43 +00:00
|
|
|
const AirportFTAClass *ap = st->Airport();
|
2007-02-20 06:39:09 +00:00
|
|
|
|
|
|
|
Vehicle *v;
|
2004-08-09 17:04:08 +00:00
|
|
|
FOR_ALL_VEHICLES(v) {
|
2007-03-08 16:27:54 +00:00
|
|
|
if (v->type == VEH_AIRCRAFT && IsNormalAircraft(v)) {
|
2006-08-28 18:53:03 +00:00
|
|
|
if (v->u.air.targetairport == st->index) { // if heading to this airport
|
|
|
|
/* update position of airplane. If plane is not flying, landing, or taking off
|
2007-04-03 16:12:28 +00:00
|
|
|
* you cannot delete airport, so it doesn't matter */
|
2006-08-28 18:53:03 +00:00
|
|
|
if (v->u.air.state >= FLYING) { // circle around
|
2007-02-16 12:10:19 +00:00
|
|
|
v->u.air.pos = v->u.air.previous_pos = AircraftGetEntryPoint(v, ap);
|
2004-08-09 17:04:08 +00:00
|
|
|
v->u.air.state = FLYING;
|
2007-05-06 21:10:49 +00:00
|
|
|
UpdateAircraftCache(v);
|
2007-02-23 11:50:43 +00:00
|
|
|
/* landing plane needs to be reset to flying height (only if in pause mode upgrade,
|
|
|
|
* in normal mode, plane is reset in AircraftController. It doesn't hurt for FLYING */
|
2007-02-25 10:49:13 +00:00
|
|
|
GetNewVehiclePosResult gp = GetNewVehiclePos(v);
|
2007-02-23 11:50:43 +00:00
|
|
|
/* set new position x,y,z */
|
2004-08-09 17:04:08 +00:00
|
|
|
SetAircraftPosition(v, gp.x, gp.y, GetAircraftFlyingAltitude(v));
|
2005-10-23 13:04:44 +00:00
|
|
|
} else {
|
2004-08-09 17:04:08 +00:00
|
|
|
assert(v->u.air.state == ENDTAKEOFF || v->u.air.state == HELITAKEOFF);
|
2007-02-20 06:39:09 +00:00
|
|
|
byte takeofftype = (v->subtype == AIR_HELICOPTER) ? HELITAKEOFF : ENDTAKEOFF;
|
2007-02-23 11:50:43 +00:00
|
|
|
/* search in airportdata for that heading
|
|
|
|
* easiest to do, since this doesn't happen a lot */
|
2007-02-20 06:39:09 +00:00
|
|
|
for (uint cnt = 0; cnt < ap->nofelements; cnt++) {
|
2004-08-09 17:04:08 +00:00
|
|
|
if (ap->layout[cnt].heading == takeofftype) {
|
|
|
|
v->u.air.pos = ap->layout[cnt].position;
|
2007-05-06 21:10:49 +00:00
|
|
|
UpdateAircraftCache(v);
|
2004-08-09 17:04:08 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|