feat: Implement DumpInventoryState class for turtle state machine to manage inventory dumping

This commit is contained in:
MayaTheShy
2026-02-20 01:45:29 -05:00
parent 68d4ee52c9
commit 1b08777f88

View File

@@ -0,0 +1,89 @@
/**
* DumpInventoryState - Dump inventory into nearby containers
* Keeps fuel items, dumps everything else
*/
import { BaseState } from './BaseState.js';
const KEEP_ITEMS = new Set([
'minecraft:coal', 'minecraft:charcoal', 'minecraft:coal_block',
'minecraft:torch', 'minecraft:lava_bucket',
]);
export class DumpInventoryState extends BaseState {
constructor(turtle, data = {}) {
super(turtle, data);
this.returnState = data.returnState || null;
this.returnData = data.returnData || {};
}
get name() {
return 'dumping';
}
get description() {
return 'Dumping inventory';
}
async *act() {
console.log(`[${this.turtle.id}] Starting inventory dump`);
// Try to dump into containers in all directions
const keepItems = [...KEEP_ITEMS].map(n => `"${n}"`).join(', ');
const result = await this.exec(`
local keepItems = {${keepItems}}
local keepSet = {}
for _, name in ipairs(keepItems) do keepSet[name] = true end
local dropFns = {
turtle.drop,
turtle.dropUp,
turtle.dropDown,
}
local dumpedCount = 0
-- Try each direction
for _, dropFn in ipairs(dropFns) do
for slot = 1, 16 do
local item = turtle.getItemDetail(slot)
if item and not keepSet[item.name] then
turtle.select(slot)
if dropFn() then
dumpedCount = dumpedCount + item.count
end
end
end
end
turtle.select(1)
return {dumped = dumpedCount}
`);
if (result) {
console.log(`[${this.turtle.id}] Dumped ${result.dumped} items`);
}
yield;
// Update inventory after dumping
await this.getInventory();
// Transition to next state
if (this.returnState) {
this.turtle.setState(this.returnState, this.returnData);
} else {
this.turtle.setState('idle');
}
}
getRecoveryData() {
return {
...super.getRecoveryData(),
data: {
returnState: this.returnState,
returnData: this.returnData,
},
};
}
}