SuperTuxKart
Loading...
Searching...
No Matches
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.
 
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)/.
 
void playEventsTill (int world_ticks, bool fast_forward)
 Replays all events from the last event played till the specified time.
 
void addEvent (EventRewinder *event_rewinder, BareNetworkString *buffer, bool confirmed, int ticks=-1)
 Adds an event to the rewind data.
 
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 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.
 
bool isRewinding () const
 Returns true if currently a rewind is happening.
 
int getNotRewoundWorldTicks () const
 
int getLatestConfirmedState () const
 Returns the time of the latest confirmed state.
 
bool useLocalEvent () const
 
void addRewindInfoEventFunction (RewindInfoEventFunction *rief)
 
void addNetworkRewindInfo (RewindInfo *ri)
 
bool shouldSaveState (int ticks)
 
void resetSmoothNetworkBody ()
 
void handleResetSmoothNetworkBody ()
 Reset all smooth network body of rewinders so the rubber band effect of moveable does not exist during firstly live join.
 
void addMissingRewinder (const std::string &name)
 
bool hasMissingRewinder (const std::string &name) const
 

Static Public Member Functions

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

Private Member Functions

 RewindManager ()
 The constructor.
 
 ~RewindManager ()
 Frees all saved state information.
 
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.
 
RewindQueue m_rewind_queue
 The queue that stores all rewind infos.
 
unsigned int m_overall_state_size
 Overall amount of memory allocated by states.
 
bool m_is_rewinding
 Indicates if currently a rewind is happening.
 
int m_state_frequency
 How much time between consecutive state saves.
 
std::atomic< int > m_not_rewound_ticks
 This stores the original World time in ticks during a rewind.
 
std::vector< RewindInfoEventFunction * > m_pending_rief
 
bool m_schedule_reset_network_body
 
std::set< std::string > m_missing_rewinders
 

Static Private Attributes

static RewindManagerm_rewind_manager [PT_COUNT]
 Singleton pointer.
 
static std::atomic_bool m_enable_rewind_manager
 En- or Disable the rewind manager.
 

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.

◆ get()

static RewindManager * RewindManager::get ( )
inlinestatic

Returns the singleton.

This function will not automatically create the singleton.

◆ 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.

◆ 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_enable_rewind_manager

std::atomic_bool RewindManager::m_enable_rewind_manager
staticprivate

En- or Disable the rewind manager.

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

◆ 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.


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