mirror of
https://github.com/oldminecraftcommunity/NostalgiaCore.git
synced 2026-11-21 12:09:43 +00:00
MobSpawner rewrite
This commit is contained in:
parent
a75b4cc4de
commit
4835c15747
6 changed files with 98 additions and 131 deletions
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
75
src/world/MobSpawner.php
Normal file
75
src/world/MobSpawner.php
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
<?php
|
||||
|
||||
class MobSpawner{
|
||||
private $server;
|
||||
public $level;
|
||||
const MOB_LIMIT = 25; //constant for now
|
||||
public function __construct(Level $level){
|
||||
$this->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;
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue