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

The network item manager is responsible for handling all network related item manager tasks - synchronisation between clients and servers. More...

#include <network_item_manager.hpp>

Inheritance diagram for NetworkItemManager:
Inheritance graph
[legend]

Public Member Functions

virtual ~NetworkItemManager ()
 Destructor. More...
 
virtual void reset () OVERRIDE
 Nothing to do here. More...
 
virtual void setItemConfirmationTime (std::weak_ptr< STKPeer > peer, int ticks) OVERRIDE
 Called by the GameProtocol when a confirmation for an item event is received by the server. More...
 
virtual void collectedItem (ItemState *item, AbstractKart *kart) OVERRIDE
 Called when a kart collects an item. More...
 
virtual void switchItems () OVERRIDE
 Called when a switch is activated. More...
 
virtual ItemdropNewItem (ItemState::ItemType type, const AbstractKart *kart, const Vec3 *server_xyz=NULL, const Vec3 *server_normal=NULL) OVERRIDE
 Called when a new item is created, e.g. More...
 
virtual BareNetworkStringsaveState (std::vector< std::string > *ru) OVERRIDE
 Saves the state of all items. More...
 
virtual void restoreState (BareNetworkString *buffer, int count) OVERRIDE
 Restores the state of the items to the current world time. More...
 
virtual void rewindToEvent (BareNetworkString *bns) OVERRIDE
 Called when an event needs to be replayed. More...
 
virtual void saveTransform () OVERRIDE
 Called before a rewind. More...
 
virtual void computeError () OVERRIDE
 Called when a rewind is finished, and is used to compute the error caused by the rewind (which is then visually smoothed over time). More...
 
virtual void undoState (BareNetworkString *buffer) OVERRIDE
 Undo the effects of the given state, but do not rewind to that state (which is done by rewindTo). More...
 
virtual void undoEvent (BareNetworkString *) OVERRIDE
 Called when an event needs to be undone. More...
 
void addLiveJoinPeer (std::weak_ptr< STKPeer > peer)
 
void erasePeerInGame (std::weak_ptr< STKPeer > peer)
 
void saveCompleteState (BareNetworkString *buffer) const
 Save all current items at current ticks in server for live join.
 
void restoreCompleteState (const BareNetworkString &buffer)
 Restore all current items at current ticks in client for live join or at the start of a race.
 
- Public Member Functions inherited from Rewinder
 Rewinder (const std::string &ui="")
 
virtual std::function< void()> getLocalStateRestoreFunction ()
 
const std::string & getUniqueIdentity () const
 
bool rewinderAdd ()
 Add this object to the list of all rewindable objects in the rewind manager.
 
template<typename T >
std::shared_ptr< T > getShared ()
 
- Public Member Functions inherited from ItemManager
virtual ~ItemManager ()
 Destructor. More...
 
virtual ItemplaceItem (ItemState::ItemType type, const Vec3 &xyz, const Vec3 &normal)
 Places a new item on the track/arena. More...
 
void update (int ticks)
 Updates all items, and handles switching items back if the switch time is over. More...
 
void updateGraphics (float dt)
 Updates the graphics, called once per rendered frame. More...
 
void checkItemHit (AbstractKart *kart)
 Checks if any item was collected by the given kart. More...
 
void reset ()
 Resets all items and removes bubble gum that is stuck on the track. More...
 
bool randomItemsForArena (const AlignedArray< btTransform > &pos)
 
bool areItemsSwitched ()
 Returns true if the items are switched atm. More...
 
unsigned int getNumberOfItems () const
 Returns the number of items. More...
 
const ItemStategetItem (unsigned int n) const
 Returns a pointer to the n-th item. More...
 
ItemStategetItem (unsigned int n)
 Returns a pointer to the n-th item. More...
 
bool itemExists (const ItemState *is) const
 
const AllItemTypesgetItemsInQuads (unsigned int n) const
 Returns a reference to the array of all items on the specified quad.
 
ItemgetFirstItemInQuad (unsigned int n) const
 Returns the first item (NULL if none) on the specified quad.
 

Static Public Member Functions

static void create ()
 Creates one instance of the item manager. More...
 
- Static Public Member Functions inherited from ItemManager
static void loadDefaultItemMeshes ()
 Loads the default item meshes (high- and low-resolution).
 
static void removeTextures ()
 Clean up all textures. More...
 
static void create ()
 Creates one instance of the item manager. More...
 
static void destroy ()
 Destroys the one instance of the item manager. More...
 
static void updateRandomSeed (uint32_t seed_number)
 
static uint32_t getRandomSeed ()
 
