Files
AVA2/src/scripting/python_item.h
T
shenleiandClaude Opus 5.5 64d25e3571 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>
2026-10-12 22:05:15 +09:00

417 lines
14 KiB
C++

#pragma once
#include "eter_base/Singleton.h"
#include "eter_net/Packet.h"
#include "eter_render/TextTailManager.h"
#include "item_sound.h"
#include "python_gameplay_modules.h"
#include "nonplayer_data.h"
#include <map>
#include <vector>
#include <string>
#include <set>
#include <cmath>
#include <algorithm>
namespace ava2 {
struct TPixelPosition {
float x = 0.0f;
float y = 0.0f;
float z = 0.0f;
};
class CPythonItem : public CSingleton<CPythonItem> {
public:
enum {
INVALID_ID = 0xffffffff,
};
enum {
VNUM_MONEY = 1,
};
enum {
USESOUND_NONE = 0,
USESOUND_DEFAULT = 1,
USESOUND_ARMOR = 2,
USESOUND_WEAPON = 3,
USESOUND_BOW = 4,
USESOUND_ACCESSORY = 5,
USESOUND_POTION = 6,
USESOUND_PORTAL = 7,
USESOUND_NUM = 8,
};
enum {
DROPSOUND_DEFAULT = 0,
DROPSOUND_ARMOR = 1,
DROPSOUND_WEAPON = 2,
DROPSOUND_BOW = 3,
DROPSOUND_ACCESSORY = 4,
DROPSOUND_NUM = 5,
};
struct TGroundItemInstance {
uint32_t dwVirtualNumber = 0;
TPixelPosition v3EndPosition{};
uint32_t eDropSoundType = DROPSOUND_DEFAULT;
bool bAnimEnded = true;
std::string stOwnership;
int32_t alSockets[ITEM_SOCKET_SLOT_MAX_NUM]{};
TPlayerItemAttribute aAttrs[ITEM_ATTRIBUTE_SLOT_MAX_NUM]{};
uint16_t itemCount = 0;
int ownerTime = 0;
void Clear() {
stOwnership.clear();
}
};
using TGroundItemInstanceMap = std::map<uint32_t, TGroundItemInstance*>;
CPythonItem() = default;
virtual ~CPythonItem() {
DeleteAllItems();
}
static CPythonItem& Instance() {
CPythonItem* ptr = InstancePtr();
if (!ptr) {
static CPythonItem s;
return s;
}
return *ptr;
}
void Create() {}
void Destroy() {
DeleteAllItems();
}
void DeleteAllItems() {
for (auto& [vid, inst] : m_GroundItemInstanceMap) {
TextTailManager::Instance().DeleteItemTextTail(vid);
delete inst;
}
m_GroundItemInstanceMap.clear();
m_dwPickedItemID = INVALID_ID;
}
template <typename TSocket = int32_t>
void CreateItem(uint32_t dwVirtualID, uint32_t dwVirtualNumber, float x, float y, float z,
bool bDrop = true,
const TSocket* alSockets = nullptr,
const TPlayerItemAttribute* aAttrs = nullptr,
unsigned short itemCount = 0,
int ownerTime = 0) {
auto it = m_GroundItemInstanceMap.find(dwVirtualID);
TGroundItemInstance* pInst = nullptr;
if (it != m_GroundItemInstanceMap.end()) {
pInst = it->second;
} else {
pInst = new TGroundItemInstance();
m_GroundItemInstanceMap[dwVirtualID] = pInst;
}
pInst->dwVirtualNumber = dwVirtualNumber;
pInst->v3EndPosition = {x, y, z};
pInst->bAnimEnded = !bDrop;
pInst->itemCount = itemCount;
pInst->ownerTime = ownerTime;
if (alSockets) {
for (size_t i = 0; i < ITEM_SOCKET_SLOT_MAX_NUM; ++i)
pInst->alSockets[i] = static_cast<int32_t>(alSockets[i]);
} else {
std::fill(std::begin(pInst->alSockets), std::end(pInst->alSockets), 0);
}
if (aAttrs) {
for (size_t i = 0; i < ITEM_ATTRIBUTE_SLOT_MAX_NUM; ++i)
pInst->aAttrs[i] = aAttrs[i];
} else {
std::fill(std::begin(pInst->aAttrs), std::end(pInst->aAttrs), TPlayerItemAttribute{});
}
std::string baseName;
int itemType = 0;
if (!GetItemNameAndType(dwVirtualNumber, &baseName, &itemType) || baseName.empty()) {
baseName = "Item " + std::to_string(dwVirtualNumber);
}
std::string szItemName;
if (itemType == 17 /* ITEM_TYPE_POLYMORPH */ && alSockets) {
std::string mobName;
if (GetNonPlayerName(uint32_t(alSockets[0]), mobName) && !mobName.empty()) {
if (itemCount > 1) {
szItemName = mobName + " (x" + std::to_string(itemCount) + ") " + baseName;
} else {
szItemName = mobName + " " + baseName;
}
}
}
if (szItemName.empty()) {
if (itemCount > 1) {
szItemName = baseName + " (x" + std::to_string(itemCount) + ")";
} else {
szItemName = baseName;
}
}
bool bHasAttr = false;
if (aAttrs) {
for (size_t i = 0; i < ITEM_ATTRIBUTE_SLOT_MAX_NUM; ++i) {
if (aAttrs[i].bType != 0 && aAttrs[i].sValue != 0) {
bHasAttr = true;
break;
}
}
}
uint32_t color = 0xFFFFFFFF; // default white
if (IsRarityItem(dwVirtualNumber)) {
int rarityType = alSockets ? int(alSockets[6 /* RARITY_TYPE_INDEX */]) : 0;
if (rarityType == 1 /* RARITY_UNCOMMUN */)
color = 0xFFD1D1D1;
else if (rarityType == 2 /* RARITY_RARE */)
color = 0xFF7CA1FF;
else if (rarityType == 3 /* RARITY_EPIC */)
color = 0xFFFF3252;
else if (rarityType == 4 /* RARITY_RELIC */)
color = 0xFF34FF30;
else if (rarityType == 5 /* RARITY_LEGENDARY */)
color = 0xFFFA30FF;
else
color = 0xFF8E8E8E;
} else if (bHasAttr) {
color = 0xFFFFAA00; // gold/orange for attributes
}
TextTailManager::Instance().RegisterItemTextTail(dwVirtualID, szItemName, x, y, z, ownerTime, pInst->stOwnership, false, color);
if (bDrop) {
PlayDropSound(dwVirtualNumber);
}
}
void DeleteItem(uint32_t dwVirtualID) {
auto it = m_GroundItemInstanceMap.find(dwVirtualID);
if (it != m_GroundItemInstanceMap.end()) {
TextTailManager::Instance().DeleteItemTextTail(dwVirtualID);
delete it->second;
m_GroundItemInstanceMap.erase(it);
}
if (m_dwPickedItemID == dwVirtualID)
m_dwPickedItemID = INVALID_ID;
}
void SetOwnership(uint32_t dwVID, const char* c_pszName) {
auto it = m_GroundItemInstanceMap.find(dwVID);
if (it == m_GroundItemInstanceMap.end())
return;
std::string name = c_pszName ? c_pszName : "";
it->second->stOwnership = name;
TextTailManager::Instance().SetItemTextTailOwner(dwVID, name, true);
}
bool GetOwnership(uint32_t dwVID, const char** c_pszName) {
auto it = m_GroundItemInstanceMap.find(dwVID);
if (it == m_GroundItemInstanceMap.end())
return false;
if (c_pszName)
*c_pszName = it->second->stOwnership.c_str();
return true;
}
bool GetGroundItemPosition(uint32_t dwVirtualID, TPixelPosition* pPosition) {
auto it = m_GroundItemInstanceMap.find(dwVirtualID);
if (it == m_GroundItemInstanceMap.end())
return false;
if (pPosition) {
pPosition->x = +it->second->v3EndPosition.x;
pPosition->y = -it->second->v3EndPosition.y;
pPosition->z = +it->second->v3EndPosition.z;
}
return true;
}
bool GetPickedItemID(uint32_t* pdwPickedItemID) {
if (m_dwPickedItemID == INVALID_ID)
return false;
if (pdwPickedItemID)
*pdwPickedItemID = m_dwPickedItemID;
return true;
}
void SetPickedItemID(uint32_t id) {
m_dwPickedItemID = id;
}
uint32_t GetVirtualNumberOfGroundItem(uint32_t dwVID) {
auto it = m_GroundItemInstanceMap.find(dwVID);
if (it == m_GroundItemInstanceMap.end())
return 0;
return it->second->dwVirtualNumber;
}
bool GetCloseMoney(const TPixelPosition& c_rPixelPosition, uint32_t* pdwItemID, uint32_t dwDistance = 300) {
uint32_t dwCloseItemID = 0;
uint32_t dwCloseItemDistance = 1000 * 1000;
for (auto& [vid, pInstance] : m_GroundItemInstanceMap) {
if (pInstance->dwVirtualNumber != VNUM_MONEY)
continue;
float dwxDistance = c_rPixelPosition.x - pInstance->v3EndPosition.x;
float dwyDistance = c_rPixelPosition.y - (-pInstance->v3EndPosition.y);
uint32_t dwDist = uint32_t(dwxDistance * dwxDistance + dwyDistance * dwyDistance);
if (dwDist < dwCloseItemDistance) {
dwCloseItemID = vid;
dwCloseItemDistance = dwDist;
}
}
if (dwCloseItemDistance > float(dwDistance) * float(dwDistance))
return false;
if (pdwItemID)
*pdwItemID = dwCloseItemID;
return true;
}
bool GetCloseItem(const TPixelPosition& c_rPixelPosition, uint32_t* pdwItemID, uint32_t dwDistance = 300, bool specific_pick = false) {
uint32_t dwCloseItemID = 0;
uint32_t dwCloseItemDistance = 1000 * 1000;
for (auto& [vid, pInstance] : m_GroundItemInstanceMap) {
float dwxDistance = c_rPixelPosition.x - pInstance->v3EndPosition.x;
float dwyDistance = c_rPixelPosition.y - (-pInstance->v3EndPosition.y);
uint32_t dwDist = uint32_t(dwxDistance * dwxDistance + dwyDistance * dwyDistance);
if (dwDist < dwCloseItemDistance) {
if (specific_pick) {
const char* c_szOwnerName = "";
if (!GetOwnership(vid, &c_szOwnerName))
continue;
int itemType = 0, itemSubType = 0;
if (!GetItemTypeAndSubType(pInstance->dwVirtualNumber, &itemType, &itemSubType))
continue;
if (itemType == 1 || itemType == 2)
continue;
dwCloseItemID = vid;
dwCloseItemDistance = dwDist;
} else {
dwCloseItemID = vid;
dwCloseItemDistance = dwDist;
}
}
}
if (dwCloseItemDistance > float(dwDistance) * float(dwDistance))
return false;
if (pdwItemID)
*pdwItemID = dwCloseItemID;
return true;
}
bool GetCloseItemVector(const std::string& myName, const TPixelPosition& c_rPixelPosition, std::vector<uint32_t>& itemVIDVector) {
uint32_t dwCloseItemDistance = 1000 * 2000;
for (auto& [vid, pInstance] : m_GroundItemInstanceMap) {
float dwxDistance = c_rPixelPosition.x - pInstance->v3EndPosition.x;
float dwyDistance = c_rPixelPosition.y - (-pInstance->v3EndPosition.y);
uint32_t dwDist = uint32_t(dwxDistance * dwxDistance + dwyDistance * dwyDistance);
if (dwDist < dwCloseItemDistance && (pInstance->stOwnership.empty() || pInstance->stOwnership == myName)) {
itemVIDVector.push_back(vid);
}
}
return true;
}
void PlayUseSound(uint32_t dwItemID) {
PlayItemUseSound(dwItemID);
}
void PlayDropSound(uint32_t dwItemID) {
PlayItemDropSound(dwItemID);
}
void PlayUsePotionSound() {
ava2::PlayUsePotionSound();
}
void SetUseSoundFileName(uint32_t eItemType, const std::string& c_rstFileName) {
SetItemUseSound(eItemType, c_rstFileName);
}
void SetDropSoundFileName(uint32_t eItemType, const std::string& c_rstFileName) {
SetItemDropSound(eItemType, c_rstFileName);
}
bool IsRarityItem(uint32_t itemVnum) {
int itemType = 0, itemSubType = 0;
if (!GetItemTypeAndSubType(itemVnum, &itemType, &itemSubType)) {
if (!GetItemTypeAndSubType(60001, &itemType, &itemSubType))
return false;
}
if (itemType == 2 /* ARMOR */)
return true;
if (itemType == 36 /* RINGS */)
return true;
if (itemType == 1 /* WEAPON */ && itemSubType != 6 && itemSubType != 9)
return true;
return false;
}
long GetRarityMaxValue(uint32_t itemVnum, uint32_t rarityType) {
if (!IsRarityItem(itemVnum))
return 0;
static const long s_maxValues[] = {50000, 75000, 100000, 150000, 200000, 300000};
if (rarityType < 6)
return s_maxValues[rarityType];
return 0;
}
long GetRarityPoint(int attrID, int rarityType) {
static const std::set<int> s_rarityAttrs = {
-4, -3, -2, -1,
19, 1, 5, 7, 6, 8, 15, 16, 12, 13, 17, 9, 10, 37, 3, 2, 41, 24, 23,
59, 60, 61, 62, 54, 55, 56, 57, 27, 28, 43, 44, 64, 65, 63, 39, 40,
38, 34, 35, 36, 71, 72, 53, 74, 75, 76, 77, 78, 79, 80
};
if (s_rarityAttrs.count(attrID) > 0) {
static const long s_points[] = {0, 5, 10, 20, 25, 30};
if (rarityType < 6)
return s_points[rarityType];
}
return 0;
}
void Update() {}
void Render() {}
// Port addition for the auto hunt driver (python_gameplay_modules.cpp ProcessAutoHunt).
const TGroundItemInstanceMap& GetGroundItems() const { return m_GroundItemInstanceMap; }
void GetInfo(std::string* pstInfo) {
if (pstInfo) {
pstInfo->append("Item: Inst " + std::to_string(m_GroundItemInstanceMap.size()));
}
}
protected:
TGroundItemInstanceMap m_GroundItemInstanceMap;
uint32_t m_dwPickedItemID = INVALID_ID;
};
} // namespace ava2
using ava2::CPythonItem;
using ava2::TPixelPosition;