1 /* $Id: bm.c,v 1.17 2002-09-04 22:28:48 btb Exp $ */
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.
17 * Bitmap and palette loading functions.
61 ubyte Sounds[MAX_SOUNDS];
62 ubyte AltSounds[MAX_SOUNDS];
65 int Num_total_object_types;
66 byte ObjType[MAX_OBJTYPE];
67 byte ObjId[MAX_OBJTYPE];
68 fix ObjStrength[MAX_OBJTYPE];
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];
75 //the polygon model number to use for the marker
76 int Marker_model_num = -1;
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;
82 //----------------- Miscellaneous bitmap pointers ---------------
84 bitmap_index cockpit_bitmap[N_COCKPIT_BITMAPS];
86 //---------------- Variables for wall textures ------------------
88 tmap_info TmapInfo[MAX_TEXTURES];
90 //---------------- Variables for object textures ----------------
92 int First_multi_bitmap_num=-1;
95 bitmap_index ObjBitmaps[MAX_OBJ_BITMAPS];
96 ushort ObjBitmapPtrs[MAX_OBJ_BITMAPS]; // These point back into ObjBitmaps, since some are used twice.
99 #define tmap_info_read_n(ti, n, fp) cfread(ti, sizeof(tmap_info), n, fp)
102 * reads n tmap_info structs from a CFILE
104 int tmap_info_read_n(tmap_info *ti, int n, CFILE *fp)
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);
124 extern int Num_bitmap_files;
125 int extra_bitmap_num;
127 bitmap_index exitmodel_bm_load_sub( char * filename )
129 bitmap_index bitmap_num;
130 grs_bitmap * new = &GameBitmaps[extra_bitmap_num];
132 int iff_error; //reference parm to avoid warning message
134 bitmap_num.index = 0;
136 //MALLOC( new, grs_bitmap, 1 );
137 iff_error = iff_read_bitmap(filename,new,BM_LINEAR,newpal);
139 if (iff_error != IFF_NO_ERROR) {
140 Error("Error loading exit model bitmap <%s> - IFF error: %s",filename,iff_errormsg(iff_error));
143 if ( iff_has_transparency )
144 gr_remap_bitmap_good( new, newpal, iff_transparent_color, 254 );
146 gr_remap_bitmap_good( new, newpal, -1, 254 );
148 new->avg_color = 0; //compute_average_pixel(new);
150 bitmap_num.index = extra_bitmap_num;
152 GameBitmaps[extra_bitmap_num++] = *new;
158 grs_bitmap *load_exit_model_bitmap(char *name)
160 Assert(N_ObjBitmaps < MAX_OBJ_BITMAPS);
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;
168 Assert(N_ObjBitmaps < MAX_OBJ_BITMAPS);
169 return &GameBitmaps[ObjBitmaps[N_ObjBitmaps-1].index];
174 free_exit_model_data()
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;
185 void ogl_cache_polymodel_textures(int model_num);
188 void load_exit_models()
193 if (!cfexist("steel1.bbm") ||
194 !cfexist("rbot061.bbm") ||
195 !cfexist("rbot062.bbm") ||
196 !cfexist("rbot063.bbm")) {
197 Warning("Can't load exit models!\n");
201 start_num = N_ObjBitmaps;
202 extra_bitmap_num = Num_bitmap_files;
203 load_exit_model_bitmap("steel1.bbm");
204 load_exit_model_bitmap("rbot061.bbm");
205 load_exit_model_bitmap("rbot062.bbm");
207 load_exit_model_bitmap("steel1.bbm");
208 load_exit_model_bitmap("rbot061.bbm");
209 load_exit_model_bitmap("rbot063.bbm");
211 if (cfexist("exit.ham")) {
213 exit_hamfile = cfopen("exit.ham","rb");
215 exit_hamfile = cfopen(":Data:exit.ham","rb");
218 exit_modelnum = N_polygon_models++;
219 destroyed_exit_modelnum = N_polygon_models++;
220 polymodel_read(&Polygon_models[exit_modelnum], exit_hamfile);
221 polymodel_read(&Polygon_models[destroyed_exit_modelnum], exit_hamfile);
222 Polygon_models[exit_modelnum].first_texture = start_num;
223 Polygon_models[destroyed_exit_modelnum].first_texture = start_num+3;
225 Polygon_models[exit_modelnum].model_data = d_malloc(Polygon_models[exit_modelnum].model_data_size);
226 Assert( Polygon_models[exit_modelnum].model_data != NULL );
227 cfread( Polygon_models[exit_modelnum].model_data, sizeof(ubyte), Polygon_models[exit_modelnum].model_data_size, exit_hamfile );
228 #ifdef WORDS_BIGENDIAN
229 swap_polygon_model_data(Polygon_models[exit_modelnum].model_data);
231 g3_init_polygon_model(Polygon_models[exit_modelnum].model_data);
233 Polygon_models[destroyed_exit_modelnum].model_data = d_malloc(Polygon_models[destroyed_exit_modelnum].model_data_size);
234 Assert( Polygon_models[destroyed_exit_modelnum].model_data != NULL );
235 cfread( Polygon_models[destroyed_exit_modelnum].model_data, sizeof(ubyte), Polygon_models[destroyed_exit_modelnum].model_data_size, exit_hamfile );
236 #ifdef WORDS_BIGENDIAN
237 swap_polygon_model_data(Polygon_models[destroyed_exit_modelnum].model_data);
239 g3_init_polygon_model(Polygon_models[destroyed_exit_modelnum].model_data);
240 cfclose(exit_hamfile);
242 } else if (cfexist("exit01.pof") && cfexist("exit01d.pof")) {
244 exit_modelnum = load_polygon_model("exit01.pof", 3, start_num, NULL);
245 destroyed_exit_modelnum = load_polygon_model("exit01d.pof", 3, start_num + 3, NULL);
248 ogl_cache_polymodel_textures(exit_modelnum);
249 ogl_cache_polymodel_textures(destroyed_exit_modelnum);
253 Warning("Can't load exit models!\n");
257 //-----------------------------------------------------------------
258 // Read data from piggy.
259 // This is called when the editor is OUT.
260 // If editor is in, bm_init_use_table() is called.
263 init_polygon_models();
264 if (! piggy_init()) // This calls bm_read_all
265 Error("Cannot open pig and/or ham file");
269 init_endlevel(); //this is in bm_init_use_tbl(), so I gues it goes here
274 void bm_read_all(CFILE * fp)
278 NumTextures = cfile_read_int(fp);
279 bitmap_index_read_n(Textures, NumTextures, fp );
280 tmap_info_read_n(TmapInfo, NumTextures, fp);
282 t = cfile_read_int(fp);
283 cfread( Sounds, sizeof(ubyte), t, fp );
284 cfread( AltSounds, sizeof(ubyte), t, fp );
286 Num_vclips = cfile_read_int(fp);
287 vclip_read_n(Vclip, Num_vclips, fp);
289 Num_effects = cfile_read_int(fp);
290 eclip_read_n(Effects, Num_effects, fp);
292 Num_wall_anims = cfile_read_int(fp);
293 wclip_read_n(WallAnims, Num_wall_anims, fp);
295 N_robot_types = cfile_read_int(fp);
296 robot_info_read_n(Robot_info, N_robot_types, fp);
298 N_robot_joints = cfile_read_int(fp);
299 jointpos_read_n(Robot_joints, N_robot_joints, fp);
301 N_weapon_types = cfile_read_int(fp);
302 weapon_info_read_n(Weapon_info, N_weapon_types, fp, Piggy_hamfile_version);
304 N_powerup_types = cfile_read_int(fp);
305 powerup_type_info_read_n(Powerup_info, N_powerup_types, fp);
307 N_polygon_models = cfile_read_int(fp);
308 polymodel_read_n(Polygon_models, N_polygon_models, fp);
310 for (i=0; i<N_polygon_models; i++ ) {
311 Polygon_models[i].model_data = d_malloc(Polygon_models[i].model_data_size);
312 Assert( Polygon_models[i].model_data != NULL );
313 cfread( Polygon_models[i].model_data, sizeof(ubyte), Polygon_models[i].model_data_size, fp );
314 #ifdef WORDS_BIGENDIAN
315 swap_polygon_model_data(Polygon_models[i].model_data);
317 g3_init_polygon_model(Polygon_models[i].model_data);
320 for (i = 0; i < N_polygon_models; i++)
321 Dying_modelnums[i] = cfile_read_int(fp);
322 for (i = 0; i < N_polygon_models; i++)
323 Dead_modelnums[i] = cfile_read_int(fp);
325 t = cfile_read_int(fp);
326 bitmap_index_read_n(Gauges, t, fp);
327 bitmap_index_read_n(Gauges_hires, t, fp);
329 N_ObjBitmaps = cfile_read_int(fp);
330 bitmap_index_read_n(ObjBitmaps, N_ObjBitmaps, fp);
331 for (i = 0; i < N_ObjBitmaps; i++)
332 ObjBitmapPtrs[i] = cfile_read_short(fp);
334 player_ship_read(&only_player_ship, fp);
336 Num_cockpits = cfile_read_int(fp);
337 bitmap_index_read_n(cockpit_bitmap, Num_cockpits, fp);
339 //@@ cfread( &Num_total_object_types, sizeof(int), 1, fp );
340 //@@ cfread( ObjType, sizeof(byte), Num_total_object_types, fp );
341 //@@ cfread( ObjId, sizeof(byte), Num_total_object_types, fp );
342 //@@ cfread( ObjStrength, sizeof(fix), Num_total_object_types, fp );
344 First_multi_bitmap_num = cfile_read_int(fp);
346 Num_reactors = cfile_read_int(fp);
347 reactor_read_n(Reactors, Num_reactors, fp);
349 Marker_model_num = cfile_read_int(fp);
351 //@@cfread( &N_controlcen_guns, sizeof(int), 1, fp );
352 //@@cfread( controlcen_gun_points, sizeof(vms_vector), N_controlcen_guns, fp );
353 //@@cfread( controlcen_gun_dirs, sizeof(vms_vector), N_controlcen_guns, fp );
355 if (Piggy_hamfile_version < 3) {
356 exit_modelnum = cfile_read_int(fp);
357 destroyed_exit_modelnum = cfile_read_int(fp);
363 //these values are the number of each item in the release of d2
364 //extra items added after the release get written in an additional hamfile
365 #define N_D2_ROBOT_TYPES 66
366 #define N_D2_ROBOT_JOINTS 1145
367 #define N_D2_POLYGON_MODELS 166
368 #define N_D2_OBJBITMAPS 422
369 #define N_D2_OBJBITMAPPTRS 502
370 #define N_D2_WEAPON_TYPES 62
372 //type==1 means 1.1, type==2 means 1.2 (with weapons)
373 void bm_read_extra_robots(char *fname,int type)
379 fp = cfopen(fname,"rb");
384 sig = cfile_read_int(fp);
385 if (sig != MAKE_SIG('X','H','A','M'))
387 version = cfile_read_int(fp);
394 t = cfile_read_int(fp);
395 N_weapon_types = N_D2_WEAPON_TYPES+t;
396 if (N_weapon_types >= MAX_WEAPON_TYPES)
397 Error("Too many weapons (%d) in <%s>. Max is %d.",t,fname,MAX_WEAPON_TYPES-N_D2_WEAPON_TYPES);
398 weapon_info_read_n(&Weapon_info[N_D2_WEAPON_TYPES], t, fp, 3);
400 //now read robot info
402 t = cfile_read_int(fp);
403 N_robot_types = N_D2_ROBOT_TYPES+t;
404 if (N_robot_types >= MAX_ROBOT_TYPES)
405 Error("Too many robots (%d) in <%s>. Max is %d.",t,fname,MAX_ROBOT_TYPES-N_D2_ROBOT_TYPES);
406 robot_info_read_n(&Robot_info[N_D2_ROBOT_TYPES], t, fp);
408 t = cfile_read_int(fp);
409 N_robot_joints = N_D2_ROBOT_JOINTS+t;
410 if (N_robot_joints >= MAX_ROBOT_JOINTS)
411 Error("Too many robot joints (%d) in <%s>. Max is %d.",t,fname,MAX_ROBOT_JOINTS-N_D2_ROBOT_JOINTS);
412 jointpos_read_n(&Robot_joints[N_D2_ROBOT_JOINTS], t, fp);
414 t = cfile_read_int(fp);
415 N_polygon_models = N_D2_POLYGON_MODELS+t;
416 if (N_polygon_models >= MAX_POLYGON_MODELS)
417 Error("Too many polygon models (%d) in <%s>. Max is %d.",t,fname,MAX_POLYGON_MODELS-N_D2_POLYGON_MODELS);
418 polymodel_read_n(&Polygon_models[N_D2_POLYGON_MODELS], t, fp);
420 for (i=N_D2_POLYGON_MODELS; i<N_polygon_models; i++ )
422 Polygon_models[i].model_data = d_malloc(Polygon_models[i].model_data_size);
423 Assert( Polygon_models[i].model_data != NULL );
424 cfread( Polygon_models[i].model_data, sizeof(ubyte), Polygon_models[i].model_data_size, fp );
425 #ifdef WORDS_BIGENDIAN
426 swap_polygon_model_data(Polygon_models[i].model_data);
428 g3_init_polygon_model(Polygon_models[i].model_data);
431 for (i = N_D2_POLYGON_MODELS; i < N_polygon_models; i++)
432 Dying_modelnums[i] = cfile_read_int(fp);
433 for (i = N_D2_POLYGON_MODELS; i < N_polygon_models; i++)
434 Dead_modelnums[i] = cfile_read_int(fp);
436 t = cfile_read_int(fp);
437 if (N_D2_OBJBITMAPS+t >= MAX_OBJ_BITMAPS)
438 Error("Too many object bitmaps (%d) in <%s>. Max is %d.",t,fname,MAX_OBJ_BITMAPS-N_D2_OBJBITMAPS);
439 bitmap_index_read_n(&ObjBitmaps[N_D2_OBJBITMAPS], t, fp);
441 t = cfile_read_int(fp);
442 if (N_D2_OBJBITMAPPTRS+t >= MAX_OBJ_BITMAPS)
443 Error("Too many object bitmap pointers (%d) in <%s>. Max is %d.",t,fname,MAX_OBJ_BITMAPS-N_D2_OBJBITMAPPTRS);
444 for (i = N_D2_OBJBITMAPPTRS; i < (N_D2_OBJBITMAPPTRS + t); i++)
445 ObjBitmapPtrs[i] = cfile_read_short(fp);
450 extern void change_filename_extension( char *dest, char *src, char *new_ext );
452 int Robot_replacements_loaded = 0;
454 void load_robot_replacements(char *level_name)
458 char ifile_name[FILENAME_LEN];
460 change_filename_extension(ifile_name, level_name, ".HXM" );
462 fp = cfopen(ifile_name,"rb");
464 if (!fp) //no robot replacement file
467 t = cfile_read_int(fp); //read id "HXM!"
469 Error("ID of HXM! file incorrect");
471 t = cfile_read_int(fp); //read version
473 Error("HXM! version too old (%d)",t);
475 t = cfile_read_int(fp); //read number of robots
477 i = cfile_read_int(fp); //read robot number
478 if (i<0 || i>=N_robot_types)
479 Error("Robots number (%d) out of range in (%s). Range = [0..%d].",i,level_name,N_robot_types-1);
480 robot_info_read_n(&Robot_info[i], 1, fp);
483 t = cfile_read_int(fp); //read number of joints
485 i = cfile_read_int(fp); //read joint number
486 if (i<0 || i>=N_robot_joints)
487 Error("Robots joint (%d) out of range in (%s). Range = [0..%d].",i,level_name,N_robot_joints-1);
488 jointpos_read_n(&Robot_joints[i], 1, fp);
491 t = cfile_read_int(fp); //read number of polygon models
494 i = cfile_read_int(fp); //read model number
495 if (i<0 || i>=N_polygon_models)
496 Error("Polygon model (%d) out of range in (%s). Range = [0..%d].",i,level_name,N_polygon_models-1);
497 polymodel_read(&Polygon_models[i], fp);
499 d_free(Polygon_models[i].model_data);
500 Polygon_models[i].model_data = d_malloc(Polygon_models[i].model_data_size);
501 Assert( Polygon_models[i].model_data != NULL );
503 cfread( Polygon_models[i].model_data, sizeof(ubyte), Polygon_models[i].model_data_size, fp );
504 #ifdef WORDS_BIGENDIAN
505 swap_polygon_model_data(Polygon_models[i].model_data);
507 g3_init_polygon_model(Polygon_models[i].model_data);
509 Dying_modelnums[i] = cfile_read_int(fp);
510 Dead_modelnums[i] = cfile_read_int(fp);
513 t = cfile_read_int(fp); //read number of objbitmaps
515 i = cfile_read_int(fp); //read objbitmap number
516 if (i<0 || i>=MAX_OBJ_BITMAPS)
517 Error("Object bitmap number (%d) out of range in (%s). Range = [0..%d].",i,level_name,MAX_OBJ_BITMAPS-1);
518 bitmap_index_read(&ObjBitmaps[i], fp);
521 t = cfile_read_int(fp); //read number of objbitmapptrs
523 i = cfile_read_int(fp); //read objbitmapptr number
524 if (i<0 || i>=MAX_OBJ_BITMAPS)
525 Error("Object bitmap pointer (%d) out of range in (%s). Range = [0..%d].",i,level_name,MAX_OBJ_BITMAPS-1);
526 ObjBitmapPtrs[i] = cfile_read_short(fp);