Skip to content
This repository has been archived by the owner on Feb 9, 2021. It is now read-only.

Commit

Permalink
Yay generation uses async tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
shoghicp committed Mar 26, 2015
1 parent 668ddee commit 72c4c01
Show file tree
Hide file tree
Showing 18 changed files with 287 additions and 1,124 deletions.
2 changes: 1 addition & 1 deletion src/pocketmine/Player.php
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@ protected function sendNextChunk(){
$Z = null;
Level::getXZ($index, $X, $Z);

if(!$this->level->populateChunk($X, $Z, !$this->spawned or count($this->usedChunks) < ($this->viewDistance - 24))){
if(!$this->level->populateChunk($X, $Z)){
if($this->spawned){
continue;
}else{
Expand Down
36 changes: 1 addition & 35 deletions src/pocketmine/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@
use pocketmine\plugin\PluginLoadOrder;
use pocketmine\plugin\PluginManager;
use pocketmine\scheduler\CallbackTask;
use pocketmine\scheduler\GarbageCollectionTask;
use pocketmine\scheduler\SendUsageTask;
use pocketmine\scheduler\ServerScheduler;
use pocketmine\tile\Chest;
Expand All @@ -100,7 +99,6 @@
use pocketmine\tile\Tile;
use pocketmine\updater\AutoUpdater;
use pocketmine\utils\Binary;
use pocketmine\utils\Cache;
use pocketmine\utils\Config;
use pocketmine\utils\LevelException;
use pocketmine\utils\MainLogger;
Expand Down Expand Up @@ -147,9 +145,6 @@ class Server{
/** @var ServerScheduler */
private $scheduler = null;

/** @var GenerationRequestManager */
private $generationManager = null;

/**
* Counts the ticks since the server start
*
Expand Down Expand Up @@ -569,13 +564,6 @@ public function getScheduler(){
return $this->scheduler;
}

/**
* @return GenerationRequestManager
*/
public function getGenerationManager(){
return $this->generationManager;
}

/**
* @return int
*/
Expand Down Expand Up @@ -1192,7 +1180,7 @@ public function generateLevel($name, $seed = null, $generator = null, $options =

foreach($order as $index => $distance){
Level::getXZ($index, $chunkX, $chunkZ);
$level->generateChunk($chunkX, $chunkZ);
$level->generateChunk($chunkX, $chunkZ, true);
}

return true;
Expand Down Expand Up @@ -1624,12 +1612,6 @@ public function __construct(\ClassLoader $autoloader, \ThreadedLogger $logger, $

$this->enablePlugins(PluginLoadOrder::STARTUP);

if($this->getProperty("chunk-generation.use-async", true)){
$this->generationManager = new GenerationRequestManager($this);
}else{
$this->generationManager = new GenerationInstanceManager($this);
}

LevelProviderManager::addProvider($this, Anvil::class);
LevelProviderManager::addProvider($this, McRegion::class);
if(extension_loaded("leveldb")){
Expand Down Expand Up @@ -1692,8 +1674,6 @@ public function __construct(\ClassLoader $autoloader, \ThreadedLogger $logger, $
$this->scheduler->scheduleDelayedRepeatingTask(new CallbackTask([$this, "doLevelGC"]), $this->getProperty("chunk-gc.period-in-ticks", 600), $this->getProperty("chunk-gc.period-in-ticks", 600));
}

$this->scheduler->scheduleRepeatingTask(new GarbageCollectionTask(), 900);

$this->enablePlugins(PluginLoadOrder::POSTWORLD);

$this->start();
Expand Down Expand Up @@ -1967,10 +1947,6 @@ public function forceShutdown(){
$this->unloadLevel($level, true);
}

if($this->generationManager instanceof GenerationRequestManager){
$this->generationManager->shutdown();
}

HandlerList::unregisterAll();

$this->scheduler->cancelAllTasks();
Expand Down Expand Up @@ -2368,16 +2344,6 @@ private function tick(){
}
}

Timings::$generationTimer->startTiming();
try{
$this->generationManager->process();
}catch(\Exception $e){
if($this->logger instanceof MainLogger){
$this->logger->logException($e);
}
}
Timings::$generationTimer->stopTiming();

if(($this->tickCounter % 100) === 0){
foreach($this->levels as $level){
$level->clearCache();
Expand Down
2 changes: 1 addition & 1 deletion src/pocketmine/level/ChunkManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public function getChunk($chunkX, $chunkZ);
* @param int $chunkZ
* @param FullChunk $chunk
*/
public function setChunk($chunkX, $chunkZ, FullChunk $chunk);
public function setChunk($chunkX, $chunkZ, FullChunk $chunk = null);

/**
* Gets the level seed
Expand Down
24 changes: 14 additions & 10 deletions src/pocketmine/level/Level.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
use pocketmine\level\format\generic\BaseLevelProvider;
use pocketmine\level\format\generic\EmptyChunkSection;
use pocketmine\level\format\LevelProvider;
use pocketmine\level\generator\GenerationTask;
use pocketmine\level\generator\Generator;
use pocketmine\math\AxisAlignedBB;
use pocketmine\math\Math;
Expand Down Expand Up @@ -173,6 +174,7 @@ class Level implements ChunkManager, Metadatable{
private $chunkSendTasks = [];

private $chunkGenerationQueue = [];
private $chunkGenerationQueueSize = 16;

private $autoSave = true;

Expand Down Expand Up @@ -306,6 +308,7 @@ public function __construct(Server $server, $name, $path, $provider){
$this->chunkTickRadius = min($this->server->getViewDistance(), max(1, (int) $this->server->getProperty("chunk-ticking.tick-radius", 4)));
$this->chunksPerTick = (int) $this->server->getProperty("chunk-ticking.per-tick", 260);
$this->chunksPopulatedPerTick = (int) $this->server->getProperty("chunk-generation.populations-per-tick", 1);
$this->chunkGenerationQueueSize = (int) $this->server->getProperty("chunk-generation.queue-size", 16);
$this->chunkTickList = [];
$this->clearChunksOnTick = (bool) $this->server->getProperty("chunk-ticking.clear-tick-list", false);

Expand All @@ -315,7 +318,6 @@ public function __construct(Server $server, $name, $path, $provider){
}

public function initLevel(){
$this->server->getGenerationManager()->openLevel($this, $this->generator, $this->provider->getGeneratorOptions());
$generator = $this->generator;
$this->generatorInstance = new $generator($this->provider->getGeneratorOptions());
$this->generatorInstance->init($this, new Random($this->getSeed()));
Expand Down Expand Up @@ -361,7 +363,6 @@ public function close(){
$this->unloadChunk($chunk->getX(), $chunk->getZ(), false);
}

$this->server->getGenerationManager()->closeLevel($this);
$this->provider->close();
$this->provider = null;
$this->blockMetadata = null;
Expand Down Expand Up @@ -1832,7 +1833,10 @@ public function generateChunkCallback($x, $z, FullChunk $chunk){
Timings::$generationTimer->stopTiming();
}

public function setChunk($x, $z, FullChunk $chunk, $unload = true){
public function setChunk($x, $z, FullChunk $chunk = null, $unload = true){
if($chunk === null){
return;
}
$index = Level::chunkHash($x, $z);
if($unload){
foreach($this->getUsingChunk($x, $z) as $player){
Expand Down Expand Up @@ -2313,14 +2317,14 @@ public function populateChunk($x, $z, $force = false){
}

public function generateChunk($x, $z, $force = false){
if(count($this->chunkGenerationQueue) >= $this->chunkGenerationQueueSize and !$force){
return;
}

if(!isset($this->chunkGenerationQueue[$index = Level::chunkHash($x, $z)])){
$this->chunkGenerationQueue[$index] = 0;
$this->server->getGenerationManager()->requestChunk($this, $x, $z, $this->getChunk($x, $z, true));
}elseif($force){
$value = ++$this->chunkGenerationQueue[$index];
if($value % 40 === 0){
$this->server->getGenerationManager()->requestChunk($this, $x, $z);
}
$this->chunkGenerationQueue[$index] = true;
$task = new GenerationTask($this, $this->generatorInstance, $this->getChunk($x, $z, true));
$this->server->getScheduler()->scheduleAsyncTask($task);
}
}

Expand Down
128 changes: 128 additions & 0 deletions src/pocketmine/level/SimpleChunkManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<?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/
*
*
*/

namespace pocketmine\level;

use pocketmine\level\format\FullChunk;

class SimpleChunkManager implements ChunkManager{

/** @var FullChunk[] */
protected $chunks = [];

protected $seed;

public function __construct($seed){
$this->seed = $seed;
}

/**
* Gets the raw block id.
*
* @param int $x
* @param int $y
* @param int $z
*
* @return int 0-255
*/
public function getBlockIdAt($x, $y, $z){
if($chunk = $this->getChunk($x >> 4, $z >> 4)){
return $chunk->getBlockId($x & 0xf, $y & 0x7f, $z & 0xf);
}
return 0;
}

/**
* Sets the raw block id.
*
* @param int $x
* @param int $y
* @param int $z
* @param int $id 0-255
*/
public function setBlockIdAt($x, $y, $z, $id){
if($chunk = $this->getChunk($x >> 4, $z >> 4)){
$chunk->setBlockId($x & 0xf, $y & 0x7f, $z & 0xf, $id);
}
}

/**
* Gets the raw block metadata
*
* @param int $x
* @param int $y
* @param int $z
*
* @return int 0-15
*/
public function getBlockDataAt($x, $y, $z){
if($chunk = $this->getChunk($x >> 4, $z >> 4)){
return $chunk->getBlockData($x & 0xf, $y & 0x7f, $z & 0xf);
}
return 0;
}

/**
* Sets the raw block metadata.
*
* @param int $x
* @param int $y
* @param int $z
* @param int $data 0-15
*/
public function setBlockDataAt($x, $y, $z, $data){
if($chunk = $this->getChunk($x >> 4, $z >> 4)){
$chunk->getBlockData($x & 0xf, $y & 0x7f, $z & 0xf, $data);
}
}

/**
* @param int $chunkX
* @param int $chunkZ
*
* @return FullChunk
*/
public function getChunk($chunkX, $chunkZ){
return isset($this->chunks[$index = Level::chunkHash($chunkX, $chunkZ)]) ? $this->chunks[$index] : null;
}

/**
* @param int $chunkX
* @param int $chunkZ
* @param FullChunk $chunk
*/
public function setChunk($chunkX, $chunkZ, FullChunk $chunk = null){
if($chunk === null){
unset($this->chunks[Level::chunkHash($chunkX, $chunkZ)]);
return;
}
$this->chunks[Level::chunkHash($chunkX, $chunkZ)] = $chunk;
}

/**
* Gets the level seed
*
* @return int
*/
public function getSeed(){
return $this->seed;
}
}
Loading

0 comments on commit 72c4c01

Please sign in to comment.