I am trying to make an AI that plays a game. Actually, I'm playing around with both StarCraft and MineCraft which are completely different kinds of games. As well, I use different programming languages for both, and one is multi-threaded (java), other event-driven single-threaded (Node.js).
Despite all those differences, I face the same problem: How can I make an AI that can handle tasks in sensible order, but also interrupt the tasks in cases of need and return to them later? How to make a sensible task chain (where one task causes another subtask)?
Examples of above can be:
- In minecraft, you're digging a block and you're attacked by a monster
- In starcraft you're leading an attack and a critical position in your base is attacked
- In minecraft, task of chopping trees is caused by the task building the house - the bot must remember why is he doing what is he doing.
- In starcraft, certain buildings are just built in order to unlock types of soldiers
I just was playing around with the idea, but I just kept writing and deleting completely stupid code...
This is what I have now, but it makes no sense. I can see many situations that will not be solved using this model.
/***
* Task pseudo class. After finishing the task, this task notifies the parent task.
* Arguments:
* action - function callback that will execute. The callback must call the finished() method in the end. The callback will receive
* this task's object as a first argument.
**/
function Task(action, parent, name) {
this.action = action;
this.after = after;
this.name = name||"Void task";
}
Task.prototype.start = function() {
console.log("Task started...");
try {
this.action(this);
}
catch(e) {
console.log("The task action has thrown an error: "+e);
}
}
Task.prototype.finished = function() {
console.log("Task over.");
}
//Override this
Task.prototype.pause = function() {};
I am afraid I have a lot of reading to do about this. But are there some general known ideas I can start with?
cancel()
property or method that you can call. It sets acancel
flag, and the task gracefully shuts down when it can. – Robert Harvey Jul 09 '15 at 20:21