Skip to content

Commit

Permalink
Change the internal seed calculation algorithm.
Browse files Browse the repository at this point in the history
  • Loading branch information
A1essandro committed Jan 9, 2017
1 parent 1347de7 commit 69626d6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
19 changes: 11 additions & 8 deletions src/DiamondAndSquare.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,9 @@ class DiamondAndSquare
const PERSISTENCE = 'persistence';
const MAP_SEED = 'map_seed';

/**
* @var number
*/
private $floatSeed = 0;

public function __construct()
{
$this->mapSeed = microtime(true);
$this->setMapSeed(microtime(true));
}

/**
Expand Down Expand Up @@ -116,7 +111,6 @@ public function setMapSeed($seed)
}

$this->mapSeed = $seed;
$this->floatSeed = is_numeric($seed) ? $seed : intval(substr(md5($seed), -8), 16);
}

/**
Expand Down Expand Up @@ -165,7 +159,7 @@ public function generate(array $options = array())
$this->terra[$x] = new SplFixedArray($this->size);
}

mt_srand($this->floatSeed * $this->size);
mt_srand($this->getInternalMapSeed());

$last = $this->size - 1;
$this->terra[0][0] = $this->getOffset($this->size);
Expand Down Expand Up @@ -276,4 +270,13 @@ private function getCellHeight($x, $y, $stepSize = 0)
}
}

/**
* Return internal seed, depend on the size
* @return int
*/
private function getInternalMapSeed()
{
return intval(substr(md5($this->mapSeed . $this->size), -7), 16);
}

}
10 changes: 5 additions & 5 deletions test/DiamondAndSquareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public function testSetSizeNotInt($sizeToSet)
*/
public function testSetInvalidMapHash($mapHash)
{
$this->diamondSquare->setMapHash($mapHash);
$this->diamondSquare->setMapSeed($mapHash);
}

public function testHashEquals()
Expand All @@ -102,12 +102,12 @@ public function testHashEquals()

//same hashes
$mapHash = uniqid();
$this->diamondSquare->setMapHash($mapHash);
$this->diamondSquare->setMapSeed($mapHash);
$map1 = $this->diamondSquare->generate();
$map2 = $this->diamondSquare->generate();

$this->assertEquals(self::expandMap($map1), self::expandMap($map2));
$this->assertEquals($mapHash, $this->diamondSquare->getMapHash());
$this->assertEquals($mapHash, $this->diamondSquare->getMapSeed());
}

public function testDifferentHashes()
Expand All @@ -118,9 +118,9 @@ public function testDifferentHashes()
$this->diamondSquare->setSize(3);
$this->diamondSquare->setPersistence(100);

$this->diamondSquare->setMapHash($mapHash1);
$this->diamondSquare->setMapSeed($mapHash1);
$map1 = $this->diamondSquare->generate();
$this->diamondSquare->setMapHash($mapHash2);
$this->diamondSquare->setMapSeed($mapHash2);
$map2 = $this->diamondSquare->generate();

$this->assertNotEquals(self::expandMap($map1), self::expandMap($map2));
Expand Down

0 comments on commit 69626d6

Please sign in to comment.