-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstructure.spawn.ts
199 lines (194 loc) · 5.14 KB
/
structure.spawn.ts
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import './defines';
enum CreepWorker {
HARVESTER = 'harvester',
UPGRADER = 'upgrader',
BUILDER = 'builder',
REPAIRER = 'repairer',
MINER = 'miner',
HAULER = 'hauler'
}
let self = {
run(spawn: StructureSpawn) {
let creeps: Creep[] = spawn.room.find(FIND_MY_CREEPS);
let harvesters = 0;
let upgraders = 0;
let builders = 0;
let miners = 0;
for(let creepName in creeps) {
let role = creeps[creepName].memory.role;
if(role == 'harvester') {
harvesters++;
} else if(role == 'upgrader') {
upgraders++;
} else if(role == 'builder') {
builders++;
} else if(role == 'miner') {
miners++;
}
}
if(harvesters < 3) {
if(harvesters == 0) {
self.createWorker(spawn, CreepWorker.HARVESTER, true);
} else {
self.createWorker(spawn, CreepWorker.HARVESTER);
}
}
if(upgraders < 4 && harvesters > 0) {
self.createUpgrader(spawn);
}
if(builders < 2 && spawn.room.controller.level >= 2) {
self.createWorker(spawn, CreepWorker.BUILDER);
}
if(spawn.spawning) {
let spawningCreep = Game.creeps[spawn.spawning.name];
spawn.room.visual.text(
'🛠️' + spawningCreep.memory.role,
spawn.pos.x + 1,
spawn.pos.y,
{align: 'left', opacity: 0.8});
}
},
updateSources: function(spawn: StructureSpawn) {
let room = spawn.room;
let terrain = room.getTerrain();
let sources = room.find(FIND_SOURCES);
if(room.memory.sources == undefined) {
room.memory.sources = {};
let roomSources = room.memory.sources;
for(let i = 0; i < sources.length; i++) {
let source = sources[i];
roomSources[source.id] = {};
for(let i = -1; i <= 1; i++) {
for(let j = -1; j <= 1; j++) {
if(i == 0 && j == 0) {
continue;
}
let x = source.pos.x + i;
let y = source.pos.y + j;
let position = room.getPositionAt(x, y);
let pathTo = Room.serializePath(spawn.pos.findPathTo(position));
let pathBack = Room.serializePath(position.findPathTo(spawn.pos));
if(terrain.get(x, y) != TERRAIN_MASK_WALL) {
roomSources[source.id]['' + x + ',' + y] = {
x: x,
y: y,
pathTo: pathTo,
pathBack: pathBack
};
}
}
}
}
}
},
createStructures(spawn: StructureSpawn) {
let spawnX = spawn.pos.x;
let spawnY = spawn.pos.y;
for(let i = -1; i <= 1; i++) {
for(let j = -1; j <= 1; j++) {
if(j != i && (j + i == -1 || j + i == 1)) {
let pos = spawn.room.getPositionAt(i + spawnX, j + spawnY);
pos.createConstructionSite(STRUCTURE_EXTENSION);
}
}
}
for(let source in spawn.room.memory.sources) {
for(let spot in spawn.room.memory.sources[source]) {
let path = Room.deserializePath(spawn.room.memory.sources[source][spot].pathTo);
for(let posName in path) {
spawn.room.createConstructionSite(path[posName].x, path[posName].y, STRUCTURE_ROAD);
}
}
}
},
createWorker(spawn: StructureSpawn, role: CreepWorker, withCurrentResources: boolean = false) {
if(withCurrentResources) {
var capacity = spawn.room.energyAvailable;
} else {
var capacity = spawn.room.energyCapacityAvailable;
}
let parts = [];
let cost = 0;
for(let i = 100; i < capacity * 4 / 6; i += 100) {
parts.push(WORK);
cost += 100;
if(!(i + 50 > capacity * 3 / 5)) {
parts.push(MOVE);
cost += 50;
i += 50;
}
}
capacity -= cost;
for(let i = 50; i < capacity; i += 50) {
parts.push(CARRY);
if(i % 100 == 0) {
parts.push(MOVE);
i += 50;
}
}
let newName = role + Game.time;
if(spawn.spawnCreep(parts, newName, {memory: {role: role}}) == 0) {
console.log('Spawning new ' + role + ': ' + newName);
}
},
createUpgrader(spawn: StructureSpawn) {
let capacity = spawn.room.energyCapacityAvailable;
let role = CreepWorker.UPGRADER;
let parts = [];
let cost = 0;
for(let i = 100; i <= capacity * 3 / 6; i += 100) {
parts.push(WORK);
cost += 100;
if(!(i + 50 > capacity * 3 / 5)) {
parts.push(MOVE);
cost += 50;
i += 50;
}
}
capacity -= cost;
for(let i = 50; i <= capacity; i += 50) {
parts.push(CARRY);
if(i % 100 == 0) {
parts.push(MOVE);
i += 50;
}
}
let newName = role + Game.time;
if(spawn.spawnCreep(parts, newName, {memory: {role: role}}) == 0) {
console.log('Spawning new ' + role + ': ' + newName);
}
},
createMiner(spawn: StructureSpawn) {
let capacity = spawn.room.energyCapacityAvailable;
let role = CreepWorker.MINER;
let parts = [];
for(let i = 100; i <= capacity; i += 100) {
parts.push(WORK);
if(i % 100 != 0) {
parts.push(MOVE);
i += 50;
}
}
let newName = role + Game.time;
if(spawn.spawnCreep(parts, newName, {memory: {role: role}}) == 0) {
console.log('Spawning new ' + role + ': ' + newName);
}
},
createHauler(spawn: StructureSpawn) {
let capacity = spawn.room.energyCapacityAvailable;
let role = CreepWorker.HAULER;
let parts = [];
for(let i = 50; i <= capacity; i += 50) {
parts.push(CARRY);
if(i % 50 == 0) {
parts.push(MOVE);
i += 50;
}
}
let newName = role + Game.time;
if(spawn.spawnCreep(parts, newName, {memory: {role: role}}) == 0) {
console.log('Spawning new ' + role + ': ' + newName);
}
}
};
export default self;