diff --git a/src/block/Block.php b/src/block/Block.php index 934a485d9..d557411fe 100644 --- a/src/block/Block.php +++ b/src/block/Block.php @@ -54,7 +54,7 @@ use function get_class; use const PHP_INT_MAX; class Block{ - public const INTERNAL_STATE_DATA_BITS = 9; + public const INTERNAL_STATE_DATA_BITS = 8; public const INTERNAL_STATE_DATA_MASK = ~(~0 << self::INTERNAL_STATE_DATA_BITS); protected BlockIdentifier $idInfo; diff --git a/src/data/runtime/RuntimeDataReader.php b/src/data/runtime/RuntimeDataReader.php index 14fbca209..ded875385 100644 --- a/src/data/runtime/RuntimeDataReader.php +++ b/src/data/runtime/RuntimeDataReader.php @@ -144,9 +144,10 @@ final class RuntimeDataReader implements RuntimeDataDescriber{ */ public function wallConnections(array &$connections) : void{ $result = []; - //TODO: we can pack this into 7 bits instead of 8 + $offset = 0; + $packed = $this->readBoundedInt(7, 0, (3 ** 4) - 1); foreach(Facing::HORIZONTAL as $facing){ - $type = $this->readBoundedInt(2, 0, 2); + $type = intdiv($packed, (3 ** $offset)) % 3; if($type !== 0){ $result[$facing] = match($type){ 1 => WallConnectionType::SHORT(), @@ -154,6 +155,7 @@ final class RuntimeDataReader implements RuntimeDataDescriber{ default => throw new AssumptionFailedError("Unreachable") }; } + $offset++; } $connections = $result; diff --git a/src/data/runtime/RuntimeDataSizeCalculator.php b/src/data/runtime/RuntimeDataSizeCalculator.php index 5678ffd81..ded209470 100644 --- a/src/data/runtime/RuntimeDataSizeCalculator.php +++ b/src/data/runtime/RuntimeDataSizeCalculator.php @@ -81,8 +81,7 @@ final class RuntimeDataSizeCalculator implements RuntimeDataDescriber{ } public function wallConnections(array &$connections) : void{ - //TODO: this can be reduced to 7 if we pack the trinary values - $this->addBits(8); + $this->addBits(7); } public function brewingStandSlots(array &$slots) : void{ diff --git a/src/data/runtime/RuntimeDataWriter.php b/src/data/runtime/RuntimeDataWriter.php index feaef3698..0b3cd73e3 100644 --- a/src/data/runtime/RuntimeDataWriter.php +++ b/src/data/runtime/RuntimeDataWriter.php @@ -133,15 +133,18 @@ final class RuntimeDataWriter implements RuntimeDataDescriber{ * @phpstan-param array $connections */ public function wallConnections(array &$connections) : void{ - //TODO: we can pack this into 7 bits instead of 8 + $packed = 0; + $offset = 0; foreach(Facing::HORIZONTAL as $facing){ - $this->writeBoundedInt(2, 0, 2, match($connections[$facing] ?? null){ + $packed += match($connections[$facing] ?? null){ null => 0, WallConnectionType::SHORT() => 1, WallConnectionType::TALL() => 2, default => throw new AssumptionFailedError("Unreachable") - }); + } * (3 ** $offset); + $offset++; } + $this->writeBoundedInt(7, 0, (3 ** 4) - 1, $packed); } /**