øAslickproductions.org/forum/index.php?PHPSESSID=5f0fck550j2m4m2fpbtkj2vkm1&topic=1235.0e:/My Web Sites/Slick Productions - FFIV Message Board/slickproductions.org/forum/indexb6a8-2.htmlslickproductions.org/forum/index.php?PHPSESSID=5f0fck550j2m4m2fpbtkj2vkm1&board=17.100e:/My Web Sites/Slick Productions - FFIV Message Board/slickproductions.org/forum/indexb6a8-2.html.zx :h^ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÈ °VHÚOKtext/htmlISO-8859-1gzip@øÕHÚÿÿÿÿÿÿÿÿWed, 11 Mar 2020 01:24:51 GMT0ó°° ®0®P®€§²ð® :h^ÿÿÿÿÿÿÿÿi*HÚ Investigating the Battle System

Author Topic: Investigating the Battle System  (Read 4312 times)

LightPhoenix

  • FF5 Hacker
  • *
  • Posts: 130
    • View Profile
Investigating the Battle System
« on: October 02, 2009, 01:04:24 PM »
I've just started looking into hacking FF5, and a question I've had is how the battle system knows which attack type to call, both generally and particularly with regards to Fight.  Anyway, this might be something already known, but I haven't seen documentation on it, so I thought I'd share.

I'd been going off of instructrtrepe's C2 bank, since it was better commented.  The attack type subroutines start at C2:6870, and process from there.  This is common knowledge, it seems.  However, how does the system know which type to call?  Given the variable size of the subroutines, I figured it had to be an index of addresses.  Keeping in mind little-endian coding, I did a search for two addresses in sequence - 6EE1 (Atk Type 31/Swords) and 6F1E (AT 32/Knives).  Sure enough, it turns out that there is an index of every attack type subroutine two-byte addresses starting at C2:7B21 through C2:7C0C.  Incidentally, this information is incorrectly presented as code in instructrtrepe's assembly file.  Lenophis' dump does not present the section as code.

I'm still looking into things, but I thought I'd share since I didn't see it anywhere on the site.

Jorgur

  • Moderator
  • *
  • Posts: 437
  • Gender: Male
    • View Profile
Re: Investigating the Battle System
« Reply #1 on: October 02, 2009, 01:25:05 PM »
Long ago, I also looked into that particular issue. I couldn't find much, but I did find that pointer table you mention, in the same way as you did. Instructrtrepe didn't spot this table in his C2 doc, so it hasn't been publicly known before now. Anyway, this is how I saw it:

The address is C2/7B21. You will find the first address pointing to some dummy code, the second pointing to the Monster fight code, third pointing to the Monster Specialty code, and so on (see attack formula list below). I belive the game system uses this table every time an action is taken.

Code: [Select]
00 No effect
01 Monster Fight
02 Monster Specialty
03 Magic Sword 1
04 Magic Sword 2
05 Magic Sword 3
06 Offensive magic
07 HP based damage + status
08 Pierce defense
09 Random damage
0A Physical magic
0B Level based damage + status
0C Pierce defense + HP leak
0D HP drain
0E MP drain
0F HP to critical
10 Restore HP
11 Restore full HP
12 Status 0 infliction
13 Status 1 infliction
14 Status 2 infliction
15 Toggle status
16 Reversing status infliction
17 Instant death
18 Instant destroy
19 Status removal
1A Resurrection
1B Whispering Wind
1C Element attribute change
1D View stats
1E Drag
1F Void
20 End battle
21 Reset
22 Quick
23 Earth Wall
24 Constant HP restored
25 Constant MP restored
26 Full HP/MP
27 Status added
28 Constant damage
29 Status 3 inflicted
2A HP based damage + HP leak
2B User HP based damage
2C Random effect
2D Ground attacks
2E Reaper's Sword
2F ?
30 Unarmed
31 Swords
32 Knives
33 Spears, Double Lance
34 Axes, Staves
35 Status infliction bows
36 Bows
37 Katanas
38 Whips
39 Bells
3A Long reach axes
3B Rods
3C Rune weapons
3D Death Claw
3E Failure (EL+TS)
3F Zombie Breath
40 Change row
41 ?
42 HP and status restored
43 !Steal
44 Escape
45 !Throw
46 !GilToss
47 !Tame
48 !Catch
49 Flirt
4A !Dance
4B L5 Doom
4C L2 Old
4D L4 Qrter
4E L3 Flare
4F Spirit
50 Goblin Punch
51 Stats up
52 Mucus
53 MP damage
54 User HP based damage (negative)
55 Fusion
56 ?
57 Random effect
58 Mind Blast
59 HP up
5A White Wind
5B ?
5C Hug
5D Zombie status
5E ?
5F ?
60 ? (ends "Unhide" statements)
61 "Stalker Attack"
62 "Byblos Attack"
63 Grand Cross
64 Chicken Knife
65 Interceptor Rocket
66 Targeting
67 Pull
68 End battle
69 !Control
6A ? (Sandworm)
6B Attribute change
6C Monster type bonus
6D Vampire
6E Brave Blade
6F Fight (strong)
70 Wormhole
71 Level Down
72 Bows strong vs. monster type
73 Spears Strong vs. monster type
74 ?
75 ?
76 ?
77 ?
78 ?
79 ?
7A ?
7B ?
7C ?
7D ?
7E ?
7F No attack

