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