Skip to content

Commit

Permalink
autospawn init
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxim-Mazurok committed May 30, 2020
1 parent cead4d3 commit 5aff552
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 1 deletion.
3 changes: 3 additions & 0 deletions build/helpers.creep.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ class HelpersCreep {
}
return result;
}
static bodyCost(body) {
return body.reduce((cost, part) => cost + BODYPART_COST[part], 0);
}
}
exports.HelpersCreep = HelpersCreep;
exports.TRANSFER_PATH = { stroke: '#ffffff' };
Expand Down
3 changes: 3 additions & 0 deletions build/helpers.find.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,8 @@ class HelpersFind {
static findLinksWithEnergy(room) {
return HelpersFind.findStructuresByType(room, STRUCTURE_LINK).filter((link) => link.energy > 0);
}
static findAllMyCreepsInRoom(room) {
return Object.values(Game.creeps).filter((creep) => creep.my && creep.room.name === room.name);
}
}
exports.HelpersFind = HelpersFind;
7 changes: 7 additions & 0 deletions build/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ const roleBuilder = require('./builder');
const roleEnergizer = require('./energizer');
const roleUptownHarvester = require('./uptown.harvester');
const roomsConfig = [
{
roomName: 'E48N17',
autoSpawn: {
enabled: true,
maxCreeps: 3,
},
},
{
roomName: 'E48N17',
claim: [
Expand Down
22 changes: 22 additions & 0 deletions build/rooms.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,28 @@ class Rooms {
});
}
}
if (config.autoSpawn &&
config.autoSpawn.enabled &&
helpers_1.HelpersFind.findAllMyCreepsInRoom(room).length <
config.autoSpawn.maxCreeps) {
const totalEnergy = helpers_1.HelpersFind.getRoomTotalEnergyForSpawningAvailable(room);
const bodyParts = [];
const bodyPartsOrder = [MOVE, WORK, CARRY];
let lastBodyPartIndex = bodyParts.length - 1;
while (helpers_1.HelpersCreep.bodyCost(bodyParts) < totalEnergy) {
lastBodyPartIndex =
lastBodyPartIndex === bodyParts.length - 1
? 0
: lastBodyPartIndex + 1;
if (helpers_1.HelpersCreep.bodyCost([
...bodyParts,
bodyPartsOrder[lastBodyPartIndex],
]) <= totalEnergy) {
bodyParts.push(bodyPartsOrder[lastBodyPartIndex]);
}
}
spawn.spawnCreep(bodyPartsOrder, Math.random().toString());
}
}
}
exports.Rooms = Rooms;
4 changes: 4 additions & 0 deletions src/helpers.creep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ export class HelpersCreep {
}
return result;
}

static bodyCost(body: BodyPartConstant[]) {
return body.reduce((cost, part) => cost + BODYPART_COST[part], 0);
}
}

export const TRANSFER_PATH = {stroke: '#ffffff'};
Expand Down
6 changes: 6 additions & 0 deletions src/helpers.find.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,10 @@ export class HelpersFind {
STRUCTURE_LINK
).filter((link: StructureLink) => link.energy > 0);
}

static findAllMyCreepsInRoom(room: Room): Creep[] {
return Object.values(Game.creeps).filter(
(creep: Creep) => creep.my && creep.room.name === room.name
);
}
}
7 changes: 7 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ const roleEnergizer = require('./energizer');
const roleUptownHarvester = require('./uptown.harvester');

const roomsConfig: RoomsConfig = [
{
roomName: 'E48N17',
autoSpawn: {
enabled: true,
maxCreeps: 3,
},
},
{
roomName: 'E48N17',
claim: [
Expand Down
31 changes: 30 additions & 1 deletion src/rooms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {Towers} from './towers';
import {Market} from './market';
import {filter} from 'lodash';
import {CreepRole} from './enums';
import {HelpersFind} from './helpers';
import {HelpersFind, HelpersCreep} from './helpers';
import {RoomsConfig, RoomConfig} from './ts';

export class Rooms {
Expand Down Expand Up @@ -75,5 +75,34 @@ export class Rooms {
});
}
}

if (
config.autoSpawn &&
config.autoSpawn.enabled &&
HelpersFind.findAllMyCreepsInRoom(room).length <
config.autoSpawn.maxCreeps
) {
const totalEnergy = HelpersFind.getRoomTotalEnergyForSpawningAvailable(
room
);
const bodyParts: BodyPartConstant[] = [];
const bodyPartsOrder = [MOVE, WORK, CARRY];
let lastBodyPartIndex = bodyParts.length - 1;
while (HelpersCreep.bodyCost(bodyParts) < totalEnergy) {
lastBodyPartIndex =
lastBodyPartIndex === bodyParts.length - 1
? 0
: lastBodyPartIndex + 1;
if (
HelpersCreep.bodyCost([
...bodyParts,
bodyPartsOrder[lastBodyPartIndex],
]) <= totalEnergy
) {
bodyParts.push(bodyPartsOrder[lastBodyPartIndex]);
}
}
spawn.spawnCreep(bodyPartsOrder, Math.random().toString());
}
}
}
4 changes: 4 additions & 0 deletions src/ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ export interface RoomConfig {
skills?: {
[index in CreepRole]?: BodyPartConstant[];
};
autoSpawn?: {
enabled: boolean;
maxCreeps: number;
};
}

export interface EnergySourcesConfig {
Expand Down

0 comments on commit 5aff552

Please sign in to comment.