Skip to content

Instantly share code, notes, and snippets.

@tildejustin
Created August 25, 2024 02:22
Show Gist options
  • Save tildejustin/0b0770726ac89ff7d2ad59fb23cf1bee to your computer and use it in GitHub Desktop.
Save tildejustin/0b0770726ac89ff7d2ad59fb23cf1bee to your computer and use it in GitHub Desktop.
something about spawn logic that i wrote sometime last year

assuming getSpawnRadius -> 10 for now for simplicity
and assuming player is further from the world border than the spawnRadius, again, for simplicity
i = 10: spawnRadius
l = 21: width / height of spawn square
m = 441: area of spawn square
k = m = 441: area of spawn square (integer overflow safe)
n = 17: spawnOffsetMultiplier, is 17 if area of spawn square is > 16, which it almost certainly is
o is random called in range of k (spawn square area)
p = 0: start of a loop maxing out in k steps, p is incremented every time
in loop:

q = o + n * p % k: random + spawnOffsetMultiplier * p mod area of spawn square
first iteration: 0
r = q mod side length of square
s = q div side length of square

proof that all spawns are still possible on 1.18 within the first iteration (p is 0), with the one random call in the code

import random
import math

results: set[tuple[int, int]] = set()
results_length = 0
spawn = (0, 0)
finished = False
i = 10
l = i * 2 + 1
k = l**2
count = 0

while (not finished):
    count += 1
    o = random.randint(0, k - 1)
    q = math.fmod(o, k)
    r = math.fmod(q, l)
    s = q // l
    results.add((spawn[0] + r - i, spawn[1] + s - i))
    # if len(results) > results_length:
    #     results_length = len(results)
    #     print(results)
    if len(results) == k:
        finished = True

print(finished)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment