From a75e286337ff20e0552eccdaae3c128d6e933d9a Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Tue, 2 Aug 2022 17:20:54 +0300 Subject: [PATCH] Minecart is in separated class --- src/API/EntityAPI.php | 3 +- src/API/LevelAPI.php | 2 +- src/entity/Entity.php | 45 ++-- src/entity/EntityRegistry.php | 4 +- src/entity/Rideable.php | 5 + src/entity/Vehicle.php | 5 + src/entity/object/Minecart.php | 41 ++++ src/math/AxisAlignedBB.php | 297 ++++++++++++++++++++++++++ src/utils/Utils.php | 26 +++ src/world/MocingObjectPosition.php | 58 +++++ src/world/generator/VoidGenerator.php | 61 ++++++ 11 files changed, 515 insertions(+), 32 deletions(-) create mode 100644 src/entity/Rideable.php create mode 100644 src/math/AxisAlignedBB.php create mode 100644 src/world/MocingObjectPosition.php create mode 100644 src/world/generator/VoidGenerator.php diff --git a/src/API/EntityAPI.php b/src/API/EntityAPI.php index 640d61a6..608e684c 100644 --- a/src/API/EntityAPI.php +++ b/src/API/EntityAPI.php @@ -59,7 +59,8 @@ class EntityAPI{ $this->server->api->console->register("spawnmob", "", [$this, "commandHandler"]); $this->server->api->console->register("despawn", "", [$this, "CommandHandler"]); } - + + public function commandHandler($cmd, $args, $issuer, $alias){ $output = ""; switch($cmd){ diff --git a/src/API/LevelAPI.php b/src/API/LevelAPI.php index 28ffffea..f3a97ea6 100644 --- a/src/API/LevelAPI.php +++ b/src/API/LevelAPI.php @@ -62,7 +62,7 @@ class LevelAPI{ $e = $this->server->api->entity->add($this->levels[$name], ENTITY_FALLING, $entity["id"], $entity); $e->setPosition(new Vector3($entity["Pos"][0], $entity["Pos"][1], $entity["Pos"][2]), $entity["Rotation"][0], $entity["Rotation"][1]); $e->setHealth($entity["Health"]); - }elseif($entity["id"] === OBJECT_PAINTING or $entity["id"] === OBJECT_ARROW){ //Painting + }elseif(Utils::getEntityTypeByID($entity["id"]) === ENTITY_OBJECT){ //Object $e = $this->server->api->entity->add($this->levels[$name], ENTITY_OBJECT, $entity["id"], $entity); $e->setPosition(new Vector3($entity["Pos"][0], $entity["Pos"][1], $entity["Pos"][2]), $entity["Rotation"][0], $entity["Rotation"][1]); $e->setHealth(1); diff --git a/src/entity/Entity.php b/src/entity/Entity.php index acca1493..58c1a388 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -108,8 +108,6 @@ class Entity extends Position{ if($this->type === OBJECT_SNOWBALL){ $this->server->schedule(1210, array($this, "update")); //Despawn $this->update(); - }elseif($this->type == OBJECT_MINECART){ - $this->update(); } break; } @@ -120,6 +118,22 @@ class Entity extends Position{ } } + public function lookOn($entity){ + $horizontal = sqrt(pow(($target->entity->x - $entity->x), 2) + pow(($target->entity->z - $entity->z) , 2)); + $vertical = $target->entity->y - ($entity->y + -0.5); /*0.5 = $entity->getEyeHeight()*/ + $pitch = -atan2($vertical, $horizontal) / M_PI * 180; //negative is up, positive is down + + $xDist = $target->entity->x - $entity->x; + $zDist = $target->entity->z - $entity->z; + + $yaw = atan2($zDist, $xDist) / M_PI * 180 - 90; + if($yaw < 0){ + $yaw += 360.0; + } + $this->yaw = $yaw; + $this->pitch = $pitch; + } + public function getDrops(){ if($this->class === ENTITY_PLAYER and $this->player instanceof Player and ($this->player->gamemode & 0x01) === 0){ $inv = array(); @@ -138,13 +152,6 @@ class Entity extends Position{ } } return $inv; - }elseif($this->class === ENTITY_OBJECT){ - switch($this->type){ - case OBJECT_MINECART: - return array( - array(MINECART, 0, 1), - ); - } } return array(); } @@ -646,26 +653,6 @@ class Entity extends Position{ $pk->speedZ = $this->speedZ; $player->dataPacket($pk); break; - case ENTITY_OBJECT: - if($this->type === OBJECT_MINECART){ - $pk = new AddEntityPacket; - $pk->eid = $this->eid; - $pk->type = $this->type; - $pk->x = $this->x; - $pk->y = $this->y; - $pk->z = $this->z; - $pk->yaw = $this->yaw; - $pk->pitch = $this->pitch; - $player->dataPacket($pk); - - $pk = new SetEntityMotionPacket; - $pk->eid = $this->eid; - $pk->speedX = $this->speedX; - $pk->speedY = $this->speedY; - $pk->speedZ = $this->speedZ; - $player->dataPacket($pk); - } - break; case ENTITY_FALLING: $pk = new AddEntityPacket; $pk->eid = $this->eid; diff --git a/src/entity/EntityRegistry.php b/src/entity/EntityRegistry.php index fd964c44..b6e26fc5 100644 --- a/src/entity/EntityRegistry.php +++ b/src/entity/EntityRegistry.php @@ -36,6 +36,7 @@ class EntityRegistry{ public static $entityList; /*Register all entities*/ public static function registerEntities(){ + console("[INFO] Registering entities..."); self::$entityList = new EntityList; self::registerEntity(Pig::class); self::registerEntity(Chicken::class); @@ -49,6 +50,7 @@ class EntityRegistry{ self::registerEntity(Arrow::class); self::registerEntity(PrimedTNT::class); self::registerEntity(Painting::class); + self::registerEntity(Minecart::class); } /*Register an Entity*/ @@ -58,7 +60,7 @@ class EntityRegistry{ //self::$entityList[$className::TYPE] = $className; self::$entityList->addEntity(new PropertyEntity($className::CLASS_TYPE, $className::TYPE, $className)); //self::$shortNames[$className] = $class->getShortName(); what is this even supposed to do? - console("[INFO] Registered a ".$className); + //console("[INFO] Registered a ".$className); return true; } diff --git a/src/entity/Rideable.php b/src/entity/Rideable.php new file mode 100644 index 00000000..411d56b0 --- /dev/null +++ b/src/entity/Rideable.php @@ -0,0 +1,5 @@ +x = isset($this->data["TileX"]) ? $this->data["TileX"]:$this->x; + $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"); + //$this->setName((isset($objects[$this->type]) ? $objects[$this->type]:$this->type)); + $this->size = 1; + + $this->update(); + } + + public function getDrops(){ + return array( + array(MINECART, 0, 1), + ); + } + + public function spawn($player){ + $pk = new AddEntityPacket; + $pk->eid = $this->eid; + $pk->type = $this->type; + $pk->x = $this->x; + $pk->y = $this->y; + $pk->z = $this->z; + $pk->yaw = $this->yaw; + $pk->pitch = $this->pitch; + $player->dataPacket($pk); + + $pk = new SetEntityMotionPacket; + $pk->eid = $this->eid; + $pk->speedX = $this->speedX; + $pk->speedY = $this->speedY; + $pk->speedZ = $this->speedZ; + $player->dataPacket($pk); + } +} \ No newline at end of file diff --git a/src/math/AxisAlignedBB.php b/src/math/AxisAlignedBB.php new file mode 100644 index 00000000..d8db3d4a --- /dev/null +++ b/src/math/AxisAlignedBB.php @@ -0,0 +1,297 @@ +minX = $minX; + $this->minY = $minY; + $this->minZ = $minZ; + $this->maxX = $maxX; + $this->maxY = $maxY; + $this->maxZ = $maxZ; + } + + public function setBounds($minX, $minY, $minZ, $maxX, $maxY, $maxZ){ + $this->minX = $minX; + $this->minY = $minY; + $this->minZ = $minZ; + $this->maxX = $maxX; + $this->maxY = $maxY; + $this->maxZ = $maxZ; + + return $this; + } + + public function addCoord($x, $y, $z){ + $minX = $this->minX; + $minY = $this->minY; + $minZ = $this->minZ; + $maxX = $this->maxX; + $maxY = $this->maxY; + $maxZ = $this->maxZ; + + if($x < 0){ + $minX += $x; + }elseif($x > 0){ + $maxX += $x; + } + + if($y < 0){ + $minY += $y; + }elseif($y > 0){ + $maxY += $y; + } + + if($z < 0){ + $minZ += $z; + }elseif($z > 0){ + $maxZ += $z; + } + + return new AxisAlignedBB($minX, $minY, $minZ, $maxX, $maxY, $maxZ); + } + + public function grow($x, $y, $z){ + return new AxisAlignedBB($this->minX - $x, $this->minY - $y, $this->minZ - $z, $this->maxX + $x, $this->maxY + $y, $this->maxZ + $z); + } + + public function expand($x, $y, $z){ + $this->minX -= $x; + $this->minY -= $y; + $this->minZ -= $z; + $this->maxX += $x; + $this->maxY += $y; + $this->maxZ += $z; + + return $this; + } + + public function offset($x, $y, $z){ + $this->minX += $x; + $this->minY += $y; + $this->minZ += $z; + $this->maxX += $x; + $this->maxY += $y; + $this->maxZ += $z; + + return $this; + } + + public function shrink($x, $y, $z){ + return new AxisAlignedBB($this->minX + $x, $this->minY + $y, $this->minZ + $z, $this->maxX - $x, $this->maxY - $y, $this->maxZ - $z); + } + + public function contract($x, $y, $z){ + $this->minX += $x; + $this->minY += $y; + $this->minZ += $z; + $this->maxX -= $x; + $this->maxY -= $y; + $this->maxZ -= $z; + + return $this; + } + + public function setBB(AxisAlignedBB $bb){ + $this->minX = $bb->minX; + $this->minY = $bb->minY; + $this->minZ = $bb->minZ; + $this->maxX = $bb->maxX; + $this->maxY = $bb->maxY; + $this->maxZ = $bb->maxZ; + return $this; + } + + public function getOffsetBoundingBox($x, $y, $z){ + return new AxisAlignedBB($this->minX + $x, $this->minY + $y, $this->minZ + $z, $this->maxX + $x, $this->maxY + $y, $this->maxZ + $z); + } + + public function calculateXOffset(AxisAlignedBB $bb, $x){ + if($bb->maxY <= $this->minY or $bb->minY >= $this->maxY){ + return $x; + } + if($bb->maxZ <= $this->minZ or $bb->minZ >= $this->maxZ){ + return $x; + } + if($x > 0 and $bb->maxX <= $this->minX){ + $x1 = $this->minX - $bb->maxX; + if($x1 < $x){ + $x = $x1; + } + } + if($x < 0 and $bb->minX >= $this->maxX){ + $x2 = $this->maxX - $bb->minX; + if($x2 > $x){ + $x = $x2; + } + } + + return $x; + } + + public function calculateYOffset(AxisAlignedBB $bb, $y){ + if($bb->maxX <= $this->minX or $bb->minX >= $this->maxX){ + return $y; + } + if($bb->maxZ <= $this->minZ or $bb->minZ >= $this->maxZ){ + return $y; + } + if($y > 0 and $bb->maxY <= $this->minY){ + $y1 = $this->minY - $bb->maxY; + if($y1 < $y){ + $y = $y1; + } + } + if($y < 0 and $bb->minY >= $this->maxY){ + $y2 = $this->maxY - $bb->minY; + if($y2 > $y){ + $y = $y2; + } + } + + return $y; + } + + public function calculateZOffset(AxisAlignedBB $bb, $z){ + if($bb->maxX <= $this->minX or $bb->minX >= $this->maxX){ + return $z; + } + if($bb->maxY <= $this->minY or $bb->minY >= $this->maxY){ + return $z; + } + if($z > 0 and $bb->maxZ <= $this->minZ){ + $z1 = $this->minZ - $bb->maxZ; + if($z1 < $z){ + $z = $z1; + } + } + if($z < 0 and $bb->minZ >= $this->maxZ){ + $z2 = $this->maxZ - $bb->minZ; + if($z2 > $z){ + $z = $z2; + } + } + + return $z; + } + + public function intersectsWith(AxisAlignedBB $bb){ + if($bb->maxX > $this->minX and $bb->minX < $this->maxX){ + if($bb->maxY > $this->minY and $bb->minY < $this->maxY){ + return $bb->maxZ > $this->minZ and $bb->minZ < $this->maxZ; + } + } + + return false; + } + + public function isVectorInside(Vector3 $vector){ + if($vector->x <= $this->minX or $vector->x >= $this->maxX){ + return false; + } + if($vector->y <= $this->minY or $vector->y >= $this->maxY){ + return false; + } + + return $vector->z > $this->minZ and $vector->z < $this->maxZ; + } + + public function getAverageEdgeLength(){ + return ($this->maxX - $this->minX + $this->maxY - $this->minY + $this->maxZ - $this->minZ) / 3; + } + + public function isVectorInYZ(Vector3 $vector){ + return $vector->y >= $this->minY and $vector->y <= $this->maxY and $vector->z >= $this->minZ and $vector->z <= $this->maxZ; + } + + public function isVectorInXZ(Vector3 $vector){ + return $vector->x >= $this->minX and $vector->x <= $this->maxX and $vector->z >= $this->minZ and $vector->z <= $this->maxZ; + } + + public function isVectorInXY(Vector3 $vector){ + return $vector->x >= $this->minX and $vector->x <= $this->maxX and $vector->y >= $this->minY and $vector->y <= $this->maxY; + } + + public function calculateIntercept(Vector3 $pos1, Vector3 $pos2){ + $v1 = $pos1->getIntermediateWithXValue($pos2, $this->minX); + $v2 = $pos1->getIntermediateWithXValue($pos2, $this->maxX); + $v3 = $pos1->getIntermediateWithYValue($pos2, $this->minY); + $v4 = $pos1->getIntermediateWithYValue($pos2, $this->maxY); + $v5 = $pos1->getIntermediateWithZValue($pos2, $this->minZ); + $v6 = $pos1->getIntermediateWithZValue($pos2, $this->maxZ); + + if($v1 !== null and !$this->isVectorInYZ($v1)){ + $v1 = null; + } + + if($v2 !== null and !$this->isVectorInYZ($v2)){ + $v2 = null; + } + + if($v3 !== null and !$this->isVectorInXZ($v3)){ + $v3 = null; + } + + if($v4 !== null and !$this->isVectorInXZ($v4)){ + $v4 = null; + } + + if($v5 !== null and !$this->isVectorInXY($v5)){ + $v5 = null; + } + + if($v6 !== null and !$this->isVectorInXY($v6)){ + $v6 = null; + } + + $vector = $v1; + + if($v2 !== null and ($vector === null or $pos1->distanceSquared($v2) < $pos1->distanceSquared($vector))){ + $vector = $v2; + } + + if($v3 !== null and ($vector === null or $pos1->distanceSquared($v3) < $pos1->distanceSquared($vector))){ + $vector = $v3; + } + + if($v4 !== null and ($vector === null or $pos1->distanceSquared($v4) < $pos1->distanceSquared($vector))){ + $vector = $v4; + } + + if($v5 !== null and ($vector === null or $pos1->distanceSquared($v5) < $pos1->distanceSquared($vector))){ + $vector = $v5; + } + + if($v6 !== null and ($vector === null or $pos1->distanceSquared($v6) < $pos1->distanceSquared($vector))){ + $vector = $v6; + } + + if($vector === null){ + return null; + } + + $f = -1; + + if($vector === $v1){ + $f = 4; + }elseif($vector === $v2){ + $f = 5; + }elseif($vector === $v3){ + $f = 0; + }elseif($vector === $v4){ + $f = 1; + }elseif($vector === $v5){ + $f = 2; + }elseif($vector === $v6){ + $f = 3; + } + + return MovingObjectPosition::fromBlock(0, 0, 0, $f, $vector); + } +} \ No newline at end of file diff --git a/src/utils/Utils.php b/src/utils/Utils.php index de36f2d6..7c608955 100644 --- a/src/utils/Utils.php +++ b/src/utils/Utils.php @@ -13,6 +13,32 @@ class Utils{ return ((@fsockopen("8.8.8.8", 80, $e = null, $n = null, 2) !== false or @fsockopen("www.linux.org", 80, $e = null, $n = null, 2) !== false or @fsockopen("www.php.net", 80, $e = null, $n = null, 2) !== false) ? true:false); }*/ + public static function getEntityTypeByID($id){ + switch($id){ + case 10: + case 11: + case 12: + case 13: + case 32: + case 33: + case 34: + case 35: + case 36: + return ENTITY_MOB; + case 62: + case 65: + case 80: + case 81: + case 82: + case 83: + case 84: + return ENTITY_OBJECT; + case 66: + return FALLING_SAND; + } + return $id; + } + public static function getCallableIdentifier(callable $variable){ if(is_array($variable)){ return sha1(strtolower(get_class($variable[0])) . "::" . strtolower($variable[1])); diff --git a/src/world/MocingObjectPosition.php b/src/world/MocingObjectPosition.php new file mode 100644 index 00000000..91e4eedd --- /dev/null +++ b/src/world/MocingObjectPosition.php @@ -0,0 +1,58 @@ +typeOfHit = 0; + $ob->blockX = $x; + $ob->blockY = $y; + $ob->blockZ = $z; + $ob->hitVector = new Vector3($hitVector->x, $hitVector->y, $hitVector->z); + return $ob; + } + + /** + * @param Entity $entity + * + * @return MovingObjectPosition + */ + public static function fromEntity(Entity $entity){ + $ob = new MovingObjectPosition; + $ob->typeOfHit = 1; + $ob->entityHit = $entity; + $ob->hitVector = new Vector3($entity->x, $entity->y, $entity->z); + return $ob; + } +} \ No newline at end of file diff --git a/src/world/generator/VoidGenerator.php b/src/world/generator/VoidGenerator.php new file mode 100644 index 00000000..2c2a37b4 --- /dev/null +++ b/src/world/generator/VoidGenerator.php @@ -0,0 +1,61 @@ +options = $options; + /*if(isset($this->options["mineshaft"])){ + $this->populators[] = new MineshaftPopulator(isset($this->options["mineshaft"]["chance"]) ? floatval($this->options["mineshaft"]["chance"]) : 0.01); + }*/ + } + + public function init(Level $level, Random $random){ + $this->level = $level; + $this->random = $random; + } + + public function generateChunk($chunkX, $chunkZ){ + for($Y = 0; $Y < 8; ++$Y){ + $this->level->setMiniChunk($chunkX, $chunkZ, $Y, $this->chunks[$Y]); + } + } + + public function populateChunk($chunkX, $chunkZ){ + foreach($this->populators as $populator){ + $this->random->setSeed((int) ($chunkX * 0xdead + $chunkZ * 0xbeef) ^ $this->level->getSeed()); + $populator->populate($this->level, $chunkX, $chunkZ, $this->random); + } + } + + public function populateLevel(){ + $this->random->setSeed($this->level->getSeed()); + } + + public function getSpawn(){ + return new Vector3(128, $this->floorLevel, 128); + } +} \ No newline at end of file