Mars Battle Engine

I've looked around for a better way to control enemies in RPG Maker VX Ace, but it doesn't seem like there's much hope without writing scripts to handle enemies. But what's the fun of making scripts to get a specific enemy to do something? Why not make a better system that I can build upon? That others can use?

Enter, the Mars Battle Engine (AKA MaBE)! Taking a hint from the Luna Engine, MaBE is named after the Greek god of war. With this, you too can reign supreme over the battlefield, taking control of your encounters and making them truly epic!

At least...that's the idea.

I've been working on building this for a little while now. It's still pretty young, but it is already pretty close to being usable. MaBE aims to be a ground layer for other scripts to extend it. For example, I want to write a script that makes enemies use attacks in a specific order. This script would only need to be dropped in to be usable for enemy configuration.

I decided to put it here to see what others thought of it. I can't promise regular updates; I'm pretty busy with life and such, and I have other projects that also need to be tended to.

Here's the current version. It's only stuff for testing MaBE's note reading stuff. It should be really easy to go from here, however.
Code:
=begin
Author: Autofire
Version: 0.1

    Mars Battle Engine

    This script enables greater control over how enemies behave in RPG Maker VX
Ace. It is configured on an enemy-by-enemy basis using the notes field for each
enemy.

    From here-on, Mars Battle Engine will be abreviated MaBE.

=== BASICS ===
    Without configuring an enemy, the MaBE sits idle and does nothing.
Furthermore, it should not get in the way of any other script that uses the note
field. When scanning the notes field for options, MaBE follows a few rules.

    1.    If a line begins with <mars-config> or <mars-end-config>, an internal
            flag is set. Then the interpreter continues to the next line.
    2.    If a line begins <mars>, then the rest of it is read.
    3.    If a line falls between a line with <mars-config> and <mars-end-config>,
            then is treated as if it started with <mars>.

(Note that, for for each of these rules, all checks are case-insensitive. That
is, <mars>, <MARS>, and <Mars> are all considered identical. Choose your
favorite.)

If a given line is marked for reading, all instances of <mars> are ignored. If
it starts with a pound symbol (i.e. '#'), then the line is ignored. (Note that
this ONLY applies if it STARTS with the '#'.) Use the '#' symbols to create
comments within <mars-config> blocks.

Once the scanning step is done, then all the remaining options are stored. The
format for each line is:

    Option-Name [Argument 1] [Argument 2] ... [Argument N]

Valid arguments vary with the option in question. Not all options require
(or accept) arguments.

Also, in RPG Maker VX Ace, lines are wrapped. However, it isn't obvious without
using notes. It's a good idea to add comments if it isn't clear.

=== OPTIONS ===
WIP

=== EXAMPLE ===
WIP

=end

#==============================================================================
# ■ Game_Enemy
#==============================================================================
class Game_Enemy < Game_Battler
    alias mars_initialize initialize
    def initialize(index, enemy_id)
        # Unfortunately, Game_Enemy's initialize
        # requires some arguments, so this isn't nice and clean
        mars_initialize(index, enemy_id)
        
        interpret_notes
    end
    
    # This is the list of arguments. It's a 2D array, with the first index being
    # a given argument and the second being the argument and its options.
    # Be wary that this may be nil. If it is, then the enemy lacks any options.
    arguments = Array.new(0)
    
    ## add_arg:
    # Adds the given list of args as a new set.
    #
    #  words:    An array. First is the argument; the rest are options.
    #                    If this is empty, then nothing happens.
    def add_arg(words)
        if(@arguments == nil) then
            @arguments = Array.new()
        end
        
        if(words.size > 0)
            @arguments[@arguments.size] = words
        end
    end
    
    # This interprets the notes for an enemy when called. It will load all
    # of the Mars engine options for that enemy as separate arrays of strings.
    # It will read lines under two conditions:
    #  1: The line starts with <mars>
    #  2: The line falls between lines with <mars-config> and <mars-end-config>
    #
    # However, in either case, empty lines are ignored. Also, all lines starting
    # with '#' are ignored. (NOTE THAT THIS DOES NOT APPLY TO '#' SYMBOLS AFTER
    # THE START OF THE LINE!)
    def interpret_notes
        found_mars_config_start = false
        @arguments = nil    # Clear the arguments; we don't want duplicates
        
        note.each_line do |line|
            # Only process lines if we've found a mars config tag
            if(found_mars_config_start || line.downcase[/^<mars>/]) then
                if(line.downcase[/^<mars-end-config>/]) then
                    found_mars_config_start = false
                else
                    # Split the line into words
                    words = line.scan(/[a-zA-Z<>#]*/)
                    words.reject! { |word| word == "" }
                    words.reject! { |word| word.downcase[/<mars>/] }
                    
                    # Ignore comments
                    if(words[0] != "#") then
                        # Add the line to our list of arguments
                        add_arg(words)
                    end
                end
            elsif(line.downcase[/^<mars-config>/]) then
                found_mars_config_start = true
            end
        end
    end
    
    alias mars_select_enemy_action select_enemy_action
    def select_enemy_action(action_list, rating_zero)
        # This is going to get updated; the return value will get adjusted
        # if MaBE decides to do so. Otherwise, RPG Maker will handle things
        value = mars_select_enemy_action(action_list, rating_zero)
        p(value)
        p(@arguments)
        return value
    end
end # End of class


If you want to try out the script, drop this into an enemy's Notes field. You'll need to run the game with the debug console turned on. (Look up the Gemini IDE or something. I'm not sure how to do it w/out Gemini). Then just enter a battle with the enemy in question.

Code:
<mars_config_begin>
Meep this is a mars config line
# I'm a comment
Beans rock # I look like a comment, but I'm not

And his music was electric...
<mars-end-config>

This doesn't get interpreted.

<Mars> But this does.
<MARS> So does this.
<mars> MaBE is case insensitive.
I stayed up late last night and worked more on MaBE. It's on its way to being fully functional! 😀

I've created a Github repo to keep all of the changes I make. I'll be posting new versions here.

Original thread: https://forums.rpgmakerweb.com/threads/mars-battle-engine.83341/