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