-
Notifications
You must be signed in to change notification settings - Fork 7
/
global.operation.js
128 lines (103 loc) · 3.55 KB
/
global.operation.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
const operationsCache = {
operations: function(key) {
this.ensureList();
return this.opsList;
},
add: function(operation) {
this.ensureList();
this.opsList.push(operation);
},
remove: function(operation) {
this.ensureList();
let index = this.opsList.indexOf(operation);
if (index > -1) {
this.opsList.splice(index, 1);
}
},
ensureList: function() {
if(!this.opsList || Game.time !== this.currentTick) {
this.opsList = _.compact(_.map((Memory.operations || []), (mem) => Operation.loadOperation(mem)));
this.currentTick = Game.time;
}
}
}
global.Operation = class Operation {
constructor(memory) {
this.memory = memory;
this.id = memory.id;
this.type = memory.type;
}
get supportRoom() {
return Game.rooms[this.memory.supportRoom];
}
set supportRoom(room) {
this.memory.supportRoom = room ? room.name : null;
}
isValid() {
return this.supportRoom && this.supportRoom.ai();
}
run() {
// TODO: call this method for all operations in main.js
// should be overridden in operations to run code once per tick for the operation
// (that is independent from rooms supporting the operation)
// e.g. picking a common target for creeps, figuring out whether the operation
// is finished/canceled, etc
}
supportRoomCallback(room) {
// should be overridden in operations to run code specific to the room supporting this operation,
// e.g. spawning creeps, setting up boosts, using observers, etc.
}
drawVisuals() {
// can be overridden to draw visuals to aid in understanding what this operation
// is doing
}
toString() {
return `[Operation ${this.type}#${this.id} from ${this.supportRoom}]`;
}
static get operations() {
return operationsCache.operations();
}
static forSupportRoom(room) {
return _.filter(Operation.operations, (op) => op.supportRoom === room);
}
static createOperation(type, initMemory) {
if(!Memory.operations) Memory.operations = [];
let subclass = operationSubclasses[type];
if(!subclass) {
console.log(`Tried to create unknown operation type ${type}`);
return;
}
if(!initMemory) initMemory = {};
let memory = { type: type, id: Operation.generateId(), ...initMemory };
let instance = new subclass(memory);
Memory.operations.push(memory);
return instance;
}
static removeOperation(operation) {
Memory.operations = _.filter(Memory.operations, (o) => o.id !== operation.id);
operationsCache.remove(operation);
}
// used internally to populate Operation.operations
static loadOperation(memory) {
let subclass = operationSubclasses[memory.type];
if(!subclass) {
console.log(`Tried to load unknown operation type ${type} from operation ${memory.id}.`);
return null;
}
return new subclass(memory);
}
static generateId() {
let id = 0;
do {
id = Math.floor(Math.random() * 100000);
} while(_.any(Memory.operations, (op) => op.id === id));
return id;
}
}
const operationSubclasses = {
attack: require("operation.attack"),
claim: require("operation.claim"),
downgrade: require("operation.downgrade"),
drain: require("operation.drain"),
scoop: require("operation.scoop")
};