-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCreepBuilder.js
81 lines (70 loc) · 2.78 KB
/
CreepBuilder.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
var Constants = require('Constants');
var Cache = require('Cache');
var ACTIONS = {
BUILD: 1,
WITHDRAW: 2
};
function creepBuilder(creep, constructionsManager, depositManager, resourceManager) {
this.creep = creep;
this.constructionsManager = constructionsManager;
this.depositManager = depositManager;
this.resourceManager = resourceManager;
};
creepBuilder.prototype.init = function() {
if(!this.remember('srcRoom')) {
this.remember('srcRoom', this.creep.room.name);
}
if (!this.remember('last-action'))
this.remember('last-action', ACTIONS.WITHDRAW);
if(this.moveToNewRoom() == true) {
return;
}
this.forceControllerUpgrade = this.remember('force-controller-upgrade');
//if(this.randomMovement() == false) {
this.act();
//}
};
creepBuilder.prototype.act = function() {
// creep carry is full
if (this.remember('last-action') == ACTIONS.WITHDRAW && this.creep.carry.energy == this.creep.carryCapacity)
this.remember('last-action', ACTIONS.BUILD);
// creep ran out of energy
if (this.remember('last-action') == ACTIONS.BUILD && this.creep.carry.energy == 0)
this.remember('last-action', ACTIONS.WITHDRAW);
if (!this.remember('targetRoom')) {
var claimPath = this.remember('claimPath');
if (claimPath != undefined) {
var index = claimPath.indexOf(this.creep.room.name);
if (index == claimPath.length - 1)
this.forget('claimPath');
else {
this.remember('targetRoom', claimPath[index + 1]);
return;
}
}
}
// creep should withdraw energy
if (this.remember('last-action') == ACTIONS.WITHDRAW) {
var source = this.depositManager.storage;
if (!source)
source = this.creep.pos.findClosestByPath(this.depositManager.getAvailableDepositsToWithdraw());
if (source && source.transfer(this.creep, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE)
this.creep.moveToIfAble(source);
else if (!source || source == null || source == undefined){
source = this.creep.pos.findClosestByPath(this.resourceManager.getSources());
if (this.creep.harvest(source) == ERR_NOT_IN_RANGE)
this.creep.moveToIfAble(source);
}
}
// creep should build
else {
if (this.forceControllerUpgrade || !this.constructionsManager.constructStructure(this)) {
if (this.creep.upgradeController(this.constructionsManager.room.controller) == ERR_NOT_IN_RANGE)
this.creep.moveToIfAble(this.constructionsManager.room.controller);
}
}
};
module.exports = creepBuilder;
//profiler setup
const profiler = require('profiler');
profiler.registerObject(creepBuilder, 'CreepBuilder');