fix: boat not following correctly the player when speeding (#127)

This commit is contained in:
Lord Cambion 2026-05-27 19:22:12 +02:00 committed by GitHub
parent 311503ec46
commit c19d5bb58e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 80 additions and 40 deletions

View file

@ -290,24 +290,53 @@ void TrackedEntity::tick(EntityTracker *tracker, vector<shared_ptr<Player> > *pl
wasRiding = false; wasRiding = false;
} }
else else
{ {
bool rot = abs(yRotn - yRotp) >= TOLERANCE_LEVEL || abs(xRotn - xRotp) >= TOLERANCE_LEVEL; // the entity have a rider, the code didnt send position updates,
if (rot) // causing desync between client and server when boat was spritning.
{
// 4J: Changed this to use deltas
broadcast(std::make_shared<MoveEntityPacket::Rot>(e->entityId, static_cast<byte>(yRota), static_cast<byte>(xRota)));
yRotp = yRotn;
xRotp = xRotn;
}
xp = Mth::floor(e->x * 32.0); bool rot = abs(yRotn - yRotp) >= TOLERANCE_LEVEL || abs(xRotn - xRotp) >= TOLERANCE_LEVEL;
yp = Mth::floor(e->y * 32.0); if (rot)
zp = Mth::floor(e->z * 32.0); {
broadcast(std::make_shared<MoveEntityPacket::Rot>(e->entityId, static_cast<byte>(yRota), static_cast<byte>(xRota)));
yRotp = yRotn;
xRotp = xRotn;
}
sendDirtyEntityData(); int xn = Mth::floor(e->x * 32.0);
int yn = Mth::floor(e->y * 32.0);
int zn = Mth::floor(e->z * 32.0);
int xa = xn - xp;
int ya = yn - yp;
int za = zn - zp;
wasRiding = true; // send only if the boat moved enough
} // or 3 seconds periodically
bool pos = abs(xa) >= TOLERANCE_LEVEL || abs(ya) >= TOLERANCE_LEVEL || abs(za) >= TOLERANCE_LEVEL
|| (tickCount % (SharedConstants::TICKS_PER_SECOND * 3) == 0);
if (pos)
{
// if deltapos is too much big use teleport.
if (xa < -128 || xa >= 128 || ya < -128 || ya >= 128 || za < -128 || za >= 128)
{
broadcast(std::make_shared<TeleportEntityPacket>(e->entityId, xn, yn, zn,
static_cast<byte>(yRotn), static_cast<byte>(xRotn)));
}
else
{
// small movement, delta
broadcast(std::make_shared<MoveEntityPacket::Pos>(e->entityId,
static_cast<char>(xa), static_cast<char>(ya), static_cast<char>(za)));
}
xp = xn;
yp = yn;
zp = zn;
}
sendDirtyEntityData();
wasRiding = true;
}
int yHeadRot = Mth::floor(e->getYHeadRot() * 256 / 360); int yHeadRot = Mth::floor(e->getYHeadRot() * 256 / 360);
if (abs(yHeadRot - yHeadRotp) >= TOLERANCE_LEVEL) if (abs(yHeadRot - yHeadRotp) >= TOLERANCE_LEVEL)

View file

@ -148,7 +148,7 @@ void Boat::lerpTo(double x, double y, double z, float yRot, float xRot, int step
{ {
if (doLerp) if (doLerp)
{ {
lSteps = steps + 5; lSteps = steps +5;
} }
else else
{ {
@ -188,6 +188,10 @@ void Boat::lerpMotion(double xd, double yd, double zd)
void Boat::tick() void Boat::tick()
{ {
Entity::tick(); Entity::tick();
if (getHurtTime() > 0) setHurtTime(getHurtTime() - 1); if (getHurtTime() > 0) setHurtTime(getHurtTime() - 1);
if (getDamage() > 0) setDamage(getDamage() - 1); if (getDamage() > 0) setDamage(getDamage() - 1);
xo = x; xo = x;
@ -199,8 +203,8 @@ void Boat::tick()
double waterPercentage = 0; double waterPercentage = 0;
for (int i = 0; i < steps; i++) for (int i = 0; i < steps; i++)
{ {
double y0 = bb->y0 + (bb->y1 - bb->y0) * (i + 0) / steps - 2 / 16.0f; double y0 = bb->y0 + (bb->y1 - bb->y0) * (i + 0) / steps + 1.5f / 16.0f;
double y1 = bb->y0 + (bb->y1 - bb->y0) * (i + 1) / steps - 2 / 16.0f; double y1 = bb->y0 + (bb->y1 - bb->y0) * (i + 1) / steps + 1.5f / 16.0f;
AABB *bb2 = AABB::newTemp(bb->x0, y0, bb->z0, bb->x1, y1, bb->z1); AABB *bb2 = AABB::newTemp(bb->x0, y0, bb->z0, bb->x1, y1, bb->z1);
if (level->containsLiquid(bb2, Material::water)) if (level->containsLiquid(bb2, Material::water))
{ {
@ -257,18 +261,19 @@ void Boat::tick()
return; return;
} }
// Bob in water // Bob in water & gravity
if (waterPercentage > 0) if (waterPercentage < 1.0)
{ {
double bob = waterPercentage * 2 - 1; double bob = waterPercentage * 2.0 - 1.0;
yd += 0.04f * bob; yd += 0.04f * bob;
} }
else
// Reimplement gravity again (??)
int tileUnder = level->getTile(Mth::floor(x), Mth::floor(y-0.15), Mth::floor(z));
if (tileUnder == 0 && !onGround)
{ {
yd -= 0.04f; if (yd < 0.0)
{
yd /= 2.0;
}
yd += 0.007f;
} }
// Rider controls // Rider controls
@ -281,24 +286,16 @@ void Boat::tick()
{ {
double riderXd = -sin(livingRider->yRot * PI / 180); double riderXd = -sin(livingRider->yRot * PI / 180);
double riderZd = cos(livingRider->yRot * PI / 180); double riderZd = cos(livingRider->yRot * PI / 180);
float mult = livingRider->isSprinting() ? 2.0f : 1.0f; double currentSpeed = sqrt(xd * xd + zd * zd);
float moveFactor = (float)forward; float moveFactor = (float)forward;
if (forward < 0) moveFactor *= 0.5f; // Move slower backwards if (forward < 0) moveFactor *= 0.5f; // Move slower backwards
xd += riderXd * acceleration * 0.05f * mult * moveFactor; xd += riderXd * acceleration * 0.05f * moveFactor;
zd += riderZd * acceleration * 0.05f * mult * moveFactor; zd += riderZd * acceleration * 0.05f * moveFactor;
} }
} }
double curSpeed = sqrt(xd * xd + zd * zd); double curSpeed = sqrt(xd * xd + zd * zd);
double maxSpeed = MAX_SPEED; double maxSpeed = MAX_SPEED;
if (rider.lock() != nullptr && rider.lock()->instanceof(eTYPE_LIVINGENTITY))
{
shared_ptr<LivingEntity> livingRider = dynamic_pointer_cast<LivingEntity>(rider.lock());
if (livingRider->isSprinting())
{
maxSpeed *= 1.5;
}
}
if (curSpeed > maxSpeed) if (curSpeed > maxSpeed)
{ {
@ -330,10 +327,12 @@ void Boat::tick()
move(xd, yd, zd); move(xd, yd, zd);
// Break boat on high speed collision // Break boat on high speed collision
float breakThreshold = (rider.lock() != nullptr) ? 0.35f : 0.20f;
if ((horizontalCollision && lastSpeed > 0.20)) if ((horizontalCollision && lastSpeed > 0.20))
{ {
if (!level->isClientSide && !removed) if (!level->isClientSide && !removed)
{ {
remove(); remove();
for (int i = 0; i < 3; i++) for (int i = 0; i < 3; i++)
{ {
@ -343,6 +342,9 @@ void Boat::tick()
{ {
spawnAtLocation(Item::stick->id, 1, 0); spawnAtLocation(Item::stick->id, 1, 0);
} }
} }
} }
else else
@ -472,10 +474,19 @@ bool Boat::interact(shared_ptr<Player> player)
if ( (rider.lock() != nullptr) && rider.lock()->instanceof(eTYPE_PLAYER) && (rider.lock() != player) ) return true; if ( (rider.lock() != nullptr) && rider.lock()->instanceof(eTYPE_PLAYER) && (rider.lock() != player) ) return true;
if (!level->isClientSide) if (!level->isClientSide)
{ {
bool isRiding = (rider.lock() == player);
if (isRiding)
{
player->xd = 0;
player->yd = 0;
player->zd = 0;
}
// 4J HEG - Fixed issue with player not being able to dismount boat (issue #4446) // 4J HEG - Fixed issue with player not being able to dismount boat (issue #4446)
player->ride( rider.lock() == player ? nullptr : shared_from_this() ); player->ride(isRiding ? nullptr : shared_from_this());
} }
return true; return true;
} }
void Boat::setDamage(float damage) void Boat::setDamage(float damage)