tron@2186: /* $Id$ */ tron@2186: truelight@1542: #include "stdafx.h" Darkvater@1891: #include "openttd.h" truelight@1542: truelight@1542: #include "command.h" tron@2163: #include "functions.h" truelight@1542: #include "gfx.h" truelight@1542: #include "map.h" truelight@1542: #include "order.h" truelight@1542: #include "saveload.h" truelight@1542: #include "station.h" truelight@1542: #include "tile.h" truelight@1542: #include "town.h" truelight@1542: #include "waypoint.h" tron@2159: #include "variables.h" truelight@1542: #include "table/sprites.h" truelight@1542: #include "table/strings.h" truelight@1542: #include "table/track_land.h" truelight@1542: truelight@1542: enum { truelight@1542: /* Max waypoints: 64000 (8 * 8000) */ truelight@1542: WAYPOINT_POOL_BLOCK_SIZE_BITS = 3, /* In bits, so (1 << 3) == 8 */ truelight@1542: WAYPOINT_POOL_MAX_BLOCKS = 8000, truelight@1542: truelight@1542: MAX_WAYPOINTS_PER_TOWN = 64, truelight@1542: }; truelight@1542: truelight@1542: /** truelight@1542: * Called if a new block is added to the waypoint-pool truelight@1542: */ truelight@1542: static void WaypointPoolNewBlock(uint start_item) truelight@1542: { truelight@1542: Waypoint *wp; truelight@1542: truelight@1542: FOR_ALL_WAYPOINTS_FROM(wp, start_item) truelight@1542: wp->index = start_item++; truelight@1542: } truelight@1542: truelight@1542: /* Initialize the town-pool */ truelight@1542: MemoryPool _waypoint_pool = { "Waypoints", WAYPOINT_POOL_MAX_BLOCKS, WAYPOINT_POOL_BLOCK_SIZE_BITS, sizeof(Waypoint), &WaypointPoolNewBlock, 0, 0, NULL }; truelight@1542: truelight@1542: /* Create a new waypoint */ truelight@1542: Waypoint *AllocateWaypoint(void) truelight@1542: { truelight@1542: Waypoint *wp; truelight@1542: truelight@1542: FOR_ALL_WAYPOINTS(wp) { truelight@1542: if (wp->xy == 0) { truelight@1542: uint index = wp->index; truelight@1542: truelight@1542: memset(wp, 0, sizeof(Waypoint)); truelight@1542: wp->index = index; truelight@1542: truelight@1542: return wp; truelight@1542: } truelight@1542: } truelight@1542: truelight@1542: /* Check if we can add a block to the pool */ truelight@1542: if (AddBlockToPool(&_waypoint_pool)) truelight@1542: return AllocateWaypoint(); truelight@1542: truelight@1542: return NULL; truelight@1542: } truelight@1542: truelight@1542: /* Fetch a waypoint by tile */ truelight@1542: Waypoint *GetWaypointByTile(TileIndex tile) truelight@1542: { truelight@1542: Waypoint *wp; truelight@1542: truelight@1542: FOR_ALL_WAYPOINTS(wp) { truelight@1542: if (wp->xy == tile) truelight@1542: return wp; truelight@1542: } truelight@1542: truelight@1542: return NULL; truelight@1542: } truelight@1542: truelight@1542: /* Update the sign for the waypoint */ truelight@1542: void UpdateWaypointSign(Waypoint *wp) truelight@1542: { truelight@1542: Point pt = RemapCoords2(TileX(wp->xy) * 16, TileY(wp->xy) * 16); truelight@1542: SetDParam(0, wp->index); truelight@1542: UpdateViewportSignPos(&wp->sign, pt.x, pt.y - 0x20, STR_WAYPOINT_VIEWPORT); truelight@1542: } truelight@1542: truelight@1542: /* Redraw the sign of a waypoint */ truelight@1542: void RedrawWaypointSign(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: truelight@1542: /* Update all signs */ truelight@1542: void UpdateAllWaypointSigns(void) truelight@1542: { truelight@1542: Waypoint *wp; truelight@1542: truelight@1542: FOR_ALL_WAYPOINTS(wp) { truelight@1542: if (wp->xy) truelight@1542: UpdateWaypointSign(wp); truelight@1542: } truelight@1542: } truelight@1542: truelight@1542: /* Set the default name for a waypoint */ truelight@1542: void MakeDefaultWaypointName(Waypoint *wp) truelight@1542: { truelight@1542: Waypoint *local_wp; truelight@1542: bool used_waypoint[MAX_WAYPOINTS_PER_TOWN]; truelight@1542: int i; truelight@1542: truelight@1542: wp->town_index = ClosestTownFromTile(wp->xy, (uint)-1)->index; truelight@1542: truelight@1542: memset(used_waypoint, 0, sizeof(used_waypoint)); truelight@1542: truelight@1542: /* Find an unused waypoint number belonging to this town */ truelight@1542: FOR_ALL_WAYPOINTS(local_wp) { truelight@1542: if (wp == local_wp) truelight@1542: continue; truelight@1542: truelight@1542: if (local_wp->xy && local_wp->string == STR_NULL && local_wp->town_index == wp->town_index) truelight@1542: used_waypoint[local_wp->town_cn] = true; truelight@1542: } truelight@1542: truelight@1542: /* Find an empty spot */ truelight@1542: for (i = 0; used_waypoint[i] && i < MAX_WAYPOINTS_PER_TOWN; i++) {} truelight@1542: truelight@1542: wp->string = STR_NULL; truelight@1542: wp->town_cn = i; truelight@1542: } truelight@1542: truelight@1542: /* Find a deleted waypoint close to a tile. */ tron@1977: static Waypoint *FindDeletedWaypointCloseTo(TileIndex tile) truelight@1542: { truelight@1542: Waypoint *wp, *best = NULL; truelight@1542: uint thres = 8, cur_dist; truelight@1542: truelight@1542: FOR_ALL_WAYPOINTS(wp) { truelight@1542: if (wp->deleted && wp->xy) { truelight@1542: cur_dist = DistanceManhattan(tile, wp->xy); 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: Darkvater@1782: /** Convert existing rail to waypoint. Eg build a waypoint station over Darkvater@1782: * piece of rail Darkvater@1782: * @param x,y coordinates where waypoint will be built Darkvater@1782: * @param p1 graphics for waypoint type, bit 8 signifies custom waypoint gfx (& 0x100) 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: */ truelight@1542: int32 CmdBuildTrainWaypoint(int x, int y, uint32 flags, uint32 p1, uint32 p2) truelight@1542: { tron@1980: TileIndex tile = TileVirtXY(x, y); truelight@1542: Waypoint *wp; truelight@1542: uint tileh; truelight@1542: uint dir; truelight@1542: truelight@1542: SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION); truelight@1542: Darkvater@1782: /* if custom gfx are used, make sure it is within bounds */ Darkvater@1783: if (p1 > 0x100 + (uint)GetCustomStationsCount(STAT_CLASS_WAYP)) return CMD_ERROR; Darkvater@1782: tron@2049: if (!IsTileType(tile, MP_RAILWAY) || ((dir = 0, _m[tile].m5 != 1) && (dir = 1, _m[tile].m5 != 2))) truelight@1542: return_cmd_error(STR_1005_NO_SUITABLE_RAILROAD_TRACK); truelight@1542: truelight@1542: if (!CheckTileOwnership(tile)) truelight@1542: return CMD_ERROR; truelight@1542: truelight@1542: if (!EnsureNoVehicle(tile)) return CMD_ERROR; truelight@1542: truelight@1542: tileh = GetTileSlope(tile, NULL); truelight@1542: if (tileh != 0) { celestar@2085: if (!_patches.build_on_slopes || IsSteepTileh(tileh) || !(tileh & (0x3 << dir)) || !(tileh & ~(0x3 << dir))) truelight@1542: return_cmd_error(STR_0007_FLAT_LAND_REQUIRED); truelight@1542: } truelight@1542: truelight@1542: /* Check if there is an already existing, deleted, waypoint close to us that we can reuse. */ truelight@1542: wp = FindDeletedWaypointCloseTo(tile); truelight@1542: if (wp == NULL) { truelight@1542: wp = AllocateWaypoint(); Darkvater@1782: if (wp == NULL) return CMD_ERROR; truelight@1542: truelight@1542: wp->town_index = 0; truelight@1542: wp->string = STR_NULL; truelight@1542: wp->town_cn = 0; truelight@1542: } truelight@1542: truelight@1542: if (flags & DC_EXEC) { truelight@1542: ModifyTile(tile, MP_MAP5, RAIL_TYPE_WAYPOINT | dir); truelight@1542: if (--p1 & 0x100) { // waypoint type 0 uses default graphics truelight@1542: // custom graphics tron@2049: _m[tile].m3 |= 16; tron@2049: _m[tile].m4 = p1 & 0xff; truelight@1542: } truelight@1542: truelight@1542: wp->deleted = 0; truelight@1542: wp->xy = tile; truelight@1542: wp->build_date = _date; truelight@1542: truelight@1542: if (wp->town_index == STR_NULL) truelight@1542: MakeDefaultWaypointName(wp); truelight@1542: truelight@1542: UpdateWaypointSign(wp); truelight@1542: RedrawWaypointSign(wp); truelight@1542: } truelight@1542: truelight@1542: return _price.build_train_depot; truelight@1542: } truelight@1542: truelight@1542: /* Internal handler to delete a waypoint */ truelight@1542: static void DoDeleteWaypoint(Waypoint *wp) truelight@1542: { truelight@1542: Order order; truelight@1542: truelight@1542: wp->xy = 0; truelight@1542: truelight@1542: order.type = OT_GOTO_WAYPOINT; truelight@1542: order.station = wp->index; truelight@1542: DeleteDestinationFromVehicleOrder(order); truelight@1542: truelight@1542: if (wp->string != STR_NULL) truelight@1542: DeleteName(wp->string); truelight@1542: truelight@1542: RedrawWaypointSign(wp); truelight@1542: } truelight@1542: truelight@1542: /* Daily loop for waypoints */ truelight@1542: void WaypointsDailyLoop(void) 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@1542: if (wp->deleted && !--wp->deleted) { truelight@1542: DoDeleteWaypoint(wp); truelight@1542: } truelight@1542: } truelight@1542: } truelight@1542: truelight@1542: /* Remove a waypoint */ tron@1977: int32 RemoveTrainWaypoint(TileIndex tile, uint32 flags, bool justremove) truelight@1542: { truelight@1542: Waypoint *wp; truelight@1542: truelight@1542: /* Make sure it's a waypoint */ tron@2049: if (!IsTileType(tile, MP_RAILWAY) || !IsRailWaypoint(_m[tile].m5)) truelight@1542: return CMD_ERROR; truelight@1542: truelight@1542: if (!CheckTileOwnership(tile) && !(_current_player == OWNER_WATER)) truelight@1542: return CMD_ERROR; truelight@1542: truelight@1542: if (!EnsureNoVehicle(tile)) truelight@1542: return CMD_ERROR; truelight@1542: truelight@1542: if (flags & DC_EXEC) { tron@2049: int direction = _m[tile].m5 & RAIL_WAYPOINT_TRACK_MASK; truelight@1542: 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) { truelight@1542: ModifyTile(tile, MP_MAP5, 1<string != STR_NULL) truelight@1542: DeleteName(wp->string); truelight@1542: truelight@1542: wp->string = str; truelight@1542: wp->town_cn = 0; truelight@1542: truelight@1542: UpdateWaypointSign(wp); truelight@1542: MarkWholeScreenDirty(); truelight@1542: } else { truelight@1542: DeleteName(str); truelight@1542: } tron@2026: } else { truelight@1542: if (flags & DC_EXEC) { truelight@1542: wp = GetWaypoint(p1); truelight@1542: if (wp->string != STR_NULL) truelight@1542: DeleteName(wp->string); truelight@1542: truelight@1542: MakeDefaultWaypointName(wp); truelight@1542: UpdateWaypointSign(wp); truelight@1542: MarkWholeScreenDirty(); truelight@1542: } truelight@1542: } truelight@1542: return 0; truelight@1542: } truelight@1542: truelight@1542: /* This hacks together some dummy one-shot Station structure for a waypoint. */ tron@1977: Station *ComposeWaypointStation(TileIndex tile) truelight@1542: { truelight@1542: Waypoint *wp = GetWaypointByTile(tile); truelight@1542: static Station stat; truelight@1542: truelight@1542: stat.train_tile = stat.xy = wp->xy; truelight@1542: stat.town = GetTown(wp->town_index); truelight@1542: stat.string_id = wp->string == STR_NULL ? /* FIXME? */ 0 : wp->string; truelight@1542: stat.build_date = wp->build_date; truelight@1542: stat.class_id = 6; truelight@1542: stat.stat_id = wp->stat_id; truelight@1542: truelight@1542: return &stat; truelight@1542: } truelight@1542: truelight@1542: extern uint16 _custom_sprites_base; truelight@1542: truelight@1542: /* Draw a waypoint */ truelight@1542: void DrawWaypointSprite(int x, int y, int stat_id, int railtype) truelight@1542: { truelight@1542: StationSpec *stat; truelight@1542: uint32 relocation; truelight@1542: DrawTileSprites *cust; truelight@1542: DrawTileSeqStruct const *seq; truelight@1542: uint32 ormod, img; truelight@1542: truelight@1542: ormod = SPRITE_PALETTE(PLAYER_SPRITE_COLOR(_local_player)); truelight@1542: truelight@1542: x += 33; truelight@1542: y += 17; truelight@1542: truelight@1542: /* draw default waypoint graphics of ID 0 */ truelight@1542: if (stat_id == 0) { truelight@1542: const DrawTrackSeqStruct *dtss = _track_depot_layout_table[4]; truelight@1542: truelight@1542: img = dtss++->image; truelight@1542: if (img & 0x8000) img = (img & 0x7FFF) + railtype*TRACKTYPE_SPRITE_PITCH; truelight@1542: DrawSprite(img, x, y); truelight@1542: truelight@1542: for (; dtss->image != 0; dtss++) { truelight@1542: Point pt = RemapCoords(dtss->subcoord_x, dtss->subcoord_y, 0); truelight@1542: img = dtss->image; truelight@1542: if (img & 0x8000) img |= ormod; truelight@1542: DrawSprite(img, x + pt.x, y + pt.y); truelight@1542: } truelight@1542: return; truelight@1542: } truelight@1542: truelight@1542: stat = GetCustomStation(STAT_CLASS_WAYP, stat_id - 1); truelight@1542: assert(stat); truelight@1542: relocation = GetCustomStationRelocation(stat, NULL, 1); truelight@1542: // emulate station tile - open with building truelight@1542: // add 1 to get the other direction truelight@1542: cust = &stat->renderdata[2]; truelight@1542: truelight@1542: img = cust->ground_sprite; truelight@1542: img += railtype * ((img < _custom_sprites_base) ? TRACKTYPE_SPRITE_PITCH : 1); truelight@1542: truelight@1542: if (img & 0x8000) img = (img & 0x7FFF); truelight@1542: DrawSprite(img, x, y); truelight@1542: truelight@1542: foreach_draw_tile_seq(seq, cust->seq) { truelight@1542: Point pt = RemapCoords(seq->delta_x, seq->delta_y, seq->delta_z); truelight@1542: uint32 image = seq->image + relocation; truelight@1542: truelight@1542: DrawSprite((image&0x3FFF) | ormod, x + pt.x, y + pt.y); truelight@1542: } truelight@1542: } truelight@1542: truelight@1542: /* Fix savegames which stored waypoints in their old format */ truelight@1542: void FixOldWaypoints(void) 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: if (wp->xy == 0) truelight@1542: continue; truelight@1542: 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: truelight@1542: void InitializeWaypoints(void) truelight@1542: { truelight@1542: CleanPool(&_waypoint_pool); truelight@1542: AddBlockToPool(&_waypoint_pool); truelight@1542: } truelight@1542: Darkvater@1881: static const SaveLoad _waypoint_desc[] = { truelight@1542: SLE_CONDVAR(Waypoint, xy, SLE_FILE_U16 | SLE_VAR_U32, 0, 5), truelight@1542: SLE_CONDVAR(Waypoint, xy, SLE_UINT32, 6, 255), truelight@1542: SLE_CONDVAR(Waypoint, town_index, SLE_UINT16, 12, 255), truelight@1542: SLE_CONDVAR(Waypoint, town_cn, SLE_UINT8, 12, 255), tron@2026: SLE_VAR(Waypoint, string, SLE_UINT16), tron@2026: SLE_VAR(Waypoint, deleted, SLE_UINT8), truelight@1542: truelight@1542: SLE_CONDVAR(Waypoint, build_date, SLE_UINT16, 3, 255), truelight@1542: SLE_CONDVAR(Waypoint, stat_id, SLE_UINT8, 3, 255), truelight@1542: truelight@1542: SLE_END() truelight@1542: }; truelight@1542: truelight@1542: static void Save_WAYP(void) truelight@1542: { truelight@1542: Waypoint *wp; truelight@1542: truelight@1542: FOR_ALL_WAYPOINTS(wp) { truelight@1542: if (wp->xy != 0) { truelight@1542: SlSetArrayIndex(wp->index); truelight@1542: SlObject(wp, _waypoint_desc); truelight@1542: } truelight@1542: } truelight@1542: } truelight@1542: truelight@1542: static void Load_WAYP(void) truelight@1542: { truelight@1542: int index; truelight@1542: truelight@1542: while ((index = SlIterateArray()) != -1) { truelight@1542: Waypoint *wp; truelight@1542: truelight@1542: if (!AddBlockIfNeeded(&_waypoint_pool, index)) truelight@1542: error("Waypoints: failed loading savegame: too many waypoints"); truelight@1542: truelight@1542: wp = GetWaypoint(index); truelight@1542: SlObject(wp, _waypoint_desc); truelight@1542: } truelight@1542: } truelight@1542: truelight@1542: const ChunkHandler _waypoint_chunk_handlers[] = { truelight@1542: { 'CHKP', Save_WAYP, Load_WAYP, CH_ARRAY | CH_LAST}, truelight@1542: };