-
Notifications
You must be signed in to change notification settings - Fork 0
/
role.workerNextRoom.js
139 lines (103 loc) · 3.83 KB
/
role.workerNextRoom.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
/*
* Module code goes here. Use 'module.exports' to export things:
* module.exports.thing = 'a thing';
*
* You can import it from another modules like this:
* var mod = require('role.worker');
* mod.thing == 'a thing'; // true
*/
var logger = require("screeps.logger");
logger = new logger("role.workerNextRoom");
logger.enabled = false;
var obj = function() {
}
var base = require('role.base');
obj.prototype = new base();
global.utils.extendFunction(obj, "init", function(name, roomManager) {
this.__init(name, roomManager);
this.workerCreepIds = [];
this.requiredCreeps = 1;
this.allowMining = true;
this.workInHomeRoom = false;
this.dangerZone = false;
}, "__");
global.utils.extendFunction(obj, "tickInit", function() {
logger.log("num", this.name, this.workerCreepIds.length, this.requiredCreeps, this.roomManager.roomName)
if (this.workerCreepIds.length < this.requiredCreeps) {
var minBodies = 0;
// if (this.roomManager.room.controller.level > 4) {
// minBodies = 3;
// }
//need some creeps
var memory = {
home: this.roomManager.roomName
}
var priority = 0;
if (this.numCreeps == 0)
priority = 10;
var req = global.utils.makeCreepRequest(this.name, "workerCreepIds", [MOVE, MOVE, CARRY, WORK], [MOVE, MOVE, CARRY, WORK], priority, memory, 3, minBodies)
global.empire.requestHelperCreep(this.roomManager.roomName, req);
return;
}
}, "__");
global.utils.extendFunction(obj, "tick", function() {
for(var i in this.workerCreepIds) {
var creep = Game.creeps[this.workerCreepIds[i]];
if (creep) {
this.runCreep(creep);
} else {
logger.log("mofo died",this.workerCreepIds[i])
delete this.workerCreepIds[i];
}
}
}, "__");
global.utils.extendFunction(obj, "tickEnd", function() {
}, "__");
obj.prototype.runCreep = function(creep) {
logger.log(creep);
if (!this.dangerZone || !creep.flee(6)) {
if ((this.allowMining && creep.memory.mining && !creep.full()) || creep.empty()) {
//creep.memory.mining = false;
if (creep.pos.roomName == this.roomManager.roomName) {
logger.log(creep.name, "in room")
if (!this.getEnergy(creep)) {
logger.log(creep.name, "can't getEnergy()", this.allowMining)
creep.memory.mining = true;
if (!this.allowMining || !creep.mineEnergyFromSource()) {
//try mining
logger.log(this.name, "can't find energy")
global.utils.moveCreep(creep, new RoomPosition(25, 25, this.roomManager.roomName));
}
}
} else {
global.utils.moveCreep(creep, new RoomPosition(25, 25, this.roomManager.roomName));
}
} else {
creep.memory.mining = false;
//do work
if (this.workInHomeRoom && creep.pos.roomName != this.homeRoomManager.roomName) {
//go to home room
global.utils.moveCreep(creep, new RoomPosition(25, 25, this.homeRoomManager.roomName));
} else {
this.doWork(creep);
}
}
}
}
obj.prototype.doWork = function(creep) {
if (!creep.doConstruction(true)) {
if (!creep.doRepair()) {
logger.log(this.name, 'nothing to do')
}
}
}
obj.prototype.getEnergy = function(creep) {
//logger.log(creep);
if (!creep.pickupEnergy()) {
if (!creep.getEnergyFromSourceContainers()) {
return false
}
}
return true;
}
module.exports = obj;