SuperTuxKart
|
Public Member Functions | |
RewindQueue () | |
The RewindQueue stores one TimeStepInfo for each time step done. | |
~RewindQueue () | |
Frees all saved state information. | |
void | reset () |
Frees all saved state information and all destroyable rewinder. | |
void | addLocalEvent (EventRewinder *event_rewinder, BareNetworkString *buffer, bool confirmed, int ticks) |
Adds an event to the rewind data. | |
void | addLocalState (BareNetworkString *buffer, bool confirmed, int ticks) |
Adds a state from the local simulation to the last created TimeStepInfo container with the current world time. | |
void | addNetworkEvent (EventRewinder *event_rewinder, BareNetworkString *buffer, int ticks) |
Adds an event to the list of network rewind data. | |
void | addNetworkState (BareNetworkString *buffer, int ticks) |
Adds a state to the list of network rewind data. | |
void | addNetworkRewindInfo (RewindInfo *ri) |
void | mergeNetworkData (int world_ticks, bool *needs_rewind, int *rewind_ticks) |
Merges thread-safe all data received from the network up to and including the current time (tick) with the current local rewind information. | |
void | replayAllEvents (int ticks) |
Replays all events (not states) that happened at the specified time. | |
bool | isEmpty () const |
bool | hasMoreRewindInfo () const |
Returns true if there is at least one more RewindInfo available. | |
int | undoUntil (int undo_ticks) |
Rewinds the rewind queue and undos all events/states stored. | |
void | insertRewindInfo (RewindInfo *ri) |
Inserts a RewindInfo object in the list of all events at the correct time. | |
int | getLatestConfirmedState () const |
Returns the time of the latest confirmed state. | |
void | next () |
Sets the current element to be the next one and returns the next RewindInfo element. | |
RewindInfo * | getCurrent () |
Returns the current RewindInfo. | |
Static Public Member Functions | |
static void | unitTesting () |
Unit tests for RewindQueue. | |
Private Types | |
typedef std::list< RewindInfo * > | AllRewindInfo |
typedef std::vector< RewindInfo * > | AllNetworkRewindInfo |
The list of all events received from the network. | |
Private Member Functions | |
void | cleanupOldRewindInfo (int ticks) |
Deletes all states and event before the given time. | |
Private Attributes | |
AllRewindInfo | m_all_rewind_info |
Synchronised< AllNetworkRewindInfo > | m_network_events |
AllRewindInfo::iterator | m_current |
Iterator to the curren time step info to be handled. | |
int | m_latest_confirmed_state_time |
Time at which the latest confirmed state is at. | |
|
private |
The list of all events received from the network.
They are stored in a separate thread (so this data structure is thread-save), and merged into m_rewind_info from the main thread. This design (as opposed to locking m_rewind_info) reduces the synchronisation between main thread and network thread.
RewindQueue::RewindQueue | ( | ) |
The RewindQueue stores one TimeStepInfo for each time step done.
The TimeStepInfo stores all states and events to be used at the given timestep. All network events (i.e. new states or client events) are stored in a separate list m_network_events. At the very start of a new time step a new TimeStepInfo object is added. Then all network events that are supposed to happen between t and t+dt are added to this newly added TimeStep (see mergeNetworkData), and are then being executed. In case of a rewind the RewindQueue finds the last TimeStepInfo with a confirmed server state (undoing the events, see undoUntil). Then the state is restored from the TimeStepInfo object (see replayAllStates) then the rewind manager re-executes the time steps (using the events stored at each timestep).
RewindQueue::~RewindQueue | ( | ) |
Frees all saved state information.
Note that the Rewinder data must be freed elsewhere.
void RewindQueue::addLocalEvent | ( | EventRewinder * | event_rewinder, |
BareNetworkString * | buffer, | ||
bool | confirmed, | ||
int | ticks | ||
) |
Adds an event to the rewind data.
The data to be stored must be allocated and not freed by the caller!
buffer | Pointer to the event data. |
ticks | Time at which the event happened. |
void RewindQueue::addLocalState | ( | BareNetworkString * | buffer, |
bool | confirmed, | ||
int | ticks | ||
) |
Adds a state from the local simulation to the last created TimeStepInfo container with the current world time.
It is not thread-safe, so needs to be called from the main thread.
rewinder | The rewinder object for this state. |
buffer | The state information. |
confirmed | If this state is confirmed to be correct (e.g. is being received from the servrer), or just a local state for faster rewinds. |
ticks | Time at which the event happened. |
void RewindQueue::addNetworkEvent | ( | EventRewinder * | event_rewinder, |
BareNetworkString * | buffer, | ||
int | ticks | ||
) |
Adds an event to the list of network rewind data.
This function is threadsafe so can be called by the network thread. The data is synched to m_tRewindInformation list by the main thread. The data to be stored must be allocated and not freed by the caller!
buffer | Pointer to the event data. |
ticks | Time at which the event happened. |
void RewindQueue::addNetworkState | ( | BareNetworkString * | buffer, |
int | ticks | ||
) |
Adds a state to the list of network rewind data.
This function is threadsafe so can be called by the network thread. The data is synched to RewindInfo list by the main thread. The data to be stored must be allocated and not freed by the caller!
buffer | Pointer to the event data. |
ticks | Time at which the event happened. |
|
private |
Deletes all states and event before the given time.
ticks | Time (in ticks). |
|
inline |
Returns the current RewindInfo.
Caller must make sure that there is at least one more RewindInfo (see hasMoreRewindInfo()).
void RewindQueue::insertRewindInfo | ( | RewindInfo * | ri | ) |
Inserts a RewindInfo object in the list of all events at the correct time.
If there are several RewindInfo at the exact same time, state RewindInfo will be insert at the front, and event info at the end of the RewindInfo with the same time.
ri | The RewindInfo object to insert. |
update_current | If set, the current pointer will be updated if necessary to point to the new event |
void RewindQueue::mergeNetworkData | ( | int | world_ticks, |
bool * | needs_rewind, | ||
int * | rewind_ticks | ||
) |
Merges thread-safe all data received from the network up to and including the current time (tick) with the current local rewind information.
world_ticks[in] | Current world time up to which network events will be merged in. |
needs_rewind[out] | True if a network event/state was received which was in the past (of this simulation), so a rewind must be performed. |
rewind_time[out] | If needs_rewind is true, the time to which a rewind must be performed (at least). Otherwise undefined. |
void RewindQueue::replayAllEvents | ( | int | ticks | ) |
Replays all events (not states) that happened at the specified time.
ticks | Time in ticks. |
int RewindQueue::undoUntil | ( | int | undo_ticks | ) |
Rewinds the rewind queue and undos all events/states stored.
It stops when the first confirmed state is reached that was recorded before the undo_time and sets the internal 'current' pointer to this state.
undo_time | To what at least events need to be undone. |
|
static |
Unit tests for RewindQueue.
It tests: