mirror of
https://github.com/oldminecraftcommunity/NostalgiaCore.git
synced 2026-11-21 12:09:43 +00:00
Updated to 1.4dev228 generation
This commit is contained in:
parent
ecb23df0e0
commit
809195b688
35 changed files with 4398 additions and 467 deletions
|
|
@ -94,7 +94,7 @@ class LevelAPI{
|
|||
if(strtoupper($this->server->api->getProperty("level-type")) == "FLAT"){
|
||||
$generator = new SuperflatGenerator($options);
|
||||
}else{
|
||||
$generator = new TemporalGenerator($options);
|
||||
$generator = new NormalGenerator($options);
|
||||
}
|
||||
}
|
||||
$gen = new WorldGenerator($generator, $name, $seed === false ? Utils::readInt(Utils::getRandomBytes(4, false)):(int) $seed);
|
||||
|
|
@ -227,20 +227,6 @@ class LevelAPI{
|
|||
return $this->server->spawn;
|
||||
}
|
||||
|
||||
public function loadMap(){
|
||||
if($this->mapName !== false and trim($this->mapName) !== ""){
|
||||
if(!file_exists($this->mapDir."level.pmf")){
|
||||
$level = new LevelImport($this->mapDir);
|
||||
$level->import();
|
||||
}
|
||||
$this->level = new PMFLevel($this->mapDir."level.pmf");
|
||||
console("[INFO] Preparing level \"".$this->level->getData("name")."\"");
|
||||
$this->time = (int) $this->level->getData("time");
|
||||
$this->seed = (int) $this->level->getData("seed");
|
||||
$this->spawn = $this->level->getSpawn();
|
||||
}
|
||||
}
|
||||
|
||||
public function getAll(){
|
||||
return $this->levels;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -63,6 +63,7 @@ class Player{
|
|||
public $blocked = true;
|
||||
public $achievements = array();
|
||||
public $chunksLoaded = array();
|
||||
private $viewDistance;
|
||||
private $chunksOrder = array();
|
||||
private $lastMeasure = 0;
|
||||
private $bandwidthRaw = 0;
|
||||
|
|
@ -112,6 +113,7 @@ class Player{
|
|||
$this->armor = array();
|
||||
$this->gamemode = $this->server->gamemode;
|
||||
$this->level = $this->server->api->level->getDefault();
|
||||
$this->viewDistance = (int) $this->server->api->getProperty("view-distance");
|
||||
$this->slot = 0;
|
||||
$this->hotbar = array(0, -1, -1, -1, -1, -1, -1, -1, -1);
|
||||
$this->packetStats = array(0,0);
|
||||
|
|
@ -148,22 +150,35 @@ class Player{
|
|||
if(!($this->entity instanceof Entity) or $this->connected === false){
|
||||
return false;
|
||||
}
|
||||
$X = $this->entity->x >> 4;
|
||||
$Z = $this->entity->z >> 4;
|
||||
$v = new Vector2($X, $Z);
|
||||
$this->chunksOrder = array();
|
||||
for($x = 0; $x < 16; ++$x){
|
||||
for($z = 0; $z < 16; ++$z){
|
||||
$dist = $v->distance(new Vector2($x, $z));
|
||||
for($y = 0; $y < 8; ++$y){
|
||||
$d = $x.":".$y.":".$z;
|
||||
if(!isset($this->chunksLoaded[$d])){
|
||||
$this->chunksOrder[$d] = $dist;
|
||||
|
||||
$newOrder = array();
|
||||
$lastLoaded = $this->chunksLoaded;
|
||||
$centerX = intval($this->entity->x >> 4);
|
||||
$centerZ = intval($this->entity->z >> 4);
|
||||
$startX = $centerX - $this->viewDistance;
|
||||
$startZ = $centerZ - $this->viewDistance;
|
||||
$finalX = $centerX + $this->viewDistance;
|
||||
$finalZ = $centerZ + $this->viewDistance;
|
||||
for($X = $startX; $X <= $finalX; ++$X){
|
||||
for($Z = $startZ; $Z <= $finalZ; ++$Z){
|
||||
$distance = abs($X - $centerX) + abs($Z - $centerZ);
|
||||
for($Y = 0; $Y < 8; ++$Y){
|
||||
$index = "$X:$Y:$Z";
|
||||
if(!isset($lastLoaded[$index])){
|
||||
$newOrder[$index] = $distance;
|
||||
}else{
|
||||
unset($lastLoaded[$index]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
asort($this->chunksOrder);
|
||||
asort($newOrder);
|
||||
$this->chunksOrder = $newOrder;
|
||||
foreach($lastLoaded as $index => $distance){
|
||||
$id = explode(":", $index);
|
||||
$this->level->freeChunk($id[0], $id[2], $this);
|
||||
unset($this->chunksLoaded[$index]);
|
||||
}
|
||||
}
|
||||
|
||||
public function getNextChunk(){
|
||||
|
|
@ -195,7 +210,7 @@ class Player{
|
|||
|
||||
$c = key($this->chunksOrder);
|
||||
$d = @$this->chunksOrder[$c];
|
||||
if($c === null or $d === null){
|
||||
if($c === null){
|
||||
$this->server->schedule(40, array($this, "getNextChunk"));
|
||||
return false;
|
||||
}
|
||||
|
|
@ -305,7 +320,7 @@ class Player{
|
|||
if($msg === true and $this->username != "" and $this->spawned !== false){
|
||||
$this->server->api->chat->broadcast($this->username." left the game");
|
||||
}
|
||||
$this->spawned = false;
|
||||
$this->spawned = false;
|
||||
console("[INFO] ".FORMAT_AQUA.$this->username.FORMAT_RESET."[/".$this->ip.":".$this->port."] logged out due to ".$reason);
|
||||
$this->windows = array();
|
||||
$this->armor = array();
|
||||
|
|
@ -1465,7 +1480,7 @@ class Player{
|
|||
|
||||
$this->sendInventory();
|
||||
$this->sendSettings();
|
||||
$this->server->schedule(50, array($this, "orderChunks"), array(), true);
|
||||
$this->server->schedule(30, array($this, "orderChunks"), array(), true);
|
||||
$this->blocked = false;
|
||||
|
||||
$pk = new SetTimePacket;
|
||||
|
|
@ -1957,9 +1972,6 @@ class Player{
|
|||
CARROT => 4,
|
||||
POTATO => 1,
|
||||
BAKED_POTATO => 6,
|
||||
//COOKIE => 2,
|
||||
//COOKED_FISH => 5,
|
||||
//RAW_FISH => 2,
|
||||
);
|
||||
$slot = $this->getSlot($this->slot);
|
||||
if($this->entity->getHealth() < 20 and isset($items[$slot->getID()])){
|
||||
|
|
|
|||
2434
src/Player.php.bak
Normal file
2434
src/Player.php.bak
Normal file
File diff suppressed because it is too large
Load diff
0
src/build/compile.sh
Executable file → Normal file
0
src/build/compile.sh
Executable file → Normal file
|
|
@ -32,7 +32,7 @@ define("SPECTATOR", 3);
|
|||
|
||||
|
||||
//Players
|
||||
define("MAX_CHUNK_RATE", 20 / arg("max-chunks-per-second", 4)); //Default rate ~256 kB/s
|
||||
define("MAX_CHUNK_RATE", 20 / arg("max-chunks-per-second", 16)); //Default rate ~256 kB/s
|
||||
define("PLAYER_MAX_QUEUE", 1024);
|
||||
|
||||
define("PLAYER_SURVIVAL_SLOTS", 36);
|
||||
|
|
|
|||
85
src/constants/GeneralConstants.php.bak
Normal file
85
src/constants/GeneralConstants.php.bak
Normal file
|
|
@ -0,0 +1,85 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
*
|
||||
* ____ _ _ __ __ _ __ __ ____
|
||||
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
|
||||
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
|
||||
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
|
||||
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* @author PocketMine Team
|
||||
* @link http://www.pocketmine.net/
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
define("PMF_LEVEL_DEFLATE_LEVEL", 6);
|
||||
|
||||
//Gamemodes
|
||||
define("SURVIVAL", 0);
|
||||
define("CREATIVE", 1);
|
||||
define("ADVENTURE", 2);
|
||||
define("VIEW", 3);
|
||||
define("VIEWER", 3);
|
||||
define("SPECTATOR", 3);
|
||||
|
||||
|
||||
//Players
|
||||
define("MAX_CHUNK_RATE", 60 / arg("max-chunks-per-second", 16)); //Default rate ~256 kB/s
|
||||
define("PLAYER_MAX_QUEUE", 1024);
|
||||
|
||||
define("PLAYER_SURVIVAL_SLOTS", 36);
|
||||
define("PLAYER_CREATIVE_SLOTS", 112);
|
||||
|
||||
|
||||
//Block Updates
|
||||
define("BLOCK_UPDATE_NORMAL", 1);
|
||||
define("BLOCK_UPDATE_RANDOM", 2);
|
||||
define("BLOCK_UPDATE_SCHEDULED", 3);
|
||||
define("BLOCK_UPDATE_WEAK", 4);
|
||||
define("BLOCK_UPDATE_TOUCH", 5);
|
||||
|
||||
|
||||
//Entities
|
||||
define("ENTITY_PLAYER", 1);
|
||||
|
||||
define("ENTITY_MOB", 2);
|
||||
define("MOB_CHICKEN", 10);
|
||||
define("MOB_COW", 11);
|
||||
define("MOB_PIG", 12);
|
||||
define("MOB_SHEEP", 13);
|
||||
|
||||
define("MOB_ZOMBIE", 32);
|
||||
define("MOB_CREEPER", 33);
|
||||
define("MOB_SKELETON", 34);
|
||||
define("MOB_SPIDER", 35);
|
||||
define("MOB_PIGMAN", 36);
|
||||
|
||||
define("ENTITY_OBJECT", 3);
|
||||
define("OBJECT_TRIPOD_CAMERA", 62);
|
||||
define("OBJECT_PRIMEDTNT", 65);
|
||||
define("OBJECT_ARROW", 80);
|
||||
define("OBJECT_SNOWBALL", 81);
|
||||
define("OBJECT_EGG", 82);
|
||||
define("OBJECT_PAINTING", 83);
|
||||
define("OBJECT_MINECART", 84);
|
||||
|
||||
define("ENTITY_ITEM", 4);
|
||||
|
||||
define("ENTITY_FALLING", 5);
|
||||
define("FALLING_SAND", 66);
|
||||
|
||||
|
||||
//TileEntities
|
||||
define("TILE_SIGN", "Sign");
|
||||
define("TILE_CHEST", "Chest");
|
||||
define("CHEST_SLOTS", 27);
|
||||
define("TILE_FURNACE", "Furnace");
|
||||
define("FURNACE_SLOTS", 3);
|
||||
|
|
@ -31,8 +31,14 @@ class StonecutterBlock extends SolidBlock{
|
|||
}
|
||||
|
||||
public function getDrops(Item $item, Player $player){
|
||||
return array(
|
||||
array($this->id, 0, 1),
|
||||
);
|
||||
if($item->isPickaxe()){
|
||||
return array(
|
||||
array($this->id, 0, 1),
|
||||
);
|
||||
} else {
|
||||
return array(
|
||||
array($this->id, 0, 0),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -19,16 +19,17 @@
|
|||
*
|
||||
*/
|
||||
|
||||
define("PMF_CURRENT_LEVEL_VERSION", 0x00);
|
||||
|
||||
class PMFLevel extends PMF{
|
||||
private $levelData = array();
|
||||
private $locationTable = array();
|
||||
const VERSION = 0x01;
|
||||
const DEFLATE_LEVEL = 9;
|
||||
|
||||
public $level;
|
||||
public $levelData = array();
|
||||
public $isLoaded = true;
|
||||
private $log = 4;
|
||||
private $payloadOffset = 0;
|
||||
private $chunks = array();
|
||||
private $chunkChange = array();
|
||||
private $chunkInfo = array();
|
||||
public $isGenerating = 0;
|
||||
|
||||
public function getData($index){
|
||||
if(!isset($this->levelData[$index])){
|
||||
|
|
@ -45,19 +46,21 @@ class PMFLevel extends PMF{
|
|||
return true;
|
||||
}
|
||||
|
||||
public function close(){
|
||||
$chunks = null;
|
||||
unset($chunks, $chunkChange, $locationTable);
|
||||
parent::close();
|
||||
public function closeLevel(){
|
||||
$this->chunks = null;
|
||||
unset($this->chunks, $this->chunkChange, $this->chunkInfo, $this->level);
|
||||
$this->close();
|
||||
}
|
||||
|
||||
public function __construct($file, $blank = false){
|
||||
$this->chunks = array();
|
||||
$this->chunkChange = array();
|
||||
$this->chunkInfo = array();
|
||||
if(is_array($blank)){
|
||||
$this->create($file, 0);
|
||||
$this->levelData = $blank;
|
||||
$this->createBlank();
|
||||
$this->isLoaded = true;
|
||||
$this->log = (int) ((string) log($this->levelData["width"], 2));
|
||||
}else{
|
||||
if($this->load($file) !== false){
|
||||
$this->parseInfo();
|
||||
|
|
@ -65,7 +68,6 @@ class PMFLevel extends PMF{
|
|||
$this->isLoaded = false;
|
||||
}else{
|
||||
$this->isLoaded = true;
|
||||
$this->log = (int) ((string) log($this->levelData["width"], 2));
|
||||
}
|
||||
}else{
|
||||
$this->isLoaded = false;
|
||||
|
|
@ -73,8 +75,8 @@ class PMFLevel extends PMF{
|
|||
}
|
||||
}
|
||||
|
||||
public function saveData($locationTable = true){
|
||||
$this->levelData["version"] = PMF_CURRENT_LEVEL_VERSION;
|
||||
public function saveData(){
|
||||
$this->levelData["version"] = PMFLevel::VERSION;
|
||||
@ftruncate($this->fp, 5);
|
||||
$this->seek(5);
|
||||
$this->write(chr($this->levelData["version"]));
|
||||
|
|
@ -84,33 +86,17 @@ class PMFLevel extends PMF{
|
|||
$this->write(Utils::writeFloat($this->levelData["spawnX"]));
|
||||
$this->write(Utils::writeFloat($this->levelData["spawnY"]));
|
||||
$this->write(Utils::writeFloat($this->levelData["spawnZ"]));
|
||||
$this->write(chr($this->levelData["width"]));
|
||||
$this->write(chr($this->levelData["height"]));
|
||||
$extra = gzdeflate($this->levelData["extra"], PMF_LEVEL_DEFLATE_LEVEL);
|
||||
$this->write(Utils::writeShort(strlen($this->levelData["generator"])).$this->levelData["generator"]);
|
||||
$settings = serialize($this->levelData["generatorSettings"]);
|
||||
$this->write(Utils::writeShort(strlen($settings)).$settings);
|
||||
$extra = gzdeflate($this->levelData["extra"], PMFLevel::DEFLATE_LEVEL);
|
||||
$this->write(Utils::writeShort(strlen($extra)).$extra);
|
||||
$this->payloadOffset = ftell($this->fp);
|
||||
|
||||
if($locationTable !== false){
|
||||
$this->writeLocationTable();
|
||||
}
|
||||
}
|
||||
|
||||
private function createBlank(){
|
||||
$this->saveData(false);
|
||||
$this->locationTable = array();
|
||||
$cnt = pow($this->levelData["width"], 2);
|
||||
@mkdir(dirname($this->file)."/chunks/", 0755);
|
||||
for($index = 0; $index < $cnt; ++$index){
|
||||
$this->chunks[$index] = false;
|
||||
$this->chunkChange[$index] = false;
|
||||
$this->locationTable[$index] = array(
|
||||
0 => 0,
|
||||
);
|
||||
$this->write(Utils::writeShort(0));
|
||||
$X = $Z = null;
|
||||
$this->getXZ($index, $X, $Z);
|
||||
@file_put_contents($this->getChunkPath($X, $Z), gzdeflate("", PMF_LEVEL_DEFLATE_LEVEL));
|
||||
}
|
||||
if(!file_exists(dirname($this->file)."/entities.yml")){
|
||||
$entities = new Config(dirname($this->file)."/entities.yml", CONFIG_YAML);
|
||||
$entities->save();
|
||||
|
|
@ -127,7 +113,8 @@ class PMFLevel extends PMF{
|
|||
}
|
||||
$this->seek(5);
|
||||
$this->levelData["version"] = ord($this->read(1));
|
||||
if($this->levelData["version"] > PMF_CURRENT_LEVEL_VERSION){
|
||||
if($this->levelData["version"] > PMFLevel::VERSION){
|
||||
console("[ERROR] New unsupported PMF Level format version #".$this->levelData["version"].", current version is #".PMFLevel::VERSION);
|
||||
return false;
|
||||
}
|
||||
$this->levelData["name"] = $this->read(Utils::readShort($this->read(2), false));
|
||||
|
|
@ -136,92 +123,130 @@ class PMFLevel extends PMF{
|
|||
$this->levelData["spawnX"] = Utils::readFloat($this->read(4));
|
||||
$this->levelData["spawnY"] = Utils::readFloat($this->read(4));
|
||||
$this->levelData["spawnZ"] = Utils::readFloat($this->read(4));
|
||||
$this->levelData["width"] = ord($this->read(1));
|
||||
$this->levelData["height"] = ord($this->read(1));
|
||||
if(($this->levelData["width"] !== 16 and $this->levelData["width"] !== 32) or $this->levelData["height"] !== 8){
|
||||
return false;
|
||||
}
|
||||
$lastseek = ftell($this->fp);
|
||||
if(($len = $this->read(2)) === false or ($this->levelData["extra"] = @gzinflate($this->read(Utils::readShort($len, false)))) === false){ //Corruption protection
|
||||
console("[NOTICE] Empty/corrupt location table detected, forcing recovery");
|
||||
fseek($this->fp, $lastseek);
|
||||
$c = gzdeflate("");
|
||||
$this->write(Utils::writeShort(strlen($c)).$c);
|
||||
$this->payloadOffset = ftell($this->fp);
|
||||
$this->levelData["extra"] = "";
|
||||
$cnt = pow($this->levelData["width"], 2);
|
||||
for($index = 0; $index < $cnt; ++$index){
|
||||
$this->write("\x00\xFF"); //Force index recreation
|
||||
}
|
||||
fseek($this->fp, $this->payloadOffset);
|
||||
if($this->levelData["version"] === 0){
|
||||
$this->read(1);
|
||||
$this->levelData["height"] = ord($this->read(1));
|
||||
}else{
|
||||
$this->payloadOffset = ftell($this->fp);
|
||||
$this->levelData["height"] = ord($this->read(1));
|
||||
if($this->levelData["height"] !== 8){
|
||||
return false;
|
||||
}
|
||||
$this->levelData["generator"] = $this->read(Utils::readShort($this->read(2), false));
|
||||
$this->levelData["generatorSettings"] = unserialize($this->read(Utils::readShort($this->read(2), false)));
|
||||
|
||||
}
|
||||
$this->levelData["extra"] = @gzinflate($this->read(Utils::readShort($this->read(2), false)));
|
||||
|
||||
if($this->levelData["version"] === 0){
|
||||
$this->upgrade_From0_To1();
|
||||
}
|
||||
return $this->readLocationTable();
|
||||
}
|
||||
|
||||
public function getIndex($X, $Z){
|
||||
$X = (int) $X;
|
||||
$Z = (int) $Z;
|
||||
return ($Z << $this->log) + $X;
|
||||
private function upgrade_From0_To1(){
|
||||
console("[NOTICE] Old PMF Level format version #0 detected, upgrading to version #1");
|
||||
for($index = 0; $index < 256; ++$index){
|
||||
$X = $index & 0x0F;
|
||||
$Z = $index >> 4;
|
||||
|
||||
$bitflags = Utils::readShort($this->read(2));
|
||||
$oldPath = dirname($this->file)."/chunks/".$Z.".".$X.".pmc";
|
||||
$chunkOld = gzopen($oldPath, "rb");
|
||||
$newPath = dirname($this->file)."/chunks/".(($X ^ $Z) & 0xff)."/".$Z.".".$X.".pmc";
|
||||
@mkdir(dirname($newPath));
|
||||
$chunkNew = gzopen($newPath, "wb".PMFLevel::DEFLATE_LEVEL);
|
||||
gzwrite($chunkNew, chr($bitflags) . "\x00\x00\x00\x01");
|
||||
while(gzeof($chunkOld) === false){
|
||||
gzwrite($chunkNew, gzread($chunkOld, 65535));
|
||||
}
|
||||
gzclose($chunkNew);
|
||||
gzclose($chunkOld);
|
||||
@unlink($oldPath);
|
||||
}
|
||||
$this->levelData["version"] = 0x01;
|
||||
$this->levelData["generator"] = "NormalGenerator";
|
||||
$this->levelData["generatorSettings"] = "";
|
||||
$this->saveData();
|
||||
}
|
||||
|
||||
public function getXZ($index, &$X = null, &$Z = null){
|
||||
$X = $index >> $this->log;
|
||||
$Z = $index & (pow($this->log, 2) - 1);
|
||||
public static function getIndex($X, $Z){
|
||||
return ($Z << 16) | ($X < 0 ? (~--$X & 0x7fff) | 0x8000 : $X & 0x7fff);
|
||||
}
|
||||
|
||||
public static function getXZ($index, &$X = null, &$Z = null){
|
||||
$Z = $index >> 16;
|
||||
$X = ($index & 0x8000) === 0x8000 ? -($index & 0x7fff) : $index & 0x7fff;
|
||||
return array($X, $Z);
|
||||
}
|
||||
|
||||
private function readLocationTable(){
|
||||
$this->locationTable = array();
|
||||
$cnt = pow($this->levelData["width"], 2);
|
||||
$this->seek($this->payloadOffset);
|
||||
for($index = 0; $index < $cnt; ++$index){
|
||||
$this->chunks[$index] = false;
|
||||
$this->chunkChange[$index] = false;
|
||||
$this->locationTable[$index] = array(
|
||||
0 => Utils::readShort($this->read(2)), //16 bit flags
|
||||
);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private function writeLocationTable(){
|
||||
$cnt = pow($this->levelData["width"], 2);
|
||||
@ftruncate($this->fp, $this->payloadOffset);
|
||||
$this->seek($this->payloadOffset);
|
||||
for($index = 0; $index < $cnt; ++$index){
|
||||
$this->write(Utils::writeShort($this->locationTable[$index][0]));
|
||||
}
|
||||
}
|
||||
|
||||
private function getChunkPath($X, $Z){
|
||||
return dirname($this->file)."/chunks/".$Z.".".$X.".pmc";
|
||||
return dirname($this->file)."/chunks/".(($X ^ $Z) & 0xff)."/".$Z.".".$X.".pmc";
|
||||
}
|
||||
|
||||
public function generateChunk($X, $Z){
|
||||
$path = $this->getChunkPath($X, $Z);
|
||||
if(!file_exists(dirname($path))){
|
||||
@mkdir(dirname($path), 0755);
|
||||
}
|
||||
$this->initCleanChunk($X, $Z);
|
||||
$ret = $this->level->generateChunk($X, $Z);
|
||||
$this->saveChunk($X, $Z);
|
||||
$this->populateChunk($X - 1, $Z);
|
||||
$this->populateChunk($X + 1, $Z);
|
||||
$this->populateChunk($X, $Z - 1);
|
||||
$this->populateChunk($X, $Z + 1);
|
||||
$this->populateChunk($X + 1, $Z + 1);
|
||||
$this->populateChunk($X + 1, $Z - 1);
|
||||
$this->populateChunk($X - 1, $Z - 1);
|
||||
$this->populateChunk($X - 1, $Z + 1);
|
||||
return $ret;
|
||||
}
|
||||
|
||||
public function populateChunk($X, $Z){
|
||||
if($this->isGenerating === 0 and
|
||||
!$this->isPopulated($X, $Z) and
|
||||
$this->isGenerated($X - 1, $Z) and
|
||||
$this->isGenerated($X, $Z - 1) and
|
||||
$this->isGenerated($X + 1, $Z) and
|
||||
$this->isGenerated($X, $Z + 1) and
|
||||
$this->isGenerated($X + 1, $Z + 1) and
|
||||
$this->isGenerated($X - 1, $Z - 1) and
|
||||
$this->isGenerated($X + 1, $Z - 1) and
|
||||
$this->isGenerated($X - 1, $Z + 1)){
|
||||
$this->level->populateChunk($X, $Z);
|
||||
$this->saveChunk($X, $Z);
|
||||
}
|
||||
}
|
||||
|
||||
public function loadChunk($X, $Z){
|
||||
|
||||
$X = (int) $X;
|
||||
$Z = (int) $Z;
|
||||
$index = self::getIndex($X, $Z);
|
||||
|
||||
if($this->isChunkLoaded($X, $Z)){
|
||||
return true;
|
||||
|
||||
}elseif(!isset($this->locationTable[$index])){
|
||||
return false;
|
||||
}
|
||||
|
||||
$info = $this->locationTable[$index];
|
||||
$this->seek($info[0]);
|
||||
|
||||
$chunk = @gzopen($this->getChunkPath($X, $Z), "rb");
|
||||
$path = $this->getChunkPath($X, $Z);
|
||||
if(!file_exists($path)){
|
||||
if($this->generateChunk($X, $Z) === false){
|
||||
return false;
|
||||
}elseif($this->isGenerating === 0){
|
||||
$this->populateChunk($X, $Z);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
$chunk = @gzopen($path, "rb");
|
||||
if($chunk === false){
|
||||
return false;
|
||||
}
|
||||
$this->chunkInfo[$index] = array(
|
||||
0 => ord(gzread($chunk, 1)),
|
||||
1 => Utils::readInt(gzread($chunk, 4)),
|
||||
);
|
||||
$this->chunks[$index] = array();
|
||||
$this->chunkChange[$index] = array(-1 => false);
|
||||
for($Y = 0; $Y < $this->levelData["height"]; ++$Y){
|
||||
for($Y = 0; $Y < $this->chunkInfo[$index][0]; ++$Y){
|
||||
$t = 1 << $Y;
|
||||
if(($info[0] & $t) === $t){
|
||||
if(($this->chunkInfo[$index][0] & $t) === $t){
|
||||
// 4096 + 2048 + 2048, Block Data, Meta, Light
|
||||
if(strlen($this->chunks[$index][$Y] = gzread($chunk, 8192)) < 8192){
|
||||
console("[NOTICE] Empty corrupt chunk detected [$X,$Z,:$Y], recovering contents", true, true, 2);
|
||||
|
|
@ -232,6 +257,9 @@ class PMFLevel extends PMF{
|
|||
}
|
||||
}
|
||||
@gzclose($chunk);
|
||||
if($this->isGenerating === 0 and !$this->isPopulated($X, $Z)){
|
||||
$this->populateChunk($X, $Z);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
@ -243,24 +271,25 @@ class PMFLevel extends PMF{
|
|||
}elseif($save !== false){
|
||||
$this->saveChunk($X, $Z);
|
||||
}
|
||||
$index = $this->getIndex($X, $Z);
|
||||
$index = self::getIndex($X, $Z);
|
||||
$this->chunks[$index] = null;
|
||||
$this->chunkChange[$index] = null;
|
||||
unset($this->chunks[$index], $this->chunkChange[$index]);
|
||||
$this->chunkInfo[$index] = null;
|
||||
unset($this->chunks[$index], $this->chunkChange[$index], $this->chunkInfo[$index]);
|
||||
return true;
|
||||
}
|
||||
|
||||
public function isChunkLoaded($X, $Z){
|
||||
$index = $this->getIndex($X, $Z);
|
||||
if(!isset($this->chunks[$index]) or $this->chunks[$index] === false){
|
||||
$index = self::getIndex($X, $Z);
|
||||
if(!isset($this->chunks[$index])){
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
protected function isMiniChunkEmpty($X, $Z, $Y){
|
||||
$index = $this->getIndex($X, $Z);
|
||||
if($this->chunks[$index][$Y] !== false){
|
||||
public function isMiniChunkEmpty($X, $Z, $Y){
|
||||
$index = self::getIndex($X, $Z);
|
||||
if(isset($this->chunks[$index]) and $this->chunks[$index][$Y] !== false){
|
||||
if(substr_count($this->chunks[$index][$Y], "\x00") < 8192){
|
||||
return false;
|
||||
}
|
||||
|
|
@ -272,48 +301,84 @@ class PMFLevel extends PMF{
|
|||
if($this->isChunkLoaded($X, $Z) === false){
|
||||
return false;
|
||||
}
|
||||
$index = $this->getIndex($X, $Z);
|
||||
$index = self::getIndex($X, $Z);
|
||||
$this->chunks[$index][$Y] = str_repeat("\x00", 8192);
|
||||
$this->chunkChange[$index][-1] = true;
|
||||
$this->chunkChange[$index][$Y] = 8192;
|
||||
$this->locationTable[$index][0] |= 1 << $Y;
|
||||
$this->chunkInfo[$index][0] |= 1 << $Y;
|
||||
return true;
|
||||
}
|
||||
|
||||
public function getMiniChunk($X, $Z, $Y){
|
||||
if($this->loadChunk($X, $Z) === false){
|
||||
if($this->isChunkLoaded($X, $Z) === false and $this->loadChunk($X, $Z) === false){
|
||||
return str_repeat("\x00", 8192);
|
||||
}
|
||||
$index = $this->getIndex($X, $Z);
|
||||
$index = self::getIndex($X, $Z);
|
||||
if(!isset($this->chunks[$index][$Y]) or $this->chunks[$index][$Y] === false){
|
||||
return str_repeat("\x00", 8192);
|
||||
}
|
||||
return $this->chunks[$index][$Y];
|
||||
}
|
||||
|
||||
public function initCleanChunk($X, $Z){
|
||||
$index = self::getIndex($X, $Z);
|
||||
if(!isset($this->chunks[$index])){
|
||||
$this->chunks[$index] = array(
|
||||
0 => false,
|
||||
1 => false,
|
||||
2 => false,
|
||||
3 => false,
|
||||
4 => false,
|
||||
5 => false,
|
||||
6 => false,
|
||||
7 => false,
|
||||
);
|
||||
$this->chunkChange[$index] = array(
|
||||
-1 => true,
|
||||
0 => 8192,
|
||||
1 => 8192,
|
||||
2 => 8192,
|
||||
3 => 8192,
|
||||
4 => 8192,
|
||||
5 => 8192,
|
||||
6 => 8192,
|
||||
7 => 8192,
|
||||
);
|
||||
$this->chunkInfo[$index] = array(
|
||||
0 => 0,
|
||||
1 => 0,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public function setMiniChunk($X, $Z, $Y, $data){
|
||||
if($this->isChunkLoaded($X, $Z) === false){
|
||||
if($this->isGenerating > 0){
|
||||
$this->initCleanChunk($X, $Z);
|
||||
}elseif($this->isChunkLoaded($X, $Z) === false){
|
||||
$this->loadChunk($X, $Z);
|
||||
}
|
||||
if(strlen($data) !== 8192){
|
||||
return false;
|
||||
}
|
||||
$index = $this->getIndex($X, $Z);
|
||||
$index = self::getIndex($X, $Z);
|
||||
$this->chunks[$index][$Y] = (string) $data;
|
||||
$this->chunkChange[$index][-1] = true;
|
||||
$this->chunkChange[$index][$Y] = 8192;
|
||||
$this->locationTable[$index][0] |= 1 << $Y;
|
||||
$this->chunkInfo[$index][0] |= 1 << $Y;
|
||||
return true;
|
||||
}
|
||||
|
||||
public function getBlockID($x, $y, $z){
|
||||
if($y > 127 or $y < 0 or $x < 0 or $z < 0 or $x > 255 or $z > 255){
|
||||
if($y > 127 or $y < 0){
|
||||
return 0;
|
||||
}
|
||||
$X = $x >> 4;
|
||||
$Z = $z >> 4;
|
||||
$Y = $y >> 4;
|
||||
$index = $this->getIndex($X, $Z);
|
||||
$index = self::getIndex($X, $Z);
|
||||
if(!isset($this->chunks[$index])){
|
||||
return 0;
|
||||
}
|
||||
$aX = $x - ($X << 4);
|
||||
$aZ = $z - ($Z << 4);
|
||||
$aY = $y - ($Y << 4);
|
||||
|
|
@ -322,17 +387,17 @@ class PMFLevel extends PMF{
|
|||
}
|
||||
|
||||
public function setBlockID($x, $y, $z, $block){
|
||||
if($y > 127 or $y < 0 or $x < 0 or $z < 0 or $x > 255 or $z > 255){
|
||||
if($y > 127 or $y < 0){
|
||||
return false;
|
||||
}
|
||||
$X = $x >> 4;
|
||||
$Z = $z >> 4;
|
||||
$Y = $y >> 4;
|
||||
$block &= 0xFF;
|
||||
if($X >= 32 or $Z >= 32 or $Y >= $this->levelData["height"] or $y < 0){
|
||||
$index = self::getIndex($X, $Z);
|
||||
if(!isset($this->chunks[$index])){
|
||||
return false;
|
||||
}
|
||||
$index = $this->getIndex($X, $Z);
|
||||
$aX = $x - ($X << 4);
|
||||
$aZ = $z - ($Z << 4);
|
||||
$aY = $y - ($Y << 4);
|
||||
|
|
@ -347,13 +412,16 @@ class PMFLevel extends PMF{
|
|||
}
|
||||
|
||||
public function getBlockDamage($x, $y, $z){
|
||||
if($y > 127 or $y < 0 or $x < 0 or $z < 0 or $x > 255 or $z > 255){
|
||||
if($y > 127 or $y < 0){
|
||||
return 0;
|
||||
}
|
||||
$X = $x >> 4;
|
||||
$Z = $z >> 4;
|
||||
$Y = $y >> 4;
|
||||
$index = $this->getIndex($X, $Z);
|
||||
$index = self::getIndex($X, $Z);
|
||||
if(!isset($this->chunks[$index])){
|
||||
return 0;
|
||||
}
|
||||
$aX = $x - ($X << 4);
|
||||
$aZ = $z - ($Z << 4);
|
||||
$aY = $y - ($Y << 4);
|
||||
|
|
@ -367,17 +435,17 @@ class PMFLevel extends PMF{
|
|||
}
|
||||
|
||||
public function setBlockDamage($x, $y, $z, $damage){
|
||||
if($y > 127 or $y < 0 or $x < 0 or $z < 0 or $x > 255 or $z > 255){
|
||||
if($y > 127 or $y < 0){
|
||||
return false;
|
||||
}
|
||||
$X = $x >> 4;
|
||||
$Z = $z >> 4;
|
||||
$Y = $y >> 4;
|
||||
$damage &= 0x0F;
|
||||
if($X >= 32 or $Z >= 32 or $Y >= $this->levelData["height"] or $y < 0){
|
||||
$index = self::getIndex($X, $Z);
|
||||
if(!isset($this->chunks[$index])){
|
||||
return false;
|
||||
}
|
||||
$index = $this->getIndex($X, $Z);
|
||||
$aX = $x - ($X << 4);
|
||||
$aZ = $z - ($Z << 4);
|
||||
$aY = $y - ($Y << 4);
|
||||
|
|
@ -406,15 +474,13 @@ class PMFLevel extends PMF{
|
|||
$X = $x >> 4;
|
||||
$Z = $z >> 4;
|
||||
$Y = $y >> 4;
|
||||
if($x < 0 or $z < 0 or $X >= $this->levelData["width"] or $Z >= $this->levelData["width"] or $Y >= $this->levelData["height"] or $y < 0){
|
||||
if($y < 0 or $y > 127){
|
||||
return array(AIR, 0);
|
||||
}
|
||||
$index = $this->getIndex($X, $Z);
|
||||
if(!isset($this->chunks[$index]) or $this->chunks[$index] === false){
|
||||
if($this->loadChunk($X, $Z) === false){
|
||||
return array(AIR, 0);
|
||||
}
|
||||
}elseif($this->chunks[$index][$Y] === false){
|
||||
$index = self::getIndex($X, $Z);
|
||||
if(!isset($this->chunks[$index]) and $this->loadChunk($X, $Z) === false){
|
||||
return array(AIR, 0);
|
||||
}elseif($this->chunks[$index][$Y] === false){
|
||||
return array(AIR, 0);
|
||||
}
|
||||
$aX = $x - ($X << 4);
|
||||
|
|
@ -431,19 +497,17 @@ class PMFLevel extends PMF{
|
|||
}
|
||||
|
||||
public function setBlock($x, $y, $z, $block, $meta = 0){
|
||||
if($y > 127 or $y < 0){
|
||||
return false;
|
||||
}
|
||||
$X = $x >> 4;
|
||||
$Z = $z >> 4;
|
||||
$Y = $y >> 4;
|
||||
$block &= 0xFF;
|
||||
$meta &= 0x0F;
|
||||
if($X >= 32 or $Z >= 32 or $Y >= $this->levelData["height"] or $y < 0){
|
||||
$index = self::getIndex($X, $Z);
|
||||
if(!isset($this->chunks[$index]) and $this->loadChunk($X, $Z) === false){
|
||||
return false;
|
||||
}
|
||||
$index = $this->getIndex($X, $Z);
|
||||
if(!isset($this->chunks[$index]) or $this->chunks[$index] === false){
|
||||
if($this->loadChunk($X, $Z) === false){
|
||||
return false;
|
||||
}
|
||||
}elseif($this->chunks[$index][$Y] === false){
|
||||
$this->fillMiniChunk($X, $Z, $Y);
|
||||
}
|
||||
|
|
@ -469,16 +533,6 @@ class PMFLevel extends PMF{
|
|||
++$this->chunkChange[$index][$Y];
|
||||
}
|
||||
$this->chunkChange[$index][-1] = true;
|
||||
if($old_b instanceof LiquidBlock)
|
||||
{
|
||||
$pos = new Position($x, $y, $z, $this->level);
|
||||
for($side = 0; $side <= 5; ++$side)
|
||||
{
|
||||
$b = $pos->getSide($side);
|
||||
if($b instanceof LavaBlock) { ServerAPI::request()->api->block->scheduleBlockUpdate(new Position($b, 0, 0, $this->level), 40, BLOCK_UPDATE_NORMAL); }
|
||||
else { ServerAPI::request()->api->block->scheduleBlockUpdate(new Position($b, 0, 0, $this->level), 10, BLOCK_UPDATE_NORMAL); }
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
|
@ -490,32 +544,70 @@ class PMFLevel extends PMF{
|
|||
if(!$this->isChunkLoaded($X, $Z)){
|
||||
return false;
|
||||
}
|
||||
$index = $this->getIndex($X, $Z);
|
||||
$index = self::getIndex($X, $Z);
|
||||
if(!isset($this->chunkChange[$index]) or $this->chunkChange[$index][-1] === false){//No changes in chunk
|
||||
return true;
|
||||
}
|
||||
|
||||
$chunk = @gzopen($this->getChunkPath($X, $Z), "wb".PMF_LEVEL_DEFLATE_LEVEL);
|
||||
$path = $this->getChunkPath($X, $Z);
|
||||
if(!file_exists(dirname($path))){
|
||||
@mkdir(dirname($path), 0755);
|
||||
}
|
||||
$bitmap = 0;
|
||||
for($Y = 0; $Y < $this->levelData["height"]; ++$Y){
|
||||
for($Y = 0; $Y < 8; ++$Y){
|
||||
if($this->chunks[$index][$Y] !== false and ((isset($this->chunkChange[$index][$Y]) and $this->chunkChange[$index][$Y] === 0) or !$this->isMiniChunkEmpty($X, $Z, $Y))){
|
||||
gzwrite($chunk, $this->chunks[$index][$Y]);
|
||||
$bitmap |= 1 << $Y;
|
||||
}else{
|
||||
$this->chunks[$index][$Y] = false;
|
||||
}
|
||||
$this->chunkChange[$index][$Y] = 0;
|
||||
}
|
||||
$chunk = @gzopen($path, "wb".PMFLevel::DEFLATE_LEVEL);
|
||||
gzwrite($chunk, chr($bitmap));
|
||||
gzwrite($chunk, Utils::writeInt($this->chunkInfo[$index][1]));
|
||||
for($Y = 0; $Y < 8; ++$Y){
|
||||
$t = 1 << $Y;
|
||||
if(($bitmap & $t) === $t){
|
||||
gzwrite($chunk, $this->chunks[$index][$Y]);
|
||||
}
|
||||
}
|
||||
gzclose($chunk);
|
||||
$this->chunkChange[$index][-1] = false;
|
||||
$this->locationTable[$index][0] = $bitmap;
|
||||
$this->seek($this->payloadOffset + ($index << 1));
|
||||
$this->write(Utils::writeShort($this->locationTable[$index][0]));
|
||||
$this->chunkInfo[$index][0] = $bitmap;
|
||||
return true;
|
||||
}
|
||||
|
||||
public function setPopulated($X, $Z){
|
||||
if(!$this->isChunkLoaded($X, $Z)){
|
||||
return false;
|
||||
}
|
||||
$index = self::getIndex($X, $Z);
|
||||
$this->chunkInfo[$index][1] |= 0b00000000000000000000000000000001;
|
||||
}
|
||||
|
||||
public function unsetPopulated($X, $Z){
|
||||
if(!$this->isChunkLoaded($X, $Z)){
|
||||
return false;
|
||||
}
|
||||
$index = self::getIndex($X, $Z);
|
||||
$this->chunkInfo[$index][1] &= ~0b00000000000000000000000000000001;
|
||||
}
|
||||
|
||||
public function isPopulated($X, $Z){
|
||||
if(!$this->isChunkLoaded($X, $Z)){
|
||||
return false;
|
||||
}
|
||||
$index = self::getIndex($X, $Z);
|
||||
return ($this->chunkInfo[$index][1] & 0b00000000000000000000000000000001) > 0;
|
||||
}
|
||||
|
||||
public function isGenerated($X, $Z){
|
||||
return file_exists($this->getChunkPath($X, $Z));
|
||||
}
|
||||
|
||||
public function doSaveRound(){
|
||||
foreach($this->chunks as $index => $chunk){
|
||||
$this->getXZ($index, $X, $Z);
|
||||
self::getXZ($index, $X, $Z);
|
||||
$this->saveChunk($X, $Z);
|
||||
}
|
||||
}
|
||||
|
|
@ -136,7 +136,7 @@ class Entity extends Position{
|
|||
$this->setHealth(10000000, "generic");
|
||||
$this->server->schedule(5, array($this, "updateFuse"), array(), true);
|
||||
$this->update();
|
||||
}elseif($this->type === (OBJECT_ARROW or OBJECT_SNOWBALL)){
|
||||
}elseif($this->type === OBJECT_ARROW){
|
||||
$this->server->schedule(1210, array($this, "update")); //Despawn
|
||||
$this->update();
|
||||
}
|
||||
|
|
@ -164,7 +164,7 @@ class Entity extends Position{
|
|||
}
|
||||
|
||||
public function getDrops(){
|
||||
if($this->class === ENTITY_PLAYER and $this->player instanceof Player and ($this->player->gamemode & 0x01) === 0){
|
||||
if($this->class === ENTITY_PLAYER and ($this->player->gamemode & 0x01) === 0){
|
||||
$inv = array();
|
||||
for($i = 0; $i < PLAYER_SURVIVAL_SLOTS; ++$i){
|
||||
$slot = $this->player->getSlot($i);
|
||||
|
|
@ -201,7 +201,6 @@ class Entity extends Position{
|
|||
case MOB_PIGMAN:
|
||||
return array(
|
||||
array(COOKED_PORKCHOP, 0, mt_rand(0,2)),
|
||||
array(GOLD_INGOT, 0, mt_rand(0,2)),
|
||||
);
|
||||
case MOB_CREEPER:
|
||||
return array(
|
||||
|
|
@ -266,7 +265,7 @@ class Entity extends Position{
|
|||
$this->close(); //Despawn timer
|
||||
return false;
|
||||
}
|
||||
}elseif($this->class === ENTITY_OBJECT and ($this->type === OBJECT_ARROW or OBJECT_SNOWBALL)){
|
||||
}elseif($this->class === ENTITY_OBJECT and $this->type === OBJECT_ARROW){
|
||||
if(($time - $this->spawntime) >= 60){
|
||||
$this->close(); //Despawn timer
|
||||
return false;
|
||||
|
|
@ -297,7 +296,7 @@ class Entity extends Position{
|
|||
}
|
||||
}
|
||||
|
||||
if($this->class !== ENTITY_PLAYER and ($this->x <= 0 or $this->z <= 0 or $this->x >= 256 or $this->z >= 256 or $this->y >= 128 or $this->y <= 0)){
|
||||
if($this->class !== ENTITY_PLAYER and ($this->y >= 128 or $this->y <= 0)){
|
||||
$this->close();
|
||||
return false;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -21,7 +21,7 @@
|
|||
|
||||
class Level{
|
||||
public $entities, $tiles, $blockUpdates, $nextSave, $players = array(), $level;
|
||||
private $time, $startCheck, $startTime, $server, $name, $usedChunks, $changedBlocks, $changedCount, $stopTime;
|
||||
private $time, $startCheck, $startTime, $server, $name, $usedChunks, $changedBlocks, $changedCount, $stopTime, $generator;
|
||||
|
||||
public function __construct(PMFLevel $level, Config $entities, Config $tiles, Config $blockUpdates, $name){
|
||||
$this->server = ServerAPI::request();
|
||||
|
|
@ -40,6 +40,17 @@ class Level{
|
|||
$this->usedChunks = array();
|
||||
$this->changedBlocks = array();
|
||||
$this->changedCount = array();
|
||||
if(class_exists($this->level->levelData["generator"])){
|
||||
$gen = $this->level->levelData["generator"];
|
||||
$this->generator = new $gen((array) $this->level->levelData["generatorSettings"]);
|
||||
}else{
|
||||
if(strtoupper($this->server->api->getProperty("level-type")) == "FLAT"){
|
||||
$this->generator = new SuperflatGenerator();
|
||||
}else{
|
||||
$this->generator = new NormalGenerator();
|
||||
}
|
||||
}
|
||||
$this->generator->init($this, new Random($this->level->levelData["seed"]));
|
||||
}
|
||||
|
||||
public function close(){
|
||||
|
|
@ -93,9 +104,8 @@ class Level{
|
|||
$now = microtime(true);
|
||||
$this->players = $this->server->api->player->getAll($this);
|
||||
|
||||
if(count($this->changedCount) > 0){
|
||||
if($this->level->isGenerating === 0 and count($this->changedCount) > 0){
|
||||
arsort($this->changedCount);
|
||||
$resendChunks = array();
|
||||
foreach($this->changedCount as $index => $count){
|
||||
if($count < 582){//Optimal value, calculated using the relation between minichunks and single packets
|
||||
break;
|
||||
|
|
@ -128,10 +138,10 @@ class Level{
|
|||
if(count($c) === 0){
|
||||
unset($this->usedChunks[$i]);
|
||||
$X = explode(".", $i);
|
||||
$Z = array_pop($X);
|
||||
$X = array_pop($X);
|
||||
$Z = (int) array_pop($X);
|
||||
$X = (int) array_pop($X);
|
||||
if(!$this->isSpawnChunk($X, $Z)){
|
||||
$this->level->unloadChunk((int) $X, (int) $Z, $this->server->saveEnabled);
|
||||
$this->level->unloadChunk($X, $Z, $this->server->saveEnabled);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -139,10 +149,23 @@ class Level{
|
|||
}
|
||||
}
|
||||
|
||||
public function generateChunk($X, $Z){
|
||||
++$this->level->isGenerating;
|
||||
$this->generator->generateChunk($X, $Z);
|
||||
--$this->level->isGenerating;
|
||||
return true;
|
||||
}
|
||||
|
||||
public function populateChunk($X, $Z){
|
||||
$this->level->setPopulated($X, $Z);
|
||||
$this->generator->populateChunk($X, $Z);
|
||||
return true;
|
||||
}
|
||||
|
||||
public function __destruct(){
|
||||
if(isset($this->level)){
|
||||
$this->save(false, false);
|
||||
$this->level->close();
|
||||
$this->level->closeLevel();
|
||||
unset($this->level);
|
||||
}
|
||||
}
|
||||
|
|
@ -392,14 +415,14 @@ class Level{
|
|||
if(!isset($this->level)){
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
if($force !== true and $this->isSpawnChunk($X, $Z)){
|
||||
return false;
|
||||
}
|
||||
Cache::remove("world:{$this->name}:$X:$Z");
|
||||
return $this->level->unloadChunk($X, $Z, $this->server->saveEnabled);
|
||||
}
|
||||
|
||||
|
||||
public function isSpawnChunk($X, $Z){
|
||||
$spawnX = $this->level->getData("spawnX") >> 4;
|
||||
$spawnZ = $this->level->getData("spawnZ") >> 4;
|
||||
|
|
|
|||
|
|
@ -64,9 +64,10 @@ class LevelImport{
|
|||
"spawnX" => $level["SpawnX"],
|
||||
"spawnY" => $level["SpawnY"],
|
||||
"spawnZ" => $level["SpawnZ"],
|
||||
"extra" => "",
|
||||
"width" => 16,
|
||||
"height" => 8
|
||||
"height" => 8,
|
||||
"generator" => "NormalGenerator",
|
||||
"generatorSettings" => "",
|
||||
"extra" => ""
|
||||
));
|
||||
$chunks = new PocketChunkParser();
|
||||
$chunks->loadFile($this->path."chunks.dat");
|
||||
|
|
|
|||
|
|
@ -20,7 +20,8 @@
|
|||
*/
|
||||
|
||||
interface LevelGenerator{
|
||||
public function __construct(array $options = array());
|
||||
|
||||
public function __construct(array $settings = array());
|
||||
|
||||
public function init(Level $level, Random $random);
|
||||
|
||||
|
|
@ -28,7 +29,9 @@ interface LevelGenerator{
|
|||
|
||||
public function populateChunk($chunkX, $chunkZ);
|
||||
|
||||
public function populateLevel();
|
||||
public function getSettings();
|
||||
|
||||
//public function populateLevel();
|
||||
|
||||
public function getSpawn();
|
||||
}
|
||||
169
src/world/generator/NormalGenerator.php
Normal file
169
src/world/generator/NormalGenerator.php
Normal file
|
|
@ -0,0 +1,169 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
*
|
||||
* ____ _ _ __ __ _ __ __ ____
|
||||
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
|
||||
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
|
||||
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
|
||||
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* @author PocketMine Team
|
||||
* @link http://www.pocketmine.net/
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
/***REM_START***/
|
||||
require_once("LevelGenerator.php");
|
||||
/***REM_END***/
|
||||
|
||||
class NormalGenerator implements LevelGenerator{
|
||||
|
||||
private $populators = array();
|
||||
private $level;
|
||||
private $random;
|
||||
private $worldHeight = 65;
|
||||
private $waterHeight = 63;
|
||||
private $noiseHills;
|
||||
private $noisePatches;
|
||||
private $noisePatchesSmall;
|
||||
private $noiseBase;
|
||||
|
||||
public function __construct(array $options = array()){
|
||||
|
||||
}
|
||||
|
||||
public function getSettings(){
|
||||
return array();
|
||||
}
|
||||
|
||||
public function init(Level $level, Random $random){
|
||||
$this->level = $level;
|
||||
$this->random = $random;
|
||||
$this->random->setSeed($this->level->getSeed());
|
||||
$this->noiseHills = new NoiseGeneratorSimplex($this->random, 3);
|
||||
$this->noisePatches = new NoiseGeneratorSimplex($this->random, 2);
|
||||
$this->noisePatchesSmall = new NoiseGeneratorSimplex($this->random, 2);
|
||||
$this->noiseBase = new NoiseGeneratorSimplex($this->random, 16);
|
||||
|
||||
|
||||
$ores = new OrePopulator();
|
||||
$ores->setOreTypes(array(
|
||||
new OreType(new CoalOreBlock(), 20, 16, 0, 128),
|
||||
new OreType(New IronOreBlock(), 20, 8, 0, 64),
|
||||
new OreType(new RedstoneOreBlock(), 8, 7, 0, 16),
|
||||
new OreType(new LapisOreBlock(), 1, 6, 0, 32),
|
||||
new OreType(new GoldOreBlock(), 2, 8, 0, 32),
|
||||
new OreType(new DiamondOreBlock(), 1, 7, 0, 16),
|
||||
new OreType(new DirtBlock(), 20, 32, 0, 128),
|
||||
new OreType(new GravelBlock(), 10, 16, 0, 128),
|
||||
));
|
||||
$this->populators[] = $ores;
|
||||
|
||||
$trees = new TreePopulator();
|
||||
$trees->setBaseAmount(3);
|
||||
$trees->setRandomAmount(0);
|
||||
$this->populators[] = $trees;
|
||||
|
||||
$tallGrass = new TallGrassPopulator();
|
||||
$tallGrass->setBaseAmount(5);
|
||||
$tallGrass->setRandomAmount(0);
|
||||
$this->populators[] = $tallGrass;
|
||||
}
|
||||
|
||||
public function generateChunk($chunkX, $chunkZ){
|
||||
$this->random->setSeed(0xdeadbeef ^ ($chunkX << 8) ^ $chunkZ ^ $this->level->getSeed());
|
||||
$hills = array();
|
||||
$patchesSmall = array();
|
||||
$base = array();
|
||||
for($z = 0; $z < 16; ++$z){
|
||||
for($x = 0; $x < 16; ++$x){
|
||||
$i = ($z << 4) + $x;
|
||||
$hills[$i] = $this->noiseHills->noise2D($x + ($chunkX << 4), $z + ($chunkZ << 4), 0.11, 12, true);
|
||||
$patches[$i] = $this->noisePatches->noise2D($x + ($chunkX << 4), $z + ($chunkZ << 4), 0.03, 16, true);
|
||||
$patchesSmall[$i] = $this->noisePatchesSmall->noise2D($x + ($chunkX << 4), $z + ($chunkZ << 4), 0.5, 4, true);
|
||||
$base[$i] = $this->noiseBase->noise2D($x + ($chunkX << 4), $z + ($chunkZ << 4), 0.7, 16, true);
|
||||
|
||||
if($base[$i] < 0){
|
||||
$base[$i] *= 0.5;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for($chunkY = 0; $chunkY < 8; ++$chunkY){
|
||||
$chunk = "";
|
||||
$startY = $chunkY << 4;
|
||||
$endY = $startY + 16;
|
||||
for($z = 0; $z < 16; ++$z){
|
||||
for($x = 0; $x < 16; ++$x){
|
||||
$i = ($z << 4) + $x;
|
||||
$height = $this->worldHeight + $hills[$i] * 14 + $base[$i] * 7;
|
||||
$height = (int) $height;
|
||||
|
||||
for($y = $startY; $y < $endY; ++$y){
|
||||
$diff = $height - $y;
|
||||
if($y <= 4 and ($y === 0 or $this->random->nextFloat() < 0.75)){
|
||||
$chunk .= "\x07"; //bedrock
|
||||
}elseif($diff > 2){
|
||||
$chunk .= "\x01"; //stone
|
||||
}elseif($diff > 0){
|
||||
if($patches[$i] > 0.7){
|
||||
$chunk .= "\x01"; //stone
|
||||
}elseif($patches[$i] < -0.8){
|
||||
$chunk .= "\x0d"; //gravel
|
||||
}else{
|
||||
$chunk .= "\x03"; //dirt
|
||||
}
|
||||
}elseif($y <= $this->waterHeight){
|
||||
if(($this->waterHeight - $y) <= 1 and $diff === 0){
|
||||
$chunk .= "\x0c"; //sand
|
||||
}elseif($diff === 0){
|
||||
if($patchesSmall[$i] > 0.3){
|
||||
$chunk .= "\x0d"; //gravel
|
||||
}elseif($patchesSmall[$i] < -0.45){
|
||||
$chunk .= "\x0c"; //sand
|
||||
}else{
|
||||
$chunk .= "\x03"; //dirt
|
||||
}
|
||||
}else{
|
||||
$chunk .= "\x09"; //still_water
|
||||
}
|
||||
}elseif($diff === 0){
|
||||
if($patches[$i] > 0.7){
|
||||
$chunk .= "\x01"; //stone
|
||||
}elseif($patches[$i] < -0.8){
|
||||
$chunk .= "\x0d"; //gravel
|
||||
}else{
|
||||
$chunk .= "\x02"; //grass
|
||||
}
|
||||
}else{
|
||||
$chunk .= "\x00";
|
||||
}
|
||||
}
|
||||
$chunk .= "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
|
||||
}
|
||||
}
|
||||
$this->level->setMiniChunk($chunkX, $chunkZ, $chunkY, $chunk);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function populateChunk($chunkX, $chunkZ){
|
||||
$this->random->setSeed(0xdeadbeef ^ ($chunkX << 8) ^ $chunkZ ^ $this->level->getSeed());
|
||||
foreach($this->populators as $populator){
|
||||
$this->random->setSeed(0xdeadbeef ^ ($chunkX << 8) ^ $chunkZ ^ $this->level->getSeed());
|
||||
$populator->populate($this->level, $chunkX, $chunkZ, $this->random);
|
||||
}
|
||||
}
|
||||
|
||||
public function getSpawn(){
|
||||
return $this->level->getSafeSpawn(new Vector3(127.5, 128, 127.5));
|
||||
}
|
||||
|
||||
}
|
||||
169
src/world/generator/NormalGenerator.php.bak
Normal file
169
src/world/generator/NormalGenerator.php.bak
Normal file
|
|
@ -0,0 +1,169 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
*
|
||||
* ____ _ _ __ __ _ __ __ ____
|
||||
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
|
||||
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
|
||||
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
|
||||
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* @author PocketMine Team
|
||||
* @link http://www.pocketmine.net/
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
/***REM_START***/
|
||||
require_once("LevelGenerator.php");
|
||||
/***REM_END***/
|
||||
|
||||
class NormalGenerator implements LevelGenerator{
|
||||
|
||||
private $populators = array();
|
||||
private $level;
|
||||
private $random;
|
||||
private $worldHeight = 66;
|
||||
private $waterHeight = 63;
|
||||
private $noiseHills;
|
||||
private $noisePatches;
|
||||
private $noisePatchesSmall;
|
||||
private $noiseBase;
|
||||
|
||||
public function __construct(array $options = array()){
|
||||
|
||||
}
|
||||
|
||||
public function getSettings(){
|
||||
return array();
|
||||
}
|
||||
|
||||
public function init(Level $level, Random $random){
|
||||
$this->level = $level;
|
||||
$this->random = $random;
|
||||
$this->random->setSeed($this->level->getSeed());
|
||||
$this->noiseHills = new NoiseGeneratorSimplex($this->random, 3);
|
||||
$this->noisePatches = new NoiseGeneratorSimplex($this->random, 2);
|
||||
$this->noisePatchesSmall = new NoiseGeneratorSimplex($this->random, 2);
|
||||
$this->noiseBase = new NoiseGeneratorSimplex($this->random, 16);
|
||||
|
||||
|
||||
$ores = new OrePopulator();
|
||||
$ores->setOreTypes(array(
|
||||
new OreType(new CoalOreBlock(), 20, 16, 0, 128),
|
||||
new OreType(New IronOreBlock(), 20, 8, 0, 64),
|
||||
new OreType(new RedstoneOreBlock(), 8, 7, 0, 16),
|
||||
new OreType(new LapisOreBlock(), 1, 6, 0, 32),
|
||||
new OreType(new GoldOreBlock(), 2, 8, 0, 32),
|
||||
new OreType(new DiamondOreBlock(), 1, 7, 0, 16),
|
||||
new OreType(new DirtBlock(), 20, 32, 0, 128),
|
||||
new OreType(new GravelBlock(), 10, 16, 0, 128),
|
||||
));
|
||||
$this->populators[] = $ores;
|
||||
|
||||
$trees = new TreePopulator();
|
||||
$trees->setBaseAmount(3);
|
||||
$trees->setRandomAmount(0);
|
||||
$this->populators[] = $trees;
|
||||
|
||||
$tallGrass = new TallGrassPopulator();
|
||||
$tallGrass->setBaseAmount(5);
|
||||
$tallGrass->setRandomAmount(0);
|
||||
$this->populators[] = $tallGrass;
|
||||
}
|
||||
|
||||
public function generateChunk($chunkX, $chunkZ){
|
||||
$this->random->setSeed(0xdeadbeef ^ ($chunkX << 8) ^ $chunkZ ^ $this->level->getSeed());
|
||||
$hills = array();
|
||||
$patchesSmall = array();
|
||||
$base = array();
|
||||
for($z = 0; $z < 16; ++$z){
|
||||
for($x = 0; $x < 16; ++$x){
|
||||
$i = ($z << 4) + $x;
|
||||
$hills[$i] = $this->noiseHills->noise2D($x + ($chunkX << 4), $z + ($chunkZ << 4), 0.11, 12, true);
|
||||
$patches[$i] = $this->noisePatches->noise2D($x + ($chunkX << 4), $z + ($chunkZ << 4), 0.03, 16, true);
|
||||
$patchesSmall[$i] = $this->noisePatchesSmall->noise2D($x + ($chunkX << 4), $z + ($chunkZ << 4), 0.5, 4, true);
|
||||
$base[$i] = $this->noiseBase->noise2D($x + ($chunkX << 4), $z + ($chunkZ << 4), 0.7, 16, true);
|
||||
|
||||
if($base[$i] < 0){
|
||||
$base[$i] *= 0.5;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for($chunkY = 0; $chunkY < 8; ++$chunkY){
|
||||
$chunk = "";
|
||||
$startY = $chunkY << 4;
|
||||
$endY = $startY + 16;
|
||||
for($z = 0; $z < 16; ++$z){
|
||||
for($x = 0; $x < 16; ++$x){
|
||||
$i = ($z << 4) + $x;
|
||||
$height = $this->worldHeight + $hills[$i] * 14 + $base[$i] * 7;
|
||||
$height = (int) $height;
|
||||
|
||||
for($y = $startY; $y < $endY; ++$y){
|
||||
$diff = $height - $y;
|
||||
if($y <= 4 and ($y === 0 or $this->random->nextFloat() < 0.75)){
|
||||
$chunk .= "\x07"; //bedrock
|
||||
}elseif($diff > 2){
|
||||
$chunk .= "\x01"; //stone
|
||||
}elseif($diff > 0){
|
||||
if($patches[$i] > 0.7){
|
||||
$chunk .= "\x01"; //stone
|
||||
}elseif($patches[$i] < -0.8){
|
||||
$chunk .= "\x0d"; //gravel
|
||||
}else{
|
||||
$chunk .= "\x03"; //dirt
|
||||
}
|
||||
}elseif($y <= $this->waterHeight){
|
||||
if(($this->waterHeight - $y) <= 1 and $diff === 0){
|
||||
$chunk .= "\x0c"; //sand
|
||||
}elseif($diff === 0){
|
||||
if($patchesSmall[$i] > 0.3){
|
||||
$chunk .= "\x0d"; //gravel
|
||||
}elseif($patchesSmall[$i] < -0.45){
|
||||
$chunk .= "\x0c"; //sand
|
||||
}else{
|
||||
$chunk .= "\x03"; //dirt
|
||||
}
|
||||
}else{
|
||||
$chunk .= "\x09"; //still_water
|
||||
}
|
||||
}elseif($diff === 0){
|
||||
if($patches[$i] > 0.7){
|
||||
$chunk .= "\x01"; //stone
|
||||
}elseif($patches[$i] < -0.8){
|
||||
$chunk .= "\x0d"; //gravel
|
||||
}else{
|
||||
$chunk .= "\x02"; //grass
|
||||
}
|
||||
}else{
|
||||
$chunk .= "\x00";
|
||||
}
|
||||
}
|
||||
$chunk .= "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
|
||||
}
|
||||
}
|
||||
$this->level->setMiniChunk($chunkX, $chunkZ, $chunkY, $chunk);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function populateChunk($chunkX, $chunkZ){
|
||||
$this->random->setSeed(0xdeadbeef ^ ($chunkX << 8) ^ $chunkZ ^ $this->level->getSeed());
|
||||
foreach($this->populators as $populator){
|
||||
$this->random->setSeed(0xdeadbeef ^ ($chunkX << 8) ^ $chunkZ ^ $this->level->getSeed());
|
||||
$populator->populate($this->level, $chunkX, $chunkZ, $this->random);
|
||||
}
|
||||
}
|
||||
|
||||
public function getSpawn(){
|
||||
return $this->level->getSafeSpawn(new Vector3(127.5, 128, 127.5));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -26,6 +26,10 @@ require_once("LevelGenerator.php");
|
|||
class SuperflatGenerator implements LevelGenerator{
|
||||
private $level, $random, $structure, $chunks, $options, $floorLevel, $populators = array();
|
||||
|
||||
public function getSettings(){
|
||||
return $this->options;
|
||||
}
|
||||
|
||||
public function __construct(array $options = array()){
|
||||
$this->preset = "2;7,59x1,3x3,2;1;spawn(radius=10 block=89),decoration(treecount=80 grasscount=45)";
|
||||
$this->options = $options;
|
||||
|
|
@ -125,9 +129,9 @@ class SuperflatGenerator implements LevelGenerator{
|
|||
}
|
||||
}
|
||||
|
||||
public function populateChunk($chunkX, $chunkZ){
|
||||
public function populateChunk($chunkX, $chunkZ){
|
||||
$this->random->setSeed(0xdeadbeef ^ ($chunkX << 8) ^ $chunkZ ^ $this->level->getSeed());
|
||||
foreach($this->populators as $populator){
|
||||
$this->random->setSeed((int) ($chunkX * 0xdead + $chunkZ * 0xbeef) ^ $this->level->getSeed());
|
||||
$populator->populate($this->level, $chunkX, $chunkZ, $this->random);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
File diff suppressed because one or more lines are too long
|
|
@ -21,10 +21,9 @@
|
|||
|
||||
class WorldGenerator{
|
||||
private $seed, $level, $path, $random, $generator, $width;
|
||||
public function __construct(LevelGenerator $generator, $name, $seed = false, $width = 16, $height = 8){
|
||||
public function __construct(LevelGenerator $generator, $name, $seed = false, $height = 8){
|
||||
$this->seed = $seed !== false ? (int) $seed:Utils::readInt(Utils::getRandomBytes(4, false));
|
||||
$this->random = new Random($this->seed);
|
||||
$this->width = (int) $width;
|
||||
$this->height = (int) $height;
|
||||
$this->path = DATA_PATH."worlds/".$name."/";
|
||||
$this->generator = $generator;
|
||||
|
|
@ -35,9 +34,10 @@ class WorldGenerator{
|
|||
"spawnX" => 128,
|
||||
"spawnY" => 128,
|
||||
"spawnZ" => 128,
|
||||
"extra" => "",
|
||||
"width" => $this->width,
|
||||
"height" => $this->height
|
||||
"height" => $this->height,
|
||||
"generator" => get_class($this->generator),
|
||||
"generatorSettings" => $this->generator->getSettings(),
|
||||
"extra" => ""
|
||||
));
|
||||
$entities = new Config($this->path."entities.yml", CONFIG_YAML);
|
||||
$tiles = new Config($this->path."tiles.yml", CONFIG_YAML);
|
||||
|
|
@ -47,23 +47,15 @@ class WorldGenerator{
|
|||
|
||||
public function generate(){
|
||||
$this->generator->init($this->level, $this->random);
|
||||
for($Z = 0; $Z < $this->width; ++$Z){
|
||||
for($X = 0; $X < $this->width; ++$X){
|
||||
$this->generator->generateChunk($X, $Z);
|
||||
|
||||
//Generate 4 chunks for spawning players
|
||||
for($Z = 7; $Z <= 8; ++$Z){
|
||||
for($X = 7; $X <= 8; ++$X){
|
||||
$this->level->level->loadChunk($X, $Z);
|
||||
}
|
||||
console("[NOTICE] Generating level ".ceil((($Z + 1)/$this->width) * 100)."%");
|
||||
}
|
||||
console("[NOTICE] Populating level");
|
||||
$this->generator->populateLevel();
|
||||
for($Z = 0; $Z < $this->width; ++$Z){
|
||||
for($X = 0; $X < $this->width; ++$X){
|
||||
$this->generator->populateChunk($X, $Z);
|
||||
}
|
||||
console("[NOTICE] Populating level ".ceil((($Z + 1)/$this->width) * 100)."%");
|
||||
}
|
||||
|
||||
$this->level->setSpawn($this->generator->getSpawn());
|
||||
$this->level->save(true, true);
|
||||
}
|
||||
|
||||
public function close(){
|
||||
|
|
|
|||
|
|
@ -19,6 +19,78 @@
|
|||
*
|
||||
*/
|
||||
|
||||
|
||||
abstract class NoiseGenerator{
|
||||
protected $perm = array();
|
||||
protected $offsetX = 0;
|
||||
protected $offsetY = 0;
|
||||
protected $offsetZ = 0;
|
||||
protected $octaves = 8;
|
||||
|
||||
public static function floor($x){
|
||||
return $x >= 0 ? (int) $x : (int) ($x - 1);
|
||||
}
|
||||
|
||||
public static function fade($x){
|
||||
return $x * $x * $x * ($x * ($x * 6 - 15) + 10);
|
||||
}
|
||||
|
||||
public static function lerp($x, $y, $z){
|
||||
return $y + $x * ($z - $y);
|
||||
}
|
||||
|
||||
public static function grad($hash, $x, $y, $z){
|
||||
$hash &= 15;
|
||||
$u = $hash < 8 ? $x : $y;
|
||||
$v = $hash < 4 ? $y : (($hash === 12 or $hash === 14) ? $x : $z);
|
||||
return (($hash & 1) === 0 ? $u : -$u) + (($hash & 2) === 0 ? $v : -$v);
|
||||
}
|
||||
|
||||
abstract public function getNoise2D($x, $z);
|
||||
|
||||
abstract public function getNoise3D($x, $y, $z);
|
||||
|
||||
public function noise2D($x, $z, $frequency, $amplitude, $normalized = false){
|
||||
$result = 0;
|
||||
$amp = 1;
|
||||
$freq = 1;
|
||||
$max = 0;
|
||||
|
||||
for($i = 0; $i < $this->octaves; ++$i){
|
||||
$result += $this->getNoise2D($x * $freq, $z * $freq) * $amp;
|
||||
$max += $amp;
|
||||
$freq *= $frequency;
|
||||
$amp *= $amplitude;
|
||||
}
|
||||
if($normalized === true){
|
||||
$result /= $max;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function noise3D($x, $y, $z, $frequency, $amplitude, $normalized = false){
|
||||
$result = 0;
|
||||
$amp = 1;
|
||||
$freq = 1;
|
||||
$max = 0;
|
||||
|
||||
for($i = 0; $i < $this->octaves; ++$i){
|
||||
$result += $this->getNoise3D($x * $freq, $y * $freq, $z * $freq) * $amp;
|
||||
$max += $amp;
|
||||
$freq *= $frequency;
|
||||
$amp *= $amplitude;
|
||||
}
|
||||
if($normalized === true){
|
||||
$result /= $max;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function setOffset($x, $y, $z){
|
||||
$this->offsetX = $x;
|
||||
$this->offsetY = $y;
|
||||
$this->offsetZ = $z;
|
||||
}
|
||||
}
|
||||
|
|
@ -23,7 +23,7 @@
|
|||
require_once("NoiseGenerator.php");
|
||||
/***REM_END***/
|
||||
|
||||
class NoiseGeneratorOctaves extends NoiseGenerator{
|
||||
abstract class NoiseGeneratorOctaves extends NoiseGenerator{
|
||||
public $octaves;
|
||||
private $generatorCollection;
|
||||
public function __construct(Random $random, $octaves){
|
||||
|
|
@ -65,4 +65,4 @@ class NoiseGeneratorOctaves extends NoiseGenerator{
|
|||
}
|
||||
return $floats;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -24,146 +24,79 @@ require_once("NoiseGenerator.php");
|
|||
/***REM_END***/
|
||||
|
||||
class NoiseGeneratorPerlin extends NoiseGenerator{
|
||||
private $permutations = array();
|
||||
public $xCoord, $yCoord, $zCoord;
|
||||
public static $grad3 = [
|
||||
[1, 1, 0], [-1, 1, 0], [1, -1, 0], [-1, -1, 0],
|
||||
[1, 0, 1], [-1, 0, 1], [1, 0, -1], [-1, 0, -1],
|
||||
[0, 1, 1], [0, -1, 1], [0, 1, -1], [0, -1, -1]
|
||||
];
|
||||
|
||||
public function __construct($random = false){
|
||||
if(!($random instanceof Random)){
|
||||
$random = new Random();
|
||||
}
|
||||
$this->xCoord = $random->nextFloat() * 256;
|
||||
$this->yCoord = $random->nextFloat() * 256;
|
||||
$this->zCoord = $random->nextFloat() * 256;
|
||||
|
||||
public function __construct(Random $random, $octaves){
|
||||
$this->octaves = $octaves;
|
||||
$this->offsetX = $random->nextFloat() * 256;
|
||||
$this->offsetY = $random->nextFloat() * 256;
|
||||
$this->offsetZ = $random->nextFloat() * 256;
|
||||
|
||||
for($i = 0; $i < 512; ++$i){
|
||||
$this->permutations[$i] = 0;
|
||||
}
|
||||
for($i = 0; $i < 256; ++$i){
|
||||
$this->permutations[$i] = $i;
|
||||
$this->perm[$i] = 0;
|
||||
}
|
||||
|
||||
for($i = 0; $i < 256; ++$i){
|
||||
$j = $random->nextRange(0, 256 - $i) + $i;
|
||||
$k = $this->permutations[$i];
|
||||
$this->permutations[$i] = $this->permutations[$j];
|
||||
$this->permutations[$j] = $k;
|
||||
$this->permutations[$i + 256] = $this->permutations[$i];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public final function curve($par1, $par2, $par3){
|
||||
return $par2 + $par1 * ($par3 - $par2);
|
||||
}
|
||||
|
||||
public function grad2D($int, $par1, $par2){
|
||||
$i = $int & 0x0F;
|
||||
$d1 = (1 - (($i & 0x08) >> 3)) * $par1;
|
||||
$d2 = ($i === 12 or $i === 14) ? $par1:($i < 4 ? 0:$par2);
|
||||
|
||||
return (($i & 0x01) === 0 ? $d1:-$d1) + (($i & 0x02) === 0 ? $d2:-$d2);
|
||||
}
|
||||
|
||||
public function grad3D($int, $par1, $par2, $par3){
|
||||
$i = $int & 0x0F;
|
||||
$d1 = $i < 8 ? $par1 : $par2;
|
||||
$d2 = ($i === 12 or $i === 14) ? $par1:($i < 4 ? $par2:$par3);
|
||||
|
||||
return (($i & 0x01) === 0 ? $d1:-$d1) + (($i & 0x02) === 0 ? $d2:-$d2);
|
||||
}
|
||||
|
||||
public function populateNoiseArray(&$floats, $par1, $par2, $par3, $int1, $int2, $int3, $par4, $par5, $par6, $par7){
|
||||
if($int2 === 1){
|
||||
$n = 0;
|
||||
$d3 = 1 / $par7;
|
||||
for($i1 = 0; $i1 < $int1; ++$i1){
|
||||
$d4 = $par1 + $i1 * $par4 + $this->xCoord;
|
||||
$i2 = (int) $d4;
|
||||
if($d4 < $i2){
|
||||
--$i2;
|
||||
}
|
||||
$i3 = $i2 & 0xFF;
|
||||
$d4 -= $i2;
|
||||
$d5 = $d4 * $d4 * $d4 * ($d4 * ($d4 * 6 - 15) + 10);
|
||||
|
||||
for($i4 = 0; $i4 < $int3; ++$i4){
|
||||
$d6 = $par3 + $i4 * $par6 + $this->zCoord;
|
||||
$i5 = (int) $d6;
|
||||
if($d6 < $i5){
|
||||
--$i5;
|
||||
}
|
||||
$i6 = $i5 & 0xFF;
|
||||
$d6 -= $i5;
|
||||
$d7 = $d6 * $d6 * $d6 * ($d6 * ($d6 * 6 - 15) + 10);
|
||||
|
||||
$i = $this->permutations[$i3];
|
||||
$j = $this->permutations[$i] + $i6;
|
||||
$k = $this->permutations[$i3 + 1];
|
||||
$m = $this->permutations[$k] + $i6;
|
||||
$d1 = $this->curve($d5, $this->grad2D($this->permutations[$j], $d4, $d6), $this->grad3D($this->permutations[$m], $d4 - 1, 0, $d6));
|
||||
$d2 = $this->curve($d5, $this->grad3D($this->permutations[$j + 1], $d4, 0, $d6 - 1), $this->grad3D($this->permutations[$m + 1], $d4 - 1, 0, $d6 - 1));
|
||||
|
||||
$d8 = $this->curve($d7, $d1, $d2);
|
||||
$floats[$n++] += $d8 * $d3;
|
||||
}
|
||||
}
|
||||
return;
|
||||
$this->perm[$i] = $random->nextRange(0, 255);
|
||||
}
|
||||
|
||||
$d9 = 1 / $par7;
|
||||
$m = -1;
|
||||
$n = 0;
|
||||
$i = 0;
|
||||
|
||||
for($i4 = 0; $i4 < $int1; ++$i4){
|
||||
$d6 = $par1 + $i4 * $par4 + $this->xCoord;
|
||||
$i5 = (int) $d6;
|
||||
if($d6 < $i5){
|
||||
--$i5;
|
||||
}
|
||||
$i6 = $i5 & 0xFF;
|
||||
$d6 -= $i5;
|
||||
$d7 = $d6 * $d6 * $d6 * ($d6 * ($d6 * 6 - 15) + 10);
|
||||
for($i = 0; $i < 256; ++$i){
|
||||
$pos = $random->nextRange(0, 255 - $i) + $i;
|
||||
$old = $this->perm[$i];
|
||||
|
||||
for($i12 = 0; $i12 < $int3; ++$i12){
|
||||
$d12 = $par3 + $i12 * $par6 + $this->zCoord;
|
||||
$i13 = (int) $d12;
|
||||
if($d12 < $i13){
|
||||
--$i13;
|
||||
}
|
||||
$i14 = $i13 & 0xFF;
|
||||
$d12 -= $i13;
|
||||
$d13 = $d12 * $d12 * $d12 * ($d12 * ($d12 * 6 - 15) + 10);
|
||||
|
||||
for($i15 = 0; $i15 < $int2; ++$i15){
|
||||
$d14 = $par2 + $i15 * $par5 + $this->yCoord;
|
||||
$i16 = (int) $d14;
|
||||
if($d14 < $i16){
|
||||
--$i16;
|
||||
}
|
||||
$d14 -= $i16;
|
||||
$d15 = $d14 * $d14 * $d14 * ($d14 * ($d14 * 6 - 15) + 10);
|
||||
|
||||
if($i15 === 0 or $i17 !== $m){
|
||||
$m = $i17;
|
||||
$i7 = $this->permutations[$i6] + $i17;
|
||||
$i8 = $this->permutations[$i7] + $i14;
|
||||
$i9 = $this->permutations[$i7 + 1] + $i14;
|
||||
$i10 = $this->permutations[$i6 + 1] + $i17;
|
||||
$n = $this->permutations[$i10] + $i14;
|
||||
$i11 = $this->permutations[$i10 + 1] + $i14;
|
||||
$d10 = $this->curve($d7, $this->grad3D($this->permutations[$i8], $d6, $d14, $d12), $this->grad3D($this->permutations[$n], $d6 - 1, $d14, $d12));
|
||||
$d4 = $this->curve($d7, $this->grad3D($this->permutations[$i9], $d6, $d14 - 1, $d12), $this->grad3D($this->permutations[$i11], $d6 - 1, $d14 - 1, $d12));
|
||||
$d11 = $this->curve($d7, $this->grad3D($this->permutations[$i8 + 1], $d6, $d14, $d12 - 1), $this->grad3D($this->permutations[$n + 1], $d6 - 1, $d14, $d12 - 1));
|
||||
$d5 = $this->curve($d7, $this->grad3D($this->permutations[$i9 + 1], $d6, $d14 - 1, $d12 - 1), $this->grad3D($this->permutations[$i11 + 1], $d6 - 1, $d14 - 1, $d12 - 1));
|
||||
}
|
||||
|
||||
$d16 = $this->curve($d15, $d10, $d4);
|
||||
$d17 = $this->curve($d15, $d11, $d5);
|
||||
$d18 = $this->curve($d13, $d16, $d17);
|
||||
$floats[$i++] += $d18 * $d9;
|
||||
}
|
||||
}
|
||||
$this->perm[$i] = $this->perm[$pos];
|
||||
$this->perm[$pos] = $old;
|
||||
$this->perm[$i + 256] = $this->perm[$i];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function getNoise3D($x, $y, $z){
|
||||
$x += $this->offsetX;
|
||||
$y += $this->offsetY;
|
||||
$z += $this->offsetZ;
|
||||
|
||||
$floorX = self::floor($x);
|
||||
$floorY = self::floor($y);
|
||||
$floorZ = self::floor($z);
|
||||
|
||||
$X = $floorX & 0xFF;
|
||||
$Y = $floorY & 0xFF;
|
||||
$Z = $floorZ & 0xFF;
|
||||
|
||||
$x -= $floorX;
|
||||
$y -= $floorY;
|
||||
$z -= $floorZ;
|
||||
|
||||
//Fade curves
|
||||
$fX = self::fade($x);
|
||||
$fY = self::fade($y);
|
||||
$fZ = self::fade($z);
|
||||
|
||||
//Cube corners
|
||||
$A = $this->perm[$X] + $Y;
|
||||
$AA = $this->perm[$A] + $Z;
|
||||
$AB = $this->perm[$A + 1] + $Z;
|
||||
$B = $this->perm[$X + 1] + $Y;
|
||||
$BA = $this->perm[$B] + $Z;
|
||||
$BB = $this->perm[$B + 1] + $Z;
|
||||
|
||||
return self::lerp($fZ, self::lerp($fY, self::lerp($fX, self::grad($this->perm[$AA], $x, $y, $z),
|
||||
self::grad($this->perm[$BA], $x - 1, $y, $z)),
|
||||
self::lerp($fX, self::grad($this->perm[$AB], $x, $y - 1, $z),
|
||||
self::grad($this->perm[$BB], $x - 1, $y - 1, $z))),
|
||||
self::lerp($fY, self::lerp($fX, self::grad($this->perm[$AA + 1], $x, $y, $z - 1),
|
||||
self::grad($this->perm[$BA + 1], $x - 1, $y, $z - 1)),
|
||||
self::lerp($fX, self::grad($this->perm[$AB + 1], $x, $y - 1, $z - 1),
|
||||
self::grad($this->perm[$BB + 1], $x - 1, $y - 1, $z - 1))));
|
||||
}
|
||||
|
||||
public function getNoise2D($x, $y){
|
||||
return $this->getNoise3D($x, $y, 0);
|
||||
}
|
||||
}
|
||||
438
src/world/generator/noise/NoiseGeneratorSimplex.php
Normal file
438
src/world/generator/noise/NoiseGeneratorSimplex.php
Normal file
|
|
@ -0,0 +1,438 @@
|
|||
<?php
|
||||
|
||||
/***REM_START***/
|
||||
require_once("NoiseGenerator.php");
|
||||
require_once("NoiseGeneratorPerlin.php");
|
||||
/***REM_END***/
|
||||
|
||||
/**
|
||||
* Generates simplex-based noise.
|
||||
* <p>
|
||||
* This is a modified version of the freely published version in the paper by
|
||||
* Stefan Gustavson at
|
||||
* <a href="http://staffwww.itn.liu.se/~stegu/simplexnoise/simplexnoise.pdf">
|
||||
* http://staffwww.itn.liu.se/~stegu/simplexnoise/simplexnoise.pdf</a>
|
||||
*/
|
||||
class NoiseGeneratorSimplex extends NoiseGeneratorPerlin{
|
||||
protected static $SQRT_3;
|
||||
protected static $SQRT_5;
|
||||
protected static $F2;
|
||||
protected static $G2;
|
||||
protected static $G22;
|
||||
protected static $F3;
|
||||
protected static $G3;
|
||||
protected static $F4;
|
||||
protected static $G4;
|
||||
protected static $G42;
|
||||
protected static $G43;
|
||||
protected static $G44;
|
||||
protected static $grad4 = [[0, 1, 1, 1],[0, 1, 1, -1],[0, 1, -1, 1],[0, 1, -1, -1],
|
||||
[0, -1, 1, 1],[0, -1, 1, -1],[0, -1, -1, 1],[0, -1, -1, -1],
|
||||
[1, 0, 1, 1],[1, 0, 1, -1],[1, 0, -1, 1],[1, 0, -1, -1],
|
||||
[-1, 0, 1, 1],[-1, 0, 1, -1],[-1, 0, -1, 1],[-1, 0, -1, -1],
|
||||
[1, 1, 0, 1],[1, 1, 0, -1],[1, -1, 0, 1],[1, -1, 0, -1],
|
||||
[-1, 1, 0, 1],[-1, 1, 0, -1],[-1, -1, 0, 1],[-1, -1, 0, -1],
|
||||
[1, 1, 1, 0],[1, 1, -1, 0],[1, -1, 1, 0],[1, -1, -1, 0],
|
||||
[-1, 1, 1, 0],[-1, 1, -1, 0],[-1, -1, 1, 0],[-1, -1, -1, 0]];
|
||||
protected static $simplex = [
|
||||
[0, 1, 2, 3],[0, 1, 3, 2],[0, 0, 0, 0],[0, 2, 3, 1],[0, 0, 0, 0],[0, 0, 0, 0],[0, 0, 0, 0],[1, 2, 3, 0],
|
||||
[0, 2, 1, 3],[0, 0, 0, 0],[0, 3, 1, 2],[0, 3, 2, 1],[0, 0, 0, 0],[0, 0, 0, 0],[0, 0, 0, 0],[1, 3, 2, 0],
|
||||
[0, 0, 0, 0],[0, 0, 0, 0],[0, 0, 0, 0],[0, 0, 0, 0],[0, 0, 0, 0],[0, 0, 0, 0],[0, 0, 0, 0],[0, 0, 0, 0],
|
||||
[1, 2, 0, 3],[0, 0, 0, 0],[1, 3, 0, 2],[0, 0, 0, 0],[0, 0, 0, 0],[0, 0, 0, 0],[2, 3, 0, 1],[2, 3, 1, 0],
|
||||
[1, 0, 2, 3],[1, 0, 3, 2],[0, 0, 0, 0],[0, 0, 0, 0],[0, 0, 0, 0],[2, 0, 3, 1],[0, 0, 0, 0],[2, 1, 3, 0],
|
||||
[0, 0, 0, 0],[0, 0, 0, 0],[0, 0, 0, 0],[0, 0, 0, 0],[0, 0, 0, 0],[0, 0, 0, 0],[0, 0, 0, 0],[0, 0, 0, 0],
|
||||
[2, 0, 1, 3],[0, 0, 0, 0],[0, 0, 0, 0],[0, 0, 0, 0],[3, 0, 1, 2],[3, 0, 2, 1],[0, 0, 0, 0],[3, 1, 2, 0],
|
||||
[2, 1, 0, 3],[0, 0, 0, 0],[0, 0, 0, 0],[0, 0, 0, 0],[3, 1, 0, 2],[0, 0, 0, 0],[3, 2, 0, 1],[3, 2, 1, 0]];
|
||||
protected $offsetW;
|
||||
|
||||
public function __construct(Random $random, $octaves){
|
||||
parent::__construct($random, $octaves);
|
||||
$this->offsetW = $random->nextFloat() * 256;
|
||||
self::$SQRT_3 = sqrt(3);
|
||||
self::$SQRT_5 = sqrt(5);
|
||||
self::$F2 = 0.5 * (self::$SQRT_3 - 1);
|
||||
self::$G2 = (3 - self::$SQRT_3) / 6;
|
||||
self::$G22 = self::$G2 * 2.0 - 1;
|
||||
self::$F3 = 1.0 / 3.0;
|
||||
self::$G3 = 1.0 / 6.0;
|
||||
self::$F4 = (self::$SQRT_5 - 1.0) / 4.0;
|
||||
self::$G4 = (5.0 - self::$SQRT_5) / 20.0;
|
||||
self::$G42 = self::$G4 * 2.0;
|
||||
self::$G43 = self::$G4 * 3.0;
|
||||
self::$G44 = self::$G4 * 4.0 - 1.0;
|
||||
}
|
||||
|
||||
protected static function dot2D($g, $x, $y){
|
||||
return $g[0] * $x + $g[1] * $y;
|
||||
}
|
||||
|
||||
protected static function dot3D($g, $x, $y, $z){
|
||||
return $g[0] * $x + $g[1] * $y + $g[2] * $z;
|
||||
}
|
||||
|
||||
protected static function dot4D($g, $x, $y, $z, $w){
|
||||
return $g[0] * $x + $g[1] * $y + $g[2] * $z + $g[3] * $w;
|
||||
}
|
||||
|
||||
public function getNoise3D($x, $y, $z){
|
||||
$x += $this->offsetX;
|
||||
$y += $this->offsetY;
|
||||
$z += $this->offsetZ;
|
||||
|
||||
// Skew the input space to determine which simplex cell we're in
|
||||
$s = ($x + $y + $z) * self::$F3; // Very nice and simple skew factor for 3D
|
||||
$i = self::floor($x + $s);
|
||||
$j = self::floor($y + $s);
|
||||
$k = self::floor($z + $s);
|
||||
$t = ($i + $j + $k) * self::$G3;
|
||||
$X0 = $i - $t; // Unskew the cell origin back to (x,y,z) space
|
||||
$Y0 = $j - $t;
|
||||
$Z0 = $k - $t;
|
||||
$x0 = $x - $X0; // The x,y,z distances from the cell origin
|
||||
$y0 = $y - $Y0;
|
||||
$z0 = $z - $Z0;
|
||||
|
||||
// For the 3D case, the simplex shape is a slightly irregular tetrahedron.
|
||||
|
||||
// Determine which simplex we are in.
|
||||
if($x0 >= $y0){
|
||||
if($y0 >= $z0){
|
||||
$i1 = 1;
|
||||
$j1 = 0;
|
||||
$k1 = 0;
|
||||
$i2 = 1;
|
||||
$j2 = 1;
|
||||
$k2 = 0;
|
||||
}// X Y Z order
|
||||
elseif($x0 >= $z0){
|
||||
$i1 = 1;
|
||||
$j1 = 0;
|
||||
$k1 = 0;
|
||||
$i2 = 1;
|
||||
$j2 = 0;
|
||||
$k2 = 1;
|
||||
}// X Z Y order
|
||||
else{
|
||||
$i1 = 0;
|
||||
$j1 = 0;
|
||||
$k1 = 1;
|
||||
$i2 = 1;
|
||||
$j2 = 0;
|
||||
$k2 = 1;
|
||||
}// Z X Y order
|
||||
}else{ // x0<y0
|
||||
if($y0 < $z0){
|
||||
$i1 = 0;
|
||||
$j1 = 0;
|
||||
$k1 = 1;
|
||||
$i2 = 0;
|
||||
$j2 = 1;
|
||||
$k2 = 1;
|
||||
}// Z Y X order
|
||||
elseif($x0 < $z0){
|
||||
$i1 = 0;
|
||||
$j1 = 1;
|
||||
$k1 = 0;
|
||||
$i2 = 0;
|
||||
$j2 = 1;
|
||||
$k2 = 1;
|
||||
}// Y Z X order
|
||||
else{
|
||||
$i1 = 0;
|
||||
$j1 = 1;
|
||||
$k1 = 0;
|
||||
$i2 = 1;
|
||||
$j2 = 1;
|
||||
$k2 = 0;
|
||||
}// Y X Z order
|
||||
}
|
||||
|
||||
// A step of (1,0,0) in (i,j,k) means a step of (1-c,-c,-c) in (x,y,z),
|
||||
// a step of (0,1,0) in (i,j,k) means a step of (-c,1-c,-c) in (x,y,z), and
|
||||
// a step of (0,0,1) in (i,j,k) means a step of (-c,-c,1-c) in (x,y,z), where
|
||||
// c = 1/6.
|
||||
$x1 = $x0 - $i1 + self::$G3; // Offsets for second corner in (x,y,z) coords
|
||||
$y1 = $y0 - $j1 + self::$G3;
|
||||
$z1 = $z0 - $k1 + self::$G3;
|
||||
$x2 = $x0 - $i2 + 2.0 * self::$G3; // Offsets for third corner in (x,y,z) coords
|
||||
$y2 = $y0 - $j2 + 2.0 * self::$G3;
|
||||
$z2 = $z0 - $k2 + 2.0 * self::$G3;
|
||||
$x3 = $x0 - 1.0 + 3.0 * self::$G3; // Offsets for last corner in (x,y,z) coords
|
||||
$y3 = $y0 - 1.0 + 3.0 * self::$G3;
|
||||
$z3 = $z0 - 1.0 + 3.0 * self::$G3;
|
||||
|
||||
// Work out the hashed gradient indices of the four simplex corners
|
||||
$ii = $i & 255;
|
||||
$jj = $j & 255;
|
||||
$kk = $k & 255;
|
||||
$gi0 = $this->perm[$ii + $this->perm[$jj + $this->perm[$kk]]] % 12;
|
||||
$gi1 = $this->perm[$ii + $i1 + $this->perm[$jj + $j1 + $this->perm[$kk + $k1]]] % 12;
|
||||
$gi2 = $this->perm[$ii + $i2 + $this->perm[$jj + $j2 + $this->perm[$kk + $k2]]] % 12;
|
||||
$gi3 = $this->perm[$ii + 1 + $this->perm[$jj + 1 + $this->perm[$kk + 1]]] % 12;
|
||||
|
||||
// Calculate the contribution from the four corners
|
||||
$t0 = 0.6 - $x0 * $x0 - $y0 * $y0 - $z0 * $z0;
|
||||
if($t0 < 0){
|
||||
$n0 = 0.0;
|
||||
}else{
|
||||
$t0 *= $t0;
|
||||
$n0 = $t0 * $t0 * self::dot3D(self::$grad3[$gi0], $x0, $y0, $z0);
|
||||
}
|
||||
|
||||
$t1 = 0.6 - $x1 * $x1 - $y1 * $y1 - $z1 * $z1;
|
||||
if($t1 < 0){
|
||||
$n1 = 0.0;
|
||||
}else{
|
||||
$t1 *= $t1;
|
||||
$n1 = $t1 * $t1 * self::dot3D(self::$grad3[$gi1], $x1, $y1, $z1);
|
||||
}
|
||||
|
||||
$t2 = 0.6 - $x2 * $x2 - $y2 * $y2 - $z2 * $z2;
|
||||
if($t2 < 0){
|
||||
$n2 = 0.0;
|
||||
}else{
|
||||
$t2 *= $t2;
|
||||
$n2 = $t2 * $t2 * self::dot3D(self::$grad3[$gi2], $x2, $y2, $z2);
|
||||
}
|
||||
|
||||
$t3 = 0.6 - $x3 * $x3 - $y3 * $y3 - $z3 * $z3;
|
||||
if($t3 < 0){
|
||||
$n3 = 0.0;
|
||||
}else{
|
||||
$t3 *= $t3;
|
||||
$n3 = $t3 * $t3 * self::dot3D(self::$grad3[$gi3], $x3, $y3, $z3);
|
||||
}
|
||||
|
||||
// Add contributions from each corner to get the noise value.
|
||||
// The result is scaled to stay just inside [-1,1]
|
||||
return 32.0 * ($n0 + $n1 + $n2 + $n3);
|
||||
}
|
||||
|
||||
public function getNoise2D($x, $y){
|
||||
$x += $this->offsetX;
|
||||
$y += $this->offsetY;
|
||||
|
||||
// Skew the input space to determine which simplex cell we're in
|
||||
$s = ($x + $y) * self::$F2; // Hairy factor for 2D
|
||||
$i = self::floor($x + $s);
|
||||
$j = self::floor($y + $s);
|
||||
$t = ($i + $j) * self::$G2;
|
||||
$X0 = $i - $t; // Unskew the cell origin back to (x,y) space
|
||||
$Y0 = $j - $t;
|
||||
$x0 = $x - $X0; // The x,y distances from the cell origin
|
||||
$y0 = $y - $Y0;
|
||||
|
||||
// For the 2D case, the simplex shape is an equilateral triangle.
|
||||
|
||||
// Determine which simplex we are in.
|
||||
if($x0 > $y0){
|
||||
$i1 = 1;
|
||||
$j1 = 0;
|
||||
}// lower triangle, XY order: (0,0)->(1,0)->(1,1)
|
||||
else{
|
||||
$i1 = 0;
|
||||
$j1 = 1;
|
||||
}// upper triangle, YX order: (0,0)->(0,1)->(1,1)
|
||||
|
||||
// A step of (1,0) in (i,j) means a step of (1-c,-c) in (x,y), and
|
||||
// a step of (0,1) in (i,j) means a step of (-c,1-c) in (x,y), where
|
||||
// c = (3-sqrt(3))/6
|
||||
|
||||
$x1 = $x0 - $i1 + self::$G2; // Offsets for middle corner in (x,y) unskewed coords
|
||||
$y1 = $y0 - $j1 + self::$G2;
|
||||
$x2 = $x0 + self::$G22; // Offsets for last corner in (x,y) unskewed coords
|
||||
$y2 = $y0 + self::$G22;
|
||||
|
||||
// Work out the hashed gradient indices of the three simplex corners
|
||||
$ii = $i & 255;
|
||||
$jj = $j & 255;
|
||||
$gi0 = $this->perm[$ii + $this->perm[$jj]] % 12;
|
||||
$gi1 = $this->perm[$ii + $i1 + $this->perm[$jj + $j1]] % 12;
|
||||
$gi2 = $this->perm[$ii + 1 + $this->perm[$jj + 1]] % 12;
|
||||
|
||||
// Calculate the contribution from the three corners
|
||||
$t0 = 0.5 - $x0 * $x0 - $y0 * $y0;
|
||||
if($t0 < 0){
|
||||
$n0 = 0.0;
|
||||
}else{
|
||||
$t0 *= $t0;
|
||||
$n0 = $t0 * $t0 * self::dot2D(self::$grad3[$gi0], $x0, $y0); // (x,y) of grad3 used for 2D gradient
|
||||
}
|
||||
|
||||
$t1 = 0.5 - $x1 * $x1 - $y1 * $y1;
|
||||
if($t1 < 0){
|
||||
$n1 = 0.0;
|
||||
}else{
|
||||
$t1 *= $t1;
|
||||
$n1 = $t1 * $t1 * self::dot2D(self::$grad3[$gi1], $x1, $y1);
|
||||
}
|
||||
|
||||
$t2 = 0.5 - $x2 * $x2 - $y2 * $y2;
|
||||
if($t2 < 0){
|
||||
$n2 = 0.0;
|
||||
}else{
|
||||
$t2 *= $t2;
|
||||
$n2 = $t2 * $t2 * self::dot2D(self::$grad3[$gi2], $x2, $y2);
|
||||
}
|
||||
|
||||
// Add contributions from each corner to get the noise value.
|
||||
// The result is scaled to return values in the interval [-1,1].
|
||||
return 70.0 * ($n0 + $n1 + $n2);
|
||||
}
|
||||
|
||||
/**
|
||||
* Computes and returns the 4D simplex noise for the given coordinates in
|
||||
* 4D space
|
||||
*
|
||||
* @param x X coordinate
|
||||
* @param y Y coordinate
|
||||
* @param z Z coordinate
|
||||
* @param w W coordinate
|
||||
* @return Noise at given location, from range -1 to 1
|
||||
*/
|
||||
/*public function getNoise4D(x, y, z, w){
|
||||
x += offsetX;
|
||||
y += offsetY;
|
||||
z += offsetZ;
|
||||
w += offsetW;
|
||||
|
||||
n0, n1, n2, n3, n4; // Noise contributions from the five corners
|
||||
|
||||
// Skew the (x,y,z,w) space to determine which cell of 24 simplices we're in
|
||||
s = (x + y + z + w) * self::$F4; // Factor for 4D skewing
|
||||
i = floor(x + s);
|
||||
j = floor(y + s);
|
||||
k = floor(z + s);
|
||||
l = floor(w + s);
|
||||
|
||||
t = (i + j + k + l) * self::$G4; // Factor for 4D unskewing
|
||||
X0 = i - t; // Unskew the cell origin back to (x,y,z,w) space
|
||||
Y0 = j - t;
|
||||
Z0 = k - t;
|
||||
W0 = l - t;
|
||||
x0 = x - X0; // The x,y,z,w distances from the cell origin
|
||||
y0 = y - Y0;
|
||||
z0 = z - Z0;
|
||||
w0 = w - W0;
|
||||
|
||||
// For the 4D case, the simplex is a 4D shape I won't even try to describe.
|
||||
// To find out which of the 24 possible simplices we're in, we need to
|
||||
// determine the magnitude ordering of x0, y0, z0 and w0.
|
||||
// The method below is a good way of finding the ordering of x,y,z,w and
|
||||
// then find the correct traversal order for the simplex we’re in.
|
||||
// First, six pair-wise comparisons are performed between each possible pair
|
||||
// of the four coordinates, and the results are used to add up binary bits
|
||||
// for an integer index.
|
||||
c1 = (x0 > y0) ? 32 : 0;
|
||||
c2 = (x0 > z0) ? 16 : 0;
|
||||
c3 = (y0 > z0) ? 8 : 0;
|
||||
c4 = (x0 > w0) ? 4 : 0;
|
||||
c5 = (y0 > w0) ? 2 : 0;
|
||||
c6 = (z0 > w0) ? 1 : 0;
|
||||
c = c1 + c2 + c3 + c4 + c5 + c6;
|
||||
i1, j1, k1, l1; // The integer offsets for the second simplex corner
|
||||
i2, j2, k2, l2; // The integer offsets for the third simplex corner
|
||||
i3, j3, k3, l3; // The integer offsets for the fourth simplex corner
|
||||
|
||||
// simplex[c] is a 4-vector with the numbers 0, 1, 2 and 3 in some order.
|
||||
// Many values of c will never occur, since e.g. x>y>z>w makes x<z, y<w and x<w
|
||||
// impossible. Only the 24 indices which have non-zero entries make any sense.
|
||||
// We use a thresholding to set the coordinates in turn from the largest magnitude.
|
||||
|
||||
// The number 3 in the "simplex" array is at the position of the largest coordinate.
|
||||
i1 = simplex[c][0] >= 3 ? 1 : 0;
|
||||
j1 = simplex[c][1] >= 3 ? 1 : 0;
|
||||
k1 = simplex[c][2] >= 3 ? 1 : 0;
|
||||
l1 = simplex[c][3] >= 3 ? 1 : 0;
|
||||
|
||||
// The number 2 in the "simplex" array is at the second largest coordinate.
|
||||
i2 = simplex[c][0] >= 2 ? 1 : 0;
|
||||
j2 = simplex[c][1] >= 2 ? 1 : 0;
|
||||
k2 = simplex[c][2] >= 2 ? 1 : 0;
|
||||
l2 = simplex[c][3] >= 2 ? 1 : 0;
|
||||
|
||||
// The number 1 in the "simplex" array is at the second smallest coordinate.
|
||||
i3 = simplex[c][0] >= 1 ? 1 : 0;
|
||||
j3 = simplex[c][1] >= 1 ? 1 : 0;
|
||||
k3 = simplex[c][2] >= 1 ? 1 : 0;
|
||||
l3 = simplex[c][3] >= 1 ? 1 : 0;
|
||||
|
||||
// The fifth corner has all coordinate offsets = 1, so no need to look that up.
|
||||
|
||||
x1 = x0 - i1 + self::$G4; // Offsets for second corner in (x,y,z,w) coords
|
||||
y1 = y0 - j1 + self::$G4;
|
||||
z1 = z0 - k1 + self::$G4;
|
||||
w1 = w0 - l1 + self::$G4;
|
||||
|
||||
x2 = x0 - i2 + self::$G42; // Offsets for third corner in (x,y,z,w) coords
|
||||
y2 = y0 - j2 + self::$G42;
|
||||
z2 = z0 - k2 + self::$G42;
|
||||
w2 = w0 - l2 + self::$G42;
|
||||
|
||||
x3 = x0 - i3 + self::$G43; // Offsets for fourth corner in (x,y,z,w) coords
|
||||
y3 = y0 - j3 + self::$G43;
|
||||
z3 = z0 - k3 + self::$G43;
|
||||
w3 = w0 - l3 + self::$G43;
|
||||
|
||||
x4 = x0 + self::$G44; // Offsets for last corner in (x,y,z,w) coords
|
||||
y4 = y0 + self::$G44;
|
||||
z4 = z0 + self::$G44;
|
||||
w4 = w0 + self::$G44;
|
||||
|
||||
// Work out the hashed gradient indices of the five simplex corners
|
||||
ii = i & 255;
|
||||
jj = j & 255;
|
||||
kk = k & 255;
|
||||
ll = l & 255;
|
||||
|
||||
gi0 = $this->perm[ii + $this->perm[jj + $this->perm[kk + $this->perm[ll]]]] % 32;
|
||||
gi1 = $this->perm[ii + i1 + $this->perm[jj + j1 + $this->perm[kk + k1 + $this->perm[ll + l1]]]] % 32;
|
||||
gi2 = $this->perm[ii + i2 + $this->perm[jj + j2 + $this->perm[kk + k2 + $this->perm[ll + l2]]]] % 32;
|
||||
gi3 = $this->perm[ii + i3 + $this->perm[jj + j3 + $this->perm[kk + k3 + $this->perm[ll + l3]]]] % 32;
|
||||
gi4 = $this->perm[ii + 1 + $this->perm[jj + 1 + $this->perm[kk + 1 + $this->perm[ll + 1]]]] % 32;
|
||||
|
||||
// Calculate the contribution from the five corners
|
||||
t0 = 0.6 - x0 * x0 - y0 * y0 - z0 * z0 - w0 * w0;
|
||||
if(t0 < 0){
|
||||
n0 = 0.0;
|
||||
}else{
|
||||
t0 *= t0;
|
||||
n0 = t0 * t0 * dot(grad4[gi0], x0, y0, z0, w0);
|
||||
}
|
||||
|
||||
t1 = 0.6 - x1 * x1 - y1 * y1 - z1 * z1 - w1 * w1;
|
||||
if(t1 < 0){
|
||||
n1 = 0.0;
|
||||
}else{
|
||||
t1 *= t1;
|
||||
n1 = t1 * t1 * dot(grad4[gi1], x1, y1, z1, w1);
|
||||
}
|
||||
|
||||
t2 = 0.6 - x2 * x2 - y2 * y2 - z2 * z2 - w2 * w2;
|
||||
if(t2 < 0){
|
||||
n2 = 0.0;
|
||||
}else{
|
||||
t2 *= t2;
|
||||
n2 = t2 * t2 * dot(grad4[gi2], x2, y2, z2, w2);
|
||||
}
|
||||
|
||||
t3 = 0.6 - x3 * x3 - y3 * y3 - z3 * z3 - w3 * w3;
|
||||
if(t3 < 0){
|
||||
n3 = 0.0;
|
||||
}else{
|
||||
t3 *= t3;
|
||||
n3 = t3 * t3 * dot(grad4[gi3], x3, y3, z3, w3);
|
||||
}
|
||||
|
||||
t4 = 0.6 - x4 * x4 - y4 * y4 - z4 * z4 - w4 * w4;
|
||||
if(t4 < 0){
|
||||
n4 = 0.0;
|
||||
}else{
|
||||
t4 *= t4;
|
||||
n4 = t4 * t4 * dot(grad4[gi4], x4, y4, z4, w4);
|
||||
}
|
||||
|
||||
// Sum up and scale the result to cover the range [-1,1]
|
||||
return 27.0 * (n0 + n1 + n2 + n3 + n4);
|
||||
}*/
|
||||
}
|
||||
108
src/world/generator/noise/OctaveGenerator.php
Normal file
108
src/world/generator/noise/OctaveGenerator.php
Normal file
|
|
@ -0,0 +1,108 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
*
|
||||
* ____ _ _ __ __ _ __ __ ____
|
||||
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
|
||||
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
|
||||
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
|
||||
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* @author PocketMine Team
|
||||
* @link http://www.pocketmine.net/
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
abstract class OctaveGenerator{
|
||||
protected $octaves;
|
||||
protected $xScale = 1;
|
||||
protected $yScale = 1;
|
||||
protected $zScale = 1;
|
||||
|
||||
public function __construct(array $octaves){
|
||||
$this->octaves = $octaves;
|
||||
}
|
||||
|
||||
public function setScale($scale){
|
||||
$this->setXScale($scale);
|
||||
$this->setYScale($scale);
|
||||
$this->setZScale($scale);
|
||||
}
|
||||
|
||||
public function getXScale(){
|
||||
return $this->xScale;
|
||||
}
|
||||
|
||||
public function setXScale($scale){
|
||||
$this->xScale = $scale;
|
||||
}
|
||||
|
||||
public function getYScale(){
|
||||
return $this->yScale;
|
||||
}
|
||||
|
||||
public function setYScale($scale){
|
||||
$this->yScale = $scale;
|
||||
}
|
||||
|
||||
public function getZScale(){
|
||||
return $this->zScale;
|
||||
}
|
||||
|
||||
public function setZScale($scale){
|
||||
$this->zScale = $scale;
|
||||
}
|
||||
|
||||
public function getOctaves(){
|
||||
$array = array();
|
||||
foreach($this->octaves as $index => $value){
|
||||
$array[$index] = clone $value;
|
||||
}
|
||||
return $array;
|
||||
}
|
||||
|
||||
//1D-noise
|
||||
public function noise1D($x, $frequency, $amplitude, $normalized = false){
|
||||
return $this->noise3D($x, 0, 0, $frequency, $amplitude, $normalized);
|
||||
}
|
||||
|
||||
//2D-noise
|
||||
public function noise2D($x, $y, $frequency, $amplitude, $normalized = false){
|
||||
return $this->noise3D($x, $y, 0, $frequency, $amplitude, $normalized);
|
||||
}
|
||||
|
||||
//3D-noise
|
||||
public function noise3D($x, $y, $z, $frequency, $amplitude, $normalized = false){
|
||||
$result = 0;
|
||||
$amp = 1;
|
||||
$freq = 1;
|
||||
$max = 0;
|
||||
|
||||
$x *= $this->xScale;
|
||||
$y *= $this->yScale;
|
||||
$z *= $this->zScale;
|
||||
|
||||
foreach($this->octaves as $noiseGenerator){
|
||||
$result += $octave->noise($x * $freq, $y * $freq, $z * $freq) * $amp;
|
||||
$max += $amp;
|
||||
$freq *= $frequency;
|
||||
$amp *= $amplitude;
|
||||
}
|
||||
if($normalized === true){
|
||||
$result /= $max;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/*public function generateNoiseOctaves($x, $y, $z, $frequency, $amplitude){
|
||||
|
||||
}*/
|
||||
}
|
||||
63
src/world/generator/noise/PerlinOctaveGenerator.php
Normal file
63
src/world/generator/noise/PerlinOctaveGenerator.php
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
*
|
||||
* ____ _ _ __ __ _ __ __ ____
|
||||
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
|
||||
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
|
||||
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
|
||||
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* @author PocketMine Team
|
||||
* @link http://www.pocketmine.net/
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
/***REM_START***/
|
||||
require_once("OctaveGenerator.php");
|
||||
/***REM_END***/
|
||||
|
||||
class PerlinOctaveGenerator extends OctaveGenerator{
|
||||
public function __construct(Random $random, $octaves){
|
||||
$this->octaves = array();
|
||||
for($o = 0; $o < $octaves; ++$o){
|
||||
$this->octaves[$o] = new NoiseGeneratorPerlin($random);
|
||||
}
|
||||
}
|
||||
|
||||
/*public function generateNoiseOctaves($x, $y, $z, $sizeX, $sizeY, $sizeZ, $fX, $fY, $fZ){
|
||||
$adouble = array_fill(0, $sizeX * $sizeY * $sizeZ, 0.0);
|
||||
|
||||
$d3 = 1.0;
|
||||
|
||||
foreach($this->octaves as $octave){
|
||||
$dX = $x * $d3 * $fX;
|
||||
$dY = $y * $d3 * $fY;
|
||||
$dZ = $x * $d3 * $fZ;
|
||||
|
||||
$x1 = NoiseGenerator::floor($dX);
|
||||
$z1 = NoiseGenerator::floor($dZ);
|
||||
|
||||
$dX -= $x1;
|
||||
$dZ -= $z1;
|
||||
|
||||
$x1 %= 16777216;
|
||||
$z1 %= 16777216;
|
||||
//$x1 &= 0xFFFFFF;
|
||||
//$z1 &= 0xFFFFFF;
|
||||
|
||||
$dX += $x1;
|
||||
$dZ += $z1;
|
||||
$octave->populateNoiseArray($adouble, $dX, $dY, $dZ, $sizeX, $sizeY, $sizeZ, $fX * $d3, $fY * $d3, $fZ * $d3, $d3);
|
||||
$d3 *= 0.5;
|
||||
}
|
||||
|
||||
return $adouble;
|
||||
}*/
|
||||
}
|
||||
|
|
@ -23,11 +23,11 @@
|
|||
class TallGrassObject{
|
||||
public static function growGrass(Level $level, Vector3 $pos, Random $random, $count = 15, $radius = 10){
|
||||
$arr = array(
|
||||
BlockAPI::get(DANDELION, 0),
|
||||
BlockAPI::get(CYAN_FLOWER, 0),
|
||||
BlockAPI::get(TALL_GRASS, 1),
|
||||
BlockAPI::get(37, 0),
|
||||
BlockAPI::get(38, 0),
|
||||
BlockAPI::get(TALL_GRASS, 1),
|
||||
BlockAPI::get(TALL_GRASS, 2),
|
||||
BlockAPI::get(TALL_GRASS, 1),
|
||||
BlockAPI::get(TALL_GRASS, 1)
|
||||
);
|
||||
$arrC = count($arr) - 1;
|
||||
|
|
|
|||
43
src/world/generator/object/grass/TallGrassObject.php.bak
Normal file
43
src/world/generator/object/grass/TallGrassObject.php.bak
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
*
|
||||
* ____ _ _ __ __ _ __ __ ____
|
||||
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
|
||||
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
|
||||
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
|
||||
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* @author PocketMine Team
|
||||
* @link http://www.pocketmine.net/
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
class TallGrassObject{
|
||||
public static function growGrass(Level $level, Vector3 $pos, Random $random, $count = 15, $radius = 10){
|
||||
$arr = array(
|
||||
BlockAPI::get(DANDELION, 0),
|
||||
BlockAPI::get(CYAN_FLOWER, 0),
|
||||
BlockAPI::get(TALL_GRASS, 1),
|
||||
BlockAPI::get(TALL_GRASS, 2),
|
||||
BlockAPI::get(TALL_GRASS, 1),
|
||||
BlockAPI::get(TALL_GRASS, 1)
|
||||
);
|
||||
$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);
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -25,7 +25,7 @@ require_once("TreeObject.php");
|
|||
|
||||
class SmallTreeObject extends TreeObject{
|
||||
public $type = 0;
|
||||
private $trunkHeight = 5;
|
||||
private $trunkHeight = 4;
|
||||
private static $leavesHeight = 4; // All trees appear to be 4 tall
|
||||
private static $leafRadii = array( 1, 1.41, 2.83, 2.24 );
|
||||
|
||||
|
|
@ -76,7 +76,7 @@ class SmallTreeObject extends TreeObject{
|
|||
{
|
||||
// The size is a slight variation on the trunkheight
|
||||
$radius = self::$leafRadii[ $leaflevel ] + $leafPre;
|
||||
$bRadius = 3;
|
||||
$bRadius = 2;
|
||||
for( $xx = -$bRadius; $xx <= $bRadius; ++ $xx )
|
||||
{
|
||||
for( $zz = -$bRadius; $zz <= $bRadius; ++ $zz )
|
||||
|
|
|
|||
104
src/world/generator/object/tree/SmallTreeObject.php.bak
Normal file
104
src/world/generator/object/tree/SmallTreeObject.php.bak
Normal file
|
|
@ -0,0 +1,104 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
*
|
||||
* ____ _ _ __ __ _ __ __ ____
|
||||
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
|
||||
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
|
||||
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
|
||||
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* @author PocketMine Team
|
||||
* @link http://www.pocketmine.net/
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
/***REM_START***/
|
||||
require_once("TreeObject.php");
|
||||
/***REM_END***/
|
||||
|
||||
class SmallTreeObject extends TreeObject{
|
||||
public $type = 0;
|
||||
private $trunkHeight = 5;
|
||||
private static $leavesHeight = 4; // All trees appear to be 4 tall
|
||||
private static $leafRadii = array( 1, 1.41, 2.83, 2.24 );
|
||||
|
||||
private $addLeavesVines = false;
|
||||
private $addLogVines = false;
|
||||
private $addCocoaPlants = false;
|
||||
|
||||
public function canPlaceObject(Level $level, Vector3 $pos, Random $random){
|
||||
$radiusToCheck = 0;
|
||||
for ($yy = 0; $yy < $this->trunkHeight + 3; ++$yy) {
|
||||
if($yy == 1 or $yy === $this->trunkHeight) {
|
||||
++$radiusToCheck;
|
||||
}
|
||||
for($xx = -$radiusToCheck; $xx < ($radiusToCheck + 1); ++$xx){
|
||||
for($zz = -$radiusToCheck; $zz < ($radiusToCheck + 1); ++$zz){
|
||||
if(!isset($this->overridable[$level->level->getBlockID($pos->x + $xx, $pos->y + $yy, $pos->z + $zz)])){
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public function placeObject(Level $level, Vector3 $pos, Random $random){
|
||||
// The base dirt block
|
||||
$dirtpos = new Vector3( $pos->x, $pos->y - 1, $pos->z );
|
||||
$level->setBlockRaw( $dirtpos, new DirtBlock() );
|
||||
|
||||
// Adjust the tree trunk's height randomly
|
||||
// plot [-14:11] int( x / 8 ) + 5
|
||||
// - min=4 (all leaves are 4 tall, some trunk must show)
|
||||
// - max=6 (top leaves are within ground-level whacking range
|
||||
// on all small trees)
|
||||
$heightPre = $random->nextRange(-14, 11);
|
||||
$this->trunkHeight = intval( $heightPre / 8 ) + 5;
|
||||
|
||||
// Adjust the starting leaf density using the trunk height as a
|
||||
// starting position (tall trees with skimpy leaves don't look
|
||||
// too good)
|
||||
$leafPre = $random->nextRange($this->trunkHeight, 10) / 20; // (TODO: seed may apply)
|
||||
|
||||
// Now build the tree (from the top down)
|
||||
$leaflevel = 0;
|
||||
for( $yy = ($this->trunkHeight + 1); $yy >= 0; --$yy )
|
||||
{
|
||||
if( $leaflevel < self::$leavesHeight )
|
||||
{
|
||||
// The size is a slight variation on the trunkheight
|
||||
$radius = self::$leafRadii[ $leaflevel ] + $leafPre;
|
||||
$bRadius = 2;
|
||||
for( $xx = -$bRadius; $xx <= $bRadius; ++ $xx )
|
||||
{
|
||||
for( $zz = -$bRadius; $zz <= $bRadius; ++ $zz )
|
||||
{
|
||||
if( sqrt(($xx * $xx) + ($zz * $zz)) <= $radius )
|
||||
{
|
||||
$leafpos = new Vector3( $pos->x + $xx,
|
||||
$pos->y + $yy,
|
||||
$pos->z + $zz );
|
||||
$level->setBlockRaw($leafpos, new LeavesBlock( $this->type ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
$leaflevel ++;
|
||||
}
|
||||
|
||||
// Place the trunk last
|
||||
if($leaflevel > 1)
|
||||
{
|
||||
$trunkpos = new Vector3( $pos->x, $pos->y + $yy, $pos->z );
|
||||
$level->setBlockRaw($trunkpos, new WoodBlock( $this->type ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -23,6 +23,8 @@
|
|||
class TreeObject{
|
||||
public $overridable = array(
|
||||
0 => true,
|
||||
2 => true,
|
||||
3 => true,
|
||||
6 => true,
|
||||
17 => true,
|
||||
18 => true,
|
||||
|
|
|
|||
|
|
@ -32,4 +32,4 @@ class MineshaftPopulator extends Populator{
|
|||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -25,9 +25,9 @@ class OrePopulator extends Populator{
|
|||
foreach($this->oreTypes as $type){
|
||||
$ore = new OreObject($random, $type);
|
||||
for($i = 0; $i < $ore->type->clusterCount; ++$i){
|
||||
$x = $random->nextRange($chunkX << 4, ($chunkX << 4) + 16);
|
||||
$x = $random->nextRange($chunkX << 4, ($chunkX << 4) + 15);
|
||||
$y = $random->nextRange($ore->type->minHeight, $ore->type->maxHeight);
|
||||
$z = $random->nextRange($chunkZ << 4, ($chunkZ << 4) + 16);
|
||||
$z = $random->nextRange($chunkZ << 4, ($chunkZ << 4) + 15);
|
||||
if($ore->canPlaceObject($level, $x, $y, $z)){
|
||||
$ore->placeObject($level, new Vector3($x, $y, $z));
|
||||
}
|
||||
|
|
|
|||
75
src/world/generator/populator/TallGrassPopulator.php
Normal file
75
src/world/generator/populator/TallGrassPopulator.php
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
*
|
||||
* ____ _ _ __ __ _ __ __ ____
|
||||
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
|
||||
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
|
||||
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
|
||||
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* @author PocketMine Team
|
||||
* @link http://www.pocketmine.net/
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
class TallGrassPopulator extends Populator{
|
||||
private $level;
|
||||
private $randomAmount;
|
||||
private $baseAmount;
|
||||
|
||||
public function setRandomAmount($amount){
|
||||
$this->randomAmount = $amount;
|
||||
}
|
||||
|
||||
public function setBaseAmount($amount){
|
||||
$this->baseAmount = $amount;
|
||||
}
|
||||
|
||||
public function populate(Level $level, $chunkX, $chunkZ, Random $random){
|
||||
$this->level = $level;
|
||||
$amount = $random->nextRange(0, $this->randomAmount + 1) + $this->baseAmount;
|
||||
for($i = 0; $i < $amount; ++$i){
|
||||
$x = $random->nextRange($chunkX << 4, ($chunkX << 4) + 15);
|
||||
$z = $random->nextRange($chunkZ << 4, ($chunkZ << 4) + 15);
|
||||
for($size = 30; $size > 0; --$size){
|
||||
$xx = $x - 7 + $random->nextRange(0, 15);
|
||||
$zz = $z - 7 + $random->nextRange(0, 15);
|
||||
$yy = $this->getHighestWorkableBlock($xx, $zz);
|
||||
$vector = new Vector3($xx, $yy, $zz);
|
||||
if($yy !== -1 and $this->canTallGrassStay($this->level->getBlockRaw($vector))){
|
||||
if(mt_rand(1,4) == 1){
|
||||
$this->level->setBlockRaw($vector, new TallGrassBlock(2));
|
||||
}else{
|
||||
$this->level->setBlockRaw($vector, new TallGrassBlock(1));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function canTallGrassStay(Block $block){
|
||||
return $block->getID() === AIR and $block->getSide(0)->getID() === GRASS;
|
||||
}
|
||||
|
||||
private function getHighestWorkableBlock($x, $z){
|
||||
for($y = 128; $y > 0; --$y){
|
||||
$b = $this->level->getBlockRaw(new Vector3($x, $y, $z));
|
||||
if($b->getID() === AIR or $b->getID() === LEAVES){
|
||||
if(--$y <= 0){
|
||||
return -1;
|
||||
}
|
||||
}else{
|
||||
break;
|
||||
}
|
||||
}
|
||||
return ++$y;
|
||||
}
|
||||
}
|
||||
75
src/world/generator/populator/TallGrassPopulator.php.bak
Normal file
75
src/world/generator/populator/TallGrassPopulator.php.bak
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
*
|
||||
* ____ _ _ __ __ _ __ __ ____
|
||||
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
|
||||
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
|
||||
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
|
||||
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* @author PocketMine Team
|
||||
* @link http://www.pocketmine.net/
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
class TallGrassPopulator extends Populator{
|
||||
private $level;
|
||||
private $randomAmount;
|
||||
private $baseAmount;
|
||||
|
||||
public function setRandomAmount($amount){
|
||||
$this->randomAmount = $amount;
|
||||
}
|
||||
|
||||
public function setBaseAmount($amount){
|
||||
$this->baseAmount = $amount;
|
||||
}
|
||||
|
||||
public function populate(Level $level, $chunkX, $chunkZ, Random $random){
|
||||
$this->level = $level;
|
||||
$amount = $random->nextRange(0, $this->randomAmount + 1) + $this->baseAmount;
|
||||
for($i = 0; $i < $amount; ++$i){
|
||||
$x = $random->nextRange($chunkX << 4, ($chunkX << 4) + 15);
|
||||
$z = $random->nextRange($chunkZ << 4, ($chunkZ << 4) + 15);
|
||||
for($size = 30; $size > 0; --$size){
|
||||
$xx = $x - 7 + $random->nextRange(0, 15);
|
||||
$zz = $z - 7 + $random->nextRange(0, 15);
|
||||
$yy = $this->getHighestWorkableBlock($xx, $zz);
|
||||
$vector = new Vector3($xx, $yy, $zz);
|
||||
if($yy !== -1 and $this->canTallGrassStay($this->level->getBlockRaw($vector))){
|
||||
if(mt_rand(1,4) == 1){
|
||||
$this->level->setBlockRaw($vector, new TallGrassBlock(2));
|
||||
}else{
|
||||
$this->level->setBlockRaw($vector, new TallGrassBlock(1);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function canTallGrassStay(Block $block){
|
||||
return $block->getID() === AIR and $block->getSide(0)->getID() === GRASS;
|
||||
}
|
||||
|
||||
private function getHighestWorkableBlock($x, $z){
|
||||
for($y = 128; $y > 0; --$y){
|
||||
$b = $this->level->getBlockRaw(new Vector3($x, $y, $z));
|
||||
if($b->getID() === AIR or $b->getID() === LEAVES){
|
||||
if(--$y <= 0){
|
||||
return -1;
|
||||
}
|
||||
}else{
|
||||
break;
|
||||
}
|
||||
}
|
||||
return ++$y;
|
||||
}
|
||||
}
|
||||
69
src/world/generator/populator/TreePopulator.php
Normal file
69
src/world/generator/populator/TreePopulator.php
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
*
|
||||
* ____ _ _ __ __ _ __ __ ____
|
||||
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
|
||||
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
|
||||
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
|
||||
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* @author PocketMine Team
|
||||
* @link http://www.pocketmine.net/
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
class TreePopulator extends Populator{
|
||||
private $level;
|
||||
private $randomAmount;
|
||||
private $baseAmount;
|
||||
|
||||
public function setRandomAmount($amount){
|
||||
$this->randomAmount = $amount;
|
||||
}
|
||||
|
||||
public function setBaseAmount($amount){
|
||||
$this->baseAmount = $amount;
|
||||
}
|
||||
|
||||
public function populate(Level $level, $chunkX, $chunkZ, Random $random){
|
||||
$this->level = $level;
|
||||
$amount = $random->nextRange(0, $this->randomAmount + 1) + $this->baseAmount;
|
||||
for($i = 0; $i < $amount; ++$i){
|
||||
$x = $random->nextRange($chunkX << 4, ($chunkX << 4) + 15);
|
||||
$z = $random->nextRange($chunkZ << 4, ($chunkZ << 4) + 15);
|
||||
$y = $this->getHighestWorkableBlock($x, $z);
|
||||
if($y === -1){
|
||||
continue;
|
||||
}
|
||||
if($random->nextFloat() > 0.75){
|
||||
$meta = SaplingBlock::BIRCH;
|
||||
}elseif(($random->nextFloat() < 0.75) and ($random->nextFloat() > 0.25)){
|
||||
$meta = SaplingBlock::OAK;
|
||||
}else{
|
||||
$meta = SaplingBlock::JUNGLE;
|
||||
}
|
||||
TreeObject::growTree($this->level, new Vector3($x, $y, $z), $random, $meta);
|
||||
}
|
||||
}
|
||||
|
||||
private function getHighestWorkableBlock($x, $z){
|
||||
for($y = 128; $y > 0; --$y){
|
||||
$b = $this->level->getBlockRaw(new Vector3($x, $y, $z));
|
||||
if($b->getID() !== DIRT and $b->getID() !== GRASS){
|
||||
if(--$y <= 0){
|
||||
return -1;
|
||||
}
|
||||
}else{
|
||||
break;
|
||||
}
|
||||
}
|
||||
return ++$y;
|
||||
}
|
||||
}
|
||||
69
src/world/generator/populator/TreePopulator.php.bak
Normal file
69
src/world/generator/populator/TreePopulator.php.bak
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
*
|
||||
* ____ _ _ __ __ _ __ __ ____
|
||||
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
|
||||
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
|
||||
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
|
||||
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* @author PocketMine Team
|
||||
* @link http://www.pocketmine.net/
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
class TreePopulator extends Populator{
|
||||
private $level;
|
||||
private $randomAmount;
|
||||
private $baseAmount;
|
||||
|
||||
public function setRandomAmount($amount){
|
||||
$this->randomAmount = $amount;
|
||||
}
|
||||
|
||||
public function setBaseAmount($amount){
|
||||
$this->baseAmount = $amount;
|
||||
}
|
||||
|
||||
public function populate(Level $level, $chunkX, $chunkZ, Random $random){
|
||||
$this->level = $level;
|
||||
$amount = $random->nextRange(0, $this->randomAmount + 1) + $this->baseAmount;
|
||||
for($i = 0; $i < $amount; ++$i){
|
||||
$x = $random->nextRange($chunkX << 4, ($chunkX << 4) + 15);
|
||||
$z = $random->nextRange($chunkZ << 4, ($chunkZ << 4) + 15);
|
||||
$y = $this->getHighestWorkableBlock($x, $z);
|
||||
if($y === -1){
|
||||
continue;
|
||||
}
|
||||
if($random->nextFloat() > 0.75){
|
||||
$meta = SaplingBlock::BIRCH;
|
||||
}elseif(($random->nextFloat() < 0.75) and ($random->nextFloat() > 0.25)){
|
||||
$meta = SaplingBlock::OAK;
|
||||
}else{
|
||||
$meta = SaplingBlock::JUNGLE;
|
||||
}
|
||||
TreeObject::growTree($this->level, new Vector3($x, $y, $z), $random, $meta);
|
||||
}
|
||||
}
|
||||
|
||||
private function getHighestWorkableBlock($x, $z){
|
||||
for($y = 128; $y > 0; --$y){
|
||||
$b = $this->level->getBlockRaw(new Vector3($x, $y, $z));
|
||||
if($b->getID() !== DIRT and $b->getID() !== GRASS){
|
||||
if(--$y <= 0){
|
||||
return -1;
|
||||
}
|
||||
}else{
|
||||
break;
|
||||
}
|
||||
}
|
||||
return ++$y;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue