Open Source Tomb Raider Engine
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

World.cpp 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  1. /* -*- Mode: C++; tab-width: 3; indent-tabs-mode: t; c-basic-offset: 3 -*- */
  2. /*================================================================
  3. *
  4. * Project : OpenRaider
  5. * Author : Terry 'Mongoose' Hendrix II
  6. * Website : http://www.westga.edu/~stu7440/
  7. * Email : stu7440@westga.edu
  8. * Object : World
  9. * License : No use w/o permission (C) 2002 Mongoose
  10. * Comments: The game world ( model )
  11. *
  12. *
  13. * This file was generated using Mongoose's C++
  14. * template generator script. <stu7440@westga.edu>
  15. *
  16. *-- History -------------------------------------------------
  17. *
  18. * 2002.12.16:
  19. * Mongoose - Created
  20. =================================================================*/
  21. #ifdef DEBUG_MEMEORY
  22. # include "memeory_test.h"
  23. #endif
  24. #include <math.h>
  25. #include "World.h"
  26. ////////////////////////////////////////////////////////////
  27. // Constructors
  28. ////////////////////////////////////////////////////////////
  29. World::World()
  30. {
  31. mClearLock = false;
  32. mFlags = 0;
  33. mEntities.setError(0x0);
  34. mRooms.setError(0x0);
  35. mMeshes.setError(0x0);
  36. mSprites.setError(0x0);
  37. mModels.setError(0x0);
  38. }
  39. World::~World()
  40. {
  41. destroy();
  42. }
  43. ////////////////////////////////////////////////////////////
  44. // Public Accessors
  45. ////////////////////////////////////////////////////////////
  46. int World::getRoomByLocation(int index, float x, float y, float z)
  47. {
  48. room_mesh_t *room = mRooms[index];
  49. if (room)
  50. {
  51. if (x > room->bbox_min[0] && x < room->bbox_max[0] &&
  52. z > room->bbox_min[2] && z < room->bbox_max[2])
  53. {
  54. if (y > room->bbox_min[1] && y < room->bbox_max[1])
  55. return index;
  56. }
  57. }
  58. return getRoomByLocation(x, y, z);
  59. }
  60. int World::getRoomByLocation(float x, float y, float z)
  61. {
  62. room_mesh_t *room;
  63. int hop = -1;
  64. for (mRooms.start(); mRooms.forward(); mRooms.next())
  65. {
  66. room = mRooms.current();
  67. if (!room)
  68. continue;
  69. if (x > room->bbox_min[0] && x < room->bbox_max[0] &&
  70. z > room->bbox_min[2] && z < room->bbox_max[2])
  71. {
  72. // This room contains current position
  73. if (y > room->bbox_min[1] && y < room->bbox_max[1])
  74. return mRooms.getCurrentIndex();
  75. // This room is above or below current position
  76. hop = mRooms.getCurrentIndex();
  77. }
  78. }
  79. // Room is -1? Must be in void, try to hop to room with same X,Z
  80. if (mFlags & fEnableHopping)
  81. return hop;
  82. return -1;
  83. }
  84. int World::getAdjoiningRoom(int index,
  85. float x, float y, float z,
  86. float x2, float y2, float z2)
  87. {
  88. room_mesh_t *room = mRooms[index];
  89. portal_t * portal;
  90. vec3_t intersect, p1, p2;
  91. p1[0] = x; p1[1] = y; p1[2] = z;
  92. p2[0] = x2; p2[1] = y2; p2[2] = z2;
  93. if (room)
  94. {
  95. for (room->portals.start(); room->portals.forward();
  96. room->portals.next())
  97. {
  98. portal = room->portals.current();
  99. if (!portal)
  100. continue;
  101. if (helIntersectionLineAndPolygon(intersect, p1, p2, 4,
  102. portal->vertices))
  103. {
  104. return portal->adjoining_room;
  105. }
  106. }
  107. }
  108. return -1;
  109. }
  110. int World::getSector(int room, float x, float z, float *floor, float *ceiling)
  111. {
  112. room_mesh_t *r;
  113. sector_t * s;
  114. int sector;
  115. r = mRooms[room];
  116. if (!r)
  117. return -1;
  118. sector = (((((int)x - (int)r->pos[0]) / 1024) * r->numZSectors) +
  119. (((int)z - (int)r->pos[2]) / 1024));
  120. if (sector > -1)
  121. {
  122. s = r->sectors[sector];
  123. if (!s)
  124. return -1;
  125. *floor = s->floor;
  126. *ceiling = s->ceiling;
  127. }
  128. return sector;
  129. }
  130. int World::getSector(int room, float x, float z)
  131. {
  132. int sector;
  133. room_mesh_t *r;
  134. r = mRooms[room];
  135. if (!r)
  136. {
  137. return -1;
  138. }
  139. sector = (((((int)x - (int)r->pos[0]) / 1024) * r->numZSectors) +
  140. (((int)z - (int)r->pos[2]) / 1024));
  141. if (sector < 0)
  142. {
  143. return -1;
  144. }
  145. return sector;
  146. }
  147. unsigned int World::getRoomInfo(int room)
  148. {
  149. room_mesh_t *r;
  150. r = mRooms[room];
  151. if (!r)
  152. {
  153. return 0;
  154. }
  155. return r->flags;
  156. }
  157. bool World::isWall(int room, int sector)
  158. {
  159. room_mesh_t *r;
  160. sector_t *sect;
  161. r = mRooms[room];
  162. if (!r)
  163. {
  164. return true;
  165. }
  166. sect = r->sectors[sector];
  167. if (!sect)
  168. {
  169. return true;
  170. }
  171. return (sector > 0 && sect->wall);
  172. }
  173. bool World::getHeightAtPosition(int index, float x, float *y, float z)
  174. {
  175. room_mesh_t *room = mRooms[index];
  176. #ifdef OBSOLETE_USING_BOXES
  177. unsigned int i;
  178. float zmax, xmax, zmin, xmin;
  179. if (!room)
  180. {
  181. return false;
  182. }
  183. // Mongoose 2002.08.14, It's 0302 - give me a fucking break --
  184. // this works albeit poorly =)
  185. for (i = 0; (int)i < room->num_boxes; ++i)
  186. {
  187. xmax = room->boxes[i].c.pos[0];
  188. xmin = room->boxes[i].a.pos[0];
  189. zmax = room->boxes[i].c.pos[2];
  190. zmin = room->boxes[i].a.pos[2];
  191. if (x < xmax && x > xmin && z < zmax && z > zmin)
  192. {
  193. //printf("%f %f %f %f\n", xmax, xmin, zmax, zmin);
  194. *y = room->boxes[i].a.pos[1]; // hhmm...room->pos[1] +
  195. return true;
  196. }
  197. }
  198. return false;
  199. #else
  200. int sector;
  201. sector_t *sect;
  202. if (!room)
  203. {
  204. return false;
  205. }
  206. // Mongoose 2002.08.14, Remember sector_z is width of sector array
  207. sector = getSector(index, x, z);
  208. sect = room->sectors[sector];
  209. if (!sect)
  210. {
  211. return true;
  212. }
  213. *y = sect->floor;
  214. return true;
  215. #endif
  216. }
  217. // Temp methods for rendering use until more refactoring is done
  218. #ifdef BAD_BLOOD
  219. model_mesh_t *World::getMesh(int index)
  220. {
  221. return mMeshes[index];
  222. }
  223. skeletal_model_t *World::getModel(int index)
  224. {
  225. return mModels[index];
  226. }
  227. room_mesh_t *World::getRoom(int index)
  228. {
  229. return mRooms[index];
  230. }
  231. Vector<entity_t *> *World::getEntities()
  232. {
  233. return &mEntities;
  234. }
  235. Vector<sprite_seq_t *> *World::getSprites()
  236. {
  237. return &mSprites;
  238. }
  239. Vector<room_mesh_t *> *World::getRooms()
  240. {
  241. return &mRooms;
  242. }
  243. #endif
  244. ////////////////////////////////////////////////////////////
  245. // Public Mutators
  246. ////////////////////////////////////////////////////////////
  247. void World::setFlag(WorldFlag flag)
  248. {
  249. mFlags |= flag;
  250. }
  251. void World::clearFlag(WorldFlag flag)
  252. {
  253. mFlags |= flag;
  254. mFlags ^= flag;
  255. }
  256. void World::destroy()
  257. {
  258. // Add some locking to check use state first
  259. if (!mClearLock)
  260. {
  261. clear();
  262. }
  263. }
  264. void World::addRoom(room_mesh_t *room)
  265. {
  266. mClearLock = false;
  267. mRooms.pushBack(room);
  268. }
  269. void World::addMesh(model_mesh_t *mesh)
  270. {
  271. if (mesh)
  272. {
  273. mClearLock = false;
  274. mMeshes.pushBack(mesh);
  275. }
  276. }
  277. void World::addEntity(entity_t *e)
  278. {
  279. if (e)
  280. {
  281. mClearLock = false;
  282. e->master = 0x0;
  283. e->moveType = worldMoveType_walk; // Walk
  284. e->room = getRoomByLocation(e->pos[0], e->pos[1], e->pos[2]);
  285. mEntities.pushBack(e);
  286. }
  287. }
  288. int World::addModel(skeletal_model_t *model)
  289. {
  290. if (model)
  291. {
  292. mClearLock = false;
  293. mModels.pushBack(model);
  294. return mModels.end();
  295. }
  296. return -1;
  297. }
  298. void World::addSprite(sprite_seq_t *sprite)
  299. {
  300. if (sprite)
  301. {
  302. mClearLock = false;
  303. mSprites.pushBack(sprite);
  304. }
  305. }
  306. void World::moveEntity(entity_t *e, char movement)
  307. {
  308. const float moved = 180.0f;
  309. const float testd = 220.0f;
  310. const float camHeight = 8.0f;
  311. float x, y, z, pitch, h, floor, ceiling;
  312. int room, sector;
  313. bool wall;
  314. unsigned int roomFlags;
  315. if (!e)
  316. {
  317. return;
  318. }
  319. switch (e->moveType)
  320. {
  321. case worldMoveType_walkNoSwim:
  322. case worldMoveType_walk:
  323. pitch = 0.0f; // in the future pitch could control jump up blocks here
  324. break;
  325. default:
  326. pitch = e->angles[2];
  327. }
  328. switch (movement)
  329. {
  330. case 'f':
  331. x = e->pos[0] + (testd * sin(e->angles[1]));
  332. y = e->pos[1] + (testd * sin(pitch));
  333. z = e->pos[2] + (testd * cos(e->angles[1]));
  334. break;
  335. case 'b':
  336. x = e->pos[0] - (testd * sin(e->angles[1]));
  337. y = e->pos[1] - (testd * sin(pitch));
  338. z = e->pos[2] - (testd * cos(e->angles[1]));
  339. break;
  340. case 'l':
  341. x = e->pos[0] - (testd * sin(e->angles[1] + 90.0));
  342. y = e->pos[1];
  343. z = e->pos[2] - (testd * cos(e->angles[1] + 90.0));
  344. break;
  345. case 'r':
  346. x = e->pos[0] + (testd * sin(e->angles[1] + 90.0));
  347. y = e->pos[1];
  348. z = e->pos[2] + (testd * cos(e->angles[1] + 90.0));
  349. break;
  350. default:
  351. return;
  352. }
  353. //room = getRoomByLocation(x, y, z);
  354. room = getRoomByLocation(e->room, x, y, z);
  355. if (room == -1) // Will we hit a portal?
  356. {
  357. #define ADJ_ROOM_CHECK
  358. #ifdef ADJ_ROOM_CHECK
  359. room = getAdjoiningRoom(e->room,
  360. e->pos[0], e->pos[1], e->pos[2],
  361. x, y, z);
  362. #else
  363. if (!mFlags & fEnableHopping)
  364. {
  365. mFlags |= fEnableHopping;
  366. room = getRoomByLocation(e->room, x, y, z);
  367. printf("Hopped\n");
  368. mFlags ^= fEnableHopping;
  369. }
  370. //room = getRoomByLocation(x, y, z);
  371. #endif
  372. if (room > -1)
  373. {
  374. printf("Crossing from room %i to %i\n", e->room, room);
  375. }
  376. }
  377. roomFlags = getRoomInfo(room);
  378. sector = getSector(room, x, z, &floor, &ceiling);
  379. wall = isWall(room, sector);
  380. // If you're underwater you may want to swim =)
  381. // ...if you're worldMoveType_walkNoSwim, you better hope it's shallow
  382. if (roomFlags & roomFlag_underWater && e->moveType == worldMoveType_walk)
  383. {
  384. e->moveType = worldMoveType_swim;
  385. }
  386. // Don't swim on land
  387. if (!(roomFlags & roomFlag_underWater) && e->moveType == worldMoveType_swim)
  388. {
  389. e->moveType = worldMoveType_walk;
  390. }
  391. // Mongoose 2002.09.02, Add check for room -> room transition
  392. // ( Only allow by movement between rooms by using portals )
  393. if ((e->moveType == worldMoveType_noClipping ||
  394. e->moveType == worldMoveType_fly ||
  395. e->moveType == worldMoveType_swim) ||
  396. room > -1 && !wall)
  397. {
  398. e->room = room;
  399. switch (movement)
  400. {
  401. case 'f':
  402. x = e->pos[0] + (moved * sin(e->angles[1]));
  403. y = e->pos[1] + (moved * sin(pitch));
  404. z = e->pos[2] + (moved * cos(e->angles[1]));
  405. break;
  406. case 'b':
  407. x = e->pos[0] - (moved * sin(e->angles[1]));
  408. y = e->pos[1] - (moved * sin(pitch));
  409. z = e->pos[2] - (moved * cos(e->angles[1]));
  410. break;
  411. case 'l':
  412. x = e->pos[0] - (moved * sin(e->angles[1] + 90.0));
  413. z = e->pos[2] - (moved * cos(e->angles[1] + 90.0));
  414. break;
  415. case 'r':
  416. x = e->pos[0] + (moved * sin(e->angles[1] + 90.0));
  417. z = e->pos[2] + (moved * cos(e->angles[1] + 90.0));
  418. break;
  419. }
  420. // FIXME: Test for vector (move vector) / plane (portal) collision here
  421. // to see if we need to switch rooms... man...
  422. h = y;
  423. getHeightAtPosition(room, x, &h, z);
  424. switch (e->moveType)
  425. {
  426. case worldMoveType_fly:
  427. case worldMoveType_swim:
  428. #ifdef DIVE_GAP
  429. // Clips to top of water, waiting for DIVE event
  430. if (h < floor)
  431. e->pos[1] = floor;
  432. else if (h > ceiling)
  433. e->pos[1] = ceiling;
  434. else
  435. e->pos[1] = y;
  436. #endif
  437. // Don't fall out of world, avoid a movement that does
  438. if (h > y - camHeight)
  439. {
  440. e->pos[0] = x;
  441. e->pos[1] = y;
  442. e->pos[2] = z;
  443. }
  444. break;
  445. case worldMoveType_walk:
  446. case worldMoveType_walkNoSwim:
  447. y = e->pos[1]; // Override vector movement walking ( er, not pretty )
  448. // Now fake gravity
  449. // Mongoose 2002.08.14, Remember TR is upside down ( you fall 'up' )
  450. //ddist = h - e->pos[1];
  451. // This is to force false gravity, by making camera stay on ground
  452. e->pos[1] = h; //roomFloor->bbox_min[1];
  453. // Check for camera below terrian and correct
  454. if (e->pos[1] < h - camHeight)
  455. {
  456. e->pos[1] = h - camHeight;
  457. }
  458. e->pos[0] = x;
  459. e->pos[2] = z;
  460. break;
  461. case worldMoveType_noClipping:
  462. e->pos[0] = x;
  463. e->pos[1] = y;
  464. e->pos[2] = z;
  465. }
  466. #ifdef OBSOLETE
  467. m_text->SetString(1,"Room %2i Sector %2i %sPos %.0f %.0f %.0f Yaw %.0f",
  468. room, sector,
  469. wall ? " Wall " : " ",
  470. e->pos[0], e->pos[1], e->pos[2], e->angles[1]);
  471. #endif
  472. }
  473. else
  474. {
  475. e->moving = false;
  476. return;
  477. }
  478. e->room = room;
  479. e->moving = true;
  480. }
  481. ////////////////////////////////////////////////////////////
  482. // Private Accessors
  483. ////////////////////////////////////////////////////////////
  484. ////////////////////////////////////////////////////////////
  485. // Private Mutators
  486. ////////////////////////////////////////////////////////////
  487. void World::clear()
  488. {
  489. room_mesh_t *room;
  490. model_mesh_t *mesh;
  491. sprite_seq_t *sprite;
  492. skeletal_model_t *model;
  493. bone_frame_t *boneframe;
  494. bone_tag_t *tag;
  495. animation_frame_t *animation;
  496. List<skeletal_model_t *> cache;
  497. mClearLock = true;
  498. mEntities.erase();
  499. for (mRooms.start(); mRooms.forward(); mRooms.next())
  500. {
  501. room = mRooms.current();
  502. if (room)
  503. {
  504. room->portals.erase();
  505. room->models.erase();
  506. room->sprites.erase();
  507. room->sectors.erase();
  508. room->boxes.erase();
  509. }
  510. }
  511. mRooms.erase();
  512. for (mMeshes.start(); mMeshes.forward(); mMeshes.next())
  513. {
  514. mesh = mMeshes.current();
  515. if (!mesh)
  516. continue;
  517. for (mesh->texturedTriangles.start();
  518. mesh->texturedTriangles.forward();
  519. mesh->texturedTriangles.next())
  520. {
  521. if (mesh->texturedTriangles.current())
  522. delete mesh->texturedTriangles.current();
  523. }
  524. for (mesh->coloredTriangles.start(); mesh->coloredTriangles.forward();
  525. mesh->coloredTriangles.next())
  526. {
  527. if (mesh->coloredTriangles.current())
  528. delete mesh->coloredTriangles.current();
  529. }
  530. for (mesh->texturedRectangles.start();
  531. mesh->texturedRectangles.forward();
  532. mesh->texturedRectangles.next())
  533. {
  534. if (mesh->texturedRectangles.current())
  535. delete mesh->texturedRectangles.current();
  536. }
  537. for (mesh->coloredRectangles.start(); mesh->coloredRectangles.forward();
  538. mesh->coloredRectangles.next())
  539. {
  540. if (mesh->coloredRectangles.current())
  541. delete mesh->coloredRectangles.current();
  542. }
  543. if (mesh->vertices)
  544. delete [] mesh->vertices;
  545. if (mesh->colors)
  546. delete [] mesh->colors;
  547. if (mesh->normals)
  548. delete [] mesh->normals;
  549. delete mesh;
  550. }
  551. mMeshes.clear();
  552. for (mSprites.start(); mSprites.forward(); mSprites.next())
  553. {
  554. sprite = mSprites.current();
  555. if (!sprite)
  556. continue;
  557. if (sprite->sprite)
  558. delete [] sprite->sprite;
  559. delete sprite;
  560. }
  561. mSprites.clear();
  562. for (mModels.start(); mModels.forward(); mModels.next())
  563. {
  564. model = mModels.current();
  565. if (!model)
  566. continue;
  567. // No smart pointers, so skip if deleted once =)
  568. if (!cache.SearchKey(model) == UINT_MAX)
  569. {
  570. cache.Add(model);
  571. }
  572. else
  573. {
  574. continue;
  575. }
  576. for (model->animation.start(); model->animation.forward();
  577. model->animation.next())
  578. {
  579. animation = model->animation.current();
  580. if (!animation)
  581. continue;
  582. for (animation->frame.start(); animation->frame.forward();
  583. animation->frame.next())
  584. {
  585. boneframe = animation->frame.current();
  586. if (!boneframe)
  587. continue;
  588. for (boneframe->tag.start(); boneframe->tag.forward();
  589. boneframe->tag.next())
  590. {
  591. tag = boneframe->tag.current();
  592. if (!tag)
  593. continue;
  594. delete tag;
  595. }
  596. delete boneframe;
  597. }
  598. delete animation;
  599. }
  600. delete model;
  601. }
  602. mModels.clear();
  603. }
  604. ////////////////////////////////////////////////////////////
  605. // Unit Test code
  606. ////////////////////////////////////////////////////////////
  607. #ifdef UNIT_TEST_WORLD
  608. int runWorldUnitTest(int argc, char *argv[])
  609. {
  610. return 0;
  611. }
  612. int main(int argc, char *argv[])
  613. {
  614. printf("[World class test]\n");
  615. runWorldUnitTest(argc, argv);
  616. return 0;
  617. }
  618. #endif