Skip to content

Commit

Permalink
Remove biome colours and fix biome id arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
dktapps committed Dec 4, 2016
1 parent 4d121f7 commit aafe0c4
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 140 deletions.
21 changes: 0 additions & 21 deletions src/pocketmine/level/Level.php
Expand Up @@ -2048,16 +2048,6 @@ public function getBiomeId(int $x, int $z) : int{
return $this->getChunk($x >> 4, $z >> 4, true)->getBiomeId($x & 0x0f, $z & 0x0f);
}

/**
* @param int $x
* @param int $z
*
* @return int[]
*/
public function getBiomeColor(int $x, int $z) : array{
return $this->getChunk($x >> 4, $z >> 4, true)->getBiomeColor($x & 0x0f, $z & 0x0f);
}

/**
* @param int $x
* @param int $z
Expand All @@ -2077,17 +2067,6 @@ public function setBiomeId(int $x, int $z, int $biomeId){
$this->getChunk($x >> 4, $z >> 4, true)->setBiomeId($x & 0x0f, $z & 0x0f, $biomeId);
}

/**
* @param int $x
* @param int $z
* @param int $R
* @param int $G
* @param int $B
*/
public function setBiomeColor(int $x, int $z, int $R, int $G, int $B){
$this->getChunk($x >> 4, $z >> 4, true)->setBiomeColor($x & 0x0f, $z & 0x0f, $R, $G, $B);
}

/**
* @param int $x
* @param int $z
Expand Down
22 changes: 0 additions & 22 deletions src/pocketmine/level/format/Chunk.php
Expand Up @@ -210,23 +210,6 @@ public function getBiomeId(int $x, int $z) : int;
*/
public function setBiomeId(int $x, int $z, int $biomeId);

/**
* @param int $x
* @param int $z
*
* @return int[] RGB bytes
*/
public function getBiomeColor(int $x, int $z) : int;

/**
* @param int $x 0-15
* @param int $z 0-15
* @param int $R 0-255
* @param int $G 0-255
* @param int $B 0-255
*/
public function setBiomeColor(int $x, int $z, int $R, int $G, int $B);

public function getBlockIdColumn(int $x, int $z) : string;

public function getBlockDataColumn(int $x, int $z) : string;
Expand Down Expand Up @@ -313,11 +296,6 @@ public function initChunk();
*/
public function getBiomeIdArray() : string;

/**
* @return int[]
*/
public function getBiomeColorArray() : array;

/**
* @return int[]
*/
Expand Down
25 changes: 17 additions & 8 deletions src/pocketmine/level/format/anvil/Anvil.php
Expand Up @@ -33,6 +33,7 @@
use pocketmine\tile\Spawnable;
use pocketmine\utils\BinaryStream;
use pocketmine\utils\ChunkException;
use pocketmine\utils\MainLogger;

class Anvil extends McRegion{

Expand Down Expand Up @@ -63,7 +64,7 @@ public static function nbtSerialize(GenericChunk $chunk) : string{
]);
}

//$nbt->BiomeColors = new IntArrayTag("BiomeColors", $chunk->getBiomeColorArray());
$nbt->Biomes = new ByteArrayTag("Biomes", $chunk->getBiomeIdArray());
$nbt->HeightMap = new IntArrayTag("HeightMap", $chunk->getHeightMapArray());

$entities = [];
Expand Down Expand Up @@ -124,22 +125,30 @@ public static function nbtDeserialize(string $data, LevelProvider $provider = nu
}
}

if(isset($chunk->BiomeColors)){
$biomeIds = GenericChunk::convertBiomeColours($chunk->BiomeColors->getValue()); //Convert back to PC format (RIP colours D:)
}elseif(isset($chunk->Biomes)){
$biomeIds = $chunk->Biomes->getValue();
}else{
$biomeIds = "";
}

$result = new GenericChunk(
$provider,
$chunk["xPos"],
$chunk["zPos"],
$subChunks,
$chunk->Entities instanceof ListTag ? $chunk->Entities->getValue() : [],
$chunk->TileEntities instanceof ListTag ? $chunk->TileEntities->getValue() : [],
/*$chunk->BiomeColors instanceof IntArrayTag ? $chunk->BiomeColors->getValue() : */ [], //TODO: remove this and revert to the original PC Biomes array
$chunk->HeightMap instanceof IntArrayTag ? $chunk->HeightMap->getValue() : []
$chunk->Entities->getValue(),
$chunk->TileEntities->getValue(),
$biomeIds,
$chunk->HeightMap->getValue()
);
$result->setLightPopulated($chunk->LightPopulated instanceof ByteTag ? ((bool) $chunk->LightPopulated->getValue()) : false);
$result->setPopulated($chunk->TerrainPopulated instanceof ByteTag ? ((bool) $chunk->TerrainPopulated->getValue()) : false);
$result->setLightPopulated(isset($chunk->LightPopulated) ? ((bool) $chunk->LightPopulated->getValue()) : false);
$result->setPopulated(isset($chunk->TerrainPopulated) ? ((bool) $chunk->TerrainPopulated->getValue()) : false);
$result->setGenerated(true);
return $result;
}catch(\Throwable $e){
echo $e->getMessage();
MainLogger::getLogger()->logException($e);
return null;
}
}
Expand Down
58 changes: 24 additions & 34 deletions src/pocketmine/level/format/generic/GenericChunk.php
Expand Up @@ -67,8 +67,8 @@ class GenericChunk implements Chunk{
/** @var int[256] */
protected $heightMap = [];

/** @var int[256] */
protected $biomeColors = [];
/** @var string */
protected $biomeIds;

protected $extraData = [];

Expand All @@ -85,10 +85,10 @@ class GenericChunk implements Chunk{
* @param SubChunk[] $subChunks
* @param CompoundTag[] $entities
* @param CompoundTag[] $tiles
* @param int[256] $biomeColors
* @param string $biomeIds
* @param int[256] $heightMap
*/
public function __construct($provider, int $chunkX, int $chunkZ, array $subChunks = [], array $entities = [], array $tiles = [], array $biomeColors = [], array $heightMap = []){
public function __construct($provider, int $chunkX, int $chunkZ, array $subChunks = [], array $entities = [], array $tiles = [], string $biomeIds = "", array $heightMap = []){
$this->provider = $provider;
$this->x = $chunkX;
$this->z = $chunkZ;
Expand Down Expand Up @@ -120,11 +120,11 @@ public function __construct($provider, int $chunkX, int $chunkZ, array $subChunk
$this->heightMap = array_fill(0, 256, 0);
}

if(count($biomeColors) === 256){
$this->biomeColors = $biomeColors;
if(strlen($biomeIds) === 256){
$this->biomeIds = $biomeIds;
}else{
assert(count($biomeColors) === 0, "Wrong HeightMap value count, expected 256, got " . count($biomeColors));
$this->biomeColors = array_fill(0, 256, 0);
assert(strlen($biomeIds) === 0, "Wrong BiomeIds value count, expected 256, got " . strlen($biomeIds));
$this->biomeIds = str_repeat("\x00", 256);
}

$this->NBTtiles = $tiles;
Expand Down Expand Up @@ -293,23 +293,12 @@ public function populateSkyLight(){
}

public function getBiomeId(int $x, int $z) : int{
return ($this->biomeColors[($z << 4) | $x] & 0xFF000000) >> 24;
return ord($this->biomeIds{($z << 4) | $x});
}

public function setBiomeId(int $x, int $z, int $biomeId){
$this->hasChanged = true;
$this->biomeColors[($z << 4) | $x] = ($this->biomeColors[($z << 4) | $x] & 0xFFFFFF) | ($biomeId << 24);
}

public function getBiomeColor(int $x, int $z) : int{
$color = $this->biomeColors[($z << 4) | $x] & 0xFFFFFF;

return [$color >> 16, ($color >> 8) & 0xFF, $color & 0xFF];
}

public function setBiomeColor(int $x, int $z, int $R, int $G, int $B){
$this->hasChanged = true;
$this->biomeColors[($z << 4) | $x] = ($this->biomeColors[($z << 4) | $x] & 0xFF000000) | (($R & 0xFF) << 16) | (($G & 0xFF) << 8) | ($B & 0xFF);
$this->biomeIds{($z << 4) | $x} = chr($biomeId & 0xff);
}

public function getBlockIdColumn(int $x, int $z) : string{
Expand Down Expand Up @@ -511,15 +500,7 @@ public function initChunk(){
}

public function getBiomeIdArray() : string{
$ids = str_repeat("\x00", 256);
foreach($this->biomeColors as $i => $d){
$ids{$i} = chr(($d & 0xFF000000) >> 24);
}
return $ids;
}

public function getBiomeColorArray() : array{
return $this->biomeColors;
return $this->biomeIds;
}

public function getHeightMapArray() : array{
Expand Down Expand Up @@ -635,7 +616,8 @@ public function networkSerialize() : string{
for($y = 0; $y < $subChunkCount; ++$y){
$result .= $this->subChunks[$y]->networkSerialize();
}

$result .= pack("v*", ...$this->heightMap)
. $this->biomeIds;
//TODO: heightmaps, tile data
return $result;
}
Expand All @@ -656,7 +638,7 @@ public static function fastSerialize(Chunk $chunk) : string{
$stream->putByte($count);
$stream->put($subChunks);
$stream->put(pack("C*", ...$chunk->getHeightMapArray()) .
pack("N*", ...$chunk->getBiomeColorArray()) .
$chunk->getBiomeIdArray() .
chr(($chunk->lightPopulated ? 1 << 2 : 0) | ($chunk->terrainPopulated ? 1 << 1 : 0) | ($chunk->terrainGenerated ? 1 : 0)));
//TODO: tiles and entities
return $stream->getBuffer();
Expand All @@ -680,9 +662,9 @@ public static function fastDeserialize(string $data, LevelProvider $provider = n
);
}
$heightMap = array_values(unpack("C*", $stream->get(256)));
$biomeColors = array_values(unpack("N*", $stream->get(1024))); //TODO: remove this
$biomeIds = $stream->get(256);

$chunk = new GenericChunk($provider, $x, $z, $subChunks, $heightMap, $biomeColors);
$chunk = new GenericChunk($provider, $x, $z, $subChunks, [], [], $biomeIds, $heightMap);
$flags = $stream->getByte();
$chunk->lightPopulated = (bool) ($flags & 4);
$chunk->terrainPopulated = (bool) ($flags & 2);
Expand Down Expand Up @@ -745,4 +727,12 @@ public static final function reorderNibbleArray(string $array) : string{
return $result;
}

public static function convertBiomeColours(array $array) : string{
$result = str_repeat("\x00", 256);
foreach($array as $i => $colour){
$result{$i} = chr(($array[$i] >> 24) & 0xff);
}
return $result;
}

}
25 changes: 17 additions & 8 deletions src/pocketmine/level/format/mcregion/McRegion.php
Expand Up @@ -32,6 +32,7 @@
use pocketmine\nbt\tag\{ByteArrayTag, ByteTag, CompoundTag, IntArrayTag, IntTag, ListTag, LongTag, StringTag};
use pocketmine\Player;
use pocketmine\utils\ChunkException;
use pocketmine\utils\MainLogger;

class McRegion extends BaseLevelProvider{

Expand Down Expand Up @@ -67,7 +68,7 @@ public static function nbtSerialize(GenericChunk $chunk) : string{
$nbt->SkyLight = new ByteArrayTag("SkyLight", $skyLight);
$nbt->BlockLight = new ByteArrayTag("BlockLight", $blockLight);

//$nbt->BiomeColors = new IntArrayTag("BiomeColors", $chunk->getBiomeColorArray());
$nbt->Biomes = new ByteArrayTag("Biomes", $chunk->getBiomeIdArray());
$nbt->HeightMap = new IntArrayTag("HeightMap", $chunk->getHeightMapArray());

$entities = [];
Expand Down Expand Up @@ -146,23 +147,31 @@ public static function nbtDeserialize(string $data, LevelProvider $provider = nu
}
$subChunks[] = new SubChunk($y, $ids, $data, $blockLight, $skyLight);
}

if(isset($chunk->BiomeColors)){
$biomeIds = GenericChunk::convertBiomeColours($chunk->BiomeColors->getValue()); //Convert back to PC format (RIP colours D:)
}elseif(isset($chunk->Biomes)){
$biomeIds = $chunk->Biomes->getValue();
}else{
$biomeIds = "";
}

$result = new GenericChunk(
$provider,
$chunk["xPos"],
$chunk["zPos"],
$subChunks,
$chunk->Entities instanceof ListTag ? $chunk->Entities->getValue() : [],
$chunk->TileEntities instanceof ListTag ? $chunk->TileEntities->getValue() : [],
/*$chunk->BiomeColors instanceof IntArrayTag ? $chunk->BiomeColors->getValue() :*/ [],
$chunk->HeightMap instanceof IntArrayTag ? $chunk->HeightMap->getValue() : []
$chunk->Entities->getValue(),
$chunk->TileEntities->getValue(),
$biomeIds,
$chunk->HeightMap->getValue()
);
$result->setLightPopulated($chunk->LightPopulated instanceof ByteTag ? ((bool) $chunk->LightPopulated->getValue()) : false);
$result->setPopulated($chunk->TerrainPopulated instanceof ByteTag ? ((bool) $chunk->TerrainPopulated->getValue()) : false);
$result->setLightPopulated(isset($chunk->LightPopulated) ? ((bool) $chunk->LightPopulated->getValue()) : false);
$result->setPopulated(isset($chunk->TerrainPopulated) ? ((bool) $chunk->TerrainPopulated->getValue()) : false);
$result->setGenerated(true);
return $result;
}catch(\Throwable $e){
echo $e->getMessage();
MainLogger::getLogger()->logException($e);
return null;
}
}
Expand Down
5 changes: 0 additions & 5 deletions src/pocketmine/level/generator/Flat.php
Expand Up @@ -111,15 +111,10 @@ protected function parsePreset($preset, $chunkX, $chunkZ){

$this->chunk = clone $this->level->getChunk($chunkX, $chunkZ);
$this->chunk->setGenerated();
$c = Biome::getBiome($biome)->getColor();
$R = $c >> 16;
$G = ($c >> 8) & 0xff;
$B = $c & 0xff;

for($Z = 0; $Z < 16; ++$Z){
for($X = 0; $X < 16; ++$X){
$this->chunk->setBiomeId($X, $Z, $biome);
$this->chunk->setBiomeColor($X, $Z, $R, $G, $B);
for($y = 0; $y < 128; ++$y){
$this->chunk->setBlock($X, $y, $Z, ...$this->structure[$y]);
}
Expand Down
28 changes: 0 additions & 28 deletions src/pocketmine/level/generator/biome/Biome.php
Expand Up @@ -75,12 +75,10 @@ abstract class Biome{

protected $rainfall = 0.5;
protected $temperature = 0.5;
protected $grassColor = 0;

protected static function register($id, Biome $biome){
self::$biomes[(int) $id] = $biome;
$biome->setId((int) $id);
$biome->grassColor = self::generateBiomeColor($biome->getTemperature(), $biome->getRainfall());
}

public static function init(){
Expand Down Expand Up @@ -175,30 +173,4 @@ public function getTemperature(){
public function getRainfall(){
return $this->rainfall;
}

private static function generateBiomeColor($temperature, $rainfall){
$x = (1 - $temperature) * 255;
$z = (1 - $rainfall * $temperature) * 255;
$c = self::interpolateColor(256, $x, $z, [0x47, 0xd0, 0x33], [0x6c, 0xb4, 0x93], [0xbf, 0xb6, 0x55], [0x80, 0xb4, 0x97]);
return ((int) ($c[0] << 16)) | (int) (($c[1] << 8)) | (int) ($c[2]);
}


private static function interpolateColor($size, $x, $z, $c1, $c2, $c3, $c4){
$l1 = self::lerpColor($c1, $c2, $x / $size);
$l2 = self::lerpColor($c3, $c4, $x / $size);

return self::lerpColor($l1, $l2, $z / $size);
}

private static function lerpColor($a, $b, $s){
$invs = 1 - $s;
return [$a[0] * $invs + $b[0] * $s, $a[1] * $invs + $b[1] * $s, $a[2] * $invs + $b[2] * $s];
}


/**
* @return int (Red|Green|Blue)
*/
abstract public function getColor();
}
7 changes: 0 additions & 7 deletions src/pocketmine/level/generator/hell/Nether.php
Expand Up @@ -120,13 +120,6 @@ public function generateChunk($chunkX, $chunkZ){

$biome = Biome::getBiome(Biome::HELL);
$chunk->setBiomeId($x, $z, $biome->getId());
$color = [0, 0, 0];
$bColor = $biome->getColor();
$color[0] += (($bColor >> 16) ** 2);
$color[1] += ((($bColor >> 8) & 0xff) ** 2);
$color[2] += (($bColor & 0xff) ** 2);

$chunk->setBiomeColor($x, $z, $color[0], $color[1], $color[2]);

for($y = 0; $y < 128; ++$y){
if($y === 0 or $y === 127){
Expand Down

0 comments on commit aafe0c4

Please sign in to comment.