I am also curious about the path from a selected ability to its attack formula code. Some abilities  have their formula # stored with their Action data (see my docs), but for most abilities, that value is "00". These abilities (e.g Fight) have their own piece of executed code, but I have yet to find whatever points each ability to their associated code.

 :edit:
By the way, congrats on your first post here!  :happy:
I hope you will be an active member of our boards.
« Last Edit: October 02, 2009, 02:49:04 PM by Jorgur »

samurai goroh

  • FF5 Hacker
  • *
  • Posts: 221
  • Gender: Male
    • View Profile
    • My page
Re: Investigating the Battle System
« Reply #2 on: October 02, 2009, 02:30:45 PM »
Looking at Jorgur's list, is what I have in my PAR codes as Attack Type...:

Code: [Select]
7E2057 XX          1st Player Attack Formula
7E20D7 XX          2nd Player Attack Formula
7E2157 XX          3rd Player Attack Formula
7E21D7 XX          4th Player Attack Formula

7E7B2D XX          Attack Type

You can watch this memory address in real time if you use the Modified version of Snes9x or the Memory Watch tool (not supported with the newest versions of Snes9x though)...
I'm the best in the universe! Just remember! [F-zero X]

LightPhoenix

  • FF5 Hacker
  • *
  • Posts: 130
    • View Profile
Re: Investigating the Battle System
« Reply #3 on: October 09, 2009, 07:23:28 PM »
Well, I'm working on analyzing it.  I've identified the part where it sends the code to the relevant damage formula (C2:6510), which is an index starting at C2:7B21.  This is addressed directly as C2, not the 7E bank.  In theory, it'd be relatively trivial to redirect the table, so as to add additional damage formulas.

Right now, I'm looking at tracing the Fight command, to determine how the game stores/accesses the correct damage formula.  I've identified 7E:7B2D as a temporary holder for the correct weapon damage formula.  For example, when attacking with a sword, this variable is 31, whereas with a dagger it is 32.  Unfortunately, I've been unable thus far to determine what routine sets this value.  I'm relatively certain it's simply a matter of searching for the correct STA command, however there are 32 commands that set 7E:7B2D.  It might take a little time.

More interestingly, in tracing the combat, it seems that the C1 bank is also heavily involved.  There are also small bits of code in the D0 bank that run.  I disassembled the C1 bank with Tracer, however I haven't had a chance to analyze it in depth.  I can say that it uses quite a bit more indirect addressing than the C2 bank, which will make it more challenging to read.  I'd be willing to share it with everyone, however it's larger than the allowed attachment size.

Jorgur

  • Moderator
  • *
  • Posts: 437
  • Gender: Male
    • View Profile
Re: Investigating the Battle System
« Reply #4 on: October 10, 2009, 08:23:58 AM »
This looks promising. We know very little about the C1 bank, so a complete disassembly of it would be a great addition to the docs.

When it's done, you can send it to the slickproductions site, but you probably need a place for work-in-progress storing. I may be able to get you uploading permission at aerdan.org, which allows for much larger file sizes. For now, you can send me anything you want uploaded via e-mail, and I'll give you the link for it.

I'll also ask the admins to give you FF5 hacker status.

Jorgur

  • Moderator
  • *
  • Posts: 437
  • Gender: Male
    • View Profile
Re: Investigating the Battle System
« Reply #5 on: November 25, 2009, 07:26:42 PM »
Any progress on the C1 disassembly?

LightPhoenix

  • FF5 Hacker
  • *
  • Posts: 130
    • View Profile
