From 5f5c2b82de1d091eead36b49c480c98652e842be Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Sat, 8 Jun 2024 13:57:24 +0300 Subject: [PATCH 1/7] rewrote riding --- src/Player.php | 39 ++++++++++++++++-------------- src/entity/Entity.php | 43 ++++++++++++++++++++++++---------- src/entity/animal/Pig.php | 13 +++------- src/entity/object/Minecart.php | 15 +++++------- 4 files changed, 62 insertions(+), 48 deletions(-) diff --git a/src/Player.php b/src/Player.php index 16b1c31a..0422054e 100755 --- a/src/Player.php +++ b/src/Player.php @@ -724,14 +724,15 @@ 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; + if($data["type"] == $this->entity->eid){ + console("dont send to {$this}"); + break; + } $this->dataPacket($pk); + console("nya"); break; case "tile.update": if($data->level === $this->level){ @@ -2466,25 +2467,29 @@ 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; + if(strlen(bin2hex($packet->buffer)) === 24 && $this->entity->linkedEntity != 0){ + $this->entity->stopRiding(); } - if($this->entity->linkedEntity instanceof Entity){ + 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 = $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; + $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(); + $e->setPosition($this->entity); + $e->sendMoveUpdate(); //$this->entity->linkedEntity->moveFlying($packet->moveStrafe, $packet->moveForward, 1); } diff --git a/src/entity/Entity.php b/src/entity/Entity.php index 8a9edd05..810422a0 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -481,13 +481,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()); @@ -1394,12 +1387,38 @@ 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){ + $e->setRiding($e, $this->eid); //why mojang + $this->linkedEntity = 0; + $e->linkedEntity = 0; + } + }else{ + $this->linkedEntity = 0; + } + } + + 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; + } + console("unlink $this $e"); + 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->server->api->dhandle("entity.link", ["rider" => $this->eid, "riding" => $e->eid, "type" => 0]); + } + + } public function isPlayer() diff --git a/src/entity/animal/Pig.php b/src/entity/animal/Pig.php index 49fb18c1..926ef3dd 100644 --- a/src/entity/animal/Pig.php +++ b/src/entity/animal/Pig.php @@ -35,7 +35,7 @@ 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 interactWith(Entity $e, $action) @@ -43,17 +43,10 @@ class Pig extends Animal implements Rideable{ 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..4371f48c 100644 --- a/src/entity/object/Minecart.php +++ b/src/entity/object/Minecart.php @@ -102,23 +102,20 @@ class Minecart extends Vehicle{ } public function interactWith(Entity $e, $action){ + console($action); if($action === InteractPacket::ACTION_HOLD && $e->isPlayer() && $this->canRide($e)){ - $this->linkedEntity = $e; - $e->isRiding = true; - $this->linkEntity($e, SetEntityLinkPacket::TYPE_RIDE); + console("set 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; } } From fb13a6123bb69a38413a6b040b5c1a967cd8eefb Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Sat, 8 Jun 2024 22:57:49 +0300 Subject: [PATCH 2/7] fixed unmount (hopefully one last time) --- src/Player.php | 29 +++++++++++++------- src/astarnavigator/TileNavigator.php | 3 ++- src/entity/Creature.php | 4 +++ src/entity/Entity.php | 18 ++++++++++--- src/entity/Living.php | 21 +++++++++------ src/entity/ai/path/PathFollower.php | 6 ++--- src/entity/animal/Pig.php | 31 +++++++++++++++------- src/entity/object/Minecart.php | 21 +++++++++++++++ src/material/item/generic/MinecartItem.php | 2 +- src/world/Level.php | 1 + 10 files changed, 100 insertions(+), 36 deletions(-) diff --git a/src/Player.php b/src/Player.php index 0422054e..cda81a4e 100755 --- a/src/Player.php +++ b/src/Player.php @@ -76,6 +76,15 @@ class Player{ public $blockUpdateQueue; public $blockUpdateQueueLength = 0; + /** + * Sent by a client while it is linked to some entity. + * @var float $moveStrafe + * @var float $moveForward + * @var boolean $isJumping + * @var boolean $isSneaking + */ + public $moveStrafe, $moveForward, $isJumping, $isSneaking; + /** * @param integer $clientID * @param string $ip @@ -727,12 +736,7 @@ class Player{ $pk->rider = $data["rider"] == $this->entity->eid ? 0 : $data["rider"]; $pk->riding = $data["riding"] == $this->entity->eid ? 0 : $data["riding"]; $pk->type = 0; - if($data["type"] == $this->entity->eid){ - console("dont send to {$this}"); - break; - } $this->dataPacket($pk); - console("nya"); break; case "tile.update": if($data->level === $this->level){ @@ -2467,8 +2471,15 @@ class Player{ } break; case ProtocolInfo::PLAYER_INPUT_PACKET: + + $this->moveStrafe = $packet->moveStrafe; + $this->moveForward = $packet->moveForward; + $this->isJumping = $packet->isJumping; + $this->isSneaking = $packet->isSneaking; + if(strlen(bin2hex($packet->buffer)) === 24 && $this->entity->linkedEntity != 0){ $this->entity->stopRiding(); + break; } if($this->entity->linkedEntity != 0){ //TODO better riding $e = $this->entity->level->entityList[$this->entity->linkedEntity] ?? false; @@ -2477,7 +2488,7 @@ class Player{ $this->entity->stopRiding(); break; } - $pk = new SetEntityMotionPacket; + /*$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; @@ -2487,10 +2498,8 @@ class Player{ $p->dataPacket(clone $pk); } } - - $e->setPosition($this->entity); - $e->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 810422a0..a9ebe505 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; @@ -1104,6 +1105,12 @@ class Entity extends Position $pk->did = -$this->data["Tile"]; $player->dataPacket($pk); } + + if($this->linkedEntity != 0 && $this->isRider){ + console("send $player"); + $player->eventHandler(["rider" => $this->eid, "riding" => $this->linkedEntity, "type" => 0], "entity.link"); //TODO fix it + } + } public function counterUpdate(){ @@ -1276,7 +1283,7 @@ class Entity extends Position ], "speedX" => $this->speedX, "speedY" => $this->speedY, - "speedZ" => $this->speedZ + "speedZ" => $this->speedZ, ]; if($this->class === ENTITY_OBJECT){ @@ -1392,9 +1399,11 @@ class Entity extends Position if(isset($this->level->entityList[$this->linkedEntity])){ $e = $this->level->entityList[$this->linkedEntity]; if(!$e->dead && !$e->closed){ - $e->setRiding($e, $this->eid); //why mojang $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; @@ -1407,7 +1416,7 @@ class Entity extends Position ConsoleAPI::warn("Tried linking $this with $e that doesnt exist in the world!"); return; } - console("unlink $this $e"); + if($this->linkedEntity == $e->eid || $e->eid == $this->eid){ $this->linkedEntity = 0; $e->linkedEntity = 0; @@ -1415,6 +1424,7 @@ class Entity extends Position }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]); } diff --git a/src/entity/Living.php b/src/entity/Living.php index 48ac144a..dfc82c4e 100644 --- a/src/entity/Living.php +++ b/src/entity/Living.php @@ -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(); } @@ -182,14 +188,13 @@ abstract class Living extends Entity implements Pathfindable{ } - $this->ai->mobController->movementTick(); - $this->ai->mobController->rotateTick(); - $this->ai->mobController->jumpTick(); + //$this->ai->mobController->movementTick(); + //$this->ai->mobController->rotateTick(); + //$this->ai->mobController->jumpTick(); 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 926ef3dd..d754697c 100644 --- a/src/entity/animal/Pig.php +++ b/src/entity/animal/Pig.php @@ -10,15 +10,14 @@ class Pig extends Animal implements Rideable{ $this->setName("Pig"); $this->setSpeed(0.25); - $this->ai->addTask(new TaskRandomWalk(1.0)); - $this->ai->addTask(new TaskLookAtPlayer(6)); - $this->ai->addTask(new TaskPanic(1.5)); - $this->ai->addTask(new TaskLookAround()); + //$this->ai->addTask(new TaskRandomWalk(1.0)); + //$this->ai->addTask(new TaskLookAtPlayer(6)); + //$this->ai->addTask(new TaskPanic(1.5)); + //$this->ai->addTask(new TaskLookAround()); $this->ai->addTask(new TaskSwimming()); - $this->ai->addTask(new TaskTempt(1.0)); - $this->ai->addTask(new TaskMate(1.0)); - $this->ai->addTask(new TaskFollowParent(1.1)); - + //$this->ai->addTask(new TaskTempt(1.0)); + //$this->ai->addTask(new TaskMate(1.0)); + //$this->ai->addTask(new TaskFollowParent(1.1)); } /** * @return boolean @@ -37,7 +36,21 @@ class Pig extends Animal implements Rideable{ public function canRide($e){ 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){ diff --git a/src/entity/object/Minecart.php b/src/entity/object/Minecart.php index 4371f48c..8ec0d713 100644 --- a/src/entity/object/Minecart.php +++ b/src/entity/object/Minecart.php @@ -78,6 +78,27 @@ class Minecart extends Vehicle{ ]; } + public function update($now){ + if($this->closed === true){ + return false; + } + + + } + + 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 } diff --git a/src/material/item/generic/MinecartItem.php b/src/material/item/generic/MinecartItem.php index a1924adb..41f88f38 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() + 1, "z" => $target->getZ() + 0.5, ]; $e = $server->api->entity->add($level, ENTITY_OBJECT, OBJECT_MINECART, $data); 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(); From 39dbd6db34d24d721020f6d1ebdd2715d84406eb Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Sun, 9 Jun 2024 05:24:57 +0300 Subject: [PATCH 3/7] minecart pushing test --- src/Player.php | 10 ++-- src/entity/Entity.php | 2 +- src/entity/Living.php | 8 +-- src/entity/animal/Pig.php | 18 +++---- src/entity/object/Minecart.php | 94 ++++++++++++++++++++++++++++++++-- 5 files changed, 110 insertions(+), 22 deletions(-) diff --git a/src/Player.php b/src/Player.php index cda81a4e..88fca808 100755 --- a/src/Player.php +++ b/src/Player.php @@ -1263,8 +1263,8 @@ class Player{ if($e->modifySpeedY){ $e->speedY = $e->modifedSpeedY; } - if($e->speedX != 0 || $e->speedY != 0 || $e->speedZ != 0 || $e->speedY != $e->lastSpeedY || $e->speedX != $e->lastSpeedX || $e->speedZ != $e->lastSpeedZ){ - if(!($e->speedY < 0 && $e->onGround) || $e->speedX != 0 || $e->speedZ != 0 || $e->speedY != $e->lastSpeedY || $e->speedX != $e->lastSpeedX || $e->speedZ != $e->lastSpeedZ){ + //if($e->speedX != 0 || $e->speedY != 0 || $e->speedZ != 0 || $e->speedY != $e->lastSpeedY || $e->speedX != $e->lastSpeedX || $e->speedZ != $e->lastSpeedZ){ + // if(!($e->speedY < 0 && $e->onGround) || $e->speedX != 0 || $e->speedZ != 0 || $e->speedY != $e->lastSpeedY || $e->speedX != $e->lastSpeedX || $e->speedZ != $e->lastSpeedZ){ $motion = new SetEntityMotionPacket(); $motion->eid = $e->eid; $motion->speedX = $e->speedX; @@ -1274,8 +1274,8 @@ class Player{ $len += 1 + strlen($motion->buffer); ++$packets; $motionSent = true; - } - } + // } + //} if($e->modifySpeedY){ $e->speedY = $svdYSpeed; $e->modifySpeedY = false; @@ -1316,7 +1316,7 @@ class Player{ $headSent = true; } if($packets <= 0) return; - //console("Update {$e}: $packets, mot: $motionSent, mov: $moveSent, hed: $headSent"); + console("Update {$e}: $packets, mot: $motionSent, mov: $moveSent, hed: $headSent"); $MTU = $this->MTU - 24; if(($this->entityMovementQueueLength + $len) >= $MTU){ $this->sendEntityMovementUpdateQueue(); diff --git a/src/entity/Entity.php b/src/entity/Entity.php index a9ebe505..9bb15d92 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -221,7 +221,7 @@ class Entity extends Position } public function isPushable(){ - return false; + return $this->isPlayer(); } public function applyCollision(Entity $collided){ diff --git a/src/entity/Living.php b/src/entity/Living.php index dfc82c4e..bb6c7ecb 100644 --- a/src/entity/Living.php +++ b/src/entity/Living.php @@ -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); } } } @@ -188,9 +188,9 @@ abstract class Living extends Entity implements Pathfindable{ } - //$this->ai->mobController->movementTick(); - //$this->ai->mobController->rotateTick(); - //$this->ai->mobController->jumpTick(); + $this->ai->mobController->movementTick(); + $this->ai->mobController->rotateTick(); + $this->ai->mobController->jumpTick(); if(abs($this->speedX) < self::MIN_POSSIBLE_SPEED) $this->speedX = 0; if(abs($this->speedZ) < self::MIN_POSSIBLE_SPEED) $this->speedZ = 0; diff --git a/src/entity/animal/Pig.php b/src/entity/animal/Pig.php index d754697c..406b703f 100644 --- a/src/entity/animal/Pig.php +++ b/src/entity/animal/Pig.php @@ -10,14 +10,14 @@ class Pig extends Animal implements Rideable{ $this->setName("Pig"); $this->setSpeed(0.25); - //$this->ai->addTask(new TaskRandomWalk(1.0)); - //$this->ai->addTask(new TaskLookAtPlayer(6)); - //$this->ai->addTask(new TaskPanic(1.5)); - //$this->ai->addTask(new TaskLookAround()); + $this->ai->addTask(new TaskRandomWalk(1.0)); + $this->ai->addTask(new TaskLookAtPlayer(6)); + $this->ai->addTask(new TaskPanic(1.5)); + $this->ai->addTask(new TaskLookAround()); $this->ai->addTask(new TaskSwimming()); - //$this->ai->addTask(new TaskTempt(1.0)); - //$this->ai->addTask(new TaskMate(1.0)); - //$this->ai->addTask(new TaskFollowParent(1.1)); + $this->ai->addTask(new TaskTempt(1.0)); + $this->ai->addTask(new TaskMate(1.0)); + $this->ai->addTask(new TaskFollowParent(1.1)); } /** * @return boolean @@ -38,7 +38,7 @@ class Pig extends Animal implements Rideable{ } public function updateEntityMovement(){ - if($this->linkedEntity != 0){ + /*if($this->linkedEntity != 0){ $e = $this->level->entityList[$this->linkedEntity] ?? false; if($e instanceof Entity){ $this->setAIMoveSpeed($this->getSpeed()); @@ -46,7 +46,7 @@ class Pig extends Animal implements Rideable{ $this->moveForward = $e->player->moveForward; $this->yaw = $e->headYaw; } - } + }*/ parent::updateEntityMovement(); } diff --git a/src/entity/object/Minecart.php b/src/entity/object/Minecart.php index 8ec0d713..03d868a3 100644 --- a/src/entity/object/Minecart.php +++ b/src/entity/object/Minecart.php @@ -63,9 +63,8 @@ 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->yOffset = $this->height / 2; + $this->setSize(0.98, 0.7); + //$this->yOffset = $this->height / 2; } public function isPickable(){ @@ -78,12 +77,101 @@ class Minecart extends Vehicle{ ]; } + 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){ + console("speedX"); + $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){ + console("nya"); + $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; } + console($this->boundingBox); + $this->updateLast(); + $this->updatePosition(); + $this->speedY -= 0.04; + //TODO port stuff + $this->comeOffTrack(0.4); + + $bb = $this->boundingBox->expand(0.2, 0, 0.2); + $minChunkX = ((int)($bb->minX)) >> 4; + $minChunkZ = ((int)($bb->minZ)) >> 4; + $maxChunkX = ((int)($bb->minX)) >> 4; + $maxChunkZ = ((int)($bb->minZ)) >> 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() && $this->boundingBox->intersectsWith($e->boundingBox)){ + if($e->isPlayer()){ + $this->applyCollision($e, true); + }else{ + $e->applyCollision($this); + console("colliding with $e"); + } + + } + } + } + } + } + + $motion = new SetEntityMotionPacket(); + $motion->eid = $e->eid; + $motion->speedX = $e->speedX; + $motion->speedY = $e->speedY; + $motion->speedZ = $e->speedZ; + foreach($this->level->players as $p){ + $p->directDataPacket(clone $motion); + } } public function close() From 5c41d9605fe90abb8a41c817968b0c24dc2a5e0c Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Sun, 9 Jun 2024 22:08:51 +0300 Subject: [PATCH 4/7] minecart rotation serversided, better sync --- src/entity/Entity.php | 1 - src/entity/object/Minecart.php | 72 +++++++++++++------- src/material/block/nonfull/RailBaseBlock.php | 4 ++ src/material/item/generic/MinecartItem.php | 2 +- 4 files changed, 53 insertions(+), 26 deletions(-) diff --git a/src/entity/Entity.php b/src/entity/Entity.php index 9bb15d92..cb8c1909 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -1107,7 +1107,6 @@ class Entity extends Position } if($this->linkedEntity != 0 && $this->isRider){ - console("send $player"); $player->eventHandler(["rider" => $this->eid, "riding" => $this->linkedEntity, "type" => 0], "entity.link"); //TODO fix it } diff --git a/src/entity/object/Minecart.php b/src/entity/object/Minecart.php index 03d868a3..c6f7579a 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); @@ -64,7 +64,8 @@ class Minecart extends Vehicle{ $this->z = isset($this->data["TileZ"]) ? $this->data["TileZ"]:$this->z; $this->setHealth(1, "generic"); //orig: 3 $this->setSize(0.98, 0.7); - //$this->yOffset = $this->height / 2; + $this->yOffset = $this->height / 2; + $this->stepHeight = 0; } public function isPickable(){ @@ -76,7 +77,9 @@ class Minecart extends Vehicle{ [MINECART, 0, 1] ]; } - + public function moveAlongTrack($x, $y, $z, $maxSpeed, $boost, $id, $meta){ + + } public function comeOffTrack($topSpeed){ if($this->speedX < -$topSpeed) $this->speedX = -$topSpeed; else if($this->speedX > $topSpeed) $this->speedX = $topSpeed; @@ -85,7 +88,6 @@ class Minecart extends Vehicle{ else if($this->speedZ > $topSpeed) $this->speedZ = $topSpeed; if($this->onGround){ - console("speedX"); $this->speedX *= 0.5; $this->speedY *= 0.5; $this->speedZ *= 0.5; @@ -101,7 +103,6 @@ class Minecart extends Vehicle{ } public function applyCollision(Entity $collided){ - console("nya"); $diffX = $collided->x - $this->x; $diffZ = $collided->z - $this->z; $dist = $diffX*$diffX + $diffZ*$diffZ; @@ -128,20 +129,54 @@ class Minecart extends Vehicle{ if($this->closed === true){ return false; } - console($this->boundingBox); $this->updateLast(); - $this->updatePosition(); + //$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->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)) >> 4; - $minChunkZ = ((int)($bb->minZ)) >> 4; - $maxChunkX = ((int)($bb->minX)) >> 4; - $maxChunkZ = ((int)($bb->minZ)) >> 4; + $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){ @@ -150,12 +185,11 @@ class Minecart extends Vehicle{ 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() && $this->boundingBox->intersectsWith($e->boundingBox)){ + if($e->isPushable() && $e->boundingBox->intersectsWith($bb)){ if($e->isPlayer()){ $this->applyCollision($e, true); }else{ $e->applyCollision($this); - console("colliding with $e"); } } @@ -163,15 +197,6 @@ class Minecart extends Vehicle{ } } } - - $motion = new SetEntityMotionPacket(); - $motion->eid = $e->eid; - $motion->speedX = $e->speedX; - $motion->speedY = $e->speedY; - $motion->speedZ = $e->speedZ; - foreach($this->level->players as $p){ - $p->directDataPacket(clone $motion); - } } public function close() @@ -196,7 +221,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; @@ -213,7 +238,6 @@ class Minecart extends Vehicle{ public function interactWith(Entity $e, $action){ console($action); if($action === InteractPacket::ACTION_HOLD && $e->isPlayer() && $this->canRide($e)){ - console("set ride"); $e->setRiding($this); return true; } 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 41f88f38..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() + 1, + "y" => $target->getY() + 0.5, "z" => $target->getZ() + 0.5, ]; $e = $server->api->entity->add($level, ENTITY_OBJECT, OBJECT_MINECART, $data); From 7b2b914865ffe3a95c09401c89cfb96bbfa1e09f Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Mon, 10 Jun 2024 07:32:17 +0300 Subject: [PATCH 5/7] riding on rails physics port(not full) --- src/entity/Entity.php | 14 ++ src/entity/object/Minecart.php | 239 ++++++++++++++++++++++++++++++++- 2 files changed, 250 insertions(+), 3 deletions(-) diff --git a/src/entity/Entity.php b/src/entity/Entity.php index cb8c1909..1a5fdbd1 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -1409,6 +1409,20 @@ class Entity extends Position } } + 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) { if(!isset($this->level->entityList[$e->eid])){ diff --git a/src/entity/object/Minecart.php b/src/entity/object/Minecart.php index c6f7579a..f199e34e 100644 --- a/src/entity/object/Minecart.php +++ b/src/entity/object/Minecart.php @@ -77,9 +77,242 @@ class Minecart extends Vehicle{ [MINECART, 0, 1] ]; } + + 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){ + //TODO additional riding physics + /* + * if ( rider->vtable->_ZNK6Entity5isMobEv(rider) ) + { + v65 = this->base.super.rider; + if ( v65[10].base.prevZ > 0.0 ) + { + v32 = (float)((float)(v65->base.yaw * 3.1416) / 180.0) * 10430.0; + v33 = this->base.super.motionX; + v34 = Mth::_sin[(unsigned __int16)(int)v32]; + v35 = (int)(float)(v32 + 16384.0); + v36 = this->base.super.motionZ; + if ( (float)((float)(v36 * v36) + (float)(v33 * v33)) < 0.01 ) + { + v37 = v36 + (float)(Mth::_sin[v35] * 0.1); + this->base.super.motionX = v33 - (float)(v34 * 0.1); + this->base.super.motionZ = v37; + } + } + } + */ + } + + $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(); + + //TODO more + /* + * v57 = this->base.super.posZ; + memset(&result, 0, sizeof(result)); + if ( Minecart::getPos(this, &result, this->base.super.posX, this->base.super.posY, v57) && v71 ) + { + v66 = (float)(posVec1.yCoord - result.yCoord) * 0.05; + v67 = Mth::sqrt( + (float)(this->base.super.motionZ * this->base.super.motionZ) + + (float)(this->base.super.motionX * this->base.super.motionX)); + if ( v67 > 0.0 ) + { + this->base.super.motionX = (float)(this->base.super.motionX / v67) * (float)(v67 + v66); + this->base.super.motionZ = (float)(this->base.super.motionZ / v67) * (float)(v67 + v66); + } + this->vtable->_ZN6Entity6setPosEfff((Entity *)this, this->base.super.posX, result.yCoord, this->base.super.posZ); + } + v58 = Mth::floor(this->base.super.posX); + v59 = Mth::floor(this->base.super.posZ); + if ( v58 != x || v59 != z ) + { + v60 = v59 - z; + v61 = Mth::sqrt( + (float)(this->base.super.motionZ * this->base.super.motionZ) + + (float)(this->base.super.motionX * this->base.super.motionX)); + this->base.super.motionX = v61 * (float)(v58 - x); + this->base.super.motionZ = v61 * (float)v60; + } + */ + + 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; @@ -145,12 +378,12 @@ class Minecart extends Vehicle{ [$id, $meta] = $this->level->level->getBlock($blockX, $blockY, $blockZ); if(RailBaseBlock::isRailID($id)){ - //$this->moveAlongTrack($blockX, $blockY, $blockZ, 0.4, 0.0078125, $id, $meta); + $this->moveAlongTrack($blockX, $blockY, $blockZ, 0.4, 0.0078125, $id, $meta); //activatorRail is a cake }else{ - //$this->comeOffTrack(0.4); + $this->comeOffTrack(0.4); } - $this->comeOffTrack(0.4); + $this->doBlocksCollision(); $this->pitch = 0; From 376240b9bc6c49ea877e845817f13688e58b27f3 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Mon, 10 Jun 2024 13:00:54 +0300 Subject: [PATCH 6/7] player can affect motion of minecart now --- src/Player.php | 19 ++++---- src/entity/Entity.php | 2 + src/entity/Living.php | 2 +- src/entity/object/Minecart.php | 83 +++++++++++++--------------------- 4 files changed, 42 insertions(+), 64 deletions(-) diff --git a/src/Player.php b/src/Player.php index 88fca808..35a7e8c4 100755 --- a/src/Player.php +++ b/src/Player.php @@ -78,12 +78,10 @@ class Player{ /** * Sent by a client while it is linked to some entity. - * @var float $moveStrafe - * @var float $moveForward * @var boolean $isJumping * @var boolean $isSneaking */ - public $moveStrafe, $moveForward, $isJumping, $isSneaking; + public $isJumping, $isSneaking; /** * @param integer $clientID @@ -1263,8 +1261,8 @@ class Player{ if($e->modifySpeedY){ $e->speedY = $e->modifedSpeedY; } - //if($e->speedX != 0 || $e->speedY != 0 || $e->speedZ != 0 || $e->speedY != $e->lastSpeedY || $e->speedX != $e->lastSpeedX || $e->speedZ != $e->lastSpeedZ){ - // if(!($e->speedY < 0 && $e->onGround) || $e->speedX != 0 || $e->speedZ != 0 || $e->speedY != $e->lastSpeedY || $e->speedX != $e->lastSpeedX || $e->speedZ != $e->lastSpeedZ){ + if($e->speedX != 0 || $e->speedY != 0 || $e->speedZ != 0 || $e->speedY != $e->lastSpeedY || $e->speedX != $e->lastSpeedX || $e->speedZ != $e->lastSpeedZ){ + if(!($e->speedY < 0 && $e->onGround) || $e->speedX != 0 || $e->speedZ != 0 || $e->speedY != $e->lastSpeedY || $e->speedX != $e->lastSpeedX || $e->speedZ != $e->lastSpeedZ){ $motion = new SetEntityMotionPacket(); $motion->eid = $e->eid; $motion->speedX = $e->speedX; @@ -1274,8 +1272,8 @@ class Player{ $len += 1 + strlen($motion->buffer); ++$packets; $motionSent = true; - // } - //} + } + } if($e->modifySpeedY){ $e->speedY = $svdYSpeed; $e->modifySpeedY = false; @@ -1316,7 +1314,7 @@ class Player{ $headSent = true; } if($packets <= 0) return; - console("Update {$e}: $packets, mot: $motionSent, mov: $moveSent, hed: $headSent"); + //console("Update {$e}: $packets, mot: $motionSent, mov: $moveSent, hed: $headSent"); $MTU = $this->MTU - 24; if(($this->entityMovementQueueLength + $len) >= $MTU){ $this->sendEntityMovementUpdateQueue(); @@ -2471,11 +2469,10 @@ class Player{ } break; case ProtocolInfo::PLAYER_INPUT_PACKET: - - $this->moveStrafe = $packet->moveStrafe; - $this->moveForward = $packet->moveForward; $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(); diff --git a/src/entity/Entity.php b/src/entity/Entity.php index 1a5fdbd1..5fb78ba7 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -106,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(); diff --git a/src/entity/Living.php b/src/entity/Living.php index bb6c7ecb..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 diff --git a/src/entity/object/Minecart.php b/src/entity/object/Minecart.php index f199e34e..2a7f32b6 100644 --- a/src/entity/object/Minecart.php +++ b/src/entity/object/Minecart.php @@ -122,7 +122,7 @@ class Minecart extends Vehicle{ if($v29 < 0) ++$y; if($v29 > 0) $y += 0.5; - + console("$x $y $z"); return [$x, $y, $z]; } @@ -168,27 +168,18 @@ class Minecart extends Vehicle{ $this->speedZ = ($speedTotal * $matZDiff) / $matDiffTotal; if($this->linkedEntity != 0 && !$this->isRider){ - //TODO additional riding physics - /* - * if ( rider->vtable->_ZNK6Entity5isMobEv(rider) ) - { - v65 = this->base.super.rider; - if ( v65[10].base.prevZ > 0.0 ) - { - v32 = (float)((float)(v65->base.yaw * 3.1416) / 180.0) * 10430.0; - v33 = this->base.super.motionX; - v34 = Mth::_sin[(unsigned __int16)(int)v32]; - v35 = (int)(float)(v32 + 16384.0); - v36 = this->base.super.motionZ; - if ( (float)((float)(v36 * v36) + (float)(v33 * v33)) < 0.01 ) - { - v37 = v36 + (float)(Mth::_sin[v35] * 0.1); - this->base.super.motionX = v33 - (float)(v34 * 0.1); - this->base.super.motionZ = v37; - } - } - } - */ + $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); @@ -238,36 +229,24 @@ class Minecart extends Vehicle{ } $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); + } - //TODO more - /* - * v57 = this->base.super.posZ; - memset(&result, 0, sizeof(result)); - if ( Minecart::getPos(this, &result, this->base.super.posX, this->base.super.posY, v57) && v71 ) - { - v66 = (float)(posVec1.yCoord - result.yCoord) * 0.05; - v67 = Mth::sqrt( - (float)(this->base.super.motionZ * this->base.super.motionZ) - + (float)(this->base.super.motionX * this->base.super.motionX)); - if ( v67 > 0.0 ) - { - this->base.super.motionX = (float)(this->base.super.motionX / v67) * (float)(v67 + v66); - this->base.super.motionZ = (float)(this->base.super.motionZ / v67) * (float)(v67 + v66); - } - this->vtable->_ZN6Entity6setPosEfff((Entity *)this, this->base.super.posX, result.yCoord, this->base.super.posZ); - } - v58 = Mth::floor(this->base.super.posX); - v59 = Mth::floor(this->base.super.posZ); - if ( v58 != x || v59 != z ) - { - v60 = v59 - z; - v61 = Mth::sqrt( - (float)(this->base.super.motionZ * this->base.super.motionZ) - + (float)(this->base.super.motionX * this->base.super.motionX)); - this->base.super.motionX = v61 * (float)(v58 - x); - this->base.super.motionZ = v61 * (float)v60; - } - */ + /*if($this->x != $x || $this->z != $z){ this breaks everything + $totalSpeed = sqrt($this->speedZ*$this->speedZ + $this->speedX*$this->speedX); + $this->speedX = ($this->x - $x) * $totalSpeed; + $this->speedZ = ($this->z - $z) * $totalSpeed; + console(($this->x - $x).":".($this->z - $z).":".$totalSpeed); + }*/ if($id == POWERED_RAIL){ $totalSpeed = sqrt($this->speedZ*$this->speedZ + $this->speedX*$this->speedX); @@ -296,7 +275,7 @@ class Minecart extends Vehicle{ if(!StaticBlock::getIsSolid($this->level->level->getBlockID($x, $y, $z + 1))){ return; } - $v64 -= 0.02; + $v64 = -0.02; } $this->speedZ = $v64; } From 01169d658cea37aeb4d2f998769dcf3ebc812a79 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Thu, 27 Jun 2024 20:18:01 +0300 Subject: [PATCH 7/7] ew that part didnt have floor --- src/entity/object/Minecart.php | 11 ++++------- src/utils/Utils.php | 2 +- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/src/entity/object/Minecart.php b/src/entity/object/Minecart.php index 2a7f32b6..ee99b5d3 100644 --- a/src/entity/object/Minecart.php +++ b/src/entity/object/Minecart.php @@ -122,7 +122,6 @@ class Minecart extends Vehicle{ if($v29 < 0) ++$y; if($v29 > 0) $y += 0.5; - console("$x $y $z"); return [$x, $y, $z]; } @@ -241,12 +240,11 @@ class Minecart extends Vehicle{ $this->setPos($this->x, $vec2[1], $this->z); } - /*if($this->x != $x || $this->z != $z){ this breaks everything + if(floor($this->x) != $x || floor($this->z) != $z){ //this breaks everything $totalSpeed = sqrt($this->speedZ*$this->speedZ + $this->speedX*$this->speedX); - $this->speedX = ($this->x - $x) * $totalSpeed; - $this->speedZ = ($this->z - $z) * $totalSpeed; - console(($this->x - $x).":".($this->z - $z).":".$totalSpeed); - }*/ + $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); @@ -448,7 +446,6 @@ class Minecart extends Vehicle{ } public function interactWith(Entity $e, $action){ - console($action); if($action === InteractPacket::ACTION_HOLD && $e->isPlayer() && $this->canRide($e)){ $e->setRiding($this); return true; 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 +//}