-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathspawner.js
167 lines (159 loc) · 4.56 KB
/
spawner.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
let objects = require('./objects');
let rooms = require('./rooms');
let shorter_path = require('./shorter_path');
/**
* @type boolean
*/
module.exports.DEBUG = true;
/**
* @param arguments string[]
*/
module.exports.log = function()
{
if (this.DEBUG !== false) console.log.apply(console, arguments);
};
/**
* @param main object
* @param room Room
* @param room_role string @example 'spawn_harvester'
* @param [opts] object spawn options
* @returns boolean
*/
module.exports.roleCreep = function(main, room, room_role, opts)
{
if (!opts) opts = {};
if (room.controller.level === 1) {
opts.accept_little = true;
}
if (!rooms.has(room, room_role)) {
this.log('wish to spawn a ', room_role);
let role = rooms.get(room, room_role, 'role');
let spawn = opts.spawn ? opts.spawn : rooms.get(room, 'spawn');
if (role && spawn) {
if (!opts.role) opts.role = role;
if (!opts.spawn) opts.spawn = spawn;
let creep = main.creep_of[role].spawn(opts);
if (creep) {
creep.memory.room = room.name;
creep.memory.room_role = room_role;
if (rooms.get(room, room_role, 'source')) creep.memory.single_source = true;
if (rooms.get(room, room_role, 'target')) creep.memory.single_target = true;
rooms.setCreep(room, room_role, creep);
return true;
}
}
}
return false;
};
/**
* Spawn needed creeps in the room
*
* @param main object
* @param room Room
* @returns boolean
*/
module.exports.room = function(main, room)
{
let spawn = rooms.get(room, 'spawn');
if (spawn && !spawn.spawning) {
// spawn the first needed creep
if (!main.count[room.name]) {
this.simpleCreep(main, room, { accept_little: true, count: 1, even_no_target: true, role: 'carrier' });
}
// spawn other creeps
else if (
this.spawnHarvester (main, room)
|| this.spawnCarrier(main, room)
|| this.roleCreep (main, room, 'controller_upgrader', { accept_little: true })
|| this.roleCreep (main, room, 'controller_harvester', { accept_little: true })
|| this.roleCreep (main, room, 'controller_carrier', { accept_little: true })
|| this.simpleCreep (main, room, { accept_little: true, count: 3, role: 'carrier' })
|| (objects.energyFull(room) && this.simpleCreep(main, room, { count: 1, role: 'builder' }))
|| (objects.energyFull(room) && this.simpleCreep(main, room, { count: 1, role: 'repairer' }))
) {
return true;
}
// count foreign-working flags
for (let flag in main.flags) if (main.flags.hasOwnProperty(flag)) {
flag = main.flags[flag];
if ((flag.name.substr(0, 3) === 'to-')) {
if (!flag.memory.harvester) {
this.simpleCreep(main, room, { even_no_target: true, flag: flag, role: 'trans_harvester' });
}
if (!flag.memory.carrier) {
// TODO
}
}
}
}
};
/**
* @param main object
* @param room Room
* @param [opts] object spawn options
* @returns boolean
*/
module.exports.simpleCreep = function(main, room, opts)
{
if (!opts) opts = {};
if (
(opts.count !== undefined)
&& main.count[room.name]
&& main.count[room.name][opts.role]
&& (main.count[room.name][opts.role] >= opts.count)
) {
return false;
}
let spawn = opts.spawn ? opts.spawn : rooms.get(room, 'spawn');
this.log(opts.role, 'targets ?');
let creepjs = main.creep_of[opts.role];
if (opts.even_no_target || creepjs.targets(spawn).length) {
this.log('> wish to spawn a ', opts.role);
if (!opts.spawn) opts.spawn = spawn;
let creep = creepjs.spawn(opts);
if (creep) {
return true;
}
}
return false;
};
/**
* @param main object
* @param room Room
* @returns boolean
*/
module.exports.spawnCarrier = function(main, room)
{
return (main.count[room.name].carrier < 2)
? this.roleCreep(main, room, 'spawn_carrier', { accept_little: true })
: false;
};
/**
* @param main object
* @param room Room
* @returns boolean
*/
module.exports.spawnHarvester = function(main, room)
{
let opts = { accept_little: true };
// rooms has two STRUCTURE_LINK ? harvester must have a CARRY body part to store energy before transfer into link
let links;
if (
!rooms.has(room, 'spawn_harvester')
&& (
(links = room.find(FIND_MY_STRUCTURES, { filter: structure => structure.structureType === STRUCTURE_LINK })).length
>= 2
)
) {
let role = rooms.get(room, 'spawn_harvester', 'role');
if (role) {
opts.body_parts = main.creep_of[role].body_parts;
opts.body_parts.push(CARRY);
}
}
let creep = this.roleCreep(main, room, 'spawn_harvester', opts);
if (creep && opts.body_parts) {
creep.memory.link = shorter_path.sort(objects.get(creep, 'spawn_source'), links).shift().id;
}
return creep;
};