Added naturally spawn mobs in world

enabling in extra.properties on "experemental-mob-features"

code from mobtest3.2-test6
This commit is contained in:
ArkQuark 2022-12-14 21:17:26 +03:00
parent f2a4cf2337
commit a75b4cc4de
4 changed files with 125 additions and 0 deletions

View file

@ -17,6 +17,10 @@ class EntityAPI{
$this->server->api->console->register("summon", "<mob>", [$this, "commandHandler"]);
$this->server->api->console->register("spawnmob", "<mob>", [$this, "commandHandler"]);
$this->server->api->console->register("despawn", "", [$this, "CommandHandler"]);
if(($this->serverSpawnAnimals or $this->serverSpawnMobs) and Entity::$updateOnTick){
(new MobSpawner())->init();
}
}

View file

@ -93,6 +93,8 @@ 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/material/Item.php");
require_once(FILE_PATH . "/src/material/item/base/ItemTool.php");
require_once(FILE_PATH . "/src/material/item/base/ItemHoe.php");

118
src/entity/MobSpawner.php Normal file
View file

@ -0,0 +1,118 @@
<?php
class MobSpawner{
public function init(){
$this->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;
}*/
}

View file

@ -7,6 +7,7 @@ class Sheep extends Animal{
parent::__construct($level, $eid, $class, $type, $data);
$this->setHealth(isset($this->data["Health"]) ? $this->data["Health"] : 8, "generic");
$this->setName("Sheep");
$this->data["Sheared"] = isset($this->data["Sheared"]) ? $this->data["Sheared"] : 0;
$this->data["Color"] = isset($this->data["Color"]) ? $this->data["Color"] : $this->sheepColor();
$this->setSize($this->isBaby() ? 0.45 : 0.9, $this->isBaby() ? 0.675 : 1.3);
$this->setSpeed(0.25);