using native MTRandom if php8.2+ is detected

This commit is contained in:
GameHerobrine 2023-12-01 06:24:53 +03:00
parent 648784b4ff
commit 70e4355d61
5 changed files with 81 additions and 55 deletions

View file

@ -52,7 +52,7 @@ define("MAJOR_VERSION", "2.0.0dev");
define("CODENAME", "Breadfence"); //i'm not very creative - kotyaralih define("CODENAME", "Breadfence"); //i'm not very creative - kotyaralih
define("CURRENT_MINECRAFT_VERSION", "v0.9.5 alpha"); define("CURRENT_MINECRAFT_VERSION", "v0.9.5 alpha");
define("CURRENT_API_VERSION", '13.1'); define("CURRENT_API_VERSION", '13.1');
define("CURRENT_PHP_VERSION", "5.5"); define("CURRENT_PHP_VERSION", "8.0");
$gitsha1 = false; $gitsha1 = false;
if(file_exists(FILE_PATH . ".git/refs/heads/master")){ //Found Git information! if(file_exists(FILE_PATH . ".git/refs/heads/master")){ //Found Git information!
define("GIT_COMMIT", strtolower(trim(file_get_contents(FILE_PATH . ".git/refs/heads/master")))); define("GIT_COMMIT", strtolower(trim(file_get_contents(FILE_PATH . ".git/refs/heads/master"))));

View file

@ -22,6 +22,8 @@ if(version_compare("8.0.0", PHP_VERSION) > 0){
++$errors; ++$errors;
} }
define("USE_NATIVE_RANDOM", version_compare("8.2.0", PHP_VERSION) <= 0);
if(php_sapi_name() !== "cli"){ if(php_sapi_name() !== "cli"){
console("[ERROR] You must run NostalgiaCore using the CLI.", true, true, 0); console("[ERROR] You must run NostalgiaCore using the CLI.", true, true, 0);
++$errors; ++$errors;

View file

@ -15,11 +15,8 @@ class Minecart extends Vehicle{
$this->z = isset($this->data["TileZ"]) ? $this->data["TileZ"]:$this->z; $this->z = isset($this->data["TileZ"]) ? $this->data["TileZ"]:$this->z;
$this->setHealth(3, "generic"); $this->setHealth(3, "generic");
//$this->setName((isset($objects[$this->type]) ? $objects[$this->type]:$this->type)); //$this->setName((isset($objects[$this->type]) ? $objects[$this->type]:$this->type));
$this->width = 1; $this->width = 0.98;
/*$this->moveVector[Entity::NORTH] = new Vector3(-1, 0, 0); $this->height = 0.7;
$this->moveVector[Entity::SOUTH] = new Vector3(1, 0, 0);
$this->moveVector[Entity::EAST] = new Vector3(0, 0, -1);
$this->moveVector[Entity::WEST] = new Vector3(0, 0, 1);*/
$this->update(); $this->update();
} }

View file

@ -1,9 +1,33 @@
<?php <?php
/** if(USE_NATIVE_RANDOM){
* Fatser implementation of Mersenne Twister eval('
*/ class MTRandom
class MTRandom {
{ public $native;
public function __construct($seed = null){
$this->native = new \Random\Randomizer(new \Random\Engine\Mt19937($seed));
}
public function setSeed($seed){
$this->native = new \Random\Randomizer(new \Random\Engine\Mt19937($seed));
}
public function genRandInt(){
return $this->native->getInt(0, 0x7fffffff);
}
public function nextInt($bound = null){
return $bound == null ? $this->native->nextInt() : $this->native->getInt(0, $bound - 1);
}
public function nextFloat(){
return $this->native->getInt(0, 0x7fffffff) / 0x7fffffff;
}
}
');
}else{
eval('
class MTRandom
{
const MAG = [0, 0x9908b0df]; const MAG = [0, 0x9908b0df];
public $mt; public $mt;
public $index = 0; public $index = 0;
@ -52,5 +76,8 @@ class MTRandom
public function nextFloat(){ public function nextFloat(){
return $this->genRandInt() / 0xffffffff; return $this->genRandInt() / 0xffffffff;
} }
}
');
} }

View file

@ -152,7 +152,7 @@ class NormalGenerator implements NewLevelGenerator{
$this->level->level->setBiomeIdArrayForChunk($chunkX, $chunkZ, $biomes); $this->level->level->setBiomeIdArrayForChunk($chunkX, $chunkZ, $biomes);
$this->level->setMiniChunk($chunkX, $chunkZ, $chunkY, $chunk); $this->level->setMiniChunk($chunkX, $chunkZ, $chunkY, $chunk);
} }
//$this->caveGenerator->generate($this->level, $chunkX, $chunkZ); TODO speedup //$this->caveGenerator->generate($this->level, $chunkX, $chunkZ); //TODO speedup
} }
public function populateChunk($chunkX, $chunkZ){ public function populateChunk($chunkX, $chunkZ){