AI Design for Java RTS

7a7adca207e7fd8d9b51fa6e83df893a
0
Dark_Storm 101 Feb 08, 2007 at 20:44

Hey all. I am developing an RTS in Java as the title says, and up till now my AI has consisted of each unit having an ‘Order’ variable which contains the action that the unit is supposed to do. MoveOrder contains the destination point and actually moves the unit and also does all my collision detection. AttackOrder holds the target and physically shoots the gun.

I think I am approaching the limits on the utility of this design. I want my units to be able to form up in formations and execute squad tactics as well as simple things like move around a unit blocking its path and general pathfinding.

I am thinking about implementing some kind of Brain class as my AI and centering all the logic in there.

I’m just looking for comments on what others have done for their general AI design as well as suggestions.

Thanks!

5 Replies

Please log in or register to post a reply.

6cad28055d4af37574fe0d4064555e53
0
GroundKeeper 101 Feb 08, 2007 at 22:25

I would suggest you to look in to vector based ai (often missrefered to as flocking) where you use simple vector operations to give autonomous agents goals and targets. It works by summing up all the sorrounding attractors and from that forming a target direction. This together with some kind of ability organisation and you can simulate most of you expected behaviours. It can easily give them behaviours that would be recognised as cooperation and it still doesn’t kill you fps.
I could throw up some links tomorrow if you are interested.

7a7adca207e7fd8d9b51fa6e83df893a
0
Dark_Storm 101 Feb 12, 2007 at 21:30

Hmm, I’m very interested.

6b7e1a4b42e4b47d92fdef8bf2bd8e2c
0
Jare 101 Feb 22, 2007 at 15:02

I usually prefer to separate the mechanics of units in two pieces:

  • Behaviours: What the unit can do, and how to do it.
  • AI: deciding what behaviour to do.

In that sense, the “MoveTo”, “Shoot”, etc you mentioned are behaviours, and you haven’t even started to work on AI. :) The Brain approach should prove a good start (give some structure if nothing else) unless you lump everything in there. The question is: how smart should each unit be on its own?

A rule-of-thumb is that AI is everything that the (human) player’s units don’t do unless the player tells them to. Behaviours can get quite complex and include interactions, such as units that can shoot on the move, aggressive / defensive stances, etc. However, behaviours should be very predictable, so both the human and the AI know what to expect when they issue an order.

You will definitely need a more global AI at the “squad” and “army” level; single units trying to be smart on their own are a recipe for disaster in a RTS. Often, the “army” AI handles the strategic, long-term decisions, creates tasks to complete, and assigns units to “squads” which then have a tactical AI that knows how to accomplish that task. In fact, if you consider the “task” to be the piece of code that handles the tactical AI for a given “squad” (group) of units, you should be able to start seeing some results and have an easily scalable architecture.

Bah, made a mess of the explanation, but I hope it gives you some ideas.

7a7adca207e7fd8d9b51fa6e83df893a
0
Dark_Storm 101 Feb 22, 2007 at 19:53

Thank you for the reply! I also believe its imperative to separate the mechanics of units from their AI. In the constructor of each unit I assign it all its variables such as movement speed and range and line of sight and the unit itself merely extends base classes that contain generic move() commands.
When I spoke of orders, an order object such as moveTo simply stores the destination and performs the necessary collision detection and calls the body’s move() method, it doesn’t actually contain any code to change the units position manually.

This is important as you said because orders could be given that would give the unit extra unintended powers on an order by order basis otherwise.

My main problem is that of, as you said, determining how smart each unit will be.

I would prefer my units in the game to be fairly autonomous in that they will defend themselves, work with other nearby units meaningfully and without any additional higher command. By this I mean that they won’t run into each other, shoot at each other, and will move around objects, maintain general formations all without a higher AI guidance controlling the units. I want each unit to know where the others are and know where it should be.

They would obviously only move from their general positions when given an order by a higher player/ai authority.

This contradicts what you were saying in that I would need a higher AI for squad and army movements.
My hope is that I will put the player in a much more macromanagement position in that they wouldn’t control their units on an individual level at all. They would assign General AIs to command armies to accomplish goals and the player would merely coordinate strategy and determine objectives.

When I say I want each unit to have a brain, I mean that yes that brain would see where the units are in its surroundings, consider whether it has any orders or not or is moving in formation or with other units, and decide what to do on its own.

Currently, each unit operates similar to a finite state machine. It is given a individual order (although my implementation of the Order class allows orders to be linked together so taht when one is accomplished it is immediately replaced with another one) and that order contains all the code to accomplish that order such as moving the unit around or firing the units weapon.

Keep in mind however, that when the order object tells the unit to “move” or “shoot” it calls one of the units own methods and the unit moves or shoots based on its own movement speed or weapon.

I want all this code and functionality to be taken out of the order class and be put into a brain class so that the units aren’t just finite state machines but rather approach a truer AI.

7a7adca207e7fd8d9b51fa6e83df893a
0
Dark_Storm 101 Mar 04, 2007 at 08:41

I am an idiot. I’ve implemented a retarded version of a finite state machine and didn’t realize it. I just need to clean it up and my units should be running and gunning in no time.

Now, I wonder if there is a way I can work a genetic algorithm in here somehow…