-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCreepLongDistanceMiner.js
126 lines (108 loc) · 4.65 KB
/
CreepLongDistanceMiner.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
var Constants = require('Constants');
var RoomHandler = require('RoomHandler');
var Cache = require('Cache');
var ACTIONS = {
HARVEST: 1,
DEPOSIT: 2
};
function creepLongDistanceMiner(creep, depositManager, defenseManager) {
this.cache = new Cache();
this.creep = creep;
this.depositManager = depositManager;
this.defenseManager = defenseManager;
}
creepLongDistanceMiner.prototype.init = function() {
/*if(!this.remember('targetRoom')) {
this.remember('targetRoom', this.resourceManager.getLonelyLongDistanceMiningRoom());
}*/
/*if(!this.remember('source')) {
var src = this.resourceManager.getAvailableResource();
if (src)
this.remember('source', src.id);
}*/
// hostiles detected
if (this.creep.room.name == this.remember('targetResourceRoom') && this.defenseManager.hostileCreeps.length > 0) {
console.log('call for backup' + this.creep.room + 'from' + this.remember('srcStorageRoom'))
this.creep.say('backup');
this.remember('previous-role', this.creep.memory.role);
var safeRoom = this.remember('srcStorageRoom');
this.defenseManager.callForScout(safeRoom);
this.remember('targetRoom', safeRoom);
this.remember('role', Constants.ROLE_CARRIER);
return;
}
if (!this.remember('srcStorageRoom'))
this.remember('srcStorageRoom', this.creep.room.name);
if(!this.remember('targetResourceRoom')) {
this.remember('targetResourceRoom', RoomHandler.get(Game.rooms[this.remember('srcStorageRoom')]).resourceManager.getLonelyLongDistanceMiningRoom());
}
if(!this.remember('srcRoom')) {
this.remember('srcRoom', this.creep.room.name);
}
if (!this.remember('last-action'))
this.remember('last-action', ACTIONS.HARVEST);
if(this.moveToNewRoom() == true) {
return;
}
//this.resource = this.resourceManager.getResourceById(this.remember('source'));
this.resource = this.creep.pos.findClosestByPath(FIND_SOURCES_ACTIVE);
this.act();
};
creepLongDistanceMiner.prototype.act = function() {
// creep carry is full
if (this.remember('last-action') == ACTIONS.HARVEST && this.creep.isFull())
this.remember('last-action', ACTIONS.DEPOSIT);
// creep finished depositing
if (this.remember('last-action') == ACTIONS.DEPOSIT && this.creep.isEmpty())
this.remember('last-action', ACTIONS.HARVEST);
// creep should harvest
if (this.remember('last-action') == ACTIONS.HARVEST) {
// found loot!
if (this.depositManager.droppedEnergy.length > 0) {
var droppedEnergy = this.creep.pos.findClosestByPath(this.depositManager.droppedEnergy);
if (this.creep.pickup(droppedEnergy) == ERR_NOT_IN_RANGE)
this.creep.moveToIfAble(droppedEnergy);
return;
}
if (this.creep.pos.roomName != this.remember('targetResourceRoom')) {
var exitDir = this.creep.room.findExitTo(this.remember('targetResourceRoom'));
var exit = this.creep.pos.findClosestByRange(exitDir);
this.creep.moveToIfAble(exit);
}
else if (this.creep.harvest(this.resource) == ERR_NOT_IN_RANGE)
this.creep.moveTo(this.resource);
}
// creep should deposit
else {
if (this.creep.pos.roomName != this.remember('srcStorageRoom')) {
var exitDir = this.creep.room.findExitTo(this.remember('srcStorageRoom'));
var exit = this.creep.pos.findClosestByRange(exitDir);
this.creep.moveToIfAble(exit);
}
else {
var storage = this.depositManager.room.storage;
// got storage
if (storage != undefined) {
if (this.creep.emptyCarry(storage) == ERR_NOT_IN_RANGE)
this.creep.moveToIfAble(storage);
}
// long distance mining before storage
else {
storage = this.creep.pos.findClosestByPath(this.depositManager.getAvailableContainersToDeposit());
if (storage != undefined) {
if (this.creep.emptyCarry(storage) == ERR_NOT_IN_RANGE)
this.creep.moveToIfAble(storage);
}
}
}
}
};
// private
function minerStructureFilter(structure) {
return Constants.minerSupplyStructures.indexOf(structure.structureType) != -1
&& (structure.energy < structure.energyCapacity || (structure.store && structure.store.energy < structure.storeCapacity));
}
module.exports = creepLongDistanceMiner;
//profiler setup
const profiler = require('profiler');
profiler.registerObject(creepLongDistanceMiner, 'CreepLongDistanceMiner');