Detect monsters and flee to safety
This commit is contained in:
@@ -20,6 +20,8 @@ import items
|
||||
importlib.reload(items)
|
||||
import data
|
||||
importlib.reload(data)
|
||||
import mobs
|
||||
importlib.reload(mobs)
|
||||
|
||||
|
||||
class FindGappleStates:
|
||||
@@ -308,12 +310,13 @@ class GatherSandStates:
|
||||
w = self.g.world
|
||||
|
||||
print('using origin', self.origin)
|
||||
s = w.find_sand_slice(self.origin, 50, self.bad_slices)
|
||||
print('Found slice:', s)
|
||||
start = time.time()
|
||||
self.prev_layer, s = w.find_sand_slice(self.origin, 200, 10, self.bad_slices, self.prev_layer)
|
||||
print('Found slice:', s, 'in', time.time() - start, 'seconds')
|
||||
|
||||
if s:
|
||||
self.slice = s
|
||||
#self.bad_slices.append(s)
|
||||
self.bad_slices.append(s)
|
||||
self.state = self.find_new_sand
|
||||
else:
|
||||
print('No slices remaining.')
|
||||
@@ -323,24 +326,23 @@ class GatherSandStates:
|
||||
print('Finding new sand...')
|
||||
w = self.g.world
|
||||
p = utils.pint(self.g.pos)
|
||||
head = utils.padd(p, path.BLOCK_ABOVE)
|
||||
|
||||
sand = w.find_sand(self.slice, 2, p)
|
||||
print('Found sand:', sand)
|
||||
|
||||
if not len(sand):
|
||||
for sand in w.find_sand(self.slice, 2, p):
|
||||
if sand not in self.bad_sand:
|
||||
print('Found sand:', sand)
|
||||
break
|
||||
else: # for
|
||||
print('No good sands left, aborting.')
|
||||
self.state = self.cleanup
|
||||
return
|
||||
|
||||
for check in sand:
|
||||
if check in self.bad_sand:
|
||||
continue
|
||||
self.sand = check
|
||||
break
|
||||
self.sand = sand
|
||||
|
||||
if utils.phyp(p, self.sand) > blocks.BREAK_DISTANCE:
|
||||
self.state = self.nav_to_sand
|
||||
else:
|
||||
if utils.phyp(head, self.sand) < blocks.BREAK_DISTANCE:
|
||||
self.state = self.dig_sand
|
||||
else:
|
||||
self.state = self.nav_to_sand
|
||||
|
||||
def nav_to_sand(self):
|
||||
w = self.g.world
|
||||
@@ -356,6 +358,7 @@ class GatherSandStates:
|
||||
self.g.path = navpath[:-1]
|
||||
self.state = self.going_to_sand
|
||||
else:
|
||||
print('Cant get to that sand')
|
||||
self.bad_sand.append(self.sand)
|
||||
self.state = self.find_new_sand
|
||||
|
||||
@@ -387,8 +390,10 @@ class GatherSandStates:
|
||||
self.state = self.idle
|
||||
|
||||
self.origin = utils.pint(self.g.pos)
|
||||
self.origin = (2019, 64, 238)
|
||||
self.slice = None
|
||||
self.bad_slices = []
|
||||
self.prev_layer = 0
|
||||
self.sand = None
|
||||
self.bad_sand = []
|
||||
self.wait_time = 0
|
||||
@@ -519,7 +524,7 @@ class SleepWithBedStates:
|
||||
|
||||
def going_to_area(self):
|
||||
if utils.pint(self.g.pos) == self.opening:
|
||||
self.g.look_at = self.area
|
||||
self.g.look_at = utils.padd(self.area, path.BLOCK_BELOW)
|
||||
self.state = self.place_bed
|
||||
|
||||
def place_bed(self):
|
||||
@@ -535,8 +540,16 @@ class SleepWithBedStates:
|
||||
self.state = self.sleep_bed
|
||||
|
||||
def sleep_bed(self):
|
||||
if self.g.time < 100:
|
||||
print('Woke up')
|
||||
w = self.g.world
|
||||
p = utils.pint(self.g.pos)
|
||||
|
||||
threats = w.find_threats(p, 30)
|
||||
if threats:
|
||||
print('Waking up due to threats')
|
||||
self.g.game.leave_bed()
|
||||
self.state = self.break_bed
|
||||
elif self.g.time < 100:
|
||||
print('Woke up time')
|
||||
self.state = self.break_bed
|
||||
|
||||
def break_bed(self):
|
||||
@@ -1004,6 +1017,96 @@ class GrabSaplingStates:
|
||||
self.state()
|
||||
|
||||
|
||||
class CheckThreatsStates:
|
||||
def idle(self):
|
||||
return None
|
||||
|
||||
def init(self):
|
||||
self.state = self.find_threats
|
||||
print('Checking for threats')
|
||||
|
||||
def find_threats(self):
|
||||
w = self.g.world
|
||||
p = utils.pint(self.g.pos)
|
||||
|
||||
threats = w.find_threats(p, 40)
|
||||
|
||||
if threats:
|
||||
print('Found', len(threats), 'threats, fleeing')
|
||||
self.state = self.find_safety
|
||||
else:
|
||||
print('Aborting, no threats')
|
||||
self.state = self.cleanup
|
||||
|
||||
def find_safety(self):
|
||||
w = self.g.world
|
||||
p = utils.pint(self.g.pos)
|
||||
|
||||
safety = w.find_blocks_indexed(p, [blocks.EMERALD_BLOCK])
|
||||
|
||||
if not safety:
|
||||
print('No emerald blocks found, aborting')
|
||||
self.state = self.cleanup
|
||||
return
|
||||
|
||||
safety.sort(key=lambda s: utils.phyp(p, s))
|
||||
print('Found emerald blocks:', safety)
|
||||
|
||||
for s in safety:
|
||||
s = utils.padd(s, path.BLOCK_ABOVE)
|
||||
navpath = w.path_to_place(p, s)
|
||||
|
||||
if navpath:
|
||||
self.g.path = navpath
|
||||
self.state = self.going_to_safety
|
||||
self.safety = s
|
||||
print('Going to safety', self.safety)
|
||||
return
|
||||
else:
|
||||
print('Cant get to safety', self.safety)
|
||||
|
||||
print('Cant get to safety, aborting')
|
||||
self.state = self.cleanup
|
||||
|
||||
def going_to_safety(self):
|
||||
if utils.pint(self.g.pos) == self.safety:
|
||||
print('At safety spot, waiting to be moved')
|
||||
self.state = self.wait_for_move
|
||||
|
||||
def wait_for_move(self):
|
||||
# wait for the server to move the bot when it's safe
|
||||
# ie. a piston + daylight sensor
|
||||
if utils.pint(self.g.pos) != self.safety:
|
||||
print('Moved, resuming job')
|
||||
self.state = self.wait
|
||||
self.wait_time = 3
|
||||
|
||||
def wait(self):
|
||||
# wait to land, etc
|
||||
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.safety = None
|
||||
self.wait_time = 0
|
||||
|
||||
def run(self):
|
||||
self.state()
|
||||
|
||||
|
||||
class JobStates:
|
||||
def idle(self):
|
||||
return []
|
||||
@@ -1018,6 +1121,7 @@ class JobStates:
|
||||
self.clear_leaves_states = ClearLeavesStates(self.g)
|
||||
self.grab_sapling_states = GrabSaplingStates(self.g)
|
||||
self.grab_sand_states = GrabSandStates(self.g)
|
||||
self.check_threats_states = CheckThreatsStates(self.g)
|
||||
|
||||
def run_machines(self, machines):
|
||||
for m in machines:
|
||||
@@ -1040,6 +1144,18 @@ class JobStates:
|
||||
]
|
||||
return machines
|
||||
|
||||
def farm_sand(self):
|
||||
machines = [
|
||||
self.check_threats_states,
|
||||
self.gather_sand_states,
|
||||
self.grab_sand_states,
|
||||
self.cache_items_states,
|
||||
self.sleep_with_bed_states,
|
||||
]
|
||||
self.sleep_with_bed_states.silent = True
|
||||
self.cache_items_states.silent = True
|
||||
return machines
|
||||
|
||||
def cache_items(self):
|
||||
machines = [
|
||||
self.cache_items_states,
|
||||
@@ -1070,7 +1186,6 @@ class JobStates:
|
||||
self.sleep_with_bed_states,
|
||||
self.cache_items_states,
|
||||
]
|
||||
|
||||
self.sleep_with_bed_states.silent = True
|
||||
self.cache_items_states.silent = True
|
||||
return machines
|
||||
|
||||
Reference in New Issue
Block a user