Fix swatter in network

This commit is contained in:
Benau 2018-07-27 13:32:19 +08:00
parent 05f5663888
commit f0b46d08b2
9 changed files with 197 additions and 113 deletions

View File

@ -56,7 +56,7 @@ Attachment::Attachment(AbstractKart* kart)
m_previous_owner = NULL;
m_bomb_sound = NULL;
m_bubble_explode_sound = NULL;
m_node_scale = 1.0f;
m_node_scale = std::numeric_limits<float>::max();
m_initial_speed = 0.0f;
// If we attach a NULL mesh, we get a NULL scene node back. So we
@ -110,9 +110,15 @@ Attachment::~Attachment()
* previous owner exists.
*/
void Attachment::set(AttachmentType type, int ticks,
AbstractKart *current_kart)
AbstractKart *current_kart,
bool disable_swatter_animation)
{
bool was_bomb = (m_type == ATTACH_BOMB);
// Don't override currently player swatter removing bomb animation
Swatter* s = dynamic_cast<Swatter*>(m_plugin);
if (s && s->isRemovingBomb())
return;
bool was_bomb = (m_type == ATTACH_BOMB) && !disable_swatter_animation;
scene::ISceneNode* bomb_scene_node = NULL;
if (was_bomb && type == ATTACH_SWATTER)
{
@ -132,7 +138,6 @@ void Attachment::set(AttachmentType type, int ticks,
}
clear();
m_node_scale = 0.3f;
// If necessary create the appropriate plugin which encapsulates
// the associated behavior
@ -160,7 +165,12 @@ void Attachment::set(AttachmentType type, int ticks,
m_node->setCurrentFrame(0);
}
m_node->setScale(core::vector3df(m_node_scale,m_node_scale,m_node_scale));
if (m_node_scale == std::numeric_limits<float>::max())
{
m_node_scale = 0.3f;
m_node->setScale(core::vector3df(m_node_scale, m_node_scale,
m_node_scale));
}
m_type = type;
m_ticks_left = ticks;
@ -238,14 +248,22 @@ void Attachment::clear()
*/
void Attachment::saveState(BareNetworkString *buffer) const
{
// We use bit 7 to indicate if a previous owner is defined for a bomb
assert(ATTACH_MAX<=127);
// We use bit 6 to indicate if a previous owner is defined for a bomb,
// bit 7 to indicate if the attachment is swatter removing animation
assert(ATTACH_MAX < 64);
uint8_t bit_7 = 0;
Swatter* s = dynamic_cast<Swatter*>(m_plugin);
if (s)
{
bit_7 = s->isRemovingBomb() ? 1 : 0;
bit_7 <<= 7;
}
uint8_t type = m_type | (( (m_type==ATTACH_BOMB) && (m_previous_owner!=NULL) )
? 0x80 : 0 );
? (1 << 6) : 0 ) | bit_7;
buffer->addUInt8(type);
if(m_type!=ATTACH_NOTHING)
{
buffer->addUInt32(m_ticks_left);
buffer->addUInt16(m_ticks_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
@ -260,19 +278,29 @@ void Attachment::saveState(BareNetworkString *buffer) const
void Attachment::rewindTo(BareNetworkString *buffer)
{
uint8_t type = buffer->getUInt8();
AttachmentType new_type = AttachmentType(type & 0x7f); // mask out bit 7
bool is_removing_bomb = (type >> 7 & 1) == 1;
Swatter* s = dynamic_cast<Swatter*>(m_plugin);
// If locally removing a bomb
if (s)
is_removing_bomb = s->isRemovingBomb();
// mask out bit 6 and 7
AttachmentType new_type = AttachmentType(type & 63);
type &= 127;
// If there is no attachment, clear the attachment if necessary and exit
if(new_type==ATTACH_NOTHING)
if (new_type == ATTACH_NOTHING && !is_removing_bomb)
{
if(m_type!=new_type) clear();
if (m_type != new_type)
clear();
return;
}
int ticks_left = buffer->getUInt32();
int16_t ticks_left = buffer->getUInt16();
// Now it is a new attachment:
if (type == (ATTACH_BOMB | 0x80)) // we have previous owner information
if (type == (ATTACH_BOMB | 64)) // we have previous owner information
{
uint8_t kart_id = buffer->getUInt8();
m_previous_owner = World::getWorld()->getKart(kart_id);
@ -282,6 +310,10 @@ void Attachment::rewindTo(BareNetworkString *buffer)
m_previous_owner = NULL;
}
// If playing kart animation, don't rewind to any attacment
if (is_removing_bomb)
return;
// Attaching an object can be expensive (loading new models, ...)
// so avoid doing this if there is no change in attachment type
// Don't use set to reset a model on local player if it's already cleared
@ -294,7 +326,9 @@ void Attachment::rewindTo(BareNetworkString *buffer)
return;
}
set(new_type, ticks_left, m_previous_owner);
set(new_type, ticks_left, m_previous_owner,
new_type == ATTACH_SWATTER && !is_removing_bomb
/*disable_swatter_animation*/);
} // rewindTo
// -----------------------------------------------------------------------------
@ -314,9 +348,7 @@ void Attachment::rewind(BareNetworkString *buffer)
*/
void Attachment::hitBanana(ItemState *item_state)
{
// Don't keep on getting achievements due to rewind!
if (m_kart->getController()->canGetAchievements() &&
!RewindManager::get()->isRewinding())
if (m_kart->getController()->canGetAchievements())
{
PlayerManager::increaseAchievement(AchievementInfo::ACHIEVE_BANANA,
"banana", 1);
@ -501,8 +533,6 @@ void Attachment::update(int ticks)
bool is_shield = m_type == ATTACH_BUBBLEGUM_SHIELD ||
m_type == ATTACH_NOLOK_BUBBLEGUM_SHIELD;
float wanted_node_scale = is_shield ?
std::max(1.0f, m_kart->getHighestPoint() * 1.1f) : 1.0f;
int slow_flashes = stk_config->time2Ticks(3.0f);
if (is_shield && m_ticks_left < slow_flashes)
{
@ -518,16 +548,6 @@ void Attachment::update(int ticks)
m_node->setVisible((division & 0x1) == 0);
}
float dt = stk_config->ticks2Time(ticks);
if (m_node_scale < wanted_node_scale)
{
m_node_scale += dt*1.5f;
if (m_node_scale > wanted_node_scale)
m_node_scale = wanted_node_scale;
m_node->setScale(core::vector3df(m_node_scale,m_node_scale,
m_node_scale) );
}
if(m_plugin)
{
bool discard = m_plugin->updateAndTestFinished(ticks);
@ -622,6 +642,26 @@ void Attachment::update(int ticks)
// ----------------------------------------------------------------------------
void Attachment::updateGraphics(float dt)
{
if (m_plugin)
m_plugin->updateGrahpics(dt);
bool is_shield = m_type == ATTACH_BUBBLEGUM_SHIELD ||
m_type == ATTACH_NOLOK_BUBBLEGUM_SHIELD;
float wanted_node_scale = is_shield ?
std::max(1.0f, m_kart->getHighestPoint() * 1.1f) : 1.0f;
if (m_node_scale < wanted_node_scale)
{
m_node_scale += dt * 1.5f;
float scale = m_node_scale;
if (scale > wanted_node_scale)
scale = wanted_node_scale;
m_node->setScale(core::vector3df(scale, scale, scale));
}
else
{
m_node_scale = std::numeric_limits<float>::max();
}
switch (m_type)
{
case ATTACH_BOMB:
@ -644,7 +684,6 @@ void Attachment::updateGraphics(float dt)
m_bomb_sound->deleteSFX();
m_bomb_sound = NULL;
}
} // updateGraphics
// ----------------------------------------------------------------------------

View File

@ -82,7 +82,7 @@ private:
AbstractKart *m_kart;
/** Time left till attachment expires. */
int m_ticks_left;
int16_t m_ticks_left;
/** For parachutes only. */
float m_initial_speed;
@ -121,7 +121,8 @@ public:
void update(int ticks);
void handleCollisionWithKart(AbstractKart *other);
void set (AttachmentType type, int ticks,
AbstractKart *previous_kart=NULL);
AbstractKart *previous_kart=NULL,
bool disable_swatter_animation = false);
virtual void rewind(BareNetworkString *buffer);
void rewindTo(BareNetworkString *buffer);
void saveState(BareNetworkString *buffer) const;
@ -135,10 +136,10 @@ public:
// ------------------------------------------------------------------------
/** Returns how much time (in ticks) is left before this attachment is
* removed. */
int getTicksLeft() const { return m_ticks_left; }
int16_t getTicksLeft() const { return m_ticks_left; }
// ------------------------------------------------------------------------
/** Sets how long this attachment will remain attached. */
void setTicksLeft(int t){ m_ticks_left = t; }
void setTicksLeft(int16_t t) { m_ticks_left = t; }
// ------------------------------------------------------------------------
/** Returns the previous owner of this attachment, used in bombs that
* are being passed between karts. */
@ -160,10 +161,6 @@ public:
float getInitialSpeed() const { return m_initial_speed; }
// ------------------------------------------------------------------------
void setInitialSpeed(float speed) { m_initial_speed = speed; }
// ------------------------------------------------------------------------
float getNodeScale() const { return m_node_scale; }
// ------------------------------------------------------------------------
void setNodeScale(float scale) { m_node_scale = scale; }
}; // Attachment

View File

@ -49,6 +49,8 @@ public:
virtual ~AttachmentPlugin() {}
virtual void updateGrahpics(float dt) = 0;
// ------------------------------------------------------------------------
/** Updates a plugin. This is called once each time frame. If the
* function returns true, the attachment is discarded. */

View File

@ -34,12 +34,12 @@
#include "io/file_manager.hpp"
#include "items/attachment_manager.hpp"
#include "items/projectile_manager.hpp"
#include "karts/abstract_kart.hpp"
#include "karts/controller/controller.hpp"
#include "karts/explosion_animation.hpp"
#include "karts/kart_properties.hpp"
#include "modes/world.hpp"
#include "modes/soccer_world.hpp"
#include "karts/abstract_kart.hpp"
#define SWAT_POS_OFFSET core::vector3df(0.0, 0.2f, -0.4f)
#define SWAT_ANGLE_MIN 45
@ -61,10 +61,9 @@ Swatter::Swatter(AbstractKart *kart, bool was_bomb,
{
m_animation_phase = SWATTER_AIMING;
m_discard_now = false;
m_discard_timeout = 0.0f;
m_target = NULL;
m_closest_kart = NULL;
m_removing_bomb = was_bomb;
m_bomb_scene_node = bomb_scene_node;
m_swat_bomb_frame = 0.0f;
@ -72,7 +71,7 @@ Swatter::Swatter(AbstractKart *kart, bool was_bomb,
m_scene_node = kart->getAttachment()->getNode();
m_scene_node->setPosition(SWAT_POS_OFFSET);
if (m_removing_bomb)
if (was_bomb)
{
m_scene_node->setMesh(attachment_manager
->getMesh(Attachment::ATTACH_SWATTER_ANIM));
@ -80,16 +79,26 @@ Swatter::Swatter(AbstractKart *kart, bool was_bomb,
m_scene_node->setAnimationSpeed(0.9f);
m_scene_node->setCurrentFrame(0.0f);
m_scene_node->setLoopMode(false);
// There are 40 frames in blender for swatter_anim.blender
// so 40 / 25 * 120
m_removed_bomb_ticks =
World::getWorld()->getTicksSinceStart() + 192;
}
else
{
m_removed_bomb_ticks = std::numeric_limits<int>::max();
m_scene_node->setAnimationSpeed(0);
}
m_swat_sound = NULL;
m_start_swat_ticks = std::numeric_limits<int>::max();
m_end_swat_ticks = std::numeric_limits<int>::max();
#ifndef SERVER_ONLY
if (kart->getIdent() == "nolok")
m_swat_sound = SFXManager::get()->createSoundSource("hammer");
else
m_swat_sound = SFXManager::get()->createSoundSource("swatter");
#endif
} // Swatter
// ----------------------------------------------------------------------------
@ -102,12 +111,49 @@ Swatter::~Swatter()
irr_driver->removeNode(m_bomb_scene_node);
m_bomb_scene_node = NULL;
}
#ifndef SERVER_ONLY
if (m_swat_sound)
{
m_swat_sound->deleteSFX();
}
#endif
} // ~Swatter
// ----------------------------------------------------------------------------
void Swatter::updateGrahpics(float dt)
{
#ifndef SERVER_ONLY
if (m_removed_bomb_ticks != std::numeric_limits<int>::max())
{
m_swat_bomb_frame += dt*25.0f;
m_scene_node->setRotation(core::vector3df(0.0, -180.0, 0.0));
m_scene_node->setCurrentFrame(m_swat_bomb_frame);
if (m_swat_bomb_frame >= 32.5f && m_bomb_scene_node != NULL)
{
m_bomb_scene_node->setPosition(m_bomb_scene_node
->getPosition() + core::vector3df(-dt*15.0f, 0.0f, 0.0f) );
m_bomb_scene_node->setRotation(m_bomb_scene_node
->getRotation() + core::vector3df(-dt*15.0f, 0.0f, 0.0f) );
}
if (m_swat_bomb_frame >= m_scene_node->getEndFrame())
{
return;
}
else if (m_swat_bomb_frame >= 35)
{
if (m_bomb_scene_node != NULL)
{
irr_driver->removeNode(m_bomb_scene_node);
m_bomb_scene_node = NULL;
}
} // bom_frame > 35
} // if removing bomb
#endif
} // updateGrahpics
// ----------------------------------------------------------------------------
/** Updates an armed swatter: it checks for any karts that are close enough
* and not invulnerable, it swats the kart.
@ -116,41 +162,16 @@ Swatter::~Swatter()
*/
bool Swatter::updateAndTestFinished(int ticks)
{
float dt = stk_config->ticks2Time(ticks);
if (m_removed_bomb_ticks != std::numeric_limits<int>::max())
{
if (World::getWorld()->getTicksSinceStart() >= m_removed_bomb_ticks)
return true;
return false;
} // if removing bomb
if (!m_discard_now)
{
if (m_removing_bomb)
{
m_swat_bomb_frame += dt*25.0f;
m_scene_node->setRotation(core::vector3df(0.0, -180.0, 0.0));
m_scene_node->setCurrentFrame(m_swat_bomb_frame);
if (m_swat_bomb_frame >= 32.5f && m_bomb_scene_node != NULL)
{
m_bomb_scene_node->setPosition(m_bomb_scene_node
->getPosition() + core::vector3df(-dt*15.0f, 0.0f, 0.0f) );
m_bomb_scene_node->setRotation(m_bomb_scene_node
->getRotation() + core::vector3df(-dt*15.0f, 0.0f, 0.0f) );
}
if (m_swat_bomb_frame >= m_scene_node->getEndFrame())
{
return true;
}
else if (m_swat_bomb_frame >= 35)
{
if (m_bomb_scene_node != NULL)
{
irr_driver->removeNode(m_bomb_scene_node);
m_bomb_scene_node = NULL;
}
} // bom_frame > 35
return false;
} // if removing bomb
switch(m_animation_phase)
switch (m_animation_phase)
{
case SWATTER_AIMING:
{
@ -161,43 +182,56 @@ bool Swatter::updateAndTestFinished(int ticks)
// Get the node corresponding to the joint at the center of the
// swatter (by swatter, I mean the thing hold in the hand, not
// the whole thing)
scene::ISceneNode* swatter_node =
m_scene_node->getJointNode("Swatter");
assert(swatter_node);
Vec3 swatter_pos = swatter_node->getAbsolutePosition();
// The joint node doesn't update in server without graphics,
// so an approximate position is used
//scene::ISceneNode* swatter_node =
// m_scene_node->getJointNode("Swatter");
//assert(swatter_node);
//Vec3 swatter_pos = swatter_node->getAbsolutePosition();
Vec3 swatter_pos = m_kart->getTrans()(Vec3(SWAT_POS_OFFSET));
float dist2 = (m_closest_kart->getXYZ()-swatter_pos).length2();
float min_dist2
= m_kart->getKartProperties()->getSwatterDistance();
if(dist2 < min_dist2)
if (dist2 < min_dist2)
{
// Start squashing
m_animation_phase = SWATTER_TO_TARGET;
m_start_swat_ticks =
World::getWorld()->getTicksSinceStart() + 20;
// Setup the animation
m_scene_node->setCurrentFrame(0.0f);
m_scene_node->setLoopMode(false);
m_scene_node->setAnimationSpeed(SWATTER_ANIMATION_SPEED);
#ifndef SERVER_ONLY
// Play swat sound
m_swat_sound->setPosition(swatter_pos);
m_swat_sound->play();
const int wt = World::getWorld()->getTicksSinceStart();
auto it = m_swat_sound_ticks.find(wt);
if (it == m_swat_sound_ticks.end())
{
m_swat_sound->setPosition(swatter_pos);
m_swat_sound->play();
m_swat_sound_ticks.insert(wt);
}
#endif
}
}
break;
case SWATTER_TO_TARGET:
{
pointToTarget();
const float middle_frame = m_scene_node->getEndFrame()/2.0f;
float current_frame = m_scene_node->getFrameNr();
// Did we just finish the first part of the movement?
if(current_frame >= middle_frame)
if (World::getWorld()->getTicksSinceStart() > m_start_swat_ticks)
{
m_start_swat_ticks = std::numeric_limits<int>::max();
// Squash the karts and items around and
// change the current phase
squashThingsAround();
m_animation_phase = SWATTER_FROM_TARGET;
const int end_ticks =
World::getWorld()->getTicksSinceStart() + 60;
if (race_manager
->getMinorMode()==RaceManager::MINOR_MODE_3_STRIKES ||
race_manager
@ -207,6 +241,7 @@ bool Swatter::updateAndTestFinished(int ticks)
// after one successful hit
m_discard_now = true;
}
m_end_swat_ticks = end_ticks;
}
}
break;
@ -215,20 +250,20 @@ bool Swatter::updateAndTestFinished(int ticks)
break;
}
}
else
m_discard_timeout += dt;
return (m_discard_now && m_discard_timeout > 0.5f ? true : false);
if (m_discard_now)
{
return World::getWorld()->getTicksSinceStart() > m_end_swat_ticks;
}
else if (World::getWorld()->getTicksSinceStart() > m_end_swat_ticks)
{
m_animation_phase = SWATTER_AIMING;
m_end_swat_ticks = std::numeric_limits<int>::max();
return false;
}
return false;
} // updateAndTestFinished
// ----------------------------------------------------------------------------
/** When the animation ends, the swatter is ready again.
*/
void Swatter::onAnimationEnd()
{
m_animation_phase = SWATTER_AIMING;
} // onAnimationEnd
// ----------------------------------------------------------------------------
/** Determine the nearest kart or item and update the current target
* accordingly.
@ -275,6 +310,7 @@ void Swatter::chooseTarget()
* default position. */
void Swatter::pointToTarget()
{
#ifndef SERVER_ONLY
if(!m_target)
{
m_scene_node->setRotation(core::vector3df());
@ -288,6 +324,7 @@ void Swatter::pointToTarget()
float angle = SWAT_ANGLE_OFFSET + atan2f(dy, dx) * 180 / M_PI;
m_scene_node->setRotation(core::vector3df(0.0, angle, 0.0));
}
#endif
} // pointToTarget
// ----------------------------------------------------------------------------

View File

@ -23,8 +23,9 @@
#include "items/attachment_plugin.hpp"
#include "utils/cpp2011.hpp"
#include "utils/no_copy.hpp"
#include "utils/random_generator.hpp"
#include <limits>
#include <set>
#include <vector3d.h>
#include <IAnimatedMeshSceneNode.h>
@ -53,9 +54,6 @@ private:
/** True if the swatter will be discarded now. */
bool m_discard_now;
/** Require for the sfx to complete. */
float m_discard_timeout;
/** The kart the swatter is aiming at. */
Moveable *m_target;
@ -63,8 +61,8 @@ private:
SFXBase *m_swat_sound;
/** True if the swatter is removing an attached bomb. */
bool m_removing_bomb;
/** Set the end ticks to complete the removing an attached bomb animation. */
int m_removed_bomb_ticks;
/** The scene node of the attachment. */
scene::IAnimatedMeshSceneNode *m_scene_node;
@ -76,21 +74,30 @@ private:
/** For some reason the built-in animation system doesn't work correctly here?? */
float m_swat_bomb_frame;
std::set<int> m_swat_sound_ticks;
int m_start_swat_ticks;
int m_end_swat_ticks;
public:
Swatter(AbstractKart *kart, bool was_bomb,
scene::ISceneNode* bomb_scene_node);
virtual ~Swatter();
void updateGrahpics(float dt) OVERRIDE;
bool updateAndTestFinished(int ticks) OVERRIDE;
// ------------------------------------------------------------------------
/** Returns if the swatter is currently aiming, i.e. can be used to
* swat an incoming projectile. */
bool isSwatterReady() const {
bool isSwatterReady() const
{
return m_animation_phase == SWATTER_AIMING;
} // isSwatterReady
// ------------------------------------------------------------------------
virtual void onAnimationEnd() OVERRIDE;
// ------------------------------------------------------------------------
bool isRemovingBomb() const
{
return m_removed_bomb_ticks != std::numeric_limits<int>::max();
}
private:
/** Determine the nearest kart or item and update the current target accordingly */

View File

@ -400,7 +400,8 @@ void LocalPlayerController::nitroNotFullSound()
*/
bool LocalPlayerController::canGetAchievements() const
{
return m_player->getConstProfile() == PlayerManager::getCurrentPlayer();
return !RewindManager::get()->isRewinding() &&
m_player->getConstProfile() == PlayerManager::getCurrentPlayer();
} // canGetAchievements
// ----------------------------------------------------------------------------

View File

@ -36,6 +36,8 @@ public:
{
} // ~NetworkPlayerController
// ------------------------------------------------------------------------
virtual bool canGetAchievements() const OVERRIDE { return false; }
// ------------------------------------------------------------------------
/** This player is not a local player. This affect e.g. special sfx and
* camera effects to be triggered. */
virtual bool isLocalPlayerController() const OVERRIDE

View File

@ -2939,7 +2939,7 @@ void Kart::updateGraphics(float dt)
if (m_custom_sounds[n] != NULL) m_custom_sounds[n]->position(getXYZ());
}
*/
#ifndef SERVER_ONLY
if (m_squash_time != std::numeric_limits<float>::max())
{
m_squash_time -= dt;
@ -2962,6 +2962,7 @@ void Kart::updateGraphics(float dt)
}
}
} // if squashed
#endif
for (int i = 0; i < EMITTER_COUNT; i++)
m_emitters[i]->setPosition(getXYZ());

View File

@ -252,7 +252,6 @@ std::function<void()> KartRewinder::getLocalStateRestoreFunction()
// Attachment local state
float initial_speed = getAttachment()->getInitialSpeed();
float node_scale = getAttachment()->getNodeScale();
// Controller local state
int steer_val_l = 0;
@ -265,14 +264,13 @@ std::function<void()> KartRewinder::getLocalStateRestoreFunction()
}
return [has_started, bounce_back_ticks, brake_ticks, min_nitro_ticks,
initial_speed, node_scale, steer_val_l, steer_val_r, this]()
initial_speed, steer_val_l, steer_val_r, this]()
{
m_has_started = has_started;
m_bounce_back_ticks = bounce_back_ticks;
m_brake_ticks = brake_ticks;
m_min_nitro_ticks = min_nitro_ticks;
getAttachment()->setInitialSpeed(initial_speed);
getAttachment()->setNodeScale(node_scale);
PlayerController* pc = dynamic_cast<PlayerController*>(m_controller);
if (pc)
{