PocketMine-MP 5.21.2 git-b2aa6396c3cc2cafdd815eacc360e1ad89599899
Loading...
Searching...
No Matches
Fire.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\block;
25
26use pocketmine\block\utils\AgeableTrait;
28use pocketmine\block\utils\SupportType;
33use function intdiv;
34use function max;
35use function min;
36use function mt_rand;
37
38class Fire extends BaseFire{
39 use AgeableTrait;
40
41 public const MAX_AGE = 15;
42
43 protected function getFireDamage() : int{
44 return 1;
45 }
46
47 private function canBeSupportedBy(Block $block) : bool{
48 return $block->getSupportType(Facing::UP) === SupportType::FULL;
49 }
50
51 public function onNearbyBlockChange() : void{
52 $world = $this->position->getWorld();
53 $down = $this->getSide(Facing::DOWN);
54 if(SoulFire::canBeSupportedBy($down)){
55 $world->setBlock($this->position, VanillaBlocks::SOUL_FIRE());
56 }elseif(!$this->canBeSupportedBy($this->getSide(Facing::DOWN)) && !$this->hasAdjacentFlammableBlocks()){
57 $world->setBlock($this->position, VanillaBlocks::AIR());
58 }else{
59 $world->scheduleDelayedBlockUpdate($this->position, mt_rand(30, 40));
60 }
61 }
62
63 public function ticksRandomly() : bool{
64 return true;
65 }
66
67 public function onRandomTick() : void{
68 $down = $this->getSide(Facing::DOWN);
69
70 $result = null;
71 if($this->age < self::MAX_AGE && mt_rand(0, 2) === 0){
72 $this->age++;
73 $result = $this;
74 }
75 $canSpread = true;
76
77 if(!$down->burnsForever()){
78 //TODO: check rain
79 if($this->age === self::MAX_AGE){
80 if(!$down->isFlammable() && mt_rand(0, 3) === 3){ //1/4 chance to extinguish
81 $canSpread = false;
82 $result = VanillaBlocks::AIR();
83 }
84 }elseif(!$this->hasAdjacentFlammableBlocks()){
85 $canSpread = false;
86 if($down->isTransparent() || $this->age > 3){
87 $result = VanillaBlocks::AIR();
88 }
89 }
90 }
91
92 $world = $this->position->getWorld();
93 if($result !== null){
94 $world->setBlock($this->position, $result);
95 }
96
97 $world->scheduleDelayedBlockUpdate($this->position, mt_rand(30, 40));
98
99 if($canSpread){
100 $this->burnBlocksAround();
101 $this->spreadFire();
102 }
103 }
104
105 public function onScheduledUpdate() : void{
106 $this->onRandomTick();
107 }
108
109 private function hasAdjacentFlammableBlocks() : bool{
110 foreach(Facing::ALL as $face){
111 if($this->getSide($face)->isFlammable()){
112 return true;
113 }
114 }
115
116 return false;
117 }
118
119 private function burnBlocksAround() : void{
120 //TODO: raise upper bound for chance in humid biomes
121
122 foreach($this->getHorizontalSides() as $side){
123 $this->burnBlock($side, 300);
124 }
125
126 //vanilla uses a 250 upper bound here, but I don't think they intended to increase the chance of incineration
127 $this->burnBlock($this->getSide(Facing::UP), 350);
128 $this->burnBlock($this->getSide(Facing::DOWN), 350);
129 }
130
131 private function burnBlock(Block $block, int $chanceBound) : void{
132 if(mt_rand(0, $chanceBound) < $block->getFlammability()){
133 $cancelled = false;
134 if(BlockBurnEvent::hasHandlers()){
135 $ev = new BlockBurnEvent($block, $this);
136 $ev->call();
137 $cancelled = $ev->isCancelled();
138 }
139 if(!$cancelled){
140 $block->onIncinerate();
141
142 $world = $this->position->getWorld();
143 if($world->getBlock($block->position)->isSameState($block)){
144 $spreadedFire = false;
145 if(mt_rand(0, $this->age + 9) < 5){ //TODO: check rain
146 $fire = clone $this;
147 $fire->age = min(self::MAX_AGE, $fire->age + (mt_rand(0, 4) >> 2));
148 $spreadedFire = $this->spreadBlock($block, $fire);
149 }
150 if(!$spreadedFire){
151 $world->setBlock($block->position, VanillaBlocks::AIR());
152 }
153 }
154 }
155 }
156 }
157
158 private function spreadFire() : void{
159 $world = $this->position->getWorld();
160 $difficultyChanceIncrease = $world->getDifficulty() * 7;
161 $ageDivisor = $this->age + 30;
162
163 for($y = -1; $y <= 4; ++$y){
164 $targetY = $y + (int) $this->position->y;
165 if($targetY < World::Y_MIN || $targetY >= World::Y_MAX){
166 continue;
167 }
168 //Higher blocks have a lower chance of catching fire
169 $randomBound = 100 + ($y > 1 ? ($y - 1) * 100 : 0);
170
171 for($z = -1; $z <= 1; ++$z){
172 $targetZ = $z + (int) $this->position->z;
173 for($x = -1; $x <= 1; ++$x){
174 if($x === 0 && $y === 0 && $z === 0){
175 continue;
176 }
177 $targetX = $x + (int) $this->position->x;
178 if(!$world->isInWorld($targetX, $targetY, $targetZ)){
179 continue;
180 }
181
182 if(!$world->isChunkLoaded($targetX >> Chunk::COORD_BIT_SIZE, $targetZ >> Chunk::COORD_BIT_SIZE)){
183 continue;
184 }
185 $block = $world->getBlockAt($targetX, $targetY, $targetZ);
186 if($block->getTypeId() !== BlockTypeIds::AIR){
187 continue;
188 }
189
190 //TODO: fire can't spread if it's raining in any horizontally adjacent block, or the current one
191
192 $encouragement = 0;
193 foreach($block->position->sides() as $vector3){
194 if($world->isInWorld($vector3->x, $vector3->y, $vector3->z)){
195 $encouragement = max($encouragement, $world->getBlockAt($vector3->x, $vector3->y, $vector3->z)->getFlameEncouragement());
196 }
197 }
198
199 if($encouragement <= 0){
200 continue;
201 }
202
203 $maxChance = intdiv($encouragement + 40 + $difficultyChanceIncrease, $ageDivisor);
204 //TODO: max chance is lowered by half in humid biomes
205
206 if($maxChance > 0 && mt_rand(0, $randomBound - 1) <= $maxChance){
207 $new = clone $this;
208 $new->age = min(self::MAX_AGE, $this->age + (mt_rand(0, 4) >> 2));
209 $this->spreadBlock($block, $new);
210 }
211 }
212 }
213 }
214 }
215
216 private function spreadBlock(Block $block, Block $newState) : bool{
217 return BlockEventHelper::spread($block, $newState, $this);
218 }
219}
getSupportType(int $facing)
Definition Block.php:943
getSide(int $side, int $step=1)
Definition Block.php:770