1 /*
2 * Copyright 2002 - Florian Schulze <crow@icculus.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2, or (at your option)
7 * any later version.
8 *
9 * This program 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 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; see the file COPYING. If not, write to
16 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
17 *
18 * This file is part of vt.
19 *
20 */
21
22 #include <SDL2/SDL.h>
23 #include <SDL2/SDL_endian.h>
24 #include "l_main.h"
25 #include "../core/system.h"
26
27 #define RCSID "$Id: l_tr2.cpp,v 1.15 2002/09/20 15:59:02 crow Exp $"
28
read_tr2_colour4(SDL_RWops * const src,tr2_colour_t & colour)29 void TR_Level::read_tr2_colour4(SDL_RWops * const src, tr2_colour_t & colour)
30 {
31 // read 6 bit color and change to 8 bit
32 colour.r = read_bitu8(src) << 2;
33 colour.g = read_bitu8(src) << 2;
34 colour.b = read_bitu8(src) << 2;
35 colour.a = read_bitu8(src) << 2;
36 }
37
read_tr2_palette16(SDL_RWops * const src,tr2_palette_t & palette)38 void TR_Level::read_tr2_palette16(SDL_RWops * const src, tr2_palette_t & palette)
39 {
40 for (int i = 0; i < 256; i++)
41 read_tr2_colour4(src, palette.colour[i]);
42 }
43
read_tr2_textile16(SDL_RWops * const src,tr2_textile16_t & textile)44 void TR_Level::read_tr2_textile16(SDL_RWops * const src, tr2_textile16_t & textile)
45 {
46 for (int i = 0; i < 256; i++) {
47 if (SDL_RWread(src, textile.pixels[i], 2, 256) < 256)
48 Sys_extError("read_tr2_textile16");
49
50 for (int j = 0; j < 256; j++)
51 textile.pixels[i][j] = SDL_SwapLE16(textile.pixels[i][j]);
52 }
53 }
54
read_tr2_box(SDL_RWops * const src,tr_box_t & box)55 void TR_Level::read_tr2_box(SDL_RWops * const src, tr_box_t & box)
56 {
57 box.zmax =-1024 * read_bitu8(src);
58 box.zmin =-1024 * read_bitu8(src);
59 box.xmin = 1024 * read_bitu8(src);
60 box.xmax = 1024 * read_bitu8(src);
61 box.true_floor =-read_bit16(src);
62 box.overlap_index = read_bitu16(src);
63 }
64
read_tr2_zone(SDL_RWops * const src,tr2_zone_t & zone)65 void TR_Level::read_tr2_zone(SDL_RWops * const src, tr2_zone_t & zone)
66 {
67 zone.GroundZone1_Normal = read_bit16(src);
68 zone.GroundZone2_Normal = read_bit16(src);
69 zone.GroundZone3_Normal = read_bit16(src);
70 zone.GroundZone4_Normal = read_bit16(src);
71 zone.FlyZone_Normal = read_bit16(src);
72 zone.GroundZone1_Alternate = read_bit16(src);
73 zone.GroundZone2_Alternate = read_bit16(src);
74 zone.GroundZone3_Alternate = read_bit16(src);
75 zone.GroundZone4_Alternate = read_bit16(src);
76 zone.FlyZone_Alternate = read_bit16(src);
77 }
78
read_tr2_room_light(SDL_RWops * const src,tr5_room_light_t & light)79 void TR_Level::read_tr2_room_light(SDL_RWops * const src, tr5_room_light_t & light)
80 {
81 read_tr_vertex32(src, light.pos);
82 light.intensity1 = read_bitu16(src);
83 light.intensity2 = read_bitu16(src);
84 light.fade1 = read_bitu32(src);
85 light.fade2 = read_bitu32(src);
86
87 light.intensity = light.intensity1;
88 light.intensity /= 4096.0f;
89
90 if(light.intensity > 1.0f)
91 light.intensity = 1.0f;
92
93 light.r_outer = light.fade1;
94 light.r_inner = light.fade1 / 2;
95
96 light.light_type = 0x01; // Point light
97
98 // all white
99 light.color.r = 0xff;
100 light.color.g = 0xff;
101 light.color.b = 0xff;
102 }
103
read_tr2_room_vertex(SDL_RWops * const src,tr5_room_vertex_t & room_vertex)104 void TR_Level::read_tr2_room_vertex(SDL_RWops * const src, tr5_room_vertex_t & room_vertex)
105 {
106 read_tr_vertex16(src, room_vertex.vertex);
107 // read and make consistent
108 room_vertex.lighting1 = (8191 - read_bit16(src)) << 2;
109 room_vertex.attributes = read_bitu16(src);
110 room_vertex.lighting2 = (8191 - read_bit16(src)) << 2;
111 // only in TR5
112 room_vertex.normal.x = 0;
113 room_vertex.normal.y = 0;
114 room_vertex.normal.z = 0;
115 room_vertex.colour.r = room_vertex.lighting2 / 32768.0f;
116 room_vertex.colour.g = room_vertex.lighting2 / 32768.0f;
117 room_vertex.colour.b = room_vertex.lighting2 / 32768.0f;
118 room_vertex.colour.a = 1.0f;
119 }
120
read_tr2_room_staticmesh(SDL_RWops * const src,tr2_room_staticmesh_t & room_static_mesh)121 void TR_Level::read_tr2_room_staticmesh(SDL_RWops * const src, tr2_room_staticmesh_t & room_static_mesh)
122 {
123 read_tr_vertex32(src, room_static_mesh.pos);
124 room_static_mesh.rotation = (float)read_bitu16(src) / 16384.0f * -90;
125 room_static_mesh.intensity1 = read_bit16(src);
126 room_static_mesh.intensity2 = read_bit16(src);
127 room_static_mesh.object_id = read_bitu16(src);
128 // make consistent
129 if (room_static_mesh.intensity1 >= 0)
130 room_static_mesh.intensity1 = (8191 - room_static_mesh.intensity1) << 2;
131 if (room_static_mesh.intensity2 >= 0)
132 room_static_mesh.intensity2 = (8191 - room_static_mesh.intensity2) << 2;
133
134 room_static_mesh.tint.b = room_static_mesh.tint.g = room_static_mesh.tint.r = (room_static_mesh.intensity2 / 16384.0f);
135 room_static_mesh.tint.a = 1.0f;
136 }
137
read_tr2_room(SDL_RWops * const src,tr5_room_t & room)138 void TR_Level::read_tr2_room(SDL_RWops * const src, tr5_room_t & room)
139 {
140 uint32_t num_data_words;
141 uint32_t i;
142 int64_t pos;
143
144 // read and change coordinate system
145 room.offset.x = (float)read_bit32(src);
146 room.offset.y = 0;
147 room.offset.z = (float)-read_bit32(src);
148 room.y_bottom = (float)-read_bit32(src);
149 room.y_top = (float)-read_bit32(src);
150
151 num_data_words = read_bitu32(src);
152
153 pos = SDL_RWseek(src, 0, RW_SEEK_CUR);
154
155 room.num_layers = 0;
156
157 room.num_vertices = read_bitu16(src);
158 room.vertices = (tr5_room_vertex_t*)calloc(room.num_vertices, sizeof(tr5_room_vertex_t));
159 for (i = 0; i < room.num_vertices; i++)
160 read_tr2_room_vertex(src, room.vertices[i]);
161
162 room.num_rectangles = read_bitu16(src);
163 room.rectangles = (tr4_face4_t*)malloc(room.num_rectangles * sizeof(tr4_face4_t));
164 for (i = 0; i < room.num_rectangles; i++)
165 read_tr_face4(src, room.rectangles[i]);
166
167 room.num_triangles = read_bitu16(src);
168 room.triangles = (tr4_face3_t*)malloc(room.num_triangles * sizeof(tr4_face3_t));
169 for (i = 0; i < room.num_triangles; i++)
170 read_tr_face3(src, room.triangles[i]);
171
172 room.num_sprites = read_bitu16(src);
173 room.sprites = (tr_room_sprite_t*)malloc(room.num_sprites * sizeof(tr_room_sprite_t));
174 for (i = 0; i < room.num_sprites; i++)
175 read_tr_room_sprite(src, room.sprites[i]);
176
177 // set to the right position in case that there is some unused data
178 SDL_RWseek(src, pos + (num_data_words * 2), RW_SEEK_SET);
179
180 room.num_portals = read_bitu16(src);
181 room.portals = (tr_room_portal_t*)malloc(room.num_portals * sizeof(tr_room_portal_t));
182 for (i = 0; i < room.num_portals; i++)
183 read_tr_room_portal(src, room.portals[i]);
184
185 room.num_zsectors = read_bitu16(src);
186 room.num_xsectors = read_bitu16(src);
187 room.sector_list = (tr_room_sector_t*)malloc(room.num_zsectors * room.num_xsectors * sizeof(tr_room_sector_t));
188 for (i = 0; i < (uint32_t)(room.num_zsectors * room.num_xsectors); i++)
189 read_tr_room_sector(src, room.sector_list[i]);
190
191 // read and make consistent
192 room.intensity1 = (8191 - read_bit16(src)) << 2;
193 room.intensity2 = (8191 - read_bit16(src)) << 2;
194 room.light_mode = read_bit16(src);
195
196 room.num_lights = read_bitu16(src);
197 room.lights = (tr5_room_light_t*)malloc(room.num_lights * sizeof(tr5_room_light_t));
198 for (i = 0; i < room.num_lights; i++)
199 read_tr2_room_light(src, room.lights[i]);
200
201 room.num_static_meshes = read_bitu16(src);
202 room.static_meshes = (tr2_room_staticmesh_t*)malloc(room.num_static_meshes * sizeof(tr2_room_staticmesh_t));
203 for (i = 0; i < room.num_static_meshes; i++)
204 read_tr2_room_staticmesh(src, room.static_meshes[i]);
205
206 room.alternate_room = read_bit16(src);
207 room.alternate_group = 0; // Doesn't exist in TR1-3
208
209 room.flags = read_bitu16(src);
210
211 if(room.flags & 0x0020)
212 {
213 room.reverb_info = 0;
214 }
215 else
216 {
217 room.reverb_info = 2;
218 }
219
220 room.light_colour.r = room.intensity1 / 16384.0f;
221 room.light_colour.g = room.intensity1 / 16384.0f;
222 room.light_colour.b = room.intensity1 / 16384.0f;
223 room.light_colour.a = 1.0f;
224 }
225
read_tr2_item(SDL_RWops * const src,tr2_item_t & item)226 void TR_Level::read_tr2_item(SDL_RWops * const src, tr2_item_t & item)
227 {
228 item.object_id = read_bit16(src);
229 item.room = read_bit16(src);
230 read_tr_vertex32(src, item.pos);
231 item.rotation = (float)read_bitu16(src) / 16384.0f * -90;
232 item.intensity1 = read_bitu16(src);
233 if (item.intensity1 >= 0)
234 item.intensity1 = (8191 - item.intensity1) << 2;
235 item.intensity2 = read_bitu16(src);
236 if (item.intensity2 >= 0)
237 item.intensity2 = (8191 - item.intensity2) << 2;
238 item.ocb = 0; // Not present in TR2!
239 item.flags = read_bitu16(src);
240 }
241
read_tr2_level(SDL_RWops * const src,bool demo)242 void TR_Level::read_tr2_level(SDL_RWops * const src, bool demo)
243 {
244 uint32_t i;
245
246 // Version
247 uint32_t file_version = read_bitu32(src);
248
249 if (file_version != 0x0000002d)
250 Sys_extError("Wrong level version");
251
252 read_tr_palette(src, this->palette);
253 read_tr2_palette16(src, this->palette16);
254
255 this->num_textiles = 0;
256 this->num_room_textiles = 0;
257 this->num_obj_textiles = 0;
258 this->num_bump_textiles = 0;
259 this->num_misc_textiles = 0;
260 this->read_32bit_textiles = false;
261
262 this->textile8_count = this->num_textiles = read_bitu32(src);
263 this->textile8 = (tr_textile8_t*)malloc(this->textile8_count * sizeof(tr_textile8_t));
264 for (i = 0; i < this->textile8_count; i++)
265 read_tr_textile8(src, this->textile8[i]);
266 this->textile16_count = this->textile8_count;
267 this->textile16 = (tr2_textile16_t*)malloc(this->textile16_count * sizeof(tr2_textile16_t));
268 for (i = 0; i < this->textile16_count; i++)
269 read_tr2_textile16(src, this->textile16[i]);
270
271 // Unused
272 if (read_bitu32(src) != 0)
273 Sys_extWarn("Bad value for 'unused'");
274
275 this->rooms_count = read_bitu16(src);
276 this->rooms = (tr5_room_t*)calloc(this->rooms_count, sizeof(tr5_room_t));
277 for (i = 0; i < this->rooms_count; i++)
278 read_tr2_room(src, this->rooms[i]);
279
280 this->floor_data_size = read_bitu32(src);
281 this->floor_data = (uint16_t*)malloc(this->floor_data_size * sizeof(uint16_t));
282 for(i = 0; i < this->floor_data_size; i++)
283 this->floor_data[i] = read_bitu16(src);
284
285 read_mesh_data(src);
286
287 this->animations_count = read_bitu32(src);
288 this->animations = (tr_animation_t*)malloc(this->animations_count * sizeof(tr_animation_t));
289 for (i = 0; i < this->animations_count; i++)
290 read_tr_animation(src, this->animations[i]);
291
292 this->state_changes_count = read_bitu32(src);
293 this->state_changes = (tr_state_change_t*)malloc(this->state_changes_count * sizeof(tr_state_change_t));
294 for (i = 0; i < this->state_changes_count; i++)
295 read_tr_state_changes(src, this->state_changes[i]);
296
297 this->anim_dispatches_count = read_bitu32(src);
298 this->anim_dispatches = (tr_anim_dispatch_t*)malloc(this->anim_dispatches_count * sizeof(tr_anim_dispatch_t));
299 for (i = 0; i < this->anim_dispatches_count; i++)
300 read_tr_anim_dispatches(src, this->anim_dispatches[i]);
301
302 this->anim_commands_count = read_bitu32(src);
303 this->anim_commands = (int16_t*)malloc(this->anim_commands_count * sizeof(int16_t));
304 for (i = 0; i < this->anim_commands_count; i++)
305 this->anim_commands[i] = read_bit16(src);
306
307 this->mesh_tree_data_size = read_bitu32(src);
308 this->mesh_tree_data = (uint32_t*)malloc(this->mesh_tree_data_size * sizeof(uint32_t));
309 for (i = 0; i < this->mesh_tree_data_size; i++)
310 this->mesh_tree_data[i] = read_bitu32(src); // 4 bytes
311
312 read_frame_moveable_data(src);
313
314 this->static_meshes_count = read_bitu32(src);
315 this->static_meshes = (tr_staticmesh_t*)malloc(this->static_meshes_count * sizeof(tr_staticmesh_t));
316 for (i = 0; i < this->static_meshes_count; i++)
317 read_tr_staticmesh(src, this->static_meshes[i]);
318
319 this->object_textures_count = read_bitu32(src);
320 this->object_textures = (tr4_object_texture_t*)malloc(this->object_textures_count * sizeof(tr4_object_texture_t));
321 for (i = 0; i < this->object_textures_count; i++)
322 read_tr_object_texture(src, this->object_textures[i]);
323
324 this->sprite_textures_count = read_bitu32(src);
325 this->sprite_textures = (tr_sprite_texture_t*)malloc(this->sprite_textures_count * sizeof(tr_sprite_texture_t));
326 for (i = 0; i < this->sprite_textures_count; i++)
327 read_tr_sprite_texture(src, this->sprite_textures[i]);
328
329 this->sprite_sequences_count = read_bitu32(src);
330 this->sprite_sequences = (tr_sprite_sequence_t*)malloc(this->sprite_sequences_count * sizeof(tr_sprite_sequence_t));
331 for (i = 0; i < this->sprite_sequences_count; i++)
332 read_tr_sprite_sequence(src, this->sprite_sequences[i]);
333
334 if (demo)
335 read_tr_lightmap(src, this->lightmap);
336
337 this->cameras_count = read_bitu32(src);
338 this->cameras = (tr_camera_t*)malloc(this->cameras_count * sizeof(tr_camera_t));
339 for (i = 0; i < this->cameras_count; i++)
340 {
341 this->cameras[i].x = read_bit32(src);
342 this->cameras[i].y = read_bit32(src);
343 this->cameras[i].z = read_bit32(src);
344
345 this->cameras[i].room = read_bit16(src);
346 this->cameras[i].unknown1 = read_bitu16(src);
347 }
348
349 this->sound_sources_count = read_bitu32(src);
350 this->sound_sources = (tr_sound_source_t*)malloc(this->sound_sources_count * sizeof(tr_sound_source_t));
351 for(i = 0; i < this->sound_sources_count; i++)
352 {
353 this->sound_sources[i].x = read_bit32(src);
354 this->sound_sources[i].y = read_bit32(src);
355 this->sound_sources[i].z = read_bit32(src);
356
357 this->sound_sources[i].sound_id = read_bitu16(src);
358 this->sound_sources[i].flags = read_bitu16(src);
359 }
360
361 this->boxes_count = read_bitu32(src);
362 this->boxes = (tr_box_t*)malloc(this->boxes_count * sizeof(tr_box_t));
363 this->zones = (tr2_zone_t*)malloc(this->boxes_count * sizeof(tr2_zone_t));
364 for (i = 0; i < this->boxes_count; i++)
365 read_tr2_box(src, this->boxes[i]);
366
367 this->overlaps_count = read_bitu32(src);
368 this->overlaps = (uint16_t*)malloc(this->overlaps_count * sizeof(uint16_t));
369 for (i = 0; i < this->overlaps_count; i++)
370 this->overlaps[i] = read_bitu16(src);
371
372 // Zones
373 for (i = 0; i < this->boxes_count; i++)
374 read_tr2_zone(src, this->zones[i]);
375
376 this->animated_textures_count = read_bitu32(src);
377 this->animated_textures_uv_count = 0; // No UVRotate in TR2
378 this->animated_textures = (uint16_t*)malloc(this->animated_textures_count * sizeof(uint16_t));
379 for (i = 0; i < this->animated_textures_count; i++)
380 {
381 this->animated_textures[i] = read_bitu16(src);
382 }
383
384 this->items_count = read_bitu32(src);
385 this->items = (tr2_item_t*)malloc(this->items_count * sizeof(tr2_item_t));
386 for (i = 0; i < this->items_count; i++)
387 read_tr2_item(src, this->items[i]);
388
389 if (!demo)
390 read_tr_lightmap(src, this->lightmap);
391
392 this->cinematic_frames_count = read_bitu16(src);
393 this->cinematic_frames = (tr_cinematic_frame_t*)malloc(this->cinematic_frames_count * sizeof(tr_cinematic_frame_t));
394 for (i = 0; i < this->cinematic_frames_count; i++)
395 {
396 read_tr_cinematic_frame(src, this->cinematic_frames[i]);
397 }
398
399 this->demo_data_count = read_bitu16(src);
400 this->demo_data = (uint8_t*)malloc(this->demo_data_count * sizeof(uint8_t));
401 for(i=0; i < this->demo_data_count; i++)
402 this->demo_data[i] = read_bitu8(src);
403
404 // Soundmap
405 this->soundmap = (int16_t*)malloc(TR_AUDIO_MAP_SIZE_TR2 * sizeof(int16_t));
406 for(i=0; i < TR_AUDIO_MAP_SIZE_TR2; i++)
407 this->soundmap[i] = read_bit16(src);
408
409 this->sound_details_count = read_bitu32(src);
410 this->sound_details = (tr_sound_details_t*)malloc(this->sound_details_count * sizeof(tr_sound_details_t));
411
412 for(i = 0; i < this->sound_details_count; i++)
413 {
414 this->sound_details[i].sample = read_bitu16(src);
415 this->sound_details[i].volume = read_bitu16(src);
416 this->sound_details[i].chance = read_bitu16(src);
417 this->sound_details[i].num_samples_and_flags_1 = read_bitu8(src);
418 this->sound_details[i].flags_2 = read_bitu8(src);
419 this->sound_details[i].sound_range = TR_AUDIO_DEFAULT_RANGE;
420 this->sound_details[i].pitch = (int16_t)TR_AUDIO_DEFAULT_PITCH;
421 }
422
423 this->sample_indices_count = read_bitu32(src);
424 this->sample_indices = (uint32_t*)malloc(this->sample_indices_count * sizeof(uint32_t));
425 for(i=0; i < this->sample_indices_count; i++)
426 this->sample_indices[i] = read_bitu32(src);
427
428 // remap all sample indices here
429 for(i = 0; i < this->sound_details_count; i++)
430 {
431 if(this->sound_details[i].sample < this->sample_indices_count)
432 {
433 this->sound_details[i].sample = this->sample_indices[this->sound_details[i].sample];
434 }
435 }
436
437 // LOAD SAMPLES
438
439 // In TR2, samples are stored in separate file called MAIN.SFX.
440 // If there is no such files, no samples are loaded.
441
442 SDL_RWops *newsrc = SDL_RWFromFile(this->sfx_path, "rb");
443 if (newsrc == NULL)
444 {
445 Sys_extWarn("read_tr2_level: failed to open \"%s\"! No samples loaded.", this->sfx_path);
446 }
447 else
448 {
449 this->samples_data_size = SDL_RWsize(newsrc);
450 this->samples_count = 0;
451 this->samples_data = (uint8_t*)malloc(this->samples_data_size * sizeof(uint8_t));
452 for(i = 0; i < this->samples_data_size; i++)
453 {
454 this->samples_data[i] = read_bitu8(newsrc);
455 if((i >= 4) && (*((uint32_t*)(this->samples_data+i-4)) == 0x46464952)) /// RIFF
456 {
457 this->samples_count++;
458 }
459 }
460
461 SDL_RWclose(newsrc);
462 newsrc = NULL;
463 }
464 }
465