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