PocketMine-MP  1.4 - API 1.10.0
 All Classes Namespaces Functions Variables Pages
GenerationLevelManager.php
1 <?php
2 
3 /*
4  *
5  * ____ _ _ __ __ _ __ __ ____
6  * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7  * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8  * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9  * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10  *
11  * This program is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * @author PocketMine Team
17  * @link http://www.pocketmine.net/
18  *
19  *
20 */
21 
22 namespace pocketmine\level\generator;
23 
27 
29 
31  protected $levels = [];
32 
34  protected $requestQueue = [];
35 
37  protected $server;
38 
40  protected $manager;
41 
42  protected $maxCount;
43 
48  public function __construct(Server $server, GenerationInstanceManager $manager){
49  $this->server = $server;
50  $this->manager = $manager;
51  $this->maxCount = $this->server->getProperty("chunk-generation.per-tick", 1);
52  }
53 
54  public function openLevel($levelID, $seed, $class, array $options){
55  if(!isset($this->levels[$levelID])){
56  $this->levels[$levelID] = new GenerationChunkManager($this, $levelID, $seed, $class, $options);
57  }
58  }
59 
60  public function generateChunk($levelID, $chunkX, $chunkZ){
61  if(isset($this->levels[$levelID])){
62  $this->levels[$levelID]->populateChunk($chunkX, $chunkZ); //Request population directly
63  if(isset($this->levels[$levelID])){
64  foreach($this->levels[$levelID]->getChangedChunks() as $index => $chunk){
65  $this->sendChunk($levelID, $chunk);
66  $this->levels[$levelID]->cleanChangedChunk($index);
67  }
68 
69  $this->levels[$levelID]->doGarbageCollection();
70  $this->levels[$levelID]->cleanChangedChunks();
71  }
72  }
73  }
74 
75  public function process(){
76  if(count($this->requestQueue) > 0){
77  $count = 0;
78  foreach($this->requestQueue as $levelID => $chunks){
79  if($count >= $this->maxCount){
80  break;
81  }
82 
83  if(count($chunks) === 0){
84  unset($this->requestQueue[$levelID]);
85  }else{
86  $key = key($chunks);
87  Level::getXZ($key, $chunkX, $chunkZ);
88  unset($this->requestQueue[$levelID][$key]);
89  $this->generateChunk($levelID, $chunkX, $chunkZ);
90  ++$count;
91  }
92  }
93  }
94  }
95 
96  public function shutdown(){
97  foreach($this->levels as $level){
98  $level->shutdown();
99  }
100  $this->levels = [];
101  }
102 
103  public function closeLevel($levelID){
104  if(isset($this->levels[$levelID])){
105  $this->levels[$levelID]->shutdown();
106  unset($this->levels[$levelID]);
107  }
108  }
109 
110  public function enqueueChunk($levelID, $chunkX, $chunkZ){
111  if(!isset($this->requestQueue[$levelID])){
112  $this->requestQueue[$levelID] = [];
113  }
114  if(!isset($this->requestQueue[$levelID][$index = Level::chunkHash($chunkX, $chunkZ)])){
115  $this->requestQueue[$levelID][$index] = 1;
116  }else{
117  $this->requestQueue[$levelID][$index]++;
118  arsort($this->requestQueue[$levelID]);
119  }
120  }
121 
129  public function requestChunk($levelID, $chunkX, $chunkZ){
130  return $this->manager->getChunk($levelID, $chunkX, $chunkZ);
131  }
132 
133  public function sendChunk($levelID, FullChunk $chunk){
134  $this->manager->receiveChunk($levelID, $chunk);
135  }
136 
140  public function getLogger(){
141  return $this->server->getLogger();
142  }
143 
144 }
static chunkHash($x, $z)
Definition: Level.php:230
__construct(Server $server, GenerationInstanceManager $manager)