]> icculus.org git repositories - btb/d2x.git/blob - main/bm.c
more fast i/o macros
[btb/d2x.git] / main / bm.c
1 /* $Id: bm.c,v 1.14 2002-08-06 04:53:48 btb Exp $ */
2 /*
3 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
4 SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
5 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
6 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
7 IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
8 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
9 FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
10 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
11 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
12 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
13 */
14
15 /*
16  *
17  * Bitmap and palette loading functions.
18  *
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <conf.h>
24 #endif
25
26 #include <stdio.h>
27 #include <stdlib.h>
28
29 #include "pstypes.h"
30 #include "inferno.h"
31 #include "gr.h"
32 #include "bm.h"
33 #include "u_mem.h"
34 #include "mono.h"
35 #include "error.h"
36 #include "object.h"
37 #include "vclip.h"
38 #include "effects.h"
39 #include "polyobj.h"
40 #include "wall.h"
41 #include "textures.h"
42 #include "game.h"
43 #ifdef NETWORK
44 #include "multi.h"
45 #endif
46 #include "iff.h"
47 #include "cfile.h"
48 #include "powerup.h"
49 #include "sounds.h"
50 #include "piggy.h"
51 #include "aistruct.h"
52 #include "robot.h"
53 #include "weapon.h"
54 #include "gauges.h"
55 #include "player.h"
56 #include "endlevel.h"
57 #include "cntrlcen.h"
58 #include "byteswap.h"
59 #include "makesig.h"
60
61 ubyte Sounds[MAX_SOUNDS];
62 ubyte AltSounds[MAX_SOUNDS];
63
64 #ifdef EDITOR
65 int Num_total_object_types;
66 byte    ObjType[MAX_OBJTYPE];
67 byte    ObjId[MAX_OBJTYPE];
68 fix     ObjStrength[MAX_OBJTYPE];
69 #endif
70
71 //for each model, a model number for dying & dead variants, or -1 if none
72 int Dying_modelnums[MAX_POLYGON_MODELS];
73 int Dead_modelnums[MAX_POLYGON_MODELS];
74
75 //the polygon model number to use for the marker
76 int     Marker_model_num = -1;
77
78 //right now there's only one player ship, but we can have another by 
79 //adding an array and setting the pointer to the active ship.
80 player_ship only_player_ship,*Player_ship=&only_player_ship;
81
82 //----------------- Miscellaneous bitmap pointers ---------------
83 int                                     Num_cockpits = 0;
84 bitmap_index            cockpit_bitmap[N_COCKPIT_BITMAPS];
85
86 //---------------- Variables for wall textures ------------------
87 int                                     Num_tmaps;
88 tmap_info                       TmapInfo[MAX_TEXTURES];
89
90 //---------------- Variables for object textures ----------------
91
92 int                                     First_multi_bitmap_num=-1;
93
94 bitmap_index            ObjBitmaps[MAX_OBJ_BITMAPS];
95 ushort                          ObjBitmapPtrs[MAX_OBJ_BITMAPS];         // These point back into ObjBitmaps, since some are used twice.
96
97 #ifdef FAST_FILE_IO
98 #define tmap_info_read_n(ti, n, fp) cfread(ti, sizeof(tmap_info), n, fp)
99 #else
100 /*
101  * reads n tmap_info structs from a CFILE
102  */
103 int tmap_info_read_n(tmap_info *ti, int n, CFILE *fp)
104 {
105         int i;
106
107         for (i = 0; i < n; i++) {
108                 ti[i].flags = cfile_read_byte(fp);
109                 ti[i].pad[0] = cfile_read_byte(fp);
110                 ti[i].pad[1] = cfile_read_byte(fp);
111                 ti[i].pad[2] = cfile_read_byte(fp);
112                 ti[i].lighting = cfile_read_fix(fp);
113                 ti[i].damage = cfile_read_fix(fp);
114                 ti[i].eclip_num = cfile_read_short(fp);
115                 ti[i].destroyed = cfile_read_short(fp);
116                 ti[i].slide_u = cfile_read_short(fp);
117                 ti[i].slide_v = cfile_read_short(fp);
118         }
119         return i;
120 }
121 #endif
122
123 //#ifdef MACINTOSH
124
125 extern int exit_modelnum,destroyed_exit_modelnum, Num_bitmap_files;
126 int N_ObjBitmaps, extra_bitmap_num;
127
128 bitmap_index exitmodel_bm_load_sub( char * filename )
129 {
130         bitmap_index bitmap_num;
131         grs_bitmap * new;
132         ubyte newpal[256*3];
133         int iff_error;          //reference parm to avoid warning message
134
135         bitmap_num.index = 0;
136
137         MALLOC( new, grs_bitmap, 1 );
138         iff_error = iff_read_bitmap(filename,new,BM_LINEAR,newpal);
139         new->bm_handle=0;
140         if (iff_error != IFF_NO_ERROR)          {
141                 Error("Error loading exit model bitmap <%s> - IFF error: %s",filename,iff_errormsg(iff_error));
142         }
143
144         if ( iff_has_transparency )
145                 gr_remap_bitmap_good( new, newpal, iff_transparent_color, 254 );
146         else
147                 gr_remap_bitmap_good( new, newpal, -1, 254 );
148
149         new->avg_color = 0;     //compute_average_pixel(new);
150
151         bitmap_num.index = extra_bitmap_num;
152
153         GameBitmaps[extra_bitmap_num++] = *new;
154
155         d_free( new );
156         return bitmap_num;
157 }
158
159 grs_bitmap *load_exit_model_bitmap(char *name)
160 {
161         Assert(N_ObjBitmaps < MAX_OBJ_BITMAPS);
162
163         {
164                 ObjBitmaps[N_ObjBitmaps] = exitmodel_bm_load_sub(name);
165                 if (GameBitmaps[ObjBitmaps[N_ObjBitmaps].index].bm_w!=64 || GameBitmaps[ObjBitmaps[N_ObjBitmaps].index].bm_h!=64)
166                         Error("Bitmap <%s> is not 64x64",name);
167                 ObjBitmapPtrs[N_ObjBitmaps] = N_ObjBitmaps;
168                 N_ObjBitmaps++;
169                 Assert(N_ObjBitmaps < MAX_OBJ_BITMAPS);
170                 return &GameBitmaps[ObjBitmaps[N_ObjBitmaps-1].index];
171         }
172 }
173
174 void load_exit_models()
175 {
176         CFILE *exit_hamfile;
177         int start_num;
178
179         start_num = N_ObjBitmaps;
180         extra_bitmap_num = Num_bitmap_files;
181         load_exit_model_bitmap("steel1.bbm");
182         load_exit_model_bitmap("rbot061.bbm");
183         load_exit_model_bitmap("rbot062.bbm");
184
185         load_exit_model_bitmap("steel1.bbm");
186         load_exit_model_bitmap("rbot061.bbm");
187         load_exit_model_bitmap("rbot063.bbm");
188
189         exit_hamfile = cfopen(":Data:exit.ham","rb");
190
191         exit_modelnum = N_polygon_models++;
192         destroyed_exit_modelnum = N_polygon_models++;
193
194         polymodel_read(&Polygon_models[exit_modelnum], exit_hamfile);
195         polymodel_read(&Polygon_models[destroyed_exit_modelnum], exit_hamfile);
196 #ifdef MACINTOSH //not sure what these are for
197         Polygon_models[exit_modelnum].first_texture = start_num;
198         Polygon_models[destroyed_exit_modelnum].first_texture = start_num+3;
199 #endif
200
201         Polygon_models[exit_modelnum].model_data = d_malloc(Polygon_models[exit_modelnum].model_data_size);
202         Assert( Polygon_models[exit_modelnum].model_data != NULL );
203         cfread( Polygon_models[exit_modelnum].model_data, sizeof(ubyte), Polygon_models[exit_modelnum].model_data_size, exit_hamfile );
204 #ifdef WORDS_BIGENDIAN
205         swap_polygon_model_data(Polygon_models[exit_modelnum].model_data);
206 #endif
207         g3_init_polygon_model(Polygon_models[exit_modelnum].model_data);
208
209         Polygon_models[destroyed_exit_modelnum].model_data = d_malloc(Polygon_models[destroyed_exit_modelnum].model_data_size);
210         Assert( Polygon_models[destroyed_exit_modelnum].model_data != NULL );
211         cfread( Polygon_models[destroyed_exit_modelnum].model_data, sizeof(ubyte), Polygon_models[destroyed_exit_modelnum].model_data_size, exit_hamfile );
212 #ifdef WORDS_BIGENDIAN
213         swap_polygon_model_data(Polygon_models[destroyed_exit_modelnum].model_data);
214 #endif
215         g3_init_polygon_model(Polygon_models[destroyed_exit_modelnum].model_data);
216
217         cfclose(exit_hamfile);
218
219 }
220
221 //#endif                // MACINTOSH
222
223 //-----------------------------------------------------------------
224 // Read data from piggy.
225 // This is called when the editor is OUT.
226 // If editor is in, bm_init_use_table() is called.
227 int bm_init()
228 {
229         init_polygon_models();
230         if (! piggy_init())                             // This calls bm_read_all
231                 Error("Cannot open pig and/or ham file");
232
233         piggy_read_sounds();
234
235         init_endlevel();                //this is in bm_init_use_tbl(), so I gues it goes here
236
237         return 0;
238 }
239
240 void bm_read_all(CFILE * fp)
241 {
242         int i,t;
243
244         NumTextures = cfile_read_int(fp);
245         bitmap_index_read_n(Textures, NumTextures, fp );
246         tmap_info_read_n(TmapInfo, NumTextures, fp);
247
248         t = cfile_read_int(fp);
249         cfread( Sounds, sizeof(ubyte), t, fp );
250         cfread( AltSounds, sizeof(ubyte), t, fp );
251
252         Num_vclips = cfile_read_int(fp);
253         vclip_read_n(Vclip, Num_vclips, fp);
254
255         Num_effects = cfile_read_int(fp);
256         eclip_read_n(Effects, Num_effects, fp);
257
258         Num_wall_anims = cfile_read_int(fp);
259         wclip_read_n(WallAnims, Num_wall_anims, fp);
260
261         N_robot_types = cfile_read_int(fp);
262         robot_info_read_n(Robot_info, N_robot_types, fp);
263
264         N_robot_joints = cfile_read_int(fp);
265         jointpos_read_n(Robot_joints, N_robot_joints, fp);
266
267         N_weapon_types = cfile_read_int(fp);
268         weapon_info_read_n(Weapon_info, N_weapon_types, fp, Piggy_hamfile_version);
269
270         N_powerup_types = cfile_read_int(fp);
271         powerup_type_info_read_n(Powerup_info, N_powerup_types, fp);
272
273         N_polygon_models = cfile_read_int(fp);
274         polymodel_read_n(Polygon_models, N_polygon_models, fp);
275
276         for (i=0; i<N_polygon_models; i++ )     {
277                 Polygon_models[i].model_data = d_malloc(Polygon_models[i].model_data_size);
278                 Assert( Polygon_models[i].model_data != NULL );
279                 cfread( Polygon_models[i].model_data, sizeof(ubyte), Polygon_models[i].model_data_size, fp );
280 #ifdef WORDS_BIGENDIAN
281                 swap_polygon_model_data(Polygon_models[i].model_data);
282 #endif
283                 g3_init_polygon_model(Polygon_models[i].model_data);
284         }
285
286         for (i = 0; i < N_polygon_models; i++)
287                 Dying_modelnums[i] = cfile_read_int(fp);
288         for (i = 0; i < N_polygon_models; i++)
289                 Dead_modelnums[i] = cfile_read_int(fp);
290
291         t = cfile_read_int(fp);
292         bitmap_index_read_n(Gauges, t, fp);
293         bitmap_index_read_n(Gauges_hires, t, fp);
294
295         N_ObjBitmaps = cfile_read_int(fp);
296         bitmap_index_read_n(ObjBitmaps, N_ObjBitmaps, fp);
297         for (i = 0; i < N_ObjBitmaps; i++)
298                 ObjBitmapPtrs[i] = cfile_read_short(fp);
299
300         player_ship_read(&only_player_ship, fp);
301
302         Num_cockpits = cfile_read_int(fp);
303         bitmap_index_read_n(cockpit_bitmap, Num_cockpits, fp);
304
305 //@@    cfread( &Num_total_object_types, sizeof(int), 1, fp );
306 //@@    cfread( ObjType, sizeof(byte), Num_total_object_types, fp );
307 //@@    cfread( ObjId, sizeof(byte), Num_total_object_types, fp );
308 //@@    cfread( ObjStrength, sizeof(fix), Num_total_object_types, fp );
309
310         First_multi_bitmap_num = cfile_read_int(fp);
311
312         Num_reactors = cfile_read_int(fp);
313         reactor_read_n(Reactors, Num_reactors, fp);
314
315         Marker_model_num = cfile_read_int(fp);
316
317         //@@cfread( &N_controlcen_guns, sizeof(int), 1, fp );
318         //@@cfread( controlcen_gun_points, sizeof(vms_vector), N_controlcen_guns, fp );
319         //@@cfread( controlcen_gun_dirs, sizeof(vms_vector), N_controlcen_guns, fp );
320
321         if (Piggy_hamfile_version < 3) {
322                 exit_modelnum = cfile_read_int(fp);
323                 destroyed_exit_modelnum = cfile_read_int(fp);
324         }
325
326 }
327
328
329 //these values are the number of each item in the release of d2
330 //extra items added after the release get written in an additional hamfile
331 #define N_D2_ROBOT_TYPES                66
332 #define N_D2_ROBOT_JOINTS               1145
333 #define N_D2_POLYGON_MODELS             166
334 #define N_D2_OBJBITMAPS                 422
335 #define N_D2_OBJBITMAPPTRS              502
336 #define N_D2_WEAPON_TYPES               62
337
338 //type==1 means 1.1, type==2 means 1.2 (with weaons)
339 void bm_read_extra_robots(char *fname,int type)
340 {
341         CFILE *fp;
342         int t,i;
343         int version;
344
345         #ifdef MACINTOSH
346                 ulong varSave = 0;
347         #endif
348
349         fp = cfopen(fname,"rb");
350
351         if (type == 2) {
352                 int sig;
353
354                 sig = cfile_read_int(fp);
355                 if (sig != MAKE_SIG('X','H','A','M'))
356                         return;
357                 version = cfile_read_int(fp);
358         }
359         else
360                 version = 0;
361
362         //read extra weapons
363
364         t = cfile_read_int(fp);
365         N_weapon_types = N_D2_WEAPON_TYPES+t;
366         if (N_weapon_types >= MAX_WEAPON_TYPES)
367                 Error("Too many weapons (%d) in <%s>.  Max is %d.",t,fname,MAX_WEAPON_TYPES-N_D2_WEAPON_TYPES);
368         weapon_info_read_n(&Weapon_info[N_D2_WEAPON_TYPES], t, fp, version);
369
370         //now read robot info
371
372         t = cfile_read_int(fp);
373         N_robot_types = N_D2_ROBOT_TYPES+t;
374         if (N_robot_types >= MAX_ROBOT_TYPES)
375                 Error("Too many robots (%d) in <%s>.  Max is %d.",t,fname,MAX_ROBOT_TYPES-N_D2_ROBOT_TYPES);
376         robot_info_read_n(&Robot_info[N_D2_ROBOT_TYPES], t, fp);
377
378         t = cfile_read_int(fp);
379         N_robot_joints = N_D2_ROBOT_JOINTS+t;
380         if (N_robot_joints >= MAX_ROBOT_JOINTS)
381                 Error("Too many robot joints (%d) in <%s>.  Max is %d.",t,fname,MAX_ROBOT_JOINTS-N_D2_ROBOT_JOINTS);
382         jointpos_read_n(&Robot_joints[N_D2_ROBOT_JOINTS], t, fp);
383
384         t = cfile_read_int(fp);
385         N_polygon_models = N_D2_POLYGON_MODELS+t;
386         if (N_polygon_models >= MAX_POLYGON_MODELS)
387                 Error("Too many polygon models (%d) in <%s>.  Max is %d.",t,fname,MAX_POLYGON_MODELS-N_D2_POLYGON_MODELS);
388         polymodel_read_n(&Polygon_models[N_D2_POLYGON_MODELS], t, fp);
389
390         for (i=N_D2_POLYGON_MODELS; i<N_polygon_models; i++ )
391         {
392                 Polygon_models[i].model_data = d_malloc(Polygon_models[i].model_data_size);
393                 Assert( Polygon_models[i].model_data != NULL );
394                 cfread( Polygon_models[i].model_data, sizeof(ubyte), Polygon_models[i].model_data_size, fp );
395 #ifdef WORDS_BIGENDIAN
396                 swap_polygon_model_data(Polygon_models[i].model_data);
397 #endif
398                 g3_init_polygon_model(Polygon_models[i].model_data);
399         }
400
401         for (i = N_D2_POLYGON_MODELS; i < N_polygon_models; i++)
402                 Dying_modelnums[i] = cfile_read_int(fp);
403         for (i = N_D2_POLYGON_MODELS; i < N_polygon_models; i++)
404                 Dead_modelnums[i] = cfile_read_int(fp);
405
406         t = cfile_read_int(fp);
407         if (N_D2_OBJBITMAPS+t >= MAX_OBJ_BITMAPS)
408                 Error("Too many object bitmaps (%d) in <%s>.  Max is %d.",t,fname,MAX_OBJ_BITMAPS-N_D2_OBJBITMAPS);
409         bitmap_index_read_n(&ObjBitmaps[N_D2_OBJBITMAPS], t, fp);
410
411         t = cfile_read_int(fp);
412         if (N_D2_OBJBITMAPPTRS+t >= MAX_OBJ_BITMAPS)
413                 Error("Too many object bitmap pointers (%d) in <%s>.  Max is %d.",t,fname,MAX_OBJ_BITMAPS-N_D2_OBJBITMAPPTRS);
414         for (i = N_D2_OBJBITMAPPTRS; i < (N_D2_OBJBITMAPPTRS + t); i++)
415                 ObjBitmapPtrs[i] = cfile_read_short(fp);
416
417         cfclose(fp);
418 }
419
420 extern void change_filename_extension( char *dest, char *src, char *new_ext );
421
422 int Robot_replacements_loaded = 0;
423
424 void load_robot_replacements(char *level_name)
425 {
426         CFILE *fp;
427         int t,i,j;
428         char ifile_name[FILENAME_LEN];
429
430         change_filename_extension(ifile_name, level_name, ".HXM" );
431
432         fp = cfopen(ifile_name,"rb");
433
434         if (!fp)                //no robot replacement file
435                 return;
436
437         t = cfile_read_int(fp);                 //read id "HXM!"
438         if (t!= 0x21584d48)
439                 Error("ID of HXM! file incorrect");
440
441         t = cfile_read_int(fp);                 //read version
442         if (t<1)
443                 Error("HXM! version too old (%d)",t);
444
445         t = cfile_read_int(fp);                 //read number of robots
446         for (j=0;j<t;j++) {
447                 i = cfile_read_int(fp);         //read robot number
448                 if (i<0 || i>=N_robot_types)
449                         Error("Robots number (%d) out of range in (%s).  Range = [0..%d].",i,level_name,N_robot_types-1);
450                 robot_info_read_n(&Robot_info[i], 1, fp);
451         }
452
453         t = cfile_read_int(fp);                 //read number of joints
454         for (j=0;j<t;j++) {
455                 i = cfile_read_int(fp);         //read joint number
456                 if (i<0 || i>=N_robot_joints)
457                         Error("Robots joint (%d) out of range in (%s).  Range = [0..%d].",i,level_name,N_robot_joints-1);
458                 jointpos_read_n(&Robot_joints[i], 1, fp);
459         }
460
461         t = cfile_read_int(fp);                 //read number of polygon models
462         for (j=0;j<t;j++)
463         {
464                 i = cfile_read_int(fp);         //read model number
465                 if (i<0 || i>=N_polygon_models)
466                         Error("Polygon model (%d) out of range in (%s).  Range = [0..%d].",i,level_name,N_polygon_models-1);
467                 polymodel_read(&Polygon_models[i], fp);
468         
469                 d_free(Polygon_models[i].model_data);
470                 Polygon_models[i].model_data = d_malloc(Polygon_models[i].model_data_size);
471                 Assert( Polygon_models[i].model_data != NULL );
472
473                 cfread( Polygon_models[i].model_data, sizeof(ubyte), Polygon_models[i].model_data_size, fp );
474 #ifdef WORDS_BIGENDIAN
475                 swap_polygon_model_data(Polygon_models[i].model_data);
476 #endif
477                 g3_init_polygon_model(Polygon_models[i].model_data);
478
479                 Dying_modelnums[i] = cfile_read_int(fp);
480                 Dead_modelnums[i] = cfile_read_int(fp);
481         }
482
483         t = cfile_read_int(fp);                 //read number of objbitmaps
484         for (j=0;j<t;j++) {
485                 i = cfile_read_int(fp);         //read objbitmap number
486                 if (i<0 || i>=MAX_OBJ_BITMAPS)
487                         Error("Object bitmap number (%d) out of range in (%s).  Range = [0..%d].",i,level_name,MAX_OBJ_BITMAPS-1);
488                 bitmap_index_read(&ObjBitmaps[i], fp);
489         }
490
491         t = cfile_read_int(fp);                 //read number of objbitmapptrs
492         for (j=0;j<t;j++) {
493                 i = cfile_read_int(fp);         //read objbitmapptr number
494                 if (i<0 || i>=MAX_OBJ_BITMAPS)
495                         Error("Object bitmap pointer (%d) out of range in (%s).  Range = [0..%d].",i,level_name,MAX_OBJ_BITMAPS-1);
496                 ObjBitmapPtrs[i] = cfile_read_short(fp);
497         }
498
499         cfclose(fp);
500 }