map.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  1. #include "map.h"
  2. #include "raycast_mesh.h"
  3. #include "../../common/Log.h"
  4. #ifdef WIN32
  5. #define _snprintf snprintf
  6. #include <WinSock2.h>
  7. #include <windows.h>
  8. #endif
  9. #include <algorithm>
  10. #include <map>
  11. #include <memory>
  12. #include <tuple>
  13. #include <vector>
  14. #include <fstream>
  15. #include <iostream>
  16. #include <boost/regex.hpp>
  17. #include <boost/filesystem.hpp>
  18. #include <boost/foreach.hpp>
  19. #include <boost/asio.hpp>
  20. #include <boost/iostreams/filtering_streambuf.hpp>
  21. #include <boost/iostreams/copy.hpp>
  22. #include <boost/iostreams/filter/gzip.hpp>
  23. struct Map::impl
  24. {
  25. RaycastMesh *rm;
  26. };
  27. inline bool file_exists(const std::string& name) {
  28. std::ifstream f(name.c_str());
  29. return f.good();
  30. }
  31. ThreadReturnType LoadMapAsync(void* mapToLoad)
  32. {
  33. Map* map = (Map*)mapToLoad;
  34. map->SetMapLoaded(false);
  35. std::string filename = "Maps/";
  36. filename += map->GetFileName();
  37. std::string deflatedFileName = filename + ".EQ2MapDeflated";
  38. filename += ".EQ2Map";
  39. if(file_exists(deflatedFileName))
  40. filename = deflatedFileName;
  41. map->SetFileName(filename);
  42. if (map->Load(filename))
  43. map->SetMapLoaded(true);
  44. map->SetMapLoading(false);
  45. THREAD_RETURN(NULL);
  46. }
  47. Map::Map(string zonename, string file) {
  48. CheckMapMutex.SetName(file + "MapMutex");
  49. SetMapLoaded(false);
  50. m_ZoneName = zonename;
  51. m_ZoneFile = file;
  52. imp = nullptr;
  53. }
  54. Map::~Map() {
  55. SetMapLoaded(false);
  56. if(imp) {
  57. imp->rm->release();
  58. safe_delete(imp);
  59. }
  60. }
  61. float Map::FindBestZ(glm::vec3 &start, glm::vec3 *result, uint32* GridID)
  62. {
  63. if (!IsMapLoaded())
  64. return BEST_Z_INVALID;
  65. if (!imp)
  66. return BEST_Z_INVALID;
  67. glm::vec3 tmp;
  68. if(!result)
  69. result = &tmp;
  70. start.z += 1.0f;//RuleI(Map, FindBestZHeightAdjust);
  71. glm::vec3 from(start.x, start.y, start.z);
  72. glm::vec3 to(start.x, start.y, BEST_Z_INVALID);
  73. float hit_distance;
  74. bool hit = false;
  75. hit = imp->rm->raycast((const RmReal*)&from, (const RmReal*)&to, (RmReal*)result, nullptr, &hit_distance, (RmUint32*)GridID);
  76. if(hit) {
  77. return result->z;
  78. }
  79. // Find nearest Z above us
  80. to.z = -BEST_Z_INVALID;
  81. hit = imp->rm->raycast((const RmReal*)&from, (const RmReal*)&to, (RmReal*)result, nullptr, &hit_distance, (RmUint32*)GridID);
  82. if (hit)
  83. {
  84. return result->z;
  85. }
  86. return BEST_Z_INVALID;
  87. }
  88. float Map::FindClosestZ(glm::vec3 &start, glm::vec3 *result) {
  89. if (!IsMapLoaded())
  90. return false;
  91. // Unlike FindBestZ, this method finds the closest Z value above or below the specified point.
  92. //
  93. if (!imp)
  94. return false;
  95. float ClosestZ = BEST_Z_INVALID;
  96. glm::vec3 tmp;
  97. if (!result)
  98. result = &tmp;
  99. glm::vec3 from(start.x, start.y, start.z);
  100. glm::vec3 to(start.x, start.y, BEST_Z_INVALID);
  101. float hit_distance;
  102. bool hit = false;
  103. uint32 grid_id = 0;
  104. // first check is below us
  105. hit = imp->rm->raycast((const RmReal*)&from, (const RmReal*)&to, (RmReal*)result, nullptr, &hit_distance, (RmUint32*)grid_id);
  106. if (hit) {
  107. ClosestZ = result->z;
  108. }
  109. // Find nearest Z above us
  110. to.z = -BEST_Z_INVALID;
  111. hit = imp->rm->raycast((const RmReal*)&from, (const RmReal*)&to, (RmReal*)result, nullptr, &hit_distance, (RmUint32*)grid_id);
  112. if (hit) {
  113. if (std::abs(from.z - result->z) < std::abs(ClosestZ - from.z))
  114. return result->z;
  115. }
  116. return ClosestZ;
  117. }
  118. bool Map::LineIntersectsZone(glm::vec3 start, glm::vec3 end, float step, glm::vec3 *result) {
  119. if (!IsMapLoaded())
  120. return false;
  121. if(!imp)
  122. return false;
  123. return imp->rm->raycast((const RmReal*)&start, (const RmReal*)&end, (RmReal*)result, nullptr, nullptr, nullptr);
  124. }
  125. bool Map::LineIntersectsZoneNoZLeaps(glm::vec3 start, glm::vec3 end, float step_mag, glm::vec3 *result) {
  126. if (!IsMapLoaded())
  127. return false;
  128. if (!imp)
  129. return false;
  130. float z = BEST_Z_INVALID;
  131. glm::vec3 step;
  132. glm::vec3 cur;
  133. cur.x = start.x;
  134. cur.y = start.y;
  135. cur.z = start.z;
  136. step.x = end.x - start.x;
  137. step.y = end.y - start.y;
  138. step.z = end.z - start.z;
  139. float factor = step_mag / sqrt(step.x*step.x + step.y*step.y + step.z*step.z);
  140. step.x *= factor;
  141. step.y *= factor;
  142. step.z *= factor;
  143. int steps = 0;
  144. if (step.x > 0 && step.x < 0.001f)
  145. step.x = 0.001f;
  146. if (step.y > 0 && step.y < 0.001f)
  147. step.y = 0.001f;
  148. if (step.z > 0 && step.z < 0.001f)
  149. step.z = 0.001f;
  150. if (step.x < 0 && step.x > -0.001f)
  151. step.x = -0.001f;
  152. if (step.y < 0 && step.y > -0.001f)
  153. step.y = -0.001f;
  154. if (step.z < 0 && step.z > -0.001f)
  155. step.z = -0.001f;
  156. //while we are not past end
  157. //always do this once, even if start == end.
  158. while(cur.x != end.x || cur.y != end.y || cur.z != end.z)
  159. {
  160. steps++;
  161. glm::vec3 me;
  162. me.x = cur.x;
  163. me.y = cur.y;
  164. me.z = cur.z;
  165. glm::vec3 hit;
  166. float best_z = FindBestZ(me, &hit);
  167. float diff = best_z - z;
  168. diff = diff < 0 ? -diff : diff;
  169. if (z <= BEST_Z_INVALID || best_z <= BEST_Z_INVALID || diff < 12.0)
  170. z = best_z;
  171. else
  172. return true;
  173. //look at current location
  174. if(LineIntersectsZone(start, end, step_mag, result))
  175. {
  176. return true;
  177. }
  178. //move 1 step
  179. if (cur.x != end.x)
  180. cur.x += step.x;
  181. if (cur.y != end.y)
  182. cur.y += step.y;
  183. if (cur.z != end.z)
  184. cur.z += step.z;
  185. //watch for end conditions
  186. if ( (cur.x > end.x && end.x >= start.x) || (cur.x < end.x && end.x <= start.x) || (step.x == 0) ) {
  187. cur.x = end.x;
  188. }
  189. if ( (cur.y > end.y && end.y >= start.y) || (cur.y < end.y && end.y <= start.y) || (step.y == 0) ) {
  190. cur.y = end.y;
  191. }
  192. if ( (cur.z > end.z && end.z >= start.z) || (cur.z < end.z && end.z < start.z) || (step.z == 0) ) {
  193. cur.z = end.z;
  194. }
  195. }
  196. //walked entire line and didnt run into anything...
  197. return false;
  198. }
  199. bool Map::CheckLoS(glm::vec3 myloc, glm::vec3 oloc)
  200. {
  201. if (!IsMapLoaded())
  202. return false;
  203. if(!imp)
  204. return false;
  205. return !imp->rm->raycast((const RmReal*)&myloc, (const RmReal*)&oloc, nullptr, nullptr, nullptr, nullptr);
  206. }
  207. // returns true if a collision happens
  208. bool Map::DoCollisionCheck(glm::vec3 myloc, glm::vec3 oloc, glm::vec3 &outnorm, float &distance) {
  209. if (!IsMapLoaded())
  210. return false;
  211. if(!imp)
  212. return false;
  213. return imp->rm->raycast((const RmReal*)&myloc, (const RmReal*)&oloc, nullptr, (RmReal *)&outnorm, (RmReal *)&distance, nullptr);
  214. }
  215. Map *Map::LoadMapFile(std::string zonename, std::string file) {
  216. std::string filename = "Maps/";
  217. filename += file;
  218. std::string deflatedFileName = filename + ".EQ2MapDeflated";
  219. filename += ".EQ2Map";
  220. if(file_exists(deflatedFileName))
  221. filename = deflatedFileName;
  222. LogWrite(MAP__INFO, 7, "Map", "Attempting to load Map File [{%s}]", filename.c_str());
  223. auto m = new Map(zonename, file);
  224. m->SetMapLoading(true);
  225. m->SetFileName(filename);
  226. #ifdef WIN32
  227. _beginthread(LoadMapAsync, 0, (void*)m);
  228. #else
  229. pthread_t t1;
  230. pthread_create(&t1, NULL, LoadMapAsync, (void*)m);
  231. pthread_detach(t1);
  232. #endif
  233. return m;
  234. }
  235. /**
  236. * @param filename
  237. * @return
  238. */
  239. bool Map::Load(const std::string &filename)
  240. {
  241. FILE *map_file = fopen(filename.c_str(), "rb");
  242. if (map_file) {
  243. LogWrite(MAP__INFO, 7, "Map", "Loading Map File [{%s}]", filename.c_str());
  244. bool loaded_map_file = LoadV2(map_file);
  245. fclose(map_file);
  246. if (loaded_map_file) {
  247. LogWrite(MAP__INFO, 7, "Map", "Loaded Map File [{%s}]", filename.c_str());
  248. }
  249. else {
  250. LogWrite(MAP__ERROR, 7, "Map", "FAILED Loading Map File [{%s}]", filename.c_str());
  251. }
  252. return loaded_map_file;
  253. }
  254. else {
  255. return false;
  256. }
  257. return false;
  258. }
  259. struct ModelEntry
  260. {
  261. struct Poly
  262. {
  263. uint32 v1, v2, v3;
  264. uint8 vis;
  265. };
  266. std::vector<glm::vec3> verts;
  267. std::vector<Poly> polys;
  268. };
  269. bool Map::LoadV2(FILE* f) {
  270. std::size_t foundDeflated = m_FileName.find(".EQ2MapDeflated");
  271. if(foundDeflated != std::string::npos)
  272. return LoadV2Deflated(f);
  273. // Read the string for the zone file name this was created for
  274. int8 strSize;
  275. char name[256];
  276. fread(&strSize, sizeof(int8), 1, f);
  277. LogWrite(MAP__DEBUG, 0, "Map", "strSize = %u", strSize);
  278. size_t len = fread(&name, sizeof(char), strSize, f);
  279. name[len] = '\0';
  280. LogWrite(MAP__DEBUG, 0, "Map", "name = %s", name);
  281. string fileName(name);
  282. std::size_t found = fileName.find(m_ZoneName);
  283. // Make sure file contents are for the correct zone
  284. if (found == std::string::npos) {
  285. fclose(f);
  286. LogWrite(MAP__ERROR, 0, "Map", "Map::LoadV2() map contents (%s) do not match its name (%s).", &name, m_ZoneName.c_str());
  287. return false;
  288. }
  289. // Read the min bounds
  290. fread(&m_MinX, sizeof(float), 1, f);
  291. fread(&m_MinZ, sizeof(float), 1, f);
  292. // Read the max bounds
  293. fread(&m_MaxX, sizeof(float), 1, f);
  294. fread(&m_MaxZ, sizeof(float), 1, f);
  295. // Read the number of grids
  296. int32 NumGrids;
  297. fread(&NumGrids, sizeof(int32), 1, f);
  298. std::vector<glm::vec3> verts;
  299. std::vector<uint32> indices;
  300. std::vector<uint32> grids;
  301. uint32 face_count = 0;
  302. // Loop through the grids loading the face list
  303. for (int32 i = 0; i < NumGrids; i++) {
  304. // Read the grid id
  305. int32 GridID;
  306. fread(&GridID, sizeof(int32), 1, f);
  307. // Read the number of vertices
  308. int32 NumFaces;
  309. fread(&NumFaces, sizeof(int32), 1, f);
  310. face_count += NumFaces;
  311. // Loop through the vertices list reading
  312. // 3 at a time to creat a triangle (face)
  313. for (int32 y = 0; y < NumFaces; ) {
  314. // Each vertex need an x,y,z coordinate and
  315. // we will be reading 3 to create the face
  316. float x1, x2, x3;
  317. float y1, y2, y3;
  318. float z1, z2, z3;
  319. // Read the first vertex
  320. fread(&x1, sizeof(float), 1, f);
  321. fread(&y1, sizeof(float), 1, f);
  322. fread(&z1, sizeof(float), 1, f);
  323. y++;
  324. // Read the second vertex
  325. fread(&x2, sizeof(float), 1, f);
  326. fread(&y2, sizeof(float), 1, f);
  327. fread(&z2, sizeof(float), 1, f);
  328. y++;
  329. // Read the third (final) vertex
  330. fread(&x3, sizeof(float), 1, f);
  331. fread(&y3, sizeof(float), 1, f);
  332. fread(&z3, sizeof(float), 1, f);
  333. y++;
  334. glm::vec3 a(x1, z1, y1);
  335. glm::vec3 b(x2, z2, y2);
  336. glm::vec3 c(x3, z3, y3);
  337. size_t sz = verts.size();
  338. verts.push_back(a);
  339. indices.push_back((uint32)sz);
  340. verts.push_back(b);
  341. indices.push_back((uint32)sz + 1);
  342. verts.push_back(c);
  343. indices.push_back((uint32)sz + 2);
  344. grids.push_back((uint32)GridID);
  345. }
  346. }
  347. face_count = face_count / 3;
  348. if (imp) {
  349. imp->rm->release();
  350. imp->rm = nullptr;
  351. }
  352. else {
  353. imp = new impl;
  354. }
  355. imp->rm = createRaycastMesh((RmUint32)verts.size(), (const RmReal*)&verts[0], face_count, &indices[0], &grids[0]);
  356. if (!imp->rm) {
  357. delete imp;
  358. imp = nullptr;
  359. return false;
  360. }
  361. return true;
  362. }
  363. bool Map::LoadV2Deflated(FILE* f) {
  364. std::ifstream file(m_FileName.c_str(), ios_base::in | ios_base::binary);
  365. boost::iostreams::filtering_streambuf<boost::iostreams::input> inbuf;
  366. inbuf.push(boost::iostreams::gzip_decompressor());
  367. inbuf.push(file);
  368. ostream out(&inbuf);
  369. std::streambuf * const srcbuf = out.rdbuf();
  370. std::streamsize size = srcbuf->in_avail();
  371. if(size == -1)
  372. {
  373. file.close();
  374. LogWrite(MAP__ERROR, 0, "Map", "Map::LoadV2Deflated() unable to deflate (%s).", m_ZoneFile.c_str());
  375. return false;
  376. }
  377. // Read the string for the zone file name this was created for
  378. int8 strSize;
  379. char* buf = new char[1024];
  380. srcbuf->sgetn(buf,sizeof(int8));
  381. memcpy(&strSize,&buf[0],sizeof(int8));
  382. LogWrite(MAP__DEBUG, 0, "Map", "strSize = %u", strSize);
  383. char name[256];
  384. srcbuf->sgetn(&name[0],strSize);
  385. name[strSize] = '\0';
  386. LogWrite(MAP__DEBUG, 0, "Map", "name = %s", name);
  387. string fileName(name);
  388. std::size_t found = fileName.find(m_ZoneName);
  389. // Make sure file contents are for the correct zone
  390. if (found == std::string::npos) {
  391. file.close();
  392. safe_delete_array(buf);
  393. LogWrite(MAP__ERROR, 0, "Map", "Map::LoadV2Deflated() map contents (%s) do not match its name (%s).", &name, m_ZoneFile.c_str());
  394. return false;
  395. }
  396. // Read the min bounds
  397. srcbuf->sgetn(buf,sizeof(float));
  398. memcpy(&m_MinX,&buf[0],sizeof(float));
  399. srcbuf->sgetn(buf,sizeof(float));
  400. memcpy(&m_MinZ,&buf[0],sizeof(float));
  401. srcbuf->sgetn(buf,sizeof(float));
  402. memcpy(&m_MaxX,&buf[0],sizeof(float));
  403. srcbuf->sgetn(buf,sizeof(float));
  404. memcpy(&m_MaxZ,&buf[0],sizeof(float));
  405. // Read the number of grids
  406. int32 NumGrids;
  407. srcbuf->sgetn(buf,sizeof(int32));
  408. memcpy(&NumGrids,&buf[0],sizeof(int32));
  409. std::vector<glm::vec3> verts;
  410. std::vector<uint32> indices;
  411. std::vector<uint32> grids;
  412. uint32 face_count = 0;
  413. // Loop through the grids loading the face list
  414. for (int32 i = 0; i < NumGrids; i++) {
  415. // Read the grid id
  416. int32 GridID;
  417. srcbuf->sgetn(buf,sizeof(int32));
  418. memcpy(&GridID,&buf[0],sizeof(int32));
  419. // Read the number of vertices
  420. int32 NumFaces;
  421. srcbuf->sgetn(buf,sizeof(int32));
  422. memcpy(&NumFaces,&buf[0],sizeof(int32));
  423. face_count += NumFaces;
  424. // Loop through the vertices list reading
  425. // 3 at a time to creat a triangle (face)
  426. for (int32 y = 0; y < NumFaces; ) {
  427. // Each vertex need an x,y,z coordinate and
  428. // we will be reading 3 to create the face
  429. float x1, x2, x3;
  430. float y1, y2, y3;
  431. float z1, z2, z3;
  432. // Read the first vertex
  433. srcbuf->sgetn(buf,sizeof(float)*3);
  434. memcpy(&x1,&buf[0],sizeof(float));
  435. memcpy(&y1,&buf[4],sizeof(float));
  436. memcpy(&z1,&buf[8],sizeof(float));
  437. y++;
  438. // Read the second vertex
  439. srcbuf->sgetn(buf,sizeof(float)*3);
  440. memcpy(&x2,&buf[0],sizeof(float));
  441. memcpy(&y2,&buf[4],sizeof(float));
  442. memcpy(&z2,&buf[8],sizeof(float));
  443. y++;
  444. // Read the third (final) vertex
  445. srcbuf->sgetn(buf,sizeof(float)*3);
  446. memcpy(&x3,&buf[0],sizeof(float));
  447. memcpy(&y3,&buf[4],sizeof(float));
  448. memcpy(&z3,&buf[8],sizeof(float));
  449. y++;
  450. glm::vec3 a(x1, z1, y1);
  451. glm::vec3 b(x2, z2, y2);
  452. glm::vec3 c(x3, z3, y3);
  453. size_t sz = verts.size();
  454. verts.push_back(a);
  455. indices.push_back((uint32)sz);
  456. verts.push_back(b);
  457. indices.push_back((uint32)sz + 1);
  458. verts.push_back(c);
  459. indices.push_back((uint32)sz + 2);
  460. grids.push_back(GridID);
  461. }
  462. }
  463. face_count = face_count / 3;
  464. if (imp) {
  465. imp->rm->release();
  466. imp->rm = nullptr;
  467. }
  468. else {
  469. imp = new impl;
  470. }
  471. imp->rm = createRaycastMesh((RmUint32)verts.size(), (const RmReal*)&verts[0], face_count, &indices[0], &grids[0]);
  472. file.close();
  473. safe_delete_array(buf);
  474. if (!imp->rm) {
  475. delete imp;
  476. imp = nullptr;
  477. return false;
  478. }
  479. return true;
  480. }
  481. void Map::RotateVertex(glm::vec3 &v, float rx, float ry, float rz) {
  482. glm::vec3 nv = v;
  483. nv.y = (std::cos(rx) * v.y) - (std::sin(rx) * v.z);
  484. nv.z = (std::sin(rx) * v.y) + (std::cos(rx) * v.z);
  485. v = nv;
  486. nv.x = (std::cos(ry) * v.x) + (std::sin(ry) * v.z);
  487. nv.z = -(std::sin(ry) * v.x) + (std::cos(ry) * v.z);
  488. v = nv;
  489. nv.x = (std::cos(rz) * v.x) - (std::sin(rz) * v.y);
  490. nv.y = (std::sin(rz) * v.x) + (std::cos(rz) * v.y);
  491. v = nv;
  492. }
  493. void Map::ScaleVertex(glm::vec3 &v, float sx, float sy, float sz) {
  494. v.x = v.x * sx;
  495. v.y = v.y * sy;
  496. v.z = v.z * sz;
  497. }
  498. void Map::TranslateVertex(glm::vec3 &v, float tx, float ty, float tz) {
  499. v.x = v.x + tx;
  500. v.y = v.y + ty;
  501. v.z = v.z + tz;
  502. }
  503. void MapRange::AddVersionRange(std::string zoneName) {
  504. boost::filesystem::path targetDir("Maps/");
  505. // crash fix since the dir isn't present
  506. if(!boost::filesystem::is_directory(targetDir))
  507. {
  508. LogWrite(MAP__ERROR, 7, "Map", "Unable to find directory %s", targetDir.c_str());
  509. return;
  510. }
  511. boost::filesystem::recursive_directory_iterator iter(targetDir), eod;
  512. boost::smatch base_match;
  513. std::string formula = "(.*\\/|.*\\\\)((" + zoneName + ")(\\-([0-9]+)\\-([0-9]+))?)(\\.EQ2Map|\\.EQ2MapDeflated)$";
  514. boost::regex re(formula.c_str());
  515. LogWrite(MAP__INFO, 0, "Map", "Map Formula to match: %s", formula.c_str());
  516. BOOST_FOREACH(boost::filesystem::path
  517. const & i, make_pair(iter, eod)) {
  518. if (is_regular_file(i)) {
  519. std::string fileName(i.string());
  520. if (boost::regex_match(fileName, base_match, re)) {
  521. boost::ssub_match base_sub_match = base_match[2];
  522. boost::ssub_match base_sub_match2 = base_match[5];
  523. boost::ssub_match base_sub_match3 = base_match[6];
  524. std::string baseMatch(base_sub_match.str().c_str());
  525. std::string baseMatch2(base_sub_match2.str().c_str());
  526. std::string baseMatch3(base_sub_match3.str().c_str());
  527. LogWrite(MAP__INFO, 0, "Map", "Map To Load: %s, size: %i, string: %s, min: %s, max: %s\n", i.string().c_str(), base_match.size(), baseMatch.c_str(), baseMatch2.c_str(), baseMatch3.c_str());
  528. Map * zonemap = Map::LoadMapFile(zoneName, base_sub_match.str().c_str());
  529. int32 min_version = 0, max_version = 0;
  530. if (strlen(base_sub_match2.str().c_str()) > 0)
  531. min_version = atoul(base_sub_match2.str().c_str());
  532. if (strlen(base_sub_match2.str().c_str()) > 0)
  533. max_version = atoul(base_sub_match3.str().c_str());
  534. version_map.insert(std::make_pair(new VersionRange(min_version, max_version), zonemap));
  535. }
  536. }
  537. }
  538. }
  539. MapRange::MapRange()
  540. {
  541. }
  542. MapRange::~MapRange()
  543. {
  544. Clear();
  545. }
  546. void MapRange::Clear()
  547. {
  548. map<VersionRange*, Map*>::iterator itr;
  549. for (itr = version_map.begin(); itr != version_map.end(); itr++)
  550. {
  551. VersionRange* range = itr->first;
  552. Map* map = itr->second;
  553. delete range;
  554. delete map;
  555. }
  556. version_map.clear();
  557. }
  558. map<VersionRange*, Map*>::iterator MapRange::FindVersionRange(int32 min_version, int32 max_version)
  559. {
  560. map<VersionRange*, Map*>::iterator itr;
  561. for (itr = version_map.begin(); itr != version_map.end(); itr++)
  562. {
  563. VersionRange* range = itr->first;
  564. // if min and max version are both in range
  565. if (range->GetMinVersion() <= min_version && max_version <= range->GetMaxVersion())
  566. return itr;
  567. // if the min version is in range, but max range is 0
  568. else if (range->GetMinVersion() <= min_version && range->GetMaxVersion() == 0)
  569. return itr;
  570. // if min version is 0 and max_version has a cap
  571. else if (range->GetMinVersion() == 0 && max_version <= range->GetMaxVersion())
  572. return itr;
  573. }
  574. return version_map.end();
  575. }
  576. map<VersionRange*, Map*>::iterator MapRange::FindMapByVersion(int32 version)
  577. {
  578. map<VersionRange*, Map*>::iterator enditr = version_map.end();
  579. map<VersionRange*, Map*>::iterator itr;
  580. for (itr = version_map.begin(); itr != version_map.end(); itr++)
  581. {
  582. VersionRange* range = itr->first;
  583. // if min and max version are both in range
  584. if(range->GetMinVersion() == 0 && range->GetMaxVersion() == 0)
  585. enditr = itr;
  586. else if (version >= range->GetMinVersion() && version <= range->GetMaxVersion())
  587. return itr;
  588. }
  589. return enditr;
  590. }