This commit is contained in:
aozhiwei 2023-02-03 11:16:48 +08:00
parent 473336892a
commit 516d7357e1
5 changed files with 73 additions and 78 deletions

View File

@ -2366,7 +2366,6 @@ void Creature::UpdateMove()
}
}
room->grid_service->MoveCreature(this);
CheckSpecObject();
}
}

View File

@ -87,7 +87,6 @@ void MapInstance::Init()
if (current_uniid_ >= FIXED_OBJECT_MAXID) {
A8_ABORT();
}
//LoadHeightData();
{
navmesh_ = dtAllocNavMesh();
FILE *fp = fopen((mt::MetaMgr::Instance()->GetResDir() + "map4.bin").c_str(), "rb");
@ -592,93 +591,85 @@ void MapInstance::MarkMapAreaPolys()
}
}
void MapInstance::LoadHeightData()
bool MapInstance::RaycastEx(int layer, const glm::vec3& start, const glm::vec3& end,
glm::vec3& hit_point, bool& hit_result,
int& same_polys_flags, std::vector<dtPolyRef>& spec_polys)
{
FILE *fp = fopen(("main3d_mapHeight.txt"), "rb");
if (!fp) {
A8_ABORT();
}
fseek(fp, 0, SEEK_END);
size_t file_size = ftell(fp);
char *p = (char*)malloc(file_size);
fseek(fp, 0, SEEK_SET);
fread(p, 1, file_size, fp);
same_polys_flags = 0;
navmesh::HeightList list;
list.ParseFromArray(p, file_size);
float spos[3];
spos[0] = start.x;
spos[1] = start.y;
spos[2] = start.z;
if (list.width() != (int)GetMapMeta()->map_width() ||
list.height() != (int)GetMapMeta()->map_height()) {
A8_ABORT();
}
float epos[3];
epos[0] = end.x;
epos[1] = end.y;
epos[2] = end.z;
dtStatus status = 0;
dtQueryFilter filter;
filter.setIncludeFlags(0xffff);
filter.setExcludeFlags(0);
dtPolyRef startRef = INVALID_NAVMESH_POLYREF;
{
std::map<int, int> height_hash;
for (auto& itr : list.datas()) {
for (auto& itr2 : itr.infos()) {
auto itr3 = height_hash.find(itr2.h());
if (itr3 != height_hash.end()) {
height_hash[itr2.h()] += 1;
} else {
height_hash[itr2.h()] = 1;
}
}
}
std::vector<std::tuple<int, int>> height_sorted;
for (auto& pair : height_hash) {
height_sorted.push_back(std::make_tuple((int)pair.first, (int)pair.second));
}
std::sort(height_sorted.begin(), height_sorted.end(),
[] (std::tuple<int, int>& a, std::tuple<int, int>& b)
{
return std::get<1>(a) > std::get<1>(b);
});
for (auto& tuple : height_sorted) {
a8::XPrintf("%d,%d\n", {std::get<0>(tuple), std::get<1>(tuple)});
const float extents[3] = {2.f/10, 4.f, 2.f/10};
float nearestPt[3];
status = navmesh_query_->findNearestPoly(spos, extents, &filter, &startRef, nearestPt);
if (startRef == INVALID_NAVMESH_POLYREF) {
return false;
}
}
if ((int)GetMapMeta()->map_width() % MAP_HEIGHT_GRID_SIZE != 0 ||
(int)GetMapMeta()->map_height() % MAP_HEIGHT_GRID_SIZE != 0 ) {
float t = 0;
int npolys;
memset(hit_normal_, 0, sizeof(hit_normal_));
status = navmesh_query_->raycast(startRef, spos, epos, &filter, &t, hit_normal_, polys_, &npolys, MAX_POLYS);
if (!dtStatusSucceed(status)) {
#ifdef DEBUG
abort();
#else
return false;
#endif
}
if (npolys <= 0) {
#ifdef DEBUG
abort();
#else
return false;
#endif
}
grid_width_ = GetMapMeta()->map_width() / MAP_HEIGHT_GRID_SIZE;
grid_height_ = GetMapMeta()->map_height() / MAP_HEIGHT_GRID_SIZE;
height_datas_.resize(grid_width_ * grid_height_);
{
int curr_x = 0;
int curr_y = 0;
for (auto& itr : list.datas()) {
if (itr.x() != curr_x) {
abort();
}
if (itr.y() != curr_y) {
abort();
}
curr_x = itr.x() + 1;
if (itr.has_endx()) {
curr_x = itr.endx() + 1;
}
//a8::XPrintf("x:%d y:%d end_x:%d\n", {itr.x(), itr.y(), itr.endx()});
if (curr_x > (int)GetMapMeta()->map_width()) {
abort();
}
if (curr_x == (int)GetMapMeta()->map_width()) {
curr_x = 0;
++curr_y;
}
if (curr_y > (int)GetMapMeta()->map_height()) {
abort();
}
for (auto& itr2 : itr.infos()) {
}
if (t > 1) {
// No Hit
hit_pos_[0] = epos[0];
hit_pos_[1] = epos[1];
hit_pos_[2] = epos[2];
hit_result = false;
} else {
if (t < 0.00001f) {
return false;
}
if (curr_y != (int)GetMapMeta()->map_height()) {
//需要处理spos == epos的情况!!!!
// Hit
dtVlerp(hit_pos_, spos, epos, t);
hit_result = true;
}
{
float closest[3];
bool pos_over_poly = false;
status = navmesh_query_->closestPointOnPoly(polys_[npolys - 1], hit_pos_, closest, &pos_over_poly);
if (!dtStatusSucceed(status)) {
abort();
}
hit_pos_[1] = closest[1];
}
free(p);
fclose(fp);
hit_point.x = hit_pos_[0];
hit_point.y = hit_pos_[1];
hit_point.z = hit_pos_[2];
return true;
}

View File

@ -49,6 +49,9 @@ class MapInstance : public std::enable_shared_from_this<MapInstance>
glm::vec3& random_pt);
bool Raycast(int layer, const glm::vec3& start, const glm::vec3& end,
glm::vec3& hit_point, bool& hit_result);
bool RaycastEx(int layer, const glm::vec3& start, const glm::vec3& end,
glm::vec3& hit_point, bool& hit_result,
int& same_polys_flags, std::vector<dtPolyRef>& spec_polys);
bool FindNearestPoint(const glm::vec3& center, float radius, glm::vec3& nearestPt);
bool GetPosHeight(const Position& pos, float& out_height);
dtPoly* GetPoly(glm::vec3 pos, int& poly_idx);
@ -60,7 +63,6 @@ class MapInstance : public std::enable_shared_from_this<MapInstance>
void CreateThings();
int AllocUniid();
void MarkMapAreaPolys();
void LoadHeightData();
private:
dtNavMesh* navmesh_ = nullptr;

View File

@ -172,7 +172,6 @@ void Movement::AddPaths(const glm::vec3& start, std::vector<glm::vec3>& paths)
p.src_pos.FromGlmVec3(owner_->room->map_instance->UnScaleEx(last_pos));
p.tar_pos.FromGlmVec3(owner_->room->map_instance->UnScaleEx(paths[i]));
p.dir = p.tar_pos.ToGlmVec3() - p.src_pos.ToGlmVec3();
p.dir.y = 0.0f;
GlmHelper::Normalize(p.dir);
p.curr_pos = p.src_pos;

View File

@ -1,5 +1,7 @@
#pragma once
#include "DetourNavMeshQuery.h"
struct MovePathPoint
{
private:
@ -8,6 +10,8 @@ private:
Position curr_pos;
glm::vec3 dir;
float distance = 0.0f;
int same_polys_flags = 0;
std::vector<dtPolyRef> spec_polys;
friend class Movement;
};