forked from VladThePaler/screeps.behaviour-action-pattern
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
287 lines (273 loc) · 10.1 KB
/
main.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/* https://github.com/ScreepsOCS/screeps.behaviour-action-pattern */
const cpuAtLoad = Game.cpu.getUsed();
// check if a path is valid
global.validatePath = path => {
let mod;
try {
mod = require(path);
}
catch (e) {
if (global.DEBUG !== false && !(e.message && e.message.startsWith('Unknown module'))) {
console.log('<font style="color:FireBrick">Error loading ' + path
+ ' caused by ' + (e.stack || e.toString()) + '</font>');
}
mod = null;
}
return mod != null;
};
// evaluate existing module overrides and store them to memory.
// return current module path to use for require
global.getPath = (modName, reevaluate = false) => {
if( reevaluate || !Memory.modules[modName] ){
// find base file
let path = './custom.' + modName;
if(!validatePath(path)) {
path = './internal.' + modName;
if(!validatePath(path))
path = './' + modName;
}
Memory.modules[modName] = path;
// find viral file
path = './internalViral.' + modName;
if(validatePath(path))
Memory.modules.internalViral[modName] = true;
else if( Memory.modules.internalViral[modName] )
delete Memory.modules.internalViral[modName];
path = './viral.' + modName;
if(validatePath(path))
Memory.modules.viral[modName] = true;
else if( Memory.modules.viral[modName] )
delete Memory.modules.viral[modName];
}
return Memory.modules[modName];
};
// try to require a module. Log errors.
global.tryRequire = (path, silent = false) => {
let mod;
try{
mod = require(path);
} catch(e) {
if( e.message && e.message.indexOf('Unknown module') > -1 ){
if(!silent) console.log(`Module "${path}" not found!`);
} else if(mod == null) {
console.log(`Error loading module "${path}"!<br/>${e.stack || e.toString()}`);
}
mod = null;
}
return mod;
};
// inject members of alien class into base class. specify a namespace to call originals from baseObject.baseOf[namespace]['<functionName>'] later
global.inject = (base, alien, namespace) => {
let keys = _.keys(alien);
for (const key of keys) {
if (typeof alien[key] === "function") {
if( namespace ){
let original = base[key];
if( !base.baseOf ) base.baseOf = {};
if( !base.baseOf[namespace] ) base.baseOf[namespace] = {};
if( !base.baseOf[namespace][key] ) base.baseOf[namespace][key] = original;
}
base[key] = alien[key].bind(base);
} else {
base[key] = alien[key]
}
}
};
// partially override a module using a registered viral file
global.infect = (mod, namespace, modName) => {
if( Memory.modules[namespace][modName] ) {
// get module from stored viral override path
let viralOverride = tryRequire(`./${namespace}.${modName}`);
// override
if( viralOverride ) {
global.inject(mod, viralOverride, namespace);
}
// cleanup
else delete Memory.modules[namespace][modName];
}
return mod;
};
// loads (require) a module. use this function anywhere you want to load a module.
// respects custom and viral overrides
global.load = (modName) => {
// read stored module path
let path = getPath(modName);
// try to load module
let mod = tryRequire(path, true);
if( !mod ) {
// re-evaluate path
path = getPath(modName, true);
// try to load module. Log error to console.
mod = tryRequire(path);
}
if( mod ) {
// load viral overrides
mod = infect(mod, 'internalViral', modName);
mod = infect(mod, 'viral', modName);
}
return mod;
};
// load code
global.install = () => {
// ensure required memory namespaces
if (Memory.modules === undefined) {
Memory.modules = {
viral: {},
internalViral: {}
};
}
// Initialize global & parameters
//let glob = load("global");
global.inject(global, load("global"));
_.assign(global, load("parameter"));
global.mainInjection = load("mainInjection");
// Load modules
_.assign(global, {
Extensions: load("extensions"),
Population: load("population"),
FlagDir: load("flagDir"),
Task: load("task"),
Tower: load("tower"),
Events: load('events'),
Grafana: GRAFANA ? load('grafana') : undefined,
});
_.assign(global.Task, {
guard: load("task.guard"),
defense: load("task.defense"),
mining: load("task.mining"),
claim: load("task.claim"),
reserve: load("task.reserve"),
pioneer: load("task.pioneer"),
attackController: load("task.attackController"),
robbing: load("task.robbing"),
reputation: load("task.reputation"),
});
Creep.Action = load("creep.Action");
Creep.Setup = load("creep.Setup");
_.assign(Creep, {
action: {
building: load("creep.action.building"),
charging: load("creep.action.charging"),
claiming: load("creep.action.claiming"),
defending: load("creep.action.defending"),
dismantling: load("creep.action.dismantling"),
feeding: load("creep.action.feeding"),
fortifying: load("creep.action.fortifying"),
fueling: load("creep.action.fueling"),
guarding: load("creep.action.guarding"),
harvesting: load("creep.action.harvesting"),
healing: load("creep.action.healing"),
idle: load("creep.action.idle"),
invading: load("creep.action.invading"),
picking: load("creep.action.picking"),
repairing: load("creep.action.repairing"),
reserving: load("creep.action.reserving"),
travelling: load("creep.action.travelling"),
storing: load("creep.action.storing"),
uncharging: load("creep.action.uncharging"),
upgrading: load("creep.action.upgrading"),
withdrawing: load("creep.action.withdrawing"),
robbing:load("creep.action.robbing"),
reallocating:load("creep.action.reallocating"),
recycling:load("creep.action.recycling"),
attackController:load("creep.action.attackController")
},
behaviour: {
claimer: load("creep.behaviour.claimer"),
hauler: load("creep.behaviour.hauler"),
healer: load("creep.behaviour.healer"),
melee: load("creep.behaviour.melee"),
miner: load("creep.behaviour.miner"),
mineralMiner: load("creep.behaviour.mineralMiner"),
remoteMiner: load("creep.behaviour.remoteMiner"),
remoteHauler: load("creep.behaviour.remoteHauler"),
remoteWorker: load("creep.behaviour.remoteWorker"),
pioneer: load("creep.behaviour.pioneer"),
privateer: load("creep.behaviour.privateer"),
recycler: load("creep.behaviour.recycler"),
ranger: load("creep.behaviour.ranger"),
upgrader: load("creep.behaviour.upgrader"),
worker: load("creep.behaviour.worker")
},
setup: {
hauler: load("creep.setup.hauler"),
healer: load("creep.setup.healer"),
miner: load("creep.setup.miner"),
mineralMiner: load("creep.setup.mineralMiner"),
privateer: load("creep.setup.privateer"),
upgrader: load("creep.setup.upgrader"),
worker: load("creep.setup.worker")
}
});
global.inject(Creep, load("creep"));
global.inject(Room, load("room"));
global.inject(Spawn, load("spawn"));
// Extend server objects
//global.extend();
Extensions.extend();
Creep.extend();
Room.extend();
Spawn.extend();
FlagDir.extend();
// custom extend
if( global.mainInjection.extend ) global.mainInjection.extend();
};
global.install();
let cpuAtFirstLoop;
module.exports.loop = function () {
const cpuAtLoop = Game.cpu.getUsed();
if (!cpuAtFirstLoop) cpuAtFirstLoop = cpuAtLoop;
// ensure required memory namespaces
if (Memory.modules === undefined) {
Memory.modules = {
viral: {},
internalViral: {}
};
}
if (Memory.debugTrace === undefined) {
Memory.debugTrace = {error:true, no:{}};
}
// ensure up to date parameters
_.assign(global, load("parameter"));
global.isNewServer = Game.cacheTime !== Game.time-1 || Game.time - Game.lastServerSwitch > 50; // enforce reload after 50 ticks
if( global.isNewServer ) Game.lastServerSwitch = Game.time;
// Flush cache
Events.flush();
FlagDir.flush();
Population.flush();
Room.flush();
Task.flush();
// custom flush
if( global.mainInjection.flush ) global.mainInjection.flush();
// analyze environment
FlagDir.analyze();
Room.analyze();
Population.analyze();
// custom analyze
if( global.mainInjection.analyze ) global.mainInjection.analyze();
// Register event hooks
Creep.register();
Spawn.register();
Task.register();
// custom register
if( global.mainInjection.register ) global.mainInjection.register();
// Execution
Population.execute();
FlagDir.execute();
Room.execute();
Creep.execute();
Spawn.execute();
// custom execute
if( global.mainInjection.execute ) global.mainInjection.execute();
// Postprocessing
if( !Memory.statistics || ( Memory.statistics.tick && Memory.statistics.tick + TIME_REPORT <= Game.time ))
load("statistics").process();
processReports();
FlagDir.cleanup();
Population.cleanup();
// custom cleanup
if( global.mainInjection.cleanup ) global.mainInjection.cleanup();
if ( GRAFANA && Game.time % GRAFANA_INTERVAL === 0 ) Grafana.run();
Game.cacheTime = Game.time;
if( DEBUG && TRACE ) trace('main', {cpuAtLoad, cpuAtFirstLoop, cpuAtLoop, cpuTick: Game.cpu.getUsed(), isNewServer: global.isNewServer, lastServerSwitch: Game.lastServerSwitch, main:'cpu'});
};