diff --git a/src/API/EntityAPI.php b/src/API/EntityAPI.php index c9d97122..c40f09fa 100644 --- a/src/API/EntityAPI.php +++ b/src/API/EntityAPI.php @@ -1,340 +1,283 @@ entities = []; - $this->server = ServerAPI::request(); - - $this->hp = [ - 10 => 4, - 11 => 10, - 12 => 10, - 13 => 8, - - 32 => 20, - 33 => 20, - 34 => 20, - 35 => 16, - 36 => 20, - ]; - - $this->mob = [ - "chicken" => 10, - "cow" => 11, - "pig" => 12, - "sheep" => 13, - - "zombie" => 32, - "creeper" => 33, - "skeleton" => 34, - "spider" => 35, - "pigman" => 36, - ]; - - $this->mobName = [ - 10 => "Chicken", - 11 => "Cow", - 12 => "Pig", - 13 => "Sheep", - - 32 => "Zombie", - 33 => "Creeper", - 34 => "Skeleton", - 35 => "Spider", - 36 => "Pigman", - ]; - - $this->serverSpawnAnimals = $this->server->api->getProperty("spawn-animals"); - $this->serverSpawnMobs = $this->server->api->getProperty("spawn-mobs"); - } - - public function init(){ - $this->server->schedule(25, [$this, "updateEntities"], [], true); - $this->server->api->console->register("summon", "", [$this, "commandHandler"]); - $this->server->api->console->register("spawnmob", "", [$this, "commandHandler"]); - $this->server->api->console->register("despawn", "", [$this, "CommandHandler"]); - } - - - public function commandHandler($cmd, $args, $issuer, $alias){ - $output = ""; - switch($cmd){ - case 'summon': - case 'spawnmob': - if(!($issuer instanceof Player)){ - $output .= "Please run this command in-game."; - break; - } - if((count($args) < 1) or (count($args) > 3)){ - $output .= "Usage: /$cmd [amount] [baby]"; - break; - } - - $type = $this->mob[strtolower($args[0])]; - if($type != (10 or 11 or 12 or 13 or 32 or 33 or 34 or 35 or 36)){ - $output .= "Unknown mob."; - break; - } - - if(count($args) == 1){//summon - - $spawnX = round($issuer->entity->x, 1, PHP_ROUND_HALF_UP); - $spawnY = round($issuer->entity->y, 1, PHP_ROUND_HALF_UP); - $spawnZ = round($issuer->entity->z, 1, PHP_ROUND_HALF_UP); - $spawnLevel = $issuer->entity->level; - - $entityit = $this->add($spawnLevel, ENTITY_MOB, $type, [ - "x" => $spawnX, - "y" => $spawnY, - "z" => $spawnZ, - "Health" => $this->hp[$type], - ]); - $this->spawnToAll($entityit, $spawnLevel); - $output .= $this->mobName[$type] . " spawned in " . $spawnX . ", " . $spawnY . ", " . $spawnZ . "."; - break; - }elseif(is_numeric($args[1])){//summon [amount] - $amount = (int) $args[1]; - if($amount > 25){ - $output .= "Cannot spawn > 25 mobs"; - break; - } - - $isBaby = 0; - if(isset($args[2]) and strtolower($args[2]) == 'baby'){//summon [amount] [baby] - if($type > 13){ - $output .= "This mob cant be baby!"; - break; - } - $isBaby = 1; - } - - $spawnX = round($issuer->entity->x, 1, PHP_ROUND_HALF_UP); - $spawnY = round($issuer->entity->y, 1, PHP_ROUND_HALF_UP); - $spawnZ = round($issuer->entity->z, 1, PHP_ROUND_HALF_UP); - $spawnLevel = $issuer->entity->level; - - for($cnt = $amount; $cnt > 0; --$cnt){ - $entityit = $this->add($spawnLevel, ENTITY_MOB, $type, [ - "x" => $spawnX, - "y" => $spawnY, - "z" => $spawnZ, - "Health" => $this->hp[$type], - "IsBaby" => $isBaby, - ]); - $this->spawnToAll($entityit, $spawnLevel); - } - - if($type == 13 or $amount == 1) - $plural = ''; - else $plural = 's'; - - if($isBaby == 1) - $baby = "Baby "; - else $baby = ''; - - $output .= $amount . " " . $baby . $this->mobName[$type] . $plural . " spawned in " . $spawnX . ", " . $spawnY . ", " . $spawnZ . "."; - - break; - }elseif(strtolower($args[1]) == 'baby'){//summon [baby] - if($type > 13){ - $output .= "This mob cant be baby!"; - break; - }else{ - $spawnX = round($issuer->entity->x, 1, PHP_ROUND_HALF_UP); - $spawnY = round($issuer->entity->y, 1, PHP_ROUND_HALF_UP); - $spawnZ = round($issuer->entity->z, 1, PHP_ROUND_HALF_UP); - $spawnLevel = $issuer->entity->level; - $entityit = $this->add($spawnLevel, ENTITY_MOB, $type, [ - "x" => $spawnX, - "y" => $spawnY, - "z" => $spawnZ, - "Health" => $this->hp[$type], - "IsBaby" => 1, - ]); - $this->spawnToAll($entityit, $spawnLevel); - $output .= "Baby " . $this->mobName[$type] . " spawned in " . $spawnX . ", " . $spawnY . ", " . $spawnZ . "."; - break; - } - break; - } - case 'despawn': - $cnt = 0; - $l = $this->server->query("SELECT EID FROM entities WHERE class = " . ENTITY_MOB . ";"); - if($l !== false and $l !== true){ - while(($e = $l->fetchArray(SQLITE3_ASSOC)) !== false){ - $e = $this->get($e["EID"]); - if($e instanceof Entity){ - $this->remove($e->eid); - $cnt++; - } - } - } - $output .= $cnt . " mobs have been despawned!"; - break; - } - return $output; - } - - public function add(Level $level, $class, $type = 0, $data = []){ - $eid = $this->eCnt++; - $efl = EntityRegistry::$entityList->getEntityFromTypeAndClass($type, $class); - if($efl instanceof PropertyEntity){ - $class = $efl->getEntityName(); - $this->entities[$eid] = new $class($level, $eid, $efl->getEntityClass(), $efl->getEntityType(), $data); - }else{ - $this->entities[$eid] = new Entity($level, $eid, $class, $type, $data); - } - $this->server->handle("entity.add", $this->entities[$eid]); - return $this->entities[$eid]; - } - - public function spawnToAll(Entity $e){ - foreach($this->server->api->player->getAll($e->level) as $player){ - if($player->eid !== false and $player->eid !== $e->eid and $e->class !== ENTITY_PLAYER and $e instanceof Entity){ - $e->spawn($player); - } - } - } - - public function get($eid){ - if(isset($this->entities[$eid])){ - return $this->entities[$eid]; - } - return false; - } - - public function remove($eid){ - if(isset($this->entities[$eid])){ - $entity = $this->entities[$eid]; - $this->entities[$eid] = null; - unset($this->entities[$eid]); - $entity->closed = true; - $this->server->query("DELETE FROM entities WHERE EID = " . $eid . ";"); - if($entity->class === ENTITY_PLAYER){ - $pk = new RemovePlayerPacket; - $pk->eid = $entity->eid; - $pk->clientID = 0; - $this->server->api->player->broadcastPacket($this->server->api->player->getAll(), $pk); - }else{ - $pk = new RemoveEntityPacket; - $pk->eid = $entity->eid; - $this->server->api->player->broadcastPacket($this->server->api->player->getAll($entity->level), $pk); - } - $this->server->api->dhandle("entity.remove", $entity); - $entity = null; - unset($entity); - } - } - - public function updateEntities(){ - $l = $this->server->query("SELECT EID FROM entities WHERE hasUpdate = 1;"); - - if($l !== false and $l !== true){ - while(($e = $l->fetchArray(SQLITE3_ASSOC)) !== false){ - $e = $this->get($e["EID"]); - if($e instanceof Entity){ - $e->update(); - $this->server->query("UPDATE entities SET hasUpdate = 0 WHERE EID = " . $e->eid . ";"); - } - } - } - } - - public function updateRadius(Position $center, $radius = 15, $class = false){ - $this->server->query("UPDATE entities SET hasUpdate = 1 WHERE level = '" . $center->level->getName() . "' " . ($class !== false ? "AND class = $class " : "") . "AND abs(x - {$center->x}) <= $radius AND abs(y - {$center->y}) <= $radius AND abs(z - {$center->z}) <= $radius;"); - } - - public function getRadius(Position $center, $radius = 15, $class = false){ - $entities = []; - $l = $this->server->query("SELECT EID FROM entities WHERE level = '" . $center->level->getName() . "' " . ($class !== false ? "AND class = $class " : "") . "AND abs(x - {$center->x}) <= $radius AND abs(y - {$center->y}) <= $radius AND abs(z - {$center->z}) <= $radius;"); - if($l !== false and $l !== true){ - while(($e = $l->fetchArray(SQLITE3_ASSOC)) !== false){ - $e = $this->get($e["EID"]); - if($e instanceof Entity){ - $entities[$e->eid] = $e; - } - } - } - return $entities; - } - - public function heal($eid, $heal, $cause){ - $this->harm($eid, -$heal, $cause); - } - - public function harm($eid, $attack, $cause, $force = false){ - $e = $this->get($eid); - if($e === false or $e->dead === true){ - return false; - } - $e->setHealth($e->getHealth() - $attack, $cause, $force); - } - - public function drop(Position $pos, Item $item){ - if($item->getID() === AIR or $item->count <= 0){ - return; - } - $i = 0; - do{ // drop the block to the first supporting block - $i++; - $v = new Vector3($pos->x, $pos->y - $i, $pos->z); - $b = $pos->level->getBlock($v); - if($b->isSolid === true) - break; - if(($b instanceof LiquidBlock) or $b->getID() === COBWEB or $b->getID() === LADDER or $b->getID() === FENCE or $b->getID() === STONE_WALL) - break; - - }while($i < 200); - if($i == 200) - $i = 0; - $data = [ - "x" => $pos->x + mt_rand(-10, 10) / 50, - "y" => $pos->y + 0.19 - $i + 1, - "z" => $pos->z + mt_rand(-10, 10) / 50, - "level" => $pos->level, - //"speedX" => mt_rand(-3, 3) / 8, - "speedY" => mt_rand(5, 8) / 2, - //"speedZ" => mt_rand(-3, 3) / 8, - "item" => $item, - ]; - if($this->server->api->handle("item.drop", $data) !== false){ - for($count = $item->count; $count > 0;){ - $item->count = min($item->getMaxStackSize(), $count); - $count -= $item->count; - $e = $this->add($pos->level, ENTITY_ITEM, $item->getID(), $data); - $this->spawnToAll($e); - $this->server->api->handle("entity.motion", $e); - } - } - } - - public function spawnAll(Player $player){ - foreach($this->getAll($player->level) as $e){ - if($e->class !== ENTITY_PLAYER){ - $e->spawn($player); - } - } - } - - public function getAll($level = null){ - if($level instanceof Level){ - $entities = []; - $l = $this->server->query("SELECT EID FROM entities WHERE level = '" . $level->getName() . "';"); - if($l !== false and $l !== true){ - while(($e = $l->fetchArray(SQLITE3_ASSOC)) !== false){ - $e = $this->get($e["EID"]); - if($e instanceof Entity){ - $entities[$e->eid] = $e; - } - } - } - return $entities; - } - return $this->entities; - } -} + + public $entities; + private $server; + private $eCnt = 1; + + function __construct(){ + $this->entities = []; + $this->server = ServerAPI::request(); + + $this->serverSpawnAnimals = $this->server->api->getProperty("spawn-animals"); + $this->serverSpawnMobs = $this->server->api->getProperty("spawn-mobs"); + } + + public function init(){ + $this->server->schedule(25, [$this, "updateEntities"], [], true); + $this->server->api->console->register("summon", "", [$this, "commandHandler"]); + $this->server->api->console->register("spawnmob", "", [$this, "commandHandler"]); + $this->server->api->console->register("despawn", "", [$this, "CommandHandler"]); + } + + + public function commandHandler($cmd, $args, $issuer, $alias){ + $mob = [ + "chicken" => 10, + "cow" => 11, + "pig" => 12, + "sheep" => 13, + + "zombie" => 32, + "creeper" => 33, + "skeleton" => 34, + "spider" => 35, + "pigman" => 36 + ]; + $output = ""; + switch($cmd){ + case 'summon': + case 'spawnmob': + if(!($issuer instanceof Player)){ + $output .= "Please run this command in-game."; + break; + } + if((count($args) < 1) or (count($args) > 3)){ + $output .= "Usage: /$cmd [amount] [baby]"; + break; + } + + if(is_int($args[0])) $type = $args[0]; + else $type = $mob[strtolower($args[0])]; + if($type != (10 or 11 or 12 or 13 or 32 or 33 or 34 or 35 or 36)){ + $output .= "Unknown mob."; + break; + } + $mobName = ucfirst(array_flip($mob)[$type]); + + if(strtolower($args[1] === "baby") or strtolower($args[2] === "baby") and $type > 13){ + $output .= "$mobName cannot be a baby!"; + break; + } + + $x = round($issuer->entity->x, 2, PHP_ROUND_HALF_UP); + $y = round($issuer->entity->y, 2, PHP_ROUND_HALF_UP); + $z = round($issuer->entity->z, 2, PHP_ROUND_HALF_UP); + $level = $issuer->entity->level; + $pos = new Position($x, $y, $z, $level); + + if(count($args) === 1){//summon + $this->summon($pos, ENTITY_MOB, $type); + $output .= "$mobName spawned in $x, $y, $z."; + break; + } + elseif(is_numeric($args[1])){//summon [amount] + $amount = (int) $args[1]; + if($amount > 25){ + $output .= "Cannot spawn > 25 mobs"; + break; + } + + if(isset($args[2]) and strtolower($args[2]) === 'baby'){//summon [amount] [baby] + $isBaby = 1; + } + + for($cnt = $amount; $cnt > 0; --$cnt){ + $this->summon($pos, ENTITY_MOB, $type, $isBaby === 1 ? ["IsBaby" => 1] : []); + } + + $output .= "$amount".($isBaby === 1 ? " Baby" : "")." $mobName".(($type !== 13 || $amount > 1) ? "s" : "")." spawned in $x, $y, $z."; + break; + } + elseif(strtolower($args[1]) == 'baby'){//summon [baby] + $this->summon($pos, ENTITY_MOB, $type, ["IsBaby" => 1]); + $output .= "Baby $mobName spawned in $x, $y, $z."; + break; + } + break; + case 'despawn': + $cnt = 0; + $l = $this->server->query("SELECT EID FROM entities WHERE class = " . ENTITY_MOB . ";"); + if($l !== false and $l !== true){ + while(($e = $l->fetchArray(SQLITE3_ASSOC)) !== false){ + $e = $this->get($e["EID"]); + if($e instanceof Entity){ + $this->remove($e->eid); + $cnt++; + } + } + } + $output .= "$cnt mobs have been despawned!"; + break; + } + return $output; + } + + public function summon(Position $pos, $class, $type, Array $data = []){ + $entity = $this->add($pos->level, $class, $type, [ + "x" => $pos->x, + "y" => $pos->y, + "z" => $pos->z + ] + $data); + $this->spawnToAll($entity, $pos->level); + } + + public function add(Level $level, $class, $type = 0, $data = []){ + $eid = $this->eCnt++; + $efl = EntityRegistry::$entityList->getEntityFromTypeAndClass($type, $class); + if($efl instanceof PropertyEntity){ + $class = $efl->getEntityName(); + $this->entities[$eid] = new $class($level, $eid, $efl->getEntityClass(), $efl->getEntityType(), $data); + }else{ + $this->entities[$eid] = new Entity($level, $eid, $class, $type, $data); + } + $this->server->handle("entity.add", $this->entities[$eid]); + return $this->entities[$eid]; + } + + public function spawnToAll(Entity $e){ + foreach($this->server->api->player->getAll($e->level) as $player){ + if($player->eid !== false and $player->eid !== $e->eid and $e->class !== ENTITY_PLAYER and $e instanceof Entity){ + $e->spawn($player); + } + } + } + + public function get($eid){ + if(isset($this->entities[$eid])){ + return $this->entities[$eid]; + } + return false; + } + + public function remove($eid){ + if(isset($this->entities[$eid])){ + $entity = $this->entities[$eid]; + $this->entities[$eid] = null; + unset($this->entities[$eid]); + $entity->closed = true; + $this->server->query("DELETE FROM entities WHERE EID = " . $eid . ";"); + if($entity->class === ENTITY_PLAYER){ + $pk = new RemovePlayerPacket; + $pk->eid = $entity->eid; + $pk->clientID = 0; + $this->server->api->player->broadcastPacket($this->server->api->player->getAll(), $pk); + }else{ + $pk = new RemoveEntityPacket; + $pk->eid = $entity->eid; + $this->server->api->player->broadcastPacket($this->server->api->player->getAll($entity->level), $pk); + } + $this->server->api->dhandle("entity.remove", $entity); + $entity = null; + unset($entity); + } + } + + public function updateEntities(){ + $l = $this->server->query("SELECT EID FROM entities WHERE hasUpdate = 1;"); + + if($l !== false and $l !== true){ + while(($e = $l->fetchArray(SQLITE3_ASSOC)) !== false){ + $e = $this->get($e["EID"]); + if($e instanceof Entity){ + $e->update(); + $this->server->query("UPDATE entities SET hasUpdate = 0 WHERE EID = " . $e->eid . ";"); + } + } + } + } + + public function updateRadius(Position $center, $radius = 15, $class = false){ + $this->server->query("UPDATE entities SET hasUpdate = 1 WHERE level = '" . $center->level->getName() . "' " . ($class !== false ? "AND class = $class " : "") . "AND abs(x - {$center->x}) <= $radius AND abs(y - {$center->y}) <= $radius AND abs(z - {$center->z}) <= $radius;"); + } + + public function getRadius(Position $center, $radius = 15, $class = false){ + $entities = []; + $l = $this->server->query("SELECT EID FROM entities WHERE level = '" . $center->level->getName() . "' " . ($class !== false ? "AND class = $class " : "") . "AND abs(x - {$center->x}) <= $radius AND abs(y - {$center->y}) <= $radius AND abs(z - {$center->z}) <= $radius;"); + if($l !== false and $l !== true){ + while(($e = $l->fetchArray(SQLITE3_ASSOC)) !== false){ + $e = $this->get($e["EID"]); + if($e instanceof Entity){ + $entities[$e->eid] = $e; + } + } + } + return $entities; + } + + public function heal($eid, $heal, $cause){ + $this->harm($eid, -$heal, $cause); + } + + public function harm($eid, $attack, $cause, $force = false){ + $e = $this->get($eid); + if($e === false or $e->dead === true){ + return false; + } + $e->setHealth($e->getHealth() - $attack, $cause, $force); + } + + public function drop(Position $pos, Item $item){ + if($item->getID() === AIR or $item->count <= 0){ + return; + } + $i = 0; + do{ // drop the block to the first supporting block + $i++; + $v = new Vector3($pos->x, $pos->y - $i, $pos->z); + $b = $pos->level->getBlock($v); + if($b->isSolid === true) + break; + if(($b instanceof LiquidBlock) or $b->getID() === COBWEB or $b->getID() === LADDER or $b->getID() === FENCE or $b->getID() === STONE_WALL) + break; + + }while($i < 200); + if($i == 200) + $i = 0; + $data = [ + "x" => $pos->x + mt_rand(-10, 10) / 50, + "y" => $pos->y + 0.19 - $i + 1, + "z" => $pos->z + mt_rand(-10, 10) / 50, + "level" => $pos->level, + //"speedX" => mt_rand(-3, 3) / 8, + "speedY" => mt_rand(5, 8) / 2, + //"speedZ" => mt_rand(-3, 3) / 8, + "item" => $item, + ]; + if($this->server->api->handle("item.drop", $data) !== false){ + for($count = $item->count; $count > 0;){ + $item->count = min($item->getMaxStackSize(), $count); + $count -= $item->count; + $e = $this->add($pos->level, ENTITY_ITEM, $item->getID(), $data); + $this->spawnToAll($e); + $this->server->api->handle("entity.motion", $e); + } + } + } + + public function spawnAll(Player $player){ + foreach($this->getAll($player->level) as $e){ + if($e->class !== ENTITY_PLAYER){ + $e->spawn($player); + } + } + } + + public function getAll($level = null){ + if($level instanceof Level){ + $entities = []; + $l = $this->server->query("SELECT EID FROM entities WHERE level = '" . $level->getName() . "';"); + if($l !== false and $l !== true){ + while(($e = $l->fetchArray(SQLITE3_ASSOC)) !== false){ + $e = $this->get($e["EID"]); + if($e instanceof Entity){ + $entities[$e->eid] = $e; + } + } + } + return $entities; + } + return $this->entities; + } +} \ No newline at end of file diff --git a/src/entity/Creature.php b/src/entity/Creature.php index 6a3a4d3f..e5be193d 100644 --- a/src/entity/Creature.php +++ b/src/entity/Creature.php @@ -9,6 +9,8 @@ abstract class Creature extends Living{ parent::__construct($level, $eid, $class, $type, $data); $this->setHealth(isset($this->data["Health"]) ? $this->data["Health"]:1, "generic"); //$this->setName((isset($mobs[$this->type]) ? $mobs[$this->type]:$this->type)); + $this->ai->addTask(new TaskLookAround()); + $this->ai->addTask(new TaskRandomWalk()); } diff --git a/src/entity/Entity.php b/src/entity/Entity.php index 9619ef8c..6dda5456 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -42,7 +42,7 @@ class Entity extends Position{ public $fallStart; private $tickCounter; private $speedMeasure = array(0, 0, 0, 0, 0, 0, 0); - protected $server; + public $server; private $isStatic; public $level; public $isInMinecart = false; @@ -94,6 +94,7 @@ class Entity extends Position{ $this->height = 0.98; $this->moveTime = 0; $this->lookTime = 0; + $this->onGround = false; switch($this->class){ case ENTITY_PLAYER: $this->player = $this->data["player"]; @@ -153,7 +154,7 @@ class Entity extends Position{ } public function isMoving(){ - return $this->moveTime > 0 || ($this->speedX > 0.01 || $this->speedX < -0.01) || ($this->speedY > 0.01 || $this->speedY < -0.01) || ($this->speedZ > 0.01 || $this->speedZ < -0.01); + return ($this->speedX > 0.01 || $this->speedX < -0.01) || ($this->speedY > 0.01 || $this->speedY < -0.01) || ($this->speedZ > 0.01 || $this->speedZ < -0.01); } public function setVelocity($vX, $vY = 0, $vZ = 0){ if($vX instanceof Vector3){ @@ -395,7 +396,14 @@ class Entity extends Position{ } return $hasUpdate; } - + + protected function checkGroundState($movX, $movY, $movZ, $dx, $dy, $dz){ + //$this->isCollidedVertically = $movY != $dy; + //$this->isCollidedHorizontally = ($movX != $dx or $movZ != $dz); + //$this->isCollided = ($this->isCollidedHorizontally or $this->isCollidedVertically); + $this->onGround = ($movY != $dy and $movY < 0); + } + public function update(){ if($this->closed === true){ return false; @@ -459,19 +467,18 @@ class Entity extends Position{ } if($this->class !== ENTITY_PLAYER){ $update = false; - if((($this->class !== ENTITY_OBJECT and $this->type !== OBJECT_PRIMEDTNT) or $support === false)){ + if(true){ $drag = 0.2; - if($this->speedX < 0.01 && $this->speedX > -0.01){ + if(Utils::in_range($this->speedX, -0.01, 0.01)){ $this->speedX = 0; } - if($this->speedZ < 0.01 && $this->speedZ > -0.01){ + if(Utils::in_range($this->speedZ, -0.01, 0.01)){ $this->speedZ = 0; } - if($this->speedY < 0.001 && $this->speedY > -0.001){ + if(Utils::in_range($this->speedY, -0.001, 0.001)){ $this->speedY = 0; } - - if($this->class === ENTITY_MOB || $this->class === ENTITY_ITEM){ + if(($this->class === ENTITY_MOB || $this->class === ENTITY_ITEM) && ($this->speedX != 0 || $this->speedY != 0 || $this->speedZ != 0)){ $blocks = $this->level->getCubes($this->boundingBox->getOffsetBoundingBox($this->speedX, $this->speedY, $this->speedZ)); foreach($blocks as $b){ $this->speedX = $b->calculateXOffset($this->boundingBox, $this->speedX); @@ -991,18 +998,6 @@ class Entity extends Position{ if($health < $this->health){ $harm = true; $dmg = $this->health - $health; - if(is_numeric($cause) && ($entity = $this->server->api->entity->get($cause)) != false){ - $d = $entity->x - $this->x; - for($d1 = $entity->z - $this->z; $d * $d + $d1 * $d1 < 0.0001; $d1 = (Utils::randomFloat() - Utils::randomFloat()) * 0.01) - { - $d = (Utils::randomFloat() - Utils::randomFloat()) * 0.01; - } - - //attackedAtYaw = (float)((Math.atan2($d1, $d) * 180D) / 3.1415927410125732D) - rotationYaw; - - $this->knockBack($d, $d1); - $this->sendMotion(); - } if($this->class === ENTITY_PLAYER and ($this->player instanceof Player)){ $points = 0; $values = array( @@ -1092,6 +1087,18 @@ class Entity extends Position{ }elseif($this->health > 0){ $this->dead = false; } + if(is_numeric($cause) && ($entity = $this->server->api->entity->get($cause)) != false){ + $d = $entity->x - $this->x; + for($d1 = $entity->z - $this->z; $d * $d + $d1 * $d1 < 0.0001; $d1 = (Utils::randomFloat() - Utils::randomFloat()) * 0.01) + { + $d = (Utils::randomFloat() - Utils::randomFloat()) * 0.01; + } + + //attackedAtYaw = (float)((Math.atan2($d1, $d) * 180D) / 3.1415927410125732D) - rotationYaw; + + $this->knockBack($d, $d1); + $this->sendMotion(); + } return true; } return false; diff --git a/src/entity/ai/tasks/TaskLookAround.php b/src/entity/ai/tasks/TaskLookAround.php index cafe726c..31c68cfb 100644 --- a/src/entity/ai/tasks/TaskLookAround.php +++ b/src/entity/ai/tasks/TaskLookAround.php @@ -5,7 +5,7 @@ class TaskLookAround extends TaskBase private $finYaw = 0; public function shouldBeExecuted(EntityAI $ai) { - return !$ai->entity->isMoving() && $ai->entity->lookTime <= 0 && mt_rand(0,30) == 1 && $ai->entity->idleTime <= 0; + return !$ai->entity->isMoving() && $ai->entity->lookTime <= 0 && mt_rand(0,30) == 1 && $ai->entity->idleTime <= 0 && !($ai->lastTask instanceof TaskLookAround); } public function wasExecuted(EntityAI $ai){ diff --git a/src/entity/ai/tasks/TaskRandomWalk.php b/src/entity/ai/tasks/TaskRandomWalk.php index 11e85cf8..e6597f64 100644 --- a/src/entity/ai/tasks/TaskRandomWalk.php +++ b/src/entity/ai/tasks/TaskRandomWalk.php @@ -2,25 +2,57 @@ class TaskRandomWalk extends TaskBase { + private $x, $z, $delay = 10, $newPos, $looking = false; public function shouldBeExecuted(EntityAI $ai) { - return !$ai->entity->pathFound && $ai->entity->moveTime <= 0 && $ai->entity->lookTime <= 0 && !($ai->entity->target instanceof Entity) && mt_rand(0,3) == 1; - } - - public function onUpdate(EntityAI $ai) - { - console("random walk"); + return !$ai->entity->pathFound && $ai->entity->moveTime <= 0 && $ai->entity->lookTime <= 0 && $ai->entity->idleTime <= 0 && !($ai->entity->target instanceof Entity) && mt_rand(0,3) == 1; } + public function wasExecuted(EntityAI $ai) { return $ai->entity->moveTime > 0; } public function onStart(EntityAI $ai) { - $ai->entity->moveTime = 100; + $this->x = mt_rand(-7, 7); + $this->z = mt_rand(-7, 7); + $this->newPos = $ai->entity->add($this->x, 0, $this->z); + $ai->entity->moveTime = 15 * Utils::distance($ai->entity, $this->newPos); } - - + public function onUpdate(EntityAI $ai) + { + if($this->delay > 0 && !$this->looking){ + --$this->delay; + try{ + return; + }finally{ + $this->testLook($ai, ($this->x > 0 ? 0.1 : ($this->x < 0 ? -0.1 : 0)), ($this->z > 0 ? 0.1 : ($this->z < 0 ? -0.1 : 0))); + $pk = new RotateHeadPacket(); + $pk->eid = $ai->entity->eid; + $pk->yaw = $ai->entity->yaw; + $ai->entity->server->api->player->broadcastPacket($ai->entity->level->players, $pk); + $this->looking = true; + } + } + if($ai->entity->moveTime <= 1){ + $ai->entity->idleTime = mt_rand(10,100); + $this->looking = false; + } + if(!$ai->entity->isMoving()){ + $ai->entity->addVelocity(($this->x > 0 ? 0.1 : ($this->x < 0 ? -0.1 : 0)), 0, ($this->z > 0 ? 0.1 : ($this->z < 0 ? -0.1 : 0))); + } + } + public function updateLook($ai){ + $ai->entity->server->query("UPDATE entities SET pitch = ".$ai->entity->pitch.", yaw = ".$ai->entity->yaw." WHERE EID = ".$ai->entity->eid.";"); + } + public function testLook($ai, $vX, $vZ){ + $x = $this->newPos->x - $ai->entity->x; + $y = $this->newPos->y - $ai->entity->y; + $z = $this->newPos->z - $ai->entity->z; + $ai->entity->yaw = -atan2($vX, $vZ) * 180 / M_PI; + $ai->entity->pitch = $y == 0 ? 0 : rad2deg(-atan2($y, sqrt(pow($x, 2) + pow($z, 2)))); + $ai->entity->server->query("UPDATE entities SET pitch = ".$ai->entity->pitch.", yaw = ".$ai->entity->yaw." WHERE EID = ".$ai->entity->eid.";"); + } } diff --git a/src/entity/animal/Chicken.php b/src/entity/animal/Chicken.php index 0da85075..b175dbdf 100644 --- a/src/entity/animal/Chicken.php +++ b/src/entity/animal/Chicken.php @@ -4,8 +4,7 @@ class Chicken extends Animal{ const TYPE = MOB_CHICKEN; function __construct(Level $level, $eid, $class, $type = 0, $data = array()){ parent::__construct($level, $eid, $class, $type, $data); - $server = ServerAPI::request(); - $server->schedule(mt_rand(0,6000) + 6000, array($this, "dropAnEgg")); + $this->server->schedule(mt_rand(0,6000) + 6000, array($this, "dropAnEgg")); $this->setHealth(isset($this->data["Health"]) ? $this->data["Health"] : 4, "generic"); $this->setName('Chicken'); $this->setSize($this->isBaby() ? 0.2 : 0.4, $this->isBaby() ? 0.35 : 0.7); diff --git a/src/entity/animal/Pig.php b/src/entity/animal/Pig.php index d3902479..8f12df31 100644 --- a/src/entity/animal/Pig.php +++ b/src/entity/animal/Pig.php @@ -8,7 +8,6 @@ class Pig extends Animal{ $this->server = ServerAPI::request(); $this->setSize($this->isBaby() ? 0.45 : 0.9, $this->isBaby() ? 0.45 : 0.9); $this->setName('Pig'); - $this->ai->addTask(new TaskLookAround()); $this->update(); } public function isFood($id){ diff --git a/src/entity/object/Arrow.php b/src/entity/object/Arrow.php index 40ebbf0e..902e7cf1 100644 --- a/src/entity/object/Arrow.php +++ b/src/entity/object/Arrow.php @@ -6,24 +6,6 @@ class Arrow extends Projectile{ parent::__construct($level, $eid, $class, $type, $data); //$this->server->schedule(1210, array($this, "update")); //Despawn } - public function update(){ - $now = microtime(true); - if(($now - $this->spawntime) >= 60){ - $this->close(); - return false; - } - $f3 = 0.99; - $f5 = 0.03; - $this->move(new Vector3($this->speedX, $this->speedY, $this->speedZ)); - $this->speedX *= $f3; - $this->speedZ *= $f3; - $this->speedY -= $f5; - $this->server->api->handle("entity.move", $this); - $this->server->api->handle("entity.motion", $this); - $this->handleUpdate(); - $this->server->schedule(1, array($this, "update"), false); - $this->lastUpdate = $now; - } public function handleUpdate(){ $pk = new MoveEntityPacket_PosRot; $pk->eid = $this->eid; diff --git a/src/math/Vector3.php b/src/math/Vector3.php index 387de79a..3b44c84a 100644 --- a/src/math/Vector3.php +++ b/src/math/Vector3.php @@ -16,6 +16,10 @@ class Vector3{ $this->z = $z; } + public function toArray(){ + return ["x" => $this->x, "y" => $this->y, "z" => $this->z]; + } + public function getFloorX(){ return (int) $this->x; } diff --git a/src/utils/Utils.php b/src/utils/Utils.php index d99c9e0a..a8886505 100644 --- a/src/utils/Utils.php +++ b/src/utils/Utils.php @@ -535,12 +535,23 @@ class Utils{ public static function round($number){ return round($number, 0, PHP_ROUND_HALF_DOWN); } - public static function distance($pos1, $pos2){ + if($pos1 instanceof Vector3){ + $pos1 = $pos1->toArray(); + } + if($pos2 instanceof Vector3){ + $pos2 = $pos2->toArray(); + } return sqrt(pow($pos1["x"] - $pos2["x"], 2) + pow($pos1["y"] - $pos2["y"], 2) + pow($pos1["z"] - $pos2["z"], 2)); } - + public static function angle3D($pos1, $pos2){ + if($pos1 instanceof Vector3){ + $pos1 = $pos1->toArray(); + } + if($pos2 instanceof Vector3){ + $pos2 = $pos2->toArray(); + } $X = $pos1["x"] - $pos2["x"]; $Z = $pos1["z"] - $pos2["z"]; $dXZ = sqrt(pow($X, 2) + pow($Z, 2)); diff --git a/src/world/Level.php b/src/world/Level.php index 79bd57e8..bde482a1 100644 --- a/src/world/Level.php +++ b/src/world/Level.php @@ -32,7 +32,10 @@ class Level{ public function close(){ $this->__destruct(); } - + /** + * @param AxisAlignedBB $aABB + * @return AxisAlignedBB[] + */ public function getCubes(AxisAlignedBB $aABB) { $aABBs = []; $x0 = floor($aABB->minX); @@ -61,6 +64,7 @@ class Level{ return $aABBs; } + public function __destruct(){ if(isset($this->level)){ $this->save(false, false);