Skip to content

Commit

Permalink
Impromed behavior to get ready now calculate more correctly the items…
Browse files Browse the repository at this point in the history
… to witdraw and craft
  • Loading branch information
sefirosweb committed Apr 5, 2024
1 parent bb0cf99 commit 7f04b07
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 64 deletions.
2 changes: 0 additions & 2 deletions base-types/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ export type Config = {
itemsCanBeEat: Array<string>
helpFriends: boolean
randomFarmArea: boolean,
firstPickUpItemsFromKnownChests: boolean,
patrol: Array<Vec3>
chests: Array<Chest>
plantAreas: Array<PlantArea>
Expand Down Expand Up @@ -353,7 +352,6 @@ export const defaultConfig: Config = {
sleepArea: undefined,
canPlaceBlocks: false,
allowSprinting: false,
firstPickUpItemsFromKnownChests: true,
canCraftItemWithdrawChest: true,
itemsToBeReady: [
{
Expand Down
3 changes: 0 additions & 3 deletions core/src/BehaviorModules/BehaviorLoadConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -47,8 +46,6 @@ export default class BehaviorLoadConfig implements StateBehavior {
this.plantAreas = []
this.itemsCanBeEat = []
this.itemsToBeReady = []
this.firstPickUpItemsFromKnownChests = false

}

onStateEntered() {
Expand Down
23 changes: 18 additions & 5 deletions core/src/BehaviorModules/BehaviorWithdrawItemChest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export default class BehaviorWithdrawItemChest implements StateBehavior {
isEndFinished: boolean
timeLimit?: ReturnType<typeof setTimeout>
items: Array<ChestTransaction>
itemIndex: number

constructor(bot: Bot, targets: LegionStateMachineTargets) {
this.active = false
Expand All @@ -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(() => {
Expand Down Expand Up @@ -93,11 +96,11 @@ export default class BehaviorWithdrawItemChest implements StateBehavior {
}

withdrawItem(container: Chest): Promise<void> {
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)
}
Expand All @@ -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)
}

Expand All @@ -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))
}

Expand All @@ -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)
}

Expand All @@ -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))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 14 additions & 5 deletions core/src/NestedStateModules/crafterJob/searchAndCraftFunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand All @@ -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

Expand All @@ -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,
Expand Down Expand Up @@ -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({
Expand Down
44 changes: 3 additions & 41 deletions core/src/NestedStateModules/getReady/goChestsFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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
Expand All @@ -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({
Expand Down Expand Up @@ -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()
}),
Expand Down
29 changes: 29 additions & 0 deletions core/src/modules/craftModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,32 @@ const craftModule = (bot: Bot) => {
return itemsToReturn
}

const checkIfItemIsOtherChest = (item: Item, allItemsToPickUp: Array<ChestTransaction>, 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<Item>, InputSharedChests: Chests): GetItemsToPickUpBatch => {
const itemsToCraft = _.cloneDeep(InputItemsToCraft)
let sharedChests: Chests = _.cloneDeep(InputSharedChests)
Expand All @@ -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,
Expand Down
7 changes: 0 additions & 7 deletions web/src/pages/ConfigureBot/Chests/ConfigurebotChests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,6 @@ export const ConfigurebotChests: React.FC = () => {

return (
<>
<Form.Check
type="switch"
id="firstPickUpItemsFromKnownChests"
label="Use a memorized chest first?"
checked={botConfig.firstPickUpItemsFromKnownChests}
onChange={() => updateConfig("firstPickUpItemsFromKnownChests", !botConfig.firstPickUpItemsFromKnownChests)}
/>
<Form.Check
type="switch"
id="canCraftItemWithdrawChest"
Expand Down

0 comments on commit 7f04b07

Please sign in to comment.