]> icculus.org git repositories - btb/d2x.git/blob - main/bm.c
support shareware datafiles\!
[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 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 start_num;
170
171         start_num = N_ObjBitmaps;
172         extra_bitmap_num = Num_bitmap_files;
173         load_exit_model_bitmap("steel1.bbm");
174         load_exit_model_bitmap("rbot061.bbm");
175         load_exit_model_bitmap("rbot062.bbm");
176
177         load_exit_model_bitmap("steel1.bbm");
178         load_exit_model_bitmap("rbot061.bbm");
179         load_exit_model_bitmap("rbot063.bbm");
180
181         exit_hamfile = cfopen(":Data:exit.ham","rb");
182
183         exit_modelnum = N_polygon_models++;
184         destroyed_exit_modelnum = N_polygon_models++;
185
186         polymodel_read(&Polygon_models[exit_modelnum], exit_hamfile);
187         polymodel_read(&Polygon_models[destroyed_exit_modelnum], exit_hamfile);
188 #ifdef MACINTOSH //not sure what these are for
189         Polygon_models[exit_modelnum].first_texture = start_num;
190         Polygon_models[destroyed_exit_modelnum].first_texture = start_num+3;
191 #endif
192
193         Polygon_models[exit_modelnum].model_data = d_malloc(Polygon_models[exit_modelnum].model_data_size);
194         Assert( Polygon_models[exit_modelnum].model_data != NULL );
195         cfread( Polygon_models[exit_modelnum].model_data, sizeof(ubyte), Polygon_models[exit_modelnum].model_data_size, exit_hamfile );
196 #ifdef WORDS_BIGENDIAN
197         swap_polygon_model_data(Polygon_models[exit_modelnum].model_data);
198 #endif
199         g3_init_polygon_model(Polygon_models[exit_modelnum].model_data);
200
201         Polygon_models[destroyed_exit_modelnum].model_data = d_malloc(Polygon_models[destroyed_exit_modelnum].model_data_size);
202         Assert( Polygon_models[destroyed_exit_modelnum].model_data != NULL );
203         cfread( Polygon_models[destroyed_exit_modelnum].model_data, sizeof(ubyte), Polygon_models[destroyed_exit_modelnum].model_data_size, exit_hamfile );
204 #ifdef WORDS_BIGENDIAN
205         swap_polygon_model_data(Polygon_models[destroyed_exit_modelnum].model_data);
206 #endif
207         g3_init_polygon_model(Polygon_models[destroyed_exit_modelnum].model_data);
208
209         cfclose(exit_hamfile);
210
211 }
212 #endif          // SHAREWARE
213
214 //#endif                // MACINTOSH
215
216 //-----------------------------------------------------------------
217 // Read data from piggy.
218 // This is called when the editor is OUT.  
219 // If editor is in, bm_init_use_table() is called.
220 int bm_init()
221 {
222         init_polygon_models();
223         if (! piggy_init())                             // This calls bm_read_all
224                 Error("Cannot open pig and/or ham file");
225
226         piggy_read_sounds();
227
228         #ifdef SHAREWARE
229         init_endlevel();                //this is in bm_init_use_tbl(), so I gues it goes here
230         #endif
231
232         return 0;
233 }
234
235 void bm_read_all(CFILE * fp)
236 {
237         int i,t;
238
239         NumTextures = cfile_read_int(fp);
240         for (i = 0; i < NumTextures; i++)
241                 bitmap_index_read(&Textures[i], fp);
242         for (i = 0; i < NumTextures; i++)
243                 tmap_info_read(&TmapInfo[i], fp);
244
245         t = cfile_read_int(fp); 
246         cfread( Sounds, sizeof(ubyte), t, fp );
247         cfread( AltSounds, sizeof(ubyte), t, fp );
248
249         Num_vclips = cfile_read_int(fp);
250         for (i = 0; i < Num_vclips; i++)
251                 vclip_read(&Vclip[i], fp);
252
253         Num_effects = cfile_read_int(fp);
254         for (i = 0; i < Num_effects; i++)
255                 eclip_read(&Effects[i], fp);
256
257         Num_wall_anims = cfile_read_int(fp);
258         for (i = 0; i < Num_wall_anims; i++)
259                 wclip_read(&WallAnims[i], fp);
260
261         N_robot_types = cfile_read_int(fp);
262         for (i = 0; i < N_robot_types; i++)
263                 robot_info_read(&Robot_info[i], fp);
264
265         N_robot_joints = cfile_read_int(fp);
266         for (i = 0; i < N_robot_joints; i++)
267                 jointpos_read(&Robot_joints[i], fp);
268
269         N_weapon_types = cfile_read_int(fp);
270         for (i = 0; i < N_weapon_types; i++)
271                 weapon_info_read(&Weapon_info[i], fp);
272
273         N_powerup_types = cfile_read_int(fp);
274         for (i = 0; i < N_powerup_types; i++)
275                 powerup_type_info_read(&Powerup_info[i], 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         for (i = 0; i < Num_reactors; i++)
327                 reactor_read(&Reactors[i], fp);
328
329         Marker_model_num = cfile_read_int(fp);
330
331         //@@cfread( &N_controlcen_guns, sizeof(int), 1, fp );
332         //@@cfread( controlcen_gun_points, sizeof(vms_vector), N_controlcen_guns, fp );
333         //@@cfread( controlcen_gun_dirs, sizeof(vms_vector), N_controlcen_guns, fp );
334
335         #ifdef SHAREWARE
336         exit_modelnum = cfile_read_int(fp);
337         destroyed_exit_modelnum = cfile_read_int(fp);
338         #endif
339
340 }
341
342
343 //these values are the number of each item in the release of d2
344 //extra items added after the release get written in an additional hamfile
345 #define N_D2_ROBOT_TYPES                66
346 #define N_D2_ROBOT_JOINTS               1145
347 #define N_D2_POLYGON_MODELS             166
348 #define N_D2_OBJBITMAPS                 422
349 #define N_D2_OBJBITMAPPTRS              502
350 #define N_D2_WEAPON_TYPES               62
351
352 //type==1 means 1.1, type==2 means 1.2 (with weaons)
353 void bm_read_extra_robots(char *fname,int type)
354 {
355         CFILE *fp;
356         int t,i;
357         int version;
358         
359         #ifdef MACINTOSH
360                 ulong varSave = 0;
361         #endif
362
363         fp = cfopen(fname,"rb");
364
365         if (type == 2) {
366                 int sig;
367
368                 sig = cfile_read_int(fp);
369                 if (sig != MAKE_SIG('X','H','A','M'))
370                         return;
371                 version = cfile_read_int(fp);
372         }
373         else
374                 version = 0;
375
376         //read extra weapons
377
378         t = cfile_read_int(fp);
379         N_weapon_types = N_D2_WEAPON_TYPES+t;
380         if (N_weapon_types >= MAX_WEAPON_TYPES)
381                 Error("Too many weapons (%d) in <%s>.  Max is %d.",t,fname,MAX_WEAPON_TYPES-N_D2_WEAPON_TYPES);
382         for (i = N_D2_WEAPON_TYPES; i < t + N_D2_WEAPON_TYPES; i++)
383                 weapon_info_read(&Weapon_info[i], fp);
384
385         //now read robot info
386
387         t = cfile_read_int(fp);
388         N_robot_types = N_D2_ROBOT_TYPES+t;
389         if (N_robot_types >= MAX_ROBOT_TYPES)
390                 Error("Too many robots (%d) in <%s>.  Max is %d.",t,fname,MAX_ROBOT_TYPES-N_D2_ROBOT_TYPES);
391         for (i = N_D2_ROBOT_TYPES; i < t + N_D2_ROBOT_TYPES; i++)
392                 robot_info_read(&Robot_info[i], fp);
393         
394         t = cfile_read_int(fp);
395         N_robot_joints = N_D2_ROBOT_JOINTS+t;
396         if (N_robot_joints >= MAX_ROBOT_JOINTS)
397                 Error("Too many robot joints (%d) in <%s>.  Max is %d.",t,fname,MAX_ROBOT_JOINTS-N_D2_ROBOT_JOINTS);
398         for (i = N_D2_ROBOT_JOINTS; i < t + N_D2_ROBOT_JOINTS; i++)
399                 jointpos_read(&Robot_joints[i], fp);
400         
401         t = cfile_read_int(fp);
402         N_polygon_models = N_D2_POLYGON_MODELS+t;
403         if (N_polygon_models >= MAX_POLYGON_MODELS)
404                 Error("Too many polygon models (%d) in <%s>.  Max is %d.",t,fname,MAX_POLYGON_MODELS-N_D2_POLYGON_MODELS);
405         for (i = N_D2_POLYGON_MODELS; i < t + N_D2_POLYGON_MODELS; i++)
406                 polymodel_read(&Polygon_models[i], fp);
407
408         for (i=N_D2_POLYGON_MODELS; i<N_polygon_models; i++ )
409         {
410                 Polygon_models[i].model_data = d_malloc(Polygon_models[i].model_data_size);
411                 Assert( Polygon_models[i].model_data != NULL );
412                 cfread( Polygon_models[i].model_data, sizeof(ubyte), Polygon_models[i].model_data_size, fp );
413                 
414 #ifdef WORDS_BIGENDIAN
415                 swap_polygon_model_data(Polygon_models[i].model_data);
416 #endif
417                 
418                 g3_init_polygon_model(Polygon_models[i].model_data);
419         }
420
421         for (i = N_D2_POLYGON_MODELS; i < N_polygon_models; i++)
422                 Dying_modelnums[i] = cfile_read_int(fp);
423         for (i = N_D2_POLYGON_MODELS; i < N_polygon_models; i++)
424                 Dead_modelnums[i] = cfile_read_int(fp);
425
426         t = cfile_read_int(fp);
427         if (N_D2_OBJBITMAPS+t >= MAX_OBJ_BITMAPS)
428                 Error("Too many object bitmaps (%d) in <%s>.  Max is %d.",t,fname,MAX_OBJ_BITMAPS-N_D2_OBJBITMAPS);
429         for (i = N_D2_OBJBITMAPS; i < (N_D2_OBJBITMAPS + t); i++)
430                 bitmap_index_read(&ObjBitmaps[i], 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(&Robot_info[i], 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(&Robot_joints[i], 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 #ifdef WORDS_BIGENDIAN
496                 swap_polygon_model_data(Polygon_models[i].model_data);
497 #endif
498                 g3_init_polygon_model(Polygon_models[i].model_data);
499
500                 Dying_modelnums[i] = cfile_read_int(fp);
501                 Dead_modelnums[i] = cfile_read_int(fp);
502         }
503
504         t = cfile_read_int(fp);                 //read number of objbitmaps
505         for (j=0;j<t;j++) {
506                 i = cfile_read_int(fp);         //read objbitmap number
507                 if (i<0 || i>=MAX_OBJ_BITMAPS)
508                         Error("Object bitmap number (%d) out of range in (%s).  Range = [0..%d].",i,level_name,MAX_OBJ_BITMAPS-1);
509                 bitmap_index_read(&ObjBitmaps[i], fp);
510         }
511
512         t = cfile_read_int(fp);                 //read number of objbitmapptrs
513         for (j=0;j<t;j++) {
514                 i = cfile_read_int(fp);         //read objbitmapptr number
515                 if (i<0 || i>=MAX_OBJ_BITMAPS)
516                         Error("Object bitmap pointer (%d) out of range in (%s).  Range = [0..%d].",i,level_name,MAX_OBJ_BITMAPS-1);
517                 ObjBitmapPtrs[i] = cfile_read_short(fp);
518         }
519
520         cfclose(fp);
521 }