mirror of
https://github.com/oldminecraftcommunity/NostalgiaCore.git
synced 2026-11-20 19:49:44 +00:00
Faster level generator
This commit is contained in:
parent
4ccaccc126
commit
b3c51c6d2e
11 changed files with 71 additions and 63 deletions
|
|
@ -22,38 +22,54 @@
|
|||
|
||||
//Unsecure, not used for "Real Randomness"
|
||||
class Random{
|
||||
private $random;
|
||||
private $x, $y, $z, $w;
|
||||
public function __construct($seed = false){
|
||||
$this->random = new twister(0);
|
||||
$this->setSeed($seed);
|
||||
}
|
||||
|
||||
public function setSeed($seed = false){
|
||||
$this->random->init_with_integer($seed !== false ? (int) $seed:Utils::readInt(Utils::getRandomBytes(4, false)));
|
||||
$seed = $seed !== false ? Utils::writeInt((int) $seed):Utils::getRandomBytes(4, false);
|
||||
$state = array();
|
||||
for($i = 0; $i < 256; ++$i){
|
||||
$state[] = $i;
|
||||
}
|
||||
for($i = $j = 0; $i < 256; ++$i){
|
||||
$j = ($j + ord($seed{$i & 0x03}) + $state[$i]) & 0xFF;
|
||||
$state[$i] ^= $state[$j];
|
||||
$state[$j] ^= $state[$i];
|
||||
$state[$i] ^= $state[$j];
|
||||
}
|
||||
$this->state = $state;
|
||||
$this->i = $this->j = 0;
|
||||
}
|
||||
|
||||
public function nextInt(){
|
||||
return $this->random->int32();
|
||||
return Utils::readInt($this->nextBytes(4)) & 0x7FFFFFFF;
|
||||
}
|
||||
|
||||
public function nextFloat(){
|
||||
return $this->random->real_closed();
|
||||
return $this->nextInt() / 0x7FFFFFFF;
|
||||
}
|
||||
|
||||
public function nextBytes($byteCount){
|
||||
$bytes = "";
|
||||
for($i = 0; $i < $byteCount; ++$i){
|
||||
$bytes .= chr($this->random->rangeint(0, 0xFF));
|
||||
$this->i = ($this->i + 1) & 0xFF;
|
||||
$this->j = ($this->j + $this->state[$this->i]) & 0xFF;
|
||||
$this->state[$this->i] ^= $this->state[$this->j];
|
||||
$this->state[$this->j] ^= $this->state[$this->i];
|
||||
$this->state[$this->i] ^= $this->state[$this->j];
|
||||
$bytes .= chr($this->state[($this->state[$this->i] + $this->state[$this->j]) & 0xFF]);
|
||||
}
|
||||
return $bytes;
|
||||
}
|
||||
|
||||
public function nextBoolean(){
|
||||
return $this->random->rangeint(0, 1) === 1;
|
||||
return ($this->nextBytes(1) & 0x01) == 0;
|
||||
}
|
||||
|
||||
public function nextRange($start = 0, $end = PHP_INT_MAX){
|
||||
return $this->random->rangeint($start, $end);
|
||||
return $start + ($this->nextInt() % ($end + 1 - $start));
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue