diff --git a/src/utils/MTRandom.php b/src/utils/MTRandom.php new file mode 100644 index 00000000..5cc5d5c3 --- /dev/null +++ b/src/utils/MTRandom.php @@ -0,0 +1,56 @@ +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; + } +} + diff --git a/src/world/generator/noise/NoiseGeneratorOctaves.php b/src/world/generator/noise/NoiseGeneratorOctaves.php index 55fb2edb..6e6bb03c 100644 --- a/src/world/generator/noise/NoiseGeneratorOctaves.php +++ b/src/world/generator/noise/NoiseGeneratorOctaves.php @@ -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){ diff --git a/src/world/generator/noise/NoiseGeneratorPerlin.php b/src/world/generator/noise/NoiseGeneratorPerlin.php index c169a8b7..c7e469ee 100644 --- a/src/world/generator/noise/NoiseGeneratorPerlin.php +++ b/src/world/generator/noise/NoiseGeneratorPerlin.php @@ -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; diff --git a/src/world/generator/vanilla/BiomeSource.php b/src/world/generator/vanilla/BiomeSource.php index 38e4fe5a..821d7f00 100644 --- a/src/world/generator/vanilla/BiomeSource.php +++ b/src/world/generator/vanilla/BiomeSource.php @@ -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){ diff --git a/src/world/generator/vanilla/VanillaGenerator.php b/src/world/generator/vanilla/VanillaGenerator.php index cdc9a8b2..663ba8d4 100644 --- a/src/world/generator/vanilla/VanillaGenerator.php +++ b/src/world/generator/vanilla/VanillaGenerator.php @@ -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; } } diff --git a/src/world/generator/vanilla/biome/Biome.php b/src/world/generator/vanilla/biome/Biome.php index 223339d9..a3f38ac2 100644 --- a/src/world/generator/vanilla/biome/Biome.php +++ b/src/world/generator/vanilla/biome/Biome.php @@ -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; } diff --git a/src/world/generator/vanilla/biome/ForestBiome.php b/src/world/generator/vanilla/biome/ForestBiome.php index 3ab5fba3..f2e21d4d 100644 --- a/src/world/generator/vanilla/biome/ForestBiome.php +++ b/src/world/generator/vanilla/biome/ForestBiome.php @@ -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; } diff --git a/src/world/generator/vanilla/biome/TaigaBiome.php b/src/world/generator/vanilla/biome/TaigaBiome.php index 1552c69c..72115de7 100644 --- a/src/world/generator/vanilla/biome/TaigaBiome.php +++ b/src/world/generator/vanilla/biome/TaigaBiome.php @@ -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{ diff --git a/src/world/generator/vanilla/feature/BirchFeature.php b/src/world/generator/vanilla/feature/BirchFeature.php index 366d59b6..f98bdf9e 100644 --- a/src/world/generator/vanilla/feature/BirchFeature.php +++ b/src/world/generator/vanilla/feature/BirchFeature.php @@ -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){ diff --git a/src/world/generator/vanilla/feature/ClayFeature.php b/src/world/generator/vanilla/feature/ClayFeature.php index 97aa136d..d6a2d392 100644 --- a/src/world/generator/vanilla/feature/ClayFeature.php +++ b/src/world/generator/vanilla/feature/ClayFeature.php @@ -1,7 +1,7 @@ level->getBlockID($x, $y, $z); diff --git a/src/world/generator/vanilla/feature/Feature.php b/src/world/generator/vanilla/feature/Feature.php index ae0dee76..f52426ad 100644 --- a/src/world/generator/vanilla/feature/Feature.php +++ b/src/world/generator/vanilla/feature/Feature.php @@ -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 } } diff --git a/src/world/generator/vanilla/feature/FlowerFeature.php b/src/world/generator/vanilla/feature/FlowerFeature.php index 57c07760..2bdacb8f 100644 --- a/src/world/generator/vanilla/feature/FlowerFeature.php +++ b/src/world/generator/vanilla/feature/FlowerFeature.php @@ -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); diff --git a/src/world/generator/vanilla/feature/OreFeature.php b/src/world/generator/vanilla/feature/OreFeature.php index f107971d..e4aa83d4 100644 --- a/src/world/generator/vanilla/feature/OreFeature.php +++ b/src/world/generator/vanilla/feature/OreFeature.php @@ -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)); diff --git a/src/world/generator/vanilla/feature/PineFeature.php b/src/world/generator/vanilla/feature/PineFeature.php index 4cfd0c05..115473d9 100644 --- a/src/world/generator/vanilla/feature/PineFeature.php +++ b/src/world/generator/vanilla/feature/PineFeature.php @@ -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; diff --git a/src/world/generator/vanilla/feature/SpruceFeature.php b/src/world/generator/vanilla/feature/SpruceFeature.php index 67f21c10..c32c7d6f 100644 --- a/src/world/generator/vanilla/feature/SpruceFeature.php +++ b/src/world/generator/vanilla/feature/SpruceFeature.php @@ -1,7 +1,7 @@ nextInt(4) + 6; $nextInt2 = 1 + $rand->nextInt(2); $i2 = $nextInt - $nextInt2; diff --git a/src/world/generator/vanilla/feature/TreeFeature.php b/src/world/generator/vanilla/feature/TreeFeature.php index 68ec51ef..2b4f5a2a 100644 --- a/src/world/generator/vanilla/feature/TreeFeature.php +++ b/src/world/generator/vanilla/feature/TreeFeature.php @@ -1,7 +1,7 @@ nextInt(3) + 4; $z2 = true; if($y < 1 || $y + $nextInt + 1 > 128) return false;