AVA2 cross-platform client parity baseline

This commit is contained in:
shen
2026-10-06 00:53:42 -07:00
commit 9f748eb05a
3271 changed files with 1275528 additions and 0 deletions
+564
View File
@@ -0,0 +1,564 @@
#include "ActorInstance.h"
#include "MapObjectManager.h"
#include "CRenderTarget.h"
#include <filesystem>
#include <cmath>
#include <algorithm>
#include <iostream>
namespace ava2 {
constexpr float DEG_TO_RAD = 3.14159265358979323846f / 180.0f;
constexpr float RAD_TO_DEG = 180.0f / 3.14159265358979323846f;
ActorInstance::ActorInstance() = default;
std::string ActorInstance::ResolvePath(const std::string& path) const {
std::string clean = path;
if (clean.rfind("d:/", 0) == 0 || clean.rfind("D:/", 0) == 0) {
clean = clean.substr(3);
}
if (std::filesystem::exists(clean)) return clean;
if (std::filesystem::exists("build/dev/" + clean)) return "build/dev/" + clean;
if (std::filesystem::exists("../" + clean)) return "../" + clean;
return clean;
}
bool ActorInstance::SetModel(const std::string& modelPath) {
std::string resolved = ResolvePath(modelPath);
if (!std::filesystem::exists(resolved)) {
std::cerr << "ActorInstance::SetModel file not found: " << modelPath << std::endl;
return false;
}
m_model = Gr2ModelInstance::Load(resolved);
if (!m_model) return false;
m_modelPath = modelPath;
return true;
}
bool ActorInstance::SetWeapon(const std::string& weaponPath, const std::string& boneName) {
if (!m_model) return false;
std::string resolved = ResolvePath(weaponPath);
if (!std::filesystem::exists(resolved)) return false;
auto weaponModel = Gr2ModelInstance::Load(resolved);
if (!weaponModel) {
std::string fallback = ResolvePath("ymir work/item/weapon/set_11_sword.gr2");
if (std::filesystem::exists(fallback)) {
weaponModel = Gr2ModelInstance::Load(fallback);
}
}
if (!weaponModel) return false;
m_model->AttachModel(boneName, std::move(weaponModel));
return true;
}
bool ActorInstance::AttachPart(const std::string& partPath) {
if (!m_model) return false;
std::string resolved = ResolvePath(partPath);
if (!std::filesystem::exists(resolved)) return false;
auto partModel = Gr2ModelInstance::Load(resolved);
if (!partModel) return false;
m_model->AttachPart(std::move(partModel));
return true;
}
void ActorInstance::RegisterMotion(MotionType type, const std::string& motionGr2Path) {
m_motionPaths[type] = motionGr2Path;
}
bool ActorInstance::PlayMotion(MotionType type, bool loop) {
if (!m_model) return false;
auto it = m_motionPaths.find(type);
if (it == m_motionPaths.end()) {
// Fallback: If combo attack not found, try generic attack
if (type == MotionType::ComboAttack1 || type == MotionType::ComboAttack2 || type == MotionType::ComboAttack3) {
it = m_motionPaths.find(MotionType::Attack);
}
}
if (it == m_motionPaths.end()) return false;
std::string resolved = ResolvePath(it->second);
if (!std::filesystem::exists(resolved)) return false;
bool loaded = m_model->LoadExternalAnimation(resolved);
if (!loaded) return false;
m_currentMotion = type;
m_animTime = 0.0f;
m_motionDuration = std::max(0.1f, m_model->GetAnimationDuration());
m_loopMotion = loop;
if (type == MotionType::Attack || type == MotionType::ComboAttack1 ||
type == MotionType::ComboAttack2 || type == MotionType::ComboAttack3 ||
type == MotionType::Skill1 || type == MotionType::Skill2 ||
type == MotionType::Skill3 || type == MotionType::Skill4 ||
type == MotionType::Skill5) {
m_isAttacking = true;
} else {
m_isAttacking = false;
}
return true;
}
bool ActorInstance::IsMotionFinished() const {
if (m_loopMotion) return false;
return m_animTime >= m_motionDuration;
}
void ActorInstance::MoveInDirection(float moveYaw, float dt, const TerrainHeightmap* terrain, const MapObjectManager* mapObjects) {
if (dt <= 0.0f) return;
float rad = moveYaw * DEG_TO_RAD;
m_yaw = moveYaw;
float dx = std::sin(rad) * m_moveSpeed * dt;
float dz = std::cos(rad) * m_moveSpeed * dt;
auto isBlocked = [&](float testX, float testZ) -> bool {
if (terrain && terrain->HasAttrMap() && terrain->IsAttrOn(testX, testZ, TerrainHeightmap::ATTR_FLAG_BLOCK)) {
return true;
}
if (mapObjects && mapObjects->CheckCollision(testX, testZ, 30.0f)) {
return true;
}
return false;
};
float nextX = m_posX + dx;
float nextZ = m_posZ + dz;
if (!isBlocked(nextX, nextZ)) {
m_posX = nextX;
m_posZ = nextZ;
} else {
// Wall sliding: attempt to slide along X axis only
if (!isBlocked(nextX, m_posZ)) {
m_posX = nextX;
} else if (!isBlocked(m_posX, nextZ)) {
// Attempt to slide along Z axis only
m_posZ = nextZ;
} else {
// Check building collision normal for sliding: v' = v - (v . n) * n
float nx = 0.0f, nz = 0.0f;
if (mapObjects && mapObjects->CheckCollisionNormal(nextX, nextZ, 30.0f, nx, nz)) {
float dot = dx * nx + dz * nz;
float slideX = dx - dot * nx;
float slideZ = dz - dot * nz;
if (!isBlocked(m_posX + slideX, m_posZ + slideZ)) {
m_posX += slideX;
m_posZ += slideZ;
}
} else if (m_hasMoveTarget) {
ClearMoveTarget();
StopMoving();
}
}
}
if (terrain) {
m_posY = terrain->GetHeightAtWorld(m_posX, m_posZ);
}
if (!m_isAttacking && m_currentMotion != MotionType::Run) {
PlayMotion(MotionType::Run, true);
}
}
void ActorInstance::StopMoving() {
if (!m_isAttacking && m_currentMotion == MotionType::Run) {
PlayMotion(MotionType::Wait, true);
}
}
void ActorInstance::Update(float dt, const TerrainHeightmap* terrain, const MapObjectManager* mapObjects) {
if (dt <= 0.0f) return;
// 0. Click-to-Move destination pathing (aligned with 40250 CPythonPlayer)
if (m_hasMoveTarget && !m_isAttacking && m_curHP > 0) {
float dx = m_targetX - m_posX;
float dz = m_targetZ - m_posZ;
float dist = std::sqrt(dx * dx + dz * dz);
float step = m_moveSpeed * dt;
if (dist <= 15.0f || dist <= step) {
bool blocked = false;
if (terrain && terrain->HasAttrMap() && terrain->IsAttrOn(m_targetX, m_targetZ, TerrainHeightmap::ATTR_FLAG_BLOCK)) blocked = true;
if (mapObjects && mapObjects->CheckCollision(m_targetX, m_targetZ, 30.0f)) blocked = true;
if (!blocked) {
m_posX = m_targetX;
m_posZ = m_targetZ;
}
if (terrain) m_posY = terrain->GetHeightAtWorld(m_posX, m_posZ);
StopMoving();
m_hasMoveTarget = false;
} else {
float moveYaw = std::atan2(dx, dz) * RAD_TO_DEG;
MoveInDirection(moveYaw, dt, terrain, mapObjects);
}
}
// Apply knockback impulse
if (std::abs(m_knockbackVx) > 0.01f || std::abs(m_knockbackVz) > 0.01f) {
float nextX = m_posX + m_knockbackVx * dt;
float nextZ = m_posZ + m_knockbackVz * dt;
if (!terrain || !terrain->HasAttrMap() || !terrain->IsAttrOn(nextX, nextZ, TerrainHeightmap::ATTR_FLAG_BLOCK)) {
m_posX = nextX;
m_posZ = nextZ;
} else {
m_knockbackVx = 0.0f;
m_knockbackVz = 0.0f;
}
m_knockbackVx *= std::max(0.0f, 1.0f - 8.0f * dt);
m_knockbackVz *= std::max(0.0f, 1.0f - 8.0f * dt);
}
// Clamp Y to terrain height
if (terrain) {
m_posY = terrain->GetHeightAtWorld(m_posX, m_posZ);
}
// Update active buffs
for (auto it = m_buffs.begin(); it != m_buffs.end(); ) {
it->second -= dt;
if (it->second <= 0.0f) {
if (it->first == "berserk") {
m_moveSpeed = 350.0f;
}
it = m_buffs.erase(it);
} else {
++it;
}
}
// Advance animation time
m_animTime += dt;
if (m_loopMotion) {
if (m_motionDuration > 0.001f && m_animTime >= m_motionDuration) {
m_animTime = std::fmod(m_animTime, m_motionDuration);
}
} else {
if (m_animTime >= m_motionDuration) {
m_isAttacking = false;
// On one-shot finish (e.g. attack or hit), return to wait or dead
if (m_curHP <= 0) {
// Keep dead pose or freeze at end of dead motion
m_animTime = m_motionDuration - 0.01f;
} else {
PlayMotion(MotionType::Wait, true);
}
}
}
}
bool ActorInstance::Attack() {
if (m_curHP <= 0) return false;
if (!m_isAttacking) {
m_comboIndex = 1;
if (!PlayMotion(MotionType::ComboAttack1, false)) {
PlayMotion(MotionType::Attack, false);
}
return true;
} else if (m_animTime > m_motionDuration * 0.35f) {
// Advance combo chain
m_comboIndex = (m_comboIndex % 3) + 1;
MotionType next = MotionType::ComboAttack1;
if (m_comboIndex == 2) next = MotionType::ComboAttack2;
else if (m_comboIndex == 3) next = MotionType::ComboAttack3;
if (!PlayMotion(next, false)) {
PlayMotion(MotionType::Attack, false);
}
return true;
}
return false;
}
bool ActorInstance::CastSkill(MotionType skillMotion) {
if (m_curHP <= 0) return false;
ClearMoveTarget();
StopMoving();
m_isAttacking = true;
m_animTime = 0.0f;
return PlayMotion(skillMotion, false);
}
void ActorInstance::SetBuff(const std::string& buffName, float duration) {
m_buffs[buffName] = duration;
if (buffName == "berserk") {
m_moveSpeed = 440.0f;
}
}
bool ActorInstance::HasBuff(const std::string& buffName) const {
auto it = m_buffs.find(buffName);
return it != m_buffs.end() && it->second > 0.0f;
}
float ActorInstance::GetBuffRemaining(const std::string& buffName) const {
auto it = m_buffs.find(buffName);
return (it != m_buffs.end()) ? it->second : 0.0f;
}
float ActorInstance::GetAttackMultiplier() const {
float mult = 1.0f;
if (HasBuff("berserk")) mult += 0.40f;
return mult;
}
int ActorInstance::GetBonusAttackDamage() const {
int bonus = 0;
if (HasBuff("aura")) bonus += 120;
return bonus;
}
void ActorInstance::Die() {
m_curHP = 0;
StopMoving();
PlayMotion(MotionType::Dead, false);
}
void ActorInstance::TakeDamage(int damage, float knockbackDirX, float knockbackDirZ) {
m_curHP = std::max(0, m_curHP - damage);
if (m_curHP <= 0) {
if (!PlayMotion(MotionType::Dead, false)) {
PlayMotion(MotionType::Damage, false);
}
} else {
PlayMotion(MotionType::Damage, false);
}
float len = std::sqrt(knockbackDirX * knockbackDirX + knockbackDirZ * knockbackDirZ);
if (len > 0.001f) {
m_knockbackVx = (knockbackDirX / len) * 260.0f;
m_knockbackVz = (knockbackDirZ / len) * 260.0f;
}
}
void ActorInstance::GetHeadPosition(float& hX, float& hY, float& hZ) const {
hX = m_posX;
hY = m_posY + (m_heightOffset + m_modelYOffset) * m_scale;
hZ = m_posZ;
}
bool ActorInstance::IsTargetInAttackRange(const ActorInstance& target, float maxRange, float maxAngleDeg) const {
float dx = target.GetX() - m_posX;
float dz = target.GetZ() - m_posZ;
float dist = std::sqrt(dx * dx + dz * dz);
if (dist > maxRange) return false;
float dirAngle = std::atan2(dx, dz) * RAD_TO_DEG;
float diff = std::abs(std::fmod(dirAngle - m_yaw + 540.0f, 360.0f) - 180.0f);
return diff <= maxAngleDeg;
}
std::vector<Render3DDraw> ActorInstance::BuildDrawCommands() {
if (!m_model) return {};
float world[16];
MakeTransformMatrix(m_yaw + 180.0f, m_posX, m_posY + m_modelYOffset * m_scale, m_posZ, m_scale, world);
auto draws = m_model->BuildDrawCommands(m_animTime, world);
// Shining system (ENABLE_SHINING_SYSTEM): weapon aura / glow
if (m_weaponShining > 0) {
float hx = 0, hy = 0, hz = 0;
if (GetBonePosition("equip_right_hand", hx, hy, hz) || GetBonePosition("Bip01 R Hand", hx, hy, hz)) {
Render3DDraw sDraw;
sDraw.alpha_blend = true;
sDraw.blend_mode = 1; // Additive
sDraw.z_write = false;
sDraw.lighting = false;
float sz = 20.0f + m_weaponShining * 4.0f;
sDraw.positions = {
hx - sz, hy - sz, hz,
hx + sz, hy - sz, hz,
hx + sz, hy + sz, hz,
hx - sz, hy + sz, hz
};
sDraw.uv0 = {0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f};
sDraw.indices = {0, 1, 2, 0, 2, 3};
sDraw.texture0 = !m_weaponShiningPath.empty() ? m_weaponShiningPath : "ymir work/effect/shiningsystem/shining00.dds";
for (int k = 0; k < 16; ++k) sDraw.world[k] = (k % 5 == 0) ? 1.0f : 0.0f;
draws.push_back(std::move(sDraw));
}
}
// Shining system: armor body glow
if (m_armorShining > 0) {
float ax = m_posX, ay = m_posY + 100.0f * m_scale, az = m_posZ;
Render3DDraw aDraw;
aDraw.alpha_blend = true;
aDraw.blend_mode = 1; // Additive
aDraw.z_write = false;
aDraw.lighting = false;
float sz = 35.0f + m_armorShining * 6.0f;
aDraw.positions = {
ax - sz, ay - sz, az,
ax + sz, ay - sz, az,
ax + sz, ay + sz, az,
ax - sz, ay + sz, az
};
aDraw.uv0 = {0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f};
aDraw.indices = {0, 1, 2, 0, 2, 3};
aDraw.texture0 = !m_armorShiningPath.empty() ? m_armorShiningPath : "ymir work/effect/shiningsystem/shining01.dds";
for (int k = 0; k < 16; ++k) aDraw.world[k] = (k % 5 == 0) ? 1.0f : 0.0f;
draws.push_back(std::move(aDraw));
}
// Shining system: acce wing glow
if (m_acceShining > 0) {
float wx = 0, wy = 0, wz = 0;
if (GetBonePosition("Bip01 Spine2", wx, wy, wz) || GetBonePosition("Bip01 Spine1", wx, wy, wz)) {
Render3DDraw wDraw;
wDraw.alpha_blend = true;
wDraw.blend_mode = 1; // Additive
wDraw.z_write = false;
wDraw.lighting = false;
float sz = 45.0f + m_acceShining * 7.0f;
wDraw.positions = {
wx - sz, wy - sz, wz,
wx + sz, wy - sz, wz,
wx + sz, wy + sz, wz,
wx - sz, wy + sz, wz
};
wDraw.uv0 = {0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f};
wDraw.indices = {0, 1, 2, 0, 2, 3};
wDraw.texture0 = !m_acceShiningPath.empty() ? m_acceShiningPath : "ymir work/effect/shiningsystem/shining_wing.dds";
for (int k = 0; k < 16; ++k) wDraw.world[k] = (k % 5 == 0) ? 1.0f : 0.0f;
draws.push_back(std::move(wDraw));
}
}
return draws;
}
bool ActorInstance::HitTestScreen(float clickX, float clickY, const Gr2SceneRenderer& renderer, float tolerancePx) const {
if (m_curHP <= 0) return false;
float bx = 0.0f, by = 0.0f, hx = 0.0f, hy = 0.0f;
if (!renderer.ProjectToScreen(m_posX, m_posY, m_posZ, bx, by)) return false;
float topY = m_posY + m_heightOffset * m_scale;
if (!renderer.ProjectToScreen(m_posX, topY, m_posZ, hx, hy)) return false;
float segX = hx - bx;
float segY = hy - by;
float segLenSq = segX * segX + segY * segY;
float t = 0.0f;
if (segLenSq > 1e-4f) {
t = std::clamp(((clickX - bx) * segX + (clickY - by) * segY) / segLenSq, 0.0f, 1.0f);
}
float projX = bx + t * segX;
float projY = by + t * segY;
float dist = std::sqrt((clickX - projX) * (clickX - projX) + (clickY - projY) * (clickY - projY));
return dist <= tolerancePx;
}
bool ActorInstance::GetBoneWorldTransform(const std::string& boneName, float* outWorld16) const {
if (!m_model || !outWorld16) return false;
float localBone[16];
if (!m_model->GetBoneWorldTransform(boneName, localBone, m_animTime)) {
return false;
}
float actorWorld[16];
MakeTransformMatrix(m_yaw + 180.0f, m_posX, m_posY + m_modelYOffset * m_scale, m_posZ, m_scale, actorWorld);
for (int r = 0; r < 4; ++r) {
for (int c = 0; c < 4; ++c) {
float sum = 0.0f;
for (int k = 0; k < 4; ++k) {
sum += localBone[r * 4 + k] * actorWorld[k * 4 + c];
}
outWorld16[r * 4 + c] = sum;
}
}
return true;
}
bool ActorInstance::GetBonePosition(const std::string& boneName, float& outX, float& outY, float& outZ) const {
float mat[16];
if (GetBoneWorldTransform(boneName, mat)) {
outX = mat[12];
outY = mat[13];
outZ = mat[14];
return true;
}
return false;
}
bool ActorInstance::SetPolymorph(uint32_t mobVnum) {
std::string mobModel = CRenderTarget::ResolveNpcModel(mobVnum);
if (mobModel.empty() || !std::filesystem::exists(ResolvePath(mobModel))) {
return false;
}
if (!m_isPolymorph) {
m_origModelPath = m_modelPath;
m_origScale = m_scale;
m_origModelYOffset = m_modelYOffset;
m_origMotions = m_motionPaths;
}
if (!SetModel(mobModel)) return false;
std::filesystem::path p(ResolvePath(mobModel));
std::string parentDir = p.parent_path().string() + "/";
m_motionPaths.clear();
auto tryRegister = [&](MotionType mt, const std::string& name) {
std::string cand = parentDir + name;
if (std::filesystem::exists(cand)) {
RegisterMotion(mt, cand);
}
};
tryRegister(MotionType::Wait, "00.gr2");
tryRegister(MotionType::Walk, "02.gr2");
tryRegister(MotionType::Run, "03.gr2");
tryRegister(MotionType::Attack, "20.gr2");
tryRegister(MotionType::ComboAttack1, "20.gr2");
tryRegister(MotionType::ComboAttack2, "20_1.gr2");
tryRegister(MotionType::Damage, "30.gr2");
tryRegister(MotionType::Dead, "31.gr2");
PlayMotion(MotionType::Wait, true);
m_isPolymorph = true;
m_polymorphVnum = mobVnum;
return true;
}
void ActorInstance::ClearPolymorph() {
if (!m_isPolymorph) return;
if (!m_origModelPath.empty()) {
SetModel(m_origModelPath);
}
m_scale = m_origScale;
m_modelYOffset = m_origModelYOffset;
m_motionPaths = m_origMotions;
PlayMotion(MotionType::Wait, true);
m_isPolymorph = false;
m_polymorphVnum = 0;
}
void ActorInstance::SetWeaponShining(int grade, const std::string& effectPath) {
m_weaponShining = grade;
m_weaponShiningPath = effectPath;
}
void ActorInstance::SetArmorShining(int grade, const std::string& effectPath) {
m_armorShining = grade;
m_armorShiningPath = effectPath;
}
void ActorInstance::SetAcceShining(int grade, const std::string& effectPath) {
m_acceShining = grade;
m_acceShiningPath = effectPath;
}
void ActorInstance::ClearShining() {
m_weaponShining = 0;
m_weaponShiningPath.clear();
m_armorShining = 0;
m_armorShiningPath.clear();
m_acceShining = 0;
m_acceShiningPath.clear();
}
} // namespace ava2