-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
222 lines (185 loc) · 6.76 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
var harvester = require('harvester');
var builder = require('builder');
var guard = require('guard');
var claimer = require('claimer');
var harvesters = [];
var HARVESTER_MIN_COUNT = 6;
var builders = [];
var BUILDER_MIN_COUNT = 2;
var contractors = [];
var CONTRACTOR_MIN_COUNT = 2;
var claimers = [];
var CLAIMER_MIN_COUNT = 2;
var meleeGuards = [];
var rangedGuards = [];
var patrolGuards = [];
for (var name in Game.creeps) {
var creep = Game.creeps[name];
if (creep.ticksToLive != 0) {
if (creep.memory.role == "harvester") {
if (!creep.memory.paused) harvester(creep);
harvesters.push(creep);
} else if (creep.memory.role == "builder") {
if (!creep.memory.paused) builder(creep);
builders.push(creep);
} else if (creep.memory.role == "contractor") {
if (!creep.memory.paused) builder(creep);
contractors.push(creep);
} else if (creep.memory.role == "meleeGuard") {
if (!creep.memory.paused) guard(creep);
meleeGuards.push(creep);
} else if (creep.memory.role == "rangedGuard") {
if (!creep.memory.paused) guard(creep);
rangedGuards.push(creep);
} else if (creep.memory.role == "patrolGuard") {
if (!creep.memory.paused) guard(creep);
patrolGuards.push(creep);
} else if (creep.memory.role == "claimer") {
if (!creep.memory.paused) claimer(creep);
claimers.push(creep);
}
}
}
var TYPE_RANGED = "ranged";
var TYPE_MELEE = "melee";
var TYPE_PATROL = "patrol";
function findFlags(type) {
var flags = [];
var flagColor = "";
if (type == TYPE_RANGED) {
flagColor = COLOR_BLUE;
} else if (type == TYPE_MELEE) {
flagColor = COLOR_RED;
} else if (type == TYPE_PATROL) {
flagColor = COLOR_GREEN;
}
flags = Game.spawns.Spawn1.room.find(FIND_FLAGS, {filter: function(flag) {
return flag.color == flagColor;
}});
return flags;
}
function findFreeFlags(type, creeps) {
return findFlags(type).filter(function(flag) {
var result = true;
for (var i in creeps) {
var creep = creeps[i];
if (creep.memory.flag.name == flag.name) {
result = false;
}
}
return result;
});
}
function checkSpawnable(body, name) {
var spawnable = Game.spawns.Spawn1.canCreateCreep(body, name);
if (!spawnable) return true
else return false
}
var spawnQueue = {
queue_: [],
addSpawn: function(role) {
this.queue_.push(role);
},
checkRoleCount: function(role) {
var types = {};
for (var i in this.queue_) {
var type = this.queue_[i];
if (types[type]) {
types[type] += 1;
} else {
types[type] = 1;
}
}
if (types[role]) return types[role];
else return 0;
},
logQueue: function() {
console.log(this.queue_);
},
clearQueue: function() {
this.queue_ = [];
},
spawn: function() {
if (this.queue_.length > 0) {
// Get the item at the top of the queue by reversing the queue,
// popping, and reversing back to the original order
this.queue_.reverse();
var role = this.queue_.pop();
this.queue_.reverse();
var creepID = Math.floor((Math.random() * 1000000) + 1);
var generalBody = [WORK, WORK, CARRY, CARRY, MOVE, MOVE];
var meleeGuardBody = [ATTACK, ATTACK, ATTACK, MOVE, MOVE, MOVE];
var rangedGuardBody = [RANGED_ATTACK, RANGED_ATTACK, MOVE, MOVE, MOVE];
switch (role) {
case "harvester":
return Game.spawns.Spawn1.createCreep(generalBody, "harvester_"+creepID, {role: "harvester"});
break;
case "claimer":
return Game.spawns.Spawn1.createCreep(generalBody, "claimer_"+creepID, {role: "claimer"});
break;
case "contractor":
return Game.spawns.Spawn1.createCreep(generalBody, "contractor_"+creepID, {role: "contractor", task: "idle"});
break;
case "builder":
return Game.spawns.Spawn1.createCreep(generalBody, "builder_"+creepID, {role: "builder", buildOnly: true, task: "idle"});
break;
case "meleeGuard":
var freeFlags = findFreeFlags(TYPE_MELEE, meleeGuards);
var flag = "";
if (freeFlags[0]) {
flag = freeFlags[0];
}
return Game.spawns.Spawn1.createCreep(meleeGuardBody, "melee_"+creepID, {role: "meleeGuard", flag: flag});
break;
case "rangedGuard":
var freeFlags = findFreeFlags(TYPE_RANGED, rangedGuards);
var flag = "";
if (freeFlags[0]) {
flag = freeFlags[0];
}
return Game.spawns.Spawn1.createCreep(rangedGuardBody, "ranged_"+creepID, {role: "rangedGuard", flag: flag});
break;
case "patrolGuard":
var freeFlags = findFreeFlags(TYPE_PATROL, patrolGuards);
var flag = "";
if (freeFlags[0]) {
flag = freeFlags[0];
}
return Game.spawns.Spawn1.createCreep(meleeGuardBody, "patrol_"+creepID, {role: "patrolGuard", flag: flag, targetPos: {}});
break;
default:
console.log("Unexpected role '"+role+"' on queue");
break;
}
}
}
};
if (harvesters.length == 0) {
// Bad times. Gotta get a new one.
if (builders.length > 0) builders[0].memory.role = "harvester";
else if (contractors.length > 0) contractors[0].memory.role = "harvester";
else if (claimers.length > 0) claimers[0].memory.role = "harvester";
}
if (harvesters.length < HARVESTER_MIN_COUNT) {
spawnQueue.addSpawn("harvester");
}
if (findFreeFlags(TYPE_MELEE, meleeGuards).length > 0) {
spawnQueue.addSpawn("meleeGuard");
}
if (findFreeFlags(TYPE_RANGED, rangedGuards).length > 0) {
spawnQueue.addSpawn("rangedGuard");
}
if (findFreeFlags(TYPE_PATROL, patrolGuards).length > 0) {
spawnQueue.addSpawn("patrolGuard");
}
if (claimers.length < CLAIMER_MIN_COUNT) {
spawnQueue.addSpawn("claimer");
}
if (builders.length < BUILDER_MIN_COUNT) {
spawnQueue.addSpawn("builder");
}
if (contractors.length < CONTRACTOR_MIN_COUNT) {
spawnQueue.addSpawn("contractor");
}
var result = spawnQueue.spawn();
//console.log(result);