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 @@