From 9b83137f1d01cc47906df038b87aef702a3b66fe Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Thu, 9 Feb 2023 12:10:49 +0800 Subject: [PATCH] 1 --- server/gameserver/mapservice.cc | 59 +++++++++++++++++++++++++++++++++ server/gameserver/mapservice.h | 2 +- 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/server/gameserver/mapservice.cc b/server/gameserver/mapservice.cc index 5443a004..c8e0dd1f 100644 --- a/server/gameserver/mapservice.cc +++ b/server/gameserver/mapservice.cc @@ -7,6 +7,7 @@ #include "entity.h" #include "roomobstacle.h" #include "room.h" +#include "mapcollider.h" MapService::MapService() { @@ -78,7 +79,65 @@ bool MapService::CanAdd(const glm::vec3& pos, int rad) void MapService::AddTriangle(mc::Triangle* tri) { + glm::vec3* verts[3] = {&tri->vert0, &tri->vert1, &tri->vert2}; + float min_x = verts[0]->x; + float max_x = verts[0]->x; + float min_y = verts[0]->z; + float max_y = verts[0]->z; + { + for (int i = 1; i < 3; ++i) { + if (verts[i]->x < min_x) { + min_x = verts[i]->x; + } + if (verts[i]->x > max_x) { + max_x = verts[i]->x; + } + if (verts[i]->z < min_y) { + min_y = verts[i]->z; + } + if (verts[i]->z > max_y) { + max_y = verts[i]->z; + } + } + min_x -= 1.0f; + max_x += 1.0f; + min_y -= 1.0f; + max_y += 1.0f; + } + { + int min_grid_x = floor(min_x / cell_width_); + int min_grid_y = floor(min_y / cell_width_); + int max_grid_x = ceil(max_x / cell_width_); + int max_grid_y = ceil(max_y / cell_width_); + if (min_grid_x == -1) { + min_grid_x = 0; + } + if (min_grid_x < 0) { + A8_ABORT(); + } + if (max_grid_x >= map_width_) { + max_grid_x = map_width_ - 1; + } + if (min_grid_y == -1) { + min_grid_y = 0; + } + if (min_grid_y < 0) { + A8_ABORT(); + } + if (max_grid_y >= map_height_) { + max_grid_y = map_height_ - 1; + } + for (int x = min_grid_x; x <= max_grid_x; ++x) { + for (int y = min_grid_y; y <= max_grid_y; ++y) { + int grid_id = x + y * map_width_; + list_head* head = &map_cells_[grid_id]; + CellNode* node = new CellNode(); + node->tri = tri; + list_add_tail(&node->entry, head); + } + } + } } int MapService::GetGridId(float world_x, float world_y) diff --git a/server/gameserver/mapservice.h b/server/gameserver/mapservice.h index 8d8d292d..c4585f9b 100644 --- a/server/gameserver/mapservice.h +++ b/server/gameserver/mapservice.h @@ -10,7 +10,7 @@ namespace mc struct CellNode { list_head entry; - CellNode* next = nullptr; + mc::Triangle* tri = nullptr; }; class Room;