Search for next block to place in 2D

This commit is contained in:
Tanner Collin 2020-12-06 23:53:28 +00:00
parent bce868a522
commit 86846a5c02
2 changed files with 55 additions and 36 deletions

64
jobs.py
View File

@ -1121,7 +1121,7 @@ class FillBlocksStates:
return return
if self.last_block: if self.last_block:
self.state = self.select_block self.state = self.select_item
else: else:
self.state = self.find_last_block self.state = self.find_last_block
@ -1131,28 +1131,29 @@ class FillBlocksStates:
print('Finding last block') print('Finding last block')
b1, b2 = utils.pboundingbox(f.coord1, f.coord2) b1, b2 = utils.pboundingbox(f.coord1, f.coord2)
box = utils.psub(b2, b1)
xz_distance = hypot(box[0]+1, box[2]+1)
y_start = f.coord1[1] y_start = f.coord1[1]
y_end = f.coord2[1] y_end = f.coord2[1]
distance = utils.phyp(f.coord1, f.coord2)
self.next_block = f.coord1 for y in range(y_start, y_end+1):
for offset in utils.search_2d(xz_distance):
check = utils.padd(f.coord1, offset)
check = (check[0], y, check[2])
for offset in utils.search_3d(distance): # ensure block is within fill area
check = utils.padd(f.coord1, offset) if check[0] < b1[0] or check[0] > b2[0]:
continue
if check[2] < b1[2] or check[2] > b2[2]:
continue
# ensure block is within fill area if w.block_at(*check) == blocks.AIR:
if check[0] < b1[0] or check[0] > b2[0]: self.state = self.select_item
continue return
if check[2] < b1[2] or check[2] > b2[2]:
continue
if w.block_at(*check) == blocks.AIR: self.last_block = check
self.state = self.select_block
return
self.last_block = check def select_item(self):
def select_block(self):
f = self.g.filling f = self.g.filling
name = blocks.BLOCKS[f.block] name = blocks.BLOCKS[f.block]
item = items.ITEMS['minecraft:'+name]['protocol_id'] item = items.ITEMS['minecraft:'+name]['protocol_id']
@ -1175,21 +1176,14 @@ class FillBlocksStates:
y_start = f.coord1[1] y_start = f.coord1[1]
y_end = f.coord2[1] y_end = f.coord2[1]
print('distance', xz_distance)
for y in range(y_start, y_end+1): for y in range(y_start, y_end+1):
for i in count(): if y not in self.iterators:
offset = utils.spiral(i) self.iterators[y] = utils.search_2d(xz_distance)
check = utils.padd(self.last_block, offset)
for offset in self.iterators[y]:
check = utils.padd(f.coord1, offset)
check = (check[0], y, check[2]) check = (check[0], y, check[2])
print('layer', y)
print('offset', offset)
print('hypot', hypot(offset[0], offset[2]))
if hypot(offset[0], offset[2]) > xz_distance:
break
# ensure block is within fill area # ensure block is within fill area
if check[0] < b1[0] or check[0] > b2[0]: if check[0] < b1[0] or check[0] > b2[0]:
continue continue
@ -1245,23 +1239,20 @@ class FillBlocksStates:
self.g.game.place_block(self.next_block, BlockFace.TOP) self.g.game.place_block(self.next_block, BlockFace.TOP)
self.g.look_at = self.next_block self.g.look_at = self.next_block
self.wait_time = 0.25 self.wait_time = 0.25
self.state = self.wait_for_block self.state = self.wait_for_block
def wait_for_block(self): def wait_for_block(self):
if self.wait_time > 0:
self.wait_time -= utils.TICK
else:
self.state = self.check_block
def check_block(self):
w = self.g.world w = self.g.world
if w.block_at(*self.next_block) != blocks.AIR: if w.block_at(*self.next_block) != blocks.AIR:
self.last_block = self.next_block self.last_block = self.next_block
self.state = self.check_obstruction
elif self.wait_time > 0:
self.wait_time -= utils.TICK
else: else:
print('Block didnt appear') print('Block didnt appear')
self.state = self.check_obstruction
self.state = self.check_obstruction
def check_obstruction(self): def check_obstruction(self):
p = utils.pint(self.g.pos) p = utils.pint(self.g.pos)
@ -1303,6 +1294,7 @@ class FillBlocksStates:
self.g = global_state self.g = global_state
self.state = self.idle self.state = self.idle
self.iterators = {}
self.wait_time = 0 self.wait_time = 0
self.last_block = None self.last_block = None
self.next_block = None self.next_block = None

View File

@ -107,6 +107,33 @@ def break_time(block_id, held_item=0, in_water=False, on_ground=True, enchantmen
return time return time
def search_2d(distance=0):
def get_neighbors(x,y,z):
return [
(x+1, y+0, z+0),
#(x+1, y+0, z+1),
(x+0, y+0, z-1),
#(x-1, y+0, z+1),
(x-1, y+0, z+0),
#(x-1, y+0, z-1),
(x+0, y+0, z+1),
#(x+1, y+0, z-1),
]
to_visit = collections.deque([(0, 0, 0)])
visited = set()
while to_visit:
cur = to_visit.pop()
if cur in visited:
continue
if distance and hypot(*cur) > distance:
continue
for neighbor in get_neighbors(*cur):
to_visit.appendleft(neighbor)
visited.add(cur)
yield cur
def search_3d(distance=0, y_limit=0): def search_3d(distance=0, y_limit=0):
def get_neighbors(x,y,z): def get_neighbors(x,y,z):
return [ return [