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/>.
|
|
|
|
*/
|
|
|
|
|
2008-05-06 15:11:33 +00:00
|
|
|
/** @file airport.cpp Functions related to airports. */
|
2007-02-23 01:48:53 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
#include "stdafx.h"
|
2010-03-18 18:38:32 +00:00
|
|
|
#include "station_base.h"
|
2010-03-05 23:21:41 +00:00
|
|
|
#include "table/strings.h"
|
2010-03-19 11:17:52 +00:00
|
|
|
#include "table/airport_movement.h"
|
2010-01-24 20:17:18 +00:00
|
|
|
#include "table/airporttile_ids.h"
|
2009-06-12 14:53:57 +00:00
|
|
|
|
2006-10-14 09:51:04 +00:00
|
|
|
|
2010-08-18 01:19:03 +00:00
|
|
|
#define AIRPORT_GENERIC(name, terminals, num_helipads, flags, delta_z) \
|
|
|
|
static AirportFTAClass _airportfta_ ## name(_airport_moving_data_ ## name, terminals, \
|
|
|
|
num_helipads, _airport_entries_ ## name, flags, _airport_fta_ ## name, delta_z);
|
|
|
|
|
|
|
|
#define AIRPORT(name, num_helipads, short_strip) \
|
|
|
|
AIRPORT_GENERIC(name, _airport_terminal_ ## name, num_helipads, AirportFTAClass::ALL | (short_strip ? AirportFTAClass::SHORT_STRIP : (AirportFTAClass::Flags)0), 0)
|
|
|
|
|
|
|
|
#define HELIPORT(name, num_helipads, delta_z) \
|
|
|
|
AIRPORT_GENERIC(name, NULL, num_helipads, AirportFTAClass::HELICOPTERS, delta_z)
|
|
|
|
|
|
|
|
AIRPORT(country, 0, true)
|
|
|
|
AIRPORT(city, 0, false)
|
|
|
|
HELIPORT(heliport, 1, 60)
|
|
|
|
AIRPORT(metropolitan, 0, false)
|
|
|
|
AIRPORT(international, 2, false)
|
|
|
|
AIRPORT(commuter, 2, true)
|
|
|
|
HELIPORT(helidepot, 1, 0)
|
|
|
|
AIRPORT(intercontinental, 2, false)
|
|
|
|
HELIPORT(helistation, 3, 0)
|
|
|
|
HELIPORT(oilrig, 1, 54)
|
|
|
|
AIRPORT_GENERIC(dummy, NULL, 0, AirportFTAClass::ALL, 0)
|
|
|
|
|
|
|
|
#undef HELIPORT
|
|
|
|
#undef AIRPORT
|
|
|
|
#undef AIRPORT_GENERIC
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2010-03-06 15:38:13 +00:00
|
|
|
#include "table/airport_defaults.h"
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-02-04 10:25:57 +00:00
|
|
|
|
|
|
|
static uint16 AirportGetNofElements(const AirportFTAbuildup *apFA);
|
2008-11-25 18:42:06 +00:00
|
|
|
static AirportFTA *AirportBuildAutomata(uint nofelements, const AirportFTAbuildup *apFA);
|
2007-02-04 10:25:57 +00:00
|
|
|
|
|
|
|
|
2010-08-05 12:03:06 +00:00
|
|
|
/**
|
|
|
|
* Rotate the airport moving data to another rotation.
|
|
|
|
* @param orig Pointer to the moving data to rotate.
|
|
|
|
* @param rotation How to rotate the moving data.
|
|
|
|
* @return The rotated moving data.
|
|
|
|
*/
|
|
|
|
AirportMovingData RotateAirportMovingData(const AirportMovingData *orig, Direction rotation, uint num_tiles_x, uint num_tiles_y)
|
|
|
|
{
|
|
|
|
AirportMovingData amd;
|
|
|
|
amd.flag = orig->flag;
|
|
|
|
amd.direction = ChangeDir(orig->direction, (DirDiff)rotation);
|
|
|
|
switch (rotation) {
|
|
|
|
case DIR_N:
|
|
|
|
amd.x = orig->x;
|
|
|
|
amd.y = orig->y;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DIR_E:
|
|
|
|
amd.x = orig->y;
|
|
|
|
amd.y = num_tiles_y * TILE_SIZE - orig->x - 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DIR_S:
|
|
|
|
amd.x = num_tiles_x * TILE_SIZE - orig->x - 1;
|
|
|
|
amd.y = num_tiles_y * TILE_SIZE - orig->y - 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DIR_W:
|
|
|
|
amd.x = num_tiles_x * TILE_SIZE - orig->y - 1;
|
|
|
|
amd.y = orig->x;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default: NOT_REACHED();
|
|
|
|
}
|
|
|
|
return amd;
|
|
|
|
}
|
2007-02-04 10:25:57 +00:00
|
|
|
|
|
|
|
AirportFTAClass::AirportFTAClass(
|
|
|
|
const AirportMovingData *moving_data_,
|
|
|
|
const byte *terminals_,
|
2010-08-13 00:36:12 +00:00
|
|
|
const byte num_helipads_,
|
2007-02-16 12:10:19 +00:00
|
|
|
const byte *entry_points_,
|
2007-02-15 20:16:33 +00:00
|
|
|
Flags flags_,
|
2006-10-13 23:08:55 +00:00
|
|
|
const AirportFTAbuildup *apFA,
|
2010-01-15 12:08:08 +00:00
|
|
|
byte delta_z_
|
2007-02-04 10:25:57 +00:00
|
|
|
) :
|
|
|
|
moving_data(moving_data_),
|
|
|
|
terminals(terminals_),
|
2010-08-13 00:36:12 +00:00
|
|
|
num_helipads(num_helipads_),
|
2007-02-15 20:16:33 +00:00
|
|
|
flags(flags_),
|
2007-02-04 13:46:21 +00:00
|
|
|
nofelements(AirportGetNofElements(apFA)),
|
2007-02-16 12:10:19 +00:00
|
|
|
entry_points(entry_points_),
|
2010-01-15 12:08:08 +00:00
|
|
|
delta_z(delta_z_)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2006-10-14 09:51:04 +00:00
|
|
|
/* Build the state machine itself */
|
2010-08-17 11:59:09 +00:00
|
|
|
this->layout = AirportBuildAutomata(this->nofelements, apFA);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-02-04 10:25:57 +00:00
|
|
|
AirportFTAClass::~AirportFTAClass()
|
|
|
|
{
|
|
|
|
for (uint i = 0; i < nofelements; i++) {
|
|
|
|
AirportFTA *current = layout[i].next;
|
2004-08-09 17:04:08 +00:00
|
|
|
while (current != NULL) {
|
2007-02-04 10:25:57 +00:00
|
|
|
AirportFTA *next = current->next;
|
2004-08-09 17:04:08 +00:00
|
|
|
free(current);
|
|
|
|
current = next;
|
|
|
|
};
|
|
|
|
}
|
2007-02-04 10:25:57 +00:00
|
|
|
free(layout);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* Get the number of elements of a source Airport state automata
|
2006-10-14 09:51:04 +00:00
|
|
|
* Since it is actually just a big array of AirportFTA types, we only
|
2010-08-01 19:44:49 +00:00
|
|
|
* know one element from the other by differing 'position' identifiers
|
|
|
|
*/
|
2006-10-13 23:08:55 +00:00
|
|
|
static uint16 AirportGetNofElements(const AirportFTAbuildup *apFA)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
uint16 nofelements = 0;
|
2006-10-13 23:08:55 +00:00
|
|
|
int temp = apFA[0].position;
|
2005-10-23 13:04:44 +00:00
|
|
|
|
2007-02-20 06:39:09 +00:00
|
|
|
for (uint i = 0; i < MAX_ELEMENTS; i++) {
|
2006-10-13 23:08:55 +00:00
|
|
|
if (temp != apFA[i].position) {
|
2004-08-09 17:04:08 +00:00
|
|
|
nofelements++;
|
2006-10-13 23:08:55 +00:00
|
|
|
temp = apFA[i].position;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2006-10-13 23:08:55 +00:00
|
|
|
if (apFA[i].position == MAX_ELEMENTS) break;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
return nofelements;
|
|
|
|
}
|
|
|
|
|
2007-02-04 10:25:57 +00:00
|
|
|
|
2008-11-25 18:42:06 +00:00
|
|
|
static AirportFTA *AirportBuildAutomata(uint nofelements, const AirportFTAbuildup *apFA)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2007-02-04 10:25:57 +00:00
|
|
|
AirportFTA *FAutomata = MallocT<AirportFTA>(nofelements);
|
2006-10-14 09:51:04 +00:00
|
|
|
uint16 internalcounter = 0;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-02-04 10:25:57 +00:00
|
|
|
for (uint i = 0; i < nofelements; i++) {
|
2007-02-20 06:39:09 +00:00
|
|
|
AirportFTA *current = &FAutomata[i];
|
2006-10-14 09:51:04 +00:00
|
|
|
current->position = apFA[internalcounter].position;
|
|
|
|
current->heading = apFA[internalcounter].heading;
|
|
|
|
current->block = apFA[internalcounter].block;
|
2006-10-13 23:08:55 +00:00
|
|
|
current->next_position = apFA[internalcounter].next;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-11-25 18:42:06 +00:00
|
|
|
/* outgoing nodes from the same position, create linked list */
|
2006-10-13 23:08:55 +00:00
|
|
|
while (current->position == apFA[internalcounter + 1].position) {
|
2007-01-11 17:29:39 +00:00
|
|
|
AirportFTA *newNode = MallocT<AirportFTA>(1);
|
2005-10-23 13:04:44 +00:00
|
|
|
|
2006-10-14 09:51:04 +00:00
|
|
|
newNode->position = apFA[internalcounter + 1].position;
|
|
|
|
newNode->heading = apFA[internalcounter + 1].heading;
|
|
|
|
newNode->block = apFA[internalcounter + 1].block;
|
2006-10-13 23:08:55 +00:00
|
|
|
newNode->next_position = apFA[internalcounter + 1].next;
|
2008-11-25 18:42:06 +00:00
|
|
|
/* create link */
|
2006-10-13 23:08:55 +00:00
|
|
|
current->next = newNode;
|
|
|
|
current = current->next;
|
2004-08-09 17:04:08 +00:00
|
|
|
internalcounter++;
|
2007-02-20 06:39:09 +00:00
|
|
|
}
|
2006-10-13 23:08:55 +00:00
|
|
|
current->next = NULL;
|
2004-08-09 17:04:08 +00:00
|
|
|
internalcounter++;
|
|
|
|
}
|
2007-02-04 10:25:57 +00:00
|
|
|
return FAutomata;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2006-07-26 03:33:12 +00:00
|
|
|
const AirportFTAClass *GetAirport(const byte airport_type)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2010-03-06 23:54:54 +00:00
|
|
|
if (airport_type == AT_DUMMY) return &_airportfta_dummy;
|
2010-03-06 15:38:13 +00:00
|
|
|
return AirportSpec::Get(airport_type)->fsm;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2010-03-18 18:38:32 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the vehicle position when an aircraft is build at the given tile
|
|
|
|
* @param hangar_tile The tile on which the vehicle is build
|
|
|
|
* @return The position (index in airport node array) where the aircraft ends up
|
|
|
|
*/
|
|
|
|
byte GetVehiclePosOnBuild(TileIndex hangar_tile)
|
|
|
|
{
|
|
|
|
const Station *st = Station::GetByTile(hangar_tile);
|
2010-03-18 21:02:20 +00:00
|
|
|
const AirportFTAClass *apc = st->airport.GetFTA();
|
2010-03-18 18:38:32 +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 */
|
|
|
|
for (uint i = 0;; i++) {
|
2010-03-19 09:58:46 +00:00
|
|
|
if (st->airport.GetHangarTile(i) == hangar_tile) {
|
2010-03-18 18:38:32 +00:00
|
|
|
assert(apc->layout[i].heading == HANGAR);
|
|
|
|
return apc->layout[i].position;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
NOT_REACHED();
|
|
|
|
}
|