tron@2186: /* $Id$ */ tron@2186: belugas@6928: /** @file waypoint.cpp */ belugas@6928: truelight@1542: #include "stdafx.h" Darkvater@1891: #include "openttd.h" truelight@1542: rubidium@8612: #include "command_func.h" maedhros@6949: #include "landscape.h" truelight@1542: #include "order.h" tron@3101: #include "rail_map.h" rubidium@8634: #include "rail.h" celestar@5573: #include "bridge_map.h" truelight@1542: #include "saveload.h" truelight@1542: #include "station.h" truelight@1542: #include "town.h" truelight@1542: #include "waypoint.h" tron@2159: #include "variables.h" KUDr@3900: #include "yapf/yapf.h" peter1138@6947: #include "newgrf.h" rubidium@8610: #include "strings_func.h" rubidium@8721: #include "gfx_func.h" rubidium@8627: #include "functions.h" rubidium@8627: #include "window_func.h" rubidium@8634: #include "economy_func.h" rubidium@8636: #include "date_func.h" rubidium@8640: #include "vehicle_func.h" rubidium@8640: #include "vehicle_base.h" rubidium@8710: #include "string_func.h" smatz@8734: #include "signal_func.h" rubidium@8750: #include "player_func.h" rubidium@8766: #include "settings_type.h" truelight@1542: rubidium@8760: #include "table/strings.h" smatz@8734: rubidium@7877: DEFINE_OLD_POOL_GENERIC(Waypoint, Waypoint) truelight@1542: truelight@1542: belugas@6928: /** belugas@6928: * Update the sign for the waypoint belugas@6928: * @param wp Waypoint to update sign */ tron@2817: static void UpdateWaypointSign(Waypoint* wp) truelight@1542: { celestar@3422: Point pt = RemapCoords2(TileX(wp->xy) * TILE_SIZE, TileY(wp->xy) * TILE_SIZE); truelight@1542: SetDParam(0, wp->index); truelight@1542: UpdateViewportSignPos(&wp->sign, pt.x, pt.y - 0x20, STR_WAYPOINT_VIEWPORT); truelight@1542: } truelight@1542: belugas@6928: /** belugas@6928: * Redraw the sign of a waypoint belugas@6928: * @param wp Waypoint to redraw sign */ tron@2752: static void RedrawWaypointSign(const Waypoint* wp) truelight@1542: { truelight@1542: MarkAllViewportsDirty( truelight@1542: wp->sign.left - 6, truelight@1542: wp->sign.top, truelight@1542: wp->sign.left + (wp->sign.width_1 << 2) + 12, truelight@1542: wp->sign.top + 48); truelight@1542: } truelight@1542: belugas@6928: /** belugas@6928: * Update all signs belugas@6928: */ rubidium@6573: void UpdateAllWaypointSigns() truelight@1542: { truelight@1542: Waypoint *wp; truelight@1542: truelight@1542: FOR_ALL_WAYPOINTS(wp) { truelight@4346: UpdateWaypointSign(wp); truelight@1542: } truelight@1542: } truelight@1542: belugas@6928: /** belugas@6928: * Set the default name for a waypoint belugas@6928: * @param wp Waypoint to work on belugas@6928: */ tron@2752: static void MakeDefaultWaypointName(Waypoint* wp) truelight@1542: { smatz@9075: uint32 used = 0; // bitmap of used waypoint numbers, sliding window with 'next' as base smatz@9075: uint32 next = 0; // first waypoint number in the bitmap smatz@9075: WaypointID idx = 0; // index where we will stop truelight@1542: truelight@1542: wp->town_index = ClosestTownFromTile(wp->xy, (uint)-1)->index; truelight@1542: smatz@9075: /* Find first unused waypoint number belonging to this town. This can never fail, smatz@9075: * as long as there can be at most 65535 waypoints in total. smatz@9075: * smatz@9075: * This does 'n * m' search, but with 32bit 'used' bitmap, it needs at most 'n * (1 + ceil(m / 32))' smatz@9075: * steps (n - number of waypoints in pool, m - number of waypoints near this town). smatz@9075: * Usually, it needs only 'n' steps. smatz@9075: * smatz@9075: * If it wasn't using 'used' and 'idx', it would just search for increasing 'next', smatz@9075: * but this way it is faster */ truelight@1542: smatz@9075: WaypointID cid = 0; // current index, goes to GetWaypointPoolSize()-1, then wraps to 0 smatz@9075: do { smatz@9075: Waypoint *lwp = GetWaypoint(cid); truelight@1542: smatz@9075: /* check only valid waypoints... */ smatz@9075: if (lwp->IsValid() && wp != lwp) { smatz@9075: /* only waypoints with 'generic' name within the same city */ smatz@9075: if (lwp->name == NULL && lwp->town_index == wp->town_index) { smatz@9075: /* if lwp->town_cn < next, uint will overflow to '+inf' */ smatz@9075: uint i = (uint)lwp->town_cn - next; truelight@1542: smatz@9075: if (i < 32) { smatz@9075: SetBit(used, i); // update bitmap smatz@9075: if (i == 0) { smatz@9075: /* shift bitmap while the lowest bit is '1'; smatz@9075: * increase the base of the bitmap too */ smatz@9075: do { smatz@9075: used >>= 1; smatz@9075: next++; smatz@9075: } while (HasBit(used, 0)); smatz@9075: /* when we are at 'idx' again at end of the loop and smatz@9075: * 'next' hasn't changed, then no waypoint had town_cn == next, smatz@9075: * so we can safely use it */ smatz@9075: idx = cid; smatz@9075: } smatz@9075: } smatz@9075: } smatz@9075: } smatz@9075: smatz@9075: cid++; smatz@9075: if (cid == GetWaypointPoolSize()) cid = 0; // wrap to zero... smatz@9075: } while (cid != idx); smatz@9075: smatz@9075: wp->town_cn = (uint16)next; // set index... smatz@9075: wp->name = NULL; // ... and use generic name truelight@1542: } truelight@1542: belugas@6928: /** belugas@6928: * Find a deleted waypoint close to a tile. belugas@6928: * @param tile to search from belugas@6928: */ tron@1977: static Waypoint *FindDeletedWaypointCloseTo(TileIndex tile) truelight@1542: { truelight@1542: Waypoint *wp, *best = NULL; tron@4077: uint thres = 8; truelight@1542: truelight@1542: FOR_ALL_WAYPOINTS(wp) { truelight@4346: if (wp->deleted) { tron@4077: uint cur_dist = DistanceManhattan(tile, wp->xy); tron@4077: truelight@1542: if (cur_dist < thres) { truelight@1542: thres = cur_dist; truelight@1542: best = wp; truelight@1542: } truelight@1542: } truelight@1542: } truelight@1542: truelight@1542: return best; truelight@1542: } truelight@1542: peter1138@2670: /** peter1138@2670: * Update waypoint graphics id against saved GRFID/localidx. peter1138@2670: * This is to ensure the chosen graphics are correct if GRF files are changed. peter1138@2670: */ rubidium@6573: void AfterLoadWaypoints() peter1138@2670: { peter1138@2670: Waypoint *wp; peter1138@2670: peter1138@2670: FOR_ALL_WAYPOINTS(wp) { peter1138@2670: uint i; peter1138@2670: peter1138@2670: if (wp->grfid == 0) continue; peter1138@2670: peter1138@2670: for (i = 0; i < GetNumCustomStations(STAT_CLASS_WAYP); i++) { belugas@3676: const StationSpec *statspec = GetCustomStationSpec(STAT_CLASS_WAYP, i); peter1138@6947: if (statspec != NULL && statspec->grffile->grfid == wp->grfid && statspec->localidx == wp->localidx) { peter1138@2670: wp->stat_id = i; peter1138@2670: break; peter1138@2670: } peter1138@2670: } peter1138@2670: } peter1138@2670: } peter1138@2670: Darkvater@1782: /** Convert existing rail to waypoint. Eg build a waypoint station over Darkvater@1782: * piece of rail tron@3491: * @param tile tile where waypoint will be built belugas@6928: * @param flags type of operation peter1138@2625: * @param p1 graphics for waypoint type, 0 indicates standard graphics Darkvater@1782: * @param p2 unused celestar@2085: * celestar@2085: * @todo When checking for the tile slope, celestar@2085: * distingush between "Flat land required" and "land sloped in wrong direction" Darkvater@1782: */ rubidium@7439: CommandCost CmdBuildTrainWaypoint(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) truelight@1542: { truelight@1542: Waypoint *wp; tron@3636: Slope tileh; tron@3101: Axis axis; truelight@1542: Darkvater@1782: /* if custom gfx are used, make sure it is within bounds */ peter1138@2625: if (p1 >= GetNumCustomStations(STAT_CLASS_WAYP)) return CMD_ERROR; Darkvater@1782: tron@3267: if (!IsTileType(tile, MP_RAILWAY) || rubidium@3792: GetRailTileType(tile) != RAIL_TILE_NORMAL || ( tron@3267: (axis = AXIS_X, GetTrackBits(tile) != TRACK_BIT_X) && tron@3267: (axis = AXIS_Y, GetTrackBits(tile) != TRACK_BIT_Y) tron@3101: )) { truelight@1542: return_cmd_error(STR_1005_NO_SUITABLE_RAILROAD_TRACK); tron@3101: } truelight@1542: tron@4077: if (!CheckTileOwnership(tile)) return CMD_ERROR; rubidium@8254: if (!EnsureNoVehicleOnGround(tile)) return CMD_ERROR; truelight@1542: truelight@1542: tileh = GetTileSlope(tile, NULL); tron@4077: if (tileh != SLOPE_FLAT && tron@4077: (!_patches.build_on_slopes || IsSteepSlope(tileh) || !(tileh & (0x3 << axis)) || !(tileh & ~(0x3 << axis)))) { tron@4077: return_cmd_error(STR_0007_FLAT_LAND_REQUIRED); truelight@1542: } truelight@1542: celestar@5573: if (MayHaveBridgeAbove(tile) && IsBridgeAbove(tile)) return_cmd_error(STR_5007_MUST_DEMOLISH_BRIDGE_FIRST); celestar@5573: truelight@1542: /* Check if there is an already existing, deleted, waypoint close to us that we can reuse. */ truelight@1542: wp = FindDeletedWaypointCloseTo(tile); rubidium@10323: if (wp == NULL && !Waypoint::CanAllocateItem()) return CMD_ERROR; rubidium@7877: rubidium@10323: if (flags & DC_EXEC) { rubidium@10323: if (wp == NULL) { rubidium@10323: wp = new Waypoint(tile); rubidium@8200: rubidium@10323: wp->town_index = INVALID_TOWN; rubidium@10323: wp->name = NULL; rubidium@10323: wp->town_cn = 0; rubidium@10323: } else { rubidium@10323: /* Move existing (recently deleted) waypoint to the new location */ rubidium@10323: rubidium@10323: /* First we update the destination for all vehicles that rubidium@10323: * have the old waypoint in their orders. */ rubidium@10323: Vehicle *v; rubidium@10323: FOR_ALL_VEHICLES(v) { rubidium@10323: if (v->type == VEH_TRAIN && rubidium@10323: v->First() == v && rubidium@10323: v->current_order.type == OT_GOTO_WAYPOINT && rubidium@10323: v->dest_tile == wp->xy) { rubidium@10323: v->dest_tile = tile; rubidium@10323: } rubidium@8200: } rubidium@10323: rubidium@10323: RedrawWaypointSign(wp); rubidium@10323: wp->xy = tile; rubidium@8200: } rubidium@8200: tron@4051: const StationSpec* statspec; tron@4051: tron@3242: MakeRailWaypoint(tile, GetTileOwner(tile), axis, GetRailType(tile), wp->index); tron@3101: MarkTileDirtyByTile(tile); peter1138@2670: tron@4051: statspec = GetCustomStationSpec(STAT_CLASS_WAYP, p1); peter1138@2670: belugas@3676: if (statspec != NULL) { tron@4051: wp->stat_id = p1; peter1138@6947: wp->grfid = statspec->grffile->grfid; belugas@3676: wp->localidx = statspec->localidx; peter1138@2670: } else { belugas@6928: /* Specified custom graphics do not exist, so use default. */ peter1138@2670: wp->stat_id = 0; peter1138@2670: wp->grfid = 0; peter1138@2670: wp->localidx = 0; truelight@1542: } peter1138@2670: truelight@1542: wp->deleted = 0; truelight@1542: wp->build_date = _date; truelight@1542: glx@8997: if (wp->town_index == INVALID_TOWN) MakeDefaultWaypointName(wp); truelight@1542: truelight@1542: UpdateWaypointSign(wp); truelight@1542: RedrawWaypointSign(wp); KUDr@5348: YapfNotifyTrackLayoutChange(tile, AxisToTrack(axis)); truelight@1542: } truelight@1542: rubidium@8726: return CommandCost(EXPENSES_CONSTRUCTION, _price.build_train_depot); truelight@1542: } truelight@1542: belugas@6928: /** belugas@6928: * Daily loop for waypoints belugas@6928: */ rubidium@6573: void WaypointsDailyLoop() truelight@1542: { truelight@1542: Waypoint *wp; truelight@1542: truelight@1542: /* Check if we need to delete a waypoint */ truelight@1542: FOR_ALL_WAYPOINTS(wp) { truelight@4500: if (wp->deleted != 0 && --wp->deleted == 0) DeleteWaypoint(wp); truelight@1542: } truelight@1542: } truelight@1542: belugas@6928: /** belugas@6928: * Remove a waypoint belugas@6928: * @param tile from which to remove waypoint belugas@6928: * @param flags type of operation belugas@6928: * @param justremove will indicate if it is removed from rail or if rails are removed too belugas@6928: * @return cost of operation or error belugas@6928: */ rubidium@7439: CommandCost RemoveTrainWaypoint(TileIndex tile, uint32 flags, bool justremove) truelight@1542: { truelight@1542: Waypoint *wp; truelight@1542: truelight@1542: /* Make sure it's a waypoint */ tron@4077: if (!IsTileType(tile, MP_RAILWAY) || tron@4077: !IsRailWaypoint(tile) || tron@4077: (!CheckTileOwnership(tile) && _current_player != OWNER_WATER) || rubidium@8254: !EnsureNoVehicleOnGround(tile)) { truelight@1542: return CMD_ERROR; tron@4077: } truelight@1542: truelight@1542: if (flags & DC_EXEC) { KUDr@5348: Track track = GetRailWaypointTrack(tile); smatz@8796: Owner owner = GetTileOwner(tile); // cannot use _current_player because of possible floods truelight@1542: wp = GetWaypointByTile(tile); truelight@1542: truelight@1542: wp->deleted = 30; // let it live for this many days before we do the actual deletion. truelight@1542: RedrawWaypointSign(wp); truelight@1542: truelight@1542: if (justremove) { tron@3242: MakeRailNormal(tile, GetTileOwner(tile), GetRailWaypointBits(tile), GetRailType(tile)); tron@3101: MarkTileDirtyByTile(tile); truelight@1542: } else { truelight@1542: DoClearSquare(tile); smatz@8802: AddTrackToSignalBuffer(tile, track, owner); truelight@1542: } KUDr@5348: YapfNotifyTrackLayoutChange(tile, track); truelight@1542: } truelight@1542: rubidium@8726: return CommandCost(EXPENSES_CONSTRUCTION, _price.remove_train_depot); truelight@1542: } truelight@1542: belugas@6928: /** belugas@6928: * Delete a waypoint tron@3491: * @param tile tile where waypoint is to be deleted belugas@6928: * @param flags type of operation Darkvater@1782: * @param p1 unused Darkvater@1782: * @param p2 unused belugas@6928: * @return cost of operation or error Darkvater@1782: */ rubidium@7439: CommandCost CmdRemoveTrainWaypoint(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) truelight@1542: { truelight@1542: return RemoveTrainWaypoint(tile, flags, true); truelight@1542: } truelight@1542: peter1138@7593: static bool IsUniqueWaypointName(const char *name) peter1138@7593: { peter1138@7593: const Waypoint *wp; peter1138@7593: char buf[512]; peter1138@7593: peter1138@7593: FOR_ALL_WAYPOINTS(wp) { peter1138@7593: SetDParam(0, wp->index); peter1138@7593: GetString(buf, STR_WAYPOINT_RAW, lastof(buf)); peter1138@7593: if (strcmp(buf, name) == 0) return false; peter1138@7593: } peter1138@7593: peter1138@7593: return true; peter1138@7593: } peter1138@7593: belugas@6928: /** belugas@6928: * Rename a waypoint. tron@3491: * @param tile unused belugas@6928: * @param flags type of operation Darkvater@1782: * @param p1 id of waypoint Darkvater@1782: * @param p2 unused belugas@6928: * @return cost of operation or error Darkvater@1782: */ rubidium@7439: CommandCost CmdRenameWaypoint(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) truelight@1542: { truelight@1542: Waypoint *wp; truelight@1542: truelight@4352: if (!IsValidWaypointID(p1)) return CMD_ERROR; Darkvater@1782: peter1138@7597: wp = GetWaypoint(p1); peter1138@7597: if (!CheckTileOwnership(wp->xy)) return CMD_ERROR; peter1138@7597: peter1138@7593: if (!StrEmpty(_cmd_text)) { peter1138@7593: if (!IsUniqueWaypointName(_cmd_text)) return_cmd_error(STR_NAME_MUST_BE_UNIQUE); peter1138@7593: truelight@1542: if (flags & DC_EXEC) { peter1138@8754: free(wp->name); peter1138@8754: wp->name = strdup(_cmd_text); truelight@1542: wp->town_cn = 0; truelight@1542: truelight@1542: UpdateWaypointSign(wp); truelight@1542: MarkWholeScreenDirty(); truelight@1542: } tron@2026: } else { truelight@1542: if (flags & DC_EXEC) { peter1138@8754: free(wp->name); truelight@1542: truelight@1542: MakeDefaultWaypointName(wp); truelight@1542: UpdateWaypointSign(wp); truelight@1542: MarkWholeScreenDirty(); truelight@1542: } truelight@1542: } rubidium@7446: return CommandCost(); truelight@1542: } truelight@1542: belugas@6928: /** belugas@6928: * This hacks together some dummy one-shot Station structure for a waypoint. belugas@6928: * @param tile on which to work belugas@6928: * @return pointer to a Station belugas@6928: */ tron@1977: Station *ComposeWaypointStation(TileIndex tile) truelight@1542: { truelight@1542: Waypoint *wp = GetWaypointByTile(tile); KUDr@5985: KUDr@5985: /* instead of 'static Station stat' use byte array to avoid Station's destructor call upon exit. As KUDr@5985: * a side effect, the station is not constructed now. */ KUDr@5990: static byte stat_raw[sizeof(Station)]; KUDr@5985: static Station &stat = *(Station*)stat_raw; truelight@1542: truelight@1542: stat.train_tile = stat.xy = wp->xy; truelight@1542: stat.town = GetTown(wp->town_index); truelight@1542: stat.build_date = wp->build_date; truelight@1542: truelight@1542: return &stat; truelight@1542: } truelight@1542: belugas@6928: /** belugas@6928: * Draw a waypoint belugas@6928: * @param x coordinate belugas@6928: * @param y coordinate belugas@6928: * @param stat_id station id belugas@6928: * @param railtype RailType to use for belugas@6928: */ tron@2520: void DrawWaypointSprite(int x, int y, int stat_id, RailType railtype) truelight@1542: { truelight@1542: x += 33; truelight@1542: y += 17; truelight@1542: peter1138@3764: if (!DrawStationTile(x, y, railtype, AXIS_X, STAT_CLASS_WAYP, stat_id)) { ludde@2261: DrawDefaultWaypointSprite(x, y, railtype); truelight@1542: } truelight@1542: } truelight@1542: rubidium@7877: Waypoint::Waypoint(TileIndex tile) rubidium@7877: { rubidium@7877: this->xy = tile; rubidium@7877: } rubidium@7877: rubidium@7877: Waypoint::~Waypoint() rubidium@7877: { peter1138@8754: free(this->name); rubidium@7909: rubidium@7909: if (CleaningPool()) return; rubidium@7909: rubidium@7877: RemoveOrderFromAllVehicles(OT_GOTO_WAYPOINT, this->index); rubidium@7877: rubidium@7877: RedrawWaypointSign(this); rubidium@7877: this->xy = 0; rubidium@7877: } rubidium@7877: belugas@6928: /** belugas@6928: * Fix savegames which stored waypoints in their old format belugas@6928: */ rubidium@6573: void FixOldWaypoints() truelight@1542: { truelight@1542: Waypoint *wp; truelight@1542: truelight@1542: /* Convert the old 'town_or_string', to 'string' / 'town' / 'town_cn' */ truelight@1542: FOR_ALL_WAYPOINTS(wp) { truelight@1542: wp->town_index = ClosestTownFromTile(wp->xy, (uint)-1)->index; truelight@1542: wp->town_cn = 0; truelight@1542: if (wp->string & 0xC000) { truelight@1542: wp->town_cn = wp->string & 0x3F; truelight@1542: wp->string = STR_NULL; truelight@1542: } truelight@1542: } truelight@1542: } truelight@1542: rubidium@6573: void InitializeWaypoints() truelight@1542: { rubidium@7877: _Waypoint_pool.CleanPool(); rubidium@7877: _Waypoint_pool.AddBlockToPool(); truelight@1542: } truelight@1542: Darkvater@1881: static const SaveLoad _waypoint_desc[] = { rubidium@4344: SLE_CONDVAR(Waypoint, xy, SLE_FILE_U16 | SLE_VAR_U32, 0, 5), rubidium@4344: SLE_CONDVAR(Waypoint, xy, SLE_UINT32, 6, SL_MAX_VERSION), rubidium@4344: SLE_CONDVAR(Waypoint, town_index, SLE_UINT16, 12, SL_MAX_VERSION), smatz@9075: SLE_CONDVAR(Waypoint, town_cn, SLE_FILE_U8 | SLE_VAR_U16, 12, 88), smatz@9075: SLE_CONDVAR(Waypoint, town_cn, SLE_UINT16, 89, SL_MAX_VERSION), peter1138@8754: SLE_CONDVAR(Waypoint, string, SLE_STRINGID, 0, 83), peter1138@8754: SLE_CONDSTR(Waypoint, name, SLE_STR, 0, 84, SL_MAX_VERSION), rubidium@4344: SLE_VAR(Waypoint, deleted, SLE_UINT8), truelight@1542: rubidium@4344: SLE_CONDVAR(Waypoint, build_date, SLE_FILE_U16 | SLE_VAR_I32, 3, 30), rubidium@4344: SLE_CONDVAR(Waypoint, build_date, SLE_INT32, 31, SL_MAX_VERSION), rubidium@4344: SLE_CONDVAR(Waypoint, localidx, SLE_UINT8, 3, SL_MAX_VERSION), rubidium@4344: SLE_CONDVAR(Waypoint, grfid, SLE_UINT32, 17, SL_MAX_VERSION), truelight@1542: truelight@1542: SLE_END() truelight@1542: }; truelight@1542: rubidium@6573: static void Save_WAYP() truelight@1542: { truelight@1542: Waypoint *wp; truelight@1542: truelight@1542: FOR_ALL_WAYPOINTS(wp) { truelight@4346: SlSetArrayIndex(wp->index); truelight@4346: SlObject(wp, _waypoint_desc); truelight@1542: } truelight@1542: } truelight@1542: rubidium@6573: static void Load_WAYP() truelight@1542: { truelight@1542: int index; truelight@1542: truelight@1542: while ((index = SlIterateArray()) != -1) { rubidium@7877: Waypoint *wp = new (index) Waypoint(); truelight@1542: SlObject(wp, _waypoint_desc); truelight@1542: } truelight@1542: } truelight@1542: rubidium@5838: extern const ChunkHandler _waypoint_chunk_handlers[] = { truelight@1542: { 'CHKP', Save_WAYP, Load_WAYP, CH_ARRAY | CH_LAST}, truelight@1542: };