tron@2186: /* $Id$ */ tron@2186: truelight@0: #include "stdafx.h" Darkvater@1891: #include "openttd.h" tron@1299: #include "debug.h" tron@679: #include "map.h" truelight@0: #include "airport.h" tron@2159: #include "macros.h" tron@2159: #include "variables.h" peter1138@3701: #include "airport_movement.h" rubidium@4261: #include "date.h" truelight@0: Darkvater@4843: /* Uncomment this to print out a full report of the airport-structure Darkvater@4843: * You should either use Darkvater@4843: * - true: full-report, print out every state and choice with string-names Darkvater@4843: * OR Darkvater@4843: * - false: give a summarized report which only shows current and next position */ Darkvater@4843: //#define DEBUG_AIRPORT false Darkvater@4843: belugas@4171: static AirportFTAClass *CountryAirport; belugas@4171: static AirportFTAClass *CityAirport; belugas@4171: static AirportFTAClass *Oilrig; belugas@4171: static AirportFTAClass *Heliport; belugas@4171: static AirportFTAClass *MetropolitanAirport; belugas@4171: static AirportFTAClass *InternationalAirport; belugas@4171: static AirportFTAClass *CommuterAirport; belugas@4171: static AirportFTAClass *HeliDepot; belugas@4171: static AirportFTAClass *IntercontinentalAirport; belugas@4171: static AirportFTAClass *HeliStation; truelight@0: Darkvater@4842: static void AirportFTAClass_Constructor(AirportFTAClass *apc, richk@4059: const byte *terminals, const byte *helipads, richk@4059: const byte entry_point, const byte acc_planes, Darkvater@4842: const AirportFTAbuildup *apFA, richk@4059: const TileIndexDiffC *depots, const byte nof_depots, tron@3876: uint size_x, uint size_y tron@3876: ); Darkvater@4842: static void AirportFTAClass_Destructor(AirportFTAClass *apc); truelight@0: Darkvater@4842: static uint16 AirportGetNofElements(const AirportFTAbuildup *apFA); Darkvater@4842: static void AirportBuildAutomata(AirportFTAClass *apc, const AirportFTAbuildup *apFA); Darkvater@4843: static byte AirportGetTerminalCount(const byte *terminals, byte *groups); Darkvater@4842: static byte AirportTestFTA(const AirportFTAClass *apc); Darkvater@4843: Darkvater@4843: #ifdef DEBUG_AIRPORT Darkvater@4843: static void AirportPrintOut(const AirportFTAClass *apc, bool full_report); Darkvater@4843: #endif /* DEBUG_AIRPORT */ truelight@0: tron@1093: void InitializeAirports(void) truelight@0: { truelight@0: // country airport celestar@950: CountryAirport = malloc(sizeof(AirportFTAClass)); tron@1019: celestar@950: AirportFTAClass_Constructor( tron@1019: CountryAirport, tron@1019: _airport_terminal_country, tron@1019: NULL, tron@1019: 16, tron@1019: ALL, tron@1019: _airport_fta_country, tron@1019: _airport_depots_country, tron@3876: lengthof(_airport_depots_country), tron@3876: 4, 3 celestar@950: ); truelight@0: truelight@0: // city airport celestar@950: CityAirport = malloc(sizeof(AirportFTAClass)); tron@1019: celestar@950: AirportFTAClass_Constructor( tron@1019: CityAirport, tron@1019: _airport_terminal_city, tron@1019: NULL, tron@1019: 19, tron@1019: ALL, tron@1019: _airport_fta_city, tron@1019: _airport_depots_city, tron@3876: lengthof(_airport_depots_city), tron@3876: 6, 6 celestar@950: ); truelight@0: truelight@0: // metropolitan airport celestar@950: MetropolitanAirport = malloc(sizeof(AirportFTAClass)); tron@1019: celestar@950: AirportFTAClass_Constructor( tron@1019: MetropolitanAirport, tron@1019: _airport_terminal_metropolitan, tron@1019: NULL, tron@1019: 20, tron@1019: ALL, tron@1019: _airport_fta_metropolitan, tron@1019: _airport_depots_metropolitan, tron@3876: lengthof(_airport_depots_metropolitan), tron@3876: 6, 6 celestar@950: ); truelight@0: truelight@0: // international airport truelight@0: InternationalAirport = (AirportFTAClass *)malloc(sizeof(AirportFTAClass)); tron@1019: celestar@950: AirportFTAClass_Constructor( tron@1019: InternationalAirport, tron@1019: _airport_terminal_international, tron@1019: _airport_helipad_international, tron@1019: 37, tron@1019: ALL, tron@1019: _airport_fta_international, tron@1019: _airport_depots_international, tron@3876: lengthof(_airport_depots_international), tron@3876: 7, 7 celestar@950: ); truelight@0: richk@4059: // intercontintental airport richk@4059: IntercontinentalAirport = (AirportFTAClass *)malloc(sizeof(AirportFTAClass)); richk@4059: richk@4059: AirportFTAClass_Constructor( richk@4059: IntercontinentalAirport, richk@4059: _airport_terminal_intercontinental, richk@4059: _airport_helipad_intercontinental, richk@4059: 43, richk@4059: ALL, richk@4059: _airport_fta_intercontinental, richk@4059: _airport_depots_intercontinental, richk@4059: lengthof(_airport_depots_intercontinental), richk@4059: 9,11 richk@4059: ); richk@4059: truelight@0: // heliport, oilrig truelight@0: Heliport = (AirportFTAClass *)malloc(sizeof(AirportFTAClass)); tron@1019: celestar@950: AirportFTAClass_Constructor( tron@1019: Heliport, tron@1019: NULL, tron@1019: _airport_helipad_heliport_oilrig, tron@1019: 7, tron@1019: HELICOPTERS_ONLY, tron@1019: _airport_fta_heliport_oilrig, tron@1019: NULL, tron@3876: 0, tron@3876: 1, 1 celestar@950: ); tron@1019: truelight@0: Oilrig = Heliport; // exactly the same structure for heliport/oilrig, so share state machine richk@4059: richk@4059: // commuter airport richk@4059: CommuterAirport = malloc(sizeof(AirportFTAClass)); richk@4059: richk@4059: AirportFTAClass_Constructor( richk@4059: CommuterAirport, richk@4059: _airport_terminal_commuter, richk@4059: _airport_helipad_commuter, richk@4059: 22, richk@4059: ALL, richk@4059: _airport_fta_commuter, richk@4059: _airport_depots_commuter, richk@4059: lengthof(_airport_depots_commuter), richk@4059: 5,4 richk@4059: ); richk@4059: richk@4059: // helidepot airport richk@4059: HeliDepot = malloc(sizeof(AirportFTAClass)); richk@4059: richk@4059: AirportFTAClass_Constructor( richk@4059: HeliDepot, richk@4059: NULL, richk@4059: _airport_helipad_helidepot, richk@4059: 4, richk@4059: HELICOPTERS_ONLY, richk@4059: _airport_fta_helidepot, richk@4059: _airport_depots_helidepot, richk@4059: lengthof(_airport_depots_helidepot), richk@4059: 2,2 richk@4059: ); richk@4059: richk@4059: // helistation airport richk@4059: HeliStation = malloc(sizeof(AirportFTAClass)); richk@4059: richk@4059: AirportFTAClass_Constructor( richk@4059: HeliStation, richk@4059: NULL, richk@4059: _airport_helipad_helistation, richk@4059: 25, richk@4059: HELICOPTERS_ONLY, richk@4059: _airport_fta_helistation, richk@4059: _airport_depots_helistation, richk@4059: lengthof(_airport_depots_helistation), richk@4059: 4,2 richk@4059: ); richk@4059: truelight@0: } truelight@0: tron@1093: void UnInitializeAirports(void) truelight@0: { truelight@0: AirportFTAClass_Destructor(CountryAirport); truelight@0: AirportFTAClass_Destructor(CityAirport); truelight@0: AirportFTAClass_Destructor(Heliport); truelight@0: AirportFTAClass_Destructor(MetropolitanAirport); truelight@0: AirportFTAClass_Destructor(InternationalAirport); richk@4059: AirportFTAClass_Destructor(CommuterAirport); richk@4059: AirportFTAClass_Destructor(HeliDepot); richk@4059: AirportFTAClass_Destructor(IntercontinentalAirport); richk@4059: AirportFTAClass_Destructor(HeliStation); truelight@0: } truelight@0: Darkvater@4842: static void AirportFTAClass_Constructor(AirportFTAClass *apc, Darkvater@4842: const byte *terminals, const byte *helipads, Darkvater@4842: const byte entry_point, const byte acc_planes, Darkvater@4842: const AirportFTAbuildup *apFA, Darkvater@4842: const TileIndexDiffC *depots, const byte nof_depots, tron@3876: uint size_x, uint size_y tron@3876: ) truelight@0: { celestar@950: byte nofterminals, nofhelipads; Darkvater@4843: byte nofterminalgroups, nofhelipadgroups; tron@1019: Darkvater@4842: apc->size_x = size_x; Darkvater@4842: apc->size_y = size_y; tron@3876: Darkvater@4843: /* Set up the terminal and helipad count for an airport. Darkvater@4843: * TODO: If there are more than 10 terminals or 4 helipads, internal variables Darkvater@4843: * need to be changed, so don't allow that for now */ Darkvater@4843: nofterminals = AirportGetTerminalCount(terminals, &nofterminalgroups); Darkvater@4843: if (nofterminals > MAX_TERMINALS) { Darkvater@4843: DEBUG(misc, 0) ("[Ap] Currently only maximum of %d terminals are supported (you wanted %d)", MAX_TERMINALS, nofterminals); Darkvater@4843: assert(nofterminals <= MAX_TERMINALS); celestar@950: } Darkvater@4842: apc->terminals = terminals; celestar@950: Darkvater@4843: nofhelipads = AirportGetTerminalCount(helipads, &nofhelipadgroups); Darkvater@4843: if (nofhelipads > MAX_HELIPADS) { Darkvater@4843: DEBUG(misc, 0) ("[Ap] Currently only maximum of %d helipads are supported (you wanted %d)", MAX_HELIPADS, nofhelipads); Darkvater@4843: assert(nofhelipads <= MAX_HELIPADS); celestar@950: } Darkvater@4842: apc->helipads = helipads; celestar@950: Darkvater@4843: /* Get the number of elements from the source table. We also double check this Darkvater@4843: * with the entry point which must be within bounds and use this information Darkvater@4843: * later on to build and validate the state machine */ Darkvater@4842: apc->nofelements = AirportGetNofElements(apFA); Darkvater@4843: if (entry_point >= apc->nofelements) { Darkvater@4843: DEBUG(misc, 0) ("[Ap] Entry (%d) must be within the airport (maximum %d)", entry_point, apc->nofelements); Darkvater@4843: assert(entry_point < apc->nofelements); Darkvater@4843: } truelight@0: Darkvater@4843: apc->acc_planes = acc_planes; Darkvater@4843: apc->entry_point = entry_point; Darkvater@4842: apc->airport_depots = depots; Darkvater@4843: apc->nof_depots = nof_depots; truelight@0: Darkvater@4843: /* Build the state machine itself */ Darkvater@4842: AirportBuildAutomata(apc, apFA); Darkvater@4843: DEBUG(misc, 1) ("[Ap] #count %3d; #term %2d (%dgrp); #helipad %2d (%dgrp); entry %3d", Darkvater@4843: apc->nofelements, nofterminals, nofterminalgroups, nofhelipads, nofhelipadgroups, apc->entry_point); truelight@0: Darkvater@4843: /* Test if everything went allright. This is only a rude static test checking Darkvater@4843: * the symantic correctness. By no means does passing the test mean that the Darkvater@4843: * airport is working correctly or will not deadlock for example */ Darkvater@4843: { byte ret = AirportTestFTA(apc); Darkvater@4843: if (ret != MAX_ELEMENTS) DEBUG(misc, 0) ("[Ap] ERROR with element: %d", ret - 1); tron@2549: assert(ret == MAX_ELEMENTS); truelight@0: } Darkvater@4843: Darkvater@4843: #ifdef DEBUG_AIRPORT Darkvater@4843: AirportPrintOut(apc, DEBUG_AIRPORT); tron@4077: #endif truelight@0: } truelight@0: Darkvater@4842: static void AirportFTAClass_Destructor(AirportFTAClass *apc) truelight@0: { truelight@0: int i; truelight@0: AirportFTA *current, *next; truelight@0: Darkvater@4842: for (i = 0; i < apc->nofelements; i++) { Darkvater@4842: current = apc->layout[i].next; truelight@0: while (current != NULL) { Darkvater@4842: next = current->next; truelight@0: free(current); truelight@0: current = next; truelight@0: }; truelight@0: } Darkvater@4842: free(apc->layout); Darkvater@4842: free(apc); truelight@0: } truelight@0: Darkvater@4843: /** Get the number of elements of a source Airport state automata Darkvater@4843: * Since it is actually just a big array of AirportFTA types, we only Darkvater@4843: * know one element from the other by differing 'position' identifiers */ Darkvater@4842: static uint16 AirportGetNofElements(const AirportFTAbuildup *apFA) truelight@0: { truelight@0: int i; truelight@0: uint16 nofelements = 0; Darkvater@4842: int temp = apFA[0].position; tron@2549: truelight@0: for (i = 0; i < MAX_ELEMENTS; i++) { Darkvater@4842: if (temp != apFA[i].position) { truelight@0: nofelements++; Darkvater@4842: temp = apFA[i].position; truelight@0: } Darkvater@4842: if (apFA[i].position == MAX_ELEMENTS) break; truelight@0: } truelight@0: return nofelements; truelight@0: } truelight@0: Darkvater@4843: /* We calculate the terminal/helipod count based on the data passed to us Darkvater@4843: * This data (terminals) contains an index as a first element as to how many Darkvater@4843: * groups there are, and then the number of terminals for each group */ Darkvater@4843: static byte AirportGetTerminalCount(const byte *terminals, byte *groups) Darkvater@4843: { Darkvater@4843: byte i; Darkvater@4843: byte nof_terminals = 0; Darkvater@4843: *groups = 0; Darkvater@4843: Darkvater@4843: if (terminals != NULL) { Darkvater@4843: i = terminals[0]; Darkvater@4843: *groups = i; Darkvater@4843: while (i-- > 0) { Darkvater@4843: terminals++; Darkvater@4843: assert(*terminals != 0); // no empty groups please Darkvater@4843: nof_terminals += *terminals; Darkvater@4843: } Darkvater@4843: } Darkvater@4843: return nof_terminals; Darkvater@4843: } Darkvater@4843: Darkvater@4842: static void AirportBuildAutomata(AirportFTAClass *apc, const AirportFTAbuildup *apFA) truelight@0: { truelight@0: AirportFTA *current; Darkvater@4843: AirportFTA *FAutomata = malloc(sizeof(AirportFTA) * apc->nofelements); Darkvater@4843: uint16 internalcounter = 0; Darkvater@4843: uint16 i; Darkvater@4843: Darkvater@4842: apc->layout = FAutomata; Darkvater@4842: for (i = 0; i < apc->nofelements; i++) { Darkvater@4842: current = &apc->layout[i]; Darkvater@4843: current->position = apFA[internalcounter].position; Darkvater@4843: current->heading = apFA[internalcounter].heading; Darkvater@4843: current->block = apFA[internalcounter].block; Darkvater@4842: current->next_position = apFA[internalcounter].next; truelight@0: truelight@0: // outgoing nodes from the same position, create linked list Darkvater@4842: while (current->position == apFA[internalcounter + 1].position) { belugas@4171: AirportFTA *newNode = malloc(sizeof(AirportFTA)); tron@2549: Darkvater@4843: newNode->position = apFA[internalcounter + 1].position; Darkvater@4843: newNode->heading = apFA[internalcounter + 1].heading; Darkvater@4843: newNode->block = apFA[internalcounter + 1].block; Darkvater@4842: newNode->next_position = apFA[internalcounter + 1].next; truelight@0: // create link Darkvater@4842: current->next = newNode; Darkvater@4842: current = current->next; truelight@0: internalcounter++; truelight@0: } // while Darkvater@4842: current->next = NULL; truelight@0: internalcounter++; truelight@0: } truelight@0: } truelight@0: Darkvater@4842: static byte AirportTestFTA(const AirportFTAClass *apc) truelight@0: { Darkvater@4843: byte position, i, next_position; Darkvater@4844: AirportFTA *current, *first; Darkvater@4843: next_position = 0; truelight@0: Darkvater@4842: for (i = 0; i < apc->nofelements; i++) { Darkvater@4842: position = apc->layout[i].position; Darkvater@4843: if (position != next_position) return i; Darkvater@4844: current = first = &apc->layout[i]; truelight@0: Darkvater@4844: for (; current != NULL; current = current->next) { Darkvater@4844: /* A heading must always be valid. The only exceptions are Darkvater@4844: * - multiple choices as start, identified by a special value of 255 Darkvater@4844: * - terminal group which is identified by a special value of 255 */ Darkvater@4844: if (current->heading > MAX_HEADINGS) { Darkvater@4844: if (current->heading != 255) return i; Darkvater@4844: if (current == first && current->next == NULL) return i; Darkvater@4844: if (current != first && current->next_position > apc->terminals[0]) return i; Darkvater@4844: } Darkvater@4844: Darkvater@4844: /* If there is only one choice, it must be at the end */ Darkvater@4844: if (current->heading == 0 && current->next != NULL) return i; Darkvater@4844: /* Obviously the elements of the linked list must have the same identifier */ Darkvater@4843: if (position != current->position) return i; Darkvater@4844: /* A next position must be within bounds */ Darkvater@4843: if (current->next_position >= apc->nofelements) return i; Darkvater@4844: } Darkvater@4843: next_position++; truelight@0: } truelight@0: return MAX_ELEMENTS; truelight@0: } truelight@0: Darkvater@4843: #ifdef DEBUG_AIRPORT tron@2549: static const char* const _airport_heading_strings[] = { truelight@0: "TO_ALL", truelight@0: "HANGAR", truelight@0: "TERM1", truelight@0: "TERM2", truelight@0: "TERM3", truelight@0: "TERM4", truelight@0: "TERM5", truelight@0: "TERM6", truelight@0: "HELIPAD1", truelight@0: "HELIPAD2", truelight@0: "TAKEOFF", truelight@0: "STARTTAKEOFF", truelight@0: "ENDTAKEOFF", truelight@0: "HELITAKEOFF", truelight@0: "FLYING", truelight@0: "LANDING", truelight@0: "ENDLANDING", truelight@0: "HELILANDING", truelight@0: "HELIENDLANDING", richk@4059: "TERM7", richk@4059: "TERM8", richk@4059: "HELIPAD3", richk@4059: "HELIPAD4", rubidium@4434: "DUMMY" // extra heading for 255 truelight@0: }; truelight@0: tron@4077: static uint AirportBlockToString(uint32 block) tron@4077: { tron@4077: uint i = 0; tron@4077: if (block & 0xffff0000) { block >>= 16; i += 16; } tron@4077: if (block & 0x0000ff00) { block >>= 8; i += 8; } tron@4077: if (block & 0x000000f0) { block >>= 4; i += 4; } tron@4077: if (block & 0x0000000c) { block >>= 2; i += 2; } tron@4077: if (block & 0x00000002) { i += 1; } tron@4077: return i; tron@4077: } tron@4077: Darkvater@4843: static void AirportPrintOut(const AirportFTAClass *apc, bool full_report) truelight@0: { Darkvater@4843: uint16 i; tron@4077: Darkvater@4843: if (!full_report) printf("(P = Current Position; NP = Next Position)\n"); Darkvater@4843: Darkvater@4843: for (i = 0; i < apc->nofelements; i++) { Darkvater@4843: AirportFTA *current = &apc->layout[i]; Darkvater@4843: Darkvater@4843: for (; current != NULL; current = current->next) { truelight@0: if (full_report) { Darkvater@4843: byte heading = (current->heading == 255) ? MAX_HEADINGS + 1 : current->heading; Darkvater@4843: printf("\tPos:%2d NPos:%2d Heading:%15s Block:%2d\n", current->position, Darkvater@4843: current->next_position, _airport_heading_strings[heading], Darkvater@4843: AirportBlockToString(current->block)); tron@2549: } else { Darkvater@4843: printf("P:%2d NP:%2d", current->position, current->next_position); truelight@0: } truelight@0: } truelight@0: printf("\n"); truelight@0: } truelight@0: } tron@2549: #endif truelight@0: belugas@4171: const AirportFTAClass *GetAirport(const byte airport_type) truelight@0: { truelight@0: //FIXME -- AircraftNextAirportPos_and_Order -> Needs something nicer, don't like this code truelight@0: // needs constant change if more airports are added truelight@0: switch (airport_type) { tron@4077: default: NOT_REACHED(); tron@4077: case AT_SMALL: return CountryAirport; tron@4077: case AT_LARGE: return CityAirport; tron@4077: case AT_METROPOLITAN: return MetropolitanAirport; tron@4077: case AT_HELIPORT: return Heliport; tron@4077: case AT_OILRIG: return Oilrig; tron@4077: case AT_INTERNATIONAL: return InternationalAirport; tron@4077: case AT_COMMUTER: return CommuterAirport; tron@4077: case AT_HELIDEPOT: return HeliDepot; tron@4077: case AT_INTERCON: return IntercontinentalAirport; tron@4077: case AT_HELISTATION: return HeliStation; truelight@0: } truelight@0: } tron@2159: peter1138@3701: const AirportMovingData *GetAirportMovingData(byte airport_type, byte position) peter1138@3701: { peter1138@3701: assert(airport_type < lengthof(_airport_moving_datas)); peter1138@3701: assert(position < GetAirport(airport_type)->nofelements); peter1138@3701: return &_airport_moving_datas[airport_type][position]; peter1138@3701: } peter1138@3701: tron@2159: uint32 GetValidAirports(void) tron@2159: { tron@2159: uint32 bytemask = _avail_aircraft; /// sets the first 3 bytes, 0 - 2, @see AdjustAvailAircraft() tron@2159: Darkvater@4843: if (_cur_year >= 1980) SETBIT(bytemask, 3); // metropolitan airport rubidium@4294: if (_cur_year >= 1990) SETBIT(bytemask, 4); // international airport rubidium@4294: if (_cur_year >= 1983) SETBIT(bytemask, 5); // commuter airport rubidium@4294: if (_cur_year >= 1976) SETBIT(bytemask, 6); // helidepot rubidium@4294: if (_cur_year >= 2002) SETBIT(bytemask, 7); // intercontinental airport rubidium@4294: if (_cur_year >= 1980) SETBIT(bytemask, 8); // helistation tron@2159: return bytemask; tron@2159: }