PocketMine-MP 5.15.1 git-5ef247620a7c6301a849b54e5ef1009217729fc8
BlockLightUpdate.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
22declare(strict_types=1);
23
24namespace pocketmine\world\light;
25
26use pocketmine\world\format\LightArray;
30use function max;
31
39 public function __construct(
40 SubChunkExplorer $subChunkExplorer,
41 array $lightFilters,
42 private array $lightEmitters
43 ){
44 parent::__construct($subChunkExplorer, $lightFilters);
45 }
46
47 protected function getCurrentLightArray() : LightArray{
48 return $this->subChunkExplorer->currentSubChunk->getBlockLightArray();
49 }
50
51 public function recalculateNode(int $x, int $y, int $z) : void{
52 if($this->subChunkExplorer->moveTo($x, $y, $z) !== SubChunkExplorerStatus::INVALID){
53 $block = $this->subChunkExplorer->currentSubChunk->getBlockStateId($x & SubChunk::COORD_MASK, $y & SubChunk::COORD_MASK, $z & SubChunk::COORD_MASK);
54 $this->setAndUpdateLight($x, $y, $z, max($this->lightEmitters[$block] ?? 0, $this->getHighestAdjacentLight($x, $y, $z) - ($this->lightFilters[$block] ?? self::BASE_LIGHT_FILTER)));
55 }
56 }
57
58 public function recalculateChunk(int $chunkX, int $chunkZ) : int{
59 if($this->subChunkExplorer->moveToChunk($chunkX, 0, $chunkZ) === SubChunkExplorerStatus::INVALID){
60 throw new \InvalidArgumentException("Chunk $chunkX $chunkZ does not exist");
61 }
62 $chunk = $this->subChunkExplorer->currentChunk;
63
64 $lightSources = 0;
65 foreach($chunk->getSubChunks() as $subChunkY => $subChunk){
66 $subChunk->setBlockLightArray(LightArray::fill(0));
67
68 foreach($subChunk->getBlockLayers() as $layer){
69 foreach($layer->getPalette() as $state){
70 if(($this->lightEmitters[$state] ?? 0) > 0){
71 $lightSources += $this->scanForLightEmittingBlocks($subChunk, $chunkX << SubChunk::COORD_BIT_SIZE, $subChunkY << SubChunk::COORD_BIT_SIZE, $chunkZ << SubChunk::COORD_BIT_SIZE);
72 break 2;
73 }
74 }
75 }
76 }
77
78 return $lightSources;
79 }
80
81 private function scanForLightEmittingBlocks(SubChunk $subChunk, int $baseX, int $baseY, int $baseZ) : int{
82 $lightSources = 0;
83 for($x = 0; $x < SubChunk::EDGE_LENGTH; ++$x){
84 for($z = 0; $z < SubChunk::EDGE_LENGTH; ++$z){
85 for($y = 0; $y < SubChunk::EDGE_LENGTH; ++$y){
86 $light = $this->lightEmitters[$subChunk->getBlockStateId($x, $y, $z)] ?? 0;
87 if($light > 0){
88 $this->setAndUpdateLight(
89 $baseX + $x,
90 $baseY + $y,
91 $baseZ + $z,
92 $light
93 );
94 $lightSources++;
95 }
96 }
97 }
98 }
99 return $lightSources;
100 }
101}
__construct(SubChunkExplorer $subChunkExplorer, array $lightFilters, private array $lightEmitters)
recalculateChunk(int $chunkX, int $chunkZ)