From fc929db6589e68409efd364f8b6d988d07e3dc40 Mon Sep 17 00:00:00 2001 From: Tanner Collin Date: Sat, 17 Apr 2021 23:48:38 +0000 Subject: [PATCH] Manage needed items and item counts better --- bot.py | 3 +++ game.py | 24 ++++++++++++++++- items.py | 16 ++++++++++-- jobs.py | 79 ++++++++++++++++++++++++++++++++++++++++++++------------ 4 files changed, 102 insertions(+), 20 deletions(-) diff --git a/bot.py b/bot.py index bfcb2ea..e1271f6 100644 --- a/bot.py +++ b/bot.py @@ -226,6 +226,9 @@ def init(global_state): g.filling = False + g.minimum_cache_slots = 33 + g.maximum_supply_slots = 27 + def bot(global_state): g = global_state diff --git a/game.py b/game.py index 1ff35b3..cda3081 100644 --- a/game.py +++ b/game.py @@ -848,7 +848,7 @@ class Game: if authed: if command == 'print': - data = data.replace('^', '.') + data = data.replace('`', '.') reply = str(eval(data)) if command == 'exit': @@ -964,6 +964,28 @@ class Game: count += item.item_count return count + def count_inventory_slots(self): + # count how many inventory slots are filled + # excludes armour, crafting slots, off-hand + count = 0 + for slot, item in self.g.inv.items(): + if item.present and slot >= 9 and slot <= 45: + count += 1 + return count + + def count_window_slots(self): + # count how many window slots are filled + # excludes player inventory + w = self.g.window + w_info = mcdata.WINDOWS[w.data.window_type] + w_container_slots = w_info.container + + count = 0 + for slot, item in w.contents.items(): + if item.present and slot in w_container_slots: + count += 1 + return count + def get_window_slot(self, item_id): # get the first slot that matches item of a window window_items = list(self.g.window.contents.items()) diff --git a/items.py b/items.py index a39a98a..37e118f 100644 --- a/items.py +++ b/items.py @@ -105,6 +105,7 @@ NETHERWART_ID = get_id('nether_wart') CARROT_ID = get_id('carrot') POTATO_ID = get_id('potato') +WHEAT_ID = get_id('wheat') WHEAT_SEEDS_ID = get_id('wheat_seeds') BEETROOT_SEEDS_ID = get_id('beetroot_seeds') PUMPKIN_ID = get_id('pumpkin') @@ -113,5 +114,16 @@ EMERALD_ID = get_id('emerald') BERRIES_ID = get_id('sweet_berries') IRON_INGOT_ID = get_id('iron_ingot') -NEEDED_ITEMS = BED_IDS | SHOVEL_IDS | AXE_IDS | FOOD_IDS | set([CHEST_ID, PUMPKIN_ID, BERRIES_ID, IRON_INGOT_ID]) -WANTED_ITEMS = SAPLING_IDS | set([NETHERWART_ID, CARROT_ID, POTATO_ID, WHEAT_SEEDS_ID, BEETROOT_SEEDS_ID]) + +INIT_NEEDED_ITEMS = BED_IDS | FOOD_IDS | set([CHEST_ID]) +NEEDED_ITEMS = INIT_NEEDED_ITEMS + +INIT_WANTED_ITEMS = set() +WANTED_ITEMS = INIT_WANTED_ITEMS + +def set_needed(items): + NEEDED_ITEMS = INIT_WANTED_ITEMS | items + +def set_wanted(items): + WANTED_ITEMS = INIT_WANTED_ITEMS | items + diff --git a/jobs.py b/jobs.py index 6fb9721..ee6ea42 100644 --- a/jobs.py +++ b/jobs.py @@ -773,6 +773,7 @@ class SleepWithBedStates: if navpath: self.g.path = navpath[:-1] self.opening = self.g.path[-1] + self.g.look_at = self.opening self.area = bed self.state = self.going_to_bed return @@ -785,7 +786,7 @@ class SleepWithBedStates: def going_to_bed(self): if utils.pint(self.g.pos) == self.opening: self.my_bed = False - self.g.look_at = self.area + self.g.look_at = utils.padd(self.area, path.BLOCK_BELOW) self.state = self.use_bed def select_bed(self): @@ -823,6 +824,7 @@ class SleepWithBedStates: return self.g.path = navpath + self.g.look_at = self.opening self.state = self.going_to_area def going_to_area(self): @@ -912,9 +914,9 @@ class CacheItemsStates: self.skip_slots = [] self.skip_items = [] - num_stacks = len([x for x in self.g.inv.values() if x.present]) + num_stacks = self.g.game.count_inventory_slots() print('Inventory amount:', num_stacks) - if num_stacks >= self.minimum: + if num_stacks >= self.g.minimum_cache_slots: self.state = self.find_trapped_chests else: print('Aborting caching, not full') @@ -1037,6 +1039,16 @@ class CacheItemsStates: w_info = mcdata.WINDOWS[w.data.window_type] w_inventory_slots = w_info.inventory + w_container_slots = w_info.container + + used_slots = self.g.game.count_window_slots() + print('used:', used_slots, 'total:', len(w_container_slots)) + if used_slots >= len(w_container_slots): + print('Container is too full, aborting') + self.g.game.close_window() + self.g.look_at = None + self.state = self.cleanup + return slot_list = [] @@ -1049,7 +1061,7 @@ class CacheItemsStates: if not slot.present: continue - if slot.item_id in self.needed_items: + if slot.item_id in items.NEEDED_ITEMS: continue if slot_num in self.skip_slots: @@ -1060,7 +1072,7 @@ class CacheItemsStates: slot_list.sort(key=lambda x: x[1].item_count, reverse=True) for slot_num, slot in slot_list: - if slot.item_id in self.wanted_items and slot.item_id not in self.skip_items: + if slot.item_id in items.WANTED_ITEMS and slot.item_id not in self.skip_items: print('skipping wanted item', slot) self.skip_slots.append(slot_num) self.skip_items.append(slot.item_id) @@ -1094,14 +1106,8 @@ class CacheItemsStates: self.g = global_state self.state = self.idle - self.minimum = 27 self.silent = False - # keep all needed items - self.needed_items = items.NEEDED_ITEMS - # keep one stack of wanted items - self.wanted_items = items.WANTED_ITEMS - self.skip_slots = [] self.skip_items = [] @@ -1121,8 +1127,16 @@ class GrabSuppliesStates: return None def init(self): + print('Started grab supplies states') self.checked_barrels = [] + used_slots = self.g.game.count_inventory_slots() + print('used:', used_slots, 'total:', self.g.maximum_supply_slots) + if used_slots >= self.g.maximum_supply_slots: + print('Inventory is too full, aborting') + self.state = self.cleanup + return + if self.supplies: self.state = self.check_supplies else: @@ -1221,12 +1235,12 @@ class GrabSuppliesStates: def choose_items(self): print('Selecting next item') for items, limits in self.supplies.items(): - minimum, maximum = limits + minimum_items, maximum_stacks = limits print('Checking items:', items) num_items = self.g.game.count_items(items) print('Have:', num_items) - if num_items >= minimum: + if num_items >= minimum_items: print('Have enough, skipping') continue @@ -1237,7 +1251,7 @@ class GrabSuppliesStates: print('Need some') self.checked_supplies.append(items) self.target_items = items - self.maximum_items = maximum + self.maximum_stacks = maximum_stacks self.count = 0 self.state = self.grab_items return @@ -1257,6 +1271,14 @@ class GrabSuppliesStates: print('Got wrong window, aborting') self.state = self.cleanup return + used_slots = self.g.game.count_inventory_slots() + print('used:', used_slots, 'total:', self.g.maximum_supply_slots) + if used_slots >= self.g.maximum_supply_slots: + print('Inventory is too full, aborting') + self.g.game.close_window() + self.g.look_at = None + self.state = self.cleanup + return w_info = mcdata.WINDOWS[w.data.window_type] w_container_slots = w_info.container @@ -1273,7 +1295,7 @@ class GrabSuppliesStates: if slot.item_id not in self.target_items: continue - if self.maximum_items and self.count >= self.maximum_items: + if self.maximum_stacks and self.count >= self.maximum_stacks: break self.count += 1 @@ -1291,7 +1313,6 @@ class GrabSuppliesStates: print('Closing barrel') self.g.game.close_window() self.g.look_at = None - self.barrels.pop(0) self.state = self.choose_barrel def cleanup(self): @@ -1312,7 +1333,7 @@ class GrabSuppliesStates: self.barrel = None self.checked_supplies = [] self.target_items = None - self.maximum_items = 0 + self.maximum_stacks = 0 self.count = 0 self.area = None self.opening = None @@ -2160,6 +2181,8 @@ class JobStates: self.grab_supplies_states.supplies = { tuple(items.SHOVEL_IDS): (1, 9), } + + items.set_needed(items.SHOVEL_IDS) return machines def cache_items(self): @@ -2200,6 +2223,9 @@ class JobStates: self.grab_supplies_states.supplies = { tuple(items.AXE_IDS): (1, 9), } + + items.set_needed(items.AXE_IDS) + items.set_wanted(items.SAPLING_IDS) return machines def farm_wart(self): @@ -2211,6 +2237,8 @@ class JobStates: ] self.sleep_with_bed_states.silent = True self.cache_items_states.silent = True + + items.set_wanted(set([items.NETHERWART_ID])) return machines def farm_crop(self): @@ -2222,6 +2250,13 @@ class JobStates: ] self.sleep_with_bed_states.silent = True self.cache_items_states.silent = True + + items.set_wanted(set([ + items.CARROT_ID, + items.POTATO_ID, + items.WHEAT_SEEDS_ID, + items.BEETROOT_SEEDS_ID, + ])) return machines def fill_blocks(self): @@ -2266,7 +2301,17 @@ class JobStates: tuple([items.PUMPKIN_ID]): (64, 9), tuple([items.BERRIES_ID]): (64, 9), tuple([items.IRON_INGOT_ID]): (64, 9), + tuple([items.WHEAT_ID]): (64, 9), + tuple([items.POTATO_ID]): (64, 9), } + + items.set_needed(set([ + items.PUMPKIN_ID, + items.BERRIES_ID, + items.IRON_INGOT_ID, + items.WHEAT_ID, + items.POTATO_ID, + ])) return machines def stop(self):