Encoding game variables into a save code!

This js code will convert game variables into a Nintendo-like "cheat" code

These are the variables that are stored in the code:

planetLoc: current planet (0-15)

mapLoc: current location on planetary map 0-4095 (64x64 grid)

bearing: 0-7 representing which of the 4 cardinal directions (NWES) or 4 ordinal directions (NE,SW,etc) player is facing. 0=N, 1=NE, 2=E, etc...

inventory: a 16-bit number that can be decoded into the map locations of up to 16 inventory objects

...and a 6-bit check digit

  • Retrieve player progress (values stored in localStorage).
  • Convert values into binary segments following the same bit allocation.
  • Compute the check digit for validation.
  • Convert the full binary string into an alphanumeric code (using a Base64-like approach).

  • planetLoc 4 bits + mapLoc 12 bits + bearing 3 bits + inventory 16 bits + checkDigit 6 bits = 41 bits

    Concatenating all values into a binary string:

    0101 010011010010 110 0111100110000001

    divide string of 1's and 0's into 6-bit chunks and convert each to a character from the Base64-like encoding table:
    (ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/).

    0=A, 1=B, 2=C, etc. the final code is "VNLPMCO"