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
+349
View File
@@ -0,0 +1,349 @@
#include "MapObjectManager.h"
#include "src/eter_render/SpeedTreeForest.h"
#include <algorithm>
#include <cmath>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <sstream>
namespace ava2 {
static void ComputeObjectOBB(MapObjectInstance& obj);
namespace {
inline void MakeFullTransformMatrix(float yawDeg, float pitchDeg, float rollDeg,
float tx, float ty, float tz, float scale, float* out) {
float ry = yawDeg * 3.14159265358979323846f / 180.0f;
float rx = pitchDeg * 3.14159265358979323846f / 180.0f;
float rz = rollDeg * 3.14159265358979323846f / 180.0f;
float cy = std::cos(ry), sy = std::sin(ry);
float cx = std::cos(rx), sx = std::sin(rx);
float cz = std::cos(rz), sz = std::sin(rz);
// Composite Euler rotation matrix (YXZ / YawPitchRoll)
out[0] = (cy * cz + sy * sx * sz) * scale;
out[1] = (cx * sz) * scale;
out[2] = (-sy * cz + cy * sx * sz) * scale;
out[3] = 0.0f;
out[4] = (-cy * sz + sy * sx * cz) * scale;
out[5] = (cx * cz) * scale;
out[6] = (sy * sz + cy * sx * cz) * scale;
out[7] = 0.0f;
out[8] = (sy * cx) * scale;
out[9] = (-sx) * scale;
out[10] = (cy * cx) * scale;
out[11] = 0.0f;
out[12] = tx;
out[13] = ty;
out[14] = tz;
out[15] = 1.0f;
}
} // namespace
MapObjectManager::MapObjectManager() = default;
std::string MapObjectManager::NormalizePath(const std::string& path) {
std::string p = path;
std::replace(p.begin(), p.end(), '\\', '/');
// Strip drive letter if present (e.g., "d:/", "D:/")
if (p.size() >= 3 && (p[1] == ':' && p[2] == '/')) {
p = p.substr(3);
}
while (!p.empty() && p.front() == '/') {
p.erase(p.begin());
}
if (std::filesystem::exists(p)) return p;
if (std::filesystem::exists("build/dev/" + p)) return "build/dev/" + p;
if (std::filesystem::exists("../" + p)) return "../" + p;
return p;
}
void MapObjectManager::RegisterProperty(uint32_t crc, const std::string& model_path) {
m_property_map[crc] = model_path;
}
size_t MapObjectManager::ScanPropertyDirectory(const std::string& prop_dir) {
if (!std::filesystem::exists(prop_dir)) return 0;
size_t count = 0;
for (const auto& entry : std::filesystem::recursive_directory_iterator(prop_dir, std::filesystem::directory_options::skip_permission_denied)) {
if (!entry.is_regular_file()) continue;
auto ext = entry.path().extension().string();
if (ext != ".prb" && ext != ".prt") continue;
std::ifstream file(entry.path());
if (!file.is_open()) continue;
std::string line;
if (!std::getline(file, line) || line.find("YPRT") == std::string::npos) continue;
std::string crc_line;
if (!std::getline(file, crc_line)) continue;
try {
uint32_t crc = static_cast<uint32_t>(std::stoul(crc_line));
std::string model_file;
while (std::getline(file, line)) {
if (line.find("file") != std::string::npos) {
size_t first = line.find('"');
size_t last = line.rfind('"');
if (first != std::string::npos && last != std::string::npos && last > first) {
model_file = line.substr(first + 1, last - first - 1);
break;
}
}
}
if (!model_file.empty()) {
m_property_map[crc] = model_file;
count++;
}
} catch (...) {
continue;
}
}
return count;
}
std::shared_ptr<Gr2ModelInstance> MapObjectManager::GetOrLoadModel(const std::string& path) {
std::string norm = NormalizePath(path);
auto it = m_model_cache.find(norm);
if (it != m_model_cache.end()) return it->second;
if (!std::filesystem::exists(norm)) {
return nullptr;
}
auto inst = Gr2ModelInstance::Load(norm);
if (inst) {
m_model_cache[norm] = std::move(inst);
return m_model_cache[norm];
}
return nullptr;
}
bool MapObjectManager::LoadAreaData(const std::string& areadata_path, float coord_scale, SpeedTreeForest* forest) {
std::ifstream file(areadata_path);
if (!file.is_open()) {
std::cerr << "MapObjectManager::LoadAreaData failed to open: " << areadata_path << std::endl;
return false;
}
std::string line;
bool in_object = false;
std::vector<std::string> block_lines;
size_t loaded_count = 0;
while (std::getline(file, line)) {
// Strip trailing CR
if (!line.empty() && line.back() == '\r') line.pop_back();
if (line.find("Start Object") != std::string::npos) {
in_object = true;
block_lines.clear();
continue;
}
if (line.find("End Object") != std::string::npos) {
in_object = false;
if (block_lines.size() >= 2) {
// Line 0: X Y Z
// Line 1: CRC
// Line 2 (optional): pitch#roll#yaw or yaw
// Line 3 (optional): height bias
float px = 0.0f, py = 0.0f, pz = 0.0f;
uint32_t crc = 0;
float yaw = 0.0f, pitch = 0.0f, roll = 0.0f;
float bias = 0.0f;
std::istringstream iss(block_lines[0]);
iss >> px >> py >> pz;
try {
crc = static_cast<uint32_t>(std::stoul(block_lines[1]));
} catch (...) {
continue;
}
if (block_lines.size() > 2) {
std::string rot_str = block_lines[2];
size_t s1 = rot_str.find('#');
if (s1 != std::string::npos) {
yaw = std::stof(rot_str.substr(0, s1));
size_t s2 = rot_str.find('#', s1 + 1);
if (s2 != std::string::npos) {
pitch = std::stof(rot_str.substr(s1 + 1, s2 - s1 - 1));
roll = std::stof(rot_str.substr(s2 + 1));
}
} else {
try { yaw = std::stof(rot_str); } catch (...) {}
}
}
if (block_lines.size() > 3) {
try { bias = std::stof(block_lines[3]); } catch (...) {}
}
if (forest && forest->HasMainTree(crc)) {
forest->CreateInstance(px * coord_scale, (pz + bias) * coord_scale, py * coord_scale, crc, yaw, coord_scale);
loaded_count++;
continue;
}
auto prop_it = m_property_map.find(crc);
if (prop_it != m_property_map.end()) {
const std::string& mfile = prop_it->second;
if (forest && (mfile.find(".spt") != std::string::npos || mfile.find(".SPT") != std::string::npos)) {
forest->CreateInstance(px * coord_scale, (pz + bias) * coord_scale, py * coord_scale, crc, yaw, coord_scale, mfile.c_str());
loaded_count++;
continue;
}
auto model = GetOrLoadModel(mfile);
if (model) {
MapObjectInstance obj;
obj.model_path = mfile;
obj.property_crc = crc;
obj.x = px * coord_scale;
obj.y = (pz + bias) * coord_scale;
obj.z = py * coord_scale;
obj.yaw = yaw;
obj.pitch = pitch;
obj.roll = roll;
obj.scale = coord_scale;
obj.model = model;
ComputeObjectOBB(obj);
m_objects.push_back(std::move(obj));
loaded_count++;
}
}
}
continue;
}
if (in_object) {
// Trim leading spaces
size_t start = line.find_first_not_of(" \t");
if (start != std::string::npos) {
block_lines.push_back(line.substr(start));
}
}
}
return loaded_count > 0 || !m_objects.empty();
}
static void ComputeObjectOBB(MapObjectInstance& obj) {
if (!obj.model) {
obj.has_collision = false;
return;
}
float minX = 0, minY = 0, minZ = 0, maxX = 0, maxY = 0, maxZ = 0;
if (obj.model->GetLocalBounds(minX, minY, minZ, maxX, maxY, maxZ)) {
float scale = obj.scale;
float hx = (maxX - minX) * 0.5f * scale;
float hy = (maxY - minY) * 0.5f * scale;
float hz = (maxZ - minZ) * 0.5f * scale;
if (hx < 10.0f && hz < 10.0f) {
obj.has_collision = false;
return;
}
obj.obb.halfExtents[0] = hx;
obj.obb.halfExtents[1] = hy;
obj.obb.halfExtents[2] = hz;
float cxLocal = (minX + maxX) * 0.5f * scale;
float cyLocal = (minY + maxY) * 0.5f * scale;
float czLocal = (minZ + maxZ) * 0.5f * scale;
float yawRad = obj.yaw * (3.14159265f / 180.0f);
obj.obb.yawRad = yawRad;
float cosY = std::cos(yawRad);
float sinY = std::sin(yawRad);
obj.obb.centerX = obj.x + (cxLocal * cosY - czLocal * sinY);
obj.obb.centerY = obj.y + cyLocal;
obj.obb.centerZ = obj.z + (cxLocal * sinY + czLocal * cosY);
obj.has_collision = true;
} else {
obj.has_collision = false;
}
}
bool MapObjectManager::AddObject(const std::string& model_path, float x, float y, float z,
float yaw, float pitch, float roll, float scale) {
auto model = GetOrLoadModel(model_path);
if (!model) return false;
MapObjectInstance obj;
obj.model_path = model_path;
obj.x = x;
obj.y = y;
obj.z = z;
obj.yaw = yaw;
obj.pitch = pitch;
obj.roll = roll;
obj.scale = scale;
obj.model = model;
ComputeObjectOBB(obj);
m_objects.push_back(std::move(obj));
return true;
}
bool MapObjectManager::CheckCollision(float worldX, float worldZ, float actorRadius) const {
for (const auto& obj : m_objects) {
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;
float dz = worldZ - obj.obb.centerZ;
if (dx * dx + dz * dz > maxR * maxR) continue;
if (obj.obb.IntersectsCircle2D(worldX, worldZ, actorRadius)) {
return true;
}
}
return false;
}
bool MapObjectManager::CheckCollisionNormal(float worldX, float worldZ, float actorRadius, float& outNx, float& outNz) const {
for (const auto& obj : m_objects) {
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;
float dz = worldZ - obj.obb.centerZ;
if (dx * dx + dz * dz > maxR * maxR) continue;
float pen = 0.0f;
if (obj.obb.GetPenetrationAndNormal(worldX, worldZ, actorRadius, outNx, outNz, pen)) {
return true;
}
}
return false;
}
std::vector<Render3DDraw> MapObjectManager::BuildDrawCommands() 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));
}
}
return all_draws;
}
void MapObjectManager::ClearObjects() {
m_objects.clear();
}
} // namespace ava2