-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreep.roleSpawnKeeper.js
More file actions
126 lines (125 loc) · 5.1 KB
/
Creep.roleSpawnKeeper.js
File metadata and controls
126 lines (125 loc) · 5.1 KB
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 CreepRoleSpawnKeeper = function() {
var creep = this;
var carryMultiplier = 0.1;
var startTime = 19051976;
var max = 100000;
var rate = 0.05;
var storageAmount = Math.floor(rate * (Game.time - startTime) + 3000);
if(storageAmount > max) {
storageAmount = max;
}
//y = (x-t)*r + 2000
if(this.memory.collecting) {
if(this.carry.energy === this.carryCapacity) {
this.memory.collecting = false;
this.say('Delivering');
}
} else {
if(this.carry.energy === 0) {
this.memory.collecting = true;
this.say('Collecting');
}
}
var storage = this.room.storage;
var droppedEnergy = _.filter(this.room.memory.found.droppedEnergy, function(energy) {
if(energy.amount > creep.carryCapacity * carryMultiplier) {
return true;
}
});
var repositories = this.getRepositories([STRUCTURE_CONTAINER, STRUCTURE_STORAGE, STRUCTURE_LINK]);
this.transfer(repositories[0], RESOURCE_ENERGY);
if(this.memory.collecting) {
var target;
if(droppedEnergy.length > 0) {
target = droppedEnergy[0];
this.room.visual.circle(target.pos, {fill: 'transparent', radius: 0.55, stroke: 'red'});
if(this.pickup(droppedEnergy[0]) === ERR_NOT_IN_RANGE) {
this.moveTo(target);
}
} else {
var result;
var structures = this.room.memory.found.structures;
var containers = _.filter(structures, function(structure) {
if(structure.structureType === STRUCTURE_CONTAINER) {
if(structure.store.energy > 0) {
return true;
}
}
});
var targets, linkClosestToStorage;
if(storage) {
var links = this.room.memory.found.links;
if(links.length > 0) {
linkClosestToStorage = this.room.storage.pos.findClosestByRange(links);
}
targets = containers.concat([linkClosestToStorage]);
} else {
targets = containers;
}
if(storage && storage.store[RESOURCE_ENERGY] > storageAmount) {
target = storage;
var amount = Math.floor(storage.store[RESOURCE_ENERGY] - storageAmount - rate * 50);
if(amount > this.carryCapacity - this.carry.energy) {
amount = this.carryCapacity - this.carry.energy;
}
if(amount > 0) {
this.room.visual.circle(target.pos, {fill: 'transparent', radius: 0.55, stroke: 'red'});
result = this.withdraw(target, RESOURCE_ENERGY, amount);
if(result === ERR_NOT_IN_RANGE) {
this.moveTo(target);
}
} else {
this.memory.collecting = false;
}
} else if(targets.length > 0) {
targets = _.filter(targets, function(structure) {
if(structure) {
var energyPercent;
if(structure.structureType === STRUCTURE_LINK) {
energyPercent = structure.energy / structure.energyCapacity;
} else if(structure.structureType === STRUCTURE_CONTAINER) {
energyPercent = structure.store.energy / structure.storeCapacity;
}
if(energyPercent > 0.5 || structure.energy > creep.carryCapacity) {
return true;
} else {
return false;
}
}
});
if(targets.length > 0) {
target = this.pos.findClosestByRange(targets);
this.room.visual.circle(target.pos, {fill: 'transparent', radius: 0.55, stroke: 'red'});
result = this.withdraw(target, RESOURCE_ENERGY);
if(result === ERR_NOT_IN_RANGE) {
this.moveTo(target);
}
}
} else {
this.memory.collecting = false;
}
}
} else {
var depositStructures = [];
if(droppedEnergy.length > 0) {
if(storage) {
depositStructures = [STRUCTURE_STORAGE];
} else {
depositStructures = [STRUCTURE_TOWER, STRUCTURE_SPAWN, STRUCTURE_EXTENSION];
}
} else if(storage) {
if(storage.store[RESOURCE_ENERGY] > storageAmount) {
depositStructures = [STRUCTURE_TOWER, STRUCTURE_SPAWN, STRUCTURE_EXTENSION];
} else {
depositStructures = [STRUCTURE_STORAGE];
}
} else {
depositStructures = [STRUCTURE_TOWER, STRUCTURE_SPAWN, STRUCTURE_EXTENSION];
}
this.deposit({
creepDepositing: false,
structures: depositStructures
});
}
};
module.exports = CreepRoleSpawnKeeper;