SuperTuxKart
rewind_queue.hpp
1 //
2 // SuperTuxKart - a fun racing game with go-kart
3 // Copyright (C) 2017 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_QUEUE_HPP
20 #define HEADER_REWIND_QUEUE_HPP
21 
22 #include "utils/synchronised.hpp"
23 
24 #include <assert.h>
25 #include <list>
26 #include <vector>
27 
28 class BareNetworkString;
29 class EventRewinder;
30 class RewindInfo;
31 class TimeStepInfo;
32 
37 {
38 private:
39 
40  typedef std::list<RewindInfo*> AllRewindInfo;
41 
42  AllRewindInfo m_all_rewind_info;
43 
49  typedef std::vector<RewindInfo*> AllNetworkRewindInfo;
50  Synchronised<AllNetworkRewindInfo> m_network_events;
51 
53  AllRewindInfo::iterator m_current;
54 
57 
58 
59  void cleanupOldRewindInfo(int ticks);
60 
61 public:
62  static void unitTesting();
63 
64  RewindQueue();
65  ~RewindQueue();
66  void reset();
67  void addLocalEvent(EventRewinder *event_rewinder, BareNetworkString *buffer,
68  bool confirmed, int ticks);
69  void addLocalState(BareNetworkString *buffer, bool confirmed, int ticks);
70  void addNetworkEvent(EventRewinder *event_rewinder,
71  BareNetworkString *buffer, int ticks);
72  void addNetworkState(BareNetworkString *buffer, int ticks);
73  void addNetworkRewindInfo(RewindInfo* ri)
74  {
75  m_network_events.lock();
76  m_network_events.getData().push_back(ri);
77  m_network_events.unlock();
78  }
79  void mergeNetworkData(int world_ticks, bool *needs_rewind,
80  int *rewind_ticks);
81  void replayAllEvents(int ticks);
82  bool isEmpty() const;
83  bool hasMoreRewindInfo() const;
84  int undoUntil(int undo_ticks);
85  void insertRewindInfo(RewindInfo *ri);
86 
87  // ------------------------------------------------------------------------
90  {
92  }
93  // ------------------------------------------------------------------------
96  void next()
97  {
98  assert(m_current != m_all_rewind_info.end());
99  m_current++;
100  return;
101  } // operator++
102 
103  // ------------------------------------------------------------------------
107  {
108  return (m_current != m_all_rewind_info.end() ) ? *m_current : NULL;
109  } // getNext
110 
111 }; // RewindQueue
112 
113 
114 #endif
115 
Describes a chain of 8-bit unsigned integers.
Definition: network_string.hpp:53
A simple class that defines an interface to event rewinding: an undo() function when going back in ti...
Definition: event_rewinder.hpp:29
Used to store rewind information for a given time for all rewind instances.
Definition: rewind_info.hpp:45
Definition: rewind_queue.hpp:37
void insertRewindInfo(RewindInfo *ri)
Inserts a RewindInfo object in the list of all events at the correct time.
Definition: rewind_queue.cpp:95
RewindInfo * getCurrent()
Returns the current RewindInfo.
Definition: rewind_queue.hpp:106
void addLocalState(BareNetworkString *buffer, bool confirmed, int ticks)
Adds a state from the local simulation to the last created TimeStepInfo container with the current wo...
Definition: rewind_queue.cpp:142
static void unitTesting()
Unit tests for RewindQueue.
Definition: rewind_queue.cpp:417
void addLocalEvent(EventRewinder *event_rewinder, BareNetworkString *buffer, bool confirmed, int ticks)
Adds an event to the rewind data.
Definition: rewind_queue.cpp:122
void reset()
Frees all saved state information and all destroyable rewinder.
Definition: rewind_queue.cpp:64
std::vector< RewindInfo * > AllNetworkRewindInfo
The list of all events received from the network.
Definition: rewind_queue.hpp:49
void mergeNetworkData(int world_ticks, bool *needs_rewind, int *rewind_ticks)
Merges thread-safe all data received from the network up to and including the current time (tick) wit...
Definition: rewind_queue.cpp:201
RewindQueue()
The RewindQueue stores one TimeStepInfo for each time step done.
Definition: rewind_queue.cpp:46
int getLatestConfirmedState() const
Returns the time of the latest confirmed state.
Definition: rewind_queue.hpp:89
void cleanupOldRewindInfo(int ticks)
Deletes all states and event before the given time.
Definition: rewind_queue.cpp:328
int undoUntil(int undo_ticks)
Rewinds the rewind queue and undos all events/states stored.
Definition: rewind_queue.cpp:366
AllRewindInfo::iterator m_current
Iterator to the curren time step info to be handled.
Definition: rewind_queue.hpp:53
void addNetworkState(BareNetworkString *buffer, int ticks)
Adds a state to the list of network rewind data.
Definition: rewind_queue.cpp:181
bool hasMoreRewindInfo() const
Returns true if there is at least one more RewindInfo available.
Definition: rewind_queue.cpp:354
void addNetworkEvent(EventRewinder *event_rewinder, BareNetworkString *buffer, int ticks)
Adds an event to the list of network rewind data.
Definition: rewind_queue.cpp:162
void next()
Sets the current element to be the next one and returns the next RewindInfo element.
Definition: rewind_queue.hpp:96
int m_latest_confirmed_state_time
Time at which the latest confirmed state is at.
Definition: rewind_queue.hpp:56
void replayAllEvents(int ticks)
Replays all events (not states) that happened at the specified time.
Definition: rewind_queue.cpp:397
~RewindQueue()
Frees all saved state information.
Definition: rewind_queue.cpp:55
void lock() const
Locks the mutex.
Definition: synchronised.hpp:95
void unlock() const
Unlocks the mutex.
Definition: synchronised.hpp:99
TYPE & getData()
Returns a reference to the original data file.
Definition: synchronised.hpp:82