Welcome to part 3 in the series on our AI scripting framework! See part 1 for the introduction and part 2 for a short description of the debugging tools.
This time, we'll look into the functions you can use to interact with the game. The technical term for this is the "API": the Application Programming Interface. Or in this case, maybe it's an AI Programming Interface!
The API defines the functions your code can call do to get information about the current game, and to perform game actions like building things and charging weapons.
Your AI script has access to two global objects with useful functions: Game and Debug. Debug just lets you write stuff to the console. The Game object is what lets you see and manipulate the game grid. It has functions to look at the current game state, like GetMyPower(), GetPiece(x, y), GetHitPoints(x, y), and functions to take actions: Build(piece, x, y, direction) and Charge(x, y).
Of course, those are very low level functions, and to make your life easier, we include several modules with useful functions, including an A* pathfinding implementation for navigating the grid. These are written in Lua, so you can look at the source code and copy/modify/reuse whatever you like! In ailib.lua, you'll find higher level function like ailib.findPieces(pieceType, player),ailib.canHitFrom(tile, withTower, withCannon, cannonDirection) and ailib.getBuildCost(pieceType, tile).
There are many ways to figure out where to go on the grid and how to get there, but you'll probably want some sort of pathfinding algorithm, and A* is great! It's my favourite algorithm. My wife thinks it's strange that I even HAVE a favourite algorithm, but hey, that's what she gets for marrying a nerd :)
If you want to know more about pathfinding in general and how we used it to create the built-in skirmish AI, check out this blog post: http://www.powargrid.com/blog/powargrid-ai-part-1-intro-and-pathfinding.
Documentation and example AI
You can find the descriptions for the Game and Debug modules, as well as the Lua utility modules, in the API documentation. If you're not sure how to use a particular function, see how the Daft Wullie example AI uses them. Still stuck? Ask us on the forum or our Steam community hub, we're happy to help!
In the next installment we'll take a look under the hood of the AI framework.
We're Michiel and Willem. Hi!