From 7f04b070bb71f0ebc436fff21feaa5f7fe3e1110 Mon Sep 17 00:00:00 2001 From: Sefirosweb Date: Sat, 6 Apr 2024 01:53:53 +0200 Subject: [PATCH] Impromed behavior to get ready now calculate more correctly the items to witdraw and craft --- base-types/src/types.ts | 2 - .../src/BehaviorModules/BehaviorLoadConfig.ts | 3 -- .../BehaviorWithdrawItemChest.ts | 23 +++++++--- .../crafterJob/goCraftingTableFunction.ts | 2 +- .../crafterJob/searchAndCraftFunction.ts | 19 +++++--- .../getReady/goChestsFunctions.ts | 44 ++----------------- core/src/modules/craftModule.ts | 29 ++++++++++++ .../Chests/ConfigurebotChests.tsx | 7 --- 8 files changed, 65 insertions(+), 64 deletions(-) diff --git a/base-types/src/types.ts b/base-types/src/types.ts index 1da46af..afe071c 100644 --- a/base-types/src/types.ts +++ b/base-types/src/types.ts @@ -110,7 +110,6 @@ export type Config = { itemsCanBeEat: Array helpFriends: boolean randomFarmArea: boolean, - firstPickUpItemsFromKnownChests: boolean, patrol: Array chests: Array plantAreas: Array @@ -353,7 +352,6 @@ export const defaultConfig: Config = { sleepArea: undefined, canPlaceBlocks: false, allowSprinting: false, - firstPickUpItemsFromKnownChests: true, canCraftItemWithdrawChest: true, itemsToBeReady: [ { diff --git a/core/src/BehaviorModules/BehaviorLoadConfig.ts b/core/src/BehaviorModules/BehaviorLoadConfig.ts index 124dba7..4172efa 100644 --- a/core/src/BehaviorModules/BehaviorLoadConfig.ts +++ b/core/src/BehaviorModules/BehaviorLoadConfig.ts @@ -25,7 +25,6 @@ export default class BehaviorLoadConfig implements StateBehavior { plantAreas: Config['plantAreas'] itemsCanBeEat: Config['itemsCanBeEat'] itemsToBeReady: Config['itemsToBeReady'] - firstPickUpItemsFromKnownChests: Config['firstPickUpItemsFromKnownChests'] constructor(bot: Bot, targets: LegionStateMachineTargets) { this.bot = bot @@ -47,8 +46,6 @@ export default class BehaviorLoadConfig implements StateBehavior { this.plantAreas = [] this.itemsCanBeEat = [] this.itemsToBeReady = [] - this.firstPickUpItemsFromKnownChests = false - } onStateEntered() { diff --git a/core/src/BehaviorModules/BehaviorWithdrawItemChest.ts b/core/src/BehaviorModules/BehaviorWithdrawItemChest.ts index f18b0b9..0749bf3 100644 --- a/core/src/BehaviorModules/BehaviorWithdrawItemChest.ts +++ b/core/src/BehaviorModules/BehaviorWithdrawItemChest.ts @@ -19,6 +19,7 @@ export default class BehaviorWithdrawItemChest implements StateBehavior { isEndFinished: boolean timeLimit?: ReturnType items: Array + itemIndex: number constructor(bot: Bot, targets: LegionStateMachineTargets) { this.active = false @@ -28,11 +29,13 @@ export default class BehaviorWithdrawItemChest implements StateBehavior { this.isEndFinished = false this.items = [] + this.itemIndex = 0 } onStateEntered() { this.isEndFinished = false - this.items = [...this.targets.items] + this.items = this.targets.items + this.itemIndex = 0 botWebsocket.log('Items to withdraw ' + JSON.stringify(this.items)) this.timeLimit = setTimeout(() => { @@ -93,11 +96,11 @@ export default class BehaviorWithdrawItemChest implements StateBehavior { } withdrawItem(container: Chest): Promise { - if (this.items.length === 0) { + if (this.items.length === this.itemIndex) { return Promise.resolve() } - const itemToWithdraw: ChestTransaction = this.items.shift() as ChestTransaction + const itemToWithdraw: ChestTransaction = this.items[this.itemIndex] as ChestTransaction if (itemToWithdraw.fromSlot !== undefined) { return this.withdrawItemFromSlot(itemToWithdraw, container) } @@ -107,6 +110,7 @@ export default class BehaviorWithdrawItemChest implements StateBehavior { : container.containerItems().filter(i => itemToWithdraw.name && i.name.includes(itemToWithdraw.name)) if (foundItems.length === 0) { + this.itemIndex++ return this.withdrawItem(container) } @@ -115,7 +119,11 @@ export default class BehaviorWithdrawItemChest implements StateBehavior { return container.withdraw(foundItems[0].type, null, quantity) - .then(() => this.withdrawItem(container)) + .then(() => { + this.items[this.itemIndex].quantity -= quantity + this.itemIndex++ + return this.withdrawItem(container) + }) .catch((err: Error) => Promise.reject(err)) } @@ -130,6 +138,7 @@ export default class BehaviorWithdrawItemChest implements StateBehavior { : container.containerItems().find(i => itemToWithdraw.name && i.name.includes(itemToWithdraw.name)) if (!foundItem) { + this.itemIndex++ return this.withdrawItem(container) } @@ -148,7 +157,11 @@ export default class BehaviorWithdrawItemChest implements StateBehavior { } return this.bot.transfer(options) - .then(() => this.withdrawItem(container)) + .then(() => { + this.items[this.itemIndex].quantity -= quantity + this.itemIndex++ + return this.withdrawItem(container) + }) .catch((err: Error) => Promise.reject(err)) } } diff --git a/core/src/NestedStateModules/crafterJob/goCraftingTableFunction.ts b/core/src/NestedStateModules/crafterJob/goCraftingTableFunction.ts index ff0d1a8..de1cfdd 100644 --- a/core/src/NestedStateModules/crafterJob/goCraftingTableFunction.ts +++ b/core/src/NestedStateModules/crafterJob/goCraftingTableFunction.ts @@ -25,7 +25,7 @@ function goCraftingTableFunction(bot: Bot, targets: LegionStateMachineTargets) { checkCraftingTable.y = 113 const goTable = new BehaviorMoveTo(bot, targets) - goTable.stateName = 'Go crafting table' + goTable.stateName = 'Move to crafting table' goTable.movements = targets.movements goTable.x = 325 goTable.y = 213 diff --git a/core/src/NestedStateModules/crafterJob/searchAndCraftFunction.ts b/core/src/NestedStateModules/crafterJob/searchAndCraftFunction.ts index a948ec2..70497d6 100644 --- a/core/src/NestedStateModules/crafterJob/searchAndCraftFunction.ts +++ b/core/src/NestedStateModules/crafterJob/searchAndCraftFunction.ts @@ -22,7 +22,7 @@ export default (bot: Bot, targets: LegionStateMachineTargets) => { exit.y = 575 const checkRecipes = new BehaviorIdle() - checkRecipes.stateName = 'checkRecipes' + checkRecipes.stateName = 'Check recipes and pickup items' checkRecipes.x = 625 checkRecipes.y = 113 @@ -37,12 +37,12 @@ export default (bot: Bot, targets: LegionStateMachineTargets) => { checkMaterials.y = 350 const checkPickUpItems = new BehaviorIdle() - checkPickUpItems.stateName = 'checkPickUpItems' + checkPickUpItems.stateName = 'Check if need pickup items' checkPickUpItems.x = 350 checkPickUpItems.y = 213 const checkCraftingTable = new BehaviorIdle() - checkCraftingTable.stateName = 'checkCraftingTable' + checkCraftingTable.stateName = 'Check need crafting table' checkCraftingTable.x = 125 checkCraftingTable.y = 350 @@ -62,7 +62,7 @@ export default (bot: Bot, targets: LegionStateMachineTargets) => { goTable.y = 462 const goTableToCraft = GoCraftingTableFunction(bot, targets) - goTableToCraft.stateName = 'Go to Craft' + goTableToCraft.stateName = 'Go to crafting table' goTableToCraft.x = 325 goTableToCraft.y = 350 @@ -82,6 +82,15 @@ export default (bot: Bot, targets: LegionStateMachineTargets) => { shouldTransition: () => true }), + new StateTransition({ + parent: checkRecipes, + child: pickUpItems, + onTransition: () => { + targets.pickUpItems = itemsToPickUpBatch.itemToPickup + }, + shouldTransition: () => itemsToPickUpBatch.itemToPickup.length > 0 && itemsToPickUpBatch.repicesUsed.length === 0 + }), + new StateTransition({ parent: checkRecipes, child: checkMaterials, @@ -122,7 +131,7 @@ export default (bot: Bot, targets: LegionStateMachineTargets) => { new StateTransition({ parent: checkPickUpItems, child: pickUpItems, - shouldTransition: () => { return targets.pickUpItems.length > 0 } + shouldTransition: () => targets.pickUpItems.length > 0 }), new StateTransition({ diff --git a/core/src/NestedStateModules/getReady/goChestsFunctions.ts b/core/src/NestedStateModules/getReady/goChestsFunctions.ts index b6294ba..8d92955 100644 --- a/core/src/NestedStateModules/getReady/goChestsFunctions.ts +++ b/core/src/NestedStateModules/getReady/goChestsFunctions.ts @@ -5,7 +5,6 @@ import BehaviorDepositItemChest from '@/BehaviorModules/BehaviorDepositItemChest import BehaviorCheckItemsInInventory from '@/BehaviorModules/BehaviorCheckItemsInInventory' import BehaviorMoveTo from '@/BehaviorModules/BehaviorMoveTo' import chestModule from '@/modules/chestModule' -import PickUpItems from '@/NestedStateModules/getReady/pickUpItems' import SearchAndCraftFunction from '@/NestedStateModules/crafterJob/searchAndCraftFunction' import { Chest, Item, LegionStateMachineTargets } from 'base-types' import { Bot } from 'mineflayer' @@ -59,11 +58,6 @@ export default (bot: Bot, targets: LegionStateMachineTargets) => { depositItems.x = 525 depositItems.y = 363 - const pickUpItems = PickUpItems(bot, targets) - pickUpItems.stateName = 'Pick Up Items' - pickUpItems.x = 325 - pickUpItems.y = 63 - const searchAndCraft = SearchAndCraftFunction(bot, targets); searchAndCraft.x = 75 searchAndCraft.y = 363 @@ -75,36 +69,12 @@ export default (bot: Bot, targets: LegionStateMachineTargets) => { const transitions = [ new StateTransition({ parent: start, - child: loadConfig, - shouldTransition: () => true - }), - - new StateTransition({ - parent: loadConfig, child: nextCheck, onTransition: () => { chestIndex = 0 chests = structuredClone(bot.config.chests) }, - shouldTransition: () => !bot.config.firstPickUpItemsFromKnownChests - }), - - new StateTransition({ - parent: loadConfig, - child: pickUpItems, - name: 'Is enabled first pickup items from know chests', - onTransition: () => { - chestIndex = 0 - chests = structuredClone(bot.config.chests) - targets.pickUpItems = findChestsToWithdraw(chests, targets.chests) - }, - shouldTransition: () => bot.config.firstPickUpItemsFromKnownChests - }), - - new StateTransition({ - parent: pickUpItems, - child: nextCheck, - shouldTransition: () => pickUpItems.isFinished() + shouldTransition: () => true }), new StateTransition({ @@ -156,16 +126,8 @@ export default (bot: Bot, targets: LegionStateMachineTargets) => { parent: withdrawItems, child: checkCraftItem, onTransition: () => { - const itemInChest = chests[chestIndex].items - - itemsToCraft = [] - - itemInChest.forEach(ic => { - if (ic.quantity > 0) { - itemsToCraft.push(ic) - } - }); - + //@ts-ignore + itemsToCraft = targets.items.filter(i => i.quantity > 0) }, shouldTransition: () => withdrawItems.isFinished() }), diff --git a/core/src/modules/craftModule.ts b/core/src/modules/craftModule.ts index 1ea3244..bf22a25 100644 --- a/core/src/modules/craftModule.ts +++ b/core/src/modules/craftModule.ts @@ -220,6 +220,32 @@ const craftModule = (bot: Bot) => { return itemsToReturn } + const checkIfItemIsOtherChest = (item: Item, allItemsToPickUp: Array, sharedChests: Chests) => { + Object.entries(sharedChests) + .forEach(([chestIndex, chest]) => { + chest.slots.every((slot, slotIndex) => { + if (item.quantity === 0) return false + if (!slot) return true + if (slot.name !== item.name) return true + if (slot.count <= 0) return true; + + const quantity = slot.count < item.quantity ? slot.count : item.quantity + item.quantity -= quantity + slot.count -= quantity + + allItemsToPickUp.push({ + fromChest: chestIndex, + fromSlot: slotIndex, + id: slot.type, + quantity + }) + + }) + }) + + return 0; + } + const getItemsToPickUpBatch = (InputItemsToCraft: Array, InputSharedChests: Chests): GetItemsToPickUpBatch => { const itemsToCraft = _.cloneDeep(InputItemsToCraft) let sharedChests: Chests = _.cloneDeep(InputSharedChests) @@ -233,6 +259,9 @@ const craftModule = (bot: Bot) => { itemsToCraft.forEach(itemToCraft => { if (!itemToCraft.name) return + checkIfItemIsOtherChest(itemToCraft, allItemsToPickUp, sharedChests) + if (itemToCraft.quantity === 0) return + const resultItemToPickup = getItemsToPickUp( itemToCraft.name, sharedChests, diff --git a/web/src/pages/ConfigureBot/Chests/ConfigurebotChests.tsx b/web/src/pages/ConfigureBot/Chests/ConfigurebotChests.tsx index e71d9dd..55fa2b3 100644 --- a/web/src/pages/ConfigureBot/Chests/ConfigurebotChests.tsx +++ b/web/src/pages/ConfigureBot/Chests/ConfigurebotChests.tsx @@ -95,13 +95,6 @@ export const ConfigurebotChests: React.FC = () => { return ( <> - updateConfig("firstPickUpItemsFromKnownChests", !botConfig.firstPickUpItemsFromKnownChests)} - />