Fixed several mixups of ticks and time.

This commit is contained in:
hiker 2018-02-22 19:25:11 +11:00
parent 7a53cf16fe
commit 81342ddd00
8 changed files with 111 additions and 76 deletions

View File

@ -49,7 +49,7 @@ Attachment::Attachment(AbstractKart* kart)
: EventRewinder()
{
m_type = ATTACH_NOTHING;
m_time_left = 0.0;
m_ticks_left = 0;
m_plugin = NULL;
m_kart = kart;
m_previous_owner = NULL;
@ -103,7 +103,7 @@ Attachment::~Attachment()
* can be passed back to the previous owner). NULL if a no
* previous owner exists.
*/
void Attachment::set(AttachmentType type, float time,
void Attachment::set(AttachmentType type, int ticks,
AbstractKart *current_kart)
{
bool was_bomb = (m_type == ATTACH_BOMB);
@ -162,7 +162,7 @@ void Attachment::set(AttachmentType type, float time,
m_node->setScale(core::vector3df(m_node_scale,m_node_scale,m_node_scale));
m_type = type;
m_time_left = time;
m_ticks_left = ticks;
m_previous_owner = current_kart;
m_node->setRotation(core::vector3df(0, 0, 0));
@ -188,7 +188,7 @@ void Attachment::set(AttachmentType type, float time,
speed_mult = 1.0f + (f * (temp_mult - 1.0f));
m_time_left = m_time_left * speed_mult;
m_ticks_left = int(m_ticks_left * speed_mult);
if (UserConfigParams::m_particles_effects > 1)
{
@ -230,7 +230,7 @@ void Attachment::clear()
m_type=ATTACH_NOTHING;
m_time_left=0.0;
m_ticks_left = 0;
m_node->setVisible(false);
m_node->setPosition(core::vector3df());
m_node->setRotation(core::vector3df());
@ -254,7 +254,7 @@ void Attachment::saveState(BareNetworkString *buffer) const
buffer->addUInt8(type);
if(m_type!=ATTACH_NOTHING)
{
buffer->addFloat(m_time_left);
buffer->addUInt32(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
@ -278,13 +278,13 @@ void Attachment::rewindTo(BareNetworkString *buffer)
return;
}
float time_left = buffer->getFloat();
int ticks_left = buffer->getUInt32();
// Attaching an object can be expensive (loading new models, ...)
// so avoid doing this if there is no change in attachment type
if(new_type == m_type)
{
setTimeLeft(time_left);
setTicksLeft(ticks_left);
return;
}
@ -299,7 +299,7 @@ void Attachment::rewindTo(BareNetworkString *buffer)
{
m_previous_owner = NULL;
}
set(new_type, time_left, m_previous_owner);
set(new_type, ticks_left, m_previous_owner);
} // rewindTo
// -----------------------------------------------------------------------------
/** Called when going forwards in time during a rewind.
@ -327,11 +327,11 @@ void Attachment::hitBanana(Item *item, int new_attachment)
if(m_type == ATTACH_BUBBLEGUM_SHIELD ||
m_type == ATTACH_NOLOK_BUBBLEGUM_SHIELD)
{
m_time_left = 0.0f;
m_ticks_left = 0;
return;
}
float leftover_time = 0.0f;
int leftover_ticks = 0;
bool add_a_new_item = true;
@ -368,11 +368,11 @@ void Attachment::hitBanana(Item *item, int new_attachment)
// if the kart already has an anvil, attach a new anvil,
// and increase the overall time
new_attachment = 1;
leftover_time = m_time_left;
leftover_ticks = m_ticks_left;
break;
case ATTACH_PARACHUTE:
new_attachment = 0;
leftover_time = m_time_left;
leftover_ticks = m_ticks_left;
break;
default:
// There is no attachment currently, but there will be one
@ -393,7 +393,9 @@ void Attachment::hitBanana(Item *item, int new_attachment)
switch (new_attachment)
{
case 0:
set(ATTACH_PARACHUTE, kp->getParachuteDuration() + leftover_time);
set(ATTACH_PARACHUTE,
int(kp->getParachuteDuration()*stk_config->m_physics_fps)
+ leftover_ticks );
m_initial_speed = m_kart->getSpeed();
// if going very slowly or backwards,
@ -401,7 +403,9 @@ void Attachment::hitBanana(Item *item, int new_attachment)
if(m_initial_speed <= 1.5) m_initial_speed = 1.5;
break ;
case 1:
set(ATTACH_ANVIL, kp->getAnvilDuration() + leftover_time);
set(ATTACH_ANVIL,
int(kp->getAnvilDuration()*stk_config->m_physics_fps)
+ leftover_ticks );
// if ( m_kart == m_kart[0] )
// sound -> playSfx ( SOUND_SHOOMF ) ;
// Reduce speed once (see description above), all other changes are
@ -410,10 +414,10 @@ void Attachment::hitBanana(Item *item, int new_attachment)
m_kart->updateWeight();
break ;
case 2:
set( ATTACH_BOMB, stk_config->m_bomb_time+leftover_time);
set( ATTACH_BOMB,
int(stk_config->m_bomb_time*stk_config->m_physics_fps)
+ leftover_ticks );
// if ( m_kart == m_kart[0] )
// sound -> playSfx ( SOUND_SHOOMF ) ;
break ;
} // switch
}
@ -440,8 +444,8 @@ void Attachment::handleCollisionWithKart(AbstractKart *other)
// If both karts have a bomb, explode them immediately:
if(attachment_other->getType()==Attachment::ATTACH_BOMB)
{
setTimeLeft(0.0f);
attachment_other->setTimeLeft(0.0f);
setTicksLeft(0);
attachment_other->setTicksLeft(0);
}
else // only this kart has a bomb, move it to the other
{
@ -450,8 +454,9 @@ void Attachment::handleCollisionWithKart(AbstractKart *other)
{
// Don't move if this bomb was from other kart originally
other->getAttachment()->set(ATTACH_BOMB,
getTimeLeft()+
stk_config->m_bomb_time_increase,
getTicksLeft()+
int(stk_config->m_bomb_time_increase
*stk_config->m_physics_fps),
m_kart);
other->playCustomSFX(SFXManager::CUSTOM_ATTACH);
clear();
@ -467,8 +472,10 @@ void Attachment::handleCollisionWithKart(AbstractKart *other)
m_kart->decreaseShieldTime();
return;
}
set(ATTACH_BOMB, other->getAttachment()->getTimeLeft()+
stk_config->m_bomb_time_increase, other);
set(ATTACH_BOMB,
other->getAttachment()->getTicksLeft()+
int(stk_config->m_bomb_time_increase*stk_config->m_physics_fps),
other);
other->getAttachment()->clear();
m_kart->playCustomSFX(SFXManager::CUSTOM_ATTACH);
}
@ -490,7 +497,7 @@ void Attachment::update(float dt)
if (m_type == ATTACH_BOMB && m_kart->getKartAnimation() != NULL)
return;
m_time_left -=dt;
m_ticks_left--; // dt always physics time step
bool is_shield = m_type == ATTACH_BUBBLEGUM_SHIELD ||
@ -498,20 +505,23 @@ void Attachment::update(float dt)
float m_wanted_node_scale = is_shield
? std::max(1.0f, m_kart->getHighestPoint()*1.1f)
: 1.0f;
int slow_flashes = 3;
if (is_shield && m_time_left < slow_flashes)
int slow_flashes = 3*stk_config->m_physics_fps;
if (is_shield && m_ticks_left < slow_flashes)
{
int flashes_per_second = 4;
int divisor = 2;
int ticks_per_flash = stk_config->m_physics_fps / 4;
float fast_flashes = 0.5F;
if (m_time_left < fast_flashes)
int fast_flashes = stk_config->m_physics_fps/2;
if (m_ticks_left < fast_flashes)
{
flashes_per_second = 12;
ticks_per_flash = stk_config->m_physics_fps / 12;
}
int mod = (int)(m_time_left * flashes_per_second * 2) % divisor;
m_node->setVisible(2*mod >= divisor);
//int divisor = 2;
//int mod = (int)(m_ticks_left * flashes_per_second * 2) % divisor;
int mod = m_ticks_left % ticks_per_flash;
m_node->setVisible(mod > ticks_per_flash);
}
if (m_node_scale < m_wanted_node_scale)
@ -552,7 +562,7 @@ void Attachment::update(float dt)
f * (kp->getParachuteUboundFraction()
- kp->getParachuteLboundFraction())))
{
m_time_left = -1;
m_ticks_left = -1;
}
}
break;
@ -570,22 +580,23 @@ void Attachment::update(float dt)
assert(false);
break;
case ATTACH_BOMB:
{
if (m_bomb_sound) m_bomb_sound->setPosition(m_kart->getXYZ());
// Mesh animation frames are 1 to 61 frames (60 steps)
// The idea is change second by second, counterclockwise 60 to 0 secs
// If longer times needed, it should be a surprise "oh! bomb activated!"
if(m_time_left <= (m_node->getEndFrame() - m_node->getStartFrame()-1))
float time_left = float(m_ticks_left) / stk_config->m_physics_fps;
if (time_left <= (m_node->getEndFrame() - m_node->getStartFrame() - 1))
{
m_node->setCurrentFrame(m_node->getEndFrame()
- m_node->getStartFrame()-1-m_time_left);
- m_node->getStartFrame() - 1 - time_left);
}
if(m_time_left<=0.0)
if (m_ticks_left <= 0)
{
HitEffect *he = new Explosion(m_kart->getXYZ(), "explosion",
"explosion_bomb.xml" );
if(m_kart->getController()->isLocalPlayerController())
"explosion_bomb.xml");
if (m_kart->getController()->isLocalPlayerController())
he->setLocalPlayerKartHit();
projectile_manager->addHitEffect(he);
ExplosionAnimation::create(m_kart);
@ -597,11 +608,12 @@ void Attachment::update(float dt)
}
}
break;
}
case ATTACH_BUBBLEGUM_SHIELD:
case ATTACH_NOLOK_BUBBLEGUM_SHIELD:
if (m_time_left < 0)
if (m_ticks_left < 0)
{
m_time_left = 0.0f;
m_ticks_left = 0;
if (m_bubble_explode_sound) m_bubble_explode_sound->deleteSFX();
m_bubble_explode_sound =
SFXManager::get()->createSoundSource("bubblegum_explode");
@ -632,7 +644,7 @@ void Attachment::update(float dt)
} // switch
// Detach attachment if its time is up.
if ( m_time_left <= 0.0f)
if ( m_ticks_left <= 0)
clear();
} // update

View File

@ -78,7 +78,7 @@ private:
AbstractKart *m_kart;
/** Time left till attachment expires. */
float m_time_left;
int m_ticks_left;
/** For parachutes only. */
float m_initial_speed;
@ -114,7 +114,7 @@ public:
void hitBanana(Item *item, int new_attachment=-1);
void update (float dt);
void handleCollisionWithKart(AbstractKart *other);
void set (AttachmentType type, float time,
void set (AttachmentType type, int ticks,
AbstractKart *previous_kart=NULL);
virtual void rewind(BareNetworkString *buffer);
void rewindTo(BareNetworkString *buffer);
@ -122,16 +122,17 @@ public:
// ------------------------------------------------------------------------
/** Sets the type of the attachment, but keeps the old time left value. */
void set (AttachmentType type) { set(type, m_time_left); }
void set (AttachmentType type) { set(type, m_ticks_left); }
// ------------------------------------------------------------------------
/** Returns the type of this attachment. */
AttachmentType getType() const { return m_type; }
// ------------------------------------------------------------------------
/** Returns how much time is left before this attachment is removed. */
float getTimeLeft() const { return m_time_left; }
/** Returns how much time (in ticks) is left before this attachment is
* removed. */
int getTicksLeft() const { return m_ticks_left; }
// ------------------------------------------------------------------------
/** Sets how long this attachment will remain attached. */
void setTimeLeft(float t){ m_time_left = t; }
void setTicksLeft(int t){ m_ticks_left = t; }
// ------------------------------------------------------------------------
/** Returns the previous owner of this attachment, used in bombs that
* are being passed between karts. */

View File

@ -267,7 +267,8 @@ void Powerup::use()
case PowerupManager::POWERUP_SWATTER:
m_kart->getAttachment()
->set(Attachment::ATTACH_SWATTER, kp->getSwatterDuration());
->set(Attachment::ATTACH_SWATTER,
int(kp->getSwatterDuration()*stk_config->m_physics_fps));
break;
case PowerupManager::POWERUP_BUBBLEGUM:
@ -302,26 +303,36 @@ void Powerup::use()
{
if (m_kart->getIdent() == "nolok")
{
m_kart->getAttachment()->set(Attachment::ATTACH_NOLOK_BUBBLEGUM_SHIELD,
kp->getBubblegumShieldDuration());
m_kart->getAttachment()
->set(Attachment::ATTACH_NOLOK_BUBBLEGUM_SHIELD,
int(kp->getBubblegumShieldDuration()
*stk_config->m_physics_fps) );
}
else
{
m_kart->getAttachment()->set(Attachment::ATTACH_BUBBLEGUM_SHIELD,
kp->getBubblegumShieldDuration());
m_kart->getAttachment()
->set(Attachment::ATTACH_BUBBLEGUM_SHIELD,
int(kp->getBubblegumShieldDuration()
*stk_config->m_physics_fps) );
}
}
else // using a bubble gum while still having a shield
{
if (m_kart->getIdent() == "nolok")
{
m_kart->getAttachment()->set(Attachment::ATTACH_NOLOK_BUBBLEGUM_SHIELD,
kp->getBubblegumShieldDuration() + m_kart->getShieldTime());
m_kart->getAttachment()
->set(Attachment::ATTACH_NOLOK_BUBBLEGUM_SHIELD,
int( (kp->getBubblegumShieldDuration()
+ m_kart->getShieldTime() )
*stk_config->m_physics_fps) );
}
else
{
m_kart->getAttachment()->set(Attachment::ATTACH_BUBBLEGUM_SHIELD,
kp->getBubblegumShieldDuration() + m_kart->getShieldTime());
m_kart->getAttachment()
->set(Attachment::ATTACH_BUBBLEGUM_SHIELD,
int(kp->getBubblegumShieldDuration()
+ m_kart->getShieldTime() )
*stk_config->m_physics_fps );
}
}
@ -350,7 +361,8 @@ void Powerup::use()
if(kart->getPosition() == 1)
{
kart->getAttachment()->set(Attachment::ATTACH_ANVIL,
kp->getAnvilDuration());
int(kp->getAnvilDuration()
*stk_config->m_physics_fps) );
kart->updateWeight();
kart->adjustSpeed(kp->getAnvilSpeedFactor() * 0.5f);
@ -397,14 +409,17 @@ void Powerup::use()
{
float rank_factor;
rank_factor = (float)(kart->getPosition() - 1) / (float)(m_kart->getPosition() - 2);
rank_factor = (float)(kart->getPosition() - 1)
/ (float)(m_kart->getPosition() - 2);
position_factor = 1.0f - rank_factor;
}
rank_mult = 1 + (position_factor * (kp->getParachuteDurationRankMult() - 1));
kart->getAttachment()->set(Attachment::ATTACH_PARACHUTE,
(kp->getParachuteDurationOther() * rank_mult));
kart->getAttachment()
->set(Attachment::ATTACH_PARACHUTE,
int(kp->getParachuteDurationOther() * rank_mult
* stk_config->m_physics_fps) );
if(kart->getController()->isLocalPlayerController())
player_kart = kart;

View File

@ -205,7 +205,10 @@ public:
virtual bool updateAndDelete(float dt);
virtual bool hit(AbstractKart* kart, PhysicalObject* obj=NULL);
virtual void setAnimation(AbstractKartAnimation *animation);
static float getTicksBetweenRubberBalls() {return m_ticks_between_balls;}
// ------------------------------------------------------------------------
/** Returns time (in ticks) between rubberballs, to avoid that in games
* with many karts too many rubber balls are in play at the same time. */
static int getTicksBetweenRubberBalls() { return m_ticks_between_balls; }
// ------------------------------------------------------------------------
/** This object does not create an explosion, all affects on
* karts are handled by this hit() function. */

View File

@ -1112,16 +1112,16 @@ bool Kart::isNearGround() const
} // isNearGround
// ------------------------------------------------------------------------
/**
* Enables a kart shield protection for a certain amount of time.
/** Enables a kart shield protection for a certain amount of time.
*/
void Kart::setShieldTime(float t)
{
if(isShielded())
{
getAttachment()->setTimeLeft(t);
getAttachment()->setTicksLeft(int(t*stk_config->m_physics_fps));
}
}
} // setShieldTime
// ------------------------------------------------------------------------
/**
* Returns true if the kart is protected by a shield.
@ -1147,7 +1147,8 @@ bool Kart::isShielded() const
float Kart::getShieldTime() const
{
if(isShielded())
return getAttachment()->getTimeLeft();
return float(getAttachment()->getTicksLeft())
/ stk_config->m_physics_fps;
else
return 0.0f;
} // getShieldTime
@ -1161,7 +1162,7 @@ void Kart::decreaseShieldTime()
{
if (isShielded())
{
getAttachment()->setTimeLeft(0.0f);
getAttachment()->setTicksLeft(0);
}
} // decreaseShieldTime

View File

@ -137,8 +137,8 @@ void WorldStatus::startEngines()
void WorldStatus::setClockMode(const ClockType mode, const float initial_time)
{
m_clock_mode = mode;
m_time_ticks = initial_time * stk_config->m_physics_fps;
m_time = initial_time;
m_time_ticks = int(initial_time * stk_config->m_physics_fps);
m_time = float(m_time_ticks) / stk_config->m_physics_fps;
} // setClockMode
//-----------------------------------------------------------------------------
@ -454,8 +454,8 @@ void WorldStatus::startReadySetGo()
*/
void WorldStatus::setTime(const float time)
{
m_time_ticks = time * stk_config->m_physics_fps;
m_time = time;
m_time_ticks = int(time * stk_config->m_physics_fps);
m_time = float(time)/stk_config->m_physics_fps;
} // setTime
//-----------------------------------------------------------------------------

View File

@ -493,7 +493,7 @@ void RaceGUIBase::drawGlobalMusicDescription()
gui::IGUIFont* font = GUIEngine::getFont();
float race_time = World::getWorld()->getTicksSinceStart()
float race_time = float(World::getWorld()->getTicksSinceStart())
/ stk_config->m_physics_fps;
// ---- Manage pulsing effect

View File

@ -168,19 +168,22 @@ void addAttachment(Attachment::AttachmentType type)
if (type == Attachment::ATTACH_ANVIL)
{
kart->getAttachment()
->set(type, kart->getKartProperties()->getAnvilDuration());
->set(type, int(kart->getKartProperties()->getAnvilDuration()
*stk_config->m_physics_fps) );
kart->adjustSpeed(kart->getKartProperties()->getAnvilSpeedFactor());
kart->updateWeight();
}
else if (type == Attachment::ATTACH_PARACHUTE)
{
kart->getAttachment()
->set(type, kart->getKartProperties()->getParachuteDuration());
->set(type, int(kart->getKartProperties()->getParachuteDuration()
*stk_config->m_physics_fps) );
}
else if (type == Attachment::ATTACH_BOMB)
{
kart->getAttachment()
->set(type, stk_config->m_bomb_time);
->set(type, int(stk_config->m_bomb_time
*stk_config->m_physics_fps) );
}
}