Faster level generator

This commit is contained in:
Shoghi Cervantes 2013-09-09 03:32:24 +02:00
parent 4ccaccc126
commit b3c51c6d2e
11 changed files with 71 additions and 63 deletions

View file

@ -32,13 +32,13 @@ class OreObject{
return $this->type;
}
public function canPlaceObject(Level $level, Vector3 $pos){
return ($level->getBlock($pos)->getID() !== AIR);
public function canPlaceObject(Level $level, $x, $y, $z){
return ($level->level->getBlockID($x, $y, $z) != AIR);
}
public function placeObject(Level $level, Vector3 $pos){
$clusterSize = (int) $this->type->clusterSize;
$angle = $this->random->nextFloat() * pi();
$angle = $this->random->nextFloat() * M_PI;
$offset = VectorMath::getDirection2D($angle)->multiply($clusterSize)->divide(8);
$x1 = $pos->x + 8 + $offset->x;
$x2 = $pos->x + 8 - $offset->x;
@ -50,7 +50,7 @@ class OreObject{
$seedX = $x1 + ($x2 - $x1) * $count / $clusterSize;
$seedY = $y1 + ($y2 - $y1) * $count / $clusterSize;
$seedZ = $z1 + ($z2 - $z1) * $count / $clusterSize;
$size = ((sin($count * (pi() / $clusterSize)) + 1) * $this->random->nextFloat() * $clusterSize / 16 + 1) / 2;
$size = ((sin($count * (M_PI / $clusterSize)) + 1) * $this->random->nextFloat() * $clusterSize / 16 + 1) / 2;
$startX = (int) ($seedX - $size);
$startY = (int) ($seedY - $size);
@ -67,9 +67,8 @@ class OreObject{
if($y > 0 and ($sizeX + $sizeY) < 1){
for($z = $startZ; $z <= $endZ; ++$z){
$sizeZ = pow(($z + 0.5 - $seedZ) / $size, 2);
$v = new Vector3($x, $y, $z);
if(($sizeX + $sizeY + $sizeZ) < 1 and $level->getBlock($v)->getID() === STONE){
$level->setBlockRaw($v, $this->type->material);
if(($sizeX + $sizeY + $sizeZ) < 1 and $level->level->getBlockID($x, $y, $z) === STONE){
$level->setBlockRaw(new Vector3($x, $y, $z), $this->type->material);
}
}
}

View file

@ -23,23 +23,21 @@
class TallGrassObject{
public static function growGrass(Level $level, Vector3 $pos, Random $random, $count = 15){
$arr = array(
array(DANDELION, 0),
array(CYAN_FLOWER, 0),
array(TALL_GRASS, 1),
array(TALL_GRASS, 1),
array(TALL_GRASS, 1),
array(TALL_GRASS, 1),
array(AIR, 0),
BlockAPI::get(DANDELION, 0),
BlockAPI::get(CYAN_FLOWER, 0),
BlockAPI::get(TALL_GRASS, 1),
BlockAPI::get(TALL_GRASS, 1),
BlockAPI::get(TALL_GRASS, 1),
BlockAPI::get(TALL_GRASS, 1)
);
$radius = ceil(sqrt($count) / 2);
$radius = 10;
$arrC = count($arr) - 1;
for($c = 0; $c < $count; ++$c){
$x = $random->nextRange($pos->x - $radius, $pos->x + $radius);
$z = $random->nextRange($pos->z - $radius, $pos->z + $radius);
$b = $level->getBlock(new Vector3($x, $pos->y + 1, $z));
$d = $level->getBlock(new Vector3($x, $pos->y, $z));
if($b->getID() === AIR and $d->getID() === GRASS){
$t = $arr[$random->nextRange(0, count($arr) - 1)];
$level->setBlockRaw($b, BlockAPI::get($t[0], $t[1]));
if($level->level->getBlockID($x, $pos->y + 1, $z) === AIR and $level->level->getBlockID($x, $pos->y, $z) === GRASS){
$t = $arr[$random->nextRange(0, $arrC)];
$level->setBlockRaw(new Vector3($x, $pos->y + 1, $z), $t);
}
}
}

View file

@ -38,8 +38,7 @@ class PineTreeObject extends TreeObject{
}
for($xx = -$checkRadius; $xx < ($checkRadius + 1); ++$xx){
for($zz = -$checkRadius; $zz < ($checkRadius + 1); ++$zz){
$block = $level->getBlock(new Vector3($pos->x + $xx, $pos->y + $yy, $pos->z + $zz));
if(!isset($this->overridable[$block->getID()])){
if(!isset($this->overridable[$level->level->getBlockID($pos->x + $xx, $pos->y + $yy, $pos->z + $zz)])){
return false;
}
}

View file

@ -41,8 +41,7 @@ class SmallTreeObject extends TreeObject{
}
for($xx = -$radiusToCheck; $xx < ($radiusToCheck + 1); ++$xx){
for($zz = -$radiusToCheck; $zz < ($radiusToCheck + 1); ++$zz){
$block = $level->getBlock(new Vector3($pos->x + $xx, $pos->y + $yy, $pos->z + $zz));
if(!isset($this->overridable[$block->getID()])){
if(!isset($this->overridable[$level->level->getBlockID($pos->x + $xx, $pos->y + $yy, $pos->z + $zz)])){
return false;
}
}
@ -53,7 +52,7 @@ class SmallTreeObject extends TreeObject{
public function placeObject(Level $level, Vector3 $pos, Random $random){
// The base dirt block
$dirtpos = new Vector3( $pos->x, $pos->y -1, $pos->z );
$dirtpos = new Vector3( $pos->x, $pos->y - 1, $pos->z );
$level->setBlockRaw( $dirtpos, new DirtBlock() );
// Adjust the tree trunk's height randomly

View file

@ -38,8 +38,7 @@ class SpruceTreeObject extends TreeObject{
}
for($xx = -$checkRadius; $xx < ($checkRadius + 1); ++$xx){
for($zz = -$checkRadius; $zz < ($checkRadius + 1); ++$zz){
$block = $level->getBlock(new Vector3($pos->x + $xx, $pos->y + $yy, $pos->z + $zz));
if(!isset($this->overridable[$block->getID()])){
if(!isset($this->overridable[$level->level->getBlockID($pos->x + $xx, $pos->y + $yy, $pos->z + $zz)])){
return false;
}
}