Desert Strife Jam AI (with code examples!)

Posted by
September 6th, 2016 3:14 am

TL:DR KISS

The Post-Jam version is still far away (I’ve hardly had any time to work on it yet) so I might as well talk about the AI of the Jam version and the plan for the Post-Jam AI.

Version one

The first version of the AI was finished in the morning of the second day. It had one goal and one goal only, EXTERMINATE! In simple terms each unit finds the closest enemy and moves towards it. They also check that the path to the enemy is not blocked, by simply finding a path to it. This could cause slowdown with bigger maps and larger unit counts, but here it is just fine and dandy.

VIEW CODE (embedding doesn’t work unfortunately…)

Version two

So far the AI doesn’t take into consideration the main mechanic: scavenging technology. Implementing such behaviour, the quick and easy way, requires nothing more than adding technology distance checking into the previous code. If there’s technology closer to the unit than enemies, then that is what the unit will go for. On top of this the unit won’t move until it has completely excavated the technology.

VIEW CODE

Version three

I could have stopped here, but there was an annoying deficiency in the artificial thought process. The units don’t prioritize actions; In other words, an attacking unit is sometimes blocked by a scavenging unit.

The fix is slightly more elaborate than the previous step, but by no means complicated: tally up the best actions from all units and choose the most important one based on the target type and distance. Here’s the final AI in action.

NEW STUFF!

VIEW CODE

The future?

There are still a few things I’d like to implement in the AI, namely threat assessment and intelligent positioning. As luck would have it, there’s a single technique which can be used to achieve both of these goals: heatmaps! A heatmap is simply an extra tilemap which contains values based on unit proximity and threat level (if you are lazy you can even reuse the world tilemap objects for this purpose). Heatmaps can be used in selecting the target (lower threat value is preferred) and in pathfinding to choose the safest route to the target.

Conclusion

Keep it simple and you’ll go surprisingly far.

Play Desert Strife here

Tags: , , , , ,


2 Responses to “Desert Strife Jam AI (with code examples!)”

  1. seththefirst says:

    Have you thought about Utility based AI.
    I think that you have a very nice use case for it. =)

    This post, has a very nice explanation about:
    https://alastaira.wordpress.com/2013/01/25/at-a-glance-functions-for-modelling-utility-based-game-ai/

    • HuvaaKoodia says:

      Interesting article with nice visualizations. Once I get the threat assessment function in, then sure, I can use utility curves to choose between attacking, scavenging and running away (something which the AI cannot even do at the moment).

      Thanks for the heads-up.

Leave a Reply

You must be logged in to post a comment.

[cache: storing page]