Open Source Tomb Raider Engine
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

LoaderTR2.cpp 26KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  1. /*!
  2. * \file src/loader/LoaderTR2.cpp
  3. * \brief TR2 level file loader
  4. *
  5. * \author xythobuz
  6. */
  7. #include <array>
  8. #include <cstdint>
  9. #include <vector>
  10. #include "global.h"
  11. #include "Log.h"
  12. #include "Mesh.h"
  13. #include "Room.h"
  14. #include "SoundManager.h"
  15. #include "TextureManager.h"
  16. #include "system/Sound.h"
  17. #include "utils/pixel.h"
  18. #include "loader/LoaderTR2.h"
  19. LoaderTR2::LoaderTR2() {
  20. }
  21. LoaderTR2::~LoaderTR2() {
  22. }
  23. int LoaderTR2::load(std::string f) {
  24. if (file.open(f) != 0) {
  25. return 1; // Could not open file
  26. }
  27. if (file.readU32() != 0x2D) {
  28. return 2; // Not a TR2 level?!
  29. }
  30. loadPaletteTextiles();
  31. file.seek(file.tell() + 4); // Unused value?
  32. loadRooms();
  33. loadFloorData();
  34. loadMeshes();
  35. loadMoveables();
  36. loadStaticMeshes();
  37. loadTextures();
  38. loadSprites();
  39. loadCameras();
  40. loadSoundSources();
  41. loadBoxesOverlapsZones();
  42. loadAnimatedTextures();
  43. loadItems();
  44. file.seek(file.tell() + 8192); // Skip Light map, only for 8bit coloring
  45. loadCinematicFrames();
  46. loadDemoData();
  47. loadSoundMap();
  48. loadSoundDetails();
  49. loadSampleIndices();
  50. loadExternalSoundFile(f);
  51. return 0; // TODO Not finished with implementation!
  52. }
  53. // ---- Textures ----
  54. void LoaderTR2::loadPaletteTextiles() {
  55. file.seek(file.tell() + 768); // Skip 8bit palette, 256 * 3 bytes
  56. // Read the 16bit palette, 256 * 4 bytes, RGBA, A unused
  57. std::array<uint32_t, 256> palette; //!< RGBA, A unused
  58. for (auto& x : palette)
  59. x = file.readU32();
  60. // TODO store palette somewhere
  61. uint32_t numTextiles = file.readU32();
  62. file.seek(file.tell() + (numTextiles * 256 * 256)); // Skip 8bit textiles
  63. // Read the 16bit textiles, numTextiles * 256 * 256 * 2 bytes
  64. for (unsigned int i = 0; i < numTextiles; i++) {
  65. std::array<uint8_t, 256 * 256 * 2> arr;
  66. for (auto& x : arr) {
  67. x = file.readU8();
  68. }
  69. // Convert 16bit textile to 32bit textile
  70. unsigned char* img = argb16to32(&arr[0], 256, 256);
  71. int r = getTextureManager().loadBufferSlot(img, 256, 256, ARGB, 32,
  72. TextureManager::TextureStorage::GAME, i);
  73. assert(r >= 0); //! \fixme properly handle error when texture could not be loaded!
  74. delete [] img;
  75. }
  76. }
  77. void LoaderTR2::loadTextures() {
  78. uint32_t numObjectTextures = file.readU32();
  79. for (unsigned int o = 0; o < numObjectTextures; o++) {
  80. // 0 means that a texture is all-opaque, and that transparency
  81. // information is ignored.
  82. // 1 means that transparency information is used. In 8-bit color,
  83. // index 0 is the transparent color, while in 16-bit color, the
  84. // top bit (0x8000) is the alpha channel (1 = opaque, 0 = transparent)
  85. uint16_t attribute = file.readU16();
  86. // Index into the textile list
  87. uint16_t tile = file.readU16();
  88. TextureTile* t = new TextureTile(attribute, tile);
  89. // The four corner vertices of the texture
  90. // The Pixel values are the actual coordinates of the vertexs pixel
  91. // The Coordinate values depend on where the other vertices are in
  92. // the object texture. And if the object texture is used to specify
  93. // a triangle, then the fourth vertexs values will all be zero
  94. // Coordinate is 1 if Pixel is the low val, 255 if high val in object texture
  95. for (int i = 0; i < 4; i++) {
  96. uint8_t xCoordinate = file.readU8();
  97. uint8_t xPixel = file.readU8();
  98. uint8_t yCoordinate = file.readU8();
  99. uint8_t yPixel = file.readU8();
  100. assert((xCoordinate != 1) || (xCoordinate != 255));
  101. assert((yCoordinate != 1) || (yCoordinate != 255));
  102. t->add(new TextureTileVertex(xCoordinate, xPixel, yCoordinate, yPixel));
  103. }
  104. getTextureManager().addTile(t);
  105. }
  106. }
  107. void LoaderTR2::loadAnimatedTextures() {
  108. uint32_t numWords = file.readU32() - 1;
  109. uint16_t numAnimatedTextures = file.readU16();
  110. std::vector<uint16_t> animatedTextures;
  111. for (unsigned int a = 0; a < numWords; a++) {
  112. animatedTextures.push_back(file.readU16());
  113. }
  114. int pos = 0;
  115. for (unsigned int a = 0; a < numAnimatedTextures; a++) {
  116. int count = animatedTextures.at(pos) + 1;
  117. if ((pos + count) >= numWords) {
  118. getLog() << "LoaderTR2: Invalid AnimatedTextures ("
  119. << pos + count << " >= " << numWords << ")!" << Log::endl;
  120. return;
  121. }
  122. for (int i = 0; i < count; i++) {
  123. getTextureManager().addAnimatedTile(a, animatedTextures.at(pos + i + 1));
  124. }
  125. pos += count + 1;
  126. }
  127. if (pos != numWords)
  128. getLog() << "LoaderTR2: Extra bytes at end of AnimatedTextures?" << Log::endl;
  129. }
  130. // ---- Rooms ----
  131. void LoaderTR2::loadRooms() {
  132. uint16_t numRooms = file.readU16();
  133. for (unsigned int i = 0; i < numRooms; i++) {
  134. // Room Header
  135. int32_t xOffset = file.read32();
  136. int32_t zOffset = file.read32();
  137. int32_t yBottom = file.read32(); // lowest point == largest y value
  138. int32_t yTop = file.read32(); // highest point == smallest y value
  139. // Number of data words (2 bytes) to follow
  140. uint32_t dataToFollow = file.readU32();
  141. std::vector<struct vertex_t> vertices;
  142. uint16_t numVertices = file.readU16();
  143. for (unsigned int v = 0; v < numVertices; v++) {
  144. struct vertex_t vert;
  145. // Vertex coordinates, relative to x/zOffset
  146. vert.x = file.read16();
  147. vert.y = file.read16();
  148. vert.z = file.read16();
  149. vert.light1 = file.read16();
  150. // Set of flags for special rendering effects
  151. // 0x8000 - Something to do with water surface?
  152. // 0x4000 - Underwater lighting modulation/movement if seen from above
  153. // 0x2000 - Water/Quicksand surface movement
  154. // 0x0010 - Normal?
  155. vert.attributes = file.readU16();
  156. vert.light2 = file.read16(); // Almost always equal to light1
  157. vertices.push_back(vert);
  158. }
  159. Room* room = new Room();
  160. uint16_t numRectangles = file.readU16();
  161. for (unsigned int r = 0; r < numRectangles; r++) {
  162. // Indices into the vertex list read just before
  163. uint16_t vertex1 = file.readU16();
  164. uint16_t vertex2 = file.readU16();
  165. uint16_t vertex3 = file.readU16();
  166. uint16_t vertex4 = file.readU16();
  167. // Index into the object-texture list
  168. uint16_t texture = file.readU16();
  169. room->getMesh().addTexturedRectangle(vertices.at(vertex1), vertices.at(vertex2),
  170. vertices.at(vertex3), vertices.at(vertex4), texture);
  171. }
  172. uint16_t numTriangles = file.readU16();
  173. for (unsigned int t = 0; t < numTriangles; t++) {
  174. // Indices into the room vertex list
  175. uint16_t vertex1 = file.readU16();
  176. uint16_t vertex2 = file.readU16();
  177. uint16_t vertex3 = file.readU16();
  178. // Index into the object-texture list
  179. uint16_t texture = file.readU16();
  180. room->getMesh().addTexturedTriangle(vertices.at(vertex1), vertices.at(vertex2),
  181. vertices.at(vertex3), texture);
  182. }
  183. uint16_t numSprites = file.readU16();
  184. for (unsigned int s = 0; s < numSprites; s++) {
  185. uint16_t vertex = file.readU16(); // Index into vertex list
  186. uint16_t texture = file.readU16(); // Index into object-texture list
  187. room->addSprite(new Sprite(vertices.at(vertex), texture));
  188. }
  189. uint16_t numPortals = file.readU16();
  190. for (unsigned int p = 0; p < numPortals; p++) {
  191. // Which room this portal leads to
  192. uint16_t adjoiningRoom = file.readU16();
  193. // Which way the portal faces
  194. // The normal points away from the adjacent room
  195. // To be seen through, it must point toward the viewpoint
  196. int16_t xNormal = file.read16();
  197. int16_t yNormal = file.read16();
  198. int16_t zNormal = file.read16();
  199. // The corners of this portal
  200. // The right-hand rule applies with respect to the normal
  201. int16_t xCorner1 = file.read16();
  202. int16_t yCorner1 = file.read16();
  203. int16_t zCorner1 = file.read16();
  204. int16_t xCorner2 = file.read16();
  205. int16_t yCorner2 = file.read16();
  206. int16_t zCorner2 = file.read16();
  207. int16_t xCorner3 = file.read16();
  208. int16_t yCorner3 = file.read16();
  209. int16_t zCorner3 = file.read16();
  210. int16_t xCorner4 = file.read16();
  211. int16_t yCorner4 = file.read16();
  212. int16_t zCorner4 = file.read16();
  213. // TODO store portals somewhere
  214. }
  215. uint16_t numZSectors = file.readU16();
  216. uint16_t numXSectors = file.readU16();
  217. for (unsigned int s = 0; s < (numZSectors * numXSectors); s++) {
  218. // Sectors are 1024*1024 world coordinates. Floor and Ceiling are
  219. // signed numbers of 256 units of height.
  220. // Floor/Ceiling value of 0x81 is used to indicate impenetrable
  221. // walls around the sector.
  222. // Floor values are used by the original engine to determine
  223. // what objects can be traversed and how. Relative steps of 1 (256)
  224. // can be walked up, 2..7 must be jumped up, larger than 7 is too high
  225. // If RoomAbove/Below is not none, the Ceiling/Floor is a collisional
  226. // portal to that room
  227. uint16_t indexFloorData = file.readU16();
  228. uint16_t indexBox = file.readU16(); // 0xFFFF if none
  229. uint8_t roomBelow = file.readU8(); // 0xFF if none
  230. int8_t floor = file.read8(); // Absolute height of floor (divided by 256)
  231. uint8_t roomAbove = file.readU8(); // 0xFF if none
  232. int8_t ceiling = file.read8(); // Absolute height of ceiling (/ 256)
  233. // TODO store sectors somewhere
  234. }
  235. int16_t intensity1 = file.read16();
  236. int16_t intensity2 = file.read16();
  237. int16_t lightMode = file.read16();
  238. uint16_t numLights = file.readU16();
  239. for (unsigned int l = 0; l < numLights; l++) {
  240. // Position of light, in world coordinates
  241. int32_t x = file.read32();
  242. int32_t y = file.read32();
  243. int32_t z = file.read32();
  244. uint16_t intensity1 = file.readU16();
  245. uint16_t intensity2 = file.readU16(); // Almost always equal to intensity1
  246. uint32_t fade1 = file.readU32(); // Falloff value?
  247. uint32_t fade2 = file.readU32(); // Falloff value?
  248. // TODO store light somewhere
  249. }
  250. uint16_t numStaticMeshes = file.readU16();
  251. for (unsigned int s = 0; s < numStaticMeshes; s++) {
  252. // Absolute position in world coordinates
  253. int32_t x = file.read32();
  254. int32_t y = file.read32();
  255. int32_t z = file.read32();
  256. // High two bits (0xC000) indicate steps of
  257. // 90 degrees (eg. (rotation >> 14) * 90)
  258. uint16_t rotation = file.readU16();
  259. // Constant lighting, 0xFFFF means use mesh lighting
  260. uint16_t intensity1 = file.readU16();
  261. uint16_t intensity2 = file.readU16();
  262. // Which StaticMesh item to draw
  263. uint16_t objectID = file.readU16();
  264. // TODO store static meshes somewhere
  265. }
  266. int16_t alternateRoom = file.read16();
  267. uint16_t flags = file.readU16();
  268. }
  269. }
  270. void LoaderTR2::loadFloorData() {
  271. uint32_t numFloorData = file.readU32();
  272. for (unsigned int f = 0; f < numFloorData; f++) {
  273. uint16_t unused = file.readU16();
  274. // TODO store floor data somewhere
  275. }
  276. if (numFloorData > 0)
  277. getLog() << "LoaderTR2: Found " << numFloorData << " words FloorData, unimplemented!" << Log::endl;
  278. }
  279. void LoaderTR2::loadSprites() {
  280. uint32_t numSpriteTextures = file.readU32();
  281. for (unsigned int s = 0; s < numSpriteTextures; s++) {
  282. uint16_t tile = file.readU16();
  283. uint8_t x = file.readU8();
  284. uint8_t y = file.readU8();
  285. uint16_t width = file.readU16(); // Actually (width * 256) + 255
  286. uint16_t height = file.readU16(); // Actually (height * 256) + 255
  287. int16_t leftSide = file.read16();
  288. int16_t topSide = file.read16();
  289. int16_t rightSide = file.read16();
  290. int16_t bottomSide = file.read16();
  291. // TODO store sprite textures somewhere
  292. }
  293. uint32_t numSpriteSequences = file.readU32();
  294. for (unsigned int s = 0; s < numSpriteSequences; s++) {
  295. int32_t objectID = file.read32(); // Item identifier, matched in Items[]
  296. int16_t negativeLength = file.read16(); // Negative sprite count
  297. int16_t offset = file.read16(); // Where sequence starts in sprite texture list
  298. // TODO store sprite sequences somewhere
  299. }
  300. }
  301. // ---- Meshes ----
  302. void LoaderTR2::loadMeshes() {
  303. // Number of bitu16s of mesh data to follow
  304. // Read all the mesh data into a buffer, because
  305. // only afterward we can read the number of meshes
  306. // in this data block
  307. uint32_t numMeshData = file.readU32();
  308. std::vector<uint16_t> buffer;
  309. for (unsigned int i = 0; i < numMeshData; i++) {
  310. buffer.push_back(file.readU16());
  311. }
  312. uint32_t numMeshPointers = file.readU32();
  313. if ((numMeshData == 0) || (numMeshPointers == 0)) {
  314. getLog() << "LoaderTR2: No mesh data found!" << Log::endl;
  315. return;
  316. }
  317. for (unsigned int i = 0; i < numMeshPointers; i++) {
  318. uint32_t meshPointer = file.readU32();
  319. char* tmpPtr = reinterpret_cast<char*>(&buffer[meshPointer]);
  320. BinaryMemory mem(tmpPtr, numMeshData - meshPointer);
  321. // TODO interpret the buffered mesh data
  322. }
  323. }
  324. void LoaderTR2::loadStaticMeshes() {
  325. uint32_t numStaticMeshes = file.readU32();
  326. for (unsigned int s = 0; s < numStaticMeshes; s++) {
  327. uint32_t objectID = file.readU32(); // Matched in Items[]
  328. uint16_t mesh = file.readU16(); // Offset into MeshPointers[]
  329. // tr2_vertex BoundingBox[2][2];
  330. // First index is which one, second index is opposite corners
  331. int16_t x11 = file.read16();
  332. int16_t y11 = file.read16();
  333. int16_t z11 = file.read16();
  334. int16_t x12 = file.read16();
  335. int16_t y12 = file.read16();
  336. int16_t z12 = file.read16();
  337. int16_t x21 = file.read16();
  338. int16_t y21 = file.read16();
  339. int16_t z21 = file.read16();
  340. int16_t x22 = file.read16();
  341. int16_t y22 = file.read16();
  342. int16_t z22 = file.read16();
  343. // Meaning uncertain. Usually 2, and 3 for objects Lara can
  344. // travel through, like TR2s skeletons and underwater plants
  345. uint16_t flags = file.readU16();
  346. // TODO store static meshes somewhere
  347. }
  348. if (numStaticMeshes > 0)
  349. getLog() << "LoaderTR2: Found " << numStaticMeshes << " StaticMeshes, unimplemented!" << Log::endl;
  350. }
  351. // ---- Moveables ----
  352. void LoaderTR2::loadMoveables() {
  353. uint32_t numAnimations = file.readU32();
  354. for (unsigned int a = 0; a < numAnimations; a++) {
  355. // *Byte* Offset into Frames[] (so divide by 2!)
  356. uint32_t frameOffset = file.readU32();
  357. uint8_t frameRate = file.readU8(); // Engine ticks per frame
  358. // Number of bit16s in Frames[] used by this animation
  359. // Be careful when parsing frames using the FrameSize value
  360. // as the size of each frame, since an animations frame range
  361. // may extend into the next animations frame range, and that
  362. // may have a different FrameSize value.
  363. uint8_t frameSize = file.readU8();
  364. uint16_t stateID = file.readU16();
  365. file.seek(file.tell() + 8); // Skip 8 unknown bytes
  366. uint16_t frameStart = file.readU16(); // First frame in this animation
  367. uint16_t frameEnd = file.readU16(); // Last frame in this animation
  368. uint16_t nextAnimation = file.readU16();
  369. uint16_t nextFrame = file.readU16();
  370. uint16_t numStateChanges = file.readU16();
  371. uint16_t stateChangeOffset = file.readU16(); // Index into StateChanges[]
  372. uint16_t numAnimCommands = file.readU16(); // How many animation commands to use
  373. uint16_t animCommandOffset = file.readU16(); // Index into AnimCommand[]
  374. // TODO store animations somewhere
  375. }
  376. uint32_t numStateChanges = file.readU32();
  377. for (unsigned int s = 0; s < numStateChanges; s++) {
  378. uint16_t stateID = file.readU16();
  379. uint16_t numAnimDispatches = file.readU16();
  380. uint16_t animDispatchOffset = file.readU16(); // Index into AnimDispatches[]
  381. // TODO store state changes somewhere
  382. }
  383. uint32_t numAnimDispatches = file.readU32();
  384. for (unsigned int a = 0; a < numAnimDispatches; a++) {
  385. int16_t low = file.read16(); // Lowest frame that uses this range
  386. int16_t high = file.read16(); // Highest frame (+1?) that uses this range
  387. int16_t nextAnimation = file.read16(); // Animation to go to
  388. int16_t nextFrame = file.read16(); // Frame offset to go to
  389. // TODO store animation dispatches somewhere
  390. }
  391. uint32_t numAnimCommands = file.readU32();
  392. std::vector<int16_t> animCommands;
  393. for (unsigned int a = 0; a < numAnimCommands; a++) {
  394. animCommands.push_back(file.read16());
  395. }
  396. uint32_t numMeshTrees = file.readU32();
  397. for (unsigned int m = 0; m < numMeshTrees; m++) {
  398. // 0x0002 - Put parent mesh on the mesh stack
  399. // 0x0001 - Pop mesh from stack, use as parent mesh
  400. // When both are not set, use previous mesh as parent mesh
  401. // When both are set, do 0x0001 first, then 0x0002, thereby
  402. // reading the stack but not changing it
  403. uint32_t flags = file.readU32();
  404. // Offset of mesh origin from the parent mesh origin
  405. /* Where the hell does this come from?!
  406. int32_t x = file.read32();
  407. int32_t y = file.read32();
  408. int32_t z = file.read32();
  409. */
  410. // TODO store mesh trees somewhere
  411. }
  412. uint32_t numFrames = file.readU32();
  413. std::vector<uint16_t> frames;
  414. for (unsigned int f = 0; f < numFrames; f++) {
  415. frames.push_back(file.readU16());
  416. }
  417. uint32_t numMoveables = file.readU32();
  418. for (unsigned int m = 0; m < numMoveables; m++) {
  419. // Item identifier, matched in Items[]
  420. uint32_t objectID = file.readU32();
  421. uint16_t numMeshes = file.readU16();
  422. uint16_t startingMesh = file.readU16(); // Offset into MeshPointers[]
  423. uint32_t meshTree = file.readU32(); // Offset into MeshTree[]
  424. // *Byte* offset into Frames[] (divide by 2 for Frames[i])
  425. uint32_t frameOffset = file.readU32();
  426. // If animation index is 0xFFFF, the object is stationary or
  427. // animated by the engine (ponytail)
  428. uint16_t animation = file.readU16();
  429. // TODO store moveables somewhere
  430. }
  431. // TODO combine all this into moveables with their animations
  432. }
  433. void LoaderTR2::loadItems() {
  434. uint32_t numItems = file.readU32();
  435. for (unsigned int i = 0; i < numItems; i++) {
  436. int16_t objectID = file.read16();
  437. int16_t room = file.read16();
  438. // Item position in world coordinates
  439. int32_t x = file.read32();
  440. int32_t y = file.read32();
  441. int32_t z = file.read32();
  442. int16_t angle = file.read16(); // (0xC000 >> 14) * 90deg
  443. int16_t intensity1 = file.read16(); // Constant lighting; -1 means mesh lighting
  444. int16_t intensity2 = file.read16(); // Almost always like intensity1
  445. // 0x0100 - Initially visible
  446. // 0x3E00 - Activation mask, open, can be XORed with related FloorData list fields.
  447. uint16_t flags = file.readU16();
  448. // TODO store items somewhere
  449. }
  450. if (numItems > 0)
  451. getLog() << "LoaderTR2: Found " << numItems << " Items, unimplemented!" << Log::endl;
  452. }
  453. void LoaderTR2::loadBoxesOverlapsZones() {
  454. uint32_t numBoxes = file.readU32();
  455. for (unsigned int b = 0; b < numBoxes; b++) {
  456. // Sectors (* 1024 units)
  457. uint8_t zMin = file.readU8();
  458. uint8_t zMax = file.readU8();
  459. uint8_t xMin = file.readU8();
  460. uint8_t xMax = file.readU8();
  461. int16_t trueFloor = file.read16(); // Y value (no scaling)
  462. // Index into overlaps[]. The high bit is sometimes set
  463. // this occurs in front of swinging doors and the like
  464. int16_t overlapIndex = file.read16();
  465. // TODO store boxes somewhere
  466. }
  467. uint32_t numOverlaps = file.readU32();
  468. std::vector<uint16_t> overlaps;
  469. for (unsigned int o = 0; o < numOverlaps; o++) {
  470. overlaps.push_back(file.readU16());
  471. }
  472. // TODO store overlaps somewhere
  473. std::vector<int16_t> zones;
  474. for (unsigned int z = 0; z < numBoxes; z++) {
  475. for (unsigned int i = 0; i < 10; i++) {
  476. zones.push_back(file.read16());
  477. }
  478. }
  479. // TODO store zones somewhere
  480. if ((numBoxes > 0) || (numOverlaps > 0))
  481. getLog() << "LoaderTR2: Found NPC NavigationHints, not yet implemented!" << Log::endl;
  482. }
  483. // ---- Sound ----
  484. void LoaderTR2::loadSoundSources() {
  485. uint32_t numSoundSources = file.readU32();
  486. for (unsigned int s = 0; s < numSoundSources; s++) {
  487. // Absolute world coordinate positions of sound source
  488. int32_t x = file.read32();
  489. int32_t y = file.read32();
  490. int32_t z = file.read32();
  491. // Internal sound index
  492. uint16_t soundID = file.readU16();
  493. // Unknown, 0x40, 0x80 or 0xC0
  494. uint16_t flags = file.readU16();
  495. SoundManager::addSoundSource(x, y, z, soundID, flags);
  496. }
  497. if (numSoundSources > 0)
  498. getLog() << "LoaderTR2: Found " << numSoundSources << " SoundSources" << Log::endl;
  499. }
  500. void LoaderTR2::loadSoundMap() {
  501. for (int i = 0; i < 370; i++) {
  502. SoundManager::addSoundMapEntry(file.read16());
  503. }
  504. }
  505. void LoaderTR2::loadSoundDetails() {
  506. uint32_t numSoundDetails = file.readU32();
  507. for (unsigned int s = 0; s < numSoundDetails; s++) {
  508. uint16_t sample = file.readU16(); // Index into SampleIndices[]
  509. uint16_t volume = file.readU16();
  510. // sound range? distance at which this sound can be heard?
  511. uint16_t unknown1 = file.readU16();
  512. // Bits 8-15: priority?
  513. // Bits 2-7: number of samples in this group
  514. // Bits 0-1: channel number?
  515. uint16_t unknown2 = file.readU16();
  516. SoundManager::addSoundDetail(sample, ((float)volume) / 32767.0f);
  517. }
  518. if (numSoundDetails > 0)
  519. getLog() << "LoaderTR2: Found " << numSoundDetails << " SoundDetails" << Log::endl;
  520. }
  521. void LoaderTR2::loadSampleIndices() {
  522. uint32_t numSampleIndices = file.readU32();
  523. for (unsigned int i = 0; i < numSampleIndices; i++) {
  524. SoundManager::addSampleIndex(file.readU32());
  525. }
  526. if (numSampleIndices > 0)
  527. getLog() << "LoaderTR2: Found " << numSampleIndices << " SampleIndices" << Log::endl;
  528. }
  529. void LoaderTR2::loadExternalSoundFile(std::string f) {
  530. size_t dir = f.find_last_of("/\\");
  531. if (dir != std::string::npos) {
  532. f.replace(dir + 1, std::string::npos, "MAIN.SFX");
  533. } else {
  534. f = "MAIN.SFX";
  535. }
  536. BinaryFile sfx;
  537. if (sfx.open(f) != 0) {
  538. getLog() << "LoaderTR2: Can't open \"" << f << "\"!" << Log::endl;
  539. return;
  540. }
  541. int riffCount = 0;
  542. while (!sfx.eof()) {
  543. char test[5];
  544. test[4] = '\0';
  545. for (int i = 0; i < 4; i++)
  546. test[i] = sfx.read8();
  547. if (std::string("RIFF") != std::string(test)) {
  548. getLog() << "LoaderTR2: External SFX invalid! (" << riffCount
  549. << ", \"" << test << "\" != \"RIFF\")" << Log::endl;
  550. return;
  551. }
  552. // riffSize is (fileLength - 8)
  553. uint32_t riffSize = sfx.readU32();
  554. unsigned char buff[riffSize + 8];
  555. sfx.seek(sfx.tell() - 8);
  556. for (int i = 0; i < (riffSize + 8); i++)
  557. buff[i] = sfx.readU8();
  558. unsigned long src;
  559. int ret = Sound::loadBuffer(buff, riffSize + 8);
  560. assert(ret >= 0);
  561. riffCount++;
  562. }
  563. if (riffCount > 0)
  564. getLog() << "LoaderTR2: Found " << riffCount << " SoundSamples" << Log::endl;
  565. }
  566. // ---- Stuff ----
  567. void LoaderTR2::loadCameras() {
  568. uint32_t numCameras = file.readU32();
  569. for (unsigned int c = 0; c < numCameras; c++) {
  570. int32_t x = file.read32();
  571. int32_t y = file.read32();
  572. int32_t z = file.read32();
  573. int16_t room = file.read16();
  574. file.seek(file.tell() + 2); // Unknown, correlates to Boxes? Zones?
  575. // TODO store cameras somewhere
  576. }
  577. if (numCameras > 0)
  578. getLog() << "LoaderTR2: Found " << numCameras << " Cameras, unimplemented!" << Log::endl;
  579. }
  580. void LoaderTR2::loadCinematicFrames() {
  581. uint16_t numCinematicFrames = file.readU16();
  582. for (unsigned int c = 0; c < numCinematicFrames; c++) {
  583. int16_t rotY = file.read16(); // Y rotation, +-32767 = +-180deg
  584. int16_t rotZ = file.read16(); // Z rotation, like rotY
  585. int16_t rotZ2 = file.read16(); // Like rotZ?
  586. int16_t posZ = file.read16(); // Camera pos relative to what?
  587. int16_t posY = file.read16();
  588. int16_t posX = file.read16();
  589. int16_t unknown = file.read16(); // Changing this can cause runtime error
  590. int16_t rotX = file.read16(); // X rotation, like rotY
  591. // TODO store cinematic frames somewhere
  592. }
  593. if (numCinematicFrames > 0)
  594. getLog() << "LoaderTR2: Found " << numCinematicFrames
  595. << " CinematicFrames, not yet implemented!" << Log::endl;
  596. }
  597. void LoaderTR2::loadDemoData() {
  598. uint16_t numDemoData = file.readU16();
  599. for (unsigned int d = 0; d < numDemoData; d++)
  600. file.readU8();
  601. // TODO store demo data somewhere, find out meaning
  602. if (numDemoData > 0)
  603. getLog() << "LoaderTR2: Found " << numDemoData << " bytes DemoData, not yet implemented!" <<
  604. Log::endl;
  605. }