WorldMapWalkmesh.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. * Copyright (C) 2022 The V-Gears Team
  3. *
  4. * This file is part of V-Gears
  5. *
  6. * V-Gears is free software: you can redistribute it and/or modify it under
  7. * terms of the GNU General Public License as published by the Free Software
  8. * Foundation, version 3.0 (GPLv3) of the License.
  9. *
  10. * V-Gears is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <algorithm>
  16. #include <tinyxml.h>
  17. #include "installer/formats/WorldMapWalkmesh.h"
  18. WorldMapWalkmesh::Triangle::Triangle(
  19. const unsigned int id, const Ogre::Vector3 a, const Ogre::Vector3 b,
  20. const Ogre::Vector3 c, const unsigned int walkability, const int event
  21. ): id_(id), a_(a), b_(b), c_(c), walkability_(walkability), event_(event){
  22. ab_.id = -1;
  23. ab_.warp = false;
  24. bc_.id = -1;
  25. bc_.warp = false;
  26. ca_.id = -1;
  27. ca_.warp = false;
  28. }
  29. unsigned int WorldMapWalkmesh::Triangle::getId() const{return id_;}
  30. Ogre::Vector3 WorldMapWalkmesh::Triangle::getA() const {return a_;}
  31. Ogre::Vector3 WorldMapWalkmesh::Triangle::getB() const {return b_;}
  32. Ogre::Vector3 WorldMapWalkmesh::Triangle::getC() const {return c_;}
  33. WorldMapWalkmesh::Triangle::Target WorldMapWalkmesh::Triangle::getAB() const {return ab_;}
  34. WorldMapWalkmesh::Triangle::Target WorldMapWalkmesh::Triangle::getBC() const {return bc_;}
  35. WorldMapWalkmesh::Triangle::Target WorldMapWalkmesh::Triangle::getCA() const {return ca_;}
  36. unsigned int WorldMapWalkmesh::Triangle::getWalkability() const {return walkability_;}
  37. int WorldMapWalkmesh::Triangle::getEvent() const {return event_;}
  38. void WorldMapWalkmesh::Triangle::setAB(const int id, const bool warp){
  39. ab_.id = std::max(id, -1);
  40. ab_.warp = warp;
  41. }
  42. void WorldMapWalkmesh::Triangle::setBC(const int id, const bool warp){
  43. bc_.id = std::max(id, -1);
  44. bc_.warp = warp;
  45. }
  46. void WorldMapWalkmesh::Triangle::setCA(const int id, const bool warp){
  47. ca_.id = std::max(id, -1);
  48. ca_.warp = warp;
  49. }
  50. WorldMapWalkmesh::WorldMapWalkmesh(const unsigned int id, const bool warp): id_(id), warp_(warp), calculated_(false){
  51. triangles_.clear();
  52. }
  53. void WorldMapWalkmesh::addTriangle(
  54. const Ogre::Vector3 a, const Ogre::Vector3 b, const Ogre::Vector3 c,
  55. const unsigned int walkability, const int event
  56. ){
  57. calculated_ = false;
  58. Triangle tri(triangles_.size(), a, b, c, walkability, event);
  59. //if (id_ == 0 && triangles_.size() < 50){
  60. // std::cout << "Adding triangle " << tri.getId() << " W: "<< walkability << " - " << tri.getWalkability()
  61. // << " E: " << event << " - " << tri.getEvent() << std::endl;
  62. //}
  63. triangles_.push_back(tri);
  64. }
  65. void WorldMapWalkmesh::generate(std::string path){
  66. if (!calculated_) calculate();
  67. TiXmlDocument doc;
  68. std::unique_ptr<TiXmlElement> xml(new TiXmlElement("walkmesh"));
  69. for (Triangle tri: triangles_){
  70. std::unique_ptr<TiXmlElement> xml_tri(new TiXmlElement("triangle"));
  71. xml_tri->SetAttribute("id", std::to_string(tri.getId()));
  72. xml_tri->SetAttribute(
  73. "a",
  74. std::to_string(static_cast<int>(tri.getA()[0])) + " "
  75. + std::to_string(static_cast<int>(tri.getA()[1])) + " "
  76. + std::to_string(static_cast<int>(tri.getA()[2]))
  77. );
  78. xml_tri->SetAttribute(
  79. "b",
  80. std::to_string(static_cast<int>(tri.getB()[0])) + " "
  81. + std::to_string(static_cast<int>(tri.getB()[1])) + " "
  82. + std::to_string(static_cast<int>(tri.getB()[2]))
  83. );
  84. xml_tri->SetAttribute(
  85. "c",
  86. std::to_string(static_cast<int>(tri.getA()[0])) + " "
  87. + std::to_string(static_cast<int>(tri.getC()[1])) + " "
  88. + std::to_string(static_cast<int>(tri.getC()[2]))
  89. );
  90. // WARN: This is never actually calculated. See the comments on calculate()
  91. if (tri.getAB().id > -1)
  92. xml_tri->SetAttribute("a_b", std::to_string(tri.getAB().id) + (tri.getAB().warp ? " warp" : ""));
  93. if (tri.getBC().id > -1)
  94. xml_tri->SetAttribute("b_c", std::to_string(tri.getBC().id) + (tri.getBC().warp ? " warp" : ""));
  95. if (tri.getCA().id > -1)
  96. xml_tri->SetAttribute("c_a", std::to_string(tri.getCA().id) + (tri.getCA().warp ? " warp" : ""));
  97. xml_tri->SetAttribute("walkability", std::to_string(tri.getWalkability()));
  98. if (tri.getEvent() > -1) xml_tri->SetAttribute("event", std::to_string(tri.getEvent()));
  99. xml->LinkEndChild(xml_tri.release());
  100. }
  101. doc.LinkEndChild(xml.release());
  102. doc.SaveFile(path);
  103. }
  104. void WorldMapWalkmesh::calculate(){
  105. // FIXME: This is super time-consumming. Either optimize it or skip it and calculate it on
  106. // runtime.
  107. calculated_ = true;
  108. return;
  109. // FIXME: The actual function starts here.
  110. if (calculated_) return;
  111. for (Triangle tri: triangles_){
  112. tri.setAB(-1, false);
  113. tri.setBC(-1, false);
  114. tri.setCA(-1, false);
  115. }
  116. for (Triangle tri: triangles_){
  117. if (tri.getAB().id != -1) continue;
  118. for (Triangle compare: triangles_){
  119. // For each triangle combination, check if any two vertices match,
  120. // and set AB/BC/CA for both of them.
  121. if (tri.getId() != compare.getId()){
  122. if (
  123. (tri.getA() == compare.getA() && tri.getB() == compare.getB())
  124. || (tri.getB() == compare.getA() && tri.getA() == compare.getB())
  125. ){
  126. // AB <-> AB match
  127. tri.setAB(compare.getId(), false);
  128. compare.setAB(tri.getId(), false);
  129. }
  130. if (
  131. (tri.getA() == compare.getB() && tri.getB() == compare.getC())
  132. || (tri.getB() == compare.getB() && tri.getA() == compare.getC())
  133. ){
  134. // AB <-> BC match
  135. tri.setAB(compare.getId(), false);
  136. compare.setBC(tri.getId(), false);
  137. }
  138. if (
  139. (tri.getA() == compare.getC() && tri.getB() == compare.getA())
  140. || (tri.getB() == compare.getC() && tri.getA() == compare.getA())
  141. ){
  142. // AB <-> CA match
  143. tri.setAB(compare.getId(), false);
  144. compare.setCA(tri.getId(), false);
  145. }
  146. if (
  147. (tri.getB() == compare.getA() && tri.getC() == compare.getB())
  148. || (tri.getC() == compare.getA() && tri.getB() == compare.getB())
  149. ){
  150. // BC <-> AB match
  151. tri.setBC(compare.getId(), false);
  152. compare.setAB(tri.getId(), false);
  153. }
  154. if (
  155. (tri.getB() == compare.getB() && tri.getC() == compare.getC())
  156. || (tri.getC() == compare.getB() && tri.getB() == compare.getC())
  157. ){
  158. // BC <-> BC match
  159. tri.setBC(compare.getId(), false);
  160. compare.setBC(tri.getId(), false);
  161. }
  162. if (
  163. (tri.getB() == compare.getC() && tri.getC() == compare.getA())
  164. || (tri.getC() == compare.getC() && tri.getB() == compare.getA())
  165. ){
  166. // BC <-> CA match
  167. tri.setBC(compare.getId(), false);
  168. compare.setCA(tri.getId(), false);
  169. }
  170. if (
  171. (tri.getC() == compare.getA() && tri.getA() == compare.getB())
  172. || (tri.getA() == compare.getA() && tri.getC() == compare.getB())
  173. ){
  174. // CA <-> AB match
  175. tri.setBC(compare.getId(), false);
  176. compare.setAB(tri.getId(), false);
  177. }
  178. if (
  179. (tri.getC() == compare.getB() && tri.getA() == compare.getC())
  180. || (tri.getA() == compare.getB() && tri.getC() == compare.getC())
  181. ){
  182. // CA <-> BC match
  183. tri.setCA(compare.getId(), false);
  184. compare.setBC(tri.getId(), false);
  185. }
  186. if (
  187. (tri.getC() == compare.getC() && tri.getA() == compare.getA())
  188. || (tri.getA() == compare.getC() && tri.getC() == compare.getA())
  189. ){
  190. // CA <-> CA match
  191. tri.setCA(compare.getId(), false);
  192. compare.setCA(tri.getId(), false);
  193. }
  194. // TODO: For warping worlds, check warping
  195. }
  196. }
  197. }
  198. calculated_ = true;
  199. }