snowballs and eggs

This commit is contained in:
GameHerobrine 2024-03-24 05:35:38 +03:00
parent 105b09e28f
commit 3df03ae96b
10 changed files with 304 additions and 34 deletions

View file

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

View file

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

View file

@ -1,5 +1,199 @@
<?php
abstract class Projectile extends Entity{
const CLASS_TYPE = ENTITY_OBJECT;
public $shooterEID;
public $shake = 0;
public $inGround = false;
public $ticksInAir = 0, $ticksInGround = 0;
public $xTile = 0, $yTile = 0, $zTile = 0;
public $inTile = false;
public function __construct(Level $level, $eid, $class, $type = 0, $data = [])
{
parent::__construct($level, $eid, $class, $type, $data);
$this->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);
}
}

View file

@ -1,8 +1,8 @@
<?php
class Arrow extends Projectile{
class Arrow extends Entity{
const TYPE = OBJECT_ARROW;
const CLASS_TYPE = ENTITY_OBJECT;
public $shooterEID = 0;
public $shotByEntity;
@ -65,6 +65,9 @@ class Arrow extends Projectile{
$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){

View file

@ -0,0 +1,32 @@
<?php
class ThrownEgg extends Projectile{
const TYPE = OBJECT_EGG;
public function onHit(MovingObjectPosition $hitResult){
if($hitResult->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();
}
}

View file

@ -0,0 +1,11 @@
<?php
class ThrownSnowball extends Projectile{
const TYPE = OBJECT_SNOWBALL;
public function onHit(MovingObjectPosition $hitResult){
if($hitResult->typeOfHit == 1){
$hitResult->entityHit->harm(0, $this->eid);
}
$this->close();
}
}

View file

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

View file

@ -1,8 +1,8 @@
<?php
class MovingObjectPosition{
/** 0 = block, 1 = entity */
public $typeOfHit;
/** 0 = block, 1 = entity, 2 = none*/
public $typeOfHit = 2;
public $blockX;
public $blockY;