SuperTuxKart
Loading...
Searching...
No Matches
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

 NetworkItemManager ()
 Creates a new instance of the item manager.
 
virtual ~NetworkItemManager ()
 Destructor.
 
virtual void reset () OVERRIDE
 Nothing to do here.
 
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.
 
virtual void collectedItem (ItemState *item, AbstractKart *kart) OVERRIDE
 Called when a kart collects an item.
 
virtual void switchItems () OVERRIDE
 Called when a switch is activated.
 
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.
 
virtual BareNetworkStringsaveState (std::vector< std::string > *ru) OVERRIDE
 Saves the state of all items.
 
virtual void restoreState (BareNetworkString *buffer, int count) OVERRIDE
 Restores the state of the items to the current world time.
 
virtual void rewindToEvent (BareNetworkString *bns) OVERRIDE
 Called when an event needs to be replayed.
 
virtual void saveTransform () OVERRIDE
 Called before a rewind.
 
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).
 
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).
 
virtual void undoEvent (BareNetworkString *) OVERRIDE
 Called when an event needs to be undone.
 
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.
 
void initServer ()
 If this is a server, initializing the peers in game.
 
- Public Member Functions inherited from Rewinder
 Rewinder (const std::string &ui="")
 
virtual void saveTransform ()=0
 Called before a rewind.
 
virtual void computeError ()=0
 Called when a rewind is finished, and is used to compute the error caused by the rewind (which is then visually smoothed over time).
 
virtual BareNetworkStringsaveState (std::vector< std::string > *ru)=0
 Provides a copy of the state of the object in one memory buffer.
 
virtual void undoEvent (BareNetworkString *buffer)=0
 Called when an event needs to be undone.
 
virtual void rewindToEvent (BareNetworkString *buffer)=0
 Called when an event needs to be replayed.
 
virtual void restoreState (BareNetworkString *buffer, int count)=0
 Called when a state needs to be replayed.
 
virtual void undoState (BareNetworkString *buffer)=0
 Undo the effects of the given state, but do not rewind to that state (which is done by rewindTo).
 
virtual void reset ()
 Nothing to do here.
 
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
 ItemManager ()
 Creates a new instance of the item manager.
 
virtual ~ItemManager ()
 Destructor.
 
virtual ItemplaceItem (ItemState::ItemType type, const Vec3 &xyz, const Vec3 &normal)
 Places a new item on the track/arena.
 
virtual ItemdropNewItem (ItemState::ItemType type, const AbstractKart *parent, const Vec3 *server_xyz=NULL, const Vec3 *normal=NULL)
 Creates a new item at the location of the kart (e.g.
 
void update (int ticks)
 Updates all items, and handles switching items back if the switch time is over.
 
void updateGraphics (float dt)
 Updates the graphics, called once per rendered frame.
 
void checkItemHit (AbstractKart *kart)
 Checks if any item was collected by the given kart.
 
void reset ()
 Resets all items and removes bubble gum that is stuck on the track.
 
virtual void collectedItem (ItemState *item, AbstractKart *kart)
 Set an item as collected.
 
virtual void switchItems ()
 Switches all items: boxes become bananas and vice versa for a certain amount of time (as defined in stk_config.xml).
 
bool randomItemsForArena (const AlignedArray< btTransform > &pos)
 
bool areItemsSwitched ()
 Returns true if the items are switched atm.
 
virtual void setItemConfirmationTime (std::weak_ptr< STKPeer > peer, int ticks)
 Only used in the NetworkItemManager.
 
unsigned int getNumberOfItems () const
 Returns the number of items.
 
const ItemStategetItem (unsigned int n) const
 Returns a pointer to the n-th item.
 
ItemStategetItem (unsigned int n)
 Returns a pointer to the n-th item.
 
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.
 
unsigned int insertItem (Item *item)
 Inserts the new item into the items management data structures, if possible reusing an existing, unused entry (e.g.
 

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.
 

Private Attributes

std::vector< ItemState * > m_confirmed_state
 A client stores a 'confirmed' item event state, which is based on the server data.
 
int m_confirmed_switch_ticks
 The switch ticks value at the lime of the last confirmed state.
 
int m_confirmed_state_time
 Time at which m_confirmed_state was taken.
 
std::mutex m_live_players_mutex
 Allow remove or add peer live.
 
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.
 
Synchronised< std::vector< ItemEventInfo > > m_item_events
 List of all items events.
 

Additional Inherited Members

- 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.
 
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.
 
static scene::IMesh * getItemModel (ItemState::ItemType type)
 Returns the mesh for a certain item.
 
static scene::IMesh * getItemLowResolutionModel (ItemState::ItemType type)
 Returns the low resolution mesh for a certain item.
 
static std::string getIcon (ItemState::ItemType type)
 Returns the mesh for a certain item.
 
static video::SColorf & getGlowColor (ItemState::ItemType type)
 Returns the glow color for an item.
 
- Protected Types inherited from ItemManager
typedef std::vector< ItemState * > AllItemTypes
 The vector of all items of the current track.
 
- 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.
 
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.
 
void insertItemInQuad (Item *item)
 Insert into the appropriate quad list, if there is a quad list (i.e.
 
void deleteItemInQuad (ItemState *item)
 Removes an items from the items-in-quad list only.
 
- Protected Attributes inherited from ItemManager
AllItemTypes m_all_items
 
std::vector< ItemState::ItemTypem_switch_to
 What item this item is switched to.
 
int m_switch_ticks
 Remaining time that items should remain switched.
 

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 ( )

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.

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


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