Skip to content

Instantly share code, notes, and snippets.

@Lincoln-LM
Lincoln-LM / all_portals_ring_star.py
Last active March 9, 2025 22:13
Exact All Portals solver via formulating the problem as the ring star problem and solving that with a MILP solver.
"""Exact All Portals solver via formulating the problem as the ring star problem and solving that
with a MILP solver.
AP paths and this solver differ slightly from ring star as they require only a path and not a cycle.
AP paths additionally can make use of the world spawn point (origin) which technically makes most
edge weights asymmetrical.
The cycle issue is resolved by using a dummy root as the root that has a cost of 0 but must always
connect to the real root.
The edge from the last real node to the dummy root is then just not drawn.
@Lincoln-LM
Lincoln-LM / all_portals_solver.py
Last active March 7, 2025 09:06
Google OR-Tools-based MCSR all portals path finder example; See better & exact solver here: https://gist.github.com/Lincoln-LM/d8985c2074861adc4f27357dfcbe21ae
import numpy as np
from matplotlib import pyplot as plt
from ortools.constraint_solver import routing_enums_pb2
from ortools.constraint_solver import pywrapcp
angle = np.random.random() * np.pi * 2
dist = np.random.randint(22784, 24320 + 1)
STARTING_POINT = (np.cos(angle) * dist, np.sin(angle) * dist)
OR_SCALE_FACTOR = 10000
STRONGHOLD_DATA = (
@Lincoln-LM
Lincoln-LM / showdown.py
Last active January 19, 2025 21:01
Pokemon Showdown V8 engine Math.random() seed cracking. More generic example: https://github.com/Lincoln-LM/js-rng-state-recovery
from array import array
import numpy as np
class Xorshift:
"""Xorshift128+ implementation used in JavaScript Engines"""
def __init__(self, seed: tuple[int]) -> None:
self.state = array("Q", seed)
@Lincoln-LM
Lincoln-LM / gc_fidget.py
Created December 4, 2024 11:55
LCRNG seed searching/cracking from recorded rand(2) outputs via lattice reduction
from fractions import Fraction
from math import ceil
from random import randint
import numpy as np
from sympy import ZZ
from sympy.polys.matrices import DM
from cdd import gmp
import cdd
MOD = 1 << 17
@Lincoln-LM
Lincoln-LM / sequence_with_jumps_64.py
Created October 19, 2024 10:17
Finds rng states of the form (seed_0, 0x82A2B175229D6A5B) that produce a given sequence of 64+ rand(2) observations when rand(121)s happen before each.
"""
Finds rng states of the form (seed_0, 0x82A2B175229D6A5B) that produce a given sequence
of 64+ rand(2) observations when rand(121)s happen before each.
The probability of an arbitrary sequence can be solved for an initial state is roughly
P(X <= max jump count) for X ~ Bin(64, 7/128) (the fact that rand(121) can reject more than once
makes this not exactly correct)
The amount of work needed to solve the sequence scales with CR(64, max jump count)
(combination with replacement; technically sum{0 <= i <= max jump count} CR(64, i) but terms before
@Lincoln-LM
Lincoln-LM / sequence_with_jumps.py
Last active October 17, 2024 13:16
Finds rng states that produces a given sequence of 128 rand(2) observations when rand(121)s happen after each
"""
Finds rng states that produces a given sequence of 128 rand(2) observations when
rand(121)s happen after each.
This example has a max jump count of 2 (# of times total that rand(121)s reject the result
of rand(128) and cause an additional advancement throughout).
The probability that an arbitrary sequence can be solved for an initial state is roughly
P(X <= max jump count) for X ~ Bin(127, 7/128) (the fact that rand(121) can reject more than once
makes this not exactly correct)
@Lincoln-LM
Lincoln-LM / console_unique.cl
Last active October 10, 2024 08:06
3ds GenHashConsoleUnique bruteforcer for Gen 6 initial seed offset finding. Heavily based on https://github.com/zoogie/seedminer/
// sha256_12 taken from https://github.com/zoogie/bfCL/blob/master/cl/sha256_16.cl
// which was modified from https://github.com/ARMmbed/mbedtls/blob/development/library/sha256.c
typedef unsigned int uint32_t;
typedef unsigned char u8;
typedef unsigned short u16;
typedef unsigned int u32;
typedef unsigned long u64;
@Lincoln-LM
Lincoln-LM / gen4ids.py
Last active October 6, 2024 05:09
(Py)OpenCL implementation of searching for a TID/SID in Pokemon DPPT/HGSS (find an initial seed that generates a given 32bit rand as the second mersenne twister call)
import pyopencl as cl
import numpy as np
import time
TID = int(input("TID: "))
SID = int(input("SID: "))
MAX_HOUR_VALUE = input("Max Hour Value (empty for full search): ")
MAX_HOUR_VALUE = int(MAX_HOUR_VALUE or 0xFF)
def untemper(y):
@Lincoln-LM
Lincoln-LM / galar_bird.py
Created September 28, 2024 22:15
SWSH rain & galar bird rng scripts
# search config
MIN_NPC_COUNT = 25
MAX_NPC_COUNT = 40
# unknown what exactly determines this value but it should be consistent for the area and not very high
MIN_FLY_VALUE = 0
MAX_FLY_VALUE = 12
# rng config
BRUTEFORCE_RANGE = 5000
@Lincoln-LM
Lincoln-LM / main.py
Last active August 31, 2024 14:02
Script to play videos on the memo pad poketch app in pokemon diamond & pearl via memory injection on desmume
"""Script to play videos on the poketch memo pad by writing tile data to memory"""
import numpy as np
# hacky; https://github.com/scikit-video/scikit-video/issues/154#issuecomment-1445239790
np.float = np.float64
np.int = np.int_
import skvideo.io
import cv2
import pymem
from pymem.pattern import pattern_scan_all