Added Entity->fastMove() for players and direct-controlled entities, improved speed checking

This commit is contained in:
Shoghi Cervantes 2015-02-22 21:16:05 +01:00
parent eb0525e892
commit bb71a3c4a6
No known key found for this signature in database
GPG Key ID: 78464DB0A7837F89
2 changed files with 73 additions and 23 deletions

View File

@ -1073,11 +1073,13 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
protected function processMovement($currentTick){
if($this->dead or !$this->spawned or !($this->newPosition instanceof Vector3)){
if(++$this->speedTicks >= 10){
$diff = ($currentTick - $this->lastSpeedTick);
if($diff >= 10){
$this->speed = new Vector3(0, 0, 0);
$this->speedTicks = -PHP_INT_MAX;
$this->lastSpeedTick = $currentTick;
}elseif($diff > 5 and $this->speedTicks < 20){
++$this->speedTicks;
}
return;
}
@ -1107,8 +1109,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
$dy = $this->newPosition->y - $this->y;
$dz = $this->newPosition->z - $this->z;
//$this->inBlock = $this->checkObstruction($this->x, ($this->boundingBox->minY + $this->boundingBox->maxY) / 2, $this->z);
$this->move($dx, $dy, $dz);
$this->fastMove($dx, $dy, $dz);
$diffX = $this->x - $this->newPosition->x;
$diffZ = $this->z - $this->newPosition->z;
@ -1124,12 +1125,6 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
if($diff > 0.0625){
$revert = true;
$this->server->getLogger()->warning($this->getName()." moved wrongly!");
}elseif($diff > 0){
$this->x = $this->newPosition->x;
$this->y = $this->newPosition->y;
$this->z = $this->newPosition->z;
$radius = $this->width / 2;
$this->boundingBox->setBounds($this->x - $radius, $this->y + $this->ySize, $this->z - $radius, $this->x + $radius, $this->y + $this->height + $this->ySize, $this->z + $radius);
}
}
}elseif($diff > 0){
@ -1181,15 +1176,21 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
}
}
$this->speed = $to->subtract($from)->divide($currentTick - $this->lastSpeedTick);
$this->speedTicks = 0;
$ticks = min(20, $currentTick - $this->lastSpeedTick + 0.5);
if($this->speedTicks > 0){
$ticks += $this->speedTicks;
}
$this->speed = $from->subtract($to)->divide($ticks);
$this->lastSpeedTick = $currentTick;
}elseif($distanceSquared == 0){
$this->speed = new Vector3(0, 0, 0);
$this->speedTicks = 0;
$this->lastSpeedTick = $currentTick;
}
if($this->speedTicks > 0){
--$this->speedTicks;
}
if($revert){
$this->lastX = $from->x;
@ -1248,19 +1249,21 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
if($this->speed and $this->isSurvival()){
$speed = sqrt($this->speed->x ** 2 + $this->speed->z ** 2);
if($speed > 0.30){
$this->highSpeedTicks += $speed > 1 ? 3 : 1;
if($speed > 0.45){
$this->highSpeedTicks += $speed > 3 ? 2 : 1;
if($this->highSpeedTicks > 40 and !$this->server->getAllowFlight()){
$this->kick("Flying is not enabled on this server");
return false;
}elseif($this->highSpeedTicks > 5){
$factor = min(1, ($speed - 0.22) / $speed);
$mot = new Vector3($this->speed->x * $factor, 0, $this->speed->z * $factor);
$this->setMotion($mot);
$this->speed = $mot;
}elseif($this->highSpeedTicks >= 10 and $this->highSpeedTicks % 4 === 0){
$this->forceMovement = $this->getPosition();
$this->speed = null;
}
}elseif($this->highSpeedTicks > 0){
$this->highSpeedTicks--;
if($speed < 22){
$this->highSpeedTicks = 0;
}else{
$this->highSpeedTicks--;
}
}
}
@ -1599,7 +1602,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
$this->forceMovement = new Vector3($this->x, $this->y, $this->z);
}
if($this->forceMovement instanceof Vector3 and ($revert or $newPos->distanceSquared($this->forceMovement) > 0.04)){
if($this->forceMovement instanceof Vector3 and (($dist = $newPos->distanceSquared($this->forceMovement)) > 0.04 or $revert)){
$pk = new MovePlayerPacket();
$pk->eid = 0;
$pk->x = $this->forceMovement->x;

View File

@ -819,6 +819,53 @@ abstract class Entity extends Location implements Metadatable{
return false;
}
public function fastMove($dx, $dy, $dz){
if($dx == 0 and $dz == 0 and $dy == 0){
return true;
}
Timings::$entityMoveTimer->startTiming();
$axisalignedbb = clone $this->boundingBox;
$newBB = $this->boundingBox->getOffsetBoundingBox($dx, $dy, $dz);
$list = $this->level->getCollisionCubes($this, $newBB->expand(-0.01, -0.01, -0.01));
if(count($list) === 0){
$this->boundingBox = $newBB;
}
$pos = new Vector3(
($this->boundingBox->minX + $this->boundingBox->maxX) / 2,
$this->boundingBox->minY + $this->ySize,
($this->boundingBox->minZ + $this->boundingBox->maxZ) / 2
);
$result = true;
if(!$this->setPosition($pos)){
$this->boundingBox->setBB($axisalignedbb);
$result = false;
}else{
if(!$this->onGround or $dy != 0){
$bb = clone $this->boundingBox;
$bb->minY -= 0.75;
$this->onGround = false;
if(count($this->level->getCollisionBlocks($bb)) > 0){
$this->onGround = true;
}
}
$this->isCollided = $this->onGround;
$this->updateFallState($dy, $this->onGround);
}
Timings::$entityMoveTimer->stopTiming();
return $result;
}
public function move($dx, $dy, $dz){
if($dx == 0 and $dz == 0 and $dy == 0){