]> icculus.org git repositories - btb/d2x.git/blob - main/bm.c
fast i/o macros
[btb/d2x.git] / main / bm.c
1 /* $Id: bm.c,v 1.13 2002-08-06 01:31:06 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 #ifdef SHAREWARE
126 extern int exit_modelnum,destroyed_exit_modelnum, Num_bitmap_files;
127 int N_ObjBitmaps, extra_bitmap_num;
128
129 bitmap_index exitmodel_bm_load_sub( char * filename )
130 {
131         bitmap_index bitmap_num;
132         grs_bitmap * new;
133         ubyte newpal[256*3];
134         int iff_error;          //reference parm to avoid warning message
135
136         bitmap_num.index = 0;
137
138         MALLOC( new, grs_bitmap, 1 );
139         iff_error = iff_read_bitmap(filename,new,BM_LINEAR,newpal);
140         new->bm_handle=0;
141         if (iff_error != IFF_NO_ERROR)          {
142                 Error("Error loading exit model bitmap <%s> - IFF error: %s",filename,iff_errormsg(iff_error));
143         }
144         
145         if ( iff_has_transparency )
146                 gr_remap_bitmap_good( new, newpal, iff_transparent_color, 254 );
147         else
148                 gr_remap_bitmap_good( new, newpal, -1, 254 );
149
150         new->avg_color = 0;     //compute_average_pixel(new);
151
152         bitmap_num.index = extra_bitmap_num;
153
154         GameBitmaps[extra_bitmap_num++] = *new;
155         
156         d_free( new );
157         return bitmap_num;
158 }
159
160 grs_bitmap *load_exit_model_bitmap(char *name)
161 {
162         Assert(N_ObjBitmaps < MAX_OBJ_BITMAPS);
163
164         {
165                 ObjBitmaps[N_ObjBitmaps] = exitmodel_bm_load_sub(name);
166                 if (GameBitmaps[ObjBitmaps[N_ObjBitmaps].index].bm_w!=64 || GameBitmaps[ObjBitmaps[N_ObjBitmaps].index].bm_h!=64)
167                         Error("Bitmap <%s> is not 64x64",name);
168                 ObjBitmapPtrs[N_ObjBitmaps] = N_ObjBitmaps;
169                 N_ObjBitmaps++;
170                 Assert(N_ObjBitmaps < MAX_OBJ_BITMAPS);
171                 return &GameBitmaps[ObjBitmaps[N_ObjBitmaps-1].index];
172         }
173 }
174
175 void load_exit_models()
176 {
177         CFILE *exit_hamfile;
178         int start_num;
179
180         start_num = N_ObjBitmaps;
181         extra_bitmap_num = Num_bitmap_files;
182         load_exit_model_bitmap("steel1.bbm");
183         load_exit_model_bitmap("rbot061.bbm");
184         load_exit_model_bitmap("rbot062.bbm");
185
186         load_exit_model_bitmap("steel1.bbm");
187         load_exit_model_bitmap("rbot061.bbm");
188         load_exit_model_bitmap("rbot063.bbm");
189
190         exit_hamfile = cfopen(":Data:exit.ham","rb");
191
192         exit_modelnum = N_polygon_models++;
193         destroyed_exit_modelnum = N_polygon_models++;
194
195         polymodel_read(&Polygon_models[exit_modelnum], exit_hamfile);
196         polymodel_read(&Polygon_models[destroyed_exit_modelnum], exit_hamfile);
197 #ifdef MACINTOSH //not sure what these are for
198         Polygon_models[exit_modelnum].first_texture = start_num;
199         Polygon_models[destroyed_exit_modelnum].first_texture = start_num+3;
200 #endif
201
202         Polygon_models[exit_modelnum].model_data = d_malloc(Polygon_models[exit_modelnum].model_data_size);
203         Assert( Polygon_models[exit_modelnum].model_data != NULL );
204         cfread( Polygon_models[exit_modelnum].model_data, sizeof(ubyte), Polygon_models[exit_modelnum].model_data_size, exit_hamfile );
205 #ifdef WORDS_BIGENDIAN
206         swap_polygon_model_data(Polygon_models[exit_modelnum].model_data);
207 #endif
208         g3_init_polygon_model(Polygon_models[exit_modelnum].model_data);
209
210         Polygon_models[destroyed_exit_modelnum].model_data = d_malloc(Polygon_models[destroyed_exit_modelnum].model_data_size);
211         Assert( Polygon_models[destroyed_exit_modelnum].model_data != NULL );
212         cfread( Polygon_models[destroyed_exit_modelnum].model_data, sizeof(ubyte), Polygon_models[destroyed_exit_modelnum].model_data_size, exit_hamfile );
213 #ifdef WORDS_BIGENDIAN
214         swap_polygon_model_data(Polygon_models[destroyed_exit_modelnum].model_data);
215 #endif
216         g3_init_polygon_model(Polygon_models[destroyed_exit_modelnum].model_data);
217
218         cfclose(exit_hamfile);
219
220 }
221 #endif          // SHAREWARE
222
223 //#endif                // MACINTOSH
224
225 //-----------------------------------------------------------------
226 // Read data from piggy.
227 // This is called when the editor is OUT.  
228 // If editor is in, bm_init_use_table() is called.
229 int bm_init()
230 {
231         init_polygon_models();
232         if (! piggy_init())                             // This calls bm_read_all
233                 Error("Cannot open pig and/or ham file");
234
235         piggy_read_sounds();
236
237         #ifdef SHAREWARE
238         init_endlevel();                //this is in bm_init_use_tbl(), so I gues it goes here
239         #endif
240
241         return 0;
242 }
243
244 void bm_read_all(CFILE * fp, int hamfile_version)
245 {
246         int i,t;
247
248         NumTextures = cfile_read_int(fp);
249         bitmap_index_read_n(Textures, NumTextures, fp );
250         tmap_info_read_n(TmapInfo, NumTextures, fp);
251
252         t = cfile_read_int(fp);
253         cfread( Sounds, sizeof(ubyte), t, fp );
254         cfread( AltSounds, sizeof(ubyte), t, fp );
255
256         Num_vclips = cfile_read_int(fp);
257         vclip_read_n(Vclip, Num_vclips, fp);
258
259         Num_effects = cfile_read_int(fp);
260         eclip_read_n(Effects, Num_effects, fp);
261
262         Num_wall_anims = cfile_read_int(fp);
263         wclip_read_n(WallAnims, Num_wall_anims, fp);
264
265         N_robot_types = cfile_read_int(fp);
266         robot_info_read_n(Robot_info, N_robot_types, fp);
267
268         N_robot_joints = cfile_read_int(fp);
269         jointpos_read_n(Robot_joints, N_robot_joints, fp);
270
271         N_weapon_types = cfile_read_int(fp);
272         weapon_info_read_n(Weapon_info, N_weapon_types, fp, hamfile_version);
273
274         N_powerup_types = cfile_read_int(fp);
275         powerup_type_info_read_n(Powerup_info, N_powerup_types, fp);
276
277         N_polygon_models = cfile_read_int(fp);
278         for (i = 0; i < N_polygon_models; i++)
279                 polymodel_read(&Polygon_models[i], fp);
280
281         for (i=0; i<N_polygon_models; i++ )     {
282                 Polygon_models[i].model_data = d_malloc(Polygon_models[i].model_data_size);
283                 Assert( Polygon_models[i].model_data != NULL );
284                 cfread( Polygon_models[i].model_data, sizeof(ubyte), Polygon_models[i].model_data_size, fp );
285 #ifdef WORDS_BIGENDIAN
286                 swap_polygon_model_data(Polygon_models[i].model_data);
287 #endif
288                 g3_init_polygon_model(Polygon_models[i].model_data);
289         }
290
291         for (i = 0; i < N_polygon_models; i++)
292                 Dying_modelnums[i] = cfile_read_int(fp);
293         for (i = 0; i < N_polygon_models; i++)
294                 Dead_modelnums[i] = cfile_read_int(fp);
295
296         t = cfile_read_int(fp);
297         for (i = 0; i < t; i++)
298                 bitmap_index_read(&Gauges[i], fp);
299         for (i = 0; i < t; i++)
300                 bitmap_index_read(&Gauges_hires[i], fp);
301
302         t = cfile_read_int(fp);
303         for (i = 0; i < t; i++)
304                 bitmap_index_read(&ObjBitmaps[i], fp);
305         for (i = 0; i < t; i++)
306                 ObjBitmapPtrs[i] = cfile_read_short(fp);
307
308 #ifdef SHAREWARE
309         N_ObjBitmaps = t;
310 #endif
311
312         player_ship_read(&only_player_ship, fp);
313
314         Num_cockpits = cfile_read_int(fp);
315         for (i = 0; i < Num_cockpits; i++)
316                 bitmap_index_read(&cockpit_bitmap[i], fp);
317
318 //@@    cfread( &Num_total_object_types, sizeof(int), 1, fp );
319 //@@    cfread( ObjType, sizeof(byte), Num_total_object_types, fp );
320 //@@    cfread( ObjId, sizeof(byte), Num_total_object_types, fp );
321 //@@    cfread( ObjStrength, sizeof(fix), Num_total_object_types, fp );
322
323         First_multi_bitmap_num = cfile_read_int(fp);
324
325         Num_reactors = cfile_read_int(fp);
326         reactor_read_n(Reactors, Num_reactors, fp);
327
328         Marker_model_num = cfile_read_int(fp);
329
330         //@@cfread( &N_controlcen_guns, sizeof(int), 1, fp );
331         //@@cfread( controlcen_gun_points, sizeof(vms_vector), N_controlcen_guns, fp );
332         //@@cfread( controlcen_gun_dirs, sizeof(vms_vector), N_controlcen_guns, fp );
333
334         #ifdef SHAREWARE
335         exit_modelnum = cfile_read_int(fp);
336         destroyed_exit_modelnum = cfile_read_int(fp);
337         #endif
338
339 }
340
341
342 //these values are the number of each item in the release of d2
343 //extra items added after the release get written in an additional hamfile
344 #define N_D2_ROBOT_TYPES                66
345 #define N_D2_ROBOT_JOINTS               1145
346 #define N_D2_POLYGON_MODELS             166
347 #define N_D2_OBJBITMAPS                 422
348 #define N_D2_OBJBITMAPPTRS              502
349 #define N_D2_WEAPON_TYPES               62
350
351 //type==1 means 1.1, type==2 means 1.2 (with weaons)
352 void bm_read_extra_robots(char *fname,int type)
353 {
354         CFILE *fp;
355         int t,i;
356         int version;
357
358         #ifdef MACINTOSH
359                 ulong varSave = 0;
360         #endif
361
362         fp = cfopen(fname,"rb");
363
364         if (type == 2) {
365                 int sig;
366
367                 sig = cfile_read_int(fp);
368                 if (sig != MAKE_SIG('X','H','A','M'))
369                         return;
370                 version = cfile_read_int(fp);
371         }
372         else
373                 version = 0;
374
375         //read extra weapons
376
377         t = cfile_read_int(fp);
378         N_weapon_types = N_D2_WEAPON_TYPES+t;
379         if (N_weapon_types >= MAX_WEAPON_TYPES)
380                 Error("Too many weapons (%d) in <%s>.  Max is %d.",t,fname,MAX_WEAPON_TYPES-N_D2_WEAPON_TYPES);
381         weapon_info_read_n(&Weapon_info[N_D2_WEAPON_TYPES], t, fp, version);
382
383         //now read robot info
384
385         t = cfile_read_int(fp);
386         N_robot_types = N_D2_ROBOT_TYPES+t;
387         if (N_robot_types >= MAX_ROBOT_TYPES)
388                 Error("Too many robots (%d) in <%s>.  Max is %d.",t,fname,MAX_ROBOT_TYPES-N_D2_ROBOT_TYPES);
389         robot_info_read_n(&Robot_info[N_D2_ROBOT_TYPES], t, fp);
390
391         t = cfile_read_int(fp);
392         N_robot_joints = N_D2_ROBOT_JOINTS+t;
393         if (N_robot_joints >= MAX_ROBOT_JOINTS)
394                 Error("Too many robot joints (%d) in <%s>.  Max is %d.",t,fname,MAX_ROBOT_JOINTS-N_D2_ROBOT_JOINTS);
395         jointpos_read_n(&Robot_joints[N_D2_ROBOT_JOINTS], t, fp);
396
397         t = cfile_read_int(fp);
398         N_polygon_models = N_D2_POLYGON_MODELS+t;
399         if (N_polygon_models >= MAX_POLYGON_MODELS)
400                 Error("Too many polygon models (%d) in <%s>.  Max is %d.",t,fname,MAX_POLYGON_MODELS-N_D2_POLYGON_MODELS);
401         for (i = N_D2_POLYGON_MODELS; i < t + N_D2_POLYGON_MODELS; i++)
402                 polymodel_read(&Polygon_models[i], fp);
403
404         for (i=N_D2_POLYGON_MODELS; i<N_polygon_models; i++ )
405         {
406                 Polygon_models[i].model_data = d_malloc(Polygon_models[i].model_data_size);
407                 Assert( Polygon_models[i].model_data != NULL );
408                 cfread( Polygon_models[i].model_data, sizeof(ubyte), Polygon_models[i].model_data_size, fp );
409 #ifdef WORDS_BIGENDIAN
410                 swap_polygon_model_data(Polygon_models[i].model_data);
411 #endif
412                 g3_init_polygon_model(Polygon_models[i].model_data);
413         }
414
415         for (i = N_D2_POLYGON_MODELS; i < N_polygon_models; i++)
416                 Dying_modelnums[i] = cfile_read_int(fp);
417         for (i = N_D2_POLYGON_MODELS; i < N_polygon_models; i++)
418                 Dead_modelnums[i] = cfile_read_int(fp);
419
420         t = cfile_read_int(fp);
421         if (N_D2_OBJBITMAPS+t >= MAX_OBJ_BITMAPS)
422                 Error("Too many object bitmaps (%d) in <%s>.  Max is %d.",t,fname,MAX_OBJ_BITMAPS-N_D2_OBJBITMAPS);
423         for (i = N_D2_OBJBITMAPS; i < (N_D2_OBJBITMAPS + t); i++)
424                 bitmap_index_read(&ObjBitmaps[i], fp);
425
426         t = cfile_read_int(fp);
427         if (N_D2_OBJBITMAPPTRS+t >= MAX_OBJ_BITMAPS)
428                 Error("Too many object bitmap pointers (%d) in <%s>.  Max is %d.",t,fname,MAX_OBJ_BITMAPS-N_D2_OBJBITMAPPTRS);
429         for (i = N_D2_OBJBITMAPPTRS; i < (N_D2_OBJBITMAPPTRS + t); i++)
430                 ObjBitmapPtrs[i] = cfile_read_short(fp);
431
432         cfclose(fp);
433 }
434
435 extern void change_filename_extension( char *dest, char *src, char *new_ext );
436
437 int Robot_replacements_loaded = 0;
438
439 void load_robot_replacements(char *level_name)
440 {
441         CFILE *fp;
442         int t,i,j;
443         char ifile_name[FILENAME_LEN];
444
445         change_filename_extension(ifile_name, level_name, ".HXM" );
446
447         fp = cfopen(ifile_name,"rb");
448
449         if (!fp)                //no robot replacement file
450                 return;
451
452         t = cfile_read_int(fp);                 //read id "HXM!"
453         if (t!= 0x21584d48)
454                 Error("ID of HXM! file incorrect");
455
456         t = cfile_read_int(fp);                 //read version
457         if (t<1)
458                 Error("HXM! version too old (%d)",t);
459
460         t = cfile_read_int(fp);                 //read number of robots
461         for (j=0;j<t;j++) {
462                 i = cfile_read_int(fp);         //read robot number
463                 if (i<0 || i>=N_robot_types)
464                         Error("Robots number (%d) out of range in (%s).  Range = [0..%d].",i,level_name,N_robot_types-1);
465                 robot_info_read_n(&Robot_info[i], 1, fp);
466         }
467
468         t = cfile_read_int(fp);                 //read number of joints
469         for (j=0;j<t;j++) {
470                 i = cfile_read_int(fp);         //read joint number
471                 if (i<0 || i>=N_robot_joints)
472                         Error("Robots joint (%d) out of range in (%s).  Range = [0..%d].",i,level_name,N_robot_joints-1);
473                 jointpos_read_n(&Robot_joints[i], 1, fp);
474         }
475
476         t = cfile_read_int(fp);                 //read number of polygon models
477         for (j=0;j<t;j++)
478         {
479                 i = cfile_read_int(fp);         //read model number
480                 if (i<0 || i>=N_polygon_models)
481                         Error("Polygon model (%d) out of range in (%s).  Range = [0..%d].",i,level_name,N_polygon_models-1);
482                 polymodel_read(&Polygon_models[i], fp);
483         
484                 d_free(Polygon_models[i].model_data);
485                 Polygon_models[i].model_data = d_malloc(Polygon_models[i].model_data_size);
486                 Assert( Polygon_models[i].model_data != NULL );
487
488                 cfread( Polygon_models[i].model_data, sizeof(ubyte), Polygon_models[i].model_data_size, fp );
489 #ifdef WORDS_BIGENDIAN
490                 swap_polygon_model_data(Polygon_models[i].model_data);
491 #endif
492                 g3_init_polygon_model(Polygon_models[i].model_data);
493
494                 Dying_modelnums[i] = cfile_read_int(fp);
495                 Dead_modelnums[i] = cfile_read_int(fp);
496         }
497
498         t = cfile_read_int(fp);                 //read number of objbitmaps
499         for (j=0;j<t;j++) {
500                 i = cfile_read_int(fp);         //read objbitmap number
501                 if (i<0 || i>=MAX_OBJ_BITMAPS)
502                         Error("Object bitmap number (%d) out of range in (%s).  Range = [0..%d].",i,level_name,MAX_OBJ_BITMAPS-1);
503                 bitmap_index_read(&ObjBitmaps[i], fp);
504         }
505
506         t = cfile_read_int(fp);                 //read number of objbitmapptrs
507         for (j=0;j<t;j++) {
508                 i = cfile_read_int(fp);         //read objbitmapptr number
509                 if (i<0 || i>=MAX_OBJ_BITMAPS)
510                         Error("Object bitmap pointer (%d) out of range in (%s).  Range = [0..%d].",i,level_name,MAX_OBJ_BITMAPS-1);
511                 ObjBitmapPtrs[i] = cfile_read_short(fp);
512         }
513
514         cfclose(fp);
515 }