From 7c598df8d15bbd8426e917f3c1c0a79f16b13e90 Mon Sep 17 00:00:00 2001 From: hiker Date: Tue, 9 Aug 2016 17:46:06 +1000 Subject: [PATCH] Save powerup state in kart status. --- src/items/powerup.cpp | 45 +++++++++++++++++++++++++++++++++++++ src/items/powerup.hpp | 6 ++++- src/karts/kart_rewinder.cpp | 10 ++++++++- 3 files changed, 59 insertions(+), 2 deletions(-) diff --git a/src/items/powerup.cpp b/src/items/powerup.cpp index 930c8c1bb..f5fadd687 100644 --- a/src/items/powerup.cpp +++ b/src/items/powerup.cpp @@ -69,6 +69,45 @@ void Powerup::reset() set( (PowerupManager::PowerupType)type, number ); } // reset +//----------------------------------------------------------------------------- +/** Save the powerup state. Called from the kart rewinder when saving the kart + * state or when a new powerup even is saved. + * \param buffer The buffer into which to save the state. + */ +void Powerup::saveState(BareNetworkString *buffer) +{ + buffer->addUInt8(uint8_t(m_type)); + if(m_type!=PowerupManager::POWERUP_NOTHING) + { + buffer->addUInt8(m_number); // number is <=255 + } +} // saveState + +//----------------------------------------------------------------------------- +/** Restore a powerup state. Called from the kart rewinder when restoring a + * state. + * \param buffer Buffer with the state of this powerup object. + */ +void Powerup::rewindTo(BareNetworkString *buffer) +{ + PowerupManager::PowerupType new_type = + PowerupManager::PowerupType(buffer->getUInt8()); + int n=0; + if(new_type==PowerupManager::POWERUP_NOTHING) + { + set(new_type, 0); + return; + } + n = buffer->getUInt8(); + if(m_type == new_type) + m_number = n; + else + { + m_number = 0; + set(new_type, n); + } +} // rewindTo + //----------------------------------------------------------------------------- /** Sets the collected items. The number of items is increased if the same * item is currently collected, otherwise replaces the existing item. It also @@ -81,9 +120,15 @@ void Powerup::set(PowerupManager::PowerupType type, int n) if (m_type==type) { m_number+=n; + // Limit to 255 (save space in network state saving) + if(m_number>255) m_number = 255; return; } m_type=type; + + // Limit to 255 (save space in network state saving) + if(n>255) n = 255; + m_number=n; if(m_sound_use != NULL) diff --git a/src/items/powerup.hpp b/src/items/powerup.hpp index aa4e117e9..bbe07ed64 100644 --- a/src/items/powerup.hpp +++ b/src/items/powerup.hpp @@ -26,6 +26,7 @@ #include "utils/random_generator.hpp" class AbstractKart; +class BareNetworkString; class Item; class SFXBase; @@ -56,9 +57,12 @@ public: void set (PowerupManager::PowerupType _type, int n=1); void reset (); Material* getIcon () const; - void adjustSound (); + void adjustSound (); void use (); void hitBonusBox (const Item &item, int newC=-1); + void saveState(BareNetworkString *buffer); + void rewindTo(BareNetworkString *buffer); + /** Returns the number of powerups. */ int getNum () const {return m_number;} diff --git a/src/karts/kart_rewinder.cpp b/src/karts/kart_rewinder.cpp index cc4d1f836..52ead37d2 100644 --- a/src/karts/kart_rewinder.cpp +++ b/src/karts/kart_rewinder.cpp @@ -19,6 +19,7 @@ #include "karts/kart_rewinder.hpp" #include "karts/abstract_kart.hpp" #include "items/attachment.hpp" +#include "items/powerup.hpp" #include "modes/world.hpp" #include "network/rewind_manager.hpp" #include "network/network_string.hpp" @@ -50,7 +51,7 @@ void KartRewinder::reset() */ BareNetworkString* KartRewinder::saveState() const { - const int MEMSIZE = 13*sizeof(float) + 9; + const int MEMSIZE = 13*sizeof(float) + 9+2; BareNetworkString *buffer = new BareNetworkString(MEMSIZE); const btRigidBody *body = m_kart->getBody(); @@ -71,6 +72,10 @@ BareNetworkString* KartRewinder::saveState() const // 3) Attachment // ------------- m_kart->getAttachment()->saveState(buffer); + + // 4) Powerup + // ---------- + m_kart->getPowerup()->saveState(buffer); return buffer; } // saveState @@ -98,6 +103,9 @@ void KartRewinder::rewindToState(BareNetworkString *buffer) // ------------- m_kart->getAttachment()->rewindTo(buffer); + // 4) Powerup + // ---------- + m_kart->getPowerup()->rewindTo(buffer); return; } // rewindToState