Skip to content

Commit ee79bc3

Browse files
committed
fix: prevent mineral and squad infinite loops
This fix addresses two critical infinite loop issues that waste CPU: 1. Mineral Creep Infinite Loop ---------------------------- Mineral creeps would infinitely loop trying to transfer resources from the terminal when insufficient amounts (1-4 units) were available. Changes: - Update validation threshold from === 0 to < LAB_REACTION_AMOUNT (5) in both setStatePrepareReactionLab1WithResource and setStatePrepareReactionLab2WithResource to match room cleanup logic - Add reaction cleanup in handleWithdrawFromSource to delete room.memory.reaction when resources are insufficient, preventing immediate re-setting of the same failed state - Enhance validation check to also verify resource amount meets LAB_REACTION_AMOUNT threshold during withdrawal 2. Squad Bouncing at Room Borders -------------------------------- Squad creeps (both siege and heal) would bounce infinitely between room exits, logging "Reached end of handling() why?" every tick. Root cause: When creeps reached target room border, they would: - Enter target room -> set routing.reached = true - Move back across border due to border tile instability - Delete routing.reached flag (not in target room anymore) - Routing system restarts -> creep moves forward again - Infinite loop continues Changes: - Add explicit if/else logic in squadsiege.action to separate traveling vs in-target-room behavior - Add border protection: only reset routing.reached if not at border tile (using isBorder check) to prevent bouncing - Don't execute siege() when traveling (not in target room) - Apply same fix to squadheal.action for consistency Both fixes ensure consistent validation across code paths and make the systems self-healing by clearing invalid states immediately.
1 parent 686d315 commit ee79bc3

File tree

3 files changed

+18
-10
lines changed

3 files changed

+18
-10
lines changed

src/role_mineral.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ function setStatePrepareReactionLab1WithResource(creep) {
348348
return false;
349349
}
350350
// Check if terminal actually has the required resource
351-
if (!creep.room.terminal || !creep.room.terminal.store[reaction.result.first] || creep.room.terminal.store[reaction.result.first] === 0) {
351+
if (!creep.room.terminal || !creep.room.terminal.store[reaction.result.first] || creep.room.terminal.store[reaction.result.first] < LAB_REACTION_AMOUNT) {
352352
return false;
353353
}
354354
creep.data.state = {
@@ -378,7 +378,7 @@ function setStatePrepareReactionLab2WithResource(creep) {
378378
return false;
379379
}
380380
// Check if terminal actually has the required resource
381-
if (!creep.room.terminal || !creep.room.terminal.store[reaction.result.second] || creep.room.terminal.store[reaction.result.second] === 0) {
381+
if (!creep.room.terminal || !creep.room.terminal.store[reaction.result.second] || creep.room.terminal.store[reaction.result.second] < LAB_REACTION_AMOUNT) {
382382
return false;
383383
}
384384
creep.data.state = {
@@ -492,9 +492,13 @@ function handleWithdrawFromSource(creep) {
492492
}
493493
creep.moveToMy(source.pos);
494494
const resource = creep.data.state.getResource(source);
495-
if (!resource) {
496-
creep.log(`No resource available from ${source}, clearing state`);
495+
if (!resource || !source.store[resource] || source.store[resource] < LAB_REACTION_AMOUNT) {
496+
creep.log(`No sufficient resource available from ${source}, clearing state and reaction`);
497497
delete creep.data.state;
498+
// Also clear the room's reaction to prevent immediate re-setting
499+
if (creep.room.memory.reaction) {
500+
delete creep.room.memory.reaction;
501+
}
498502
return true;
499503
}
500504
const response = creep.withdraw(source, resource);

src/role_squadheal.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,14 @@ roles.squadheal.preMove = function(creep, directions) {
4747
}
4848
};
4949

50-
// TODO need to check if it works
5150
roles.squadheal.action = function(creep) {
5251
creep.selfHeal();
5352
if (creep.room.name !== creep.memory.routing.targetRoom) {
54-
// creep.log('Not in room');
53+
// Not in target room yet - traveling
5554
if (creep.hits < creep.hitsMax) {
5655
creep.moveRandom();
57-
} else {
58-
// creep.log('delete?');
56+
} else if (!creep.pos.isBorder(-1)) {
57+
// Only reset routing if not at border to prevent bouncing
5958
delete creep.memory.routing.reached;
6059
}
6160
return true;

src/role_squadsiege.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,20 @@ roles.squadsiege.preMove = function(creep, directions) {
7676
return false;
7777
};
7878

79-
// TODO need to check if it works
8079
roles.squadsiege.action = function(creep) {
8180
creep.say('action');
81+
8282
if (creep.room.name !== creep.memory.routing.targetRoom) {
83+
// Not in target room yet - traveling
8384
if (creep.hits < creep.hitsMax) {
8485
creep.moveRandom();
85-
} else {
86+
} else if (!creep.pos.isBorder(-1)) {
87+
// Only reset routing if not at border to prevent bouncing
8688
delete creep.memory.routing.reached;
8789
}
90+
return true; // Don't execute siege when traveling
8891
}
92+
93+
// In target room - execute siege action
8994
return creep.siege();
9095
};

0 commit comments

Comments
 (0)