map.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  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. // Calculate how many cells we need
  296. // in both the X and Z direction
  297. float width = m_MaxX - m_MinX;
  298. float height = m_MaxZ - m_MinZ;
  299. m_NumCellsX = ceil(width / m_CellSize);
  300. m_NumCellsZ = ceil(height / m_CellSize);
  301. // Read the number of grids
  302. int32 NumGrids;
  303. fread(&NumGrids, sizeof(int32), 1, f);
  304. std::vector<glm::vec3> verts;
  305. std::vector<uint32> indices;
  306. std::vector<uint32> grids;
  307. uint32 face_count = 0;
  308. // Loop through the grids loading the face list
  309. for (int32 i = 0; i < NumGrids; i++) {
  310. // Read the grid id
  311. int32 GridID;
  312. fread(&GridID, sizeof(int32), 1, f);
  313. // Read the number of vertices
  314. int32 NumFaces;
  315. fread(&NumFaces, sizeof(int32), 1, f);
  316. face_count += NumFaces;
  317. // Loop through the vertices list reading
  318. // 3 at a time to creat a triangle (face)
  319. for (int32 y = 0; y < NumFaces; ) {
  320. // Each vertex need an x,y,z coordinate and
  321. // we will be reading 3 to create the face
  322. float x1, x2, x3;
  323. float y1, y2, y3;
  324. float z1, z2, z3;
  325. // Read the first vertex
  326. fread(&x1, sizeof(float), 1, f);
  327. fread(&y1, sizeof(float), 1, f);
  328. fread(&z1, sizeof(float), 1, f);
  329. y++;
  330. // Read the second vertex
  331. fread(&x2, sizeof(float), 1, f);
  332. fread(&y2, sizeof(float), 1, f);
  333. fread(&z2, sizeof(float), 1, f);
  334. y++;
  335. // Read the third (final) vertex
  336. fread(&x3, sizeof(float), 1, f);
  337. fread(&y3, sizeof(float), 1, f);
  338. fread(&z3, sizeof(float), 1, f);
  339. y++;
  340. glm::vec3 a(x1, z1, y1);
  341. glm::vec3 b(x2, z2, y2);
  342. glm::vec3 c(x3, z3, y3);
  343. size_t sz = verts.size();
  344. verts.push_back(a);
  345. indices.push_back((uint32)sz);
  346. verts.push_back(b);
  347. indices.push_back((uint32)sz + 1);
  348. verts.push_back(c);
  349. indices.push_back((uint32)sz + 2);
  350. grids.push_back((uint32)GridID);
  351. }
  352. }
  353. face_count = face_count / 3;
  354. if (imp) {
  355. imp->rm->release();
  356. imp->rm = nullptr;
  357. }
  358. else {
  359. imp = new impl;
  360. }
  361. imp->rm = createRaycastMesh((RmUint32)verts.size(), (const RmReal*)&verts[0], face_count, &indices[0], &grids[0]);
  362. if (!imp->rm) {
  363. delete imp;
  364. imp = nullptr;
  365. return false;
  366. }
  367. return true;
  368. }
  369. bool Map::LoadV2Deflated(FILE* f) {
  370. std::ifstream file(m_FileName.c_str(), ios_base::in | ios_base::binary);
  371. boost::iostreams::filtering_streambuf<boost::iostreams::input> inbuf;
  372. inbuf.push(boost::iostreams::gzip_decompressor());
  373. inbuf.push(file);
  374. ostream out(&inbuf);
  375. std::streambuf * const srcbuf = out.rdbuf();
  376. std::streamsize size = srcbuf->in_avail();
  377. if(size == -1)
  378. {
  379. file.close();
  380. LogWrite(MAP__ERROR, 0, "Map", "Map::LoadV2Deflated() unable to deflate (%s).", m_ZoneFile.c_str());
  381. return false;
  382. }
  383. // Read the string for the zone file name this was created for
  384. int8 strSize;
  385. char* buf = new char[1024];
  386. srcbuf->sgetn(buf,sizeof(int8));
  387. memcpy(&strSize,&buf[0],sizeof(int8));
  388. LogWrite(MAP__DEBUG, 0, "Map", "strSize = %u", strSize);
  389. char name[256];
  390. srcbuf->sgetn(&name[0],strSize);
  391. name[strSize] = '\0';
  392. LogWrite(MAP__DEBUG, 0, "Map", "name = %s", name);
  393. string fileName(name);
  394. std::size_t found = fileName.find(m_ZoneName);
  395. // Make sure file contents are for the correct zone
  396. if (found == std::string::npos) {
  397. file.close();
  398. safe_delete_array(buf);
  399. LogWrite(MAP__ERROR, 0, "Map", "Map::LoadV2Deflated() map contents (%s) do not match its name (%s).", &name, m_ZoneFile.c_str());
  400. return false;
  401. }
  402. // Read the min bounds
  403. srcbuf->sgetn(buf,sizeof(float));
  404. memcpy(&m_MinX,&buf[0],sizeof(float));
  405. srcbuf->sgetn(buf,sizeof(float));
  406. memcpy(&m_MinZ,&buf[0],sizeof(float));
  407. srcbuf->sgetn(buf,sizeof(float));
  408. memcpy(&m_MaxX,&buf[0],sizeof(float));
  409. srcbuf->sgetn(buf,sizeof(float));
  410. memcpy(&m_MaxZ,&buf[0],sizeof(float));
  411. // Calculate how many cells we need
  412. // in both the X and Z direction
  413. float width = m_MaxX - m_MinX;
  414. float height = m_MaxZ - m_MinZ;
  415. m_NumCellsX = ceil(width / m_CellSize);
  416. m_NumCellsZ = ceil(height / m_CellSize);
  417. // Read the number of grids
  418. int32 NumGrids;
  419. srcbuf->sgetn(buf,sizeof(int32));
  420. memcpy(&NumGrids,&buf[0],sizeof(int32));
  421. std::vector<glm::vec3> verts;
  422. std::vector<uint32> indices;
  423. std::vector<uint32> grids;
  424. uint32 face_count = 0;
  425. // Loop through the grids loading the face list
  426. for (int32 i = 0; i < NumGrids; i++) {
  427. // Read the grid id
  428. int32 GridID;
  429. srcbuf->sgetn(buf,sizeof(int32));
  430. memcpy(&GridID,&buf[0],sizeof(int32));
  431. // Read the number of vertices
  432. int32 NumFaces;
  433. srcbuf->sgetn(buf,sizeof(int32));
  434. memcpy(&NumFaces,&buf[0],sizeof(int32));
  435. face_count += NumFaces;
  436. // Loop through the vertices list reading
  437. // 3 at a time to creat a triangle (face)
  438. for (int32 y = 0; y < NumFaces; ) {
  439. // Each vertex need an x,y,z coordinate and
  440. // we will be reading 3 to create the face
  441. float x1, x2, x3;
  442. float y1, y2, y3;
  443. float z1, z2, z3;
  444. // Read the first vertex
  445. srcbuf->sgetn(buf,sizeof(float)*3);
  446. memcpy(&x1,&buf[0],sizeof(float));
  447. memcpy(&y1,&buf[4],sizeof(float));
  448. memcpy(&z1,&buf[8],sizeof(float));
  449. y++;
  450. // Read the second vertex
  451. srcbuf->sgetn(buf,sizeof(float)*3);
  452. memcpy(&x2,&buf[0],sizeof(float));
  453. memcpy(&y2,&buf[4],sizeof(float));
  454. memcpy(&z2,&buf[8],sizeof(float));
  455. y++;
  456. // Read the third (final) vertex
  457. srcbuf->sgetn(buf,sizeof(float)*3);
  458. memcpy(&x3,&buf[0],sizeof(float));
  459. memcpy(&y3,&buf[4],sizeof(float));
  460. memcpy(&z3,&buf[8],sizeof(float));
  461. y++;
  462. glm::vec3 a(x1, z1, y1);
  463. glm::vec3 b(x2, z2, y2);
  464. glm::vec3 c(x3, z3, y3);
  465. size_t sz = verts.size();
  466. verts.push_back(a);
  467. indices.push_back((uint32)sz);
  468. verts.push_back(b);
  469. indices.push_back((uint32)sz + 1);
  470. verts.push_back(c);
  471. indices.push_back((uint32)sz + 2);
  472. grids.push_back(GridID);
  473. }
  474. }
  475. face_count = face_count / 3;
  476. if (imp) {
  477. imp->rm->release();
  478. imp->rm = nullptr;
  479. }
  480. else {
  481. imp = new impl;
  482. }
  483. imp->rm = createRaycastMesh((RmUint32)verts.size(), (const RmReal*)&verts[0], face_count, &indices[0], &grids[0]);
  484. file.close();
  485. safe_delete_array(buf);
  486. if (!imp->rm) {
  487. delete imp;
  488. imp = nullptr;
  489. return false;
  490. }
  491. return true;
  492. }
  493. void Map::RotateVertex(glm::vec3 &v, float rx, float ry, float rz) {
  494. glm::vec3 nv = v;
  495. nv.y = (std::cos(rx) * v.y) - (std::sin(rx) * v.z);
  496. nv.z = (std::sin(rx) * v.y) + (std::cos(rx) * v.z);
  497. v = nv;
  498. nv.x = (std::cos(ry) * v.x) + (std::sin(ry) * v.z);
  499. nv.z = -(std::sin(ry) * v.x) + (std::cos(ry) * v.z);
  500. v = nv;
  501. nv.x = (std::cos(rz) * v.x) - (std::sin(rz) * v.y);
  502. nv.y = (std::sin(rz) * v.x) + (std::cos(rz) * v.y);
  503. v = nv;
  504. }
  505. void Map::ScaleVertex(glm::vec3 &v, float sx, float sy, float sz) {
  506. v.x = v.x * sx;
  507. v.y = v.y * sy;
  508. v.z = v.z * sz;
  509. }
  510. void Map::TranslateVertex(glm::vec3 &v, float tx, float ty, float tz) {
  511. v.x = v.x + tx;
  512. v.y = v.y + ty;
  513. v.z = v.z + tz;
  514. }
  515. void MapRange::AddVersionRange(std::string zoneName) {
  516. boost::filesystem::path targetDir("Maps/");
  517. // crash fix since the dir isn't present
  518. if(!boost::filesystem::is_directory(targetDir))
  519. {
  520. LogWrite(MAP__ERROR, 7, "Map", "Unable to find directory %s", targetDir.c_str());
  521. return;
  522. }
  523. boost::filesystem::recursive_directory_iterator iter(targetDir), eod;
  524. boost::smatch base_match;
  525. std::string formula = "(.*\\/|.*\\\\)((" + zoneName + ")(\\-([0-9]+)\\-([0-9]+))?)(\\.EQ2Map|\\.EQ2MapDeflated)$";
  526. boost::regex re(formula.c_str());
  527. LogWrite(MAP__INFO, 0, "Map", "Map Formula to match: %s", formula.c_str());
  528. BOOST_FOREACH(boost::filesystem::path
  529. const & i, make_pair(iter, eod)) {
  530. if (is_regular_file(i)) {
  531. std::string fileName(i.string());
  532. if (boost::regex_match(fileName, base_match, re)) {
  533. boost::ssub_match base_sub_match = base_match[2];
  534. boost::ssub_match base_sub_match2 = base_match[5];
  535. boost::ssub_match base_sub_match3 = base_match[6];
  536. std::string baseMatch(base_sub_match.str().c_str());
  537. std::string baseMatch2(base_sub_match2.str().c_str());
  538. std::string baseMatch3(base_sub_match3.str().c_str());
  539. 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());
  540. Map * zonemap = Map::LoadMapFile(zoneName, base_sub_match.str().c_str());
  541. int32 min_version = 0, max_version = 0;
  542. if (strlen(base_sub_match2.str().c_str()) > 0)
  543. min_version = atoul(base_sub_match2.str().c_str());
  544. if (strlen(base_sub_match2.str().c_str()) > 0)
  545. max_version = atoul(base_sub_match3.str().c_str());
  546. version_map.insert(std::make_pair(new VersionRange(min_version, max_version), zonemap));
  547. }
  548. }
  549. }
  550. }
  551. MapRange::MapRange()
  552. {
  553. }
  554. MapRange::~MapRange()
  555. {
  556. Clear();
  557. }
  558. void MapRange::Clear()
  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. Map* map = itr->second;
  565. delete range;
  566. delete map;
  567. }
  568. version_map.clear();
  569. }
  570. map<VersionRange*, Map*>::iterator MapRange::FindVersionRange(int32 min_version, int32 max_version)
  571. {
  572. map<VersionRange*, Map*>::iterator itr;
  573. for (itr = version_map.begin(); itr != version_map.end(); itr++)
  574. {
  575. VersionRange* range = itr->first;
  576. // if min and max version are both in range
  577. if (range->GetMinVersion() <= min_version && max_version <= range->GetMaxVersion())
  578. return itr;
  579. // if the min version is in range, but max range is 0
  580. else if (range->GetMinVersion() <= min_version && range->GetMaxVersion() == 0)
  581. return itr;
  582. // if min version is 0 and max_version has a cap
  583. else if (range->GetMinVersion() == 0 && max_version <= range->GetMaxVersion())
  584. return itr;
  585. }
  586. return version_map.end();
  587. }
  588. map<VersionRange*, Map*>::iterator MapRange::FindMapByVersion(int32 version)
  589. {
  590. map<VersionRange*, Map*>::iterator enditr = version_map.end();
  591. map<VersionRange*, Map*>::iterator itr;
  592. for (itr = version_map.begin(); itr != version_map.end(); itr++)
  593. {
  594. VersionRange* range = itr->first;
  595. // if min and max version are both in range
  596. if(range->GetMinVersion() == 0 && range->GetMaxVersion() == 0)
  597. enditr = itr;
  598. else if (version >= range->GetMinVersion() && version <= range->GetMaxVersion())
  599. return itr;
  600. }
  601. return enditr;
  602. }