mirror of
https://github.com/oldminecraftcommunity/NostalgiaCore.git
synced 2026-11-20 11:39:43 +00:00
MobSpawner rewrite
This commit is contained in:
parent
a75b4cc4de
commit
4835c15747
6 changed files with 98 additions and 131 deletions
|
|
@ -17,10 +17,6 @@ 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();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -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 . ";");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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***/
|
||||
/***REM_END***/
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,118 +0,0 @@
|
|||
<?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;
|
||||
}*/
|
||||
|
||||
}
|
||||
|
|
@ -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