2023-09-21 16:23:00 +00:00
|
|
|
from dataclasses import dataclass, field
|
|
|
|
|
|
|
|
from module.base.timer import Timer
|
2023-08-20 17:28:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class Waypoint:
|
|
|
|
# Position to goto, (x, y)
|
|
|
|
position: tuple
|
|
|
|
# Position diff < threshold is considered as arrived
|
|
|
|
# `threshold` is used first if it is set
|
|
|
|
threshold: int = None
|
|
|
|
# If `threshold` is not set, `waypoint_threshold` and `endpoint_threshold` are used
|
|
|
|
waypoint_threshold: int = 10
|
|
|
|
endpoint_threshold: int = 3
|
2023-09-21 16:23:00 +00:00
|
|
|
# Max move speed, 'run_2x', 'straight_run', 'run', 'walk'
|
2023-08-20 17:28:30 +00:00
|
|
|
# See MapControl._goto() for details of each speed level
|
2023-09-30 10:37:37 +00:00
|
|
|
speed: str = 'run'
|
2023-10-04 17:58:14 +00:00
|
|
|
# Min move speed, 'run' or 'walk'
|
|
|
|
min_speed: str = 'walk'
|
|
|
|
# Lock joystick direction on the way to this waypoint, -180~180
|
|
|
|
lock_direction: int = None
|
|
|
|
# Trigger handle_combat_interact() if position diff < radius
|
|
|
|
# Usually to use 7 if interact is required, 0 for no interact
|
|
|
|
interact_radius: int = 0
|
2023-08-20 17:28:30 +00:00
|
|
|
|
|
|
|
"""
|
|
|
|
The following attributes are only be used if this waypoint is the end point of goto()
|
|
|
|
"""
|
|
|
|
# True to enable endpoint optimizations, character will smoothly approach target position
|
|
|
|
# False to stop all controls at arrive
|
2023-09-19 19:24:52 +00:00
|
|
|
end_opt: bool = True
|
2023-08-20 17:28:30 +00:00
|
|
|
# Set rotation after arrive, 0~360
|
2023-09-19 19:24:52 +00:00
|
|
|
end_rotation: int = None
|
|
|
|
end_rotation_threshold: int = 15
|
2023-08-20 17:28:30 +00:00
|
|
|
|
2023-09-21 16:23:00 +00:00
|
|
|
"""
|
|
|
|
Walk
|
|
|
|
"""
|
|
|
|
# A list of expected events, e.g. ['enemy', 'item']
|
|
|
|
# - "enemy", finished any combat
|
|
|
|
# - "item", destroyed any destructive objects
|
|
|
|
# - "interact", have map interact option (interact is not handled)
|
|
|
|
# - callable, A function that returns bool, True represents stop
|
|
|
|
# Or empty list [] for just walking
|
|
|
|
expected_end: list = field(default_factory=lambda: [])
|
|
|
|
# If triggered any expected event, consider arrive and stop walking
|
|
|
|
early_stop: bool = True
|
|
|
|
# Confirm timer if arrived but didn't trigger any expected event
|
2023-10-03 09:59:51 +00:00
|
|
|
unexpected_confirm: Timer = field(default_factory=lambda: Timer(3, count=15))
|
2023-09-21 16:23:00 +00:00
|
|
|
|
2023-08-20 17:28:30 +00:00
|
|
|
def __str__(self):
|
|
|
|
return f'Waypoint({self.position})'
|
|
|
|
|
|
|
|
__repr__ = __str__
|
|
|
|
|
2023-09-22 04:34:12 +00:00
|
|
|
def run_2x(self) -> "Waypoint":
|
2023-09-21 16:23:00 +00:00
|
|
|
"""
|
|
|
|
Product a Waypoint object with overridden "speed",
|
|
|
|
see Waypoint class for args.
|
|
|
|
"""
|
2023-09-22 04:34:12 +00:00
|
|
|
self.speed = 'run_2x'
|
|
|
|
return self
|
2023-09-21 16:23:00 +00:00
|
|
|
|
2023-09-22 04:34:12 +00:00
|
|
|
def straight_run(self) -> "Waypoint":
|
|
|
|
self.speed = 'straight_run'
|
|
|
|
return self
|
2023-09-21 16:23:00 +00:00
|
|
|
|
2023-09-22 04:34:12 +00:00
|
|
|
def run(self) -> "Waypoint":
|
|
|
|
self.speed = 'run'
|
|
|
|
return self
|
2023-09-21 16:23:00 +00:00
|
|
|
|
2023-09-22 04:34:12 +00:00
|
|
|
def walk(self) -> "Waypoint":
|
|
|
|
self.speed = 'walk'
|
|
|
|
return self
|
|
|
|
|
|
|
|
def set_threshold(self, threshold) -> "Waypoint":
|
|
|
|
self.threshold = threshold
|
|
|
|
return self
|
2023-09-21 16:23:00 +00:00
|
|
|
|
2023-08-20 17:28:30 +00:00
|
|
|
def get_threshold(self, end):
|
|
|
|
"""
|
|
|
|
Args:
|
|
|
|
end: True if this is an end point
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
int
|
|
|
|
"""
|
|
|
|
if self.threshold is not None:
|
|
|
|
return self.threshold
|
|
|
|
if end:
|
|
|
|
return self.endpoint_threshold
|
|
|
|
else:
|
|
|
|
return self.waypoint_threshold
|
|
|
|
|
2023-09-21 16:23:00 +00:00
|
|
|
@staticmethod
|
|
|
|
def expected_to_str(results: list) -> list[str]:
|
|
|
|
return [result.__name__ if callable(result) else str(result) for result in results]
|
|
|
|
|
|
|
|
def match_results(self, results) -> list[str]:
|
|
|
|
"""
|
|
|
|
Args:
|
|
|
|
results:
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
list[str]: A list if matched results
|
|
|
|
"""
|
|
|
|
if not results and not self.expected_end:
|
|
|
|
return []
|
|
|
|
|
|
|
|
results = set(self.expected_to_str(results))
|
|
|
|
expected_end = set(self.expected_to_str(self.expected_end))
|
|
|
|
same = results.intersection(expected_end)
|
|
|
|
|
|
|
|
return list(same)
|
|
|
|
|
2023-08-20 17:28:30 +00:00
|
|
|
|
|
|
|
def ensure_waypoint(point) -> Waypoint:
|
|
|
|
"""
|
|
|
|
Args:
|
|
|
|
point: Position (x, y) or Waypoint object
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Waypoint:
|
|
|
|
"""
|
2023-09-21 16:23:00 +00:00
|
|
|
|
2023-08-20 17:28:30 +00:00
|
|
|
if isinstance(point, Waypoint):
|
|
|
|
return point
|
|
|
|
return Waypoint(point)
|
|
|
|
|
|
|
|
|
2023-09-21 16:23:00 +00:00
|
|
|
def ensure_waypoints(points) -> list[Waypoint]:
|
|
|
|
if not isinstance(points, (list, tuple)):
|
|
|
|
points = [points]
|
|
|
|
return [ensure_waypoint(point) for point in points]
|