SPGrid.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  1. /*
  2. EQ2Emulator: Everquest II Server Emulator
  3. Copyright (C) 2007 EQ2EMulator Development Team (http://www.eq2emulator.net)
  4. This file is part of EQ2Emulator.
  5. EQ2Emulator is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. EQ2Emulator is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with EQ2Emulator. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include "SPGrid.h"
  17. #include "../../common/Log.h"
  18. SPGrid::SPGrid(string file, int32 cellSize) {
  19. m_ZoneFile = file;
  20. m_CellSize = cellSize;
  21. m_MinX = 0;
  22. m_MinZ = 0;
  23. m_MaxX = 0;
  24. m_MaxZ = 0;
  25. m_NumCellsX = 0;
  26. m_NumCellsZ = 0;
  27. m_NumFaceCellsX = 0;
  28. m_NumFaceCellsZ = 0;
  29. }
  30. SPGrid::~SPGrid() {
  31. vector<Cell>::iterator CellItr;
  32. map<int32, vector<Face*> >::iterator MapItr;
  33. vector<Face*>::iterator FaceItr;
  34. // Loop through the vector of cells
  35. /*for (CellItr = m_Cells.begin(); CellItr != m_Cells.end(); CellItr++) {
  36. // Loop through the map of vertices on this cell
  37. for (MapItr = (*CellItr).FaceList.begin(); MapItr != (*CellItr).FaceList.end(); MapItr++) {
  38. // Loop through the vector of faces in the map and delete the pointers
  39. for (FaceItr = (*MapItr).second.begin(); FaceItr != (*MapItr).second.end(); FaceItr++) {
  40. safe_delete((*FaceItr));
  41. }
  42. }
  43. }*/
  44. }
  45. bool SPGrid::Init() {
  46. // Make sure we have a zone file
  47. if (m_ZoneFile.empty()) {
  48. LogWrite(ZONE__ERROR, 0, "SPGrid", "SPGrid::Init() m_ZoneFile is empty.");
  49. return false;
  50. }
  51. // Make sure we have a cell size
  52. if (m_CellSize == 0)
  53. m_CellSize = CELLSIZEDEFAULT;
  54. // Open the map file for this zone
  55. string filePath = "Maps/" + m_ZoneFile + ".EQ2Map";
  56. FILE* file = fopen(filePath.c_str(), "rb");
  57. if (file == nullptr) {
  58. LogWrite(ZONE__WARNING, 0, "SPGrid", "SPGrid::Init() unable to open the map file for %s. (zoneserver will continue to run fine without it)", m_ZoneFile.c_str());
  59. return false;
  60. }
  61. // Read the string for the zone file name this was created for
  62. int8 strSize;
  63. char name[256];
  64. fread(&strSize, sizeof(int8), 1, file);
  65. LogWrite(ZONE__DEBUG, 0, "SPGrid", "strSize = %u", strSize);
  66. size_t len = fread(&name, sizeof(char), strSize, file);
  67. name[len] = '\0';
  68. LogWrite(ZONE__DEBUG, 0, "SPGrid", "name = %s", name);
  69. string fileName(name);
  70. std::size_t found = fileName.find(m_ZoneFile);
  71. // Make sure file contents are for the correct zone
  72. if (found == std::string::npos) {
  73. fclose(file);
  74. LogWrite(ZONE__ERROR, 0, "SPGrid", "SPGrid::Init() map contents (%s) do not match its name (%s).", &name, m_ZoneFile.c_str());
  75. return false;
  76. }
  77. // Read the min bounds
  78. fread(&m_MinX, sizeof(float), 1, file);
  79. fread(&m_MinZ, sizeof(float), 1, file);
  80. LogWrite(ZONE__DEBUG, 0, "SPGrid", "minx = %f, minz = %f", m_MinX, m_MinZ);
  81. // Read the max bounds
  82. fread(&m_MaxX, sizeof(float), 1, file);
  83. fread(&m_MaxZ, sizeof(float), 1, file);
  84. LogWrite(ZONE__DEBUG, 0, "SPGrid", "maxx = %f, maxz = %f", m_MaxX, m_MaxZ);
  85. // Calculate how many cells we need
  86. // in both the X and Z direction
  87. float width = m_MaxX - m_MinX;
  88. float height = m_MaxZ - m_MinZ;
  89. m_NumCellsX = ceil(width / m_CellSize);
  90. m_NumCellsZ = ceil(height / m_CellSize);
  91. LogWrite(ZONE__DEBUG, 0, "SPGrid", "CellSize = %u, x cells = %u, z cells = %u", m_CellSize, m_NumCellsX, m_NumCellsZ);
  92. // Allocate all the cells
  93. m_Cells.resize(m_NumCellsZ * m_NumCellsX);
  94. m_NumFaceCellsX = ceil(width / FACECELLSIZEDEFAULT);
  95. m_NumFaceCellsZ = ceil(height / FACECELLSIZEDEFAULT);
  96. m_FaceCells.resize(m_NumFaceCellsX * m_NumFaceCellsZ);
  97. // Read the number of grids
  98. int32 NumGrids;
  99. fread(&NumGrids, sizeof(int32), 1, file);
  100. LogWrite(ZONE__DEBUG, 0, "SPGrid", "NumGrids = %u", NumGrids);
  101. // Loop through the grids loading the face list
  102. for (int32 i = 0; i < NumGrids; i++) {
  103. // Read the grid id
  104. int32 GridID;
  105. fread(&GridID, sizeof(int32), 1, file);
  106. LogWrite(ZONE__DEBUG, 0, "SPGrid", "GridID = %u", GridID);
  107. // Read the number of vertices
  108. int32 NumFaces;
  109. fread(&NumFaces, sizeof(int32), 1, file);
  110. LogWrite(ZONE__DEBUG, 0, "SPGrid", "NumFaces = %u", NumFaces);
  111. // Loop through the vertices list reading
  112. // 3 at a time to creat a triangle (face)
  113. for (int32 y = 0; y < NumFaces; ) {
  114. // Each vertex need an x,y,z coordinate and
  115. // we will be reading 3 to create the face
  116. float x1, x2, x3;
  117. float y1, y2, y3;
  118. float z1, z2, z3;
  119. // Read the first vertex
  120. fread(&x1, sizeof(float), 1, file);
  121. fread(&y1, sizeof(float), 1, file);
  122. fread(&z1, sizeof(float), 1, file);
  123. y++;
  124. // Read the second vertex
  125. fread(&x2, sizeof(float), 1, file);
  126. fread(&y2, sizeof(float), 1, file);
  127. fread(&z2, sizeof(float), 1, file);
  128. y++;
  129. // Read the third (final) vertex
  130. fread(&x3, sizeof(float), 1, file);
  131. fread(&y3, sizeof(float), 1, file);
  132. fread(&z3, sizeof(float), 1, file);
  133. y++;
  134. // Create the face and add it to the grid
  135. Face* face = new Face;
  136. face->Vertex1[0] = x1;
  137. face->Vertex1[1] = y1;
  138. face->Vertex1[2] = z1;
  139. face->Vertex2[0] = x2;
  140. face->Vertex2[1] = y2;
  141. face->Vertex2[2] = z2;
  142. face->Vertex3[0] = x3;
  143. face->Vertex3[1] = y3;
  144. face->Vertex3[2] = z3;
  145. AddFace(face, GridID);
  146. }
  147. }
  148. fclose(file);
  149. /*map<int32, vector<Face*> >::iterator itr;
  150. vector<Face*>::iterator itr2;
  151. for (int32 i = 0; i < m_Cells.size(); i++) {
  152. Cell& cell = m_Cells[i];
  153. for (itr = cell.FaceList.begin(); itr != cell.FaceList.end(); itr++) {
  154. float min_x = 0.0f;
  155. float min_y = 0.0f;
  156. float min_z = 0.0f;
  157. float max_x = 0.0f;
  158. float max_y = 0.0f;
  159. float max_z = 0.0f;
  160. for (itr2 = (*itr).second.begin(); itr2 != (*itr).second.end(); itr2++) {
  161. Face* face = (*itr2);
  162. if (min_x == 0.0f || face->Vertex1[0] < min_x)
  163. min_x = face->Vertex1[0];
  164. if (face->Vertex2[0] < min_x)
  165. min_x = face->Vertex2[0];
  166. if (face->Vertex3[0] < min_x)
  167. min_x = face->Vertex3[0];
  168. if (min_y == 0.0f || face->Vertex1[1] < min_y)
  169. min_y = face->Vertex1[1];
  170. if (face->Vertex2[1] < min_y)
  171. min_y = face->Vertex2[1];
  172. if (face->Vertex3[1] < min_y)
  173. min_y = face->Vertex3[1];
  174. if (min_z == 0.0f || face->Vertex1[2] < min_z)
  175. min_z = face->Vertex1[2];
  176. if (face->Vertex2[2] < min_z)
  177. min_z = face->Vertex2[2];
  178. if (face->Vertex3[2] < min_z)
  179. min_z = face->Vertex3[2];
  180. // Max bounds
  181. if (max_x == 0.0f || face->Vertex1[0] > max_x)
  182. max_x = face->Vertex1[0];
  183. if (face->Vertex2[0] > max_x)
  184. max_x = face->Vertex2[0];
  185. if (face->Vertex3[0] > max_x)
  186. max_x = face->Vertex3[0];
  187. if (max_y == 0.0f || face->Vertex1[1] > max_y)
  188. max_y = face->Vertex1[1];
  189. if (face->Vertex2[1] > max_y)
  190. max_y = face->Vertex2[1];
  191. if (face->Vertex3[1] > max_y)
  192. max_y = face->Vertex3[1];
  193. if (max_z == 0.0f || face->Vertex1[2] > max_z)
  194. max_z = face->Vertex1[2];
  195. if (face->Vertex2[2] > max_z)
  196. max_z = face->Vertex2[2];
  197. if (face->Vertex3[2] > max_z)
  198. max_z = face->Vertex3[2];
  199. }
  200. GridBounds* bounds = new GridBounds;
  201. bounds->MinBounds[0] = min_x;
  202. bounds->MinBounds[1] = min_y;
  203. bounds->MinBounds[2] = min_z;
  204. bounds->MaxBounds[0] = max_x;
  205. bounds->MaxBounds[1] = max_y;
  206. bounds->MaxBounds[2] = max_z;
  207. cell.GridBounds[(*itr).first] = bounds;
  208. }
  209. }*/
  210. return true;
  211. }
  212. Cell* SPGrid::GetCell(int32 x, int32 z) {
  213. if (x >= m_NumCellsX)
  214. x = m_NumCellsX - 1;
  215. if (z >= m_NumCellsZ)
  216. z = m_NumCellsZ - 1;
  217. return &m_Cells[z * m_NumCellsX + x];
  218. }
  219. Cell* SPGrid::GetCell(float x, float z) {
  220. // As cell grid coordinates are all positive we need to
  221. // modify the coordinates by subtracting the min bounds
  222. float newX = x - m_MinX;
  223. float newZ = z - m_MinZ;
  224. // Get the cell coordinates by doing int division
  225. // with the modified coordinates and the cell size
  226. int32 CellX = (int32)(newX / m_CellSize);
  227. int32 CellZ = (int32)(newZ / m_CellSize);
  228. return GetCell(CellX, CellZ);
  229. }
  230. FaceCell* SPGrid::GetFaceCell(int32 x, int32 z) {
  231. if (x >= m_NumFaceCellsX)
  232. x = m_NumFaceCellsX - 1;
  233. if (z >= m_NumFaceCellsZ)
  234. z = m_NumFaceCellsZ - 1;
  235. return &m_FaceCells[z * m_NumFaceCellsX + x];
  236. }
  237. FaceCell* SPGrid::GetFaceCell(float x, float z) {
  238. // As cell grid coordinates are all positive we need to
  239. // modify the coordinates by subtracting the min bounds
  240. float newX = x - m_MinX;
  241. float newZ = z - m_MinZ;
  242. // Get the cell coordinates by doing int division
  243. // with the modified coordinates and the cell size
  244. int32 CellX = (int32)(newX / FACECELLSIZEDEFAULT);
  245. int32 CellZ = (int32)(newZ / FACECELLSIZEDEFAULT);
  246. return GetFaceCell(CellX, CellZ);
  247. }
  248. void SPGrid::AddFace(Face* face, int32 grid) {
  249. // As each face has three vertices we will need to check the cell
  250. // for all of them and add the face to each cell that it is within
  251. face->grid_id = grid;
  252. // Get the cell at the first vertex position (X and Z, Y is vertical in EQ2)
  253. // as this is the first check we will add it to this cell and compare it
  254. // to the other two cells we get for the other two verticies
  255. FaceCell* cell = GetFaceCell(face->Vertex1[0], face->Vertex1[2]);
  256. cell->FaceList[grid].push_back(face);
  257. // Get the cells for the other two verticies and compare
  258. FaceCell* cell2 = GetFaceCell(face->Vertex2[0], face->Vertex2[2]);
  259. FaceCell* cell3 = GetFaceCell(face->Vertex3[0], face->Vertex3[2]);
  260. // If cell 2 is not the same cell as the original cell then add the face to cell2
  261. if (cell2 != cell)
  262. cell2->FaceList[grid].push_back(face);
  263. // If cell 3 is not the same as the original cell AND not the same as cell 2 then add the face to cell 3
  264. if (cell3 != cell && cell3 != cell2)
  265. cell3->FaceList[grid].push_back(face);
  266. }
  267. float rayIntersectsTriangle(float *p, float *d, float *v0, float *v1, float *v2);
  268. int32 SPGrid::GetGridID(Spawn * spawn) {
  269. FaceCell* cell = GetFaceCell(spawn->GetX(), spawn->GetZ());
  270. /*if (cell->GridBounds.size() == 1)
  271. return cell->FaceList.begin()->first;*/
  272. // Create the starting point for the trace
  273. float point[3];
  274. point[0] = spawn->GetX();
  275. point[1] = spawn->GetY() + 3.0f; // Small bump to make sure we are above ground when we do the trace
  276. point[2] = spawn->GetZ();
  277. // Create the direction for the trace, as we want what
  278. // is below it will just be -1 in the y direction
  279. float direction[3];
  280. direction[0] = 0.0f;
  281. direction[1] = -1.0f;
  282. direction[2] = 0.0f;
  283. float MinDistance = 0.0f;
  284. int32 Grid = 0;
  285. /*map<int32, GridBounds*>::iterator itr;
  286. for (itr = cell->GridBounds.begin(); itr != cell->GridBounds.end(); itr++) {
  287. GridBounds* bounds = (*itr).second;
  288. if (point[0] >= bounds->MinBounds[0] && point[1] >= bounds->MinBounds[1] && point[2] >= bounds->MinBounds[2]
  289. && point[0] <= bounds->MaxBounds[0] && point[1] <= bounds->MaxBounds[1] && point[2] <= bounds->MaxBounds[2]) {
  290. vector<Face*>::iterator itr2;
  291. for (itr2 = cell->FaceList[(*itr).first].begin(); itr2 != cell->FaceList[(*itr).first].end(); itr2++) {
  292. Face* face = *itr2;
  293. float distance;
  294. if ((distance = rayIntersectsTriangle(point, direction, face->Vertex1, face->Vertex2, face->Vertex3)) != 0) {
  295. if (MinDistance == 0.0f || distance < MinDistance) {
  296. MinDistance = distance;
  297. Grid = (*itr).first;
  298. }
  299. }
  300. }
  301. }
  302. }*/
  303. map<int32, vector<Face*> >::iterator mapitr;
  304. for (mapitr = cell->FaceList.begin(); mapitr != cell->FaceList.end(); mapitr++) {
  305. vector<Face*>::iterator itr;
  306. for (itr = (*mapitr).second.begin(); itr != (*mapitr).second.end(); itr++) {
  307. Face* face = *itr;
  308. float distance;
  309. if ((distance = rayIntersectsTriangle(point, direction, face->Vertex1, face->Vertex2, face->Vertex3)) != 0) {
  310. if (MinDistance == 0.0f || distance < MinDistance) {
  311. MinDistance = distance;
  312. Grid = (*mapitr).first;
  313. }
  314. }
  315. }
  316. }
  317. return Grid;
  318. }
  319. void SPGrid::AddSpawn(Spawn * spawn) {
  320. Cell* cell = GetCell(spawn->GetX(), spawn->GetZ());
  321. AddSpawn(spawn, cell);
  322. }
  323. void SPGrid::AddSpawn(Spawn * spawn, Cell * cell) {
  324. cell->SpawnList.push_back(spawn);
  325. spawn->Cell_Info.CurrentCell = cell;
  326. spawn->Cell_Info.CellListIndex = cell->SpawnList.size() - 1;
  327. }
  328. void SPGrid::RemoveSpawnFromCell(Spawn * spawn) {
  329. if (spawn->Cell_Info.CurrentCell) {
  330. vector<Spawn*>& spawns = spawn->Cell_Info.CurrentCell->SpawnList;
  331. // Only do the vector swap if the vector has more than 1 spawn in it
  332. if (spawns.size() > 1) {
  333. // Swap the last spawn in this list to our position and update its stored index to match its new index
  334. spawns[spawn->Cell_Info.CellListIndex] = spawns.back();
  335. spawns[spawn->Cell_Info.CellListIndex]->Cell_Info.CellListIndex = spawn->Cell_Info.CellListIndex;
  336. }
  337. // Remove the last spawn from the list which should now be the spawn passed as a parameter
  338. spawns.pop_back();
  339. // Reset the spawns CellInfo to default values now that it is no longer in a cell
  340. spawn->Cell_Info.CellListIndex = -1;
  341. spawn->Cell_Info.CurrentCell = nullptr;
  342. }
  343. }
  344. float SPGrid::GetBestY(float x, float y, float z)
  345. {
  346. float temp_y = 0;
  347. float best_y = 999999.0f;
  348. FaceCell* startCell = GetFaceCell(x, z);
  349. float tmpY = y + 0.5f;
  350. float point[3];
  351. point[0] = x;
  352. point[1] = tmpY; // Small bump to make sure we are above ground when we do the trace
  353. point[2] = z;
  354. float MinDistance = 0.0f;
  355. // Create the direction for the trace, as we want what
  356. // is below it will just be -1 in the y direction
  357. float direction[3];
  358. direction[0] = 0.0f;
  359. direction[1] = -1.0f;
  360. direction[2] = 0.0f;
  361. Face* lastFace = 0;
  362. int32 Grid = 0;
  363. float BestZ = -999999.0f;
  364. map<int32, vector<Face*> >::iterator mapitr;
  365. for (mapitr = startCell->FaceList.begin(); mapitr != startCell->FaceList.end(); mapitr++) {
  366. vector<Face*>::iterator itr;
  367. for (itr = (*mapitr).second.begin(); itr != (*mapitr).second.end(); itr++) {
  368. Face* face = *itr;
  369. float distance;
  370. if ((distance = rayIntersectsTriangle(point, direction, face->Vertex1, face->Vertex2, face->Vertex3)) != 0) {
  371. if (MinDistance == 0.0f || distance < MinDistance) {
  372. BestZ = face->Vertex2[1];
  373. MinDistance = distance;
  374. lastFace = face;
  375. Grid = (*mapitr).first;
  376. }
  377. }
  378. }
  379. }
  380. printf("GridID: %i, BestZ: %f yIn:% f\n", Grid, BestZ, y);
  381. float endY = 999999.0f;
  382. if (lastFace)
  383. {
  384. /* for (int i = 0; i < 3; i++)
  385. {
  386. for (int z = 0; z < 3; z++)
  387. {
  388. if (i == 0)
  389. printf("Face%i-%i: %f\n", i, z, lastFace->Vertex1[z]);
  390. else if (i == 1)
  391. printf("Face%i-%i: %f\n", i, z, lastFace->Vertex2[z]);
  392. else if (i == 2)
  393. printf("Face%i-%i: %f\n", i, z, lastFace->Vertex3[z]);
  394. }
  395. }*/
  396. endY = lastFace->Vertex2[1];
  397. }
  398. return endY;
  399. }
  400. Face* SPGrid::GetClosestFace(float x, float y, float z)
  401. {
  402. float temp_y = 0;
  403. float best_y = 999999.0f;
  404. FaceCell* startCell = GetFaceCell(x, z);
  405. float tmpY = y + 0.5f;
  406. float point[3];
  407. point[0] = x;
  408. point[1] = tmpY; // Small bump to make sure we are above ground when we do the trace
  409. point[2] = z;
  410. float MinDistance = 0.0f;
  411. // Create the direction for the trace, as we want what
  412. // is below it will just be -1 in the y direction
  413. float direction[3];
  414. direction[0] = 0.0f;
  415. direction[1] = -1.0f;
  416. direction[2] = 0.0f;
  417. Face* lastFace = 0;
  418. int32 Grid = 0;
  419. float BestZ = -999999.0f;
  420. map<int32, vector<Face*> >::iterator mapitr;
  421. for (mapitr = startCell->FaceList.begin(); mapitr != startCell->FaceList.end(); mapitr++) {
  422. vector<Face*>::iterator itr;
  423. for (itr = (*mapitr).second.begin(); itr != (*mapitr).second.end(); itr++) {
  424. Face* face = *itr;
  425. float distance;
  426. if ((distance = rayIntersectsTriangle(point, direction, face->Vertex1, face->Vertex2, face->Vertex3)) != 0) {
  427. if (MinDistance == 0.0f || distance < MinDistance) {
  428. BestZ = face->Vertex2[1];
  429. MinDistance = distance;
  430. lastFace = face;
  431. Grid = (*mapitr).first;
  432. }
  433. }
  434. }
  435. }
  436. return lastFace;
  437. }
  438. Face* SPGrid::FindPath(float x, float y, float z, float targX, float targY, float targZ, bool forceEndCell)
  439. {
  440. float MinDistance = 0.0f;
  441. float MinDistanceEnd = 999999.0f;
  442. // Create the starting point for the trace
  443. float point[3];
  444. point[0] = x;
  445. point[1] = y + 1.0f; // Small bump to make sure we are above ground when we do the trace
  446. point[2] = z;
  447. float pointEnd[3];
  448. pointEnd[0] = targX;
  449. pointEnd[1] = y + 1.0f; // Small bump to make sure we are above ground when we do the trace
  450. pointEnd[2] = targZ;
  451. // Create the direction for the trace, as we want what
  452. // is below it will just be -1 in the y direction
  453. float direction[3];
  454. if (!forceEndCell)
  455. {
  456. if (targX > x)
  457. direction[0] = -0.5f;
  458. else
  459. direction[0] = 0.5f;
  460. }
  461. else
  462. {
  463. if (targX > x)
  464. direction[0] = 1.0f;
  465. else// if (targZ < z)
  466. direction[0] = -1.0f;
  467. }
  468. //if (targY < y)
  469. direction[1] = -1.0f;
  470. //else
  471. // direction[1] = .5f;
  472. //direction[1] = -1.0f;
  473. if (forceEndCell)
  474. {
  475. if (targZ > z)
  476. direction[2] = -0.5f;
  477. else
  478. direction[2] = 0.5f;
  479. }
  480. else
  481. {
  482. if (targZ > z)
  483. direction[2] = 1.0f;
  484. else// if ( targX < x )
  485. direction[2] = -1.0f;
  486. }
  487. FaceCell* startCell = GetFaceCell(x, z);
  488. FaceCell* endCell = GetFaceCell(x, z);
  489. Face* startFace = GetClosestFace(x, y, z);
  490. if (startFace == NULL)
  491. return 0;
  492. //float tmpDistance = rayIntersectsTriangle(pointEnd, direction, startFace->Vertex1, startFace->Vertex2, startFace->Vertex3);
  493. //if (tmpDistance != 0.0f && tmpDistance < 15.0f)
  494. // return 0;
  495. Face* nextFace = 0;
  496. Face* endFace = GetClosestFace(targX, targY, targZ);
  497. float distBetweenEachOther = 999999.0f;
  498. map<int32, vector<Face*> >::iterator mapitr;
  499. if (endFace != NULL && startCell->FaceList.count(endFace->grid_id))
  500. mapitr = startCell->FaceList.find(endFace->grid_id);
  501. else if (startFace != NULL)
  502. mapitr = startCell->FaceList.find(startFace->grid_id);
  503. else
  504. return 0;
  505. //FILE* pFile;
  506. //pFile = fopen("vertices.txt", "a+");
  507. char msg[256];
  508. //_snprintf(msg, 256, "%f %f %f - %f %f %f\n", x,y,z,targX,targY,targZ);
  509. //fwrite(msg, 1, strnlen(msg, 256), pFile);
  510. for (; mapitr != startCell->FaceList.end(); mapitr++) {
  511. vector<Face*>::iterator itr;
  512. for (itr = (*mapitr).second.begin(); itr != (*mapitr).second.end(); itr++) {
  513. Face* face = *itr;
  514. float distance;
  515. float distanceend;
  516. distance = rayIntersectsTriangle(point, direction, face->Vertex1, face->Vertex2, face->Vertex3);
  517. //distanceend = rayIntersectsTriangle(pointEnd, direction, face->Vertex1, face->Vertex2, face->Vertex3);
  518. float tmpx1 = face->Vertex1[0] - pointEnd[0];
  519. float tmpy1 = face->Vertex1[1] - pointEnd[1];
  520. float tmpz1 = face->Vertex1[2] - pointEnd[2];
  521. float tmpDistBetweenEachOther = sqrt(tmpx1 * tmpx1 + tmpy1 * tmpy1 + tmpz1 * tmpz1);
  522. snprintf(msg, 256, "%f (%f): Face: %f %f %f\n", tmpDistBetweenEachOther, distance, face->Vertex1[0], face->Vertex1[1], face->Vertex1[2]);
  523. if (face == startFace)
  524. {
  525. printf("Hit Start Cell..%s\n",msg);
  526. break;
  527. }
  528. else if (face == endFace)
  529. {
  530. printf("Hit End Cell..%s\n",msg);
  531. //continue;
  532. }
  533. //fwrite(msg, 1, strnlen(msg,256), pFile);
  534. //printf("%f: Face: %f %f %f... distance: %f..\n", tmpDistBetweenEachOther, face->Vertex1[0], face->Vertex1[1], face->Vertex1[2],distance);
  535. if (distance > 0.0f && ((MinDistance == 0.0f || distance < MinDistance) || (tmpDistBetweenEachOther < distBetweenEachOther))) {
  536. printf("%f (%f): !HIT! Face: %f %f %f\n", tmpDistBetweenEachOther, distance, face->Vertex1[0], face->Vertex1[1], face->Vertex1[2]);
  537. distBetweenEachOther = tmpDistBetweenEachOther;
  538. nextFace = face;
  539. MinDistance = distance;
  540. }
  541. }
  542. }
  543. /*
  544. fwrite("\n", sizeof(char), 1, pFile);
  545. if (forceEndCell)
  546. fwrite("Y", sizeof(char), 1, pFile);
  547. fwrite("\n\n", sizeof(char), 2, pFile);
  548. fclose(pFile);*/
  549. Face* anotherAttempt = 0;
  550. if (!forceEndCell)
  551. {
  552. printf("ForceEndCellSet:\n");
  553. anotherAttempt = FindPath(x, y, z, targX, targY, targZ, true);
  554. }
  555. if (!nextFace)
  556. {
  557. if (anotherAttempt)
  558. nextFace = anotherAttempt;
  559. else
  560. nextFace = endFace;
  561. /*if (!forceEndCell)
  562. return FindPath(x, y, z, targX, targY, targZ, true);
  563. nextFace = endFace;*/
  564. }
  565. return nextFace;
  566. }
  567. /**********************************************************************
  568. Math functions/macros to test a ray intersection in 3D space
  569. **********************************************************************/
  570. /* a = b - c */
  571. #define vector(a,b,c) \
  572. (a)[0] = (b)[0] - (c)[0]; \
  573. (a)[1] = (b)[1] - (c)[1]; \
  574. (a)[2] = (b)[2] - (c)[2];
  575. #define crossProduct(a,b,c) \
  576. (a)[0] = (b)[1] * (c)[2] - (c)[1] * (b)[2]; \
  577. (a)[1] = (b)[2] * (c)[0] - (c)[2] * (b)[0]; \
  578. (a)[2] = (b)[0] * (c)[1] - (c)[0] * (b)[1];
  579. #define innerProduct(v,q) \
  580. ((v)[0] * (q)[0] + \
  581. (v)[1] * (q)[1] + \
  582. (v)[2] * (q)[2])
  583. // all parameters should be vectors (float[3])
  584. float rayIntersectsTriangle(float *p, float *d, float *v0, float *v1, float *v2) {
  585. float e1[3], e2[3], h[3], s[3], q[3];
  586. float a, f, u, v;
  587. vector(e1, v1, v0);
  588. vector(e2, v2, v0);
  589. crossProduct(h, d, e2);
  590. a = innerProduct(e1, h);
  591. if (a > -0.00001 && a < 0.00001)
  592. return 0;
  593. f = 1 / a;
  594. vector(s, p, v0);
  595. u = f * (innerProduct(s, h));
  596. if (u < 0.0 || u > 1.0)
  597. return 0;
  598. crossProduct(q, s, e1);
  599. v = f * innerProduct(d, q);
  600. if (v < 0.0 || u + v > 1.0)
  601. return 0;
  602. // at this stage we can compute t to find out where
  603. // the intersection point is on the line
  604. float t = f * innerProduct(e2, q);
  605. if (t > 0.00001) // ray intersection
  606. return t;
  607. else // this means that there is a line intersection
  608. // but not a ray intersection
  609. return 0;
  610. }