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