From 7d0cef0e2eeb91c74c1f5c5edcf4fd91ddd2f5d6 Mon Sep 17 00:00:00 2001 From: Tanner Collin Date: Sat, 17 Apr 2021 22:04:41 +0000 Subject: [PATCH] Fix order of grabbing supplies and checking barrels --- game.py | 3 +- jobs.py | 93 ++++++++++++++++++++++++++++++++++++++------------------- 2 files changed, 64 insertions(+), 32 deletions(-) diff --git a/game.py b/game.py index 6d44f24..1ff35b3 100644 --- a/game.py +++ b/game.py @@ -75,7 +75,8 @@ class MCWorld: continue if distance and utils.phyp(center, block) > distance: continue - result.append(block) + if block not in result: + result.append(block) result.sort(key=lambda x: utils.phyp(center, x)) return result diff --git a/jobs.py b/jobs.py index bcc6e90..6fb9721 100644 --- a/jobs.py +++ b/jobs.py @@ -743,7 +743,7 @@ class SleepWithBedStates: self.beds = [] for bed in result: - if bed not in self.beds and bed not in self.bad_beds: + if bed not in self.bad_beds: self.beds.append(bed) print('Found:', self.beds) @@ -1121,6 +1121,8 @@ class GrabSuppliesStates: return None def init(self): + self.checked_barrels = [] + if self.supplies: self.state = self.check_supplies else: @@ -1128,33 +1130,22 @@ class GrabSuppliesStates: self.state = self.cleanup def check_supplies(self): - - # TODO: only visit each barrel once - # switch the supplies and barrels steps - + # check if we need to grab anything for items, limits in self.supplies.items(): minimum, maximum = limits print('Checking items:', items) num_items = self.g.game.count_items(items) print('Have:', num_items) - if items in self.checked_supplies: - print('Already checked, skipping') - continue - if num_items >= minimum: print('Have enough, skipping') continue - self.target_items = items - self.checked_supplies.append(items) - self.maximum_items = maximum - self.count = 0 + print('Need at least one item') self.state = self.find_barrels return - self.checked_supplies = [] - print('Aborting, don\'t need any more supplies') + print('Aborting, dont need any supplies') self.state = self.cleanup def find_barrels(self): @@ -1168,16 +1159,26 @@ class GrabSuppliesStates: def choose_barrel(self): print('Choosing a barrel...') + for barrel in self.barrels: + if barrel in self.checked_barrels: + continue + if barrel in self.bad_barrels: + continue + + self.barrel = barrel + self.state = self.path_to_barrel + return + + print('No barrels') + self.state = self.cleanup + + def path_to_barrel(self): + print('Finding path to barrel') w = self.g.world p = utils.pint(self.g.pos) c = self.g.chunks - if not len(self.barrels): - print('No barrels') - self.state = self.no_more_barrels - return - - barrel = self.barrels[0] + barrel = self.barrel tmp = c.get_block_at(*barrel) c.set_block_at(*barrel, blocks.AIR) @@ -1189,11 +1190,15 @@ class GrabSuppliesStates: if navpath: self.g.path = navpath[:-1] self.opening = self.g.path[-1] + self.checked_barrels.append(barrel) self.area = barrel self.state = self.going_to_barrel + self.checked_supplies = [] return else: - self.barrels.pop(0) + print('No path, blacklisting barrel') + self.bad_barrels.append(barrel) + self.state = self.choose_barrel def going_to_barrel(self): if utils.pint(self.g.pos) == self.opening: @@ -1207,11 +1212,38 @@ class GrabSuppliesStates: self.state = self.wait def wait(self): - # wait for server to send us barrel contents + # wait for server to send us inventory contents if self.wait_time > 0: self.wait_time -= utils.TICK else: + self.state = self.choose_items + + def choose_items(self): + print('Selecting next item') + for items, limits in self.supplies.items(): + minimum, maximum = limits + print('Checking items:', items) + num_items = self.g.game.count_items(items) + print('Have:', num_items) + + if num_items >= minimum: + print('Have enough, skipping') + continue + + if items in self.checked_supplies: + print('Already checked, skipping') + continue + + print('Need some') + self.checked_supplies.append(items) + self.target_items = items + self.maximum_items = maximum + self.count = 0 self.state = self.grab_items + return + + print('Aborting, dont need any more supplies') + self.state = self.close_barrel def grab_items(self): if self.g.item_lock: return @@ -1251,8 +1283,9 @@ class GrabSuppliesStates: self.g.game.click_window(slot_num, 0, 1, slot) return - print('Nothing left to move') - self.state = self.close_barrel + print('None left to move') + self.wait_time = 0.25 + self.state = self.wait def close_barrel(self): print('Closing barrel') @@ -1261,9 +1294,6 @@ class GrabSuppliesStates: self.barrels.pop(0) self.state = self.choose_barrel - def no_more_barrels(self): - self.state = self.check_supplies - def cleanup(self): self.state = self.done @@ -1275,14 +1305,15 @@ class GrabSuppliesStates: self.g = global_state self.state = self.idle - self.checked_supplies = [] self.supplies = {} self.barrels = [] - + self.checked_barrels = [] + self.bad_barrels = [] + self.barrel = None + self.checked_supplies = [] self.target_items = None self.maximum_items = 0 self.count = 0 - self.area = None self.opening = None self.wait_time = 0