>_
Screeps ai
The main goal of this repo is to provide a flexible framework for the ai.
Ai rules / Ai constrains
- All creeps must have a role
Installing
- Make sure you've installed node 0.12 (0.10 should currently work fine too).
- Open a terminal and install grunt with npm like this:
npm install -g grunt-cli
. - Make now sure the terminal is pointing to this project. Then run
npm install
to install dependencies.
Build configuration
- Open
Gruntfile.js
in an editor, look forscreeps: {
, fill in the options and save the file. - In a terminal open in a copy of this project: type
grunt deploy
- If done properly, this should push the code to screeps.com succesfully.
Some tricks
grunt deploy
: Pushes code to screeps.comgrunt codegen
: Generates_generated
and puts it inbuild/deploy/
grunt test
: To make sure that the code is passing the checksgrunt
is currently set togrunt test
Code
structure
scripts/main.js
is the boot file, its being executed once a tick in the screeps sandbox.
Please note that not all code of my current ai are included in this repo.
AI script files
_settings.js
Drain for global settings._generic.js
Functions that only uses javascript features_utils.js
Functions using the screeps API
AI Object
When including _generated
it will expose the AI global.
The AI object has _api
, _generic
, _utils
and _settings
included,
so there is no need to include these files.
Simply use the AI object to access their functions.
It also includes code from extensions accessible from AI.extensions
.
_generated
is a generated file that only exists in the build/deploy
folder when using grunt deploy
or grunt codegen
Extensions
After using require('_generated')
it will expose the extensions as
AI.extensions.<type>.<plugin>
.
These files are coming from the extensions/
directory with a structure as
extensions/<package>/<type>/<plugin>
.
These include all files ending with .js
, excluding the files starting with a .
like .thisFileWillBeIgnoredAnyway.js
.
When generating the extensions, all packages are virtually merged into a single package.
This is how it ends up using the AI.extensions.<type>.<plugin>
format.
Current extension types are:
- commands
- Located at
extensions/<any directory>/commands/
- Executes flag commands
- Used when called by
extensions/tools/hooks/flags
orAI.exec(<command>, ...)
- Located at
- hooks (replaced units)
- Located at
extensions/<any directory>/hooks/
- Events when hooks are called (see
scripts/stage_controller
):- firstTurn: Called when the AI is doing its first turn
- preController: Called before giving spawns and creepers orders
- postController: Called before shutting down the AI
- Located at
- roles
- Located at
extensions/<any directory>/roles/
- Gives creeps orders
- Called by stage_creeps, the plugin with the corresponding
Creep.memory.role
will be executed
- Located at
- targets
- Collects or filters to get certain type of objects
- Usable by
AI.get(target, options)
orRoom.get(target, options)
for getting objects AI.extensions.target.filter
contains customizable filters
Ai code
scripts/main
is the boot file. From there it includes other files in the script/
directory.
Most of these scripts (called stages) utilizes code from extensions.
Call hierarchy
`- Start timer
`- Run stage controllers (stage_*.js)
| `- stage_setup Prepares game state for AI
| `- stage_controller.pre() Triggers firstTurn (if needed) and preControllers in hook extensions
| `- stage_creeps() Iterates over every creep (give orders)
| `- stage_spawners() Iterates over every spawner (give spawn jobs)
| `- stage_controller.post() Shutdown and triggers postController in hook extensions
`- Stop timer