Improve finding sand by using slices
This commit is contained in:
@@ -21,6 +21,8 @@ importlib.reload(items)
|
||||
import data
|
||||
importlib.reload(data)
|
||||
|
||||
BREAK_DISTANCE = 5
|
||||
|
||||
|
||||
class FindGappleStates:
|
||||
def idle(self):
|
||||
@@ -291,37 +293,62 @@ class GatherSandStates:
|
||||
return self.g.chunks.get_block_at(*p) in blocks.NON_SOLID_IDS
|
||||
|
||||
def bsand(self, p):
|
||||
return self.g.chunks.get_block_at(*p) == 66
|
||||
return self.g.chunks.get_block_at(*p) == blocks.SAND
|
||||
|
||||
def idle(self):
|
||||
return None
|
||||
|
||||
def init(self):
|
||||
self.state = self.find_new_sand
|
||||
self.state = self.find_new_slice
|
||||
|
||||
def find_new_slice(self):
|
||||
print('Finding new slice...')
|
||||
w = self.g.world
|
||||
|
||||
#o = utils.padd(self.origin, (0, 10, 0))
|
||||
print('using origin', self.origin)
|
||||
self.skip, s = w.find_sand_slice(self.origin, 50)
|
||||
print('Found slice:', s, 'skip:', self.skip)
|
||||
|
||||
if s:
|
||||
self.slice = s
|
||||
self.state = self.find_new_sand
|
||||
else:
|
||||
print('No slices remaining.')
|
||||
self.state = self.cleanup
|
||||
|
||||
def find_new_sand(self):
|
||||
print('Finding new sand...')
|
||||
w = self.g.world
|
||||
p = utils.pint(self.g.pos)
|
||||
|
||||
sand = w.find_sand(p, 50, self.origin)
|
||||
sand = w.find_sand(self.slice, 2, p)
|
||||
print('Found sand:', sand)
|
||||
|
||||
if not len(sand):
|
||||
self.state = self.cleanup
|
||||
return
|
||||
|
||||
for check in sand:
|
||||
if check in self.bad_sand:
|
||||
continue
|
||||
self.sand = check
|
||||
break
|
||||
|
||||
self.state = self.nav_to_sand
|
||||
if utils.phyp(p, self.sand) > BREAK_DISTANCE:
|
||||
self.state = self.nav_to_sand
|
||||
else:
|
||||
self.state = self.dig_sand
|
||||
|
||||
def nav_to_sand(self):
|
||||
w = self.g.world
|
||||
p = utils.pint(self.g.pos)
|
||||
c = self.g.chunks
|
||||
|
||||
self.g.chunks.set_block_at(*self.sand, blocks.AIR)
|
||||
tmp = c.get_block_at(*self.sand)
|
||||
c.set_block_at(*self.sand, blocks.AIR)
|
||||
navpath = w.path_to_place(p, self.sand)
|
||||
self.g.chunks.set_block_at(*self.sand, blocks.SAND)
|
||||
c.set_block_at(*self.sand, tmp)
|
||||
|
||||
if navpath:
|
||||
self.g.path = navpath[:-1]
|
||||
@@ -338,27 +365,11 @@ class GatherSandStates:
|
||||
def dig_sand(self):
|
||||
if not self.g.breaking:
|
||||
if self.bsand(self.sand):
|
||||
self.g.look_at = self.sand
|
||||
self.g.game.break_block(self.sand)
|
||||
print('digging sand')
|
||||
else:
|
||||
self.state = self.get_sand
|
||||
|
||||
def get_sand(self):
|
||||
w = self.g.world
|
||||
p = utils.pint(self.g.pos)
|
||||
navpath = w.path_to_place(p, self.sand)
|
||||
|
||||
if navpath:
|
||||
self.g.path = navpath
|
||||
self.state = self.going_to_item
|
||||
else:
|
||||
self.bad_sand.append(self.sand)
|
||||
self.state = self.find_new_sand
|
||||
|
||||
def going_to_item(self):
|
||||
if utils.pint(self.g.pos) == self.sand:
|
||||
self.g.look_at = self.sand
|
||||
self.state = self.cleanup
|
||||
self.state = self.find_new_sand
|
||||
|
||||
def cleanup(self):
|
||||
self.g.look_at = None
|
||||
@@ -374,6 +385,8 @@ class GatherSandStates:
|
||||
self.state = self.idle
|
||||
|
||||
self.origin = utils.pint(self.g.pos)
|
||||
self.skip = (0, 0)
|
||||
self.slice = None
|
||||
self.sand = None
|
||||
self.bad_sand = []
|
||||
self.wait_time = 0
|
||||
@@ -382,6 +395,75 @@ class GatherSandStates:
|
||||
self.state()
|
||||
|
||||
|
||||
class GrabSandStates:
|
||||
def idle(self):
|
||||
return None
|
||||
|
||||
def init(self):
|
||||
self.state = self.find_sand
|
||||
print('Trying to grab sand')
|
||||
|
||||
def find_sand(self):
|
||||
w = self.g.world
|
||||
p = utils.pint(self.g.pos)
|
||||
|
||||
sand = w.find_objects(items.SAND_ID)
|
||||
|
||||
if not sand:
|
||||
print('No sand objects found, aborting')
|
||||
self.state = self.cleanup
|
||||
return
|
||||
|
||||
sand.sort(key=lambda s: utils.phyp(p, (s.x, s.y, s.z)))
|
||||
|
||||
for s in sand:
|
||||
s_pos = utils.pint((s.x, s.y, s.z))
|
||||
check = utils.padd(s_pos, path.BLOCK_BELOW)
|
||||
|
||||
if utils.phyp(p, s_pos) > 6:
|
||||
continue
|
||||
if s.entity_id in self.eid_blacklist:
|
||||
continue
|
||||
# skip if the sand is floating
|
||||
if self.g.chunks.get_block_at(*check) in {0}:
|
||||
continue
|
||||
|
||||
navpath = w.path_to_place(p, s_pos)
|
||||
|
||||
if navpath:
|
||||
self.g.path = navpath
|
||||
self.state = self.going_to_sand
|
||||
self.sand = s_pos
|
||||
self.eid_blacklist.append(s.entity_id)
|
||||
print('Going to sand', self.sand)
|
||||
return
|
||||
|
||||
print('Cant get to any more sand, aborting')
|
||||
self.state = self.cleanup
|
||||
|
||||
def going_to_sand(self):
|
||||
if utils.pint(self.g.pos) == self.sand:
|
||||
self.state = self.find_sand
|
||||
|
||||
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.sand = None
|
||||
self.eid_blacklist = []
|
||||
|
||||
def run(self):
|
||||
self.state()
|
||||
|
||||
|
||||
class SleepWithBedStates:
|
||||
def idle(self):
|
||||
return None
|
||||
@@ -395,7 +477,6 @@ class SleepWithBedStates:
|
||||
|
||||
def select_bed(self):
|
||||
if self.g.game.select_item(items.BED_IDS):
|
||||
self.g.look_at = utils.padd(self.area, path.BLOCK_BELOW)
|
||||
self.state = self.find_bed_spot
|
||||
else:
|
||||
print('No bed, aborting.')
|
||||
@@ -482,7 +563,7 @@ class SleepWithBedStates:
|
||||
self.g = global_state
|
||||
self.state = self.idle
|
||||
|
||||
self.silent = False
|
||||
self.silent = True
|
||||
|
||||
self.area = None
|
||||
self.opening = None
|
||||
@@ -503,7 +584,7 @@ class CacheItemsStates:
|
||||
|
||||
num_stacks = len([x for x in self.g.inv.values() if x.present])
|
||||
print('Inventory amount:', num_stacks)
|
||||
if num_stacks >= 27:
|
||||
if num_stacks >= self.minimum:
|
||||
self.state = self.find_trapped_chests
|
||||
else:
|
||||
print('Aborting caching, not full')
|
||||
@@ -676,6 +757,7 @@ class CacheItemsStates:
|
||||
self.g = global_state
|
||||
self.state = self.idle
|
||||
|
||||
self.minimum = 27
|
||||
self.silent = False
|
||||
|
||||
# keep all needed items
|
||||
@@ -806,9 +888,7 @@ class ClearLeavesStates:
|
||||
w = self.g.world
|
||||
p = utils.pint(self.g.pos)
|
||||
|
||||
break_distance = 5
|
||||
|
||||
for l in w.find_leaves(p, break_distance):
|
||||
for l in w.find_leaves(p, BREAK_DISTANCE):
|
||||
self.leaves.append(l)
|
||||
|
||||
self.state = self.break_leaves
|
||||
@@ -879,7 +959,7 @@ class GrabSaplingStates:
|
||||
if s.entity_id in self.eid_blacklist:
|
||||
continue
|
||||
|
||||
# slip if the sapling is floating
|
||||
# skip if the sapling is floating
|
||||
if self.g.chunks.get_block_at(*check) in blocks.LEAF_IDS | {0}:
|
||||
continue
|
||||
|
||||
@@ -945,13 +1025,15 @@ class JobStates:
|
||||
|
||||
def gather_sand(self):
|
||||
s1 = self.gather_sand_states
|
||||
s2 = self.sleep_with_bed_states
|
||||
s3 = self.cache_items_states
|
||||
s2 = self.grab_sand_states
|
||||
s3 = self.sleep_with_bed_states
|
||||
s4 = self.cache_items_states
|
||||
|
||||
if s1.state == s1.idle:
|
||||
s1.state = s1.init
|
||||
s2.state = s2.init
|
||||
s3.state = s3.init
|
||||
s4.state = s4.init
|
||||
elif s1.state == s1.done:
|
||||
if s2.state != s2.done:
|
||||
s2.run()
|
||||
@@ -961,9 +1043,14 @@ class JobStates:
|
||||
s3.run()
|
||||
return
|
||||
|
||||
if s4.state != s4.done:
|
||||
s4.run()
|
||||
return
|
||||
|
||||
s1.state = s1.init
|
||||
s2.state = s2.init
|
||||
s3.state = s3.init
|
||||
s4.state = s4.init
|
||||
return
|
||||
|
||||
s1.run()
|
||||
@@ -1051,6 +1138,7 @@ class JobStates:
|
||||
self.plant_tree_states = PlantTreeStates(self.g)
|
||||
self.clear_leaves_states = ClearLeavesStates(self.g)
|
||||
self.grab_sapling_states = GrabSaplingStates(self.g)
|
||||
self.grab_sand_states = GrabSandStates(self.g)
|
||||
self.state = self.idle
|
||||
|
||||
def __init__(self, global_state):
|
||||
@@ -1066,6 +1154,7 @@ class JobStates:
|
||||
self.plant_tree_states = PlantTreeStates(self.g)
|
||||
self.clear_leaves_states = ClearLeavesStates(self.g)
|
||||
self.grab_sapling_states = GrabSaplingStates(self.g)
|
||||
self.grab_sand_states = GrabSandStates(self.g)
|
||||
|
||||
def tick(self):
|
||||
self.state()
|
||||
|
||||
Reference in New Issue
Block a user