Liquid: make some code slightly less cancerous to read

This commit is contained in:
Dylan K. Taylor 2021-09-18 15:28:45 +01:00
parent 3836a57ba6
commit 1b5071e074
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D

View File

@ -273,13 +273,15 @@ abstract class Liquid extends Transparent{
public function onScheduledUpdate() : void{
$multiplier = $this->getFlowDecayPerBlock();
$world = $this->position->getWorld();
if(!$this->isSource()){
$smallestFlowDecay = -100;
$this->adjacentSources = 0;
$smallestFlowDecay = $this->getSmallestFlowDecay($this->position->getWorld()->getBlockAt($this->position->x, $this->position->y, $this->position->z - 1), $smallestFlowDecay);
$smallestFlowDecay = $this->getSmallestFlowDecay($this->position->getWorld()->getBlockAt($this->position->x, $this->position->y, $this->position->z + 1), $smallestFlowDecay);
$smallestFlowDecay = $this->getSmallestFlowDecay($this->position->getWorld()->getBlockAt($this->position->x - 1, $this->position->y, $this->position->z), $smallestFlowDecay);
$smallestFlowDecay = $this->getSmallestFlowDecay($this->position->getWorld()->getBlockAt($this->position->x + 1, $this->position->y, $this->position->z), $smallestFlowDecay);
$smallestFlowDecay = $this->getSmallestFlowDecay($world->getBlockAt($this->position->x, $this->position->y, $this->position->z - 1), $smallestFlowDecay);
$smallestFlowDecay = $this->getSmallestFlowDecay($world->getBlockAt($this->position->x, $this->position->y, $this->position->z + 1), $smallestFlowDecay);
$smallestFlowDecay = $this->getSmallestFlowDecay($world->getBlockAt($this->position->x - 1, $this->position->y, $this->position->z), $smallestFlowDecay);
$smallestFlowDecay = $this->getSmallestFlowDecay($world->getBlockAt($this->position->x + 1, $this->position->y, $this->position->z), $smallestFlowDecay);
$newDecay = $smallestFlowDecay + $multiplier;
$falling = false;
@ -288,12 +290,12 @@ abstract class Liquid extends Transparent{
$newDecay = -1;
}
if($this->getEffectiveFlowDecay($this->position->getWorld()->getBlockAt($this->position->x, $this->position->y + 1, $this->position->z)) >= 0){
if($this->getEffectiveFlowDecay($world->getBlockAt($this->position->x, $this->position->y + 1, $this->position->z)) >= 0){
$falling = true;
}
if($this->adjacentSources >= 2 and $this instanceof Water){
$bottomBlock = $this->position->getWorld()->getBlockAt($this->position->x, $this->position->y - 1, $this->position->z);
$bottomBlock = $world->getBlockAt($this->position->x, $this->position->y - 1, $this->position->z);
if($bottomBlock->isSolid() or ($bottomBlock instanceof Water and $bottomBlock->isSource())){
$newDecay = 0;
$falling = false;
@ -302,17 +304,17 @@ abstract class Liquid extends Transparent{
if($falling !== $this->falling or (!$falling and $newDecay !== $this->decay)){
if(!$falling and $newDecay < 0){
$this->position->getWorld()->setBlock($this->position, VanillaBlocks::AIR());
$world->setBlock($this->position, VanillaBlocks::AIR());
return;
}
$this->falling = $falling;
$this->decay = $falling ? 0 : $newDecay;
$this->position->getWorld()->setBlock($this->position, $this); //local block update will cause an update to be scheduled
$world->setBlock($this->position, $this); //local block update will cause an update to be scheduled
}
}
$bottomBlock = $this->position->getWorld()->getBlockAt($this->position->x, $this->position->y - 1, $this->position->z);
$bottomBlock = $world->getBlockAt($this->position->x, $this->position->y - 1, $this->position->z);
$this->flowIntoBlock($bottomBlock, 0, true);
@ -327,19 +329,19 @@ abstract class Liquid extends Transparent{
$flags = $this->getOptimalFlowDirections();
if($flags[0]){
$this->flowIntoBlock($this->position->getWorld()->getBlockAt($this->position->x - 1, $this->position->y, $this->position->z), $adjacentDecay, false);
$this->flowIntoBlock($world->getBlockAt($this->position->x - 1, $this->position->y, $this->position->z), $adjacentDecay, false);
}
if($flags[1]){
$this->flowIntoBlock($this->position->getWorld()->getBlockAt($this->position->x + 1, $this->position->y, $this->position->z), $adjacentDecay, false);
$this->flowIntoBlock($world->getBlockAt($this->position->x + 1, $this->position->y, $this->position->z), $adjacentDecay, false);
}
if($flags[2]){
$this->flowIntoBlock($this->position->getWorld()->getBlockAt($this->position->x, $this->position->y, $this->position->z - 1), $adjacentDecay, false);
$this->flowIntoBlock($world->getBlockAt($this->position->x, $this->position->y, $this->position->z - 1), $adjacentDecay, false);
}
if($flags[3]){
$this->flowIntoBlock($this->position->getWorld()->getBlockAt($this->position->x, $this->position->y, $this->position->z + 1), $adjacentDecay, false);
$this->flowIntoBlock($world->getBlockAt($this->position->x, $this->position->y, $this->position->z + 1), $adjacentDecay, false);
}
}
}
@ -368,6 +370,7 @@ abstract class Liquid extends Transparent{
private function calculateFlowCost(int $blockX, int $blockY, int $blockZ, int $accumulatedCost, int $maxCost, int $originOpposite, int $lastOpposite) : int{
$cost = 1000;
$world = $this->position->getWorld();
for($j = 0; $j < 4; ++$j){
if($j === $originOpposite or $j === $lastOpposite){
continue;
@ -388,10 +391,10 @@ abstract class Liquid extends Transparent{
}
if(!isset($this->flowCostVisited[$hash = World::blockHash($x, $y, $z)])){
$blockSide = $this->position->getWorld()->getBlockAt($x, $y, $z);
$blockSide = $world->getBlockAt($x, $y, $z);
if(!$this->canFlowInto($blockSide)){
$this->flowCostVisited[$hash] = self::BLOCKED;
}elseif($this->position->getWorld()->getBlockAt($x, $y - 1, $z)->canBeFlowedInto()){
}elseif($world->getBlockAt($x, $y - 1, $z)->canBeFlowedInto()){
$this->flowCostVisited[$hash] = self::CAN_FLOW_DOWN;
}else{
$this->flowCostVisited[$hash] = self::CAN_FLOW;
@ -424,6 +427,7 @@ abstract class Liquid extends Transparent{
* @return bool[]
*/
private function getOptimalFlowDirections() : array{
$world = $this->position->getWorld();
$flowCost = array_fill(0, 4, 1000);
$maxCost = intdiv(4, $this->getFlowDecayPerBlock());
for($j = 0; $j < 4; ++$j){
@ -440,11 +444,11 @@ abstract class Liquid extends Transparent{
}elseif($j === 3){
++$z;
}
$block = $this->position->getWorld()->getBlockAt($x, $y, $z);
$block = $world->getBlockAt($x, $y, $z);
if(!$this->canFlowInto($block)){
$this->flowCostVisited[World::blockHash($x, $y, $z)] = self::BLOCKED;
}elseif($this->position->getWorld()->getBlockAt($x, $y - 1, $z)->canBeFlowedInto()){
}elseif($world->getBlockAt($x, $y - 1, $z)->canBeFlowedInto()){
$this->flowCostVisited[World::blockHash($x, $y, $z)] = self::CAN_FLOW_DOWN;
$flowCost[$j] = $maxCost = 0;
}elseif($maxCost > 0){