-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrole.distancepeasant.js
117 lines (96 loc) · 3.76 KB
/
role.distancepeasant.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
/*
The distance peasant is to be used for lower room controller levels to distance mine, there should be an emphasis on carry capacity.
The distance peasant has two states:
-harvesting: the peasants is going out to harvest resources
-spending: the peasant returns home to spend their resources
The distance peasant's memory is as follows:
-work: room name of source
-source: flag of the source
-flag: the name of the flag which spawned the creep so that duplicates aren't spawned , one flag per creep
The distance peasant's create function only returns true if the spawner is busy or a claimer is made.
TODO:
-make the distance peasant drop off at a storage if there is one? maybe a container?
*/
var role_proto = require('prototype.role');
var roleDistancePeasant = {
parts: [[WORK,CARRY,MOVE,MOVE],
[WORK,CARRY,CARRY,MOVE,MOVE,MOVE]],
costs: [200,350],
create: function(spawn,info) {
if (!!spawn.spawning){
// since it returns null otherwise
//skip this if the spawn is busy
return true;
}
var source=info[2];
memory={spawn:spawn.name,
home:spawn.room.name,
role: "distancepeasant",
job:"harvesting",
source:source,
work:Game.flags[source].pos.roomName,
flag:info.join("_")};
var num= 1;
var name= memory.role+num;
var body = this.parts[ this.costs.indexOf(_.max(this.costs.filter((c) => {return (c<spawn.room.energyCapacityAvailable);})))];
while(spawn.canCreateCreep(body,name)=== ERR_NAME_EXISTS){
num+=1;
name= memory.role+num;
}
memory.num=num;
if(spawn.canCreateCreep(body,name) == OK){
console.log("building a "+memory.role +" named " +name + " for room " + memory.home);
spawn.createCreep(body, name,memory);
return true;
}
return false;
},
/** @param {Creep} creep **/
run: function() {
var creep= this.creep;
this.getOffEdge();
//control what job the peasant does
if((creep.memory.job == "harvesting")&&(this.creep.carry.energy == this.creep.carryCapacity) ){
creep.memory.job = "spending";
creep.say("Spending time!");
}
else if((creep.memory.job == "spending")&&(this.creep.carry.energy == 0)){
creep.memory.job = "harvesting";
creep.say("Harvest time!");
}
if(creep.memory.job == "harvesting"){
this.harvest();
}
else if(creep.memory.job== "spending"){
this.spend();
}
},
harvest: function(){
// if there is a harvester blocking access to the source, the peasant should withdraw from the source
var creep= this.creep;
if (!this.gotoroom(creep.memory.work)){
return 0;
}
if( creep.pos.isEqualTo(Game.flags[creep.memory.source])){
var source = creep.pos.findClosestByRange(FIND_SOURCES);
result= creep.harvest(source);
if (result!= OK){
if (Memory.verbose){console.log("harvesting error:" +result);}
}
}
else{
creep.moveTo(Game.flags[creep.memory.source]);
}
},
spend:function(){
// the distance peasant only upgrades because that's easier
var creep= this.creep;
if (!this.gotoroom(creep.memory.home)){
return 0;
}
if(creep.upgradeController(creep.room.controller) == ERR_NOT_IN_RANGE) {
creep.moveTo(creep.room.controller);
}
}
};
module.exports= roleDistancePeasant;