Skip to content

Commit

Permalink
various refactorings
Browse files Browse the repository at this point in the history
  • Loading branch information
arghasen committed Apr 17, 2024
1 parent 70627e3 commit 06e44e5
Show file tree
Hide file tree
Showing 7 changed files with 323 additions and 326 deletions.
59 changes: 59 additions & 0 deletions src/slowdeath/city/creepBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Role, roleNames } from "slowdeath/creepActions/constants";
export function getContinuousHarvesterBody(energyCapacityAvailable: number): BodyPartConstant[] {
let body: BodyPartConstant[] = [];
if (energyCapacityAvailable > 1000) {
Expand Down Expand Up @@ -81,3 +82,61 @@ export function getSpawnCost(body: BodyPartConstant[]) {
}
return cost;
}

export function getContinuousHarvestor(energyCapacityAvailable: number): CreepSpawnData {
return {
build: getContinuousHarvesterBody(energyCapacityAvailable),
name: `cont_harv-${Game.time}`,
options: { memory: { role: Role.CONTINUOUS_HARVESTER, harvesting: false } }
};
}

export function getClaimer(energyCapacityAvailable: number): CreepSpawnData | null {
let body: BodyPartConstant[] = [];
if (energyCapacityAvailable >= 650) {
body = [CLAIM, MOVE];
return {
build: body,
name: `creep-${Game.time}`,
options: {
memory: {
role: Role.CLAIMER,
moveLoc: Memory.createClaimer?.loc,
identifier: Memory.createClaimer?.identifier,
harvesting: false
}
}
};
}
return null;
}

export function getHauler(energyCapacityAvailable: number, role: Role): CreepSpawnData {
return {
build: getHaulerBody(energyCapacityAvailable),
name: `${roleNames[role]}-${Game.time}`,
options: { memory: { role: role, harvesting: false } }
};
}

export function getRemoteBuilder(energyCapacityAvailable: number): CreepSpawnData {
return {
build: getBuilderBody(energyCapacityAvailable),
name: `rembuilder-${Game.time}`,
options: {
memory: {
role: Role.BUILDER,
moveLoc: Memory.needBuilder.moveLoc,
harvesting: false
}
}
};
}

export function getEmergencyCreep(): CreepSpawnData {
return {
build: [WORK, CARRY, MOVE],
name: `$emergency-${Game.time}`,
options: { memory: { role: Role.HARVESTER, harvesting: false } }
};
}
129 changes: 50 additions & 79 deletions src/slowdeath/city/employment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ export class Employment extends Process {
private rcl = 0;

public main() {
this.initilize();
this.getWorkerCounts();
this.populationBasedEmployer();
this.runCreepActions();
}

private initilize() {
this.metadata = this.data as CityData;
this.room = Game.rooms[this.metadata.roomName];
if (this.room.controller) {
Expand All @@ -32,9 +39,6 @@ export class Employment extends Process {
logger.debug(`${this.className}: Starting employment in ${this.metadata.roomName}`);
this.myCreeps = _.values(Game.creeps);
logger.info(`${this.room.name} energy Available: ${this.room.energyAvailable} capacity: ${this.room.energyCapacityAvailable}`);
this.getWorkerCounts();
this.populationBasedEmployer();
this.runCreepActions();
}

private populationBasedEmployer() {
Expand All @@ -53,42 +57,32 @@ export class Employment extends Process {
return cur < max * scale;
}
}

private createWorkers(employ: (cur: number, max: number) => boolean) {

// FIXME: Improve this logic
const spawnQueue = this.room.memory.spawnQueue;

spawnQueue.forEach((role)=>{this.workerCounter(role);})

const buildersRequired = (this.rcl >= 5 && this.numBuilders >= 1) ? false : true;
spawnQueue.forEach((role) => { this.workerCounter(role); })
const buildersRequired = !(this.rcl >= 4 && this.numBuilders >= 1 && !this.room.memory.extraBuilders);
if (
employ(this.numHarversters, MaxRolePopulation.harvesters) &&
!this.room.memory.continuousHarvestingStarted
) {
spawnQueue.push(Role.ROLE_HARVESTER);
spawnQueue.push(Role.HARVESTER);
} else if (
employ(this.numHaulers, MaxRolePopulation.haulers) &&
this.room.memory.continuousHarvestingStarted
) {
spawnQueue.push(Role.ROLE_HAULER);
} else if (employ(this.numBuilders, this.dynamicEmployer(Role.ROLE_BUILDER)) && buildersRequired) {
spawnQueue.push(Role.ROLE_BUILDER);
} else if (employ(this.numUpgraders, this.dynamicEmployer(Role.ROLE_UPGRADER))) {
spawnQueue.push(Role.ROLE_UPGRADER);
spawnQueue.push(Role.HAULER);
} else if (employ(this.numBuilders, this.dynamicEmployer(Role.BUILDER)) && buildersRequired) {
spawnQueue.push(Role.BUILDER);
} else if (employ(this.numUpgraders, this.dynamicEmployer(Role.UPGRADER))) {
spawnQueue.push(Role.UPGRADER);
}
}

private checkEmemrgencySituation(totWorkers: number) {
if (totWorkers < PopulationScaler[this.rcl] && this.rcl <= 2) {
this.room.memory.critical = true;
}
else if (totWorkers < PopulationScaler[this.rcl] * 2 && this.rcl >= 3) {
this.room.memory.critical = true;
}
else {
this.room.memory.critical = false;
}
this.room.memory.critical = (totWorkers < PopulationScaler[this.rcl]);
}

private waitForContiniousHarvester(totWorkers: number) {
Expand All @@ -115,21 +109,17 @@ export class Employment extends Process {
}

private storeHarvestingStatus() {
if (this.numContinuousHarvesters >= 1) {
this.room.memory.continuousHarvestingStarted = true;
} else {
this.room.memory.continuousHarvestingStarted = false;
}
this.room.memory.continuousHarvestingStarted = (this.numContinuousHarvesters >= 1)
}

private dynamicEmployer(role: Role): number {
if (role == Role.ROLE_BUILDER) {
if (role == Role.BUILDER) {
if (this.room.memory.extraBuilders) {
return MaxRolePopulation.builders + 1;
} else {
return MaxRolePopulation.builders;
}
} else if (role == Role.ROLE_UPGRADER) {
} else if (role == Role.UPGRADER) {
if (this.room.memory.extraBuilders) {
return MaxRolePopulation.upgrader - 1;
} else {
Expand Down Expand Up @@ -159,63 +149,44 @@ export class Employment extends Process {
};

private workerCounter(role: Role) {
let unemployed = false;
switch (role) {
case Role.ROLE_HARVESTER:
this.numHarversters++;
break;
case Role.ROLE_UPGRADER:
this.numUpgraders++;
break;
case Role.ROLE_HAULER:
this.numHaulers++;
break;
case Role.ROLE_BUILDER:
this.numBuilders++;
break;
case Role.ROLE_CONTINUOUS_HARVESTER:
this.numContinuousHarvesters++;
break;
case Role.ROLE_CLAIMER:
this.numClaimer++;
break;
case Role.ROLE_DISMANTLER:
break;
default:
unemployed = true;
break;
const roleCounters: Record<Role, () => void> = {
[Role.HARVESTER]: () => this.numHarversters++,
[Role.UPGRADER]: () => this.numUpgraders++,
[Role.HAULER]: () => this.numHaulers++,
[Role.BUILDER]: () => this.numBuilders++,
[Role.CONTINUOUS_HARVESTER]: () => this.numContinuousHarvesters++,
[Role.CLAIMER]: () => this.numClaimer++,
[Role.DISMANTLER]: () => { },
[Role.REM_UPGRADER]: () => { },
};

if (role in roleCounters) {
roleCounters[role]();
return false;
}
return unemployed;

return true;
}

private runCreepActions() {
const creepActions: Record<Role, (creep: Creep) => void> = {
[Role.HARVESTER]: Harvester.run,
[Role.HAULER]: Hauler.run,
[Role.BUILDER]: Builder.run,
[Role.UPGRADER]: Upgrader.run,
[Role.CONTINUOUS_HARVESTER]: ContinuousHarvester.run,
[Role.CLAIMER]: Claimer.run,
[Role.DISMANTLER]: Dismantler.run,
[Role.REM_UPGRADER]: () => { },
};

for (const creep of this.myCreeps) {
if (creep.pos.roomName !== this.room.name) {
continue;
}
switch (creep.memory.role) {
case Role.ROLE_HARVESTER:
Harvester.run(creep);
break;
case Role.ROLE_HAULER:
Hauler.run(creep);
break;
case Role.ROLE_BUILDER:
Builder.run(creep);
break;
case Role.ROLE_UPGRADER:
Upgrader.run(creep);
break;
case Role.ROLE_CONTINUOUS_HARVESTER:
ContinuousHarvester.run(creep);
break;
case Role.ROLE_CLAIMER:
Claimer.run(creep);
break;
case Role.ROLE_DISMANTLER:
// Dismantler.run(creep);
default:
_.noop();

if (creep.memory.role in creepActions) {
creepActions[creep.memory.role as Role](creep);
}
}
}
Expand Down
36 changes: 20 additions & 16 deletions src/slowdeath/city/military.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,22 @@ export class Military extends Process {
protected className = "military";
private metadata?: CityData;
private towers: StructureTower[] = [];
private defendedRoomThisTick: boolean = false;
public main() {
this.metadata = this.data as CityData;
logger.debug(`${this.className}: Starting military for ${this.metadata.roomName}`);
const room = Game.rooms[this.metadata.roomName];
this.towers = room.find(FIND_MY_STRUCTURES, {
filter: { structureType: STRUCTURE_TOWER }
});

this.defendRoom(room);
if (!this.defendedRoomThisTick) {
this.towers = this.findTowers(room);
if (!this.defendRoom(room)) {
this.repairRoom(room);
}
}

private findTowers(room: Room): StructureTower[] {
return room.find(FIND_MY_STRUCTURES, {
filter: { structureType: STRUCTURE_TOWER }
});
}

private repairRoom(room: Room): void {
const repairTower = this.towers[0];
if (repairTower) {
Expand All @@ -33,17 +34,20 @@ export class Military extends Process {
}
}

private defendRoom(room: Room): void {
private defendRoom(room: Room): boolean {
const hostiles: Creep[] = room.find(FIND_HOSTILE_CREEPS);
if (hostiles.length > 0) {
const hasHostiles = hostiles.length > 0;
if (hasHostiles) {
Game.notify(`enemy spotted in room ${room.name}`);

this.towers.forEach((tower: AnyOwnedStructure): void => {
if (tower.structureType === STRUCTURE_TOWER) {
tower.attack(hostiles[0]);
}
});
this.defendedRoomThisTick = true;
this.attackHostiles(hostiles);
}
return hasHostiles;
}
private attackHostiles(hostiles: Creep[]): void {
this.towers.forEach((tower: AnyOwnedStructure): void => {
if (tower.structureType === STRUCTURE_TOWER) {
tower.attack(hostiles[0]);
}
});
}
}
Loading

0 comments on commit 06e44e5

Please sign in to comment.