SuperTuxKart
Loading...
Searching...
No Matches
rewinder.hpp
1//
2// SuperTuxKart - a fun racing game with go-kart
3// Copyright (C) 2013 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_REWINDER_HPP
20#define HEADER_REWINDER_HPP
21
22#include <cassert>
23#include <functional>
24#include <string>
25#include <memory>
26#include <vector>
27
29
30enum RewinderName : char
31{
32 RN_ITEM_MANAGER = 0x01,
33 RN_KART = 0x02,
34 RN_RED_FLAG = 0x03,
35 RN_BLUE_FLAG = 0x04,
36 RN_CAKE = 0x05,
37 RN_BOWLING = 0x06,
38 RN_PLUNGER = 0x07,
39 RN_RUBBERBALL = 0x08,
40 RN_PHYSICAL_OBJ = 0x09
41};
42
43class Rewinder : public std::enable_shared_from_this<Rewinder>
44{
45protected:
46 void setUniqueIdentity(const std::string& uid) { m_unique_identity = uid; }
47private:
57 std::string m_unique_identity;
58
59public:
60 Rewinder(const std::string& ui = "") { m_unique_identity = ui; }
61
62 virtual ~Rewinder() {}
63
67 virtual void saveTransform() = 0;
68
71 virtual void computeError() = 0;
72
78 virtual BareNetworkString* saveState(std::vector<std::string>* ru) = 0;
79
83 virtual void undoEvent(BareNetworkString *buffer) = 0;
84
88 virtual void rewindToEvent(BareNetworkString *buffer) = 0;
89
94 virtual void restoreState(BareNetworkString *buffer, int count) = 0;
95
100 virtual void undoState(BareNetworkString *buffer) = 0;
101
102 // -------------------------------------------------------------------------
104 virtual void reset() {}
105 // -------------------------------------------------------------------------
106 virtual std::function<void()> getLocalStateRestoreFunction()
107 { return nullptr; }
108 // -------------------------------------------------------------------------
109 const std::string& getUniqueIdentity() const
110 {
111 assert(!m_unique_identity.empty() && m_unique_identity.size() < 255);
112 return m_unique_identity;
113 }
114 // -------------------------------------------------------------------------
115 bool rewinderAdd();
116 // -------------------------------------------------------------------------
117 template<typename T> std::shared_ptr<T> getShared()
118 { return std::dynamic_pointer_cast<T>(shared_from_this()); }
119
120}; // Rewinder
121#endif
122
Describes a chain of 8-bit unsigned integers.
Definition: network_string.hpp:53
Definition: rewinder.hpp:44
virtual void saveTransform()=0
Called before a rewind.
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 BareNetworkString * saveState(std::vector< std::string > *ru)=0
Provides a copy of the state of the object in one memory buffer.
virtual void reset()
Nothing to do here.
Definition: rewinder.hpp:104
bool rewinderAdd()
Add this object to the list of all rewindable objects in the rewind manager.
Definition: rewinder.cpp:27
virtual void rewindToEvent(BareNetworkString *buffer)=0
Called when an event needs to be replayed.
virtual void undoEvent(BareNetworkString *buffer)=0
Called when an event needs to be undone.
std::string m_unique_identity
Currently it has 2 usages:
Definition: rewinder.hpp:57
virtual void computeError()=0
Called when a rewind is finished, and is used to compute the error caused by the rewind (which is the...
virtual void restoreState(BareNetworkString *buffer, int count)=0
Called when a state needs to be replayed.