-
Notifications
You must be signed in to change notification settings - Fork 41
/
Mission.ts
744 lines (657 loc) · 28.1 KB
/
Mission.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
import {Operation} from "../operations/Operation";
import {SpawnGroup} from "../SpawnGroup";
import {HeadCountOptions, TransportAnalysis} from "../../interfaces";
import {DESTINATION_REACHED, MAX_HARVEST_DISTANCE, MAX_HARVEST_PATH} from "../../config/constants";
import {helper} from "../../helpers/helper";
import {Agent} from "./Agent";
import {empire} from "../../helpers/loopHelper";
import {ROOMTYPE_SOURCEKEEPER, WorldMap, ROOMTYPE_ALLEY} from "../WorldMap";
import {Traveler} from "../Traveler";
import {RoomHelper} from "../RoomHelper";
export abstract class Mission {
flag: Flag;
memory: any;
spawnGroup: SpawnGroup;
sources: Source[];
room: Room;
name: string;
operation: Operation;
allowSpawn: boolean;
hasVision: boolean;
waypoints: Flag[];
partnerPairing: {[role: string]: Agent[]} = {};
distanceToSpawn: number;
constructor(operation: Operation, name: string, allowSpawn: boolean = true) {
this.name = name;
this.flag = operation.flag;
this.room = operation.room;
this.spawnGroup = operation.spawnGroup;
this.sources = operation.sources;
if (!operation.memory[name]) operation.memory[name] = {};
this.memory = operation.memory[name];
this.allowSpawn = allowSpawn;
this.operation = operation;
if (this.room) this.hasVision = true;
// initialize memory to be used by this mission
if (!this.memory.hc) this.memory.hc = {};
if (operation.waypoints && operation.waypoints.length > 0) {
this.waypoints = operation.waypoints;
}
}
/**
* Init Phase - Used to initialize values for the following phases
*/
public abstract initMission();
/**
* RoleCall Phase - Used to find creeps and spawn any extra that are needed
*/
public abstract roleCall();
/**
* MissionAction Phase - Primary phase for world-changing functions like creep.harvest(), tower.attack(), etc.
*/
public abstract missionActions();
/**
* Finish Phase - Do any remaining work that needs to happen after the other phases
*/
public abstract finalizeMission();
/**
* Invalidate Cache Phase - Do any housekeeping that might need to be done
*/
public abstract invalidateMissionCache();
public setBoost(activateBoost: boolean) {
let oldValue = this.memory.activateBoost;
this.memory.activateBoost = activateBoost;
return `changing boost activation for ${this.name} in ${this.operation.name} from ${oldValue} to ${activateBoost}`;
}
public setMax(max: number) {
let oldValue = this.memory.max;
this.memory.max = max;
return `changing max creeps for ${this.name} in ${this.operation.name} from ${oldValue} to ${max}`;
}
public setSpawnGroup(spawnGroup: SpawnGroup) {
this.spawnGroup = spawnGroup;
}
public invalidateSpawnDistance() {
if (this.memory.distanceToSpawn) {
console.log(`SPAWN: resetting distance for ${this.name} in ${this.operation.name}`);
this.memory.distanceToSpawn = undefined;
}
}
/**
* General purpose function for spawning creeps
* @param roleName - Used to find creeps belonging to this role, examples: miner, energyCart
* @param getBody - function that returns the body to be used if a new creep needs to be spawned
* @param getMax - function that returns how many creeps are currently desired, pass 0 to halt spawning
* @param options - Optional parameters like prespawn interval, whether to disable attack notifications, etc.
* @returns {Agent[]}
*/
protected headCount(roleName: string, getBody: () => string[], getMax: () => number,
options: HeadCountOptions = {}): Agent[] {
let agentArray = [];
if (!this.memory.hc[roleName]) this.memory.hc[roleName] = this.findOrphans(roleName);
let creepNames = this.memory.hc[roleName] as string[];
let count = 0;
for (let i = 0; i < creepNames.length; i++) {
let creepName = creepNames[i];
let creep = Game.creeps[creepName];
if (creep) {
let agent = new Agent(creep, this);
let prepared = this.prepAgent(agent, options);
if (prepared) agentArray.push(agent);
let ticksNeeded = 0;
if (options.prespawn !== undefined) {
ticksNeeded += creep.body.length * 3;
ticksNeeded += options.prespawn;
}
if (!creep.ticksToLive || creep.ticksToLive > ticksNeeded) { count++; }
}
else {
creepNames.splice(i, 1);
delete Memory.creeps[creepName];
i--;
}
}
let spawnGroup = this.spawnGroup;
if (options.altSpawnGroup) {
spawnGroup = options.altSpawnGroup;
}
let allowSpawn = spawnGroup.isAvailable && this.allowSpawn && (this.hasVision || options.blindSpawn);
if (allowSpawn && count < getMax()) {
let creepName = `${this.operation.name}_${roleName}_${Math.floor(Math.random() * 100)}`;
let outcome = spawnGroup.spawn(getBody(), creepName, options.memory, options.reservation);
if (_.isString(outcome)) { creepNames.push(creepName); }
}
return agentArray;
}
protected spawnSharedAgent(roleName: string, getBody: () => string[]): Agent {
let spawnMemory = this.spawnGroup.spawns[0].memory;
if (!spawnMemory.communityRoles) spawnMemory.communityRoles = {};
let employerName = this.operation.name + this.name;
let creep: Creep;
if (spawnMemory.communityRoles[roleName]) {
let creepName = spawnMemory.communityRoles[roleName];
creep = Game.creeps[creepName];
if (creep && Game.map.getRoomLinearDistance(this.spawnGroup.room.name, creep.room.name) <= 3) {
if (creep.memory.employer === employerName || (!creep.memory.lastTickEmployed || Game.time - creep.memory.lastTickEmployed > 1)) {
creep.memory.employer = employerName;
creep.memory.lastTickEmployed = Game.time;
return new Agent(creep, this);
}
}
else {
delete Memory.creeps[creepName];
delete spawnMemory.communityRoles[roleName];
}
}
if (!creep && this.spawnGroup.isAvailable) {
let creepName = "community_" + roleName;
while (Game.creeps[creepName]) {
creepName = "community_" + roleName + "_" + Math.floor(Math.random() * 100);
}
let outcome = this.spawnGroup.spawn(getBody(), creepName, undefined, undefined);
if (_.isString(outcome)) {
spawnMemory.communityRoles[roleName] = outcome;
}
else if (Game.time % 10 !== 0 && outcome !== ERR_NOT_ENOUGH_RESOURCES) {
console.log(`error spawning community ${roleName} in ${this.operation.name} outcome: ${outcome}`);
}
}
}
/**
* Returns creep body array with desired number of parts in this order: WORK → CARRY → MOVE
* @param workCount
* @param carryCount
* @param movecount
* @returns {string[]}
*/
protected workerBody(workCount: number, carryCount: number, movecount: number): string[] {
let body: string [] = [];
for (let i = 0; i < workCount; i++) {
body.push(WORK);
}
for (let i = 0; i < carryCount; i++) {
body.push(CARRY);
}
for (let i = 0; i < movecount; i++) {
body.push(MOVE);
}
return body;
}
protected configBody(config: {[partType: string]: number}): string[] {
let body: string[] = [];
for (let partType in config) {
let amount = config[partType];
for (let i = 0; i < amount; i++) {
body.push(partType);
}
}
return body;
}
/**
* Returns creep body array with the desired ratio of parts, governed by how much spawn energy is possible
* @param workRatio
* @param carryRatio
* @param moveRatio
* @param spawnFraction - proportion of spawn energy to be used up to 50 body parts, .5 would use half, 1 would use all
* @param limit - set a limit to the number of units (useful if you know the exact limit, like with miners)
* @returns {string[]}
*/
protected bodyRatio(workRatio: number, carryRatio: number, moveRatio: number, spawnFraction: number, limit?: number): string[] {
let sum = workRatio * 100 + carryRatio * 50 + moveRatio * 50;
let partsPerUnit = workRatio + carryRatio + moveRatio;
if (!limit) limit = Math.floor(50 / partsPerUnit);
let maxUnits = Math.min(Math.floor((this.spawnGroup.maxSpawnEnergy * spawnFraction) / sum), limit);
return this.workerBody(workRatio * maxUnits, carryRatio * maxUnits, moveRatio * maxUnits);
}
/**
* General purpose checking for creep load
* @param creep
* @returns {boolean}
*/
protected hasLoad(creep: Creep): boolean {
if (creep.memory.hasLoad && _.sum(creep.carry) === 0) {
creep.memory.hasLoad = false;
}
else if (!creep.memory.hasLoad && _.sum(creep.carry) === creep.carryCapacity) {
creep.memory.hasLoad = true;
}
return creep.memory.hasLoad;
}
// deprecated
/**
* Used to determine cart count/size based on transport distance and the bandwidth needed
* @param distance - distance (or average distance) from point A to point B
* @param load - how many resource units need to be transported per tick (example: 10 for an energy source)
* @returns {{body: string[], cartsNeeded: number}}
*/
protected cacheTransportAnalysis(distance: number, load: number): TransportAnalysis {
if (!this.memory.transportAnalysis || load !== this.memory.transportAnalysis.load
|| distance !== this.memory.transportAnalysis.distance) {
this.memory.transportAnalysis = Mission.analyzeTransport(distance, load, this.spawnGroup.maxSpawnEnergy)
}
return this.memory.transportAnalysis;
}
// deprecated
static analyzeTransport(distance: number, load: number, maxSpawnEnergy: number): TransportAnalysis {
// cargo units are just 2 CARRY, 1 MOVE, which has a capacity of 100 and costs 150
let maxUnitsPossible = Math.min(Math.floor(maxSpawnEnergy /
((BODYPART_COST[CARRY] * 2) + BODYPART_COST[MOVE])), 16);
let bandwidthNeeded = distance * load * 2.1;
let cargoUnitsNeeded = Math.ceil(bandwidthNeeded / (CARRY_CAPACITY * 2));
let cartsNeeded = Math.ceil(cargoUnitsNeeded / maxUnitsPossible);
let cargoUnitsPerCart = Math.floor(cargoUnitsNeeded / cartsNeeded);
return {
load: load,
distance: distance,
cartsNeeded: cartsNeeded,
carryCount: cargoUnitsPerCart * 2,
moveCount: cargoUnitsPerCart,
};
}
// deprecated
static loadFromSource(source: Source): number {
return Math.max(source.energyCapacity, SOURCE_ENERGY_CAPACITY) / ENERGY_REGEN_TIME;
}
protected getFlagSet(identifier: string, max = 10): Flag[] {
let flags = [];
for (let i = 0; i < max; i++) {
let flag = Game.flags[this.operation.name + identifier + i];
if (flag) {
flags.push(flag);
}
}
return flags;
}
protected flagLook(lookConstant: string, identifier: string, max = 10) {
let objects = [];
let flags = this.getFlagSet(identifier, max);
for (let flag of flags) {
if (flag.room) {
let object = _.head(flag.pos.lookFor(lookConstant));
if (object) {
objects.push(object);
}
else {
flag.remove();
}
}
}
return objects;
}
// deprecated, use similar function on TransportGuru
getStorage(pos: RoomPosition): StructureStorage {
if (this.memory.tempStorageId) {
let storage = Game.getObjectById<StructureStorage>(this.memory.tempStorageId);
if (storage) {
return storage;
}
else {
console.log("ATTN: Clearing temporary storage id due to not finding object in", this.operation.name);
this.memory.tempStorageId = undefined;
}
}
// invalidated periodically
if (!this.memory.nextStorageCheck || Game.time >= this.memory.nextStorageCheck) {
let bestStorages = RoomHelper.findClosest({pos: pos}, empire.network.storages,
{linearDistanceLimit: MAX_HARVEST_DISTANCE });
bestStorages = _.filter(bestStorages, value => value.distance < MAX_HARVEST_PATH);
let resultPosition;
if (bestStorages.length > 0) {
let result = bestStorages[0].destination;
resultPosition = result.pos;
this.memory.storageId = result.id;
this.memory.nextStorageCheck = Game.time + helper.randomInterval(10000); // Around 10 hours
} else {
this.memory.nextStorageCheck = Game.time + 100; // Around 6 minutes
}
console.log(`MISSION: finding storage for ${this.operation.name}, result: ${resultPosition}`);
}
if (this.memory.storageId) {
let storage = Game.getObjectById<StructureStorage>(this.memory.storageId);
if (storage && storage.room.controller.level >= 4) {
return storage;
} else {
this.memory.storageId = undefined;
this.memory.nextStorageCheck = Game.time;
return this.getStorage(pos);
}
}
}
private findOrphans(roleName: string) {
let creepNames = [];
for (let creepName in Game.creeps) {
if (creepName.indexOf(this.operation.name + "_" + roleName + "_") > -1) {
creepNames.push(creepName);
}
}
return creepNames;
}
protected recycleAgent(agent: Agent) {
let spawn = this.spawnGroup.spawns[0];
if (agent.pos.isNearTo(spawn)) {
spawn.recycleCreep(agent.creep);
}
else {
agent.travelTo(spawn);
}
}
private prepAgent(agent: Agent, options: HeadCountOptions) {
if (!agent.memory.prep) {
if (options.disableNotify) {
this.disableNotify(agent)
}
let boosted = agent.seekBoost(agent.memory.boosts, agent.memory.allowUnboosted);
if (!boosted) return false;
if (agent.creep.spawning) return false;
if (!options.skipMoveToRoom && (agent.pos.roomName !== this.flag.pos.roomName || agent.pos.isNearExit(1))) {
agent.avoidSK(this.flag);
return;
}
agent.memory.prep = true;
}
return true;
}
protected findPartnerships(agents: Agent[], role: string) {
for (let agent of agents) {
if (!agent.memory.partner) {
if (!this.partnerPairing[role]) this.partnerPairing[role] = [];
this.partnerPairing[role].push(agent);
for (let otherRole in this.partnerPairing) {
if (role === otherRole) continue;
let otherCreeps = this.partnerPairing[otherRole];
let closestCreep;
let smallestAgeDifference = Number.MAX_VALUE;
for (let otherCreep of otherCreeps) {
let ageDifference = Math.abs(agent.ticksToLive - otherCreep.ticksToLive);
if (ageDifference < smallestAgeDifference) {
smallestAgeDifference = ageDifference;
closestCreep = otherCreep;
}
}
if (closestCreep) {
closestCreep.memory.partner = agent.name;
agent.memory.partner = closestCreep.name;
}
}
}
}
}
protected getPartner(agent: Agent, possibilities: Agent[]): Agent {
for (let possibility of possibilities) {
if (possibility.name === agent.memory.partner) {
return possibility;
}
}
}
protected findDistanceToSpawn(destination: RoomPosition): number {
if (!this.memory.distanceToSpawn) {
let roomLinearDistance = Game.map.getRoomLinearDistance(this.spawnGroup.pos.roomName, destination.roomName);
if (roomLinearDistance <= OBSERVER_RANGE) {
let ret = empire.traveler.findTravelPath(this.spawnGroup, {pos: destination});
if (ret.incomplete) {
console.log(`SPAWN: error finding distance in ${this.operation.name} for object at ${destination}`);
console.log(`fallback to linearRoomDistance`);
this.memory.distanceToSpawn = roomLinearDistance * 50 + 25;
} else {
this.memory.distanceToSpawn = ret.path.length;
}
}
else {
console.log(`SPAWN: likely portal travel detected in ${this.operation.name}, setting distance to 200`);
this.memory.distanceToSpawn = 200;
}
}
return this.memory.distanceToSpawn;
}
protected disableNotify(creep: Creep | Agent) {
if (creep instanceof Agent) {
creep = creep.creep;
}
if (!creep.memory.notifyDisabled) {
creep.notifyWhenAttacked(false);
creep.memory.notifyDisabled = true;
}
}
protected pavePath(start: {pos: RoomPosition}, finish: {pos: RoomPosition}, rangeAllowance: number, ignoreLimit = false): number {
if (Game.time - this.memory.paveTick < 1000) return;
if (Game.map.getRoomLinearDistance(start.pos.roomName, finish.pos.roomName) > 2) {
console.log(`PAVER: path too long: ${start.pos.roomName} to ${finish.pos.roomName}`);
return;
}
let path = this.findPavedPath(start.pos, finish.pos, rangeAllowance);
if (!path) {
console.log(`incomplete pavePath, please investigate (${this.operation.name}), start: ${start.pos}, finish: ${finish.pos}, mission: ${this.name}`);
return;
}
let newConstructionPos = this.examinePavedPath(path);
if (newConstructionPos && (ignoreLimit || Object.keys(Game.constructionSites).length < 60)) {
if (!Game.cache.placedRoad) {
Game.cache.placedRoad = true;
console.log(`PAVER: placed road ${newConstructionPos} in ${this.operation.name}`);
newConstructionPos.createConstructionSite(STRUCTURE_ROAD);
}
}
else {
this.memory.paveTick = Game.time;
if (_.last(path).inRangeTo(finish.pos, rangeAllowance)) {
return path.length;
}
}
}
// This path making will only be valid for an origin/destination with a roomdistance less than 3
protected findPavedPath(start: RoomPosition, finish: RoomPosition, rangeAllowance: number): RoomPosition[] {
const ROAD_COST = 3;
const PLAIN_COST = 4;
const SWAMP_COST = 5;
const AVOID_COST = 7;
let maxDistance = Game.map.getRoomLinearDistance(start.roomName, finish.roomName);
let ret = PathFinder.search(start, [{pos: finish, range: rangeAllowance}], {
plainCost: PLAIN_COST,
swampCost: SWAMP_COST,
maxOps: 12000,
roomCallback: (roomName: string): CostMatrix | boolean => {
// disqualify rooms that involve a circuitous path
if (Game.map.getRoomLinearDistance(start.roomName, roomName) > maxDistance) {
return false;
}
// disqualify enemy rooms
if (Traveler.checkOccupied(roomName)) {
return false;
}
let room = Game.rooms[roomName];
if (!room) {
let roomType = WorldMap.roomTypeFromName(roomName);
if (roomType === ROOMTYPE_ALLEY) {
let matrix = new PathFinder.CostMatrix();
return helper.blockOffExits(matrix, AVOID_COST, roomName);
}
return;
}
let matrix = new PathFinder.CostMatrix();
Traveler.addStructuresToMatrix(room, matrix, ROAD_COST);
// avoid controller
if (room.controller) {
helper.blockOffPosition(matrix, room.controller, 3, AVOID_COST);
}
// avoid container/link adjacency
let sources = room.find<Source>(FIND_SOURCES);
for (let source of sources) {
let structure = source.findMemoStructure<Structure>(STRUCTURE_CONTAINER, 1);
if (!structure) {
structure = source.findMemoStructure<Structure>(STRUCTURE_LINK, 1);
}
if (structure) {
helper.blockOffPosition(matrix, structure, 1, AVOID_COST);
}
}
// add construction sites too
let constructionSites = room.find<ConstructionSite>(FIND_MY_CONSTRUCTION_SITES);
for (let site of constructionSites) {
if (site.structureType === STRUCTURE_ROAD) {
matrix.set(site.pos.x, site.pos.y, ROAD_COST);
}
else {
matrix.set(site.pos.x, site.pos.y, 0xff);
}
}
// avoid going too close to lairs
for (let lair of room.findStructures<StructureKeeperLair>(STRUCTURE_KEEPER_LAIR)) {
helper.blockOffPosition(matrix, lair, 1, AVOID_COST);
}
return matrix;
},
});
if (!ret.incomplete) {
return ret.path;
}
}
private examinePavedPath(path: RoomPosition[]) {
let repairIds = [];
let hitsToRepair = 0;
for (let i = 0; i < path.length; i++) {
let position = path[i];
if (!Game.rooms[position.roomName]) return;
if (position.isNearExit(0)) continue;
let road = position.lookForStructure(STRUCTURE_ROAD);
if (road) {
repairIds.push(road.id);
hitsToRepair += road.hitsMax - road.hits;
// TODO: calculate how much "a whole lot" should be based on paver repair rate
const A_WHOLE_LOT = 1000000;
if (!this.memory.roadRepairIds && (hitsToRepair > A_WHOLE_LOT || road.hits < road.hitsMax * .20)) {
console.log(`PAVER: I'm being summoned in ${this.operation.name}`);
this.memory.roadRepairIds = repairIds;
}
continue;
}
let construction = position.lookFor<ConstructionSite>(LOOK_CONSTRUCTION_SITES)[0];
if (construction) continue;
return position;
}
}
protected paverActions(paver: Agent) {
// paver, healthyself
if (paver.hits < paver.hitsMax) {
if (paver.room.hostiles.length === 0 && !paver.pos.isNearExit(0)) {
let tower = paver.pos.findClosestByRange(paver.room.findStructures<StructureTower>(STRUCTURE_TOWER));
if (tower) {
tower.heal(paver.creep);
return;
}
}
let healersInRoom = _.filter(paver.room.find<Creep>(FIND_MY_CREEPS), c => c.getActiveBodyparts(HEAL));
if (healersInRoom.length > 0) {
paver.idleOffRoad();
return;
}
if (paver.getActiveBodyparts(WORK) === 0) {
paver.travelTo(this.spawnGroup);
return;
}
}
let hasLoad = paver.hasLoad();
if (!hasLoad) {
paver.procureEnergy(this.findRoadToRepair());
return;
}
let road = this.findRoadToRepair();
if (!road) {
console.log(`this is ${this.operation.name} paver, checking out with ${paver.ticksToLive} ticks to live`);
delete Memory.creeps[paver.name];
paver.idleOffRoad(this.room.controller);
return;
}
let paving = false;
if (paver.pos.inRangeTo(road, 3) && !paver.pos.isNearExit(0)) {
paving = paver.repair(road) === OK;
let hitsLeftToRepair = road.hitsMax - road.hits;
if (hitsLeftToRepair > 10000) {
paver.yieldRoad(road, true);
}
else if (hitsLeftToRepair > 1500) {
paver.yieldRoad(road, false)
}
}
else {
paver.travelTo(road, {range: 0});
}
if (!paving) {
road = paver.pos.lookForStructure(STRUCTURE_ROAD) as StructureRoad;
if (road && road.hits < road.hitsMax) paver.repair(road);
}
paver.stealNearby("creep");
}
private findRoadToRepair(): StructureRoad {
if (!this.memory.roadRepairIds) return;
let road = Game.getObjectById<StructureRoad>(this.memory.roadRepairIds[0]);
if (road && road.hits < road.hitsMax) {
return road;
}
else {
this.memory.roadRepairIds.shift();
if (this.memory.roadRepairIds.length > 0) {
return this.findRoadToRepair();
}
else {
this.memory.roadRepairIds = undefined;
}
}
}
protected spawnPaver(): Agent {
if (this.room.controller && this.room.controller.level === 1) return;
let paverBody = () => { return this.bodyRatio(1, 3, 2, 1, 5); };
return this.spawnSharedAgent("paver", paverBody);
}
protected registerPrespawn(agent: Agent) {
if (!agent.memory.registered) {
agent.memory.registered = true;
const SANITY_CHECK = CREEP_LIFE_TIME / 2;
this.memory.prespawn = Math.max(CREEP_LIFE_TIME - agent.creep.ticksToLive, SANITY_CHECK);
}
}
protected medicActions(defender: Agent) {
let hurtCreep = this.findHurtCreep(defender);
if (!hurtCreep) {
defender.idleNear(this.flag, 12);
return;
}
// move to creep
let range = defender.pos.getRangeTo(hurtCreep);
if (range > 1) {
defender.travelTo(hurtCreep, {movingTarget: true});
}
else {
defender.yieldRoad(hurtCreep, true);
}
if (range === 1) {
defender.heal(hurtCreep);
}
else if (range <= 3) {
defender.rangedHeal(hurtCreep);
}
}
private findHurtCreep(defender: Agent) {
if (!this.room) return;
if (defender.memory.healId) {
let creep = Game.getObjectById(defender.memory.healId) as Creep;
if (creep && creep.room.name === defender.room.name && creep.hits < creep.hitsMax) {
return creep;
}
else {
defender.memory.healId = undefined;
return this.findHurtCreep(defender);
}
}
else if (!defender.memory.healCheck || Game.time - defender.memory.healCheck > 25) {
defender.memory.healCheck = Game.time;
let hurtCreep = _(this.room.find<Creep>(FIND_MY_CREEPS))
.filter((c: Creep) => c.hits < c.hitsMax && c.ticksToLive > 100)
.sortBy((c: Creep) => -c.partCount(WORK))
.head();
if (hurtCreep) {
defender.memory.healId = hurtCreep.id;
return hurtCreep;
}
}
}
}