static void disableItemCollection ()
 Disable item collection, useful to test client mispreditions or client/server disagreements. More...
 
static scene::IMesh * getItemModel (ItemState::ItemType type)
 Returns the mesh for a certain item. More...
 
static scene::IMesh * getItemLowResolutionModel (ItemState::ItemType type)
 Returns the low resolution mesh for a certain item. More...
 
static video::SColorf & getGlowColor (ItemState::ItemType type)
 Returns the glow color for an item. More...
 
static ItemManagerget ()
 Return an instance of the item manager (it does not automatically create one, call create for that). More...
 

Static Public Attributes

static bool m_network_item_debugging = false
 

Private Member Functions

void forwardTime (int ticks)
 Progresses the time for all item by the given number of ticks. More...
 
 NetworkItemManager ()
 Creates a new instance of the item manager. More...
 

Private Attributes

std::vector< ItemState * > m_confirmed_state
 A client stores a 'confirmed' item event state, which is based on the server data. More...
 
int m_confirmed_switch_ticks
 The switch ticks value at the lime of the last confirmed state. More...
 
int m_confirmed_state_time
 Time at which m_confirmed_state was taken. More...
 
std::mutex m_live_players_mutex
 Allow remove or add peer live. More...
 
std::map< std::weak_ptr< STKPeer >, int32_t, std::owner_less< std::weak_ptr< STKPeer > > > m_last_confirmed_item_ticks
 Stores on the server the latest confirmed tick from each client. More...
 
Synchronised< std::vector< ItemEventInfo > > m_item_events
 List of all items events. More...
 

Additional Inherited Members

- Protected Types inherited from ItemManager
typedef std::vector< ItemState * > AllItemTypes
 The vector of all items of the current track. More...
 
- Protected Member Functions inherited from Rewinder
void setUniqueIdentity (const std::string &uid)
 
- Protected Member Functions inherited from ItemManager
void deleteItem (ItemState *item)
 Removes an items from the items-in-quad list, from the list of all items, and then frees the item itself. More...
 
