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.

LoaderTR1.cpp 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*!
  2. * \file src/loader/LoaderTR1.cpp
  3. * \brief TR1 level file loader
  4. *
  5. * \author xythobuz
  6. */
  7. #include "global.h"
  8. #include "Game.h"
  9. #include "Log.h"
  10. #include "SoundManager.h"
  11. #include "World.h"
  12. #include "loader/LoaderTR1.h"
  13. int LoaderTR1::load(std::string f) {
  14. if (file.open(f) != 0) {
  15. return 1; // Could not open file
  16. }
  17. uint32_t version = file.readU32();
  18. if (version != 0x20) {
  19. return 2; // Not a TR1 level?!
  20. }
  21. loadTextures();
  22. file.seek(file.tell() + 4); // Unused value?
  23. loadRooms();
  24. loadFloorData();
  25. loadMeshes();
  26. loadMoveables();
  27. loadStaticMeshes();
  28. loadTextiles();
  29. loadSprites();
  30. loadCameras();
  31. loadSoundSources();
  32. loadBoxesOverlapsZones();
  33. loadAnimatedTextures();
  34. loadItems();
  35. file.seek(file.tell() + 8192); // TODO light map!
  36. loadPalette();
  37. loadCinematicFrames();
  38. loadDemoData();
  39. loadSoundMap();
  40. loadSoundDetails();
  41. loadSoundSamples();
  42. return 0;
  43. }
  44. void LoaderTR1::loadPalette() {
  45. // Read the 8bit palette, 256 * 3 bytes, RGB
  46. for (int i = 0; i < 256; i++) {
  47. uint8_t r = file.readU8();
  48. uint8_t g = file.readU8();
  49. uint8_t b = file.readU8();
  50. // Color values range from 0 to 63, so multiply by 4
  51. static const uint8_t lightFactor = 4;
  52. r *= lightFactor;
  53. g *= lightFactor;
  54. b *= lightFactor;
  55. glm::vec4 c(r / 255.0f, g / 255.0f, b / 255.0f, 1.0f);
  56. TextureManager::setPalette(i, c);
  57. }
  58. }
  59. void LoaderTR1::loadTextures() {
  60. uint32_t numTextures = file.readU32();
  61. for (unsigned int i = 0; i < numTextures; i++) {
  62. std::array<uint8_t, 256 * 256> arr;
  63. for (auto& x : arr) {
  64. x = file.readU8(); // Palette index
  65. }
  66. TextureManager::addIndexedTexture(&arr[0], 256, 256);
  67. }
  68. if (numTextures > 0)
  69. Log::get(LOG_INFO) << "LoaderTR1: Found " << numTextures << " Textures!" << Log::endl;
  70. else
  71. Log::get(LOG_INFO) << "LoaderTR1: No Textures in this level?!" << Log::endl;
  72. }
  73. void LoaderTR1::loadRoomLights() {
  74. int16_t intensity = file.read16();
  75. uint16_t numLights = file.readU16();
  76. for (unsigned int l = 0; l < numLights; l++) {
  77. // Position of light, in world coordinates
  78. int32_t x = file.read32();
  79. int32_t y = file.read32();
  80. int32_t z = file.read32();
  81. uint16_t intensity1 = file.readU16();
  82. uint32_t fade = file.readU32(); // Falloff value?
  83. // TODO store light somewhere
  84. }
  85. }
  86. void LoaderTR1::loadRoomStaticMeshes(std::vector<StaticModel*>& staticModels) {
  87. uint16_t numStaticMeshes = file.readU16();
  88. for (unsigned int s = 0; s < numStaticMeshes; s++) {
  89. // Absolute position in world coordinates
  90. int32_t x = file.read32();
  91. int32_t y = file.read32();
  92. int32_t z = file.read32();
  93. // High two bits (0xC000) indicate steps of
  94. // 90 degrees (eg. (rotation >> 14) * 90)
  95. uint16_t rotation = file.readU16();
  96. // Constant lighting, 0xFFFF means use mesh lighting
  97. //! \todo Use static mesh lighting information
  98. uint16_t intensity1 = file.readU16();
  99. // Which StaticMesh item to draw
  100. uint16_t objectID = file.readU16();
  101. staticModels.push_back(new StaticModel(glm::vec3(x, y, z),
  102. glm::radians((rotation >> 14) * 90.0f),
  103. objectID));
  104. }
  105. }
  106. void LoaderTR1::loadRoomVertex(RoomVertexTR2& vert) {
  107. vert.x = file.read16();
  108. vert.y = file.read16();
  109. vert.z = file.read16();
  110. vert.light1 = file.read16();
  111. vert.attributes = 0;
  112. vert.light2 = vert.light1;
  113. }
  114. void LoaderTR1::loadItems() {
  115. uint32_t numItems = file.readU32();
  116. for (unsigned int i = 0; i < numItems; i++) {
  117. int16_t objectID = file.read16();
  118. int16_t room = file.read16();
  119. // Item position in world coordinates
  120. int32_t x = file.read32();
  121. int32_t y = file.read32();
  122. int32_t z = file.read32();
  123. uint16_t angle = file.readU16(); // (0xC000 >> 14) * 90deg
  124. int16_t intensity = file.read16(); // Constant lighting; -1 means mesh lighting
  125. // 0x0100 - Initially visible
  126. // 0x3E00 - Activation mask, open, can be XORed with related FloorData list fields.
  127. uint16_t flags = file.readU16();
  128. glm::vec3 pos(
  129. static_cast<float>(x),
  130. static_cast<float>(y),
  131. static_cast<float>(z)
  132. );
  133. glm::vec3 rot(
  134. 0.0f,
  135. glm::radians(((angle >> 14) & 0x03) * 90.0f),
  136. 0.0f
  137. );
  138. Entity* e = new Entity(objectID, room, pos, rot);
  139. getWorld().addEntity(e);
  140. if (objectID == 0) {
  141. Game::setLara(getWorld().sizeEntity() - 1);
  142. }
  143. }
  144. if (numItems > 0)
  145. Log::get(LOG_INFO) << "LoaderTR1: Found " << numItems << " Items!" << Log::endl;
  146. else
  147. Log::get(LOG_INFO) << "LoaderTR1: No Items in this level?!" << Log::endl;
  148. }
  149. void LoaderTR1::loadBoxesOverlapsZones() {
  150. uint32_t numBoxes = file.readU32();
  151. for (unsigned int b = 0; b < numBoxes; b++) {
  152. // Sectors (not scaled!)
  153. int32_t zMin = file.read32();
  154. int32_t zMax = file.read32();
  155. int32_t xMin = file.read32();
  156. int32_t xMax = file.read32();
  157. int16_t trueFloor = file.read16(); // Y value (no scaling)
  158. // Index into overlaps[]. The high bit is sometimes set
  159. // this occurs in front of swinging doors and the like
  160. uint16_t overlapIndex = file.readU16();
  161. // TODO store boxes somewhere
  162. }
  163. uint32_t numOverlaps = file.readU32();
  164. std::vector<std::vector<uint16_t>> overlaps;
  165. overlaps.emplace_back();
  166. unsigned int list = 0;
  167. for (unsigned int o = 0; o < numOverlaps; o++) {
  168. // Apparently used by NPCs to decide where to go next.
  169. // List of neighboring boxes for each box.
  170. // Each entry is a uint16, 0x8000 set marks end of list.
  171. uint16_t e = file.readU16();
  172. overlaps.at(list).push_back(e);
  173. if (e & 0x8000) {
  174. overlaps.emplace_back();
  175. list++;
  176. }
  177. }
  178. // TODO store overlaps somewhere
  179. for (unsigned int z = 0; z < numBoxes; z++) {
  180. // Normal room state
  181. int16_t ground1 = file.read16();
  182. int16_t ground2 = file.read16();
  183. int16_t fly = file.read16();
  184. // Alternate room state
  185. int16_t ground1alt = file.read16();
  186. int16_t ground2alt = file.read16();
  187. int16_t flyAlt = file.read16();
  188. // TODO store zones somewhere
  189. }
  190. if ((numBoxes > 0) || (numOverlaps > 0))
  191. Log::get(LOG_INFO) << "LoaderTR1: Found NPC NavigationHints (" << numBoxes
  192. << ", " << numOverlaps << ", " << list << "), unimplemented!" << Log::endl;
  193. else
  194. Log::get(LOG_INFO) << "LoaderTR1: No NPC NavigationHints in this level?!" << Log::endl;
  195. }
  196. void LoaderTR1::loadSoundMap() {
  197. for (int i = 0; i < 256; i++) {
  198. SoundManager::addSoundMapEntry(file.read16());
  199. }
  200. }
  201. void LoaderTR1::loadSoundSamples() {
  202. uint32_t soundSampleSize = file.readU32();
  203. std::vector<uint8_t> buffer;
  204. for (int i = 0; i < soundSampleSize; i++) {
  205. buffer.push_back(file.readU8());
  206. }
  207. uint32_t numSampleIndices = file.readU32();
  208. for (unsigned int i = 0; i < numSampleIndices; i++) {
  209. SoundManager::addSampleIndex(i);
  210. uint32_t sampleOffset = file.readU32();
  211. assertLessThan(sampleOffset, soundSampleSize);
  212. char* tmpPtr = reinterpret_cast<char*>(&buffer[sampleOffset]);
  213. BinaryMemory sample(tmpPtr, soundSampleSize - sampleOffset);
  214. int ret = loadSoundFiles(sample, 1);
  215. assertEqual(ret, 1);
  216. }
  217. if (numSampleIndices > 0)
  218. Log::get(LOG_INFO) << "LoaderTR1: Found " << numSampleIndices << " SoundSamples" << Log::endl;
  219. else
  220. Log::get(LOG_INFO) << "LoaderTR1: No SoundSamples in this level?!" << Log::endl;
  221. }
  222. int LoaderTR1::getPaletteIndex(uint16_t index) {
  223. return index;
  224. }