AIWrapper:ModularAI: Difference between revisions

From Spring
Jump to navigationJump to search
mNo edit summary
No edit summary
Line 38: Line 38:
This priority determines in what order modules are notified of any events, as well as the sequence in which they are updated and initialized. This can be important if for example the initialization of a component requires another component to be already initialized.
This priority determines in what order modules are notified of any events, as well as the sequence in which they are updated and initialized. This can be important if for example the initialization of a component requires another component to be already initialized.
Note that a priority number means a higher priority. A module with priority 1 will be treated before a module with priority 2. The priority number works as an index in a list. The order in which two modules with the same priority number are handled is undefined, and may vary from moment to moment.
Note that a priority number means a higher priority. A module with priority 1 will be treated before a module with priority 2. The priority number works as an index in a list. The order in which two modules with the same priority number are handled is undefined, and may vary from moment to moment.
If no priority is specified for a module, it is given a default priority of 10.





Revision as of 11:27, 9 June 2009

ModularAI

The ModularAI wrapper is a wrapper around the Java Interface. It divides AI functionality up in modules. Each module is self-contained, easily customizable and can be swapped for any the module serving the same function without breaking the AI.

The design goal of the ModularAI wrapper is to provide the AI developer with easily customizable default behaviour as well as the ability to add his or her own, without hiding any of the full power of the Java interface underneath.

PM LoidThanead for a copy of the latest version.


Overview

A ModularAI consists of a collection of modules. Each module is responsible for one aspect of the AI's behaviour. The behaviour of a module could be to manage the AI's contruction queue. Another module might be used to plan and/or execute attacks.

The AI communicates with the modules through Messages. When an event happens in the game, all modules are notified. Modules can also communicate with one another through messages.

To simplify unit handling, the ModularAI wrapper introduces the SimpleUnit. This is a wrapper around the Unit object that provides a convenient interface for giving orders to units. It also makes the unit's properties (i.e. current/max speed, current/max health, available weapons etc) easily accessible.


Use

To use the ModularAI wrapper, you should first have a regular Java AI set up, as described in AI:Development:Lang:Java. Next, add the ModularAI.jar file to your project. (Make sure it is located in the jlib subdirectory of your AI's folder.) Then, instead of having MyJavaAI extend AbstractOOAI, extend DefaultModularAI (or ModularAI).

ModularAI differs from AbstractOOAI in a number of small but significant ways. Firstly, it overwrites all of AbstractOOAI's methods in order to provide its modules with information about any event that happens. This means you should not use the different event methods, nor init() or update(). If you intend to strictly use modules to build your AI (as is recommended), you won't need these methods. However, there may be situations in which it is convenient to still be able to have your main AI class respond to these events, so ModularAI provides you with alternatives. Instead of init() and update(), use initAI() and updateAI(). These are called immediately after ModularAI.update() and ModularAI.init(). Instead of the event methods, overwrite the method receiveMessage(). ModularAI's receiveMessage() method works identically to the equally named method of a module, which is explained below in the section 'creating a new Module.' Alternatively, if you overwrite any of AbstractOOAI's methods yourself you can first call the appropiate method of ModularAI. (I.e.: call super.update(frame).)

At this point, you technically have a working ModularAI. You may want to export your code and see if there are no errors. However, to make full use of the ModularAI wrapper you will need to know how to use modules.


Adding a module

If you extended DefaultModularAI, your AI already has a number of modules available. However, you will likely want to add your own module(s).

Adding a module is straightforward. In MyJavaAI, either in the constructor or initAI() (or init(), if you made sure to call super.init()), use the method addModule() to add a new module. This method comes in two variants. The first only takes the module that you wish to add as its argument. The second also lets you specify a priority. This priority determines in what order modules are notified of any events, as well as the sequence in which they are updated and initialized. This can be important if for example the initialization of a component requires another component to be already initialized. Note that a priority number means a higher priority. A module with priority 1 will be treated before a module with priority 2. The priority number works as an index in a list. The order in which two modules with the same priority number are handled is undefined, and may vary from moment to moment. If no priority is specified for a module, it is given a default priority of 10.


Creating a new Module

Communicating with a module

Existing modules

Implementing unit-specific behaviour