Fixes Game Source Pos

Fixes Game Source Pos
This commit is contained in:
2025-07-07 08:41:47 -03:00
parent bb3f797fc3
commit 4f28d240e0
6 changed files with 104 additions and 20 deletions

View File

@@ -9,11 +9,22 @@ declare global {
Types added in this `global` block are in an ambient, global context. This is needed because `main.ts` is a module file (uses import or export).
Interfaces matching on name from @types/screeps will be merged. This is how you can extend the 'built-in' interfaces from @types/screeps.
*/
interface SourceState {
id: string;
pos: RoomPosition;
maxHarvesters: number|null;
currentHarvesters: number;
}
interface GameState {
sourcesStates: { [sourceId: string]: SourceState };
}
// Memory extension samples
interface Memory {
uuid: number;
log: any;
spawnHandlers: { [name: string]: string };
spawnStates: { [name: string]: GameState };
}
interface CreepMemory {
@@ -32,8 +43,13 @@ declare global {
export const loop = () => {
Memory.spawnStates = Memory.spawnStates || {};
for (const spawnName of Object.keys(Game.spawns)) {
// Run the handler
new SpawnHandler(Game.spawns[spawnName]).run();
const spawnState = Memory.spawnStates[spawnName] || {};
const spawnHandler = new SpawnHandler(Game.spawns[spawnName]);
Memory.spawnStates[spawnName] = spawnHandler.run(spawnState);
}
};

View File

@@ -1,21 +1,50 @@
import getSourceById from "utils/funcs/get_source_by_id";
import { RoleHandler } from "./roleHandler.interface";
class HarvesterHandler extends RoleHandler {
public static run(creep: Creep): void {
public static run(creep: Creep, state: GameState): GameState {
const source = this.findClosestSource(creep, state);
console.log(`Running HarvesterHandler for creep: ${creep.name}`);
if (creep.store.getFreeCapacity() > 0) {
const sources = creep.room.find(FIND_SOURCES);
if (creep.harvest(sources[0]) === ERR_NOT_IN_RANGE) {
creep.moveTo(sources[0]);
if (creep.harvest(source) === ERR_NOT_IN_RANGE) {
creep.moveTo(source, { reusePath: true });
}
} else {
const spawn = Game.spawns['Spawn1'];
if (creep.transfer(spawn, RESOURCE_ENERGY) === ERR_NOT_IN_RANGE) {
creep.moveTo(spawn);
creep.moveTo(spawn, { reusePath: true });
}
}
return state;
}
private static findClosestSource(creep: Creep, state: GameState): Source|null {
for (const source of creep.room.find(FIND_SOURCES)) {
}
let closestSourceId = null;
for (const sourceId in state.sourcesStates) {
if (!closestSourceId) {
closestSourceId = sourceId;
}
const sourceInfo = state.sourcesStates[sourceId];
const creepPos = creep.pos;
if (creepPos.getRangeTo(sourceInfo["pos"]) < creepPos.getRangeTo(state.sourcesStates[closestSourceId]["pos"])) {
closestSourceId = sourceId;
}
}
if (!closestSourceId) {
console.warn(`No sources found for creep: ${creep.name}`);
return null;
}
return getSourceById(closestSourceId);
}
}

View File

@@ -1,3 +1,3 @@
export abstract class RoleHandler {
static run(creep: Creep): void {};
static run(creep: Creep, state: GameState): GameState {};
}

View File

@@ -9,8 +9,8 @@ class SpawnHandler {
return this.spawn.name;
}
public run(): void {
this.validateSpawnState();
public run(state: GameState): GameState {
this.updateSpawnState(state);
for(const name in Game.creeps) {
const creep = Game.creeps[name];
@@ -21,7 +21,29 @@ class SpawnHandler {
continue;
}
roleDefinition.handler.run(creep);
state = roleDefinition.handler.run(creep, state);
}
this.validateSpawnState();
return state;
}
private updateSpawnState(state: GameState) {
const sources = this.spawn.room.find(FIND_SOURCES);
for (const source of sources) {
if (!state.sourcesStates) {
state.sourcesStates = {};
}
const sourceId = source.id.toString();
if (!state.sourcesStates[sourceId]) {
state.sourcesStates[sourceId] = {
"id": sourceId,
"pos": source.pos,
"maxHarvesters": null,
"currentHarvesters": 0
};
}
}
}

View File

@@ -24,7 +24,7 @@ export type GameConfig = {
export const DEFAULT_GAME_CONFIG: GameConfig = {
maxCreeps: 15,
minCreepsPerRole: {
harvester: 5,
harvester: 15,
upgrader: 0,
builder: 0
}

View File

@@ -0,0 +1,17 @@
const getSourceById = (sourceId: string): Source | null => {
if (!sourceId) {
console.warn("getSourceById called with an empty or undefined sourceId.");
return null;
}
const source = Game.getObjectById<Source>(sourceId);
if (!source) {
console.warn(`No source found with ID: ${sourceId}`);
return null;
}
return source;
}
export default getSourceById;