-
Notifications
You must be signed in to change notification settings - Fork 0
/
status.spawn.js
85 lines (82 loc) · 2.81 KB
/
status.spawn.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
/**
* A series of helper functions designed to quickly calculate the state changes for a spawn.
* Uses the spawn as a parameter and acts as static calculation functions.
*
* @type {Object}
*/
module.exports = {
spawnRatio: {
"totalPeace": 5,
"harvester": 2,
"builder": 3,
"totalWar": 10
},
/**
*
* @param {StructureSpawn} spawn
*/
spawnNext: function (spawn) {
let openSources = 0;
for (let each of spawn.room.find(FIND_SOURCES)) {
for (let position in spawn.room.lookForAtArea(LOOK_TERRAIN, each.pos.y + 1, each.pos.x - 1, each.pos.y - 1, each.pos.x + 1, true)) {
if (position.terrain !== "wall") {
openSources += 1;
}
}
}
let roomScale = (spawn.room.controller.level * openSources);
if (!Memory.war) {
if (roomScale > spawn.room.find(FIND_MY_CREEPS).length) {
if (spawn.room.find(FIND_MY_CREEPS,
{
filter: function (creep) {
return creep.memory.role === "harvester"
}
}).length < Math.ceil(roomScale * (this.spawnRatio.harvester / this.spawnRatio.totalPeace))) {
return "harvester";
} else if (spawn.room.find(FIND_MY_CREEPS,
{
filter: function (creep) {
return creep.memory.role === "builder"
}
}).length < Math.floor(roomScale * (this.spawnRatio.builder / this.spawnRatio.totalPeace))) {
return "builder"
}
else {
return false;
}
}
}
},
/**
*
*
* @param {StructureSpawn} spawn
*/
spawnTime: function(spawn) {
let energy = spawn.energyCapacity;
let time = 0;
while (energy > 50) {
if (energy > 100) {energy -= 100; time += 3;}//one WORK part
if (energy > 50) {energy -= 50; time += 3;}//one CARRY part
if (energy > 50) {energy -= 50; time += 3;}//one MOVE part
}
return time;
},
/**
*
* @param {StructureSpawn} spawn The spawn to run calculations on.
*/
shouldSpawn: function (spawn) {
let spawnable = this.spawnNext(spawn);
if (spawnable && !(spawn.spawning) && spawn.energy === spawn.energyCapacity) {
for (let each of spawn.room.find(FIND_MY_CREEPS)) {
if (each.pos.findClosestByPath(FIND_MY_SPAWNS) === spawn && this.spawnTime(spawn) > (each.ticksToLive - 10)) {
return "renew";
}
}
return spawnable;
}
return "renew";
}
};