(svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
- Feature: [pbs] Implement autoplacement of pbs blocks, when a block has an entry and an exit pbs signal, covert the entire block to pbs. Can be turned off in the patch settings. - Feature: [pbs] Allow showing of reserved status by making the tracks darker, when the pbs debug level is at least 1.replace/41b28d7194a279bdc17475d4fbe2ea6ec885a466
parent
b872cf7f7b
commit
60ddaf95f0
Binary file not shown.
Binary file not shown.
@ -0,0 +1,291 @@
|
||||
#include "stdafx.h"
|
||||
#include "openttd.h"
|
||||
#include "pbs.h"
|
||||
#include "functions.h"
|
||||
#include "debug.h"
|
||||
#include "map.h"
|
||||
#include "tile.h"
|
||||
#include "npf.h"
|
||||
#include "pathfind.h"
|
||||
#include "depot.h"
|
||||
|
||||
/** @file pbs.c Path-Based-Signalling implementation file
|
||||
* @see pbs.h */
|
||||
|
||||
/* reserved track encoding:
|
||||
normal railway tracks:
|
||||
map3lo bits 4..6 = 'Track'number of reserved track + 1, if this is zero it means nothing is reserved on this tile
|
||||
map3lo bit 7 = if this is set, then the opposite track ('Track'number^1) is also reserved
|
||||
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.
|
||||
*/
|
||||
static const int16 encrt_to_unavail[16] = {
|
||||
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:
|
||||
if ((_map5[tile] & ~1) == 0xC4) {
|
||||
// waypoint
|
||||
SETBIT(_map3_lo[tile], 6);
|
||||
} else {
|
||||
// normal rail track
|
||||
byte encrt = (_map3_hi[tile] & 0xF0) >> 4; // get current encoded info (see comments at top of file)
|
||||
|
||||
if (encrt == 0) // nothing reserved before
|
||||
encrt = track + 1;
|
||||
else if (encrt == (track^1) + 1) // opposite track reserved before
|
||||
encrt |= 8;
|
||||
|
||||
_map3_hi[tile] &= ~0xF0;
|
||||
_map3_hi[tile] |= encrt << 4;
|
||||
}
|
||||
break;
|
||||
case MP_TUNNELBRIDGE:
|
||||
_map3_hi[tile] |= (1 << track) & 3;
|
||||
break;
|
||||
case MP_STATION:
|
||||
SETBIT(_map3_lo[tile], 6);
|
||||
break;
|
||||
case MP_STREET:
|
||||
// make sure it is a railroad crossing
|
||||
if (!IsLevelCrossing(tile)) return;
|
||||
SETBIT(_map5[tile], 0);
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
};
|
||||
// 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:
|
||||
if ((_map5[tile] & ~1) == 0xC4) {
|
||||
// waypoint
|
||||
// check if its reserved
|
||||
if (!HASBIT(_map3_lo[tile], 6)) return 0;
|
||||
// return the track for the correct direction
|
||||
return HASBIT(_map5[tile], 0) ? 2 : 1;
|
||||
} else {
|
||||
// normal track
|
||||
byte res = encrt_to_reserved[(_map3_hi[tile] & 0xF0) >> 4];
|
||||
assert(res != 0xFF);
|
||||
return res;
|
||||
};
|
||||
case MP_TUNNELBRIDGE:
|
||||
return (_map3_hi[tile] & 3);
|
||||
case MP_STATION:
|
||||
// check if its reserved
|
||||
if (!HASBIT(_map3_lo[tile], 6)) return 0;
|
||||
// return the track for the correct direction
|
||||
return HASBIT(_map5[tile], 0) ? 2 : 1;
|
||||
case MP_STREET:
|
||||
// make sure its a railroad crossing
|
||||
if (!IsLevelCrossing(tile)) return 0;
|
||||
// check if its reserved
|
||||
if (!HASBIT(_map5[tile], 0)) return 0;
|
||||
// return the track for the correct direction
|
||||
return HASBIT(_map5[tile], 3) ? 1 : 2;
|
||||
default:
|
||||
return 0;
|
||||
};
|
||||
};
|
||||
|
||||
uint16 PBSTileUnavail(TileIndex tile) {
|
||||
assert(IsValidTile(tile));
|
||||
switch (GetTileType(tile)) {
|
||||
case MP_RAILWAY:
|
||||
if ((_map5[tile] & ~1) == 0xC4) {
|
||||
// waypoint
|
||||
return HASBIT(_map3_lo[tile], 6) ? TRACKDIR_BIT_MASK : 0;
|
||||
} else {
|
||||
// normal track
|
||||
uint16 res = encrt_to_unavail[(_map3_hi[tile] & 0xF0) >> 4];
|
||||
assert(res != 0xFFFF);
|
||||
return res;
|
||||
};
|
||||
case MP_TUNNELBRIDGE:
|
||||
return (_map3_hi[tile] & 3) | ((_map3_hi[tile] & 3) << 8);
|
||||
case MP_STATION:
|
||||
return HASBIT(_map3_lo[tile], 6) ? TRACKDIR_BIT_MASK : 0;
|
||||
case MP_STREET:
|
||||
// make sure its a railroad crossing
|
||||
if (!IsLevelCrossing(tile)) return 0;
|
||||
// check if its reserved
|
||||
return (HASBIT(_map5[tile], 0)) ? TRACKDIR_BIT_MASK : 0;
|
||||
default:
|
||||
return 0;
|
||||
};
|
||||
};
|
||||
|
||||
void PBSClearTrack(TileIndex tile, Track track) {
|
||||
assert(IsValidTile(tile));
|
||||
assert(track <= 5);
|
||||
switch (GetTileType(tile)) {
|
||||
case MP_RAILWAY:
|
||||
if ((_map5[tile] & ~1) == 0xC4) {
|
||||
// waypoint
|
||||
CLRBIT(_map3_lo[tile], 6);
|
||||
} else {
|
||||
// normal rail track
|
||||
byte encrt = (_map3_hi[tile] & 0xF0) >> 4;
|
||||
|
||||
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;
|
||||
|
||||
_map3_hi[tile] &= ~0xF0;
|
||||
_map3_hi[tile] |= encrt << 4;
|
||||
}
|
||||
break;
|
||||
case MP_TUNNELBRIDGE:
|
||||
_map3_hi[tile] &= ~((1 << track) & 3);
|
||||
break;
|
||||
case MP_STATION:
|
||||
CLRBIT(_map3_lo[tile], 6);
|
||||
break;
|
||||
case MP_STREET:
|
||||
// make sure it is a railroad crossing
|
||||
if (!IsLevelCrossing(tile)) return;
|
||||
CLRBIT(_map5[tile], 0);
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
};
|
||||
// if debugging, mark tile dirty to show reserved status
|
||||
if (_debug_pbs_level >= 1)
|
||||
MarkTileDirtyByTile(tile);
|
||||
};
|
||||
|
||||
void PBSClearPath(TileIndex tile, Trackdir trackdir) {
|
||||
uint16 res;
|
||||
FindLengthOfTunnelResult flotr;
|
||||
assert(IsValidTile(tile));
|
||||
assert((trackdir & ~8) <= 5);
|
||||
do {
|
||||
PBSClearTrack(tile, trackdir & 7);
|
||||
|
||||
if (IsTileType(tile, MP_TUNNELBRIDGE) && (_map5[tile] & 0xF0)==0 && (unsigned)(_map5[tile] & 3) == TrackdirToExitdir(trackdir)) {
|
||||
// this is a tunnel
|
||||
flotr = FindLengthOfTunnel(tile, TrackdirToExitdir(trackdir));
|
||||
|
||||
tile = flotr.tile;
|
||||
} else {
|
||||
byte exitdir = TrackdirToExitdir(trackdir);
|
||||
if (IsTileDepotType(tile, TRANSPORT_RAIL) && (exitdir != GetDepotDirection(tile, TRANSPORT_RAIL)))
|
||||
return;
|
||||
tile = AddTileIndexDiffCWrap(tile, TileIndexDiffCByDir(exitdir));
|
||||
if (IsTileDepotType(tile, TRANSPORT_RAIL) && (exitdir != ReverseDiagdir(GetDepotDirection(tile, TRANSPORT_RAIL))))
|
||||
return;
|
||||
};
|
||||
|
||||
res = PBSTileReserved(tile);
|
||||
res |= res << 8;
|
||||
res &= TrackdirReachesTrackdirs(trackdir);
|
||||
trackdir = FindFirstBit2x64(res);
|
||||
|
||||
} while (res != 0);
|
||||
};
|
||||
|
||||
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;
|
||||
};
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
bool PBSIsPbsDepot(uint tile)
|
||||
{
|
||||
SetSignalsDataPbs ssd;
|
||||
bool result = false;
|
||||
DiagDirection direction = GetDepotDirection(tile,TRANSPORT_RAIL);
|
||||
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;
|
||||
};
|
||||
|
||||
return result;
|
||||
}
|
@ -0,0 +1,82 @@
|
||||
#ifndef PBS_H
|
||||
#define PBS_H
|
||||
|
||||
/** @file pbs.h Path-Based-Signalling header file
|
||||
* @see pbs.c */
|
||||
|
||||
#include "vehicle.h"
|
||||
#include "tile.h"
|
||||
#include "map.h"
|
||||
#include "rail.h"
|
||||
|
||||
/**
|
||||
* constants used for pbs_mode argument of npf-functions
|
||||
*/
|
||||
enum pbs_modes {
|
||||
PBS_MODE_NONE = 0, // no pbs
|
||||
PBS_MODE_GREEN = 1, // look for green exit signal from pbs block
|
||||
PBS_MODE_ANY = 2, // look for any exit signal from block
|
||||
};
|
||||
|
||||
/**
|
||||
* constants used for v->u.rail.pbs_status
|
||||
*/
|
||||
enum PBSStatus {
|
||||
PBS_STAT_NONE = 0,
|
||||
PBS_STAT_HAS_PATH = 1,
|
||||
PBS_STAT_NEED_PATH = 2,
|
||||
};
|
||||
|
||||
|
||||
void PBSReserveTrack(TileIndex tile, Track track);
|
||||
/**<
|
||||
* Marks a track as reserved.
|
||||
* @param tile The tile of the track.
|
||||
* @param track The track to reserve, valid values 0-5.
|
||||
*/
|
||||
|
||||
byte PBSTileReserved(TileIndex tile);
|
||||
/**<
|
||||
* Check which tracks are reserved on a tile.
|
||||
* @param tile The tile which you want to check.
|
||||
* @return The tracks reserved on that tile, each of the bits 0-5 is set when the corresponding track is reserved.
|
||||
*/
|
||||
|
||||
uint16 PBSTileUnavail(TileIndex tile);
|
||||
/**<
|
||||
* Check which trackdirs are unavailable due to reserved tracks on a tile.
|
||||
* @param tile The tile which you want to check.
|
||||
* @return The tracks reserved on that tile, each of the bits 0-5,8-13 is set when the corresponding trackdir is unavailable.
|
||||
*/
|
||||
|
||||
void PBSClearTrack(TileIndex tile, Track track);
|
||||
/**<
|
||||
* Unreserves a track.
|
||||
* @param tile The tile of the track.
|
||||
* @param track The track to unreserve, valid values 0-5.
|
||||
*/
|
||||
|
||||
void PBSClearPath(TileIndex tile, Trackdir trackdir);
|
||||
/**<
|
||||
* Follows a planned(reserved) path, and unreserves the tracks.
|
||||
* @param tile The tile on which the path starts
|
||||
* @param trackdir The trackdirection in which the path starts
|
||||
*/
|
||||
|
||||
bool PBSIsPbsSignal(TileIndex tile, Trackdir trackdir);
|
||||
/**<
|
||||
* Checks if there are pbs signals on a track.
|
||||
* @param tile The tile you want to check
|
||||
* @param trackdir The trackdir you want to check
|
||||
* @return True when there are pbs signals on that tile
|
||||
*/
|
||||
|
||||
bool PBSIsPbsDepot(uint tile);
|
||||
/**<
|
||||
* Checks if a depot is inside a pbs block.
|
||||
* Tis means that the block it is in needs to have at least 1 signal, and that all signals in it need to be pbs signals.
|
||||
* @param tile The depot tile to check
|
||||
* @return True when the depot is inside a pbs block.
|
||||
*/
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue