diff --git a/src/API/EntityAPI.php b/src/API/EntityAPI.php index 85fc5f4d..b654e603 100644 --- a/src/API/EntityAPI.php +++ b/src/API/EntityAPI.php @@ -17,10 +17,6 @@ class EntityAPI{ $this->server->api->console->register("summon", "", [$this, "commandHandler"]); $this->server->api->console->register("spawnmob", "", [$this, "commandHandler"]); $this->server->api->console->register("despawn", "", [$this, "CommandHandler"]); - - if(($this->serverSpawnAnimals or $this->serverSpawnMobs) and Entity::$updateOnTick){ - (new MobSpawner())->init(); - } } @@ -185,7 +181,7 @@ class EntityAPI{ public function remove($eid){ if(isset($this->entities[$eid])){ $this->entities[$eid]->closed = true; - if($this->entities[$eid]->isPlayer()){ + if($this->entities[$eid]->isPlayer()){ $pk = new RemovePlayerPacket; $pk->eid = $eid; $pk->clientID = 0; @@ -196,8 +192,8 @@ class EntityAPI{ $this->server->api->player->broadcastPacket($this->entities[$eid]->level->players, $pk); } $this->server->api->dhandle("entity.remove", $this->entities[$eid]); + unset($this->entities[$eid]->level->entityList[$eid]); unset($this->entities[$eid]); - $this->entities[$eid] = null; $this->server->query("DELETE FROM entities WHERE EID = " . $eid . ";"); } } diff --git a/src/dependencies.php b/src/dependencies.php index eefbe7a8..d022c015 100644 --- a/src/dependencies.php +++ b/src/dependencies.php @@ -93,7 +93,7 @@ require_once(FILE_PATH . "/src/entity/Creature.php"); require_once(FILE_PATH . "/src/entity/Ageable.php"); require_once(FILE_PATH . "/src/entity/Animal.php"); -require_once(FILE_PATH . "/src/entity/MobSpawner.php"); +require_once(FILE_PATH . "/src/world/MobSpawner.php"); require_once(FILE_PATH . "/src/material/Item.php"); require_once(FILE_PATH . "/src/material/item/base/ItemTool.php"); @@ -125,4 +125,4 @@ define("SOURCE_SHA1SUM", bin2hex($sha1sum)); if(!file_exists(DATA_PATH . "server.properties") and arg("no-wizard", false) != true){ $installer = new Installer(); } -/***REM_END***/ \ No newline at end of file +/***REM_END***/ diff --git a/src/entity/Entity.php b/src/entity/Entity.php index 10f097e4..8d3dd572 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -179,7 +179,12 @@ class Entity extends Position $this->outOfWorld(); } } - + + public function isType(){ + return in_array($this->type, func_get_args()); + } + + public function addVelocity($vX, $vY = 0, $vZ = 0) { if($vX instanceof Vector3){ @@ -463,12 +468,15 @@ class Entity extends Position return $hasUpdate; } + + + public function isInVoid(){ return $this->y < -1.6; } - - public function update() - { + + + public function update(){ if($this->closed === true){ return false; } diff --git a/src/entity/MobSpawner.php b/src/entity/MobSpawner.php deleted file mode 100644 index 61cd69e6..00000000 --- a/src/entity/MobSpawner.php +++ /dev/null @@ -1,118 +0,0 @@ -server = ServerAPI::request(); - $this->server->api->schedule(40, [$this, "checkForSpawn"], [], true); - } - - public function checkForSpawn(){ - if(count($this->server->api->player->online()) <= 0) return; - if($this->countMobs() >= 25) return; - $this->spawnMobs(); - } - - public function countMobs(){ - $l = $this->server->query("SELECT EID FROM entities WHERE class = ".ENTITY_MOB.";"); - $cnt = 0; - if($l !== false and $l !== true){ - while(($e = $l->fetchArray(SQLITE3_ASSOC)) !== false){ - $cnt++; - } - } - return $cnt; - } - - public function spawnMobs(){ - $o = $this->server->api->player->online(); - $randPlayer = mt_rand(0, (count($o) - 1)); - $this->world = $this->server->api->player->get($o[$randPlayer])->level; - $this->gameTime = $this->world->getTime(); - - if($this->gameTime <= 9500 and $this->server->api->getProperty("spawn-animals")){ - $this->spawnAnimal(); - } - elseif($this->gameTime >= 10000 and $this->server->api->getProperty("spawn-mobs")){ - $this->spawnMonster(); - } - } - - public function spawnAnimal(){ - $type = mt_rand(10, 13); - $x = mt_rand(5, 250); //need create a radius near player - $z = mt_rand(5, 250); - $y = $this->getY($x, $z); - if(!$y) return; - - $this->addMob(new Position($x+.5, $y, $z+.5, $this->world), ENTITY_MOB, $type); - } - - public function spawnMonster(){ - $type = mt_rand(32, 35); - $x = mt_rand(5, 250); //need create a radius near player - $z = mt_rand(5, 250); - $y = $this->getY($x, $z); - if(!$y) return; - - $this->addMob(new Position($x+.5, $y, $z+.5, $this->world), ENTITY_MOB, $type); - } - - public function getY($x, $z){ - if($this->gameTime <= 9500){ - for($y = 127; $y > 0; --$y){ //get highest block - $block = $this->world->getBlock(new Vector3($x, $y, $z))->getID(); - if($block === GRASS){ - return ++$y; - } - if($block !== AIR){ - if($block == 18 or $block == 78 or $block == 31){ //Ignore Leaves, Snow Layer, Tall Grass - continue; - } - return false; - } - } - return false; - } - else{ //need recode that - for($y = 1; $y < 127; ++$y){ //get lowest block - $block = $this->world->getBlock(new Vector3($x, $y, $z)); - if($block->getID() === AIR){ - $downBlock = $this->world->getBlock(new Vector3($x, $y-1, $z)); - if($y === 1){ - return false; - } - if($downBlock instanceof LiquidBlock or $downBlock instanceof TransparentBlock){ - return false; - } - if(!($this->world->getBlock(new Vector3($x, $y+1, $z)) instanceof TransparentBlock)){ - return false; - } - return $y; - } - } - } - } - - public function addMob(Position $pos, $class, $type){ - $this->server->api->entity->summon($pos, $class, $type); - } - - /*public function checkForDespawn(){ - $players = $this->server->api->player->getAll(); - $randEid = null; - $entity = $this->server->api->entity->get($randEid); - - if(!$this->removeWhenFarAway($entity)) return; - - foreach($players as $p){ - if(Utils::distance($p, $entity) >= 48){ - $this->server->api->entity->remove($eid); - } - } - } - - public function removeWhenFarAway($entity){ - if($entity instanceof Monster) return true; - else return false; - }*/ - -} \ No newline at end of file diff --git a/src/world/Level.php b/src/world/Level.php index 1c10ba84..6826838f 100644 --- a/src/world/Level.php +++ b/src/world/Level.php @@ -31,6 +31,7 @@ class Level{ $this->usedChunks = []; $this->changedBlocks = []; $this->changedCount = []; + $this->mobSpawner = new MobSpawner($this); } public function close(){ @@ -77,6 +78,7 @@ class Level{ $this->level->close(); unset($this->level); } + unset($this->mobSpawner->level); } public function save($force = false, $extra = true){ @@ -268,6 +270,10 @@ class Level{ $e->update(); } } + + if(Entity::$updateOnTick && $server->ticks % 40 === 0){ //40 ticks delay + $this->mobSpawner->handle(); + } } public function setBlock(Vector3 $pos, Block $block, $update = true, $tiles = false, $direct = false){ @@ -536,4 +542,4 @@ class Level{ } return $this->server->api->block->scheduleBlockUpdate($pos, $delay, $type); } -} \ No newline at end of file +} diff --git a/src/world/MobSpawner.php b/src/world/MobSpawner.php new file mode 100644 index 00000000..85194f90 --- /dev/null +++ b/src/world/MobSpawner.php @@ -0,0 +1,75 @@ +server = ServerAPI::request(); + $this->level = $level; + } + + public function countEntities(){ + $ents = 0; + foreach($this->level->entityList as $e){ + if(!$e->isPlayer() && $e->class === ENTITY_MOB){ + ++$ents; + } + } + return $ents; + } + + public function handle(){ + if($this->countEntities() > self::MOB_LIMIT && count($this->level->players) <= 0){ + return false; //not spawning + } + return $this->spawnMobs(); + } + + public function spawnMobs(){ + + $t = $this->level->getTime(); + if($this->server->api->getProperty("spawn-animals") && $t <= 9500){ //Animal + $type = mt_rand(10, 13); + $baby = false; //TODO baby + }else + if($this->server->api->getProperty("spawn-mobs") && $t >= 10000){ //Monster + $type = mt_rand(32, 35); + $baby = 2; + }else{ + return false; + } + $x = mt_rand(0,255); + $z = mt_rand(0,255); + $y = $this->getSafeY($x, $z); + if(!$y || $y < 0){ + return false; + } + $data = $this->genPosData($x, $y, $z); + if($baby != 2) $data["IsBaby"] = $baby; + $e = $this->server->api->entity->add($this->level, 2, $type, $data); + if($e instanceof Entity){ + $this->server->api->entity->spawnToAll($e); + //console("[DEBUG] $type spawned at $x, $y, $z"); + } + return true; + } + + private function genPosData($x, $y, $z){ + return [ + "x" => $x, + "y" => $y, + "z" => $z + ]; + } + + protected function getSafeY($x, $z){ //first safe block + for($y = 0; $y < 128; ++$y){ + if($this->level->level->getBlock($x, $y, $z)[0] === AIR){ + return $y; //TODO checking aabb + } + } + return false; + } +} +