-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrole.claimer.js
77 lines (62 loc) · 2.22 KB
/
role.claimer.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
/*
The Claimer will be created with a room in mind to claim
HOWTO
place a blue flag in the room you want to claim, it will be removed once the room is claimed
The Claimer's memory :
-work: the room name of the room I want to claim
-flag : the creeps unique identifier
The claimer's create function only returns true if the spawner is busy or a claimer is made.
*/
var role_proto = require('prototype.role');
var roleClaimer = {
parts: [[MOVE,CLAIM]],
// TODO make a helper function for finding the costs
costs: [650],
create: function(spawn,params) {
if (!!spawn.spawning){
// since it returns null otherwise
//skip this if the spawn is busy
return true;
}
var flag = Game.flags[params.join("_")];
//if I own the room, remove the flag
if (flag.room != undefined){
if(flag.room.controller.my){
flag.remove();
return false;
}
}
memory={spawn:spawn.name,
home:spawn.room.name,
role: "claimer",
work:Game.flags[params.join("_")].pos.roomName,
flag:params.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;
},
run:function(){
var creep= this.creep;
this.getOffEdge();
var creep= this.creep;
if (!this.gotoroom(creep.memory.work)){
return 0;
}
var controller = creep.room.controller;
if(creep.claimController(controller) ==ERR_NOT_IN_RANGE){
creep.moveTo(controller, {maxRooms:1});
}
}
};
module.exports=roleClaimer;