Skip to content

Instantly share code, notes, and snippets.

@ZoomTen
Created November 5, 2024 14:59
Show Gist options
  • Save ZoomTen/9b7945bcab324ba72d66b5cac7018c75 to your computer and use it in GitHub Desktop.
Save ZoomTen/9b7945bcab324ba72d66b5cac7018c75 to your computer and use it in GitHub Desktop.
from std/strutils import nil
type
uint4 = 0'u8 .. 15'u8
Dv {.packed.} = object
defense {.bitsize: 4.}: uint4
attack {.bitsize: 4.}: uint4
special {.bitsize: 4.}: uint4
speed {.bitsize: 4.}: uint4
MoveType = enum
Normal = 0
Fighting
Flying
Poison
Ground
Rock
Bird
Bug
Ghost
Steel
UnusedTypes
Curse = 19
Fire
Water
Grass
Electric
Psychic
Ice
Dragon
Dark
HiddenPowerStat = tuple[power: uint8, moveType: MoveType]
func dvToHiddenPowerType(dv: Dv): HiddenPowerStat =
result.power = block:
let
spcHighBit = (dv.special and 0b1000)
spdHighBit = (dv.speed and 0b1000)
defHighBit = (dv.defense and 0b1000)
atkHighBit = (dv.attack and 0b1000)
highBitCombined =
((spcHighBit shr 3) or (spdHighBit shr 2) or (defHighBit shr 1) or atkHighBit)
(((highBitCombined * 5) + (dv.special and 0b11)) shr 1) + 31
result.moveType = block:
var x = (dv.defense and 0b11) + ((dv.attack and 0b11) shl 2)
block adjustTypeIndex:
inc x # skip normal
if x < ord(MoveType.Bird):
break adjustTypeIndex
inc x # skip BIRD
if x < ord(MoveType.UnusedTypes):
break adjustTypeIndex
x += (ord(MoveType.Curse #[end of unused types]#) + 1 - ord(MoveType.UnusedTypes))
typeof(result.moveType)(x)
when isMainModule:
for i in low(uint16)..high(uint16):
echo dvToHiddenPowerType(cast[Dv](i))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment