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,26 +1,30 @@
<?php <?php
/***REM_START***/
require_once("Random.php");
/***REM_END***/
/* /*
Mersenne Twister, 1.1.0 Mersenne Twister, 1.1.0
----------------------- -----------------------
This is an implementation of the Mersenne Twister. The current version This is an implementation of the Mersenne Twister. The current version
number is 1.1.0. number is 1.1.0.
Much of the code here was derived from the C code in the file Much of the code here was derived from the C code in the file
mt19937ar.c, which is in the archive mt19937ar.sep.tgz, available from mt19937ar.c, which is in the archive mt19937ar.sep.tgz, available from
http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/MT2002/emt19937ar.html. http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/MT2002/emt19937ar.html.
Some of the original C code is preserved in a few of the comments below. Some of the original C code is preserved in a few of the comments below.
It is non-trivial to map the C code onto PHP, because PHP has no It is non-trivial to map the C code onto PHP, because PHP has no
unsigned integer type. What we do here is to use integers when we can, unsigned integer type. What we do here is to use integers when we can,
and floats when we must. and floats when we must.
Only the class `twister' is part of the API; everything else is private. Only the class `twister' is part of the API; everything else is private.
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);
@ -69,9 +125,9 @@ class MersenneTwister{ //Does it even used? //rename back if any problems will b
$mt[$mti] = add_2(mul(1812433253, $mt[$mti] = add_2(mul(1812433253,
($mt[$mti - 1] ^ (($mt[$mti - 1] >> 30) & 3))), $mti); ($mt[$mti - 1] ^ (($mt[$mti - 1] >> 30) & 3))), $mti);
/* /*
mt[mti] = mt[mti] =
(1812433253UL * (mt[mti-1] ^ (mt[mti-1] >> 30)) + mti); (1812433253UL * (mt[mti-1] ^ (mt[mti-1] >> 30)) + mti);
*/ */
} }
} }
@ -89,8 +145,8 @@ class MersenneTwister{ //Does it even used? //rename back if any problems will b
foreach($filenames as $filename){ foreach($filenames as $filename){
$contents = $contents =
file_get_contents($filename, false, null, 0, file_get_contents($filename, false, null, 0,
$limit_applies ? $limit - strlen($data) : -1); $limit_applies ? $limit - strlen($data) : -1);
if($contents === false){ if($contents === false){
throw new Exception("problem reading from $filename"); throw new Exception("problem reading from $filename");
@ -120,7 +176,7 @@ class MersenneTwister{ //Does it even used? //rename back if any problems will b
function init_with_array(array $integer_array){ function init_with_array(array $integer_array){
$integer_array = $integer_array =
array_map("force_32_bit_int", $integer_array); array_map("force_32_bit_int", $integer_array);
$mt = &$this->mt; $mt = &$this->mt;
$mti = &$this->mti; $mti = &$this->mti;
@ -136,9 +192,9 @@ class MersenneTwister{ //Does it even used? //rename back if any problems will b
mul_by_1664525($mt[$i - 1] ^ (($mt[$i - 1] >> 30) & 3)), mul_by_1664525($mt[$i - 1] ^ (($mt[$i - 1] >> 30) & 3)),
$integer_array[$j], $j); $integer_array[$j], $j);
/* /*
mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1664525UL)) mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1664525UL))
+ init_key[j] + j; + init_key[j] + j;
*/ */
$i++; $i++;
$j++; $j++;
if($i >= N){ if($i >= N){
@ -153,9 +209,9 @@ class MersenneTwister{ //Does it even used? //rename back if any problems will b
$mt[$i] = sub($mt[$i] ^ $mt[$i] = sub($mt[$i] ^
mul($mt[$i - 1] ^ (($mt[$i - 1] >> 30) & 3), 1566083941), $i); mul($mt[$i - 1] ^ (($mt[$i - 1] >> 30) & 3), 1566083941), $i);
/* /*
mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1566083941UL)) mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1566083941UL))
- i; - i;
*/ */
$i++; $i++;
if($i >= N){ if($i >= N){
$mt[0] = $mt[N - 1]; $mt[0] = $mt[N - 1];
@ -184,7 +240,7 @@ class MersenneTwister{ //Does it even used? //rename back if any problems will b
for(; $kk < N - 1; $kk++){ for(; $kk < N - 1; $kk++){
$y = ($mt[$kk] & UPPER_MASK) | ($mt[$kk + 1] & LOWER_MASK); $y = ($mt[$kk] & UPPER_MASK) | ($mt[$kk + 1] & LOWER_MASK);
$mt[$kk] = $mt[$kk] =
$mt[$kk + (M - N)] ^ (($y >> 1) & MASK31) ^ $mag01[$y & 1]; $mt[$kk + (M - N)] ^ (($y >> 1) & MASK31) ^ $mag01[$y & 1];
} }
$y = ($mt[N - 1] & UPPER_MASK) | ($mt[0] & LOWER_MASK); $y = ($mt[N - 1] & UPPER_MASK) | ($mt[0] & LOWER_MASK);
$mt[N - 1] = $mt[M - 1] ^ (($y >> 1) & MASK31) ^ $mag01[$y & 1]; $mt[N - 1] = $mt[M - 1] ^ (($y >> 1) & MASK31) ^ $mag01[$y & 1];
@ -224,7 +280,7 @@ class MersenneTwister{ //Does it even used? //rename back if any problems will b
if($remainder == 0){ if($remainder == 0){
return $lower_bound + return $lower_bound +
($this->int32() & unsigned2signed($size_of_range - 1)); ($this->int32() & unsigned2signed($size_of_range - 1));
}else{ }else{
$start_of_partial_range = $pow_2_32 - $remainder; $start_of_partial_range = $pow_2_32 - $remainder;
$start_as_int = unsigned2signed($start_of_partial_range); $start_as_int = unsigned2signed($start_of_partial_range);
@ -233,7 +289,7 @@ class MersenneTwister{ //Does it even used? //rename back if any problems will b
}while($rand >= $start_as_int && $rand < 0); }while($rand >= $start_as_int && $rand < 0);
$result = $lower_bound + $result = $lower_bound +
fmod(signed2unsigned($rand), $size_of_range); fmod(signed2unsigned($rand), $size_of_range);
return intval($result); return intval($result);
} }
@ -277,7 +333,7 @@ class MersenneTwister{ //Does it even used? //rename back if any problems will b
function rangereal_open($lower_bound, $upper_bound){ function rangereal_open($lower_bound, $upper_bound){
do{ do{
$rand = $lower_bound + $rand = $lower_bound +
$this->real_open() * ($upper_bound - $lower_bound); $this->real_open() * ($upper_bound - $lower_bound);
}while($rand <= $lower_bound || $rand >= $upper_bound); }while($rand <= $lower_bound || $rand >= $upper_bound);
return $rand; return $rand;
@ -292,25 +348,25 @@ class MersenneTwister{ //Does it even used? //rename back if any problems will b
function rangereal_halfopen($lower_bound, $upper_bound){ function rangereal_halfopen($lower_bound, $upper_bound){
do{ do{
$rand = $lower_bound + $rand = $lower_bound +
$this->real_halfopen() * ($upper_bound - $lower_bound); $this->real_halfopen() * ($upper_bound - $lower_bound);
}while($rand >= $upper_bound); }while($rand >= $upper_bound);
/* /*
$rand cannot go any lower than $lower_bound, because $rand cannot go any lower than $lower_bound, because
$this->real_halfopen() cannot go any lower than 0 $this->real_halfopen() cannot go any lower than 0
*/ */
return $rand; return $rand;
} }
/* /*
in each of the next 3 functions, we loop until we have a number that in each of the next 3 functions, we loop until we have a number that
meets the function's post-condition. this may be more work than meets the function's post-condition. this may be more work than
is really necessary, but i am concerned about rounding errors. is really necessary, but i am concerned about rounding errors.
why no rangereal_closed? because, due to the aforementioned why no rangereal_closed? because, due to the aforementioned
rounding errors, i am unable to guarantee that $upper_bound rounding errors, i am unable to guarantee that $upper_bound
would be a possible return value of such a function. would be a possible return value of such a function.
*/ */
function real_halfopen(){ function real_halfopen(){
return signed2unsigned($this->int32()) * (1.0 / 4294967296.0); return signed2unsigned($this->int32()) * (1.0 / 4294967296.0);
@ -319,7 +375,7 @@ class MersenneTwister{ //Does it even used? //rename back if any problems will b
function rangereal_halfopen2($lower_bound, $upper_bound){ function rangereal_halfopen2($lower_bound, $upper_bound){
do{ do{
$rand = $lower_bound + $rand = $lower_bound +
$this->real_halfopen2() * ($upper_bound - $lower_bound); $this->real_halfopen2() * ($upper_bound - $lower_bound);
}while($rand >= $upper_bound); }while($rand >= $upper_bound);
return $rand; return $rand;
@ -327,9 +383,10 @@ class MersenneTwister{ //Does it even used? //rename back if any problems will b
function real_halfopen2(){ function real_halfopen2(){
return ((($this->int32() & MASK27) * 67108864.0) + return ((($this->int32() & MASK27) * 67108864.0) +
($this->int32() & MASK26)) * ($this->int32() & MASK26)) *
(1.0 / 9007199254740992.0); (1.0 / 9007199254740992.0);
} }
} }
function signed2unsigned($signed_integer){ function signed2unsigned($signed_integer){
@ -337,7 +394,7 @@ function signed2unsigned($signed_integer){
## assert(($signed_integer & ~MASK32) === 0); ## assert(($signed_integer & ~MASK32) === 0);
return $signed_integer >= 0 ? $signed_integer : return $signed_integer >= 0 ? $signed_integer :
TWO_TO_THE_32 + $signed_integer; TWO_TO_THE_32 + $signed_integer;
} }
function unsigned2signed($unsigned_integer){ function unsigned2signed($unsigned_integer){
@ -351,21 +408,21 @@ function unsigned2signed($unsigned_integer){
function force_32_bit_int($x){ function force_32_bit_int($x){
/* /*
it would be un-PHP-like to require is_integer($x), it would be un-PHP-like to require is_integer($x),
so we have to handle cases like this: so we have to handle cases like this:
$x === pow(2, 31) $x === pow(2, 31)
$x === strval(pow(2, 31)) $x === strval(pow(2, 31))
we are also opting to do something sensible (rather than dying) we are also opting to do something sensible (rather than dying)
if the seed is outside the range of a 32-bit unsigned integer. if the seed is outside the range of a 32-bit unsigned integer.
*/ */
if(is_integer($x)){ if(is_integer($x)){
/* /*
we mask in case we are on a 64-bit machine and at least one we mask in case we are on a 64-bit machine and at least one
bit is set between position 32 and position 63. bit is set between position 32 and position 63.
*/ */
return $x & MASK32; return $x & MASK32;
}else{ }else{
$x = floatval($x); $x = floatval($x);
@ -383,23 +440,23 @@ function force_32_bit_int($x){
} }
/* /*
takes 2 integers, treats them as unsigned 32-bit integers, takes 2 integers, treats them as unsigned 32-bit integers,
and adds them. and adds them.
it works by splitting each integer into it works by splitting each integer into
2 "half-integers", then adding the high and low half-integers 2 "half-integers", then adding the high and low half-integers
separately. separately.
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);
return (((($n1 >> 16) & 0xffff) + return (((($n1 >> 16) & 0xffff) +
(($n2 >> 16) & 0xffff) + (($n2 >> 16) & 0xffff) +
($x >> 16)) << 16) | ($x & 0xffff); ($x >> 16)) << 16) | ($x & 0xffff);
} }
# takes 2 integers, treats them as unsigned 32-bit integers, # takes 2 integers, treats them as unsigned 32-bit integers,
@ -411,9 +468,9 @@ function add_3($n1, $n2, $n3){
$x = ($n1 & 0xffff) + ($n2 & 0xffff) + ($n3 & 0xffff); $x = ($n1 & 0xffff) + ($n2 & 0xffff) + ($n3 & 0xffff);
return (((($n1 >> 16) & 0xffff) + return (((($n1 >> 16) & 0xffff) +
(($n2 >> 16) & 0xffff) + (($n2 >> 16) & 0xffff) +
(($n3 >> 16) & 0xffff) + (($n3 >> 16) & 0xffff) +
($x >> 16)) << 16) | ($x & 0xffff); ($x >> 16)) << 16) | ($x & 0xffff);
} }
# takes 2 integers, treats them as unsigned 32-bit integers, # takes 2 integers, treats them as unsigned 32-bit integers,
@ -424,42 +481,42 @@ function add_3($n1, $n2, $n3){
# #
function sub($a, $b){ function sub($a, $b){
return (($a & MASK31) - ($b & MASK31)) ^ return (($a & MASK31) - ($b & MASK31)) ^
(($a ^ $b) & 0x80000000); (($a ^ $b) & 0x80000000);
} }
function mul($a, $b){ function mul($a, $b){
/* /*
a and b, considered as unsigned integers, can be expressed as follows: a and b, considered as unsigned integers, can be expressed as follows:
a = 2**16 * a1 + a2, a = 2**16 * a1 + a2,
b = 2**16 * b1 + b2, b = 2**16 * b1 + b2,
where where
0 <= a2 < 2**16, 0 <= a2 < 2**16,
0 <= b2 < 2**16. 0 <= b2 < 2**16.
given those 2 equations, what this function essentially does is to given those 2 equations, what this function essentially does is to
use the following identity: use the following identity:
a * b = 2**32 * a1 * b1 + 2**16 * a1 * b2 + 2**16 * b1 * a2 + a2 * b2 a * b = 2**32 * a1 * b1 + 2**16 * a1 * b2 + 2**16 * b1 * a2 + a2 * b2
note that the first term, i.e. 2**32 * a1 * b1, is unnecessary here, note that the first term, i.e. 2**32 * a1 * b1, is unnecessary here,
so we don't compute it. so we don't compute it.
we could make the following code clearer by using intermediate we could make the following code clearer by using intermediate
variables, but that would probably hurt performance. variables, but that would probably hurt performance.
*/ */
return unsigned2signed( return unsigned2signed(
fmod( fmod(
TWO_TO_THE_16 * TWO_TO_THE_16 *
/* /*
the next line of code calculates a1 * b2, the next line of code calculates a1 * b2,
the line after that calculates b1 * a2, the line after that calculates b1 * a2,
and the line after that calculates a2 * b2. and the line after that calculates a2 * b2.
*/ */
((($a >> 16) & 0xffff) * ($b & 0xffff) + ((($a >> 16) & 0xffff) * ($b & 0xffff) +
(($b >> 16) & 0xffff) * ($a & 0xffff)) + (($b >> 16) & 0xffff) * ($a & 0xffff)) +
($a & 0xffff) * ($b & 0xffff), ($a & 0xffff) * ($b & 0xffff),
@ -468,19 +525,19 @@ 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));
} }
function do_bc_op($bc_op, array $numbers){ function do_bc_op($bc_op, array $numbers){
$modulus = pow(2, 32); $modulus = pow(2, 32);
$result = $result =
call_user_func_array($bc_op, call_user_func_array($bc_op,
array_map("signed2unsigned", $numbers)); array_map("signed2unsigned", $numbers));
$result = bcmod($result, $modulus); $result = bcmod($result, $modulus);

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]);