diff --git a/src/entity/Entity.php b/src/entity/Entity.php index 506e4c46..99217779 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -76,6 +76,9 @@ class Entity extends Position public $check = true; public $width = 1; public $height = 1; + /** + * @var XorShift128Random + */ public $random; public $radius; public $inAction = false; @@ -113,7 +116,7 @@ class Entity extends Position function __construct(Level $level, $eid, $class, $type = 0, $data = []) { - $this->random = new Random(); + $this->random = new XorShift128Random(); $this->last = [&$this->lastX, &$this->lastY, &$this->lastZ, &$this->lastYaw, &$this->lastPitch, &$this->lastTime]; //pointers to variables $this->canBeAttacked = false; $this->hasKnockback = false; diff --git a/src/utils/XorShift128Random.php b/src/utils/XorShift128Random.php new file mode 100644 index 00000000..8c874065 --- /dev/null +++ b/src/utils/XorShift128Random.php @@ -0,0 +1,55 @@ +setSeed($seed === null ? microtime(true) : $seed); + } + + public function nextGaussian(){ + if($this->haveNextNextGaussian){ + $this->haveNextNextGaussian = false; + return $this->nextNextGaussian; + }else{ + 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; + } + } + + public function setSeed($seed){ + $this->x = 123456789 ^ $seed; + $this->y = 362436069 ^ ($seed << 17) | (($seed >> 15) & 0x7fffffff) & 0xffffffff; + $this->z = 521288629 ^ ($seed << 31) | (($seed >> 1 ) & 0x7fffffff) & 0xffffffff; + $this->w = 88675123 ^ ($seed << 18) | (($seed >> 14) & 0x7fffffff) & 0xffffffff; + } + + public function gen(){ + $t = ($this->x ^ ($this->x << 11)) & 0xffffffff; + $this->x = $this->y; + $this->y = $this->z; + $this->z = $this->w; + $this->w = ($this->w ^ (($this->w >> 19) & 0x7fffffff) ^ ($t ^ (($t >> 8) & 0x7fffffff))) & 0xffffffff; + + return $this->w << 32 >> 32; + } + + public function nextInt($bound = null){ + if($bound == null) return $this->gen(); + return (($this->gen() & 0x7fffffff) % $bound); + } + + public function nextFloat(){ + return (($this->gen() & 0x7fffffff) / 0x7fffffff); + } +}