Fixed player spawning in ungenerated terrain (#4087)

fixes #4044
fixes #2724

this is significantly more complex than I hoped for, but it's a start... and it works.
This commit is contained in:
Dylan T
2021-03-26 21:36:27 +00:00
committed by GitHub
parent 64886707b2
commit f047ecfd2d
7 changed files with 355 additions and 75 deletions

View File

@ -0,0 +1,76 @@
<?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/
*
*
*/
declare(strict_types=1);
namespace pocketmine\world;
use function spl_object_id;
final class ChunkPopulationPromise{
/**
* @var \Closure[]
* @phpstan-var array<int, \Closure() : void>
*/
private array $onSuccess = [];
/**
* @var \Closure[]
* @phpstan-var array<int, \Closure() : void>
*/
private array $onFailure = [];
private ?bool $success = null;
/**
* @phpstan-param \Closure() : void $onSuccess
* @phpstan-param \Closure() : void $onFailure
*/
public function onCompletion(\Closure $onSuccess, \Closure $onFailure) : void{
if($this->success !== null){
$this->success ? $onSuccess() : $onFailure();
}else{
$this->onSuccess[spl_object_id($onSuccess)] = $onSuccess;
$this->onFailure[spl_object_id($onFailure)] = $onFailure;
}
}
public function resolve() : void{
$this->success = true;
foreach($this->onSuccess as $callback){
$callback();
}
$this->onSuccess = [];
$this->onFailure = [];
}
public function reject() : void{
$this->success = false;
foreach($this->onFailure as $callback){
$callback();
}
$this->onSuccess = [];
$this->onFailure = [];
}
public function isCompleted() : bool{
return $this->success !== null;
}
}

View File

@ -238,6 +238,16 @@ class World implements ChunkManager{
private $chunkLock = [];
/** @var int */
private $maxConcurrentChunkPopulationTasks = 2;
/**
* @var ChunkPopulationPromise[] chunkHash => promise
* @phpstan-var array<int, ChunkPopulationPromise>
*/
private array $chunkPopulationRequestMap = [];
/**
* @var \SplQueue (queue of chunkHashes)
* @phpstan-var \SplQueue<int>
*/
private \SplQueue $chunkPopulationRequestQueue;
/** @var bool[] */
private $generatorRegisteredWorkers = [];
@ -390,6 +400,20 @@ class World implements ChunkManager{
$this->server->getLogger()->info($this->server->getLanguage()->translateString("pocketmine.level.preparing", [$this->displayName]));
$this->generator = GeneratorManager::getInstance()->getGenerator($this->provider->getWorldData()->getGenerator(), true);
//TODO: validate generator options
$this->chunkPopulationRequestQueue = new \SplQueue();
$this->addOnUnloadCallback(function() : void{
$this->logger->debug("Cancelling unfulfilled generation requests");
foreach($this->chunkPopulationRequestMap as $chunkHash => $promise){
$promise->reject();
unset($this->chunkPopulationRequestMap[$chunkHash]);
}
if(count($this->chunkPopulationRequestMap) !== 0){
//TODO: this might actually get hit because generation rejection callbacks might try to schedule new
//requests, and we can't prevent that right now because there's no way to detect "unloading" state
throw new AssumptionFailedError("New generation requests scheduled during unload");
}
});
$this->folderName = $name;
@ -626,6 +650,10 @@ class World implements ChunkManager{
if(count($this->chunkLoaders[$chunkHash]) === 0){
unset($this->chunkLoaders[$chunkHash]);
$this->unloadChunkRequest($chunkX, $chunkZ, true);
if(isset($this->chunkPopulationRequestMap[$chunkHash]) && !isset($this->activeChunkPopulationTasks[$chunkHash])){
$this->chunkPopulationRequestMap[$chunkHash]->reject();
unset($this->chunkPopulationRequestMap[$chunkHash]);
}
}
if(--$this->loaderCounter[$loaderId] === 0){
@ -2005,9 +2033,33 @@ class World implements ChunkManager{
return isset($this->chunkLock[World::chunkHash($chunkX, $chunkZ)]);
}
private function drainPopulationRequestQueue() : void{
$failed = [];
while(count($this->activeChunkPopulationTasks) < $this->maxConcurrentChunkPopulationTasks && !$this->chunkPopulationRequestQueue->isEmpty()){
$nextChunkHash = $this->chunkPopulationRequestQueue->dequeue();
World::getXZ($nextChunkHash, $nextChunkX, $nextChunkZ);
if(isset($this->chunkPopulationRequestMap[$nextChunkHash])){
assert(!isset($this->activeChunkPopulationTasks[$nextChunkHash]), "Population for chunk $nextChunkX $nextChunkZ already running");
$this->logger->debug("Fulfilling population request for chunk $nextChunkX $nextChunkZ");
$this->orderChunkPopulation($nextChunkX, $nextChunkZ, null);
if(!isset($this->activeChunkPopulationTasks[$nextChunkHash])){
$failed[] = $nextChunkHash;
}
}else{
$this->logger->debug("Population request for chunk $nextChunkX $nextChunkZ was discarded before it could be fulfilled");
}
}
//these requests failed even though they weren't rate limited; we can't directly re-add them to the back of the
//queue because it would result in an infinite loop
foreach($failed as $hash){
$this->chunkPopulationRequestQueue->enqueue($hash);
}
}
public function generateChunkCallback(int $x, int $z, ?Chunk $chunk) : void{
Timings::$generationCallback->startTiming();
if(isset($this->activeChunkPopulationTasks[$index = World::chunkHash($x, $z)])){
if(isset($this->chunkPopulationRequestMap[$index = World::chunkHash($x, $z)]) && isset($this->activeChunkPopulationTasks[$index])){
if($chunk === null){
throw new AssumptionFailedError("Primary chunk should never be NULL");
}
@ -2016,7 +2068,6 @@ class World implements ChunkManager{
$this->unlockChunk($x + $xx, $z + $zz);
}
}
unset($this->activeChunkPopulationTasks[$index]);
$oldChunk = $this->loadChunk($x, $z);
$this->setChunk($x, $z, $chunk, false);
@ -2027,11 +2078,17 @@ class World implements ChunkManager{
$listener->onChunkPopulated($x, $z, $chunk);
}
}
unset($this->activeChunkPopulationTasks[$index]);
$this->chunkPopulationRequestMap[$index]->resolve();
unset($this->chunkPopulationRequestMap[$index]);
$this->drainPopulationRequestQueue();
}elseif($this->isChunkLocked($x, $z)){
$this->unlockChunk($x, $z);
if($chunk !== null){
$this->setChunk($x, $z, $chunk, false);
}
$this->drainPopulationRequestQueue();
}elseif($chunk !== null){
$this->setChunk($x, $z, $chunk, false);
}
@ -2464,6 +2521,11 @@ class World implements ChunkManager{
unset($this->blockCache[$chunkHash]);
unset($this->changedBlocks[$chunkHash]);
if(array_key_exists($chunkHash, $this->chunkPopulationRequestMap)){
$this->chunkPopulationRequestMap[$chunkHash]->reject();
unset($this->chunkPopulationRequestMap[$chunkHash]);
}
$this->timings->doChunkUnload->stopTiming();
return true;
@ -2605,43 +2667,64 @@ class World implements ChunkManager{
}
}
private function enqueuePopulationRequest(int $chunkX, int $chunkZ, ?ChunkLoader $associatedChunkLoader) : ChunkPopulationPromise{
$chunkHash = World::chunkHash($chunkX, $chunkZ);
$this->chunkPopulationRequestQueue->enqueue($chunkHash);
$promise = $this->chunkPopulationRequestMap[$chunkHash] = new ChunkPopulationPromise();
if($associatedChunkLoader === null){
$temporaryLoader = new class implements ChunkLoader{};
$this->registerChunkLoader($temporaryLoader, $chunkX, $chunkZ);
$promise->onCompletion(
static function() : void{},
fn() => $this->unregisterChunkLoader($temporaryLoader, $chunkX, $chunkZ)
);
}
return $promise;
}
/**
* Attempts to initiate asynchronous generation/population of the target chunk, if it's currently reasonable to do
* so (and if it isn't already generated/populated).
* If the generator is busy, the request will be put into a queue and delayed until a better time.
*
* This method can fail for the following reasons:
* - The generation queue for this world is currently full (intended to prevent CPU overload with non-essential generation)
* - The target chunk is already being generated/populated
* - The target chunk is locked for use by another async operation (usually population)
*
* @return bool whether the chunk has been successfully populated already
* TODO: the return values don't make a lot of sense, but currently stuff depends on them :<
* A ChunkLoader can be associated with the generation request to ensure that the generation request is cancelled if
* no loaders are attached to the target chunk. If no loader is provided, one will be assigned (and automatically
* removed when the generation request completes).
*/
public function requestChunkPopulation(int $chunkX, int $chunkZ) : bool{
if(count($this->activeChunkPopulationTasks) >= $this->maxConcurrentChunkPopulationTasks){
return false;
public function requestChunkPopulation(int $chunkX, int $chunkZ, ?ChunkLoader $associatedChunkLoader) : ChunkPopulationPromise{
$chunkHash = World::chunkHash($chunkX, $chunkZ);
$promise = $this->chunkPopulationRequestMap[$chunkHash] ?? null;
if($promise !== null && isset($this->activeChunkPopulationTasks[$chunkHash])){
//generation is already running
return $promise;
}
return $this->orderChunkPopulation($chunkX, $chunkZ);
if(count($this->activeChunkPopulationTasks) >= $this->maxConcurrentChunkPopulationTasks){
//too many chunks are already generating; delay resolution of the request until later
return $promise ?? $this->enqueuePopulationRequest($chunkX, $chunkZ, $associatedChunkLoader);
}
return $this->orderChunkPopulation($chunkX, $chunkZ, $associatedChunkLoader);
}
/**
* Initiates asynchronous generation/population of the target chunk, if it's not already generated/populated.
* If generation has already been requested for the target chunk, the promise for the already active request will be
* returned directly.
*
* This method can fail for the following reasons:
* - The target chunk is already being generated/populated
* - The target chunk is locked for use by another async operation (usually population)
*
* @return bool whether the chunk has been successfully populated already
* TODO: the return values don't make sense, but currently stuff depends on them :<
* If the chunk is currently locked (for example due to another chunk using it for async generation), the request
* will be queued and executed at the earliest opportunity.
*/
public function orderChunkPopulation(int $x, int $z) : bool{
if(isset($this->activeChunkPopulationTasks[$index = World::chunkHash($x, $z)])){
return false;
public function orderChunkPopulation(int $x, int $z, ?ChunkLoader $associatedChunkLoader) : ChunkPopulationPromise{
$index = World::chunkHash($x, $z);
$promise = $this->chunkPopulationRequestMap[$index] ?? null;
if($promise !== null && isset($this->activeChunkPopulationTasks[$index])){
//generation is already running
return $promise;
}
for($xx = -1; $xx <= 1; ++$xx){
for($zz = -1; $zz <= 1; ++$zz){
if($this->isChunkLocked($x + $xx, $z + $zz)){
return false;
//chunk is already in use by another generation request; queue the request for later
return $promise ?? $this->enqueuePopulationRequest($x, $z, $associatedChunkLoader);
}
}
}
@ -2651,6 +2734,11 @@ class World implements ChunkManager{
Timings::$population->startTiming();
$this->activeChunkPopulationTasks[$index] = true;
if($promise === null){
$promise = new ChunkPopulationPromise();
$this->chunkPopulationRequestMap[$index] = $promise;
}
for($xx = -1; $xx <= 1; ++$xx){
for($zz = -1; $zz <= 1; ++$zz){
$this->lockChunk($x + $xx, $z + $zz);
@ -2665,10 +2753,13 @@ class World implements ChunkManager{
$this->workerPool->submitTaskToWorker($task, $workerId);
Timings::$population->stopTiming();
return false;
return $promise;
}
return true;
//chunk is already populated; return a pre-resolved promise that will directly fire callbacks assigned
$result = new ChunkPopulationPromise();
$result->resolve();
return $result;
}
public function doChunkGarbageCollection() : void{

View File

@ -285,7 +285,7 @@ class WorldManager{
foreach((new ChunkSelector())->selectChunks(3, $centerX, $centerZ) as $index){
World::getXZ($index, $chunkX, $chunkZ);
$world->orderChunkPopulation($chunkX, $chunkZ);
$world->orderChunkPopulation($chunkX, $chunkZ, null);
}
}