Compare commits
5 Commits
restructur
...
a3841bd173
| Author | SHA1 | Date | |
|---|---|---|---|
|
a3841bd173
|
|||
|
a4b8081006
|
|||
|
f8bb65c0f4
|
|||
|
be66fe0822
|
|||
|
fb689cc300
|
3
.gitignore
vendored
3
.gitignore
vendored
@@ -3,4 +3,5 @@
|
||||
/node_modules
|
||||
|
||||
# Screeps Config
|
||||
.screeps.yml
|
||||
.screeps.yml
|
||||
docker/.env
|
||||
|
||||
1
docker/.env.sample
Normal file
1
docker/.env.sample
Normal file
@@ -0,0 +1 @@
|
||||
STEAM_KEY="<https://steamcommunity.com/dev/apikey>"
|
||||
21
docker/config.yml
Normal file
21
docker/config.yml
Normal file
@@ -0,0 +1,21 @@
|
||||
serverConfig:
|
||||
tickRate: 100
|
||||
whitelist:
|
||||
- YoshiUnfriendly
|
||||
gclToCPU: true
|
||||
maxCPU: 100
|
||||
baseCPU: 20
|
||||
stepCPU: 10
|
||||
|
||||
mods:
|
||||
- screepsmod-auth
|
||||
- screepsmod-admin-utils
|
||||
- screepsmod-mongo
|
||||
bots:
|
||||
simplebot: screepsbot-zeswarm
|
||||
|
||||
launcherOptions:
|
||||
# If set, automatically ensures all mods are updated
|
||||
autoUpdate: false
|
||||
# If set, forward console messages to terminal
|
||||
logConsole: false
|
||||
38
docker/docker-compose.yml
Normal file
38
docker/docker-compose.yml
Normal file
@@ -0,0 +1,38 @@
|
||||
services:
|
||||
mongo:
|
||||
container_name: screeps-mongo
|
||||
image: mongo:4.4.18
|
||||
volumes:
|
||||
- mongo-data:/data/db
|
||||
restart: unless-stopped
|
||||
|
||||
redis:
|
||||
container_name: screeps-redis
|
||||
image: redis:7
|
||||
volumes:
|
||||
- redis-data:/data
|
||||
restart: unless-stopped
|
||||
|
||||
screeps:
|
||||
container_name: screeps-server
|
||||
image: ghcr.io/jomik/screeps-server:edge
|
||||
depends_on:
|
||||
- mongo
|
||||
- redis
|
||||
ports:
|
||||
- 21025:21025
|
||||
environment:
|
||||
MONGO_HOST: mongo
|
||||
REDIS_HOST: redis
|
||||
STEAM_KEY: ${STEAM_KEY:?"Missing steam key"}
|
||||
volumes:
|
||||
- ./config.yml:/screeps/config.yml
|
||||
- screeps-data:/data
|
||||
- screeps-mods:/screeps/mods
|
||||
restart: unless-stopped
|
||||
|
||||
volumes:
|
||||
screeps-data:
|
||||
screeps-mods:
|
||||
redis-data:
|
||||
mongo-data:
|
||||
@@ -6,6 +6,7 @@
|
||||
"build": "node esbuild.config.mjs",
|
||||
"push:main": "npm run build && screeps-api --server main upload dist/*.js",
|
||||
"push:sim": "npm run build && screeps-api --server main upload --branch sim dist/*.js",
|
||||
"push:local": "npm run build && screeps-api --server local upload dist/*.js",
|
||||
"format": "prettier --config .prettierrc 'src/**/*.ts' --write && eslint --fix src/"
|
||||
},
|
||||
"repository": {
|
||||
|
||||
36
src/CreepRunner.ts
Normal file
36
src/CreepRunner.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
import { CreepRole, CreepRoles } from "types/creeps";
|
||||
import { getRoomCreeps } from "utils/funcs/getRoomCreeps";
|
||||
|
||||
class CreepRunner {
|
||||
public static run(room: Room, state: GameState): GameState {
|
||||
for (const name in getRoomCreeps(room)) {
|
||||
const creep = Game.creeps[name];
|
||||
|
||||
if (!creep) {
|
||||
this.clearDeadCreepMemory(name, state);
|
||||
continue;
|
||||
}
|
||||
|
||||
const roleDefinition = CreepRoles[creep.memory.role as CreepRole];
|
||||
if (!roleDefinition) {
|
||||
this.clearDeadCreepMemory(name, state);
|
||||
continue;
|
||||
}
|
||||
|
||||
state = roleDefinition.handler.run(creep, state);
|
||||
}
|
||||
|
||||
return state;
|
||||
}
|
||||
|
||||
private static clearDeadCreepMemory(creepName: string, state: GameState): void {
|
||||
console.log(`Creep ${creepName} is dead, cleaning up memory.`);
|
||||
|
||||
const roleDefinition = CreepRoles[Memory.creeps[creepName].role as CreepRole];
|
||||
roleDefinition.handler.destroy(Memory.creeps[creepName], state);
|
||||
|
||||
delete Memory.creeps[creepName]; // Clean up memory for dead creeps
|
||||
}
|
||||
}
|
||||
|
||||
export default CreepRunner;
|
||||
88
src/RequisitionsManager.ts
Normal file
88
src/RequisitionsManager.ts
Normal file
@@ -0,0 +1,88 @@
|
||||
import { CreepRequisition, CreepRole, CreepRoles } from "types/creeps";
|
||||
import { DEFAULT_GAME_CONFIG } from "types/gameConfig";
|
||||
import { get_role_cost } from "utils/funcs/getRoleCost";
|
||||
import { getRoomCreeps } from "utils/funcs/getRoomCreeps";
|
||||
import { sortCreepRolesByPriority } from "utils/funcs/sortCreepRolesByPriority";
|
||||
|
||||
class RequisitionsManager {
|
||||
public static validateState(room: Room, state: GameState): GameState {
|
||||
const creepRequisition = this.getRoomRequisition(room);
|
||||
|
||||
if (Object.values(creepRequisition).every(count => count <= 0)) {
|
||||
return state;
|
||||
}
|
||||
|
||||
const totalCreeps = Object.values(room.find(FIND_MY_CREEPS)).length;
|
||||
if (totalCreeps >= state.maxHarvesters) {
|
||||
return state; // No need to spawn more creeps
|
||||
}
|
||||
|
||||
for (const spawn of room.find(FIND_MY_SPAWNS)) {
|
||||
this.fulfillSpawnRequisition(spawn, creepRequisition);
|
||||
}
|
||||
|
||||
return state;
|
||||
}
|
||||
|
||||
private static fulfillSpawnRequisition(spawn: StructureSpawn, creepRequisition: CreepRequisition): boolean {
|
||||
if (spawn.spawning) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const rolesToSpawn = sortCreepRolesByPriority(creepRequisition);
|
||||
|
||||
for (const role of rolesToSpawn) {
|
||||
if (spawn.store[RESOURCE_ENERGY] < get_role_cost(role)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const newName = `${role.name}_${Game.time}`;
|
||||
const spawnResult = spawn.spawnCreep(role.body, newName, {
|
||||
memory: {
|
||||
role: role.name,
|
||||
room: spawn.room.name,
|
||||
spawnId: spawn.id,
|
||||
working: false
|
||||
}
|
||||
});
|
||||
if (spawnResult === OK) {
|
||||
console.log(`Spawn ${spawn.name} successfully spawned a new ${role.name}: ${newName}.`);
|
||||
return true; // Exit after spawning one creep
|
||||
} else {
|
||||
console.error(`Spawn ${spawn.name} failed to spawn a new ${role.name}: ${spawnResult}`);
|
||||
}
|
||||
}
|
||||
|
||||
return false; // No creeps were spawned
|
||||
}
|
||||
|
||||
private static getRoomRequisition(room: Room): CreepRequisition {
|
||||
const creepCounts: Record<string, number> = {};
|
||||
for (const creepMemory of Object.values(getRoomCreeps(room))) {
|
||||
const role = creepMemory.role;
|
||||
creepCounts[role] = (creepCounts[role] || 0) + 1;
|
||||
}
|
||||
|
||||
const requisition: CreepRequisition = {
|
||||
harvester: 0,
|
||||
upgrader: 0,
|
||||
builder: 0
|
||||
};
|
||||
for (const role in DEFAULT_GAME_CONFIG.minCreepsPerRole) {
|
||||
if (!(role in CreepRoles)) {
|
||||
console.log(`Unknown creep role: ${role}`);
|
||||
continue;
|
||||
}
|
||||
const roleType = role as CreepRole;
|
||||
requisition[roleType] = DEFAULT_GAME_CONFIG.minCreepsPerRole[roleType] - (creepCounts[role] || 0);
|
||||
|
||||
if (requisition[roleType] < 0) {
|
||||
requisition[roleType] = 0; // Ensure we don't have negative requisitions
|
||||
}
|
||||
}
|
||||
|
||||
return requisition;
|
||||
}
|
||||
}
|
||||
|
||||
export default RequisitionsManager;
|
||||
59
src/RoomInspector.ts
Normal file
59
src/RoomInspector.ts
Normal file
@@ -0,0 +1,59 @@
|
||||
import { checkPositionWalkable } from "utils/funcs/checkPosition";
|
||||
import {
|
||||
createSourcePositionMatrix,
|
||||
forEachMatrixSpot,
|
||||
getPositionWithDelta,
|
||||
PositionSpotStatus,
|
||||
setSpotStatus
|
||||
} from "utils/positions";
|
||||
|
||||
class RoomInspector {
|
||||
public static inspectState(room: Room, state: GameState): GameState {
|
||||
if (!this.stateWasInitialized(state)) {
|
||||
state = this.initializeState(room, state);
|
||||
}
|
||||
return state;
|
||||
}
|
||||
|
||||
private static stateWasInitialized(state: GameState): boolean {
|
||||
return !!state.sourcesStates;
|
||||
}
|
||||
|
||||
private static initializeState(room: Room, state: GameState): GameState {
|
||||
state.sourcesStates = {};
|
||||
state.maxHarvesters = 0;
|
||||
|
||||
for (const source of room.find(FIND_SOURCES)) {
|
||||
this.configureSourceState(source, state);
|
||||
}
|
||||
|
||||
return state;
|
||||
}
|
||||
|
||||
private static configureSourceState(source: Source, state: GameState): void {
|
||||
const sourceId = source.id.toString();
|
||||
|
||||
if (!state.sourcesStates[sourceId]) {
|
||||
state.sourcesStates[sourceId] = {
|
||||
id: sourceId,
|
||||
pos: source.pos,
|
||||
spots: createSourcePositionMatrix()
|
||||
};
|
||||
}
|
||||
|
||||
forEachMatrixSpot(state.sourcesStates[sourceId].spots, (delta, status) => {
|
||||
if (status !== PositionSpotStatus.UNKNOWN) {
|
||||
return; // Skip known spots
|
||||
}
|
||||
const pos = getPositionWithDelta(source.pos, delta);
|
||||
if (checkPositionWalkable(pos)) {
|
||||
setSpotStatus(state.sourcesStates[sourceId].spots, delta, PositionSpotStatus.EMPTY);
|
||||
state.maxHarvesters += 1;
|
||||
} else {
|
||||
setSpotStatus(state.sourcesStates[sourceId].spots, delta, PositionSpotStatus.INVALID);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export default RoomInspector;
|
||||
@@ -1,162 +1,14 @@
|
||||
import { CreepRequisition, CreepRole, CreepRoles, RoleDefinition } from "types/creeps";
|
||||
import { DEFAULT_GAME_CONFIG } from "types/gameConfig";
|
||||
import { checkPositionWalkable } from "utils/funcs/checkPosition";
|
||||
import { get_role_cost as get_role_cost } from "utils/funcs/getRoleCost";
|
||||
import {
|
||||
createSourcePositionMatrix,
|
||||
forEachMatrixSpot,
|
||||
getPositionWithDelta,
|
||||
PositionSpotStatus,
|
||||
setSpotStatus
|
||||
} from "utils/positions";
|
||||
import CreepRunner from "CreepRunner";
|
||||
import RequisitionsManager from "RequisitionsManager";
|
||||
import RoomInspector from "RoomInspector";
|
||||
|
||||
class RoomRunner {
|
||||
public static run(room: Room, state: GameState): GameState {
|
||||
this.updateSpawnState(room, state);
|
||||
state = RoomInspector.inspectState(room, state);
|
||||
|
||||
for (const name in this.getRoomCreeps(room)) {
|
||||
if (!Game.creeps[name]) {
|
||||
console.log(`Creep ${name} is dead, cleaning up memory.`);
|
||||
state = CreepRunner.run(room, state);
|
||||
|
||||
const roleDefinition = CreepRoles[Memory.creeps[name].role as CreepRole];
|
||||
roleDefinition.handler.destroy(Memory.creeps[name], state);
|
||||
delete Memory.creeps[name]; // Clean up memory for dead creeps
|
||||
|
||||
continue; // Skip to the next creep
|
||||
}
|
||||
const creep = Game.creeps[name];
|
||||
|
||||
const roleDefinition = CreepRoles[creep.memory.role as CreepRole];
|
||||
if (!roleDefinition) {
|
||||
console.log(`Creep ${creep.name} has an unknown role: ${creep.memory.role}`);
|
||||
continue;
|
||||
}
|
||||
|
||||
state = roleDefinition.handler.run(creep, state);
|
||||
}
|
||||
|
||||
for (const spawn of room.find(FIND_MY_SPAWNS)) {
|
||||
this.validateSpawnState(spawn, state);
|
||||
}
|
||||
|
||||
return state;
|
||||
}
|
||||
|
||||
private static getRoomCreeps(room: Room): Record<string, CreepMemory> {
|
||||
return Object.keys(Memory.creeps)
|
||||
.filter(name => Memory.creeps[name].room === room.name)
|
||||
.reduce((creeps: Record<string, CreepMemory>, creepName: string) => {
|
||||
creeps[creepName] = Memory.creeps[creepName];
|
||||
return creeps;
|
||||
}, {});
|
||||
}
|
||||
|
||||
private static updateSpawnState(room: Room, state: GameState) {
|
||||
const sources = room.find(FIND_SOURCES);
|
||||
if (!state.sourcesStates) {
|
||||
state.sourcesStates = {};
|
||||
state.maxHarvesters = 0;
|
||||
}
|
||||
for (const source of sources) {
|
||||
const sourceId = source.id.toString();
|
||||
|
||||
if (!state.sourcesStates[sourceId]) {
|
||||
state.sourcesStates[sourceId] = {
|
||||
id: sourceId,
|
||||
pos: source.pos,
|
||||
spots: createSourcePositionMatrix()
|
||||
};
|
||||
forEachMatrixSpot(state.sourcesStates[sourceId].spots, (delta, status) => {
|
||||
if (status !== PositionSpotStatus.UNKNOWN) {
|
||||
return; // Skip known spots
|
||||
}
|
||||
const pos = getPositionWithDelta(source.pos, delta);
|
||||
if (checkPositionWalkable(pos)) {
|
||||
setSpotStatus(state.sourcesStates[sourceId].spots, delta, PositionSpotStatus.EMPTY);
|
||||
state.maxHarvesters = state.maxHarvesters + 1;
|
||||
} else {
|
||||
setSpotStatus(state.sourcesStates[sourceId].spots, delta, PositionSpotStatus.INVALID);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static validateSpawnState(spawn: StructureSpawn, state: GameState) {
|
||||
if (spawn.spawning) {
|
||||
// console.log(`Spawn ${this.spawn.name} is currently spawning a creep.`);
|
||||
return;
|
||||
}
|
||||
|
||||
const creepRequisition = this.checksNeedsCreeps(spawn.room);
|
||||
if (Object.values(creepRequisition).every(count => count <= 0)) {
|
||||
// console.log(`Spawn ${this.spawn.name} has no creep needs.`);
|
||||
return;
|
||||
}
|
||||
|
||||
const totalCreeps = Object.values(Game.creeps).length;
|
||||
if (totalCreeps >= state.maxHarvesters) {
|
||||
return;
|
||||
}
|
||||
|
||||
const rolesToSpawn = this.sortCreepRolesByPriority(creepRequisition);
|
||||
|
||||
for (const role of rolesToSpawn) {
|
||||
if (spawn.store[RESOURCE_ENERGY] < get_role_cost(role)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const newName = `${role.name}_${Game.time}`;
|
||||
const spawnResult = spawn.spawnCreep(role.body, newName, {
|
||||
memory: {
|
||||
role: role.name,
|
||||
room: spawn.room.name,
|
||||
spawnId: spawn.id,
|
||||
working: false
|
||||
}
|
||||
});
|
||||
if (spawnResult === OK) {
|
||||
console.log(`Spawn ${spawn.name} successfully spawned a new ${role.name}: ${newName}.`);
|
||||
return; // Exit after spawning one creep
|
||||
} else {
|
||||
console.error(`Spawn ${spawn.name} failed to spawn a new ${role.name}: ${spawnResult}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static checksNeedsCreeps(room: Room): CreepRequisition {
|
||||
const creepCounts: Record<string, number> = {};
|
||||
for (const creepMemory of Object.values(this.getRoomCreeps(room))) {
|
||||
const role = creepMemory.role;
|
||||
creepCounts[role] = (creepCounts[role] || 0) + 1;
|
||||
}
|
||||
|
||||
const requisition: CreepRequisition = {
|
||||
harvester: 0,
|
||||
upgrader: 0,
|
||||
builder: 0
|
||||
};
|
||||
for (const role in DEFAULT_GAME_CONFIG.minCreepsPerRole) {
|
||||
if (!(role in CreepRoles)) {
|
||||
console.log(`Unknown creep role: ${role}`);
|
||||
continue;
|
||||
}
|
||||
const roleType = role as CreepRole;
|
||||
requisition[roleType] = DEFAULT_GAME_CONFIG.minCreepsPerRole[roleType] - (creepCounts[role] || 0);
|
||||
|
||||
if (requisition[roleType] < 0) {
|
||||
requisition[roleType] = 0; // Ensure we don't have negative requisitions
|
||||
}
|
||||
}
|
||||
|
||||
return requisition;
|
||||
}
|
||||
|
||||
private static sortCreepRolesByPriority(requisition: CreepRequisition): RoleDefinition[] {
|
||||
return Object.keys(requisition)
|
||||
.filter(role => requisition[role as CreepRole] > 0)
|
||||
.map(role => CreepRoles[role as CreepRole])
|
||||
.sort((a, b) => a.priority - b.priority);
|
||||
return RequisitionsManager.validateState(room, state);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
13
src/main.ts
13
src/main.ts
@@ -50,9 +50,14 @@ export const loop = () => {
|
||||
Memory.roomStateRegistry = Memory.roomStateRegistry || {};
|
||||
|
||||
for (const roomName of Object.keys(Game.rooms)) {
|
||||
Memory.roomStateRegistry[roomName] = RoomRunner.run(
|
||||
Game.rooms[roomName],
|
||||
Memory.roomStateRegistry[roomName] || {}
|
||||
);
|
||||
try {
|
||||
Memory.roomStateRegistry[roomName] = RoomRunner.run(
|
||||
Game.rooms[roomName],
|
||||
Memory.roomStateRegistry[roomName] || {}
|
||||
);
|
||||
} catch (error) {
|
||||
console.log(`Error running RoomRunner for room ${roomName}:`, error);
|
||||
delete Memory.roomStateRegistry[roomName]
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
8
src/utils/funcs/getRoomCreeps.ts
Normal file
8
src/utils/funcs/getRoomCreeps.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
export const getRoomCreeps = (room: Room): Record<string, CreepMemory> => {
|
||||
return Object.keys(Memory.creeps)
|
||||
.filter(name => Memory.creeps[name].room === room.name)
|
||||
.reduce((creeps: Record<string, CreepMemory>, creepName: string) => {
|
||||
creeps[creepName] = Memory.creeps[creepName];
|
||||
return creeps;
|
||||
}, {});
|
||||
};
|
||||
8
src/utils/funcs/sortCreepRolesByPriority.ts
Normal file
8
src/utils/funcs/sortCreepRolesByPriority.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import { CreepRequisition, CreepRole, CreepRoles, RoleDefinition } from "types/creeps";
|
||||
|
||||
export const sortCreepRolesByPriority = (requisition: CreepRequisition): RoleDefinition[] => {
|
||||
return Object.keys(requisition)
|
||||
.filter(role => requisition[role as CreepRole] > 0)
|
||||
.map(role => CreepRoles[role as CreepRole])
|
||||
.sort((a, b) => a.priority - b.priority);
|
||||
};
|
||||
0
src/utils/positions/positionMatrix.ts
Normal file
0
src/utils/positions/positionMatrix.ts
Normal file
@@ -18,6 +18,6 @@
|
||||
"allowSyntheticDefaultImports": true,
|
||||
"types": ["screeps"]
|
||||
},
|
||||
"include": ["src/**/*"],
|
||||
"include": ["src/**/*", "src/RequisitionsManager.ts"],
|
||||
"exclude": ["node_modules", "dist"]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user