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