Improve searching for crops

This commit is contained in:
Tanner Collin 2021-04-23 01:25:44 +00:00
parent 9874e23aa6
commit 23891066c0
5 changed files with 69 additions and 54 deletions

View File

@ -26,7 +26,6 @@ from munch import Munch
from mosfet import blocks
from mosfet import game
from mosfet import world
from mosfet import items
from mosfet import job
from mosfet import mcdata
@ -35,6 +34,7 @@ from mosfet import path
from mosfet import print_help
from mosfet import utils
from mosfet import vector
from mosfet import world
for module in [
blocks,
@ -47,6 +47,7 @@ for module in [
print_help,
utils,
vector,
world,
]:
importlib.reload(module)

View File

@ -35,8 +35,7 @@ class GatherCropStates:
blocks.MATURE_BEETROOT_ID,
]
for crop in w.find_blocks_3d(p, mature_crops, 50, 20):
print('Found crop:', crop)
for crop in w.find_blocks_3d(p, mature_crops, 50, 20, True):
if crop not in self.bad_crops:
break
else: # for
@ -44,6 +43,7 @@ class GatherCropStates:
self.state = self.cleanup
return
print('Found crop:', crop)
self.crop = crop
self.type_id = w.block_at(*crop)
self.state = self.nav_to_crop
@ -55,10 +55,12 @@ class GatherCropStates:
navpath = w.path_to_place(p, self.crop)
if navpath:
print('Going to crop', self.crop)
self.g.path = navpath
self.g.look_at = utils.padd(self.crop, path.BLOCK_BELOW)
self.state = self.going_to_crop
else:
print('Cant get to it, blacklisting')
self.bad_crops.append(self.crop)
self.wait_time = 0.5
self.state = self.wait_to_restart

View File

@ -29,8 +29,7 @@ class GatherWartStates:
p = utils.pint(self.g.pos)
mature_wart = max(blocks.NETHERWART_IDS)
for wart in w.find_blocks_3d(p, [mature_wart], 50, 20):
print('Found wart:', wart)
for wart in w.find_blocks_3d(p, [mature_wart], 50, 20, True):
if wart not in self.bad_warts:
break
else: # for
@ -38,6 +37,7 @@ class GatherWartStates:
self.state = self.cleanup
return
print('Found wart:', wart)
self.wart = wart
self.state = self.nav_to_wart
@ -48,11 +48,21 @@ class GatherWartStates:
navpath = w.path_to_place(p, self.wart)
if navpath:
print('Going to wart', self.crop)
self.g.path = navpath
self.g.look_at = utils.padd(self.wart, path.BLOCK_BELOW)
self.state = self.going_to_wart
else:
print('Cant get to it, blacklisting')
self.bad_warts.append(wart)
self.wait_time = 0.5
self.state = self.wait_to_restart
def wait_to_restart(self):
# prevent timeouts
if self.wait_time > 0:
self.wait_time -= utils.TICK
else:
self.state = self.find_new_wart
def going_to_wart(self):

View File

@ -133,49 +133,32 @@ def search_2d(distance=0):
visited.add(cur)
yield cur
def search_3d(distance=0, y_limit=0):
def get_neighbors(x,y,z):
return [
(x+1, y+1, z+0),
(x+1, y-1, z+0),
(x+1, y+1, z+1),
(x+1, y+0, z+1),
(x+1, y-1, z+1),
(x+1, y+1, z-1),
(x+1, y+0, z-1),
(x+1, y-1, z-1),
(x+1, y+0, z+0),
(x+0, y+1, z+0),
(x+0, y-1, z+0),
(x+0, y+1, z+1),
(x+0, y+0, z+1),
(x+0, y-1, z+1),
(x+0, y+1, z-1),
(x+0, y+0, z-1),
(x+0, y-1, z-1),
(x-1, y+1, z+0),
(x-1, y-1, z+0),
(x-1, y+1, z+1),
(x-1, y+0, z+1),
(x-1, y-1, z+1),
(x-1, y+1, z-1),
(x-1, y+0, z-1),
(x-1, y-1, z-1),
(x-1, y+0, z+0),
]
to_visit = collections.deque([(0, 0, 0)])
visited = set()
while to_visit:
cur = to_visit.pop()
if cur in visited:
continue
if y_limit and abs(cur[1]) > y_limit:
continue
if distance and hypot(*cur) > distance:
continue
for neighbor in get_neighbors(*cur):
to_visit.appendleft(neighbor)
visited.add(cur)
yield cur
def get_neighbors_3d(x,y,z):
return [
#(x+1, y+1, z+0),
#(x+1, y-1, z+0),
#(x+1, y+1, z+1),
#(x+1, y+0, z+1),
#(x+1, y-1, z+1),
#(x+1, y+1, z-1),
#(x+1, y+0, z-1),
#(x+1, y-1, z-1),
(x+1, y+0, z+0),
(x+0, y+1, z+0),
(x+0, y-1, z+0),
#(x+0, y+1, z+1),
(x+0, y+0, z+1),
#(x+0, y-1, z+1),
#(x+0, y+1, z-1),
(x+0, y+0, z-1),
#(x+0, y-1, z-1),
#(x-1, y+1, z+0),
#(x-1, y-1, z+0),
#(x-1, y+1, z+1),
#(x-1, y+0, z+1),
#(x-1, y-1, z+1),
#(x-1, y+1, z-1),
#(x-1, y+0, z-1),
#(x-1, y-1, z-1),
(x-1, y+0, z+0),
]

View File

@ -1,3 +1,4 @@
import collections
import re
import time
import random
@ -24,9 +25,27 @@ class World:
return False
return True
def find_blocks_3d(self, center, block_ids, distance=0, y_limit=0):
for offset in utils.search_3d(distance, y_limit):
check = utils.padd(center, offset)
def find_blocks_3d(self, center, block_ids, distance=0, y_limit=0, thru_air=False):
to_visit = collections.deque([(0, 0, 0)])
visited = set()
while to_visit:
cur = to_visit.pop()
if cur in visited:
continue
if y_limit and abs(cur[1]) > y_limit:
continue
if distance and hypot(*cur) > distance:
continue
check = utils.padd(center, cur)
if not thru_air or self.block_at(*check) in blocks.NON_SOLID_IDS:
for neighbor in utils.get_neighbors_3d(*cur):
to_visit.appendleft(neighbor)
visited.add(cur)
if self.block_at(*check) in block_ids:
yield check