1
0
Fork 0

(svn r6769) -Codechange: Minor syntax, const correctness, variable localization, coding

style changes to airport code, and cleanup of airport.c. Should not change
 any functionality.
release/0.5
Darkvater 2006-10-14 09:51:04 +00:00
parent 9c508259a9
commit 4dff87dc32
3 changed files with 119 additions and 130 deletions

View File

@ -28,7 +28,7 @@
static bool AirportMove(Vehicle *v, const AirportFTAClass *apc); static bool AirportMove(Vehicle *v, const AirportFTAClass *apc);
static bool AirportSetBlocks(Vehicle *v, AirportFTA *current_pos, const AirportFTAClass *apc); static bool AirportSetBlocks(Vehicle *v, AirportFTA *current_pos, const AirportFTAClass *apc);
static bool AirportHasBlock(Vehicle *v, AirportFTA *current_pos, const AirportFTAClass *apc); static bool AirportHasBlock(Vehicle *v, const AirportFTA *current_pos, const AirportFTAClass *apc);
static bool AirportFindFreeTerminal(Vehicle *v, const AirportFTAClass *apc); static bool AirportFindFreeTerminal(Vehicle *v, const AirportFTAClass *apc);
static bool AirportFindFreeHelipad(Vehicle *v, const AirportFTAClass *apc); static bool AirportFindFreeHelipad(Vehicle *v, const AirportFTAClass *apc);
static void AirportGoToNextPosition(Vehicle *v, const AirportFTAClass *apc); static void AirportGoToNextPosition(Vehicle *v, const AirportFTAClass *apc);
@ -1517,9 +1517,7 @@ static void AircraftEventHandler_AtTerminal(Vehicle *v, const AirportFTAClass *a
if (v->current_order.type == OT_NOTHING) return; if (v->current_order.type == OT_NOTHING) return;
// if the block of the next position is busy, stay put // if the block of the next position is busy, stay put
if (AirportHasBlock(v, &apc->layout[v->u.air.pos], apc)) { if (AirportHasBlock(v, &apc->layout[v->u.air.pos], apc)) return;
return;
}
// airport-road is free. We either have to go to another airport, or to the hangar // airport-road is free. We either have to go to another airport, or to the hangar
// ---> start moving // ---> start moving
@ -1790,14 +1788,14 @@ static bool AirportMove(Vehicle *v, const AirportFTAClass *apc)
} }
// returns true if the road ahead is busy, eg. you must wait before proceeding // returns true if the road ahead is busy, eg. you must wait before proceeding
static bool AirportHasBlock(Vehicle *v, AirportFTA *current_pos, const AirportFTAClass *apc) static bool AirportHasBlock(Vehicle *v, const AirportFTA *current_pos, const AirportFTAClass *apc)
{ {
const AirportFTA* reference = &apc->layout[v->u.air.pos]; const AirportFTA *reference = &apc->layout[v->u.air.pos];
const AirportFTA* next = &apc->layout[current_pos->next_position]; const AirportFTA *next = &apc->layout[current_pos->next_position];
// same block, then of course we can move // same block, then of course we can move
if (apc->layout[current_pos->position].block != next->block) { if (apc->layout[current_pos->position].block != next->block) {
const Station* st = GetStation(v->u.air.targetairport); const Station *st = GetStation(v->u.air.targetairport);
uint32 airport_flags = next->block; uint32 airport_flags = next->block;
// check additional possible extra blocks // check additional possible extra blocks
@ -1817,9 +1815,8 @@ static bool AirportHasBlock(Vehicle *v, AirportFTA *current_pos, const AirportFT
// returns true on success. Eg, next block was free and we have occupied it // returns true on success. Eg, next block was free and we have occupied it
static bool AirportSetBlocks(Vehicle *v, AirportFTA *current_pos, const AirportFTAClass *apc) static bool AirportSetBlocks(Vehicle *v, AirportFTA *current_pos, const AirportFTAClass *apc)
{ {
AirportFTA* next = &apc->layout[current_pos->next_position]; AirportFTA *next = &apc->layout[current_pos->next_position];
AirportFTA* reference = &apc->layout[v->u.air.pos]; AirportFTA *reference = &apc->layout[v->u.air.pos];
AirportFTA* current;
// if the next position is in another block, check it and wait until it is free // if the next position is in another block, check it and wait until it is free
if ((apc->layout[current_pos->position].block & next->block) != next->block) { if ((apc->layout[current_pos->position].block & next->block) != next->block) {
@ -1827,7 +1824,7 @@ static bool AirportSetBlocks(Vehicle *v, AirportFTA *current_pos, const AirportF
Station* st = GetStation(v->u.air.targetairport); Station* st = GetStation(v->u.air.targetairport);
//search for all all elements in the list with the same state, and blocks != N //search for all all elements in the list with the same state, and blocks != N
// this means more blocks should be checked/set // this means more blocks should be checked/set
current = current_pos; AirportFTA *current = current_pos;
if (current == reference) current = current->next; if (current == reference) current = current->next;
while (current != NULL) { while (current != NULL) {
if (current->heading == current_pos->heading && current->block != 0) { if (current->heading == current_pos->heading && current->block != 0) {
@ -1880,9 +1877,6 @@ static uint GetNumTerminals(const AirportFTAClass *apc)
static bool AirportFindFreeTerminal(Vehicle *v, const AirportFTAClass *apc) static bool AirportFindFreeTerminal(Vehicle *v, const AirportFTAClass *apc)
{ {
AirportFTA *temp;
Station *st;
/* example of more terminalgroups /* 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}, * {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: * Heading 255 denotes a group. We see 2 groups here:
@ -1894,8 +1888,9 @@ static bool AirportFindFreeTerminal(Vehicle *v, const AirportFTAClass *apc)
* fails, then attempt fails and plane waits * fails, then attempt fails and plane waits
*/ */
if (apc->terminals[0] > 1) { if (apc->terminals[0] > 1) {
st = GetStation(v->u.air.targetairport); Station *st = GetStation(v->u.air.targetairport);
temp = apc->layout[v->u.air.pos].next; AirportFTA *temp = apc->layout[v->u.air.pos].next;
while (temp != NULL) { while (temp != NULL) {
if (temp->heading == 255) { if (temp->heading == 255) {
if (!HASBITS(st->airport_flags, temp->block)) { if (!HASBITS(st->airport_flags, temp->block)) {

214
airport.c
View File

@ -10,6 +10,13 @@
#include "airport_movement.h" #include "airport_movement.h"
#include "date.h" #include "date.h"
/* Uncomment this to print out a full report of the airport-structure
* You should either use
* - true: full-report, print out every state and choice with string-names
* OR
* - false: give a summarized report which only shows current and next position */
//#define DEBUG_AIRPORT false
static AirportFTAClass *CountryAirport; static AirportFTAClass *CountryAirport;
static AirportFTAClass *CityAirport; static AirportFTAClass *CityAirport;
static AirportFTAClass *Oilrig; static AirportFTAClass *Oilrig;
@ -32,10 +39,12 @@ static void AirportFTAClass_Destructor(AirportFTAClass *apc);
static uint16 AirportGetNofElements(const AirportFTAbuildup *apFA); static uint16 AirportGetNofElements(const AirportFTAbuildup *apFA);
static void AirportBuildAutomata(AirportFTAClass *apc, const AirportFTAbuildup *apFA); static void AirportBuildAutomata(AirportFTAClass *apc, const AirportFTAbuildup *apFA);
static byte AirportGetTerminalCount(const byte *terminals, byte *groups);
static byte AirportTestFTA(const AirportFTAClass *apc); static byte AirportTestFTA(const AirportFTAClass *apc);
#if 0
static void AirportPrintOut(const AirportFTAClass *apc, const bool full_report); #ifdef DEBUG_AIRPORT
#endif static void AirportPrintOut(const AirportFTAClass *apc, bool full_report);
#endif /* DEBUG_AIRPORT */
void InitializeAirports(void) void InitializeAirports(void)
{ {
@ -200,82 +209,57 @@ static void AirportFTAClass_Constructor(AirportFTAClass *apc,
) )
{ {
byte nofterminals, nofhelipads; byte nofterminals, nofhelipads;
byte nofterminalgroups = 0; byte nofterminalgroups, nofhelipadgroups;
byte nofhelipadgroups = 0;
const byte *curr;
int i;
nofterminals = nofhelipads = 0;
apc->size_x = size_x; apc->size_x = size_x;
apc->size_y = size_y; apc->size_y = size_y;
//now we read the number of terminals we have /* Set up the terminal and helipad count for an airport.
if (terminals != NULL) { * TODO: If there are more than 10 terminals or 4 helipads, internal variables
i = terminals[0]; * need to be changed, so don't allow that for now */
nofterminalgroups = i; nofterminals = AirportGetTerminalCount(terminals, &nofterminalgroups);
curr = terminals; if (nofterminals > MAX_TERMINALS) {
while (i-- > 0) { DEBUG(misc, 0) ("[Ap] Currently only maximum of %d terminals are supported (you wanted %d)", MAX_TERMINALS, nofterminals);
curr++; assert(nofterminals <= MAX_TERMINALS);
assert(*curr != 0); //we don't want to have an empty group
nofterminals += *curr;
}
} }
apc->terminals = terminals; apc->terminals = terminals;
//read helipads nofhelipads = AirportGetTerminalCount(helipads, &nofhelipadgroups);
if (helipads != NULL) { if (nofhelipads > MAX_HELIPADS) {
i = helipads[0]; DEBUG(misc, 0) ("[Ap] Currently only maximum of %d helipads are supported (you wanted %d)", MAX_HELIPADS, nofhelipads);
nofhelipadgroups = i; assert(nofhelipads <= MAX_HELIPADS);
curr = helipads;
while (i-- > 0) {
curr++;
assert(*curr != 0); //no empty groups please
nofhelipads += *curr;
}
} }
apc->helipads = helipads; apc->helipads = helipads;
// if there are more terminals than 6, internal variables have to be changed, so don't allow that /* Get the number of elements from the source table. We also double check this
// same goes for helipads * with the entry point which must be within bounds and use this information
if (nofterminals > MAX_TERMINALS) { printf("Currently only maximum of %2d terminals are supported (you wanted %2d)\n", MAX_TERMINALS, nofterminals);} * later on to build and validate the state machine */
if (nofhelipads > MAX_HELIPADS) { printf("Currently only maximum of %2d helipads are supported (you wanted %2d)\n", MAX_HELIPADS, nofhelipads);}
// terminals/helipads are divided into groups. Groups are computed by dividing the number
// of terminals by the number of groups. Half in half. If #terminals is uneven, first group
// will get the less # of terminals
assert(nofterminals <= MAX_TERMINALS);
assert(nofhelipads <= MAX_HELIPADS);
apc->nofelements = AirportGetNofElements(apFA); apc->nofelements = AirportGetNofElements(apFA);
// check if (entry_point >= apc->nofelements) {
if (entry_point >= apc->nofelements) {printf("Entry point (%2d) must be within the airport positions (which is max %2d)\n", entry_point, apc->nofelements);} DEBUG(misc, 0) ("[Ap] Entry (%d) must be within the airport (maximum %d)", entry_point, apc->nofelements);
assert(entry_point < apc->nofelements); assert(entry_point < apc->nofelements);
}
apc->acc_planes = acc_planes; apc->acc_planes = acc_planes;
apc->entry_point = entry_point; apc->entry_point = entry_point;
apc->airport_depots = depots; apc->airport_depots = depots;
apc->nof_depots = nof_depots; apc->nof_depots = nof_depots;
/* Build the state machine itself */
// build the state machine
AirportBuildAutomata(apc, apFA); AirportBuildAutomata(apc, apFA);
DEBUG(misc, 1) ("#Elements %2d; #Terminals %2d in %d group(s); #Helipads %2d in %d group(s); Entry Point %d", DEBUG(misc, 1) ("[Ap] #count %3d; #term %2d (%dgrp); #helipad %2d (%dgrp); entry %3d",
apc->nofelements, nofterminals, nofterminalgroups, nofhelipads, nofhelipadgroups, apc->entry_point apc->nofelements, nofterminals, nofterminalgroups, nofhelipads, nofhelipadgroups, apc->entry_point);
);
/* Test if everything went allright. This is only a rude static test checking
{ * the symantic correctness. By no means does passing the test mean that the
byte ret = AirportTestFTA(apc); * airport is working correctly or will not deadlock for example */
if (ret != MAX_ELEMENTS) printf("ERROR with element: %d\n", ret - 1); { byte ret = AirportTestFTA(apc);
if (ret != MAX_ELEMENTS) DEBUG(misc, 0) ("[Ap] ERROR with element: %d", ret - 1);
assert(ret == MAX_ELEMENTS); assert(ret == MAX_ELEMENTS);
} }
// print out full information
// true -- full info including heading, block, etc #ifdef DEBUG_AIRPORT
// false -- short info, only position and next position AirportPrintOut(apc, DEBUG_AIRPORT);
#if 0
AirportPrintOut(apc, false);
#endif #endif
} }
@ -296,6 +280,9 @@ static void AirportFTAClass_Destructor(AirportFTAClass *apc)
free(apc); free(apc);
} }
/** Get the number of elements of a source Airport state automata
* Since it is actually just a big array of AirportFTA types, we only
* know one element from the other by differing 'position' identifiers */
static uint16 AirportGetNofElements(const AirportFTAbuildup *apFA) static uint16 AirportGetNofElements(const AirportFTAbuildup *apFA)
{ {
int i; int i;
@ -312,29 +299,49 @@ static uint16 AirportGetNofElements(const AirportFTAbuildup *apFA)
return nofelements; return nofelements;
} }
/* We calculate the terminal/helipod count based on the data passed to us
* This data (terminals) contains an index as a first element as to how many
* groups there are, and then the number of terminals for each group */
static byte AirportGetTerminalCount(const byte *terminals, byte *groups)
{
byte i;
byte nof_terminals = 0;
*groups = 0;
if (terminals != NULL) {
i = terminals[0];
*groups = i;
while (i-- > 0) {
terminals++;
assert(*terminals != 0); // no empty groups please
nof_terminals += *terminals;
}
}
return nof_terminals;
}
static void AirportBuildAutomata(AirportFTAClass *apc, const AirportFTAbuildup *apFA) static void AirportBuildAutomata(AirportFTAClass *apc, const AirportFTAbuildup *apFA)
{ {
AirportFTA *FAutomata;
AirportFTA *current; AirportFTA *current;
uint16 internalcounter, i; AirportFTA *FAutomata = malloc(sizeof(AirportFTA) * apc->nofelements);
FAutomata = malloc(sizeof(AirportFTA) * apc->nofelements); uint16 internalcounter = 0;
apc->layout = FAutomata; uint16 i;
internalcounter = 0;
apc->layout = FAutomata;
for (i = 0; i < apc->nofelements; i++) { for (i = 0; i < apc->nofelements; i++) {
current = &apc->layout[i]; current = &apc->layout[i];
current->position = apFA[internalcounter].position; current->position = apFA[internalcounter].position;
current->heading = apFA[internalcounter].heading; current->heading = apFA[internalcounter].heading;
current->block = apFA[internalcounter].block; current->block = apFA[internalcounter].block;
current->next_position = apFA[internalcounter].next; current->next_position = apFA[internalcounter].next;
// outgoing nodes from the same position, create linked list // outgoing nodes from the same position, create linked list
while (current->position == apFA[internalcounter + 1].position) { while (current->position == apFA[internalcounter + 1].position) {
AirportFTA *newNode = malloc(sizeof(AirportFTA)); AirportFTA *newNode = malloc(sizeof(AirportFTA));
newNode->position = apFA[internalcounter + 1].position; newNode->position = apFA[internalcounter + 1].position;
newNode->heading = apFA[internalcounter + 1].heading; newNode->heading = apFA[internalcounter + 1].heading;
newNode->block = apFA[internalcounter + 1].block; newNode->block = apFA[internalcounter + 1].block;
newNode->next_position = apFA[internalcounter + 1].next; newNode->next_position = apFA[internalcounter + 1].next;
// create link // create link
current->next = newNode; current->next = newNode;
@ -348,28 +355,28 @@ static void AirportBuildAutomata(AirportFTAClass *apc, const AirportFTAbuildup *
static byte AirportTestFTA(const AirportFTAClass *apc) static byte AirportTestFTA(const AirportFTAClass *apc)
{ {
byte position, i, next_element; byte position, i, next_position;
AirportFTA *temp; AirportFTA *current;
next_element = 0; next_position = 0;
for (i = 0; i < apc->nofelements; i++) { for (i = 0; i < apc->nofelements; i++) {
position = apc->layout[i].position; position = apc->layout[i].position;
if (position != next_element) return i; if (position != next_position) return i;
temp = &apc->layout[i]; current = &apc->layout[i];
do { do {
if (temp->heading > MAX_HEADINGS && temp->heading != 255) return i; if (current->heading > MAX_HEADINGS && current->heading != 255) return i;
if (temp->heading == 0 && temp->next != 0) return i; if (current->heading == 0 && current->next != 0) return i;
if (position != temp->position) return i; if (position != current->position) return i;
if (temp->next_position >= apc->nofelements) return i; if (current->next_position >= apc->nofelements) return i;
temp = temp->next; current = current->next;
} while (temp != NULL); } while (current != NULL);
next_element++; next_position++;
} }
return MAX_ELEMENTS; return MAX_ELEMENTS;
} }
#if 0 #ifdef DEBUG_AIRPORT
static const char* const _airport_heading_strings[] = { static const char* const _airport_heading_strings[] = {
"TO_ALL", "TO_ALL",
"HANGAR", "HANGAR",
@ -397,7 +404,6 @@ static const char* const _airport_heading_strings[] = {
"DUMMY" // extra heading for 255 "DUMMY" // extra heading for 255
}; };
static uint AirportBlockToString(uint32 block) static uint AirportBlockToString(uint32 block)
{ {
uint i = 0; uint i = 0;
@ -409,35 +415,23 @@ static uint AirportBlockToString(uint32 block)
return i; return i;
} }
static void AirportPrintOut(const AirportFTAClass *apc, bool full_report)
static void AirportPrintOut(const AirportFTAClass *apc, const bool full_report)
{ {
byte heading; uint16 i;
uint i;
if (!full_report) printf("(P = Current Position; NP = Next Position)\n");
printf("(P = Current Position; NP = Next Position)\n");
for (i = 0; i < apc->nofelements; i++) { for (i = 0; i < apc->nofelements; i++) {
const AirportFTA* temp = &apc->layout[i]; AirportFTA *current = &apc->layout[i];
if (full_report) { for (; current != NULL; current = current->next) {
heading = (temp->heading == 255) ? MAX_HEADINGS + 1 : temp->heading;
printf("Pos:%2d NPos:%2d Heading:%15s Block:%2d\n",
temp->position, temp->next_position,
_airport_heading_strings[heading], AirportBlockToString(temp->block)
);
} else {
printf("P:%2d NP:%2d", temp->position, temp->next_position);
}
while (temp->next != NULL) {
temp = temp->next;
if (full_report) { if (full_report) {
heading = (temp->heading == 255) ? MAX_HEADINGS + 1 : temp->heading; byte heading = (current->heading == 255) ? MAX_HEADINGS + 1 : current->heading;
printf("Pos:%2d NPos:%2d Heading:%15s Block:%2d\n", printf("\tPos:%2d NPos:%2d Heading:%15s Block:%2d\n", current->position,
temp->position, temp->next_position, current->next_position, _airport_heading_strings[heading],
_airport_heading_strings[heading], AirportBlockToString(temp->block) AirportBlockToString(current->block));
);
} else { } else {
printf("P:%2d NP:%2d", temp->position, temp->next_position); printf("P:%2d NP:%2d", current->position, current->next_position);
} }
} }
printf("\n"); printf("\n");
@ -475,7 +469,7 @@ uint32 GetValidAirports(void)
{ {
uint32 bytemask = _avail_aircraft; /// sets the first 3 bytes, 0 - 2, @see AdjustAvailAircraft() uint32 bytemask = _avail_aircraft; /// sets the first 3 bytes, 0 - 2, @see AdjustAvailAircraft()
if (_cur_year >= 1980) SETBIT(bytemask, 3); // metropilitan airport if (_cur_year >= 1980) SETBIT(bytemask, 3); // metropolitan airport
if (_cur_year >= 1990) SETBIT(bytemask, 4); // international airport if (_cur_year >= 1990) SETBIT(bytemask, 4); // international airport
if (_cur_year >= 1983) SETBIT(bytemask, 5); // commuter airport if (_cur_year >= 1983) SETBIT(bytemask, 5); // commuter airport
if (_cur_year >= 1976) SETBIT(bytemask, 6); // helidepot if (_cur_year >= 1976) SETBIT(bytemask, 6); // helidepot

View File

@ -369,9 +369,9 @@ static const AirportMovingData _airport_moving_data_oilrig[9] = {
/////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////
/////**********Movement Machine on Airports*********************/////// /////**********Movement Machine on Airports*********************///////
// first element of depots array tells us how many depots there are (to know size of array) /* First element of depots array tells us how many depots there are (to know size of array)
// this may be changed later when airports are moved to external file * this may be changed later when airports are moved to external file
* First element of terminals array tells us how many terminal-groups there are */
static const TileIndexDiffC _airport_depots_country[] = {{3, 0}}; static const TileIndexDiffC _airport_depots_country[] = {{3, 0}};
static const byte _airport_terminal_country[] = {1, 2}; static const byte _airport_terminal_country[] = {1, 2};
static const AirportFTAbuildup _airport_fta_country[] = { static const AirportFTAbuildup _airport_fta_country[] = {