-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrole.peasant.js
148 lines (130 loc) · 5.28 KB
/
role.peasant.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
/*
The peasant is to be used for low room controller levels
the peasant has the following states:
-harvesting
-spendging
-upgrading
*/
var role_proto = require('prototype.role');
var rolePeasant = {
parts: [[WORK,CARRY,MOVE],
[WORK,WORK,CARRY,CARRY,MOVE,MOVE]],
create: function(spawn) {
if (!!spawn.spawning){
// since it returns null otherwise
//skip this if the spawn is busy
return true;
}
memory={spawn:spawn.name,
home:spawn.room.name,
role: "peasant",
job:"harvesting"};
var num= 1;
var name= memory.role+num;
var num_peasants = _.sum(Game.creeps, (c) => c.memory.role == 'peasant' && c.memory.home ==memory.home);
if ((spawn.room.energyCapacityAvailable>=400)&&(num_peasants>0) ){
var body =this.parts[1];
}else{
var body =this.parts[0];
}
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;
//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 != "harvesting")&&(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();
}else if(creep.memory.job == "upgrading"){
this.upgrade();
}
},
spend: function(){
var creep= this.creep;
var construction_site = creep.pos.findClosestByRange(FIND_CONSTRUCTION_SITES);
var extension = creep.pos.findClosestByRange(FIND_STRUCTURES,{
filter: (structure) => {return ((structure.structureType== STRUCTURE_EXTENSION)&&(structure.energy <50)) ;} } );
var towers = creep.room.find(FIND_STRUCTURES, {
filter: (structure) => {
return (structure.structureType == STRUCTURE_TOWER) && structure.energy < 0.65*structure.energyCapacity;
}
});
// first check if the spawn is full?
if(Game.spawns[creep.memory.spawn].energy < 300){
if(creep.transfer(Game.spawns[creep.memory.spawn], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
creep.moveTo(Game.spawns[creep.memory.spawn]);
}
}else if(extension){// then check the extensions
if(creep.transfer(extension, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
creep.moveTo(extension);
}
}
else if( (creep.memory.num %2 ==0 )&&(construction_site) ){
//half will be builders
if(creep.build(construction_site) == ERR_NOT_IN_RANGE) {
creep.moveTo(construction_site);
}
}else if( (creep.memory.num %2 ==1 )&&(towers.length)) {
if(creep.transfer(towers[0], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
creep.moveTo(towers[0]);
}
}
else{
// if nothing to build and spawn is full, upgrade the controller
// changing the job stops them from walking back halfway to fill the spawn lol
creep.memory.job = "upgrading"
if(creep.upgradeController(creep.room.controller) == ERR_NOT_IN_RANGE) {
creep.moveTo(creep.room.controller);
}
}
},
harvest: function(){
// if there is a harvester blocking access to the source, the peasant should withdraw from the source
var creep= this.creep;
var sources = creep.room.find(FIND_SOURCES);
var target = sources[creep.memory.num%2];
// if there is a non-empty container nearby, draw from that rather than trying to mine
var container = target.pos.findInRange(FIND_STRUCTURES,1, {filter : (s) =>{return (s.structureType== STRUCTURE_CONTAINER)&& (s.store[RESOURCE_ENERGY]>100) ;}} );
if (container.length){
if(creep.withdraw(container[0], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
creep.moveTo(container[0]);
}
}
else{
result= creep.harvest(target);
if (result== ERR_NOT_IN_RANGE){
creep.moveTo(target);
}
else if (result!= OK){
if (Memory.verbose){console.log("peasant harvesting error:" +result);}
}
}
},
upgrade: function(){
var creep= this.creep;
if(creep.upgradeController(creep.room.controller) == ERR_NOT_IN_RANGE) {
creep.moveTo(creep.room.controller);
}
}
};
module.exports = rolePeasant;