Add 3D BFS iterator
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import importlib
|
||||
import collections
|
||||
from math import floor, ceil, sqrt, hypot
|
||||
|
||||
import blocks
|
||||
@@ -94,3 +95,50 @@ def break_time(block_id, held_item=0, in_water=False, on_ground=True, enchantmen
|
||||
if not on_ground: time *= 5.0
|
||||
|
||||
return time
|
||||
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user