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" peter1138@2546: #include "pbs.h" truelight@1542: #include "table/sprites.h" truelight@1542: #include "table/strings.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 */ tron@2752: static 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: /* Update the sign for the waypoint */ tron@2817: static 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 */ 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: 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 */ tron@2752: static 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: 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: */ peter1138@2670: void UpdateAllWaypointCustomGraphics(void) 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++) { peter1138@2670: const StationSpec *spec = GetCustomStation(STAT_CLASS_WAYP, i); peter1138@2670: if (spec != NULL && spec->grfid == wp->grfid && spec->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 Darkvater@1782: * @param x,y coordinates where waypoint will be built 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: */ 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 */ peter1138@2625: if (p1 >= GetNumCustomStations(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) { peter1138@2670: const StationSpec *spec = NULL; peter1138@2546: bool reserved = PBSTileReserved(tile) != 0; peter1138@2670: ModifyTile(tile, MP_MAP2 | MP_MAP5, wp->index, RAIL_TYPE_WAYPOINT | dir); peter1138@2670: peter1138@2670: if (GB(p1, 0, 8) < GetNumCustomStations(STAT_CLASS_WAYP)) peter1138@2670: spec = GetCustomStation(STAT_CLASS_WAYP, GB(p1, 0, 8)); peter1138@2670: peter1138@2670: if (spec != NULL) { peter1138@2670: SETBIT(_m[tile].m3, 4); peter1138@2670: wp->stat_id = GB(p1, 0, 8); peter1138@2670: wp->grfid = spec->grfid; peter1138@2670: wp->localidx = spec->localidx; peter1138@2670: } else { peter1138@2670: // Specified custom graphics do not exist, so use default. peter1138@2670: CLRBIT(_m[tile].m3, 4); peter1138@2670: wp->stat_id = 0; peter1138@2670: wp->grfid = 0; peter1138@2670: wp->localidx = 0; truelight@1542: } peter1138@2670: peter1138@2603: if (reserved) { peter1138@2603: PBSReserveTrack(tile, dir); peter1138@2603: } else { peter1138@2603: PBSClearTrack(tile, dir); peter1138@2603: } 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 */ truelight@2668: if (!IsTileType(tile, MP_RAILWAY) || !IsRailWaypoint(tile)) 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) { peter1138@2546: bool reserved = PBSTileReserved(tile) != 0; peter1138@2670: ModifyTile(tile, MP_MAP2_CLEAR | 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: ludde@2261: truelight@1542: /* Draw a waypoint */ tron@2520: void DrawWaypointSprite(int x, int y, int stat_id, RailType railtype) truelight@1542: { peter1138@2624: const StationSpec *stat; truelight@1542: uint32 relocation; peter1138@2624: const DrawTileSprites *cust; truelight@1542: DrawTileSeqStruct const *seq; celestar@2254: const RailtypeInfo *rti = GetRailTypeInfo(railtype); 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: peter1138@2625: stat = GetCustomStation(STAT_CLASS_WAYP, stat_id); peter1138@2625: if (stat == NULL) { peter1138@2625: // stat is NULL for default waypoints and when waypoint graphics are peter1138@2625: // not loaded. ludde@2261: DrawDefaultWaypointSprite(x, y, railtype); truelight@1542: return; truelight@1542: } truelight@1542: 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; celestar@2254: img += (img < _custom_sprites_base) ? rti->total_offset : railtype; truelight@1542: celestar@2187: if (img & PALETTE_MODIFIER_COLOR) img = (img & SPRITE_MASK); 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; celestar@2187: DrawSprite((image & SPRITE_MASK) | 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: peter1138@2670: SLE_CONDVAR(Waypoint, build_date, SLE_UINT16, 3, 255), peter1138@2670: SLE_CONDVAR(Waypoint, localidx, SLE_UINT8, 3, 255), peter1138@2670: SLE_CONDVAR(Waypoint, grfid, SLE_UINT32, 17, 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: };