forked from Garethp/Screeps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
roles_miner.js
107 lines (80 loc) · 2.52 KB
/
roles_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
/**
* This guy just finds a source, and stays near it. His job is just to mine away and let the energy fall on the ground
*
* @TODO: See if we can't implement preffered spawn spots close to their source
* @param creep
*/
var miner = {
parts: [
[Game.MOVE, Game.WORK, Game.WORK, Game.WORK, Game.WORK],
[Game.MOVE, Game.WORK, Game.WORK, Game.WORK, Game.WORK, Game.WORK]
],
getOpenSource: function()
{
var creep = this.creep;
var source = creep.pos.findNearest(Game.SOURCES, {
filter: function(source)
{
if(Memory.sources[source.id] == undefined || Memory.sources[source.id].miner == undefined || Memory.sources[source.id].miner == creep.id)
return true;
if(Game.getObjectById(Memory.sources[source.id].miner) == null)
return true;
return false;
}
});
return source;
},
setSourceToMine: function(source)
{
var creep = this.creep;
if(!source)
return;
if(Memory.sources[source.id] == undefined)
Memory.sources[source.id] = { id: source.id };
Memory.sources[source.id].miner = creep.id;
creep.memory.source = source.id;
var helperSpawn = source.pos.findNearest(Game.MY_SPAWNS);
var steps = helperSpawn.pos.findPathTo(source).length * 2;
var creepsNeeded = Math.round((steps * 8) / 100);
if(creepsNeeded > 5)
creepsNeeded = 5;
for(var i = 0; i < creepsNeeded; i++)
Memory.spawnQue.unshift({ type: 'miner_helper', memory: {
miner: creep.id
}});
creep.memory.helpersNeeded = creepsNeeded;
},
onSpawn: function()
{
var creep = this.creep;
creep.memory.isNearSource = false;
creep.memory.helpers = [];
var source = this.getOpenSource();
this.setSourceToMine(source);
creep.memory.onCreated = true;
},
action: function()
{
var creep = this.creep;
//Basically, each miner can empty a whole source by themselves. Also, since they're slow, we don't have them
//moving away from the source when it's empty, it'd regenerate before they got to another one.
//For this, we assign one miner to one source, and they stay with it
var source = Game.getObjectById(creep.memory.source);
if(source == null) {
var source = this.getOpenSource();
if(!source)
return;
this.setSourceToMine(source);
}
if(creep.pos.inRangeTo(source, 5))
creep.memory.isNearSource = true;
else
creep.memory.isNearSource = false;
if(Memory.sources[source.id] == undefined)
Memory.sources[source.id] = { id: source.id };
Memory.sources[source.id].miner = creep.id;
creep.moveTo(source);
creep.harvest(source);
}
};
module.exports = miner;