mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-09-08 02:42:58 +00:00
Nuke Block->meta, split into variant and state properties, lots of cleanup
This is a major change to the way block metadata is handled within the PM core. This separates variant metadata (which really ought to be part of the ID) from state metadata, and in a couple of cases flattens separate states of blocks together. The result of this is that invalid variants can be much more easily detected, and additionally state handling is much cleaner since meta is only needed at the serialize layer instead of throughout the code.
This commit is contained in:
@ -44,6 +44,24 @@ abstract class Liquid extends Transparent{
|
||||
private const CAN_FLOW = 0;
|
||||
private const BLOCKED = -1;
|
||||
|
||||
/** @var bool */
|
||||
protected $falling = false;
|
||||
/** @var int */
|
||||
protected $decay = 0; //PC "level" property
|
||||
|
||||
protected function writeStateToMeta() : int{
|
||||
return $this->decay | ($this->falling ? 0x08 : 0);
|
||||
}
|
||||
|
||||
public function readStateFromMeta(int $meta) : void{
|
||||
$this->decay = $meta & 0x07;
|
||||
$this->falling = ($meta & 0x08) !== 0;
|
||||
}
|
||||
|
||||
public function getStateBitmask() : int{
|
||||
return 0b1111;
|
||||
}
|
||||
|
||||
public function hasEntityCollision() : bool{
|
||||
return true;
|
||||
}
|
||||
@ -84,35 +102,20 @@ abstract class Liquid extends Transparent{
|
||||
|
||||
abstract public function getBucketEmptySound() : int;
|
||||
|
||||
public function getFluidHeightPercent(){
|
||||
$d = $this->meta;
|
||||
if($d >= 8){
|
||||
$d = 0;
|
||||
}
|
||||
|
||||
return ($d + 1) / 9;
|
||||
public function isSource() : bool{
|
||||
return !$this->falling and $this->decay === 0;
|
||||
}
|
||||
|
||||
protected function getFlowDecay(Block $block) : int{
|
||||
if($block->getId() !== $this->getId()){
|
||||
return -1;
|
||||
}
|
||||
|
||||
return $block->getDamage();
|
||||
public function getFluidHeightPercent(){
|
||||
return (($this->falling ? 0 : $this->decay) + 1) / 9;
|
||||
}
|
||||
|
||||
protected function getEffectiveFlowDecay(Block $block) : int{
|
||||
if($block->getId() !== $this->getId()){
|
||||
if(!($block instanceof Liquid) or $block->getId() !== $this->getId()){
|
||||
return -1;
|
||||
}
|
||||
|
||||
$decay = $block->getDamage();
|
||||
|
||||
if($decay >= 8){
|
||||
$decay = 0;
|
||||
}
|
||||
|
||||
return $decay;
|
||||
return $block->falling ? 0 : $block->decay;
|
||||
}
|
||||
|
||||
public function clearCaches() : void{
|
||||
@ -170,7 +173,7 @@ abstract class Liquid extends Transparent{
|
||||
}
|
||||
}
|
||||
|
||||
if($this->getDamage() >= 8){
|
||||
if($this->falling){
|
||||
if(
|
||||
!$this->canFlowInto($this->level->getBlockAt($this->x, $this->y, $this->z - 1)) or
|
||||
!$this->canFlowInto($this->level->getBlockAt($this->x, $this->y, $this->z + 1)) or
|
||||
@ -214,10 +217,9 @@ abstract class Liquid extends Transparent{
|
||||
}
|
||||
|
||||
public function onScheduledUpdate() : void{
|
||||
$decay = $this->getFlowDecay($this);
|
||||
$multiplier = $this->getFlowDecayPerBlock();
|
||||
|
||||
if($decay > 0){
|
||||
if(!$this->isSource()){
|
||||
$smallestFlowDecay = -100;
|
||||
$this->adjacentSources = 0;
|
||||
$smallestFlowDecay = $this->getSmallestFlowDecay($this->level->getBlockAt($this->x, $this->y, $this->z - 1), $smallestFlowDecay);
|
||||
@ -226,80 +228,81 @@ abstract class Liquid extends Transparent{
|
||||
$smallestFlowDecay = $this->getSmallestFlowDecay($this->level->getBlockAt($this->x + 1, $this->y, $this->z), $smallestFlowDecay);
|
||||
|
||||
$newDecay = $smallestFlowDecay + $multiplier;
|
||||
$falling = false;
|
||||
|
||||
if($newDecay >= 8 or $smallestFlowDecay < 0){
|
||||
$newDecay = -1;
|
||||
}
|
||||
|
||||
if(($topFlowDecay = $this->getFlowDecay($this->level->getBlockAt($this->x, $this->y + 1, $this->z))) >= 0){
|
||||
$newDecay = $topFlowDecay | 0x08;
|
||||
if($this->getEffectiveFlowDecay($this->level->getBlockAt($this->x, $this->y + 1, $this->z)) >= 0){
|
||||
$falling = true;
|
||||
}
|
||||
|
||||
if($this->adjacentSources >= 2 and $this instanceof Water){
|
||||
$bottomBlock = $this->level->getBlockAt($this->x, $this->y - 1, $this->z);
|
||||
if($bottomBlock->isSolid()){
|
||||
$newDecay = 0;
|
||||
}elseif($bottomBlock instanceof Water and $bottomBlock->getDamage() === 0){
|
||||
if($bottomBlock->isSolid() or ($bottomBlock instanceof Water and $bottomBlock->isSource())){
|
||||
$newDecay = 0;
|
||||
$falling = false;
|
||||
}
|
||||
}
|
||||
|
||||
if($newDecay !== $decay){
|
||||
$decay = $newDecay;
|
||||
if($decay < 0){
|
||||
if($newDecay !== $this->decay or $falling !== $this->falling){
|
||||
if(!$falling and $newDecay < 0){
|
||||
$this->level->setBlock($this, BlockFactory::get(Block::AIR), true, true);
|
||||
}else{
|
||||
$this->level->setBlock($this, BlockFactory::get($this->id, $decay), true, true);
|
||||
$this->level->scheduleDelayedBlockUpdate($this, $this->tickRate());
|
||||
return;
|
||||
}
|
||||
|
||||
$this->falling = $falling;
|
||||
$this->decay = $falling ? 0 : $newDecay;
|
||||
$this->level->setBlock($this, $this, true, true); //local block update will cause an update to be scheduled
|
||||
}
|
||||
}
|
||||
|
||||
$bottomBlock = $this->level->getBlockAt($this->x, $this->y - 1, $this->z);
|
||||
|
||||
$this->flowIntoBlock($bottomBlock, 0, true);
|
||||
|
||||
if($this->isSource() or !$bottomBlock->canBeFlowedInto()){
|
||||
if($this->falling){
|
||||
$adjacentDecay = 1; //falling liquid behaves like source block
|
||||
}else{
|
||||
$adjacentDecay = $this->decay + $multiplier;
|
||||
}
|
||||
|
||||
if($adjacentDecay < 8){
|
||||
$flags = $this->getOptimalFlowDirections();
|
||||
|
||||
if($flags[0]){
|
||||
$this->flowIntoBlock($this->level->getBlockAt($this->x - 1, $this->y, $this->z), $adjacentDecay, false);
|
||||
}
|
||||
|
||||
if($flags[1]){
|
||||
$this->flowIntoBlock($this->level->getBlockAt($this->x + 1, $this->y, $this->z), $adjacentDecay, false);
|
||||
}
|
||||
|
||||
if($flags[2]){
|
||||
$this->flowIntoBlock($this->level->getBlockAt($this->x, $this->y, $this->z - 1), $adjacentDecay, false);
|
||||
}
|
||||
|
||||
if($flags[3]){
|
||||
$this->flowIntoBlock($this->level->getBlockAt($this->x, $this->y, $this->z + 1), $adjacentDecay, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if($decay >= 0){
|
||||
$bottomBlock = $this->level->getBlockAt($this->x, $this->y - 1, $this->z);
|
||||
|
||||
$this->flowIntoBlock($bottomBlock, $decay | 0x08);
|
||||
|
||||
if($decay === 0 or !$bottomBlock->canBeFlowedInto()){
|
||||
if($decay >= 8){
|
||||
$adjacentDecay = 1;
|
||||
}else{
|
||||
$adjacentDecay = $decay + $multiplier;
|
||||
}
|
||||
|
||||
if($adjacentDecay < 8){
|
||||
$flags = $this->getOptimalFlowDirections();
|
||||
|
||||
if($flags[0]){
|
||||
$this->flowIntoBlock($this->level->getBlockAt($this->x - 1, $this->y, $this->z), $adjacentDecay);
|
||||
}
|
||||
|
||||
if($flags[1]){
|
||||
$this->flowIntoBlock($this->level->getBlockAt($this->x + 1, $this->y, $this->z), $adjacentDecay);
|
||||
}
|
||||
|
||||
if($flags[2]){
|
||||
$this->flowIntoBlock($this->level->getBlockAt($this->x, $this->y, $this->z - 1), $adjacentDecay);
|
||||
}
|
||||
|
||||
if($flags[3]){
|
||||
$this->flowIntoBlock($this->level->getBlockAt($this->x, $this->y, $this->z + 1), $adjacentDecay);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$this->checkForHarden();
|
||||
}
|
||||
$this->checkForHarden();
|
||||
}
|
||||
|
||||
protected function flowIntoBlock(Block $block, int $newFlowDecay) : void{
|
||||
protected function flowIntoBlock(Block $block, int $newFlowDecay, bool $falling) : void{
|
||||
if($this->canFlowInto($block) and !($block instanceof Liquid)){
|
||||
if($block->getId() > 0){
|
||||
$this->level->useBreakOn($block);
|
||||
}
|
||||
|
||||
$this->level->setBlock($block, BlockFactory::get($this->getId(), $newFlowDecay), true, true);
|
||||
$this->level->scheduleDelayedBlockUpdate($block, $this->tickRate());
|
||||
$new = clone $this;
|
||||
$new->falling = $falling;
|
||||
$new->decay = $falling ? 0 : $newFlowDecay;
|
||||
$this->level->setBlock($block, $new, true, true);
|
||||
}
|
||||
}
|
||||
|
||||
@ -407,13 +410,15 @@ abstract class Liquid extends Transparent{
|
||||
}
|
||||
|
||||
private function getSmallestFlowDecay(Block $block, int $decay) : int{
|
||||
$blockDecay = $this->getFlowDecay($block);
|
||||
|
||||
if($blockDecay < 0){
|
||||
if(!($block instanceof Liquid) or $block->getId() !== $this->getId()){
|
||||
return $decay;
|
||||
}elseif($blockDecay === 0){
|
||||
}
|
||||
|
||||
$blockDecay = $block->decay;
|
||||
|
||||
if($block->isSource()){
|
||||
++$this->adjacentSources;
|
||||
}elseif($blockDecay >= 8){
|
||||
}elseif($block->falling){
|
||||
$blockDecay = 0;
|
||||
}
|
||||
|
||||
@ -433,6 +438,6 @@ abstract class Liquid extends Transparent{
|
||||
}
|
||||
|
||||
protected function canFlowInto(Block $block) : bool{
|
||||
return $block->canBeFlowedInto() and !($block instanceof Liquid and $block->meta === 0); //TODO: I think this should only be liquids of the same type
|
||||
return $block->canBeFlowedInto() and !($block instanceof Liquid and $block->isSource()); //TODO: I think this should only be liquids of the same type
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user