From 60be9fb2da936e65c93408ef433eb6b0d273e148 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Thu, 21 Mar 2024 04:27:58 +0300 Subject: [PATCH 1/8] arrows test --- src/Player.php | 25 ++-- src/entity/object/Arrow.php | 178 ++++++++++++++++++++--------- src/material/Block.php | 64 +++++++++-- src/material/StaticBlock.php | 57 ++++++--- src/math/Vector3.php | 16 ++- src/world/Level.php | 18 +-- src/world/MovingObjectPosition.php | 7 +- 7 files changed, 259 insertions(+), 106 deletions(-) diff --git a/src/Player.php b/src/Player.php index a30c24b6..aed5ab4e 100755 --- a/src/Player.php +++ b/src/Player.php @@ -1226,8 +1226,8 @@ class Player{ $motionSent = false; $moveSent = false; $headSent = false; - 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; @@ -1237,9 +1237,9 @@ class Player{ $len += 1 + strlen($motion->buffer); ++$packets; $motionSent = true; - } - } - if($e->x != $e->lastX || $e->y != $e->lastY || $e->z != $e->lastZ || $e->yaw != $e->lastYaw || $e->pitch != $e->lastPitch){ + // } + //} + //if($e->x != $e->lastX || $e->y != $e->lastY || $e->z != $e->lastZ || $e->yaw != $e->lastYaw || $e->pitch != $e->lastPitch){ $move = new MoveEntityPacket_PosRot(); $move->eid = $e->eid; $move->x = $e->x; @@ -1251,8 +1251,8 @@ class Player{ $len += strlen($move->buffer) + 1; ++$packets; $moveSent = true; - } - if($e->headYaw != $e->lastHeadYaw){ + //} + //if($e->headYaw != $e->lastHeadYaw){ $headyaw = new RotateHeadPacket(); $headyaw->eid = $e->eid; $headyaw->yaw = $e->headYaw; @@ -1260,7 +1260,7 @@ class Player{ $len += strlen($headyaw->buffer) + 1; ++$packets; $headSent = true; - } + //} if($packets <= 0) return; //console("Update {$e}: $packets, mot: $motionSent, mov: $moveSent, hed: $headSent"); $MTU = $this->MTU - 24; @@ -1879,9 +1879,12 @@ class Player{ //CANCEL but i am too lazy $power = 0.1; } - $e->critical = ($power === 1); - $e->shoot($e->speedX, $e->speedY, $e->speedZ, ($power+$power) * 1.5, 1.0); - $this->server->api->entity->spawnToAll($e); + if(DEBUG){ + $e->critical = ($power === 1); + $e->shoot($e->speedX, $e->speedY, $e->speedZ, ($power+$power) * 1.5, 1.0); + $this->server->api->entity->spawnToAll($e); + } + } } } diff --git a/src/entity/object/Arrow.php b/src/entity/object/Arrow.php index 20f948ab..2d053e45 100644 --- a/src/entity/object/Arrow.php +++ b/src/entity/object/Arrow.php @@ -3,12 +3,20 @@ class Arrow extends Projectile{ const TYPE = OBJECT_ARROW; - public $criticial = false; + public $shooterEID = 0; public $shotByEntity; - public $airTicks = 0; + public $inWall = false; public $groundTicks = 0; + + public $xTile, $yTile, $zTile; + public $inTile, $inData; + public $shake; + public $inGround; + public $airTicks = 0; + public $criticial = false; + function __construct(Level $level, $eid, $class, $type = 0, $data = [], $shooter = false){ parent::__construct($level, $eid, $class, $type, $data); $this->gravity = 0.05; @@ -66,68 +74,128 @@ class Arrow extends Projectile{ } } public function update($now){ - $this->needsUpdate = false; //TODO reenable - return; - //parent::update(); - if($this->closed || ($this->x > 255 || $this->x < 0 || $this->y < 0 || $this->z < 0 || $this->z > 255) || $this->groundTicks > 200) { //remove after 10 seconds in wall, idc about vanilla - $this->server->api->entity->remove($this->eid); - return; - } - + $this->lastX = $this->x; + $this->lastY = $this->y; + $this->lastZ = $this->z; + $this->lastPitch = $this->pitch; + $this->lastYaw = $this->yaw; $this->needsUpdate = true; - if($this->inWall) { - ++$this->groundTicks; - return; //yeah whatever - } - if($this->speedX != 0 or $this->speedY != 0 or $this->speedZ != 0){ - $f = sqrt(($this->speedX * $this->speedX) + ($this->speedZ * $this->speedZ)); - $this->yaw = (atan2($this->speedX, $this->speedZ) * 180 / M_PI); - $this->pitch = (atan2($this->speedY, $f) * 180 / M_PI); - } - $rt = $this->boundingBox->shrink(0.4, 0.4, 0.4)->addCoord($this->speedX, $this->speedY, $this->speedZ); - for($x = floor($rt->minX); $x < ceil($rt->maxX); ++$x){ - for($z = floor($rt->minZ); $z < ceil($rt->maxZ); ++$z){ - for($y = ceil($rt->minY); $y < ceil($rt->maxY); ++$y){ - $b = $this->level->level->getBlockID($x, $y, $z); - if(StaticBlock::getIsSolid($b)){ - $bb = StaticBlock::getBoundingBoxForBlockCoords($b, $x, $y, $z); - $this->speedY = $bb->calculateYOffset($this->boundingBox, $this->speedY); - $this->speedX = $bb->calculateXOffset($this->boundingBox, $this->speedX); - $this->speedZ = $bb->calculateZOffset($this->boundingBox, $this->speedZ); - $this->inWall = true; - break; - } + $this->handleWaterMovement(); //TODO: maybe just call parent::update(); ? + if($this->fire > 0){ + if(!$this->isImmuneToFire){ + if(($this->fire % 20) == 0){ + $this->harm(1, "burning"); } + --$this->fire; + }else{ + $this->fire -= 4; + if($this->fire <= 0) $this->fire = 0; } - } - if(!$this->inWall){ - $bbexp = $this->boundingBox->addCoord($this->speedX, $this->speedY, $this->speedZ)->expand(0, 0.2, 0); - foreach($this->level->entityList as $e){ - if($e instanceof Entity && $e->eid !== $this->eid && !$e->closed && $e->canBeShot() && $e->boundingBox->intersectsWith($bbexp)){ - if($this->shotByEntity && $this->shooterEID === $e->eid && $this->airTicks < 5) continue; - $dmg = ceil(sqrt($this->speedX * $this->speedX + $this->speedY * $this->speedY + $this->speedZ * $this->speedZ) * 2); - if($this->criticial){ - $dmg += mt_rand(0, (int)($dmg/2+1)); - } - $e->harm($dmg, $this->eid); - $this->closed = true; - break; - } + + if($this->fire <= 0){ + $this->updateMetadata(); } } - $this->x += $this->speedX; - $this->y += $this->speedY; - $this->z += $this->speedZ; - ++$this->airTicks; //TODO onGround state + if($this->handleLavaMovement()){ + if(!$this->isImmuneToFire){ + $this->harm(4, "fire"); + $oldOnFire = $this->fire; + if($oldOnFire < 20*30) $this->fire = 20*30; //30 seconds + } + } - $this->speedX *= 0.99; - $this->speedY *= 0.99; - $this->speedZ *= 0.99; - $this->speedY -= $this->gravity; + if($this->isInVoid()){ + $this->outOfWorld(); + } + //Entity::update end + + + if($this->lastPitch == $this->lastYaw && $this->lastYaw == 0){ + $v1 = sqrt($this->speedX*$this->speedX + $this->speedZ*$this->speedZ); + $this->lastYaw = $this->yaw = atan2($this->speedX, $this->speedZ) * 180 / M_PI; + $this->lastPitch = $this->pitch = atan2($this->speedY, $v1) * 180 / M_PI; + } + + $v16 = $this->level->level->getBlockID($this->xTile, $this->yTile, $this->zTile); + if($v16 > 0){ + //TODO this->inGround = true + checks + } + + if($this->shake > 0){ + --$this->shake; + } + + if($this->inGround){ + //TODO despawn after 1200 if if & meta == this->inTIle, this->inData + }else{ + ++$this->airTicks; + $start = new Vector3($this->x, $this->y, $this->z); + $end = new Vector3($this->x + $this->speedX, $this->y + $this->speedY, $this->z + $this->speedZ); + /** + * @var MovingObjectPosition $v4 + */ + $v4 = $this->level->rayTraceBlocks($start, $end); + $start = new Vector3($this->x, $this->y, $this->z); //TODO remove? + if($v4 != null){ + $end = new Vector3($v4->hitVector->x, $v4->hitVector->y, $v4->hitVector->z); + }else{ + $end = new Vector3($this->x + $this->speedX, $this->y + $this->speedY, $this->z + $this->speedZ); + } + + //TODO entity collisions + console($v4); + if($v4 != null){ + if($v4->entityHit != null){ + //TODO entity hit + }else{ + $this->xTile = $v4->blockX; + $this->yTile = $v4->blockY; + $this->zTile = $v4->blockZ; + + [$this->inTile, $this->inData] = $this->level->level->getBlock($this->xTile, $this->yTile, $this->zTile); + + $this->speedX = $v4->hitVector->x - $this->x; + $this->speedY = $v4->hitVector->y - $this->y; + $this->speedZ = $v4->hitVector->z - $this->z; + + $v21 = sqrt($this->speedX*$this->speedX + $this->speedY*$this->speedY + $this->speedZ*$this->speedZ); + $this->x -= $this->speedX / $v21 * 0.05; + $this->y -= $this->speedY / $v21 * 0.05; + $this->z -= $this->speedZ / $v21 * 0.05; + + $this->inGround = true; + $this->shake = 7; + $this->criticial = false; + } + } + + $this->x += $this->speedX; + $this->y += $this->speedY; + $this->z += $this->speedZ; + $v21 = sqrt($this->speedX*$this->speedX + $this->speedZ*$this->speedZ); + $this->yaw = atan2($this->speedX, $this->speedZ) * 180 / M_PI; + $this->pitch = atan2($this->speedY, $v21) * 180 / M_PI; + + $this->pitch = $this->lastPitch + ($this->pitch - $this->lastPitch) * 0.2; + $this->yaw = $this->lastYaw + ($this->yaw - $this->lastYaw) * 0.2; + + $v24 = $this->inWater ? 0.8 : 0.99; + $this->speedX *= $v24; + $this->speedY *= $v24; + $this->speedZ *= $v24; + $this->speedY -= 0.05; + //$this->setP + $v7 = $this->width / 2; + $v8 = $this->height; + $this->boundingBox->setBounds($this->x - $v7, $this->y - $this->yOffset /*+ $this->ySize*/, $this->z - $v7, $this->x + $v7, $this->y - $this->yOffset + $v8 /*+ $this->ySize*/, $this->z + $v7); + + $this->doBlocksCollision(); + console($this); + + } - $this->updatePosition(); } diff --git a/src/material/Block.php b/src/material/Block.php index 4884c85e..345d624b 100755 --- a/src/material/Block.php +++ b/src/material/Block.php @@ -155,19 +155,63 @@ abstract class Block extends Position{ public $z = 0; public $slipperiness; - public static $minX = 0, $minY = 0, $minZ = 0, $maxX = 1, $maxY = 1, $maxZ = 1; - public static function setBlockBounds($minX, $minY, $minZ, $maxX, $maxY, $maxZ){ //TODO call for some specific blocks - static::$minX = $minX; - static::$minY = $minY; - static::$minZ = $minZ; - static::$maxX = $maxX; - static::$maxY = $maxY; - static::$maxZ = $maxZ; - } public static function getAABB(Level $level, $x, $y, $z){ - return new AxisAlignedBB(static::$minX + $x, static::$minY + $y, static::$minZ + $z, static::$maxX + $x, static::$maxY + $y, static::$maxZ + $z); + return StaticBlock::getAABB($level, $x, $y, $z); } + public static function containsX($id, $v){ + return $v == null ? false : $v->y >= StaticBlock::$minYs[$id] && $v->y <= StaticBlock::$maxYs[$id] && $v->z >= StaticBlock::$minZs[$id] && $v->z <= StaticBlock::$maxZs[$id]; + } + + public static function containsY($id, $v){ + return $v == null ? false : $v->x >= StaticBlock::$minXs[$id] && $v->x <= StaticBlock::$maxXs[$id] && $v->z >= StaticBlock::$minZs[$id] && $v->z <= StaticBlock::$maxZs[$id]; + } + + public static function containsZ($id, $v){ + return $v == null ? false : $v->x >= StaticBlock::$minXs[$id] && $v->x <= StaticBlock::$maxXs[$id] && $v->y >= StaticBlock::$minYs[$id] && $v->y <= StaticBlock::$maxYs[$id]; + } + + public static function clip(Level $level, $x, $y, $z, Vector3 $start, Vector3 $end){ + //self::updateShape($level, $x, $y, $z); TODO update shape + $id = $level->level->getBlockID($x, $y, $z); + + $start = $start->subtract($x, $y, $z); + $end = $end->subtract($x, $y, $z); + + $v7 = $start->clipX($end, StaticBlock::$minXs[$id]); + $v8 = $start->clipX($end, StaticBlock::$maxXs[$id]); + $v9 = $start->clipY($end, StaticBlock::$minYs[$id]); + $v10 = $start->clipY($end, StaticBlock::$maxYs[$id]); + $v11 = $start->clipZ($end, StaticBlock::$minZs[$id]); + $v12 = $start->clipZ($end, StaticBlock::$maxZs[$id]); + + if(!self::containsX($id, $v7)) $v7 = null; + if(!self::containsX($id, $v8)) $v8 = null; + if(!self::containsY($id, $v9)) $v9 = null; + if(!self::containsY($id, $v10)) $v10 = null; + if(!self::containsZ($id, $v11)) $v11 = null; + if(!self::containsZ($id, $v12)) $v12 = null; + + $v13 = null; + if($v7 != null && ($v13 == null || $start->distanceSquared($v7) < $start->distanceSquared($v13))) $v13 = $v7; + if($v8 != null && ($v13 == null || $start->distanceSquared($v8) < $start->distanceSquared($v13))) $v13 = $v8; + if($v9 != null && ($v13 == null || $start->distanceSquared($v9) < $start->distanceSquared($v13))) $v13 = $v9; + if($v10 != null && ($v13 == null || $start->distanceSquared($v10) < $start->distanceSquared($v13))) $v13 = $v10; + if($v11 != null && ($v13 == null || $start->distanceSquared($v11) < $start->distanceSquared($v13))) $v13 = $v11; + if($v12 != null && ($v13 == null || $start->distanceSquared($v12) < $start->distanceSquared($v13))) $v13 = $v12; + + if($v13 == null) return null; + + $v14 = -1; + if($v13 == $v7) $v14 = 4; + if($v13 == $v8) $v14 = 5; + if($v13 == $v9) $v14 = 0; + if($v13 == $v10) $v14 = 1; + if($v13 == $v11) $v14 = 2; + if($v13 == $v12) $v14 = 3; + + return MovingObjectPosition::fromBlock($x, $y, $z, $v14, $v13->add($x, $y, $z)); + } public static function addVelocityToEntity(Level $level, $x, $y, $z, Entity $entity, Vector3 $velocityVector){} public static function onRandomTick(Level $level, $x, $y, $z){} diff --git a/src/material/StaticBlock.php b/src/material/StaticBlock.php index 7613b25c..669050ec 100644 --- a/src/material/StaticBlock.php +++ b/src/material/StaticBlock.php @@ -6,6 +6,7 @@ class StaticBlock { const DEFAULT_SLIPPERINESS = 0.6; const DEFAULT_HARDNESS = 0; + private static $NULL_BOUNDS; public static $isSolid = []; public static $isTransparent = []; @@ -19,6 +20,8 @@ class StaticBlock public static $hardness = []; public static $slipperiness = []; public static $boundingBoxes = []; + public static $minXs = [], $minYs = [], $minZs = [], $maxXs = [], $maxYs = [], $maxZs = []; + public static function init(){ self::$NULL_BOUNDS = new AxisAlignedBB(0, 0, 0, 0, 0, 0); foreach(Block::$class as $nonstaticname){ @@ -39,32 +42,35 @@ class StaticBlock FireBlock::setFlammabilityAndCatchingChance($b->getID(), 0, 0); + self::setBlockBounds($b->getID(), 0, 0, 0, 1, 1, 1); } - Block::$class[BED_BLOCK]::setBlockBounds(0, 0, 0, 1, 0.5625, 1); + self::setBlockBounds(BED_BLOCK, 0, 0, 0, 1, 0.5625, 1); //Cake: has bounds based on world state //Chest: has bounds based on world state - Block::$class[WHEAT_BLOCK]::setBlockBounds(0, 0, 0, 1, 0.25, 1); - Block::$class[CARROT_BLOCK]::setBlockBounds(0, 0, 0, 1, 0.25, 1); - Block::$class[POTATO_BLOCK]::setBlockBounds(0, 0, 0, 1, 0.25, 1); - Block::$class[BEETROOT_BLOCK]::setBlockBounds(0, 0, 0, 1, 0.25, 1); //i assume it extends class Crops - Block::$class[WHEAT_BLOCK]::setBlockBounds(0, 0, 0, 1, 0.25, 1); - Block::$class[DEAD_BUSH]::setBlockBounds(0.1, 0, 0.1, 0.9, 0.8, 0.9); - Block::$class[DOOR_BLOCK]::setBlockBounds(0, 0, 0, 1, 1, 1); //has bounds based on rotation - Block::$class[FARMLAND]::setBlockBounds(0, 0, 0, 1, 0.9375, 1); + self::setBlockBounds(WHEAT_BLOCK, 0, 0, 0, 1, 0.25, 1); + self::setBlockBounds(CARROT_BLOCK, 0, 0, 0, 1, 0.25, 1); + self::setBlockBounds(POTATO_BLOCK,0, 0, 0, 1, 0.25, 1); + self::setBlockBounds(BEETROOT_BLOCK, 0, 0, 0, 1, 0.25, 1); //i assume it extends class Crops + self::setBlockBounds(WHEAT_BLOCK, 0, 0, 0, 1, 0.25, 1); + self::setBlockBounds(DEAD_BUSH, 0.1, 0, 0.1, 0.9, 0.8, 0.9); + self::setBlockBounds(DOOR_BLOCK, 0, 0, 0, 1, 1, 1); //has bounds based on rotation + self::setBlockBounds(FARMLAND, 0, 0, 0, 1, 0.9375, 1); //Fence: bounds on state //Fence Gate: bounds on state - Block::$class[TALL_GRASS]::setBlockBounds(0.1, 0, 0.1, 0.9, 0.8, 0.9); - Block::$class[MELON_STEM]::setBlockBounds(0.5 - 0.125, 0, 0.5 - 0.125, 0.5 + 0.125, 0.25, 0.5 + 0.125); - Block::$class[PUMPKIN_STEM]::setBlockBounds(0.5 - 0.125, 0, 0.5 - 0.125, 0.5 + 0.125, 0.25, 0.5 + 0.125); - Block::$class[SAPLING]::setBlockBounds(0.1, 0, 0.1, 0.9, 0.8, 0.9); - Block::$class[BROWN_MUSHROOM]::setBlockBounds(0.3, 0, 0.3, 0.8, 0.4, 0.8); - Block::$class[SLAB]::setBlockBounds(0, 0, 0, 1, 0.5, 1); + self::setBlockBounds(TALL_GRASS, 0.1, 0, 0.1, 0.9, 0.8, 0.9); + self::setBlockBounds(MELON_STEM, 0.5 - 0.125, 0, 0.5 - 0.125, 0.5 + 0.125, 0.25, 0.5 + 0.125); + self::setBlockBounds(PUMPKIN_STEM, 0.5 - 0.125, 0, 0.5 - 0.125, 0.5 + 0.125, 0.25, 0.5 + 0.125); + self::setBlockBounds(SAPLING, 0.1, 0, 0.1, 0.9, 0.8, 0.9); + self::setBlockBounds(BROWN_MUSHROOM, 0.3, 0, 0.3, 0.8, 0.4, 0.8); + self::setBlockBounds(SLAB, 0, 0, 0, 1, 0.5, 1); //Ladder: based on rotation //Glass Pane: based on state - RailBaseBlock::setBlockBounds(0, 0, 0, 1, 0.125, 1); - Block::$class[SUGARCANE_BLOCK]::setBlockBounds(0.5 - 0.375, 0, 0.5 - 0.375, 0.5 + 0.375, 1, 0.5 + 0.375); - Block::$class[SNOW_LAYER]::setBlockBounds(0, 0, 0, 1, 0.125, 1); + self::setBlockBounds(RAIL, 0, 0, 0, 1, 0.125, 1); + self::setBlockBounds(POWERED_RAIL, 0, 0, 0, 1, 0.125, 1); + + self::setBlockBounds(SUGARCANE_BLOCK, 0.5 - 0.375, 0, 0.5 - 0.375, 0.5 + 0.375, 1, 0.5 + 0.375); + self::setBlockBounds(SNOW_LAYER, 0, 0, 0, 1, 0.125, 1); //Stairs: based on different factors //Stone wall: based on state @@ -92,6 +98,21 @@ class StaticBlock FireBlock::setFlammabilityAndCatchingChance(SPONGE, 30, 60); } + public static function setBlockBounds($blockID, $minX, $minY, $minZ, $maxX, $maxY, $maxZ){ + self::$maxXs[$blockID] = $maxX; + self::$maxYs[$blockID] = $maxY; + self::$maxZs[$blockID] = $maxZ; + + self::$minXs[$blockID] = $minX; + self::$minYs[$blockID] = $minY; + self::$minZs[$blockID] = $minZ; + } + + public static function getAABB(Level $level, $x, $y, $z){ + $id = $level->level->getBlockID($x, $y, $z); + return new AxisAlignedBB(self::$minXs[$id] + $x, self::$minYs[$id] + $y, self::$minZs[$id] + $z, self::$maxXs[$id] + $x, self::$maxYs[$id] + $y, self::$maxZs[$id] + $z); //TODO get bb from self::$boundingBoxes ? + } + public static function getBlock($id){ return Block::$class[$id] ?? Block::$class[0]; } diff --git a/src/math/Vector3.php b/src/math/Vector3.php index 9e8c9955..852ccf2e 100644 --- a/src/math/Vector3.php +++ b/src/math/Vector3.php @@ -197,7 +197,7 @@ class Vector3{ * * @return Vector3 */ - public function getIntermediateWithXValue(Vector3 $v, $x){ + public function clipX(Vector3 $v, $x){ $xDiff = $v->x - $this->x; $yDiff = $v->y - $this->y; $zDiff = $v->z - $this->z; @@ -214,6 +214,9 @@ class Vector3{ return new Vector3($this->x + $xDiff * $f, $this->y + $yDiff * $f, $this->z + $zDiff * $f); } } + public function getIntermediateWithXValue(Vector3 $v, $y){ + return $this->clipZ($v, $y); + } /** * Returns a new vector with y value equal to the second parameter, along the line between this vector and the @@ -224,7 +227,7 @@ class Vector3{ * * @return Vector3 */ - public function getIntermediateWithYValue(Vector3 $v, $y){ + public function clipY(Vector3 $v, $y){ $xDiff = $v->x - $this->x; $yDiff = $v->y - $this->y; $zDiff = $v->z - $this->z; @@ -242,6 +245,9 @@ class Vector3{ } } + public function getIntermediateWithYValue(Vector3 $v, $y){ + return $this->clipY($v, $y); + } /** * Returns a new vector with z value equal to the second parameter, along the line between this vector and the * passed in vector, or null if not possible. @@ -251,7 +257,7 @@ class Vector3{ * * @return Vector3 */ - public function getIntermediateWithZValue(Vector3 $v, $z){ + public function clipZ(Vector3 $v, $z){ $xDiff = $v->x - $this->x; $yDiff = $v->y - $this->y; $zDiff = $v->z - $this->z; @@ -268,4 +274,8 @@ class Vector3{ return new Vector3($this->x + $xDiff * $f, $this->y + $yDiff * $f, $this->z + $zDiff * $f); } } + + public function getIntermediateWithZValue(Vector3 $v, $y){ + return $this->clipZ($v, $y); + } } \ No newline at end of file diff --git a/src/world/Level.php b/src/world/Level.php index 168dff6c..33686a83 100644 --- a/src/world/Level.php +++ b/src/world/Level.php @@ -82,7 +82,7 @@ class Level{ return true; } - /* TODO neccessary for projectiles + // TODO neccessary for projectiles public function rayTraceBlocks(Vector3 $start, Vector3 $end){ $par3 = false; //TODO move to params? @@ -97,6 +97,8 @@ class Level{ [$startID, $startMeta] = $this->level->getBlock($xStart, $yStart, $zStart); $block = StaticBlock::getBlock($startID); + console("($xStart, $yStart, $zStart): $startID"); + console("start end: $start $end BEFORE"); //$block::updateShape($this, $xStart, $yStart, $zStart); //TODO better way to do it $aabb = $block::getAABB($this, $xStart, $yStart, $zStart); if($startID > 0 && $aabb != null){ //TODO also block::canColideCheck @@ -107,7 +109,6 @@ class Level{ for($i = 0; $i <= 200; ++$i){ if($xStart == $xEnd && $yStart == $yEnd && $zStart == $zEnd){ //also add checks for nan? - console("$start $end"); return null; } @@ -135,7 +136,6 @@ class Level{ if($v40) $v23 = ($v17 - $start->y) / $v29; if($v41) $v25 = ($v19 - $start->z) / $v31; - $v33 = false; if($v21 < $v23 && $v21 < $v25){ $v42 = $xEnd > $xStart ? 4 : 5; @@ -151,8 +151,8 @@ class Level{ }else{ $v42 = $zEnd > $zStart ? 2 : 3; - $start->x += $v27 * $v23; - $start->y += $v29 * $v21; + $start->x += $v27 * $v25; + $start->y += $v29 * $v25; $start->z = $v19; } @@ -167,18 +167,20 @@ class Level{ [$blockID, $blockMeta] = $this->level->getBlock($xStart, $yStart, $zStart); $block = StaticBlock::getBlock($blockID); - + console("$i($xStart, $yStart, $zStart): $blockID"); + console("start end: $start $end BEFORE"); + $aabb = $block::getAABB($this, $xStart, $yStart, $zStart); if($startID > 0 && $aabb != null){ //TODO also block::canColideCheck $v38 = $block::clip($this, $xStart, $yStart, $zStart, $start, $end); if($v38 != null) return $v38; } + console("start end: $start $end AFTER"); } return null; } - */ - //TODO mayPlace + public function isLavaInBB($aabb){ $minX = floor($aabb->minX); $maxX = floor($aabb->maxX + 1); diff --git a/src/world/MovingObjectPosition.php b/src/world/MovingObjectPosition.php index 7b495b19..486d6d68 100644 --- a/src/world/MovingObjectPosition.php +++ b/src/world/MovingObjectPosition.php @@ -23,7 +23,11 @@ class MovingObjectPosition{ protected function __construct(){ } - + + public function __toString(){ + return "MovingObjectPosition: {blockXYZ: {$this->blockX} {$this->blockY} {$this->blockZ}, side: {$this->sideHit} hitVec: {$this->hitVector} entityHit: {$this->entityHit}}"; + } + /** * @param int $x * @param int $y @@ -39,6 +43,7 @@ class MovingObjectPosition{ $ob->blockX = $x; $ob->blockY = $y; $ob->blockZ = $z; + $ob->sideHit = $side; $ob->hitVector = new Vector3($hitVector->x, $hitVector->y, $hitVector->z); return $ob; } From 2ce333af5e14428428ed14aeaa8c041ccab790a2 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Sat, 23 Mar 2024 23:33:13 +0300 Subject: [PATCH 2/8] arrows block collision fix, uncommented commented code --- src/Player.php | 78 ++++++++++++++++++------------------- src/entity/object/Arrow.php | 27 +++++++++---- src/world/Level.php | 16 +++----- 3 files changed, 64 insertions(+), 57 deletions(-) diff --git a/src/Player.php b/src/Player.php index aed5ab4e..0a3e7951 100755 --- a/src/Player.php +++ b/src/Player.php @@ -1226,8 +1226,8 @@ class Player{ $motionSent = false; $moveSent = false; $headSent = false; - //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; @@ -1237,9 +1237,9 @@ class Player{ $len += 1 + strlen($motion->buffer); ++$packets; $motionSent = true; - // } - //} - //if($e->x != $e->lastX || $e->y != $e->lastY || $e->z != $e->lastZ || $e->yaw != $e->lastYaw || $e->pitch != $e->lastPitch){ + } + } + if($e->x != $e->lastX || $e->y != $e->lastY || $e->z != $e->lastZ || $e->yaw != $e->lastYaw || $e->pitch != $e->lastPitch){ $move = new MoveEntityPacket_PosRot(); $move->eid = $e->eid; $move->x = $e->x; @@ -1251,8 +1251,8 @@ class Player{ $len += strlen($move->buffer) + 1; ++$packets; $moveSent = true; - //} - //if($e->headYaw != $e->lastHeadYaw){ + } + if($e->headYaw != $e->lastHeadYaw){ $headyaw = new RotateHeadPacket(); $headyaw->eid = $e->eid; $headyaw->yaw = $e->headYaw; @@ -1260,7 +1260,7 @@ class Player{ $len += strlen($headyaw->buffer) + 1; ++$packets; $headSent = true; - //} + } if($packets <= 0) return; //console("Update {$e}: $packets, mot: $motionSent, mov: $moveSent, hed: $headSent"); $MTU = $this->MTU - 24; @@ -1845,46 +1845,44 @@ class Player{ switch($packet->action){ case 5: //Shot arrow - if($this->entity->inAction === true){ - if($this->getSlot($this->slot)->getID() === BOW){ + if($this->entity->inAction){ + if($this->getSlot($this->slot)->getID() === BOW){ //TODO check arrow count if($this->startAction !== false){ - $time = microtime(true) - $this->startAction; - $d = [ - "x" => $this->entity->x, - "y" => $this->entity->y + 1.6, - "z" => $this->entity->z, - "yaw" => $this->entity->yaw, - "pitch" => $this->entity->pitch - ]; - $e = $this->server->api->entity->add($this->level, ENTITY_OBJECT, OBJECT_ARROW, $d); - $e->speedX = -sin(($e->yaw / 180) * M_PI) * cos(($e->pitch / 180) * M_PI); - $e->speedZ = cos(($e->yaw / 180) * M_PI) * cos(($e->pitch / 180) * M_PI); - $e->speedY = -sin(($e->pitch / 180) * M_PI); - $e->shooterEID = $this->entity->eid; - $e->shotByEntity = true; - /** - * Max usage: 72000ticks - * initalPower = 72000 - (72000 - usedCtr) - * power = initialPower / 20' - * power = (power*power+power*2)/3 - * powerMax is 1, powerMin is 0.1 - * args: xvel, yvel, zvel, (power+power)*1.5, 1.0 - */ - $initalPower = $this->entity->inActionCounter; $power = $initalPower / 20; $power = ($power * $power + $power * 2) / 3; - if($power > 1.0) $power = 1; - elseif($power < 0.1){ - //CANCEL but i am too lazy - $power = 0.1; - } - if(DEBUG){ + if($power >= 0.1){ + if($power > 1) $power = 1; + $this->server->dhandle("player.shoot", [ + "player" => $this, + "power" => &$power, + ]); + + $d = [ + "x" => $this->entity->x, + "y" => $this->entity->y + 1.6, + "z" => $this->entity->z, + "yaw" => $this->entity->yaw, + "pitch" => $this->entity->pitch + ]; + $e = $this->server->api->entity->add($this->level, ENTITY_OBJECT, OBJECT_ARROW, $d); + $e->speedX = -sin(($e->yaw / 180) * M_PI) * cos(($e->pitch / 180) * M_PI); + $e->speedZ = cos(($e->yaw / 180) * M_PI) * cos(($e->pitch / 180) * M_PI); + $e->speedY = -sin(($e->pitch / 180) * M_PI); + $e->shooterEID = $this->entity->eid; + $e->shotByEntity = true; + /** + * Max usage: 72000ticks + * initalPower = 72000 - (72000 - usedCtr) + * power = initialPower / 20' + * power = (power*power+power*2)/3 + * powerMax is 1, powerMin is 0.1 + * args: xvel, yvel, zvel, (power+power)*1.5, 1.0 + */ $e->critical = ($power === 1); $e->shoot($e->speedX, $e->speedY, $e->speedZ, ($power+$power) * 1.5, 1.0); $this->server->api->entity->spawnToAll($e); } - } } } diff --git a/src/entity/object/Arrow.php b/src/entity/object/Arrow.php index 2d053e45..f2e07c61 100644 --- a/src/entity/object/Arrow.php +++ b/src/entity/object/Arrow.php @@ -7,15 +7,13 @@ class Arrow extends Projectile{ public $shooterEID = 0; public $shotByEntity; - public $inWall = false; - public $groundTicks = 0; - public $xTile, $yTile, $zTile; public $inTile, $inData; public $shake; public $inGround; public $airTicks = 0; public $criticial = false; + public $groundTicks = 0; function __construct(Level $level, $eid, $class, $type = 0, $data = [], $shooter = false){ parent::__construct($level, $eid, $class, $type, $data); @@ -127,7 +125,25 @@ class Arrow extends Projectile{ } if($this->inGround){ - //TODO despawn after 1200 if if & meta == this->inTIle, this->inData + + $this->speedZ = $this->speedX = $this->speedY = 0; + + [$blockID, $blockMeta] = $this->level->level->getBlock($this->xTile, $this->yTile, $this->zTile); + + if($blockID == $this->inTile && $blockMeta == $this->inData){ + ++$this->groundTicks; + if($this->groundTicks >= 1200){ //TODO customizeable? + $this->close(); + return; + } + }else{ + $this->inGround = false; + $this->speedX *= lcg_value() * 0.2; + $this->speedY *= lcg_value() * 0.2; + $this->speedZ *= lcg_value() * 0.2; + $this->groundTicks = $this->airTicks = 0; + } + }else{ ++$this->airTicks; $start = new Vector3($this->x, $this->y, $this->z); @@ -144,7 +160,6 @@ class Arrow extends Projectile{ } //TODO entity collisions - console($v4); if($v4 != null){ if($v4->entityHit != null){ //TODO entity hit @@ -191,8 +206,6 @@ class Arrow extends Projectile{ $this->boundingBox->setBounds($this->x - $v7, $this->y - $this->yOffset /*+ $this->ySize*/, $this->z - $v7, $this->x + $v7, $this->y - $this->yOffset + $v8 /*+ $this->ySize*/, $this->z + $v7); $this->doBlocksCollision(); - console($this); - } diff --git a/src/world/Level.php b/src/world/Level.php index 33686a83..939564d6 100644 --- a/src/world/Level.php +++ b/src/world/Level.php @@ -97,8 +97,7 @@ class Level{ [$startID, $startMeta] = $this->level->getBlock($xStart, $yStart, $zStart); $block = StaticBlock::getBlock($startID); - console("($xStart, $yStart, $zStart): $startID"); - console("start end: $start $end BEFORE"); + //$block::updateShape($this, $xStart, $yStart, $zStart); //TODO better way to do it $aabb = $block::getAABB($this, $xStart, $yStart, $zStart); if($startID > 0 && $aabb != null){ //TODO also block::canColideCheck @@ -108,7 +107,7 @@ class Level{ for($i = 0; $i <= 200; ++$i){ - if($xStart == $xEnd && $yStart == $yEnd && $zStart == $zEnd){ //also add checks for nan? + if(is_nan($start->x) || is_nan($start->y) || is_nan($start->z) || ($xStart == $xEnd && $yStart == $yEnd && $zStart == $zEnd)){ //also add checks for nan? return null; } @@ -132,9 +131,9 @@ class Level{ $v29 = $end->y - $start->y; $v31 = $end->z - $start->z; - if($v39) $v21 = ($v15 - $start->x) / $v27; - if($v40) $v23 = ($v17 - $start->y) / $v29; - if($v41) $v25 = ($v19 - $start->z) / $v31; + if($v39) $v21 = $v27 == 0 ? ($v15 - $start->x)*INF : ($v15 - $start->x) / $v27; + if($v40) $v23 = $v29 == 0 ? ($v17 - $start->y)*INF : ($v17 - $start->y) / $v29; + if($v41) $v25 = $v31 == 0 ? ($v19 - $start->z)*INF : ($v19 - $start->z) / $v31; if($v21 < $v23 && $v21 < $v25){ $v42 = $xEnd > $xStart ? 4 : 5; @@ -167,15 +166,12 @@ class Level{ [$blockID, $blockMeta] = $this->level->getBlock($xStart, $yStart, $zStart); $block = StaticBlock::getBlock($blockID); - console("$i($xStart, $yStart, $zStart): $blockID"); - console("start end: $start $end BEFORE"); $aabb = $block::getAABB($this, $xStart, $yStart, $zStart); - if($startID > 0 && $aabb != null){ //TODO also block::canColideCheck + if($blockID > 0 && $aabb != null){ //TODO also block::canColideCheck $v38 = $block::clip($this, $xStart, $yStart, $zStart, $start, $end); if($v38 != null) return $v38; } - console("start end: $start $end AFTER"); } return null; From 3b15c26b0c94d4469269255bb426545d9b7aafb7 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Sun, 24 Mar 2024 01:52:15 +0300 Subject: [PATCH 3/8] entity collisions --- src/Player.php | 2 +- src/entity/Entity.php | 10 +++- src/entity/Living.php | 4 ++ src/entity/ai/tasks/TaskEatTileGoal.php | 9 +++- src/entity/object/Arrow.php | 67 +++++++++++++++++-------- src/entity/object/FallingSand.php | 4 ++ src/entity/object/Minecart.php | 4 ++ src/entity/object/Painting.php | 4 ++ src/entity/object/PrimedTNT.php | 4 ++ src/entity/object/TripodCamera.php | 4 ++ src/math/AxisAlignedBB.php | 19 ++++--- src/math/Vector3.php | 2 +- src/world/Level.php | 5 +- 13 files changed, 104 insertions(+), 34 deletions(-) diff --git a/src/Player.php b/src/Player.php index 0a3e7951..864a099a 100755 --- a/src/Player.php +++ b/src/Player.php @@ -1879,7 +1879,7 @@ class Player{ * powerMax is 1, powerMin is 0.1 * args: xvel, yvel, zvel, (power+power)*1.5, 1.0 */ - $e->critical = ($power === 1); + $e->critical = ($power == 1); $e->shoot($e->speedX, $e->speedY, $e->speedZ, ($power+$power) * 1.5, 1.0); $this->server->api->entity->spawnToAll($e); } diff --git a/src/entity/Entity.php b/src/entity/Entity.php index e1dd9c88..f3a635f9 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -10,7 +10,6 @@ class Entity extends Position public $counter = 0; public $fallDistance = 0; public static $updateOnTick, $allowedAI; - public $isCollidable; public $canBeAttacked; public $moveTime, $lookTime, $idleTime, $knockbackTime = 0; public $needsUpdate = true; @@ -167,6 +166,7 @@ class Entity extends Position $this->hasGravity = true; $this->canBeAttacked = true; $this->fireResistance = 20; + break; case ENTITY_OBJECT: $this->x = isset($this->data["TileX"]) ? $this->data["TileX"] : $this->x; @@ -367,6 +367,14 @@ class Entity extends Position } } + /** + * Can entity be collided with or not + * @return boolean + */ + public function isPickable(){ + return $this->isPlayer() ? !$this->dead : false; + } + public function handleLavaMovement(){ //TODO maybe try merging with water? $this->inLava = $this->level->isLavaInBB($this->boundingBox->expand(-0.1, -0.4, -0.1)); return $this->inLava; diff --git a/src/entity/Living.php b/src/entity/Living.php index 5ed6ac24..c912c351 100644 --- a/src/entity/Living.php +++ b/src/entity/Living.php @@ -30,6 +30,10 @@ abstract class Living extends Entity implements Pathfindable{ //if(self::$despawnMobs) $this->server->schedule(self::$despawnTimer, [$this, "close"]); //900*20 } + public function isPickable(){ + return !$this->dead; + } + public function fall(){ $dmg = ceil($this->fallDistance - 3); if($dmg > 0){ diff --git a/src/entity/ai/tasks/TaskEatTileGoal.php b/src/entity/ai/tasks/TaskEatTileGoal.php index 3dc4a9ec..880f06d3 100644 --- a/src/entity/ai/tasks/TaskEatTileGoal.php +++ b/src/entity/ai/tasks/TaskEatTileGoal.php @@ -15,7 +15,12 @@ class TaskEatTileGoal extends TaskBase public function onUpdate(EntityAI $ai) { - if (--$this->selfCounter === 4) + if($ai->isStarted("TaskEatTileGoal")){ + $this->reset(); + return false; + } + + if (--$this->selfCounter == 4) { $id = $ai->entity->level->level->getBlockID($ai->entity->x, $ai->entity->y, $ai->entity->z); $idb = $ai->entity->level->level->getBlockID($ai->entity->x, $ai->entity->y - 1, $ai->entity->z); @@ -32,7 +37,7 @@ class TaskEatTileGoal extends TaskBase public function canBeExecuted(EntityAI $ai) { - if($ai->isStarted("TaskRandomWalk")) return false; + if($ai->isStarted("TaskRandomWalk") || $ai->isStarted("TaskEatTileGoal")) return false; if(mt_rand(0, ($ai->entity instanceof Ageable && $ai->entity->isBaby()) ? 50 : 1000) == 0){ $idm = $ai->entity->level->level->getBlock($ai->entity->x, $ai->entity->y, $ai->entity->z); $idb = $ai->entity->level->level->getBlockID($ai->entity->x, $ai->entity->y - 1, $ai->entity->z); diff --git a/src/entity/object/Arrow.php b/src/entity/object/Arrow.php index f2e07c61..5e5993c0 100644 --- a/src/entity/object/Arrow.php +++ b/src/entity/object/Arrow.php @@ -58,19 +58,7 @@ class Arrow extends Projectile{ //$this->update(); //TODO i guess? $ticksInGround = 0; } - public function sendMoveUpdate() - { - $pk = new MoveEntityPacket_PosRot(); - $pk->eid = $this->eid; - $pk->x = $this->x; - $pk->y = $this->y; - $pk->z = $this->z; - $pk->yaw = $this->yaw; - $pk->pitch = $this->pitch; - foreach($this->level->players as $p){ //sending packets directly makes movement less laggy - $p->directDataPacket(clone $pk); - } - } + public function update($now){ $this->lastX = $this->x; $this->lastY = $this->y; @@ -115,20 +103,13 @@ class Arrow extends Projectile{ $this->lastPitch = $this->pitch = atan2($this->speedY, $v1) * 180 / M_PI; } - $v16 = $this->level->level->getBlockID($this->xTile, $this->yTile, $this->zTile); - if($v16 > 0){ - //TODO this->inGround = true + checks - } - if($this->shake > 0){ --$this->shake; } if($this->inGround){ - - $this->speedZ = $this->speedX = $this->speedY = 0; - [$blockID, $blockMeta] = $this->level->level->getBlock($this->xTile, $this->yTile, $this->zTile); + $this->speedZ = $this->speedX = $this->speedY = 0; if($blockID == $this->inTile && $blockMeta == $this->inData){ ++$this->groundTicks; @@ -159,10 +140,54 @@ class Arrow extends Projectile{ $end = new Vector3($this->x + $this->speedX, $this->y + $this->speedY, $this->z + $this->speedZ); } + + $entities = $this->level->getEntitiesInAABB($this->boundingBox->addCoord($this->speedX, $this->speedY, $this->speedZ)->expand(1, 1, 1)); + $bestDist = 0; + $bestEnt = null; + + foreach($entities as $eid => $ent){ + if($eid != $this->eid && $ent->isPickable() && ($eid != $this->shooterEID || $this->airTicks >= 5)){ + + $v12 = $ent->boundingBox->expand(0.3, 0.3, 0.3); + $v13 = $v12->calculateIntercept($start, $end); + + if($v13 != null){ + $dist = $start->distance($v13->hitVector); + + if($dist < $bestDist || $bestDist == 0){ + $bestEnt = $ent; + } + } + } + } + + if($bestEnt != null){ + $v4 = MovingObjectPosition::fromEntity($bestEnt); + } + //TODO entity collisions if($v4 != null){ if($v4->entityHit != null){ //TODO entity hit + $v49 = sqrt($this->speedY*$this->speedY + $this->speedX*$this->speedX + $this->speedZ*$this->speedZ); + $damage = ceil($v49+$v49); + + if($this->criticial){ + $damage += mt_rand(0, $damage / 2 + 1); + } + + + if($v4->entityHit->harm($damage, $this->eid)){ + //vanilla seems to increase arrow count if $v4->entity is mob + $this->close(); + }else{ + $this->speedX *= -0.1; + $this->speedY *= -0.1; + $this->speedZ *= -0.1; + $this->yaw += 180; + $this->lastYaw += 180; + $this->airTicks = 0; + } }else{ $this->xTile = $v4->blockX; $this->yTile = $v4->blockY; diff --git a/src/entity/object/FallingSand.php b/src/entity/object/FallingSand.php index 10291dca..d9e76137 100644 --- a/src/entity/object/FallingSand.php +++ b/src/entity/object/FallingSand.php @@ -16,6 +16,10 @@ class FallingSand extends Entity{ $this->gravity = 0.04; } + public function isPickable(){ + return !$this->dead; + } + public function update($now){ if($this->closed) return; diff --git a/src/entity/object/Minecart.php b/src/entity/object/Minecart.php index 32145414..8fc8156c 100644 --- a/src/entity/object/Minecart.php +++ b/src/entity/object/Minecart.php @@ -68,6 +68,10 @@ class Minecart extends Vehicle{ $this->yOffset = $this->height / 2; } + public function isPickable(){ + return !$this->dead; + } + public function getDrops(){ return [ [MINECART, 0, 1] diff --git a/src/entity/object/Painting.php b/src/entity/object/Painting.php index c43b402e..5c43fe41 100644 --- a/src/entity/object/Painting.php +++ b/src/entity/object/Painting.php @@ -20,6 +20,10 @@ class Painting extends Entity{ ]; } + public function isPickable(){ + return true; + } + public function createSaveData(){ $data = parent::createSaveData(); diff --git a/src/entity/object/PrimedTNT.php b/src/entity/object/PrimedTNT.php index 34cbe170..b25578a3 100644 --- a/src/entity/object/PrimedTNT.php +++ b/src/entity/object/PrimedTNT.php @@ -16,6 +16,10 @@ class PrimedTNT extends Entity{ //$this->server->schedule(5, [$this, "updateFuse"], [], true); } + public function isPickable(){ + return !$this->dead; + } + public function getMetadata(){ $d = parent::getMetadata(); $d[16]["value"] = (int) $this->data["fuse"]; diff --git a/src/entity/object/TripodCamera.php b/src/entity/object/TripodCamera.php index ec4baed9..6855cd3c 100644 --- a/src/entity/object/TripodCamera.php +++ b/src/entity/object/TripodCamera.php @@ -8,6 +8,10 @@ class TripodCamera extends Entity{ $this->setHealth(PHP_INT_MAX, "generic"); } + public function isPickable(){ + return !$this->dead; + } + public function interactWith(Entity $e, $action){ if($e->isPlayer() and $action === InteractPacket::ACTION_HOLD){ //todo diff --git a/src/math/AxisAlignedBB.php b/src/math/AxisAlignedBB.php index 5415c001..7708fe3b 100644 --- a/src/math/AxisAlignedBB.php +++ b/src/math/AxisAlignedBB.php @@ -220,14 +220,19 @@ class AxisAlignedBB{ 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; } - + + /** + * @param Vector3 $pos1 + * @param Vector3 $pos2 + * @return NULL|MovingObjectPosition + */ 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); + $v1 = $pos1->clipX($pos2, $this->minX); + $v2 = $pos1->clipX($pos2, $this->maxX); + $v3 = $pos1->clipY($pos2, $this->minY); + $v4 = $pos1->clipY($pos2, $this->maxY); + $v5 = $pos1->clipZ($pos2, $this->minZ); + $v6 = $pos1->clipZ($pos2, $this->maxZ); if($v1 !== null and !$this->isVectorInYZ($v1)){ $v1 = null; diff --git a/src/math/Vector3.php b/src/math/Vector3.php index 852ccf2e..f72e0461 100644 --- a/src/math/Vector3.php +++ b/src/math/Vector3.php @@ -215,7 +215,7 @@ class Vector3{ } } public function getIntermediateWithXValue(Vector3 $v, $y){ - return $this->clipZ($v, $y); + return $this->clipX($v, $y); } /** diff --git a/src/world/Level.php b/src/world/Level.php index 939564d6..3f3cfb5b 100644 --- a/src/world/Level.php +++ b/src/world/Level.php @@ -630,7 +630,10 @@ class Level{ } return $ents; } - + /** + * @param AxisAlignedBB $bb + * @return Entity[] + */ public function getEntitiesInAABB(AxisAlignedBB $bb){ $minChunkX = ((int)($bb->minX)) >> 4; $minChunkZ = ((int)($bb->minZ)) >> 4; From db5ad749d222b656e19d7b7390d01176740f717a Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Sun, 24 Mar 2024 02:48:40 +0300 Subject: [PATCH 4/8] skeletons will attack the player --- src/entity/Entity.php | 6 + src/entity/ai/EntityAI.php | 6 + src/entity/ai/tasks/TaskLookAround.php | 2 +- src/entity/ai/tasks/TaskRandomWalk.php | 4 +- src/entity/ai/tasks/TaskRangedAttack.php | 147 +++++++++++++++++++++++ src/entity/monster/Skeleton.php | 2 +- 6 files changed, 163 insertions(+), 4 deletions(-) create mode 100644 src/entity/ai/tasks/TaskRangedAttack.php diff --git a/src/entity/Entity.php b/src/entity/Entity.php index f3a635f9..6293a201 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -1,5 +1,7 @@ level->rayTraceBlocks(new Vector($this->x, $this->y + $this->getEyeHeight(), $this->z), new Vector($e->x, $e->y + $e->getEyeHeight(), $e->z)) == null; + } + /** * Can entity be collided with or not * @return boolean diff --git a/src/entity/ai/EntityAI.php b/src/entity/ai/EntityAI.php index e6ec6f67..1552ee27 100644 --- a/src/entity/ai/EntityAI.php +++ b/src/entity/ai/EntityAI.php @@ -24,6 +24,12 @@ class EntityAI $this->mobController = new MobController($entity); } + + public function canSee(Entity $entity){ + //TODO caches + return $this->entity->canSee($entity); + } + /** * Add a task for entity * @param TaskBase $task diff --git a/src/entity/ai/tasks/TaskLookAround.php b/src/entity/ai/tasks/TaskLookAround.php index bb25edc5..a931d98a 100644 --- a/src/entity/ai/tasks/TaskLookAround.php +++ b/src/entity/ai/tasks/TaskLookAround.php @@ -19,7 +19,7 @@ class TaskLookAround extends TaskBase public function canBeExecuted(EntityAI $ai) { - return !$ai->entity->inPanic && !$ai->entity->isMoving() && lcg_value() < 0.02 && !$ai->isStarted("TaskLookAtPlayer") && !$ai->isStarted("TaskMate") && !$ai->isStarted("TaskTempt") && !$ai->entity->hasPath(); /*Vanilla value*/ + return !$ai->entity->inPanic && !$ai->entity->isMoving() && lcg_value() < 0.02 && !$ai->isStarted("TaskLookAtPlayer") && !$ai->isStarted("TaskAttackPlayer") && !$ai->isStarted("TaskMate") && !$ai->isStarted("TaskTempt") && !$ai->entity->hasPath(); /*Vanilla value*/ } public function onUpdate(EntityAI $ai) diff --git a/src/entity/ai/tasks/TaskRandomWalk.php b/src/entity/ai/tasks/TaskRandomWalk.php index ad64ad61..4b68a746 100644 --- a/src/entity/ai/tasks/TaskRandomWalk.php +++ b/src/entity/ai/tasks/TaskRandomWalk.php @@ -26,7 +26,7 @@ class TaskRandomWalk extends TaskBase public function onUpdate(EntityAI $ai) { - if(($ai->entity instanceof Creeper && $ai->entity->isIgnited()) || $ai->isStarted("TaskTempt") || $ai->isStarted("TaskFollowParent") ) { + if(($ai->entity instanceof Creeper && $ai->entity->isIgnited()) || $ai->isStarted("TaskTempt") || $ai->isStarted("TaskFollowParent") || $ai->isStarted("TaskRangedAttack")) { $this->reset(); return false; //TODO Better way: block movement } @@ -37,7 +37,7 @@ class TaskRandomWalk extends TaskBase public function canBeExecuted(EntityAI $ai) { - if(($ai->entity instanceof Creeper && $ai->entity->isIgnited()) || $ai->entity->hasPath() || $ai->isStarted("TaskTempt") ||$ai->isStarted("TaskAttackPlayer")) { + if(($ai->entity instanceof Creeper && $ai->entity->isIgnited()) || $ai->entity->hasPath() || $ai->isStarted("TaskTempt") || $ai->isStarted("TaskAttackPlayer") || $ai->isStarted("TaskRangedAttack")) { return false; } // i really need mutexBits return !$ai->entity->inPanic && !$ai->isStarted("TaskMate") && !$ai->isStarted("TaskEatTileGoal") && !$ai->isStarted("TaskLookAround") && !$ai->isStarted("TaskFollowParent") && !$ai->isStarted("TaskLookAtPlayer") && mt_rand(0, 120) == 0; diff --git a/src/entity/ai/tasks/TaskRangedAttack.php b/src/entity/ai/tasks/TaskRangedAttack.php new file mode 100644 index 00000000..161cde28 --- /dev/null +++ b/src/entity/ai/tasks/TaskRangedAttack.php @@ -0,0 +1,147 @@ +speedMultiplier = $speed; + $this->rangeSquared = $range*$range; + $this->server = ServerAPI::request(); + } + + public function onStart(EntityAI $ai) + { + $this->selfCounter = 1; + $this->ticksNoSeen = 0; + } + + public function onEnd(EntityAI $ai) + { + $ai->entity->target = false; + + } + + public function onUpdate(EntityAI $ai) + { + if(!$this->isTargetValid($ai)){ + $this->reset(); + $this->onEnd($ai); + return false; + } + --$this->attackCounter; + //if(!$ai->canSee($ai->entity->target)){ useless without pathfinder =< + // ++$this->ticksNoSeen; + //}else{ + // $this->ticksNoSeen = 0; + //} + + $dist = $ai->entity->distanceSquared($ai->entity->target); + + if($dist > 100){ + $ai->mobController->setMovingTarget($ai->entity->target->x, $ai->entity->target->y, $ai->entity->target->z, $this->speedMultiplier); + } + + $ai->mobController->setLookPosition($ai->entity->target->x, $ai->entity->target->y + 0.12, $ai->entity->target->z, 10, $ai->entity->getVerticalFaceSpeed()); + + if($this->attackCounter <= 0){ + $this->rangedAttack($ai->entity, $ai->entity->target); + $this->attackCounter = 60; + } + } + + public function rangedAttack($selfEntity, $target){ + $d = [ + "x" => $selfEntity->x, + "y" => $selfEntity->y + 1.6, + "z" => $selfEntity->z, + "yaw" => $selfEntity->yaw, + "pitch" => $selfEntity->pitch + ]; + /** + * @var Arrow $arrow + */ + $arrow = $this->server->api->entity->add($selfEntity->level, ENTITY_OBJECT, OBJECT_ARROW, $d); + $arrow->shotByEntity = true; + $arrow->shooterEID = $selfEntity->eid; + $posY = ($target->y + $target->getEyeHeight() - 0.1); + $diffX = $target->x - $selfEntity->x; + $diffY = ($target->boundingBox->minY + ($target->height / 3)) - $posY; + $diffZ = $target->z - $selfEntity->z; + $v12 = sqrt($diffX * $diffX + $diffZ * $diffZ); + //if($v12 >= 1.0e-7){ + $yaw = ((atan2($diffZ, $diffX) * 180) / M_PI) - 90; + $pitch = -((atan2($diffY, $v12) * 180) / M_PI); + $v16 = $diffX / $v12; + $v18 = $diffZ / $v12; + $arrow->x = $selfEntity->x + $v16; + $arrow->y = $posY; + $arrow->z = $selfEntity->z + $v18; + $arrow->yaw = $yaw; + $arrow->pitch = $pitch; + $v20 = $v12 * 0.2; + $arrow->shoot($diffX, $diffY + $v20, $diffZ, 1.6, 12); + $this->server->api->entity->spawnToAll($arrow); + //} + } + + + public function isTargetValid(EntityAI $ai){ + $e = $ai->entity; + if($e->target instanceof Entity && !$e->target->closed){ + $t = $e->target; + $xDiff = ($t->x - $e->x); + $yDiff = ($t->y - $e->y); + $zDiff = ($t->z - $e->z); + return ($xDiff*$xDiff + $yDiff*$yDiff + $zDiff*$zDiff) <= $this->rangeSquared; + } + return false; + } + + public function tryTargeting(EntityAI $ai){ + $e = $ai->entity; + if($e->target instanceof Entity){ + $t = $e->target; + $xDiff = ($t->x - $e->x); + $yDiff = ($t->y - $e->y); + $zDiff = ($t->z - $e->z); + if(($xDiff*$xDiff + $yDiff*$yDiff + $zDiff*$zDiff) <= $this->rangeSquared){ + return true; + } + } + $bestTargetDistance = INF; + $closestTarget = null; + foreach($e->level->players as $p){ + if($p->spawned){ + $pt = $p->entity; + $xDiff = $pt->x - $e->x; + $yDiff = $pt->y - $e->y; + $zDiff = $pt->z - $e->z; + $d = ($xDiff*$xDiff + $yDiff*$yDiff + $zDiff*$zDiff); + if($d <= $this->rangeSquared){ + if($bestTargetDistance >= $d){ + $closestTarget = $pt; + $bestTargetDistance = $d; + } + } + } + } + + if($closestTarget != null){ + $e->target = $closestTarget; //TODO dont save entity object ? + return true; + } + return false; + } + + + public function canBeExecuted(EntityAI $ai) + { + return $this->tryTargeting($ai); + } + +} + diff --git a/src/entity/monster/Skeleton.php b/src/entity/monster/Skeleton.php index 8b275661..5d90b4d3 100644 --- a/src/entity/monster/Skeleton.php +++ b/src/entity/monster/Skeleton.php @@ -8,11 +8,11 @@ class Skeleton extends Monster{ $this->setName("Skeleton"); $this->ai->removeTask("TaskAttackPlayer"); $this->setSpeed(0.25); - //$this->ai->addTask(new TaskDestroyServerPerformance()); $this->ai->addTask(new TaskRandomWalk(1.0)); $this->ai->addTask(new TaskLookAround()); $this->ai->addTask(new TaskSwimming()); + $this->ai->addTask(new TaskRangedAttack(1.0, 16)); } public function getAttackDamage(){ From 0a1a4f33f3af0c55b3563a99f18e9641741b7036 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Sun, 24 Mar 2024 02:49:58 +0300 Subject: [PATCH 5/8] why did u use \Ds\Vector --- src/entity/Entity.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/entity/Entity.php b/src/entity/Entity.php index 6293a201..4091b880 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -1,7 +1,5 @@ level->rayTraceBlocks(new Vector($this->x, $this->y + $this->getEyeHeight(), $this->z), new Vector($e->x, $e->y + $e->getEyeHeight(), $e->z)) == null; + return $this->level->rayTraceBlocks(new Vector3($this->x, $this->y + $this->getEyeHeight(), $this->z), new Vector3($e->x, $e->y + $e->getEyeHeight(), $e->z)) == null; } /** From 105b09e28f39405a69b07fe98d71a12ecdf91e73 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Sun, 24 Mar 2024 03:18:07 +0300 Subject: [PATCH 6/8] bow will consume durability, arrows will be deleted --- src/Player.php | 14 ++++++++++++-- src/entity/Entity.php | 6 +++++- src/material/item/tool/BowItem.php | 2 +- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/Player.php b/src/Player.php index 864a099a..951acc64 100755 --- a/src/Player.php +++ b/src/Player.php @@ -633,7 +633,6 @@ class Player{ foreach($this->hotbar as $slot){ $hotbar[] = $slot <= -1 ? -1 : $slot + 9; } - $pk = new ContainerSetContentPacket; $pk->windowid = 0; $pk->slots = $this->inventory; @@ -1846,7 +1845,8 @@ class Player{ switch($packet->action){ case 5: //Shot arrow if($this->entity->inAction){ - if($this->getSlot($this->slot)->getID() === BOW){ //TODO check arrow count + $arrowSlot = $this->hasItem(ARROW); + if($this->getSlot($this->slot)->getID() === BOW && (($this->gamemode & 0x01) == 0x1 || $arrowSlot !== false)){ //TODO check arrow count if($this->startAction !== false){ $initalPower = $this->entity->inActionCounter; $power = $initalPower / 20; @@ -1882,8 +1882,18 @@ class Player{ $e->critical = ($power == 1); $e->shoot($e->speedX, $e->speedY, $e->speedZ, ($power+$power) * 1.5, 1.0); $this->server->api->entity->spawnToAll($e); + if(($this->gamemode & 0x01) == 0x0) { + $bow = $this->getSlot($this->slot); + if(++$bow->meta >= $bow->getMaxDurability()){ + $this->inventory[$this->slot] = BlockAPI::getItem(AIR, 0, 0); + } + $this->removeItem(ARROW, 0, 1, false); + $this->sendInventory(); + } } } + }else{ //inv desynced, resend + $this->sendInventory(); } } $this->startAction = false; diff --git a/src/entity/Entity.php b/src/entity/Entity.php index 4091b880..43d9f737 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -1318,7 +1318,11 @@ class Entity extends Position } $dmg = $this->applyArmor($dmg, $cause); $ret = $this->setHealth(max(- 128, $this->getHealth() - ((int) $dmg)), $cause, $force); - + + if($this->isPlayer() && $this->player->gamemode & 0x01 == 0x01){ + return false; + } + if ($ret != false && $this->hasKnockback && is_numeric($cause) && ($entity = $this->server->api->entity->get($cause)) != false) { $d = $entity->x - $this->x; diff --git a/src/material/item/tool/BowItem.php b/src/material/item/tool/BowItem.php index 0b2d5ef8..6adf23dd 100644 --- a/src/material/item/tool/BowItem.php +++ b/src/material/item/tool/BowItem.php @@ -2,7 +2,7 @@ class BowItem extends Item{ public function __construct($meta = 0, $count = 1){ - parent::__construct(BOW, 0, $count, "Bow"); + parent::__construct(BOW, $meta, $count, "Bow"); } public function isTool(){ //it is tool too return true; From 3df03ae96b16e8eb619e285487b3d5c5e68e6fb6 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Sun, 24 Mar 2024 05:35:38 +0300 Subject: [PATCH 7/8] snowballs and eggs --- src/Player.php | 52 ++++++- src/entity/Entity.php | 32 ++-- src/entity/Projectile.php | 194 +++++++++++++++++++++++++ src/entity/object/Arrow.php | 7 +- src/entity/object/Egg.php | 0 src/entity/object/Snowball.php | 0 src/entity/object/ThrownEgg.php | 32 ++++ src/entity/object/ThrownSnowball.php | 11 ++ src/entity/registry/EntityRegistry.php | 6 +- src/world/MovingObjectPosition.php | 4 +- 10 files changed, 304 insertions(+), 34 deletions(-) delete mode 100644 src/entity/object/Egg.php delete mode 100644 src/entity/object/Snowball.php create mode 100644 src/entity/object/ThrownEgg.php create mode 100644 src/entity/object/ThrownSnowball.php diff --git a/src/Player.php b/src/Player.php index 951acc64..3a1352f9 100755 --- a/src/Player.php +++ b/src/Player.php @@ -1752,7 +1752,7 @@ class Player{ } $blockVector = new Vector3($packet->x, $packet->y, $packet->z); - + if(($this->spawned === false or $this->blocked === true) and $packet->face >= 0 and $packet->face <= 5){ $target = $this->level->getBlock($blockVector); $block = $target->getSide($packet->face); @@ -1827,11 +1827,51 @@ class Player{ $pk->meta = $block->getMetadata(); $this->dataPacket($pk); break; - }elseif($packet->face === 0xff and $this->server->handle("player.action", $data) !== false){ - $this->entity->inAction = true; - $this->entity->inActionCounter = 0; - $this->startAction = microtime(true); - $this->entity->updateMetadata(); + }elseif($packet->face === 0xff){ + + $slotItem = $this->getHeldItem(); + if($slotItem->getID() == SNOWBALL || $slotItem->getID() == EGG){ //TODO better way + $x = $packet->x * 0.000030518; + $y = $packet->y * 0.000030518; + $z = $packet->z * 0.000030518; + + $d = sqrt($x*$x + $y*$y + $z*$z); + + if($d >= 0.0001){ + $shootX = $x / $d; + $shootY = $y / $d; + $shootZ = $z / $d; + + $data = [ + "x" => $this->entity->x, + "y" => $this->entity->y + $this->entity->getEyeHeight(), + "z" => $this->entity->z, + "yaw" => $this->entity->yaw, + "pitch" => $this->entity->pitch, + "shooter" => $this->entity, + "shootX" => $shootX, + "shootY" => $shootY, + "shootZ" => $shootZ + ]; + + if($slotItem->getID() == EGG){ + $e = $this->server->api->entity->add($this->entity->level, ENTITY_OBJECT, OBJECT_EGG, $data); + }else{ + $e = $this->server->api->entity->add($this->level, ENTITY_OBJECT, OBJECT_SNOWBALL, $data); + } + + + $this->server->api->entity->spawnToAll($e); + } + + }else{ + if($this->server->handle("player.action", $data) !== false){ + $this->entity->inAction = true; + $this->entity->inActionCounter = 0; + $this->startAction = microtime(true); + $this->entity->updateMetadata(); + } + } } break; case ProtocolInfo::PLAYER_ACTION_PACKET: diff --git a/src/entity/Entity.php b/src/entity/Entity.php index 43d9f737..04df2ecf 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -101,7 +101,7 @@ class Entity extends Position public $inWeb; public $inLava; - function __construct(Level $level, $eid, $class, $type = 0, $data = array()) + function __construct(Level $level, $eid, $class, $type = 0, $data = []) { $this->random = new Random(); $this->last = [&$this->lastX, &$this->lastY, &$this->lastZ, &$this->lastYaw, &$this->lastPitch, &$this->lastTime]; //pointers to variables @@ -174,15 +174,7 @@ class Entity extends Position $this->z = isset($this->data["TileZ"]) ? $this->data["TileZ"] : $this->z; $this->setHealth(1, "generic"); $this->stepHeight = false; - - $this->width = 1; - $this->height = 1; - if($this->type === OBJECT_SNOWBALL){ - $this->server->schedule(1210, array( - $this, - "update" - )); - } + $this->setSize(1, 1); break; } $this->radius = $this->width / 2; @@ -1421,19 +1413,15 @@ class Entity extends Position public function setHealth($health, $cause = "generic", $force = false) { $health = (int) $health; - $harm = false; - if($health < $this->health){ - $harm = true; - $dmg = $this->health - $health; - if(($this->class !== ENTITY_PLAYER or (($this->player instanceof Player) and (($this->player->gamemode & 0x01) === 0x00 or $force === true))) and ($this->dmgcounter[0] < microtime(true) or $this->dmgcounter[1] < $dmg) and !$this->dead){ - $this->dmgcounter[0] = microtime(true) + 0.5; - $this->dmgcounter[1] = $dmg; - } else{ - return false; // Entity inmunity - } - } elseif($health === $this->health and ! $this->dead){ - return false; + $harm = true; + $dmg = $this->health - $health; + if(($this->class !== ENTITY_PLAYER or (($this->player instanceof Player) and (($this->player->gamemode & 0x01) === 0x00 or $force === true))) and ($this->dmgcounter[0] < microtime(true) or $this->dmgcounter[1] < $dmg) and !$this->dead){ + $this->dmgcounter[0] = microtime(true) + 0.5; + $this->dmgcounter[1] = $dmg; + } else{ + return false; // Entity inmunity } + if($this->server->api->dhandle("entity.health.change", array( "entity" => $this, "eid" => $this->eid, diff --git a/src/entity/Projectile.php b/src/entity/Projectile.php index fd1a72bc..a390bed2 100644 --- a/src/entity/Projectile.php +++ b/src/entity/Projectile.php @@ -1,5 +1,199 @@ gravity = 0.03; + $this->setSize(0.25, 0.25); + + $this->x -= cos($this->yaw / 180 * M_PI) * 0.16; + $this->y -= 0.1; + $this->z -= cos($this->yaw / 180 * M_PI) * 0.16; + $this->boundingBox->setBounds($this->x - $this->radius, $this->y - $this->yOffset /*+ $this->ySize*/, $this->z - $this->radius, $this->x + $this->radius, $this->y - $this->yOffset + $this->height /*+ $this->ySize*/, $this->z + $this->radius); + $shooter = $data["shooter"] ?? false; + if($shooter instanceof Entity){ + $this->shooterEID = $shooter->eid; + if($shooter->isPlayer()){ + $shootX = $data["shootX"] ?? 0; + $shootY = $data["shootY"] ?? 0; + $shootZ = $data["shootZ"] ?? 0; + $throwPower = 1.5; + $this->shoot($shootX, $shootY, $shootZ, $throwPower, 1.0); + }else{ + /* + * TODO shoot if not player + * $pitch = $this->pitch; + $v18 = cos($pitch/180 * 3.1416); + $v19 = cos($pitch/180 * 3.1416); //this one uses $pitch+getThrowUpAngleOffset, but last is 0 + $throwPower = 1.5; + $this->shoot(, , , $throwPower, 1.0);*/ + } + } + + } + + public function shoot($d, $d1, $d2, $f, $f1){ //unchecked, may be not same as arrow + $f2 = sqrt($d * $d + $d1 * $d1 + $d2 * $d2); + $d /= $f2; + $d1 /= $f2; + $d2 /= $f2; + $d += $this->random->nextGaussian() * 0.0075 * $f1; + $d1 += $this->random->nextGaussian() * 0.0075 * $f1; + $d2 += $this->random->nextGaussian() * 0.0075 * $f1; + $d *= $f; + $d1 *= $f; + $d2 *= $f; + $this->speedX = $d; + $this->speedY = $d1; + $this->speedZ = $d2; + $f3 = sqrt($d * $d + $d2 * $d2); + $this->yaw = (atan2($d, $d2) * 180) / M_PI; + $this->pitch = (atan2($d1, $f3) * 180) / M_PI; + $this->updatePosition(); + } + + public function update($now){ + $this->lastX = $this->x; + $this->lastY = $this->y; + $this->lastZ = $this->z; + $this->lastPitch = $this->pitch; + $this->lastYaw = $this->yaw; + $this->lastSpeedX = $this->speedX; + $this->lastSpeedY = $this->speedY; + $this->lastSpeedZ = $this->speedZ; + $this->needsUpdate = true; + $this->handleWaterMovement(); //TODO: maybe just call parent::update(); ? + if($this->fire > 0){ + if(!$this->isImmuneToFire){ + if(($this->fire % 20) == 0){ + $this->harm(1, "burning"); + } + --$this->fire; + }else{ + $this->fire -= 4; + if($this->fire <= 0) $this->fire = 0; + } + + if($this->fire <= 0){ + $this->updateMetadata(); + } + } + + if($this->handleLavaMovement()){ + if(!$this->isImmuneToFire){ + $this->harm(4, "fire"); + $oldOnFire = $this->fire; + if($oldOnFire < 20*30) $this->fire = 20*30; //30 seconds + } + } + + if($this->isInVoid()){ + $this->outOfWorld(); + } + //Entity::update end + + if($this->shake > 0) --$this->shake; + + if(!$this->inGround){ + ++$this->ticksInAir; + goto CHECK_COLLISIONS; //TODO dont use gotos + } + + $blockID = $this->level->level->getBlockID($this->xTile, $this->yTile, $this->zTile); + if($this->inTile != $blockID){ + $this->speedX += (lcg_value() * 0.2); + $this->speedY += (lcg_value() * 0.2); + $this->speedZ += (lcg_value() * 0.2); + $this->inGround = false; + $this->ticksInAir = $this->ticksInGround = 0; + CHECK_COLLISIONS: + + $start = new Vector3($this->x, $this->y, $this->z); + $end = new Vector3($this->x + $this->speedX, $this->y + $this->speedY, $this->z + $this->speedZ); + /** + * @var MovingObjectPosition $v4 + */ + $v4 = $this->level->rayTraceBlocks($start, $end); + $start = new Vector3($this->x, $this->y, $this->z); //TODO remove? + if($v4 != null){ + $end = new Vector3($v4->hitVector->x, $v4->hitVector->y, $v4->hitVector->z); + }else{ + $end = new Vector3($this->x + $this->speedX, $this->y + $this->speedY, $this->z + $this->speedZ); + } + + $entities = $this->level->getEntitiesInAABB($this->boundingBox->addCoord($this->speedX, $this->speedY, $this->speedZ)->expand(1, 1, 1)); + $bestDist = 0; + $bestEnt = null; + + foreach($entities as $eid => $ent){ + if($eid != $this->eid && $ent->isPickable() && ($eid != $this->shooterEID || $this->ticksInAir >= 5)){ + + $v12 = $ent->boundingBox->expand(0.3, 0.3, 0.3); + $v13 = $v12->calculateIntercept($start, $end); + + if($v13 != null){ + $dist = $start->distance($v13->hitVector); + + if($dist < $bestDist || $bestDist == 0){ + $bestEnt = $ent; + } + } + } + } + + if($bestEnt != null){ + $v4 = MovingObjectPosition::fromEntity($bestEnt); + } + + if($v4 != null){ + $this->onHit($v4); + } + $this->x += $this->speedX; + $this->y += $this->speedY; + $this->z += $this->speedZ; + $v21 = sqrt($this->speedX*$this->speedX + $this->speedZ*$this->speedZ); + $this->yaw = atan2($this->speedX, $this->speedZ) * 180 / M_PI; + $this->pitch = atan2($this->speedY, $v21) * 180 / M_PI; + + $this->pitch = $this->lastPitch + ($this->pitch - $this->lastPitch) * 0.2; + $this->yaw = $this->lastYaw + ($this->yaw - $this->lastYaw) * 0.2; + + $v24 = $this->inWater ? 0.8 : 0.99; + $this->speedX *= $v24; + $this->speedY *= $v24; + $this->speedZ *= $v24; + $this->speedY -= $this->gravity; + $v7 = $this->width / 2; + $v8 = $this->height; + $this->boundingBox->setBounds($this->x - $v7, $this->y - $this->yOffset /*+ $this->ySize*/, $this->z - $v7, $this->x + $v7, $this->y - $this->yOffset + $v8 /*+ $this->ySize*/, $this->z + $v7); + } + } + + public function onHit(MovingObjectPosition $hitResult){ + + } + + 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->speedX = $this->speedX; + $pk->speedY = $this->speedY; + $pk->speedZ = $this->speedZ; + $pk->did = 0; + $player->dataPacket($pk); + } } \ No newline at end of file diff --git a/src/entity/object/Arrow.php b/src/entity/object/Arrow.php index 5e5993c0..61477938 100644 --- a/src/entity/object/Arrow.php +++ b/src/entity/object/Arrow.php @@ -1,8 +1,8 @@ lastZ = $this->z; $this->lastPitch = $this->pitch; $this->lastYaw = $this->yaw; + $this->lastSpeedX = $this->speedX; + $this->lastSpeedY = $this->speedY; + $this->lastSpeedZ = $this->speedZ; $this->needsUpdate = true; $this->handleWaterMovement(); //TODO: maybe just call parent::update(); ? if($this->fire > 0){ diff --git a/src/entity/object/Egg.php b/src/entity/object/Egg.php deleted file mode 100644 index e69de29b..00000000 diff --git a/src/entity/object/Snowball.php b/src/entity/object/Snowball.php deleted file mode 100644 index e69de29b..00000000 diff --git a/src/entity/object/ThrownEgg.php b/src/entity/object/ThrownEgg.php new file mode 100644 index 00000000..661bbe0f --- /dev/null +++ b/src/entity/object/ThrownEgg.php @@ -0,0 +1,32 @@ +typeOfHit == 1){ + $hitResult->entityHit->harm(0, $this->eid); + } + + $rand = mt_rand(0, 7); + if($rand == 0){ + $count = mt_rand(0, 31) == 0 ? 4 : 1; + $data = [ + "x" => $this->x, + "y" => $this->y, + "z" => $this->z, + "yaw" => $this->yaw, + "pitch" => 0, + "IsBaby" => true + ]; + for($i = 0; $i < $count; ++$i){ + $chicken = $this->server->api->entity->add($this->level, ENTITY_MOB, MOB_CHICKEN, $data); + + $this->server->api->entity->spawnToAll($chicken); + } + } + + $this->close(); + } + + +} \ No newline at end of file diff --git a/src/entity/object/ThrownSnowball.php b/src/entity/object/ThrownSnowball.php new file mode 100644 index 00000000..a6758cd3 --- /dev/null +++ b/src/entity/object/ThrownSnowball.php @@ -0,0 +1,11 @@ +typeOfHit == 1){ + $hitResult->entityHit->harm(0, $this->eid); + } + + $this->close(); + } +} \ No newline at end of file diff --git a/src/entity/registry/EntityRegistry.php b/src/entity/registry/EntityRegistry.php index 9ad3781b..565c65b2 100644 --- a/src/entity/registry/EntityRegistry.php +++ b/src/entity/registry/EntityRegistry.php @@ -5,8 +5,8 @@ class EntityRegistry{ /*Register all entities*/ public static function registerEntities(){ - console("[INFO] Registering entities..."); - self::$entityList = new EntityList; + ConsoleAPI::info("Registering entities..."); + self::$entityList = new EntityList(); self::registerEntity(Pig::class); self::registerEntity(Chicken::class); self::registerEntity(Sheep::class); @@ -22,6 +22,8 @@ class EntityRegistry{ self::registerEntity(Minecart::class); self::registerEntity(FallingSand::class); self::registerEntity(ItemEntity::class); + self::registerEntity(ThrownEgg::class); + self::registerEntity(ThrownSnowball::class); } /*Register an Entity*/ diff --git a/src/world/MovingObjectPosition.php b/src/world/MovingObjectPosition.php index 486d6d68..2e6a6dfc 100644 --- a/src/world/MovingObjectPosition.php +++ b/src/world/MovingObjectPosition.php @@ -1,8 +1,8 @@ Date: Sun, 24 Mar 2024 06:26:20 +0300 Subject: [PATCH 8/8] fixed dropped item loading, player arrows can be collected now, tweaked other stuff --- src/Player.php | 6 ++++-- src/entity/Entity.php | 15 ++++++++++---- src/entity/Projectile.php | 19 ++++++++++++++++- src/entity/object/Arrow.php | 41 +++++++++++++++++++++++++++++-------- 4 files changed, 65 insertions(+), 16 deletions(-) diff --git a/src/Player.php b/src/Player.php index 3a1352f9..1c790a06 100755 --- a/src/Player.php +++ b/src/Player.php @@ -1848,7 +1848,7 @@ class Player{ "z" => $this->entity->z, "yaw" => $this->entity->yaw, "pitch" => $this->entity->pitch, - "shooter" => $this->entity, + "shooter" => $this->entity->eid, "shootX" => $shootX, "shootY" => $shootY, "shootZ" => $shootZ @@ -1860,6 +1860,7 @@ class Player{ $e = $this->server->api->entity->add($this->level, ENTITY_OBJECT, OBJECT_SNOWBALL, $data); } + $this->removeItem($slotItem->getID(), $slotItem->meta, 1); $this->server->api->entity->spawnToAll($e); } @@ -1903,7 +1904,8 @@ class Player{ "y" => $this->entity->y + 1.6, "z" => $this->entity->z, "yaw" => $this->entity->yaw, - "pitch" => $this->entity->pitch + "pitch" => $this->entity->pitch, + "shooter" => $this->entity->eid, ]; $e = $this->server->api->entity->add($this->level, ENTITY_OBJECT, OBJECT_ARROW, $d); $e->speedX = -sin(($e->yaw / 180) * M_PI) * cos(($e->pitch / 180) * M_PI); diff --git a/src/entity/Entity.php b/src/entity/Entity.php index 04df2ecf..f3527e9b 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -396,8 +396,8 @@ class Entity extends Position if($this->isPlayer() && $this->player->spawned === true && $this->player->blocked !== true && !$this->dead){ $myBB = $this->boundingBox->grow(1, 0.5, 1); - foreach($this->server->api->entity->getRadius($this, 2, ENTITY_ITEM) as $item){ - if(!$item->closed && $item->spawntime > 0 && $item->delayBeforePickup <= 0){ + foreach($this->server->api->entity->getRadius($this, 2, false) as $item){ + if($item->class === ENTITY_ITEM && !$item->closed && $item->spawntime > 0 && $item->delayBeforePickup <= 0){ if($item->boundingBox->intersectsWith($myBB)){ if((($this->player->gamemode & 0x01) === 1 || $this->player->hasSpace($item->type, $item->meta, $item->stack) === true) && $this->server->api->dhandle("player.pickup", array( "eid" => $this->player->eid, @@ -410,7 +410,11 @@ class Entity extends Position $item->close(); } } - + }else if($item->class == ENTITY_OBJECT && !$item->closed && $item->type == OBJECT_ARROW && $item->shotByPlayer && $item->inGround && $item->shake <= 0 && $item->boundingBox->intersectsWith($myBB)){ + if(($this->player->gamemode & 0x01) == 1 || $this->player->hasSpace(ARROW, 0, 1)){ + $this->player->addItem(ARROW, 0, 1); + $item->close(); + } } } unset($myBB); @@ -1262,6 +1266,9 @@ class Entity extends Position 0 => $this->yaw, 1 => $this->pitch, ], + "speedX" => $this->speedX, + "speedY" => $this->speedY, + "speedZ" => $this->speedZ ]; if($this->class === ENTITY_OBJECT){ @@ -1274,7 +1281,7 @@ class Entity extends Position } if($this->class === ENTITY_ITEM){ $data["Item"] = [ - "id" => $this->type, + "id" => $this->itemID, "Damage" => $this->meta, "Count" => $this->stack, ]; diff --git a/src/entity/Projectile.php b/src/entity/Projectile.php index a390bed2..3c37424b 100644 --- a/src/entity/Projectile.php +++ b/src/entity/Projectile.php @@ -19,6 +19,13 @@ abstract class Projectile extends Entity{ $this->z -= cos($this->yaw / 180 * M_PI) * 0.16; $this->boundingBox->setBounds($this->x - $this->radius, $this->y - $this->yOffset /*+ $this->ySize*/, $this->z - $this->radius, $this->x + $this->radius, $this->y - $this->yOffset + $this->height /*+ $this->ySize*/, $this->z + $this->radius); $shooter = $data["shooter"] ?? false; + if($shooter !== false) $shooter = $this->level->entityList[$shooter] ?? false; + $this->inTile = $data["inTile"] ?? $this->inTile; + $this->inGround = $data["inGround"] ?? $this->inGround; + $this->xTile = $data["xTile"] ?? $this->xTile; + $this->yTile = $data["yTile"] ?? $this->yTile; + $this->zTile = $data["zTile"] ?? $this->zTile; + if($shooter instanceof Entity){ $this->shooterEID = $shooter->eid; if($shooter->isPlayer()){ @@ -180,7 +187,17 @@ abstract class Projectile extends Entity{ public function onHit(MovingObjectPosition $hitResult){ } - + public function createSaveData(){ + $data = parent::createSaveData(); + + $data["inTile"] = $this->inTile; + $data["inGround"] = $this->inGround; + $data["xTile"] = $this->xTile; + $data["yTile"] = $this->yTile; + $data["zTile"] = $this->zTile; + + return $data; + } public function spawn($player) { $pk = new AddEntityPacket(); diff --git a/src/entity/object/Arrow.php b/src/entity/object/Arrow.php index 61477938..ff1e86af 100644 --- a/src/entity/object/Arrow.php +++ b/src/entity/object/Arrow.php @@ -5,8 +5,6 @@ class Arrow extends Entity{ const CLASS_TYPE = ENTITY_OBJECT; public $shooterEID = 0; - public $shotByEntity; - public $xTile, $yTile, $zTile; public $inTile, $inData; public $shake; @@ -14,18 +12,41 @@ class Arrow extends Entity{ public $airTicks = 0; public $criticial = false; public $groundTicks = 0; + public $shotByPlayer = false; - function __construct(Level $level, $eid, $class, $type = 0, $data = [], $shooter = false){ + function __construct(Level $level, $eid, $class, $type = 0, $data = []){ parent::__construct($level, $eid, $class, $type, $data); $this->gravity = 0.05; $this->setSize(0.5, 0.5); $this->setName("Arrow"); - $this->shooterEID = $shooter; - $this->shotByEntity = $shooter instanceof Entity; + $shooter = $data["shooter"] ?? false; + if($shooter !== false){ + $this->shooterEID = $shooter; + $this->shotByPlayer = $level->entityList[$shooter]->isPlayer(); + } $this->airTicks = $this->groundTicks = 0; + $this->inTile = $data["inTile"] ?? $this->inTile; + $this->inData = $data["inData"] ?? $this->inData; + $this->inGround = $data["inGround"] ?? $this->inGround; + $this->xTile = $data["xTile"] ?? $this->xTile; + $this->yTile = $data["yTile"] ?? $this->yTile; + $this->zTile = $data["zTile"] ?? $this->zTile; + $this->shotByPlayer = $data["shotByPlayer"] ?? $this->shotByPlayer; + //$this->server->schedule(1210, array($this, "update")); //Despawn } - + public function createSaveData(){ + $data = parent::createSaveData(); + + $data["inTile"] = $this->inTile; + $data["inData"] = $this->inData; + $data["inGround"] = $this->inGround; + $data["xTile"] = $this->xTile; + $data["yTile"] = $this->yTile; + $data["zTile"] = $this->zTile; + $data["shotByPlayer"] = $this->shotByPlayer; + return $data; + } public function handleUpdate(){ $pk = new MoveEntityPacket_PosRot; $pk->eid = $this->eid; @@ -203,9 +224,11 @@ class Arrow extends Entity{ $this->speedZ = $v4->hitVector->z - $this->z; $v21 = sqrt($this->speedX*$this->speedX + $this->speedY*$this->speedY + $this->speedZ*$this->speedZ); - $this->x -= $this->speedX / $v21 * 0.05; - $this->y -= $this->speedY / $v21 * 0.05; - $this->z -= $this->speedZ / $v21 * 0.05; + if($v21 != 0){ + $this->x -= $this->speedX / $v21 * 0.05; + $this->y -= $this->speedY / $v21 * 0.05; + $this->z -= $this->speedZ / $v21 * 0.05; + } $this->inGround = true; $this->shake = 7;