-
Notifications
You must be signed in to change notification settings - Fork 0
/
tasks.gathering.js
151 lines (134 loc) · 4.44 KB
/
tasks.gathering.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
/*
* 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('tasks.gathering');
* mod.thing == 'a thing'; // true
*/
var logger = require("screeps.logger");
logger = new logger("tasks.gathering");
logger.enabled = false;
var Task = require("tasks.base");
class mining extends Task{
constructor() {
super();
this.name = Task.MINING;
//this.displayThisTask = true;
}
assignCreep(creep) {
let amtAssigned = 1;
this.assignments[creep.id] = amtAssigned;
}
preformTask(creep) {
let source = Game.getObjectById(this.data.sourceId);
logger.log(creep, "mining!", source)
if (source) {
let moveTarget = source;
let sourceProc = this.kernel.getProcess("source-" + source.id);
let stopMining = false;
if (creep.pos.isNearTo(source) && creep.memory.role == "miner" && sourceProc && sourceProc.cont) {
moveTarget = sourceProc.cont;
//logger.log(creep, _.sum(sourceProc.cont.store), sourceProc.cont.storeCapacity)
if (_.sum(sourceProc.cont.store) == sourceProc.cont.storeCapacity) {
stopMining = true;
}
}
let range = moveTarget == source ? 1 : 0;
if (!creep.pos.inRangeTo(moveTarget, range)) {
global.creepActions.moveTo(creep, moveTarget, range)
}
if (!stopMining && creep.pos.isNearTo(source)) {
let ret = creep.harvest(source);
}
} else {
logger.log(creep, "no source, moving to pos")
//no visiblity on source, move to data.pos
let target = new RoomPosition(this.pos.x, this.pos.y, this.pos.roomName);
global.creepActions.moveTo(creep, target);
}
//logger.log(creep, creep.carryCapacity != 0, _.sum(creep.carry) == creep.carryCapacity, _.sum(creep.carry), creep.carryCapacity)
return creep.carryCapacity != 0 && _.sum(creep.carry) == creep.carryCapacity;
}
}
class pickup extends Task{
constructor() {
super();
this.name = Task.PICKUP;
this.displayThisTask=true;
}
assignCreep(creep) {
let amtAssigned = creep.carryCapacity - _.sum(creep.carry);
this.assignments[creep.id] = amtAssigned;
}
preformTask(creep) {
let pile = Game.getObjectById(this.data.pileId);
if (!pile) {
logger.log(creep.name, "My pile doesn't exist anymore! considering job done.", this.data.pileId)
return true;
}
if(global.creepActions.moveTo(creep, pile)) {
let ret = creep.pickup(pile);
}
return _.sum(creep.carry) == creep.carryCapacity || pile.amount == 0;
}
}
class pickupController extends pickup{
constructor() {
super();
this.name = Task.PICKUPATCONTROLLER;
this.displayThisTask=true;
}
}
class pickupFromContainer extends Task{
constructor() {
super();
this.name = Task.PICKUPENERGYCONT;
this.displayThisTask = true;
}
assignCreep(creep) {
let amtAssigned = creep.carryCapacity - _.sum(creep.carry);
this.assignments[creep.id] = amtAssigned;
}
preformTask(creep) {
let cont = Game.getObjectById(this.data.targetId);
if (!cont) {
return true;
}
if(global.creepActions.moveTo(creep, cont)) {
let ret = creep.withdraw(cont, RESOURCE_ENERGY);
logger.log(creep, "grabed", cont, ret)
}
//logger.log(creep, "pickup", )
return creep.carryCapacity == _.sum(creep.carry) || cont.store.energy == 0;
}
}
class pickupFromSpawnCont extends pickupFromContainer {
constructor() {
super();
this.name = Task.PICKUPENERGYSPAWN;
this.displayThisTask = false;
}
}
class pickupFromControllerCont extends pickupFromContainer {
constructor() {
super();
this.name = Task.PICKUPENERGYCONTROLLER;
this.displayThisTask = false;
}
}
let all = [
mining,
pickup,
pickupController,
pickupFromContainer,
pickupFromSpawnCont,
pickupFromControllerCont,
];
let map = {};
for(let i in all) {
let one = all[i];
let inst = new one();
map[inst.name] = one;
}
module.exports = map;