Fix cross-platform rendering and optimize frame performance
Correct selection orientation, game-entry lifecycle, mounted NPCs, labels and skill panels. Add the native GPU scene path, resident geometry, bounded texture and Python caches, and collision broad phase. Pin the independently maintained Metin2 SDK through a submodule and document reproducible setup. Include rendering regressions and macOS packaging improvements. Validation: client Debug build and 12 targeted client tests passed; SDK build and all 14 SDK tests passed.
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
#include "MapObjectManager.h"
|
||||
#include <limits>
|
||||
#include "src/eter_render/SpeedTreeForest.h"
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
@@ -130,7 +131,7 @@ std::shared_ptr<Gr2ModelInstance> MapObjectManager::GetOrLoadModel(const std::st
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
bool MapObjectManager::LoadAreaData(const std::string& areadata_path, float coord_scale, SpeedTreeForest* forest) {
|
||||
bool MapObjectManager::LoadAreaData(const std::string& areadata_path, float coord_scale, SpeedTreeForest* forest, bool native_y) {
|
||||
std::ifstream file(areadata_path);
|
||||
if (!file.is_open()) {
|
||||
std::cerr << "MapObjectManager::LoadAreaData failed to open: " << areadata_path << std::endl;
|
||||
@@ -166,6 +167,8 @@ bool MapObjectManager::LoadAreaData(const std::string& areadata_path, float coor
|
||||
|
||||
std::istringstream iss(block_lines[0]);
|
||||
iss >> px >> py >> pz;
|
||||
// AVA2 areadata stores the render-space Y sign; packets use positive map Y.
|
||||
if (native_y) py = -py;
|
||||
|
||||
try {
|
||||
crc = static_cast<uint32_t>(std::stoul(block_lines[1]));
|
||||
@@ -182,6 +185,12 @@ bool MapObjectManager::LoadAreaData(const std::string& areadata_path, float coor
|
||||
if (s2 != std::string::npos) {
|
||||
pitch = std::stof(rot_str.substr(s1 + 1, s2 - s1 - 1));
|
||||
roll = std::stof(rot_str.substr(s2 + 1));
|
||||
if (native_y) {
|
||||
// Native roll rotates about Z (up); that is engine Y yaw.
|
||||
const float nativeYaw = yaw;
|
||||
yaw = roll;
|
||||
roll = -nativeYaw;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
try { yaw = std::stof(rot_str); } catch (...) {}
|
||||
@@ -296,8 +305,44 @@ bool MapObjectManager::AddObject(const std::string& model_path, float x, float y
|
||||
return true;
|
||||
}
|
||||
|
||||
const std::vector<size_t>& MapObjectManager::CollisionCandidates(float x,float z,float radius) const {
|
||||
constexpr double cell=512;
|
||||
if(m_collisionCount!=m_objects.size()) {
|
||||
m_collisionCells.clear();m_largeCollisionObjects.clear();m_allCollisionObjects.clear();
|
||||
for(size_t i=0;i<m_objects.size();++i) {
|
||||
const auto& obj=m_objects[i];if(!obj.has_collision)continue;
|
||||
m_allCollisionObjects.push_back(i);
|
||||
const double r=std::max(std::abs(obj.obb.halfExtents[0]),std::abs(obj.obb.halfExtents[2]))*1.5;
|
||||
const double cx=obj.obb.centerX,cz=obj.obb.centerZ;
|
||||
if(!std::isfinite(r) || !std::isfinite(cx) || !std::isfinite(cz) || std::abs(cx)+r>1e9 || std::abs(cz)+r>1e9 || r>4096) {
|
||||
m_largeCollisionObjects.push_back(i);continue;
|
||||
}
|
||||
const auto x0=int64_t(std::floor((cx-r)/cell)),x1=int64_t(std::floor((cx+r)/cell));
|
||||
const auto z0=int64_t(std::floor((cz-r)/cell)),z1=int64_t(std::floor((cz+r)/cell));
|
||||
if((x1-x0+1)*(z1-z0+1)>256){m_largeCollisionObjects.push_back(i);continue;}
|
||||
for(auto gx=x0;gx<=x1;++gx)for(auto gz=z0;gz<=z1;++gz)m_collisionCells[{gx,gz}].push_back(i);
|
||||
}
|
||||
m_collisionCount=m_objects.size();
|
||||
}
|
||||
m_collisionCandidates.clear();
|
||||
if(!std::isfinite(x) || !std::isfinite(z) || !std::isfinite(radius) || radius<0 || radius>4096 || std::abs(double(x))+radius>1e9 || std::abs(double(z))+radius>1e9) {
|
||||
m_collisionCandidates=m_allCollisionObjects;return m_collisionCandidates;
|
||||
}
|
||||
m_collisionCandidates.insert(m_collisionCandidates.end(),m_largeCollisionObjects.begin(),m_largeCollisionObjects.end());
|
||||
const auto x0=int64_t(std::floor((double(x)-radius)/cell)),x1=int64_t(std::floor((double(x)+radius)/cell));
|
||||
const auto z0=int64_t(std::floor((double(z)-radius)/cell)),z1=int64_t(std::floor((double(z)+radius)/cell));
|
||||
for(auto gx=x0;gx<=x1;++gx)for(auto gz=z0;gz<=z1;++gz) {
|
||||
auto it=m_collisionCells.find({gx,gz});if(it!=m_collisionCells.end())m_collisionCandidates.insert(m_collisionCandidates.end(),it->second.begin(),it->second.end());
|
||||
}
|
||||
// Preserve the original first-hit normal when OBBs overlap, independent of cell order.
|
||||
std::sort(m_collisionCandidates.begin(),m_collisionCandidates.end());
|
||||
m_collisionCandidates.erase(std::unique(m_collisionCandidates.begin(),m_collisionCandidates.end()),m_collisionCandidates.end());
|
||||
return m_collisionCandidates;
|
||||
}
|
||||
|
||||
bool MapObjectManager::CheckCollision(float worldX, float worldZ, float actorRadius) const {
|
||||
for (const auto& obj : m_objects) {
|
||||
for (auto index : CollisionCandidates(worldX,worldZ,actorRadius)) {
|
||||
const auto& obj=m_objects[index];
|
||||
if (!obj.has_collision) continue;
|
||||
float maxR = std::max(obj.obb.halfExtents[0], obj.obb.halfExtents[2]) * 1.5f + actorRadius;
|
||||
float dx = worldX - obj.obb.centerX;
|
||||
@@ -312,7 +357,8 @@ bool MapObjectManager::CheckCollision(float worldX, float worldZ, float actorRad
|
||||
}
|
||||
|
||||
bool MapObjectManager::CheckCollisionNormal(float worldX, float worldZ, float actorRadius, float& outNx, float& outNz) const {
|
||||
for (const auto& obj : m_objects) {
|
||||
for (auto index : CollisionCandidates(worldX,worldZ,actorRadius)) {
|
||||
const auto& obj=m_objects[index];
|
||||
if (!obj.has_collision) continue;
|
||||
float maxR = std::max(obj.obb.halfExtents[0], obj.obb.halfExtents[2]) * 1.5f + actorRadius;
|
||||
float dx = worldX - obj.obb.centerX;
|
||||
@@ -327,23 +373,46 @@ bool MapObjectManager::CheckCollisionNormal(float worldX, float worldZ, float ac
|
||||
return false;
|
||||
}
|
||||
|
||||
std::vector<Render3DDraw> MapObjectManager::BuildDrawCommands() const {
|
||||
std::vector<Render3DDraw> MapObjectManager::BuildDrawCommands(const float* view, const float* proj) const {
|
||||
std::vector<Render3DDraw> all_draws;
|
||||
for (const auto& obj : m_objects) {
|
||||
if (!obj.model) continue;
|
||||
|
||||
float world[16];
|
||||
MakeFullTransformMatrix(obj.yaw, obj.pitch, obj.roll, obj.x, obj.y, obj.z, obj.scale, world);
|
||||
auto draws = obj.model->BuildDrawCommands(-1.0f, world);
|
||||
for (auto& d : draws) {
|
||||
all_draws.push_back(std::move(d));
|
||||
if (!obj.draws_cached) {
|
||||
obj.cached_draws = obj.model->BuildDrawCommands(-1.0f, world, true);
|
||||
std::fill_n(obj.bounds_min, 3, std::numeric_limits<float>::max());
|
||||
std::fill_n(obj.bounds_max, 3, std::numeric_limits<float>::lowest());
|
||||
for (const auto& draw : obj.cached_draws) {
|
||||
for (size_t i=0; i+2<draw.positions.size(); i+=3) {
|
||||
for (int axis=0; axis<3; ++axis) {
|
||||
float v = draw.positions[i]*draw.world[axis] + draw.positions[i+1]*draw.world[4+axis] +
|
||||
draw.positions[i+2]*draw.world[8+axis] + draw.world[12+axis];
|
||||
obj.bounds_min[axis] = std::min(obj.bounds_min[axis], v);
|
||||
obj.bounds_max[axis] = std::max(obj.bounds_max[axis], v);
|
||||
}
|
||||
}
|
||||
}
|
||||
auto& streams=m_meshStreams[obj.model.get()];
|
||||
for (size_t i=0;i<obj.cached_draws.size();++i) {
|
||||
auto& draw=obj.cached_draws[i];
|
||||
draw=Render3DDraw::ShareGeometry(std::move(draw));
|
||||
if(i<streams.size()) draw.geometry=streams[i];
|
||||
else streams.push_back(draw.geometry);
|
||||
}
|
||||
obj.draws_cached = true;
|
||||
}
|
||||
if (!IsBoundsVisible(obj.bounds_min, obj.bounds_max, view, proj)) continue;
|
||||
all_draws.insert(all_draws.end(), obj.cached_draws.begin(), obj.cached_draws.end());
|
||||
}
|
||||
return all_draws;
|
||||
}
|
||||
|
||||
void MapObjectManager::ClearObjects() {
|
||||
m_objects.clear();
|
||||
m_collisionCount=std::numeric_limits<size_t>::max();
|
||||
m_collisionCells.clear();m_allCollisionObjects.clear();m_largeCollisionObjects.clear();m_collisionCandidates.clear();
|
||||
}
|
||||
|
||||
} // namespace ava2
|
||||
|
||||
Reference in New Issue
Block a user