274 lines
8.0 KiB
Python
274 lines
8.0 KiB
Python
import importlib
|
|
import collections
|
|
from math import floor, ceil, sqrt
|
|
|
|
from mosfet.info import blocks
|
|
from mosfet.info import mcdata
|
|
|
|
TICK = 0.05
|
|
|
|
def hypot(*coordinates):
|
|
# python's 3D hypot is too new, so we'll use our own
|
|
return sqrt(sum(x**2 for x in coordinates))
|
|
|
|
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 bot_name_family(name):
|
|
"""Return the group name for a bot ending in one digit from 1 to 9."""
|
|
name = str(name or '').casefold()
|
|
if len(name) > 1 and name[-1] in '123456789':
|
|
return name[:-1]
|
|
return name
|
|
|
|
|
|
def find_nearby_bots(global_state, position, distance):
|
|
"""Find nearby online bots sharing this bot's numbered name family."""
|
|
own_name = str(global_state.get('name', '') or '')
|
|
family = bot_name_family(own_name)
|
|
if not family:
|
|
return []
|
|
|
|
names = global_state.get('player_names') or {}
|
|
players = global_state.get('players') or {}
|
|
result = []
|
|
for entity_id, player in players.items():
|
|
player_uuid = str(player.get('player_uuid', '') or '')
|
|
name = (player.get('name') or names.get(player_uuid) or
|
|
names.get(player_uuid.casefold()))
|
|
if not name:
|
|
continue
|
|
name = str(name)
|
|
if (name.casefold() == own_name.casefold() or
|
|
bot_name_family(name) != family):
|
|
continue
|
|
player_position = (player.x, player.y, player.z)
|
|
player_distance = phyp(position, player_position)
|
|
if player_distance <= distance:
|
|
result.append({
|
|
'entity_id': entity_id,
|
|
'name': name,
|
|
'position': player_position,
|
|
'distance': player_distance,
|
|
})
|
|
result.sort(key=lambda bot: (bot['distance'], bot['name']))
|
|
return result
|
|
|
|
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
|
|
return hypot(p1[0] - p2[0], height_diff, p1[2] - p2[2]) + origin_distance*0.5
|
|
|
|
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]))
|
|
|
|
def pint(p):
|
|
return (floor(p[0]), floor(p[1]), floor(p[2]))
|
|
|
|
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)
|
|
|
|
def cap(x, amount):
|
|
sign = 1 if x >= 0 else -1
|
|
return sign * min(abs(x), amount)
|
|
|
|
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)
|
|
|
|
def _enchantment_level(enchantments, *names):
|
|
if isinstance(enchantments, dict):
|
|
for name in names:
|
|
value = enchantments.get(name)
|
|
if value is None:
|
|
continue
|
|
if isinstance(value, dict):
|
|
value = value.get('level', value.get('lvl', 0))
|
|
return int(value or 0)
|
|
else:
|
|
for enchantment in enchantments or ():
|
|
if not isinstance(enchantment, dict):
|
|
continue
|
|
name = enchantment.get('name')
|
|
if isinstance(name, dict):
|
|
name = name.get('id', name.get('name'))
|
|
if any(str(name).endswith(candidate) for candidate in names):
|
|
return int(enchantment.get(
|
|
'level', enchantment.get('lvl', 0)) or 0)
|
|
return 0
|
|
|
|
|
|
def break_time(block_id, held_item=0, in_water=False, on_ground=True,
|
|
enchantments=None, effects=None, creative=False):
|
|
"""Return Mineflayer-style vanilla dig time in seconds."""
|
|
if creative:
|
|
return 0.0
|
|
|
|
held_item = str(held_item)
|
|
block_data = blocks.get(block_id)
|
|
hardness = block_data.get('hardness', 0)
|
|
if hardness < 0:
|
|
return float('inf')
|
|
if hardness == 0:
|
|
return 0.0
|
|
|
|
harvest_tools = block_data.get('harvestTools')
|
|
can_harvest = (not harvest_tools or
|
|
held_item in harvest_tools)
|
|
material = block_data.get('material', 'n/a')
|
|
tool_multipliers = mcdata.mcd.materials.get(material, {})
|
|
speed = tool_multipliers.get(held_item, 1.0) or 1.0
|
|
|
|
efficiency = _enchantment_level(
|
|
enchantments, 'efficiency', 'minecraft:efficiency')
|
|
if efficiency > 0 and speed > 1.0:
|
|
speed += efficiency * efficiency + 1
|
|
|
|
effects = effects or {}
|
|
|
|
def effect_level(*names):
|
|
for name in names:
|
|
value = effects.get(name)
|
|
if value is None:
|
|
continue
|
|
if isinstance(value, dict):
|
|
amplifier = value.get('amplifier')
|
|
if amplifier is not None:
|
|
return int(amplifier) + 1
|
|
return int(value.get('level', 0) or 0)
|
|
# Runtime effect dictionaries use zero-based amplifiers.
|
|
return int(value) + 1
|
|
return 0
|
|
|
|
haste = max(effect_level('haste', 'minecraft:haste'),
|
|
effect_level('conduit_power', 'minecraft:conduit_power'))
|
|
if haste > 0:
|
|
speed *= 1 + 0.2 * haste
|
|
|
|
fatigue = effect_level(
|
|
'mining_fatigue', 'minecraft:mining_fatigue')
|
|
if fatigue > 0:
|
|
speed *= (0.3, 0.09, 0.0027, 0.00081, 0.000243)[
|
|
min(fatigue, 4) - 1]
|
|
|
|
if speed <= 0:
|
|
return float('inf')
|
|
|
|
aqua_affinity = _enchantment_level(
|
|
enchantments, 'aqua_affinity', 'minecraft:aqua_affinity')
|
|
if in_water and aqua_affinity == 0:
|
|
speed /= 5.0
|
|
if not on_ground:
|
|
speed /= 5.0
|
|
|
|
delta = speed / hardness / (30.0 if can_harvest else 100.0)
|
|
if delta <= 0:
|
|
return float('inf')
|
|
if delta >= 1.0:
|
|
return 0.0
|
|
return ceil(1.0 / delta) * TICK
|
|
|
|
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 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),
|
|
]
|