SuperTuxKart
Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes | Static Private Attributes | List of all members
RewindManager Class Reference

This class manages rewinding. More...

#include <rewind_manager.hpp>

Public Member Functions

void reset ()
 Frees all saved state information and all destroyable rewinder.
 
void update (int ticks)
 Determines if a new state snapshot should be taken, and if so calls all rewinder to do so. More...
 
void rewindTo (int target_ticks, int ticks_now, bool fast_forward)
 Rewinds to the specified time, then goes forward till the current World::getTime() is reached again: it will replay everything before World::getTime(), but not the events at World::getTime() (or later)/. More...
 
void playEventsTill (int world_ticks, bool fast_forward)
 Replays all events from the last event played till the specified time. More...
 
void addEvent (EventRewinder *event_rewinder, BareNetworkString *buffer, bool confirmed, int ticks=-1)
 Adds an event to the rewind data. More...
 
void addNetworkEvent (EventRewinder *event_rewinder, BareNetworkString *buffer, int ticks)
 Adds an event to the list of network rewind data. More...
 
void addNetworkState (BareNetworkString *buffer, int ticks)
 Adds a state to the list of network rewind data. More...
 
void saveState ()
 Saves a state using the GameProtocol function to combine several independent rewinders to write one state.
 
std::shared_ptr< RewindergetRewinder (const std::string &name)
 
bool addRewinder (std::shared_ptr< Rewinder > rewinder)
 Adds a Rewinder to the list of all rewinders. More...
 
bool isRewinding () const
 Returns true if currently a rewind is happening. More...
 
int getNotRewoundWorldTicks () const
 
int getLatestConfirmedState () const
 Returns the time of the latest confirmed state. More...
 
bool useLocalEvent () const
 
void addRewindInfoEventFunction (RewindInfoEventFunction *rief)
 
void addNetworkRewindInfo (RewindInfo *ri)
 
bool shouldSaveState (int ticks)
 
void resetSmoothNetworkBody ()
 Reset all smooth network body of rewinders so the rubber band effect of moveable does not exist during firstly live join.
 

Static Public Member Functions

static RewindManagercreate ()
 Creates the singleton. More...
 
static void destroy ()
 Destroys the singleton. More...
 
static void setEnable (bool m)
 En- or disables rewinding. More...
 
static bool isEnabled ()
 Returns if rewinding is enabled or not. More...
 
static RewindManagerget ()
 Returns the singleton. More...
 

Private Member Functions

 RewindManager ()
 The constructor.
 
 ~RewindManager ()
 Frees all saved state information. More...
 
void clearExpiredRewinder ()
 
void mergeRewindInfoEventFunction ()
 

Private Attributes

std::map< int, std::vector< std::function< void()> > > m_local_state
 
std::map< std::string, std::weak_ptr< Rewinder > > m_all_rewinder
 A list of all objects that can be rewound. More...
 
RewindQueue m_rewind_queue
 The queue that stores all rewind infos. More...
 
unsigned int m_overall_state_size
 Overall amount of memory allocated by states. More...
 
bool m_is_rewinding
 Indicates if currently a rewind is happening. More...
 
int m_state_frequency
 How much time between consecutive state saves. More...
 
std::atomic< int > m_not_rewound_ticks
 This stores the original World time in ticks during a rewind. More...
 
std::vector< RewindInfoEventFunction * > m_pending_rief
 

Static Private Attributes

static RewindManagerm_rewind_manager = NULL
 Singleton pointer. More...
 
static bool m_enable_rewind_manager = false
 En- or Disable the rewind manager. More...
 

Detailed Description

This class manages rewinding.

It keeps track of:

  1. Go back in time: Determine the latest time t_min < T so that each rewindable objects has at least one state before T. For each state that is skipped during this process undoState() is being called, and for each event undoEvent() of the Rewinder.
  2. Restore state at time t_min For each Rewinder the state at time t_min is restored by calling rewindToState(char *). TODO: atm there is no guarantee that each object will have a state at a given time. We either need to work around that, or make sure to store at least an unconfirmed state whenever we receive a confirmed state.
  3. Rerun the simulation till the current time t_current is reached:
    1. Determine the time t_next of the next frame. This is either current_time + 1/60 (physics default time step size), or less if RewindInfo at an earlier time is available). This determines the time step size for the next frame (i.e. t_next - t_current).
    2. For all RewindInfo at time t_next call:
      • restoreState() if the RewindInfo is a confirmed state
      • discardState() if the RewindInfo is an unconfirmed state TODO: still missing, and instead of discard perhaps store a new state??
      • rewindToEvent() if the RewindInfo is an event
    3. Do one step of world simulation, using the updated (confirmed) states and newly set events (e.g. kart input).

