-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrole.builder.js
More file actions
110 lines (103 loc) · 4.35 KB
/
Copy pathrole.builder.js
File metadata and controls
110 lines (103 loc) · 4.35 KB
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
const BodyPartSelector=require('utilities.bodyParts');
const Util=require('utilities');
module.exports = {
run: function(creep) {
const STATE_PICKING_UP=0;
const STATE_HARVESTING=1;
const STATE_BUILDING=2;
const STATE_UPGRADING=3;
const state=creep.memory.state;
switch(state){
case STATE_PICKING_UP:
let ret=true;
let id=creep.getId(CREEP_ID_PICKUP);
let changed=false;
if(id===undefined || Game.getObjectById(id)===null){
changed=true;
creep.setId(CREEP_ID_PICKUP,undefined);
ret=creep.setBuilding(CREEP_ID_PICKUP,
(building) => building.structureType === STRUCTURE_STORAGE
&& building.store[RESOURCE_ENERGY] > 0,
(a,b)=>{return b.store[RESOURCE_ENERGY]-a.store[RESOURCE_ENERGY]});
if(!ret) {
ret = creep.setBuilding(CREEP_ID_PICKUP,
(building) => building.structureType === STRUCTURE_CONTAINER
&& building.store[RESOURCE_ENERGY] > 0,
(a,b)=>{return b.store[RESOURCE_ENERGY]-a.store[RESOURCE_ENERGY]});
}
}
if(ret){
if(changed){
id=creep.getId(CREEP_ID_PICKUP);
}
creep.withdrawResource(CREEP_ID_PICKUP,RESOURCE_ENERGY);
if (creep.carry.energy === creep.carryCapacity) {
creep.memory.state = STATE_BUILDING;
creep.setId(CREEP_ID_PICKUP,undefined);
}
}
else{
creep.memory.state = STATE_HARVESTING;
creep.setId(CREEP_ID_PICKUP,undefined);
}
break;
case STATE_HARVESTING:
let sId=creep.getId(CREEP_ID_SOURCE);
if(sId===undefined) {
const roomCreeps=creep.room.getMyCreeps();
const sources=creep.room.getSources();
creep.setSource(roomCreeps, sources);
}
creep.gather();
if(creep.carry.energy===creep.carryCapacity){
creep.memory.state=STATE_BUILDING;
creep.setId(CREEP_ID_SOURCE,undefined);
}
break;
case STATE_BUILDING:
let hasTarget=true;
let cId=creep.getId(CREEP_ID_SITE);
if(cId===undefined){
hasTarget=creep.setConstructionSite(CREEP_ID_SITE);
if(hasTarget)
cId=creep.getId(CREEP_ID_SITE);
}
if(hasTarget)
creep.construct(CREEP_ID_SITE);
else{
creep.memory.state=STATE_UPGRADING;
creep.setId(CREEP_ID_SITE,undefined);
}
if(creep.carry.energy===0){
creep.memory.state=STATE_PICKING_UP;
creep.setId(CREEP_ID_SITE,undefined);
}
break;
case STATE_UPGRADING:
let canUpgrade=true;
if(creep.memory.controllerX===undefined || creep.memory.controllerY===undefined)
canUpgrade=creep.setControllerMem();
if(canUpgrade) {
creep.upgrade();
if (creep.carry.energy === 0) {
creep.memory.state = STATE_PICKING_UP;
creep.memory.controllerX=undefined;
creep.memory.controllerY=undefined;
}
}
else{
creep.memory.state = STATE_PICKING_UP;
creep.memory.controllerX=undefined;
creep.memory.controllerY=undefined;
}
break;
default:
creep.memory.state=STATE_PICKING_UP;
break;
}
},
spawn: function(spawn,energy,room){
spawn.spawnCreep(BodyPartSelector.workerParts(energy),Util.selectCreepName('builder'),
{memory: {role: 'builder',state: 0,home: room.name}});
}
};