-
Notifications
You must be signed in to change notification settings - Fork 0
/
prototype.creep.js
81 lines (71 loc) · 3.03 KB
/
prototype.creep.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 roleHarvester = require('role.harvester');
var roleUpgrader = require('role.upgrader');
var roleBuilder = require('role.builder');
var roleRepairer = require('role.repairer');
var roleDefender = require('role.defender');
var roleMiner = require('role.miner');
var roleLorry = require('role.lorry');
var roleMineralHarvester = require('role.mineralHarvester');
module.exports = function() {
Creep.prototype.run = function() {
var result = false;
if(!this.spawning){
if(this.memory.role == 'harvester') {
result = roleHarvester.run(this);
}else if(this.memory.role == 'upgrader') {
result = roleUpgrader.run(this);
}else if(this.memory.role == 'builder') {
result = roleBuilder.run(this);
}else if(this.memory.role == 'repairer'){
result = roleRepairer.run(this);
}else if(this.memory.role == 'defender'){
result = roleDefender.run(this);
}else if(this.memory.role == 'defender'){
result = roleDefender.run(this);
}else if(this.memory.role == 'miner'){
result = roleMiner.run(this);
}else if(this.memory.role == 'lorry'){
result = roleLorry.run(this);
}else if(this.memory.role == 'mineralHarvester'){
result = roleMineralHarvester.run(this);
}
//Fallback roles
if(!result){
result = roleHarvester.run(this);
}
if(!result){
result = roleBuilder.run(this);
}
if(!result){
result = roleUpgrader.run(this);
}
}
};
Creep.prototype.harvestEnergy = function() {
var sources = this.room.find(FIND_SOURCES, {filter: (s) => s.id != '5bbcae5b9099fc012e638d92'});
if(this.harvest(sources[0]) == ERR_NOT_IN_RANGE) {
this.moveTo(sources[0], {visualizePathStyle: {stroke: '#ffaa00'}});
}
};
Creep.prototype.getEnergy = function() {
let storages = this.room.find(FIND_STRUCTURES,
{filter: (s) => s.structureType == STRUCTURE_STORAGE && s.store.getUsedCapacity(RESOURCE_ENERGY) != 0})
if(storages.length > 0){
if(this.withdraw(storages[0], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
this.moveTo(storages[0], {visualizePathStyle: {stroke: '#ffaa00'}});
}
return;
}
let container = this.pos.findClosestByPath(FIND_STRUCTURES,
{filter: (s) => s.structureType == STRUCTURE_CONTAINER && s.store.getUsedCapacity(RESOURCE_ENERGY) != 0
&& s.isOpen()})
if(container){
if(this.withdraw(container, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
this.moveTo(container, {visualizePathStyle: {stroke: '#ffaa00'}});
}
return;
}
//If no storage or container with energy is available, the creep will harvest it itself.
this.harvestEnergy();
};
};