Minor Fix Creep Colision

This commit is contained in:
2025-07-07 21:22:27 -03:00
parent c2a9df5e77
commit f9af46c501
5 changed files with 92 additions and 42 deletions

View File

@@ -1,6 +1,6 @@
import { getSourceById, getSpawnById } from "utils/funcs/get_by_id"; import { getSourceById, getSpawnById } from "utils/funcs/get_by_id";
import { RoleHandler } from "./roleHandler.interface"; import { RoleHandler } from "./roleHandler.interface";
import { getNextEmptySpot } from "types/source"; import { forEachMatrixSpot, getNextEmptySpot, getPositionWithDelta, setSpotStatus, SourceSpotStatus } from "types/source";
@@ -10,37 +10,82 @@ class HarvesterHandler extends RoleHandler {
case "spawn": case "spawn":
this.onSpawnDestination(creep, state); this.onSpawnDestination(creep, state);
break; break;
default: case "source":
this.onSourceDestination(creep, state); this.onSourceDestination(creep, state);
break; break;
default:
this.onFindNewSource(creep, state);
break;
} }
return state; return state;
} }
private static onSourceDestination(creep: Creep, state: GameState) { private static onFindNewSource(creep: Creep, state: GameState) {
if (!creep.memory.destination) { if (creep.memory.destination) {
console.log(`Creep ${creep.name} already has a destination set.`);
return; // Already has a destination, no need to find a new one
}
const sources = this.findClosestSource(creep, state); const sources = this.findClosestSource(creep, state);
if (sources.length > 0) {
for (const source of sources) {
const sourceState = state.sourcesStates[source.id];
const emptySpot = getNextEmptySpot(sourceState.spots);
if (!emptySpot) {
continue; // No empty spots available, skip to next source
}
setSpotStatus(
sourceState.spots,
emptySpot,
SourceSpotStatus.OCCUPIED
);
creep.memory.destination = { creep.memory.destination = {
id: sources[0].id, id: source.id,
type: "source", type: "source",
sourceSpot: getNextEmptySpot(state.sourcesStates[sources[0].id].spots) || { x: 0, y: 0 } sourceSpot: emptySpot
}; };
} else { console.log(`Source ${creep.memory.destination.id} - `, state.sourcesStates[creep.memory.destination.id].spots)
console.warn(`No sources found for creep: ${creep.name}`); return
}
console.log(`Creep ${creep.name} could not find a valid source.`);
}
private static onSourceDestination(creep: Creep, state: GameState) {
if (!creep.memory.destination || creep.memory.destination.type !== "source") {
console.log(`Creep ${creep.name} has no valid destination set.`);
delete creep.memory.destination;
return; return;
} }
if (creep.store.getFreeCapacity(RESOURCE_ENERGY) === 0) {
setSpotStatus(
state.sourcesStates[creep.memory.destination.id].spots,
creep.memory.destination.sourceSpot,
SourceSpotStatus.EMPTY
);
console.log(`Source ${creep.memory.destination.id} - `, state.sourcesStates[creep.memory.destination.id].spots)
creep.memory.destination = {
id: creep.memory.spawnId,
type: "spawn"
};
return;
} }
const source = getSourceById(creep.memory.destination.id); const source = getSourceById(creep.memory.destination.id);
if (!source) { if (!source) {
console.warn(`Source not found for creep: ${creep.name}`); console.log(`Source not found for creep: ${creep.name}`);
return; return;
} }
if (creep.harvest(source) === ERR_NOT_IN_RANGE) { if (creep.harvest(source) === ERR_NOT_IN_RANGE) {
creep.moveTo(source, { reusePath: true }); const sourceSpotPosition = getPositionWithDelta(
source.pos, creep.memory.destination.sourceSpot
)
creep.moveTo(sourceSpotPosition, { reusePath: true, ignoreCreeps: true });
} }
} }
@@ -51,29 +96,34 @@ class HarvesterHandler extends RoleHandler {
type: "spawn" type: "spawn"
} }
} }
if (creep.store.getFreeCapacity() <= 0) {
if (creep.store.getUsedCapacity(RESOURCE_ENERGY) === 0) {
delete creep.memory.destination; delete creep.memory.destination;
return; return;
} }
const spawn = getSpawnById(creep.memory.destination.id); const spawn = getSpawnById(creep.memory.destination.id);
if (!spawn) { if (!spawn) {
console.warn(`Spawn not found for creep: ${creep.name}`); console.log(`Spawn not found for creep: ${creep.name}`);
return; return;
} }
if (creep.transfer(spawn, RESOURCE_ENERGY) === ERR_NOT_IN_RANGE) { if (creep.transfer(spawn, RESOURCE_ENERGY) === ERR_NOT_IN_RANGE) {
creep.moveTo(spawn, { reusePath: true }); creep.moveTo(spawn, { reusePath: true, ignoreCreeps: true });
} }
} }
private static findClosestSource(creep: Creep, state: GameState): Source[] { private static findClosestSource(creep: Creep, state: GameState): Source[] {
return Object.keys(state.sourcesStates) const sources = Object.keys(state.sourcesStates)
.map(sourceId => getSourceById(sourceId)) .map(sourceId => getSourceById(sourceId))
.filter(source => source !== null) .filter(source => source !== null)
.sort((a, b) => creep.pos.getRangeTo(a) - creep.pos.getRangeTo(b)); .sort((a, b) => Math.abs(creep.pos.getRangeTo(a) - creep.pos.getRangeTo(b)))
// console.log(`Creep ${creep.name} found ${sources.length} sources.`);
return sources;
} }
} }

View File

