Add feature: realistic train braking

Add setting to select train braking model.
This commit is contained in:
Jonathan G Rennison
2021-01-25 02:33:14 +00:00
parent 2b02318c7e
commit ed0ffb6220
37 changed files with 2556 additions and 291 deletions

View File

@@ -49,11 +49,108 @@ struct PBSWaitingPositionRestrictedSignalInfo {
Trackdir trackdir = INVALID_TRACKDIR;
};
PBSTileInfo FollowTrainReservation(const Train *v, Vehicle **train_on_res = nullptr);
enum TrainReservationLookAheadItemType : byte {
TRLIT_STATION = 0, ///< Station/waypoint
TRLIT_REVERSE = 1, ///< Reverse behind signal
TRLIT_TRACK_SPEED = 2, ///< Track or bridge speed limit
TRLIT_SPEED_RESTRICTION = 3, ///< Speed restriction
TRLIT_SIGNAL = 4, ///< Signal
TRLIT_CURVE_SPEED = 5, ///< Curve speed limit
};
struct TrainReservationLookAheadItem {
int32 start;
int32 end;
int16 z_pos;
uint16 data_id;
TrainReservationLookAheadItemType type;
};
struct TrainReservationLookAheadCurve {
int32 position;
DirDiff dir_diff;
};
enum TrainReservationLookAheadFlags {
TRLF_TB_EXIT_FREE = 0, ///< Reservation ends at signalled tunnel/bridge entrance and the corresponding exit is free, but may not be reserved
TRLF_DEPOT_END = 1, ///< Reservation ends at a depot
TRLF_APPLY_ADVISORY = 2, ///< Apply advisory speed limit on next iteration
TRLF_CHUNNEL = 3, ///< Reservation ends at a signalled chunnel entrance
};
struct TrainReservationLookAhead {
TileIndex reservation_end_tile; ///< Tile the reservation ends.
Trackdir reservation_end_trackdir; ///< The reserved trackdir on the end tile.
int32 current_position; ///< Current position of the train on the reservation
int32 reservation_end_position; ///< Position of the end of the reservation
int16 reservation_end_z; ///< The z coordinate of the reservation end
int16 tunnel_bridge_reserved_tiles; ///< How many tiles a reservation into the tunnel/bridge currently extends into the wormhole
uint16 flags; ///< Flags (TrainReservationLookAheadFlags)
uint16 speed_restriction;
std::deque<TrainReservationLookAheadItem> items;
std::deque<TrainReservationLookAheadCurve> curves;
int32 RealEndPosition() const
{
return this->reservation_end_position - (this->tunnel_bridge_reserved_tiles * TILE_SIZE);
}
void AddStation(int tiles, StationID id, int16 z_pos)
{
int end = this->RealEndPosition();
this->items.push_back({ end, end + (((int)TILE_SIZE) * tiles), z_pos, id, TRLIT_STATION });
}
void AddReverse(int16 z_pos)
{
int end = this->RealEndPosition();
this->items.push_back({ end, end, z_pos, 0, TRLIT_REVERSE });
}
void AddTrackSpeedLimit(uint16 speed, int offset, int duration, int16 z_pos)
{
int end = this->RealEndPosition();
this->items.push_back({ end + offset, end + offset + duration, z_pos, speed, TRLIT_TRACK_SPEED });
}
void AddSpeedRestriction(uint16 speed, int16 z_pos)
{
int end = this->RealEndPosition();
this->items.push_back({ end, end, z_pos, speed, TRLIT_SPEED_RESTRICTION });
this->speed_restriction = speed;
}
void AddSignal(uint16 target_speed, int offset, int16 z_pos)
{
int end = this->RealEndPosition();
this->items.push_back({ end + offset, end + offset, z_pos, target_speed, TRLIT_SIGNAL });
}
void AddCurveSpeedLimit(uint16 target_speed, int offset, int16 z_pos)
{
int end = this->RealEndPosition();
this->items.push_back({ end + offset, end + offset, z_pos, target_speed, TRLIT_CURVE_SPEED });
}
};
/** Flags for FollowTrainReservation */
enum FollowTrainReservationFlags {
FTRF_NONE = 0, ///< No flags
FTRF_IGNORE_LOOKAHEAD = 0x01, ///< No use of cached lookahead
};
DECLARE_ENUM_AS_BIT_SET(FollowTrainReservationFlags)
PBSTileInfo FollowTrainReservation(const Train *v, Vehicle **train_on_res = nullptr, FollowTrainReservationFlags flags = FTRF_NONE);
void ApplyAvailableFreeTunnelBridgeTiles(TrainReservationLookAhead *lookahead, int free_tiles, TileIndex tile, TileIndex end);
void TryCreateLookAheadForTrainInTunnelBridge(Train *t);
void FillTrainReservationLookAhead(Train *v);
bool IsSafeWaitingPosition(const Train *v, TileIndex tile, Trackdir trackdir, bool include_line_end, bool forbid_90deg = false);
bool IsWaitingPositionFree(const Train *v, TileIndex tile, Trackdir trackdir, bool forbid_90deg = false, PBSWaitingPositionRestrictedSignalInfo *restricted_signal_info = nullptr);
Train *GetTrainForReservation(TileIndex tile, Track track);
CommandCost CheckTrainReservationPreventsTrackModification(TileIndex tile, Track track);
CommandCost CheckTrainReservationPreventsTrackModification(const Train *v);
CommandCost CheckTrainInTunnelBridgePreventsTrackModification(TileIndex start, TileIndex end);
/**
* Check whether some of tracks is reserved on a tile.