Add tests
This commit is contained in:
parent
3bd6b0ac2c
commit
a64f40b6cc
11 changed files with 2252 additions and 21 deletions
|
|
@ -51,7 +51,6 @@ export const createAction = (name: string, action: (creep: Creep) => Action): Ac
|
|||
if ((creep.memory.state ?? 0) > state) {
|
||||
return Success(creep, state);
|
||||
}
|
||||
//console.log(`[${creep.name}] Running action ${name}`)
|
||||
return action(creep)(creep, state);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
12
src/Constants/BodyPartCosts.ts
Normal file
12
src/Constants/BodyPartCosts.ts
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
const BodyPartCosts = {
|
||||
[MOVE]: 50,
|
||||
[WORK]: 100,
|
||||
[CARRY]: 50,
|
||||
[ATTACK]: 80,
|
||||
[RANGED_ATTACK]: 150,
|
||||
[HEAL]: 250,
|
||||
[CLAIM]: 600,
|
||||
[TOUGH]: 10
|
||||
}
|
||||
|
||||
export default BodyPartCosts;
|
||||
|
|
@ -15,7 +15,7 @@ export const Clerk: WorkerDefinition = {
|
|||
.repeat(),
|
||||
name: 'clerk',
|
||||
requiredCreeps: (room: Room) => 2,
|
||||
bodyDefinition: (energy: number) => [WORK].concat(new Array(Math.floor(energy / 150)).fill([MOVE, CARRY, CARRY]).reduce((x, y) => x.concat(y), [])),
|
||||
bodyDefinition: (energy: number) => energy < 100 ? [] : [WORK].concat(new Array(Math.floor((energy - 100) / 150)).fill([MOVE, CARRY, CARRY]).reduce((x, y) => x.concat(y), [])),
|
||||
motivationalThougts: [
|
||||
"Carrying 🎒",
|
||||
"💗 working"
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ export const Miner: WorkerDefinition = {
|
|||
.repeat(),
|
||||
name: 'miner',
|
||||
requiredCreeps: (room: Room) => room.find(FIND_STRUCTURES, { filter: { structureType: STRUCTURE_CONTAINER }}).length > 0 ? 4 : 0,
|
||||
bodyDefinition: (energy: number) => [WORK, WORK, MOVE, CARRY].concat(new Array(Math.floor((energy - 300) / 100)).fill(WORK)),
|
||||
bodyDefinition: (energy: number) => energy < 300 ? [] : [WORK, WORK, MOVE, CARRY].concat(new Array(Math.floor((energy - 300) / 100)).fill(WORK)),
|
||||
motivationalThougts: [
|
||||
"RocknStone"
|
||||
]
|
||||
|
|
|
|||
4
src/Workers/index.ts
Normal file
4
src/Workers/index.ts
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
export { Clerk } from "./Clerk";
|
||||
export { Constructor } from "./Constructor";
|
||||
export { Miner } from "./Miner";
|
||||
export { Upgrader } from "./Upgrader";
|
||||
13
src/Workers/worker.test.ts
Normal file
13
src/Workers/worker.test.ts
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
import BodyPartCosts from '../Constants/BodyPartCosts';
|
||||
import * as Workers from './index';
|
||||
|
||||
describe('Test Creep Workers', () => {
|
||||
console.log(Workers.Clerk)
|
||||
for (const [moduleName, worker] of Object.entries(Workers)) {
|
||||
test(`${moduleName}: Body parts cost calculation is correct`, () => {
|
||||
for (let cost = 0; cost < 1500; cost++) {
|
||||
expect(worker.bodyDefinition(cost).map((x) => BodyPartCosts[x]).reduce((x, y) => x + y, 0)).toBeLessThanOrEqual(cost);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
|
@ -2,14 +2,15 @@ export interface WorkerDefinition {
|
|||
runAction: (creep: Creep, spawn: StructureSpawn) => void,
|
||||
name: string,
|
||||
requiredCreeps: (room: Room) => number,
|
||||
bodyDefinition: (energy: number, spawn: StructureSpawn) => BodyPartConstant[],
|
||||
motivationalThougts?: Array<string>
|
||||
bodyDefinition: (energy: number) => BodyPartConstant[],
|
||||
motivationalThougts?: string[]
|
||||
}
|
||||
|
||||
export const spawnWorkers = (spawn: StructureSpawn, workers: WorkerDefinition[]): void => {
|
||||
for (const worker of workers) {
|
||||
for (let i = 0; i < worker.requiredCreeps(spawn.room); i++) {
|
||||
const ret = spawn.spawnCreep(worker.bodyDefinition(spawn.store.getCapacity(RESOURCE_ENERGY) + spawn.room.find(FIND_MY_STRUCTURES, { filter: { structureType: STRUCTURE_EXTENSION }}).length * 50, spawn), worker.name + i.toString());
|
||||
const ret = spawn.spawnCreep(worker.bodyDefinition(
|
||||
spawn.store.getCapacity(RESOURCE_ENERGY) + (spawn.room.find(FIND_MY_STRUCTURES, { filter: { structureType: STRUCTURE_EXTENSION }}).length * (Object.keys(Game.creeps).length < 2 ? (25 * Object.keys(Game.creeps).length) : 50))), worker.name + i.toString());
|
||||
if (ret === OK || ret === ERR_NOT_ENOUGH_ENERGY) {
|
||||
return;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue