chunks are now unloaded from memory + rewrite

This commit is contained in:
GameHerobrine 2023-07-01 05:40:00 +03:00
parent 5cdfedfc75
commit c371fb9c8a
7 changed files with 298 additions and 208 deletions

View file

@ -475,13 +475,13 @@ class PlayerAPI{
public function remove($CID){ public function remove($CID){
if(isset($this->server->clients[$CID])){ if(isset($this->server->clients[$CID])){
$player = $this->server->clients[$CID]; $player = $this->server->clients[$CID];
unset($this->server->clients[$CID]);
$player->close(); $player->close();
if($player->username != "" and ($player->data instanceof Config)){ if($player->username != "" and ($player->data instanceof Config)){
$this->saveOffline($player->data); $this->saveOffline($player->data);
} }
$this->server->query("DELETE FROM players WHERE name = '" . $player->username . "';"); $this->server->query("DELETE FROM players WHERE name = '" . $player->username . "';");
$this->server->api->entity->remove($player->eid); $this->server->api->entity->remove($player->eid);
unset($this->server->clients[$CID]);
if($player->entity instanceof Entity){ if($player->entity instanceof Entity){
unset($player->entity->player); unset($player->entity->player);
//unset($player->entity); //unset($player->entity);

View file

@ -391,15 +391,21 @@ class Player{
$this->server->api->dhandle("player.armor", $data); $this->server->api->dhandle("player.armor", $data);
} }
} }
public function stopUsingChunk($X, $Z){
$ind = "$X:$Z";
if(isset($this->chunksLoaded[$ind])) unset($this->chunksLoaded[$ind]);
if(isset($this->chunksOrder[$ind])) unset($this->chunksOrder[$ind]);
$this->dataPacket(new UnloadChunkPacket($X, $Z));
}
public function orderChunks(){ public function orderChunks(){
if(!($this->entity instanceof Entity) or $this->connected === false){ if(!($this->entity instanceof Entity) or $this->connected === false){
return false; return false;
} }
$X = $this->entity->x >> 4; $X = ((int)$this->entity->x) >> 4;
$Z = $this->entity->z >> 4; $Z = ((int)$this->entity->z) >> 4;
$this->chunksOrder = []; $this->chunksOrder = [];
if($this->level->generatorType != 0) $chunkToUnload = $this->chunksLoaded; //if($this->level->generatorType != 0) $chunkToUnload = $this->chunksLoaded;
$startX = $this->level->generatorType === 1 ? $X - 4 : 0; $startX = $this->level->generatorType === 1 ? $X - 4 : 0;
$stopX = $this->level->generatorType === 1 ? $X + 4 : 15; $stopX = $this->level->generatorType === 1 ? $X + 4 : 15;
$startZ = $this->level->generatorType === 1 ? $Z - 4 : 0; $startZ = $this->level->generatorType === 1 ? $Z - 4 : 0;
@ -407,15 +413,15 @@ class Player{
for($x = $startX; $x <= $stopX; ++$x){ for($x = $startX; $x <= $stopX; ++$x){
for($z = $startZ; $z <= $stopZ; ++$z){ for($z = $startZ; $z <= $stopZ; ++$z){
$d = $x . ":" . $z; $d = $x . ":" . $z;
if($this->level->generatorType != 0) unset($chunkToUnload[$d]); //if($this->level->generatorType != 0) unset($chunkToUnload[$d]);
//if($x < 0 || $x > 15 || $z < 0 || $z > 15) continue; //TODO infinite worlds //if($x < 0 || $x > 15 || $z < 0 || $z > 15) continue;
if(!isset($this->chunksLoaded[$d])){ if(!isset($this->chunksLoaded[$d])){
$this->chunksOrder[$d] = abs($x - $X) + abs($z - $Z); $this->chunksOrder[$d] = abs($x - $X) + abs($z - $Z);
} }
} }
} }
asort($this->chunksOrder); asort($this->chunksOrder);
arsort($chunkToUnload); /*arsort($chunkToUnload);
if($this->level->generatorType != 0){ if($this->level->generatorType != 0){
foreach($chunkToUnload as $chunk => $useless){ foreach($chunkToUnload as $chunk => $useless){
$chunkI = explode(":", $chunk); $chunkI = explode(":", $chunk);
@ -426,7 +432,7 @@ class Player{
$this->level->freeChunk($cX, $cZ, $this); $this->level->freeChunk($cX, $cZ, $this);
$this->dataPacket(new UnloadChunkPacket($cX, $cZ)); $this->dataPacket(new UnloadChunkPacket($cX, $cZ));
} }
} }*/
$this->reload = true; $this->reload = true;
@ -460,7 +466,7 @@ class Player{
} }
} }
} }
$this->stopUsingChunk($X, $Z); //just in case
$pk = new FullChunkDataPacket; $pk = new FullChunkDataPacket;
$pk->chunkX = $X; $pk->chunkX = $X;
$pk->chunkZ = $Z; $pk->chunkZ = $Z;
@ -476,10 +482,7 @@ class Player{
if(count($this->chunksOrder) <= 0 && $this->level->generatorType != 0){ if(count($this->chunksOrder) <= 0 && $this->level->generatorType != 0){
$this->orderChunks(); $this->orderChunks();
} }
//if($this->reload && $this->level->generatorType != 0){
$this->getNextChunk($this->level); $this->getNextChunk($this->level);
// $this->reload = false;
//}
} }
public function getNextChunk($world){ public function getNextChunk($world){

View file

@ -219,7 +219,14 @@ class PMFLevel extends PMF{
$aZ = $z & 15; $aZ = $z & 15;
$this->chunkInfo[$index][0][$aX + ($aZ << 4)] = chr($id); $this->chunkInfo[$index][0][$aX + ($aZ << 4)] = chr($id);
} }
public function forceUnloadChunk($X, $Z, $save = true){
$X = (int) $X;
$Z = (int) $Z;
$index = $this->getIndex($X, $Z);
$this->chunks[$index] = null;
$this->chunkChange[$index] = null;
unset($this->chunks[$index], $this->chunkChange[$index]);
}
public function unloadChunk($X, $Z, $save = true){ public function unloadChunk($X, $Z, $save = true){
$X = (int) $X; $X = (int) $X;
$Z = (int) $Z; $Z = (int) $Z;

View file

@ -1,5 +1,7 @@
<?php <?php
/***REM_START***/
require_once("Random.php");
/***REM_END***/
/* /*
Mersenne Twister, 1.1.0 Mersenne Twister, 1.1.0
----------------------- -----------------------
@ -20,7 +22,9 @@
http://kingfisher.nfshost.com/sw/twister/ http://kingfisher.nfshost.com/sw/twister/
*/
Who made such a weird names? - GameHerobrine
*/
const N = 624; const N = 624;
const M = 397; const M = 397;
const MATRIX_A = 0x9908b0df; const MATRIX_A = 0x9908b0df;
@ -41,7 +45,7 @@ $val = MASK31 | (MASK31 << 1);
define("MASK32", $val); define("MASK32", $val);
class MersenneTwister{ //Does it even used? //rename back if any problems will be spotted class MersenneTwister extends Random{
const N = N; const N = N;
# the class constant is not used anywhere in this namespace, # the class constant is not used anywhere in this namespace,
@ -55,6 +59,58 @@ class MersenneTwister{ //Does it even used? //rename back if any problems will b
} }
} }
/**RANDOM IMPL START**/
public function setSeed($seed = false){
$this->init_with_integer($seed === false ? Utils::getRandomBytes(4, false) : $seed);
}
public function nextGaussian(){
if($this->haveNextNextGaussian){
$this->haveNextNextGaussian = false;
return $this->nextNextGaussian;
}else{
$v1 = $v2 = $s = null;
do{
$v1 = 2 * $this->nextFloat() - 1; // between -1.0 and 1.0
$v2 = 2 * $this->nextFloat() - 1; // between -1.0 and 1.0
$s = $v1 * $v1 + $v2 * $v2;
}while($s >= 1 || $s == 0);
$multiplier = sqrt(-2 * log($s) / $s);
$this->nextNextGaussian = $v2 * $multiplier;
$this->haveNextNextGaussian = true;
return $v1 * $multiplier;
}
}
function nextInt(){
return ($this->int32() >> 1);
}
function nextFloat(){
return $this->int32() * 2.3283064365386963E-010;
}
function nextBytes($byteCount){
return 0; //idc, not implemented
}
function nextSignedFloat(){
return $this->nextFloat(); //idc, not implemented
}
function nextSignedInt(){ //TODO
return $this->int31();
}
public function nextBoolean(){
return 0; //idc not implemented
}
public function nextRange($start = 0, $end = PHP_INT_MAX){
return $end === 0 ? 0 : (($this->int32() + $start) % $end);
}
/**RANDOM IMPL END**/
function init_with_integer($integer_seed){ function init_with_integer($integer_seed){
$integer_seed = force_32_bit_int($integer_seed); $integer_seed = force_32_bit_int($integer_seed);
@ -330,6 +386,7 @@ class MersenneTwister{ //Does it even used? //rename back if any problems will b
($this->int32() & MASK26)) * ($this->int32() & MASK26)) *
(1.0 / 9007199254740992.0); (1.0 / 9007199254740992.0);
} }
} }
function signed2unsigned($signed_integer){ function signed2unsigned($signed_integer){
@ -393,7 +450,7 @@ function force_32_bit_int($x){
a slight complication is that the sum of the low half-integers a slight complication is that the sum of the low half-integers
may not fit into 16 bits; any "overspill" is added to the sum may not fit into 16 bits; any "overspill" is added to the sum
of the high half-integers. of the high half-integers.
*/ */
function add_2($n1, $n2){ function add_2($n1, $n2){
$x = ($n1 & 0xffff) + ($n2 & 0xffff); $x = ($n1 & 0xffff) + ($n2 & 0xffff);
@ -469,7 +526,7 @@ function mul($a, $b){
/* /*
mul_by_1664525($x) should be more efficient than mul(1664525, $x). mul_by_1664525($x) should be more efficient than mul(1664525, $x).
*/ */
function mul_by_1664525($n){ function mul_by_1664525($n){
return unsigned2signed(fmod(1664525 * ($n >= 0 ? return unsigned2signed(fmod(1664525 * ($n >= 0 ?
$n : (TWO_TO_THE_32 + $n)), TWO_TO_THE_32)); $n : (TWO_TO_THE_32 + $n)), TWO_TO_THE_32));

View file

@ -3,9 +3,9 @@
//Unsecure, not used for "Real Randomness" //Unsecure, not used for "Real Randomness"
class Random{ class Random{
private $x, $y, $z, $w; public $x, $y, $z, $w;
private $haveNextNextGaussian = false; public $haveNextNextGaussian = false;
private $nextNextGaussian = 0; public $nextNextGaussian = 0;
public function __construct($seed = false){ public function __construct($seed = false){
$this->setSeed($seed); $this->setSeed($seed);

View file

@ -246,7 +246,7 @@ class Level{
} }
public function useChunk($X, $Z, Player $player){ public function useChunk($X, $Z, Player $player){
//if($X > 15 || $X < 0 || $Z > 15 || $Z < 0) return; //TODO inf worlds //ConsoleAPI::debug("$player uses $X $Z");
if(!isset($this->usedChunks[$X . "." . $Z])){ if(!isset($this->usedChunks[$X . "." . $Z])){
$this->usedChunks[$X . "." . $Z] = []; $this->usedChunks[$X . "." . $Z] = [];
} }
@ -390,6 +390,29 @@ class Level{
if(Entity::$updateOnTick && $server->ticks % 40 === 0){ //40 ticks delay if(Entity::$updateOnTick && $server->ticks % 40 === 0){ //40 ticks delay
$this->mobSpawner->handle(); $this->mobSpawner->handle();
} }
if($this->generatorType !== 0){ //chunk unloading
foreach($this->usedChunks as $c => $eids){
$xz = explode(".", $c); //bad idea, TODO use better indexes
foreach($eids as $cid => $_){
$p = nullsafe($this->server->clients[$cid], false);
if($p instanceof Player){
$dist = abs($xz[0] - ($p->entity->x >> 4)) + abs($xz[1] - ($p->entity->z >> 4));
if($dist > 8){
//ConsoleAPI::debug("{$p->player} freeing chunk: {$xz[0]} {$xz[1]}");
$this->freeChunk($xz[0], $xz[1], $p);
$p->stopUsingChunk($xz[0], $xz[1]);
}
}
}
if(count($eids) <= 0){
$outcome = $this->unloadChunk($xz[0], $xz[1], true);
unset($this->usedChunks[$c]);
//ConsoleAPI::debug("Unloading: {$xz[0]} {$xz[1]}, status: $outcome");
}
}
}
} }
public function setBlock(Vector3 $pos, Block $block, $update = true, $tiles = false, $direct = false){ public function setBlock(Vector3 $pos, Block $block, $update = true, $tiles = false, $direct = false){

View file

@ -54,7 +54,7 @@ class ExperimentalGenerator implements NewLevelGenerator{
public function init(Level $level, Random $random){ public function init(Level $level, Random $random){
$this->level = $level; $this->level = $level;
$this->random = $random; $this->random = $random;//new MersenneTwister();
$this->random->setSeed($this->level->level->getSeed()); $this->random->setSeed($this->level->level->getSeed());
$this->noiseBase = new NoiseGeneratorPerlin($this->random, 4); $this->noiseBase = new NoiseGeneratorPerlin($this->random, 4);
$this->selector = new BiomeSelector($this->random, BiomeSelector::$biomes[BIOME_PLAINS]); $this->selector = new BiomeSelector($this->random, BiomeSelector::$biomes[BIOME_PLAINS]);