-
Notifications
You must be signed in to change notification settings - Fork 0
/
role2.delivery.js
115 lines (110 loc) · 3.59 KB
/
role2.delivery.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
const BodyBuilder = require('util.bodybuilder');
ROLE = "delivery";
module.exports = {
spawn: function(spawn) {
// only carry+move
// only spawn if room has a lot of sitting energy
const energyAvailable = spawn.room.energyAvailable;
// if(energyAvailable < MIN_COST*2 || spawn.spawning) { return false; }
var builder = new BodyBuilder([CARRY, CARRY, MOVE, MOVE], energyAvailable);
const body = builder.extend([CARRY, MOVE], limit=8);
spawn.spawnCreep(body, `${ROLE}-${spawn.name}-${Game.time}`, {memory: {
role: ROLE,
filling: true,
cost: body.cost,
}});
},
run: function(creep) {
if(creep.carry.energy == 0) creep.memory.filling = true;
if(creep.memory.filling) {
fill(creep);
}
if(!creep.memory.filling) {
deliver(creep);
}
},
ROLE,
};
function fill(creep) {
var src = Game.getObjectById(creep.memory.src);
if(src && src.store && src.store.energy < 20) src = null;
if(!src) { // find one
src = creep.pos.findClosestByPath(FIND_DROPPED_RESOURCES, {filter:
r => r.resourceType == RESOURCE_ENERGY && r.amount > 20});
if(!src) {
src = creep.pos.findClosestByPath(FIND_STRUCTURES, {filter:
s => s.structureType == STRUCTURE_STORAGE && s.store.energy > 20});
}
if(!src) {
if(creep.carry.energy > 0) creep.memory.filling = false;
return ERR_NOT_FOUND;
}
creep.memory.src = src.id;
}
const srcDist = creep.pos.getRangeTo(src);
if(srcDist > 1) {
creep.moveTo(src);
}
if(srcDist <= 1) {
var avail;
var ret;
if(src instanceof Resource) {
avail = src.amount;
ret = creep.pickup(src);
} else {
console.log(src, src.store);
avail = src.store.energy;
ret = creep.withdraw(src, RESOURCE_ENERGY);
}
if(creep.carry.energy + avail >= creep.carryCapacity) {
creep.memory.filling = false;
}
} else {
return ERR_NOT_IN_RANGE;
}
}
function deliver(creep) {
var dst = Game.getObjectById(creep.memory.dest);
if(dst && dst.carryCapacity - _.sum(dst.carry) < 20) dst = null;
if(!dst) {
dst = creep.pos.findClosestByPath(FIND_MY_CREEPS, {filter: c => {
if(c.id == creep.id) return false;
const total = _.sum(c.carry);
// TODO(baptr): Figure out how to identify creeps that need energy.
if(c.carryCapacity - total > 20) {
return true;
}
return false;
}});
if(!dst) {
dst = creep.pos.findClosestByPath(FIND_MY_STRUCTURES, {filter: s => {
if(s.structureType != STRUCTURE_EXTENSION || s.structureType != STRUCTURE_SPAWN) {
return false;
}
return s.energy < s.energyCapacity;
}})
}
if(!dst) {
if(creep.carry.energy < creep.carryCapacity) {
creep.memory.filling = true;
}
return ERR_NOT_FOUND;
}
creep.memory.dest = dst.id;
}
const dist = creep.pos.getRangeTo(dst);
if(dist >= 1) {
creep.moveTo(dst);
}
if(dist <= 1) {
var dstSpace = 0;
if(dst instanceof Creep) {
dstSpace = dst.carryCapacity - _.sum(dst.carry);
} else {
dstSpace = dst.storeCapacity - _.sum(dst.store);
}
if(creep.transfer(dst, RESOURCE_ENERGY) == OK) {
creep.memory.delivered += Math.min(dstSpace, creep.carry.energy);
}
}
}