diff --git a/src/Player.php b/src/Player.php index 16b1c31a..35a7e8c4 100755 --- a/src/Player.php +++ b/src/Player.php @@ -76,6 +76,13 @@ class Player{ public $blockUpdateQueue; public $blockUpdateQueueLength = 0; + /** + * Sent by a client while it is linked to some entity. + * @var boolean $isJumping + * @var boolean $isSneaking + */ + public $isJumping, $isSneaking; + /** * @param integer $clientID * @param string $ip @@ -724,12 +731,8 @@ class Player{ switch($event){ case "entity.link": $pk = new SetEntityLinkPacket(); - if($data["rider"] === $this->eid){ - $pk->rider = 0; - }else{ - $pk->rider = $data["rider"]; - } - $pk->riding = $data["riding"]; + $pk->rider = $data["rider"] == $this->entity->eid ? 0 : $data["rider"]; + $pk->riding = $data["riding"] == $this->entity->eid ? 0 : $data["riding"]; $pk->type = 0; $this->dataPacket($pk); break; @@ -2466,26 +2469,34 @@ class Player{ } break; case ProtocolInfo::PLAYER_INPUT_PACKET: - if(strlen(bin2hex($packet->buffer)) === 24 && $this->entity->linkedEntity instanceof Entity){ - $this->entity->linkedEntity->linkEntity($this->entity->linkedEntity, SetEntityLinkPacket::TYPE_RIDE); - $this->entity->linkedEntity->linkedEntity = false; - $this->entity->linkedEntity = false; + $this->isJumping = $packet->isJumping; + $this->isSneaking = $packet->isSneaking; + $this->entity->moveForward = $packet->moveForward; + $this->entity->moveStrafing = $packet->moveStrafe; + + if(strlen(bin2hex($packet->buffer)) === 24 && $this->entity->linkedEntity != 0){ + $this->entity->stopRiding(); + break; } - if($this->entity->linkedEntity instanceof Entity){ - $pk = new SetEntityMotionPacket; - $pk->eid = $this->entity->linkedEntity->eid; - $pk->speedX = ($this->entity->x - $this->entity->linkedEntity->x)*1.2; - $pk->speedY = ($this->entity->y - $this->entity->linkedEntity->y)*1.2; - $pk->speedZ = ($this->entity->z - $this->entity->linkedEntity->z)*1.2; + if($this->entity->linkedEntity != 0){ //TODO better riding + $e = $this->entity->level->entityList[$this->entity->linkedEntity] ?? false; + if($e === false) { + ConsoleAPI::warn("Player is riding on entity that doesnt exist in world! ({$this->iusername}, {$this->entity->linkedEntity})"); + $this->entity->stopRiding(); + break; + } + /*$pk = new SetEntityMotionPacket; + $pk->eid = $e->eid; + $pk->speedX = ($this->entity->x - $e->x)*1.2; + $pk->speedY = ($this->entity->y - $e->y)*1.2; + $pk->speedZ = ($this->entity->z - $e->z)*1.2; foreach($this->level->players as $p){ if($p->entity->eid != $this->entity->eid){ $p->dataPacket(clone $pk); } } - - $this->entity->linkedEntity->setPosition($this->entity); - $this->entity->linkedEntity->sendMoveUpdate(); - //$this->entity->linkedEntity->moveFlying($packet->moveStrafe, $packet->moveForward, 1); + console("{$e} {$this->entity}"); + //$this->entity->linkedEntity->moveFlying($packet->moveStrafe, $packet->moveForward, 1);*/ } break; diff --git a/src/astarnavigator/TileNavigator.php b/src/astarnavigator/TileNavigator.php index 98ce61e3..c29c4fec 100644 --- a/src/astarnavigator/TileNavigator.php +++ b/src/astarnavigator/TileNavigator.php @@ -19,6 +19,7 @@ class TileNavigator } public function reconstructPath($path, $current){ + console("path built"); $totalPath = [$current]; while (isset($path[(string)$current])) { @@ -172,7 +173,7 @@ class TileNavigator $path[$neighbor] = $current; if($targetDist < $closestToTargetDist){ - console("found new best to target: $neighbor($closestToTargetDist -> $targetDist)"); + //console("found new best to target: $neighbor($closestToTargetDist -> $targetDist)"); $closestToTarget = $neighbor; $closestToTargetDist = $targetDist; diff --git a/src/entity/Creature.php b/src/entity/Creature.php index 159ff7f6..8ee78114 100644 --- a/src/entity/Creature.php +++ b/src/entity/Creature.php @@ -73,6 +73,10 @@ abstract class Creature extends Living{ $pk->speedY = $this->speedY; $pk->speedZ = $this->speedZ; $player->dataPacket($pk); + + if($this->linkedEntity != 0 && $this->isRider){ + $player->eventHandler(["rider" => $this->eid, "riding" => $this->linkedEntity, "type" => 0], "entity.link"); + } } } diff --git a/src/entity/Entity.php b/src/entity/Entity.php index 8a9edd05..5fb78ba7 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -69,7 +69,8 @@ class Entity extends Position public $level; public $isRiding = false; public $lastUpdate; - public $linkedEntity = null; + public $linkedEntity = 0; + public $isRider = false; public $check = true; public $width = 1; public $height = 1; @@ -105,6 +106,8 @@ class Entity extends Position public $inWeb; public $inLava; + public $moveStrafing, $moveForward; + function __construct(Level $level, $eid, $class, $type = 0, $data = []) { $this->random = new Random(); @@ -220,7 +223,7 @@ class Entity extends Position } public function isPushable(){ - return false; + return $this->isPlayer(); } public function applyCollision(Entity $collided){ @@ -481,13 +484,6 @@ class Entity extends Position $hasUpdate = true; } - $startX = floor($this->boundingBox->minX); - $startY = floor($this->boundingBox->minY); - $startZ = floor($this->boundingBox->minZ); - $endX = ceil($this->boundingBox->maxX); - $endY = ceil($this->boundingBox->maxY); - $endZ = ceil($this->boundingBox->maxZ); - if(!($this instanceof Painting) && !($this->isPlayer() && $this->player->isSleeping !== false)){ //TODO better way to fix $x = floor($this->x); $y = floor($this->y + $this->getEyeHeight()); @@ -1111,6 +1107,11 @@ class Entity extends Position $pk->did = -$this->data["Tile"]; $player->dataPacket($pk); } + + if($this->linkedEntity != 0 && $this->isRider){ + $player->eventHandler(["rider" => $this->eid, "riding" => $this->linkedEntity, "type" => 0], "entity.link"); //TODO fix it + } + } public function counterUpdate(){ @@ -1283,7 +1284,7 @@ class Entity extends Position ], "speedX" => $this->speedX, "speedY" => $this->speedY, - "speedZ" => $this->speedZ + "speedZ" => $this->speedZ, ]; if($this->class === ENTITY_OBJECT){ @@ -1394,12 +1395,55 @@ class Entity extends Position { return $this->setHealth(min(20, $this->getHealth() + ((int) $health)), $cause); } - - public function linkEntity(Entity $e, $type) + + public function stopRiding(){ + if(isset($this->level->entityList[$this->linkedEntity])){ + $e = $this->level->entityList[$this->linkedEntity]; + if(!$e->dead && !$e->closed){ + $this->linkedEntity = 0; + $e->linkedEntity = 0; + $this->server->api->dhandle("entity.link", ["rider" => $this->eid, "riding" => -1, "type" => 1]); + $this->server->api->dhandle("entity.link", ["rider" => $this->linkedEntity, "riding" => -1, "type" => 1]); + $this->isRider = false; + } + }else{ + $this->linkedEntity = 0; + } + } + + public function setPos($x, $y, $z){ + $this->x = $x; + $this->y = $y; + $this->z = $z; + + $this->boundingBox->minX = $x - $this->radius; + $this->boundingBox->minY = ($y - $this->yOffset); //TODO what is this + $this->ySize; + $this->boundingBox->minZ = $z - $this->radius; + + $this->boundingBox->maxX = $x + $this->radius; + $this->boundingBox->maxY = $this->boundingBox->minY + $this->height; + $this->boundingBox->maxZ = $z + $this->radius; + } + + public function setRiding(Entity $e, $type = 0) { - $this->linkedEntity = $e; - $e->linkedEntity = $this; - $this->server->api->dhandle("entity.link", ["rider" => $e->eid, "riding" => $this->eid, "type" => 0]); + if(!isset($this->level->entityList[$e->eid])){ + ConsoleAPI::warn("Tried linking $this with $e that doesnt exist in the world!"); + return; + } + + if($this->linkedEntity == $e->eid || $e->eid == $this->eid){ + $this->linkedEntity = 0; + $e->linkedEntity = 0; + $this->server->api->dhandle("entity.link", ["rider" => $this->eid, "riding" => $e->eid, "type" => $type]); + }else{ + $this->linkedEntity = $e->eid; + $e->linkedEntity = $this->eid; + $this->isRider = true; + $this->server->api->dhandle("entity.link", ["rider" => $this->eid, "riding" => $e->eid, "type" => 0]); + } + + } public function isPlayer() diff --git a/src/entity/Living.php b/src/entity/Living.php index 48ac144a..79fe2263 100644 --- a/src/entity/Living.php +++ b/src/entity/Living.php @@ -6,7 +6,7 @@ abstract class Living extends Entity implements Pathfindable{ public static $pathfinderTest = false; - public $moveStrafing, $moveForward; + /** * @var Entity $target * @var EntityAI $ai @@ -99,7 +99,7 @@ abstract class Living extends Entity implements Pathfindable{ foreach($this->level->entityListPositioned[$ind] ?? [] as $entid){ if(($this->level->entityList[$entid] ?? null) instanceof Entity){ if($this->level->entityList[$entid]->isPushable() && $bb->intersectsWith($this->level->entityList[$entid]->boundingBox)){ - $this->level->entityList[$entid]->applyCollision($this); + $this->level->entityList[$entid]->applyCollision($this, true); } } } @@ -134,15 +134,21 @@ abstract class Living extends Entity implements Pathfindable{ //$target = new PathTileXYZ($this->x + mt_rand(-10, 10), $this->y + mt_rand(-1, 1), $this->z + mt_rand(-10, 10), $this->level); $pl = null; if(count($this->level->players) > 0){ - $pl = array_values($this->level->players)[0]; + //$pl = array_values($this->level->players)[0]; //$target = new PathTileXYZ($pl->entity->x, $pl->entity->y, $pl->entity->z, $this->level); } if(self::$pathfind){ - $this->path = $this->pathFinder->navigate($this->level, (int)$this->x, (int)$this->y, (int)$this->z, (int)$pl->entity->x, (int)$pl->entity->y, (int)$pl->entity->z, 10); + $this->path = $this->pathFinder->navigate( + $this->level, + (int)$this->x, (int)$this->y, (int)$this->z, + //(int)$pl->entity->x, (int)$pl->entity->y, (int)$pl->entity->z, + (int)$this->x + mt_rand(-16, 16), (int)$this->y, (int)$this->z + mt_rand(-16, 16), + 16 + ); } - if($this->path){ + /*if($this->path){ console("Found path of length ".count($this->path)); foreach($this->path as $node){ @@ -161,7 +167,7 @@ abstract class Living extends Entity implements Pathfindable{ $player->dataPacket($pk); } } - } + }*/ } $this->pathFollower->followPath(); } @@ -189,7 +195,6 @@ abstract class Living extends Entity implements Pathfindable{ if(abs($this->speedX) < self::MIN_POSSIBLE_SPEED) $this->speedX = 0; if(abs($this->speedZ) < self::MIN_POSSIBLE_SPEED) $this->speedZ = 0; if(abs($this->speedY) < self::MIN_POSSIBLE_SPEED) $this->speedY = 0; - $this->moveStrafing *= 0.98; $this->moveForward *= 0.98; $savedLandFactor = $this->landMovementFactor; diff --git a/src/entity/ai/path/PathFollower.php b/src/entity/ai/path/PathFollower.php index a48a2293..c17cde93 100644 --- a/src/entity/ai/path/PathFollower.php +++ b/src/entity/ai/path/PathFollower.php @@ -24,7 +24,7 @@ class PathFollower{ $this->entity->ai->mobController->setMovingTarget($moveX, $moveY, $moveZ, 1.0); if($this->entity->boundingBox->isXYZInsideNS($moveX, $moveY, $moveZ)){ ++$this->entity->currentIndex; - console("next"); + //console("next"); $this->entity->currentNode = null; } @@ -35,13 +35,13 @@ class PathFollower{ $this->entity->path = null; $this->entity->currentIndex = 0; - foreach($this->entity->pathEIDS as $eid){ + /*foreach($this->entity->pathEIDS as $eid){ $pk = new RemoveEntityPacket(); $pk->eid = $eid; foreach($this->entity->level->players as $player){ $player->dataPacket($pk); } - } + }*/ } } diff --git a/src/entity/animal/Pig.php b/src/entity/animal/Pig.php index 49fb18c1..406b703f 100644 --- a/src/entity/animal/Pig.php +++ b/src/entity/animal/Pig.php @@ -18,7 +18,6 @@ class Pig extends Animal implements Rideable{ $this->ai->addTask(new TaskTempt(1.0)); $this->ai->addTask(new TaskMate(1.0)); $this->ai->addTask(new TaskFollowParent(1.1)); - } /** * @return boolean @@ -35,25 +34,32 @@ class Pig extends Animal implements Rideable{ } public function canRide($e){ - return $this->isSaddled() && !($this->linkedEntity instanceof Entity); + return $this->isSaddled() && $this->linkedEntity == 0 && $e->linkedEntity == 0; } - + + public function updateEntityMovement(){ + /*if($this->linkedEntity != 0){ + $e = $this->level->entityList[$this->linkedEntity] ?? false; + if($e instanceof Entity){ + $this->setAIMoveSpeed($this->getSpeed()); + $this->moveStrafing = $e->player->moveStrafe; + $this->moveForward = $e->player->moveForward; + $this->yaw = $e->headYaw; + } + }*/ + + parent::updateEntityMovement(); + } + public function interactWith(Entity $e, $action) { if($e->isPlayer() && $action === InteractPacket::ACTION_HOLD){ $slot = $e->player->getHeldItem(); if($this->canRide($e)){ - $this->linkedEntity = $e; - $e->isRiding = true; - $this->linkEntity($e, SetEntityLinkPacket::TYPE_RIDE); - return true; - } - if($e->isRiding && $this->linkedEntity instanceof Entity && $this->linkedEntity->eid === $e->eid){ - $this->linkEntity($e, SetEntityLinkPacket::TYPE_REMOVE); - $e->isRiding = false; - $this->linkedEntity = 0; + $e->setRiding($this); return true; } + if($slot->getID() === SADDLE){ if(!$this->isSaddled()){ $e->player->removeItem($slot->getID(), 0, 1); diff --git a/src/entity/object/Minecart.php b/src/entity/object/Minecart.php index 97ae7e26..ee99b5d3 100644 --- a/src/entity/object/Minecart.php +++ b/src/entity/object/Minecart.php @@ -54,7 +54,7 @@ class Minecart extends Vehicle{ private $minecartX = 0, $minecartY = 0, $minecartZ = 0; private $turnProgress = 0; - + public $isInReverse = false; function __construct(Level $level, $eid, $class, $type = 0, $data = []){ parent::__construct($level, $eid, $class, $type, $data); @@ -63,9 +63,9 @@ class Minecart extends Vehicle{ $this->y = isset($this->data["TileY"]) ? $this->data["TileY"]:$this->y; $this->z = isset($this->data["TileZ"]) ? $this->data["TileZ"]:$this->z; $this->setHealth(1, "generic"); //orig: 3 - $this->width = 0.98; - $this->height = 0.7; + $this->setSize(0.98, 0.7); $this->yOffset = $this->height / 2; + $this->stepHeight = 0; } public function isPickable(){ @@ -78,6 +78,350 @@ class Minecart extends Vehicle{ ]; } + public function getPos($x, $y, $z){ + $blockX = floor($x); + $blockY = floor($y); + $blockZ = floor($z); + + if(RailBaseBlock::isRailBlock($this->level, $blockX, $blockY - 1, $blockZ)) --$blockY; + + [$id, $meta] = $this->level->level->getBlock($blockX, $blockY, $blockZ); + if(RailBaseBlock::isRailID($id)){ + + if($id == POWERED_RAIL) $meta &= 7; + + $mat = self::$matrix[$meta]; + $v13 = 0; + $v15 = $blockX + 0.5 + $mat[0][0] * 0.5; + $v17 = $blockY + 0.5 + $mat[0][1] * 0.5; + $v19 = $blockZ + 0.5 + $mat[0][2] * 0.5; + + $v21 = $blockX + 0.5 + $mat[1][0] * 0.5; + $v23 = $blockY + 0.5 + $mat[1][1] * 0.5; + $v25 = $blockZ + 0.5 + $mat[1][2] * 0.5; + + $v27 = $v21 - $v15; + $v29 = ($v23 - $v17) * 2; + $v31 = $v25 - $v19; + + if($v27 == 0){ + $x = $blockX + 0.5; + $v13 = $z - $blockZ; + }else if($v31 == 0){ + $z = $blockZ + 0.5; + $v13 = $x - $blockX; + }else{ + $v33 = $x - $v15; + $v35 = $z - $v19; + $v13 = ($v33*$v27 + $v35*$v31) * 2; + } + + $x = $v15 + $v27 * $v13; + $y = $v17 + $v29 * $v13; + $z = $v19 + $v31 * $v13; + + if($v29 < 0) ++$y; + if($v29 > 0) $y += 0.5; + return [$x, $y, $z]; + } + + return false; + } + + public function moveAlongTrack($x, $y, $z, $maxSpeed, $boost, $id, $meta){ + $this->fallDistance = 0; + $vec = $this->getPos($this->x, $this->y, $this->z); + $this->y = $y; + + if($id == POWERED_RAIL) $meta &= 7; + + if($meta >= 2 && $meta <= 5) $this->y = $y + 1; + + switch($meta){ + case 2: + $this->speedX -= $boost; + break; + case 3: + $this->speedX += $boost; + break; + case 4: + $this->speedZ += $boost; + break; + case 5: + $this->speedZ -= $boost; + break; + } + + $mat = self::$matrix[$meta]; + $matXDiff = $mat[1][0] - $mat[0][0]; + $matZDiff = $mat[1][2] - $mat[0][2]; + $matDiffTotal = sqrt($matXDiff*$matXDiff + $matZDiff*$matZDiff); + + if(($this->speedZ * $matZDiff + $this->speedX * $matXDiff) < 0){ + $matXDiff = -$matXDiff; + $matZDiff = -$matZDiff; + } + $speedTotal = sqrt($this->speedZ*$this->speedZ + $this->speedX*$this->speedX); + if($speedTotal > 2) $speedTotal = 2; + $this->speedX = ($speedTotal * $matXDiff) / $matDiffTotal; + $this->speedZ = ($speedTotal * $matZDiff) / $matDiffTotal; + + if($this->linkedEntity != 0 && !$this->isRider){ + $rider = $this->level->entityList[$this->linkedEntity] ?? false; + if($rider instanceof Entity && ($rider->isPlayer() || $rider->class == ENTITY_MOB)){ + if($rider->moveForward > 0){ + $v32 = sin($this->yaw * M_PI / 180); + $v33 = cos($this->yaw * M_PI / 180); + + if($this->speedZ*$this->speedZ + $this->speedX*$this->speedX < 0.01){ + $this->speedX -= $v32*0.1; + $this->speedZ += $v33*0.1; + } + } + } + } + + $v38 = ($x + 0.5) + ($mat[0][0] * 0.5); + $v39 = ($z + 0.5) + ($mat[0][2] * 0.5); + $v40 = (($x + 0.5) + ($mat[1][0] * 0.5)) - $v38; + $v41 = (($z + 0.5) + ($mat[1][2] * 0.5)) - $v39; + + if($v40 == 0){ + $v42 = $this->z - $z; + }else if($v41 == 0){ + $v42 = $this->x - $x; + }else{ + $v44 = (($this->z - $v39) * $v41) + (($this->x - $v38) * $v40); + $v42 = $v44 + $v44; + } + + $this->x = $v38 + ($v40 * $v42); + $this->z = $v39 + ($v41 * $v42); + + $this->setPos($this->x, $this->y + $this->yOffset + 0.00001, $this->z); + $dx = $this->speedX; + $dz = $this->speedZ; + if($this->linkedEntity != 0 && !$this->isRider){ + $dx *= 0.75; + $dz *= 0.75; + } + + if($dx < -$maxSpeed) $dx = -$maxSpeed; + else if($dx > $maxSpeed) $dx = $maxSpeed; + + if($dz < -$maxSpeed) $dz = -$maxSpeed; + else if($dz > $maxSpeed) $dz = $maxSpeed; + + $this->move($dx, 0, $dz); + + if($mat[0][1]){ + if((floor($this->x) - $x) == $mat[0][0] && (floor($this->z) - $z) == $mat[0][2]){ + $this->setPos($this->x, $mat[0][1] + $this->y, $this->z); + }else if($mat[1][1]){ + goto mat_1_1; + } + }else if($mat[1][1]){ + mat_1_1: + if((floor($this->x) - $x) == $mat[1][0] && (floor($this->z) - $z) == $mat[1][2]){ + $this->setPos($this->x, $mat[1][1] + $this->y, $this->z); + } + } + + $this->applyNaturalSlowdown(); + $vec2 = $this->getPos($this->x, $this->y, $this->z); + if($vec2 !== false && $vec !== false){ + $yDiff = ($vec[1] - $vec2[1]) * 0.05; + $totalSpeed = sqrt($this->speedZ*$this->speedZ + $this->speedX*$this->speedX); + + if($totalSpeed > 0){ + $this->speedX = ($this->speedX / $totalSpeed) * ($totalSpeed + $yDiff); + $this->speedZ = ($this->speedZ / $totalSpeed) * ($totalSpeed + $yDiff); + } + $this->setPos($this->x, $vec2[1], $this->z); + } + + if(floor($this->x) != $x || floor($this->z) != $z){ //this breaks everything + $totalSpeed = sqrt($this->speedZ*$this->speedZ + $this->speedX*$this->speedX); + $this->speedX = (floor($this->x) - $x) * $totalSpeed; + $this->speedZ = (floor($this->z) - $z) * $totalSpeed; + } + + if($id == POWERED_RAIL){ + $totalSpeed = sqrt($this->speedZ*$this->speedZ + $this->speedX*$this->speedX); + if($totalSpeed > 0.01){ + $this->speedX += (($this->speedX / $totalSpeed) * 0.06); + $this->speedZ += (($this->speedZ / $totalSpeed) * 0.06); + return; + } + + if($meta == 1){ + //TODO Level::isSolidBlockingTile + if(StaticBlock::getIsSolid($this->level->level->getBlockID($x - 1, $y, $z))){ + $v63 = 0.02; + }else{ + if(!StaticBlock::getIsSolid($this->level->level->getBlockID($x + 1, $y, $z))){ + return; + } + $v63 = -0.02; + } + $this->speedX = $v63; + }else if($meta == 0){ + //TODO Level::isSolidBlockingTile + if(StaticBlock::getIsSolid($this->level->level->getBlockID($x, $y, $z - 1))){ + return; + }else{ + if(!StaticBlock::getIsSolid($this->level->level->getBlockID($x, $y, $z + 1))){ + return; + } + $v64 = -0.02; + } + $this->speedZ = $v64; + } + } + + } + + + public function applyNaturalSlowdown(){ + $mult = $this->linkedEntity != 0 && !$this->isRider ? 0.997 : 0.96; + + $this->speedX *= $mult; + $this->speedY = 0; + $this->speedZ *= $mult; + } + + public function comeOffTrack($topSpeed){ + if($this->speedX < -$topSpeed) $this->speedX = -$topSpeed; + else if($this->speedX > $topSpeed) $this->speedX = $topSpeed; + + if($this->speedZ < -$topSpeed) $this->speedZ = -$topSpeed; + else if($this->speedZ > $topSpeed) $this->speedZ = $topSpeed; + + if($this->onGround){ + $this->speedX *= 0.5; + $this->speedY *= 0.5; + $this->speedZ *= 0.5; + } + + $this->move($this->speedX, $this->speedY, $this->speedZ); + + if(!$this->onGround){ + $this->speedX *= 0.95; + $this->speedY *= 0.95; + $this->speedZ *= 0.95; + } + + } + public function applyCollision(Entity $collided){ + $diffX = $collided->x - $this->x; + $diffZ = $collided->z - $this->z; + $dist = $diffX*$diffX + $diffZ*$diffZ; + if($dist >= 0.0001){ + $sqrtMax = sqrt($dist); + $diffX /= $sqrtMax; + $diffZ /= $sqrtMax; + + $col = (($v = 1 / $sqrtMax) > 1 ? 1 : $v); + $diffX *= $col; + $diffZ *= $col; + $diffX *= 0.1; + $diffZ *= 0.1; + + $diffX *= 0.5; + $diffZ *= 0.5; + + $this->addVelocity(-$diffX, 0, -$diffZ); + $collided->addVelocity($diffX / 4, 0, $diffZ / 4); + } + //parent::applyCollision($collided); + } + public function update($now){ + if($this->closed === true){ + return false; + } + $this->updateLast(); + //$this->updatePosition(); + + $this->speedY -= 0.04; + //TODO port stuff + + $blockX = floor($this->x); + $blockY = floor($this->y); + $blockZ = floor($this->z); + + if(RailBaseBlock::isRailBlock($this->level, $blockX, $blockY - 1, $blockZ)){ + --$blockY; + } + + [$id, $meta] = $this->level->level->getBlock($blockX, $blockY, $blockZ); + if(RailBaseBlock::isRailID($id)){ + $this->moveAlongTrack($blockX, $blockY, $blockZ, 0.4, 0.0078125, $id, $meta); + //activatorRail is a cake + }else{ + $this->comeOffTrack(0.4); + } + + $this->doBlocksCollision(); + + $this->pitch = 0; + $diffX = $this->lastX - $this->x; + $diffZ = $this->lastZ - $this->z; + + if($diffX*$diffX + $diffZ*$diffZ > 0.001){ + $this->yaw = atan2($diffZ, $diffX) * 180 / M_PI; + + if($this->isInReverse) $this->yaw += 180; + } + + $yw = fmod($this->yaw - $this->lastYaw, 360); + if($yw >= 180) $yw -= 360; + if($yw < 180) $yw += 360; + + if($yw < -170 || $yw >= 170){ + $this->isInReverse = !$this->isInReverse; + $this->yaw = $this->yaw + 180; + } + + $bb = $this->boundingBox->expand(0.2, 0, 0.2); + $minChunkX = ((int)($bb->minX - 2)) >> 4; + $minChunkZ = ((int)($bb->minZ - 2)) >> 4; + $maxChunkX = ((int)($bb->minX + 2)) >> 4; + $maxChunkZ = ((int)($bb->minZ + 2)) >> 4; + + //TODO also index by chunkY? + for($chunkX = $minChunkX; $chunkX <= $maxChunkX; ++$chunkX){ + for($chunkZ = $minChunkZ; $chunkZ <= $maxChunkZ; ++$chunkZ){ + $ind = "$chunkX $chunkZ"; + foreach($this->level->entityListPositioned[$ind] ?? [] as $entid){ + $e = ($this->level->entityList[$entid] ?? null); + if($e instanceof Entity && $e->eid != $this->eid && $e->eid != $this->linkedEntity){ + if($e->isPushable() && $e->boundingBox->intersectsWith($bb)){ + if($e->isPlayer()){ + $this->applyCollision($e, true); + }else{ + $e->applyCollision($this); + } + + } + } + } + } + } + } + + public function close() + { + parent::close(); + if($this->linkedEntity != 0){ + $ent = $this->level->entityList[$this->linkedEntity] ?? false; + if($ent instanceof Entity){ + $ent->stopRiding(); + }else{ + ConsoleAPI::warn("$this is being ridden by invalid entity {$this->linkedEntity}"); + } + } + } + public function isPushable(){ return false; //TODO replace with true } @@ -87,7 +431,7 @@ class Minecart extends Vehicle{ $pk->eid = $this->eid; $pk->type = $this->type; $pk->x = $this->x; - $pk->y = $this->y; + $pk->y = $this->y; //+ $this->yOffset; $pk->z = $this->z; $pk->yaw = $this->yaw; $pk->pitch = $this->pitch; @@ -103,22 +447,17 @@ class Minecart extends Vehicle{ public function interactWith(Entity $e, $action){ if($action === InteractPacket::ACTION_HOLD && $e->isPlayer() && $this->canRide($e)){ - $this->linkedEntity = $e; - $e->isRiding = true; - $this->linkEntity($e, SetEntityLinkPacket::TYPE_RIDE); + $e->setRiding($this); return true; } - if($e->isPlayer() && $action === InteractPacket::ACTION_HOLD){ - $this->linkEntity($e, SetEntityLinkPacket::TYPE_REMOVE); - $this->linkedEntity = 0; - $e->isRiding = false; - return true; + if($action === InteractPacket::ACTION_ATTACK && $e->eid == $this->linkedEntity){ + return false; //TODO more vanilla way? } parent::interactWith($e, $action); } public function canRide($e) { - return !($this->linkedEntity instanceof Entity) && !$e->isRiding; + return $this->linkedEntity == 0 && $e->linkedEntity == 0; } } diff --git a/src/material/block/nonfull/RailBaseBlock.php b/src/material/block/nonfull/RailBaseBlock.php index 49c760c2..f74a085c 100644 --- a/src/material/block/nonfull/RailBaseBlock.php +++ b/src/material/block/nonfull/RailBaseBlock.php @@ -8,6 +8,10 @@ abstract class RailBaseBlock extends FlowableBlock //TODO move some methods here return $id === POWERED_RAIL || $id === RAIL; } + public static function isRailID($id){ + return $id == POWERED_RAIL || $id == RAIL; + } + public static function getAABB(Level $level, $x, $y, $z){ return null; } diff --git a/src/material/item/generic/MinecartItem.php b/src/material/item/generic/MinecartItem.php index a1924adb..b8923748 100755 --- a/src/material/item/generic/MinecartItem.php +++ b/src/material/item/generic/MinecartItem.php @@ -13,7 +13,7 @@ class MinecartItem extends Item{ $server = ServerAPI::request(); $data = [ "x" => $target->getX() + 0.5, - "y" => $target->getY() + 0.8, + "y" => $target->getY() + 0.5, "z" => $target->getZ() + 0.5, ]; $e = $server->api->entity->add($level, ENTITY_OBJECT, OBJECT_MINECART, $data); diff --git a/src/utils/Utils.php b/src/utils/Utils.php index 27a06662..af283412 100644 --- a/src/utils/Utils.php +++ b/src/utils/Utils.php @@ -651,4 +651,4 @@ class Utils{ //if(Utils::isOnline() === false){ //Utils::$online = false; -//} \ No newline at end of file +//} diff --git a/src/world/Level.php b/src/world/Level.php index 78b1b90b..56ffdee2 100644 --- a/src/world/Level.php +++ b/src/world/Level.php @@ -295,6 +295,7 @@ class Level{ if($extra !== false){ $entities = []; + foreach($this->entityList as $entity){ if($entity instanceof Entity){ $entities[] = $entity->createSaveData();