Free and Open Source real time strategy game with a new take on micro-management

New AI System

From Globulation2

Revision as of 01:26, 2 October 2009 by Fede (talk | contribs) (category:en)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

The New AI System (Codenamed Echo)

The new AI System is a system for the creation of custom AI's in Glob2. The original system was very low level, and highly integrated into the rest of Glob2. The new system is backed by this existing system, but provides high level functionality to the proccess. It is still in the design and testing stages.

The system tries to reduce how much the AI script has to handle. The AI script has to check if its buildings destroyed, but the AI system will actually keep track of whats been destroyed and what hasn't. The AI script has to order building construction, but the AI system goes through the tedious task of choosing a location, placing an order, adjusting the number of units working on the building, tracking its location, etc.

Constructing Buildings

The new system handles construction by queuing up buildings to be constructed. It uses a constraint based system to decide where to place a building. Some constraints the system must live up to, they can not be overridden, where as other constraints the system simply tries to minimize with respect to their weights. A constraint can be any number of things, such as the distance to the nearest grain of wheat, or the walking distance to the closest hospital. Currently, the following constraints are possible:

  • Distance to the nearest of any ressource
  • Distance to the nearest building of any type or specific types
  • Distance to water

There may be, and probably will be, other constraints in the future. An example of this system in action would be deciding where to place an Inn. You want it to be close to wheat, but also close to your base, and reasnobly close to fruits, but far from enemies. So, you use the following set of constraints to decide its location:

  • The distance to the nearest wheat should be minimized, with a weight of 2
  • The Inn can not be more than 10 units away from wheat
  • The distance to the nearest building should be minimized, with a weight of 1
  • The distance to fruit must be minimized, with a weight of 0.5
  • The distance to the enemy must be maximized, with a weight of 0.5

These cosntraints would place an Inn in a reasnoble location, with nearness to wheat beeing the most important factor, but the distance to other settlement, fruits and enemies also playing an important factor in the decision.

