static bool CheckShipLeaveDepot(Ship *v) { if (!v->IsChainInDepot()) return false; /* We are leaving a depot, but have to go to the exact same one; re-enter */ if (v->current_order.IsType(OT_GOTO_DEPOT) && IsShipDepotTile(v->tile) && GetDepotIndex(v->tile) == v->current_order.GetDestination()) { VehicleEnterDepot(v); return true; } TileIndex tile = v->tile; Axis axis = GetShipDepotAxis(tile); DiagDirection north_dir = ReverseDiagDir(AxisToDiagDir(axis)); TileIndex north_neighbour = TILE_ADD(tile, TileOffsByDiagDir(north_dir)); DiagDirection south_dir = AxisToDiagDir(axis); TileIndex south_neighbour = TILE_ADD(tile, 2 * TileOffsByDiagDir(south_dir)); TrackBits north_tracks = DiagdirReachesTracks(north_dir) & GetTileShipTrackStatus(north_neighbour); TrackBits south_tracks = DiagdirReachesTracks(south_dir) & GetTileShipTrackStatus(south_neighbour); if (north_tracks && south_tracks) { /* Ask pathfinder for best direction */ bool reverse = false; bool path_found; switch (_settings_game.pf.pathfinder_for_ships) { case VPF_OPF: reverse = OPFShipChooseTrack(v, north_neighbour, north_dir, north_tracks, path_found) == INVALID_TRACK; break; // OPF always allows reversing case VPF_NPF: reverse = NPFShipCheckReverse(v); break; case VPF_YAPF: reverse = YapfShipCheckReverse(v); break; default: NOT_REACHED(); } if (reverse) north_tracks = TRACK_BIT_NONE; } if (north_tracks) { /* Leave towards north */ v->direction = DiagDirToDir(north_dir); } else if (south_tracks) { /* Leave towards south */ v->direction = DiagDirToDir(south_dir); } else { /* Both ways blocked */ return false; } v->state = AxisToTrackBits(axis); v->vehstatus &= ~VS_HIDDEN; v->cur_speed = 0; v->UpdateViewport(true, true); SetWindowDirty(WC_VEHICLE_DEPOT, v->tile); PlayShipSound(v); VehicleServiceInDepot(v); InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile); SetWindowClassesDirty(WC_SHIPS_LIST); return false; }
ETileArea(const BaseStation *st, TileIndex tile, TriggerArea ta) { switch (ta) { default: NOT_REACHED(); case TA_TILE: this->tile = tile; this->w = 1; this->h = 1; break; case TA_PLATFORM: { TileIndex start, end; Axis axis = GetRailStationAxis(tile); TileIndexDiff delta = TileOffsByDiagDir(AxisToDiagDir(axis)); for (end = tile; IsRailStationTile(end + delta) && IsCompatibleTrainStationTile(end + delta, tile); end += delta) { /* Nothing */ } for (start = tile; IsRailStationTile(start - delta) && IsCompatibleTrainStationTile(start - delta, tile); start -= delta) { /* Nothing */ } this->tile = start; this->w = TileX(end) - TileX(start) + 1; this->h = TileY(end) - TileY(start) + 1; break; } case TA_WHOLE: st->GetTileArea(this, Station::IsExpected(st) ? STATION_RAIL : STATION_WAYPOINT); break; } }
/** * Convert existing rail to waypoint. Eg build a waypoint station over * piece of rail * @param start_tile northern most tile where waypoint will be built * @param flags type of operation * @param p1 various bitstuffed elements * - p1 = (bit 4) - orientation (Axis) * - p1 = (bit 8-15) - width of waypoint * - p1 = (bit 16-23) - height of waypoint * - p1 = (bit 24) - allow waypoints directly adjacent to other waypoints. * @param p2 various bitstuffed elements * - p2 = (bit 0- 7) - custom station class * - p2 = (bit 8-15) - custom station id * @param text unused * @return the cost of this operation or an error */ CommandCost CmdBuildRailWaypoint(TileIndex start_tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text) { /* Unpack parameters */ Axis axis = Extract<Axis, 4, 1>(p1); byte width = GB(p1, 8, 8); byte height = GB(p1, 16, 8); bool adjacent = HasBit(p1, 24); StationClassID spec_class = Extract<StationClassID, 0, 8>(p2); byte spec_index = GB(p2, 8, 8); StationID station_to_join = GB(p2, 16, 16); /* Check if the given station class is valid */ if (spec_class != STAT_CLASS_WAYP) return CMD_ERROR; if (spec_index >= StationClass::Get(spec_class)->GetSpecCount()) return CMD_ERROR; /* The number of parts to build */ byte count = axis == AXIS_X ? height : width; if ((axis == AXIS_X ? width : height) != 1) return CMD_ERROR; if (count == 0 || count > _settings_game.station.station_spread) return CMD_ERROR; bool reuse = (station_to_join != NEW_STATION); if (!reuse) station_to_join = INVALID_STATION; bool distant_join = (station_to_join != INVALID_STATION); if (distant_join && (!_settings_game.station.distant_join_stations || !Waypoint::IsValidID(station_to_join))) return CMD_ERROR; /* Make sure the area below consists of clear tiles. (OR tiles belonging to a certain rail station) */ StationID est = INVALID_STATION; /* Check whether the tiles we're building on are valid rail or not. */ TileIndexDiff offset = TileOffsByDiagDir(AxisToDiagDir(OtherAxis(axis))); for (int i = 0; i < count; i++) { TileIndex tile = start_tile + i * offset; CommandCost ret = IsValidTileForWaypoint(tile, axis, &est); if (ret.Failed()) return ret; } Waypoint *wp = NULL; TileArea new_location(TileArea(start_tile, width, height)); CommandCost ret = FindJoiningWaypoint(est, station_to_join, adjacent, new_location, &wp); if (ret.Failed()) return ret; /* Check if there is an already existing, deleted, waypoint close to us that we can reuse. */ TileIndex center_tile = start_tile + (count / 2) * offset; if (wp == NULL && reuse) wp = FindDeletedWaypointCloseTo(center_tile, STR_SV_STNAME_WAYPOINT, _current_company); if (wp != NULL) { /* Reuse an existing waypoint. */ if (wp->owner != _current_company) return_cmd_error(STR_ERROR_TOO_CLOSE_TO_ANOTHER_WAYPOINT); /* check if we want to expand an already existing waypoint? */ if (wp->train_station.tile != INVALID_TILE) { CommandCost ret = CanExpandRailStation(wp, new_location, axis); if (ret.Failed()) return ret; } CommandCost ret = wp->rect.BeforeAddRect(start_tile, width, height, StationRect::ADD_TEST); if (ret.Failed()) return ret; } else { /* allocate and initialize new waypoint */ if (!Waypoint::CanAllocateItem()) return_cmd_error(STR_ERROR_TOO_MANY_STATIONS_LOADING); } if (flags & DC_EXEC) { if (wp == NULL) { wp = new Waypoint(start_tile); } else if (!wp->IsInUse()) { /* Move existing (recently deleted) waypoint to the new location */ wp->xy = start_tile; } wp->owner = GetTileOwner(start_tile); wp->rect.BeforeAddRect(start_tile, width, height, StationRect::ADD_TRY); wp->delete_ctr = 0; wp->facilities |= FACIL_TRAIN; wp->build_date = _date; wp->string_id = STR_SV_STNAME_WAYPOINT; wp->train_station = new_location; if (wp->town == NULL) MakeDefaultName(wp); wp->UpdateVirtCoord(); const StationSpec *spec = StationClass::Get(spec_class)->GetSpec(spec_index); byte *layout_ptr = AllocaM(byte, count); if (spec == NULL) { /* The layout must be 0 for the 'normal' waypoints by design. */ memset(layout_ptr, 0, count); } else { /* But for NewGRF waypoints we like to have their style. */ GetStationLayout(layout_ptr, count, 1, spec); } byte map_spec_index = AllocateSpecToStation(spec, wp, true); Company *c = Company::Get(wp->owner); for (int i = 0; i < count; i++) { TileIndex tile = start_tile + i * offset; byte old_specindex = HasStationTileRail(tile) ? GetCustomStationSpecIndex(tile) : 0; if (!HasStationTileRail(tile)) c->infrastructure.station++; bool reserved = IsTileType(tile, MP_RAILWAY) ? HasBit(GetRailReservationTrackBits(tile), AxisToTrack(axis)) : HasStationReservation(tile); MakeRailWaypoint(tile, wp->owner, wp->index, axis, layout_ptr[i], GetRailType(tile)); SetCustomStationSpecIndex(tile, map_spec_index); SetRailStationReservation(tile, reserved); MarkTileDirtyByTile(tile); DeallocateSpecFromStation(wp, old_specindex); YapfNotifyTrackLayoutChange(tile, AxisToTrack(axis)); } DirtyCompanyInfrastructureWindows(wp->owner); } return CommandCost(EXPENSES_CONSTRUCTION, count * _price[PR_BUILD_WAYPOINT_RAIL]); }
/** * Finds the southern end of a bridge starting at a middle tile * @param t the bridge tile to find the bridge ramp for */ TileIndex GetSouthernBridgeEnd(TileIndex t) { return GetBridgeEnd(t, AxisToDiagDir(GetBridgeAxis(t))); }
/** * Finds the northern end of a bridge starting at a middle tile * @param t the bridge tile to find the bridge ramp for */ TileIndex GetNorthernBridgeEnd(TileIndex t) { return GetBridgeEnd(t, ReverseDiagDir(AxisToDiagDir(GetBridgeAxis(t)))); }