2005-07-24 14:12:37 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
#include "stdafx.h"
|
2005-06-02 19:30:21 +00:00
|
|
|
#include "openttd.h"
|
2005-02-05 15:58:59 +00:00
|
|
|
#include "debug.h"
|
2005-07-22 07:02:20 +00:00
|
|
|
#include "functions.h"
|
2004-11-25 10:47:30 +00:00
|
|
|
#include "table/strings.h"
|
2004-12-15 22:18:54 +00:00
|
|
|
#include "map.h"
|
2005-01-29 12:19:05 +00:00
|
|
|
#include "tile.h"
|
2004-08-09 17:04:08 +00:00
|
|
|
#include "vehicle.h"
|
2005-05-11 16:17:03 +00:00
|
|
|
#include "depot.h"
|
2004-08-09 17:04:08 +00:00
|
|
|
#include "engine.h"
|
|
|
|
#include "command.h"
|
|
|
|
#include "station.h"
|
|
|
|
#include "news.h"
|
2004-11-05 23:12:33 +00:00
|
|
|
#include "sound.h"
|
2004-08-09 17:04:08 +00:00
|
|
|
#include "player.h"
|
|
|
|
#include "airport.h"
|
2005-05-02 23:59:11 +00:00
|
|
|
#include "vehicle_gui.h"
|
2005-10-15 11:06:54 +00:00
|
|
|
#include "table/sprites.h"
|
2006-02-03 12:55:21 +00:00
|
|
|
#include "newgrf_engine.h"
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
static bool AirportMove(Vehicle *v, const AirportFTAClass *Airport);
|
|
|
|
static bool AirportSetBlocks(Vehicle *v, AirportFTA *current_pos, const AirportFTAClass *Airport);
|
|
|
|
static bool AirportHasBlock(Vehicle *v, AirportFTA *current_pos, const AirportFTAClass *Airport);
|
|
|
|
static bool AirportFindFreeTerminal(Vehicle *v, const AirportFTAClass *Airport);
|
|
|
|
static bool AirportFindFreeHelipad(Vehicle *v, const AirportFTAClass *Airport);
|
|
|
|
static void AirportGoToNextPosition(Vehicle *v, const AirportFTAClass *Airport);
|
2005-01-05 13:15:27 +00:00
|
|
|
static void CrashAirplane(Vehicle *v);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
static void AircraftNextAirportPos_and_Order(Vehicle *v);
|
|
|
|
static byte GetAircraftFlyingAltitude(const Vehicle *v);
|
|
|
|
|
|
|
|
static const SpriteID _aircraft_sprite[] = {
|
|
|
|
0x0EB5, 0x0EBD, 0x0EC5, 0x0ECD,
|
|
|
|
0x0ED5, 0x0EDD, 0x0E9D, 0x0EA5,
|
|
|
|
0x0EAD, 0x0EE5, 0x0F05, 0x0F0D,
|
|
|
|
0x0F15, 0x0F1D, 0x0F25, 0x0F2D,
|
|
|
|
0x0EED, 0x0EF5, 0x0EFD, 0x0F35,
|
|
|
|
0x0E9D, 0x0EA5, 0x0EAD, 0x0EB5,
|
|
|
|
0x0EBD, 0x0EC5
|
|
|
|
};
|
|
|
|
|
2005-03-23 11:12:17 +00:00
|
|
|
/* Find the nearest hangar to v
|
|
|
|
* INVALID_STATION is returned, if the player does not have any suitable
|
|
|
|
* airports (like helipads only)
|
|
|
|
*/
|
2005-05-12 00:11:37 +00:00
|
|
|
static StationID FindNearestHangar(const Vehicle *v)
|
2005-01-27 15:43:44 +00:00
|
|
|
{
|
2005-03-23 11:12:17 +00:00
|
|
|
const Station *st;
|
|
|
|
uint best = 0;
|
2005-05-12 00:11:37 +00:00
|
|
|
StationID index = INVALID_STATION;
|
2005-01-27 15:43:44 +00:00
|
|
|
|
|
|
|
FOR_ALL_STATIONS(st) {
|
2005-03-23 11:12:17 +00:00
|
|
|
if (st->owner == v->owner && st->facilities & FACIL_AIRPORT &&
|
|
|
|
GetAirport(st->airport_type)->nof_depots > 0) {
|
|
|
|
uint distance;
|
|
|
|
|
|
|
|
// don't crash the plane if we know it can't land at the airport
|
|
|
|
if (HASBIT(v->subtype, 1) && st->airport_type == AT_SMALL &&
|
|
|
|
!_cheats.no_jetcrash.value)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
distance = DistanceSquare(v->tile, st->airport_tile);
|
|
|
|
if (distance < best || index == INVALID_STATION) {
|
|
|
|
best = distance;
|
|
|
|
index = st->index;
|
2005-01-27 15:43:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2005-03-23 11:12:17 +00:00
|
|
|
return index;
|
2005-01-27 15:43:44 +00:00
|
|
|
}
|
|
|
|
|
2005-04-02 00:17:16 +00:00
|
|
|
#if 0
|
2005-01-27 15:43:44 +00:00
|
|
|
// returns true if vehicle v have an airport in the schedule, that has a hangar
|
|
|
|
static bool HaveHangarInOrderList(Vehicle *v)
|
|
|
|
{
|
|
|
|
const Order *order;
|
|
|
|
|
|
|
|
FOR_VEHICLE_ORDERS(v, order) {
|
|
|
|
const Station *st = GetStation(order->station);
|
|
|
|
if (st->owner == v->owner && st->facilities & FACIL_AIRPORT) {
|
|
|
|
// If an airport doesn't have terminals (so no landing space for airports),
|
|
|
|
// it surely doesn't have any hangars
|
|
|
|
if (GetAirport(st->airport_type)->terminals != NULL)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2005-04-02 00:17:16 +00:00
|
|
|
#endif
|
2005-01-27 15:43:44 +00:00
|
|
|
|
2005-05-11 16:17:03 +00:00
|
|
|
int GetAircraftImage(const Vehicle *v, byte direction)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
int spritenum = v->spritenum;
|
|
|
|
|
|
|
|
if (is_custom_sprite(spritenum)) {
|
|
|
|
int sprite = GetCustomVehicleSprite(v, direction);
|
|
|
|
|
2006-02-06 09:18:04 +00:00
|
|
|
if (sprite != 0) return sprite;
|
2005-09-26 19:01:49 +00:00
|
|
|
spritenum = orig_aircraft_vehicle_info[v->engine_type - AIRCRAFT_ENGINES_INDEX].image_index;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
return direction + _aircraft_sprite[spritenum];
|
|
|
|
}
|
|
|
|
|
2005-10-01 12:43:34 +00:00
|
|
|
void DrawAircraftEngine(int x, int y, EngineID engine, uint32 image_ormod)
|
2004-11-13 20:37:57 +00:00
|
|
|
{
|
2004-12-03 21:57:05 +00:00
|
|
|
int spritenum = AircraftVehInfo(engine)->image_index;
|
2005-01-28 12:20:06 +00:00
|
|
|
int sprite = (6 + _aircraft_sprite[spritenum]);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2004-11-13 20:37:57 +00:00
|
|
|
if (is_custom_sprite(spritenum)) {
|
2005-01-28 12:20:06 +00:00
|
|
|
sprite = GetCustomVehicleIcon(engine, 6);
|
|
|
|
if (!sprite)
|
2005-09-26 19:01:49 +00:00
|
|
|
spritenum = orig_aircraft_vehicle_info[engine - AIRCRAFT_ENGINES_INDEX].image_index;
|
2004-11-13 20:37:57 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-01-28 12:20:06 +00:00
|
|
|
DrawSprite(sprite | image_ormod, x, y);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-01-28 12:20:06 +00:00
|
|
|
if ((AircraftVehInfo(engine)->subtype & 1) == 0) {
|
2005-10-15 11:06:54 +00:00
|
|
|
DrawSprite(SPR_ROTOR_STOPPED, x, y - 5);
|
2005-01-28 12:20:06 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2005-12-14 06:20:23 +00:00
|
|
|
static int32 EstimateAircraftCost(EngineID engine_type)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2004-12-03 21:57:05 +00:00
|
|
|
return AircraftVehInfo(engine_type)->base_cost * (_price.aircraft_base>>3)>>5;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-05-12 00:11:37 +00:00
|
|
|
/** Build an aircraft.
|
|
|
|
* @param x,y tile coordinates of depot where aircraft is built
|
|
|
|
* @param p1 aircraft type being built (engine)
|
|
|
|
* @param p2 unused
|
|
|
|
*/
|
2004-08-09 17:04:08 +00:00
|
|
|
int32 CmdBuildAircraft(int x, int y, uint32 flags, uint32 p1, uint32 p2)
|
|
|
|
{
|
|
|
|
int32 value;
|
|
|
|
Vehicle *vl[3], *v, *u, *w;
|
2005-02-04 14:24:23 +00:00
|
|
|
UnitID unit_num;
|
2005-06-25 06:15:43 +00:00
|
|
|
TileIndex tile = TileVirtXY(x, y);
|
2005-05-30 14:45:36 +00:00
|
|
|
const AircraftVehicleInfo *avi;
|
2004-08-09 17:04:08 +00:00
|
|
|
Engine *e;
|
|
|
|
|
2006-01-18 14:12:26 +00:00
|
|
|
if (!IsEngineBuildable(p1, VEH_Aircraft)) return_cmd_error(STR_ENGINE_NOT_BUILDABLE);
|
2005-01-27 21:00:05 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
value = EstimateAircraftCost(p1);
|
|
|
|
|
2005-09-13 13:30:18 +00:00
|
|
|
// to just query the cost, it is not neccessary to have a valid tile (automation/AI)
|
2005-05-12 00:11:37 +00:00
|
|
|
if (flags & DC_QUERY_COST) return value;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-09-13 13:30:18 +00:00
|
|
|
if (!IsAircraftHangarTile(tile) || !IsTileOwner(tile, _current_player)) return CMD_ERROR;
|
|
|
|
|
|
|
|
SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES);
|
|
|
|
|
2005-05-30 14:45:36 +00:00
|
|
|
avi = AircraftVehInfo(p1);
|
2004-08-09 17:04:08 +00:00
|
|
|
// allocate 2 or 3 vehicle structs, depending on type
|
2004-12-03 21:57:05 +00:00
|
|
|
if (!AllocateVehicles(vl, (avi->subtype & 1) == 0 ? 3 : 2) ||
|
2005-11-14 19:48:04 +00:00
|
|
|
IsOrderPoolFull()) {
|
|
|
|
return_cmd_error(STR_00E1_TOO_MANY_VEHICLES_IN_GAME);
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
unit_num = GetFreeUnitNumber(VEH_Aircraft);
|
|
|
|
if (unit_num > _patches.max_aircraft)
|
|
|
|
return_cmd_error(STR_00E1_TOO_MANY_VEHICLES_IN_GAME);
|
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
|
|
|
v = vl[0];
|
|
|
|
u = vl[1];
|
|
|
|
|
|
|
|
v->unitnumber = unit_num;
|
|
|
|
v->type = u->type = VEH_Aircraft;
|
|
|
|
v->direction = 3;
|
|
|
|
|
|
|
|
v->owner = u->owner = _current_player;
|
|
|
|
|
|
|
|
v->tile = tile;
|
|
|
|
// u->tile = 0;
|
|
|
|
|
2005-01-07 17:02:43 +00:00
|
|
|
x = TileX(tile) * 16 + 5;
|
|
|
|
y = TileY(tile) * 16 + 3;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
v->x_pos = u->x_pos = x;
|
|
|
|
v->y_pos = u->y_pos = y;
|
|
|
|
|
|
|
|
u->z_pos = GetSlopeZ(x, y);
|
|
|
|
v->z_pos = u->z_pos + 1;
|
|
|
|
|
|
|
|
v->x_offs = v->y_offs = -1;
|
|
|
|
// u->delta_x = u->delta_y = 0;
|
|
|
|
|
|
|
|
v->sprite_width = v->sprite_height = 2;
|
|
|
|
v->z_height = 5;
|
|
|
|
|
|
|
|
u->sprite_width = u->sprite_height = 2;
|
|
|
|
u->z_height = 1;
|
|
|
|
|
|
|
|
v->vehstatus = VS_HIDDEN | VS_STOPPED | VS_DEFPAL;
|
|
|
|
u->vehstatus = VS_HIDDEN | VS_UNCLICKABLE | VS_DISASTER;
|
|
|
|
|
2004-12-03 21:57:05 +00:00
|
|
|
v->spritenum = avi->image_index;
|
2004-08-09 17:04:08 +00:00
|
|
|
// v->cargo_count = u->number_of_pieces = 0;
|
|
|
|
|
2005-01-07 08:38:27 +00:00
|
|
|
v->cargo_cap = avi->passenger_capacity;
|
2004-12-03 21:57:05 +00:00
|
|
|
u->cargo_cap = avi->mail_capacity;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
v->cargo_type = CT_PASSENGERS;
|
|
|
|
u->cargo_type = CT_MAIL;
|
|
|
|
|
|
|
|
v->string_id = STR_SV_AIRCRAFT_NAME;
|
|
|
|
// v->next_order_param = v->next_order = 0;
|
|
|
|
|
|
|
|
// v->load_unload_time_rem = 0;
|
|
|
|
// v->progress = 0;
|
2005-02-02 16:16:43 +00:00
|
|
|
v->last_station_visited = INVALID_STATION;
|
2004-08-09 17:04:08 +00:00
|
|
|
// v->destination_coords = 0;
|
|
|
|
|
2004-12-03 21:57:05 +00:00
|
|
|
v->max_speed = avi->max_speed;
|
|
|
|
v->acceleration = avi->acceleration;
|
2005-10-01 12:43:34 +00:00
|
|
|
v->engine_type = p1;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2004-12-03 21:57:05 +00:00
|
|
|
v->subtype = (avi->subtype & 1) == 0 ? 0 : 2;
|
2004-08-09 17:04:08 +00:00
|
|
|
v->value = value;
|
|
|
|
|
|
|
|
u->subtype = 4;
|
|
|
|
|
2005-06-07 18:13:49 +00:00
|
|
|
e = GetEngine(p1);
|
2004-08-09 17:04:08 +00:00
|
|
|
v->reliability = e->reliability;
|
|
|
|
v->reliability_spd_dec = e->reliability_spd_dec;
|
|
|
|
v->max_age = e->lifelength * 366;
|
|
|
|
|
|
|
|
_new_aircraft_id = v->index;
|
2005-10-29 21:54:28 +00:00
|
|
|
_new_vehicle_id = v->index;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-09-13 13:30:18 +00:00
|
|
|
v->u.air.pos = MAX_ELEMENTS;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-09-13 13:30:18 +00:00
|
|
|
/* When we click on hangar we know the tile it is on. By that we know
|
|
|
|
* its position in the array of depots the airport has.....we can search
|
|
|
|
* layout for #th position of depot. Since layout must start with a listing
|
|
|
|
* of all depots, it is simple */
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2005-07-13 18:04:01 +00:00
|
|
|
const Station* st = GetStation(_m[tile].m2);
|
2005-09-13 13:30:18 +00:00
|
|
|
const AirportFTAClass* apc = GetAirport(st->airport_type);
|
2005-03-23 11:54:20 +00:00
|
|
|
uint i;
|
|
|
|
|
2005-09-13 13:30:18 +00:00
|
|
|
for (i = 0; i < apc->nof_depots; i++) {
|
|
|
|
if (st->airport_tile + ToTileIndexDiff(apc->airport_depots[i]) == tile) {
|
|
|
|
assert(apc->layout[i].heading == HANGAR);
|
|
|
|
v->u.air.pos = apc->layout[i].position;
|
2004-08-09 17:04:08 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// to ensure v->u.air.pos has been given a value
|
|
|
|
assert(v->u.air.pos != MAX_ELEMENTS);
|
|
|
|
}
|
|
|
|
|
|
|
|
v->u.air.state = HANGAR;
|
|
|
|
v->u.air.previous_pos = v->u.air.pos;
|
2005-07-13 18:04:01 +00:00
|
|
|
v->u.air.targetairport = _m[tile].m2;
|
2004-08-09 17:04:08 +00:00
|
|
|
v->next = u;
|
|
|
|
|
|
|
|
v->service_interval = _patches.servint_aircraft;
|
|
|
|
|
|
|
|
v->date_of_last_service = _date;
|
2005-05-30 10:18:18 +00:00
|
|
|
v->build_year = u->build_year = _cur_year;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
v->cur_image = u->cur_image = 0xEA0;
|
|
|
|
|
2005-12-28 22:29:59 +00:00
|
|
|
v->random_bits = VehicleRandomBits();
|
|
|
|
u->random_bits = VehicleRandomBits();
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
VehiclePositionChanged(v);
|
|
|
|
VehiclePositionChanged(u);
|
|
|
|
|
2005-05-12 00:11:37 +00:00
|
|
|
// Aircraft with 3 vehicles (chopper)?
|
2004-08-09 17:04:08 +00:00
|
|
|
if (v->subtype == 0) {
|
|
|
|
w = vl[2];
|
|
|
|
|
|
|
|
u->next = w;
|
|
|
|
|
|
|
|
w->type = VEH_Aircraft;
|
|
|
|
w->direction = 0;
|
|
|
|
w->owner = _current_player;
|
|
|
|
w->x_pos = v->x_pos;
|
|
|
|
w->y_pos = v->y_pos;
|
|
|
|
w->z_pos = v->z_pos + 5;
|
|
|
|
w->x_offs = w->y_offs = -1;
|
|
|
|
w->sprite_width = w->sprite_height = 2;
|
|
|
|
w->z_height = 1;
|
|
|
|
w->vehstatus = VS_HIDDEN | VS_UNCLICKABLE;
|
|
|
|
w->subtype = 6;
|
2005-10-15 11:06:54 +00:00
|
|
|
w->cur_image = SPR_ROTOR_STOPPED;
|
2005-12-28 22:29:59 +00:00
|
|
|
w->random_bits = VehicleRandomBits();
|
2004-08-09 17:04:08 +00:00
|
|
|
VehiclePositionChanged(w);
|
|
|
|
}
|
|
|
|
|
|
|
|
InvalidateWindow(WC_VEHICLE_DEPOT, v->tile);
|
2004-12-10 18:16:08 +00:00
|
|
|
RebuildVehicleLists();
|
2004-08-09 17:04:08 +00:00
|
|
|
InvalidateWindow(WC_COMPANY, v->owner);
|
2005-11-08 23:18:09 +00:00
|
|
|
if (IsLocalPlayer())
|
|
|
|
InvalidateWindow(WC_REPLACE_VEHICLE, VEH_Aircraft); //updates the replace Aircraft window
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsAircraftHangarTile(TileIndex tile)
|
|
|
|
{
|
|
|
|
// 0x56 - hangar facing other way international airport (86)
|
|
|
|
// 0x20 - hangar large airport (32)
|
|
|
|
// 0x41 - hangar small airport (65)
|
2005-01-16 11:24:58 +00:00
|
|
|
return IsTileType(tile, MP_STATION) &&
|
2005-07-13 18:04:01 +00:00
|
|
|
(_m[tile].m5 == 32 || _m[tile].m5 == 65 || _m[tile].m5 == 86);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2005-10-28 20:04:54 +00:00
|
|
|
bool CheckStoppedInHangar(const Vehicle* v)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2005-03-23 11:54:20 +00:00
|
|
|
if (!(v->vehstatus & VS_STOPPED) || !IsAircraftHangarTile(v->tile)) {
|
2004-08-09 17:04:08 +00:00
|
|
|
_error_message = STR_A01B_AIRCRAFT_MUST_BE_STOPPED;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-11-14 19:44:06 +00:00
|
|
|
static void DoDeleteAircraft(Vehicle *v)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
DeleteWindowById(WC_VEHICLE_VIEW, v->index);
|
2004-12-10 18:16:08 +00:00
|
|
|
RebuildVehicleLists();
|
2004-08-09 17:04:08 +00:00
|
|
|
InvalidateWindow(WC_COMPANY, v->owner);
|
|
|
|
DeleteVehicleChain(v);
|
2005-01-18 09:35:31 +00:00
|
|
|
InvalidateWindowClasses(WC_AIRCRAFT_LIST);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2005-05-11 00:00:27 +00:00
|
|
|
/** Sell an aircraft.
|
|
|
|
* @param x,y unused
|
|
|
|
* @param p1 vehicle ID to be sold
|
|
|
|
* @param p2 unused
|
|
|
|
*/
|
2004-08-09 17:04:08 +00:00
|
|
|
int32 CmdSellAircraft(int x, int y, uint32 flags, uint32 p1, uint32 p2)
|
|
|
|
{
|
|
|
|
Vehicle *v;
|
|
|
|
|
2005-01-30 20:50:06 +00:00
|
|
|
if (!IsVehicleIndex(p1)) return CMD_ERROR;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-01-06 22:31:58 +00:00
|
|
|
v = GetVehicle(p1);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-01-30 18:51:08 +00:00
|
|
|
if (v->type != VEH_Aircraft || !CheckOwnership(v->owner) || !CheckStoppedInHangar(v))
|
2004-08-09 17:04:08 +00:00
|
|
|
return CMD_ERROR;
|
|
|
|
|
2005-01-30 20:50:06 +00:00
|
|
|
SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES);
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
if (flags & DC_EXEC) {
|
|
|
|
// Invalidate depot
|
|
|
|
InvalidateWindow(WC_VEHICLE_DEPOT, v->tile);
|
|
|
|
DoDeleteAircraft(v);
|
2005-11-08 23:18:09 +00:00
|
|
|
if (IsLocalPlayer())
|
|
|
|
InvalidateWindow(WC_REPLACE_VEHICLE, VEH_Aircraft); // updates the replace Aircraft window
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return -(int32)v->value;
|
|
|
|
}
|
|
|
|
|
2005-05-11 00:00:27 +00:00
|
|
|
/** Start/Stop an aircraft.
|
|
|
|
* @param x,y unused
|
2005-05-12 00:11:37 +00:00
|
|
|
* @param p1 aircraft ID to start/stop
|
2005-05-11 00:00:27 +00:00
|
|
|
* @param p2 unused
|
|
|
|
*/
|
2004-08-09 17:04:08 +00:00
|
|
|
int32 CmdStartStopAircraft(int x, int y, uint32 flags, uint32 p1, uint32 p2)
|
|
|
|
{
|
|
|
|
Vehicle *v;
|
|
|
|
|
2005-01-30 20:50:06 +00:00
|
|
|
if (!IsVehicleIndex(p1)) return CMD_ERROR;
|
|
|
|
|
2005-01-06 22:31:58 +00:00
|
|
|
v = GetVehicle(p1);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-05-11 00:00:27 +00:00
|
|
|
if (v->type != VEH_Aircraft || !CheckOwnership(v->owner)) return CMD_ERROR;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
// cannot stop airplane when in flight, or when taking off / landing
|
2005-05-11 00:00:27 +00:00
|
|
|
if (v->u.air.state >= STARTTAKEOFF)
|
2004-08-09 17:04:08 +00:00
|
|
|
return_cmd_error(STR_A017_AIRCRAFT_IS_IN_FLIGHT);
|
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
|
|
|
v->vehstatus ^= VS_STOPPED;
|
2004-12-21 23:27:58 +00:00
|
|
|
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
|
2004-08-09 17:04:08 +00:00
|
|
|
InvalidateWindow(WC_VEHICLE_DEPOT, v->tile);
|
2005-01-18 09:35:31 +00:00
|
|
|
InvalidateWindowClasses(WC_AIRCRAFT_LIST);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-05-12 00:11:37 +00:00
|
|
|
/** Send an aircraft to the hangar.
|
|
|
|
* @param x,y unused
|
|
|
|
* @param p1 vehicle ID to send to the hangar
|
|
|
|
* @param p2 various bitmasked elements
|
|
|
|
* - p2 = 0 - aircraft goes to the depot and stays there (user command)
|
|
|
|
* - p2 non-zero - aircraft will try to goto a depot, but not stop there (eg forced servicing)
|
|
|
|
* - p2 (bit 17) - aircraft will try to goto a depot at the next airport
|
|
|
|
*/
|
2004-08-09 17:04:08 +00:00
|
|
|
int32 CmdSendAircraftToHangar(int x, int y, uint32 flags, uint32 p1, uint32 p2)
|
|
|
|
{
|
|
|
|
Vehicle *v;
|
|
|
|
|
2005-01-30 20:50:06 +00:00
|
|
|
if (!IsVehicleIndex(p1)) return CMD_ERROR;
|
|
|
|
|
2005-01-06 22:31:58 +00:00
|
|
|
v = GetVehicle(p1);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-05-12 00:11:37 +00:00
|
|
|
if (v->type != VEH_Aircraft || !CheckOwnership(v->owner)) return CMD_ERROR;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-01-27 20:38:19 +00:00
|
|
|
if (v->current_order.type == OT_GOTO_DEPOT && p2 == 0) {
|
2004-08-09 17:04:08 +00:00
|
|
|
if (flags & DC_EXEC) {
|
2004-12-05 12:43:04 +00:00
|
|
|
if (v->current_order.flags & OF_UNLOAD) v->cur_order_index++;
|
|
|
|
v->current_order.type = OT_DUMMY;
|
|
|
|
v->current_order.flags = 0;
|
2004-12-21 23:27:58 +00:00
|
|
|
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
} else {
|
2005-02-01 22:04:53 +00:00
|
|
|
bool next_airport_has_hangar = true;
|
2005-05-12 00:11:37 +00:00
|
|
|
/* XXX - I don't think p2 is any valid station cause all calls use either 0, 1, or 1<<16!!!!!!!!! */
|
|
|
|
StationID next_airport_index = (HASBIT(p2, 17)) ? (StationID)p2 : v->u.air.targetairport;
|
|
|
|
const Station *st = GetStation(next_airport_index);
|
2004-08-09 17:04:08 +00:00
|
|
|
// If an airport doesn't have terminals (so no landing space for airports),
|
|
|
|
// it surely doesn't have any hangars
|
2005-05-12 00:11:37 +00:00
|
|
|
if (!IsValidStation(st) || st->airport_tile == 0 || GetAirport(st->airport_type)->nof_depots == 0) {
|
|
|
|
StationID station;
|
|
|
|
|
|
|
|
if (p2 != 0) return CMD_ERROR;
|
|
|
|
// the aircraft has to search for a hangar on its own
|
|
|
|
station = FindNearestHangar(v);
|
|
|
|
|
|
|
|
next_airport_has_hangar = false;
|
|
|
|
if (station == INVALID_STATION) return CMD_ERROR;
|
|
|
|
st = GetStation(station);
|
|
|
|
next_airport_index = station;
|
|
|
|
|
2005-02-01 22:04:53 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
2004-12-05 12:43:04 +00:00
|
|
|
v->current_order.type = OT_GOTO_DEPOT;
|
2005-02-01 22:04:53 +00:00
|
|
|
v->current_order.flags = HASBIT(p2, 16) ? 0 : OF_NON_STOP | OF_FULL_LOAD;
|
2005-01-27 15:43:44 +00:00
|
|
|
v->current_order.station = next_airport_index;
|
2004-12-21 23:27:58 +00:00
|
|
|
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
|
2005-02-01 22:04:53 +00:00
|
|
|
if (HASBIT(p2, 17) || (p2 == 0 && v->u.air.state == FLYING && !next_airport_has_hangar)) {
|
|
|
|
// the aircraft is now heading for a different hangar than the next in the orders
|
2005-01-27 15:43:44 +00:00
|
|
|
AircraftNextAirportPos_and_Order(v);
|
|
|
|
v->u.air.targetairport = next_airport_index;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-05-14 12:36:16 +00:00
|
|
|
/** Refits an aircraft to the specified cargo type.
|
|
|
|
* @param x,y unused
|
|
|
|
* @param p1 vehicle ID of the aircraft to refit
|
|
|
|
* @param p2 various bitstuffed elements
|
2005-11-14 08:09:57 +00:00
|
|
|
* - p2 = (bit 0-7) - the new cargo type to refit to
|
2005-05-14 12:36:16 +00:00
|
|
|
*/
|
2004-08-09 17:04:08 +00:00
|
|
|
int32 CmdRefitAircraft(int x, int y, uint32 flags, uint32 p1, uint32 p2)
|
|
|
|
{
|
2005-05-14 12:36:16 +00:00
|
|
|
Vehicle *v;
|
2004-08-09 17:04:08 +00:00
|
|
|
int pass, mail;
|
|
|
|
int32 cost;
|
2005-11-14 08:09:57 +00:00
|
|
|
CargoID new_cid = GB(p2, 0, 8);
|
2005-05-14 12:36:16 +00:00
|
|
|
const AircraftVehicleInfo *avi;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-01-30 20:50:06 +00:00
|
|
|
if (!IsVehicleIndex(p1)) return CMD_ERROR;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-01-06 22:31:58 +00:00
|
|
|
v = GetVehicle(p1);
|
2005-01-30 20:50:06 +00:00
|
|
|
|
2005-05-14 12:36:16 +00:00
|
|
|
if (v->type != VEH_Aircraft || !CheckOwnership(v->owner)) return CMD_ERROR;
|
2005-10-30 11:00:54 +00:00
|
|
|
if (!CheckStoppedInHangar(v)) return_cmd_error(STR_A01B_AIRCRAFT_MUST_BE_STOPPED);
|
2005-01-30 20:50:06 +00:00
|
|
|
|
2005-01-07 09:51:16 +00:00
|
|
|
avi = AircraftVehInfo(v->engine_type);
|
2005-01-30 20:50:06 +00:00
|
|
|
|
2005-05-14 12:36:16 +00:00
|
|
|
/* Check cargo */
|
2005-11-29 22:29:59 +00:00
|
|
|
if (new_cid > NUM_CARGO || !CanRefitTo(v->engine_type, new_cid)) return CMD_ERROR;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-01-30 20:50:06 +00:00
|
|
|
SET_EXPENSES_TYPE(EXPENSES_AIRCRAFT_RUN);
|
|
|
|
|
2005-05-14 12:36:16 +00:00
|
|
|
switch (new_cid) {
|
2005-01-07 09:51:16 +00:00
|
|
|
case CT_PASSENGERS:
|
|
|
|
pass = avi->passenger_capacity;
|
|
|
|
break;
|
|
|
|
case CT_MAIL:
|
|
|
|
pass = avi->passenger_capacity + avi->mail_capacity;
|
|
|
|
break;
|
|
|
|
case CT_GOODS:
|
|
|
|
pass = avi->passenger_capacity + avi->mail_capacity;
|
|
|
|
pass /= 2;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
pass = avi->passenger_capacity + avi->mail_capacity;
|
|
|
|
pass /= 4;
|
|
|
|
break;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2006-02-11 09:24:51 +00:00
|
|
|
_returned_refit_capacity = pass;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
cost = 0;
|
2005-05-14 12:36:16 +00:00
|
|
|
if (IS_HUMAN_PLAYER(v->owner) && new_cid != v->cargo_type) {
|
2004-08-09 17:04:08 +00:00
|
|
|
cost = _price.aircraft_base >> 7;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
2005-05-14 12:36:16 +00:00
|
|
|
Vehicle *u;
|
2004-08-09 17:04:08 +00:00
|
|
|
v->cargo_cap = pass;
|
|
|
|
|
|
|
|
u = v->next;
|
2005-05-14 12:36:16 +00:00
|
|
|
mail = (new_cid != CT_PASSENGERS) ? 0 : avi->mail_capacity;
|
2004-08-09 17:04:08 +00:00
|
|
|
u->cargo_cap = mail;
|
2005-10-30 15:50:08 +00:00
|
|
|
v->cargo_count = u->cargo_count = 0;
|
2005-05-14 12:36:16 +00:00
|
|
|
v->cargo_type = new_cid;
|
2004-08-09 17:04:08 +00:00
|
|
|
InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
|
|
|
|
}
|
|
|
|
|
|
|
|
return cost;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void CheckIfAircraftNeedsService(Vehicle *v)
|
|
|
|
{
|
2005-11-13 13:43:55 +00:00
|
|
|
const Station* st;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-11-14 19:48:04 +00:00
|
|
|
if (_patches.servint_aircraft == 0) return;
|
|
|
|
if (!VehicleNeedsService(v)) return;
|
|
|
|
if (v->vehstatus & VS_STOPPED) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2004-12-05 12:43:04 +00:00
|
|
|
if (v->current_order.type == OT_GOTO_DEPOT &&
|
2005-03-20 08:43:29 +00:00
|
|
|
v->current_order.flags & OF_HALT_IN_DEPOT)
|
2004-08-09 17:04:08 +00:00
|
|
|
return;
|
|
|
|
|
2005-11-14 19:48:04 +00:00
|
|
|
if (_patches.gotodepot && VehicleHasDepotOrders(v)) return;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2005-01-06 22:31:58 +00:00
|
|
|
st = GetStation(v->current_order.station);
|
2004-08-09 17:04:08 +00:00
|
|
|
// only goto depot if the target airport has terminals (eg. it is airport)
|
2005-01-09 08:49:40 +00:00
|
|
|
if (st->xy != 0 && st->airport_tile != 0 && GetAirport(st->airport_type)->terminals != NULL) {
|
2004-08-09 17:04:08 +00:00
|
|
|
// printf("targetairport = %d, st->index = %d\n", v->u.air.targetairport, st->index);
|
|
|
|
// v->u.air.targetairport = st->index;
|
2004-12-05 12:43:04 +00:00
|
|
|
v->current_order.type = OT_GOTO_DEPOT;
|
|
|
|
v->current_order.flags = OF_NON_STOP;
|
2004-12-21 23:27:58 +00:00
|
|
|
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
|
2004-12-05 12:43:04 +00:00
|
|
|
} else if (v->current_order.type == OT_GOTO_DEPOT) {
|
|
|
|
v->current_order.type = OT_DUMMY;
|
|
|
|
v->current_order.flags = 0;
|
2004-12-21 23:27:58 +00:00
|
|
|
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void OnNewDay_Aircraft(Vehicle *v)
|
|
|
|
{
|
|
|
|
int32 cost;
|
|
|
|
|
2005-11-14 19:48:04 +00:00
|
|
|
if (v->subtype > 2) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-11-14 19:48:04 +00:00
|
|
|
if ((++v->day_counter & 7) == 0) DecreaseVehicleValue(v);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-01-17 21:54:45 +00:00
|
|
|
CheckOrders(v->index, OC_INIT);
|
2004-08-11 10:15:38 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
CheckVehicleBreakdown(v);
|
|
|
|
AgeVehicle(v);
|
|
|
|
CheckIfAircraftNeedsService(v);
|
|
|
|
|
2005-11-14 19:48:04 +00:00
|
|
|
if (v->vehstatus & VS_STOPPED) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2004-12-03 21:57:05 +00:00
|
|
|
cost = AircraftVehInfo(v->engine_type)->running_cost * _price.aircraft_running / 364;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
v->profit_this_year -= cost >> 8;
|
|
|
|
|
|
|
|
SET_EXPENSES_TYPE(EXPENSES_AIRCRAFT_RUN);
|
|
|
|
SubtractMoneyFromPlayerFract(v->owner, cost);
|
|
|
|
|
|
|
|
InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
|
2005-01-18 09:35:31 +00:00
|
|
|
InvalidateWindowClasses(WC_AIRCRAFT_LIST);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2005-01-22 20:23:18 +00:00
|
|
|
void AircraftYearlyLoop(void)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
Vehicle *v;
|
|
|
|
|
|
|
|
FOR_ALL_VEHICLES(v) {
|
|
|
|
if (v->type == VEH_Aircraft && v->subtype <= 2) {
|
|
|
|
v->profit_last_year = v->profit_this_year;
|
|
|
|
v->profit_this_year = 0;
|
|
|
|
InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void AgeAircraftCargo(Vehicle *v)
|
|
|
|
{
|
2005-11-14 19:48:04 +00:00
|
|
|
if (_age_cargo_skip_counter != 0) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
do {
|
2005-11-14 19:48:04 +00:00
|
|
|
if (v->cargo_days != 0xFF) v->cargo_days++;
|
|
|
|
v = v->next;
|
|
|
|
} while (v != NULL);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void HelicopterTickHandler(Vehicle *v)
|
|
|
|
{
|
|
|
|
Vehicle *u;
|
|
|
|
int tick,spd;
|
|
|
|
uint16 img;
|
|
|
|
|
|
|
|
u = v->next->next;
|
|
|
|
|
2005-11-14 19:48:04 +00:00
|
|
|
if (u->vehstatus & VS_HIDDEN) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2004-09-10 19:02:27 +00:00
|
|
|
// if true, helicopter rotors do not rotate. This should only be the case if a helicopter is
|
2004-08-09 17:04:08 +00:00
|
|
|
// loading/unloading at a terminal or stopped
|
2004-12-05 12:43:04 +00:00
|
|
|
if (v->current_order.type == OT_LOADING || (v->vehstatus & VS_STOPPED)) {
|
2004-08-09 17:04:08 +00:00
|
|
|
if (u->cur_speed != 0) {
|
|
|
|
u->cur_speed++;
|
2005-10-15 11:06:54 +00:00
|
|
|
if (u->cur_speed >= 0x80 && u->cur_image == SPR_ROTOR_MOVING_3) {
|
2004-08-09 17:04:08 +00:00
|
|
|
u->cur_speed = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (u->cur_speed == 0)
|
|
|
|
u->cur_speed = 0x70;
|
|
|
|
|
|
|
|
if (u->cur_speed >= 0x50)
|
|
|
|
u->cur_speed--;
|
|
|
|
}
|
|
|
|
|
|
|
|
tick = ++u->tick_counter;
|
|
|
|
spd = u->cur_speed >> 4;
|
|
|
|
|
|
|
|
if (spd == 0) {
|
2005-10-15 11:06:54 +00:00
|
|
|
img = SPR_ROTOR_STOPPED;
|
2005-10-23 13:04:44 +00:00
|
|
|
if (u->cur_image == img) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
} else if (tick >= spd) {
|
|
|
|
u->tick_counter = 0;
|
|
|
|
img = u->cur_image + 1;
|
2005-10-15 11:06:54 +00:00
|
|
|
if (img > SPR_ROTOR_MOVING_3) img = SPR_ROTOR_MOVING_1;
|
2005-10-23 13:04:44 +00:00
|
|
|
} else {
|
2004-08-09 17:04:08 +00:00
|
|
|
return;
|
2005-10-23 13:04:44 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-10-23 13:04:44 +00:00
|
|
|
u->cur_image = img;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
BeginVehicleMove(u);
|
|
|
|
VehiclePositionChanged(u);
|
|
|
|
EndVehicleMove(u);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void SetAircraftPosition(Vehicle *v, int x, int y, int z)
|
|
|
|
{
|
|
|
|
Vehicle *u;
|
|
|
|
int yt;
|
|
|
|
|
|
|
|
v->x_pos = x;
|
|
|
|
v->y_pos = y;
|
|
|
|
v->z_pos = z;
|
|
|
|
|
|
|
|
v->cur_image = GetAircraftImage(v, v->direction);
|
|
|
|
|
|
|
|
BeginVehicleMove(v);
|
|
|
|
VehiclePositionChanged(v);
|
|
|
|
EndVehicleMove(v);
|
|
|
|
|
|
|
|
u = v->next;
|
|
|
|
|
|
|
|
yt = y - ((v->z_pos-GetSlopeZ(x, y-1)) >> 3);
|
|
|
|
u->x_pos = x;
|
|
|
|
u->y_pos = yt;
|
|
|
|
u->z_pos = GetSlopeZ(x,yt);
|
|
|
|
u->cur_image = v->cur_image;
|
|
|
|
|
|
|
|
BeginVehicleMove(u);
|
|
|
|
VehiclePositionChanged(u);
|
|
|
|
EndVehicleMove(u);
|
|
|
|
|
2005-11-14 19:48:04 +00:00
|
|
|
u = u->next;
|
|
|
|
if (u != NULL) {
|
2004-08-09 17:04:08 +00:00
|
|
|
u->x_pos = x;
|
|
|
|
u->y_pos = y;
|
|
|
|
u->z_pos = z + 5;
|
|
|
|
|
|
|
|
BeginVehicleMove(u);
|
|
|
|
VehiclePositionChanged(u);
|
|
|
|
EndVehicleMove(u);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ServiceAircraft(Vehicle *v)
|
|
|
|
{
|
|
|
|
Vehicle *u;
|
|
|
|
|
|
|
|
v->cur_speed = 0;
|
|
|
|
v->subspeed = 0;
|
|
|
|
v->progress = 0;
|
|
|
|
v->vehstatus |= VS_HIDDEN;
|
|
|
|
|
|
|
|
u = v->next;
|
|
|
|
u->vehstatus |= VS_HIDDEN;
|
2005-11-14 19:48:04 +00:00
|
|
|
u = u->next;
|
|
|
|
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);
|
|
|
|
InvalidateWindow(WC_VEHICLE_DEPOT, v->tile);
|
|
|
|
|
2004-12-09 21:46:56 +00:00
|
|
|
VehicleServiceInDepot(v);
|
2004-08-09 17:04:08 +00:00
|
|
|
InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
|
|
|
|
}
|
|
|
|
|
2005-10-23 13:04:44 +00:00
|
|
|
static void PlayAircraftSound(const Vehicle* v)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2004-12-03 21:57:05 +00:00
|
|
|
SndPlayVehicleFx(AircraftVehInfo(v->engine_type)->sfx, v);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool UpdateAircraftSpeed(Vehicle *v)
|
|
|
|
{
|
|
|
|
uint spd = v->acceleration * 2;
|
|
|
|
byte t;
|
|
|
|
|
|
|
|
v->subspeed = (t=v->subspeed) + (byte)spd;
|
2005-11-14 19:48:04 +00:00
|
|
|
spd = min(v->cur_speed + (spd >> 8) + (v->subspeed < t), v->max_speed);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// adjust speed for broken vehicles
|
2005-11-14 19:48:04 +00:00
|
|
|
if (v->vehstatus & VS_AIRCRAFT_BROKEN) spd = min(spd, 27);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
//updates statusbar only if speed have changed to save CPU time
|
|
|
|
if (spd != v->cur_speed) {
|
|
|
|
v->cur_speed = spd;
|
|
|
|
if (_patches.vehicle_speed)
|
2004-12-21 23:27:58 +00:00
|
|
|
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2005-11-14 19:48:04 +00:00
|
|
|
if (!(v->direction & 1)) spd = spd * 3 / 4;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-11-14 19:48:04 +00:00
|
|
|
if (spd == 0) return false;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-11-14 19:48:04 +00:00
|
|
|
if ((byte)++spd == 0) return true;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
v->progress = (t = v->progress) - (byte)spd;
|
|
|
|
|
2005-11-14 19:48:04 +00:00
|
|
|
return t < v->progress;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// get Aircraft running altitude
|
|
|
|
static byte GetAircraftFlyingAltitude(const Vehicle *v)
|
|
|
|
{
|
2005-06-03 22:25:06 +00:00
|
|
|
switch (v->max_speed) {
|
|
|
|
case 37: return 162;
|
|
|
|
case 74: return 171;
|
|
|
|
default: return 180;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-02-22 19:38:44 +00:00
|
|
|
static bool AircraftController(Vehicle *v)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
Station *st;
|
|
|
|
const AirportMovingData *amd;
|
|
|
|
Vehicle *u;
|
|
|
|
byte z,dirdiff,newdir,maxz,curz;
|
|
|
|
GetNewVehiclePosResult gp;
|
|
|
|
uint dist;
|
|
|
|
int x,y;
|
|
|
|
|
2005-01-06 22:31:58 +00:00
|
|
|
st = GetStation(v->u.air.targetairport);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
// prevent going to 0,0 if airport is deleted.
|
|
|
|
{
|
2005-06-24 12:38:35 +00:00
|
|
|
TileIndex tile = st->airport_tile;
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
if (tile == 0) tile = st->xy;
|
|
|
|
// xy of destination
|
2005-01-07 17:02:43 +00:00
|
|
|
x = TileX(tile) * 16;
|
|
|
|
y = TileY(tile) * 16;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// get airport moving data
|
|
|
|
assert(v->u.air.pos < GetAirport(st->airport_type)->nofelements);
|
|
|
|
amd = &_airport_moving_datas[st->airport_type][v->u.air.pos];
|
|
|
|
|
|
|
|
// Helicopter raise
|
|
|
|
if (amd->flag & AMED_HELI_RAISE) {
|
|
|
|
u = v->next->next;
|
|
|
|
|
|
|
|
// Make sure the rotors don't rotate too fast
|
|
|
|
if (u->cur_speed > 32) {
|
|
|
|
v->cur_speed = 0;
|
2005-11-14 19:48:04 +00:00
|
|
|
if (--u->cur_speed == 32) SndPlayVehicleFx(SND_18_HELICOPTER, v);
|
2004-08-09 17:04:08 +00:00
|
|
|
} else {
|
|
|
|
u->cur_speed = 32;
|
|
|
|
if (UpdateAircraftSpeed(v)) {
|
|
|
|
v->tile = 0;
|
|
|
|
|
|
|
|
// Reached altitude?
|
|
|
|
if (v->z_pos >= 184) {
|
|
|
|
v->cur_speed = 0;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
SetAircraftPosition(v, v->x_pos, v->y_pos, v->z_pos+1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Helicopter landing.
|
|
|
|
if (amd->flag & AMED_HELI_LOWER) {
|
|
|
|
if (UpdateAircraftSpeed(v)) {
|
|
|
|
if (st->airport_tile == 0) {
|
2005-02-22 19:38:44 +00:00
|
|
|
// FIXME - AircraftController -> if station no longer exists, do not land
|
2004-08-09 17:04:08 +00:00
|
|
|
// 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
|
|
|
|
v->u.air.state = FLYING;
|
|
|
|
AircraftNextAirportPos_and_Order(v);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Vehicle is now at the airport.
|
|
|
|
v->tile = st->airport_tile;
|
|
|
|
|
|
|
|
// Find altitude of landing position.
|
|
|
|
z = GetSlopeZ(x, y) + 1;
|
|
|
|
if (st->airport_type == AT_OILRIG) z += 54;
|
|
|
|
if (st->airport_type == AT_HELIPORT) z += 60;
|
|
|
|
|
|
|
|
if (z == v->z_pos) {
|
|
|
|
u = v->next->next;
|
|
|
|
|
|
|
|
// Increase speed of rotors. When speed is 80, we've landed.
|
2005-11-14 19:48:04 +00:00
|
|
|
if (u->cur_speed >= 80) return true;
|
|
|
|
u->cur_speed += 4;
|
2004-08-09 17:04:08 +00:00
|
|
|
} else if (v->z_pos > z) {
|
|
|
|
SetAircraftPosition(v, v->x_pos, v->y_pos, v->z_pos-1);
|
|
|
|
} else {
|
|
|
|
SetAircraftPosition(v, v->x_pos, v->y_pos, v->z_pos+1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get distance from destination pos to current pos.
|
|
|
|
dist = myabs(x + amd->x - v->x_pos) + myabs(y + amd->y - v->y_pos);
|
|
|
|
|
|
|
|
// Need exact position?
|
2005-08-25 16:53:51 +00:00
|
|
|
if (!(amd->flag & AMED_EXACTPOS) && dist <= (amd->flag & AMED_SLOWTURN ? 8U : 4U))
|
2004-08-09 17:04:08 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
// At final pos?
|
|
|
|
if (dist == 0) {
|
2005-11-14 19:48:04 +00:00
|
|
|
if (v->cur_speed > 12) v->cur_speed = 12;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
// Change direction smoothly to final direction.
|
|
|
|
dirdiff = amd->direction - v->direction;
|
2004-09-10 19:02:27 +00:00
|
|
|
// if distance is 0, and plane points in right direction, no point in calling
|
2004-08-09 17:04:08 +00:00
|
|
|
// UpdateAircraftSpeed(). So do it only afterwards
|
|
|
|
if (dirdiff == 0) {
|
|
|
|
v->cur_speed = 0;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2005-11-14 19:48:04 +00:00
|
|
|
if (!UpdateAircraftSpeed(v)) return false;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
v->direction = (v->direction+((dirdiff&7)<5?1:-1)) & 7;
|
|
|
|
v->cur_speed >>= 1;
|
|
|
|
|
|
|
|
SetAircraftPosition(v, v->x_pos, v->y_pos, v->z_pos);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2005-11-14 19:48:04 +00:00
|
|
|
if (!(amd->flag & AMED_NOSPDCLAMP) && v->cur_speed > 12) v->cur_speed = 12;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-11-14 19:48:04 +00:00
|
|
|
if (!UpdateAircraftSpeed(v)) return false;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-11-14 19:48:04 +00:00
|
|
|
if (v->load_unload_time_rem != 0) v->load_unload_time_rem--;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
// Turn. Do it slowly if in the air.
|
|
|
|
newdir = GetDirectionTowards(v, x + amd->x, y + amd->y);
|
|
|
|
if (newdir != v->direction) {
|
|
|
|
if (amd->flag & AMED_SLOWTURN) {
|
2005-11-14 19:48:04 +00:00
|
|
|
if (v->load_unload_time_rem == 0) v->load_unload_time_rem = 8;
|
2004-08-21 10:54:47 +00:00
|
|
|
v->direction = newdir;
|
2004-08-09 17:04:08 +00:00
|
|
|
} else {
|
|
|
|
v->cur_speed >>= 1;
|
|
|
|
v->direction = newdir;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Move vehicle.
|
|
|
|
GetNewVehiclePos(v, &gp);
|
|
|
|
v->tile = gp.new_tile;
|
|
|
|
|
|
|
|
// If vehicle is in the air, use tile coordinate 0.
|
2005-11-14 19:48:04 +00:00
|
|
|
if (amd->flag & (AMED_TAKEOFF | AMED_SLOWTURN | AMED_LAND)) v->tile = 0;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
// Adjust Z for land or takeoff?
|
|
|
|
z = v->z_pos;
|
|
|
|
|
|
|
|
if (amd->flag & AMED_TAKEOFF) {
|
2005-11-14 19:48:04 +00:00
|
|
|
z += 2;
|
2004-08-09 17:04:08 +00:00
|
|
|
maxz = GetAircraftFlyingAltitude(v);
|
2005-11-14 19:48:04 +00:00
|
|
|
if (z > maxz) z = maxz;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (amd->flag & AMED_LAND) {
|
|
|
|
if (st->airport_tile == 0) {
|
|
|
|
v->u.air.state = FLYING;
|
|
|
|
AircraftNextAirportPos_and_Order(v);
|
|
|
|
// get aircraft back on running altitude
|
|
|
|
SetAircraftPosition(v, gp.x, gp.y, GetAircraftFlyingAltitude(v));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
curz = GetSlopeZ(x, y) + 1;
|
|
|
|
|
|
|
|
if (curz > z) {
|
|
|
|
z++;
|
|
|
|
} else {
|
2005-11-14 19:48:04 +00:00
|
|
|
int t = max(1, dist - 4);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
z -= ((z - curz) + t - 1) / t;
|
|
|
|
if (z < curz) z = curz;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We've landed. Decrase speed when we're reaching end of runway.
|
|
|
|
if (amd->flag & AMED_BRAKE) {
|
|
|
|
curz = GetSlopeZ(x, y) + 1;
|
|
|
|
|
2005-11-14 19:48:04 +00:00
|
|
|
if (z > curz) {
|
|
|
|
z--;
|
|
|
|
} else if (z < curz) {
|
|
|
|
z++;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-11-14 19:48:04 +00:00
|
|
|
if (dist < 64 && v->cur_speed > 12) v->cur_speed -= 4;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SetAircraftPosition(v, gp.x, gp.y, z);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const int8 _crashed_aircraft_moddir[4] = {
|
|
|
|
-1,0,0,1
|
|
|
|
};
|
|
|
|
|
|
|
|
static void HandleCrashedAircraft(Vehicle *v)
|
|
|
|
{
|
|
|
|
uint32 r;
|
|
|
|
Station *st;
|
2005-01-05 13:15:27 +00:00
|
|
|
int z;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
v->u.air.crashed_counter++;
|
|
|
|
|
2005-01-06 22:31:58 +00:00
|
|
|
st = GetStation(v->u.air.targetairport);
|
2005-01-05 13:15:27 +00:00
|
|
|
|
|
|
|
// make aircraft crash down to the ground
|
2005-01-24 22:09:20 +00:00
|
|
|
if (v->u.air.crashed_counter < 500 && st->airport_tile==0 && ((v->u.air.crashed_counter % 3) == 0) ) {
|
|
|
|
z = GetSlopeZ(v->x_pos, v->y_pos);
|
2005-01-05 13:15:27 +00:00
|
|
|
v->z_pos -= 1;
|
2005-01-24 22:09:20 +00:00
|
|
|
if (v->z_pos == z) {
|
|
|
|
v->u.air.crashed_counter = 500;
|
|
|
|
v->z_pos++;
|
|
|
|
}
|
2005-01-05 13:15:27 +00:00
|
|
|
}
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
if (v->u.air.crashed_counter < 650) {
|
|
|
|
if (CHANCE16R(1,32,r)) {
|
2005-07-20 15:29:28 +00:00
|
|
|
v->direction = (v->direction + _crashed_aircraft_moddir[GB(r, 16, 2)]) & 7;
|
2004-08-09 17:04:08 +00:00
|
|
|
SetAircraftPosition(v, v->x_pos, v->y_pos, v->z_pos);
|
|
|
|
r = Random();
|
|
|
|
CreateEffectVehicleRel(v,
|
2005-07-20 15:29:28 +00:00
|
|
|
GB(r, 0, 4) + 4,
|
|
|
|
GB(r, 4, 4) + 4,
|
|
|
|
GB(r, 8, 4),
|
2005-02-12 15:53:32 +00:00
|
|
|
EV_EXPLOSION_SMALL);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
} else if (v->u.air.crashed_counter >= 10000) {
|
|
|
|
// remove rubble of crashed airplane
|
|
|
|
|
|
|
|
// 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
|
|
|
|
CLRBITS(st->airport_flags, RUNWAY_IN_block);
|
|
|
|
|
|
|
|
BeginVehicleMove(v);
|
|
|
|
EndVehicleMove(v);
|
|
|
|
|
|
|
|
DoDeleteAircraft(v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void HandleBrokenAircraft(Vehicle *v)
|
|
|
|
{
|
|
|
|
if (v->breakdown_ctr != 1) {
|
|
|
|
v->breakdown_ctr = 1;
|
|
|
|
v->vehstatus |= VS_AIRCRAFT_BROKEN;
|
|
|
|
|
|
|
|
if (v->breakdowns_since_last_service != 255)
|
|
|
|
v->breakdowns_since_last_service++;
|
|
|
|
InvalidateWindow(WC_VEHICLE_VIEW, v->index);
|
|
|
|
InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void HandleAircraftSmoke(Vehicle *v)
|
|
|
|
{
|
2005-11-16 12:01:46 +00:00
|
|
|
static const struct {
|
|
|
|
int8 x;
|
|
|
|
int8 y;
|
|
|
|
} smoke_pos[] = {
|
|
|
|
{ 5, 5 },
|
|
|
|
{ 6, 0 },
|
|
|
|
{ 5, -5 },
|
|
|
|
{ 0, -6 },
|
|
|
|
{ -5, -5 },
|
|
|
|
{ -6, 0 },
|
|
|
|
{ -5, 5 },
|
|
|
|
{ 0, 6 }
|
|
|
|
};
|
|
|
|
|
2005-11-14 19:48:04 +00:00
|
|
|
if (!(v->vehstatus & VS_AIRCRAFT_BROKEN)) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
if (v->cur_speed < 10) {
|
|
|
|
v->vehstatus &= ~VS_AIRCRAFT_BROKEN;
|
|
|
|
v->breakdown_ctr = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((v->tick_counter & 0x1F) == 0) {
|
|
|
|
CreateEffectVehicleRel(v,
|
2005-11-16 12:01:46 +00:00
|
|
|
smoke_pos[v->direction].x,
|
|
|
|
smoke_pos[v->direction].y,
|
2004-08-09 17:04:08 +00:00
|
|
|
2,
|
2005-02-12 15:53:32 +00:00
|
|
|
EV_SMOKE
|
2004-08-09 17:04:08 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ProcessAircraftOrder(Vehicle *v)
|
|
|
|
{
|
2005-01-16 14:42:53 +00:00
|
|
|
const Order *order;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-02-10 06:33:56 +00:00
|
|
|
switch (v->current_order.type) {
|
|
|
|
case OT_GOTO_DEPOT:
|
|
|
|
if (!(v->current_order.flags & OF_PART_OF_ORDERS)) return;
|
|
|
|
if (v->current_order.flags & OF_SERVICE_IF_NEEDED &&
|
|
|
|
!VehicleNeedsService(v)) {
|
|
|
|
v->cur_order_index++;
|
|
|
|
}
|
|
|
|
break;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2006-02-10 06:33:56 +00:00
|
|
|
case OT_LOADING: return;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-11-14 19:48:04 +00:00
|
|
|
if (v->cur_order_index >= v->num_orders) v->cur_order_index = 0;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-01-15 19:06:22 +00:00
|
|
|
order = GetVehicleOrder(v, v->cur_order_index);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-01-15 19:06:22 +00:00
|
|
|
if (order == NULL) {
|
2004-12-05 12:43:04 +00:00
|
|
|
v->current_order.type = OT_NOTHING;
|
|
|
|
v->current_order.flags = 0;
|
2004-08-09 17:04:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2006-02-10 06:33:56 +00:00
|
|
|
if (order->type == OT_DUMMY && !CheckForValidOrders(v)) CrashAirplane(v);
|
2005-01-05 13:48:38 +00:00
|
|
|
|
2005-01-15 19:06:22 +00:00
|
|
|
if (order->type == v->current_order.type &&
|
|
|
|
order->flags == v->current_order.flags &&
|
|
|
|
order->station == v->current_order.station)
|
2004-08-09 17:04:08 +00:00
|
|
|
return;
|
|
|
|
|
2005-01-15 19:06:22 +00:00
|
|
|
v->current_order = *order;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
// orders are changed in flight, ensure going to the right station
|
2005-01-15 19:06:22 +00:00
|
|
|
if (order->type == OT_GOTO_STATION && v->u.air.state == FLYING) {
|
2004-08-09 17:04:08 +00:00
|
|
|
AircraftNextAirportPos_and_Order(v);
|
2005-01-15 19:06:22 +00:00
|
|
|
v->u.air.targetairport = order->station;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2005-01-15 19:06:22 +00:00
|
|
|
InvalidateVehicleOrder(v);
|
|
|
|
|
2005-01-18 09:35:31 +00:00
|
|
|
InvalidateWindowClasses(WC_AIRCRAFT_LIST);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void HandleAircraftLoading(Vehicle *v, int mode)
|
|
|
|
{
|
2005-11-14 19:48:04 +00:00
|
|
|
if (v->current_order.type == OT_NOTHING) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2004-12-05 12:43:04 +00:00
|
|
|
if (v->current_order.type != OT_DUMMY) {
|
2005-11-14 19:48:04 +00:00
|
|
|
if (v->current_order.type != OT_LOADING) return;
|
|
|
|
if (mode != 0) return;
|
|
|
|
if (--v->load_unload_time_rem != 0) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2004-12-05 12:43:04 +00:00
|
|
|
if (v->current_order.flags & OF_FULL_LOAD && CanFillVehicle(v)) {
|
2004-08-09 17:04:08 +00:00
|
|
|
SET_EXPENSES_TYPE(EXPENSES_AIRCRAFT_INC);
|
|
|
|
LoadUnloadVehicle(v);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2004-12-05 12:43:04 +00:00
|
|
|
Order b = v->current_order;
|
|
|
|
v->current_order.type = OT_NOTHING;
|
|
|
|
v->current_order.flags = 0;
|
2005-11-14 19:48:04 +00:00
|
|
|
if (!(b.flags & OF_NON_STOP)) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
v->cur_order_index++;
|
2005-01-15 19:06:22 +00:00
|
|
|
InvalidateVehicleOrder(v);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2005-01-05 13:15:27 +00:00
|
|
|
static void CrashAirplane(Vehicle *v)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
uint16 amt;
|
2005-01-05 13:15:27 +00:00
|
|
|
Station *st;
|
|
|
|
StringID newsitem;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
v->vehstatus |= VS_CRASHED;
|
|
|
|
v->u.air.crashed_counter = 0;
|
|
|
|
|
2005-02-12 15:53:32 +00:00
|
|
|
CreateEffectVehicleRel(v, 4, 4, 8, EV_EXPLOSION_LARGE);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
InvalidateWindow(WC_VEHICLE_VIEW, v->index);
|
|
|
|
|
|
|
|
amt = 2;
|
|
|
|
if (v->cargo_type == CT_PASSENGERS) amt += v->cargo_count;
|
2004-12-02 22:53:07 +00:00
|
|
|
SetDParam(0, amt);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
v->cargo_count = 0;
|
|
|
|
v->next->cargo_count = 0,
|
2005-01-06 22:31:58 +00:00
|
|
|
st = GetStation(v->u.air.targetairport);
|
2005-11-14 19:48:04 +00:00
|
|
|
if (st->airport_tile == 0) {
|
2005-01-05 13:15:27 +00:00
|
|
|
newsitem = STR_PLANE_CRASH_OUT_OF_FUEL;
|
|
|
|
} else {
|
|
|
|
SetDParam(1, st->index);
|
|
|
|
newsitem = STR_A034_PLANE_CRASH_DIE_IN_FIREBALL;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2004-12-02 22:53:07 +00:00
|
|
|
SetDParam(1, st->index);
|
2005-01-05 13:15:27 +00:00
|
|
|
AddNewsItem(newsitem,
|
2004-08-09 17:04:08 +00:00
|
|
|
NEWS_FLAGS(NM_THIN, NF_VIEWPORT|NF_VEHICLE, NT_ACCIDENT, 0),
|
|
|
|
v->index,
|
|
|
|
0);
|
|
|
|
|
2004-12-04 09:26:39 +00:00
|
|
|
SndPlayVehicleFx(SND_12_EXPLOSION, v);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2005-01-05 13:15:27 +00:00
|
|
|
static void MaybeCrashAirplane(Vehicle *v)
|
|
|
|
{
|
|
|
|
Station *st;
|
|
|
|
uint16 prob;
|
2005-10-23 13:04:44 +00:00
|
|
|
uint i;
|
2005-01-05 13:15:27 +00:00
|
|
|
|
2005-01-06 22:31:58 +00:00
|
|
|
st = GetStation(v->u.air.targetairport);
|
2005-01-05 13:15:27 +00:00
|
|
|
|
|
|
|
//FIXME -- MaybeCrashAirplane -> increase crashing chances of very modern airplanes on smaller than AT_METROPOLITAN airports
|
|
|
|
prob = 0x10000 / 1500;
|
|
|
|
if (st->airport_type == AT_SMALL && (AircraftVehInfo(v->engine_type)->subtype & 2) && !_cheats.no_jetcrash.value) {
|
|
|
|
prob = 0x10000 / 20;
|
|
|
|
}
|
|
|
|
|
2005-10-03 21:20:01 +00:00
|
|
|
if (GB(Random(), 0, 16) > prob) return;
|
2005-01-05 13:15:27 +00:00
|
|
|
|
|
|
|
// Crash the airplane. Remove all goods stored at the station.
|
2005-10-23 13:04:44 +00:00
|
|
|
for (i = 0; i != NUM_CARGO; i++) {
|
2005-01-05 13:15:27 +00:00
|
|
|
st->goods[i].rating = 1;
|
2005-10-11 13:54:21 +00:00
|
|
|
SB(st->goods[i].waiting_acceptance, 0, 12, 0);
|
2005-01-05 13:15:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CrashAirplane(v);
|
|
|
|
}
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// we've landed and just arrived at a terminal
|
|
|
|
static void AircraftEntersTerminal(Vehicle *v)
|
|
|
|
{
|
|
|
|
Station *st;
|
2004-12-05 12:43:04 +00:00
|
|
|
Order old_order;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-11-14 19:48:04 +00:00
|
|
|
if (v->current_order.type == OT_GOTO_DEPOT) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-01-06 22:31:58 +00:00
|
|
|
st = GetStation(v->u.air.targetairport);
|
2004-08-09 17:04:08 +00:00
|
|
|
v->last_station_visited = v->u.air.targetairport;
|
|
|
|
|
|
|
|
/* Check if station was ever visited before */
|
|
|
|
if (!(st->had_vehicle_of_type & HVOT_AIRCRAFT)) {
|
|
|
|
uint32 flags;
|
|
|
|
|
|
|
|
st->had_vehicle_of_type |= HVOT_AIRCRAFT;
|
2004-12-02 22:53:07 +00:00
|
|
|
SetDParam(0, st->index);
|
2004-08-09 17:04:08 +00:00
|
|
|
// show newsitem of celebrating citizens
|
|
|
|
flags = (v->owner == _local_player) ? NEWS_FLAGS(NM_THIN, NF_VIEWPORT|NF_VEHICLE, NT_ARRIVAL_PLAYER, 0) : NEWS_FLAGS(NM_THIN, NF_VIEWPORT|NF_VEHICLE, NT_ARRIVAL_OTHER, 0);
|
|
|
|
AddNewsItem(
|
|
|
|
STR_A033_CITIZENS_CELEBRATE_FIRST,
|
|
|
|
flags,
|
|
|
|
v->index,
|
|
|
|
0);
|
|
|
|
}
|
|
|
|
|
2004-12-05 12:43:04 +00:00
|
|
|
old_order = v->current_order;
|
|
|
|
v->current_order.type = OT_LOADING;
|
|
|
|
v->current_order.flags = 0;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2004-12-05 12:43:04 +00:00
|
|
|
if (old_order.type == OT_GOTO_STATION &&
|
|
|
|
v->current_order.station == v->last_station_visited) {
|
|
|
|
v->current_order.flags =
|
|
|
|
(old_order.flags & (OF_FULL_LOAD | OF_UNLOAD)) | OF_NON_STOP;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SET_EXPENSES_TYPE(EXPENSES_AIRCRAFT_INC);
|
|
|
|
LoadUnloadVehicle(v);
|
2004-12-21 23:27:58 +00:00
|
|
|
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
|
2005-01-18 09:35:31 +00:00
|
|
|
InvalidateWindowClasses(WC_AIRCRAFT_LIST);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2005-11-14 19:48:04 +00:00
|
|
|
static bool ValidateAircraftInHangar(uint data_a, uint data_b)
|
2005-01-19 10:00:37 +00:00
|
|
|
{
|
2005-11-13 13:43:55 +00:00
|
|
|
const Vehicle* v = GetVehicle(data_a);
|
2005-01-19 10:00:37 +00:00
|
|
|
|
|
|
|
return (IsAircraftHangarTile(v->tile) && (v->vehstatus & VS_STOPPED));
|
|
|
|
}
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
static void AircraftEnterHangar(Vehicle *v)
|
|
|
|
{
|
2004-12-05 12:43:04 +00:00
|
|
|
Order old_order;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
ServiceAircraft(v);
|
2005-01-18 09:35:31 +00:00
|
|
|
InvalidateWindowClasses(WC_AIRCRAFT_LIST);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-11-17 08:52:47 +00:00
|
|
|
TriggerVehicle(v, VEHICLE_TRIGGER_DEPOT);
|
|
|
|
|
2004-12-05 12:43:04 +00:00
|
|
|
if (v->current_order.type == OT_GOTO_DEPOT) {
|
2004-08-09 17:04:08 +00:00
|
|
|
InvalidateWindow(WC_VEHICLE_VIEW, v->index);
|
|
|
|
|
2004-12-05 12:43:04 +00:00
|
|
|
old_order = v->current_order;
|
|
|
|
v->current_order.type = OT_NOTHING;
|
|
|
|
v->current_order.flags = 0;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-03-20 08:43:29 +00:00
|
|
|
if (HASBIT(old_order.flags, OFB_PART_OF_ORDERS)) {
|
2004-12-05 12:43:04 +00:00
|
|
|
v->cur_order_index++;
|
2005-03-20 08:43:29 +00:00
|
|
|
} else if (HASBIT(old_order.flags, OFB_HALT_IN_DEPOT)) { // force depot visit
|
2004-08-09 17:04:08 +00:00
|
|
|
v->vehstatus |= VS_STOPPED;
|
2005-01-18 09:35:31 +00:00
|
|
|
InvalidateWindowClasses(WC_AIRCRAFT_LIST);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
if (v->owner == _local_player) {
|
2004-12-02 22:53:07 +00:00
|
|
|
SetDParam(0, v->unitnumber);
|
2005-01-19 10:00:37 +00:00
|
|
|
AddValidatedNewsItem(
|
2004-08-09 17:04:08 +00:00
|
|
|
STR_A014_AIRCRAFT_IS_WAITING_IN,
|
|
|
|
NEWS_FLAGS(NM_SMALL, NF_VIEWPORT|NF_VEHICLE, NT_ADVICE, 0),
|
|
|
|
v->index,
|
2005-01-19 10:00:37 +00:00
|
|
|
0,
|
|
|
|
ValidateAircraftInHangar);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void AircraftLand(Vehicle *v)
|
|
|
|
{
|
|
|
|
v->sprite_width = v->sprite_height = 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void AircraftLandAirplane(Vehicle *v)
|
|
|
|
{
|
|
|
|
AircraftLand(v);
|
2004-12-04 09:26:39 +00:00
|
|
|
SndPlayVehicleFx(SND_17_SKID_PLANE, v);
|
2004-08-09 17:04:08 +00:00
|
|
|
MaybeCrashAirplane(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
// set the right pos when heading to other airports after takeoff
|
|
|
|
static void AircraftNextAirportPos_and_Order(Vehicle *v)
|
|
|
|
{
|
2005-11-13 13:43:55 +00:00
|
|
|
const Station* st;
|
2004-08-09 17:04:08 +00:00
|
|
|
const AirportFTAClass *Airport;
|
|
|
|
|
2004-12-05 12:43:04 +00:00
|
|
|
if (v->current_order.type == OT_GOTO_STATION ||
|
|
|
|
v->current_order.type == OT_GOTO_DEPOT)
|
|
|
|
v->u.air.targetairport = v->current_order.station;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-01-06 22:31:58 +00:00
|
|
|
st = GetStation(v->u.air.targetairport);
|
2004-08-09 17:04:08 +00:00
|
|
|
Airport = GetAirport(st->airport_type);
|
|
|
|
v->u.air.pos = v->u.air.previous_pos = Airport->entry_point;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void AircraftLeaveHangar(Vehicle *v)
|
|
|
|
{
|
|
|
|
v->cur_speed = 0;
|
|
|
|
v->subspeed = 0;
|
|
|
|
v->progress = 0;
|
|
|
|
v->direction = 3;
|
|
|
|
v->vehstatus &= ~VS_HIDDEN;
|
|
|
|
{
|
|
|
|
Vehicle *u = v->next;
|
|
|
|
u->vehstatus &= ~VS_HIDDEN;
|
|
|
|
|
|
|
|
// Rotor blades
|
2005-11-14 19:48:04 +00:00
|
|
|
u = u->next;
|
|
|
|
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);
|
|
|
|
InvalidateWindow(WC_VEHICLE_DEPOT, v->tile);
|
2005-01-18 09:35:31 +00:00
|
|
|
InvalidateWindowClasses(WC_AIRCRAFT_LIST);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
/////////////////// AIRCRAFT MOVEMENT SCHEME ////////////////////////////////
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
static void AircraftEventHandler_EnterTerminal(Vehicle *v, const AirportFTAClass *Airport)
|
|
|
|
{
|
|
|
|
AircraftEntersTerminal(v);
|
|
|
|
v->u.air.state = Airport->layout[v->u.air.pos].heading;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void AircraftEventHandler_EnterHangar(Vehicle *v, const AirportFTAClass *Airport)
|
|
|
|
{
|
|
|
|
AircraftEnterHangar(v);
|
|
|
|
v->u.air.state = Airport->layout[v->u.air.pos].heading;
|
|
|
|
}
|
|
|
|
|
|
|
|
// In an Airport Hangar
|
|
|
|
static void AircraftEventHandler_InHangar(Vehicle *v, const AirportFTAClass *Airport)
|
|
|
|
{
|
|
|
|
// if we just arrived, execute EnterHangar first
|
|
|
|
if (v->u.air.previous_pos != v->u.air.pos) {
|
|
|
|
AircraftEventHandler_EnterHangar(v, Airport);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2004-12-05 12:43:04 +00:00
|
|
|
// if we were sent to the depot, stay there
|
|
|
|
if (v->current_order.type == OT_GOTO_DEPOT && (v->vehstatus & VS_STOPPED)) {
|
|
|
|
v->current_order.type = OT_NOTHING;
|
|
|
|
v->current_order.flags = 0;
|
2004-08-09 17:04:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2004-12-05 12:43:04 +00:00
|
|
|
if (v->current_order.type != OT_GOTO_STATION &&
|
|
|
|
v->current_order.type != OT_GOTO_DEPOT)
|
2004-08-09 17:04:08 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
// if the block of the next position is busy, stay put
|
2005-10-23 13:04:44 +00:00
|
|
|
if (AirportHasBlock(v, &Airport->layout[v->u.air.pos], Airport)) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
// We are already at the target airport, we need to find a terminal
|
2004-12-05 12:43:04 +00:00
|
|
|
if (v->current_order.station == v->u.air.targetairport) {
|
2004-08-09 17:04:08 +00:00
|
|
|
// FindFreeTerminal:
|
|
|
|
// 1. Find a free terminal, 2. Occupy it, 3. Set the vehicle's state to that terminal
|
2005-10-23 13:04:44 +00:00
|
|
|
if (v->subtype != 0) {
|
|
|
|
if (!AirportFindFreeTerminal(v, Airport)) return; // airplane
|
|
|
|
} else {
|
|
|
|
if (!AirportFindFreeHelipad(v, Airport)) return; // helicopter
|
|
|
|
}
|
|
|
|
} else { // Else prepare for launch.
|
2004-08-09 17:04:08 +00:00
|
|
|
// airplane goto state takeoff, helicopter to helitakeoff
|
|
|
|
v->u.air.state = (v->subtype != 0) ? TAKEOFF : HELITAKEOFF;
|
|
|
|
}
|
|
|
|
AircraftLeaveHangar(v);
|
|
|
|
AirportMove(v, Airport);
|
|
|
|
}
|
|
|
|
|
|
|
|
// At one of the Airport's Terminals
|
|
|
|
static void AircraftEventHandler_AtTerminal(Vehicle *v, const AirportFTAClass *Airport)
|
|
|
|
{
|
|
|
|
// if we just arrived, execute EnterTerminal first
|
|
|
|
if (v->u.air.previous_pos != v->u.air.pos) {
|
|
|
|
AircraftEventHandler_EnterTerminal(v, Airport);
|
|
|
|
// on an airport with helipads, a helicopter will always land there
|
|
|
|
// and get serviced at the same time - patch setting
|
|
|
|
if (_patches.serviceathelipad) {
|
2005-01-09 08:49:40 +00:00
|
|
|
if (v->subtype == 0 && Airport->helipads != NULL) {
|
2004-08-09 17:04:08 +00:00
|
|
|
// an exerpt of ServiceAircraft, without the invisibility stuff
|
|
|
|
v->date_of_last_service = _date;
|
|
|
|
v->breakdowns_since_last_service = 0;
|
2005-06-07 18:13:49 +00:00
|
|
|
v->reliability = GetEngine(v->engine_type)->reliability;
|
2004-08-09 17:04:08 +00:00
|
|
|
InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2004-12-05 12:43:04 +00:00
|
|
|
if (v->current_order.type == OT_NOTHING) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
// if the block of the next position is busy, stay put
|
|
|
|
if (AirportHasBlock(v, &Airport->layout[v->u.air.pos], Airport)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// airport-road is free. We either have to go to another airport, or to the hangar
|
|
|
|
// ---> start moving
|
|
|
|
|
2004-12-05 12:43:04 +00:00
|
|
|
switch (v->current_order.type) {
|
2004-08-09 17:04:08 +00:00
|
|
|
case OT_GOTO_STATION: // ready to fly to another airport
|
|
|
|
// airplane goto state takeoff, helicopter to helitakeoff
|
|
|
|
v->u.air.state = (v->subtype != 0) ? TAKEOFF : HELITAKEOFF;
|
|
|
|
break;
|
|
|
|
case OT_GOTO_DEPOT: // visit hangar for serivicing, sale, etc.
|
2005-11-14 19:48:04 +00:00
|
|
|
if (v->current_order.station == v->u.air.targetairport) {
|
2004-08-09 17:04:08 +00:00
|
|
|
v->u.air.state = HANGAR;
|
2005-11-14 19:48:04 +00:00
|
|
|
} else {
|
2004-08-09 17:04:08 +00:00
|
|
|
v->u.air.state = (v->subtype != 0) ? TAKEOFF : HELITAKEOFF;
|
2005-11-14 19:48:04 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
break;
|
|
|
|
default: // orders have been deleted (no orders), goto depot and don't bother us
|
2004-12-05 12:43:04 +00:00
|
|
|
v->current_order.type = OT_NOTHING;
|
|
|
|
v->current_order.flags = 0;
|
2004-08-09 17:04:08 +00:00
|
|
|
v->u.air.state = HANGAR;
|
|
|
|
}
|
|
|
|
AirportMove(v, Airport);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void AircraftEventHandler_General(Vehicle *v, const AirportFTAClass *Airport)
|
|
|
|
{
|
2004-08-16 22:35:17 +00:00
|
|
|
DEBUG(misc, 0) ("OK, you shouldn't be here, check your Airport Scheme!");
|
|
|
|
assert(0);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void AircraftEventHandler_TakeOff(Vehicle *v, const AirportFTAClass *Airport) {
|
|
|
|
PlayAircraftSound(v); // play takeoffsound for airplanes
|
|
|
|
v->u.air.state = STARTTAKEOFF;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void AircraftEventHandler_StartTakeOff(Vehicle *v, const AirportFTAClass *Airport)
|
|
|
|
{
|
|
|
|
v->sprite_width = v->sprite_height = 24; // ??? no idea what this is
|
|
|
|
v->u.air.state = ENDTAKEOFF;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void AircraftEventHandler_EndTakeOff(Vehicle *v, const AirportFTAClass *Airport)
|
|
|
|
{
|
|
|
|
v->u.air.state = FLYING;
|
|
|
|
// get the next position to go to, differs per airport
|
|
|
|
AircraftNextAirportPos_and_Order(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void AircraftEventHandler_HeliTakeOff(Vehicle *v, const AirportFTAClass *Airport)
|
|
|
|
{
|
2005-09-30 20:37:25 +00:00
|
|
|
const Player* p = GetPlayer(v->owner);
|
2004-08-09 17:04:08 +00:00
|
|
|
v->sprite_width = v->sprite_height = 24; // ??? no idea what this is
|
|
|
|
v->u.air.state = FLYING;
|
|
|
|
// get the next position to go to, differs per airport
|
|
|
|
AircraftNextAirportPos_and_Order(v);
|
2005-01-24 10:50:57 +00:00
|
|
|
|
|
|
|
// check if the aircraft needs to be replaced or renewed and send it to a hangar if needed
|
2005-11-14 19:48:04 +00:00
|
|
|
if (v->owner == _local_player && (
|
2006-01-12 15:52:18 +00:00
|
|
|
EngineHasReplacementForPlayer(p, v->engine_type) ||
|
2005-11-14 19:48:04 +00:00
|
|
|
(p->engine_renew && v->age - v->max_age > p->engine_renew_months * 30)
|
|
|
|
)) {
|
2005-03-19 21:16:22 +00:00
|
|
|
_current_player = _local_player;
|
|
|
|
DoCommandP(v->tile, v->index, 1, NULL, CMD_SEND_AIRCRAFT_TO_HANGAR | CMD_SHOW_NO_ERROR);
|
|
|
|
_current_player = OWNER_NONE;
|
2005-01-24 10:50:57 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void AircraftEventHandler_Flying(Vehicle *v, const AirportFTAClass *Airport)
|
|
|
|
{
|
|
|
|
Station *st;
|
|
|
|
byte landingtype;
|
|
|
|
AirportFTA *current;
|
|
|
|
uint16 tcur_speed, tsubspeed;
|
|
|
|
|
2005-01-06 22:31:58 +00:00
|
|
|
st = GetStation(v->u.air.targetairport);
|
2004-08-09 17:04:08 +00:00
|
|
|
// flying device is accepted at this station
|
|
|
|
// small airport --> no helicopters (AIRCRAFT_ONLY)
|
|
|
|
// all other airports --> all types of flying devices (ALL)
|
|
|
|
// heliport/oilrig, etc --> no airplanes (HELICOPTERS_ONLY)
|
|
|
|
// runway busy or not allowed to use this airstation, circle
|
2005-11-14 19:48:04 +00:00
|
|
|
if (v->subtype != Airport->acc_planes &&
|
|
|
|
st->airport_tile != 0 &&
|
|
|
|
(st->owner == OWNER_NONE || st->owner == v->owner)) {
|
2004-08-09 17:04:08 +00:00
|
|
|
// {32,FLYING,NOTHING_block,37}, {32,LANDING,N,33}, {32,HELILANDING,N,41},
|
|
|
|
// if it is an airplane, look for LANDING, for helicopter HELILANDING
|
|
|
|
// it is possible to choose from multiple landing runways, so loop until a free one is found
|
|
|
|
landingtype = (v->subtype != 0) ? LANDING : HELILANDING;
|
|
|
|
current = Airport->layout[v->u.air.pos].next_in_chain;
|
|
|
|
while (current != NULL) {
|
|
|
|
if (current->heading == landingtype) {
|
|
|
|
// 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
|
|
|
|
tcur_speed = v->cur_speed;
|
|
|
|
tsubspeed = v->subspeed;
|
|
|
|
if (!AirportHasBlock(v, current, Airport)) {
|
|
|
|
v->u.air.state = landingtype; // LANDING / HELILANDING
|
|
|
|
// 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!
|
|
|
|
v->u.air.pos = current->next_position;
|
|
|
|
SETBITS(st->airport_flags, Airport->layout[v->u.air.pos].block);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
v->cur_speed = tcur_speed;
|
|
|
|
v->subspeed = tsubspeed;
|
|
|
|
}
|
|
|
|
current = current->next_in_chain;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
v->u.air.state = FLYING;
|
|
|
|
v->u.air.pos = Airport->layout[v->u.air.pos].next_position;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void AircraftEventHandler_Landing(Vehicle *v, const AirportFTAClass *Airport)
|
|
|
|
{
|
2005-09-30 20:37:25 +00:00
|
|
|
const Player* p = GetPlayer(v->owner);
|
2004-08-09 17:04:08 +00:00
|
|
|
AircraftLandAirplane(v); // maybe crash airplane
|
|
|
|
v->u.air.state = ENDLANDING;
|
2005-01-27 20:38:19 +00:00
|
|
|
// check if the aircraft needs to be replaced or renewed and send it to a hangar if needed
|
|
|
|
if (v->current_order.type != OT_GOTO_DEPOT && v->owner == _local_player) {
|
|
|
|
// only the vehicle owner needs to calculate the rest (locally)
|
2006-01-12 15:52:18 +00:00
|
|
|
if (EngineHasReplacementForPlayer(p, v->engine_type) ||
|
2005-08-06 16:07:22 +00:00
|
|
|
(p->engine_renew && v->age - v->max_age > (p->engine_renew_months * 30))) {
|
2005-05-12 00:11:37 +00:00
|
|
|
// send the aircraft to the hangar at next airport (bit 17 set)
|
2005-01-27 20:38:19 +00:00
|
|
|
_current_player = _local_player;
|
|
|
|
DoCommandP(v->tile, v->index, 1 << 16, NULL, CMD_SEND_AIRCRAFT_TO_HANGAR | CMD_SHOW_NO_ERROR);
|
|
|
|
_current_player = OWNER_NONE;
|
|
|
|
}
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void AircraftEventHandler_HeliLanding(Vehicle *v, const AirportFTAClass *Airport)
|
|
|
|
{
|
|
|
|
AircraftLand(v); // helicopters don't crash
|
|
|
|
v->u.air.state = HELIENDLANDING;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void AircraftEventHandler_EndLanding(Vehicle *v, const AirportFTAClass *Airport)
|
|
|
|
{
|
|
|
|
// next block busy, don't do a thing, just wait
|
2005-10-23 13:04:44 +00:00
|
|
|
if (AirportHasBlock(v, &Airport->layout[v->u.air.pos], Airport)) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
// if going to terminal (OT_GOTO_STATION) choose one
|
|
|
|
// 1. in case all terminals are busy AirportFindFreeTerminal() returns false or
|
|
|
|
// 2. not going for terminal (but depot, no order),
|
|
|
|
// --> get out of the way to the hangar.
|
2004-12-05 12:43:04 +00:00
|
|
|
if (v->current_order.type == OT_GOTO_STATION) {
|
2005-10-23 13:04:44 +00:00
|
|
|
if (AirportFindFreeTerminal(v, Airport)) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
v->u.air.state = HANGAR;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static void AircraftEventHandler_HeliEndLanding(Vehicle *v, const AirportFTAClass *Airport)
|
|
|
|
{
|
|
|
|
// next block busy, don't do a thing, just wait
|
2005-10-23 13:04:44 +00:00
|
|
|
if (AirportHasBlock(v, &Airport->layout[v->u.air.pos], Airport)) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
// if going to helipad (OT_GOTO_STATION) choose one. If airport doesn't have helipads, choose terminal
|
|
|
|
// 1. in case all terminals/helipads are busy (AirportFindFreeHelipad() returns false) or
|
|
|
|
// 2. not going for terminal (but depot, no order),
|
|
|
|
// --> get out of the way to the hangar IF there are terminals on the airport.
|
|
|
|
// --> else TAKEOFF
|
|
|
|
// the reason behind this is that if an airport has a terminal, it also has a hangar. Airplanes
|
|
|
|
// must go to a hangar.
|
2004-12-05 12:43:04 +00:00
|
|
|
if (v->current_order.type == OT_GOTO_STATION) {
|
2005-10-23 13:04:44 +00:00
|
|
|
if (AirportFindFreeHelipad(v, Airport)) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2005-01-09 08:49:40 +00:00
|
|
|
v->u.air.state = (Airport->terminals != NULL) ? HANGAR : HELITAKEOFF;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
typedef void AircraftStateHandler(Vehicle *v, const AirportFTAClass *Airport);
|
|
|
|
static AircraftStateHandler * const _aircraft_state_handlers[] = {
|
|
|
|
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
|
|
|
|
};
|
|
|
|
|
2005-11-13 13:43:55 +00:00
|
|
|
static void AirportClearBlock(const Vehicle* v, const AirportFTAClass* Airport)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
// we have left the previous block, and entered the new one. Free the previous block
|
|
|
|
if (Airport->layout[v->u.air.previous_pos].block != Airport->layout[v->u.air.pos].block) {
|
2005-11-14 19:48:04 +00:00
|
|
|
Station* st = GetStation(v->u.air.targetairport);
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
CLRBITS(st->airport_flags, Airport->layout[v->u.air.previous_pos].block);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void AirportGoToNextPosition(Vehicle *v, const AirportFTAClass *Airport)
|
|
|
|
{
|
|
|
|
// 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
|
|
|
|
|
|
|
AirportClearBlock(v, Airport);
|
|
|
|
AirportMove(v, Airport); // move aircraft to next position
|
|
|
|
}
|
|
|
|
|
|
|
|
// gets pos from vehicle and next orders
|
|
|
|
static bool AirportMove(Vehicle *v, const AirportFTAClass *Airport)
|
|
|
|
{
|
|
|
|
AirportFTA *current;
|
|
|
|
byte prev_pos;
|
|
|
|
bool retval = false;
|
|
|
|
|
|
|
|
// error handling
|
|
|
|
if (v->u.air.pos >= Airport->nofelements) {
|
2004-08-16 22:35:17 +00:00
|
|
|
DEBUG(misc, 0) ("position %d is not valid for current airport. Max position is %d", v->u.air.pos, Airport->nofelements-1);
|
2004-08-09 17:04:08 +00:00
|
|
|
assert(v->u.air.pos < Airport->nofelements);
|
|
|
|
}
|
|
|
|
|
|
|
|
current = &Airport->layout[v->u.air.pos];
|
|
|
|
// we have arrived in an important state (eg terminal, hangar, etc.)
|
|
|
|
if (current->heading == v->u.air.state) {
|
|
|
|
prev_pos = v->u.air.pos; // location could be changed in state, so save it before-hand
|
|
|
|
_aircraft_state_handlers[v->u.air.state](v, Airport);
|
2005-10-23 13:04:44 +00:00
|
|
|
if (v->u.air.state != FLYING) v->u.air.previous_pos = prev_pos;
|
2004-08-09 17:04:08 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
v->u.air.previous_pos = v->u.air.pos; // save previous location
|
|
|
|
|
|
|
|
// there is only one choice to move to
|
|
|
|
if (current->next_in_chain == NULL) {
|
|
|
|
if (AirportSetBlocks(v, current, Airport)) {
|
|
|
|
v->u.air.pos = current->next_position;
|
|
|
|
} // move to next position
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
// there are more choices to choose from, choose the one that
|
|
|
|
// matches our heading
|
|
|
|
do {
|
|
|
|
if (v->u.air.state == current->heading || current->heading == TO_ALL) {
|
2005-11-14 19:48:04 +00:00
|
|
|
if (AirportSetBlocks(v, current, Airport)) {
|
|
|
|
v->u.air.pos = current->next_position;
|
|
|
|
} // move to next position
|
|
|
|
return retval;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
current = current->next_in_chain;
|
|
|
|
} while (current != NULL);
|
|
|
|
|
2004-08-16 22:35:17 +00:00
|
|
|
DEBUG(misc, 0) ("Cannot move further on Airport...! pos:%d state:%d", v->u.air.pos, v->u.air.state);
|
|
|
|
DEBUG(misc, 0) ("Airport entry point: %d, Vehicle: %d", Airport->entry_point, v->index);
|
2004-08-09 17:04:08 +00:00
|
|
|
assert(0);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// returns true if the road ahead is busy, eg. you must wait before proceeding
|
|
|
|
static bool AirportHasBlock(Vehicle *v, AirportFTA *current_pos, const AirportFTAClass *Airport)
|
|
|
|
{
|
2005-11-13 13:43:55 +00:00
|
|
|
const AirportFTA* reference = &Airport->layout[v->u.air.pos];
|
|
|
|
const AirportFTA* next = &Airport->layout[current_pos->next_position];
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
// same block, then of course we can move
|
|
|
|
if (Airport->layout[current_pos->position].block != next->block) {
|
2005-11-13 13:43:55 +00:00
|
|
|
const Station* st = GetStation(v->u.air.targetairport);
|
|
|
|
uint32 airport_flags = next->block;
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// check additional possible extra blocks
|
|
|
|
if (current_pos != reference && current_pos->block != NOTHING_block) {
|
|
|
|
airport_flags |= current_pos->block;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (HASBITS(st->airport_flags, airport_flags)) {
|
|
|
|
v->cur_speed = 0;
|
|
|
|
v->subspeed = 0;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// returns true on success. Eg, next block was free and we have occupied it
|
|
|
|
static bool AirportSetBlocks(Vehicle *v, AirportFTA *current_pos, const AirportFTAClass *Airport)
|
|
|
|
{
|
2005-11-14 19:48:04 +00:00
|
|
|
AirportFTA* next = &Airport->layout[current_pos->next_position];
|
|
|
|
AirportFTA* reference = &Airport->layout[v->u.air.pos];
|
|
|
|
AirportFTA* current;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
// if the next position is in another block, check it and wait until it is free
|
|
|
|
if (Airport->layout[current_pos->position].block != next->block) {
|
2005-11-14 19:48:04 +00:00
|
|
|
uint32 airport_flags = next->block;
|
|
|
|
Station* st = GetStation(v->u.air.targetairport);
|
|
|
|
|
2004-08-09 17:04:08 +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
|
|
|
|
current = current_pos;
|
2005-10-23 13:04:44 +00:00
|
|
|
if (current == reference) current = current->next_in_chain;
|
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;
|
|
|
|
}
|
|
|
|
current = current->next_in_chain;
|
|
|
|
};
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
if (HASBITS(st->airport_flags, airport_flags)) {
|
|
|
|
v->cur_speed = 0;
|
|
|
|
v->subspeed = 0;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (next->block != NOTHING_block) {
|
|
|
|
SETBITS(st->airport_flags, airport_flags); // occupy next block
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool FreeTerminal(Vehicle *v, byte i, byte last_terminal)
|
|
|
|
{
|
2005-01-06 22:31:58 +00:00
|
|
|
Station *st = GetStation(v->u.air.targetairport);
|
2004-08-09 17:04:08 +00:00
|
|
|
for (; i < last_terminal; i++) {
|
|
|
|
if (!HASBIT(st->airport_flags, i)) {
|
|
|
|
// TERMINAL# HELIPAD#
|
|
|
|
v->u.air.state = i + TERM1; // start moving to that terminal/helipad
|
|
|
|
SETBIT(st->airport_flags, i); // occupy terminal/helipad
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2005-11-14 19:48:04 +00:00
|
|
|
static uint GetNumTerminals(const AirportFTAClass *Airport)
|
2005-01-09 08:49:40 +00:00
|
|
|
{
|
2005-11-14 19:48:04 +00:00
|
|
|
uint num = 0;
|
|
|
|
uint i;
|
2005-01-15 08:58:31 +00:00
|
|
|
|
2005-11-14 19:48:04 +00:00
|
|
|
for (i = Airport->terminals[0]; i > 0; i--) num += Airport->terminals[i];
|
2005-01-15 08:58:31 +00:00
|
|
|
|
2005-01-09 08:49:40 +00:00
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
2004-09-10 19:02:27 +00:00
|
|
|
static bool AirportFindFreeTerminal(Vehicle *v, const AirportFTAClass *Airport)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
AirportFTA *temp;
|
|
|
|
Station *st;
|
|
|
|
|
|
|
|
/* example of more terminalgroups
|
|
|
|
{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)
|
2004-09-10 19:02:27 +00:00
|
|
|
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
|
2004-08-09 17:04:08 +00:00
|
|
|
fails, then attempt fails and plane waits
|
|
|
|
*/
|
2005-01-09 08:49:40 +00:00
|
|
|
if (Airport->terminals[0] > 1) {
|
2005-01-06 22:31:58 +00:00
|
|
|
st = GetStation(v->u.air.targetairport);
|
2004-08-09 17:04:08 +00:00
|
|
|
temp = Airport->layout[v->u.air.pos].next_in_chain;
|
|
|
|
while (temp != NULL) {
|
|
|
|
if (temp->heading == 255) {
|
|
|
|
if (!HASBITS(st->airport_flags, temp->block)) {
|
2005-01-09 08:49:40 +00:00
|
|
|
int target_group;
|
|
|
|
int i;
|
|
|
|
int group_start = 0;
|
|
|
|
int group_end;
|
|
|
|
|
|
|
|
//read which group do we want to go to?
|
|
|
|
//(the first free group)
|
|
|
|
target_group = temp->next_position + 1;
|
|
|
|
|
|
|
|
//at what terminal does the group start?
|
|
|
|
//that means, sum up all terminals of
|
|
|
|
//groups with lower number
|
2005-10-23 13:04:44 +00:00
|
|
|
for (i = 1; i < target_group; i++)
|
2005-01-09 08:49:40 +00:00
|
|
|
group_start += Airport->terminals[i];
|
|
|
|
|
|
|
|
group_end = group_start + Airport->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
|
|
|
}
|
|
|
|
temp = temp->next_in_chain;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if there is only 1 terminalgroup, all terminals are checked (starting from 0 to max)
|
2005-01-09 08:49:40 +00:00
|
|
|
return FreeTerminal(v, 0, GetNumTerminals(Airport));
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2005-11-14 19:48:04 +00:00
|
|
|
static uint GetNumHelipads(const AirportFTAClass *Airport)
|
2005-01-09 08:49:40 +00:00
|
|
|
{
|
2005-11-14 19:48:04 +00:00
|
|
|
uint num = 0;
|
|
|
|
uint i;
|
2005-01-15 08:58:31 +00:00
|
|
|
|
2005-11-14 19:48:04 +00:00
|
|
|
for (i = Airport->helipads[0]; i > 0; i--) num += Airport->helipads[i];
|
2005-01-15 08:58:31 +00:00
|
|
|
|
2005-01-09 08:49:40 +00:00
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-09-10 19:02:27 +00:00
|
|
|
static bool AirportFindFreeHelipad(Vehicle *v, const AirportFTAClass *Airport)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
Station *st;
|
|
|
|
AirportFTA *temp;
|
|
|
|
|
|
|
|
// if an airport doesn't have helipads, use terminals
|
2005-10-23 13:04:44 +00:00
|
|
|
if (Airport->helipads == NULL) return AirportFindFreeTerminal(v, Airport);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
// if there are more helicoptergroups, pick one, just as in AirportFindFreeTerminal()
|
2005-01-09 08:49:40 +00:00
|
|
|
if (Airport->helipads[0] > 1) {
|
2005-01-06 22:31:58 +00:00
|
|
|
st = GetStation(v->u.air.targetairport);
|
2004-08-09 17:04:08 +00:00
|
|
|
temp = Airport->layout[v->u.air.pos].next_in_chain;
|
|
|
|
while (temp != NULL) {
|
|
|
|
if (temp->heading == 255) {
|
|
|
|
if (!HASBITS(st->airport_flags, temp->block)) {
|
2005-01-09 08:49:40 +00:00
|
|
|
int target_group;
|
|
|
|
int i;
|
|
|
|
int group_start = 0;
|
|
|
|
int group_end;
|
|
|
|
|
|
|
|
//read which group do we want to go to?
|
|
|
|
//(the first free group)
|
|
|
|
target_group = temp->next_position + 1;
|
|
|
|
|
|
|
|
//at what terminal does the group start?
|
|
|
|
//that means, sum up all terminals of
|
|
|
|
//groups with lower number
|
2006-02-01 07:36:15 +00:00
|
|
|
for (i = 1; i < target_group; i++)
|
2005-01-09 08:49:40 +00:00
|
|
|
group_start += Airport->helipads[i];
|
|
|
|
|
|
|
|
group_end = group_start + Airport->helipads[target_group];
|
2005-10-23 13:04:44 +00:00
|
|
|
if (FreeTerminal(v, group_start, group_end)) return true;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2005-10-23 13:04:44 +00:00
|
|
|
} else {
|
|
|
|
/* once the heading isn't 255, we've exhausted the possible blocks.
|
|
|
|
* So we cannot move */
|
|
|
|
return false;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
temp = temp->next_in_chain;
|
|
|
|
}
|
2005-10-23 13:04:44 +00:00
|
|
|
} else {
|
|
|
|
// only 1 helicoptergroup, check all helipads
|
|
|
|
// The blocks for helipads start after the last terminal (MAX_TERMINALS)
|
|
|
|
return FreeTerminal(v, MAX_TERMINALS, GetNumHelipads(Airport) + MAX_TERMINALS);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
return false; // it shouldn't get here anytime, but just to be sure
|
|
|
|
}
|
|
|
|
|
|
|
|
static void AircraftEventHandler(Vehicle *v, int loop)
|
|
|
|
{
|
|
|
|
v->tick_counter++;
|
|
|
|
|
|
|
|
if (v->vehstatus & VS_CRASHED) {
|
|
|
|
HandleCrashedAircraft(v);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2005-11-14 19:48:04 +00:00
|
|
|
if (v->vehstatus & VS_STOPPED) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
/* aircraft is broken down? */
|
|
|
|
if (v->breakdown_ctr != 0) {
|
|
|
|
if (v->breakdown_ctr <= 2) {
|
|
|
|
HandleBrokenAircraft(v);
|
|
|
|
} else {
|
|
|
|
v->breakdown_ctr--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
HandleAircraftSmoke(v);
|
|
|
|
ProcessAircraftOrder(v);
|
|
|
|
HandleAircraftLoading(v, loop);
|
|
|
|
|
2005-11-14 19:48:04 +00:00
|
|
|
if (v->current_order.type >= OT_LOADING) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
// pass the right airport structure to the functions
|
|
|
|
// DEREF_STATION gets target airport (Station *st), its type is passed to GetAirport
|
|
|
|
// that returns the correct layout depending on type
|
2005-01-06 22:31:58 +00:00
|
|
|
AirportGoToNextPosition(v, GetAirport(GetStation(v->u.air.targetairport)->airport_type));
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Aircraft_Tick(Vehicle *v)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2005-11-14 19:48:04 +00:00
|
|
|
if (v->subtype > 2) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-11-14 19:48:04 +00:00
|
|
|
if (v->subtype == 0) HelicopterTickHandler(v);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
AgeAircraftCargo(v);
|
|
|
|
|
2005-11-14 19:48:04 +00:00
|
|
|
for (i = 0; i != 6; i++) {
|
2004-08-09 17:04:08 +00:00
|
|
|
AircraftEventHandler(v, i);
|
|
|
|
if (v->type != VEH_Aircraft) // In case it was deleted
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-11-14 19:48:04 +00:00
|
|
|
void UpdateOilRig(void)
|
2005-01-09 08:49:40 +00:00
|
|
|
{
|
2005-11-14 19:48:04 +00:00
|
|
|
Station* st;
|
2005-01-15 08:58:31 +00:00
|
|
|
|
2005-01-09 08:49:40 +00:00
|
|
|
FOR_ALL_STATIONS(st) {
|
|
|
|
if (st->airport_type == 5) st->airport_type = AT_OILRIG;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// need to be called to load aircraft from old version
|
2005-01-22 20:23:18 +00:00
|
|
|
void UpdateOldAircraft(void)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
Station *st;
|
|
|
|
Vehicle *v_oldstyle;
|
|
|
|
GetNewVehiclePosResult gp;
|
|
|
|
|
|
|
|
// set airport_flags to 0 for all airports just to be sure
|
|
|
|
FOR_ALL_STATIONS(st) {
|
|
|
|
st->airport_flags = 0; // reset airport
|
|
|
|
// type of oilrig has been moved, update it (3-5)
|
2005-10-23 13:04:44 +00:00
|
|
|
if (st->airport_type == 3) st->airport_type = AT_OILRIG;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
FOR_ALL_VEHICLES(v_oldstyle) {
|
|
|
|
// airplane has another vehicle with subtype 4 (shadow), helicopter also has 3 (rotor)
|
|
|
|
// skip those
|
|
|
|
if (v_oldstyle->type == VEH_Aircraft && v_oldstyle->subtype <= 2) {
|
|
|
|
// airplane in terminal stopped doesn't hurt anyone, so goto next
|
2005-11-14 19:48:04 +00:00
|
|
|
if (v_oldstyle->vehstatus & VS_STOPPED && v_oldstyle->u.air.state == 0) {
|
2004-08-09 17:04:08 +00:00
|
|
|
v_oldstyle->u.air.state = HANGAR;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
AircraftLeaveHangar(v_oldstyle); // make airplane visible if it was in a depot for example
|
|
|
|
v_oldstyle->vehstatus &= ~VS_STOPPED; // make airplane moving
|
|
|
|
v_oldstyle->u.air.state = FLYING;
|
|
|
|
AircraftNextAirportPos_and_Order(v_oldstyle); // move it to the entry point of the airport
|
|
|
|
GetNewVehiclePos(v_oldstyle, &gp); // get the position of the plane (to be used for setting)
|
|
|
|
v_oldstyle->tile = 0; // aircraft in air is tile=0
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// correct speed of helicopter-rotors
|
2005-10-23 13:04:44 +00:00
|
|
|
if (v_oldstyle->subtype == 0) v_oldstyle->next->next->cur_speed = 32;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
// set new position x,y,z
|
|
|
|
SetAircraftPosition(v_oldstyle, gp.x, gp.y, GetAircraftFlyingAltitude(v_oldstyle));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-09-10 19:02:27 +00:00
|
|
|
void UpdateAirplanesOnNewStation(Station *st)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
GetNewVehiclePosResult gp;
|
|
|
|
Vehicle *v;
|
|
|
|
byte takeofftype;
|
|
|
|
uint16 cnt;
|
|
|
|
// only 1 station is updated per function call, so it is enough to get entry_point once
|
|
|
|
const AirportFTAClass *ap = GetAirport(st->airport_type);
|
|
|
|
FOR_ALL_VEHICLES(v) {
|
|
|
|
if (v->type == VEH_Aircraft && v->subtype <= 2) {
|
|
|
|
if (v->u.air.targetairport == st->index) { // if heading to this airport
|
|
|
|
/* update position of airplane. If plane is not flying, landing, or taking off
|
|
|
|
you cannot delete airport, so it doesn't matter
|
|
|
|
*/
|
|
|
|
if (v->u.air.state >= FLYING) { // circle around
|
|
|
|
v->u.air.pos = v->u.air.previous_pos = ap->entry_point;
|
|
|
|
v->u.air.state = FLYING;
|
|
|
|
// landing plane needs to be reset to flying height (only if in pause mode upgrade,
|
2005-02-22 19:38:44 +00:00
|
|
|
// in normal mode, plane is reset in AircraftController. It doesn't hurt for FLYING
|
2004-08-09 17:04:08 +00:00
|
|
|
GetNewVehiclePos(v, &gp);
|
|
|
|
// set new position x,y,z
|
|
|
|
SetAircraftPosition(v, gp.x, gp.y, GetAircraftFlyingAltitude(v));
|
2005-10-23 13:04:44 +00:00
|
|
|
} else {
|
2004-08-09 17:04:08 +00:00
|
|
|
assert(v->u.air.state == ENDTAKEOFF || v->u.air.state == HELITAKEOFF);
|
|
|
|
takeofftype = (v->subtype == 0) ? HELITAKEOFF : ENDTAKEOFF;
|
|
|
|
// search in airportdata for that heading
|
|
|
|
// easiest to do, since this doesn't happen a lot
|
|
|
|
for (cnt = 0; cnt < ap->nofelements; cnt++) {
|
|
|
|
if (ap->layout[cnt].heading == takeofftype) {
|
|
|
|
v->u.air.pos = ap->layout[cnt].position;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|