Created separate file for ItemEventInfo.

This commit is contained in:
hiker 2018-05-15 18:05:46 +10:00
parent 0d850201fb
commit ad82dca6cc
5 changed files with 180 additions and 78 deletions

54
src/items/item_event_info.cpp Executable file
View File

@ -0,0 +1,54 @@
//
// SuperTuxKart - a fun racing game with go-kart
// Copyright (C) 2018 Joerg Henrichs
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 3
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "items/item_event_info.hpp"
#include "network/network_config.hpp"
#include "network/protocols/game_protocol.hpp"
#include "network/rewind_manager.hpp"
#include "network/stk_host.hpp"
/** Loads an event from a server message. It helps encapsulate the encoding
* of events from and into a message buffer.
* \param buffer A network string with the event data.
* \param count The number of bytes read will be subtracted from this value.
*/
ItemEventInfo::ItemEventInfo(BareNetworkString *buffer, int *count)
{
m_ticks = buffer->getTime();
m_kart_id = buffer->getUInt8();
m_index = buffer->getUInt16();
*count -= 7;
} // ItemEventInfo(BareNetworkString, int *count)
//-----------------------------------------------------------------------------
/** Stores this event into a network string.
* \param buffer The network string to which the data should be appended.
*/
void ItemEventInfo::saveState(BareNetworkString *buffer)
{
assert(NetworkConfig::get()->isServer());
buffer->addTime(m_ticks).addUInt8(m_kart_id).addUInt16(m_index);
if(isNewItem())
{
}
} // saveState

114
src/items/item_event_info.hpp Executable file
View File

@ -0,0 +1,114 @@
//
// SuperTuxKart - a fun racing game with go-kart
// Copyright (C) 2018 Joerg Henrichs
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 3
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#ifndef HEADER_ITEM_EVENT_INFO_HPP
#define HEADER_ITEM_EVENT_INFO_HPP
#include "items/item.hpp"
#include "utils/vec3.hpp"
#include <assert.h>
class BareNetworkString;
// ------------------------------------------------------------------------
/** This class stores a delta, i.e. an item event (either collection of
* an item, adding a new item, or an item switch being activated). All
* those deltas will be applied to the confirmed state to get a new state.
*/
class ItemEventInfo
{
private:
/** Time at which this event happens. */
int m_ticks;
/** Index of this item in the item list. Only used when creating
* new items (e.g. bubble gum). */
int m_index;
/** The kart id that collected an item if >=0; if -1 it indicates
* a new item, and a -2 indicates a switch being used. */
int m_kart_id;
/** In case of new items the position of the new item. */
Vec3 m_xyz;
public:
/** Constructor for collecting an existing item.
* \param ticks Time of the event.
* \param item_id The index of the item that was collected.
* \param kart_id the kart that collected the item. */
ItemEventInfo(int ticks, int index, int kart_id)
: m_ticks(ticks), m_index(index), m_kart_id(kart_id)
{
assert(kart_id >= 0);
} // ItemEventInfo(collected existing item)
// --------------------------------------------------------------------
/** Constructor for creating a new item (i.e. a bubble gum is dropped).
*/
ItemEventInfo(int ticks, ItemState::ItemType type, int index,
const Vec3 &xyz)
: m_ticks(ticks), m_index(index), m_kart_id(-1), m_xyz(xyz)
{
} // ItemEventInfo(new item)
// --------------------------------------------------------------------
/** Constructor for switching items. */
ItemEventInfo(int ticks) : m_ticks(ticks), m_kart_id(-2)
{
} // ItemEventInfo(switch)
// --------------------------------------------------------------------
ItemEventInfo(BareNetworkString *buffer, int *count);
// --------------------------------------------------------------------
void saveState(BareNetworkString *buffer);
/** Returns if this event represents a new item. */
bool isNewItem() const { return m_kart_id == -1; }
// --------------------------------------------------------------------
/** Returns true if this event represents collection of an item. */
bool isItemCollection() const { return m_kart_id >= 0; }
// --------------------------------------------------------------------
/** Returns true if this event represent a switch usage. */
bool isSwitch() const { return m_kart_id == -2; }
// --------------------------------------------------------------------
/** Returns the index of this item. */
int getIndex() const { return m_index; }
// --------------------------------------------------------------------
/** Returns the time of the event in ticks. */
int getTicks() const { return m_ticks; }
// --------------------------------------------------------------------
/** Returns the id of the kart that collected an item. Only allowed
* to be called when this event is an item collection. */
int getKartId() const
{
assert(isItemCollection());
return m_kart_id;
} // getKartId
// --------------------------------------------------------------------
/** Returns the location of a new item. Only allowed to be called when
* this is a new item event. */
const Vec3& getXYZ() const
{
assert(isNewItem());
return m_xyz;
} // getXYZ
}; // class ItemEventInfo
#endif

30
src/items/network_item_manager.cpp Normal file → Executable file
View File

