mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-09-01 07:43:03 +00:00
Extracted a ChunkSelector unit from Player
if anyone asks me why not use a static method, it's because I want to make this more dynamic going forward, and statics are anything but.
This commit is contained in:
parent
a5d77d5106
commit
36727aabf7
68
src/player/ChunkSelector.php
Normal file
68
src/player/ChunkSelector.php
Normal file
@ -0,0 +1,68 @@
|
||||
<?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\player;
|
||||
|
||||
use pocketmine\world\World;
|
||||
|
||||
//TODO: turn this into an interface?
|
||||
final class ChunkSelector{
|
||||
|
||||
/**
|
||||
* @preturn \Generator|int[]
|
||||
* @phpstan-return \Generator<int, int, void, void>
|
||||
*/
|
||||
public function selectChunks(int $radius, int $centerX, int $centerZ) : \Generator{
|
||||
$radiusSquared = $radius ** 2;
|
||||
|
||||
for($x = 0; $x < $radius; ++$x){
|
||||
for($z = 0; $z <= $x; ++$z){
|
||||
if(($x ** 2 + $z ** 2) > $radiusSquared){
|
||||
break; //skip to next band
|
||||
}
|
||||
|
||||
//If the chunk is in the radius, others at the same offsets in different quadrants are also guaranteed to be.
|
||||
|
||||
/* Top right quadrant */
|
||||
yield World::chunkHash($centerX + $x, $centerZ + $z);
|
||||
/* Top left quadrant */
|
||||
yield World::chunkHash($centerX - $x - 1, $centerZ + $z);
|
||||
/* Bottom right quadrant */
|
||||
yield World::chunkHash($centerX + $x, $centerZ - $z - 1);
|
||||
/* Bottom left quadrant */
|
||||
yield World::chunkHash($centerX - $x - 1, $centerZ - $z - 1);
|
||||
|
||||
if($x !== $z){
|
||||
/* Top right quadrant mirror */
|
||||
yield World::chunkHash($centerX + $z, $centerZ + $x);
|
||||
/* Top left quadrant mirror */
|
||||
yield World::chunkHash($centerX - $z - 1, $centerZ + $x);
|
||||
/* Bottom right quadrant mirror */
|
||||
yield World::chunkHash($centerX + $z, $centerZ - $x - 1);
|
||||
/* Bottom left quadrant mirror */
|
||||
yield World::chunkHash($centerX - $z - 1, $centerZ - $x - 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -211,6 +211,8 @@ class Player extends Human implements CommandSender, ChunkLoader, ChunkListener,
|
||||
protected $spawnChunkLoadCount = 0;
|
||||
/** @var int */
|
||||
protected $chunksPerTick;
|
||||
/** @var ChunkSelector */
|
||||
protected $chunkSelector;
|
||||
|
||||
/** @var bool[] map: raw UUID (string) => bool */
|
||||
protected $hiddenPlayers = [];
|
||||
@ -280,6 +282,7 @@ class Player extends Human implements CommandSender, ChunkLoader, ChunkListener,
|
||||
$this->perm = new PermissibleBase($this);
|
||||
$this->chunksPerTick = (int) $this->server->getConfigGroup()->getProperty("chunk-sending.per-tick", 4);
|
||||
$this->spawnThreshold = (int) (($this->server->getConfigGroup()->getProperty("chunk-sending.spawn-radius", 4) ** 2) * M_PI);
|
||||
$this->chunkSelector = new ChunkSelector();
|
||||
|
||||
$namedtag = $this->server->getOfflinePlayerData($this->username); //TODO: make this async
|
||||
|
||||
@ -838,43 +841,6 @@ class Player extends Human implements CommandSender, ChunkLoader, ChunkListener,
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Generator<int, int, void, void>
|
||||
*/
|
||||
protected function selectChunks(int $radius, int $centerX, int $centerZ) : \Generator{
|
||||
$radiusSquared = $radius ** 2;
|
||||
|
||||
for($x = 0; $x < $radius; ++$x){
|
||||
for($z = 0; $z <= $x; ++$z){
|
||||
if(($x ** 2 + $z ** 2) > $radiusSquared){
|
||||
break; //skip to next band
|
||||
}
|
||||
|
||||
//If the chunk is in the radius, others at the same offsets in different quadrants are also guaranteed to be.
|
||||
|
||||
/* Top right quadrant */
|
||||
yield World::chunkHash($centerX + $x, $centerZ + $z);
|
||||
/* Top left quadrant */
|
||||
yield World::chunkHash($centerX - $x - 1, $centerZ + $z);
|
||||
/* Bottom right quadrant */
|
||||
yield World::chunkHash($centerX + $x, $centerZ - $z - 1);
|
||||
/* Bottom left quadrant */
|
||||
yield World::chunkHash($centerX - $x - 1, $centerZ - $z - 1);
|
||||
|
||||
if($x !== $z){
|
||||
/* Top right quadrant mirror */
|
||||
yield World::chunkHash($centerX + $z, $centerZ + $x);
|
||||
/* Top left quadrant mirror */
|
||||
yield World::chunkHash($centerX - $z - 1, $centerZ + $x);
|
||||
/* Bottom right quadrant mirror */
|
||||
yield World::chunkHash($centerX + $z, $centerZ - $x - 1);
|
||||
/* Bottom left quadrant mirror */
|
||||
yield World::chunkHash($centerX - $z - 1, $centerZ - $x - 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected function orderChunks() : void{
|
||||
if(!$this->isConnected() or $this->viewDistance === -1){
|
||||
return;
|
||||
@ -885,7 +851,7 @@ class Player extends Human implements CommandSender, ChunkLoader, ChunkListener,
|
||||
$newOrder = [];
|
||||
$unloadChunks = $this->usedChunks;
|
||||
|
||||
foreach($this->selectChunks(
|
||||
foreach($this->chunkSelector->selectChunks(
|
||||
$this->server->getAllowedViewDistance($this->viewDistance),
|
||||
$this->location->getFloorX() >> 4,
|
||||
$this->location->getFloorZ() >> 4
|
||||
|
Loading…
x
Reference in New Issue
Block a user