Pack wall connections into 7 bits for runtime data encoding

This commit is contained in:
Dylan K. Taylor 2023-03-04 15:47:34 +00:00
parent 95c18ef99a
commit b9d62de29d
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D
4 changed files with 12 additions and 8 deletions

View File

@ -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;

View File

@ -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;

View File

@ -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{

View File

@ -133,15 +133,18 @@ final class RuntimeDataWriter implements RuntimeDataDescriber{
* @phpstan-param array<Facing::NORTH|Facing::EAST|Facing::SOUTH|Facing::WEST, WallConnectionType> $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);
}
/**