122 lines
3.8 KiB
Python
122 lines
3.8 KiB
Python
"""Pick up dropped sand using 26.3 entity and navigation state."""
|
|
import time
|
|
|
|
from mosfet import utils
|
|
from mosfet import path
|
|
from mosfet.info import blocks
|
|
from mosfet.info import items
|
|
|
|
|
|
class GrabSandStates:
|
|
PICKUP_DISTANCE = 1.5
|
|
PICKUP_TIMEOUT = 5.0
|
|
|
|
def idle(self):
|
|
return None
|
|
|
|
def init(self):
|
|
self.eid_blacklist.clear()
|
|
self.state = self.find_sand
|
|
print('Looking for dropped sand')
|
|
|
|
def _object(self, entity_id):
|
|
obj = self.g.get('objects', {}).get(entity_id)
|
|
if obj is None or obj.get('item_id') != items.SAND_ID:
|
|
return None
|
|
return obj
|
|
|
|
def _skip_target(self):
|
|
if self.entity_id is not None:
|
|
self.eid_blacklist.add(self.entity_id)
|
|
self.g.path = []
|
|
self.g.look_at = None
|
|
self.state = self.find_sand
|
|
|
|
def find_sand(self):
|
|
world = self.g.world
|
|
position = utils.pint(self.g.pos)
|
|
candidates = [
|
|
(entity_id, obj) for entity_id, obj in
|
|
self.g.get('objects', {}).items()
|
|
if obj.get('item_id') == items.SAND_ID and
|
|
entity_id not in self.eid_blacklist
|
|
]
|
|
candidates.sort(key=lambda pair: utils.phyp(
|
|
position, (pair[1].x, pair[1].y, pair[1].z)))
|
|
|
|
for entity_id, obj in candidates:
|
|
item_position = (obj.x, obj.y, obj.z)
|
|
if utils.phyp(position, item_position) > 6:
|
|
continue
|
|
item_block = utils.pint(item_position)
|
|
support = world.block_at(*utils.padd(item_block, path.BLOCK_BELOW))
|
|
if support is None or support in blocks.NON_SOLID_IDS:
|
|
continue
|
|
|
|
self.entity_id = entity_id
|
|
self.sand = item_block
|
|
if utils.phyp(tuple(self.g.pos), item_position) <= self.PICKUP_DISTANCE:
|
|
self.pickup_deadline = time.monotonic() + self.PICKUP_TIMEOUT
|
|
self.state = self.wait_for_pickup
|
|
return
|
|
|
|
navpath = world.path_to_place(position, item_block)
|
|
if not navpath:
|
|
self.eid_blacklist.add(entity_id)
|
|
continue
|
|
|
|
self.entity_id = entity_id
|
|
self.sand = item_block
|
|
self.g.path = navpath
|
|
self.g.look_at = item_block
|
|
print('Going to dropped sand', self.sand)
|
|
self.state = self.going_to_sand
|
|
return
|
|
|
|
print('No reachable dropped sand nearby')
|
|
self.state = self.cleanup
|
|
|
|
def going_to_sand(self):
|
|
obj = self._object(self.entity_id)
|
|
if obj is None:
|
|
self.g.path = []
|
|
self.state = self.find_sand
|
|
return
|
|
|
|
item_position = (obj.x, obj.y, obj.z)
|
|
distance = utils.phyp(tuple(self.g.pos), item_position)
|
|
if distance <= self.PICKUP_DISTANCE:
|
|
self.g.path = []
|
|
self.pickup_deadline = time.monotonic() + self.PICKUP_TIMEOUT
|
|
self.state = self.wait_for_pickup
|
|
elif not self.g.path:
|
|
self._skip_target()
|
|
|
|
def wait_for_pickup(self):
|
|
if self._object(self.entity_id) is None:
|
|
print('Picked up dropped sand')
|
|
self.g.look_at = None
|
|
self.state = self.find_sand
|
|
elif time.monotonic() > self.pickup_deadline:
|
|
print('Could not pick up dropped sand; skipping', self.sand)
|
|
self._skip_target()
|
|
|
|
def cleanup(self):
|
|
self.g.path = []
|
|
self.g.look_at = None
|
|
self.state = self.done
|
|
|
|
def done(self):
|
|
return None
|
|
|
|
def __init__(self, global_state):
|
|
self.g = global_state
|
|
self.state = self.idle
|
|
self.sand = None
|
|
self.entity_id = None
|
|
self.eid_blacklist = set()
|
|
self.pickup_deadline = None
|
|
|
|
def run(self):
|
|
self.state()
|