Engine-free AutoHunt state machine (search/engage/loot/return/paused) driven from the game loop through the existing manual paths: move target, target packet, normal attack, quickslot skills, item pick-up and belt potions. player.SetAutoHunt/GetAutoHuntState/SetAutoHuntOption with autohunt.cfg; AUTO button on the combat wheel (tap toggles, long press opens settings). Tests: ava2.auto_hunt, extended ava2.touch_game; 54/54. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
234 lines
6.7 KiB
Python
234 lines
6.7 KiB
Python
# -*- coding: utf-8 -*-
|
|
# 移动端自动狩猎(docs/MOBILE-ARCHITECTURE.md 第 6 步)。原版没有这个功能,是本客户端新增的;
|
|
# 逻辑在原生 src/game/auto_hunt.cpp,只走玩家手动操作本来就有的路径(走路、攻击键、快捷栏技能、
|
|
# 拾取、快捷栏药水)。这里只有 HUD 上的 AUTO 键和设置窗。
|
|
# AUTO 键:轻点开关;长按打开设置
|
|
# 设置项保存在 metin2.cfg 同目录的 autohunt.cfg(原生 player.SetAutoHuntOption 负责读写)
|
|
import ui
|
|
import grp
|
|
import app
|
|
import player
|
|
import chat
|
|
import wndMgr
|
|
import ui_mobile_widgets as widgets
|
|
|
|
Color = widgets.Color
|
|
STYLE_AUTO_ON = (Color(0.20, 0.45, 0.18, 0.90), Color(0.05, 0.15, 0.05, 0.90), Color(0.75, 1.0, 0.55), Color(0.20, 0.40, 0.12), 3.0)
|
|
STYLE_AUTO_ON_DOWN = (Color(0.35, 0.65, 0.30, 0.95), Color(0.10, 0.25, 0.08, 0.95), Color(0.90, 1.0, 0.70), Color(0.30, 0.50, 0.18), 3.0)
|
|
|
|
STATE_TEXT = {
|
|
"search": "Search",
|
|
"engage": "Fight",
|
|
"loot": "Loot",
|
|
"return": "Return",
|
|
"paused": "Paused",
|
|
}
|
|
|
|
|
|
# 设置窗全局一个:HUD 按安全区重建时 AUTO 键会重建,设置窗不跟着重建
|
|
_settings = None
|
|
|
|
|
|
def OpenSettings():
|
|
global _settings
|
|
if not _settings:
|
|
_settings = AutoHuntSettingsWindow()
|
|
_settings.Open()
|
|
|
|
|
|
def Toggle():
|
|
on = player.SetAutoHunt(0 if player.IsAutoHunt() else 1)
|
|
chat.AppendChat(chat.CHAT_TYPE_INFO, "Auto hunt ON" if on else "Auto hunt OFF")
|
|
return on
|
|
|
|
|
|
class AutoHuntButton(widgets.RoundButton):
|
|
"""AUTO 键:轻点开关自动狩猎,长按(0.6 秒)打开设置窗。开启时绿色,下方显示当前状态。"""
|
|
LONG_PRESS = 600
|
|
|
|
def __init__(self, diameter):
|
|
widgets.RoundButton.__init__(self, diameter)
|
|
self.SetLabel("AUTO")
|
|
self.pressTime = 0
|
|
self.longPressed = False
|
|
self.isOn = False
|
|
self.lastState = None
|
|
|
|
self.state_text = ui.TextLine()
|
|
self.state_text.SetParent(self)
|
|
self.state_text.AddFlag("not_pick")
|
|
self.state_text.SetPosition(diameter / 2, diameter + 1)
|
|
self.state_text.SetHorizontalAlignCenter()
|
|
self.state_text.SetOutline()
|
|
self.state_text.SetPackedFontColor(grp.GenerateColor(0.75, 1.0, 0.55, 1.0))
|
|
self.state_text.Show()
|
|
self.__Apply(False)
|
|
|
|
def __del__(self):
|
|
widgets.RoundButton.__del__(self)
|
|
|
|
def __Apply(self, on):
|
|
self.isOn = on
|
|
if on:
|
|
self.style, self.downStyle = STYLE_AUTO_ON, STYLE_AUTO_ON_DOWN
|
|
else:
|
|
self.style, self.downStyle = widgets.STYLE_FRAME, widgets.STYLE_FRAME_DOWN
|
|
self.disc.SetStyle(self.style)
|
|
if not on:
|
|
self.state_text.SetText("")
|
|
self.lastState = None
|
|
|
|
def OnMouseLeftButtonDown(self):
|
|
self.pressTime = app.GetGlobalTime()
|
|
self.longPressed = False
|
|
return widgets.RoundButton.OnMouseLeftButtonDown(self)
|
|
|
|
def OnMouseLeftButtonUp(self):
|
|
self.disc.SetStyle(self.style)
|
|
held = self.pressTime
|
|
self.pressTime = 0
|
|
if self.longPressed or not self.IsIn():
|
|
return True
|
|
if held:
|
|
self.__Apply(Toggle())
|
|
return True
|
|
|
|
def OnUpdate(self):
|
|
if self.pressTime and not self.longPressed and app.GetGlobalTime() - self.pressTime >= self.LONG_PRESS:
|
|
self.longPressed = True
|
|
self.disc.SetStyle(self.style)
|
|
OpenSettings()
|
|
|
|
on = player.IsAutoHunt()
|
|
if on != self.isOn:
|
|
# 死亡等原因由原生自行关闭
|
|
self.__Apply(on)
|
|
if on:
|
|
state = player.GetAutoHuntState()[0]
|
|
if state != self.lastState:
|
|
self.lastState = state
|
|
self.state_text.SetText(STATE_TEXT.get(state, ""))
|
|
|
|
|
|
class AutoHuntSettingsWindow(ui.BoardWithTitleBar):
|
|
"""自动狩猎设置:范围、技能、拾取、打石头、HP/SP 药水阈值。药水用底部药水栏第 1/2 格。"""
|
|
WIDTH = 300
|
|
ROW = 32
|
|
RADIUS_STEP = 500
|
|
PERCENT_STEP = 10
|
|
|
|
def __init__(self):
|
|
ui.BoardWithTitleBar.__init__(self)
|
|
self.SetWindowName("AutoHuntSettings")
|
|
self.AddFlag("movable")
|
|
self.AddFlag("float")
|
|
self.SetTitleName("Auto Hunt")
|
|
self.SetCloseEvent(ui.__mem_func__(self.Close))
|
|
|
|
rows = [
|
|
("radius", "Range", self.__RadiusText),
|
|
("skills", "Use skills", None),
|
|
("pickup", "Pick up items", None),
|
|
("stones", "Attack metin stones", None),
|
|
("hp_percent", "HP potion (belt 1) below", self.__PercentText),
|
|
("sp_percent", "SP potion (belt 2) below", self.__PercentText),
|
|
]
|
|
self.values = {}
|
|
self.parts = []
|
|
y = 38
|
|
for key, label, fmt in rows:
|
|
text = ui.TextLine()
|
|
text.SetParent(self)
|
|
text.SetPosition(16, y + 6)
|
|
text.SetText(label)
|
|
text.Show()
|
|
self.parts.append(text)
|
|
if fmt:
|
|
self.__AddStepper(key, y, fmt)
|
|
else:
|
|
self.__AddToggle(key, y)
|
|
y += self.ROW
|
|
|
|
hint = ui.TextLine()
|
|
hint.SetParent(self)
|
|
hint.SetPosition(16, y + 4)
|
|
hint.SetPackedFontColor(grp.GenerateColor(0.7, 0.7, 0.7, 1.0))
|
|
hint.SetText("Skills: current quick page. Moving pauses 3 s.")
|
|
hint.Show()
|
|
self.parts.append(hint)
|
|
|
|
self.SetSize(self.WIDTH, y + 34)
|
|
self.SetCenterPosition()
|
|
self.Hide()
|
|
|
|
def __del__(self):
|
|
ui.BoardWithTitleBar.__del__(self)
|
|
|
|
def __RadiusText(self, value):
|
|
return "%d m" % (value / 100)
|
|
|
|
def __PercentText(self, value):
|
|
return "Off" if value <= 0 else "%d%%" % value
|
|
|
|
def __SmallButton(self, x, y, text, event):
|
|
button = ui.Button()
|
|
button.SetParent(self)
|
|
button.SetPosition(x, y)
|
|
button.SetUpVisual("d:/ymir work/ui/public/small_button_01.sub")
|
|
button.SetOverVisual("d:/ymir work/ui/public/small_button_02.sub")
|
|
button.SetDownVisual("d:/ymir work/ui/public/small_button_03.sub")
|
|
button.SetText(text)
|
|
button.SetEvent(event)
|
|
button.Show()
|
|
self.parts.append(button)
|
|
return button
|
|
|
|
def __AddStepper(self, key, y, fmt):
|
|
value = ui.TextLine()
|
|
value.SetParent(self)
|
|
value.SetPosition(self.WIDTH - 66, y + 6)
|
|
value.SetHorizontalAlignCenter()
|
|
value.Show()
|
|
self.parts.append(value)
|
|
step = self.RADIUS_STEP if key == "radius" else self.PERCENT_STEP
|
|
self.__SmallButton(self.WIDTH - 118, y + 2, "-", lambda: self.__Step(key, -step))
|
|
self.__SmallButton(self.WIDTH - 58, y + 2, "+", lambda: self.__Step(key, step))
|
|
self.values[key] = (value, fmt)
|
|
|
|
def __AddToggle(self, key, y):
|
|
button = self.__SmallButton(self.WIDTH - 72, y + 2, "", lambda: self.__Flip(key))
|
|
self.values[key] = (button, None)
|
|
|
|
def __Step(self, key, delta):
|
|
value = player.GetAutoHuntOption(key) + delta
|
|
if key == "radius":
|
|
value = max(500, min(5000, value))
|
|
else:
|
|
value = max(0, min(90, value))
|
|
player.SetAutoHuntOption(key, value)
|
|
self.Refresh()
|
|
|
|
def __Flip(self, key):
|
|
player.SetAutoHuntOption(key, 0 if player.GetAutoHuntOption(key) else 1)
|
|
self.Refresh()
|
|
|
|
def Refresh(self):
|
|
for key, (widget, fmt) in self.values.items():
|
|
value = player.GetAutoHuntOption(key)
|
|
if fmt:
|
|
widget.SetText(fmt(value))
|
|
else:
|
|
widget.SetText("On" if value else "Off")
|
|
|
|
def Open(self):
|
|
self.Refresh()
|
|
self.Show()
|
|
self.SetTop()
|
|
|
|
def Close(self):
|
|
self.Hide()
|
|
|
|
def OnPressEscapeKey(self):
|
|
self.Close()
|
|
return True
|