Don't sleep in occupied beds

This commit is contained in:
Tanner Collin 2021-04-25 00:33:55 +00:00
parent 8b85de2b2a
commit d69f9cf09e
2 changed files with 37 additions and 11 deletions

View File

@ -42,7 +42,12 @@ class SleepWithBedStates:
self.beds = [] self.beds = []
for bed in result: for bed in result:
if bed not in self.bad_beds: if bed in self.bad_beds:
continue
if w.check_bed_occupied(bed):
continue
self.beds.append(bed) self.beds.append(bed)
print('Found:', self.beds) print('Found:', self.beds)
@ -84,6 +89,7 @@ class SleepWithBedStates:
def going_to_bed(self): def going_to_bed(self):
if utils.pint(self.g.pos) == self.opening: if utils.pint(self.g.pos) == self.opening:
print('At existing bed')
self.my_bed = False self.my_bed = False
self.g.look_at = utils.padd(self.area, path.BLOCK_BELOW) self.g.look_at = utils.padd(self.area, path.BLOCK_BELOW)
self.state = self.use_bed self.state = self.use_bed
@ -128,17 +134,28 @@ class SleepWithBedStates:
def going_to_area(self): def going_to_area(self):
if utils.pint(self.g.pos) == self.opening: if utils.pint(self.g.pos) == self.opening:
print('At bed area')
self.g.look_at = utils.padd(self.area, path.BLOCK_BELOW) self.g.look_at = utils.padd(self.area, path.BLOCK_BELOW)
self.state = self.place_bed self.state = self.place_bed
def place_bed(self): def place_bed(self):
print('Placing bed')
self.g.game.place_block(self.area, BlockFace.TOP) self.g.game.place_block(self.area, BlockFace.TOP)
self.my_bed = True self.my_bed = True
self.state = self.use_bed self.state = self.use_bed
def use_bed(self): def use_bed(self):
if self.g.time > 12550: w = self.g.world
print('Sleeping')
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) self.g.game.place_block(self.area, BlockFace.TOP)
if not self.silent: if not self.silent:
self.g.chat.send('zzz') self.g.chat.send('zzz')
@ -148,12 +165,12 @@ class SleepWithBedStates:
w = self.g.world w = self.g.world
p = utils.pint(self.g.pos) p = utils.pint(self.g.pos)
threats = w.find_threats(p, 30) threats = w.find_threats(p, 10)
if threats: if threats:
print('Waking up due to threats:') print('Waking up due to threats:')
print(threats) print(threats)
self.g.game.leave_bed() self.g.game.leave_bed()
self.state = self.break_bed self.state = self.cleanup
elif self.g.time < 100: elif self.g.time < 100:
print('Woke up time') print('Woke up time')
self.state = self.break_bed self.state = self.break_bed
@ -165,7 +182,6 @@ class SleepWithBedStates:
else: else:
self.state = self.cleanup self.state = self.cleanup
def collect_bed(self): def collect_bed(self):
if not self.g.breaking: if not self.g.breaking:
self.g.path = [utils.padd(self.area, utils.spiral(n)) for n in range(9)] self.g.path = [utils.padd(self.area, utils.spiral(n)) for n in range(9)]

View File

@ -259,6 +259,16 @@ class World:
result.append(utils.padd(area, direction)) result.append(utils.padd(area, direction))
return result 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): def find_cache_openings(self, area):
return self.find_bed_openings(area) return self.find_bed_openings(area)