Section::Section(Section const& section) :myComponent(new Component), room(new Rect), hasRoom(section.HasRoom()), hasPath(section.HasPath()) { *myComponent = *section.myComponent; *room = *section.room; roomConnected = section.roomConnected; groupId = section.groupId; }
std::vector<std::vector<Section*>> MysteryDungeonMaker::ClassifyGroups() { std::vector<std::vector<Section*>> groups; int groupId = 0; int mapHeight=dungeonSize->DungeonRowNum(); int sectionColumnNum = dungeonSize->DungeonColumnNum(); for (int i = 0; i < mapHeight; i++) { for (int j = 0; j < sectionColumnNum; j++) { Section* current = §ions[i][j]; if (current->HasRoom()) { if (groups.empty()) { groups.push_back(current->SetGroupId(groupId++)); } else { int notMarked = 0; for (size_t i_groups = 0; i_groups < groups.size(); i_groups++) { if (!DungeonMakerHelper::HasComponent(groups[i_groups], current->GetComponent())) { notMarked++; } } if (notMarked == groups.size()) groups.push_back(current->SetGroupId(groupId++)); } } } } return groups; }
std::vector<Component> MysteryDungeonMaker::SearchShortestRoute(const Section& start) { std::queue<const Section*> queue; std::vector <Component> visited; std::map<Component, Component> routeMap;//<key,back> const Section* current; const Section* goal = nullptr; bool isGoaled = false; queue.push(&start); visited.push_back(start.GetComponent()); int mapHeight=dungeonSize->DungeonRowNum(); int sectionColumnNum = dungeonSize->DungeonColumnNum(); while (!queue.empty() && !isGoaled) { current = queue.front(); queue.pop(); for (int k = 0; k < 4; k++) { int i = current->GetComponent().i + up_down[k]; int j = current->GetComponent().j + right_left[k]; if ((0 <= i && i < mapHeight) && (0 <= j && j < sectionColumnNum)) { Section* next = §ions[i][j]; if (next->HasRoom() && next->GetGroupId() != start.GetGroupId()) { goal = next; isGoaled = true; routeMap.insert(std::make_pair(next->GetComponent(), current->GetComponent())); break; } if (find(visited.begin(), visited.end(), next->GetComponent()) == visited.end() && !sections[i][j].HasPath() && !sections[i][j].HasRoom()) { queue.push(next); routeMap.insert(std::make_pair(next->GetComponent(), current->GetComponent())); } visited.push_back(next->GetComponent()); } } } std::vector<Component> route; if (isGoaled) { Component back; back = goal->GetComponent(); route.push_back(goal->GetComponent()); while (back != start.GetComponent()) { back = routeMap.find(back)->second; route.push_back(back); } } reverse(route.begin(), route.end()); return route; }