Re: Investigating the Battle System
« Reply #6 on: January 01, 2010, 12:23:59 PM »
Sorry for dropping off the face of the earth.  I have not had as much time as I did when I started, due to starting a new job.

The C1 bank is devilishly complex to decipher, much more so than the battle functions.  There's a lot of indirect addressing (ie, pointers) and index tables.  However, as I thought, it does in fact control battle animation and graphics.

The C1 bank is called from C2 by function 0069:

Code: [Select]
C2/0069: 22 00 00 C1  JSR $C10000
C2/006D: 60           RTS

Once in the C1 bank, there's an immediate indirect call to a function dictated by the value in A.  This value is set before the call to C1:0000, and can range from 00 to 0E.  Thus, there are sixteen possible functions which I've taken to calling "C1 Functions."  Interestingly, functions 4 and 8 don't seem to be called by C2.

Code: [Select]
C1/0000: 4C 0C 00    JMP $000C

C1/000C: 0A          ASL A               ; Get indirect address from index table based on A     
C1/000D: AA          TAX                 ; (Defined before calling this bank)     
C1/000E: BF 21 00 C1 LDA $C10021,x 
C1/0012: 85 7A        STA $7A       
C1/0014: BF 22 00 C1 LDA $C10022,x 
C1/0018: 85 7B        STA $7B       
C1/001A: 20 1E 00       JSR $001E   
C1/001D: 6B          RTL ; RTS Long - Return to C2 Bank

C1/001E: 6C 7A 00    JMP ($007A) ; Indirect jump to address at $7A:7B

The most interesting of these is C1 Function A, which is called nine times.  Preventing it from being called causes numerous graphical glitches in battle (but none on the map).  I'm currently working on it, but there are some additional snags.

One subroutine, C1:02F2 is called 83 times throughout C1, including several in C1 Function A.  This was obviously of interest, and starts off like so:

Code: [Select]
********************************
C1:02F2
********************************
C1/02F2 20 1D FD    JSR $FD1D     
C1/02F5 20 6E 3A    JSR $3A6E     
C1/02F8 20 2D AD    JSR $AD2D     
C1/02FB 4C EF 37    JMP $37EF     

********************************
C1:FD1D
********************************
C1/FD1D 22 AA 7C D9 JSL $D97CAA   
C1/FD21 60          RTS                     

********************************
D9:7CAA
********************************
D9/7CAA E6 A5       INC $A5
D9/7CAC A5 A5       LDA $A5
D9/7CAE 0D 9A DB    ORA $DB9A
D9/7CB1 D0 F9       BNE $7CAC
D9/7CB3 6B          RTL

As you can see, there's a problem in the D9:7CAA subroutine - in theory if $A5 starts at 0 (and it does), you will never break out of this loop.  However, an interrupt is called on vertical blanking that breaks out of this function; $A5 is set to 0 during this interrupt.  It's during this function that I believe graphics are drawn to the screen.  It also contains a large number of the subroutines in the C1 bank, as well as subroutines in C2, C4, D0, and others.

I'm currently working on understanding this "VBlank Function" right now.

Incidentally, most of the other C1 Functions seem to be called in very specific situations which I have yet to determine.  In fact, many of the calls in C2 to C1 Fxn A seemed to have no effect when replaced with NOPs.  Thus far, I've determined that the Fxn A call in C2:4904 is involved with character animations, and C2:5481 with the GP/XP/AP windows.  One of the calls is also involved with the treasure screen, but I don't know which one.

Jorgur

  • Moderator
  • *
  • Posts: 437
  • Gender: Male
    • View Profile
Re: Investigating the Battle System
« Reply #7 on: January 03, 2010, 04:01:09 AM »
Glad to see you're still active, more or less. Also good to see you are making some progress.

In case you come across it, I'm looking for the connection between the Attack Index list and the actual animations/sound effects. My guess is one pointer table within one of the D-banks pointing to graphics and one somewhere in the C-area pointing to sound effects (in order by the Attack Index list).

LightPhoenix

  • FF5 Hacker
  • *
  • Posts: 130
    • View Profile
Re: Investigating the Battle System
« Reply #8 on: January 03, 2010, 11:06:01 AM »
I was just thinking about that same link myself.  I'll let you know if I come across it.

I am relatively certain that despite all the indirect addressing in C1, any links to be found will be in C2 or the D banks.  Given how C1 is called, I believe all of the animation information is set up somewhere when the attack is called in C2.  C1 is then called with those parameters in place, setting up the animation.  My hope is that if this can be discovered, it can shed light on how animation works in C1.