Trying to create a specific AI

I’m brand new to working with AI, so while I know what I want to do, I have no idea how to do it.
(I’m working with FE7, if that matters.)

What I want is to make an AI that has the units move as close to a specific tile as possible, and then attack whatever’s nearby.
Edit: I’ve realized I haven’t been clear that what I want is for both of these things to happen on the same turn.

I’ve managed to make it so they move as close as possible, but then don’t ever attack. I’ve also made it so they move as close as possible, but only if there’s no attackable units in range to distract them.

How can I make the AI do what I want it to do? I could really use some help.

For AI documentation, check this thread:

Have them move to a specific area without attacking anything along the way
Eg. Ai1 do nothing ai2 target coord

Then have a range event in that area that changes the ai1 of the enemies to attack

Would this work for you? Easiest way, I think

IIRC you can’t change the AI of an active unit, and an area event triggers while the unit is active.
You can change the AI of the units inside the area, but you can’t change the AI of the unit that activated the event.

you can set a flag to trigger an event to change their ai as a turn event

There is probably a more elegant way to do this but i haven’t messed around with it in a long time

I would probably try making it a post-action routine to change AI with a little asm as a better way if I needed this :thinking:
I’ll try asking Shuusuke

or actually I guess you could just write an asm routine to go in the ai script that conditionally changes ai lol
but that’s asm, which not everyone wants to do

These AIs do what you say you want to do. If the unit is not at the specified range, they will move towards a specific coordinate, and if they are their AI changes to move towards enemies.

On the topic of changing enemy AI, you could give the enemy a unique Id and you won’t have to use an area affecting event to change the AI. You could change specific units AI.

I know that much. The problem is that I don’t know what changes to make that will get them to do what I want.

In this case, I want them to move towards the specified tile and then attack anything nearby- on the same turn. I can’t figure out how to get this to work, since I can only get them to move towards the tile OR attack something each turn, not both.

AI1 runs before AI2, so it first judges whether the unit can attack or not. If the unit can’t attack, then it moves. What you’re saying you want would invert the order of AIs which I don’t know if anyone has done something like that.

So the workaround that I’d consider simpler would be by alternating the unit’s AI and unsetting their “already moved” state to effectively give them another chance to move, but with the AI that makes them stay in place. If the methods to unset the active unit really didn’t work, then you’d have no choice but to look into ASM to make the game judge AI2 before AI1 for this specific AI.

EDIT: Went to check since I’m not familiar with FE7, it doesn’t seem to even have a way to unset the active unit to test what I’d suggested. Another problem is that even unsetting the unit’s “already moved” state, it still doesn’t put them back on the AI cycle so they don’t act again. This was changed for the dancer AI fix, but you’d have to find a way to make it into an ASMC.

EDIT2: Furthermore, it seems that in FE7, you can’t change the AI of a unit that already moved, which is another obstacle that you’d have to work through with ASM.

EDIT3: It looks like you can just copy AI2 commands into AI1 and it’ll work, so by doing this, you could technically “invert” the order of the AI that gets executed. This way, there’s only 1 problem you’d have to figure out, which is that when an AI unit moves, they get greyed out and end their turn, so you can’t make them move AND attack unless you change this.

I see. This is super useful stuff to know! With any luck, I can switch some AI1 and AI2 commands to get the results I’m looking for. Either way, thanks!

Has anyone tried AI opcodes 19 or 1A? They don’t seem to be used in vanilla, but they are both listed under the Assault category in FEBuilder. 19 seems to allow numerous terrain types, while 1A appears to be a pointer to such data. If it actually is an attacking type like its category suggests, this could potentially be a solution for you. I could also use this kind of thing if it works at all in FE7, so l might test it in a few days if l get a chance.

Er… I’m not exactly sure what you’re referring to. FE7 doesn’t have 19 or 1A as options for AI1, and AI2 doesn’t control attacking, as far as I understand.

Maybe I’m just not understanding something, but I’m a noob at this who knows basically nothing. Could you explain what AI opcodes 19 and 1A are?

My apologies. l meant in the Advanced Editors section, there’s the AI menu you can access from there (the button is just called “AI”). In this menu, you can directly edit what each AI type will do. This is how you might accomplish @Shuusuke’s suggestion. As for the opcodes (maybe not their official name, but it makes sense to me), you can change them on the right side by double-clicking one of them; it’s similar to the Event editing interface. The opcodes l refer to are named “Attack towards specified terrain tiles” and “Move towards specified terrain tiles”. One or neither of these may do what you want, but it is worth the try.

I recommend changing AI1 behavior 01, 02, 04, or 05 since those are undesirable except in special cases. An enemy who doesn’t attack based on randomness feels weird to fight, in my opinion. If you need to replace something in AI2, one of the many “Move towards coordinate” behaviors should suffice, as it is unlikely you need all of them.

I’m kind of bad at explaining things, so l hope l made enough sense.

From the sound of things this would probably require some ASM work to make it work how you’re envisioning.

For clarity’s sake, do you mean a setup where the AI has the enemy move to the closest space to the target tile it can reach, and attack only if it can do so from that space?

Or do you mean something where the AI will prioritize attacking over being as close to the target tile as it can technically get, but will always do so from the closest available space to the target tile?

I have tested the two opcodes l mentioned. They are just movement commands. It does not make units attack. 19 and 1A opcodes are only useful if you want a unit to move towards a throne or gate, for example.

I believe they want the unit to move towards the specified tile, and then attack anything nearby all in one turn. l agree, it might require assembly to get them to move to a tile but still search for an adjacent target.

Alright, that makes a lot more sense. Thanks!

Yeah, I did read that part; I was just wondering, essentially, if the AI would “prioritize” getting as close as possible to the tile, or being able to attack an enemy, where the two conflict. Actually, I think a visual aid might help here. With green representing the target tile, red representing the unit with the desired AI, and blue representing a potential target that unit can attack… and also assuming the red unit has 5 movement and 1 range.

Would @pengie9290’s preferred behavior be this?

Or this?

…is what I’m asking.

The first one. That’s why they said “make an AI that has the units move as close to a specific tile as possible, and then attack whatever’s nearby.”

1 Like

Gotcha. The phrasing just wasn’t too clear to me, in that I could see it meaning a number of different specific behaviors.

The first one. I’d want them to move as close to the green square as possible. Only after they’ve gotten as close as possible would I want them to try attacking. (In this case, they’d need a weapon with 2 range to do so.)

1 Like