Add Creep Actions
This commit is contained in:
parent
848e69dc05
commit
a73f1f7004
9 changed files with 221 additions and 57 deletions
61
src/Actions/Action.ts
Normal file
61
src/Actions/Action.ts
Normal 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
23
src/Actions/harvest.ts
Normal 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
15
src/Actions/moveTo.ts
Normal 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;
|
||||
}
|
||||
}
|
||||
});
|
||||
22
src/Actions/transferEnergy.ts
Normal file
22
src/Actions/transferEnergy.ts
Normal 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;
|
||||
}
|
||||
}
|
||||
});
|
||||
19
src/Actions/upgradeController.ts
Normal file
19
src/Actions/upgradeController.ts
Normal 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;
|
||||
}
|
||||
}
|
||||
});
|
||||
23
src/index.ts
23
src/index.ts
|
|
@ -1,10 +1,25 @@
|
|||
import { runAction } from "./Actions/Action";
|
||||
import { harvestFromClosestActiveSource } from "./Actions/harvest";
|
||||
import { transferEnergy } from "./Actions/transferEnergy";
|
||||
import { upgradeController } from "./Actions/upgradeController";
|
||||
|
||||
export function loop() {
|
||||
const spawn = Game.spawns.Spawn1;
|
||||
const controller = spawn.room.controller;
|
||||
if (!controller) {
|
||||
return;
|
||||
}
|
||||
spawn.spawnCreep([WORK, CARRY, CARRY, MOVE], 'w6');
|
||||
spawn.spawnCreep([WORK, CARRY, CARRY, MOVE], 'w5');
|
||||
spawn.spawnCreep([WORK, CARRY, CARRY, MOVE], 'w4');
|
||||
spawn.spawnCreep([WORK, CARRY, CARRY, MOVE], 'w3');
|
||||
spawn.spawnCreep([WORK, CARRY, CARRY, MOVE], 'w2');
|
||||
spawn.spawnCreep([WORK, CARRY, CARRY, MOVE], 'w1');
|
||||
for (const creep of Object.values(Game.creeps)) {
|
||||
const source = creep.pos.findClosestByPath(FIND_SOURCES_ACTIVE);
|
||||
if (source && creep.harvest(source) === ERR_NOT_IN_RANGE) {
|
||||
creep.moveTo(source);
|
||||
}
|
||||
runAction(creep, harvestFromClosestActiveSource())
|
||||
.andThen(transferEnergy(spawn))
|
||||
.or(upgradeController(controller))
|
||||
.repeat()
|
||||
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue