Other great resources: Official JS API docs, Scripting Forum
Making your first AI mod
Scenario
I want to make an AI mod for warzone, where do I start?Solution
An AI mod requires you to do 5 things:
- Create folder structure in which to place your files
- Create an ".ai" file that tells Warzone about your AI script
- Create a ".js" file that contains your AI script
- Test it works
- Package it as a .wz file
Let's get started...
1. Folder structure
Find your mod folder – see How do I install a mod? to get the location of it. As that page explains, create the "mods" and "autoload" folders if they don't exist yet.
You then need to create a folder structure like this:
- (parent folders)
- mods/
- autoload/
- Tracebot/- this is the folder that contains your mod
- multiplay/
- skirmish/- this is where your AI files live
- tracebot.ai – this tells warzone about your AI script
- tracebot.js – this is your AI script
- skirmish/- this is where your AI files live
- multiplay/
- Tracebot/- this is the folder that contains your mod
- autoload/
- mods/
Now let's write our fake AI script...
2. tracebot.js
Let's start with a super-simple AI script that displays information about the first 5 map features in the in-game console:
function tracebot() { var features = enumFeature(-1,""); var limit = Math.min(5,features.length); // limit our trace to 5 items at most var f; for (var i = 0; i < limit; i++) { f = features[i]; console("feature #"+i+", player:"+f.player+", name:"+f.name+", health:"+f.health); } } function eventStartLevel() { setTimer("tracebot",10000); }
Save that as "tracebot.js" in the skirmish folder you created above. Here's a ready made file to save you some time: tracebot.js
If you're interested, here's some links to the JS API functions/events used in that script:
- enumFeature() – gets a list of all features (trees, oil resources, etc.) on the map
- Feature object – the type of objects returned by enumFeature()
- console() – displays text on the in-game console
- eventStartLevel() – this is what kicks your script in to action once a game starts
- setTimer() – repeatedly call a function in your script at a set interval
Now we need to tell Warzone what that file is...
3. tracebot.ai
Warzone finds AI mods by looking for ".ai" files in the "skirmish" folder. The file tells Warzone what your AI is called, and where to find it's script file. Here's what it's contents look like for our tracebot:
[AI] name = "Tracebot" js = tracebot.js
That's fairly self-explanatory
Now put that file in to the same "skirmish" folder as before. Here's a ready made file to save you time: tracebot.ai
4. Test that it works!
Now load up Warzone. When warzone starts, it scans through all mods in the "mods/autoload/" folder and if it finds something it understands it will enable the mod. In our case, Warzone will spot the "tracebot.ai" file in the "skirmish" sub-folder and make the Tracebot AI available in skirmishes and multiplayer games.
So, let's start a skirmish. From the main menu: Single Player > Start Skirmish Game
Then select the 2 player map called Sk-Startup - this is a good map for testing AIs on because it's relatively small, it includes optional scavenger faction, and there are some interesting terrain paths.
Then select the Tracebot AI and start the game.
Now, once you're in the game, enable cheat mode by pressing Shift + Backspace. The open the debug menu by pressing Ctrl + O.
Assuming you've set-up the game as shown above, you're player 0 and the Tracebot AI is player 1. Click "1" on the debug menu and you'll be able to watch things from the AI's perspective. After a few seconds, you'll see a load of stuff appearing on Tracebot's (player 1's) console:
So there you have it. Tracebot is alive and kicking!
5. Package your mod
Now that we know Tracebot works, the final stage is to package it as a mod.
Start by moving the Tracebot folder, and everything in it, from the "autoload" folder to the "mods" folder. This will disable your mod, but we'll get it back soon.
Next, compress the Tracebot folder using ZIP compression - there are loads of tools out there such as PowerArchiver, 7ZIP and others that you can use to do this. If you're on an Apple Mac, simply right-click the folder and choose "Compress", everything is so much easier on a Mac
You should now have a "tracebot.zip" file. Turning this in to a .wz file is actually a very complex task. Ok, I lied, it's really simple: Rename the file to "tracebot.wz".
Finally, move that ".wz" file in to the "autoload" folder and then restart Warzone. Do a quick test, as shown above, to make sure it's still working OK.
If you run in to problems with this step, it's likely that you didn't ZIP the folders properly – you need to ensure that the ZIP, when extracted, includes the "Tracebot" folder.
So, there's your first AI mod, packaged and ready to go!