@ -112,8 +112,7 @@ void NetworkItemManager::collectedItem(Item *item, AbstractKart *kart)
m_item_events.lock();
m_item_events.getData().emplace_back(World::getWorld()->getTimeTicks(),
item->getItemId(),
kart->getWorldKartId(),
/*item_info*/0);
kart->getWorldKartId());
m_item_events.unlock();
ItemManager::collectedItem(item, kart);
}
@ -172,7 +171,7 @@ void NetworkItemManager::setItemConfirmationTime(int host_id, int ticks)
// entry can be deleted.
m_item_events.lock();
auto p = m_item_events.getData().begin();
while (p != m_item_events.getData().end() && p->m_ticks < min_time)
while (p != m_item_events.getData().end() && p->getTicks() < min_time)
p++;
m_item_events.getData().erase(m_item_events.getData().begin(), p);
m_item_events.unlock();
@ -215,8 +214,7 @@ BareNetworkString* NetworkItemManager::saveState()
+ sizeof(uint8_t) ) );
for (auto p : m_item_events.getData())
{
s->addTime(p.m_ticks).addUInt16(p.m_index);
if (p.m_index > -1) s->addUInt8(p.m_kart_id);
p.saveState(s);
}
m_item_events.unlock();
Log::verbose("NIM", "Including %d item update at %d",
@ -259,20 +257,12 @@ void NetworkItemManager::restoreState(BareNetworkString *buffer, int count)
{
// 1) Decode the event in the message
// ----------------------------------
int ticks = buffer->getTime();
int item_index = buffer->getUInt16();
int kart_id = -1;
count -= 6;
if(item_index>-1)
{
kart_id = buffer->getUInt8();
count --;
} // item_id>-1
ItemEventInfo iei(buffer, &count);
// 2) If the event needs to be applied, forward
// the time to the time of this event:
// --------------------------------------------
int dt = ticks - current_time;
int dt = iei.getTicks() - current_time;
// Skip an event that are 'in the past' (i.e. have been sent again by
// the server because it has not yet received confirmation from all
// clients.
@ -282,15 +272,15 @@ void NetworkItemManager::restoreState(BareNetworkString *buffer, int count)
if (dt>0) forwardTime(dt);
// TODO: apply the various events types, atm only collection is supported:
ItemState *item_state = m_confirmed_state[item_index];
if (item_index > -1)
ItemState *item_state = m_confirmed_state[iei.getIndex()];
if(iei.isItemCollection())
{
// An item on the track was collected:
AbstractKart *kart = World::getWorld()->getKart(kart_id);
m_confirmed_state[item_index]->collected(kart);
AbstractKart *kart = World::getWorld()->getKart(iei.getKartId());
m_confirmed_state[iei.getIndex()]->collected(kart);
}
current_time = ticks;
current_time = iei.getTicks();
} // while count >0
// Inform the server which events have been received.

58
src/items/network_item_manager.hpp Normal file → Executable file
View File

@ -19,6 +19,7 @@
#ifndef HEADER_NETWORK_ITEM_MANAGER_HPP
#define HEADER_NETWORK_ITEM_MANAGER_HPP
#include "items/item_event_info.hpp"
#include "items/item_manager.hpp"
#include "network/rewinder.hpp"
#include "utils/cpp2011.hpp"
@ -48,63 +49,6 @@ private:
/** Stores on the server the latest confirmed tick from each client. */
Synchronised< std::vector<int> > m_last_confirmed_item_ticks;
// ------------------------------------------------------------------------
/** This class stores a delta, i.e. an item event (either collection of
* an item, adding a new item, or an item switch being activated). All
* those deltas will be applied to the confirmed state to get a new state.
*/
class ItemEventInfo
{
public:
/** Time at which this event happens. */
int m_ticks;
/** Index of this item in the item list. Only used when creating
* new items (e.g. bubble gum). */
int m_index;
/** Additional info about the item: -1 if a switch is activated.
* Otherwise it contains information about what item the kart gets
* (e.g. banana --> anchor or bomb). */
int m_item_info;
/** The kart id that collected an item ,
* otherwise undefined. */
int m_kart_id;
/** In case of new items the position of the new item. */
Vec3 m_xyz;
/** Constructor for collecting an existing item.
* \param ticks Time of the event.
* \param item_id The index of the item that was collected.
* \param kart_id the kart that collected the item. */
ItemEventInfo(int ticks, int index, int kart_id, int item_info)
: m_ticks(ticks), m_index(index), m_kart_id(kart_id),
m_item_info(item_info)
{
} // ItemEventInfo(collected existing item)
// --------------------------------------------------------------------
/** Constructor for creating a new item (i.e. a bubble gum is dropped).
*/
ItemEventInfo(int ticks, ItemState::ItemType type, int index,
const Vec3 &xyz)
{
m_ticks = ticks;
m_index = index;
m_xyz = xyz;
} // ItemEventInfo(new item)
// --------------------------------------------------------------------
/** Constructor for switching items. */
ItemEventInfo(int ticks)
: m_ticks(ticks), m_kart_id(-1), m_item_info(-1)
{
} // ItemEventInfo(switch)
}; // class ItemEventInfo
// ------------------------------------------------------------------------
/** List of all items events. */
Synchronised< std::vector<ItemEventInfo> > m_item_events;