map.cpp 18 KB

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