From b4fb10686c4641f3cb590c42ae38bfdabbfe9459 Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Thu, 6 Aug 2020 18:49:47 +0800 Subject: [PATCH] 1 --- server/gameserver/mapsearchnode.cc | 10 ++++++---- server/gameserver/mapsearchnode.h | 4 ++-- server/gameserver/mapservice.cc | 23 ++++++++++++++++++++--- server/gameserver/mapservice.h | 6 ++++-- 4 files changed, 32 insertions(+), 11 deletions(-) diff --git a/server/gameserver/mapsearchnode.cc b/server/gameserver/mapsearchnode.cc index 8ca5f3a..5088d04 100644 --- a/server/gameserver/mapsearchnode.cc +++ b/server/gameserver/mapsearchnode.cc @@ -2,6 +2,8 @@ #include "mapsearchnode.h" #include "mapservice.h" +#include "human.h" +#include "room.h" float MapSearchNode::GoalDistanceEstimate(MapSearchNode &node_goal) { @@ -31,14 +33,14 @@ bool MapSearchNode::GetSuccessors(AStarSearch *astarsearch, MapSe } MapSearchNode new_node; - new_node.map_service = map_service; + new_node.hum = hum; for (int index = 0; index < GRID9_AGROUD_CELL_NUM; ++index) { int cell_x = x + GRID9_AGROUND_CELL_INDEX[index][0]; int cell_y = y + GRID9_AGROUND_CELL_INDEX[index][1]; if (parent_x == cell_x && parent_y == cell_y) { continue; } - if (map_service->GetMap(cell_x, cell_y ) < TILE_STATE_CLOSED) { + if (hum->room->map_service->GetMap(cell_x, cell_y ) < TILE_STATE_CLOSED) { new_node = MapSearchNode(cell_x, cell_y); astarsearch->AddSuccessor(new_node); } @@ -50,9 +52,9 @@ bool MapSearchNode::GetSuccessors(AStarSearch *astarsearch, MapSe float MapSearchNode::GetCost(MapSearchNode &successor) { if (x != successor.x && y != successor.y) { - return (float)(map_service->GetMap(x, y) + 0.41421356); + return (float)(hum->room->map_service->GetMap(x, y) + 0.41421356); } - return (float)map_service->GetMap(x, y); + return (float)hum->room->map_service->GetMap(x, y); } bool MapSearchNode::IsSameState(MapSearchNode &rhs) diff --git a/server/gameserver/mapsearchnode.h b/server/gameserver/mapsearchnode.h index 08b2ce8..8d3a639 100644 --- a/server/gameserver/mapsearchnode.h +++ b/server/gameserver/mapsearchnode.h @@ -2,13 +2,13 @@ #include "framework/stlastar.h" -class MapService; +class Human; class MapSearchNode { public: int x = 0; // the (x,y) positions of the node int y = 0; - MapService* map_service = nullptr; + Human* hum = nullptr; MapSearchNode() {} MapSearchNode(int px, int py) {x = px; y = py; } diff --git a/server/gameserver/mapservice.cc b/server/gameserver/mapservice.cc index f64352a..f6273b3 100644 --- a/server/gameserver/mapservice.cc +++ b/server/gameserver/mapservice.cc @@ -267,9 +267,23 @@ FindPathStatus* MapService::AddFindPathRequest(Human* hum, status->hum = hum; status->start_pos = start_pos; status->end_pos = end_pos; - status->curr_step = 0; + status->search_step = 0; status->max_step_num = max_step_num; list_add_tail(&status->entry, &findpath_list_); + { + MapSearchNode node_start; + MapSearchNode node_goal; + node_start.hum = hum; + node_goal.hum = hum; + + node_start.x = int(start_pos.x / map_width_); + node_start.y = int(start_pos.y / map_height_); + + node_goal.x = int(end_pos.x / map_width_); + node_goal.y = int(end_pos.y / map_height_); + + status->astar_search.SetStartAndGoalStates(node_start, node_goal); + } findpath_requests_hash_[hum] = status; return status; } @@ -290,12 +304,15 @@ void MapService::DelFindPathRequest(Human* hum, FindPathStatus* status) void MapService::FindPathUpdate(FindPathStatus* find_status) { - + do { + find_status->search_state = find_status->astar_search.SearchStep(); + ++find_status->search_step; + } while (find_status->search_state == AStarSearch::SEARCH_STATE_SEARCHING); } bool MapService::FindOk(FindPathStatus* find_status) { - return false; + return find_status->search_state == AStarSearch::SEARCH_STATE_SUCCEEDED; } bool MapService::MoveDone(FindPathStatus* find_status) diff --git a/server/gameserver/mapservice.h b/server/gameserver/mapservice.h index 781f011..6a52884 100644 --- a/server/gameserver/mapservice.h +++ b/server/gameserver/mapservice.h @@ -1,6 +1,6 @@ #pragma once -#include "framework/stlastar.h" +#include "mapsearchnode.h" class Human; class ColliderComponent; @@ -35,11 +35,13 @@ struct FindPathStatus Human* hum = nullptr; a8::Vec2 start_pos; a8::Vec2 end_pos; - int curr_step = 0; + int search_step = 0; int max_step_num = 0; int path_index = 0; std::vector out_points; list_head entry; + int search_state = AStarSearch::SEARCH_STATE_SEARCHING; + AStarSearch astar_search; FindPathStatus() {