-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprototype.Spawn.js
More file actions
133 lines (123 loc) · 3.53 KB
/
prototype.Spawn.js
File metadata and controls
133 lines (123 loc) · 3.53 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
127
128
129
130
131
var roleMiner = require('role.miner')
var roleWorker = require('role.worker')
var roleTransport = require('role.transport')
var roleScout = require('role.scout')
var roleAttacker = require('role.attacker')
global.globalMiner = new roleMiner()
global.globalWorker = new roleWorker()
global.globalTransport = new roleTransport()
global.globalScout = new roleScout()
global.globalAttacker = new roleAttacker()
var MIN_ATTACKERS = 0
global.CREEP_TYPES = {
miner: {
body : [MOVE,WORK],
maxMultiplier : 5,
memory : { role: "miner" },
object : roleMiner
},
worker: {
body : [MOVE,WORK,CARRY],
maxMultiplier : 9999,
memory : { role: "worker" },
object : roleWorker
},
transport: {
body : [MOVE,CARRY,CARRY],
maxMultiplier : 9999,
memory : { role: "transport" },
object : roleTransport
},
scout: {
body : [MOVE,CLAIM],
maxMultiplier : 2,
memory : { role: "scout" },
object : roleScout
},
attacker: {
body : [MOVE,ATTACK],
maxMultiplier : 2,
memory : { role: "attacker" },
object : roleAttacker
},
}
StructureSpawn.prototype.spawnCreeps = function() {
let result = OK
try{
result = this.spawn()
} catch(err){
console.log(err)
}
return result
}
StructureSpawn.prototype.spawnInfo = function(){
var infostr;
if(this.spawning) {
var spawningCreep = Game.creeps[this.spawning.name];
infostr = '🛠️' + spawningCreep.memory.role;
}
else{
infostr = "🔋" + this.room.energyAvailable + "/" + this.room.energyCapacityAvailable
}
this.room.visual.text(
infostr,
this.pos.x - 1,
this.pos.y,
{align: 'right', opacity: 0.8});
}
StructureSpawn.prototype.creepCost = function(body){
let cost = 0
for(let part of body){
cost += BODYPART_COST[part]
}
return cost
}
StructureSpawn.prototype.spawn = function() {
let creepType = ""
if(this.spawning){
return OK
}
_.forEach( CREEP_TYPES, (value, key, map) => {
let genCreep = new value["object"]()
if(genCreep.find().length + Memory.spawnQueue.filter(queue => queue == key) < genCreep.wanted()){
Memory.spawnQueue.push(key)
}
})
if(Memory.spawnQueue.length > 0){
creepType = Memory.spawnQueue.shift()
let energyAvailable = this.room.energyAvailable
let energyCapacity = this.room.energyCapacityAvailable
let bodyUnit = CREEP_TYPES[creepType]["body"]
let bodyUnitCost = this.creepCost(bodyUnit)
let bodyMax = CREEP_TYPES[creepType]["maxMultiplier"]
let bodyMultiplier = Math.min(Math.floor(energyCapacity/bodyUnitCost), bodyMax)
let memory = CREEP_TYPES[creepType]["memory"]
let name = creepType + Game.time
if(globalTransport.find().length == 0 || globalMiner.find().length == 0){
energyCapacity = 300
Memory.spawnQueue = _.reduce(Memory.spawnQueue, function(acc, val){
if(val == "transport" || val == "miner"){
acc.push(val)
}
return acc
}, [])
}
let body = []
for(let i=1; i <= bodyMultiplier; i++){
body = body.concat(bodyUnit)
if(this.creepCost(body) + bodyUnitCost > energyCapacity){
break
}
}
if(body.length == 0){
return OK
}
if(this.creepCost(body) > energyAvailable) {
Memory.spawnQueue.unshift(creepType)
return OK
}
console.log("spawning " + name)
return this.spawnCreep(body, name, { memory: memory })
}
return OK
}