diff options
Diffstat (limited to 'src/aircraft_cmd.cpp')
-rw-r--r-- | src/aircraft_cmd.cpp | 91 |
1 files changed, 32 insertions, 59 deletions
diff --git a/src/aircraft_cmd.cpp b/src/aircraft_cmd.cpp index 6664cef72..6dc0e05ee 100644 --- a/src/aircraft_cmd.cpp +++ b/src/aircraft_cmd.cpp @@ -71,14 +71,6 @@ void Aircraft::UpdateDeltaXY(Direction direction) this->y_extent = GB(x, 24, 8); } - -/** - * this maps the terminal to its corresponding state and block flag - * currently set for 10 terms, 4 helipads - */ -static const byte _airport_terminal_state[] = {2, 3, 4, 5, 6, 7, 19, 20, 0, 0, 8, 9, 21, 22}; -static const byte _airport_terminal_flag[] = {0, 1, 2, 3, 4, 5, 22, 23, 0, 0, 6, 7, 24, 25}; - static bool AirportMove(Aircraft *v, const AirportFTAClass *apc); static bool AirportSetBlocks(Aircraft *v, const AirportFTA *current_pos, const AirportFTAClass *apc); static bool AirportHasBlock(Aircraft *v, const AirportFTA *current_pos, const AirportFTAClass *apc); @@ -1638,7 +1630,6 @@ static AircraftStateHandler * const _aircraft_state_handlers[] = { AircraftEventHandler_AtTerminal, // TERM7 = 19 AircraftEventHandler_AtTerminal, // TERM8 = 20 AircraftEventHandler_AtTerminal, // HELIPAD3 = 21 - AircraftEventHandler_AtTerminal, // HELIPAD4 = 22 }; static void AirportClearBlock(const Aircraft *v, const AirportFTAClass *apc) @@ -1780,14 +1771,39 @@ static bool AirportSetBlocks(Aircraft *v, const AirportFTA *current_pos, const A return true; } +/** + * Combination of aircraft state for going to a certain terminal and the + * airport flag for that terminal block. + */ +struct MovementTerminalMapping { + AirportMovementStates state; ///< Aircraft movement state when going to this terminal. + uint64 airport_flag; ///< Bitmask in the airport flags that need to be free for this terminal. +}; + +/** A list of all valid terminals and their associated blocks. */ +static const MovementTerminalMapping _airport_terminal_mapping[] = { + {TERM1, TERM1_block}, + {TERM2, TERM2_block}, + {TERM3, TERM3_block}, + {TERM4, TERM4_block}, + {TERM5, TERM5_block}, + {TERM6, TERM6_block}, + {TERM7, TERM7_block}, + {TERM8, TERM8_block}, + {HELIPAD1, HELIPAD1_block}, + {HELIPAD2, HELIPAD2_block}, + {HELIPAD3, HELIPAD3_block}, +}; + static bool FreeTerminal(Aircraft *v, byte i, byte last_terminal) { + assert(last_terminal <= lengthof(_airport_terminal_mapping)); Station *st = Station::Get(v->targetairport); for (; i < last_terminal; i++) { - if (!HasBit(st->airport.flags, _airport_terminal_flag[i])) { + if ((st->airport.flags & _airport_terminal_mapping[i].airport_flag) == 0) { /* TERMINAL# HELIPAD# */ - v->state = _airport_terminal_state[i]; // start moving to that terminal/helipad - SetBit(st->airport.flags, _airport_terminal_flag[i]); // occupy terminal/helipad + v->state = _airport_terminal_mapping[i].state; // start moving to that terminal/helipad + SETBITS(st->airport.flags, _airport_terminal_mapping[i].airport_flag); // occupy terminal/helipad return true; } } @@ -1850,58 +1866,15 @@ static bool AirportFindFreeTerminal(Aircraft *v, const AirportFTAClass *apc) return FreeTerminal(v, 0, GetNumTerminals(apc)); } -static uint GetNumHelipads(const AirportFTAClass *apc) -{ - uint num = 0; - - for (uint i = apc->helipads[0]; i > 0; i--) num += apc->helipads[i]; - - return num; -} - - static bool AirportFindFreeHelipad(Aircraft *v, const AirportFTAClass *apc) { /* if an airport doesn't have helipads, use terminals */ if (apc->helipads == NULL) return AirportFindFreeTerminal(v, apc); - /* if there are more helicoptergroups, pick one, just as in AirportFindFreeTerminal() */ - if (apc->helipads[0] > 1) { - const Station *st = Station::Get(v->targetairport); - const AirportFTA *temp = apc->layout[v->pos].next; - - while (temp != NULL) { - if (temp->heading == 255) { - if (!(st->airport.flags & temp->block)) { - - /* read which group do we want to go to? - * (the first free group) */ - uint target_group = temp->next_position + 1; - - /* at what terminal does the group start? - * that means, sum up all terminals of - * groups with lower number */ - uint group_start = 0; - for (uint i = 1; i < target_group; i++) { - group_start += apc->helipads[i]; - } - - uint group_end = group_start + apc->helipads[target_group]; - if (FreeTerminal(v, group_start, group_end)) return true; - } - } else { - /* once the heading isn't 255, we've exhausted the possible blocks. - * So we cannot move */ - return false; - } - temp = temp->next; - } - } else { - /* only 1 helicoptergroup, check all helipads - * The blocks for helipads start after the last terminal (MAX_TERMINALS) */ - return FreeTerminal(v, MAX_TERMINALS, GetNumHelipads(apc) + MAX_TERMINALS); - } - return false; // it shouldn't get here anytime, but just to be sure + assert(apc->helipads[0] == 1); + /* only 1 helicoptergroup, check all helipads + * The blocks for helipads start after the last terminal (MAX_TERMINALS) */ + return FreeTerminal(v, MAX_TERMINALS, apc->helipads[1] + MAX_TERMINALS); } static bool AircraftEventHandler(Aircraft *v, int loop) |