Constructor & Destructor Documentation

◆ ~RewindManager()

RewindManager::~RewindManager ( )
private

Frees all saved state information.

Note that the Rewinder data must be freed elsewhere.

Member Function Documentation

◆ addEvent()

void RewindManager::addEvent ( EventRewinder event_rewinder,
BareNetworkString buffer,
bool  confirmed,
int  ticks = -1 
)

Adds an event to the rewind data.

The data to be stored must be allocated and not freed by the caller!

Parameters
timeTime at which the event was recorded. If time is not specified (or set to -1), the current world time is used.
bufferPointer to the event data.

◆ addNetworkEvent()

void RewindManager::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_rewind_info by the main thread. The data to be stored must be allocated and not freed by the caller!

Parameters
timeTime at which the event was recorded.
bufferPointer to the event data.

◆ addNetworkState()

void RewindManager::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 m_rewind_info by the main thread. The data to be stored must be allocated and not freed by the caller!

Parameters
timeTime at which the event was recorded.
bufferPointer to the event data.

◆ addRewinder()

bool RewindManager::addRewinder ( std::shared_ptr< Rewinder rewinder)

Adds a Rewinder to the list of all rewinders.

Returns
true If successfully added, false otherwise.

◆ create()

RewindManager * RewindManager::create ( )
static

Creates the singleton.

◆ destroy()

void RewindManager::destroy ( )
static

Destroys the singleton.

◆ get()

static RewindManager* RewindManager::get ( )
inlinestatic

Returns the singleton.

This function will not automatically create the singleton.

◆ getLatestConfirmedState()

int RewindManager::getLatestConfirmedState ( ) const
inline

Returns the time of the latest confirmed state.

◆ isEnabled()

static bool RewindManager::isEnabled ( )
inlinestatic

Returns if rewinding is enabled or not.

◆ isRewinding()

bool RewindManager::isRewinding ( ) const
inline

Returns true if currently a rewind is happening.

◆ playEventsTill()

void RewindManager::playEventsTill ( int  world_ticks,
bool  fast_forward 
)

Replays all events from the last event played till the specified time.

Parameters
world_ticksUp to (and inclusive) which time events will be replayed.
fast_forwardIf true, then only rewinders in network will be updated, but not the physics.

◆ rewindTo()

void RewindManager::rewindTo ( int  rewind_ticks,
int  now_ticks,
bool  fast_forward 
)

Rewinds to the specified time, then goes forward till the current World::getTime() is reached again: it will replay everything before World::getTime(), but not the events at World::getTime() (or later)/.

Parameters
rewind_ticksTime to rewind to.
now_ticksUp to which ticks events are replayed: up to but EXCLUDING new_ticks (the event at now_ticks are played in the calling subroutine playEventsTill).
fast_forwardIf true, then only rewinders in network will be updated, but not the physics.

◆ setEnable()

static void RewindManager::setEnable ( bool  m)
inlinestatic

En- or disables rewinding.

◆ update()

void RewindManager::update ( int  ticks_not_used)

Determines if a new state snapshot should be taken, and if so calls all rewinder to do so.

Parameters
ticks_not_usedNUmber of physics time steps - should be 1.

Member Data Documentation

◆ m_all_rewinder

std::map<std::string, std::weak_ptr<Rewinder> > RewindManager::m_all_rewinder
private

A list of all objects that can be rewound.

◆ m_enable_rewind_manager

bool RewindManager::m_enable_rewind_manager = false
staticprivate

En- or Disable the rewind manager.

This is used to disable storing rewind data in case of local races only.

◆ m_is_rewinding

bool RewindManager::m_is_rewinding
private

Indicates if currently a rewind is happening.

◆ m_not_rewound_ticks

std::atomic<int> RewindManager::m_not_rewound_ticks
private

This stores the original World time in ticks during a rewind.

It is used to detect if a client's local time need adjustment to reduce rewinds.

◆ m_overall_state_size

unsigned int RewindManager::m_overall_state_size
private

Overall amount of memory allocated by states.

◆ m_rewind_manager

RewindManager * RewindManager::m_rewind_manager = NULL
staticprivate

Singleton pointer.

◆ m_rewind_queue

RewindQueue RewindManager::m_rewind_queue
private

The queue that stores all rewind infos.

◆ m_state_frequency

int RewindManager::m_state_frequency
private

How much time between consecutive state saves.


The documentation for this class was generated from the following files: