222 lines
5.8 KiB
Python
222 lines
5.8 KiB
Python
import re
|
|
import time
|
|
import importlib
|
|
import random
|
|
from itertools import count
|
|
from math import floor
|
|
|
|
from minecraft.networking.types import BlockFace
|
|
|
|
from mosfet.protocol.managers import ChunkNotLoadedException
|
|
|
|
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
|
|
|
|
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)
|
|
|
|
result = w.find_blocks_indexed(p, blocks.BED_IDS, 80)
|
|
|
|
self.beds = []
|
|
for bed in result:
|
|
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
|
|
|
|
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)
|
|
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)
|
|
self.state = self.select_bed
|
|
|
|
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
|
|
|
|
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:
|
|
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):
|
|
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, 10)
|
|
|
|
if threats:
|
|
print('Waking up due to threats:')
|
|
print(threats)
|
|
self.g.game.leave_bed()
|
|
self.state = self.cleanup
|
|
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
|
|
|
|
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()
|