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){
if(isset($this->server->clients[$CID])){
$player = $this->server->clients[$CID];
unset($this->server->clients[$CID]);
$player->close();
if($player->username != "" and ($player->data instanceof Config)){
$this->saveOffline($player->data);
}
$this->server->query("DELETE FROM players WHERE name = '" . $player->username . "';");
$this->server->api->entity->remove($player->eid);
unset($this->server->clients[$CID]);
if($player->entity instanceof Entity){
unset($player->entity->player);
//unset($player->entity);

View file

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

View file

@ -219,7 +219,14 @@ class PMFLevel extends PMF{
$aZ = $z & 15;
$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){
$X = (int) $X;
$Z = (int) $Z;

View file

@ -1,5 +1,7 @@
<?php
/***REM_START***/
require_once("Random.php");
/***REM_END***/
/*
Mersenne Twister, 1.1.0
-----------------------
@ -20,6 +22,8 @@
http://kingfisher.nfshost.com/sw/twister/
Who made such a weird names? - GameHerobrine
*/
const N = 624;
const M = 397;
@ -41,7 +45,7 @@ $val = MASK31 | (MASK31 << 1);
define("MASK32", $val);
class MersenneTwister{ //Does it even used? //rename back if any problems will be spotted
class MersenneTwister extends Random{
const N = N;
# 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){
$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)) *
(1.0 / 9007199254740992.0);
}
}
function signed2unsigned($signed_integer){

View file

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

View file

@ -246,7 +246,7 @@ class Level{
}
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])){
$this->usedChunks[$X . "." . $Z] = [];
}
@ -390,6 +390,29 @@ class Level{
if(Entity::$updateOnTick && $server->ticks % 40 === 0){ //40 ticks delay
$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){

View file

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