[FE7] The Official AI Documentation Thread

Pretty much.
Each unit’s AI byte 1 and AI byte 2 leads to multiple commands/routines that actually determine the unit’s actions. For example:

AI byte 2 0x05 (loot-n-escape AI used by thieves)

10 00 FF 00 00000000 00000000 00000000
02 06 0C 00 00000000 00000000 00000000
03 00 FF 00 00000000 00000000 00000000

Post #16 documents the general function of FE7’s unit AI bytes. (Also it seems the recent forum update messed up the appearance of wiki posts)

Oh, I see. Good thing I didn’t spend any actual time/effort in finding it. I just kind of found it by accident since it’s located just before the Door Key, Lockpick, and Antitoxin routine table.

Do we know the routine in which the game determines items the AI uses to heal?

Routine for AI using Vulnerary and Elixir:
Routine at 080397DC. Called at 08034F62
080397FA 286B cmp r0, #0x6B // Vulnerary
08039804 286C cmp r0, #0x6C // Elixir

Sweeeet thanks gryz now I can look at implementing Tonics for AI

“1(0x02): Recovery mode? Has moved?”

– my docs on AI Flags.

“08039808 4914 ldr r1,=#0x203A8EC @Vuln. Start of list of Unit IDs to process for AI (?)
0803980A 317B add r1,#0x7B @Idk state of enemy? Enemy flags? I called it “AI Flags”
0803980C 2002 mov r0,#0x2
0803980E 7809 ldrb r1,[r1]
08039810 4008 and r0,r1 @I think this is recovery mode.”

Code for vulnerary.

Guys
Guys
I think I got something right for once lol

1 Like

You got tonics to work? Great! (I was afraid I was going to be asked to add in tonic support)

Wait. You did it inline?
Oh. Of course. The add and bl instructions being replaced are redundant and unnecessary. It’s so obvious. How did I not see that? I am no hacker!

You did your part in finding it, and for t hat I’m grateful :wink:

All I did was run a trace and then CTRL+F for vulnerary’s id. Nothing complicated. Easy enough to do half-awake at 3 AM at least.

If 0203A8EC+0x7B has 0x02 set, unit does not retreat before using healing item.

Routine at 0803C0C8:
If AI4 = 0x20, set bit 0x02 of 0203A8EC+0x7B.

Jump element 0x07 (08037B78) used in AI1 0x03, 0x04, 0x05:
Sets bit 0x02 of 0203A8EC+0x7B as part of the routine.

Well, that explains why AI1 0x03 and AI4 0x20 both cause movement restrictions.

AI [03,03,XX,00] = Unit retreats before using healing item.
AI [03,03,XX,20] = Unit does NOT retreat before using healing item.
While both AI1 0x03 and AI4 0x20 set the same flag, the “use healing item” routine is called before AI1 is processed.

Something else about routine 0803C0C8:
If AI4 does not have bit 0x20 set, then unset bit 0x02 of 0203A8EC+0x7B (perform AND with 0xFD)

At the beginning of the enemy phase, routine 080348E0 writes 0x01 to 0203A8EC+0x7B (plus does other things). Then for each enemy, routine 0803C0C8 is called which sets or unsets bit 0x02 depending on AI4. Bit 0x02 is also set if AI1 = 0x03 (jump element 0x07)

Near the end of the enemy phase (and player phase) 0x04 gets written to 0203A8EC+0x7B then later 0x00. If I set bit 0x04 earlier in enemy phase, enemies start attacking each other. So it seems bit 0x04 is what allows berserk units to attack their allies.

1 Like

Are the routines just the conditions that the staff will be used?

Who woke me up? Go away. I have quit hacking forever.

Uhh… (that was a long time ago) I believe it covers both the condition and the unit instructions.
Look at what gets written to the stack. I think that data tells where to move unit, what action to perform, and on which target.

[10:17:01 PM] Crazy Colorz: Hey check this out
[10:17:04 PM] Crazy Colorz: AI 0xE - 08B9856C - Heal Allies
01 00 FF 00 00 00 00 00 29 A8 03 08 58 3B 1D 08
03 00 FF 00 00 00 00 00 00 00 00 00 00 00 00 00