Once the order for a building is placed. the system will construct the building. If, for some reason, the building is destroyed while under construction (or doesn't appear in the first place), the system will forgett about it, erasing its record and reallocating its units elsewhere. Eventually, however, the building will come into play, the system will record its existance and watch it closely.

Upgrades And Repairs

If you decide a building needs to be upgraded or repaired, the process in doing so is simple. The system will take in an order, given the building and the number of units to work on it, and will follow through with the construction until the buildings completion. This part of the system is very simple.

Flags

Ordering the construction of a flag takes up a similair process as the construction of a new building. You can do it by using the same constraint system. However, you are likely to use a very different combination of constraints in order to place a flag, as in the following example.

Suppose you want to attack an enemy base, in particular, you want to attack their Inns. You could use the following, very simple constraint to get the job done:

  • Distance to the nearest enemy Inn must be below 1
  • The walking distance to the nearest swarm on our teams side should be minimized, with a weight of 1

This would place a flag attacking the nearest Inn to one of our swarms. Simple but intelligent. One could even add in a few more constraints for added intelligence, such as avoiding enemy towers. Flags are simple and easy to create.

Building Maintance

Maintaining buildings and flags after their constructed is an important task that applies to most of the game. Changing the number of units assigned to a building, or building specific options, will be a simple interface function, such as adjust_inn(inn_id, units_working), or the more complicated swarm, for example: adjust_swarm(swarm_id, units_working, worker_ratio, explorer_ratio, warrior_ratio). Obtaining inn_id's and swarm_id's is the tricky part. Flags will also be adjusted using the same manner.


The system will return to you a unique, system provided ID for a building after you call for its construction. However, the building will not actually be constructed untill after the system finishes its calculation. So, the AI systems ID will be mapped to the ID used in the rest of the Glob2 (if you've seen its interfaces) automatically. Its important that you check to see if a building still exists, for example, using the following: still_exists(building_id). Calling for a building that no longer exists to be changed will cause an error, which can be silent or fatal depending on your configuration.

Obtaining Information

There will be a variety of functions at the disposal to obtain information. Such functions allow for finding buildings that fit criteria, while others count up units of a particular type or criteria, or statistics about deaths. Heres a list of the currently thought of "knowledge" functions:

  • Finding buildings fitting criteria, available criteria are:
    • Building Type
    • Building Health
    • Recent Health Losses
    • Any construction type constraint (such as closeness to ressources or other buildings)
  • Counting numbers of units fitting criteria, available criteria are:
    • Unit type
    • Unit health
    • Unit skill levels
  • Finding all the players the match criteria:
    • Number of units or buildings matching above criteria
    • Diplomacy

Others may be added later.

Alliances & Diplomacy

Aliances are handled by a couple of simple interface functions, such as set_vision_on(player_id), or set_inn_view_off(player_id). The player_id can be obtained using the functions shown in the **Obtaining Information** section above. It is all a simple proccess.

Technicalities

Globulation 2 has a saving system requiring every module to save its entire contents to a stream when called upon, and to load its contents from a stream in the same manner. This responsibility is passed onto the AI script as well, which must provide save and load functions for all of its stored data.

Globulation 2 operates in ticks, their being arround 25 ticks per second. The AI System is called every tick, and these ticks are delegated to the AI script, which must also operate in ticks for the time being (there may eventually be a timed rule based system, however that is to be discussed). The AI Script will be called every tick, where it should go about its buisness. Its very important that the AI does not try to do computainally expensive tasks every tick, or the computer will be overloaded very easily. Infact, the Nicowar AI only does something 1 out of ever 4 ticks, and most of the time, its something fairly simple. A similar theme can be used in an AI script to keep cpu usage down.

A Pseuodo Example

Heres a simple AI loosely based on war rush, that would use the above system to do many of its tasks:

  • At the start, set the swarm to construct workers, and assign 10 workers to it.
  • When there are atleast 5 workers, construct a barracks as close to wood as possible.
  • When there are 10 workers and the barracks is completed, set the swarm to construct 1 worker to every 3 warriors.
  • When there are 10 warriors, place a war flag on top an enemy building nearestto our base, and assign 20 level 2 warriors to it
    • As well, place an Inn near wheat with a weight of 3, but also near the swarm with a weight of 1
  • Find an enemy building that is near a friendly war flag, if none exists, then:
    • Find and delete the friendly war flag, place a new flag with the same criteria as the last
  • Repeat the above, continually placing new war flags and new Inns until death does its part

While the simplicity of the AI seems apparrent, an underlieing implementation would have to do much caretaking to get everything done. However, with the AI system, this can all be done in maybe 30 lines of code, creating an early attacking, simple AI.

The Programming Language Of Choice

The system is designed in such a way that it is seperated from the rest of Globulation 2, which means it can single handedly be embedded in an interpretted language. While some of the AI's using the system may be done in C++, the option of using an interpretted language is very attractive, especcially to people with little to no experience in programming. While the interpretted language of choice has been disputed several times, some attractive entities include Python [1], Squirrel [2], Tea [3], Ruby [4], Lua [5], and SPL [6].

All of these languages offer their own attractions, and it would be wonderfull if we could have them all, but thats allot of work for something so small.

Why more Globulation 2 AI's

While Globulation 2 is certainly a human-oriented game, its open source qualities seem to reel in nerds who like to automate things. As a human playing the game can be fun, making an AI is a long proccess, but it yields something you can be proud of, with out having to do much work to prove its weight in gold. To show off to your buddies, just open up the program, start Glob2 with your AI and voila, its doing the work for you while you're showing off.

I think its more that existing Globulation 2 AI's aren't that great, even the champion Nicowar has its weaknesses relating to overgrowth and overusage. To make an AI that truly challenges players, you have to make a system that allows the good players to put their ideas right into play. Without the complicated back-end work that goes into a Globulation2 AI, the player is free to pour his own strategy into the script directly, making it an instant challenge to all who dare fight it.

Misc