-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrole.miner.js
121 lines (113 loc) · 4.38 KB
/
role.miner.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
module.exports = {
// a function to run the logic for this role
run: function(creep) {
if (creep.memory.target != undefined && creep.room.name != creep.memory.target && creep.memory.working == false) {
// find exit to target room
var exit = creep.room.findExitTo(creep.memory.target);
// move to exit
creep.travelTo(creep.pos.findClosestByRange(exit));
// return the function to not do anything else
return;
}
// get source
let source = Game.getObjectById(creep.memory.sourceId);
if (source) {
if (creep.memory.isCarryCreep) {
// find container next to source
let container = source.pos.findInRange(FIND_STRUCTURES, 1, {
filter: s => s.structureType == STRUCTURE_CONTAINER
})[0];
// if creep is on top of the container
if (container && !creep.pos.isEqualTo(container.pos)) {
// harvest source
creep.travelTo(container);
}
else {
// if creep is by the source
if (creep.pos.inRangeTo(source, 1)) {
const totalCarry = _.sum(creep.carry);
if (totalCarry >= 40) {
link = Game.getObjectById(creep.memory.link.id)
if (link != undefined) {
if (totalCarry > link.energyCapacity - link.energy) {
creep.transfer(link, RESOURCE_ENERGY, link.energyCapacity - link.energy);
} else {
creep.transfer(link, RESOURCE_ENERGY, totalCarry);
}
}
}
// if the creep is on top of a container that is not full, harvest energy.
if(container){
if(_.sum(container.store) < container.storeCapacity){
creep.harvest(source);
}
}
// else if there is no container and the link is full, harvest until the creep is full.
else if(totalCarry != creep.carryCapacity){
creep.harvest(source);
}
}
// if creep is not on top of the container, move to it
else {
let goal = {
pos: source.pos,
range: 1
};
let ret = PathFinder.search(
creep.pos, goal, {
// We need to set the defaults costs higher so that we
// can set the road cost lower in `roomCallback`
plainCost: 2,
swampCost: 10,
roomCallback: function(roomName) {
let room = Game.rooms[roomName];
// In this example `room` will always exist, but since
// PathFinder supports searches which span multiple rooms
// you should be careful!
if (!room) return;
let costs = new PathFinder.CostMatrix;
room.find(FIND_STRUCTURES).forEach(function(struct) {
if (struct.structureType === STRUCTURE_ROAD) {
// Favor roads over plain tiles
costs.set(struct.pos.x, struct.pos.y, 1);
} else if (struct.structureType !== STRUCTURE_CONTAINER &&
(struct.structureType !== STRUCTURE_RAMPART ||
!struct.my)) {
// Can't walk through non-walkable buildings
costs.set(struct.pos.x, struct.pos.y, 0xff);
}
});
// Avoid creeps in the room
room.find(FIND_CREEPS).forEach(function(creep) {
costs.set(creep.pos.x, creep.pos.y, 0xff);
});
return costs;
},
}
);
let pos = ret.path[0];
creep.move(creep.pos.getDirectionTo(pos));
}
}
}
else {
// find container next to source
let container = source.pos.findInRange(FIND_STRUCTURES, 1, {
filter: s => s.structureType == STRUCTURE_CONTAINER
})[0];
// if creep is on top of the container
if (container) {
if (creep.pos.isEqualTo(container.pos)) {
// harvest source
creep.harvest(source);
}
// if creep is not on top of the container
else {
// move towards it
creep.travelTo(container);
}
}
}
}
}
};