Skip to content

Instantly share code, notes, and snippets.

@mathewmariani
Last active September 19, 2023 20:45
Show Gist options
  • Save mathewmariani/8b416c7340e867d911318ef5ec9121e6 to your computer and use it in GitHub Desktop.
Save mathewmariani/8b416c7340e867d911318ef5ec9121e6 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdint.h>
static uint8_t reg[4] = {0x0, 0x0, 0x0, 0x0};
void reset()
{
for (uint8_t i = 0; i < 4; ++i)
{
reg[i] = 0x0;
}
}
void tick(int8_t y)
{
reg[0] = 5 * reg[0] + 1;
reg[1] = ((reg[1] & 0x80) == (reg[1] & 0x10)) ?
2 * reg[1] + 1 : 2 * reg[1];
reg[2+y] = (reg[0] ^ reg[1]);
}
uint16_t number(void)
{
for (int8_t y = 1; y >= 0; --y)
{
tick(y);
}
return ((uint16_t)reg[2] << 0x8) | reg[3];
}
int main()
{
for (int i = 0; i < 27776; ++i)
{
printf("%d\n", number());
}
return 0;
}
class SuperMarioWorldRNG {
constructor() {
this.rng = new Uint8Array(4)
}
get number() {
for (let y = 1; y >= 0; --y) {
this.tick(y)
}
return (this.rng[2] << 0x8) | this.rng[3];
}
tick(y) {
this.rng[0] = 5 * this.rng[0] + 1
this.rng[1] = ((this.rng[1] & 0x80) == (this.rng[1] & 0x10)) ?
2 * this.rng[1] + 1 : 2 * this.rng[1]
this.rng[2+y] = this.rng[0] ^ this.rng[1]
}
reset() {
for (let i = 0; i < 4; ++i) {
this.rng[i] = 0x0
}
}
}
console.log("Super Mario World RNG")
let rng = new SuperMarioWorldRNG()
// for (let i = 0; i <= 27776; ++i) {
// console.log(rng.number)
// }
let particle_speed = function() {
let rnd = rng.number
return {
x: (rnd >>> 8) & 0x7,
y: (rnd & 0x7),
}
}
console.log(particle_speed())
local bit = require("bit")
local band, bor, bxor = bit.band, bit.bor, bit.bxor
local lshift, rshift = bit.lshift, bit.rshift
local rng = { 0x0, 0x0, 0x0, 0x0 }
local tick = function(y)
rng[1] = band(5 * rng[1] + 1, 0xff)
rng[2] = band(rng[2], 0x80) == band(rng[2], 0x10)
and 2 * rng[2] + 1 or 2 * rng[2]
rng[2] = band(rng[2], 0xff)
rng[3+y] = bxor(rng[1], rng[2])
end
local number = function()
for y = 1, 0, -1 do
tick(y)
end
--return rng[3], rng[4]
return bor(lshift(rng[3], 0x8), rng[4])
end
for i = 0, 27776 do
print(number(), rng[3], rng[4])
end
class SuperMarioWorldRNG {
static reset() {
__reg = [0x0, 0x0, 0x0, 0x0]
}
static number {
for (y in [1, 0]) { tick(y) }
return (__reg[2] << 0x8) | __reg[3]
}
static tick(y) {
__reg[0] = ((5 * __reg[0] + 1) >> 0) & 0xFF
__reg[1] = ((__reg[1] & 0x80) == (__reg[1] & 0x10)) ?
2 * __reg[1] + 1 : 2 * __reg[1]
__reg[1] = (__reg[1] >> 0) & 0xFF
__reg[2+y] = ((__reg[0] ^ __reg[1]) >> 0) & 0xFF
}
}
SuperMarioWorldRNG.reset()
(1..25).each {
System.print(SuperMarioWorldRNG.number)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment