øAslickproductions.org/forum/index.php?PHPSESSID=5f0fck550j2m4m2fpbtkj2vkm1&topic=2012.0e:/My Web Sites/Slick Productions - FFIV Message Board/slickproductions.org/forum/index7f4c-2.htmlslickproductions.org/forum/index.php?PHPSESSID=5f0fck550j2m4m2fpbtkj2vkm1&board=17.60e:/My Web Sites/Slick Productions - FFIV Message Board/slickproductions.org/forum/index7f4c-2.html.zxŠ9h^ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÈà•¤1ºOKtext/htmlISO-8859-1gzip8:Ö1ºÿÿÿÿÿÿÿÿWed, 11 Mar 2020 01:22:42 GMT0ó°° ®0®P®€§²ð®‰9h^ÿÿÿÿÿÿÿÿu 1º LUT @ C0/FEC0 used for RNG

Author Topic: LUT @ C0/FEC0 used for RNG  (Read 4384 times)

samurai goroh

  • FF5 Hacker
  • *
  • Posts: 221
  • Gender: Male
    • View Profile
    • My page
LUT @ C0/FEC0 used for RNG
« on: February 12, 2015, 12:07:47 PM »
There's a Lookup Table (LUT) located at C0/FEC0, which is used for RNG stuff, like random battle encounters. This table has the size of 256 & contains every number from 0x00 to 0xFF in a random setup.
Code: [Select]
07 B6 F0 1F 55 5B 37 E3 AE 4F B2 5E 99 F6 77 CB
60 8F 43 3E A7 4C 2D 88 C7 68 D7 D1 C2 F2 C1 DD
AA 93 16 F7 26 04 36 A1 46 4E 56 BE 6C 6E 80 D5
B5 8E A4 9E E7 CA CE 21 FF 0F D4 8C E6 D3 98 47
F4 0D 15 ED C4 E4 35 78 BA DA 27 61 AB B9 C3 7D
85 FC 95 6B 30 AD 86 20 8D CD 7E 9F E5 EF DB 59
EB 05 14 C9 24 2C A0 3C 44 69 40 71 64 3A 74 7C
84 13 94 9C 96 AC B4 BC 03 DE 54 DC C5 D8 0C B7
25 0B 01 1C 23 2B 33 3B 97 1B 62 2F B0 E0 73 CC
02 4A FE 9B A3 6D 19 38 75 BD 66 87 3F AF F3 FB
83 0A 12 1A 22 53 90 CF 7A 8B 52 5A 49 6A 72 28
58 8A BF 0E 06 A2 FD FA 41 65 D2 4D E2 5C 1D 45
1E 09 11 B3 5F 29 79 39 2E 2A 51 D9 5D A6 EA 31
81 89 10 67 F5 A9 42 82 70 9D 92 57 E1 3D F1 F9
EE 08 91 18 20 B1 A5 BB C6 48 50 9A D6 7F 7B E9
76 DF 32 6F 34 A8 D0 B8 63 C8 C0 EC 4B E8 17 F8

There are some references on a few files, but it was never brought up to discuss:
instructrtrepe - C2 Bank
Lenophis - Bank C0 disassembly
I'm the best in the universe! Just remember! [F-zero X]

samurai goroh

  • FF5 Hacker
  • *
  • Posts: 221
  • Gender: Male
    • View Profile
    • My page
Re: LUT @ C0/FEC0 used for RNG
« Reply #1 on: February 12, 2015, 12:45:38 PM »
As I mentioned above, one of its uses is to determine whether or not a random battle is triggered.

There's a danger meter, (2 Bytes) that might increase each time you take a step. There's also the position which is increased each time you take a step (1 Byte) & also an offset (1 Byte) which is incremented by 0x11 (17 dec) every time there's an overflow in the position. (Since 17 is a prime number, it makes possible up to 65,536 [256*256] numbers before having the list repeated).

When playing, there are 3 addresses to watch for:
7E16A8 XXXX = Danger
7E0B4F XX = Steps
7E0B5F XX = Steps Overflow Offset

Determining a random encounter:
1. Walk 1 tile
2. Increment "Danger" counter (can be by 0x0010, 0x0040, 0x00E0, 0x0100 or 0x0180 depending on tile)
3. Increment "Steps" counter by 1
4. Use the LUT to take a value to compare using "Steps" as index ( LUT[Steps] )
5. (Add value on 4. with overflow offset) mod 256 (to remain as 1 Byte)
6. Compare result from 5. with the high byte of "Danger" (so is 1 Byte against 1 Byte)
7. If "Danger" is higher than result, then random battle is triggered.

The "Danger" value is always reset after certain events like a transition of screen (new room), opening map or menu (ONLY in world map) or after a battle (can be random or forced).

This explains why is possible to evade battles, which can be exploited on the GBA with the soft reset. And also explains why there isn't a counter that determines how many steps are needed for the next random battle.


This information was explained in 2012 by Omnigamer in this topic:
http://tasvideos.org/forum/viewtopic.php?p=352775#352775


Edit: The danger counter increment seems (so far) to categorize as follow:

0000 - No encounters (Like Towns, save points, etc.)
0010 - Sea
0040 - Ghost Towns (Only parts of: Karnak Castle, Lonka Ruins & Ancient Pyramid)
00E0 - Dungeons
0100 - Outworld
0180 - Moogle Sands
« Last Edit: February 13, 2015, 11:21:49 AM by samurai goroh »
I'm the best in the universe! Just remember! [F-zero X]

Jorgur

  • Moderator
  • *
  • Posts: 437
  • Gender: Male
    • View Profile
Re: LUT @ C0/FEC0 used for RNG
« Reply #2 on: February 13, 2015, 04:32:00 AM »
Interesting! Why not add this to the wiki?

samurai goroh

  • FF5 Hacker
  • *
  • Posts: 221
  • Gender: Male
    • View Profile
    • My page
Re: LUT @ C0/FEC0 used for RNG
« Reply #3 on: February 13, 2015, 09:54:20 AM »
Well, some research on all the uses this table has first would be good I believe.

However, I can add on the ROM overview that the space at C0/FEC0 is a LUT
I'm the best in the universe! Just remember! [F-zero X]

Grimoire LD

  • FF4 Hacker
  • *
  • Posts: 1,684
    • View Profile
Re: LUT @ C0/FEC0 used for RNG
« Reply #4 on: February 13, 2015, 11:24:51 PM »
I thought those sequence of bytes looked familiar...

That Exact Same RNG set up is used in FFIV at 7E1900 in RAM

07 B6 F0 1F 55 5B 37 E3 AE 4F B2 5E 99 F6 77 CB
60 8F 43 3E A7 4C 2D 88 C7 68 D7 D1 C2 F2 C1 DD
AA 93 16 F7 26 04 36 A1 46 4E 56 BE 6C 6E 80 D5
B5 8E A4 9E E7 CA CE 21 FF 0F D4 8C E6 D3 98 47
F4 0D 15 ED C4 E4 35 78 BA DA 27 61 AB B9 C3 7D
85 FC 95 6B 30 AD 86 00 8D CD 7E 9F E5 EF DB 59
EB 05 14 C9 24 2C A0 3C 44 69 40 71 64 3A 74 7C
84 13 94 9C 96 AC B4 BC 03 DE 54 DC C5 D8 0C B7
25 0B 01 1C 23 2B 33 3B 97 1B 62 2F B0 E0 73 CC
02 4A FE 9B A3 6D 19 38 75 BD 66 87 3F AF F3 FB
83 0A 12 1A 22 53 90 CF 7A 8B 52 5A 49 6A 72 28
58 8A BF 0E 06 A2 FD FA 41 65 D2 4D E2 5C 1D 45
1E 09 11 B3 5F 29 79 39 2E 2A 51 D9 5D A6 EA 31
81 89 10 67 F5 A9 42 82 70 9D 92 57 E1 3D F1 F9
EE 08 91 18 20 B1 A5 BB C6 48 50 9A D6 7F 7B E9
76 DF 32 6F 34 A8 D0 B8 63 C8 C0 EC 4B E8 17 F8

I wonder if this also carries to other Square games of the era that used any RNG? I've been wondering how much if FFV is unique to itself and how much was lifted from FFIV or if it was restarted from scratch. This is actually the first proof I've seen of any relation between the two games.

samurai goroh

  • FF5 Hacker
  • *
  • Posts: 221
  • Gender: Male
    • View Profile
    • My page
Re: LUT @ C0/FEC0 used for RNG
« Reply #5 on: February 14, 2015, 10:28:46 AM »
Well, as they say: If it ain't broke, don't fix it.

I made a quick hex search on Final Fantasy VI (Technically 3 because it's the US version) and you can find the same table at C0/FF00. So it would seem at least all 3 SNES games share that LUT for RNG.
I'm the best in the universe! Just remember! [F-zero X]

LightPhoenix

  • FF5 Hacker
  • *
  • Posts: 130
    • View Profile
Re: LUT @ C0/FEC0 used for RNG
« Reply #6 on: March 02, 2015, 08:36:42 PM »
On a similar note, there are two tables used in FF5 located at CE/F400 and CE/F600 that may be duplicated across different games as well.  These are both LUTs for sin x; F400 returns 16-bit precision and F600 returns 8-bit precision.

samurai goroh

  • FF5 Hacker
  • *
  • Posts: 221
  • Gender: Male
    • View Profile
    • My page
Re: LUT @ C0/FEC0 used for RNG
« Reply #7 on: March 02, 2015, 08:55:39 PM »
Not sure what's the sin function used for in the game, but I would guess one use is for when you use the airship and the world has this round effect on the top of the screen.
I'm the best in the universe! Just remember! [F-zero X]

LightPhoenix

  • FF5 Hacker
  • *
  • Posts: 130
    • View Profile
Re: LUT @ C0/FEC0 used for RNG
« Reply #8 on: March 06, 2015, 03:52:30 AM »
It's used in a lot of the spell animations in FF5.  Almost anything that has either a circular motion/pattern or a motion that "eases in" so to speak uses it in the calculations for X and Y.  I can provide an example or two when I've got my laptop on and I'm not quite so drunk.