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 "l_main.h"
24 #include "../core/system.h"
25 
26 #define RCSID "$Id: l_tr3.cpp,v 1.15 2002/09/20 15:59:02 crow Exp $"
27 
read_tr3_room_light(SDL_RWops * const src,tr5_room_light_t & light)28 void TR_Level::read_tr3_room_light(SDL_RWops * const src, tr5_room_light_t & light)
29 {
30     read_tr_vertex32(src, light.pos);
31     light.color.r = read_bitu8(src);
32     light.color.g = read_bitu8(src);
33     light.color.b = read_bitu8(src);
34     light.color.a = read_bitu8(src);
35     light.intensity1 = read_bitu16(src);
36     light.intensity2 = read_bitu16(src);
37     light.fade1 = read_bitu16(src);
38     light.fade2 = read_bitu16(src);
39 
40     light.intensity = light.intensity1;
41     light.intensity /= 4096.0f;
42 
43     if(light.intensity > 1.0f)
44         light.intensity = 1.0f;
45 
46     light.r_outer = light.fade1;
47     light.r_inner = light.fade1 / 2;
48 
49     light.light_type = 0x01; // Point light
50 }
51 
read_tr3_room_vertex(SDL_RWops * const src,tr5_room_vertex_t & room_vertex)52 void TR_Level::read_tr3_room_vertex(SDL_RWops *const src, tr5_room_vertex_t & room_vertex)
53 {
54     read_tr_vertex16(src, room_vertex.vertex);
55     // read and make consistent
56     room_vertex.lighting1 = read_bit16(src);
57     room_vertex.attributes = read_bitu16(src);
58     room_vertex.lighting2 = read_bit16(src);
59     // only in TR5
60     room_vertex.normal.x = 0;
61     room_vertex.normal.y = 0;
62     room_vertex.normal.z = 0;
63 
64     room_vertex.colour.r = ((room_vertex.lighting2 & 0x7C00) >> 10  ) / 62.0f;
65     room_vertex.colour.g = ((room_vertex.lighting2 & 0x03E0) >> 5   ) / 62.0f;
66     room_vertex.colour.b = ((room_vertex.lighting2 & 0x001F)        ) / 62.0f;
67     room_vertex.colour.a = 1.0f;
68 }
69 
read_tr3_room_staticmesh(SDL_RWops * const src,tr2_room_staticmesh_t & room_static_mesh)70 void TR_Level::read_tr3_room_staticmesh(SDL_RWops *const src, tr2_room_staticmesh_t & room_static_mesh)
71 {
72     read_tr_vertex32(src, room_static_mesh.pos);
73     room_static_mesh.rotation = (float)read_bitu16(src) / 16384.0f * -90;
74     room_static_mesh.intensity1 = read_bit16(src);
75     room_static_mesh.intensity2 = read_bit16(src);
76     room_static_mesh.object_id = read_bitu16(src);
77 
78     room_static_mesh.tint.r = ((room_static_mesh.intensity1 & 0x001F)        ) / 62.0f;
79 
80     room_static_mesh.tint.g = ((room_static_mesh.intensity1 & 0x03E0) >> 5   ) / 62.0f;
81 
82     room_static_mesh.tint.b = ((room_static_mesh.intensity1 & 0x7C00) >> 10  ) / 62.0f;
83     room_static_mesh.tint.a = 1.0f;
84 }
85 
read_tr3_room(SDL_RWops * const src,tr5_room_t & room)86 void TR_Level::read_tr3_room(SDL_RWops * const src, tr5_room_t & room)
87 {
88     uint32_t num_data_words;
89     uint32_t i;
90     int64_t pos;
91 
92     // read and change coordinate system
93     room.offset.x = (float)read_bit32(src);
94     room.offset.y = 0;
95     room.offset.z = (float)-read_bit32(src);
96     room.y_bottom = (float)-read_bit32(src);
97     room.y_top = (float)-read_bit32(src);
98 
99     num_data_words = read_bitu32(src);
100 
101     pos = SDL_RWseek(src, 0, RW_SEEK_CUR);
102 
103     room.num_layers = 0;
104 
105     room.num_vertices = read_bitu16(src);
106     room.vertices = (tr5_room_vertex_t*)calloc(room.num_vertices, sizeof(tr5_room_vertex_t));
107     for (i = 0; i < room.num_vertices; i++)
108         read_tr3_room_vertex(src, room.vertices[i]);
109 
110     room.num_rectangles = read_bitu16(src);
111     room.rectangles = (tr4_face4_t*)malloc(room.num_rectangles * sizeof(tr4_face4_t));
112     for (i = 0; i < room.num_rectangles; i++)
113         read_tr_face4(src, room.rectangles[i]);
114 
115     room.num_triangles = read_bitu16(src);
116     room.triangles = (tr4_face3_t*)malloc(room.num_triangles * sizeof(tr4_face3_t));
117     for (i = 0; i < room.num_triangles; i++)
118         read_tr_face3(src, room.triangles[i]);
119 
120     room.num_sprites = read_bitu16(src);
121     room.sprites = (tr_room_sprite_t*)malloc(room.num_sprites * sizeof(tr_room_sprite_t));
122     for (i = 0; i < room.num_sprites; i++)
123         read_tr_room_sprite(src, room.sprites[i]);
124 
125     // set to the right position in case that there is some unused data
126     SDL_RWseek(src, pos + (num_data_words * 2), RW_SEEK_SET);
127 
128     room.num_portals = read_bitu16(src);
129     room.portals = (tr_room_portal_t*)malloc(room.num_portals * sizeof(tr_room_portal_t));
130     for (i = 0; i < room.num_portals; i++)
131         read_tr_room_portal(src, room.portals[i]);
132 
133     room.num_zsectors = read_bitu16(src);
134     room.num_xsectors = read_bitu16(src);
135     room.sector_list = (tr_room_sector_t*)malloc(room.num_zsectors * room.num_xsectors * sizeof(tr_room_sector_t));
136     for (i = 0; i < (uint32_t)(room.num_zsectors * room.num_xsectors); i++)
137         read_tr_room_sector(src, room.sector_list[i]);
138 
139     room.intensity1 = read_bit16(src);
140     room.intensity2 = read_bit16(src);
141 
142     // only in TR2
143     room.light_mode = 0;
144 
145     room.num_lights = read_bitu16(src);
146     room.lights = (tr5_room_light_t*)malloc(room.num_lights * sizeof(tr5_room_light_t));
147     for (i = 0; i < room.num_lights; i++)
148         read_tr3_room_light(src, room.lights[i]);
149 
150     room.num_static_meshes = read_bitu16(src);
151     room.static_meshes = (tr2_room_staticmesh_t*)malloc(room.num_static_meshes * sizeof(tr2_room_staticmesh_t));
152     for (i = 0; i < room.num_static_meshes; i++)
153         read_tr3_room_staticmesh(src, room.static_meshes[i]);
154 
155     room.alternate_room  = read_bit16(src);
156     room.alternate_group = 0;   // Doesn't exist in TR1-3
157 
158     room.flags = read_bitu16(src);
159 
160     if(room.flags & 0x0080)
161     {
162         room.flags |= 0x0002;   // Move quicksand flag to another bit to avoid confusion with NL flag.
163         room.flags ^= 0x0080;
164     }
165 
166     // Only in TR3-5
167 
168     room.water_scheme = read_bitu8(src);
169     room.reverb_info = read_bitu8(src);
170 
171     SDL_RWseek(src, 1, SEEK_CUR);   // Alternate_group override?
172 
173     room.light_colour.r = room.intensity1 / 65534.0f;
174     room.light_colour.g = room.intensity1 / 65534.0f;
175     room.light_colour.b = room.intensity1 / 65534.0f;
176     room.light_colour.a = 1.0f;
177 }
178 
read_tr3_item(SDL_RWops * const src,tr2_item_t & item)179 void TR_Level::read_tr3_item(SDL_RWops *const src, tr2_item_t & item)
180 {
181     item.object_id = read_bit16(src);
182     item.room = read_bit16(src);
183     read_tr_vertex32(src, item.pos);
184     item.rotation = (float)read_bitu16(src) / 16384.0f * -90;
185     item.intensity1 = read_bitu16(src);
186     item.intensity2 = read_bitu16(src);
187     item.ocb = 0;   // Not present in TR3!
188     item.flags = read_bitu16(src);
189 }
190 
read_tr3_level(SDL_RWops * const src)191 void TR_Level::read_tr3_level(SDL_RWops *const src)
192 {
193     uint32_t i;
194 
195     // Version
196     uint32_t file_version = read_bitu32(src);
197 
198     if ((file_version != 0xFF080038) && (file_version != 0xFF180038) && (file_version != 0xFF180034) )
199         Sys_extError("Wrong level version");
200 
201     read_tr_palette(src, this->palette);
202     read_tr2_palette16(src, this->palette16);
203 
204     this->num_textiles = 0;
205     this->num_room_textiles = 0;
206     this->num_obj_textiles = 0;
207     this->num_bump_textiles = 0;
208     this->num_misc_textiles = 0;
209     this->read_32bit_textiles = false;
210 
211     this->textile8_count = this->num_textiles = read_bitu32(src);
212     this->textile8 = (tr_textile8_t*)malloc(this->textile8_count * sizeof(tr_textile8_t));
213     for (i = 0; i < this->textile8_count; i++)
214         read_tr_textile8(src, this->textile8[i]);
215     this->textile16_count = this->textile8_count;
216         this->textile16 = (tr2_textile16_t*)malloc(this->textile16_count * sizeof(tr2_textile16_t));
217     for (i = 0; i < this->textile16_count; i++)
218         read_tr2_textile16(src, this->textile16[i]);
219 
220     if(file_version == 0xFF180034)                                          // VICT.TR2
221     {
222         return;                                                             // Here only palette and textiles
223     }
224 
225     // Unused
226     if (read_bitu32(src) != 0)
227         Sys_extWarn("Bad value for 'unused'");
228 
229     this->rooms_count = read_bitu16(src);
230     this->rooms = (tr5_room_t*)calloc(this->rooms_count, sizeof(tr5_room_t));
231     for (i = 0; i < this->rooms_count; i++)
232         read_tr3_room(src, this->rooms[i]);
233 
234     this->floor_data_size = read_bitu32(src);
235     this->floor_data = (uint16_t*)malloc(this->floor_data_size * sizeof(uint16_t));
236     for(i = 0; i < this->floor_data_size; i++)
237             this->floor_data[i] = read_bitu16(src);
238 
239     read_mesh_data(src);
240 
241     this->animations_count = read_bitu32(src);
242     this->animations = (tr_animation_t*)malloc(this->animations_count * sizeof(tr_animation_t));
243     for (i = 0; i < this->animations_count; i++)
244         read_tr_animation(src, this->animations[i]);
245 
246     this->state_changes_count = read_bitu32(src);
247     this->state_changes = (tr_state_change_t*)malloc(this->state_changes_count * sizeof(tr_state_change_t));
248     for (i = 0; i < this->state_changes_count; i++)
249             read_tr_state_changes(src, this->state_changes[i]);
250 
251     this->anim_dispatches_count = read_bitu32(src);
252     this->anim_dispatches = (tr_anim_dispatch_t*)malloc(this->anim_dispatches_count * sizeof(tr_anim_dispatch_t));
253     for (i = 0; i < this->anim_dispatches_count; i++)
254         read_tr_anim_dispatches(src, this->anim_dispatches[i]);
255 
256     this->anim_commands_count = read_bitu32(src);
257     this->anim_commands = (int16_t*)malloc(this->anim_commands_count * sizeof(int16_t));
258     for (i = 0; i < this->anim_commands_count; i++)
259         this->anim_commands[i] = read_bit16(src);
260 
261     this->mesh_tree_data_size = read_bitu32(src);
262     this->mesh_tree_data = (uint32_t*)malloc(this->mesh_tree_data_size * sizeof(uint32_t));
263     for (i = 0; i < this->mesh_tree_data_size; i++)
264         this->mesh_tree_data[i] = read_bitu32(src);                     // 4 bytes
265 
266     read_frame_moveable_data(src);
267 
268     this->static_meshes_count = read_bitu32(src);
269     this->static_meshes = (tr_staticmesh_t*)malloc(this->static_meshes_count * sizeof(tr_staticmesh_t));
270     for (i = 0; i < this->static_meshes_count; i++)
271         read_tr_staticmesh(src, this->static_meshes[i]);
272 
273     this->sprite_textures_count = read_bitu32(src);
274     this->sprite_textures = (tr_sprite_texture_t*)malloc(this->sprite_textures_count * sizeof(tr_sprite_texture_t));
275     for (i = 0; i < this->sprite_textures_count; i++)
276         read_tr_sprite_texture(src, this->sprite_textures[i]);
277 
278     this->sprite_sequences_count = read_bitu32(src);
279     this->sprite_sequences = (tr_sprite_sequence_t*)malloc(this->sprite_sequences_count * sizeof(tr_sprite_sequence_t));
280     for (i = 0; i < this->sprite_sequences_count; i++)
281         read_tr_sprite_sequence(src, this->sprite_sequences[i]);
282 
283     this->cameras_count = read_bitu32(src);
284     this->cameras = (tr_camera_t*)malloc(this->cameras_count * sizeof(tr_camera_t));
285     for (i = 0; i < this->cameras_count; i++)
286     {
287         this->cameras[i].x = read_bit32(src);
288         this->cameras[i].y = read_bit32(src);
289         this->cameras[i].z = read_bit32(src);
290 
291         this->cameras[i].room     = read_bit16(src);
292         this->cameras[i].unknown1 = read_bitu16(src);
293     }
294 
295     this->sound_sources_count = read_bitu32(src);
296     this->sound_sources = (tr_sound_source_t*)malloc(this->sound_sources_count * sizeof(tr_sound_source_t));
297     for(i = 0; i < this->sound_sources_count; i++)
298     {
299         this->sound_sources[i].x = read_bit32(src);
300         this->sound_sources[i].y = read_bit32(src);
301         this->sound_sources[i].z = read_bit32(src);
302 
303         this->sound_sources[i].sound_id = read_bitu16(src);
304         this->sound_sources[i].flags = read_bitu16(src);
305     }
306 
307     this->boxes_count = read_bitu32(src);
308     this->boxes = (tr_box_t*)malloc(this->boxes_count * sizeof(tr_box_t));
309     this->zones = (tr2_zone_t*)malloc(this->boxes_count * sizeof(tr2_zone_t));
310     for (i = 0; i < this->boxes_count; i++)
311         read_tr2_box(src, this->boxes[i]);
312 
313     this->overlaps_count = read_bitu32(src);
314     this->overlaps = (uint16_t*)malloc(this->overlaps_count * sizeof(uint16_t));
315     for (i = 0; i < this->overlaps_count; i++)
316         this->overlaps[i] = read_bitu16(src);
317 
318     // Zones
319     for (i = 0; i < this->boxes_count; i++)
320         read_tr2_zone(src, this->zones[i]);
321 
322     this->animated_textures_count = read_bitu32(src);
323     this->animated_textures_uv_count = 0; // No UVRotate in TR3
324     this->animated_textures = (uint16_t*)malloc(this->animated_textures_count * sizeof(uint16_t));
325     for (i = 0; i < this->animated_textures_count; i++)
326     {
327         this->animated_textures[i] = read_bitu16(src);
328     }
329 
330     this->object_textures_count = read_bitu32(src);
331     this->object_textures = (tr4_object_texture_t*)malloc(this->object_textures_count * sizeof(tr4_object_texture_t));
332     for (i = 0; i < this->object_textures_count; i++)
333         read_tr_object_texture(src, this->object_textures[i]);
334 
335     this->items_count = read_bitu32(src);
336     this->items = (tr2_item_t*)malloc(this->items_count * sizeof(tr2_item_t));
337     for (i = 0; i < this->items_count; i++)
338         read_tr3_item(src, this->items[i]);
339 
340     read_tr_lightmap(src, this->lightmap);
341 
342     this->cinematic_frames_count = read_bitu16(src);
343     this->cinematic_frames = (tr_cinematic_frame_t*)malloc(this->cinematic_frames_count * sizeof(tr_cinematic_frame_t));
344     for (i = 0; i < this->cinematic_frames_count; i++)
345     {
346         read_tr_cinematic_frame(src, this->cinematic_frames[i]);
347     }
348 
349     this->demo_data_count = read_bitu16(src);
350     this->demo_data = (uint8_t*)malloc(this->demo_data_count * sizeof(uint8_t));
351     for(i=0; i < this->demo_data_count; i++)
352         this->demo_data[i] = read_bitu8(src);
353 
354     // Soundmap
355     this->soundmap = (int16_t*)malloc(TR_AUDIO_MAP_SIZE_TR3 * sizeof(int16_t));
356     for(i = 0; i < TR_AUDIO_MAP_SIZE_TR3; i++)
357         this->soundmap[i] = read_bit16(src);
358 
359     this->sound_details_count = read_bitu32(src);
360     this->sound_details = (tr_sound_details_t*)malloc(this->sound_details_count * sizeof(tr_sound_details_t));
361     for(i = 0; i < this->sound_details_count; i++)
362     {
363         this->sound_details[i].sample      = read_bitu16(src);
364         this->sound_details[i].volume      = (uint16_t)read_bitu8(src);
365         this->sound_details[i].sound_range = (uint16_t)read_bitu8(src);
366         this->sound_details[i].chance      = (uint16_t)read_bit8(src);
367         this->sound_details[i].pitch       = (int16_t)read_bit8(src);
368         this->sound_details[i].num_samples_and_flags_1 = read_bitu8(src);
369         this->sound_details[i].flags_2 = read_bitu8(src);
370     }
371 
372     this->sample_indices_count = read_bitu32(src);
373     this->sample_indices = (uint32_t*)malloc(this->sample_indices_count * sizeof(uint32_t));
374     for(i=0; i < this->sample_indices_count; i++)
375         this->sample_indices[i] = read_bitu32(src);
376 
377     // remap all sample indices here
378     for(i = 0; i < this->sound_details_count; i++)
379     {
380         if(this->sound_details[i].sample >= 0 && this->sound_details[i].sample < this->sample_indices_count)
381         {
382             this->sound_details[i].sample = this->sample_indices[this->sound_details[i].sample];
383         }
384     }
385 
386     // LOAD SAMPLES
387 
388     // In TR3, samples are stored in separate file called MAIN.SFX.
389     // If there is no such files, no samples are loaded.
390 
391     SDL_RWops *newsrc = SDL_RWFromFile(this->sfx_path, "rb");
392     if (newsrc == NULL)
393     {
394         Sys_extWarn("read_tr2_level: failed to open \"%s\"! No samples loaded.", this->sfx_path);
395     }
396     else
397     {
398         this->samples_data_size = SDL_RWsize(newsrc);
399         this->samples_count = 0;
400         this->samples_data = (uint8_t*)malloc(this->samples_data_size * sizeof(uint8_t));
401         for(i = 0; i < this->samples_data_size; i++)
402         {
403             this->samples_data[i] = read_bitu8(newsrc);
404             if((i >= 4) && (*((uint32_t*)(this->samples_data+i-4)) == 0x46464952))   /// RIFF
405             {
406                 this->samples_count++;
407             }
408         }
409 
410         SDL_RWclose(newsrc);
411         newsrc = NULL;
412     }
413 }
414