Throw designated items at signs during caching
This commit is contained in:
1 parent
46b0657bcf
commit
ce3be28d0c
4 files changed
+469
-2
No files matched your search
+99
-1
@@ -4,7 +4,7 @@ from collections import namedtuple
|
||||
import re
|
||||
import time
|
||||
import random
|
||||
from math import floor
|
||||
from math import cos, floor, sin
|
||||
from itertools import count
|
||||
from copy import copy
|
||||
|
||||
@@ -99,6 +99,104 @@ class World:
|
||||
result.sort(key=lambda sign: utils.phyp(center, sign['position']))
|
||||
return result
|
||||
|
||||
@staticmethod
|
||||
def _sign_text_normal(sign, state_id):
|
||||
properties = blocks.PROPS.get(state_id, {})
|
||||
facing = properties.get('facing')
|
||||
cardinal = {
|
||||
'north': (0.0, 0.0, -1.0),
|
||||
'south': (0.0, 0.0, 1.0),
|
||||
'west': (-1.0, 0.0, 0.0),
|
||||
'east': (1.0, 0.0, 0.0),
|
||||
}
|
||||
normal = cardinal.get(facing)
|
||||
if normal is None:
|
||||
try:
|
||||
rotation = int(properties.get('rotation', 0)) % 16
|
||||
except (TypeError, ValueError):
|
||||
rotation = 0
|
||||
angle = rotation * (2 * 3.141592653589793 / 16)
|
||||
normal = (-sin(angle), 0.0, cos(angle))
|
||||
if sign.get('side') == 'back':
|
||||
normal = tuple(-component for component in normal)
|
||||
return normal
|
||||
|
||||
def find_throw_signs(self, center, bot_name, distance=80):
|
||||
"""Find nearby signs directing this bot to throw an item at them."""
|
||||
bot_name = str(bot_name).casefold()
|
||||
group_name = (bot_name[:-1] if len(bot_name) > 1 and
|
||||
bot_name[-1] in '123' else None)
|
||||
addresses = {bot_name}
|
||||
if group_name:
|
||||
addresses.add(group_name)
|
||||
|
||||
result = []
|
||||
for sign in self.find_signs(center, distance):
|
||||
text = ' '.join(line for line in sign['lines'] if line.strip())
|
||||
text = text.casefold().replace('minecraft:', '')
|
||||
tokens = re.findall(r'[a-z0-9_]+', text)
|
||||
if (len(tokens) < 3 or tokens[0] not in addresses or
|
||||
tokens[1] != 'throw'):
|
||||
continue
|
||||
item_name = '_'.join(tokens[2:])
|
||||
item_ids = items.storage_item_ids_for_label(item_name)
|
||||
if not item_ids:
|
||||
item_ids = items.get_set({item_name})
|
||||
if not item_ids:
|
||||
continue
|
||||
|
||||
state_id = self.block_at(*sign['position'])
|
||||
result.append({
|
||||
'position': tuple(sign['position']),
|
||||
'side': sign['side'],
|
||||
'lines': sign['lines'],
|
||||
'item_name': item_name,
|
||||
'item_ids': item_ids,
|
||||
'normal': self._sign_text_normal(sign, state_id),
|
||||
})
|
||||
result.sort(key=lambda directive: utils.phyp(
|
||||
center, directive['position']))
|
||||
return result
|
||||
|
||||
def find_throw_openings(self, sign, center, radius=3):
|
||||
"""Find standable, visible throw positions within radius of a sign."""
|
||||
sign_pos = tuple(sign['position'])
|
||||
normal_x, _, normal_z = sign['normal']
|
||||
face_point = (sign_pos[0] + 0.5 + normal_x * 0.49,
|
||||
sign_pos[1] + 0.5,
|
||||
sign_pos[2] + 0.5 + normal_z * 0.49)
|
||||
openings = []
|
||||
for dx in range(-radius, radius + 1):
|
||||
for dy in range(-radius, radius + 1):
|
||||
for dz in range(-radius, radius + 1):
|
||||
distance = utils.hypot(dx, dy, dz)
|
||||
if distance > radius or distance == 0:
|
||||
continue
|
||||
position = (sign_pos[0] + dx, sign_pos[1] + dy,
|
||||
sign_pos[2] + dz)
|
||||
if not self._cache_opening_standable(position):
|
||||
continue
|
||||
eye = (position[0] + 0.5, position[1] + 1.62,
|
||||
position[2] + 0.5)
|
||||
if not self._has_cache_line_of_sight(
|
||||
eye, face_point, sign_pos):
|
||||
continue
|
||||
horizontal_distance = utils.hypot(dx, dz)
|
||||
alignment = (
|
||||
(dx * normal_x + dz * normal_z) / horizontal_distance
|
||||
if horizontal_distance else -1.0)
|
||||
openings.append({
|
||||
'position': position,
|
||||
'look_at': sign_pos,
|
||||
'distance': distance,
|
||||
'alignment': alignment,
|
||||
'path_distance': utils.phyp(center, position),
|
||||
})
|
||||
openings.sort(key=lambda opening: (
|
||||
-opening['alignment'], opening['path_distance'],
|
||||
opening['distance'], opening['position']))
|
||||
return openings
|
||||
|
||||
def _double_chest_positions(self, chest):
|
||||
state_id = self.block_at(*chest)
|
||||
properties = blocks.PROPS.get(state_id, {})
|
||||
|
||||
Reference in new issue
Block a user