tron@2186: /* $Id$ */ tron@2186: truelight@0: #include "stdafx.h" Darkvater@1891: #include "openttd.h" tron@507: #include "table/strings.h" tron@2163: #include "functions.h" tron@679: #include "map.h" tron@1209: #include "tile.h" truelight@0: #include "vehicle.h" truelight@0: #include "command.h" truelight@0: #include "pathfind.h" truelight@0: #include "station.h" truelight@0: #include "news.h" truelight@0: #include "engine.h" truelight@0: #include "player.h" tron@337: #include "sound.h" matthijs@1247: #include "npf.h" truelight@1313: #include "depot.h" tron@2159: #include "vehicle_gui.h" truelight@0: truelight@0: static const uint16 _ship_sprites[] = {0x0E5D, 0x0E55, 0x0E65, 0x0E6D}; truelight@0: static const byte _ship_sometracks[4] = {0x19, 0x16, 0x25, 0x2A}; truelight@0: tron@1977: static byte GetTileShipTrackStatus(TileIndex tile) tron@1977: { truelight@159: uint32 r = GetTileTrackStatus(tile, TRANSPORT_WATER); truelight@0: return r | r >> 8; truelight@0: } truelight@0: tron@2477: void DrawShipEngine(int x, int y, EngineID engine, uint32 image_ormod) truelight@0: { tron@538: int spritenum = ShipVehInfo(engine)->image_index; truelight@0: truelight@0: if (is_custom_sprite(spritenum)) { darkvater@358: int sprite = GetCustomVehicleIcon(engine, 6); truelight@0: tron@2639: if (sprite != 0) { truelight@0: DrawSprite(sprite | image_ormod, x, y); truelight@0: return; truelight@0: } peter1138@2464: spritenum = orig_ship_vehicle_info[engine - SHIP_ENGINES_INDEX].image_index; truelight@0: } truelight@0: DrawSprite((6 + _ship_sprites[spritenum]) | image_ormod, x, y); truelight@0: } truelight@0: Darkvater@1790: int GetShipImage(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: tron@2639: if (sprite != 0) return sprite; peter1138@2464: spritenum = orig_ship_vehicle_info[v->engine_type - SHIP_ENGINES_INDEX].image_index; truelight@0: } truelight@0: return _ship_sprites[spritenum] + direction; truelight@0: } truelight@0: tron@2630: static const Depot* FindClosestShipDepot(const Vehicle* v) truelight@0: { tron@2630: const Depot* depot; tron@2630: const Depot* best_depot = NULL; tron@1977: uint dist; tron@1977: uint best_dist = (uint)-1; tron@1977: TileIndex tile; tron@1977: TileIndex tile2 = v->tile; truelight@0: matthijs@1247: if (_patches.new_pathfinding_all) { matthijs@1247: NPFFoundTargetData ftd; matthijs@1752: byte trackdir = GetVehicleTrackdir(v); matthijs@2006: ftd = NPFRouteToDepotTrialError(v->tile, trackdir, TRANSPORT_WATER, v->owner, INVALID_RAILTYPE); tron@2639: if (ftd.best_bird_dist == 0) { truelight@1313: best_depot = GetDepotByTile(ftd.node.tile); /* Found target */ tron@2639: } else { truelight@1313: best_depot = NULL; /* Did not find target */ tron@2639: } matthijs@1247: } else { truelight@1313: FOR_ALL_DEPOTS(depot) { truelight@1313: tile = depot->xy; matthijs@1330: if (IsValidDepot(depot) && IsTileDepotType(tile, TRANSPORT_WATER) && IsTileOwner(tile, v->owner)) { matthijs@1247: dist = DistanceManhattan(tile, tile2); matthijs@1247: if (dist < best_dist) { matthijs@1247: best_dist = dist; truelight@1313: best_depot = depot; matthijs@1247: } truelight@0: } truelight@0: } truelight@0: } truelight@0: return best_depot; truelight@0: } truelight@0: truelight@0: static void CheckIfShipNeedsService(Vehicle *v) truelight@0: { tron@2630: const Depot* depot; truelight@0: tron@2639: if (_patches.servint_ships == 0) return; tron@2639: if (!VehicleNeedsService(v)) return; tron@2639: if (v->vehstatus & VS_STOPPED) return; truelight@193: 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@0: truelight@1313: depot = FindClosestShipDepot(v); truelight@0: truelight@1313: if (depot == NULL || DistanceManhattan(v->tile, depot->xy) > 12) { tron@555: 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: return; truelight@0: } truelight@0: tron@555: v->current_order.type = OT_GOTO_DEPOT; tron@555: v->current_order.flags = OF_NON_STOP; truelight@1313: v->current_order.station = depot->index; truelight@1313: v->dest_tile = depot->xy; darkvater@755: InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); truelight@0: } truelight@0: truelight@0: void OnNewDay_Ship(Vehicle *v) truelight@0: { truelight@0: int32 cost; truelight@0: truelight@0: if ((++v->day_counter & 7) == 0) truelight@0: DecreaseVehicleValue(v); truelight@0: truelight@0: CheckVehicleBreakdown(v); truelight@0: AgeVehicle(v); truelight@0: CheckIfShipNeedsService(v); truelight@0: celestar@1053: CheckOrders(v->index, OC_INIT); truelight@193: tron@2639: if (v->vehstatus & VS_STOPPED) return; dominik@19: tron@538: cost = ShipVehInfo(v->engine_type)->running_cost * _price.ship_running / 364; truelight@0: v->profit_this_year -= cost >> 8; truelight@0: truelight@0: SET_EXPENSES_TYPE(EXPENSES_SHIP_RUN); truelight@0: SubtractMoneyFromPlayerFract(v->owner, cost); truelight@0: truelight@0: InvalidateWindow(WC_VEHICLE_DETAILS, v->index); Celestar@1055: //we need this for the profit Celestar@1055: InvalidateWindowClasses(WC_SHIPS_LIST); truelight@0: } truelight@0: truelight@0: static void HandleBrokenShip(Vehicle *v) truelight@0: { truelight@0: if (v->breakdown_ctr != 1) { truelight@0: v->breakdown_ctr = 1; truelight@0: v->cur_speed = 0; truelight@0: truelight@0: if (v->breakdowns_since_last_service != 255) truelight@0: v->breakdowns_since_last_service++; truelight@193: truelight@0: InvalidateWindow(WC_VEHICLE_VIEW, v->index); truelight@0: InvalidateWindow(WC_VEHICLE_DETAILS, v->index); truelight@193: tron@541: SndPlayVehicleFx((_opt.landscape != LT_CANDY) ? tron@541: SND_10_TRAIN_BREAKDOWN : SND_3A_COMEDY_BREAKDOWN_2, v); truelight@0: truelight@0: if (!(v->vehstatus & VS_HIDDEN)) { truelight@0: Vehicle *u = CreateEffectVehicleRel(v, 4, 4, 5, EV_BREAKDOWN_SMOKE); tron@2639: if (u != NULL) u->u.special.unk0 = v->breakdown_delay * 2; truelight@0: } truelight@0: } truelight@0: truelight@0: if (!(v->tick_counter & 1)) { truelight@0: if (!--v->breakdown_delay) { truelight@0: v->breakdown_ctr = 0; bjarni@1151: InvalidateWindow(WC_VEHICLE_VIEW, v->index); truelight@0: } truelight@0: } truelight@0: } truelight@0: truelight@0: static void MarkShipDirty(Vehicle *v) truelight@0: { truelight@0: v->cur_image = GetShipImage(v, v->direction); truelight@0: MarkAllViewportsDirty(v->left_coord, v->top_coord, v->right_coord + 1, v->bottom_coord + 1); truelight@0: } truelight@0: truelight@0: static void PlayShipSound(Vehicle *v) truelight@0: { tron@538: SndPlayVehicleFx(ShipVehInfo(v->engine_type)->sfx, v); truelight@0: } truelight@0: tron@909: static const TileIndexDiffC _dock_offs[] = { tron@909: { 2, 0}, tron@909: {-2, 0}, tron@909: { 0, 2}, tron@909: { 2, 0}, tron@909: { 0, -2}, tron@909: { 0, 0}, tron@909: { 0, 0}, tron@909: { 0, 0} truelight@0: }; truelight@0: truelight@0: static void ProcessShipOrder(Vehicle *v) truelight@0: { truelight@1024: const Order *order; truelight@0: tron@555: if (v->current_order.type >= OT_GOTO_DEPOT && tron@555: v->current_order.type <= OT_LEAVESTATION) { tron@555: if (v->current_order.type != OT_GOTO_DEPOT || tron@555: !(v->current_order.flags & OF_UNLOAD)) truelight@0: return; truelight@0: } truelight@0: 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)) { truelight@0: v->cur_order_index++; truelight@0: } truelight@0: truelight@0: truelight@0: if (v->cur_order_index >= v->num_orders) truelight@0: v->cur_order_index = 0; truelight@0: truelight@1024: order = GetVehicleOrder(v, v->cur_order_index); truelight@0: truelight@1024: if (order == NULL) { truelight@1024: v->current_order.type = OT_NOTHING; tron@555: v->current_order.flags = 0; truelight@0: v->dest_tile = 0; truelight@0: return; truelight@0: } truelight@0: 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@1024: if (order->type == OT_GOTO_STATION) { truelight@1024: const Station *st; truelight@1024: truelight@1024: if (order->station == v->last_station_visited) truelight@1266: v->last_station_visited = INVALID_STATION; truelight@193: truelight@1024: st = GetStation(order->station); truelight@0: if (st->dock_tile != 0) { tron@2049: v->dest_tile = TILE_ADD(st->dock_tile, ToTileIndexDiff(_dock_offs[_m[st->dock_tile].m5-0x4B])); truelight@0: } truelight@1024: } else if (order->type == OT_GOTO_DEPOT) { truelight@1313: v->dest_tile = GetDepot(order->station)->xy; truelight@0: } else { truelight@0: v->dest_tile = 0; truelight@0: } truelight@1024: truelight@1024: InvalidateVehicleOrder(v); celestar@1020: Celestar@1055: InvalidateWindowClasses(WC_SHIPS_LIST); truelight@0: } truelight@0: truelight@0: static void HandleShipLoading(Vehicle *v) truelight@0: { tron@2639: if (v->current_order.type == OT_NOTHING) return; truelight@193: tron@555: if (v->current_order.type != OT_DUMMY) { tron@2639: if (v->current_order.type != OT_LOADING) return; tron@2639: if (--v->load_unload_time_rem) return; truelight@0: tron@555: if (v->current_order.flags & OF_FULL_LOAD && CanFillVehicle(v)) { truelight@0: SET_EXPENSES_TYPE(EXPENSES_SHIP_INC); truelight@0: if (LoadUnloadVehicle(v)) { truelight@0: InvalidateWindow(WC_SHIPS_LIST, v->owner); truelight@0: MarkShipDirty(v); truelight@0: } truelight@0: return; truelight@0: } truelight@0: PlayShipSound(v); truelight@193: truelight@0: { tron@555: Order b = v->current_order; tron@555: v->current_order.type = OT_LEAVESTATION; tron@555: v->current_order.flags = 0; tron@2639: if (!(b.flags & OF_NON_STOP)) return; truelight@0: } truelight@0: } truelight@0: truelight@0: v->cur_order_index++; truelight@1024: InvalidateVehicleOrder(v); truelight@0: } truelight@0: truelight@0: static void UpdateShipDeltaXY(Vehicle *v, int dir) truelight@0: { truelight@0: #define MKIT(d,c,b,a) ((a&0xFF)<<24) | ((b&0xFF)<<16) | ((c&0xFF)<<8) | ((d&0xFF)<<0) truelight@0: static const uint32 _delta_xy_table[8] = { truelight@0: MKIT( -3, -3, 6, 6), truelight@0: MKIT(-16, -3, 32, 6), truelight@0: MKIT( -3, -3, 6, 6), truelight@0: MKIT( -3,-16, 6, 32), truelight@0: MKIT( -3, -3, 6, 6), truelight@0: MKIT(-16, -3, 32, 6), truelight@0: MKIT( -3, -3, 6, 6), truelight@0: MKIT( -3,-16, 6, 32), truelight@0: }; truelight@0: #undef MKIT truelight@0: uint32 x = _delta_xy_table[dir]; tron@2150: v->x_offs = GB(x, 0, 8); tron@2150: v->y_offs = GB(x, 8, 8); tron@2150: v->sprite_width = GB(x, 16, 8); tron@2150: v->sprite_height = GB(x, 24, 8); truelight@0: } truelight@0: truelight@0: static void RecalcShipStuff(Vehicle *v) truelight@0: { truelight@0: UpdateShipDeltaXY(v, v->direction); truelight@0: v->cur_image = GetShipImage(v, v->direction); truelight@0: MarkShipDirty(v); truelight@0: InvalidateWindow(WC_VEHICLE_DEPOT, v->tile); truelight@0: } truelight@0: tron@909: static const TileIndexDiffC _ship_leave_depot_offs[] = { tron@909: {-1, 0}, tron@909: { 0, -1} truelight@0: }; truelight@0: truelight@0: static void CheckShipLeaveDepot(Vehicle *v) truelight@0: { tron@1977: TileIndex tile; truelight@0: int d; truelight@0: uint m; truelight@0: tron@2639: if (v->u.ship.state != 0x80) return; truelight@0: truelight@0: tile = v->tile; tron@2049: d = (_m[tile].m5&2) ? 1 : 0; truelight@0: truelight@0: // Check first side tron@909: if (_ship_sometracks[d] & GetTileShipTrackStatus(TILE_ADD(tile, ToTileIndexDiff(_ship_leave_depot_offs[d])))) { truelight@0: m = (d==0) ? 0x101 : 0x207; truelight@0: // Check second side tron@909: } else if (_ship_sometracks[d+2] & GetTileShipTrackStatus(TILE_ADD(tile, -2 * ToTileIndexDiff(_ship_leave_depot_offs[d])))) { truelight@0: m = (d==0) ? 0x105 : 0x203; truelight@0: } else { truelight@0: return; truelight@0: } tron@2635: v->direction = GB(m, 0, 8); tron@2635: v->u.ship.state = GB(m, 8, 8); truelight@0: v->vehstatus &= ~VS_HIDDEN; truelight@0: truelight@0: v->cur_speed = 0; truelight@0: RecalcShipStuff(v); truelight@0: truelight@0: PlayShipSound(v); bjarni@578: VehicleServiceInDepot(v); Celestar@1055: InvalidateWindowClasses(WC_SHIPS_LIST); truelight@0: } truelight@0: truelight@0: static bool ShipAccelerate(Vehicle *v) truelight@0: { truelight@0: uint spd; truelight@0: byte t; truelight@0: truelight@0: spd = min(v->cur_speed + 1, v->max_speed); truelight@0: truelight@0: //updates statusbar only if speed have changed to save CPU time truelight@0: if (spd != v->cur_speed) { truelight@193: v->cur_speed = spd; truelight@0: if (_patches.vehicle_speed) darkvater@755: InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); truelight@0: } truelight@0: truelight@0: // Decrease somewhat when turning tron@2639: if (!(v->direction & 1)) spd = spd * 3 / 4; truelight@0: tron@2639: if (spd == 0) return false; tron@2639: if ((byte)++spd == 0) return true; truelight@0: truelight@0: v->progress = (t = v->progress) - (byte)spd; truelight@0: truelight@0: return (t < v->progress); truelight@0: } truelight@0: tron@2817: static int32 EstimateShipCost(EngineID engine_type) Darkvater@1793: { Darkvater@1793: return ShipVehInfo(engine_type)->base_cost * (_price.ship_base>>3)>>5; Darkvater@1793: } truelight@0: truelight@0: static void ShipEnterDepot(Vehicle *v) truelight@0: { truelight@0: v->u.ship.state = 0x80; truelight@0: v->vehstatus |= VS_HIDDEN; truelight@0: v->cur_speed = 0; truelight@0: RecalcShipStuff(v); truelight@193: bjarni@578: VehicleServiceInDepot(v); bjarni@578: truelight@0: InvalidateWindow(WC_VEHICLE_DETAILS, v->index); truelight@0: tron@445: TriggerVehicle(v, VEHICLE_TRIGGER_DEPOT); tron@445: tron@555: if (v->current_order.type == OT_GOTO_DEPOT) { tron@555: Order t; tron@555: truelight@0: InvalidateWindow(WC_VEHICLE_VIEW, v->index); truelight@193: tron@555: t = v->current_order; tron@555: v->current_order.type = OT_DUMMY; tron@555: v->current_order.flags = 0; truelight@0: celestar@1530: if (HASBIT(t.flags, OFB_PART_OF_ORDERS)) { tron@555: v->cur_order_index++; celestar@1530: } else if (HASBIT(t.flags, OFB_HALT_IN_DEPOT)) { truelight@0: v->vehstatus |= VS_STOPPED; truelight@0: if (v->owner == _local_player) { tron@534: SetDParam(0, v->unitnumber); truelight@0: AddNewsItem( truelight@0: STR_981C_SHIP_IS_WAITING_IN_DEPOT, truelight@0: NEWS_FLAGS(NM_SMALL, NF_VIEWPORT|NF_VEHICLE, NT_ADVICE, 0), truelight@0: v->index, truelight@0: 0); truelight@0: } truelight@0: } truelight@0: } Celestar@1055: InvalidateWindowClasses(WC_SHIPS_LIST); truelight@0: } truelight@0: tron@2630: static void ShipArrivesAt(const Vehicle* v, Station* st) truelight@0: { truelight@0: /* Check if station was ever visited before */ truelight@0: if (!(st->had_vehicle_of_type & HVOT_SHIP)) { truelight@0: uint32 flags; tron@2639: truelight@0: st->had_vehicle_of_type |= HVOT_SHIP; tron@2639: tron@534: SetDParam(0, st->index); 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_9833_CITIZENS_CELEBRATE_FIRST, truelight@0: flags, truelight@0: v->index, truelight@0: 0); truelight@0: } truelight@0: } truelight@0: truelight@0: typedef struct { tron@1977: TileIndex skiptile; tron@1977: TileIndex dest_coords; truelight@0: uint best_bird_dist; truelight@0: uint best_length; truelight@0: } PathFindShip; truelight@0: tron@1977: static bool ShipTrackFollower(TileIndex tile, PathFindShip *pfs, int track, uint length, byte *state) truelight@0: { truelight@0: // Found dest? truelight@0: if (tile == pfs->dest_coords) { truelight@0: pfs->best_bird_dist = 0; truelight@193: truelight@0: pfs->best_length = minu(pfs->best_length, length); truelight@0: return true; truelight@0: } truelight@0: truelight@0: // Skip this tile in the calculation truelight@0: if (tile != pfs->skiptile) { tron@1245: pfs->best_bird_dist = minu(pfs->best_bird_dist, DistanceMaxPlusManhattan(pfs->dest_coords, tile)); truelight@0: } truelight@193: truelight@0: return false; truelight@0: } truelight@0: truelight@0: static const byte _ship_search_directions[6][4] = { truelight@0: { 0, 9, 2, 9 }, truelight@0: { 9, 1, 9, 3 }, truelight@0: { 9, 0, 3, 9 }, truelight@0: { 1, 9, 9, 2 }, truelight@0: { 3, 2, 9, 9 }, truelight@0: { 9, 9, 1, 0 }, truelight@0: }; truelight@0: truelight@0: static const byte _pick_shiptrack_table[6] = {1, 3, 2, 2, 0, 0}; truelight@0: tron@1977: static uint FindShipTrack(Vehicle *v, TileIndex tile, int dir, uint bits, TileIndex skiptile, int *track) truelight@0: { truelight@0: PathFindShip pfs; truelight@0: int i, best_track; truelight@0: uint best_bird_dist = 0; truelight@0: uint best_length = 0; truelight@0: uint r; truelight@0: byte ship_dir = v->direction & 3; truelight@0: truelight@0: pfs.dest_coords = v->dest_tile; truelight@0: pfs.skiptile = skiptile; truelight@0: truelight@0: best_track = -1; truelight@0: truelight@0: do { truelight@0: i = FIND_FIRST_BIT(bits); truelight@0: bits = KILL_FIRST_BIT(bits); truelight@193: truelight@0: pfs.best_bird_dist = (uint)-1; truelight@0: pfs.best_length = (uint)-1; truelight@0: truelight@159: FollowTrack(tile, 0x3800 | TRANSPORT_WATER, _ship_search_directions[i][dir], (TPFEnumProc*)ShipTrackFollower, NULL, &pfs); truelight@193: truelight@0: if (best_track >= 0) { truelight@0: if (pfs.best_bird_dist != 0) { truelight@0: /* neither reached the destination, pick the one with the smallest bird dist */ truelight@0: if (pfs.best_bird_dist > best_bird_dist) goto bad; truelight@0: if (pfs.best_bird_dist < best_bird_dist) goto good; truelight@0: } else { truelight@0: if (pfs.best_length > best_length) goto bad; truelight@0: if (pfs.best_length < best_length) goto good; truelight@0: } truelight@193: truelight@193: /* if we reach this position, there's two paths of equal value so far. truelight@0: * pick one randomly. */ tron@2150: r = GB(Random(), 0, 8); truelight@0: if (_pick_shiptrack_table[i] == ship_dir) r += 80; truelight@0: if (_pick_shiptrack_table[best_track] == ship_dir) r -= 80; truelight@0: if (r <= 127) goto bad; truelight@0: } truelight@0: good:; truelight@0: best_track = i; truelight@0: best_bird_dist = pfs.best_bird_dist; truelight@0: best_length = pfs.best_length; truelight@0: bad:; truelight@0: truelight@0: } while (bits != 0); truelight@0: truelight@0: *track = best_track; truelight@0: return best_bird_dist; truelight@0: } truelight@0: matthijs@1247: /* returns the track to choose on the next tile, or -1 when it's better to matthijs@1247: * reverse. The tile given is the tile we are about to enter, enterdir is the matthijs@1247: * direction in which we are entering the tile */ tron@1977: static int ChooseShipTrack(Vehicle *v, TileIndex tile, int enterdir, uint tracks) truelight@0: { matthijs@1247: assert(enterdir>=0 && enterdir<=3); truelight@0: matthijs@1247: if (_patches.new_pathfinding_all) { matthijs@1247: NPFFindStationOrTileData fstd; matthijs@1247: NPFFoundTargetData ftd; tron@1977: TileIndex src_tile = TILE_ADD(tile, TileOffsByDir(ReverseDiagdir(enterdir))); matthijs@1752: byte trackdir = GetVehicleTrackdir(v); matthijs@1752: assert (trackdir != 0xFF); /* Check that we are not in a depot */ matthijs@1247: matthijs@1247: NPFFillWithOrderData(&fstd, v); matthijs@1247: Darkvater@2916: ftd = NPFRouteToStationOrTile(src_tile, trackdir, &fstd, TRANSPORT_WATER, v->owner, INVALID_RAILTYPE); matthijs@1247: tron@2639: if (ftd.best_trackdir != 0xff) { matthijs@1698: /* If ftd.best_bird_dist is 0, we found our target and ftd.best_trackdir contains matthijs@1698: the direction we need to take to get there, if ftd.best_bird_dist is not 0, matthijs@1698: we did not find our target, but ftd.best_trackdir contains the direction leading matthijs@1698: to the tile closest to our target. */ matthijs@1247: return ftd.best_trackdir & 7; /* TODO: Wrapper function? */ tron@2639: } else { matthijs@1698: return -1; /* Already at target, reverse? */ tron@2639: } matthijs@1247: } else { matthijs@1247: uint b; matthijs@1247: uint tot_dist, dist; matthijs@1247: int track; tron@1977: TileIndex tile2; matthijs@1247: matthijs@1247: tile2 = TILE_ADD(tile, -TileOffsByDir(enterdir)); matthijs@1247: tot_dist = (uint)-1; matthijs@1247: matthijs@1247: /* Let's find out how far it would be if we would reverse first */ matthijs@1942: b = GetTileShipTrackStatus(tile2) & _ship_sometracks[ReverseDiagdir(enterdir)] & v->u.ship.state; matthijs@1247: if (b != 0) { matthijs@1942: dist = FindShipTrack(v, tile2, ReverseDiagdir(enterdir), b, tile, &track); matthijs@1247: if (dist != (uint)-1) matthijs@1247: tot_dist = dist + 1; matthijs@1247: } matthijs@1247: /* And if we would not reverse? */ matthijs@1247: dist = FindShipTrack(v, tile, enterdir, tracks, 0, &track); matthijs@1247: if (dist > tot_dist) matthijs@1247: /* We could better reverse */ matthijs@1247: return -1; matthijs@1247: return track; truelight@0: } truelight@0: } truelight@0: truelight@0: static const byte _new_vehicle_direction_table[11] = { truelight@0: 0, 7, 6, 0, truelight@0: 1, 0, 5, 0, truelight@0: 2, 3, 4, truelight@0: }; truelight@0: tron@1977: static int ShipGetNewDirectionFromTiles(TileIndex new_tile, TileIndex old_tile) truelight@0: { tron@926: uint offs = (TileY(new_tile) - TileY(old_tile) + 1) * 4 + tron@926: TileX(new_tile) - TileX(old_tile) + 1; truelight@0: assert(offs < 11 && offs != 3 && offs != 7); truelight@0: return _new_vehicle_direction_table[offs]; truelight@0: } truelight@0: truelight@0: static int ShipGetNewDirection(Vehicle *v, int x, int y) truelight@0: { truelight@0: uint offs = (y - v->y_pos + 1) * 4 + (x - v->x_pos + 1); truelight@0: assert(offs < 11 && offs != 3 && offs != 7); truelight@0: return _new_vehicle_direction_table[offs]; truelight@0: } truelight@0: tron@1977: static int GetAvailShipTracks(TileIndex tile, int dir) truelight@0: { truelight@159: uint32 r = GetTileTrackStatus(tile, TRANSPORT_WATER); truelight@0: return (byte) ((r | r >> 8)) & _ship_sometracks[dir]; truelight@0: } truelight@0: truelight@0: static const byte _ship_subcoord[4][6][3] = { truelight@0: { truelight@0: {15, 8, 1}, truelight@0: { 0, 0, 0}, truelight@0: { 0, 0, 0}, truelight@0: {15, 8, 2}, truelight@0: {15, 7, 0}, truelight@0: { 0, 0, 0}, truelight@0: }, truelight@0: { truelight@0: { 0, 0, 0}, truelight@0: { 8, 0, 3}, truelight@0: { 7, 0, 2}, truelight@0: { 0, 0, 0}, truelight@0: { 8, 0, 4}, truelight@0: { 0, 0, 0}, truelight@0: }, truelight@0: { truelight@0: { 0, 8, 5}, truelight@0: { 0, 0, 0}, truelight@0: { 0, 7, 6}, truelight@0: { 0, 0, 0}, truelight@0: { 0, 0, 0}, truelight@0: { 0, 8, 4}, truelight@0: }, truelight@0: { truelight@0: { 0, 0, 0}, truelight@0: { 8,15, 7}, truelight@0: { 0, 0, 0}, truelight@0: { 8,15, 6}, truelight@0: { 0, 0, 0}, truelight@0: { 7,15, 0}, truelight@0: } truelight@0: }; truelight@0: truelight@0: static void ShipController(Vehicle *v) truelight@0: { truelight@0: GetNewVehiclePosResult gp; truelight@0: uint32 r; truelight@0: const byte *b; truelight@0: int dir,track,tracks; truelight@0: truelight@0: v->tick_counter++; truelight@0: truelight@0: if (v->breakdown_ctr != 0) { truelight@0: if (v->breakdown_ctr <= 2) { truelight@0: HandleBrokenShip(v); truelight@0: return; truelight@0: } truelight@0: v->breakdown_ctr--; truelight@0: } truelight@0: tron@2639: if (v->vehstatus & VS_STOPPED) return; truelight@0: truelight@0: ProcessShipOrder(v); truelight@0: HandleShipLoading(v); truelight@0: tron@2639: if (v->current_order.type == OT_LOADING) return; truelight@0: truelight@0: CheckShipLeaveDepot(v); truelight@0: tron@2639: if (!ShipAccelerate(v)) return; truelight@0: truelight@0: BeginVehicleMove(v); truelight@0: truelight@0: if (GetNewVehiclePos(v, &gp)) { truelight@0: // staying in tile truelight@0: if (v->u.ship.state == 0x80) { truelight@0: gp.x = v->x_pos; truelight@0: gp.y = v->y_pos; truelight@0: } else { truelight@0: /* isnot inside depot */ truelight@0: r = VehicleEnterTile(v, gp.new_tile, gp.x, gp.y); truelight@0: if (r & 0x8) goto reverse_direction; truelight@0: matthijs@1751: /* A leave station order only needs one tick to get processed, so we can matthijs@1751: * always skip ahead. */ tron@555: if (v->current_order.type == OT_LEAVESTATION) { tron@555: v->current_order.type = OT_NOTHING; tron@555: v->current_order.flags = 0; darkvater@755: InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); matthijs@1751: } else if (v->dest_tile != 0) { matthijs@1751: /* We have a target, let's see if we reached it... */ matthijs@1751: if (v->current_order.type == OT_GOTO_STATION && matthijs@1751: IsBuoyTile(v->dest_tile) && matthijs@1751: DistanceManhattan(v->dest_tile, gp.new_tile) <= 3) { matthijs@1751: /* We got within 3 tiles of our target buoy, so let's skip to our matthijs@1751: * next order */ matthijs@1751: v->cur_order_index++; matthijs@1751: v->current_order.type = OT_DUMMY; matthijs@1751: InvalidateVehicleOrder(v); matthijs@1751: } else { matthijs@1751: /* Non-buoy orders really need to reach the tile */ matthijs@1751: if (v->dest_tile == gp.new_tile) { matthijs@1751: if (v->current_order.type == OT_GOTO_DEPOT) { matthijs@1751: if ((gp.x&0xF)==8 && (gp.y&0xF)==8) { matthijs@1751: ShipEnterDepot(v); matthijs@1751: return; matthijs@1751: } matthijs@1751: } else if (v->current_order.type == OT_GOTO_STATION) { matthijs@1751: Station *st; matthijs@1751: matthijs@1751: v->last_station_visited = v->current_order.station; matthijs@1751: matthijs@1751: /* Process station in the orderlist. */ matthijs@1751: st = GetStation(v->current_order.station); matthijs@1751: if (st->facilities & FACIL_DOCK) { /* ugly, ugly workaround for problem with ships able to drop off cargo at wrong stations */ matthijs@1751: v->current_order.type = OT_LOADING; matthijs@1751: v->current_order.flags &= OF_FULL_LOAD | OF_UNLOAD; matthijs@1751: v->current_order.flags |= OF_NON_STOP; matthijs@1751: ShipArrivesAt(v, st); matthijs@1751: matthijs@1751: SET_EXPENSES_TYPE(EXPENSES_SHIP_INC); matthijs@1751: if (LoadUnloadVehicle(v)) { matthijs@1751: InvalidateWindow(WC_SHIPS_LIST, v->owner); matthijs@1751: MarkShipDirty(v); matthijs@1751: } matthijs@1751: InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); matthijs@1751: } else { /* leave stations without docks right aways */ matthijs@1751: v->current_order.type = OT_LEAVESTATION; matthijs@1751: v->current_order.flags = 0; matthijs@1751: v->cur_order_index++; matthijs@1751: InvalidateVehicleOrder(v); matthijs@1751: } matthijs@1751: } matthijs@1751: } matthijs@1751: } truelight@0: } truelight@0: } truelight@0: } else { truelight@0: // new tile Darkvater@1401: if (TileX(gp.new_tile) >= MapMaxX() || TileY(gp.new_tile) >= MapMaxY()) tron@926: goto reverse_direction; truelight@0: truelight@0: dir = ShipGetNewDirectionFromTiles(gp.new_tile, gp.old_tile); truelight@0: assert(dir == 1 || dir == 3 || dir == 5 || dir == 7); truelight@0: dir>>=1; truelight@0: tracks = GetAvailShipTracks(gp.new_tile, dir); truelight@0: if (tracks == 0) truelight@0: goto reverse_direction; truelight@0: truelight@0: // Choose a direction, and continue if we find one truelight@0: track = ChooseShipTrack(v, gp.new_tile, dir, tracks); truelight@0: if (track < 0) truelight@0: goto reverse_direction; truelight@0: truelight@0: b = _ship_subcoord[dir][track]; truelight@193: truelight@0: gp.x = (gp.x&~0xF) | b[0]; truelight@0: gp.y = (gp.y&~0xF) | b[1]; truelight@0: truelight@0: /* Call the landscape function and tell it that the vehicle entered the tile */ truelight@0: r = VehicleEnterTile(v, gp.new_tile, gp.x, gp.y); truelight@0: if (r&0x8) goto reverse_direction; truelight@0: truelight@0: if (!(r&0x4)) { truelight@0: v->tile = gp.new_tile; truelight@0: v->u.ship.state = 1 << track; truelight@0: } truelight@0: truelight@0: v->direction = b[2]; truelight@0: } truelight@0: truelight@0: /* update image of ship, as well as delta XY */ truelight@0: dir = ShipGetNewDirection(v, gp.x, gp.y); truelight@0: v->x_pos = gp.x; truelight@0: v->y_pos = gp.y; truelight@0: v->z_pos = GetSlopeZ(gp.x, gp.y); truelight@0: truelight@0: getout: truelight@0: UpdateShipDeltaXY(v, dir); truelight@0: v->cur_image = GetShipImage(v, dir); truelight@0: VehiclePositionChanged(v); truelight@0: EndVehicleMove(v); truelight@0: return; truelight@0: truelight@0: reverse_direction: truelight@0: dir = v->direction ^ 4; truelight@0: v->direction = dir; truelight@0: goto getout; truelight@0: } truelight@0: truelight@0: static void AgeShipCargo(Vehicle *v) truelight@0: { tron@2639: if (_age_cargo_skip_counter != 0) return; tron@2639: if (v->cargo_days != 255) v->cargo_days++; truelight@0: } truelight@0: truelight@0: void Ship_Tick(Vehicle *v) truelight@0: { truelight@0: AgeShipCargo(v); truelight@0: ShipController(v); truelight@0: } truelight@0: truelight@0: tron@1093: void ShipsYearlyLoop(void) truelight@0: { truelight@0: Vehicle *v; truelight@0: truelight@0: FOR_ALL_VEHICLES(v) { truelight@0: if (v->type == VEH_Ship) { 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: Darkvater@1793: /** Build a ship. Darkvater@1793: * @param x,y tile coordinates of depot where ship is built Darkvater@1793: * @param p1 ship type being built (engine) Darkvater@1793: * @param p2 unused Darkvater@1793: */ truelight@0: int32 CmdBuildShip(int x, int y, uint32 flags, uint32 p1, uint32 p2) truelight@0: { truelight@0: int32 value; truelight@0: Vehicle *v; truelight@1282: UnitID unit_num; tron@1980: TileIndex tile = TileVirtXY(x, y); truelight@0: Engine *e; truelight@193: bjarni@2854: if (!IsEngineBuildable(p1, VEH_Ship)) return_cmd_error(STR_ENGINE_NOT_BUILDABLE); bjarni@1196: truelight@0: SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES); truelight@0: truelight@0: value = EstimateShipCost(p1); Darkvater@1793: if (flags & DC_QUERY_COST) return value; truelight@0: pasky@1443: /* The ai_new queries the vehicle cost before building the route, pasky@1443: * so we must check against cheaters no sooner than now. --pasky */ pasky@1443: if (!IsTileDepotType(tile, TRANSPORT_WATER)) return CMD_ERROR; tron@1901: if (!IsTileOwner(tile, _current_player)) return CMD_ERROR; pasky@1443: truelight@0: v = AllocateVehicle(); truelight@1024: if (v == NULL || IsOrderPoolFull() || truelight@0: (unit_num = GetFreeUnitNumber(VEH_Ship)) > _patches.max_ships) truelight@0: return_cmd_error(STR_00E1_TOO_MANY_VEHICLES_IN_GAME); truelight@193: truelight@0: if (flags & DC_EXEC) { tron@538: const ShipVehicleInfo *svi = ShipVehInfo(p1); tron@538: truelight@0: v->unitnumber = unit_num; truelight@0: truelight@0: v->owner = _current_player; truelight@0: v->tile = tile; tron@926: x = TileX(tile) * 16 + 8; tron@926: y = TileY(tile) * 16 + 8; truelight@0: v->x_pos = x; truelight@0: v->y_pos = y; truelight@0: v->z_pos = GetSlopeZ(x,y); truelight@0: truelight@0: v->z_height = 6; truelight@0: v->sprite_width = 6; truelight@0: v->sprite_height = 6; truelight@0: v->x_offs = -3; truelight@0: v->y_offs = -3; truelight@0: v->vehstatus = VS_HIDDEN | VS_STOPPED | VS_DEFPAL; truelight@193: tron@538: v->spritenum = svi->image_index; tron@538: v->cargo_type = svi->cargo_type; tron@538: v->cargo_cap = svi->capacity; truelight@0: v->value = value; truelight@193: truelight@1266: v->last_station_visited = INVALID_STATION; tron@538: v->max_speed = svi->max_speed; tron@2477: v->engine_type = p1; 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: _new_ship_id = v->index; bjarni@2564: _new_vehicle_id = v->index; truelight@0: truelight@0: v->string_id = STR_SV_SHIP_NAME; truelight@0: v->u.ship.state = 0x80; truelight@0: truelight@0: v->service_interval = _patches.servint_ships; truelight@0: v->date_of_last_service = _date; truelight@0: v->build_year = _cur_year; truelight@0: v->cur_image = 0x0E5E; truelight@0: v->type = VEH_Ship; peter1138@2804: v->random_bits = VehicleRandomBits(); truelight@0: truelight@0: VehiclePositionChanged(v); 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_Ship); // updates the replace Ship window truelight@0: } truelight@193: truelight@0: return value; truelight@0: } truelight@0: Darkvater@1793: /** Sell a ship. Darkvater@1793: * @param x,y unused Darkvater@1793: * @param p1 vehicle ID to be sold Darkvater@1793: * @param p2 unused Darkvater@1793: */ truelight@0: int32 CmdSellShip(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@193: truelight@919: v = GetVehicle(p1); truelight@0: Darkvater@1793: if (v->type != VEH_Ship || !CheckOwnership(v->owner)) return CMD_ERROR; truelight@0: bjarni@1237: SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES); bjarni@1237: matthijs@1330: if (!IsTileDepotType(v->tile, TRANSPORT_WATER) || v->u.road.state != 0x80 || !(v->vehstatus&VS_STOPPED)) truelight@0: return_cmd_error(STR_980B_SHIP_MUST_BE_STOPPED_IN); truelight@193: truelight@0: if (flags & DC_EXEC) { truelight@0: InvalidateWindow(WC_VEHICLE_DEPOT, v->tile); tron@588: RebuildVehicleLists(); truelight@0: InvalidateWindow(WC_COMPANY, v->owner); truelight@0: DeleteWindowById(WC_VEHICLE_VIEW, v->index); truelight@0: DeleteVehicle(v); bjarni@2618: if (IsLocalPlayer()) bjarni@2618: InvalidateWindow(WC_REPLACE_VEHICLE, VEH_Ship); // updates the replace Ship window truelight@0: } truelight@193: truelight@0: return -(int32)v->value; truelight@0: } truelight@0: Darkvater@1793: /** Start/Stop a ship. Darkvater@1793: * @param x,y unused Darkvater@1793: * @param p1 ship ID to start/stop Darkvater@1793: * @param p2 unused Darkvater@1793: */ truelight@0: int32 CmdStartStopShip(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_Ship || !CheckOwnership(v->owner)) return CMD_ERROR; 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); Darkvater@1793: InvalidateWindowClasses(WC_SHIPS_LIST); truelight@0: } truelight@0: truelight@0: return 0; truelight@0: } truelight@0: Darkvater@1793: /** Send a ship to the depot. Darkvater@1793: * @param x,y unused Darkvater@1793: * @param p1 vehicle ID to send to the depot Darkvater@1793: * @param p2 unused Darkvater@1793: */ truelight@0: int32 CmdSendShipToDepot(int x, int y, uint32 flags, uint32 p1, uint32 p2) truelight@0: { truelight@0: Vehicle *v; Darkvater@1793: const Depot *dep; 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_Ship || !CheckOwnership(v->owner)) return CMD_ERROR; truelight@0: Darkvater@1793: if (v->vehstatus & VS_CRASHED) return CMD_ERROR; matthijs@1757: Darkvater@1793: /* If the current orders are already goto-depot */ tron@555: if (v->current_order.type == OT_GOTO_DEPOT) { truelight@0: if (flags & DC_EXEC) { Darkvater@1793: /* If the orders to 'goto depot' are in the orders list (forced servicing), Darkvater@1793: * then skip to the next order; effectively cancelling this forced service */ celestar@1530: if (HASBIT(v->current_order.flags, OFB_PART_OF_ORDERS)) celestar@1530: v->cur_order_index++; celestar@1530: 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: } Darkvater@1793: return 0; Darkvater@1793: } truelight@0: Darkvater@1793: dep = FindClosestShipDepot(v); Darkvater@1793: if (dep == NULL) Darkvater@1793: return_cmd_error(STR_981A_UNABLE_TO_FIND_LOCAL_DEPOT); Darkvater@1793: Darkvater@1793: if (flags & DC_EXEC) { Darkvater@1793: v->dest_tile = dep->xy; Darkvater@1793: v->current_order.type = OT_GOTO_DEPOT; Darkvater@1793: v->current_order.flags = OF_NON_STOP | OF_HALT_IN_DEPOT; Darkvater@1793: v->current_order.station = dep->index; Darkvater@1793: InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); truelight@0: } truelight@0: truelight@0: return 0; truelight@0: } truelight@0: truelight@0: Darkvater@1802: /** Refits a ship to the specified cargo type. Darkvater@1802: * @param x,y unused Darkvater@1802: * @param p1 vehicle ID of the ship to refit Darkvater@1802: * @param p2 various bitstuffed elements Darkvater@1802: * - p2 = (bit 0-7) - the new cargo type to refit to (p2 & 0xFF) Darkvater@1802: */ truelight@0: int32 CmdRefitShip(int x, int y, uint32 flags, uint32 p1, uint32 p2) truelight@0: { truelight@0: Vehicle *v; truelight@0: int32 cost; Darkvater@1802: CargoID new_cid = p2 & 0xFF; //gets the cargo number bjarni@1237: bjarni@1237: if (!IsVehicleIndex(p1)) return CMD_ERROR; truelight@193: truelight@919: v = GetVehicle(p1); bjarni@1237: Darkvater@1802: if (v->type != VEH_Ship || !CheckOwnership(v->owner)) return CMD_ERROR; bjarni@2567: bjarni@2567: if (!IsTileDepotType(v->tile, TRANSPORT_WATER) || !(v->vehstatus&VS_STOPPED) || v->u.ship.state != 0x80) Darkvater@1802: return_cmd_error(STR_980B_SHIP_MUST_BE_STOPPED_IN); bjarni@2567: truelight@0: Darkvater@1802: /* Check cargo */ Darkvater@1802: if (!ShipVehInfo(v->engine_type)->refittable) return CMD_ERROR; peter1138@2704: if (new_cid > NUM_CARGO || !CanRefitTo(v->engine_type, new_cid)) return CMD_ERROR; tron@915: bjarni@1237: SET_EXPENSES_TYPE(EXPENSES_SHIP_RUN); bjarni@1237: truelight@0: cost = 0; Darkvater@1802: if (IS_HUMAN_PLAYER(v->owner) && new_cid != v->cargo_type) { truelight@0: cost = _price.ship_base >> 7; truelight@0: } truelight@0: truelight@0: if (flags & DC_EXEC) { bjarni@2568: v->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: }