From e6b816357985e7487869c7f58142740356c27fd2 Mon Sep 17 00:00:00 2001 From: Benau Date: Fri, 26 Oct 2018 20:32:19 +0800 Subject: [PATCH] Don't add deleting flyable to state --- src/items/flyable.cpp | 32 ++++++++++++++++++++++++-------- src/items/flyable.hpp | 2 ++ src/items/plunger.cpp | 3 +++ src/items/rubber_ball.cpp | 3 +++ 4 files changed, 32 insertions(+), 8 deletions(-) diff --git a/src/items/flyable.cpp b/src/items/flyable.cpp index 6d5fd80bd..5591577a3 100644 --- a/src/items/flyable.cpp +++ b/src/items/flyable.cpp @@ -609,17 +609,32 @@ void Flyable::moveToInfinity() // ---------------------------------------------------------------------------- BareNetworkString* Flyable::saveState(std::vector* ru) { + if (m_has_hit_something) + return NULL; + ru->push_back(getUniqueIdentity()); BareNetworkString *buffer = new BareNetworkString(); CompressNetworkBody::compress(m_body->getWorldTransform(), m_body->getLinearVelocity(), m_body->getAngularVelocity(), buffer, m_body, m_motion_state); - uint16_t hit_and_ticks = (m_has_hit_something ? 1 << 15 : 0) | - m_ticks_since_thrown; - buffer->addUInt16(hit_and_ticks); + buffer->addUInt16(m_ticks_since_thrown); return buffer; } // saveState +// ---------------------------------------------------------------------------- +std::function Flyable::getLocalStateRestoreFunction() +{ + // Avoid circular reference + std::weak_ptr fw = getShared(); + return [fw]() + { + std::shared_ptr f = fw.lock(); + if (!f || f->m_has_undone_destruction) + return; + f->m_has_server_state = false; + }; +} // getLocalStateRestoreFunction + // ---------------------------------------------------------------------------- void Flyable::restoreState(BareNetworkString *buffer, int count) { @@ -639,10 +654,9 @@ void Flyable::restoreState(BareNetworkString *buffer, int count) setTrans(t); } uint16_t hit_and_ticks = buffer->getUInt16(); - m_has_hit_something = (hit_and_ticks >> 15) == 1; + // Next network version remove ~(1 << 15) m_ticks_since_thrown = hit_and_ticks & ~(1 << 15); - if (!m_has_server_state) - m_has_server_state = true; + m_has_server_state = true; } // restoreState // ---------------------------------------------------------------------------- @@ -721,10 +735,12 @@ void Flyable::handleUndoDestruction() RewindInfoEventFunction(World::getWorld()->getTicksSinceStart(), /*undo_function*/[f, uid]() { + f->m_has_hit_something = false; projectile_manager->addByUID(uid, f); }, /*replay_function*/[f, uid]() { + f->m_has_hit_something = true; projectile_manager->removeByUID(uid); f->moveToInfinity(); })); @@ -738,8 +754,8 @@ void Flyable::computeError() World::getWorld()->getTicksSinceStart() > m_check_created_ticks) { const std::string& uid = getUniqueIdentity(); - Log::warn("Flyable", "Item %s failed to be created on server, " - "remove it locally", uid.c_str()); + Log::warn("Flyable", "Item %s doesn't exist on server, " + "remove it locally.", uid.c_str()); projectile_manager->removeByUID(uid); } } // computeError diff --git a/src/items/flyable.hpp b/src/items/flyable.hpp index 91cb88af1..00aa8a4ce 100644 --- a/src/items/flyable.hpp +++ b/src/items/flyable.hpp @@ -256,6 +256,8 @@ public: // ------------------------------------------------------------------------ virtual void addRewindInfoEventFunctionAfterFiring(); // ------------------------------------------------------------------------ + virtual std::function getLocalStateRestoreFunction() OVERRIDE; + // ------------------------------------------------------------------------ bool isUndoCreation() const { return m_undo_creation; } // ------------------------------------------------------------------------ bool hasUndoneDestruction() const { return m_has_undone_destruction; } diff --git a/src/items/plunger.cpp b/src/items/plunger.cpp index 2e080a8b9..82d329556 100644 --- a/src/items/plunger.cpp +++ b/src/items/plunger.cpp @@ -254,6 +254,9 @@ void Plunger::hideNodeWhenUndoDestruction() BareNetworkString* Plunger::saveState(std::vector* ru) { BareNetworkString* buffer = Flyable::saveState(ru); + if (!buffer) + return NULL; + buffer->addUInt16(m_keep_alive).addUInt8(m_moved_to_infinity ? 1 : 0); if (m_rubber_band) buffer->addUInt8(m_rubber_band->get8BitState()); diff --git a/src/items/rubber_ball.cpp b/src/items/rubber_ball.cpp index 006340b0d..6d6df02e4 100644 --- a/src/items/rubber_ball.cpp +++ b/src/items/rubber_ball.cpp @@ -793,6 +793,9 @@ bool RubberBall::hit(AbstractKart* kart, PhysicalObject* object) BareNetworkString* RubberBall::saveState(std::vector* ru) { BareNetworkString* buffer = Flyable::saveState(ru); + if (!buffer) + return NULL; + buffer->addUInt32(m_last_aimed_graph_node); buffer->add(m_control_points[0]); buffer->add(m_control_points[1]);