Last active
April 26, 2016 17:38
-
-
Save adachic/981cbda9dca55bbf3c7eb7abeefa9aa3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//経路を同期的に算出し、返す | |
cocos2d::Vector<MapPosition *> PathFinder::findPathSync( | |
MapPosition *start, | |
MapPosition *end, | |
const cocos2d::Map<std::string, SDGameParts *> &jungleGym, | |
bool unitIgnore) { | |
std::vector<int> *ans = new std::vector<int>(); | |
{ | |
//自分のエリア | |
std::string tag1 = SDMapNode::makeTag(start->m_x, start->m_y, start->m_z ); | |
auto srcAreaId = m_areaIds.at(tag1); | |
//目的地エリア | |
std::string tag2 = SDMapNode::makeTag(end->m_x, end->m_y, end->m_z); | |
auto dstAreaId = m_areaIds.at(tag2); | |
//自分のエリア〜目的地エリアへのエリアパス | |
std::vector<int> *already = new std::vector<int>(); | |
_getAreaPath(srcAreaId, dstAreaId, already, ans); | |
ans->push_back(srcAreaId); | |
ans->reserve(ans->size()); | |
already->clear(); | |
delete already; | |
} | |
auto srcAreaId = -1; | |
auto dstAreaId = -1; | |
cocos2d::Vector<MapPosition *> path; | |
for(int areaId:*ans){ | |
if(srcAreaId == -1){ | |
srcAreaId = areaId; | |
} | |
for(std::pair<int,cocos2d::Vector<MapPosition *>> pair :m_AreaLinePaths.at(srcAreaId)){ | |
if(pair.first == areaId){ | |
for(auto pos:pair.second){ | |
path.pushBack(pos); | |
} | |
path.popBack(); | |
} | |
} | |
srcAreaId = areaId; | |
} | |
path.pushBack(end); | |
return path; | |
} | |
//ansが答え。グラフの辺が入る。 | |
bool PathFinder::_getAreaPath(int srcAreaId, int dstAreaId, std::vector<int> *already, std::vector<int> *ans) { | |
already->push_back(srcAreaId); | |
if (srcAreaId == dstAreaId) { | |
return true; | |
} | |
for (int dst:m_AreaLines[srcAreaId]) { | |
auto shouldSkip = false; | |
for (int al:*already) { | |
if (al == dst) { | |
shouldSkip = true; | |
break; | |
} | |
} | |
if(shouldSkip){ | |
continue; | |
} | |
if(_getAreaPath(dst, dstAreaId, already, ans)){ | |
ans->push_back(dst); | |
return true; | |
} | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment