# Simulation¶

## simulation.*¶

class ts2.simulation.Simulation(options, trackItems, routes, trainTypes, services, trns, messageLogger)[source]

Bases: PyQt5.QtCore.QObject

The Simulation class holds all the game logic.

__init__(options, trackItems, routes, trainTypes, services, trns, messageLogger)[source]
Parameters: options – trackItems – routes – trainTypes – services – trns – messageLogger –
activateRoute(siId, persistent=False, force=False)[source]

This slot is normally connected to a SignalItem signalSelected signal, which itself is emitted when a signal is left-clicked.

It is in charge of:

• Checking whether this is the first signal to be selected, if it the case, _selectedSignal is set to this signal and the function returns.
• Otherwise, it checks whether there exists a possible route between _selectedSignal and this signal. If it is the case, and that no other active route conflicts with this route, it is activated.

The following signals are emitted depending of the situation:

• routeActivated
• noRouteBetweenSignals
• conflictingRoute
Parameters: siId (str) – ID of the

SignalItem owner of the SignalGraphicItem that has been left-clicked.

addTrain(train)[source]

Adds a train to the trains list.

Parameters: train – The train instance to add to the list
Returns: Checks that all TrackItem‘s are

conflictingRoute

pyqtSignal(Route)

context
Returns: the context of this Simulation object GAME

Find the items that are linked together through their coordinates and populate the _nextItem and _previousItem variables of each items.

currentTime
Returns: the current sim time QtCore.QTime
desactivateRoute(siId)[source]

This slot is normally connected to the SignalItem‘s signalUnSelected, which itself is emitted when a signal is right-clicked. It is in charge of deactivating the routes starting from this signal.

Parameters: siId – The ID of the signalItem owner of the signalGraphicsItem that has been right-clicked.
static distanceBetween(p1, p2)[source]

Calculates the distance between both points p1 and p2 in pixels @param p1 @param p2 @return

findRoute(si1, si2)[source]

Checks whether a route exists between two signals.

Parameters: si1 – The SignalItem of

the first signal :param si2: The SignalItem of the second signal :return: The route between signal si1 and si2 if it exists, otherwise None :rtype: Route or None

for_json()[source]

Dumps the simulation to JSON.

getLineItem(placeCode, trackCode)[source]
Parameters: placeCode – trackCode – the LineItem instance defined by

placeCode and trackCode.

initialize(simulationWindow)[source]

Initializes the simulation.

Parameters: simulationWindow –
messageLogger
Returns: the message logger of the simulation. MessageLogger
noRouteBetweenSignals

pyqtSignal(SignalItem, SignalItem)

option(key)[source]
Parameters: key (str) – the simulation option specified by key mixed
pause(paused=True)[source]

Toggle pause.

Parameters: paused – If paused is True pause the game, else continue.
place(placeCode)[source]
Parameters: placeCode (str) – a place defined by placeCode. Place or None
places
Returns: places dictionary dict of Place‘s
routes
Returns: the routes of the simulation Route
saveGame(fileName)[source]

Saves the game.

Parameters: fileName (str) – fileName to write
scene
Returns: the QGraphicsScene on which the simulation scenery is

displayed

scorer
Returns: the scorer instance of the simulation Scorer
selectedServiceModel
Returns: the selected service model ServiceInfoModel
selectedTrainModel
Returns: the trainlist model TrainInfoModel
selectionChanged

pyqtSignal()

service(serviceCode)[source]
Parameters: serviceCode (str) – a service defined by serviceCode. Service or None
serviceListModel
Returns: the service model ServiceListModel
services
Returns: services dictionary dict of Service‘s
setTimeFactor(timeFactor)[source]
Parameters: timeFactor (int) – Sets the time factor to timeFactor.
startTime
Returns: the time at which the simulation starts. QtCore.QTime
timeChanged

pyqtSignal(QtCore.QTime)

timeElapsed

pyqtSignal(float)

timerOut()[source]

Changes the simulation time and emits the timeChanged and the timeElapsed signals This function is normally connected to the timer timeout signal.

trackItem(tiId)[source]
Parameters: tiId – trackitem id the trackItems with tiId TrackItem or None
trackItems
Returns: the trackItems in simulation. dict of TrackItem
trainListModel
Returns: the trainlist model TrainListModel
trainSelected

pyqtSignal(int)

trainStatusChanged

pyqtSignal(int)

trainTypes
Returns: a dict of the TrainType of

the simulation :rtype: dict

trains

Todo

@trains property docs

updateContext(tabNum)[source]

Updates the context of the simulation. Does nothing in the base class.

updatePlaces()[source]

Updates the places dictionary from TrackItem data.

updateSelection()[source]

Updates the trackItem selection. Does nothing in the base simulation class.

ts2.simulation.json_hook(dct)[source]

ts2.simulation.load(simulationWindow, jsonStream)[source]

Loads the simulation from jsonStream and returns it.

1. We create the graph of objects from json.load(). When initialized, each object stores its JSON data.
2. When all the objects are created, we call the initialize() method of the Simulation which calls in turn the initialize() method of each object.
ts2.simulation.translate(str, str, str disambiguation=None, int n=-1) → str