#include "MapObjectManager.h" #include #include "src/eter_render/SpeedTreeForest.h" #include #include #include #include #include #include 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(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 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, bool native_y) { 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 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; // AVA2 areadata stores the render-space Y sign; packets use positive map Y. if (native_y) py = -py; try { crc = static_cast(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)); 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 (...) {} } } 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; } const std::vector& 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;i1e9 || 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 (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; 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 (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; 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 MapObjectManager::BuildDrawCommands(const float* view, const float* proj) const { std::vector refs; AppendDrawRefs(view, proj, 0, refs); std::vector all_draws; all_draws.reserve(refs.size()); for (const auto& ref : refs) all_draws.push_back(*ref.draw); return all_draws; } void MapObjectManager::AppendDrawRefs(const float* view, const float* proj, std::size_t before, std::vector& out, const float* light_direction, const float* light_ambient) const { for (const auto& obj : m_objects) { if (!obj.model) continue; if (m_maxDistanceSq > 0.0f) { const float dx = obj.x - m_centerX, dz = obj.z - m_centerZ; if (dx * dx + dz * dz >= m_maxDistanceSq) continue; } float world[16]; MakeFullTransformMatrix(obj.yaw, obj.pitch, obj.roll, obj.x, obj.y, obj.z, obj.scale, world); if (!obj.draws_cached) { obj.cached_draws = obj.model->BuildDrawCommands(-1.0f, world, true); std::fill_n(obj.bounds_min, 3, std::numeric_limits::max()); std::fill_n(obj.bounds_max, 3, std::numeric_limits::lowest()); for (const auto& draw : obj.cached_draws) { for (size_t i=0; i+2::max(); m_collisionCells.clear();m_allCollisionObjects.clear();m_largeCollisionObjects.clear();m_collisionCandidates.clear(); } } // namespace ava2