Renamed getState to saveState, and added support for saving a kart's

attachmend in a state.
This commit is contained in:
hiker
2016-08-08 10:33:12 +10:00
parent 40697ab5f7
commit b3f932a0e3
6 changed files with 67 additions and 15 deletions

View File

@@ -216,6 +216,49 @@ void Attachment::clear()
m_kart->updateWeight();
} // clear
// -----------------------------------------------------------------------------
void Attachment::saveState(BareNetworkString *buffer)
{
// We use bit 7 to indicate if a previous owner is defined for a bomb
assert(ATTACH_MAX<=127);
uint8_t type = m_type | (( (m_type==ATTACH_BOMB) && (m_previous_owner!=NULL) )
? 0x80 : 0 );
buffer->addUInt8(type);
if(m_type!=ATTACH_NOTHING)
{
buffer->addFloat(m_time_left);
if(m_type==ATTACH_BOMB && m_previous_owner)
buffer->addUInt8(m_previous_owner->getWorldKartId());
// m_initial_speed is not saved, on restore state it will
// be set to the kart speed, which has already been restored
}
} // saveState
// -----------------------------------------------------------------------------
void Attachment::rewindTo(BareNetworkString *buffer)
{
uint8_t type = buffer->getUInt8();
AttachmentType new_type = AttachmentType(type & 0x7f); // mask out bit 7
if(new_type==ATTACH_NOTHING)
{
clear();
}
else
{
float time_left = buffer->getFloat();
if(type== (ATTACH_BOMB | 0x80) )
{
uint8_t kart_id = buffer->getUInt8();
m_previous_owner = World::getWorld()->getKart(kart_id);
}
else
{
m_previous_owner = NULL;
}
set(new_type, time_left, m_previous_owner);
} // if something is attached
} // rewindTo
// -----------------------------------------------------------------------------
/** Randomly selects the new attachment. For a server process, the
* attachment can be passed into this function.

View File

@@ -28,6 +28,7 @@
using namespace irr;
class AbstractKart;
class BareNetworkString;
class Item;
class SFXBase;
@@ -111,6 +112,8 @@ public:
void handleCollisionWithKart(AbstractKart *other);
void set (AttachmentType type, float time,
AbstractKart *previous_kart=NULL);
void rewindTo(BareNetworkString *buffer);
void saveState(BareNetworkString *buffer);
// ------------------------------------------------------------------------
/** Sets the type of the attachment, but keeps the old time left value. */

View File

@@ -39,13 +39,15 @@ KartRewinder::KartRewinder(AbstractKart *kart) : Rewinder(/*can_be_destroyed*/ f
* \param[out] buffer Address of the memory buffer.
* \returns Size of allocated memory, or -1 in case of an error.
*/
BareNetworkString* KartRewinder::getState() const
BareNetworkString* KartRewinder::saveState() const
{
const int MEMSIZE = 13*sizeof(float) + 9;
BareNetworkString *buffer = new BareNetworkString(MEMSIZE);
const btRigidBody *body = m_kart->getBody();
// 1) Physics values: transform and velocities
// -------------------------------------------
const btTransform &t = body->getWorldTransform();
buffer->add(t.getOrigin());
btQuaternion q = t.getRotation();
@@ -53,36 +55,40 @@ BareNetworkString* KartRewinder::getState() const
buffer->add(body->getLinearVelocity());
buffer->add(body->getAngularVelocity());
// Attachment
Attachment::AttachmentType atype = m_kart->getAttachment()->getType();
//buffer->addUInt8(uint8_t(atype));
if(atype!=Attachment::ATTACH_NOTHING)
{
//buffer->addFloat(m_kart->getAttachment()->getTimeLeft());
}
// Steering information
// 2) Steering and other player controls
// -------------------------------------
m_kart->getControls().copyToBuffer(buffer);
// 3) Attachment
// -------------
m_kart->getAttachment()->saveState(buffer);
return buffer;
} // getState
} // saveState
// ----------------------------------------------------------------------------
/** Actually rewind to the specified state. */
void KartRewinder::rewindToState(BareNetworkString *buffer)
{
buffer->reset(); // make sure the buffer is read from the beginning
// 1) Physics values: transform and velocities
// -------------------------------------------
btTransform t;
t.setOrigin(buffer->getVec3());
t.setRotation(buffer->getQuat());
btRigidBody *body = m_kart->getBody();
body->proceedToTransform(t);
body->setLinearVelocity(buffer->getVec3());
body->setAngularVelocity(buffer->getVec3());
// 2) Steering and other controls
// ------------------------------
m_kart->getControls().setFromBuffer(buffer);
// 3) Attachment
// -------------
m_kart->getAttachment()->rewindTo(buffer);
return;
} // rewindToState

View File

@@ -38,7 +38,7 @@ private:
public:
KartRewinder(AbstractKart *kart);
virtual ~KartRewinder() {};
virtual BareNetworkString* getState() const;
virtual BareNetworkString* saveState() const;
virtual void rewindToState(BareNetworkString *p) OVERRIDE;
virtual void rewindToEvent(BareNetworkString *p) OVERRIDE;

View File

@@ -242,7 +242,7 @@ void RewindManager::saveStates()
// For now always create a snapshot.
for(unsigned int i=0; i<m_all_rewinder.size(); i++)
{
BareNetworkString *buffer = m_all_rewinder[i]->getState();
BareNetworkString *buffer = m_all_rewinder[i]->saveState();
if(buffer && buffer->size()>=0)
{
m_overall_state_size += buffer->size();

View File

@@ -34,7 +34,7 @@ public:
* \param[out] buffer The address of the memory buffer with the state.
* \return Size of the buffer, or -1 in case of an error.
*/
virtual BareNetworkString* getState() const = 0;
virtual BareNetworkString* saveState() const = 0;
/** Called when an event needs to be undone. This is called while going
* backwards for rewinding - all stored events will get an 'undo' call.