AI 0xF - 08B9858C - Alternate between AI1 0x0E and “action without moving”
01 00 FF 00 00 00 00 00 29 A8 03 08 58 3B 1D 08
07 64 FF 00 00 00 00 00 00 00 00 00 00 00 00 00
03 00 FF 00 00 00 00 00 00 00 00 00 00 00 00 00
[10:17:15 PM] Crazy Colorz: I think it really does just execute the first AI that evaluates into something
[10:17:27 PM] Crazy Colorz: Let me try editing my custom AI thing
[10:17:31 PM] zahlman: ?
[10:17:34 PM] Crazy Colorz: Like, take out all the gotos
[10:17:45 PM] Crazy Colorz: Like it doesn’t need a 03 to finalize the AI
[10:17:47 PM] zahlman: what’s with the zero probabilities for the heal AI
[10:17:58 PM] Crazy Colorz: I think the probabilities are only used with 05 and 07?
[10:18:01 PM] Crazy Colorz: intSys go figure
[10:18:54 PM] zahlman: ah I see, you’re thinking that 01 (heal) always evaluates to an action, and the 07 has a 100% chance
[10:19:07 PM] zahlman: so on odd turns it will find the heal action, and pause at that point in the ‘loop’
[10:19:13 PM] zahlman: and on even turns then it will do the wait
[10:19:21 PM] zahlman: and then the 03 command just tells it to loop?
[10:19:39 PM] Crazy Colorz: I think 03 is just a goto or something?
[10:19:43 PM] zahlman: yeah
[10:19:48 PM] zahlman: it seems that 03 is goto and 1b is label?
[10:20:23 PM] zahlman: (I bet they were numbered based on like, the order in which they realized they were necessary, lol)[10:17:01 PM] Crazy Colorz: Hey check this out
[10:17:04 PM] Crazy Colorz: AI 0xE - 08B9856C - Heal Allies
01 00 FF 00 00 00 00 00 29 A8 03 08 58 3B 1D 08
03 00 FF 00 00 00 00 00 00 00 00 00 00 00 00 00

AI 0xF - 08B9858C - Alternate between AI1 0x0E and “action without moving”
01 00 FF 00 00 00 00 00 29 A8 03 08 58 3B 1D 08
07 64 FF 00 00 00 00 00 00 00 00 00 00 00 00 00
03 00 FF 00 00 00 00 00 00 00 00 00 00 00 00 00
[10:17:15 PM] Crazy Colorz: I think it really does just execute the first AI that evaluates into something
[10:17:27 PM] Crazy Colorz: Let me try editing my custom AI thing
[10:17:31 PM] zahlman: ?
[10:17:34 PM] Crazy Colorz: Like, take out all the gotos
[10:17:45 PM] Crazy Colorz: Like it doesn’t need a 03 to finalize the AI
[10:17:47 PM] zahlman: what’s with the zero probabilities for the heal AI
[10:17:58 PM] Crazy Colorz: I think the probabilities are only used with 05 and 07?
[10:18:01 PM] Crazy Colorz: intSys go figure
[10:18:54 PM] zahlman: ah I see, you’re thinking that 01 (heal) always evaluates to an action, and the 07 has a 100% chance
[10:19:07 PM] zahlman: so on odd turns it will find the heal action, and pause at that point in the ‘loop’
[10:19:13 PM] zahlman: and on even turns then it will do the wait
[10:19:21 PM] zahlman: and then the 03 command just tells it to loop?
[10:19:39 PM] Crazy Colorz: I think 03 is just a goto or something?
[10:19:43 PM] zahlman: yeah
[10:19:48 PM] zahlman: it seems that 03 is goto and 1b is label?
[10:20:23 PM] zahlman: (I bet they were numbered based on like, the order in which they realized they were necessary, lol)

o7

single tear

Whoa. Wait. Huh?
I thought I told you before that 03 was used like a goto and not finalize?

I only just figured that out. Oops. Also, I was trusting what I parsed in the 03 routine, and I guess my logic was a bit off. I already knew and said the Program Counter thing, but I assumed it kepy running until it hit a 03 or something similar.

Alright.
Oh, that reminds me, I think 030013B0 might be used to indicate whether to continue/stop executing commands in current primary/secondary AI entry. 00 = Continue. 01 = Stop.

1 Like

Yep, have that noted, but didn’t put the pieces together until now. Also, @Gryz, you really should read the routines I’ve annotated. They’re in the united dropbox.

On AI Command 00:
Edit: See @Gryz’s post a few below this one

1 Like