2005-07-24 14:12:37 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
|
|
|
|
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* @file aircraft_cmd.cpp
|
2010-08-01 19:44:49 +00:00
|
|
|
* This file deals with aircraft and airport movements functionalities
|
|
|
|
*/
|
2007-02-23 01:48:53 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
#include "stdafx.h"
|
2006-06-05 12:43:41 +00:00
|
|
|
#include "aircraft.h"
|
2007-04-12 13:07:15 +00:00
|
|
|
#include "landscape.h"
|
2008-03-28 08:53:36 +00:00
|
|
|
#include "news_func.h"
|
2006-02-03 12:55:21 +00:00
|
|
|
#include "newgrf_engine.h"
|
2006-09-27 18:17:01 +00:00
|
|
|
#include "newgrf_sound.h"
|
2007-02-10 13:37:32 +00:00
|
|
|
#include "spritecache.h"
|
2007-12-21 19:49:27 +00:00
|
|
|
#include "strings_func.h"
|
2007-12-21 21:50:46 +00:00
|
|
|
#include "command_func.h"
|
2007-12-25 11:26:07 +00:00
|
|
|
#include "window_func.h"
|
2007-12-26 13:50:40 +00:00
|
|
|
#include "date_func.h"
|
2007-12-27 13:35:39 +00:00
|
|
|
#include "vehicle_func.h"
|
2007-12-29 09:24:26 +00:00
|
|
|
#include "sound_func.h"
|
2009-01-31 20:16:06 +00:00
|
|
|
#include "cheat_type.h"
|
2010-01-15 16:41:15 +00:00
|
|
|
#include "company_base.h"
|
2009-01-12 17:11:45 +00:00
|
|
|
#include "ai/ai.hpp"
|
2011-12-19 20:59:36 +00:00
|
|
|
#include "game/game.hpp"
|
2008-09-30 20:51:04 +00:00
|
|
|
#include "company_func.h"
|
2008-04-20 11:12:07 +00:00
|
|
|
#include "effectvehicle_func.h"
|
2009-06-24 17:39:54 +00:00
|
|
|
#include "station_base.h"
|
2010-01-15 16:41:15 +00:00
|
|
|
#include "engine_base.h"
|
|
|
|
#include "core/random_func.hpp"
|
2010-05-31 20:22:57 +00:00
|
|
|
#include "core/backup_type.hpp"
|
2011-11-24 12:38:48 +00:00
|
|
|
#include "zoom_func.h"
|
2014-09-21 11:12:42 +00:00
|
|
|
#include "disaster_vehicle.h"
|
2018-07-19 19:17:07 +00:00
|
|
|
#include "framerate_type.h"
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-01-13 01:21:35 +00:00
|
|
|
#include "table/strings.h"
|
|
|
|
|
2014-04-23 20:13:33 +00:00
|
|
|
#include "safeguards.h"
|
|
|
|
|
2018-05-22 17:43:34 +00:00
|
|
|
void Aircraft::UpdateDeltaXY()
|
2007-05-01 16:35:14 +00:00
|
|
|
{
|
2010-08-17 11:59:52 +00:00
|
|
|
this->x_offs = -1;
|
|
|
|
this->y_offs = -1;
|
|
|
|
this->x_extent = 2;
|
|
|
|
this->y_extent = 2;
|
|
|
|
|
2007-05-01 16:35:14 +00:00
|
|
|
switch (this->subtype) {
|
|
|
|
default: NOT_REACHED();
|
2010-08-17 11:59:52 +00:00
|
|
|
|
2007-05-01 16:35:14 +00:00
|
|
|
case AIR_AIRCRAFT:
|
|
|
|
case AIR_HELICOPTER:
|
2009-05-22 20:07:26 +00:00
|
|
|
switch (this->state) {
|
2010-08-17 11:59:52 +00:00
|
|
|
default: break;
|
2007-05-01 16:35:14 +00:00
|
|
|
case ENDTAKEOFF:
|
|
|
|
case LANDING:
|
|
|
|
case HELILANDING:
|
2010-08-17 11:59:52 +00:00
|
|
|
case FLYING:
|
|
|
|
this->x_extent = 24;
|
|
|
|
this->y_extent = 24;
|
|
|
|
break;
|
2007-05-01 16:35:14 +00:00
|
|
|
}
|
2008-04-01 14:03:20 +00:00
|
|
|
this->z_extent = 5;
|
2007-05-01 16:35:14 +00:00
|
|
|
break;
|
|
|
|
|
2010-08-17 11:59:52 +00:00
|
|
|
case AIR_SHADOW:
|
|
|
|
this->z_extent = 1;
|
|
|
|
this->x_offs = 0;
|
|
|
|
this->y_offs = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AIR_ROTOR:
|
|
|
|
this->z_extent = 1;
|
|
|
|
break;
|
|
|
|
}
|
2007-05-01 16:35:14 +00:00
|
|
|
}
|
|
|
|
|
2009-05-22 20:03:26 +00:00
|
|
|
static bool AirportMove(Aircraft *v, const AirportFTAClass *apc);
|
|
|
|
static bool AirportSetBlocks(Aircraft *v, const AirportFTA *current_pos, const AirportFTAClass *apc);
|
|
|
|
static bool AirportHasBlock(Aircraft *v, const AirportFTA *current_pos, const AirportFTAClass *apc);
|
|
|
|
static bool AirportFindFreeTerminal(Aircraft *v, const AirportFTAClass *apc);
|
|
|
|
static bool AirportFindFreeHelipad(Aircraft *v, const AirportFTAClass *apc);
|
|
|
|
static void CrashAirplane(Aircraft *v);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
};
|
|
|
|
|
2013-11-26 16:08:58 +00:00
|
|
|
template <>
|
|
|
|
bool IsValidImageIndex<VEH_AIRCRAFT>(uint8 image_index)
|
|
|
|
{
|
|
|
|
return image_index < lengthof(_aircraft_sprite);
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
};
|
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* Find the nearest hangar to v
|
2008-09-30 20:39:50 +00:00
|
|
|
* INVALID_STATION is returned, if the company does not have any suitable
|
2005-03-23 11:12:17 +00:00
|
|
|
* 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
|
|
|
*/
|
2009-05-22 20:03:26 +00:00
|
|
|
static StationID FindNearestHangar(const Aircraft *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);
|
2009-10-06 19:17:07 +00:00
|
|
|
const AircraftVehicleInfo *avi = AircraftVehInfo(v->engine_type);
|
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;
|
|
|
|
|
2010-03-18 21:02:20 +00:00
|
|
|
const AirportFTAClass *afc = st->airport.GetFTA();
|
2010-03-19 09:48:44 +00:00
|
|
|
if (!st->airport.HasHangar() || (
|
2007-02-15 20:35:45 +00:00
|
|
|
/* don't crash the plane if we know it can't land at the airport */
|
2009-06-01 11:43:36 +00:00
|
|
|
(afc->flags & AirportFTAClass::SHORT_STRIP) &&
|
2009-10-06 19:17:07 +00:00
|
|
|
(avi->subtype & AIR_FAST) &&
|
2009-06-01 11:43:36 +00:00
|
|
|
!_cheats.no_jetcrash.value)) {
|
2007-02-15 20:35:45 +00:00
|
|
|
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 */
|
2010-02-22 14:17:07 +00:00
|
|
|
uint distance = DistanceSquare(vtile, st->airport.tile);
|
2012-12-26 12:45:19 +00:00
|
|
|
if (v->acache.cached_max_range_sqr != 0) {
|
|
|
|
/* Check if our current destination can be reached from the depot airport. */
|
|
|
|
const Station *cur_dest = GetTargetAirportIfValid(v);
|
|
|
|
if (cur_dest != NULL && DistanceSquare(st->airport.tile, cur_dest->airport.tile) > v->acache.cached_max_range_sqr) continue;
|
|
|
|
}
|
2007-02-15 20:35:45 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-10-16 14:57:56 +00:00
|
|
|
void Aircraft::GetImage(Direction direction, EngineImageType image_type, VehicleSpriteSeq *result) const
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2008-04-21 20:50:58 +00:00
|
|
|
uint8 spritenum = this->spritenum;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
if (is_custom_sprite(spritenum)) {
|
2016-10-16 14:57:56 +00:00
|
|
|
GetCustomVehicleSprite(this, direction, image_type, result);
|
|
|
|
if (result->IsValid()) return;
|
2008-04-21 20:50:58 +00:00
|
|
|
|
2011-11-01 00:21:08 +00:00
|
|
|
spritenum = this->GetEngine()->original_image_index;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2008-04-21 20:50:58 +00:00
|
|
|
|
2013-11-26 16:08:58 +00:00
|
|
|
assert(IsValidImageIndex<VEH_AIRCRAFT>(spritenum));
|
2016-10-16 14:57:56 +00:00
|
|
|
result->Set(direction + _aircraft_sprite[spritenum]);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2016-10-16 14:57:56 +00:00
|
|
|
void GetRotorImage(const Aircraft *v, EngineImageType image_type, VehicleSpriteSeq *result)
|
2006-05-01 20:05:03 +00:00
|
|
|
{
|
2007-01-27 12:29:55 +00:00
|
|
|
assert(v->subtype == AIR_HELICOPTER);
|
2006-05-01 20:05:03 +00:00
|
|
|
|
2009-05-22 20:03:26 +00:00
|
|
|
const Aircraft *w = v->Next()->Next();
|
2006-05-01 20:05:03 +00:00
|
|
|
if (is_custom_sprite(v->spritenum)) {
|
2016-10-16 14:57:56 +00:00
|
|
|
GetCustomRotorSprite(v, false, image_type, result);
|
|
|
|
if (result->IsValid()) return;
|
2006-05-01 20:05:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Return standard rotor sprites if there are no custom sprites for this helicopter */
|
2016-10-16 14:57:56 +00:00
|
|
|
result->Set(SPR_ROTOR_STOPPED + w->state);
|
2006-05-01 20:05:03 +00:00
|
|
|
}
|
|
|
|
|
2016-10-16 14:57:56 +00:00
|
|
|
static void GetAircraftIcon(EngineID engine, EngineImageType image_type, VehicleSpriteSeq *result)
|
2004-11-13 20:37:57 +00:00
|
|
|
{
|
2009-10-06 19:17:07 +00:00
|
|
|
const Engine *e = Engine::Get(engine);
|
|
|
|
uint8 spritenum = e->u.air.image_index;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2004-11-13 20:37:57 +00:00
|
|
|
if (is_custom_sprite(spritenum)) {
|
2016-10-16 14:57:56 +00:00
|
|
|
GetCustomVehicleIcon(engine, DIR_W, image_type, result);
|
|
|
|
if (result->IsValid()) return;
|
2008-04-21 20:50:58 +00:00
|
|
|
|
2009-10-06 19:17:07 +00:00
|
|
|
spritenum = e->original_image_index;
|
2008-04-20 21:38:20 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2013-11-26 16:08:58 +00:00
|
|
|
assert(IsValidImageIndex<VEH_AIRCRAFT>(spritenum));
|
2016-10-16 14:57:56 +00:00
|
|
|
result->Set(DIR_W + _aircraft_sprite[spritenum]);
|
2008-04-21 20:50:58 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2011-11-01 16:51:47 +00:00
|
|
|
void DrawAircraftEngine(int left, int right, int preferred_x, int y, EngineID engine, PaletteID pal, EngineImageType image_type)
|
2008-04-21 20:50:58 +00:00
|
|
|
{
|
2016-10-16 14:57:56 +00:00
|
|
|
VehicleSpriteSeq seq;
|
|
|
|
GetAircraftIcon(engine, image_type, &seq);
|
|
|
|
|
2016-10-16 14:58:38 +00:00
|
|
|
Rect rect;
|
|
|
|
seq.GetBounds(&rect);
|
2015-02-01 20:54:24 +00:00
|
|
|
preferred_x = Clamp(preferred_x,
|
2016-10-16 14:58:38 +00:00
|
|
|
left - UnScaleGUI(rect.left),
|
|
|
|
right - UnScaleGUI(rect.right));
|
|
|
|
|
|
|
|
seq.Draw(preferred_x, y, pal, pal == PALETTE_CRASH);
|
2008-04-21 20:50:58 +00:00
|
|
|
|
|
|
|
if (!(AircraftVehInfo(engine)->subtype & AIR_CTOL)) {
|
2016-10-16 14:57:56 +00:00
|
|
|
VehicleSpriteSeq rotor_seq;
|
|
|
|
GetCustomRotorIcon(engine, image_type, &rotor_seq);
|
|
|
|
if (!rotor_seq.IsValid()) rotor_seq.Set(SPR_ROTOR_STOPPED);
|
2016-10-16 14:58:38 +00:00
|
|
|
rotor_seq.Draw(preferred_x, y - ScaleGUITrad(5), PAL_NONE, false);
|
2005-01-28 12:20:06 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
2012-12-23 01:00:25 +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.
|
|
|
|
* @param[out] width The width of the sprite.
|
|
|
|
* @param[out] height The height of the sprite.
|
|
|
|
* @param[out] xoffs Number of pixels to shift the sprite to the right.
|
|
|
|
* @param[out] yoffs Number of pixels to shift the sprite downwards.
|
|
|
|
* @param image_type Context the sprite is used in.
|
2007-02-10 13:37:32 +00:00
|
|
|
*/
|
2012-12-23 01:00:25 +00:00
|
|
|
void GetAircraftSpriteSize(EngineID engine, uint &width, uint &height, int &xoffs, int &yoffs, EngineImageType image_type)
|
2007-02-10 13:37:32 +00:00
|
|
|
{
|
2016-10-16 14:57:56 +00:00
|
|
|
VehicleSpriteSeq seq;
|
|
|
|
GetAircraftIcon(engine, image_type, &seq);
|
|
|
|
|
2016-10-16 14:58:38 +00:00
|
|
|
Rect rect;
|
|
|
|
seq.GetBounds(&rect);
|
2007-02-10 13:37:32 +00:00
|
|
|
|
2016-10-16 14:58:38 +00:00
|
|
|
width = UnScaleGUI(rect.right - rect.left + 1);
|
|
|
|
height = UnScaleGUI(rect.bottom - rect.top + 1);
|
|
|
|
xoffs = UnScaleGUI(rect.left);
|
|
|
|
yoffs = UnScaleGUI(rect.top);
|
2007-02-10 13:37:32 +00:00
|
|
|
}
|
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* Build an aircraft.
|
2010-08-17 23:15:55 +00:00
|
|
|
* @param tile tile of the depot where aircraft is built.
|
|
|
|
* @param flags type of operation.
|
|
|
|
* @param e the engine to build.
|
|
|
|
* @param data unused.
|
2018-10-28 02:17:36 +00:00
|
|
|
* @param[out] ret the vehicle that has been built.
|
2010-08-17 23:15:55 +00:00
|
|
|
* @return the cost of this operation or an error.
|
2005-05-12 00:11:37 +00:00
|
|
|
*/
|
2010-08-17 23:15:55 +00:00
|
|
|
CommandCost CmdBuildAircraft(TileIndex tile, DoCommandFlag flags, const Engine *e, uint16 data, Vehicle **ret)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-10-06 19:17:07 +00:00
|
|
|
const AircraftVehicleInfo *avi = &e->u.air;
|
2010-08-19 13:44:41 +00:00
|
|
|
const Station *st = Station::GetByTile(tile);
|
2005-09-13 13:30:18 +00:00
|
|
|
|
2007-02-23 11:50:43 +00:00
|
|
|
/* Prevent building aircraft types at places which can't handle them */
|
2010-08-19 13:44:41 +00:00
|
|
|
if (!CanVehicleUseStation(e->index, st)) return CMD_ERROR;
|
|
|
|
|
2013-08-09 18:43:44 +00:00
|
|
|
/* Make sure all aircraft end up in the first tile of the hangar. */
|
2010-08-19 13:44:41 +00:00
|
|
|
tile = st->airport.GetHangarTile(st->airport.GetHangarNum(tile));
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
2009-05-22 20:03:26 +00:00
|
|
|
Aircraft *v = new Aircraft(); // aircraft
|
|
|
|
Aircraft *u = new Aircraft(); // shadow
|
2010-08-17 23:15:55 +00:00
|
|
|
*ret = v;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-01-10 18:56:51 +00:00
|
|
|
v->direction = DIR_SE;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
v->owner = u->owner = _current_company;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
v->tile = tile;
|
|
|
|
|
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;
|
|
|
|
|
2011-11-04 10:18:13 +00:00
|
|
|
u->z_pos = GetSlopePixelZ(x, y);
|
2004-08-09 17:04:08 +00:00
|
|
|
v->z_pos = u->z_pos + 1;
|
|
|
|
|
|
|
|
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
|
|
|
|
2005-01-07 08:38:27 +00:00
|
|
|
v->cargo_cap = avi->passenger_capacity;
|
2013-05-19 14:22:04 +00:00
|
|
|
v->refit_cap = 0;
|
2004-12-03 21:57:05 +00:00
|
|
|
u->cargo_cap = avi->mail_capacity;
|
2013-05-19 14:22:04 +00:00
|
|
|
u->refit_cap = 0;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-02-21 12:52:41 +00:00
|
|
|
v->cargo_type = e->GetDefaultCargoType();
|
2004-08-09 17:04:08 +00:00
|
|
|
u->cargo_type = CT_MAIL;
|
|
|
|
|
2008-01-12 19:58:06 +00:00
|
|
|
v->name = NULL;
|
2005-02-02 16:16:43 +00:00
|
|
|
v->last_station_visited = INVALID_STATION;
|
2013-05-19 14:22:04 +00:00
|
|
|
v->last_loading_station = INVALID_STATION;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2004-12-03 21:57:05 +00:00
|
|
|
v->acceleration = avi->acceleration;
|
2010-08-17 23:15:55 +00:00
|
|
|
v->engine_type = e->index;
|
|
|
|
u->engine_type = e->index;
|
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);
|
2018-05-22 17:43:34 +00:00
|
|
|
v->UpdateDeltaXY();
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-01-27 12:29:55 +00:00
|
|
|
u->subtype = AIR_SHADOW;
|
2018-05-22 17:43:34 +00:00
|
|
|
u->UpdateDeltaXY();
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
v->reliability = e->reliability;
|
|
|
|
v->reliability_spd_dec = e->reliability_spd_dec;
|
2009-06-16 13:52:18 +00:00
|
|
|
v->max_age = e->GetLifeLengthInDays();
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-10-29 21:54:28 +00:00
|
|
|
_new_vehicle_id = v->index;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2010-03-18 18:38:32 +00:00
|
|
|
v->pos = GetVehiclePosOnBuild(tile);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-05-22 20:07:26 +00:00
|
|
|
v->state = HANGAR;
|
|
|
|
v->previous_pos = v->pos;
|
|
|
|
v->targetairport = GetStationIndex(tile);
|
2007-08-30 13:09:44 +00:00
|
|
|
v->SetNext(u);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2013-02-14 17:04:01 +00:00
|
|
|
v->SetServiceInterval(Company::Get(_current_company)->settings.vehicle.servint_aircraft);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2016-10-16 14:57:56 +00:00
|
|
|
v->sprite_seq.Set(SPR_IMG_QUERY);
|
|
|
|
u->sprite_seq.Set(SPR_IMG_QUERY);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
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;
|
2007-11-20 13:35:54 +00:00
|
|
|
if (e->flags & ENGINE_EXCLUSIVE_PREVIEW) SetBit(v->vehicle_flags, VF_BUILT_AS_PROTOTYPE);
|
2013-02-14 17:24:55 +00:00
|
|
|
v->SetServiceIntervalIsPercent(Company::Get(_current_company)->settings.vehicle.servint_ispercent);
|
2007-02-28 17:59:05 +00:00
|
|
|
|
2009-05-31 12:03:14 +00:00
|
|
|
v->InvalidateNewGRFCacheOfChain();
|
|
|
|
|
2011-11-09 16:38:50 +00:00
|
|
|
v->cargo_cap = e->DetermineCapacity(v, &u->cargo_cap);
|
2009-05-31 11:31:10 +00:00
|
|
|
|
2009-05-31 12:03:14 +00:00
|
|
|
v->InvalidateNewGRFCacheOfChain();
|
|
|
|
|
2011-12-13 00:43:35 +00:00
|
|
|
UpdateAircraftCache(v, true);
|
2007-04-18 18:37:40 +00:00
|
|
|
|
2014-09-20 15:31:26 +00:00
|
|
|
v->UpdatePosition();
|
|
|
|
u->UpdatePosition();
|
2004-08-09 17:04:08 +00:00
|
|
|
|
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) {
|
2009-05-22 20:03:26 +00:00
|
|
|
Aircraft *w = new Aircraft();
|
2010-08-17 23:15:55 +00:00
|
|
|
w->engine_type = e->index;
|
2007-01-10 18:56:51 +00:00
|
|
|
w->direction = DIR_N;
|
2008-09-30 20:39:50 +00:00
|
|
|
w->owner = _current_company;
|
2004-08-09 17:04:08 +00:00
|
|
|
w->x_pos = v->x_pos;
|
|
|
|
w->y_pos = v->y_pos;
|
2010-11-08 21:22:21 +00:00
|
|
|
w->z_pos = v->z_pos + ROTOR_Z_OFFSET;
|
2004-08-09 17:04:08 +00:00
|
|
|
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;
|
2016-10-16 14:57:56 +00:00
|
|
|
w->sprite_seq.Set(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 */
|
2009-05-22 20:07:26 +00:00
|
|
|
w->state = HRS_ROTOR_STOPPED;
|
2018-05-22 17:43:34 +00:00
|
|
|
w->UpdateDeltaXY();
|
2007-08-30 13:09:44 +00:00
|
|
|
|
|
|
|
u->SetNext(w);
|
2014-09-20 15:31:26 +00:00
|
|
|
w->UpdatePosition();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-17 23:15:55 +00:00
|
|
|
return CommandCost();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-04-11 08:14:43 +00:00
|
|
|
bool Aircraft::FindClosestDepot(TileIndex *location, DestinationID *destination, bool *reverse)
|
|
|
|
{
|
2008-09-16 15:15:41 +00:00
|
|
|
const Station *st = GetTargetAirportIfValid(this);
|
2008-04-11 08:14:43 +00:00
|
|
|
/* If the station is not a valid airport or if it has no hangars */
|
2010-03-19 09:48:44 +00:00
|
|
|
if (st == NULL || !st->airport.HasHangar()) {
|
2008-04-11 08:14:43 +00:00
|
|
|
/* the aircraft has to search for a hangar on its own */
|
|
|
|
StationID station = FindNearestHangar(this);
|
|
|
|
|
|
|
|
if (station == INVALID_STATION) return false;
|
|
|
|
|
2009-05-16 23:34:14 +00:00
|
|
|
st = Station::Get(station);
|
2008-04-11 08:14:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (location != NULL) *location = st->xy;
|
|
|
|
if (destination != NULL) *destination = st->index;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-05-22 20:03:26 +00:00
|
|
|
static void CheckIfAircraftNeedsService(Aircraft *v)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-05-26 21:59:49 +00:00
|
|
|
if (Company::Get(v->owner)->settings.vehicle.servint_aircraft == 0 || !v->NeedsAutomaticServicing()) return;
|
2012-07-07 15:39:46 +00:00
|
|
|
if (v->IsChainInDepot()) {
|
2006-09-03 11:49:38 +00:00
|
|
|
VehicleServiceInDepot(v);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-12-23 18:45:05 +00:00
|
|
|
/* When we're parsing conditional orders and the like
|
|
|
|
* we don't want to consider going to a depot too. */
|
|
|
|
if (!v->current_order.IsType(OT_GOTO_DEPOT) && !v->current_order.IsType(OT_GOTO_STATION)) return;
|
|
|
|
|
2009-05-16 23:34:14 +00:00
|
|
|
const Station *st = Station::Get(v->current_order.GetDestination());
|
2009-05-22 15:13:50 +00:00
|
|
|
|
|
|
|
assert(st != NULL);
|
|
|
|
|
2010-01-21 19:31:25 +00:00
|
|
|
/* only goto depot if the target airport has a depot */
|
2010-03-19 09:48:44 +00:00
|
|
|
if (st->airport.HasHangar() && CanVehicleUseStation(v, st)) {
|
2008-04-08 18:53:25 +00:00
|
|
|
v->current_order.MakeGoToDepot(st->index, ODTFB_SERVICE);
|
2011-12-16 16:58:55 +00:00
|
|
|
SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, WID_VV_START_STOP);
|
2008-04-05 23:36:54 +00:00
|
|
|
} else if (v->current_order.IsType(OT_GOTO_DEPOT)) {
|
|
|
|
v->current_order.MakeDummy();
|
2011-12-16 16:58:55 +00:00
|
|
|
SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, WID_VV_START_STOP);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-22 21:33:08 +00:00
|
|
|
Money Aircraft::GetRunningCost() const
|
|
|
|
{
|
2011-11-01 00:21:08 +00:00
|
|
|
const Engine *e = this->GetEngine();
|
2009-11-24 13:12:34 +00:00
|
|
|
uint cost_factor = GetVehicleProperty(this, PROP_AIRCRAFT_RUNNING_COST_FACTOR, e->u.air.running_cost);
|
2011-11-01 00:23:41 +00:00
|
|
|
return GetPrice(PR_RUNNING_AIRCRAFT, cost_factor, e->GetGRF());
|
2009-01-22 21:33:08 +00:00
|
|
|
}
|
|
|
|
|
2008-02-01 22:02:14 +00:00
|
|
|
void Aircraft::OnNewDay()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-07-13 16:37:27 +00:00
|
|
|
if (!this->IsNormalAircraft()) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-02-01 22:02:14 +00:00
|
|
|
if ((++this->day_counter & 7) == 0) DecreaseVehicleValue(this);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-02-01 22:02:14 +00:00
|
|
|
CheckOrders(this);
|
2004-08-11 10:15:38 +00:00
|
|
|
|
2008-02-01 22:02:14 +00:00
|
|
|
CheckVehicleBreakdown(this);
|
|
|
|
AgeVehicle(this);
|
|
|
|
CheckIfAircraftNeedsService(this);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-02-13 19:24:40 +00:00
|
|
|
if (this->running_ticks == 0) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-01-22 21:33:08 +00:00
|
|
|
CommandCost cost(EXPENSES_AIRCRAFT_RUN, this->GetRunningCost() * this->running_ticks / (DAYS_IN_YEAR * DAY_TICKS));
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-02-13 19:24:40 +00:00
|
|
|
this->profit_this_year -= cost.GetCost();
|
|
|
|
this->running_ticks = 0;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
SubtractMoneyFromCompanyFract(this->owner, cost);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-09-13 19:15:59 +00:00
|
|
|
SetWindowDirty(WC_VEHICLE_DETAILS, this->index);
|
|
|
|
SetWindowClassesDirty(WC_AIRCRAFT_LIST);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2009-05-22 20:03:26 +00:00
|
|
|
static void HelicopterTickHandler(Aircraft *v)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-05-22 20:03:26 +00:00
|
|
|
Aircraft *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 */
|
2008-04-05 23:36:54 +00:00
|
|
|
if (v->current_order.IsType(OT_LOADING) || (v->vehstatus & VS_STOPPED)) {
|
2004-08-09 17:04:08 +00:00
|
|
|
if (u->cur_speed != 0) {
|
|
|
|
u->cur_speed++;
|
2009-05-22 20:07:26 +00:00
|
|
|
if (u->cur_speed >= 0x80 && u->state == HRS_ROTOR_MOVING_3) {
|
2004-08-09 17:04:08 +00:00
|
|
|
u->cur_speed = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2010-07-24 10:14:39 +00:00
|
|
|
if (u->cur_speed == 0) {
|
2004-08-09 17:04:08 +00:00
|
|
|
u->cur_speed = 0x70;
|
2010-07-24 10:14:39 +00:00
|
|
|
}
|
|
|
|
if (u->cur_speed >= 0x50) {
|
2004-08-09 17:04:08 +00:00
|
|
|
u->cur_speed--;
|
2010-07-24 10:14:39 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2016-10-16 14:57:56 +00:00
|
|
|
VehicleSpriteSeq seq;
|
2004-08-09 17:04:08 +00:00
|
|
|
if (spd == 0) {
|
2009-05-22 20:07:26 +00:00
|
|
|
u->state = HRS_ROTOR_STOPPED;
|
2016-10-16 14:57:56 +00:00
|
|
|
GetRotorImage(v, EIT_ON_MAP, &seq);
|
|
|
|
if (u->sprite_seq == seq) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
} else if (tick >= spd) {
|
|
|
|
u->tick_counter = 0;
|
2009-05-22 20:07:26 +00:00
|
|
|
u->state++;
|
|
|
|
if (u->state > HRS_ROTOR_MOVING_3) u->state = HRS_ROTOR_MOVING_1;
|
2016-10-16 14:57:56 +00:00
|
|
|
GetRotorImage(v, EIT_ON_MAP, &seq);
|
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
|
|
|
|
2016-10-16 14:57:56 +00:00
|
|
|
u->sprite_seq = seq;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2014-09-20 15:31:26 +00:00
|
|
|
u->UpdatePositionAndViewport();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2010-11-05 16:34:22 +00:00
|
|
|
/**
|
|
|
|
* Set aircraft position.
|
|
|
|
* @param v Aircraft to position.
|
|
|
|
* @param x New X position.
|
|
|
|
* @param y New y position.
|
|
|
|
* @param z New z position.
|
|
|
|
*/
|
2009-05-22 20:03:26 +00:00
|
|
|
void SetAircraftPosition(Aircraft *v, int x, int y, int z)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
v->x_pos = x;
|
|
|
|
v->y_pos = y;
|
|
|
|
v->z_pos = z;
|
|
|
|
|
2014-09-20 15:31:26 +00:00
|
|
|
v->UpdatePosition();
|
2009-08-08 18:45:12 +00:00
|
|
|
v->UpdateViewport(true, false);
|
2016-10-16 14:57:56 +00:00
|
|
|
if (v->subtype == AIR_HELICOPTER) {
|
|
|
|
GetRotorImage(v, EIT_ON_MAP, &v->Next()->Next()->sprite_seq);
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-05-22 20:03:26 +00:00
|
|
|
Aircraft *u = v->Next();
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-11-19 18:38:10 +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;
|
2011-11-04 10:18:13 +00:00
|
|
|
u->y_pos = y - ((v->z_pos - GetSlopePixelZ(safe_x, safe_y)) >> 3);
|
2006-08-13 14:46:16 +00:00
|
|
|
|
2007-11-19 18:38:10 +00:00
|
|
|
safe_y = Clamp(u->y_pos, 0, MapMaxY() * TILE_SIZE);
|
2011-11-04 10:18:13 +00:00
|
|
|
u->z_pos = GetSlopePixelZ(safe_x, safe_y);
|
2016-10-16 14:59:44 +00:00
|
|
|
u->sprite_seq.CopyWithoutPalette(v->sprite_seq); // the shadow is never coloured
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2014-09-20 15:31:26 +00:00
|
|
|
u->UpdatePositionAndViewport();
|
2004-08-09 17:04:08 +00:00
|
|
|
|
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;
|
2010-11-08 21:22:21 +00:00
|
|
|
u->z_pos = z + ROTOR_Z_OFFSET;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2014-09-20 15:31:26 +00:00
|
|
|
u->UpdatePositionAndViewport();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
2010-11-05 16:34:22 +00:00
|
|
|
* Handle Aircraft specific tasks when an Aircraft enters a hangar
|
2006-10-04 12:01:59 +00:00
|
|
|
* @param *v Vehicle that enters the hangar
|
|
|
|
*/
|
2009-05-22 20:03:26 +00:00
|
|
|
void HandleAircraftEnterHangar(Aircraft *v)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
v->subspeed = 0;
|
|
|
|
v->progress = 0;
|
|
|
|
|
2009-05-22 20:03:26 +00:00
|
|
|
Aircraft *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);
|
|
|
|
}
|
|
|
|
|
2009-01-10 00:31:47 +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
|
|
|
|
2011-01-18 22:17:15 +00:00
|
|
|
/**
|
|
|
|
* Update cached values of an aircraft.
|
|
|
|
* Currently caches callback 36 max speed.
|
|
|
|
* @param v Vehicle
|
2011-12-13 00:43:35 +00:00
|
|
|
* @param update_range Update the aircraft range.
|
2011-01-18 22:17:15 +00:00
|
|
|
*/
|
2011-12-13 00:43:35 +00:00
|
|
|
void UpdateAircraftCache(Aircraft *v, bool update_range)
|
2007-04-18 18:37:40 +00:00
|
|
|
{
|
2009-09-22 19:28:57 +00:00
|
|
|
uint max_speed = GetVehicleProperty(v, PROP_AIRCRAFT_SPEED, 0);
|
2007-04-18 18:37:40 +00:00
|
|
|
if (max_speed != 0) {
|
2010-07-16 17:45:34 +00:00
|
|
|
/* Convert from original units to km-ish/h */
|
|
|
|
max_speed = (max_speed * 128) / 10;
|
2007-04-18 18:37:40 +00:00
|
|
|
|
2010-11-06 12:53:31 +00:00
|
|
|
v->vcache.cached_max_speed = max_speed;
|
2007-04-18 18:37:40 +00:00
|
|
|
} else {
|
2010-11-06 12:50:34 +00:00
|
|
|
/* Use the default max speed of the vehicle. */
|
2010-11-09 10:56:02 +00:00
|
|
|
v->vcache.cached_max_speed = AircraftVehInfo(v->engine_type)->max_speed;
|
2007-04-18 18:37:40 +00:00
|
|
|
}
|
2011-08-03 20:55:08 +00:00
|
|
|
|
|
|
|
/* Update cargo aging period. */
|
|
|
|
v->vcache.cached_cargo_age_period = GetVehicleProperty(v, PROP_AIRCRAFT_CARGO_AGE_PERIOD, EngInfo(v->engine_type)->cargo_age_period);
|
|
|
|
Aircraft *u = v->Next(); // Shadow for mail
|
|
|
|
u->vcache.cached_cargo_age_period = GetVehicleProperty(u, PROP_AIRCRAFT_CARGO_AGE_PERIOD, EngInfo(u->engine_type)->cargo_age_period);
|
2011-12-13 00:43:35 +00:00
|
|
|
|
|
|
|
/* Update aircraft range. */
|
|
|
|
if (update_range) {
|
|
|
|
v->acache.cached_max_range = GetVehicleProperty(v, PROP_AIRCRAFT_RANGE, AircraftVehInfo(v->engine_type)->max_range);
|
|
|
|
/* Squared it now so we don't have to do it later all the time. */
|
|
|
|
v->acache.cached_max_range_sqr = v->acache.cached_max_range * v->acache.cached_max_range;
|
|
|
|
}
|
2007-04-18 18:37:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
2011-12-19 17:48:04 +00:00
|
|
|
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
|
|
|
|
*/
|
2009-05-22 20:03:26 +00:00
|
|
|
static int UpdateAircraftSpeed(Aircraft *v, uint speed_limit = SPEED_LIMIT_NONE, bool hard_limit = true)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2013-03-22 21:27:13 +00:00
|
|
|
/**
|
|
|
|
* 'acceleration' has the unit 3/8 mph/tick. This function is called twice per tick.
|
|
|
|
* So the speed amount we need to accelerate is:
|
|
|
|
* acceleration * 3 / 16 mph = acceleration * 3 / 16 * 16 / 10 km-ish/h
|
|
|
|
* = acceleration * 3 / 10 * 256 * (km-ish/h / 256)
|
|
|
|
* ~ acceleration * 77 (km-ish/h / 256)
|
|
|
|
*/
|
|
|
|
uint spd = v->acceleration * 77;
|
2004-08-09 17:04:08 +00:00
|
|
|
byte t;
|
|
|
|
|
2008-02-27 21:07:12 +00:00
|
|
|
/* Adjust speed limits by plane speed factor to prevent taxiing
|
|
|
|
* and take-off speeds being too low. */
|
2008-05-29 15:13:28 +00:00
|
|
|
speed_limit *= _settings_game.vehicle.plane_speed;
|
2008-02-27 21:07:12 +00:00
|
|
|
|
2010-11-06 12:53:31 +00:00
|
|
|
if (v->vcache.cached_max_speed < speed_limit) {
|
2007-04-18 18:37:40 +00:00
|
|
|
if (v->cur_speed < speed_limit) hard_limit = false;
|
2010-11-06 12:53:31 +00:00
|
|
|
speed_limit = v->vcache.cached_max_speed;
|
2007-04-18 18:37:40 +00:00
|
|
|
}
|
|
|
|
|
2009-07-16 10:13:33 +00:00
|
|
|
v->subspeed = (t = v->subspeed) + (byte)spd;
|
2007-03-02 12:01:24 +00:00
|
|
|
|
2008-02-15 13:28:13 +00:00
|
|
|
/* Aircraft's current speed is used twice so that very fast planes are
|
|
|
|
* forced to slow down rapidly in the short distance needed. The magic
|
|
|
|
* value 16384 was determined to give similar results to the old speed/48
|
|
|
|
* method at slower speeds. This also results in less reduction at slow
|
|
|
|
* speeds to that aircraft do not get to taxi speed straight after
|
|
|
|
* touchdown. */
|
2008-02-27 21:07:12 +00:00
|
|
|
if (!hard_limit && v->cur_speed > speed_limit) {
|
2008-05-29 15:13:28 +00:00
|
|
|
speed_limit = v->cur_speed - max(1, ((v->cur_speed * v->cur_speed) / 16384) / _settings_game.vehicle.plane_speed);
|
2008-02-27 21:07:12 +00:00
|
|
|
}
|
2007-03-02 12:01:24 +00:00
|
|
|
|
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;
|
2011-12-16 16:58:55 +00:00
|
|
|
SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, WID_VV_START_STOP);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2008-02-27 21:07:12 +00:00
|
|
|
/* Adjust distance moved by plane speed setting */
|
2008-05-29 15:13:28 +00:00
|
|
|
if (_settings_game.vehicle.plane_speed > 1) spd /= _settings_game.vehicle.plane_speed;
|
2008-02-27 21:07:12 +00:00
|
|
|
|
2013-01-08 22:46:42 +00:00
|
|
|
/* Convert direction-independent speed into direction-dependent speed. (old movement method) */
|
2010-07-04 13:07:47 +00:00
|
|
|
spd = v->GetOldAdvanceSpeed(spd);
|
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
|
|
|
/**
|
2014-09-21 06:35:34 +00:00
|
|
|
* Get the tile height below the aircraft.
|
|
|
|
* This function is needed because aircraft can leave the mapborders.
|
|
|
|
*
|
|
|
|
* @param v The vehicle to get the height for.
|
|
|
|
* @return The height in pixels from 'z_pos' 0.
|
2007-02-02 15:04:59 +00:00
|
|
|
*/
|
2014-09-21 06:35:34 +00:00
|
|
|
int GetTileHeightBelowAircraft(const Vehicle *v)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2014-09-21 06:35:34 +00:00
|
|
|
int safe_x = Clamp(v->x_pos, 0, MapMaxX() * TILE_SIZE);
|
|
|
|
int safe_y = Clamp(v->y_pos, 0, MapMaxY() * TILE_SIZE);
|
|
|
|
return TileHeight(TileVirtXY(safe_x, safe_y)) * TILE_HEIGHT;
|
|
|
|
}
|
2010-11-08 21:26:32 +00:00
|
|
|
|
2014-09-21 06:35:34 +00:00
|
|
|
/**
|
|
|
|
* Get the 'flight level' bounds, in pixels from 'z_pos' 0 for a particular
|
|
|
|
* vehicle for normal flight situation.
|
|
|
|
* When the maximum is reached the vehicle should consider descending.
|
|
|
|
* When the minimum is reached the vehicle should consider ascending.
|
|
|
|
*
|
2018-10-28 02:17:36 +00:00
|
|
|
* @param v The vehicle to get the flight levels for.
|
|
|
|
* @param[out] min_level The minimum bounds for flight level.
|
|
|
|
* @param[out] max_level The maximum bounds for flight level.
|
2014-09-21 06:35:34 +00:00
|
|
|
*/
|
|
|
|
void GetAircraftFlightLevelBounds(const Vehicle *v, int *min_level, int *max_level)
|
|
|
|
{
|
|
|
|
int base_altitude = GetTileHeightBelowAircraft(v);
|
|
|
|
if (v->type == VEH_AIRCRAFT && Aircraft::From(v)->subtype == AIR_HELICOPTER) {
|
|
|
|
base_altitude += HELICOPTER_HOLD_MAX_FLYING_ALTITUDE - PLANE_HOLD_MAX_FLYING_ALTITUDE;
|
|
|
|
}
|
2007-02-02 15:04:59 +00:00
|
|
|
|
|
|
|
/* Make sure eastbound and westbound planes do not "crash" into each
|
2013-01-08 22:46:42 +00:00
|
|
|
* other by providing them with vertical separation
|
2007-02-02 15:04:59 +00:00
|
|
|
*/
|
|
|
|
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 */
|
2014-09-21 06:35:34 +00:00
|
|
|
base_altitude += min(20 * (v->vcache.cached_max_speed / 200) - 90, 0);
|
|
|
|
|
|
|
|
if (min_level != NULL) *min_level = base_altitude + AIRCRAFT_MIN_FLYING_ALTITUDE;
|
|
|
|
if (max_level != NULL) *max_level = base_altitude + AIRCRAFT_MAX_FLYING_ALTITUDE;
|
|
|
|
}
|
2007-02-02 15:04:59 +00:00
|
|
|
|
2014-09-21 06:35:34 +00:00
|
|
|
/**
|
|
|
|
* Gets the maximum 'flight level' for the holding pattern of the aircraft,
|
2018-10-28 02:17:36 +00:00
|
|
|
* in pixels 'z_pos' 0, depending on terrain below.
|
2014-09-21 06:35:34 +00:00
|
|
|
*
|
|
|
|
* @param v The aircraft that may or may not need to decrease its altitude.
|
|
|
|
* @return Maximal aircraft holding altitude, while in normal flight, in pixels.
|
|
|
|
*/
|
|
|
|
int GetAircraftHoldMaxAltitude(const Aircraft *v)
|
|
|
|
{
|
|
|
|
int tile_height = GetTileHeightBelowAircraft(v);
|
|
|
|
|
|
|
|
return tile_height + ((v->subtype == AIR_HELICOPTER) ? HELICOPTER_HOLD_MAX_FLYING_ALTITUDE : PLANE_HOLD_MAX_FLYING_ALTITUDE);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
2014-09-21 06:41:11 +00:00
|
|
|
int GetAircraftFlightLevel(T *v, bool takeoff)
|
2014-09-21 06:35:34 +00:00
|
|
|
{
|
|
|
|
/* Aircraft is in flight. We want to enforce it being somewhere
|
|
|
|
* between the minimum and the maximum allowed altitude. */
|
|
|
|
int aircraft_min_altitude;
|
|
|
|
int aircraft_max_altitude;
|
|
|
|
GetAircraftFlightLevelBounds(v, &aircraft_min_altitude, &aircraft_max_altitude);
|
|
|
|
int aircraft_middle_altitude = (aircraft_min_altitude + aircraft_max_altitude) / 2;
|
|
|
|
|
2018-04-30 16:52:32 +00:00
|
|
|
/* If those assumptions would be violated, aircraft would behave fairly strange. */
|
2014-09-21 06:35:34 +00:00
|
|
|
assert(aircraft_min_altitude < aircraft_middle_altitude);
|
|
|
|
assert(aircraft_middle_altitude < aircraft_max_altitude);
|
|
|
|
|
|
|
|
int z = v->z_pos;
|
|
|
|
if (z < aircraft_min_altitude ||
|
|
|
|
(HasBit(v->flags, VAF_IN_MIN_HEIGHT_CORRECTION) && z < aircraft_middle_altitude)) {
|
|
|
|
/* Ascend. And don't fly into that mountain right ahead.
|
|
|
|
* And avoid our aircraft become a stairclimber, so if we start
|
|
|
|
* correcting altitude, then we stop correction not too early. */
|
|
|
|
SetBit(v->flags, VAF_IN_MIN_HEIGHT_CORRECTION);
|
|
|
|
z += takeoff ? 2 : 1;
|
|
|
|
} else if (!takeoff && (z > aircraft_max_altitude ||
|
|
|
|
(HasBit(v->flags, VAF_IN_MAX_HEIGHT_CORRECTION) && z > aircraft_middle_altitude))) {
|
|
|
|
/* Descend lower. You are an aircraft, not an space ship.
|
|
|
|
* And again, don't stop correcting altitude too early. */
|
|
|
|
SetBit(v->flags, VAF_IN_MAX_HEIGHT_CORRECTION);
|
|
|
|
z--;
|
|
|
|
} else if (HasBit(v->flags, VAF_IN_MIN_HEIGHT_CORRECTION) && z >= aircraft_middle_altitude) {
|
|
|
|
/* Now, we have corrected altitude enough. */
|
|
|
|
ClrBit(v->flags, VAF_IN_MIN_HEIGHT_CORRECTION);
|
|
|
|
} else if (HasBit(v->flags, VAF_IN_MAX_HEIGHT_CORRECTION) && z <= aircraft_middle_altitude) {
|
|
|
|
/* Now, we have corrected altitude enough. */
|
|
|
|
ClrBit(v->flags, VAF_IN_MAX_HEIGHT_CORRECTION);
|
|
|
|
}
|
|
|
|
|
|
|
|
return z;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2014-09-21 11:17:47 +00:00
|
|
|
template int GetAircraftFlightLevel(DisasterVehicle *v, bool takeoff);
|
2014-09-21 11:12:42 +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.
|
2010-08-05 12:00:09 +00:00
|
|
|
* @param rotation The rotation of the airport.
|
2014-09-21 06:35:34 +00:00
|
|
|
* @return The index of the entry point
|
2007-03-29 13:52:34 +00:00
|
|
|
*/
|
2010-08-05 12:00:09 +00:00
|
|
|
static byte AircraftGetEntryPoint(const Aircraft *v, const AirportFTAClass *apc, Direction rotation)
|
2007-03-29 13:52:34 +00:00
|
|
|
{
|
|
|
|
assert(v != NULL);
|
|
|
|
assert(apc != NULL);
|
|
|
|
|
2008-09-16 15:15:41 +00:00
|
|
|
/* In the case the station doesn't exit anymore, set target tile 0.
|
|
|
|
* It doesn't hurt much, aircraft will go to next order, nearest hangar
|
|
|
|
* or it will simply crash in next tick */
|
|
|
|
TileIndex tile = 0;
|
|
|
|
|
2009-05-22 20:07:26 +00:00
|
|
|
const Station *st = Station::GetIfValid(v->targetairport);
|
2009-05-18 16:21:28 +00:00
|
|
|
if (st != NULL) {
|
2008-12-26 18:01:15 +00:00
|
|
|
/* Make sure we don't go to INVALID_TILE if the airport has been removed. */
|
2010-02-22 14:17:07 +00:00
|
|
|
tile = (st->airport.tile != INVALID_TILE) ? st->airport.tile : st->xy;
|
2008-09-16 15:15:41 +00:00
|
|
|
}
|
2007-03-29 13:52:34 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2015-10-30 16:20:00 +00:00
|
|
|
dir = ChangeDiagDir(dir, DiagDirDifference(DIAGDIR_NE, DirToDiagDir(rotation)));
|
2007-03-29 13:52:34 +00:00
|
|
|
return apc->entry_points[dir];
|
|
|
|
}
|
|
|
|
|
2010-01-28 21:59:18 +00:00
|
|
|
|
|
|
|
static void MaybeCrashAirplane(Aircraft *v);
|
|
|
|
|
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
|
|
|
|
*/
|
2009-05-22 20:03:26 +00:00
|
|
|
static bool AircraftController(Aircraft *v)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2007-03-02 12:01:24 +00:00
|
|
|
int count;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-09-16 15:15:41 +00:00
|
|
|
/* NULL if station is invalid */
|
2009-05-22 20:07:26 +00:00
|
|
|
const Station *st = Station::GetIfValid(v->targetairport);
|
2008-12-26 18:01:15 +00:00
|
|
|
/* INVALID_TILE if there is no station */
|
|
|
|
TileIndex tile = INVALID_TILE;
|
2010-08-05 12:03:06 +00:00
|
|
|
Direction rotation = DIR_N;
|
|
|
|
uint size_x = 1, size_y = 1;
|
2008-09-16 15:15:41 +00:00
|
|
|
if (st != NULL) {
|
2010-08-05 12:03:06 +00:00
|
|
|
if (st->airport.tile != INVALID_TILE) {
|
|
|
|
tile = st->airport.tile;
|
|
|
|
rotation = st->airport.rotation;
|
|
|
|
size_x = st->airport.w;
|
|
|
|
size_y = st->airport.h;
|
|
|
|
} else {
|
|
|
|
tile = st->xy;
|
|
|
|
}
|
2008-09-16 15:15:41 +00:00
|
|
|
}
|
|
|
|
/* DUMMY if there is no station or no airport */
|
2010-03-18 21:02:20 +00:00
|
|
|
const AirportFTAClass *afc = tile == INVALID_TILE ? GetAirport(AT_DUMMY) : st->airport.GetFTA();
|
2007-03-29 13:52:34 +00:00
|
|
|
|
2008-12-26 18:01:15 +00:00
|
|
|
/* prevent going to INVALID_TILE if airport is deleted. */
|
2010-02-22 14:17:07 +00:00
|
|
|
if (st == NULL || st->airport.tile == INVALID_TILE) {
|
2007-03-29 13:52:34 +00:00
|
|
|
/* Jump into our "holding pattern" state machine if possible */
|
2009-05-22 20:07:26 +00:00
|
|
|
if (v->pos >= afc->nofelements) {
|
2010-08-05 12:00:09 +00:00
|
|
|
v->pos = v->previous_pos = AircraftGetEntryPoint(v, afc, DIR_N);
|
2009-05-22 20:07:26 +00:00
|
|
|
} else if (v->targetairport != v->current_order.GetDestination()) {
|
2007-12-01 14:04:16 +00:00
|
|
|
/* If not possible, just get out of here fast */
|
2009-05-22 20:07:26 +00:00
|
|
|
v->state = FLYING;
|
2007-12-01 14:04:16 +00:00
|
|
|
UpdateAircraftCache(v);
|
|
|
|
AircraftNextAirportPos_and_Order(v);
|
|
|
|
/* get aircraft back on running altitude */
|
2014-09-21 06:35:34 +00:00
|
|
|
SetAircraftPosition(v, v->x_pos, v->y_pos, GetAircraftFlightLevel(v));
|
2007-12-01 14:04:16 +00:00
|
|
|
return false;
|
|
|
|
}
|
2007-03-29 13:52:34 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-02-23 11:50:43 +00:00
|
|
|
/* get airport moving data */
|
2010-08-05 12:03:06 +00:00
|
|
|
const AirportMovingData amd = RotateAirportMovingData(afc->MovingData(v->pos), rotation, size_x, size_y);
|
2007-03-29 13:52:34 +00:00
|
|
|
|
|
|
|
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 */
|
2010-08-05 12:02:22 +00:00
|
|
|
if (amd.flag & AMED_HELI_RAISE) {
|
2009-05-22 20:03:26 +00:00
|
|
|
Aircraft *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;
|
2010-03-07 15:10:39 +00:00
|
|
|
if (--u->cur_speed == 32) {
|
|
|
|
if (!PlayVehicleSound(v, VSE_START)) {
|
|
|
|
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;
|
2014-09-21 06:35:34 +00:00
|
|
|
|
|
|
|
int z_dest;
|
|
|
|
GetAircraftFlightLevelBounds(v, &z_dest, NULL);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-02-23 11:50:43 +00:00
|
|
|
/* Reached altitude? */
|
2010-11-08 21:22:21 +00:00
|
|
|
if (v->z_pos >= z_dest) {
|
2004-08-09 17:04:08 +00:00
|
|
|
v->cur_speed = 0;
|
|
|
|
return true;
|
|
|
|
}
|
2010-11-08 21:22:21 +00:00
|
|
|
SetAircraftPosition(v, v->x_pos, v->y_pos, min(v->z_pos + count, z_dest));
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-02-23 11:50:43 +00:00
|
|
|
/* Helicopter landing. */
|
2010-08-05 12:02:22 +00:00
|
|
|
if (amd.flag & AMED_HELI_LOWER) {
|
2008-09-16 15:15:41 +00:00
|
|
|
if (st == NULL) {
|
2008-03-05 18:33:31 +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 */
|
2009-05-22 20:07:26 +00:00
|
|
|
v->state = FLYING;
|
2008-03-05 18:33:31 +00:00
|
|
|
UpdateAircraftCache(v);
|
|
|
|
AircraftNextAirportPos_and_Order(v);
|
|
|
|
return false;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-03-05 18:33:31 +00:00
|
|
|
/* Vehicle is now at the airport. */
|
2008-09-16 15:15:41 +00:00
|
|
|
v->tile = tile;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-03-05 18:33:31 +00:00
|
|
|
/* Find altitude of landing position. */
|
2011-11-04 10:18:13 +00:00
|
|
|
int z = GetSlopePixelZ(x, y) + 1 + afc->delta_z;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-03-05 18:33:31 +00:00
|
|
|
if (z == v->z_pos) {
|
|
|
|
Vehicle *u = v->Next()->Next();
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-03-05 18:33:31 +00:00
|
|
|
/* Increase speed of rotors. When speed is 80, we've landed. */
|
|
|
|
if (u->cur_speed >= 80) return true;
|
|
|
|
u->cur_speed += 4;
|
|
|
|
} else {
|
|
|
|
count = UpdateAircraftSpeed(v);
|
|
|
|
if (count > 0) {
|
|
|
|
if (v->z_pos > z) {
|
|
|
|
SetAircraftPosition(v, v->x_pos, v->y_pos, max(v->z_pos - count, z));
|
|
|
|
} else {
|
|
|
|
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. */
|
2010-08-05 12:02:22 +00:00
|
|
|
uint dist = abs(x + amd.x - v->x_pos) + abs(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? */
|
2010-08-05 12:02:22 +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. */
|
2010-08-05 12:02:22 +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;
|
|
|
|
}
|
|
|
|
|
2010-08-05 12:02:22 +00:00
|
|
|
if (amd.flag & AMED_BRAKE && v->cur_speed > SPEED_LIMIT_TAXI * _settings_game.vehicle.plane_speed) {
|
2010-01-28 21:59:18 +00:00
|
|
|
MaybeCrashAirplane(v);
|
|
|
|
if ((v->vehstatus & VS_CRASHED) != 0) return false;
|
|
|
|
}
|
|
|
|
|
2007-03-02 12:01:24 +00:00
|
|
|
uint speed_limit = SPEED_LIMIT_TAXI;
|
|
|
|
bool hard_limit = true;
|
|
|
|
|
2010-08-05 12:02:22 +00:00
|
|
|
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; }
|
2007-03-02 12:01:24 +00:00
|
|
|
|
|
|
|
count = UpdateAircraftSpeed(v, speed_limit, hard_limit);
|
|
|
|
if (count == 0) return false;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2010-01-09 14:43:08 +00:00
|
|
|
if (v->turn_counter != 0) v->turn_counter--;
|
2009-09-03 12:11:31 +00:00
|
|
|
|
2007-03-02 12:01:24 +00:00
|
|
|
do {
|
2007-03-18 21:44:00 +00:00
|
|
|
|
|
|
|
GetNewVehiclePosResult gp;
|
|
|
|
|
2010-08-05 12:02:22 +00:00
|
|
|
if (dist < 4 || (amd.flag & AMED_LAND)) {
|
2007-03-18 21:44:00 +00:00
|
|
|
/* move vehicle one pixel towards target */
|
2010-08-05 12:02:22 +00:00
|
|
|
gp.x = (v->x_pos != (x + amd.x)) ?
|
|
|
|
v->x_pos + ((x + amd.x > v->x_pos) ? 1 : -1) :
|
2007-03-18 21:44:00 +00:00
|
|
|
v->x_pos;
|
2010-08-05 12:02:22 +00:00
|
|
|
gp.y = (v->y_pos != (y + amd.y)) ?
|
|
|
|
v->y_pos + ((y + amd.y > v->y_pos) ? 1 : -1) :
|
2007-03-18 21:44:00 +00:00
|
|
|
v->y_pos;
|
|
|
|
|
2010-02-22 14:17:07 +00:00
|
|
|
/* Oilrigs must keep v->tile as st->airport.tile, since the landing pad is in a non-airport tile */
|
2010-03-18 21:02:20 +00:00
|
|
|
gp.new_tile = (st->airport.type == AT_OILRIG) ? st->airport.tile : TileVirtXY(gp.x, gp.y);
|
2007-03-18 21:44:00 +00:00
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
/* Turn. Do it slowly if in the air. */
|
2010-08-05 12:02:22 +00:00
|
|
|
Direction newdir = GetDirectionTowards(v, x + amd.x, y + amd.y);
|
2007-03-18 21:44:00 +00:00
|
|
|
if (newdir != v->direction) {
|
2010-08-05 12:02:22 +00:00
|
|
|
if (amd.flag & AMED_SLOWTURN && v->number_consecutive_turns < 8 && v->subtype == AIR_AIRCRAFT) {
|
2010-01-09 14:43:08 +00:00
|
|
|
if (v->turn_counter == 0 || newdir == v->last_direction) {
|
2009-09-09 00:03:35 +00:00
|
|
|
if (newdir == v->last_direction) {
|
|
|
|
v->number_consecutive_turns = 0;
|
|
|
|
} else {
|
|
|
|
v->number_consecutive_turns++;
|
|
|
|
}
|
2010-01-09 14:43:08 +00:00
|
|
|
v->turn_counter = 2 * _settings_game.vehicle.plane_speed;
|
2009-09-03 12:11:31 +00:00
|
|
|
v->last_direction = v->direction;
|
|
|
|
v->direction = newdir;
|
|
|
|
}
|
2009-09-04 20:31:47 +00:00
|
|
|
|
|
|
|
/* Move vehicle. */
|
|
|
|
gp = GetNewVehiclePos(v);
|
2009-09-03 12:11:31 +00:00
|
|
|
} else {
|
2007-03-18 21:44:00 +00:00
|
|
|
v->cur_speed >>= 1;
|
2009-09-03 12:11:31 +00:00
|
|
|
v->direction = newdir;
|
2009-09-04 20:31:47 +00:00
|
|
|
|
|
|
|
/* When leaving a terminal an aircraft often goes to a position
|
|
|
|
* directly in front of it. If it would move while turning it
|
|
|
|
* would need an two extra turns to end up at the correct position.
|
|
|
|
* To make it easier just disallow all moving while turning as
|
|
|
|
* long as an aircraft is on the ground. */
|
|
|
|
gp.x = v->x_pos;
|
|
|
|
gp.y = v->y_pos;
|
|
|
|
gp.new_tile = gp.old_tile = v->tile;
|
2007-03-18 21:44:00 +00:00
|
|
|
}
|
2009-09-04 20:31:47 +00:00
|
|
|
} else {
|
2009-09-09 00:03:35 +00:00
|
|
|
v->number_consecutive_turns = 0;
|
2009-09-04 20:31:47 +00:00
|
|
|
/* Move vehicle. */
|
|
|
|
gp = GetNewVehiclePos(v);
|
2007-03-02 12:01:24 +00:00
|
|
|
}
|
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. */
|
2010-08-05 12:02:22 +00:00
|
|
|
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? */
|
2011-11-04 11:52:19 +00:00
|
|
|
int z = v->z_pos;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2010-08-05 12:02:22 +00:00
|
|
|
if (amd.flag & AMED_TAKEOFF) {
|
2014-09-21 06:35:34 +00:00
|
|
|
z = GetAircraftFlightLevel(v, true);
|
|
|
|
} else if (amd.flag & AMED_HOLD) {
|
|
|
|
/* Let the plane drop from normal flight altitude to holding pattern altitude */
|
|
|
|
if (z > GetAircraftHoldMaxAltitude(v)) z--;
|
|
|
|
} else if ((amd.flag & AMED_SLOWTURN) && (amd.flag & AMED_NOSPDCLAMP)) {
|
|
|
|
z = GetAircraftFlightLevel(v);
|
2007-03-02 12:01:24 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2010-08-05 12:02:22 +00:00
|
|
|
if (amd.flag & AMED_LAND) {
|
2010-02-22 14:17:07 +00:00
|
|
|
if (st->airport.tile == INVALID_TILE) {
|
2007-03-02 12:01:24 +00:00
|
|
|
/* Airport has been removed, abort the landing procedure */
|
2009-05-22 20:07:26 +00:00
|
|
|
v->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 */
|
2014-09-21 06:35:34 +00:00
|
|
|
SetAircraftPosition(v, gp.x, gp.y, GetAircraftFlightLevel(v));
|
2007-03-02 12:01:24 +00:00
|
|
|
continue;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2011-11-04 11:52:19 +00:00
|
|
|
int curz = GetSlopePixelZ(x + amd.x, y + amd.y) + 1;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-10-11 12:35:16 +00:00
|
|
|
/* We're not flying below our destination, right? */
|
|
|
|
assert(curz <= z);
|
|
|
|
int t = max(1U, dist - 4);
|
|
|
|
int delta = z - curz;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-10-11 12:35:16 +00:00
|
|
|
/* Only start lowering when we're sufficiently close for a 1:1 glide */
|
|
|
|
if (delta >= t) {
|
2010-04-18 14:56:05 +00:00
|
|
|
z -= CeilDiv(z - curz, t);
|
2007-03-02 12:01:24 +00:00
|
|
|
}
|
2009-10-11 12:35:16 +00:00
|
|
|
if (z < curz) z = curz;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2009-10-11 12:35:16 +00:00
|
|
|
/* We've landed. Decrease speed when we're reaching end of runway. */
|
2010-08-05 12:02:22 +00:00
|
|
|
if (amd.flag & AMED_BRAKE) {
|
2011-11-04 11:52:19 +00:00
|
|
|
int curz = GetSlopePixelZ(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;
|
|
|
|
}
|
|
|
|
|
2010-11-05 16:34:22 +00:00
|
|
|
/**
|
|
|
|
* Handle crashed aircraft \a v.
|
|
|
|
* @param v Crashed aircraft.
|
|
|
|
*/
|
2009-05-22 20:03:26 +00:00
|
|
|
static bool HandleCrashedAircraft(Aircraft *v)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-05-22 20:07:26 +00:00
|
|
|
v->crashed_counter += 3;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-09-16 15:15:41 +00:00
|
|
|
Station *st = GetTargetAirportIfValid(v);
|
2005-01-05 13:15:27 +00:00
|
|
|
|
2007-02-23 11:50:43 +00:00
|
|
|
/* make aircraft crash down to the ground */
|
2009-05-22 20:07:26 +00:00
|
|
|
if (v->crashed_counter < 500 && st == NULL && ((v->crashed_counter % 3) == 0) ) {
|
2013-11-28 19:37:24 +00:00
|
|
|
int z = GetSlopePixelZ(Clamp(v->x_pos, 0, MapMaxX() * TILE_SIZE), Clamp(v->y_pos, 0, MapMaxY() * TILE_SIZE));
|
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) {
|
2009-05-22 20:07:26 +00:00
|
|
|
v->crashed_counter = 500;
|
2005-01-24 22:09:20 +00:00
|
|
|
v->z_pos++;
|
|
|
|
}
|
2005-01-05 13:15:27 +00:00
|
|
|
}
|
|
|
|
|
2009-05-22 20:07:26 +00:00
|
|
|
if (v->crashed_counter < 650) {
|
2007-02-20 06:39:09 +00:00
|
|
|
uint32 r;
|
2009-07-16 10:13:33 +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,
|
2007-10-09 15:48:41 +00:00
|
|
|
GB(r, 0, 4) - 4,
|
|
|
|
GB(r, 4, 4) - 4,
|
2005-07-20 15:29:28 +00:00
|
|
|
GB(r, 8, 4),
|
2005-02-12 15:53:32 +00:00
|
|
|
EV_EXPLOSION_SMALL);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2009-05-22 20:07:26 +00:00
|
|
|
} else if (v->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 */
|
2008-09-16 15:15:41 +00:00
|
|
|
if (st != NULL) {
|
2010-03-18 21:02:20 +00:00
|
|
|
CLRBITS(st->airport.flags, RUNWAY_IN_block);
|
|
|
|
CLRBITS(st->airport.flags, RUNWAY_IN_OUT_block); // commuter airport
|
|
|
|
CLRBITS(st->airport.flags, RUNWAY_IN2_block); // intercontinental
|
2008-09-16 15:15:41 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-12-26 21:16:15 +00:00
|
|
|
delete v;
|
2009-05-22 13:53:14 +00:00
|
|
|
|
|
|
|
return false;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2009-05-22 13:53:14 +00:00
|
|
|
|
|
|
|
return true;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-05-06 13:59:11 +00:00
|
|
|
/**
|
|
|
|
* Handle smoke of broken aircraft.
|
|
|
|
* @param v Aircraft
|
|
|
|
* @param mode Is this the non-first call for this vehicle in this tick?
|
|
|
|
*/
|
|
|
|
static void HandleAircraftSmoke(Aircraft *v, bool mode)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
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
|
|
|
|
2013-05-06 13:59:11 +00:00
|
|
|
/* Stop smoking when landed */
|
2004-08-09 17:04:08 +00:00
|
|
|
if (v->cur_speed < 10) {
|
|
|
|
v->vehstatus &= ~VS_AIRCRAFT_BROKEN;
|
|
|
|
v->breakdown_ctr = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-05-06 13:59:11 +00:00
|
|
|
/* Spawn effect et most once per Tick, i.e. !mode */
|
|
|
|
if (!mode && (v->tick_counter & 0x0F) == 0) {
|
2004-08-09 17:04:08 +00:00
|
|
|
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,
|
2011-05-28 09:45:12 +00:00
|
|
|
EV_BREAKDOWN_SMOKE_AIRCRAFT
|
2004-08-09 17:04:08 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-22 20:03:26 +00:00
|
|
|
void HandleMissingAircraftOrders(Aircraft *v)
|
2008-04-05 12:01:34 +00:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2008-09-16 15:15:41 +00:00
|
|
|
const Station *st = GetTargetAirportIfValid(v);
|
|
|
|
if (st == NULL) {
|
2010-06-05 12:16:12 +00:00
|
|
|
Backup<CompanyByte> cur_company(_current_company, v->owner, FILE_LINE);
|
2010-09-08 18:55:58 +00:00
|
|
|
CommandCost ret = DoCommand(v->tile, v->index, 0, DC_EXEC, CMD_SEND_VEHICLE_TO_DEPOT);
|
2010-05-31 20:22:57 +00:00
|
|
|
cur_company.Restore();
|
2007-02-27 16:18:31 +00:00
|
|
|
|
2010-01-18 22:57:21 +00:00
|
|
|
if (ret.Failed()) CrashAirplane(v);
|
2008-04-05 23:36:54 +00:00
|
|
|
} else if (!v->current_order.IsType(OT_GOTO_DEPOT)) {
|
2008-04-05 12:01:34 +00:00
|
|
|
v->current_order.Free();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2008-04-05 12:01:34 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
|
2008-04-05 12:01:34 +00:00
|
|
|
TileIndex Aircraft::GetOrderStationLocation(StationID station)
|
|
|
|
{
|
|
|
|
/* Orders are changed in flight, ensure going to the right station. */
|
2009-05-22 20:07:26 +00:00
|
|
|
if (this->state == FLYING) {
|
2008-04-05 12:01:34 +00:00
|
|
|
AircraftNextAirportPos_and_Order(this);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2008-04-05 12:01:34 +00:00
|
|
|
/* Aircraft do not use dest-tile */
|
|
|
|
return 0;
|
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
|
|
|
{
|
2013-08-04 14:02:27 +00:00
|
|
|
this->colourmap = PAL_NONE;
|
|
|
|
this->UpdateViewport(true, false);
|
2016-10-16 14:57:56 +00:00
|
|
|
if (this->subtype == AIR_HELICOPTER) {
|
|
|
|
GetRotorImage(this, EIT_ON_MAP, &this->Next()->Next()->sprite_seq);
|
|
|
|
}
|
2006-05-02 19:24:02 +00:00
|
|
|
}
|
|
|
|
|
2009-12-04 20:29:46 +00:00
|
|
|
|
|
|
|
uint Aircraft::Crash(bool flooded)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-12-04 20:29:46 +00:00
|
|
|
uint pass = Vehicle::Crash(flooded) + 2; // pilots
|
|
|
|
this->crashed_counter = flooded ? 9000 : 0; // max 10000, disappear pretty fast when flooded
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-12-04 20:29:46 +00:00
|
|
|
return pass;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2010-11-05 16:34:22 +00:00
|
|
|
/**
|
|
|
|
* Bring the aircraft in a crashed state, create the explosion animation, and create a news item about the crash.
|
|
|
|
* @param v Aircraft that crashed.
|
|
|
|
*/
|
2009-12-04 20:29:46 +00:00
|
|
|
static void CrashAirplane(Aircraft *v)
|
|
|
|
{
|
|
|
|
CreateEffectVehicleRel(v, 4, 4, 8, EV_EXPLOSION_LARGE);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-12-04 20:29:46 +00:00
|
|
|
uint pass = v->Crash();
|
|
|
|
SetDParam(0, pass);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2013-02-17 14:10:15 +00:00
|
|
|
v->cargo.Truncate();
|
|
|
|
v->Next()->cargo.Truncate();
|
2008-09-16 15:15:41 +00:00
|
|
|
const Station *st = GetTargetAirportIfValid(v);
|
2007-02-20 06:39:09 +00:00
|
|
|
StringID newsitem;
|
2008-09-16 15:15:41 +00:00
|
|
|
if (st == NULL) {
|
2009-08-04 18:04:33 +00:00
|
|
|
newsitem = STR_NEWS_PLANE_CRASH_OUT_OF_FUEL;
|
2005-01-05 13:15:27 +00:00
|
|
|
} else {
|
|
|
|
SetDParam(1, st->index);
|
2009-04-21 23:40:56 +00:00
|
|
|
newsitem = STR_NEWS_AIRCRAFT_CRASH;
|
2005-01-05 13:15:27 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
AI::NewEvent(v->owner, new ScriptEventVehicleCrashed(v->index, v->tile, st == NULL ? ScriptEventVehicleCrashed::CRASH_AIRCRAFT_NO_AIRPORT : ScriptEventVehicleCrashed::CRASH_PLANE_LANDING));
|
2011-12-19 20:59:36 +00:00
|
|
|
Game::NewEvent(new ScriptEventVehicleCrashed(v->index, v->tile, st == NULL ? ScriptEventVehicleCrashed::CRASH_AIRCRAFT_NO_AIRPORT : ScriptEventVehicleCrashed::CRASH_PLANE_LANDING));
|
2009-01-12 17:11:45 +00:00
|
|
|
|
2012-05-26 14:16:03 +00:00
|
|
|
AddVehicleNewsItem(newsitem, NT_ACCIDENT, v->index, st != NULL ? st->index : INVALID_STATION);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2010-11-10 22:56:05 +00:00
|
|
|
ModifyStationRatingAround(v->tile, v->owner, -160, 30);
|
2012-12-23 21:09:09 +00:00
|
|
|
if (_settings_client.sound.disaster) SndPlayVehicleFx(SND_12_EXPLOSION, v);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2010-11-05 16:34:22 +00:00
|
|
|
/**
|
|
|
|
* Decide whether aircraft \a v should crash.
|
|
|
|
* @param v Aircraft to test.
|
|
|
|
*/
|
2009-05-22 20:03:26 +00:00
|
|
|
static void MaybeCrashAirplane(Aircraft *v)
|
2005-01-05 13:15:27 +00:00
|
|
|
{
|
2010-01-28 22:34:14 +00:00
|
|
|
if (_settings_game.vehicle.plane_crashes == 0) return;
|
|
|
|
|
2009-05-22 20:07:26 +00:00
|
|
|
Station *st = Station::Get(v->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 */
|
2010-01-29 17:56:36 +00:00
|
|
|
uint32 prob = (0x4000 << _settings_game.vehicle.plane_crashes);
|
2010-03-18 21:02:20 +00:00
|
|
|
if ((st->airport.GetFTA()->flags & AirportFTAClass::SHORT_STRIP) &&
|
2009-06-01 11:43:36 +00:00
|
|
|
(AircraftVehInfo(v->engine_type)->subtype & AIR_FAST) &&
|
2007-02-15 20:35:45 +00:00
|
|
|
!_cheats.no_jetcrash.value) {
|
2010-01-28 22:34:14 +00:00
|
|
|
prob /= 20;
|
|
|
|
} else {
|
|
|
|
prob /= 1500;
|
2005-01-05 13:15:27 +00:00
|
|
|
}
|
|
|
|
|
2010-01-28 21:59:18 +00:00
|
|
|
if (GB(Random(), 0, 22) > 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;
|
2013-02-17 14:10:15 +00:00
|
|
|
st->goods[i].cargo.Truncate();
|
2005-01-05 13:15:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CrashAirplane(v);
|
|
|
|
}
|
|
|
|
|
2010-11-05 16:34:22 +00:00
|
|
|
/**
|
|
|
|
* Aircraft arrives at a terminal. If it is the first aircraft, throw a party.
|
|
|
|
* Start loading cargo.
|
|
|
|
* @param v Aircraft that arrived.
|
|
|
|
*/
|
2009-05-22 20:03:26 +00:00
|
|
|
static void AircraftEntersTerminal(Aircraft *v)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2008-04-05 23:36:54 +00:00
|
|
|
if (v->current_order.IsType(OT_GOTO_DEPOT)) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-05-22 20:07:26 +00:00
|
|
|
Station *st = Station::Get(v->targetairport);
|
|
|
|
v->last_station_visited = v->targetairport;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
/* Check if station was ever visited before */
|
|
|
|
if (!(st->had_vehicle_of_type & HVOT_AIRCRAFT)) {
|
|
|
|
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 */
|
2009-05-24 16:52:42 +00:00
|
|
|
AddVehicleNewsItem(
|
2009-04-21 23:40:56 +00:00
|
|
|
STR_NEWS_FIRST_AIRCRAFT_ARRIVAL,
|
2012-05-26 14:16:03 +00:00
|
|
|
(v->owner == _local_company) ? NT_ARRIVAL_COMPANY : NT_ARRIVAL_OTHER,
|
2004-08-09 17:04:08 +00:00
|
|
|
v->index,
|
2008-09-13 10:19:51 +00:00
|
|
|
st->index
|
|
|
|
);
|
2011-11-29 23:15:35 +00:00
|
|
|
AI::NewEvent(v->owner, new ScriptEventStationFirstVehicle(st->index, v->index));
|
2011-12-19 20:59:36 +00:00
|
|
|
Game::NewEvent(new ScriptEventStationFirstVehicle(st->index, v->index));
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-01-13 18:55:54 +00:00
|
|
|
v->BeginLoading();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2010-11-05 16:34:22 +00:00
|
|
|
/**
|
|
|
|
* Aircraft touched down at the landing strip.
|
|
|
|
* @param v Aircraft that landed.
|
|
|
|
*/
|
2009-05-22 20:03:26 +00:00
|
|
|
static void AircraftLandAirplane(Aircraft *v)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2018-05-22 17:43:34 +00:00
|
|
|
v->UpdateDeltaXY();
|
2007-05-01 16:35:14 +00:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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 */
|
2009-05-22 20:03:26 +00:00
|
|
|
void AircraftNextAirportPos_and_Order(Aircraft *v)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2008-09-16 15:15:41 +00:00
|
|
|
if (v->current_order.IsType(OT_GOTO_STATION) || v->current_order.IsType(OT_GOTO_DEPOT)) {
|
2009-05-22 20:07:26 +00:00
|
|
|
v->targetairport = v->current_order.GetDestination();
|
2008-09-16 15:15:41 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-09-16 15:15:41 +00:00
|
|
|
const Station *st = GetTargetAirportIfValid(v);
|
2010-03-18 21:02:20 +00:00
|
|
|
const AirportFTAClass *apc = st == NULL ? GetAirport(AT_DUMMY) : st->airport.GetFTA();
|
2010-08-05 12:00:09 +00:00
|
|
|
Direction rotation = st == NULL ? DIR_N : st->airport.rotation;
|
|
|
|
v->pos = v->previous_pos = AircraftGetEntryPoint(v, apc, rotation);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2011-08-13 12:47:18 +00:00
|
|
|
/**
|
|
|
|
* Aircraft is about to leave the hangar.
|
|
|
|
* @param v Aircraft leaving.
|
2011-09-09 21:27:57 +00:00
|
|
|
* @param exit_dir The direction the vehicle leaves the hangar.
|
|
|
|
* @note This function is called in AfterLoadGame for old savegames, so don't rely
|
|
|
|
* on any data to be valid, especially don't rely on the fact that the vehicle
|
|
|
|
* is actually on the ground inside a depot.
|
2011-08-13 12:47:18 +00:00
|
|
|
*/
|
2011-09-09 21:27:57 +00:00
|
|
|
void AircraftLeaveHangar(Aircraft *v, Direction exit_dir)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
v->cur_speed = 0;
|
|
|
|
v->subspeed = 0;
|
|
|
|
v->progress = 0;
|
2011-09-09 21:27:57 +00:00
|
|
|
v->direction = exit_dir;
|
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);
|
2009-09-13 19:15:59 +00:00
|
|
|
SetWindowClassesDirty(WC_AIRCRAFT_LIST);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
/////////////////// AIRCRAFT MOVEMENT SCHEME ////////////////////////////////
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2009-05-22 20:03:26 +00:00
|
|
|
static void AircraftEventHandler_EnterTerminal(Aircraft *v, const AirportFTAClass *apc)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
AircraftEntersTerminal(v);
|
2009-05-22 20:07:26 +00:00
|
|
|
v->state = apc->layout[v->pos].heading;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2010-11-05 16:34:22 +00:00
|
|
|
/**
|
|
|
|
* Aircraft arrived in an airport hangar.
|
|
|
|
* @param v Aircraft in the hangar.
|
|
|
|
* @param apc Airport description containing the hangar.
|
|
|
|
*/
|
2009-05-22 20:03:26 +00:00
|
|
|
static void AircraftEventHandler_EnterHangar(Aircraft *v, const AirportFTAClass *apc)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2006-10-04 12:01:59 +00:00
|
|
|
VehicleEnterDepot(v);
|
2009-05-22 20:07:26 +00:00
|
|
|
v->state = apc->layout[v->pos].heading;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2010-11-05 16:34:22 +00:00
|
|
|
/**
|
|
|
|
* Handle aircraft movement/decision making in an airport hangar.
|
|
|
|
* @param v Aircraft in the hangar.
|
|
|
|
* @param apc Airport description containing the hangar.
|
|
|
|
*/
|
2009-05-22 20:03:26 +00:00
|
|
|
static void AircraftEventHandler_InHangar(Aircraft *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 */
|
2009-05-22 20:07:26 +00:00
|
|
|
if (v->previous_pos != v->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 */
|
2008-04-05 23:36:54 +00:00
|
|
|
if (v->current_order.IsType(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;
|
|
|
|
}
|
|
|
|
|
2008-04-05 23:36:54 +00:00
|
|
|
if (!v->current_order.IsType(OT_GOTO_STATION) &&
|
|
|
|
!v->current_order.IsType(OT_GOTO_DEPOT))
|
2004-08-09 17:04:08 +00:00
|
|
|
return;
|
|
|
|
|
2011-04-10 10:47:21 +00:00
|
|
|
/* We are leaving a hangar, but have to go to the exact same one; re-enter */
|
|
|
|
if (v->current_order.IsType(OT_GOTO_DEPOT) && v->current_order.GetDestination() == v->targetairport) {
|
|
|
|
VehicleEnterDepot(v);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-02-23 11:50:43 +00:00
|
|
|
/* if the block of the next position is busy, stay put */
|
2009-05-22 20:07:26 +00:00
|
|
|
if (AirportHasBlock(v, &apc->layout[v->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 */
|
2009-05-22 20:07:26 +00:00
|
|
|
if (v->current_order.GetDestination() == v->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 */
|
2009-05-22 20:07:26 +00:00
|
|
|
v->state = (v->subtype == AIR_HELICOPTER) ? HELITAKEOFF : TAKEOFF;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2011-09-09 21:27:57 +00:00
|
|
|
const Station *st = Station::GetByTile(v->tile);
|
|
|
|
AircraftLeaveHangar(v, st->airport.GetHangarExitDirection(v->tile));
|
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 */
|
2009-05-22 20:03:26 +00:00
|
|
|
static void AircraftEventHandler_AtTerminal(Aircraft *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 */
|
2009-05-22 20:07:26 +00:00
|
|
|
if (v->previous_pos != v->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
|
2009-02-08 12:25:13 +00:00
|
|
|
* and get serviced at the same time - setting */
|
2008-05-29 15:13:28 +00:00
|
|
|
if (_settings_game.order.serviceathelipad) {
|
2010-08-13 00:36:12 +00:00
|
|
|
if (v->subtype == AIR_HELICOPTER && apc->num_helipads > 0) {
|
2013-01-08 22:46:42 +00:00
|
|
|
/* an excerpt 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;
|
2011-11-01 00:21:08 +00:00
|
|
|
v->reliability = v->GetEngine()->reliability;
|
2009-09-13 19:15:59 +00:00
|
|
|
SetWindowDirty(WC_VEHICLE_DETAILS, v->index);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-05-22 15:13:50 +00:00
|
|
|
if (v->current_order.IsType(OT_NOTHING)) 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 */
|
2009-05-22 20:07:26 +00:00
|
|
|
if (AirportHasBlock(v, &apc->layout[v->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
|
|
|
|
2008-09-13 11:00:30 +00:00
|
|
|
bool go_to_hangar = false;
|
2008-04-05 23:36:54 +00:00
|
|
|
switch (v->current_order.GetType()) {
|
2004-08-09 17:04:08 +00:00
|
|
|
case OT_GOTO_STATION: // ready to fly to another airport
|
|
|
|
break;
|
2013-01-08 22:46:42 +00:00
|
|
|
case OT_GOTO_DEPOT: // visit hangar for servicing, sale, etc.
|
2009-05-22 20:07:26 +00:00
|
|
|
go_to_hangar = v->current_order.GetDestination() == v->targetairport;
|
2004-08-09 17:04:08 +00:00
|
|
|
break;
|
2008-07-22 23:29:08 +00:00
|
|
|
case OT_CONDITIONAL:
|
|
|
|
/* In case of a conditional order we just have to wait a tick
|
|
|
|
* longer, so the conditional order can actually be processed;
|
|
|
|
* we should not clear the order as that makes us go nowhere. */
|
|
|
|
return;
|
2004-08-09 17:04:08 +00:00
|
|
|
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();
|
2010-03-19 09:48:44 +00:00
|
|
|
go_to_hangar = Station::Get(v->targetairport)->airport.HasHangar();
|
2008-09-13 11:00:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (go_to_hangar) {
|
2009-05-22 20:07:26 +00:00
|
|
|
v->state = HANGAR;
|
2008-09-13 11:00:30 +00:00
|
|
|
} else {
|
|
|
|
/* airplane goto state takeoff, helicopter to helitakeoff */
|
2009-05-22 20:07:26 +00:00
|
|
|
v->state = (v->subtype == AIR_HELICOPTER) ? HELITAKEOFF : TAKEOFF;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2006-10-13 23:08:55 +00:00
|
|
|
AirportMove(v, apc);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2009-05-22 20:03:26 +00:00
|
|
|
static void AircraftEventHandler_General(Aircraft *v, const AirportFTAClass *apc)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-05-13 16:52:51 +00:00
|
|
|
error("OK, you shouldn't be here, check your Airport Scheme!");
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2009-05-22 20:03:26 +00:00
|
|
|
static void AircraftEventHandler_TakeOff(Aircraft *v, const AirportFTAClass *apc)
|
2007-02-20 06:39:09 +00:00
|
|
|
{
|
2004-08-09 17:04:08 +00:00
|
|
|
PlayAircraftSound(v); // play takeoffsound for airplanes
|
2009-05-22 20:07:26 +00:00
|
|
|
v->state = STARTTAKEOFF;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2009-05-22 20:03:26 +00:00
|
|
|
static void AircraftEventHandler_StartTakeOff(Aircraft *v, const AirportFTAClass *apc)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-05-22 20:07:26 +00:00
|
|
|
v->state = ENDTAKEOFF;
|
2018-05-22 17:43:34 +00:00
|
|
|
v->UpdateDeltaXY();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2009-05-22 20:03:26 +00:00
|
|
|
static void AircraftEventHandler_EndTakeOff(Aircraft *v, const AirportFTAClass *apc)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-05-22 20:07:26 +00:00
|
|
|
v->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);
|
|
|
|
}
|
|
|
|
|
2009-05-22 20:03:26 +00:00
|
|
|
static void AircraftEventHandler_HeliTakeOff(Aircraft *v, const AirportFTAClass *apc)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-05-22 20:07:26 +00:00
|
|
|
v->state = FLYING;
|
2018-05-22 17:43:34 +00:00
|
|
|
v->UpdateDeltaXY();
|
2007-05-01 16:35:14 +00:00
|
|
|
|
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
|
|
|
|
2008-01-16 13:59:08 +00:00
|
|
|
/* Send the helicopter to a hangar if needed for replacement */
|
2009-12-19 22:04:02 +00:00
|
|
|
if (v->NeedsAutomaticServicing()) {
|
2010-06-05 12:16:12 +00:00
|
|
|
Backup<CompanyByte> cur_company(_current_company, v->owner, FILE_LINE);
|
2010-09-08 21:02:12 +00:00
|
|
|
DoCommand(v->tile, v->index | DEPOT_SERVICE | DEPOT_LOCATE_HANGAR, 0, DC_EXEC, CMD_SEND_VEHICLE_TO_DEPOT);
|
2010-05-31 20:22:57 +00:00
|
|
|
cur_company.Restore();
|
2005-01-24 10:50:57 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2009-05-22 20:03:26 +00:00
|
|
|
static void AircraftEventHandler_Flying(Aircraft *v, const AirportFTAClass *apc)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-05-22 20:07:26 +00:00
|
|
|
Station *st = Station::Get(v->targetairport);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2012-04-17 19:43:18 +00:00
|
|
|
/* Runway busy, not allowed to use this airstation or closed, circle. */
|
|
|
|
if (CanVehicleUseStation(v, st) && (st->owner == OWNER_NONE || st->owner == v->owner) && !(st->airport.flags & AIRPORT_CLOSED_block)) {
|
2009-03-15 00:32:18 +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;
|
2009-05-22 20:07:26 +00:00
|
|
|
const AirportFTA *current = apc->layout[v->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)) {
|
2009-05-22 20:07:26 +00:00
|
|
|
v->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! */
|
2009-05-22 20:07:26 +00:00
|
|
|
v->pos = current->next_position;
|
2010-03-18 21:02:20 +00:00
|
|
|
SETBITS(st->airport.flags, apc->layout[v->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
|
|
|
}
|
|
|
|
}
|
2009-05-22 20:07:26 +00:00
|
|
|
v->state = FLYING;
|
|
|
|
v->pos = apc->layout[v->pos].next_position;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2009-05-22 20:03:26 +00:00
|
|
|
static void AircraftEventHandler_Landing(Aircraft *v, const AirportFTAClass *apc)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-05-22 20:07:26 +00:00
|
|
|
v->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 */
|
2009-12-19 22:04:02 +00:00
|
|
|
if (v->NeedsAutomaticServicing()) {
|
2010-06-05 12:16:12 +00:00
|
|
|
Backup<CompanyByte> cur_company(_current_company, v->owner, FILE_LINE);
|
2010-09-08 21:02:12 +00:00
|
|
|
DoCommand(v->tile, v->index | DEPOT_SERVICE, 0, DC_EXEC, CMD_SEND_VEHICLE_TO_DEPOT);
|
2010-05-31 20:22:57 +00:00
|
|
|
cur_company.Restore();
|
2005-01-27 20:38:19 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2009-05-22 20:03:26 +00:00
|
|
|
static void AircraftEventHandler_HeliLanding(Aircraft *v, const AirportFTAClass *apc)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-05-22 20:07:26 +00:00
|
|
|
v->state = HELIENDLANDING;
|
2018-05-22 17:43:34 +00:00
|
|
|
v->UpdateDeltaXY();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2009-05-22 20:03:26 +00:00
|
|
|
static void AircraftEventHandler_EndLanding(Aircraft *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 */
|
2009-05-22 20:07:26 +00:00
|
|
|
if (AirportHasBlock(v, &apc->layout[v->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. */
|
2008-04-05 23:36:54 +00:00
|
|
|
if (v->current_order.IsType(OT_GOTO_STATION)) {
|
2006-10-13 23:08:55 +00:00
|
|
|
if (AirportFindFreeTerminal(v, apc)) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2009-05-22 20:07:26 +00:00
|
|
|
v->state = HANGAR;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2009-05-22 20:03:26 +00:00
|
|
|
static void AircraftEventHandler_HeliEndLanding(Aircraft *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 */
|
2009-05-22 20:07:26 +00:00
|
|
|
if (AirportHasBlock(v, &apc->layout[v->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. */
|
2008-04-05 23:36:54 +00:00
|
|
|
if (v->current_order.IsType(OT_GOTO_STATION)) {
|
2006-10-13 23:08:55 +00:00
|
|
|
if (AirportFindFreeHelipad(v, apc)) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2010-03-19 09:48:44 +00:00
|
|
|
v->state = Station::Get(v->targetairport)->airport.HasHangar() ? HANGAR : HELITAKEOFF;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2010-11-05 16:34:22 +00:00
|
|
|
/**
|
|
|
|
* Signature of the aircraft handler function.
|
|
|
|
* @param v Aircraft to handle.
|
|
|
|
* @param apc Airport state machine.
|
|
|
|
*/
|
2009-05-22 20:03:26 +00:00
|
|
|
typedef void AircraftStateHandler(Aircraft *v, const AirportFTAClass *apc);
|
2010-11-05 16:34:22 +00:00
|
|
|
/** Array of handler functions for each target of the aircraft. */
|
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
|
2004-08-09 17:04:08 +00:00
|
|
|
};
|
|
|
|
|
2009-05-22 20:03:26 +00:00
|
|
|
static void AirportClearBlock(const Aircraft *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 */
|
2009-05-22 20:07:26 +00:00
|
|
|
if (apc->layout[v->previous_pos].block != apc->layout[v->pos].block) {
|
|
|
|
Station *st = Station::Get(v->targetairport);
|
2005-11-14 19:48:04 +00:00
|
|
|
|
2010-03-18 21:02:20 +00:00
|
|
|
CLRBITS(st->airport.flags, apc->layout[v->previous_pos].block);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-22 20:03:26 +00:00
|
|
|
static void AirportGoToNextPosition(Aircraft *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
|
|
|
|
2010-03-18 21:02:20 +00:00
|
|
|
const AirportFTAClass *apc = Station::Get(v->targetairport)->airport.GetFTA();
|
2007-02-21 19:49:18 +00:00
|
|
|
|
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 */
|
2009-05-22 20:03:26 +00:00
|
|
|
static bool AirportMove(Aircraft *v, const AirportFTAClass *apc)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2007-02-23 11:50:43 +00:00
|
|
|
/* error handling */
|
2009-05-22 20:07:26 +00:00
|
|
|
if (v->pos >= apc->nofelements) {
|
|
|
|
DEBUG(misc, 0, "[Ap] position %d is not valid for current airport. Max position is %d", v->pos, apc->nofelements-1);
|
|
|
|
assert(v->pos < apc->nofelements);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2009-05-22 20:07:26 +00:00
|
|
|
const AirportFTA *current = &apc->layout[v->pos];
|
2007-02-23 11:50:43 +00:00
|
|
|
/* we have arrived in an important state (eg terminal, hangar, etc.) */
|
2009-05-22 20:07:26 +00:00
|
|
|
if (current->heading == v->state) {
|
|
|
|
byte prev_pos = v->pos; // location could be changed in state, so save it before-hand
|
|
|
|
byte prev_state = v->state;
|
|
|
|
_aircraft_state_handlers[v->state](v, apc);
|
|
|
|
if (v->state != FLYING) v->previous_pos = prev_pos;
|
|
|
|
if (v->state != prev_state || v->pos != prev_pos) UpdateAircraftCache(v);
|
2004-08-09 17:04:08 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-05-22 20:07:26 +00:00
|
|
|
v->previous_pos = v->pos; // save previous location
|
2004-08-09 17:04:08 +00:00
|
|
|
|
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)) {
|
2009-05-22 20:07:26 +00:00
|
|
|
v->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 {
|
2009-05-22 20:07:26 +00:00
|
|
|
if (v->state == current->heading || current->heading == TO_ALL) {
|
2006-10-13 23:08:55 +00:00
|
|
|
if (AirportSetBlocks(v, current, apc)) {
|
2009-05-22 20:07:26 +00:00
|
|
|
v->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);
|
|
|
|
|
2009-05-22 20:07:26 +00:00
|
|
|
DEBUG(misc, 0, "[Ap] cannot move further on Airport! (pos %d state %d) for vehicle %d", v->pos, v->state, v->index);
|
2009-07-31 13:25:20 +00:00
|
|
|
NOT_REACHED();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2010-11-05 16:34:22 +00:00
|
|
|
/** returns true if the road ahead is busy, eg. you must wait before proceeding. */
|
2009-05-22 20:03:26 +00:00
|
|
|
static bool AirportHasBlock(Aircraft *v, const AirportFTA *current_pos, const AirportFTAClass *apc)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-05-22 20:07:26 +00:00
|
|
|
const AirportFTA *reference = &apc->layout[v->pos];
|
2006-10-14 09:51:04 +00:00
|
|
|
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) {
|
2009-05-22 20:07:26 +00:00
|
|
|
const Station *st = Station::Get(v->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;
|
|
|
|
}
|
|
|
|
|
2010-03-18 21:02:20 +00:00
|
|
|
if (st->airport.flags & airport_flags) {
|
2004-08-09 17:04:08 +00:00
|
|
|
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
|
|
|
|
*/
|
2009-05-22 20:03:26 +00:00
|
|
|
static bool AirportSetBlocks(Aircraft *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];
|
2009-05-22 20:07:26 +00:00
|
|
|
const AirportFTA *reference = &apc->layout[v->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;
|
2011-01-15 15:36:58 +00:00
|
|
|
}
|
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
|
|
|
|
2009-05-22 20:07:26 +00:00
|
|
|
Station *st = Station::Get(v->targetairport);
|
2010-03-18 21:02:20 +00:00
|
|
|
if (st->airport.flags & airport_flags) {
|
2004-08-09 17:04:08 +00:00
|
|
|
v->cur_speed = 0;
|
|
|
|
v->subspeed = 0;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (next->block != NOTHING_block) {
|
2010-03-18 21:02:20 +00:00
|
|
|
SETBITS(st->airport.flags, airport_flags); // occupy next block
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-08-13 00:21:03 +00:00
|
|
|
/**
|
|
|
|
* Combination of aircraft state for going to a certain terminal and the
|
|
|
|
* airport flag for that terminal block.
|
|
|
|
*/
|
|
|
|
struct MovementTerminalMapping {
|
|
|
|
AirportMovementStates state; ///< Aircraft movement state when going to this terminal.
|
|
|
|
uint64 airport_flag; ///< Bitmask in the airport flags that need to be free for this terminal.
|
|
|
|
};
|
|
|
|
|
|
|
|
/** A list of all valid terminals and their associated blocks. */
|
|
|
|
static const MovementTerminalMapping _airport_terminal_mapping[] = {
|
|
|
|
{TERM1, TERM1_block},
|
|
|
|
{TERM2, TERM2_block},
|
|
|
|
{TERM3, TERM3_block},
|
|
|
|
{TERM4, TERM4_block},
|
|
|
|
{TERM5, TERM5_block},
|
|
|
|
{TERM6, TERM6_block},
|
|
|
|
{TERM7, TERM7_block},
|
|
|
|
{TERM8, TERM8_block},
|
|
|
|
{HELIPAD1, HELIPAD1_block},
|
|
|
|
{HELIPAD2, HELIPAD2_block},
|
|
|
|
{HELIPAD3, HELIPAD3_block},
|
|
|
|
};
|
|
|
|
|
2010-11-05 16:34:22 +00:00
|
|
|
/**
|
|
|
|
* Find a free terminal or helipad, and if available, assign it.
|
|
|
|
* @param v Aircraft looking for a free terminal or helipad.
|
|
|
|
* @param i First terminal to examine.
|
|
|
|
* @param last_terminal Terminal number to stop examining.
|
|
|
|
* @return A terminal or helipad has been found, and has been assigned to the aircraft.
|
|
|
|
*/
|
2009-05-22 20:03:26 +00:00
|
|
|
static bool FreeTerminal(Aircraft *v, byte i, byte last_terminal)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2010-08-13 00:21:03 +00:00
|
|
|
assert(last_terminal <= lengthof(_airport_terminal_mapping));
|
2009-05-22 20:07:26 +00:00
|
|
|
Station *st = Station::Get(v->targetairport);
|
2004-08-09 17:04:08 +00:00
|
|
|
for (; i < last_terminal; i++) {
|
2010-08-13 00:21:03 +00:00
|
|
|
if ((st->airport.flags & _airport_terminal_mapping[i].airport_flag) == 0) {
|
2007-02-23 11:50:43 +00:00
|
|
|
/* TERMINAL# HELIPAD# */
|
2010-08-13 00:21:03 +00:00
|
|
|
v->state = _airport_terminal_mapping[i].state; // start moving to that terminal/helipad
|
|
|
|
SETBITS(st->airport.flags, _airport_terminal_mapping[i].airport_flag); // occupy terminal/helipad
|
2004-08-09 17:04:08 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-11-05 16:34:22 +00:00
|
|
|
/**
|
|
|
|
* Get the number of terminals at the airport.
|
2018-10-28 02:17:36 +00:00
|
|
|
* @param apc Airport description.
|
2010-11-05 16:34:22 +00:00
|
|
|
* @return Number of terminals.
|
|
|
|
*/
|
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;
|
|
|
|
}
|
|
|
|
|
2010-11-05 16:34:22 +00:00
|
|
|
/**
|
|
|
|
* Find a free terminal, and assign it if available.
|
|
|
|
* @param v Aircraft to handle.
|
|
|
|
* @param apc Airport state machine.
|
|
|
|
* @return Found a free terminal and assigned it.
|
|
|
|
*/
|
2009-05-22 20:03:26 +00:00
|
|
|
static bool AirportFindFreeTerminal(Aircraft *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) {
|
2009-05-22 20:07:26 +00:00
|
|
|
const Station *st = Station::Get(v->targetairport);
|
|
|
|
const AirportFTA *temp = apc->layout[v->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) {
|
2010-03-18 21:02:20 +00:00
|
|
|
if (!(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
|
|
|
}
|
|
|
|
|
2010-11-05 16:34:22 +00:00
|
|
|
/**
|
|
|
|
* Find a free helipad, and assign it if available.
|
|
|
|
* @param v Aircraft to handle.
|
|
|
|
* @param apc Airport state machine.
|
|
|
|
* @return Found a free helipad and assigned it.
|
|
|
|
*/
|
2009-05-22 20:03:26 +00:00
|
|
|
static bool AirportFindFreeHelipad(Aircraft *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 */
|
2010-08-13 00:36:12 +00:00
|
|
|
if (apc->num_helipads == 0) return AirportFindFreeTerminal(v, apc);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2010-08-13 00:21:03 +00:00
|
|
|
/* only 1 helicoptergroup, check all helipads
|
|
|
|
* The blocks for helipads start after the last terminal (MAX_TERMINALS) */
|
2010-08-13 00:36:12 +00:00
|
|
|
return FreeTerminal(v, MAX_TERMINALS, apc->num_helipads + MAX_TERMINALS);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2011-12-13 00:43:35 +00:00
|
|
|
/**
|
|
|
|
* Handle the 'dest too far' flag and the corresponding news message for aircraft.
|
|
|
|
* @param v The aircraft.
|
|
|
|
* @param too_far True if the current destination is too far away.
|
|
|
|
*/
|
|
|
|
static void AircraftHandleDestTooFar(Aircraft *v, bool too_far)
|
|
|
|
{
|
|
|
|
if (too_far) {
|
|
|
|
if (!HasBit(v->flags, VAF_DEST_TOO_FAR)) {
|
|
|
|
SetBit(v->flags, VAF_DEST_TOO_FAR);
|
2011-12-16 16:58:55 +00:00
|
|
|
SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, WID_VV_START_STOP);
|
2011-12-13 00:43:59 +00:00
|
|
|
AI::NewEvent(v->owner, new ScriptEventAircraftDestTooFar(v->index));
|
2011-12-13 00:43:35 +00:00
|
|
|
if (v->owner == _local_company) {
|
|
|
|
/* Post a news message. */
|
|
|
|
SetDParam(0, v->index);
|
2012-05-26 14:15:52 +00:00
|
|
|
AddVehicleAdviceNewsItem(STR_NEWS_AIRCRAFT_DEST_TOO_FAR, v->index);
|
2011-12-13 00:43:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (HasBit(v->flags, VAF_DEST_TOO_FAR)) {
|
|
|
|
/* Not too far anymore, clear flag and message. */
|
|
|
|
ClrBit(v->flags, VAF_DEST_TOO_FAR);
|
2011-12-16 16:58:55 +00:00
|
|
|
SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, WID_VV_START_STOP);
|
2011-12-13 00:43:35 +00:00
|
|
|
DeleteVehicleNews(v->index, STR_NEWS_AIRCRAFT_DEST_TOO_FAR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-22 20:03:26 +00:00
|
|
|
static bool AircraftEventHandler(Aircraft *v, int loop)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
if (v->vehstatus & VS_CRASHED) {
|
2009-05-22 13:53:14 +00:00
|
|
|
return HandleCrashedAircraft(v);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2009-05-22 13:53:14 +00:00
|
|
|
if (v->vehstatus & VS_STOPPED) return true;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2010-08-28 14:14:37 +00:00
|
|
|
v->HandleBreakdown();
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2013-05-06 13:59:11 +00:00
|
|
|
HandleAircraftSmoke(v, loop != 0);
|
2008-04-05 12:01:34 +00:00
|
|
|
ProcessOrders(v);
|
2007-05-07 16:21:34 +00:00
|
|
|
v->HandleLoading(loop != 0);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-05-22 13:53:14 +00:00
|
|
|
if (v->current_order.IsType(OT_LOADING) || v->current_order.IsType(OT_LEAVESTATION)) return true;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2012-03-25 11:29:33 +00:00
|
|
|
if (v->state >= ENDTAKEOFF && v->state <= HELIENDLANDING) {
|
2011-12-13 00:43:35 +00:00
|
|
|
/* If we are flying, unconditionally clear the 'dest too far' state. */
|
|
|
|
AircraftHandleDestTooFar(v, false);
|
|
|
|
} else if (v->acache.cached_max_range_sqr != 0) {
|
|
|
|
/* Check the distance to the next destination. This code works because the target
|
|
|
|
* airport is only updated after take off and not on the ground. */
|
|
|
|
Station *cur_st = Station::GetIfValid(v->targetairport);
|
|
|
|
Station *next_st = v->current_order.IsType(OT_GOTO_STATION) || v->current_order.IsType(OT_GOTO_DEPOT) ? Station::GetIfValid(v->current_order.GetDestination()) : NULL;
|
|
|
|
|
|
|
|
if (cur_st != NULL && cur_st->airport.tile != INVALID_TILE && next_st != NULL && next_st->airport.tile != INVALID_TILE) {
|
|
|
|
uint dist = DistanceSquare(cur_st->airport.tile, next_st->airport.tile);
|
|
|
|
AircraftHandleDestTooFar(v, dist > v->acache.cached_max_range_sqr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!HasBit(v->flags, VAF_DEST_TOO_FAR)) AirportGoToNextPosition(v);
|
2009-05-22 13:53:14 +00:00
|
|
|
|
|
|
|
return true;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2009-05-22 13:53:14 +00:00
|
|
|
bool Aircraft::Tick()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-07-13 16:37:27 +00:00
|
|
|
if (!this->IsNormalAircraft()) return true;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2018-07-19 19:17:07 +00:00
|
|
|
PerformanceAccumulator framerate(PFE_GL_AIRCRAFT);
|
|
|
|
|
2013-05-06 13:59:11 +00:00
|
|
|
this->tick_counter++;
|
|
|
|
|
2008-02-13 19:24:40 +00:00
|
|
|
if (!(this->vehstatus & VS_STOPPED)) this->running_ticks++;
|
|
|
|
|
2007-07-01 19:24:54 +00:00
|
|
|
if (this->subtype == AIR_HELICOPTER) HelicopterTickHandler(this);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-03-28 18:43:01 +00:00
|
|
|
this->current_order_time++;
|
|
|
|
|
2007-03-02 12:01:24 +00:00
|
|
|
for (uint i = 0; i != 2; i++) {
|
2009-05-22 13:53:14 +00:00
|
|
|
/* stop if the aircraft was deleted */
|
|
|
|
if (!AircraftEventHandler(this, i)) return false;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2009-05-22 13:53:14 +00:00
|
|
|
|
|
|
|
return true;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2005-01-09 08:49:40 +00:00
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* Returns aircraft's target station if v->target_airport
|
2008-09-16 15:15:41 +00:00
|
|
|
* is a valid station with airport.
|
|
|
|
* @param v vehicle to get target airport for
|
|
|
|
* @return pointer to target station, NULL if invalid
|
|
|
|
*/
|
2009-05-22 20:03:26 +00:00
|
|
|
Station *GetTargetAirportIfValid(const Aircraft *v)
|
2008-09-16 15:15:41 +00:00
|
|
|
{
|
|
|
|
assert(v->type == VEH_AIRCRAFT);
|
|
|
|
|
2009-05-22 20:07:26 +00:00
|
|
|
Station *st = Station::GetIfValid(v->targetairport);
|
2009-05-18 16:21:28 +00:00
|
|
|
if (st == NULL) return NULL;
|
2008-09-16 15:15:41 +00:00
|
|
|
|
2010-02-22 14:17:07 +00:00
|
|
|
return st->airport.tile == INVALID_TILE ? NULL : st;
|
2008-09-16 15:15:41 +00:00
|
|
|
}
|
|
|
|
|
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 */
|
2010-03-18 21:02:20 +00:00
|
|
|
const AirportFTAClass *ap = st->airport.GetFTA();
|
2010-08-17 21:50:58 +00:00
|
|
|
Direction rotation = st->airport.tile == INVALID_TILE ? DIR_N : st->airport.rotation;
|
2007-02-20 06:39:09 +00:00
|
|
|
|
2009-05-26 22:10:13 +00:00
|
|
|
Aircraft *v;
|
|
|
|
FOR_ALL_AIRCRAFT(v) {
|
2010-08-17 21:50:58 +00:00
|
|
|
if (!v->IsNormalAircraft() || v->targetairport != st->index) continue;
|
|
|
|
assert(v->state == FLYING);
|
|
|
|
v->pos = v->previous_pos = AircraftGetEntryPoint(v, ap, rotation);
|
|
|
|
UpdateAircraftCache(v);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|