Log In  


Cart #pattern_shifter_nonexdog-3 | 2024-12-19 | Code ▽ | Embed ▽ | No License
2

Small disclaimer: You are free to use these in your projects, free of charge !! Only thing I ask in return is attribution by crediting me somewhere in the project.

Here are two little code snippets to shift patterns!! The token cost is 55 tokens for the vertical shifting code and 76 tokens for the horizontal shifting code. These code snippets also account for additional settings on bits 0b0.111 of the pattern (to learn what the hell that is, check this part of the manual). I am choosing to leave the snippets as they are in the cartridge in here to people can see (and try to understand if they want to) what is going on under the hood without reading something that looks like a keysmash.

function shift_pattern_v(pat, offset)
 offset = flr(offset) % 4

 local mask = 0x0000.ffff <<> 4 * offset
 local copy = pat & mask
 copy <<= (4 - abs(offset)) * 4 * sgn(offset)

 local shifted = flr(pat >>> 4 * offset)

 return shifted | copy | pat & 0b.111
end


function shift_pattern_h(pat, offset)
 offset = flr(offset) % 4
 local new_pattern = 0

 for i = 0, 3 do
  local nibble = pat >>> 4 * i
  nibble &= 0xf

  local mask = 0x00f0.f << offset
  local copy = nibble & mask
  copy <<= (4 - abs(offset)) * sgn(offset)

  nibble >>>= offset
  nibble |= copy
  nibble &= 0xf

  nibble <<= 4 * i
  new_pattern |= nibble
 end

 return new_pattern | pat & 0b.111
end

That being said, if there is a need to use a bit less tokens, I also provide these "squished down" versions, which are 46 and 72 tokens respectively (aka the keysmash versions).

function shift_pattern_v(pat, offset)
 offset = flr(offset) % 4

 return flr(pat >>> 4 * offset) | (pat & 0x0000.ffff <<> 4 * offset) << (4 - abs(offset)) * 4 * sgn(offset) | pat & 0b.111
end

function shift_pattern_h(pat, offset)
 offset = flr(offset) % 4
 local new_pattern = 0

 for i = 0, 3 do
  local nibble = pat >>> 4 * i & 0xf

  local copy = (nibble & 0x00f0.f << offset) << (4 - abs(offset)) * sgn(offset)

  nibble = (nibble >>> offset | copy) & 0xf

  new_pattern |= nibble << 4 * i
 end

 return new_pattern | pat & 0b.111
end

2



[Please log in to post a comment]