SuperTuxKart
network_item_manager.hpp
1 //
2 // SuperTuxKart - a fun racing game with go-kart
3 // Copyright (C) 2018 Joerg Henrichs
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 3
8 // of the License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 
19 #ifndef HEADER_NETWORK_ITEM_MANAGER_HPP
20 #define HEADER_NETWORK_ITEM_MANAGER_HPP
21 
22 #include "items/item_event_info.hpp"
23 #include "items/item_manager.hpp"
24 #include "network/rewinder.hpp"
25 #include "utils/cpp2011.hpp"
26 #include "utils/synchronised.hpp"
27 
28 #include <map>
29 #include <memory>
30 #include <mutex>
31 
32 class STKPeer;
33 
44 class NetworkItemManager : public Rewinder, public ItemManager
45 {
46 private:
47 
50  std::vector<ItemState*> m_confirmed_state;
51 
54 
57 
60 
62  std::map<std::weak_ptr<STKPeer>, int32_t,
63  std::owner_less<std::weak_ptr<STKPeer> > > m_last_confirmed_item_ticks;
64 
67 
68  void forwardTime(int ticks);
69 public:
70 
71  static bool m_network_item_debugging;
72 
74  virtual ~NetworkItemManager();
75  virtual void reset() OVERRIDE;
76  virtual void setItemConfirmationTime(std::weak_ptr<STKPeer> peer,
77  int ticks) OVERRIDE;
78  virtual void collectedItem(ItemState *item, AbstractKart *kart) OVERRIDE;
79  virtual void switchItems() OVERRIDE;
80  virtual Item* dropNewItem(ItemState::ItemType type,
81  const AbstractKart *kart,
82  const Vec3 *server_xyz = NULL,
83  const Vec3 *server_normal = NULL) OVERRIDE;
84  virtual BareNetworkString* saveState(std::vector<std::string>* ru)
85  OVERRIDE;
86  virtual void restoreState(BareNetworkString *buffer, int count) OVERRIDE;
87  // ------------------------------------------------------------------------
88  virtual void rewindToEvent(BareNetworkString *bns) OVERRIDE {};
89  // ------------------------------------------------------------------------
90  virtual void saveTransform() OVERRIDE {};
91  // ------------------------------------------------------------------------
92  virtual void computeError() OVERRIDE {};
93  // ------------------------------------------------------------------------
94  virtual void undoState(BareNetworkString *buffer) OVERRIDE {};
95  // ------------------------------------------------------------------------
96  virtual void undoEvent(BareNetworkString*) OVERRIDE {};
97  // ------------------------------------------------------------------------
98  void addLiveJoinPeer(std::weak_ptr<STKPeer> peer)
99  {
100  std::lock_guard<std::mutex> lock(m_live_players_mutex);
101  m_last_confirmed_item_ticks[peer] = 0;
102  }
103  // ------------------------------------------------------------------------
104  void erasePeerInGame(std::weak_ptr<STKPeer> peer)
105  {
106  std::lock_guard<std::mutex> lock(m_live_players_mutex);
107  m_last_confirmed_item_ticks.erase(peer);
108  }
109  // ------------------------------------------------------------------------
110  void saveCompleteState(BareNetworkString* buffer) const;
111  // ------------------------------------------------------------------------
112  void restoreCompleteState(const BareNetworkString& buffer);
113  // ------------------------------------------------------------------------
114  void initServer();
115 
116 }; // NetworkItemManager
117 
118 #endif
virtual Item * dropNewItem(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.
Definition: network_item_manager.cpp:140
void initServer()
If this is a server, initializing the peers in game.
Definition: network_item_manager.cpp:50
virtual ~NetworkItemManager()
Destructor.
Definition: network_item_manager.cpp:67
A wrapper around bullets btVector3 to include conventient conversion functions (e.g.
Definition: vec3.hpp:34
void restoreCompleteState(const BareNetworkString &buffer)
Restore all current items at current ticks in client for live join or at the start of a race...
Definition: network_item_manager.cpp:543
Definition: rewinder.hpp:43
A variable that is automatically synchronised using pthreads mutex.
Definition: synchronised.hpp:27
virtual void restoreState(BareNetworkString *buffer, int count) OVERRIDE
Restores the state of the items to the current world time.
Definition: network_item_manager.cpp:278
void saveCompleteState(BareNetworkString *buffer) const
Save all current items at current ticks in server for live join.
Definition: network_item_manager.cpp:522
virtual void reset() OVERRIDE
Nothing to do here.
Definition: network_item_manager.cpp:76
NetworkItemManager()
Creates a new instance of the item manager.
Definition: network_item_manager.cpp:39
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...
Definition: network_item_manager.cpp:171
Definition: item.hpp:323
int m_confirmed_state_time
Time at which m_confirmed_state was taken.
Definition: network_item_manager.hpp:56
virtual void collectedItem(ItemState *item, AbstractKart *kart) OVERRIDE
Called when a kart collects an item.
Definition: network_item_manager.cpp:88
virtual void undoEvent(BareNetworkString *) OVERRIDE
Called when an event needs to be undone.
Definition: network_item_manager.hpp:96
virtual void saveTransform() OVERRIDE
Called before a rewind.
Definition: network_item_manager.hpp:90
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.
Definition: network_item_manager.hpp:63
Synchronised< std::vector< ItemEventInfo > > m_item_events
List of all items events.
Definition: network_item_manager.hpp:66
virtual void rewindToEvent(BareNetworkString *bns) OVERRIDE
Called when an event needs to be replayed.
Definition: network_item_manager.hpp:88
Describes a chain of 8-bit unsigned integers.
Definition: network_string.hpp:52
Definition: item_manager.hpp:46
std::mutex m_live_players_mutex
Allow remove or add peer live.
Definition: network_item_manager.hpp:59
Represents a peer. This class is used to interface the ENetPeer structure.
Definition: stk_peer.hpp:75
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)...
Definition: network_item_manager.hpp:94
virtual BareNetworkString * saveState(std::vector< std::string > *ru) OVERRIDE
Saves the state of all items.
Definition: network_item_manager.cpp:222
std::vector< ItemState * > m_confirmed_state
A client stores a &#39;confirmed&#39; item event state, which is based on the server data.
Definition: network_item_manager.hpp:50
void forwardTime(int ticks)
Progresses the time for all item by the given number of ticks.
Definition: network_item_manager.cpp:252
Contains the state information of an item, i.e.
Definition: item.hpp:52
An abstract interface for the actual karts.
Definition: abstract_kart.hpp:61
int m_confirmed_switch_ticks
The switch ticks value at the lime of the last confirmed state.
Definition: network_item_manager.hpp:53
virtual void computeError() OVERRIDE
Called when a rewind is finished, and is used to compute the error caused by the rewind (which is the...
Definition: network_item_manager.hpp:92
The network item manager is responsible for handling all network related item manager tasks - synchro...
Definition: network_item_manager.hpp:44
virtual void switchItems() OVERRIDE
Called when a switch is activated.
Definition: network_item_manager.cpp:117