don't click here

Sonic 3 Blue Spheres: Relationship between Stages and Levels numerical system?

Discussion in 'General Sonic Discussion' started by BlueSpheres, Feb 28, 2024.

  1. DigitalDuck

    DigitalDuck

    Arriving four years late. Member
    5,448
    500
    93
    Lincs, UK
    TurBoa, S1RL
    The level number shown by the game is one more than the internal level number (which starts at 0). levelnum = 1 in those algorithms corresponds to level 2. This is the reason the last line of my code is return level + 1.

    Level 1: 00, 01, 02, 03 becomes stage (0 * 128^3) + (1 * 128^2) + (2 * 128^1) + (3 * 128^0) = 16384 + 256 + 3 = 16643
    Level 2: 01, 04, 07, 10 becomes stage (1 * 128^3) + (4 * 128^2) + (7 * 128^1) + (10 * 128^0) = 2097152 + 65536 + 896 + 10 = 2163594

    Because 128016000 has nine digits.

    I imagine it's just intended as a sanity check to prevent things breaking rather than an ending point, and it's easy to check against a bit than an arbitrary value.
     
  2. BlueSpheres

    BlueSpheres

    Member
    9
    0
    1
    So, quoting the Sonic Wiki Zone:

    Based on Blue Sphere Generator the highest combination of chunks is 127, 125, 123, 121.

    So the resulting stage is:
    127×128^(3)+125×128^(2)+123×128^(1)+121x128^(0) = 268.402.169;

    The point is that more answer I get, more questions popped from my head

    1. Why the map generator created by MainMemory use a different chunk positioning respect to the one I linked below?
    2. Does anyone imagine why the pattern of numerator is (N-1), (3N-2),... And the possible reason why the denominator pattern is 128, 127,...?
    3. In the Digital duck formula, why I have different exponent? I suppose it is for a positional purpose but I prefer ask it

    [Sorry for so many questions.
    Furthermore, I can imagine it could be annoying since I assumed that for the members of the forum are trivial questions.
    I really appreciate your support!]
     
  3. MainMemory

    MainMemory

    Kate the Wolf Tech Member
    4,799
    385
    63
    SonLVL
    I'm not sure why, but the generator you linked has rotated everything by 90 degrees. Mine matches the way the game's code handles things internally. Technically the layouts end up the same though. Also, the highest combination of chunks that make a valid level is actually 127, 126, 125, 124:
    upload_2024-3-30_21-24-47.png
     
  4. DigitalDuck

    DigitalDuck

    Arriving four years late. Member
    5,448
    500
    93
    Lincs, UK
    TurBoa, S1RL
    As MainMemory said above, the highest stage number that comes from a level is from level 26898304, which has chunks 127, 126, 125, and 124 and gives a stage number of 268418812. The highest stage number possible is 268435455 with chunks 127, 127, 127, 127, but this can't be created from a level number.

    If they were all just n (or n-1 if you're using the visual level number) then you'd have all chunks the same until they hit the modulus, and it'd be too predictable and not "random". If each chunk was modulo 128, then you'd only have at most 128 different levels, regardless of what you're doing with the rest of the formula. The number of unique levels is calculated by taking the lowest common multiple of each modulus; the lowest common multiple of 128, 127, 126, and 125 is 128016000.

    The multipliers are probably 1, 3, 5, and 7 because 1, 2, 3, 4 would make the third chunk have a common factor in both its multiplier and its modulus; this would mean it would have a cycle of only 42 chunks instead of 126. 126 is very much a poor modulus choice here - it shares a factor of 2 with 128; this is why if the first chunk is even, the third chunk must also be even, and if the first chunk is odd, the third chunk must also be odd. If it were replaced with 123 to create 128, 127, 123, 125, you'd have 249936000 unique levels. Note that 128, 127, 125, 123 would only have 49987200 levels, as the third chunk would have a cycle of 25 levels instead of 125 (only giving you chunk numbers ending in 2 or 7) unless you also changed the multiplier; incidentally, 1, 2, 3, 4 would work in this case.

    A better way to do it, although harder on Mega Drive hardware (as it requires multiplying two 32-bit numbers together, although it can be done a bit more easily as we only need the lower bits), would be to take the level number and multiply it by a suitable constant (e.g. 0x9E3779B, or phi * 2^28, for maximum "randomness" but any constant works as long as it's odd), and then read the last 28 bits as your stage number. This would give you the maximum 268435456 distinct levels.

    In a decimal number, 1848 represents the number you get when you do (1 * 10^3) + (8 * 10^2) + (4 * 10^1) + (8 * 10^0) = 1000 + 800 + 40 + 8 = 1848

    The stage number is just that but in base 128 instead, because there are 128 chunks.
     
    • Informative Informative x 2
    • Like Like x 1
    • Agree Agree x 1
    • Useful Useful x 1
    • List