Mobile step 6: client-side auto hunt with AUTO button
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>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
ed2c185135
commit
64d25e3571
@@ -0,0 +1,233 @@
|
||||
# -*- 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
|
||||
@@ -8,6 +8,7 @@ import wndMgr
|
||||
import touch
|
||||
import constInfo
|
||||
import ui_mobile_widgets as widgets
|
||||
import ui_mobile_autohunt
|
||||
|
||||
class MobileCombatWheel(ui.Window):
|
||||
"""
|
||||
@@ -17,10 +18,12 @@ class MobileCombatWheel(ui.Window):
|
||||
- 攻击键外两圈共 8 个快捷格 = 当前快捷页的 8 格(PC 任务栏 1-4 / F1-F4 同一份数据),
|
||||
第 4 页留给底部药水栏,所以这里只在第 1-3 页间切换
|
||||
- 小按钮:快捷页切换、上下马(原版 Ctrl+G 的 /ride)、拾取(原版 ` / Z 键)
|
||||
- AUTO 键(第 6 步自动狩猎,ui_mobile_autohunt.py):轻点开关,长按设置
|
||||
"""
|
||||
ATTACK_RADIUS = 60
|
||||
SLOT_DIAMETER = 50
|
||||
SMALL_DIAMETER = 40
|
||||
AUTO_DIAMETER = 46
|
||||
# (半径, 角度):角度从 +x 逆时针,内外圈各 4 格,外圈错开半格;相邻格间距大于格子直径
|
||||
SLOT_LAYOUT = [(108, 90), (108, 120), (108, 150), (108, 180),
|
||||
(172, 97.5), (172, 122.5), (172, 147.5), (172, 172.5)]
|
||||
@@ -87,6 +90,10 @@ class MobileCombatWheel(ui.Window):
|
||||
self.mount_button.SetLabel("Ride")
|
||||
self.pickup_button = self.__MakeSmall(cx - 86 - d / 2, cy + 82 - d / 2, ui.__mem_func__(self.__OnPickUp))
|
||||
self.pickup_button.SetLabel("Pick")
|
||||
self.auto_button = ui_mobile_autohunt.AutoHuntButton(self.AUTO_DIAMETER)
|
||||
self.auto_button.SetParent(self)
|
||||
self.auto_button.SetPosition(cx + 70 - self.AUTO_DIAMETER / 2, cy - 160 - self.AUTO_DIAMETER / 2)
|
||||
self.auto_button.Show()
|
||||
|
||||
self.page = -1
|
||||
self.lastRefresh = 0
|
||||
@@ -135,6 +142,7 @@ class MobileCombatWheel(ui.Window):
|
||||
if pressed != self.attack_pressed:
|
||||
self.attack_pressed = pressed
|
||||
self.attack_disc.SetStyle(widgets.STYLE_ATTACK_DOWN if pressed else widgets.STYLE_ATTACK)
|
||||
self.auto_button.OnUpdate()
|
||||
|
||||
# 与 uitaskbar.TaskBar.OnUpdate 相同的 500 ms 刷新(冷却、数量、激活状态)
|
||||
if app.GetGlobalTime() - self.lastRefresh > 500:
|
||||
|
||||
Reference in New Issue
Block a user