SPGrid.cpp 19 KB

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