2005-07-24 14:12:37 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2005-07-04 14:58:55 +00:00
|
|
|
#include "stdafx.h"
|
|
|
|
#include "openttd.h"
|
|
|
|
#include "functions.h"
|
2005-07-22 07:02:20 +00:00
|
|
|
#include "pbs.h"
|
2005-07-04 14:58:55 +00:00
|
|
|
#include "debug.h"
|
|
|
|
#include "map.h"
|
|
|
|
#include "tile.h"
|
|
|
|
#include "npf.h"
|
|
|
|
#include "pathfind.h"
|
|
|
|
#include "depot.h"
|
2005-11-16 14:41:01 +00:00
|
|
|
#include "waypoint.h"
|
2005-07-04 14:58:55 +00:00
|
|
|
|
|
|
|
/** @file pbs.c Path-Based-Signalling implementation file
|
|
|
|
* @see pbs.h */
|
|
|
|
|
|
|
|
/* reserved track encoding:
|
|
|
|
normal railway tracks:
|
2005-07-06 10:43:36 +00:00
|
|
|
map3hi bits 4..6 = 'Track'number of reserved track + 1, if this is zero it means nothing is reserved on this tile
|
|
|
|
map3hi bit 7 = if this is set, then the opposite track ('Track'number^1) is also reserved
|
2005-07-04 14:58:55 +00:00
|
|
|
waypoints/stations:
|
|
|
|
map3lo bit 6 set = track is reserved
|
|
|
|
tunnels/bridges:
|
|
|
|
map3hi bit 0 set = track with 'Track'number 0 is reserved
|
|
|
|
map3hi bit 1 set = track with 'Track'number 1 is reserved
|
|
|
|
level crossings:
|
|
|
|
map5 bit 0 set = the rail track is reserved
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* maps an encoded reserved track (from map3lo bits 4..7)
|
|
|
|
* to the tracks that are reserved.
|
|
|
|
* 0xFF are invalid entries and should never be accessed.
|
|
|
|
*/
|
|
|
|
static const byte encrt_to_reserved[16] = {
|
|
|
|
0x00, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0xFF,
|
|
|
|
0xFF, 0xFF, 0xFF, 0x0C, 0x0C, 0x30, 0x30, 0xFF
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* maps an encoded reserved track (from map3lo bits 4..7)
|
|
|
|
* to the track(dir)s that are unavailable due to reservations.
|
|
|
|
* 0xFFFF are invalid entries and should never be accessed.
|
|
|
|
*/
|
2005-07-23 19:48:24 +00:00
|
|
|
static const uint16 encrt_to_unavail[16] = {
|
2005-07-04 14:58:55 +00:00
|
|
|
0x0000, 0x3F3F, 0x3F3F, 0x3737, 0x3B3B, 0x1F1F, 0x2F2F, 0xFFFF,
|
|
|
|
0xFFFF, 0xFFFF, 0xFFFF, 0x3F3F, 0x3F3F, 0x3F3F, 0x3F3F, 0xFFFF
|
|
|
|
};
|
|
|
|
|
|
|
|
void PBSReserveTrack(TileIndex tile, Track track) {
|
|
|
|
assert(IsValidTile(tile));
|
|
|
|
assert(track <= 5);
|
|
|
|
switch (GetTileType(tile)) {
|
|
|
|
case MP_RAILWAY:
|
2005-11-16 14:41:01 +00:00
|
|
|
if (IsRailWaypoint(tile)) {
|
2005-07-04 14:58:55 +00:00
|
|
|
// waypoint
|
2005-07-13 18:04:01 +00:00
|
|
|
SETBIT(_m[tile].m3, 6);
|
2005-07-04 14:58:55 +00:00
|
|
|
} else {
|
|
|
|
// normal rail track
|
2005-10-05 07:20:26 +00:00
|
|
|
byte encrt = GB(_m[tile].m4, 4, 4); // get current encoded info (see comments at top of file)
|
2005-07-04 14:58:55 +00:00
|
|
|
|
|
|
|
if (encrt == 0) // nothing reserved before
|
|
|
|
encrt = track + 1;
|
|
|
|
else if (encrt == (track^1) + 1) // opposite track reserved before
|
|
|
|
encrt |= 8;
|
|
|
|
|
2005-10-05 07:20:26 +00:00
|
|
|
SB(_m[tile].m4, 4, 4, encrt);
|
2005-07-04 14:58:55 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case MP_TUNNELBRIDGE:
|
2005-07-13 18:04:01 +00:00
|
|
|
_m[tile].m4 |= (1 << track) & 3;
|
2005-07-04 14:58:55 +00:00
|
|
|
break;
|
|
|
|
case MP_STATION:
|
2005-07-13 18:04:01 +00:00
|
|
|
SETBIT(_m[tile].m3, 6);
|
2005-07-04 14:58:55 +00:00
|
|
|
break;
|
|
|
|
case MP_STREET:
|
|
|
|
// make sure it is a railroad crossing
|
|
|
|
if (!IsLevelCrossing(tile)) return;
|
2005-07-13 18:04:01 +00:00
|
|
|
SETBIT(_m[tile].m5, 0);
|
2005-07-04 14:58:55 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return;
|
2005-07-23 19:48:24 +00:00
|
|
|
}
|
2005-07-04 14:58:55 +00:00
|
|
|
// if debugging, mark tile dirty to show reserved status
|
|
|
|
if (_debug_pbs_level >= 1)
|
|
|
|
MarkTileDirtyByTile(tile);
|
|
|
|
}
|
|
|
|
|
|
|
|
byte PBSTileReserved(TileIndex tile) {
|
|
|
|
assert(IsValidTile(tile));
|
|
|
|
switch (GetTileType(tile)) {
|
|
|
|
case MP_RAILWAY:
|
2005-11-16 14:41:01 +00:00
|
|
|
if (IsRailWaypoint(tile)) {
|
2005-07-04 14:58:55 +00:00
|
|
|
// waypoint
|
|
|
|
// check if its reserved
|
2005-07-13 18:04:01 +00:00
|
|
|
if (!HASBIT(_m[tile].m3, 6)) return 0;
|
2005-07-04 14:58:55 +00:00
|
|
|
// return the track for the correct direction
|
2005-07-13 18:04:01 +00:00
|
|
|
return HASBIT(_m[tile].m5, 0) ? 2 : 1;
|
2005-07-04 14:58:55 +00:00
|
|
|
} else {
|
|
|
|
// normal track
|
2005-10-05 07:20:26 +00:00
|
|
|
byte res = encrt_to_reserved[GB(_m[tile].m4, 4, 4)];
|
2005-07-04 14:58:55 +00:00
|
|
|
assert(res != 0xFF);
|
|
|
|
return res;
|
2005-07-23 19:48:24 +00:00
|
|
|
}
|
2005-07-04 14:58:55 +00:00
|
|
|
case MP_TUNNELBRIDGE:
|
2005-10-05 07:20:26 +00:00
|
|
|
return GB(_m[tile].m4, 0, 2);
|
2005-07-04 14:58:55 +00:00
|
|
|
case MP_STATION:
|
|
|
|
// check if its reserved
|
2005-07-13 18:04:01 +00:00
|
|
|
if (!HASBIT(_m[tile].m3, 6)) return 0;
|
2005-07-04 14:58:55 +00:00
|
|
|
// return the track for the correct direction
|
2005-07-13 18:04:01 +00:00
|
|
|
return HASBIT(_m[tile].m5, 0) ? 2 : 1;
|
2005-07-04 14:58:55 +00:00
|
|
|
case MP_STREET:
|
|
|
|
// make sure its a railroad crossing
|
|
|
|
if (!IsLevelCrossing(tile)) return 0;
|
|
|
|
// check if its reserved
|
2005-07-13 18:04:01 +00:00
|
|
|
if (!HASBIT(_m[tile].m5, 0)) return 0;
|
2005-07-04 14:58:55 +00:00
|
|
|
// return the track for the correct direction
|
2005-07-13 18:04:01 +00:00
|
|
|
return HASBIT(_m[tile].m5, 3) ? 1 : 2;
|
2005-07-04 14:58:55 +00:00
|
|
|
default:
|
|
|
|
return 0;
|
2005-07-23 19:48:24 +00:00
|
|
|
}
|
|
|
|
}
|
2005-07-04 14:58:55 +00:00
|
|
|
|
|
|
|
uint16 PBSTileUnavail(TileIndex tile) {
|
|
|
|
assert(IsValidTile(tile));
|
|
|
|
switch (GetTileType(tile)) {
|
|
|
|
case MP_RAILWAY:
|
2005-11-16 14:41:01 +00:00
|
|
|
if (IsRailWaypoint(tile)) {
|
2005-07-04 14:58:55 +00:00
|
|
|
// waypoint
|
2005-07-13 18:04:01 +00:00
|
|
|
return HASBIT(_m[tile].m3, 6) ? TRACKDIR_BIT_MASK : 0;
|
2005-07-04 14:58:55 +00:00
|
|
|
} else {
|
|
|
|
// normal track
|
2005-10-05 07:20:26 +00:00
|
|
|
uint16 res = encrt_to_unavail[GB(_m[tile].m4, 4, 4)];
|
2005-07-04 14:58:55 +00:00
|
|
|
assert(res != 0xFFFF);
|
|
|
|
return res;
|
2005-07-23 19:48:24 +00:00
|
|
|
}
|
2005-07-04 14:58:55 +00:00
|
|
|
case MP_TUNNELBRIDGE:
|
2005-10-05 07:20:26 +00:00
|
|
|
return GB(_m[tile].m4, 0, 2) | (GB(_m[tile].m4, 0, 2) << 8);
|
2005-07-04 14:58:55 +00:00
|
|
|
case MP_STATION:
|
2005-07-13 18:04:01 +00:00
|
|
|
return HASBIT(_m[tile].m3, 6) ? TRACKDIR_BIT_MASK : 0;
|
2005-07-04 14:58:55 +00:00
|
|
|
case MP_STREET:
|
|
|
|
// make sure its a railroad crossing
|
|
|
|
if (!IsLevelCrossing(tile)) return 0;
|
|
|
|
// check if its reserved
|
2005-07-13 18:04:01 +00:00
|
|
|
return (HASBIT(_m[tile].m5, 0)) ? TRACKDIR_BIT_MASK : 0;
|
2005-07-04 14:58:55 +00:00
|
|
|
default:
|
|
|
|
return 0;
|
2005-07-23 19:48:24 +00:00
|
|
|
}
|
|
|
|
}
|
2005-07-04 14:58:55 +00:00
|
|
|
|
|
|
|
void PBSClearTrack(TileIndex tile, Track track) {
|
|
|
|
assert(IsValidTile(tile));
|
|
|
|
assert(track <= 5);
|
|
|
|
switch (GetTileType(tile)) {
|
|
|
|
case MP_RAILWAY:
|
2005-11-16 14:41:01 +00:00
|
|
|
if (IsRailWaypoint(tile)) {
|
2005-07-04 14:58:55 +00:00
|
|
|
// waypoint
|
2005-07-13 18:04:01 +00:00
|
|
|
CLRBIT(_m[tile].m3, 6);
|
2005-07-04 14:58:55 +00:00
|
|
|
} else {
|
|
|
|
// normal rail track
|
2005-10-05 07:20:26 +00:00
|
|
|
byte encrt = GB(_m[tile].m4, 4, 4);
|
2005-07-04 14:58:55 +00:00
|
|
|
|
|
|
|
if (encrt == track + 1)
|
|
|
|
encrt = 0;
|
|
|
|
else if (encrt == track + 1 + 8)
|
|
|
|
encrt = (track^1) + 1;
|
|
|
|
else if (encrt == (track^1) + 1 + 8)
|
|
|
|
encrt &= 7;
|
|
|
|
|
2005-10-05 07:20:26 +00:00
|
|
|
SB(_m[tile].m4, 4, 4, encrt);
|
2005-07-04 14:58:55 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case MP_TUNNELBRIDGE:
|
2005-07-13 18:04:01 +00:00
|
|
|
_m[tile].m4 &= ~((1 << track) & 3);
|
2005-07-04 14:58:55 +00:00
|
|
|
break;
|
|
|
|
case MP_STATION:
|
2005-07-13 18:04:01 +00:00
|
|
|
CLRBIT(_m[tile].m3, 6);
|
2005-07-04 14:58:55 +00:00
|
|
|
break;
|
|
|
|
case MP_STREET:
|
|
|
|
// make sure it is a railroad crossing
|
|
|
|
if (!IsLevelCrossing(tile)) return;
|
2005-07-13 18:04:01 +00:00
|
|
|
CLRBIT(_m[tile].m5, 0);
|
2005-07-04 14:58:55 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return;
|
2005-07-23 19:48:24 +00:00
|
|
|
}
|
2005-07-04 14:58:55 +00:00
|
|
|
// if debugging, mark tile dirty to show reserved status
|
|
|
|
if (_debug_pbs_level >= 1)
|
|
|
|
MarkTileDirtyByTile(tile);
|
2005-07-23 19:48:24 +00:00
|
|
|
}
|
2005-07-04 14:58:55 +00:00
|
|
|
|
2005-07-17 20:09:02 +00:00
|
|
|
void PBSClearPath(TileIndex tile, Trackdir trackdir, TileIndex end_tile, Trackdir end_trackdir) {
|
2005-07-04 14:58:55 +00:00
|
|
|
uint16 res;
|
|
|
|
FindLengthOfTunnelResult flotr;
|
|
|
|
assert(IsValidTile(tile));
|
2005-07-17 20:09:02 +00:00
|
|
|
assert(IsValidTrackdir(trackdir));
|
|
|
|
|
2005-07-04 14:58:55 +00:00
|
|
|
do {
|
2005-07-17 20:09:02 +00:00
|
|
|
PBSClearTrack(tile, TrackdirToTrack(trackdir));
|
|
|
|
|
|
|
|
if (tile == end_tile && TrackdirToTrack(trackdir) == TrackdirToTrack(end_trackdir))
|
|
|
|
return;
|
2005-07-04 14:58:55 +00:00
|
|
|
|
2005-10-05 07:20:26 +00:00
|
|
|
if (IsTileType(tile, MP_TUNNELBRIDGE) &&
|
|
|
|
GB(_m[tile].m5, 4, 4) == 0 &&
|
|
|
|
GB(_m[tile].m5, 0, 2) == TrackdirToExitdir(trackdir)) {
|
2005-07-04 14:58:55 +00:00
|
|
|
// this is a tunnel
|
|
|
|
flotr = FindLengthOfTunnel(tile, TrackdirToExitdir(trackdir));
|
|
|
|
|
|
|
|
tile = flotr.tile;
|
|
|
|
} else {
|
|
|
|
byte exitdir = TrackdirToExitdir(trackdir);
|
|
|
|
tile = AddTileIndexDiffCWrap(tile, TileIndexDiffCByDir(exitdir));
|
2005-07-23 19:48:24 +00:00
|
|
|
}
|
2005-07-04 14:58:55 +00:00
|
|
|
|
|
|
|
res = PBSTileReserved(tile);
|
|
|
|
res |= res << 8;
|
|
|
|
res &= TrackdirReachesTrackdirs(trackdir);
|
|
|
|
trackdir = FindFirstBit2x64(res);
|
|
|
|
|
|
|
|
} while (res != 0);
|
2005-07-23 19:48:24 +00:00
|
|
|
}
|
2005-07-04 14:58:55 +00:00
|
|
|
|
|
|
|
bool PBSIsPbsSignal(TileIndex tile, Trackdir trackdir)
|
|
|
|
{
|
|
|
|
assert(IsValidTile(tile));
|
|
|
|
assert(IsValidTrackdir(trackdir));
|
|
|
|
|
|
|
|
if (!_patches.new_pathfinding_all)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!IsTileType(tile, MP_RAILWAY))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (GetRailTileType(tile) != RAIL_TYPE_SIGNALS)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!HasSignalOnTrackdir(tile, trackdir))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (GetSignalType(tile, TrackdirToTrack(trackdir)) == 4)
|
|
|
|
return true;
|
|
|
|
else
|
|
|
|
return false;
|
2005-07-23 19:48:24 +00:00
|
|
|
}
|
2005-07-04 14:58:55 +00:00
|
|
|
|
|
|
|
typedef struct SetSignalsDataPbs {
|
|
|
|
int cur;
|
|
|
|
|
|
|
|
// these are used to keep track of the signals.
|
|
|
|
byte bit[NUM_SSD_ENTRY];
|
|
|
|
TileIndex tile[NUM_SSD_ENTRY];
|
|
|
|
} SetSignalsDataPbs;
|
|
|
|
|
|
|
|
// This function stores the signals inside the SetSignalsDataPbs struct, passed as callback to FollowTrack() in the PBSIsPbsSegment() function below
|
|
|
|
static bool SetSignalsEnumProcPBS(uint tile, SetSignalsDataPbs *ssd, int trackdir, uint length, byte *state)
|
|
|
|
{
|
|
|
|
// the tile has signals?
|
|
|
|
if (IsTileType(tile, MP_RAILWAY)) {
|
|
|
|
if (HasSignalOnTrack(tile, TrackdirToTrack(trackdir))) {
|
|
|
|
|
|
|
|
if (ssd->cur != NUM_SSD_ENTRY) {
|
|
|
|
ssd->tile[ssd->cur] = tile; // remember the tile index
|
|
|
|
ssd->bit[ssd->cur] = TrackdirToTrack(trackdir); // and the controlling bit number
|
|
|
|
ssd->cur++;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
} else if (IsTileDepotType(tile, TRANSPORT_RAIL))
|
|
|
|
return true; // don't look further if the tile is a depot
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2005-07-22 08:40:19 +00:00
|
|
|
bool PBSIsPbsSegment(uint tile, Trackdir trackdir)
|
2005-07-04 14:58:55 +00:00
|
|
|
{
|
|
|
|
SetSignalsDataPbs ssd;
|
2005-07-22 08:40:19 +00:00
|
|
|
bool result = PBSIsPbsSignal(tile, trackdir);
|
|
|
|
DiagDirection direction = TrackdirToExitdir(trackdir);//GetDepotDirection(tile,TRANSPORT_RAIL);
|
2005-07-04 14:58:55 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
ssd.cur = 0;
|
|
|
|
|
|
|
|
FollowTrack(tile, 0xC000 | TRANSPORT_RAIL, direction, (TPFEnumProc*)SetSignalsEnumProcPBS, NULL, &ssd);
|
|
|
|
for(i=0; i!=ssd.cur; i++) {
|
|
|
|
uint tile = ssd.tile[i];
|
|
|
|
byte bit = ssd.bit[i];
|
|
|
|
if (!PBSIsPbsSignal(tile, bit) && !PBSIsPbsSignal(tile, bit | 8))
|
|
|
|
return false;
|
|
|
|
result = true;
|
2005-07-23 19:48:24 +00:00
|
|
|
}
|
2005-07-04 14:58:55 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|