virtual unsigned int insertItem (Item *item)
 Inserts the new item into the items management data structures, if possible reusing an existing, unused entry (e.g. More...
 
void switchItemsInternal (std::vector< ItemState * > &all_items)
 Switches all items: boxes become bananas and vice versa for a certain amount of time (as defined in stk_config.xml).
 
void setSwitchItems (const std::vector< int > &switch_items)
 Sets which objects is getting switched to what. More...
 
void insertItemInQuad (Item *item)
 Insert into the appropriate quad list, if there is a quad list (i.e. More...
 
void deleteItemInQuad (ItemState *item)
 Removes an items from the items-in-quad list only. More...
 
 ItemManager ()
 Creates a new instance of the item manager. More...
 
- Protected Attributes inherited from ItemManager
AllItemTypes m_all_items
 
std::vector< ItemState::ItemTypem_switch_to
 What item this item is switched to. More...
 
int m_switch_ticks
 Remaining time that items should remain switched. More...
 
- Static Protected Attributes inherited from ItemManager
static std::shared_ptr< ItemManagerm_item_manager
 The instance of ItemManager while a race is on. More...
 

Detailed Description

The network item manager is responsible for handling all network related item manager tasks - synchronisation between clients and servers.

It maintains one 'confirmed' state on the clients, based on the latest server update. The server sends updates that only contains the delta between the last confirmed and the current server state. Eash client confirms to the server which deltas it has received. Once all clients have received a delta, the server will remove it from the list of deltas.

Constructor & Destructor Documentation

◆ NetworkItemManager()

NetworkItemManager::NetworkItemManager ( )
private

Creates a new instance of the item manager.

This is done at startup of each race. We must save the item state first (so that it is restored first), otherwise state updates for a kart could be overwritten by e.g. simulating the item collection later (which resets bubblegum counter), so a rewinder uid of "I" which is less than "Kx" (kart rewinder with id x)

◆ ~NetworkItemManager()

NetworkItemManager::~NetworkItemManager ( )
virtual

Destructor.

Cleans up all items and meshes stored.

Member Function Documentation

◆ collectedItem()

void NetworkItemManager::collectedItem ( ItemState item,
AbstractKart kart 
)
virtual

Called when a kart collects an item.

In network games only the server acts on this event.

Parameters
itemThe item that was collected.
kartThe kart that collected the item.

Reimplemented from ItemManager.

◆ computeError()

virtual void NetworkItemManager::computeError ( )
inlinevirtual

Called when a rewind is finished, and is used to compute the error caused by the rewind (which is then visually smoothed over time).

Implements Rewinder.

◆ create()

void NetworkItemManager::create ( )
static

Creates one instance of the item manager.

◆ dropNewItem()

Item * NetworkItemManager::dropNewItem ( ItemState::ItemType  type,
const AbstractKart kart,
const Vec3 server_xyz = NULL,
const Vec3 server_normal = NULL 
)
virtual

Called when a new item is created, e.g.

bubble gum.

Parameters
typeType of the item.
kartIn case of a dropped item used to avoid that a kart is affected by its own items.
server_xyzIn case of rewind the server's position of this item.
server_normalIn case of rewind the server's normal of this item.

Reimplemented from ItemManager.

◆ forwardTime()

void NetworkItemManager::forwardTime ( int  ticks)
private

Progresses the time for all item by the given number of ticks.

Used when computing a new state from a confirmed state.

Parameters
ticksNumber of ticks that need to be simulated.

◆ reset()

void NetworkItemManager::reset ( )
virtual

Nothing to do here.

Reimplemented from Rewinder.

◆ restoreState()

void NetworkItemManager::restoreState ( BareNetworkString buffer,
int  count 
)
virtual

Restores the state of the items to the current world time.

It takes the last saved confirmed state, applies any updates from the server, and then syncs up the confirmed state to the in-race items. It uses exactly 'count' bytes of the message.

Parameters
bufferthe state content.
countNumber of bytes used for this state.

Implements Rewinder.

◆ rewindToEvent()

virtual void NetworkItemManager::rewindToEvent ( BareNetworkString buffer)
inlinevirtual

Called when an event needs to be replayed.

This is called during rewind, i.e. when going forward in time again.

Implements Rewinder.

◆ saveState()

BareNetworkString * NetworkItemManager::saveState ( std::vector< std::string > *  ru)
virtual

Saves the state of all items.

This is done by using a state that has been confirmed by all clients as a base, and then only adding any changes applied to that state later. As clients keep on confirming events the confirmed event will be moved forward in time, and older events can be deleted (and not sent to the clients anymore). This function is also called on the client in the first frame of a race to save the initial state, which is the first confirmed state by all clients.

Implements Rewinder.

◆ saveTransform()

virtual void NetworkItemManager::saveTransform ( )
inlinevirtual

Called before a rewind.

Is used to save the previous position of an object before a rewind, so that the error due to a rewind can be computed.

Implements Rewinder.

◆ setItemConfirmationTime()

void NetworkItemManager::setItemConfirmationTime ( std::weak_ptr< STKPeer peer,
int  ticks 
)
virtual

Called by the GameProtocol when a confirmation for an item event is received by the server.

Once all hosts have confirmed an event, it can be deleted and won't be sent to any clients again.

Parameters
peerPeer confirming the latest event time received.
ticksTime at which the last event was received.

Reimplemented from ItemManager.

◆ switchItems()

void NetworkItemManager::switchItems ( )
virtual

Called when a switch is activated.

On the server adds this information to the item state so it can be sent to all clients.

Reimplemented from ItemManager.

◆ undoEvent()

virtual void NetworkItemManager::undoEvent ( BareNetworkString buffer)
inlinevirtual

Called when an event needs to be undone.

This is called while going backwards for rewinding - all stored events will get an 'undo' call.

Implements Rewinder.

◆ undoState()

virtual void NetworkItemManager::undoState ( BareNetworkString buffer)
inlinevirtual

Undo the effects of the given state, but do not rewind to that state (which is done by rewindTo).

This is called while going backwards for rewinding - all stored events will get an 'undo' call.

Implements Rewinder.

Member Data Documentation

◆ m_confirmed_state

std::vector<ItemState*> NetworkItemManager::m_confirmed_state
private

A client stores a 'confirmed' item event state, which is based on the server data.

This is used in case of rewind.

◆ m_confirmed_state_time

int NetworkItemManager::m_confirmed_state_time
private

Time at which m_confirmed_state was taken.

◆ m_confirmed_switch_ticks

int NetworkItemManager::m_confirmed_switch_ticks
private

The switch ticks value at the lime of the last confirmed state.

◆ m_item_events

Synchronised< std::vector<ItemEventInfo> > NetworkItemManager::m_item_events
private

List of all items events.

◆ m_last_confirmed_item_ticks

std::map<std::weak_ptr<STKPeer>, int32_t, std::owner_less<std::weak_ptr<STKPeer> > > NetworkItemManager::m_last_confirmed_item_ticks
private

Stores on the server the latest confirmed tick from each client.

◆ m_live_players_mutex

std::mutex NetworkItemManager::m_live_players_mutex
private

Allow remove or add peer live.


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