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 34 /** \ingroup items 35 * The network item manager is responsible for handling all network related 36 * item manager tasks - synchronisation between clients and servers. It 37 * maintains one 'confirmed' state on the clients, based on the latest 38 * server update. The server sends updates that only contains the delta 39 * between the last confirmed and the current server state. Eash client 40 * confirms to the server which deltas it has received. Once all clients 41 * have received a delta, the server will remove it from the list of 42 * deltas. 43 */ 44 class NetworkItemManager : public Rewinder, public ItemManager 45 { 46 private: 47 48 /** A client stores a 'confirmed' item event state, which is based on the 49 * server data. This is used in case of rewind. */ 50 std::vector<ItemState*> m_confirmed_state; 51 52 /** The switch ticks value at the lime of the last confirmed state. */ 53 int m_confirmed_switch_ticks; 54 55 /** Time at which m_confirmed_state was taken. */ 56 int m_confirmed_state_time; 57 58 /** Allow remove or add peer live. */ 59 std::mutex m_live_players_mutex; 60 61 /** Stores on the server the latest confirmed tick from each client. */ 62 std::map<std::weak_ptr<STKPeer>, int32_t, 63 std::owner_less<std::weak_ptr<STKPeer> > > m_last_confirmed_item_ticks; 64 65 /** List of all items events. */ 66 Synchronised< std::vector<ItemEventInfo> > m_item_events; 67 68 void forwardTime(int ticks); 69 public: 70 71 static bool m_network_item_debugging; 72 73 NetworkItemManager(); 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 // ------------------------------------------------------------------------ rewindToEvent(BareNetworkString * bns)88 virtual void rewindToEvent(BareNetworkString *bns) OVERRIDE {}; 89 // ------------------------------------------------------------------------ saveTransform()90 virtual void saveTransform() OVERRIDE {}; 91 // ------------------------------------------------------------------------ computeError()92 virtual void computeError() OVERRIDE {}; 93 // ------------------------------------------------------------------------ undoState(BareNetworkString * buffer)94 virtual void undoState(BareNetworkString *buffer) OVERRIDE {}; 95 // ------------------------------------------------------------------------ undoEvent(BareNetworkString *)96 virtual void undoEvent(BareNetworkString*) OVERRIDE {}; 97 // ------------------------------------------------------------------------ addLiveJoinPeer(std::weak_ptr<STKPeer> peer)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 // ------------------------------------------------------------------------ erasePeerInGame(std::weak_ptr<STKPeer> peer)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 119