-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrole.elf.js
194 lines (167 loc) · 6.67 KB
/
role.elf.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
The elf is a house elf like in Harry Potter. It exists to do little mainteance task in satelite rooms
The elf has the following states:
-building : spending resources to build a consturction site
-idling: the creep has resources and is awaiting a job
-fetching : getting resources from the an appropriate source
-fixing : repairing a structure
The elf's memory consits of the following:
-job: the elf's current job
-work: the room the elf lives in
-flag: the elf's unique identifier
-target: the elf's target to repair until fully healed or out of resources
Notes:
-the elf should repair structures that are below 70% until they are 95% healed
- the elf prioritizes repairs over other jobs
- the elf should only spawn when there are construction sites
*/
var role_proto = require('prototype.role');
var roleElf = {
parts: [[WORK,CARRY,MOVE],
[WORK,WORK,CARRY,CARRY,MOVE,MOVE],
[WORK,WORK,CARRY,CARRY,CARRY,CARRY,MOVE,MOVE,MOVE]],
costs: [200,400,550],
create: function(spawn,params){
if (!!spawn.spawning){
// since it returns null otherwise
//skip creating this creep if the spawn is busy
return true;
}
memory={spawn:spawn.name,
home:spawn.room.name,
role: "elf",
job:"fetching",
work:Game.flags[params.join("_")].pos.roomName,
flag:params.join("_")};
var num= 1;
var name= memory.role+num;
if (Game.rooms[memory.work]=== undefined){
return false;
}
if (!Game.rooms[memory.work].find(FIND_CONSTRUCTION_SITES).length){
return false;
}
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 +" in "+ memory.home+ " for room " + memory.work);
spawn.createCreep(body, name,memory);
return true;
}
return false;
},
run: function(){
var creep= this.creep;
this.getOffEdge();
if (Memory[creep.memory.work]!= undefined && Memory[creep.memory.work].defense!= undefined && Memory[creep.memory.work].defense.defcon >=1){
// the elf is in danger, it should flee
this.flee();
return;
}
//send the elf to it's room
if (!this.gotoroom(creep.memory.work,safely=true)){
return 0;
}
//determine the creep's task
if((creep.memory.job!="fetching") && creep.carry.energy == 0) {
creep.memory.job="fetching";
creep.say('fetching');
}
if((creep.memory.job=="fetching") &&( (creep.carry.energy == creep.carryCapacity)||(creep.carry.energy >=100) )) {
creep.memory.job="idling";
creep.say('awaiting task');
}
// perform the creep's assigned task
if(creep.memory.job=="fetching"){
creep.say(":Scooter:");
this.fetch();
}
else if(creep.memory.job=="idling"){
creep.say("\u1F62A';");
this.idle();
}
else if(creep.memory.job=="fixing"){
creep.say(":hammer:");
this.fix();
}
else if(creep.memory.job=="building"){
creep.say(":hammer_pick:");
this.build();
}
},
fetch: function(){
//the elf should scavenge loose energy off the ground
var creep = this.creep;
var money = creep.pos.findClosestByRange(FIND_DROPPED_ENERGY);
var target = creep.pos.findClosestByRange(FIND_STRUCTURES, {
filter: (structure) => {
return ((structure.structureType == STRUCTURE_CONTAINER) &&
(structure.store[RESOURCE_ENERGY] > (0.01* structure.storeCapacity)));
}});
if(money){
result= creep.pickup(money);
if( result== ERR_NOT_IN_RANGE) {
creep.moveTo(money,{maxRooms:1});
}else if(result!=OK){
console.log("elf pickup error: " +result);
}
}
else if(target){
result= creep.withdraw(target, RESOURCE_ENERGY);
if( result== ERR_NOT_IN_RANGE) {
creep.moveTo(target,{maxRooms:1});
}else if(result!=OK){
console.log("elf withdraw error: " +result);
}
}
},
idle:function(){
//find a task for the elf
var creep = this.creep;
var decrepitBuilding = creep.pos.findClosestByRange(FIND_STRUCTURES,
{filter: (structure) => (structure.hitsMax*0.4 >=structure.hits)&&(structure.structureType!= STRUCTURE_CONTROLLER) } );
var constructionSite=creep.pos.findClosestByRange(FIND_CONSTRUCTION_SITES)
if (decrepitBuilding){
creep.memory.job="fixing";
creep.memory.target= decrepitBuilding.id;
creep.say("fixing!");
}
else if(constructionSite){
creep.memory.job="building";
creep.memory.target= constructionSite.id;
creep.say("building!");
}
},
fix:function(){
// the elf fixes the target until they run out of energy or the target is full health
var creep = this.creep;
var target = Game.getObjectById(creep.memory.target);
var fix_result=creep.repair(target);
if (fix_result==ERR_NOT_IN_RANGE){
creep.moveTo(target,{maxRooms:1});
}
if(target.hits==target.hitsMax){
//I'm assuming this is from the structure being full healed
creep.memory.job="idling";
// TODO check that it's fine that I leave the old target id, this is to save on cpu
}
},
build: function(){
//the elf builds the target until they run out of energy or the structure is complete
var creep = this.creep;
var target = Game.getObjectById(creep.memory.target);
var build_result=creep.build(target);
if(build_result == ERR_NOT_IN_RANGE) {
creep.moveTo(target,{maxRooms:1});
}
else if(build_result == ERR_INVALID_TARGET){
//will again assum this means the structure has been completed
creep.memory.job="idling";
}
}
};
module.exports = roleElf;