-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
100 lines (87 loc) · 5.22 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
var helper = require('helper');
var roleMulti = require('role.multi');
var roleRepair = require('role.repair');
var roleHarvester = require('role.harvester');
var roleUpgrader = require('role.upgrader');
var newCostMatrix = require('newCostMatrix');
var roleClaimer = require('role.claimer');
var tower = require('tower');
module.exports.loop = function () {
for(var name in Memory.creeps) {
if(!Game.creeps[name]) { delete Memory.creeps[name]; }
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
//Loop rooms
for(var rm in Game.rooms){
var curRoom = Game.rooms[rm]
var spawn = curRoom.find(FIND_STRUCTURES, {filter: { structureType: STRUCTURE_SPAWN } })
//
//
helper.buildRoads(curRoom);
//helper.removeUnfinishedRoads(curRoom);
//
//
var multi = _.filter(Game.creeps, (creep) => creep.memory.role == 'multi' && curRoom.name == creep.room.name);
var repair = _.filter(Game.creeps, (creep) => creep.memory.role == 'repair' && curRoom.name == creep.room.name);
var harvester = _.filter(Game.creeps, (creep) => creep.memory.role == 'harvester' && curRoom.name == creep.room.name);
var upgrader = _.filter(Game.creeps, (creep) => creep.memory.role == 'upgrader' && curRoom.name == creep.room.name);
var claimer = _.filter(Game.creeps, (creep) => creep.memory.role == 'claimer' && curRoom.name == creep.room.name);
if (spawn[0] != null) {
newCostMatrix.run(spawn[0]);
var creepType = '';
var cnt = 0;
if(multi.length < helper.maxCreep(curRoom,'multi')) { creepType = 'multi';
} else if(harvester.length < helper.maxCreep(curRoom,'harvester')) { creepType = 'harvester';
} else if(repair.length < helper.maxCreep(curRoom,'repair')) { creepType = 'repair';
} else if(upgrader.length < helper.maxCreep(curRoom,'upgrader')) { creepType = 'upgrader';
} else if(claimer.length < helper.maxCreep(curRoom,'claimer')) { creepType = 'claimer';
}
//determine name... dupes... need to find available #
if (creepType!=''){
var cnt = 0;
for (var i = 1; i<=helper.maxCreep(curRoom,creepType); i++){
if(!Game.creeps[curRoom.name + '.' + helper.toTitleCase(creepType) + String(i)]) {
cnt = i;
break;
}
}
var newName = spawn[0].createCreep(helper.calcBody(curRoom,'role.'+creepType), curRoom.name + '.' + helper.toTitleCase(creepType) + String(cnt), {role: creepType, home: curRoom.name});
if (_.isString(newName)) {
console.log(rm + '-- Multi:' + multi.length + '/' + helper.maxCreep(curRoom,'multi') + ', Harvester:' + harvester.length + '/' + helper.maxCreep(curRoom,'harvester') + ', Repair:' + repair.length + '/' + helper.maxCreep(curRoom,'repair') + ', Upgrader:' + upgrader.length + '/' + helper.maxCreep(curRoom,'upgrader') + ', Claimer:' + claimer.length + '/' + helper.maxCreep(curRoom,'claimer') +', Total Creeps:' + _.filter(Game.creeps, (creep) => curRoom.name == creep.room.name).length);
console.log('Spawning new ' + creepType + ' in room '+ curRoom.name +' named: ' + newName);
} else if (newName == -3) {
console.log("Can't spawn " + creepType + " in room " +curRoom.name+ " because the creep name is taken already!");
}
}
//if all creeps died, spawn the worst possible one to get things going again
//console.log(_.filter(Game.creeps, (c) => curRoom.name==creep.room.name))
if(_.filter(Game.creeps, (creep) => curRoom.name == creep.room.name).length == 0) {
var cnt = multi.length; cnt += 1;
var newName = spawn[0].createCreep([MOVE,MOVE,WORK,CARRY,CARRY], curRoom.name + '.Multi' + String(cnt), {role: 'multi'});
if (_.isString(newName)) { console.log('Spawning new multi in '+ curRoom.name +': ' + newName); }
}
}
var towers = curRoom.find(FIND_MY_STRUCTURES, { filter: {structureType: STRUCTURE_TOWER } });
if (towers != undefined){
for (i = 0; i < towers.length; i++) {
tower.run(towers[i]);
}
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
//Loop Creeps
for(var name in Game.creeps) {
var creep = Game.creeps[name];
if(creep.memory.role == 'multi') {
roleMulti.run(creep);
} else if (creep.memory.role == 'repair') {
roleRepair.run(creep);
} else if (creep.memory.role == 'harvester') {
roleHarvester.run(creep);
} else if (creep.memory.role == 'upgrader') {
roleUpgrader.run(creep);
} else if (creep.memory.role == 'claimer') {
roleClaimer.run(creep);
}
}
}