From d69f9cf09e2fefb137e27a7515b20db7ffdc8409 Mon Sep 17 00:00:00 2001 From: Tanner Collin Date: Sun, 25 Apr 2021 00:33:55 +0000 Subject: [PATCH] Don't sleep in occupied beds --- mosfet/jobs/sleep_with_bed.py | 38 +++++++++++++++++++++++++---------- mosfet/world.py | 10 +++++++++ 2 files changed, 37 insertions(+), 11 deletions(-) diff --git a/mosfet/jobs/sleep_with_bed.py b/mosfet/jobs/sleep_with_bed.py index b4ae799..fa3a093 100644 --- a/mosfet/jobs/sleep_with_bed.py +++ b/mosfet/jobs/sleep_with_bed.py @@ -42,8 +42,13 @@ class SleepWithBedStates: self.beds = [] for bed in result: - if bed not in self.bad_beds: - self.beds.append(bed) + if bed in self.bad_beds: + continue + + if w.check_bed_occupied(bed): + continue + + self.beds.append(bed) print('Found:', self.beds) self.state = self.choose_bed @@ -84,6 +89,7 @@ class SleepWithBedStates: def going_to_bed(self): if utils.pint(self.g.pos) == self.opening: + print('At existing bed') self.my_bed = False self.g.look_at = utils.padd(self.area, path.BLOCK_BELOW) self.state = self.use_bed @@ -128,32 +134,43 @@ class SleepWithBedStates: def going_to_area(self): if utils.pint(self.g.pos) == self.opening: + print('At bed area') self.g.look_at = utils.padd(self.area, path.BLOCK_BELOW) self.state = self.place_bed def place_bed(self): + print('Placing bed') self.g.game.place_block(self.area, BlockFace.TOP) self.my_bed = True self.state = self.use_bed def use_bed(self): - if self.g.time > 12550: - print('Sleeping') - self.g.game.place_block(self.area, BlockFace.TOP) - if not self.silent: - self.g.chat.send('zzz') - self.state = self.sleep_bed + w = self.g.world + + if self.g.time < 12550: + return + + if w.check_bed_occupied(self.area): + print('Bed occupied, aborting.') + self.state = self.cleanup + return + + print('Using bed') + self.g.game.place_block(self.area, BlockFace.TOP) + if not self.silent: + self.g.chat.send('zzz') + self.state = self.sleep_bed def sleep_bed(self): w = self.g.world p = utils.pint(self.g.pos) - threats = w.find_threats(p, 30) + threats = w.find_threats(p, 10) if threats: print('Waking up due to threats:') print(threats) self.g.game.leave_bed() - self.state = self.break_bed + self.state = self.cleanup elif self.g.time < 100: print('Woke up time') self.state = self.break_bed @@ -165,7 +182,6 @@ class SleepWithBedStates: else: self.state = self.cleanup - def collect_bed(self): if not self.g.breaking: self.g.path = [utils.padd(self.area, utils.spiral(n)) for n in range(9)] diff --git a/mosfet/world.py b/mosfet/world.py index 1192e17..550fe51 100644 --- a/mosfet/world.py +++ b/mosfet/world.py @@ -259,6 +259,16 @@ class World: result.append(utils.padd(area, direction)) return result + def check_bed_occupied(self, bed): + # returns true if the bed is occupied by a player + print('Checking bed occupancy:', bed) + for player in self.g.players.values(): + ppos = utils.pint((player.x, player.y, player.z)) + if utils.phyp(bed, ppos) <= 1 and player.y - int(player.y) == 0.6875: + print('Bed is occupied by:', player, self.g.player_names[player.player_uuid]) + return True + return False + def find_cache_openings(self, area): return self.find_bed_openings(area)