The basic process is as follows:
Battle begins; the bot creates a savestate.
It's the player's turn. The bot needs to decide what input to generate, and it does this by referring to a table of strings representing either inputs or discrete menu decisions, or whatever the situation dictates.
Of course, the table is empty at first, so it just does shit at random.
The battle proceeds until either the player or the enemy is dead. The bot then assesses the outcome, measuring frames taken to complete the battle, or whatever user-defined desirable state it has to work with.
Now, throughout the battle, the bot keeps a record of the inputs it's generating. Having also quantified the outcome produced by these inputs, it
inserts the input sequence into the input table where the index at which it is inserted is defined by the relative fitness of the battle's outcome. The first iteration is now complete.
The bot reloads the initial savestate, and fights again. This time, the bot will loop downward through the list of input sequences it has already tried, starting with the best one, and randomly picks one to mutate. Since these inputs are already sorted by fitness, it has the highest probability of picking the most sane sequence, and mutating it into something hopefully more refined, and indeed, hopefully
frame perfect.
PS. I'm not completely lucid right now, so I hope that all made sense.
edit: Oh, you asked about implementation in Lua.
Here's an
old version of the 7th Saga bot for your perusal. It's so awful, I decided a rewrite was in order, so take whatever you see in there with a grain of salt.