mirror of
https://github.com/oldminecraftcommunity/NostalgiaCore.git
synced 2026-11-22 20:49:43 +00:00
reworked mob spawn, added mob despawn if far away, mobs will not die if their pos is >255 or <0, getBlockID/getBlock return INVISIBLE_BEDROCK if pos >255 or <0 (xz)
This commit is contained in:
parent
271f82f5ed
commit
9ee4b08657
6 changed files with 98 additions and 25 deletions
|
|
@ -184,12 +184,18 @@ class EntityAPI{
|
||||||
|
|
||||||
public function remove($eid){
|
public function remove($eid){
|
||||||
if(isset($this->entities[$eid])){
|
if(isset($this->entities[$eid])){
|
||||||
|
$level = $this->entities[$eid]->level;
|
||||||
$this->entities[$eid]->closed = true;
|
$this->entities[$eid]->closed = true;
|
||||||
if($this->entities[$eid]->level instanceof Level){
|
if($level instanceof Level){
|
||||||
$cX = (int)$this->entities[$eid]->x >> 4;
|
$cX = (int)$this->entities[$eid]->x >> 4;
|
||||||
$cZ = (int)$this->entities[$eid]->z >> 4;
|
$cZ = (int)$this->entities[$eid]->z >> 4;
|
||||||
$index = "$cX $cZ";
|
$index = "$cX $cZ";
|
||||||
unset($this->entities[$eid]->level->entityListPositioned[$index][$eid]);
|
unset($level->entityListPositioned[$index][$eid]);
|
||||||
|
if(isset($level->mobSpawner->entityAffectedPlayers[$eid])){
|
||||||
|
$pid = $level->mobSpawner->entityAffectedPlayers[$eid];
|
||||||
|
unset($level->mobSpawner->entityAffectedPlayers[$eid]);
|
||||||
|
unset($level->mobSpawner->playerAffectedEIDS[$pid][$eid]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if($this->entities[$eid]->isPlayer()){
|
if($this->entities[$eid]->isPlayer()){
|
||||||
$pk = new RemovePlayerPacket;
|
$pk = new RemovePlayerPacket;
|
||||||
|
|
|
||||||
|
|
@ -414,10 +414,10 @@ class Entity extends Position
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if($this->class !== ENTITY_PLAYER and ($this->x <= 0 or $this->z <= 0 or $this->x >= 256 or $this->z >= 256 or $this->y >= 128 or $this->y <= 0)){
|
//if($this->class !== ENTITY_PLAYER and ($this->x <= 0 or $this->z <= 0 or $this->x >= 256 or $this->z >= 256 or $this->y >= 128 or $this->y <= 0)){
|
||||||
$this->close();
|
// $this->close();
|
||||||
return false;
|
// return false;
|
||||||
}
|
//}
|
||||||
|
|
||||||
if($this->dead === true){
|
if($this->dead === true){
|
||||||
$this->fire = 0;
|
$this->fire = 0;
|
||||||
|
|
|
||||||
|
|
@ -90,7 +90,12 @@ abstract class Living extends Entity implements Pathfindable{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if($this->closed) return;
|
if($this->closed) return;
|
||||||
|
|
||||||
|
$check = $this->level->mobSpawner->checkDespawn($this);
|
||||||
|
if($check){
|
||||||
|
$this->close();
|
||||||
|
return;
|
||||||
|
}
|
||||||
parent::update($now);
|
parent::update($now);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -332,7 +332,11 @@ class PMFLevel extends PMF{
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getBlockID($x, $y, $z){
|
public function getBlockID($x, $y, $z){
|
||||||
if($x < 0 || $x > 255 || $z < 0 || $z > 255 || $y < 0 || $y > 127){
|
if($x < 0 || $x > 255 || $z < 0 || $z > 255){
|
||||||
|
return INVISIBLE_BEDROCK;
|
||||||
|
}
|
||||||
|
|
||||||
|
if($y > 127 || $y < 0){
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
$X = $x >> 4;
|
$X = $x >> 4;
|
||||||
|
|
@ -428,7 +432,10 @@ class PMFLevel extends PMF{
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getBlock($x, $y, $z){
|
public function getBlock($x, $y, $z){
|
||||||
if($x < 0 || $x > 255 || $z < 0 || $z > 255 || $y < 0 || $y > 127){
|
if($x < 0 || $x > 255 || $z < 0 || $z > 255){
|
||||||
|
return [INVISIBLE_BEDROCK, 0];
|
||||||
|
}
|
||||||
|
if($y < 0 || $y > 127){
|
||||||
return [AIR, 0];
|
return [AIR, 0];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -467,7 +467,7 @@ class Level{
|
||||||
|
|
||||||
$this->checkSleep();
|
$this->checkSleep();
|
||||||
|
|
||||||
if($server->ticks % 40 === 0){ //40 ticks delay
|
if($server->ticks % 10 === 0){ //10 ticks delay
|
||||||
$this->mobSpawner->handle();
|
$this->mobSpawner->handle();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,14 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
class MobSpawner{
|
class MobSpawner{
|
||||||
public static $spawnAnimals = false, $spawnMobs = false;
|
public static $spawnAnimals = false, $spawnMobs = false, $maxMobsNearPlayerAtOnce = 10;
|
||||||
private $server;
|
private $server;
|
||||||
public $level;
|
public $level;
|
||||||
|
public $totalMobsPerPlayer;
|
||||||
|
public $playerAffectedEIDS = [];
|
||||||
|
public $entityAffectedPlayers = [];
|
||||||
public static $MOB_LIMIT = 50;
|
public static $MOB_LIMIT = 50;
|
||||||
|
|
||||||
public function __construct(Level $level){
|
public function __construct(Level $level){
|
||||||
$this->server = ServerAPI::request();
|
$this->server = ServerAPI::request();
|
||||||
$this->level = $level;
|
$this->level = $level;
|
||||||
|
|
@ -13,11 +17,38 @@ class MobSpawner{
|
||||||
public function countEntities(){
|
public function countEntities(){
|
||||||
return $this->level->totalMobsAmount;
|
return $this->level->totalMobsAmount;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function checkDespawn(Living $living){
|
||||||
|
if(!isset($this->entityAffectedPlayers[$living->eid])){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$playerID = $this->entityAffectedPlayers[$living->eid];
|
||||||
|
$player = $this->level->entityList[$playerID] ?? false;
|
||||||
|
if($player === false){
|
||||||
|
//TODO try retargetting other player?
|
||||||
|
return true;
|
||||||
|
}else{
|
||||||
|
$diffX = $living->x - $player->x;
|
||||||
|
$diffZ = $living->z - $player->z;
|
||||||
|
$dist = $diffX*$diffX + $diffZ*$diffZ;
|
||||||
|
if($dist <= 512){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $dist > 4096 || mt_rand($dist, 4096) == $dist; //force despawn 64 blocks away or despawn randomly
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
public function handle(){
|
public function handle(){
|
||||||
if($this->countEntities() > self::$MOB_LIMIT || count($this->level->players) <= 0){
|
if($this->countEntities() > self::$MOB_LIMIT || count($this->level->players) <= 0){
|
||||||
return false; //not spawning
|
return false; //not spawning
|
||||||
}
|
}
|
||||||
|
$svd = $this->totalMobsPerPlayer;
|
||||||
|
$this->totalMobsPerPlayer = min(ceil(self::$MOB_LIMIT / count($this->level->players)), self::$maxMobsNearPlayerAtOnce);
|
||||||
|
if($svd != $this->totalMobsPerPlayer) ConsoleAPI::debug("Changed total mobs per player from $svd to {$this->totalMobsPerPlayer}.");
|
||||||
|
|
||||||
return $this->spawnMobs();
|
return $this->spawnMobs();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -34,21 +65,45 @@ class MobSpawner{
|
||||||
}else{
|
}else{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
$x = mt_rand(0,255);
|
|
||||||
$z = mt_rand(0,255);
|
|
||||||
$y = $this->getSafeY($x, $z, $grassOnly, $type >= 32 && $type <= 36 && $type != 35);
|
|
||||||
if(!$y || $y < 0){
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
$data = $this->genPosData($x, $y + 0.5, $z);
|
|
||||||
if($baby != 2) $data["IsBaby"] = $baby;
|
|
||||||
|
|
||||||
$e = $this->server->api->entity->add($this->level, 2, $type, $data);
|
foreach($this->level->players as $player){
|
||||||
|
|
||||||
if($e instanceof Entity){
|
if(isset($this->playerAffectedEIDS[$player->entity->eid]) && count($this->playerAffectedEIDS[$player->entity->eid]) > $this->totalMobsPerPlayer){
|
||||||
$this->server->api->entity->spawnToAll($e);
|
continue;
|
||||||
//console("[DEBUG] $type spawned at $x, $y, $z");
|
}
|
||||||
|
|
||||||
|
$x = mt_rand($player->entity->x - 32, $player->entity->x + 32);
|
||||||
|
$z = mt_rand($player->entity->z - 32, $player->entity->z + 32);
|
||||||
|
$diffX = $x-$player->entity->x;
|
||||||
|
$diffZ = $z-$player->entity->z;
|
||||||
|
$dist = $diffX*$diffX + $diffZ*$diffZ;
|
||||||
|
if($dist < 768){
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$y = $this->getSafeY($x, $z, $grassOnly, $type >= 32 && $type <= 36 && $type != 35);
|
||||||
|
if(!$y || $y < 0){
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
$data = $this->genPosData($x, $y + 0.5, $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");
|
||||||
|
}
|
||||||
|
if(!isset($this->playerAffectedEIDS[$player->entity->eid])){
|
||||||
|
$this->playerAffectedEIDS[$player->entity->eid] = [$e->eid => true];
|
||||||
|
}else{
|
||||||
|
$this->playerAffectedEIDS[$player->entity->eid][$e->eid] = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->entityAffectedPlayers[$e->eid] = $player->entity->eid;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue