From 61fe4896b27b9a89d4fd7cf5075724c900c75bf9 Mon Sep 17 00:00:00 2001 From: Nasr Date: Wed, 4 Sep 2024 18:21:37 -0400 Subject: [PATCH] masks --- src/constants.cairo | 5 +++-- src/systems/actions.cairo | 14 +++++++------- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/constants.cairo b/src/constants.cairo index 12389c9..65ab74b 100644 --- a/src/constants.cairo +++ b/src/constants.cairo @@ -1,7 +1,8 @@ pub const X_BOUND: u32 = 100; pub const Y_BOUND: u32 = 100; -pub const ADDRESS_BITMAP: u256 = 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0000_u256; - +pub const ADDRESS_MASK: u256 = 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0000; +pub const POWERUP_MASK: u256 = 0xFF00; +pub const POWERUP_DATA_MASK: u256 = 0x00FF; pub const TILE_MODEL_SELECTOR: felt252 = selector_from_tag!("flippyflop-Tile"); pub const GAME_ID: felt252 = 0x0; \ No newline at end of file diff --git a/src/systems/actions.cairo b/src/systems/actions.cairo index 01efa8e..cb54800 100644 --- a/src/systems/actions.cairo +++ b/src/systems/actions.cairo @@ -15,7 +15,7 @@ mod actions { use core::poseidon::poseidon_hash_span; use dojo::model::{FieldLayout, Layout}; use flippyflop::tokens::flip::{IFlip, IFlipDispatcher, IFlipDispatcherTrait}; - use flippyflop::constants::{GAME_ID, ADDRESS_BITMAP, X_BOUND, Y_BOUND, TILE_MODEL_SELECTOR}; + use flippyflop::constants::{GAME_ID, ADDRESS_MASK, POWERUP_MASK, POWERUP_DATA_MASK, X_BOUND, Y_BOUND, TILE_MODEL_SELECTOR}; fn get_random_powerup(seed: felt252) -> PowerUp { let tx_hash = get_tx_info().transaction_hash; @@ -48,18 +48,18 @@ mod actions { }; let mut packed: u256 = 0_u256; - packed = packed | (address_bits & ADDRESS_BITMAP); - packed = packed | ((powerup_type * 256_u256) & 0xFF00_u256); - packed = packed | (powerup_data & 0x00FF_u256); + packed = packed | (address_bits & ADDRESS_MASK); + packed = packed | ((powerup_type * 256_u256) & POWERUP_MASK); + packed = packed | (powerup_data & POWERUP_DATA_MASK); packed.try_into().unwrap() } fn unpack_flipped_data(flipped: felt252) -> (ContractAddress, PowerUp) { let flipped_u256: u256 = flipped.into(); - let address: felt252 = (flipped_u256 & ADDRESS_BITMAP).try_into().unwrap(); - let powerup_type: felt252 = ((flipped_u256 & 0xFF00_u256) / 256_u256).try_into().unwrap(); - let powerup_data = flipped_u256 & 0x00FF_u256; + let address: felt252 = (flipped_u256 & ADDRESS_MASK).try_into().unwrap(); + let powerup_type: felt252 = ((flipped_u256 & POWERUP_MASK) / 256_u256).try_into().unwrap(); + let powerup_data = flipped_u256 & POWERUP_DATA_MASK; let powerup = match powerup_type { 0 => PowerUp::None,