-
Notifications
You must be signed in to change notification settings - Fork 10
Task
Tasks are in their most basic form a wrapper around various actions that a creep can perform. Each creep has a memory.taskList item which contains an array of tasks for the creep to perform. Each tick each creep works it's way through its task list.
In the main.function we call
raceBase.moveCreeps();
Now if we inspect raceBase.moveCreeps() we see that each creep is passed to the roleBase.run method.
for (var creepName in Game.creeps) {
roleBase.run(Game.creeps[creepName]);
}
If we now look at roleBase.run()
run: function(creep) {
if (!creep.memory.tasks || !creep.memory.tasks.tasklist) {
roleBase.switchRoles(creep, gc.ROLE_FLEXI_STORAGE_PORTER);
}
var tasks = require("tasks");
tasks.doTasks(creep);
},
We see that after setting a default task if none is avalable, the tasks.doTasks method is called on each creep. This has a while loop which calls the doTask method of the task at the top of the creep's taskList array as follows.
var moduleName = "task." + task.taskType;
var taskModule = require(moduleName);
result = taskModule.prototype.doTask(creep, task, doneActions);
Hence: each task object consists of a constructor and a prototype.doTask method
Each task must have the following
- The filename must start "task."
- The main exported function is a constructor.
- The constructor must create a taskType member with a value equal to the part of the filename after "task.". Ideally there should be a matching TASK_ entry in gc.js
- Have a loop boolean member variable.
- There must be a prototype.doTask member
- doTask must return one of //task results RESULT_FINISHED: "finished", RESULT_UNFINISHED: "unfinished", RESULT_ROLLBACK: "rollback", RESULT_RESET: "reset",
The main doTask method must return one of a set number of return values depending on the result of the action. Further, the value of the loop member effects how the result is processed.
- finished This indicates that the task has completed. If loop is true, the task is moved to the bottom of the taskList array. Otherwise, the task is spliced from the array, and the taskList is reduced in length by one. The task.doTasks method now tries to do the next task in the taskList.
- unfinished The task has not been completed. The creep taskList remains unchanged and the task.doTasks method exits for this creep.
- rollback Used to repeat repetitive actions pairs. Like repeating build, move, build, move loops. The last item in the taskList is poped then unshifed into the head of the list, pushing the just completed action into the second position. The doTasks method now attempts to perform the doTask method of the new head task.
- reset Indicates that the task has self-modified the creeps taskList and any cached values of the taskList are invalid. In practice, it just terminates the creeps tasks for this tick, and the creep has a new tasklist for next tick.