refactor: streamline safety checks and error handling in mining operation for improved reliability
This commit is contained in:
@@ -40,48 +40,59 @@ export class MiningState extends BaseState {
|
|||||||
console.log(`[${this.turtle.id}] Starting mining operation`);
|
console.log(`[${this.turtle.id}] Starting mining operation`);
|
||||||
|
|
||||||
while (!this.cancelled) {
|
while (!this.cancelled) {
|
||||||
// Safety checks
|
try {
|
||||||
const fuel = await this.checkFuel();
|
// Safety checks
|
||||||
if (fuel !== 'unlimited' && fuel < this.minFuel) {
|
const fuel = await this.checkFuel();
|
||||||
console.log(`[${this.turtle.id}] Low fuel (${fuel}), attempting refuel`);
|
if (fuel !== 'unlimited' && fuel < this.minFuel) {
|
||||||
const refueled = await this.tryRefuel();
|
console.log(`[${this.turtle.id}] Low fuel (${fuel}), attempting refuel`);
|
||||||
if (!refueled) {
|
const refueled = await this.tryRefuel();
|
||||||
console.log(`[${this.turtle.id}] Cannot refuel, going home`);
|
if (!refueled) {
|
||||||
this.turtle.setState('goHome', { reason: 'low_fuel' });
|
console.log(`[${this.turtle.id}] Cannot refuel, going home`);
|
||||||
|
this.turtle.setState('goHome', { reason: 'low_fuel' });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check inventory
|
||||||
|
const isFull = await this.isInventoryFull();
|
||||||
|
if (isFull) {
|
||||||
|
console.log(`[${this.turtle.id}] Inventory full, going home to dump`);
|
||||||
|
this.turtle.setState('goHome', { reason: 'inventory_full', returnState: 'mining', returnData: this.data });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check distance from home
|
||||||
|
if (this._isTooFar()) {
|
||||||
|
console.log(`[${this.turtle.id}] Too far from home, returning`);
|
||||||
|
this.turtle.setState('goHome', { reason: 'too_far' });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Mark current position as visited
|
||||||
|
const pos = this.turtle.position;
|
||||||
|
if (pos) {
|
||||||
|
this.visitedPositions.add(`${pos.x},${pos.y},${pos.z}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Scan surroundings for ores
|
||||||
|
const scanResult = await this.scanSurroundings();
|
||||||
|
|
||||||
|
// Mine any ores found in surroundings
|
||||||
|
yield* this._mineAdjacentOres();
|
||||||
|
|
||||||
|
// Explore step - try to find new areas
|
||||||
|
yield* this._exploreStep();
|
||||||
|
|
||||||
|
} catch (error) {
|
||||||
|
const isTimeout = error.message?.includes('timed out');
|
||||||
|
if (isTimeout) {
|
||||||
|
console.warn(`[${this.turtle.id}] Mining exec timeout, will retry next iteration`);
|
||||||
|
await this._sleep(3000);
|
||||||
|
} else {
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check inventory
|
|
||||||
const isFull = await this.isInventoryFull();
|
|
||||||
if (isFull) {
|
|
||||||
console.log(`[${this.turtle.id}] Inventory full, going home to dump`);
|
|
||||||
this.turtle.setState('goHome', { reason: 'inventory_full', returnState: 'mining', returnData: this.data });
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check distance from home
|
|
||||||
if (this._isTooFar()) {
|
|
||||||
console.log(`[${this.turtle.id}] Too far from home, returning`);
|
|
||||||
this.turtle.setState('goHome', { reason: 'too_far' });
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Mark current position as visited
|
|
||||||
const pos = this.turtle.position;
|
|
||||||
if (pos) {
|
|
||||||
this.visitedPositions.add(`${pos.x},${pos.y},${pos.z}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Scan surroundings for ores
|
|
||||||
const scanResult = await this.scanSurroundings();
|
|
||||||
|
|
||||||
// Mine any ores found in surroundings
|
|
||||||
yield* this._mineAdjacentOres();
|
|
||||||
|
|
||||||
// Explore step - try to find new areas
|
|
||||||
yield* this._exploreStep();
|
|
||||||
|
|
||||||
yield;
|
yield;
|
||||||
await this._sleep(200);
|
await this._sleep(200);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user