Add end ticks handling to kart animation

This commit is contained in:
Benau 2018-08-19 16:27:42 +08:00
parent 7970d138e5
commit 2307005159
6 changed files with 53 additions and 22 deletions

View File

@ -42,9 +42,11 @@ AbstractKartAnimation::AbstractKartAnimation(AbstractKart *kart,
m_name = name; m_name = name;
m_end_transform = btTransform(btQuaternion(0.0f, 0.0f, 0.0f, 1.0f)); m_end_transform = btTransform(btQuaternion(0.0f, 0.0f, 0.0f, 1.0f));
m_end_transform.setOrigin(Vec3(std::numeric_limits<float>::max())); m_end_transform.setOrigin(Vec3(std::numeric_limits<float>::max()));
m_end_ticks = -1;
m_created_ticks = World::getWorld()->getTicksSinceStart(); m_created_ticks = World::getWorld()->getTicksSinceStart();
m_check_created_ticks = std::make_shared<int>(-1); m_check_created_ticks = std::make_shared<int>(-1);
m_confirmed_by_network = false; m_confirmed_by_network = false;
m_ignore_undo = false;
// Remove previous animation if there is one // Remove previous animation if there is one
#ifndef DEBUG #ifndef DEBUG
// Use this code in non-debug mode to avoid a memory leak (and messed // Use this code in non-debug mode to avoid a memory leak (and messed
@ -98,7 +100,7 @@ AbstractKartAnimation::~AbstractKartAnimation()
m_kart->setTrans(transform); m_kart->setTrans(transform);
Physics::getInstance()->addKart(m_kart); Physics::getInstance()->addKart(m_kart);
if (RewindManager::get()->useLocalEvent()) if (RewindManager::get()->useLocalEvent() && !m_ignore_undo)
{ {
AbstractKart* kart = m_kart; AbstractKart* kart = m_kart;
Vec3 angular_velocity = kart->getBody()->getAngularVelocity(); Vec3 angular_velocity = kart->getBody()->getAngularVelocity();
@ -143,7 +145,8 @@ void AbstractKartAnimation::addNetworkAnimationChecker()
} // addNetworkAnimationChecker } // addNetworkAnimationChecker
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
void AbstractKartAnimation::checkNetworkAnimationCreationSucceed() void AbstractKartAnimation::
checkNetworkAnimationCreationSucceed(const btTransform& fallback_trans)
{ {
if (!m_confirmed_by_network && *m_check_created_ticks != -1 && if (!m_confirmed_by_network && *m_check_created_ticks != -1 &&
World::getWorld()->getTicksSinceStart() > *m_check_created_ticks) World::getWorld()->getTicksSinceStart() > *m_check_created_ticks)
@ -151,6 +154,8 @@ void AbstractKartAnimation::checkNetworkAnimationCreationSucceed()
Log::warn("AbstractKartAnimation", Log::warn("AbstractKartAnimation",
"No animation has been created on server, remove locally."); "No animation has been created on server, remove locally.");
m_timer = -1.0f; m_timer = -1.0f;
m_end_transform = fallback_trans;
m_ignore_undo = true;
} }
} // checkNetworkAnimationCreationSucceed } // checkNetworkAnimationCreationSucceed

View File

@ -50,6 +50,8 @@ private:
bool m_confirmed_by_network; bool m_confirmed_by_network;
bool m_ignore_undo;
protected: protected:
/** A pointer to the kart which is animated by this class. */ /** A pointer to the kart which is animated by this class. */
AbstractKart *m_kart; AbstractKart *m_kart;
@ -59,6 +61,8 @@ protected:
btTransform m_end_transform; btTransform m_end_transform;
int m_end_ticks;
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
void addNetworkAnimationChecker(); void addNetworkAnimationChecker();
public: public:
@ -78,16 +82,18 @@ public:
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
const btTransform& getEndTransform() const { return m_end_transform; } const btTransform& getEndTransform() const { return m_end_transform; }
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
void setEndTransform(const btTransform& t) void setEndTransformTicks(const btTransform& t, int ticks)
{ {
if (!useEarlyEndTransform()) if (!useEarlyEndTransform())
return; return;
m_confirmed_by_network = true; m_confirmed_by_network = true;
m_end_transform = t; m_end_transform = t;
m_end_ticks = ticks;
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
void checkNetworkAnimationCreationSucceed(); void checkNetworkAnimationCreationSucceed(const btTransform& fallback_trans);
// ----------------------------------------------------------------------------
int getEndTicks() const { return m_end_ticks; }
}; // AbstractKartAnimation }; // AbstractKartAnimation
#endif #endif

View File

@ -23,6 +23,8 @@
#include "items/attachment.hpp" #include "items/attachment.hpp"
#include "karts/abstract_kart.hpp" #include "karts/abstract_kart.hpp"
#include "karts/kart_properties.hpp" #include "karts/kart_properties.hpp"
#include "modes/world.hpp"
#include "network/network_config.hpp"
#include "tracks/track.hpp" #include "tracks/track.hpp"
/** A static create function that does only create an explosion if /** A static create function that does only create an explosion if
@ -83,6 +85,12 @@ ExplosionAnimation::ExplosionAnimation(AbstractKart *kart,
// Non-direct hits will be only affected half as much. // Non-direct hits will be only affected half as much.
if(!direct_hit) m_timer*=0.5f; if(!direct_hit) m_timer*=0.5f;
if (NetworkConfig::get()->isNetworking() &&
NetworkConfig::get()->isServer())
{
m_end_ticks = stk_config->time2Ticks(m_timer) + World::getWorld()
->getTicksSinceStart();
}
// Half of the overall time is spent in raising, so only use // Half of the overall time is spent in raising, so only use
// half of the explosion time here. // half of the explosion time here.
// Velocity after t seconds is: // Velocity after t seconds is:
@ -108,7 +116,7 @@ ExplosionAnimation::ExplosionAnimation(AbstractKart *kart,
float t = m_kart->getKartProperties()->getExplosionInvulnerabilityTime(); float t = m_kart->getKartProperties()->getExplosionInvulnerabilityTime();
m_kart->setInvulnerableTicks(stk_config->time2Ticks(t)); m_kart->setInvulnerableTicks(stk_config->time2Ticks(t));
m_kart->showStarEffect(t); m_kart->showStarEffect(t);
m_kart->getAttachment()->clear(); m_kart->getAttachment()->clear();
addNetworkAnimationChecker(); addNetworkAnimationChecker();
} // ExplosionAnimation } // ExplosionAnimation

View File

@ -52,6 +52,8 @@ KartRewinder::KartRewinder(const std::string& ident,
*/ */
void KartRewinder::reset() void KartRewinder::reset()
{ {
m_transfrom_from_network =
btTransform(btQuaternion(0.0f, 0.0f, 0.0f, 1.0f));
Kart::reset(); Kart::reset();
Rewinder::reset(); Rewinder::reset();
SmoothNetworkBody::setEnable(true); SmoothNetworkBody::setEnable(true);
@ -86,7 +88,7 @@ void KartRewinder::computeError()
m_skidding->checkSmoothing(); m_skidding->checkSmoothing();
} }
else else
ka->checkNetworkAnimationCreationSucceed(); ka->checkNetworkAnimationCreationSucceed(m_transfrom_from_network);
float diff = fabsf(m_prev_steering - AbstractKart::getSteerPercent()); float diff = fabsf(m_prev_steering - AbstractKart::getSteerPercent());
if (diff > 0.05f) if (diff > 0.05f)
@ -136,6 +138,7 @@ BareNetworkString* KartRewinder::saveState(std::vector<std::string>* ru)
buffer->add(trans.getOrigin()); buffer->add(trans.getOrigin());
btQuaternion quat = trans.getRotation(); btQuaternion quat = trans.getRotation();
buffer->add(quat); buffer->add(quat);
buffer->addUInt32(ka->getEndTicks());
} }
else else
{ {
@ -199,19 +202,19 @@ void KartRewinder::restoreState(BareNetworkString *buffer, int count)
// 2) Kart animation status or transform and velocities // 2) Kart animation status or transform and velocities
// ----------- // -----------
btTransform t; m_transfrom_from_network.setOrigin(buffer->getVec3());
t.setOrigin(buffer->getVec3()); m_transfrom_from_network.setRotation(buffer->getQuat());
t.setRotation(buffer->getQuat());
Vec3 lv = buffer->getVec3();
Vec3 av = buffer->getVec3();
if (has_animation) if (has_animation)
{ {
int end_ticks = buffer->getUInt32();
AbstractKartAnimation* ka = getKartAnimation(); AbstractKartAnimation* ka = getKartAnimation();
if (ka) if (ka)
ka->setEndTransform(t); ka->setEndTransformTicks(m_transfrom_from_network, end_ticks);
} }
Vec3 lv = buffer->getVec3();
Vec3 av = buffer->getVec3();
// Don't restore to phyics position if showing kart animation // Don't restore to phyics position if showing kart animation
if (!getKartAnimation()) if (!getKartAnimation())
{ {
@ -222,10 +225,10 @@ void KartRewinder::restoreState(BareNetworkString *buffer, int count)
body->setAngularVelocity(av); body->setAngularVelocity(av);
// This function also reads the velocity, so it must be called // This function also reads the velocity, so it must be called
// after the velocities are set // after the velocities are set
body->proceedToTransform(t); body->proceedToTransform(m_transfrom_from_network);
// Update kart transform in case that there are access to its value // Update kart transform in case that there are access to its value
// before Moveable::update() is called (which updates the transform) // before Moveable::update() is called (which updates the transform)
setTrans(t); setTrans(m_transfrom_from_network);
} }
m_vehicle->setMinSpeed(buffer->getFloat()); m_vehicle->setMinSpeed(buffer->getFloat());

View File

@ -30,10 +30,7 @@ class KartRewinder : public Rewinder, public Kart
{ {
private: private:
// Flags to indicate the different event types btTransform m_transfrom_from_network;
enum { EVENT_CONTROL = 0x01,
EVENT_ATTACH = 0x02 };
float m_prev_steering, m_steering_smoothing_dt, m_steering_smoothing_time; float m_prev_steering, m_steering_smoothing_dt, m_steering_smoothing_time;
public: public:

View File

@ -26,6 +26,7 @@
#include "karts/kart_properties.hpp" #include "karts/kart_properties.hpp"
#include "modes/three_strikes_battle.hpp" #include "modes/three_strikes_battle.hpp"
#include "modes/world_with_rank.hpp" #include "modes/world_with_rank.hpp"
#include "network/network_config.hpp"
#include "physics/physics.hpp" #include "physics/physics.hpp"
#include "physics/triangle_mesh.hpp" #include "physics/triangle_mesh.hpp"
#include "tracks/drive_graph.hpp" #include "tracks/drive_graph.hpp"
@ -62,6 +63,13 @@ RescueAnimation::RescueAnimation(AbstractKart *kart, bool is_auto_rescue)
m_orig_rotation = m_kart->getRotation(); m_orig_rotation = m_kart->getRotation();
m_kart->getAttachment()->clear(); m_kart->getAttachment()->clear();
if (NetworkConfig::get()->isNetworking() &&
NetworkConfig::get()->isServer())
{
m_end_ticks = stk_config->time2Ticks(m_timer) + World::getWorld()
->getTicksSinceStart();
}
// Determine maximum rescue height with up-raycast // Determine maximum rescue height with up-raycast
float max_height = m_kart->getKartProperties()->getRescueHeight(); float max_height = m_kart->getKartProperties()->getRescueHeight();
float hit_dest = maximumHeight(); float hit_dest = maximumHeight();
@ -94,10 +102,14 @@ RescueAnimation::RescueAnimation(AbstractKart *kart, bool is_auto_rescue)
if (race_manager->getMinorMode()==RaceManager::MINOR_MODE_BATTLE && if (race_manager->getMinorMode()==RaceManager::MINOR_MODE_BATTLE &&
!is_auto_rescue) !is_auto_rescue)
{ {
ThreeStrikesBattle *world=(ThreeStrikesBattle*)World::getWorld(); World::getWorld()->kartHit(m_kart->getWorldKartId());
world->kartHit(m_kart->getWorldKartId());
if (UserConfigParams::m_arena_ai_stats) if (UserConfigParams::m_arena_ai_stats)
world->increaseRescueCount(); {
ThreeStrikesBattle* tsb = dynamic_cast<ThreeStrikesBattle*>
(World::getWorld());
if (tsb)
tsb->increaseRescueCount();
}
} }
addNetworkAnimationChecker(); addNetworkAnimationChecker();