-
Notifications
You must be signed in to change notification settings - Fork 0
/
role.farvester4.js
116 lines (97 loc) · 5.58 KB
/
role.farvester4.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
var roleSupporter = require('role.supporter');
module.exports = {
// a function to run the logic for this role
run: function (creep) {
var hostiles = creep.room.find(FIND_HOSTILE_CREEPS);
if(hostiles.length > 0){
roleSupporter.run(creep);
}
else{
// if creep is bringing energy to a structure but has no energy left
if (creep.memory.working == true && creep.carry.energy == 0) {
// switch state
creep.memory.working = false;
creep.say('Farvesting');
}
// if creep is harvesting energy but is full
else if (creep.memory.working == false && creep.carry.energy == creep.carryCapacity) {
// switch state
creep.memory.working = true;
creep.say('Fill Spawn');
}
//Variables for the flags. When creating the Flags,
//they have to be named 'Harvest1' or 'Harvest2' etc.
var flagBoost4 = Game.flags.Boost4;
var flagHarvest4 = Game.flags.Harvest4;
var homeRoom = creep.memory.homeroom;
//This switches when you leave the room.
//It's how the creep knows if it's in his homeroom
var roomSwitch = undefined;
if(flagHarvest4 != undefined){
//If we're in the room we want to farm, switch the roomSwitch to TRUE
if (creep.room == flagHarvest4.room) {
roomSwitch = true;
}
//If we're still at home, we have not yet switched rooms, hence FALSE
else {
roomSwitch = false;
}
//If the creep is NOT in the home room
//...and he's WORKING (Meaning he's full of evergy)
//...and we're in another room then go home
if (creep.room != homeRoom && creep.memory.working == true && roomSwitch == true){
creep.moveTo(Game.flags.Boost4, {visualizePathStyle: {stroke: '#ffaa00', opacity: .6, lineStyle: 'dashed'}});
}
//if we're in the home room, go explore!!
if (creep.room != flagHarvest4.room && creep.memory.working == false && roomSwitch == false) creep.moveTo(flagHarvest4);
// if creep is supposed to transfer energy to a structure
if (creep.memory.working == true && roomSwitch == false) {
//Prefer containers over spawn, extension or tower
var containers = creep.pos.findClosestByPath(FIND_STRUCTURES, { filter: (s) => (s.structureType == STRUCTURE_CONTAINER && s.store.energy < s.storeCapacity) });
// if we found one
if (containers) {
console.log("TRANSFERRING TO CONTAINERS: " + containers);
// try to transfer energy, if it is not in range
if (creep.transfer(containers, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
// move towards it
creep.moveTo(containers, {visualizePathStyle: {stroke: '#ffaa00', opacity: .6, lineStyle: 'dashed'}});
}
}
else {
// find closest spawn, extension or tower which is not full
var structure = creep.pos.findClosestByPath(FIND_STRUCTURES, { filter: (s) => (s.structureType == STRUCTURE_SPAWN || s.structureType == STRUCTURE_EXTENSION || s.structureType == STRUCTURE_TOWER) && s.energy < s.energyCapacity });
// if we found one
if (structure) {
// try to transfer energy, if it is not in range
if (creep.transfer(structure, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
// move towards it
creep.moveTo(structure, {visualizePathStyle: {stroke: '#ffaa00', opacity: .6, lineStyle: 'dashed'}});
}
}
}
}
else {
//If there's energy dropped, pick it up!
var droppedEnergy = creep.pos.findClosestByPath(FIND_DROPPED_RESOURCES, {filter: (d) => {return (d.resourceType == RESOURCE_ENERGY)}});
if (droppedEnergy) {
if (creep.pickup(droppedEnergy) == ERR_NOT_IN_RANGE) {
creep.moveTo(droppedEnergy, {visualizePathStyle: {stroke: '#ffaa00', opacity: .6, lineStyle: 'dashed'}});
}
}
// if creep is supposed to harvest energy from source
else {
if (creep.memory.working == false && roomSwitch == true) {
// find closest source
var source = creep.pos.findClosestByPath(FIND_SOURCES_ACTIVE);
// try to harvest energy, if the source is not in range
if (creep.harvest(source) == ERR_NOT_IN_RANGE) {
// move towards the source
creep.moveTo(source, {visualizePathStyle: {stroke: '#ffaa00', opacity: .6, lineStyle: 'dashed'}});
}
}
}
}
}
}
}
};