PocketMine-MP 5.35.1 git-e32e836dad793a3a3c8ddd8927c00e112b1e576a
Loading...
Searching...
No Matches
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;
27use pocketmine\world\format\PalettedBlockArray;
31use function max;
32
40 public function __construct(
41 SubChunkExplorer $subChunkExplorer,
42 array $lightFilters,
43 private array $lightEmitters
44 ){
45 parent::__construct($subChunkExplorer, $lightFilters);
46 }
47
48 protected function getCurrentLightArray() : LightArray{
49 return $this->subChunkExplorer->currentSubChunk->getBlockLightArray();
50 }
51
52 public function recalculateNode(int $x, int $y, int $z) : void{
53 if($this->subChunkExplorer->moveTo($x, $y, $z) !== SubChunkExplorerStatus::INVALID){
54 $block = $this->subChunkExplorer->currentSubChunk->getBlockStateId($x & SubChunk::COORD_MASK, $y & SubChunk::COORD_MASK, $z & SubChunk::COORD_MASK);
55 $this->setAndUpdateLight($x, $y, $z, max($this->lightEmitters[$block] ?? 0, $this->getHighestAdjacentLight($x, $y, $z) - ($this->lightFilters[$block] ?? self::BASE_LIGHT_FILTER)));
56 }
57 }
58
59 private function layerHasLightEmitter(?PalettedBlockArray $layer) : bool{
60 if($layer === null){
61 return false;
62 }
63
64 foreach($layer->getPalette() as $state){
65 if(($this->lightEmitters[$state] ?? 0) > 0){
66 return true;
67 }
68 }
69
70 return false;
71 }
72
73 public function recalculateChunk(int $chunkX, int $chunkZ) : int{
74 if($this->subChunkExplorer->moveToChunk($chunkX, 0, $chunkZ) === SubChunkExplorerStatus::INVALID){
75 throw new \InvalidArgumentException("Chunk $chunkX $chunkZ does not exist");
76 }
77 $chunk = $this->subChunkExplorer->currentChunk;
78
79 $lightSources = 0;
80 foreach($chunk->getSubChunks() as $subChunkY => $subChunk){
81 $subChunk->setBlockLightArray(LightArray::fill(0));
82
83 if(
84 $this->layerHasLightEmitter($subChunk->getBlockLayer0()) ||
85 $this->layerHasLightEmitter($subChunk->getBlockLayer1())
86 ){
87 $lightSources += $this->scanForLightEmittingBlocks($subChunk, $chunkX << SubChunk::COORD_BIT_SIZE, $subChunkY << SubChunk::COORD_BIT_SIZE, $chunkZ << SubChunk::COORD_BIT_SIZE);
88 }
89 }
90
91 return $lightSources;
92 }
93
94 private function scanForLightEmittingBlocks(SubChunk $subChunk, int $baseX, int $baseY, int $baseZ) : int{
95 $lightSources = 0;
96 for($x = 0; $x < SubChunk::EDGE_LENGTH; ++$x){
97 for($z = 0; $z < SubChunk::EDGE_LENGTH; ++$z){
98 for($y = 0; $y < SubChunk::EDGE_LENGTH; ++$y){
99 $light = $this->lightEmitters[$subChunk->getBlockStateId($x, $y, $z)] ?? 0;
100 if($light > 0){
101 $this->setAndUpdateLight(
102 $baseX + $x,
103 $baseY + $y,
104 $baseZ + $z,
105 $light
106 );
107 $lightSources++;
108 }
109 }
110 }
111 }
112 return $lightSources;
113 }
114}
__construct(SubChunkExplorer $subChunkExplorer, array $lightFilters, private array $lightEmitters)
recalculateChunk(int $chunkX, int $chunkZ)