@@ -14,17 +14,17 @@ class SpawnHandler {
public run(state: GameState): GameState { public run(state: GameState): GameState {
this.updateSpawnState(state); this.updateSpawnState(state);
// for(const name in Game.creeps) { for(const name in Game.creeps) {
// const creep = Game.creeps[name]; const creep = Game.creeps[name];
// const roleDefinition = CreepRoles[creep.memory.role as CreepRole]; const roleDefinition = CreepRoles[creep.memory.role as CreepRole];
// if (!roleDefinition) { if (!roleDefinition) {
// console.warn(`Creep ${creep.name} has an unknown role: ${creep.memory.role}`); console.log(`Creep ${creep.name} has an unknown role: ${creep.memory.role}`);
// continue; continue;
// } }
// state = roleDefinition.handler.run(creep, state); state = roleDefinition.handler.run(creep, state);
// } }
this.validateSpawnState(); this.validateSpawnState();
@@ -63,19 +63,19 @@ class SpawnHandler {
private validateSpawnState() { private validateSpawnState() {
if (this.spawn.spawning) { if (this.spawn.spawning) {
console.log(`Spawn ${this.spawn.name} is currently spawning a creep.`); // console.log(`Spawn ${this.spawn.name} is currently spawning a creep.`);
return; return;
} }
const creepRequisition = this.checksNeedsCreeps(); const creepRequisition = this.checksNeedsCreeps();
if (Object.values(creepRequisition).every(count => count <= 0)) { if (Object.values(creepRequisition).every(count => count <= 0)) {
console.log(`Spawn ${this.spawn.name} has no creep needs.`); // console.log(`Spawn ${this.spawn.name} has no creep needs.`);
return; return;
} }
const totalCreeps = Object.values(Game.creeps).length; const totalCreeps = Object.values(Game.creeps).length;
if (totalCreeps >= DEFAULT_GAME_CONFIG.maxCreeps) { if (totalCreeps >= DEFAULT_GAME_CONFIG.maxCreeps) {
console.log(`Spawn ${this.spawn.name} cannot spawn more creeps, limit reached.`); // console.log(`Spawn ${this.spawn.name} cannot spawn more creeps, limit reached.`);
return; return;
} }
@@ -83,7 +83,7 @@ class SpawnHandler {
for (const role of rolesToSpawn) { for (const role of rolesToSpawn) {
if (this.spawn.store[RESOURCE_ENERGY] < get_role_cost(role)) { if (this.spawn.store[RESOURCE_ENERGY] < get_role_cost(role)) {
console.log(`Spawn ${this.spawn.name} does not have enough energy to spawn a ${role.name}.`); // console.log(`Spawn ${this.spawn.name} does not have enough energy to spawn a ${role.name}.`);
continue; continue;
} }
@@ -119,7 +119,7 @@ class SpawnHandler {
}; };
for (const role in DEFAULT_GAME_CONFIG.minCreepsPerRole) { for (const role in DEFAULT_GAME_CONFIG.minCreepsPerRole) {
if (!(role in CreepRoles)) { if (!(role in CreepRoles)) {
console.warn(`Unknown creep role: ${role}`); console.log(`Unknown creep role: ${role}`);
continue; continue;
} }
const roleType = role as CreepRole; const roleType = role as CreepRole;

View File

@@ -22,9 +22,9 @@ export type GameConfig = {
* @type {GameConfig} * @type {GameConfig}
*/ */
export const DEFAULT_GAME_CONFIG: GameConfig = { export const DEFAULT_GAME_CONFIG: GameConfig = {
maxCreeps: 1, maxCreeps: 7,
minCreepsPerRole: { minCreepsPerRole: {
harvester: 1, harvester: 7,
upgrader: 0, upgrader: 0,
builder: 0 builder: 0
} }

View File

@@ -34,7 +34,7 @@ export const createSourcePositionMatrix = () : SourcePositionMatrix => {
export const getNextEmptySpot = (matrix: SourcePositionMatrix): PositionDelta | null => { export const getNextEmptySpot = (matrix: SourcePositionMatrix): PositionDelta | null => {
const index = matrix.findIndex( status => status === SourceSpotStatus.EMPTY || status === SourceSpotStatus.UNKNOWN); const index = matrix.findIndex( status => status === SourceSpotStatus.EMPTY);
if (index === -1) { if (index === -1) {
return null; // No empty spot found return null; // No empty spot found

View File

@@ -1,12 +1,12 @@
export const getSourceById = (sourceId: string): Source | null => { export const getSourceById = (sourceId: string): Source | null => {
if (!sourceId) { if (!sourceId) {
console.warn("getSourceById called with an empty or undefined sourceId."); console.log("getSourceById called with an empty or undefined sourceId.");
return null; return null;
} }
const source = Game.getObjectById<Source>(sourceId); const source = Game.getObjectById<Source>(sourceId);
if (!source) { if (!source) {
console.warn(`No source found with ID: ${sourceId}`); console.log(`No source found with ID: ${sourceId}`);
return null; return null;
} }
@@ -16,13 +16,13 @@ export const getSourceById = (sourceId: string): Source | null => {
export const getSpawnById = (spawnId: string): StructureSpawn | null => { export const getSpawnById = (spawnId: string): StructureSpawn | null => {
if (!spawnId) { if (!spawnId) {
console.warn("getSpawnById called with an empty or undefined spawnId."); console.log("getSpawnById called with an empty or undefined spawnId.");
return null; return null;
} }
const spawn = Game.getObjectById<StructureSpawn>(spawnId); const spawn = Game.getObjectById<StructureSpawn>(spawnId);
if (!spawn) { if (!spawn) {
console.warn(`No spawn found with ID: ${spawnId}`); console.log(`No spawn found with ID: ${spawnId}`);
return null; return null;
} }