minecraft-bot/mosfet/jobs/sleep_with_bed.py

230 lines
6.0 KiB
Python
Raw Normal View History

2021-04-19 01:34:54 +00:00
import re
import time
import importlib
import random
from itertools import count
from math import floor
2021-04-19 01:34:54 +00:00
from minecraft.networking.types import BlockFace
2021-04-22 00:46:54 +00:00
from mosfet.protocol.managers import ChunkNotLoadedException
2021-04-19 01:34:54 +00:00
2021-04-22 00:46:54 +00:00
from mosfet import utils
from mosfet import path
from mosfet.info import blocks
from mosfet.info import items
from mosfet.info import mcdata
from mosfet.info import mobs
2021-04-19 01:34:54 +00:00
class SleepWithBedStates:
def idle(self):
return None
def init(self):
if self.g.time < 12000:
print('Aborting sleep, not night')
self.state = self.cleanup
return
if self.g.dimension != 'overworld':
print('Aborting sleep, not in overworld')
self.state = self.cleanup
return
self.state = self.find_beds
def find_beds(self):
print('Finding beds...')
w = self.g.world
p = utils.pint(self.g.pos)
2021-04-24 22:34:25 +00:00
result = w.find_blocks_indexed(p, blocks.BED_IDS, 80)
2021-04-19 01:34:54 +00:00
self.beds = []
for bed in result:
2021-04-25 00:33:55 +00:00
if bed in self.bad_beds:
continue
if w.check_bed_occupied(bed):
continue
self.beds.append(bed)
2021-04-19 01:34:54 +00:00
print('Found:', self.beds)
self.state = self.choose_bed
def choose_bed(self):
print('Choosing a bed...')
w = self.g.world
p = utils.pint(self.g.pos)
c = self.g.chunks
if not len(self.beds):
print('No beds')
self.state = self.select_bed
return
bed = self.beds[0]
print('Chose:', bed)
navpath = w.path_to_place_faked(p, bed)
2021-04-19 01:34:54 +00:00
print('navpath:', navpath)
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
else:
self.beds.pop(0)
self.bad_beds.append(bed)
print('Cant get to bed, blacklisting')
time.sleep(0.1)
2021-04-19 01:34:54 +00:00
self.state = self.select_bed
def going_to_bed(self):
if utils.pint(self.g.pos) == self.opening:
2021-04-25 00:33:55 +00:00
print('At existing bed')
2021-04-19 01:34:54 +00:00
self.my_bed = False
self.g.look_at = utils.padd(self.area, path.BLOCK_BELOW)
self.state = self.use_bed
def select_bed(self):
if self.g.game.select_item(items.BED_IDS):
self.state = self.find_bed_spot
else:
print('No bed, aborting.')
self.state = self.cleanup
def find_bed_spot(self):
print('Finding a bed spot...')
w = self.g.world
p = utils.pint(self.g.pos)
for area in w.find_bed_areas(p, 100):
print('Found area:', area)
if area not in self.bad_areas:
break
else: # for
print('Unable to find area')
self.state = self.cleanup
return
self.area = area
openings = w.find_bed_openings(self.area)
for o in openings:
navpath = w.path_to_place(p, o)
self.opening = o
if navpath: break
else: # for
print('Unable to get to bed area', self.area)
self.bad_areas.append(self.area)
self.state = self.cleanup
return
self.g.path = navpath
self.g.look_at = self.opening
self.state = self.going_to_area
def going_to_area(self):
if utils.pint(self.g.pos) == self.opening:
2021-04-25 00:33:55 +00:00
print('At bed area')
2021-04-19 01:34:54 +00:00
self.g.look_at = utils.padd(self.area, path.BLOCK_BELOW)
self.state = self.place_bed
def place_bed(self):
2021-04-25 00:33:55 +00:00
print('Placing bed')
2021-04-19 01:34:54 +00:00
self.g.game.place_block(self.area, BlockFace.TOP)
self.my_bed = True
self.wait_time = 0.5
self.state = self.wait_use
def wait_use(self):
# wait to use the bed
if self.wait_time > 0:
self.wait_time -= utils.TICK
else:
self.state = self.use_bed
2021-04-19 01:34:54 +00:00
def use_bed(self):
2021-04-25 00:33:55 +00:00
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
2021-04-19 01:34:54 +00:00
def sleep_bed(self):
w = self.g.world
p = utils.pint(self.g.pos)
2021-04-25 00:33:55 +00:00
threats = w.find_threats(p, 10)
2021-04-25 00:59:43 +00:00
2021-04-19 01:34:54 +00:00
if threats:
print('Waking up due to threats:')
print(threats)
self.g.game.leave_bed()
2021-04-25 00:33:55 +00:00
self.state = self.cleanup
2021-04-19 01:34:54 +00:00
elif self.g.time < 100:
print('Woke up time')
self.state = self.break_bed
elif self.g.correction_count:
print('Woke up by movement')
self.state = self.break_bed
2021-04-19 01:34:54 +00:00
def break_bed(self):
if self.my_bed:
self.g.game.break_block(self.area)
self.state = self.collect_bed
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)]
self.wait_time = 2
self.state = self.wait
def wait(self):
# wait to pick up bed
if self.wait_time > 0:
self.wait_time -= utils.TICK
else:
self.state = self.cleanup
def cleanup(self):
self.g.look_at = None
self.state = self.done
def done(self):
# never gets ran, placeholder
return None
def __init__(self, global_state):
self.g = global_state
self.state = self.idle
self.silent = False
self.my_bed = False
self.beds = []
self.bad_beds = []
self.area = None
self.opening = None
self.bad_areas = []
self.wait_time = 0
def run(self):
self.state()