forked from VladThePaler/screeps.behaviour-action-pattern
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask.js
93 lines (91 loc) · 3.06 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
let mod = {};
module.exports = mod;
// load task memory & flush caches
mod.flush = function () {
const tasks = [
Task.guard,
Task.defense,
Task.claim,
Task.reserve,
Task.mining,
Task.pioneer,
Task.attackController,
Task.robbing,
Task.reputation,
];
for (let i = tasks.length - 1; i >= 0; i--) {
if (tasks[i].flush) {
tasks[i].flush();
}
}
};
// register tasks (hook up into events)
mod.register = function () {
const tasks = [
Task.guard,
Task.defense,
Task.claim,
Task.reserve,
Task.mining,
Task.pioneer,
Task.attackController,
Task.robbing,
Task.reputation,
];
for (let i = tasks.length - 1; i >= 0; i--) {
if (tasks[i].register) {
tasks[i].register();
}
}
};
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 = {};