Skip to content

Commit e47aba8

Browse files
committed
fix: prevent mineral creep infinite loop on insufficient resources
This fix addresses the remaining edge case where 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 This ensures consistent validation across all code paths and makes the system self-healing by clearing invalid reactions immediately.
1 parent 686d315 commit e47aba8

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
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);

0 commit comments

Comments
 (0)