12 years ago #14


1. Improved PRNG

Developed by anomie

I've been looking at enhancing the random number generator to put in something that has a period better than 256. It turns out to be pretty easy to add a fairly good PRNG with a period of 65535. We've got plenty of bytes from eliminating the lookup tables, and hopefully increasing the overhead from 14 to 38 cycles doesn't break anything.

The bad news is, the "steps until next battle" will be screwed up if we just replace the PRNG. The way it works now, it gets a 'threshold' number depending on where you are (0xa for world map, 3 for ship, and a value from the table at 0x2cc11 for maps). Then each step chooses the next random number, and if the number is less than the threshold you get a battle. Due to the layout of the PRNG lookup table, it ends up at 11-40 steps for the world map, 30-124 for the ship, and 11-46 for most maps[1]. With my new PRNG, those change to 1-207, 1-585, and 1-293. Not good.

[1] Sky Castle 5F gets 1-27; the elemental ToFR floors get 11-46 (but with more bias to smaller values), 11-40, 11-32, and 7-32; and Chaos's floor would get 142-370.

I think I might have a solution. If I keep a counter that starts as a random number between 120 and 375 and subtracts a location-specific value for each step, I get the following encounter rates:

Ship: Current 30-124 steps, avg 85.3; New 41-126, avg 83.4.
World Map: Current 11-40, avg 25.6; New 13-38, avg 25.3.
Most maps: Current 11-46, avg 32.0; New 16-47, avg 21.4.
Warmech's Bridge: Current 1-27, avg 10.7; New 6-16, avg 10.8.
ToFR Earth floor: Current 11-46, avg 28.4; New 14-42, avg 28.2.
ToFR Fire floor: Current 11-40, avg 25.6; New 13-38, avg 25.3.
ToFR Water floor: Current 11-32, avg 23.3; New 11-35, avg 23.1.
ToFR Air floor: Current 7-32, avg 21.3; New 11-32, avg 21.2.
ToFR Chaos floor: Current 142-370, avg 256.0; New 121-376, avg 249.2. (not that this is used anyway)

The summary is that the averages are about the same, but the shortest time between battles is larger. It should respond reasonably to changing terrain.

As for increasing rarity of monster encounters, the new PRNG generates every value 1-255 with equal probability (256/65535) and 0 with probability 255/65535 so the only change there would actually ever-so-slightly increase the probability of rare encounters. OTOH, I'm going to have to lose the part where the "which random battle do you get" PRNG is completely independent of the "steps until next battle" and "battle action" PRNGs, unless I can find an extra byte or two somewhere in RAM.

035db6: ea ea ea ea 20 e7 fc

03c0b9: 4c 02 fd

03c0c4: 20 1a fd

03c56e: 20 e7
03c570: fc 29 3f aa bc 8c c5 b1 10 85 6a 18 60 ea ea ea
03c580: ea 38 a5 f5 e5 f8 85 f5 a5 f6 e9 00 b0 0b 20 e7
03c590: fc 18 69 78 85 f5 a9 00 2a 85 f6 60

03c517: 85 f8 20 71 c5 ea ea

03cdd7: 20 71 c5 ea ea

03f110: ea ea ea ea ea ea ea ea ea ea ea ea ea ea ea ea
[... repeat until ...]
03f200: ea ea ea ea ea ea ea ea ea ea ea ea ea ea ea ea

03fcf7: 06 f7 6e 8a 68 90 0e a5 f7
03fd00: 49 4b 85 f7 ad 8a 68 49 5f 8d 8a 68 ad 8a 68 45
03fd10: f7 60 a2 00 8a 7d 00 61 e8 d0 fa 85 f7 0d 8a 68
03fd20: d0 02 e6 f7 20 7e c5 4c b7 c0 20 6d c7 a2 00 8a
03fd30: 7d 00 61 e8 e8 d0 f9 8d 8a 68 8a 7d 01 61 e8 e8
03fd40: d0 f9 85 f7 0d 8a 68 d0 02 e6 f7 4c 7e c5 ea ea
03fd50: ea ea ea ea ea ea ea ea ea ea ea ea ea ea ea ea
[... repeat until ...]
03fdf0: ea ea ea ea ea ea ea ea ea ea ea ea ea ea ea

The function posted earlier is the actual PRNG function. The other bits replace calls to the other old PRNG, implements a new random encounter algorithm (the old algorithm depends on specific characteristics of the old PRNG), and seeds the PRNG when the game is turned on to prevent the Kyzoku farming and the like.