SuperTuxKart
rewind_info.hpp
1 //
2 // SuperTuxKart - a fun racing game with go-kart
3 // Copyright (C) 2016 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_REWIND_INFO_HPP
20 #define HEADER_REWIND_INFO_HPP
21 
22 #include "network/event_rewinder.hpp"
24 #include "utils/cpp2011.hpp"
25 #include "utils/leak_check.hpp"
26 #include "utils/ptr_vector.hpp"
27 
28 #include <assert.h>
29 #include <functional>
30 #include <string>
31 #include <vector>
32 
45 {
46 private:
47  LEAK_CHECK();
48 
50  int m_ticks;
51 
56 
57 public:
58  RewindInfo(int ticks, bool is_confirmed);
59 
60  void setTicks(int ticks);
61 
63  virtual void undo() = 0;
65  virtual void restore() = 0;
68  virtual void replay() = 0;
69  // ------------------------------------------------------------------------
70  virtual ~RewindInfo() { }
71  // ------------------------------------------------------------------------
73  int getTicks() const { return m_ticks; }
74  // ------------------------------------------------------------------------
76  void setConfirmed(bool b) { m_is_confirmed = b; }
77  // ------------------------------------------------------------------------
79  bool isConfirmed() const { return m_is_confirmed; }
80  // ------------------------------------------------------------------------
82  virtual bool isEvent() const { return false; }
83  // ------------------------------------------------------------------------
85  virtual bool isState() const { return false; }
86  // ------------------------------------------------------------------------
87 }; // RewindInfo
88 
89 // ============================================================================
93 {
94 private:
95  std::vector<std::string> m_rewinder_using;
96 
97  int m_start_offset;
98 
101 
102 public:
103  // ------------------------------------------------------------------------
104  RewindInfoState(int ticks, int start_offset,
105  std::vector<std::string>& rewinder_using,
106  std::vector<uint8_t>& buffer);
107  // ------------------------------------------------------------------------
108  RewindInfoState(int ticks, BareNetworkString *buffer, bool is_confirmed);
109  // ------------------------------------------------------------------------
110  virtual ~RewindInfoState() { delete m_buffer; }
111  // ------------------------------------------------------------------------
112  virtual void restore();
113  // ------------------------------------------------------------------------
115  BareNetworkString *getBuffer() const { return m_buffer; }
116  // ------------------------------------------------------------------------
117  virtual bool isState() const { return true; }
118  // ------------------------------------------------------------------------
121  virtual void undo()
122  {
123  // Nothing being done in case of an undo that goes further back
124  } // undo
125  // ------------------------------------------------------------------------
128  virtual void replay()
129  {
130  assert(false);
131  } // replay
132 }; // class RewindInfoState
133 
134 // ============================================================================
136 {
137 private:
140 
143 public:
144  RewindInfoEvent(int ticks, EventRewinder *event_rewinder,
145  BareNetworkString *buffer, bool is_confirmed);
146  virtual ~RewindInfoEvent()
147  {
148  delete m_buffer;
149  } // ~RewindInfoEvent
150 
151  // ------------------------------------------------------------------------
153  void restore() {}
154  // ------------------------------------------------------------------------
155  virtual bool isEvent() const { return true; }
156  // ------------------------------------------------------------------------
159  virtual void undo()
160  {
161  m_buffer->reset();
163  } // undo
164  // ------------------------------------------------------------------------
168  virtual void replay()
169  {
170  // Make sure to reset the buffer so we read from the beginning
171  m_buffer->reset();
173  } // rewind
174  // ------------------------------------------------------------------------
177 }; // class RewindIndoEvent
178 
179 
180 // ============================================================================
182 {
183 private:
184  const std::function<void()> m_undo_function, m_replay_function,
185  m_destroy_function;
186 public:
187  RewindInfoEventFunction(int ticks,
188  std::function<void()> undo_function = [](){},
189  std::function<void()> replay_function = [](){},
190  std::function<void()> destroy_function = [](){})
191  : RewindInfo(ticks, true/*is_confirmed*/),
192  m_undo_function(undo_function), m_replay_function(replay_function),
193  m_destroy_function(destroy_function) {}
194  // ------------------------------------------------------------------------
195  ~RewindInfoEventFunction() { m_destroy_function(); }
196  // ------------------------------------------------------------------------
198  void restore() {}
199  // ------------------------------------------------------------------------
200  virtual bool isEvent() const { return true; }
201  // ------------------------------------------------------------------------
202  virtual void undo() { m_undo_function(); }
203  // ------------------------------------------------------------------------
204  virtual void replay() { m_replay_function(); }
205 }; // class RewindInfoEventFunction
206 
207 #endif
Describes a chain of 8-bit unsigned integers.
Definition: network_string.hpp:53
void reset()
Allows one to read a buffer from the beginning again.
Definition: network_string.hpp:147
A simple class that defines an interface to event rewinding: an undo() function when going back in ti...
Definition: event_rewinder.hpp:29
virtual void undo(BareNetworkString *buffer)=0
Called when an event needs to be undone.
virtual void rewind(BareNetworkString *buffer)=0
Called when an event needs to be replayed.
Definition: rewind_info.hpp:182
virtual void undo()
Called when going back in time to undo any rewind information.
Definition: rewind_info.hpp:202
virtual void replay()
This is called while going forwards in time again to reach current time.
Definition: rewind_info.hpp:204
virtual bool isEvent() const
If this RewindInfo is an event.
Definition: rewind_info.hpp:200
void restore()
An event is never 'restored', it is only rewound.
Definition: rewind_info.hpp:198
Definition: rewind_info.hpp:136
EventRewinder * m_event_rewinder
Pointer to the event rewinder responsible for this event.
Definition: rewind_info.hpp:139
virtual bool isEvent() const
If this RewindInfo is an event.
Definition: rewind_info.hpp:155
BareNetworkString * getBuffer()
Returns the buffer with the event information in it.
Definition: rewind_info.hpp:176
virtual void replay()
This is called while going forwards in time again to reach current time.
Definition: rewind_info.hpp:168
void restore()
An event is never 'restored', it is only rewound.
Definition: rewind_info.hpp:153
BareNetworkString * m_buffer
Buffer with the event data.
Definition: rewind_info.hpp:142
virtual void undo()
Called when going back in time to undo any rewind information.
Definition: rewind_info.hpp:159
A class that stores a game state and can rewind it.
Definition: rewind_info.hpp:93
virtual void replay()
Called when rewinding from a past state to 'now'.
Definition: rewind_info.hpp:128
virtual void undo()
Called when going back in time to undo any rewind information.
Definition: rewind_info.hpp:121
BareNetworkString * getBuffer() const
Returns a pointer to the state buffer.
Definition: rewind_info.hpp:115
virtual bool isState() const
If this RewindInfo is an event.
Definition: rewind_info.hpp:117
BareNetworkString * m_buffer
Pointer to the buffer which stores all states.
Definition: rewind_info.hpp:100
virtual void restore()
Rewinds to this state.
Definition: rewind_info.cpp:77
Used to store rewind information for a given time for all rewind instances.
Definition: rewind_info.hpp:45
virtual bool isEvent() const
If this RewindInfo is an event.
Definition: rewind_info.hpp:82
void setTicks(int ticks)
Adjusts the time of this RewindInfo.
Definition: rewind_info.cpp:42
virtual void replay()=0
This is called while going forwards in time again to reach current time.
RewindInfo(int ticks, bool is_confirmed)
Constructor for a state: it only takes the size, and allocates a buffer for all state info.
Definition: rewind_info.cpp:31
int m_ticks
Time when this RewindInfo was taken.
Definition: rewind_info.hpp:50
virtual void undo()=0
Called when going back in time to undo any rewind information.
bool isConfirmed() const
Returns if this RewindInfo is confirmed.
Definition: rewind_info.hpp:79
virtual bool isState() const
If this RewindInfo is an event.
Definition: rewind_info.hpp:85
int getTicks() const
Returns the time at which this RewindInfo was saved.
Definition: rewind_info.hpp:73
void setConfirmed(bool b)
Sets if this RewindInfo is confirmed or not.
Definition: rewind_info.hpp:76
virtual void restore()=0
This is called to restore a state before replaying the events.
bool m_is_confirmed
A confirmed event is one that was sent from the server.
Definition: rewind_info.hpp:55
Defines functions to easily manipulate 8-bit network destinated strings.