forked from VladThePaler/screeps.behaviour-action-pattern
-
Notifications
You must be signed in to change notification settings - Fork 0
/
task.js
109 lines (106 loc) · 4.34 KB
/
task.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
let mod = {};
module.exports = mod;
mod.tasks = [];
mod.populate = function() {
Task.addTasks(...[
Task.attackController,
Task.claim,
Task.defense,
Task.guard,
Task.labTech,
Task.mining,
Task.pioneer,
Task.reputation,
Task.reserve,
Task.robbing,
]);
};
mod.addTasks = (...task) => Task.tasks.push(...task);
mod.installTask = (...taskNames) => {
taskNames.forEach(taskName => {
Task[taskName] = load(`task.${taskName}`);
Task.addTasks(Task[taskName]);
});
};
// load task memory & flush caches
mod.flush = function () {
Task.tasks.forEach(task => {
if (task.flush) task.flush();
});
};
// temporary hack to avoid registering twice internally, remove and fix internal when merged.
mod.selfRegister = true;
// register tasks (hook up into events)
mod.register = function () {
Task.tasks.forEach(task => {
// Extending of any other kind
if (task.register) task.register();
// Flag Events
if (task.handleFlagFound) Flag.found.on(flag => task.handleFlagFound(flag));
if (task.handleFlagRemoved) Flag.FlagRemoved.on(flagName => task.handleFlagRemoved(flagName));
// Creep Events
if (task.handleSpawningStarted) Creep.spawningStarted.on(params => task.handleSpawningStarted(params));
if (task.handleSpawningCompleted) Creep.spawningCompleted.on(creep => task.handleSpawningCompleted(creep));
if (task.handleCreepDied) {
Creep.predictedRenewal.on(creep => task.handleCreepDied(creep.name));
Creep.died.on(name => task.handleCreepDied(name));
}
if (task.handleCreepError) Creep.error.on(errorData => task.handleCreepError(errorData));
// Room events
if (task.handleNewInvader) Room.newInvader.on(invader => task.handleNewInvader(invader));
if (task.handleKnownInvader) Room.knownInvader.on(invaderID => task.handleKnownInvader(invaderID));
if (task.handleGoneInvader) Room.goneInvader.on(invaderID => task.handleGoneInvader(invaderID));
if (task.handleRoomDied) Room.collapsed.on(room => task.handleRoomDied(room));
});
};
mod.memory = (task, s) => { // task: (string) name of the task, s: (string) any selector for that task, could be room name, flag name, enemy name
if( !Memory.tasks ) Memory.tasks = {};
if( !Memory.tasks[task] ) Memory.tasks[task] = {};
if( !Memory.tasks[task][s] ) Memory.tasks[task][s] = {};
return Memory.tasks[task][s];
};
mod.clearMemory = (task, s) => {
if( Memory.tasks[task] && Memory.tasks[task][s] )
delete Memory.tasks[task][s];
};
mod.cache = (task, s) => {
if( !cache[task] ) cache[task] = {};
if( !cache[task][s] ) cache[task][s] = {};
return cache[task][s];
};
mod.clearCache = (task, s) => {
if( cache[task] && cache[task][s] )
delete cache[task][s];
};
// creepDefinition: { queue, name, behaviour, fixedBody, multiBody }
// destiny: { task, targetName }
// roomParams: { targetRoom, minRCL = 0, maxRange = Infinity, minEnergyAvailable = 0, minEnergyCapacity = 0, callBack = null, allowTargetRoom = false, rangeRclRatio = 3, rangeQueueRatio = 51 }
mod.spawn = (creepDefinition, destiny, roomParams, onQueued) => {
// get nearest room
let room = roomParams.explicit ? Game.rooms[roomParams.explicit] : Room.findSpawnRoom(roomParams);
if( !room ) return null;
// define new creep
if(!destiny) destiny = {};
if(!destiny.room && roomParams.targetRoom) destiny.room = roomParams.targetRoom;
let parts = Creep.compileBody(room, creepDefinition);
let name = `${creepDefinition.name || creepDefinition.behaviour}-${destiny.targetName}`;
let creepSetup = {
parts: parts,
name: name,
behaviour: creepDefinition.behaviour,
destiny: destiny,
queueRoom: room.name
};
if( creepSetup.parts.length === 0 ) {
// creep has no body.
global.logSystem(flag.pos.roomName, dye(CRAYON.error, `${destiny.task} task tried to queue a zero parts body ${creepDefinition.behaviour} creep. Aborted.` ));
return null;
}
// queue creep for spawning
let queue = room['spawnQueue' + creepDefinition.queue] || room.spawnQueueLow;
queue.push(creepSetup);
// save queued creep to task memory
if( onQueued ) onQueued(creepSetup);
return creepSetup;
};
const cache = {};