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:
shenlei
2026-10-12 22:05:15 +09:00
co-authored by Claude Opus 5.5
parent ed2c185135
commit 64d25e3571
13 changed files with 1085 additions and 2 deletions
+195
View File
@@ -15,6 +15,8 @@
#include "game_type.h"
#include "guild_state.h"
#include "skill_cooldown.h"
#include "auto_hunt.h"
#include "python_system.h"
#include "eter_net/PythonNetworkStream.h"
#include "eter_net/NetworkActorManager.h"
#include "eter_render/TerrainHeightmap.h"
@@ -37,6 +39,7 @@
#include <fstream>
#include <sstream>
#include <iostream>
#include <iterator>
#include <cctype>
#include <algorithm>
@@ -1103,6 +1106,106 @@ bool ProcessPlayerAttack(float cameraYaw, float frameDt, bool wasMoving) {
return stopSent;
}
// Mobile step 6 auto hunt (src/game/auto_hunt.h). Fills the logic's view of the world from the
// same state manual play uses and carries out its decisions through the manual paths: actor move
// target (main.cpp sends the move packets), PlayerNewAttack (= held attack key), ClickSkillSlot,
// the pick-up packet and quickslot use.
static AutoHunt s_autoHunt;
static bool IsAutoHuntSkillReady(int skillSlot, float now) {
auto it = s_playerSkills.find(skillSlot);
if (it == s_playerSkills.end() || it->second.index == 0 || it->second.level <= 0) return false;
const int index = it->second.index;
if (index == 121 || index == 122 || index == 123) return false; // fishing / combo
if (GetSkillTypeById(uint32_t(index)) == 3 /* SKILL_TYPE_GUILD */) return false;
if (it->second.cooldown.cooling(now, s_coolTimeEnabled)) return false;
const auto* data = FindSkillDescription(uint32_t(index));
const bool toggle = data && (data->attributes & (1u << 1 /* SKILL_ATTRIBUTE_TOGGLE */));
return !(toggle && it->second.active);
}
static bool IsAutoHuntPotionSlot(int global) {
if (global < 0 || global >= int(s_playerQuickSlots.size())) return false;
const auto& quick = s_playerQuickSlots[global];
return quick.type == 1 /* SLOT_TYPE_INVENTORY */ && GetPlayerItemData(1, uint16_t(quick.pos)).count > 0;
}
static int PointPercent(int cur, int max) {
auto c = s_playerPoints.find(cur), m = s_playerPoints.find(max);
if (c == s_playerPoints.end() || m == s_playerPoints.end() || m->second <= 0) return 100;
return int(c->second * 100 / m->second);
}
void ProcessAutoHunt() {
if (!s_autoHunt.IsOn()) return;
auto& manager = NetworkActorManager::Instance();
auto* main = manager.GetMainInstance();
if (!main || !main->m_pActorInstance) return;
auto& inst = *main->m_pActorInstance;
AutoHuntInput in;
in.now = GetServerMSec();
in.x = inst.GetX();
in.z = inst.GetZ();
in.dead = inst.IsDead();
in.canAct = inst.CanAct();
in.busy = inst.IsAttacking() || inst.IsUsingSkill();
float yaw;
in.manual = s_isAtkKey || GetPlayerMovementYaw(0.0f, yaw) || IsPlayerMouseMoving();
in.hpPercent = PointPercent(5 /* POINT_HP */, 6 /* POINT_MAX_HP */);
in.spPercent = PointPercent(7 /* POINT_SP */, 8 /* POINT_MAX_SP */);
in.attackRange = inst.IsBowMode() ? 900.0f : 150.0f;
in.hpSlotUsable = IsAutoHuntPotionSlot(s_autoHunt.options.hpSlot);
in.spSlotUsable = IsAutoHuntPotionSlot(s_autoHunt.options.spSlot);
for (const auto& [vid, actor] : manager.GetActorDict()) {
if (vid == main->m_dwVID || !actor.m_pActorInstance) continue;
if (actor.m_bType != 0 && actor.m_bType != 2) continue; // TYPE_ENEMY / TYPE_STONE
const auto& v = *actor.m_pActorInstance;
if (v.IsDead()) continue;
in.mobs.push_back({vid, v.GetX(), v.GetZ(), v.GetDefendingRadius(), actor.m_bType == 2});
}
for (const auto& [vid, item] : CPythonItem::Instance().GetGroundItems()) {
if (!item->stOwnership.empty() && item->stOwnership != s_playerName) continue;
in.items.push_back({vid, item->v3EndPosition.x, -item->v3EndPosition.y});
}
// The combat wheel shows quickslot pages 1-3 (ui_mobile_combat.py); page 4 is the potion belt.
const float nowSec = CTimer::Instance().GetCurrentSecond();
const int page = s_quickPage < 3 ? s_quickPage : 0;
for (int local = 0; local < 8; ++local) {
const auto& quick = s_playerQuickSlots[page * 8 + local];
if (quick.type == 2 /* SLOT_TYPE_SKILL */ && IsAutoHuntSkillReady(quick.pos, nowSec))
in.readySkillSlots.push_back(quick.pos);
}
const AutoHuntOutput out = s_autoHunt.Update(in);
if (out.move == AutoHuntOutput::Move::Go) {
inst.SetMoveTarget(out.moveX, out.moveZ);
} else if (out.move == AutoHuntOutput::Move::Stop) {
inst.ClearMoveTarget();
inst.StopMoving();
}
if (out.useQuickSlot >= 0) use_global_quick_slot(out.useQuickSlot);
if (out.target && out.target != s_targetVID) {
s_targetVID = out.target;
PythonNetworkStream::Instance().SendTargetPacket(out.target);
}
if (out.attack) {
if (const auto* victim = manager.GetActorData(out.attack); victim && victim->m_pActorInstance) {
if (!inst.IsAttacking()) {
const float dx = victim->m_pActorInstance->GetX() - inst.GetX();
const float dz = victim->m_pActorInstance->GetZ() - inst.GetZ();
inst.SetYaw(std::fmod(std::atan2(dx, dz) * 180.0f / 3.14159265f + 360.0f, 360.0f));
}
if (out.skillSlot >= 0) ClickSkillSlot(out.skillSlot);
else PlayerNewAttack();
}
}
if (out.pick) PythonNetworkStream::Instance().SendItemPickUpPacket(out.pick);
if (out.stopped) player_window_call("OnAutoHuntStopped");
}
bool IsAutoHuntOn() { return s_autoHunt.IsOn(); }
static float s_fCmrRotSpd = 0.0f;
static uint32_t s_inGuildAreaID = 0xffffffff;
@@ -3524,6 +3627,93 @@ static PyObject* player_GetItemUnSealLeftTime(PyObject*, PyObject*) {
return Py_BuildValue("i", 0);
}
// Mobile step 6 auto hunt (ui_mobile_autohunt.py). Not in the original player module.
static void auto_hunt_load();
static PyObject* player_SetAutoHunt(PyObject*, PyObject* args) {
int on = 0;
if (!PyArg_ParseTuple(args, "i", &on)) return nullptr;
auto_hunt_load();
if (on && !s_autoHunt.IsOn()) {
const auto* main = NetworkActorManager::Instance().GetMainInstance();
if (main && main->m_pActorInstance) s_autoHunt.Start(main->m_pActorInstance->GetX(), main->m_pActorInstance->GetZ());
} else if (!on && s_autoHunt.IsOn()) {
s_autoHunt.Stop();
if (auto* main = NetworkActorManager::Instance().GetMainInstance(); main && main->m_pActorInstance) {
main->m_pActorInstance->ClearMoveTarget();
main->m_pActorInstance->StopMoving();
}
}
return PyInt_FromLong(s_autoHunt.IsOn());
}
static PyObject* player_IsAutoHunt(PyObject*, PyObject*) { return PyInt_FromLong(s_autoHunt.IsOn()); }
static PyObject* player_GetAutoHuntState(PyObject*, PyObject*) {
return Py_BuildValue("(si)", AutoHuntStateName(s_autoHunt.GetState()), int(s_autoHunt.GetTarget()));
}
static int* auto_hunt_int_option(const std::string& name) {
auto& o = s_autoHunt.options;
if (name == "hp_percent") return &o.hpPercent;
if (name == "sp_percent") return &o.spPercent;
if (name == "hp_slot") return &o.hpSlot;
if (name == "sp_slot") return &o.spSlot;
return nullptr;
}
static bool* auto_hunt_bool_option(const std::string& name) {
auto& o = s_autoHunt.options;
if (name == "skills") return &o.useSkills;
if (name == "pickup") return &o.pickUp;
if (name == "stones") return &o.attackStones;
return nullptr;
}
// autohunt.cfg next to metin2.cfg (CPythonSystem config directory), "name value" per line.
static std::string auto_hunt_config_path() {
std::string directory = CPythonSystem::GetConfigDirectory();
if (!directory.empty() && directory.back() != '/' && directory.back() != '\\') directory += '/';
return directory + "autohunt.cfg";
}
static void auto_hunt_set(const std::string& name, int value) {
if (name == "radius") s_autoHunt.options.radius = float(std::clamp(value, 300, 5000));
else if (int* i = auto_hunt_int_option(name)) *i = name == "hp_slot" || name == "sp_slot" ? std::clamp(value, 0, 31) : std::clamp(value, 0, 100);
else if (bool* b = auto_hunt_bool_option(name)) *b = value != 0;
}
static void auto_hunt_load() {
static bool loaded = false;
if (loaded) return;
loaded = true;
std::ifstream file(auto_hunt_config_path());
std::string name; int value;
while (file >> name >> value) auto_hunt_set(name, value);
}
static void auto_hunt_save() {
const auto& o = s_autoHunt.options;
std::ofstream file(auto_hunt_config_path(), std::ios::trunc);
file << "radius " << int(o.radius) << "\nskills " << o.useSkills << "\npickup " << o.pickUp
<< "\nstones " << o.attackStones << "\nhp_percent " << o.hpPercent << "\nsp_percent " << o.spPercent
<< "\nhp_slot " << o.hpSlot << "\nsp_slot " << o.spSlot << "\n";
}
static PyObject* player_SetAutoHuntOption(PyObject*, PyObject* args) {
char* name = nullptr; int value = 0;
if (!PyArg_ParseTuple(args, "si", &name, &value)) return nullptr;
const std::string key(name);
if (key != "radius" && !auto_hunt_int_option(key) && !auto_hunt_bool_option(key)) {
PyErr_Format(PyExc_KeyError, "%s", name);
return nullptr;
}
auto_hunt_load();
auto_hunt_set(key, value);
auto_hunt_save();
Py_RETURN_NONE;
}
static PyObject* player_GetAutoHuntOption(PyObject*, PyObject* args) {
char* name = nullptr;
if (!PyArg_ParseTuple(args, "s", &name)) return nullptr;
auto_hunt_load();
if (std::string(name) == "radius") return PyInt_FromLong(long(s_autoHunt.options.radius));
if (int* i = auto_hunt_int_option(name)) return PyInt_FromLong(*i);
if (bool* b = auto_hunt_bool_option(name)) return PyInt_FromLong(*b);
PyErr_Format(PyExc_KeyError, "%s", name);
return nullptr;
}
void initPlayer() {
static PyMethodDef methods[] = {
{"GetAutoPotionInfo", player_GetAutoPotionInfo, METH_VARARGS, nullptr},
@@ -3691,6 +3881,11 @@ void initPlayer() {
{"GetGem", player_GetGem, METH_VARARGS, nullptr},
{"SetEventGame", player_SetEventGame, METH_VARARGS, nullptr},
{"GetEventGame", player_GetEventGame, METH_VARARGS, nullptr},
{"SetAutoHunt", player_SetAutoHunt, METH_VARARGS, nullptr},
{"IsAutoHunt", player_IsAutoHunt, METH_VARARGS, nullptr},
{"GetAutoHuntState", player_GetAutoHuntState, METH_VARARGS, nullptr},
{"SetAutoHuntOption", player_SetAutoHuntOption, METH_VARARGS, nullptr},
{"GetAutoHuntOption", player_GetAutoHuntOption, METH_VARARGS, nullptr},
{nullptr, nullptr, 0, nullptr}
};
NetworkActorManager::SetPartyMemberQuery(IsPartyMemberByVID);