added mtrandom from nc 0.9.5

This commit is contained in:
GameHerobrine 2023-11-30 07:23:15 +03:00
parent f3228ece20
commit 38ee2f8b09
16 changed files with 75 additions and 19 deletions

56
src/utils/MTRandom.php Normal file
View file

@ -0,0 +1,56 @@
<?php
/**
* Fatser implementation of Mersenne Twister
*/
class MTRandom
{
const MAG = [0, 0x9908b0df];
public $mt;
public $index = 0;
public function __construct($seed = null){
$this->setSeed($seed == null ? (int)microtime(1) : $seed);
}
public function setSeed($seed){
$this->mt[0] = $seed & 0xffffffff;
for($this->index = 1; $this->index < 624; ++$this->index){
$this->mt[$this->index] = (0x6c078965 * ($this->mt[$this->index-1] >> 30 ^ $this->mt[$this->index - 1]) + $this->index) & 0xffffffff;
}
}
public function genRandInt(){
if($this->index >= 624 || $this->index < 0){
if($this->index >= 625 || $this->index < 0) $this->setSeed(4357);
for($kk = 0; $kk < 227; ++$kk){
$y = ($this->mt[$kk] & 0x80000000) | ($this->mt[$kk + 1] & 0x7fffffff);
$this->mt[$kk] = $this->mt[$kk+397] ^ ($y >> 1) ^ MTRandom::MAG[$y & 0x1];
}
for(;$kk < 623; ++$kk){
$y = ($this->mt[$kk] & 0x80000000) | ($this->mt[$kk + 1] & 0x7fffffff);
$this->mt[$kk] = $this->mt[$kk-227] ^ ($y >> 1) ^ MTRandom::MAG[$y & 0x1];
}
$y = ($this->mt[623] & 0x80000000) | ($this->mt[0] & 0x7fffffff);
$this->mt[623] = $this->mt[396] ^ ($y >> 1) ^ MTRandom::MAG[$y & 0x1];
$this->index = 0;
}
$y = $this->mt[$this->index++];
$y ^= ($y >> 11);
$y ^= ($y << 7) & 0x9d2c5680;
$y ^= ($y << 15) & 0xefc60000;
$y ^= ($y >> 18);
return $y;
}
public function nextInt($bound = null){
return $bound == null ? $this->genRandInt() >> 1 : $this->genRandInt() % $bound;
}
public function nextFloat(){
return $this->genRandInt() / 0xffffffff;
}
}

View file

