2020-09-16 06:49:15 +00:00
|
|
|
import importlib
|
2020-10-16 08:56:11 +00:00
|
|
|
import collections
|
2020-09-17 01:12:01 +00:00
|
|
|
from math import floor, ceil, sqrt, hypot
|
2020-09-08 21:33:42 +00:00
|
|
|
|
2020-09-16 06:49:15 +00:00
|
|
|
import blocks
|
2020-12-14 05:40:17 +00:00
|
|
|
import mcdata
|
2020-09-16 06:49:15 +00:00
|
|
|
|
|
|
|
TICK = 0.05
|
|
|
|
|
2020-09-08 21:33:42 +00:00
|
|
|
def padd(p1, p2):
|
|
|
|
return (p1[0] + p2[0], p1[1] + p2[1], p1[2] + p2[2])
|
|
|
|
|
|
|
|
def psub(p1, p2):
|
|
|
|
return (p1[0] - p2[0], p1[1] - p2[1], p1[2] - p2[2])
|
|
|
|
|
|
|
|
def pmul(p, s):
|
|
|
|
return (s*p[0], s*p[1], s*p[2])
|
|
|
|
|
|
|
|
def phyp(p1, p2):
|
|
|
|
return hypot(p1[0] - p2[0], p1[1] - p2[1], p1[2] - p2[2])
|
|
|
|
|
|
|
|
def phyp_bias(p1, p2, origin):
|
|
|
|
origin_distance = phyp(origin, p2)
|
|
|
|
height_diff = p2[1] - p1[1]
|
|
|
|
height_diff = height_diff*8 if height_diff < 0 else height_diff*0.5
|
2020-09-17 09:11:08 +00:00
|
|
|
return hypot(p1[0] - p2[0], height_diff, p1[2] - p2[2]) + origin_distance*0.5
|
2020-09-08 21:33:42 +00:00
|
|
|
|
2020-09-25 21:51:36 +00:00
|
|
|
def phyp_king(p1, p2):
|
|
|
|
# calculates the Chebyshev distance
|
|
|
|
return max(abs(p1[0] - p2[0]), abs(p1[1] - p2[1]), abs(p1[2] - p2[2]))
|
|
|
|
|
2020-09-08 21:33:42 +00:00
|
|
|
def pint(p):
|
|
|
|
return (floor(p[0]), floor(p[1]), floor(p[2]))
|
|
|
|
|
2020-12-05 10:56:56 +00:00
|
|
|
def pboundingbox(p1, p2):
|
|
|
|
b1 = (min(p1[0], p2[0]), min(p1[1], p2[1]), min(p1[2], p2[2]))
|
|
|
|
b2 = (max(p1[0], p2[0]), max(p1[1], p2[1]), max(p1[2], p2[2]))
|
|
|
|
return b1, b2
|
|
|
|
|
|
|
|
def pvolume(p1, p2):
|
|
|
|
b1, b2 = pboundingbox(p1, p2)
|
|
|
|
box = psub(b2, b1)
|
|
|
|
return (box[0]+1) * (box[1]+1) * (box[2]+1)
|
|
|
|
|
2020-09-08 21:33:42 +00:00
|
|
|
def cap(x, amount):
|
|
|
|
sign = 1 if x >= 0 else -1
|
|
|
|
return sign * min(abs(x), amount)
|
|
|
|
|
2020-09-17 01:12:01 +00:00
|
|
|
def spiral(n):
|
|
|
|
# return x, 0, z coords along a spiral at step n
|
|
|
|
# I forget where I found this
|
|
|
|
n += 1
|
|
|
|
k = ceil((sqrt(n)-1)/2)
|
|
|
|
t = 2 * k + 1
|
|
|
|
m = t**2
|
|
|
|
t = t - 1
|
|
|
|
if n >= m-t:
|
|
|
|
return k-(m-n), 0, -k
|
|
|
|
else:
|
|
|
|
m = m-t
|
|
|
|
if n >= m-t:
|
|
|
|
return -k, 0, -k+(m-n)
|
|
|
|
else:
|
|
|
|
m = m-t
|
|
|
|
if n >= m-t:
|
|
|
|
return -k+(m-n), 0, k
|
|
|
|
else:
|
|
|
|
return k, 0, k-(m-n-t)
|
|
|
|
|
|
|
|
def alternate(n, amount):
|
|
|
|
# return 0, y, 0 where y alternates +/- by amount
|
|
|
|
# example: 0, 2, -2, 4, -4, 6, -6 for amount = 2
|
|
|
|
sign = 1 if n % 2 else -1
|
|
|
|
return (0, ceil(n/2) * sign * amount, 0)
|
|
|
|
|
|
|
|
def diffrange(n):
|
|
|
|
# same as range(n+1) but can go negative
|
|
|
|
sign = 1 if n >= 0 else -1
|
|
|
|
return range(0, n+sign, sign)
|
|
|
|
|
2020-09-16 06:49:15 +00:00
|
|
|
def break_time(block_id, held_item=0, in_water=False, on_ground=True, enchantments=[], effects={}):
|
|
|
|
# from PrismarineJS/prismarine-block
|
2020-12-02 11:12:51 +00:00
|
|
|
held_item = str(held_item)
|
2020-09-21 01:08:23 +00:00
|
|
|
block_data = blocks.get(block_id)
|
2020-09-16 06:49:15 +00:00
|
|
|
|
2020-09-21 01:08:23 +00:00
|
|
|
can_harvest = 'harvestTools' not in block_data or str(held_item) in block_data['harvestTools']
|
|
|
|
material = block_data.get('material', 'n/a')
|
2020-12-14 05:40:17 +00:00
|
|
|
tool_multipliers = mcdata.mcd.materials.get(material, [])
|
2020-09-16 06:49:15 +00:00
|
|
|
is_best_tool = held_item in tool_multipliers
|
2020-09-21 01:08:23 +00:00
|
|
|
time = block_data['hardness']
|
2020-09-16 06:49:15 +00:00
|
|
|
|
|
|
|
if can_harvest:
|
|
|
|
time *= 1.5
|
|
|
|
else:
|
|
|
|
time *= 5.0
|
|
|
|
|
|
|
|
if is_best_tool:
|
|
|
|
speed_multiplier = tool_multipliers[held_item]
|
|
|
|
# TODO: calc efficiency, haste, mining fatigue
|
|
|
|
else:
|
|
|
|
speed_multiplier = 1.0
|
|
|
|
|
|
|
|
time /= speed_multiplier
|
|
|
|
if in_water: time *= 5.0
|
|
|
|
if not on_ground: time *= 5.0
|
|
|
|
|
|
|
|
return time
|
2020-10-16 08:56:11 +00:00
|
|
|
|
2020-12-06 23:53:28 +00:00
|
|
|
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
|
|
|
|
|
2020-10-16 08:56:11 +00:00
|
|
|
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
|