top of page
Writer's pictureCallum Pearce

RTS AI


There are two big mechanics that are needed in order to get my game to a completed state. Combat and AI. This blog post is going to be on AI. Although the AI is not yet complete, there is still a lot that has happened to mention.

AI

As it stands I currently have an AI that is able to gather and build. This is all dependent on a system I have created involving strategies. An "AiStrategy" scriptable object was created allowing me to create the strategies so the AI can follow them. Each strategy has an objective type. This is can currently be defined as one of the following, create building, create unit or gather. Depending on its type it can then have other elements to it. If it is a create unit objective type, then it will get a reference to the intended units scriptable object. Similar situation with a create building where it gets a reference to the intended buildings scriptable object. If it is a gather strategy, then it is given a resource that it is intending to gather. Finally, one additional field was added which is prerequisite buildings. This is used for creating units, it needs to make sure it has the correct building to create that unit.

I have set these strategies up like this, so that the main loop of the AI can run a function that will check any strategies in it's queue. Then depending on the objective type it will run different functions, all of which will return a strategy error. A strategy error is an enum defined which contains all the possible errors that could happen that didn't allow the strategy to go through. There are several errors, but the most common ones that will occur include cost, population, missing prerequisite and location invalid. Cost is returned when the player does not have enough resources. It will then work out what resource(s) are missing then add the appropriate gather strategy. Population is returned when the AI does not have high enough max population, therefore a BuildHouse strategy would be added into the queue. Missing prerequisite is returned when a unit is created without it's required building. So it will add the building it needs into the queue. Finally, location invalid is returned when trying to create a building. This happens when a selected location to build a building is unbuildable. This can happen a lot so it will try about 4 times before it will increase the area in which the AI is currently building (this helps the AI build in a town like fashion and not just everywhere). There are a few more errors but they do not occur as much.

A lot of additional measures were required to make sure infinite loops or a stupidly long queue is formed. As the AI is making this check every frame, and it takes time to perform these strategies, without appropriate prevention the queue would just exceed in size and cause major problems. So the above were renamed to "AiStrategies" and another script called Strategies were formed. This script contained the following information, the AIStrategey it is trying to achieve, it's progress, it's error, whether that error has been attempted and what number strategy it is. So it needs to know what strategy it is doing, that is why an AIStrategy is there. It then needs to know what progress it is at. In can either be in queue, or in progress. If it is in queue it will perform straight away, if it is in progress, it will still perform to see if the error has either gone or changed, but if it hasn't it moves onto the next strategy in the queue. As soon as it has ever done a strategy it will stop where it is in the queue, remove it and start again. It has a reference to what its current error is. Next it has an error attempted variable that is turned to true, if it has attempted the error and failed, this prevents the same error from being fixed and causing the long queue. Finally, a strategy number was added purely for testing to work out what number strategy it was on.

This is a very basic look at the AI. There are a lot more measures to make sure everything is right, and other fixes to different errors. The planned progress on the AI is to allow the AI to start building an army, and sending it towards the player so there is actually some form of conflict. Before this can be attempted combat is going to be done.

14 views0 comments

Recent Posts

See All
bottom of page