@ -7,7 +7,7 @@ require_once("NoiseGenerator.php");
class NoiseGeneratorOctaves extends NoiseGenerator{
public $octaves;
private $generatorCollection;
public function __construct(MersenneTwister $random, $octaves){
public function __construct(MTRandom $random, $octaves){
$this->generatorCollection = array();
$this->octaves = (int) $octaves;
for($o = 0; $o < $this->octaves; ++$o){

View file

@ -9,7 +9,7 @@ class NoiseGeneratorPerlin extends NoiseGenerator{
public $xCoord, $yCoord, $zCoord;
private $permutations = [];
public function __construct(MersenneTwister $random){
public function __construct(MTRandom $random){
$this->xCoord = $random->nextFloat() * 256;
$this->yCoord = $random->nextFloat() * 256;
$this->zCoord = $random->nextFloat() * 256;

View file

@ -16,9 +16,9 @@ class BiomeSource
*/
public $temperatureNoises, $rainfallNoises, $detailNoises;
public function __construct(Level $level){
$this->temperatureNoise = new NoiseGeneratorOctaves(new MersenneTwister($level->getSeed() * 9871), 4);
$this->rainfallNoise = new NoiseGeneratorOctaves(new MersenneTwister($level->getSeed() * 39811), 4);
$this->detailNoise = new NoiseGeneratorOctaves(new MersenneTwister($level->getSeed() * 543321), 2);
$this->temperatureNoise = new NoiseGeneratorOctaves(new MTRandom($level->getSeed() * 9871), 4);
$this->rainfallNoise = new NoiseGeneratorOctaves(new MTRandom($level->getSeed() * 39811), 4);
$this->detailNoise = new NoiseGeneratorOctaves(new MTRandom($level->getSeed() * 543321), 2);
}
public function getBiome($x, $z){

View file

@ -51,7 +51,7 @@ class VanillaGenerator implements LevelGenerator
{
$this->level = $level;
$this->biomeSource = new BiomeSource($level);
$this->rand = new MersenneTwister($level->getSeed());
$this->rand = new MTRandom($level->getSeed());
$this->upperInterpolationNoise = new NoiseGeneratorOctaves($this->rand, 16);
$this->lowerInterpolationNoise = new NoiseGeneratorOctaves($this->rand, 16);
$this->interpolationNoise = new NoiseGeneratorOctaves($this->rand, 8);
@ -229,7 +229,7 @@ class VanillaGenerator implements LevelGenerator
$b = $biome->topBlock;
$b2 = $biome->fillerBlock;
for($blockY = 127; $blockY >= 0; --$blockY){
if($blockY <= $this->rand->nextIntBndn(5)){
if($blockY <= $this->rand->nextInt(5)){
$chunks[$blockY >> 4][($blockY & 0xf) + ($blockZ << 5) + ($blockX << 9)] = "\x07";
}else{
$b3 = ord($chunks[$blockY >> 4][($blockY & 0xf) + ($blockZ << 5) + ($blockX << 9)]);
@ -261,7 +261,7 @@ class VanillaGenerator implements LevelGenerator
--$i;
$chunks[$blockY >> 4][($blockY & 0xf) + ($blockZ << 5) + ($blockX << 9)] = chr($b2);
if($i == 0 && $b2 == SAND){
$i = $this->rand->nextIntBndn(4);
$i = $this->rand->nextInt(4);
$b2 = SANDSTONE;
}
}

View file

@ -76,7 +76,7 @@ class Biome
$this->name = $name;
}
public function getTreeFeature(MersenneTwister $rand){
public function getTreeFeature(MTRandom $rand){
$rand->nextInt(); //it is necessary
return Feature::$TREE;
}

View file

@ -2,7 +2,7 @@
class ForestBiome extends Biome
{
public function getTreeFeature(MersenneTwister $rand){
public function getTreeFeature(MTRandom $rand){
if($rand->nextInt(5) == 0){
return Feature::$BIRCH_TREE;
}

View file

@ -2,7 +2,7 @@
class TaigaBiome extends Biome
{
public function getTreeFeature(MersenneTwister $rand){
public function getTreeFeature(MTRandom $rand){
if($rand->nextInt(3) == 0){
return Feature::$PINE_TREE;
}else{

View file

@ -2,7 +2,7 @@
class BirchFeature extends Feature
{
public function place(Level $level, MersenneTwister $rand, $x, $y, $z){
public function place(Level $level, MTRandom $rand, $x, $y, $z){
$nextInt = $rand->nextInt(3) + 5;
$z2 = true;
if($y < 1 || $y + $nextInt + 1 > 128){

View file

@ -1,7 +1,7 @@
<?php
class ClayFeature
{
public static function place(Level $level, MersenneTwister $rand, $x, $y, $z){
public static function place(Level $level, MTRandom $rand, $x, $y, $z){
$size = 32;
$clayid = CLAY_BLOCK;
$id = $level->level->getBlockID($x, $y, $z);

View file

@ -15,7 +15,7 @@ abstract class Feature
Feature::$MUSHROOM_RED = new FlowerFeature(RED_MUSHROOM);
}
public function place(Level $level, MersenneTwister $rand, $x, $y, $z){ //TODO pass VanillaGenerator instance and update heightmap on ceratin block placement
public function place(Level $level, MTRandom $rand, $x, $y, $z){ //TODO pass VanillaGenerator instance and update heightmap on ceratin block placement
}
}

View file

@ -7,7 +7,7 @@ class FlowerFeature extends Feature
$this->id = $id;
}
public function place(Level $level, MersenneTwister $rand, $x, $y, $z){ //TODO pass VanillaGenerator instance and update heightmap on ceratin block placement
public function place(Level $level, MTRandom $rand, $x, $y, $z){ //TODO pass VanillaGenerator instance and update heightmap on ceratin block placement
for($i = 0; $i < 64; ++$i){
$xPos = ($x + $rand->nextInt(8)) - $rand->nextInt(8);
$yPos = ($y + $rand->nextInt(4)) - $rand->nextInt(4);

View file

@ -2,7 +2,7 @@
class OreFeature
{
public static function place(Level $level, MersenneTwister $rand, $x, $y, $z, $id, $amount){
public static function place(Level $level, MTRandom $rand, $x, $y, $z, $id, $amount){
$nextFloat = $rand->nextFloat() * 3.1415927;
$sin = ($x + 8 + ((sin($nextFloat) * $amount) / 8));

View file

@ -2,7 +2,7 @@
class PineFeature extends Feature
{
public function place(Level $level, MersenneTwister $rand, $x, $y, $z){
public function place(Level $level, MTRandom $rand, $x, $y, $z){
$l = $rand->nextInt(5) + 7;
$i1 = $l - $rand->nextInt(2) - 3;
$j1 = $l - $i1;

View file

@ -1,7 +1,7 @@
<?php
class SpruceFeature extends Feature
{
public function place(Level $level, MersenneTwister $rand, $x, $y, $z){
public function place(Level $level, MTRandom $rand, $x, $y, $z){
$nextInt = $rand->nextInt(4) + 6;
$nextInt2 = 1 + $rand->nextInt(2);
$i2 = $nextInt - $nextInt2;

View file

@ -1,7 +1,7 @@
<?php
class TreeFeature extends Feature
{
public function place(Level $level, MersenneTwister $rand, $x, $y, $z){
public function place(Level $level, MTRandom $rand, $x, $y, $z){
$nextInt = $rand->nextInt(3) + 4;
$z2 = true;
if($y < 1 || $y + $nextInt + 1 > 128) return false;