-
Notifications
You must be signed in to change notification settings - Fork 0
/
role.wallRepairer.js
77 lines (67 loc) · 3.01 KB
/
role.wallRepairer.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
require('prototype.Creep')();
var roleBuilder = require('role.builder');
var Utils = require('help.functions');
module.exports = {
// a function to run the logic for this role
run: function(creep, cache) {
if(creep.spawning){ return;}
Utils.pickupNearbyEnergy(creep);
// if creep is trying to repair something but has no energy left
switch(creep.carry.energy)
{ case 0: creep.memory.working = false; creep.memory.stateTransition = true; break;
case creep.carryCapacity: creep.memory.working = true; creep.memory.stateTransition = true; break;
}
// roleBuilder.run(creep);return;
// if creep is supposed to repair something
if (creep.memory.working == true) {
// find all walls in the room
var targets = cache.get(creep.pos.roomName+'_walls');
var target = undefined;
//console.log(targets)
if(_.isUndefined(targets) || targets < 0){roleBuilder.run(creep); return;}
if(creep.memory.stateTransition){
creep.memory.stateTransition = false;
var wallTarget = undefined;
var wallTargetPerc = 1.0;
var rampartTarget = undefined;
var rampartTargetPerc = 1.0;
for(let i in targets) {
if(((targets[i].hits/targets[i].hitsMax) < wallTargetPerc) && targets[i].structureType == STRUCTURE_WALL) {
wallTarget = targets[i];
wallTargetPerc = targets[i].hits/targets[i].hitsMax;
}
if(((targets[i].hits/targets[i].hitsMax) < rampartTargetPerc) && targets[i].structureType == STRUCTURE_RAMPART) {
rampartTarget = targets[i];
rampartTargetPerc = targets[i].hits/targets[i].hitsMax;
}
}
if( rampartTarget && rampartTarget.hits != rampartTarget.hitsMax && rampartTarget.hits < wallTarget.hits) {
target = rampartTarget;
} else {
target = wallTarget;
}
creep.memory.target = target.id;
}
if(!_.isUndefined(creep.memory.target))
target = Game.getObjectById(creep.memory.target);
//console.log(creep.name + '\tTarget pos: ' + target.pos);
// if we find a wall that has to be repaired
if (target != undefined) {
// try to repair it, if not in range
if (creep.repair(target) == ERR_NOT_IN_RANGE) {
// move towards it
creep.moveTo(target);
}
}
// if we can't fine one
else {
// look for construction sites
roleBuilder.run(creep);
}
}
// if creep is supposed to harvest energy from source
else {
Utils.harvest(creep);
}
}
};