Add Creep Actions

This commit is contained in:
uwap 2022-12-21 11:57:49 +01:00
parent 848e69dc05
commit a73f1f7004
9 changed files with 221 additions and 57 deletions

61
src/Actions/Action.ts Normal file
View file

@ -0,0 +1,61 @@
declare global {
interface CreepMemory {
state?: number
}
}
export interface ChainableAction {
or: (action: Action) => ChainableAction,
and: (action: Action) => ChainableAction,
andThen: (action: Action) => ChainableAction,
repeat: () => void
}
export const NoOp: Action = (creep: Creep, state: number = 0) => ({
or: () => NoOp(creep, state),
and: () => NoOp(creep, state),
andThen: () => NoOp(creep, state),
repeat: () => {}
});
export const Success: Action = (creep: Creep, state: number = 0) => ({
or: () => Success(creep, state),
and: (action: Action) => action(creep, state),
andThen: (action: Action) => {
creep.memory.state = state + 1;
return action(creep, state + 1);
},
repeat: () => {
creep.memory.state = 0;
}
});
export const InProgress: Action = (creep: Creep, state: number = 0) => ({
or: () => InProgress(creep, state),
and: (action: Action) => action(creep, state),
andThen: () => NoOp(creep, state),
repeat: () => {}
});
export const Fail: Action = (creep: Creep, state: number = 0) => ({
or: (action: Action) => action(creep, state),
and: () => NoOp(creep, state),
andThen: () => NoOp(creep, state),
repeat: () => {
console.log('Warning: Last task in series failed for creep ' + creep.name);
}
})
export const createAction = (name: string, action: (creep: Creep) => Action): Action => {
return (creep: Creep, state: number = 0) => {
if ((creep.memory.state ?? 0) > state) {
return Success(creep, state);
}
console.log(`[${creep.name}] Running action ${name}`)
return action(creep)(creep, state);
}
}
export const runAction = (creep: Creep, action: Action): ChainableAction => action(creep);
export type Action = (creep: Creep, state?: number) => ChainableAction;

23
src/Actions/harvest.ts Normal file
View file

@ -0,0 +1,23 @@
import { createAction, Fail, InProgress, Success } from "./Action";
import { moveTo } from "./moveTo";
export const harvestFromClosestActiveSource = () => createAction('harvestFromClosestActiveSource', (creep: Creep) => {
const source = creep.pos.findClosestByPath(FIND_SOURCES_ACTIVE);
if (!source) {
return Fail;
}
if (creep.store.getFreeCapacity(RESOURCE_ENERGY) === 0) {
return Success;
}
switch(creep.harvest(source)) {
case OK: {
return InProgress;
}
case ERR_NOT_IN_RANGE: {
return moveTo(source);
}
default: {
return Fail;
}
}
});

15
src/Actions/moveTo.ts Normal file
View file

@ -0,0 +1,15 @@
import { createAction, Fail, InProgress } from "./Action";
export const moveTo = (pos: _HasRoomPosition | RoomPosition) => createAction('moveTo', (creep: Creep) => {
switch(creep.moveTo(pos)) {
case OK: {
return InProgress;
}
case ERR_TIRED: {
return InProgress;
}
default: {
return Fail;
}
}
});

View file

@ -0,0 +1,22 @@
import { createAction, Fail, InProgress, Success } from "./Action";
import { moveTo } from "./moveTo";
export const transferEnergy = (target: Creep | StructureSpawn) => createAction('transferEnergy', (creep: Creep) => {
if (target.store.getFreeCapacity(RESOURCE_ENERGY) === 0) {
return Fail;
}
switch(creep.transfer(target, RESOURCE_ENERGY)) {
case OK: {
return InProgress;
}
case ERR_NOT_ENOUGH_RESOURCES: {
return Success;
}
case ERR_NOT_IN_RANGE: {
return moveTo(target);
}
default: {
return Fail;
}
}
});

View file

@ -0,0 +1,19 @@
import { createAction, Fail, InProgress, Success } from "./Action";
import { moveTo } from "./moveTo";
export const upgradeController = (controller: StructureController) => createAction('upgradeController', (creep: Creep) => {
switch(creep.upgradeController(controller)) {
case OK: {
return InProgress;
}
case ERR_NOT_ENOUGH_RESOURCES: {
return Success;
}
case ERR_NOT_IN_RANGE: {
return moveTo(controller);
}
default: {
return Fail;
}
}
});