Cleanup unnecessary states and events (i.e. everything up to

the last confirmed state).
This commit is contained in:
hiker 2018-04-19 23:54:36 +10:00
parent 8c420d68d7
commit 5990e172b6
3 changed files with 50 additions and 1 deletions

View File

@ -94,7 +94,7 @@ private:
public:
RewindInfoState(int ticks, BareNetworkString *buffer,
bool is_confirmed);
virtual ~RewindInfoState() {};
virtual ~RewindInfoState() { delete m_buffer; };
virtual void rewind();
// ------------------------------------------------------------------------

View File

@ -77,6 +77,7 @@ void RewindQueue::reset()
m_all_rewind_info.clear();
m_current = m_all_rewind_info.end();
m_latest_confirmed_state_time = -1;
} // reset
// ----------------------------------------------------------------------------
@ -141,6 +142,10 @@ void RewindQueue::addLocalState(BareNetworkString *buffer,
RewindInfo *ri = new RewindInfoState(ticks, buffer, confirmed);
assert(ri);
insertRewindInfo(ri);
if (confirmed && m_latest_confirmed_state_time < ticks)
{
cleanupOldRewindInfo(ticks);
}
} // addLocalState
// ----------------------------------------------------------------------------
@ -209,6 +214,7 @@ void RewindQueue::mergeNetworkData(int world_ticks, bool *needs_rewind,
// FIXME: making m_network_events sorted would prevent the need to
// go through the whole list of events
int latest_confirmed_state = -1;
AllNetworkRewindInfo::iterator i = m_network_events.getData().begin();
while (i != m_network_events.getData().end())
{
@ -256,13 +262,41 @@ void RewindQueue::mergeNetworkData(int world_ticks, bool *needs_rewind,
*rewind_ticks = (*i)->getTicks();
} // if client and ticks < world_ticks
if ((*i)->isState() && (*i)->getTicks() > latest_confirmed_state &&
(*i)->isConfirmed())
{
latest_confirmed_state = (*i)->getTicks();
}
i = m_network_events.getData().erase(i);
} // for i in m_network_events
m_network_events.unlock();
if (latest_confirmed_state > m_latest_confirmed_state_time)
{
cleanupOldRewindInfo(latest_confirmed_state);
}
} // mergeNetworkData
// ----------------------------------------------------------------------------
/** Deletes all states and event before the given time.
* \param ticks Time (in ticks).
*/
void RewindQueue::cleanupOldRewindInfo(int ticks)
{
auto i = m_all_rewind_info.begin();
while ( (*i)->getTicks() < ticks)
{
if (m_current == i) next();
delete *i;
i = m_all_rewind_info.erase(i);
}
} // cleanupOldRewindInfo
// ----------------------------------------------------------------------------
bool RewindQueue::isEmpty() const
{
@ -437,4 +471,14 @@ void RewindQueue::unitTesting()
b1.next();
assert(b1.m_current == b1.m_all_rewind_info.end());
// 3) Test that if cleanupOldRewindInfo is called, it will if necessary
// adjust m_current to point to the latest confirmed state.
RewindQueue b2;
b2.addNetworkState(NULL, 1);
b2.addNetworkState(NULL, 2);
b2.addNetworkState(NULL, 3);
b2.mergeNetworkData(4, &needs_rewind, &rewind_ticks);
assert((*b2.m_current)->getTicks() == 3);
} // unitTesting

View File

@ -53,7 +53,12 @@ private:
/** Iterator to the curren time step info to be handled. */
AllRewindInfo::iterator m_current;
/** Time at which the latest confirmed state is at. */
int m_latest_confirmed_state_time;
void insertRewindInfo(RewindInfo *ri);
void cleanupOldRewindInfo(int ticks);
public:
static void unitTesting();