]> icculus.org git repositories - btb/d2x.git/blob - main/bm.c
comments/formatting
[btb/d2x.git] / main / bm.c
1 /* $Id: bm.c,v 1.33 2003-06-07 20:46:33 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  * Old Log:
20  * Revision 1.1  1995/05/16  15:23:08  allender
21  * Initial revision
22  *
23  * Revision 2.3  1995/03/14  16:22:04  john
24  * Added cdrom alternate directory stuff.
25  *
26  * Revision 2.2  1995/03/07  16:51:48  john
27  * Fixed robots not moving without edtiro bug.
28  *
29  * Revision 2.1  1995/03/06  15:23:06  john
30  * New screen techniques.
31  *
32  * Revision 2.0  1995/02/27  11:27:05  john
33  * New version 2.0, which has no anonymous unions, builds with
34  * Watcom 10.0, and doesn't require parsing BITMAPS.TBL.
35  *
36  */
37
38 #ifdef HAVE_CONFIG_H
39 #include <conf.h>
40 #endif
41
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45
46 #include "pstypes.h"
47 #include "inferno.h"
48 #include "gr.h"
49 #include "bm.h"
50 #include "u_mem.h"
51 #include "mono.h"
52 #include "error.h"
53 #include "object.h"
54 #include "vclip.h"
55 #include "effects.h"
56 #include "polyobj.h"
57 #include "wall.h"
58 #include "textures.h"
59 #include "game.h"
60 #ifdef NETWORK
61 #include "multi.h"
62 #endif
63 #include "iff.h"
64 #include "cfile.h"
65 #include "powerup.h"
66 #include "sounds.h"
67 #include "piggy.h"
68 #include "aistruct.h"
69 #include "robot.h"
70 #include "weapon.h"
71 #include "gauges.h"
72 #include "player.h"
73 #include "endlevel.h"
74 #include "cntrlcen.h"
75 #include "makesig.h"
76 #include "interp.h"
77
78 ubyte Sounds[MAX_SOUNDS];
79 ubyte AltSounds[MAX_SOUNDS];
80
81 #ifdef EDITOR
82 int Num_total_object_types;
83 byte    ObjType[MAX_OBJTYPE];
84 byte    ObjId[MAX_OBJTYPE];
85 fix     ObjStrength[MAX_OBJTYPE];
86 #endif
87
88 //for each model, a model number for dying & dead variants, or -1 if none
89 int Dying_modelnums[MAX_POLYGON_MODELS];
90 int Dead_modelnums[MAX_POLYGON_MODELS];
91
92 //the polygon model number to use for the marker
93 int     Marker_model_num = -1;
94
95 //right now there's only one player ship, but we can have another by
96 //adding an array and setting the pointer to the active ship.
97 player_ship only_player_ship,*Player_ship=&only_player_ship;
98
99 //----------------- Miscellaneous bitmap pointers ---------------
100 int             Num_cockpits = 0;
101 bitmap_index    cockpit_bitmap[N_COCKPIT_BITMAPS];
102
103 //---------------- Variables for wall textures ------------------
104 int             Num_tmaps;
105 tmap_info       TmapInfo[MAX_TEXTURES];
106
107 //---------------- Variables for object textures ----------------
108
109 int             First_multi_bitmap_num=-1;
110
111 int             N_ObjBitmaps;
112 bitmap_index    ObjBitmaps[MAX_OBJ_BITMAPS];
113 ushort          ObjBitmapPtrs[MAX_OBJ_BITMAPS];     // These point back into ObjBitmaps, since some are used twice.
114
115 #ifdef FAST_FILE_IO
116 #define tmap_info_read_n(ti, n, fp) cfread(ti, sizeof(tmap_info), n, fp)
117 #else
118 /*
119  * reads n tmap_info structs from a CFILE
120  */
121 int tmap_info_read_n(tmap_info *ti, int n, CFILE *fp)
122 {
123         int i;
124
125         for (i = 0; i < n; i++) {
126                 ti[i].flags = cfile_read_byte(fp);
127                 ti[i].pad[0] = cfile_read_byte(fp);
128                 ti[i].pad[1] = cfile_read_byte(fp);
129                 ti[i].pad[2] = cfile_read_byte(fp);
130                 ti[i].lighting = cfile_read_fix(fp);
131                 ti[i].damage = cfile_read_fix(fp);
132                 ti[i].eclip_num = cfile_read_short(fp);
133                 ti[i].destroyed = cfile_read_short(fp);
134                 ti[i].slide_u = cfile_read_short(fp);
135                 ti[i].slide_v = cfile_read_short(fp);
136         }
137         return i;
138 }
139 #endif
140
141 int tmap_info_read_n_d1(tmap_info *ti, int n, CFILE *fp)
142 {
143         int i;
144
145         for (i = 0; i < n; i++) {
146                 cfseek(fp, 13, SEEK_CUR);// skip filename
147                 ti[i].flags = cfile_read_byte(fp);
148                 ti[i].lighting = cfile_read_fix(fp);
149                 ti[i].damage = cfile_read_fix(fp);
150                 ti[i].eclip_num = cfile_read_int(fp);
151         }
152         return i;
153 }
154
155
156 //-----------------------------------------------------------------
157 // Read data from piggy.
158 // This is called when the editor is OUT.
159 // If editor is in, bm_init_use_table() is called.
160 int bm_init()
161 {
162         init_polygon_models();
163         if (! piggy_init())                             // This calls bm_read_all
164                 Error("Cannot open pig and/or ham file");
165
166         piggy_read_sounds();
167
168         init_endlevel();                //this is in bm_init_use_tbl(), so I gues it goes here
169
170         return 0;
171 }
172
173 void bm_read_all(CFILE * fp)
174 {
175         int i,t;
176
177         NumTextures = cfile_read_int(fp);
178         bitmap_index_read_n(Textures, NumTextures, fp );
179         tmap_info_read_n(TmapInfo, NumTextures, fp);
180
181         t = cfile_read_int(fp);
182         cfread( Sounds, sizeof(ubyte), t, fp );
183         cfread( AltSounds, sizeof(ubyte), t, fp );
184
185         Num_vclips = cfile_read_int(fp);
186         vclip_read_n(Vclip, Num_vclips, fp);
187
188         Num_effects = cfile_read_int(fp);
189         eclip_read_n(Effects, Num_effects, fp);
190
191         Num_wall_anims = cfile_read_int(fp);
192         wclip_read_n(WallAnims, Num_wall_anims, fp);
193
194         N_robot_types = cfile_read_int(fp);
195         robot_info_read_n(Robot_info, N_robot_types, fp);
196
197         N_robot_joints = cfile_read_int(fp);
198         jointpos_read_n(Robot_joints, N_robot_joints, fp);
199
200         N_weapon_types = cfile_read_int(fp);
201         weapon_info_read_n(Weapon_info, N_weapon_types, fp, Piggy_hamfile_version);
202
203         N_powerup_types = cfile_read_int(fp);
204         powerup_type_info_read_n(Powerup_info, N_powerup_types, fp);
205
206         N_polygon_models = cfile_read_int(fp);
207         polymodel_read_n(Polygon_models, N_polygon_models, fp);
208
209         for (i=0; i<N_polygon_models; i++ )
210                 polygon_model_data_read(&Polygon_models[i], fp);
211
212         for (i = 0; i < N_polygon_models; i++)
213                 Dying_modelnums[i] = cfile_read_int(fp);
214         for (i = 0; i < N_polygon_models; i++)
215                 Dead_modelnums[i] = cfile_read_int(fp);
216
217         t = cfile_read_int(fp);
218         bitmap_index_read_n(Gauges, t, fp);
219         bitmap_index_read_n(Gauges_hires, t, fp);
220
221         N_ObjBitmaps = cfile_read_int(fp);
222         bitmap_index_read_n(ObjBitmaps, N_ObjBitmaps, fp);
223         for (i = 0; i < N_ObjBitmaps; i++)
224                 ObjBitmapPtrs[i] = cfile_read_short(fp);
225
226         player_ship_read(&only_player_ship, fp);
227
228         Num_cockpits = cfile_read_int(fp);
229         bitmap_index_read_n(cockpit_bitmap, Num_cockpits, fp);
230
231 //@@    cfread( &Num_total_object_types, sizeof(int), 1, fp );
232 //@@    cfread( ObjType, sizeof(byte), Num_total_object_types, fp );
233 //@@    cfread( ObjId, sizeof(byte), Num_total_object_types, fp );
234 //@@    cfread( ObjStrength, sizeof(fix), Num_total_object_types, fp );
235
236         First_multi_bitmap_num = cfile_read_int(fp);
237
238         Num_reactors = cfile_read_int(fp);
239         reactor_read_n(Reactors, Num_reactors, fp);
240
241         Marker_model_num = cfile_read_int(fp);
242
243         //@@cfread( &N_controlcen_guns, sizeof(int), 1, fp );
244         //@@cfread( controlcen_gun_points, sizeof(vms_vector), N_controlcen_guns, fp );
245         //@@cfread( controlcen_gun_dirs, sizeof(vms_vector), N_controlcen_guns, fp );
246
247         if (Piggy_hamfile_version < 3) {
248                 exit_modelnum = cfile_read_int(fp);
249                 destroyed_exit_modelnum = cfile_read_int(fp);
250         }
251         else
252                 exit_modelnum = destroyed_exit_modelnum = N_polygon_models;
253 }
254
255 #define D1_MAX_TEXTURES 800
256 #define D1_MAX_SOUNDS 250
257 #define D1_MAX_VCLIPS 70
258 #define D1_MAX_EFFECTS 60
259 #define D1_MAX_WALL_ANIMS 30
260 #define D1_MAX_ROBOT_TYPES 30
261 #define D1_MAX_ROBOT_JOINTS 600
262 #define D1_MAX_WEAPON_TYPES 30
263 #define D1_MAX_POWERUP_TYPES 29
264 #define D1_MAX_GAUGE_BMS 80
265 #define D1_MAX_OBJ_BITMAPS 210
266 #define D1_MAX_COCKPIT_BITMAPS 4
267 #define D1_MAX_OBJTYPE 100
268 #define D1_MAX_POLYGON_MODELS 85
269
270 #define D1_TMAP_INFO_SIZE 26
271 #define D1_VCLIP_SIZE 66
272 #define D1_ROBOT_INFO_SIZE 486
273 #define D1_WEAPON_INFO_SIZE 115
274
275 #define D1_LAST_STATIC_TMAP_NUM 324
276
277 // store the Textures[] array as read from the descent 2 pig.
278 short *d2_Textures_backup = NULL;
279
280 void undo_bm_read_all_d1() {
281         if (d2_Textures_backup) {
282                 int i;
283                 for (i = 0; i < D1_LAST_STATIC_TMAP_NUM; i++)
284                         Textures[i].index = d2_Textures_backup[i];
285                 d_free(d2_Textures_backup);
286                 d2_Textures_backup = NULL;
287         }
288 }
289
290 /*
291  * used by piggy_d1_init to read in descent 1 pigfile
292  */
293 void bm_read_all_d1(CFILE * fp)
294 {
295         int i;
296
297         atexit(undo_bm_read_all_d1);
298
299         /*NumTextures = */ cfile_read_int(fp);
300         //bitmap_index_read_n(Textures, D1_MAX_TEXTURES, fp );
301         //for (i = 0; i < D1_MAX_TEXTURES; i++)
302         //      Textures[i].index = cfile_read_short(fp) + 600;  
303         //cfseek(fp, D1_MAX_TEXTURES * sizeof(short), SEEK_CUR);
304         MALLOC(d2_Textures_backup, short, D1_LAST_STATIC_TMAP_NUM);
305         for (i = 0; i < D1_LAST_STATIC_TMAP_NUM; i++) {
306                 d2_Textures_backup[i] = Textures[i].index;
307                 Textures[i].index = cfile_read_short(fp) + 521;
308         }
309         cfseek(fp, (D1_MAX_TEXTURES - D1_LAST_STATIC_TMAP_NUM) * sizeof(short), SEEK_CUR);
310
311         //tmap_info_read_n_d1(TmapInfo, D1_MAX_TEXTURES, fp);
312         cfseek(fp, D1_MAX_TEXTURES * D1_TMAP_INFO_SIZE, SEEK_CUR);
313
314         /*
315         cfread( Sounds, sizeof(ubyte), D1_MAX_SOUNDS, fp );
316         cfread( AltSounds, sizeof(ubyte), D1_MAX_SOUNDS, fp );
317         */cfseek(fp, D1_MAX_SOUNDS * 2, SEEK_CUR);
318
319         /*Num_vclips = */ cfile_read_int(fp);
320         //vclip_read_n(Vclip, D1_MAX_VCLIPS, fp);
321         cfseek(fp, D1_MAX_VCLIPS * D1_VCLIP_SIZE, SEEK_CUR);
322
323         /*
324         Num_effects = cfile_read_int(fp);
325         eclip_read_n(Effects, D1_MAX_EFFECTS, fp);
326
327         Num_wall_anims = cfile_read_int(fp);
328         wclip_read_n_d1(WallAnims, D1_MAX_WALL_ANIMS, fp);
329         */
330
331         /*
332         N_robot_types = cfile_read_int(fp);
333         //robot_info_read_n(Robot_info, D1_MAX_ROBOT_TYPES, fp);
334         cfseek(fp, D1_MAX_ROBOT_TYPES * D1_ROBOT_INFO_SIZE, SEEK_CUR);
335
336         N_robot_joints = cfile_read_int(fp);
337         jointpos_read_n(Robot_joints, D1_MAX_ROBOT_JOINTS, fp);
338
339         N_weapon_types = cfile_read_int(fp);
340         //weapon_info_read_n(Weapon_info, D1_MAX_WEAPON_TYPES, fp, Piggy_hamfile_version);
341         cfseek(fp, D1_MAX_WEAPON_TYPES * D1_WEAPON_INFO_SIZE, SEEK_CUR);
342
343         N_powerup_types = cfile_read_int(fp);
344         powerup_type_info_read_n(Powerup_info, D1_MAX_POWERUP_TYPES, fp);
345         */
346
347         /* in the following code are bugs, solved by hack
348         N_polygon_models = cfile_read_int(fp);
349         polymodel_read_n(Polygon_models, N_polygon_models, fp);
350         for (i=0; i<N_polygon_models; i++ )
351                 polygon_model_data_read(&Polygon_models[i], fp);
352         */cfseek(fp, 521490-160, SEEK_SET); // OK, I admit, this is a dirty hack
353         //bitmap_index_read_n(Gauges, D1_MAX_GAUGE_BMS, fp);
354         cfseek(fp, D1_MAX_GAUGE_BMS * sizeof(bitmap_index), SEEK_CUR);
355
356         /*
357         for (i = 0; i < D1_MAX_POLYGON_MODELS; i++)
358                 Dying_modelnums[i] = cfile_read_int(fp);
359         for (i = 0; i < D1_MAX_POLYGON_MODELS; i++)
360                 Dead_modelnums[i] = cfile_read_int(fp);
361         */ cfseek(fp, D1_MAX_POLYGON_MODELS * 8, SEEK_CUR);
362
363         //bitmap_index_read_n(ObjBitmaps, D1_MAX_OBJ_BITMAPS, fp);
364         cfseek(fp, D1_MAX_OBJ_BITMAPS * sizeof(bitmap_index), SEEK_CUR);
365         for (i = 0; i < D1_MAX_OBJ_BITMAPS; i++)
366                 cfseek(fp, 2, SEEK_CUR);//ObjBitmapPtrs[i] = cfile_read_short(fp);
367
368         //player_ship_read(&only_player_ship, fp);
369         cfseek(fp, sizeof(player_ship), SEEK_CUR);
370
371         /*Num_cockpits = */ cfile_read_int(fp);
372         //bitmap_index_read_n(cockpit_bitmap, D1_MAX_COCKPIT_BITMAPS, fp);
373         cfseek(fp, D1_MAX_COCKPIT_BITMAPS * sizeof(bitmap_index), SEEK_CUR);
374
375         /*
376         cfread( Sounds, sizeof(ubyte), D1_MAX_SOUNDS, fp );
377         cfread( AltSounds, sizeof(ubyte), D1_MAX_SOUNDS, fp );
378         */cfseek(fp, D1_MAX_SOUNDS * 2, SEEK_CUR);
379
380         /*Num_total_object_types = */ cfile_read_int( fp );
381         /*
382         cfread( ObjType, sizeof(byte), D1_MAX_OBJTYPE, fp );
383         cfread( ObjId, sizeof(byte), D1_MAX_OBJTYPE, fp );
384         for (i=0; i<D1_MAX_OBJTYPE; i++ )
385                 ObjStrength[i] = cfile_read_int( fp );
386         */ cfseek(fp, D1_MAX_OBJTYPE * 6, SEEK_CUR);
387
388         /*First_multi_bitmap_num =*/ cfile_read_int(fp);
389         /*Reactors[0].n_guns = */ cfile_read_int( fp );
390         /*for (i=0; i<4; i++)
391                 cfile_read_vector(&(Reactors[0].gun_points[i]), fp);
392         for (i=0; i<4; i++)
393                 cfile_read_vector(&(Reactors[0].gun_dirs[i]), fp);
394         */cfseek(fp, 8 * 12, SEEK_CUR);
395
396         /*exit_modelnum = */ cfile_read_int(fp);
397         /*destroyed_exit_modelnum = */ cfile_read_int(fp);
398 }
399
400 //these values are the number of each item in the release of d2
401 //extra items added after the release get written in an additional hamfile
402 #define N_D2_ROBOT_TYPES                66
403 #define N_D2_ROBOT_JOINTS               1145
404 #define N_D2_POLYGON_MODELS     166
405 #define N_D2_OBJBITMAPS                 422
406 #define N_D2_OBJBITMAPPTRS              502
407 #define N_D2_WEAPON_TYPES               62
408
409 extern int Num_bitmap_files;
410 int extra_bitmap_num = 0;
411
412 void bm_free_extra_objbitmaps()
413 {
414         int i;
415
416         if (!extra_bitmap_num)
417                 extra_bitmap_num = Num_bitmap_files;
418
419         for (i = Num_bitmap_files; i < extra_bitmap_num; i++)
420         {
421                 N_ObjBitmaps--;
422                 d_free(GameBitmaps[i].bm_data);
423         }
424         extra_bitmap_num = Num_bitmap_files;
425 }
426
427 void bm_free_extra_models()
428 {
429         while (N_polygon_models > N_D2_POLYGON_MODELS)
430                 free_model(&Polygon_models[--N_polygon_models]);
431         while (N_polygon_models > exit_modelnum)
432                 free_model(&Polygon_models[--N_polygon_models]);
433 }
434
435 //type==1 means 1.1, type==2 means 1.2 (with weapons)
436 void bm_read_extra_robots(char *fname,int type)
437 {
438         CFILE *fp;
439         int t,i;
440         int version;
441
442         fp = cfopen(fname,"rb");
443
444         if (type == 2) {
445                 int sig;
446
447                 sig = cfile_read_int(fp);
448                 if (sig != MAKE_SIG('X','H','A','M'))
449                         return;
450                 version = cfile_read_int(fp);
451         }
452         else
453                 version = 0;
454
455         bm_free_extra_models();
456         bm_free_extra_objbitmaps();
457
458         //read extra weapons
459
460         t = cfile_read_int(fp);
461         N_weapon_types = N_D2_WEAPON_TYPES+t;
462         if (N_weapon_types >= MAX_WEAPON_TYPES)
463                 Error("Too many weapons (%d) in <%s>.  Max is %d.",t,fname,MAX_WEAPON_TYPES-N_D2_WEAPON_TYPES);
464         weapon_info_read_n(&Weapon_info[N_D2_WEAPON_TYPES], t, fp, 3);
465
466         //now read robot info
467
468         t = cfile_read_int(fp);
469         N_robot_types = N_D2_ROBOT_TYPES+t;
470         if (N_robot_types >= MAX_ROBOT_TYPES)
471                 Error("Too many robots (%d) in <%s>.  Max is %d.",t,fname,MAX_ROBOT_TYPES-N_D2_ROBOT_TYPES);
472         robot_info_read_n(&Robot_info[N_D2_ROBOT_TYPES], t, fp);
473
474         t = cfile_read_int(fp);
475         N_robot_joints = N_D2_ROBOT_JOINTS+t;
476         if (N_robot_joints >= MAX_ROBOT_JOINTS)
477                 Error("Too many robot joints (%d) in <%s>.  Max is %d.",t,fname,MAX_ROBOT_JOINTS-N_D2_ROBOT_JOINTS);
478         jointpos_read_n(&Robot_joints[N_D2_ROBOT_JOINTS], t, fp);
479
480         t = cfile_read_int(fp);
481         N_polygon_models = N_D2_POLYGON_MODELS+t;
482         if (N_polygon_models >= MAX_POLYGON_MODELS)
483                 Error("Too many polygon models (%d) in <%s>.  Max is %d.",t,fname,MAX_POLYGON_MODELS-N_D2_POLYGON_MODELS);
484         polymodel_read_n(&Polygon_models[N_D2_POLYGON_MODELS], t, fp);
485
486         for (i=N_D2_POLYGON_MODELS; i<N_polygon_models; i++ )
487                 polygon_model_data_read(&Polygon_models[i], fp);
488
489         for (i = N_D2_POLYGON_MODELS; i < N_polygon_models; i++)
490                 Dying_modelnums[i] = cfile_read_int(fp);
491         for (i = N_D2_POLYGON_MODELS; i < N_polygon_models; i++)
492                 Dead_modelnums[i] = cfile_read_int(fp);
493
494         t = cfile_read_int(fp);
495         if (N_D2_OBJBITMAPS+t >= MAX_OBJ_BITMAPS)
496                 Error("Too many object bitmaps (%d) in <%s>.  Max is %d.",t,fname,MAX_OBJ_BITMAPS-N_D2_OBJBITMAPS);
497         bitmap_index_read_n(&ObjBitmaps[N_D2_OBJBITMAPS], t, fp);
498
499         t = cfile_read_int(fp);
500         if (N_D2_OBJBITMAPPTRS+t >= MAX_OBJ_BITMAPS)
501                 Error("Too many object bitmap pointers (%d) in <%s>.  Max is %d.",t,fname,MAX_OBJ_BITMAPS-N_D2_OBJBITMAPPTRS);
502         for (i = N_D2_OBJBITMAPPTRS; i < (N_D2_OBJBITMAPPTRS + t); i++)
503                 ObjBitmapPtrs[i] = cfile_read_short(fp);
504
505         cfclose(fp);
506 }
507
508 extern void change_filename_extension( char *dest, char *src, char *new_ext );
509
510 int Robot_replacements_loaded = 0;
511
512 void load_robot_replacements(char *level_name)
513 {
514         CFILE *fp;
515         int t,i,j;
516         char ifile_name[FILENAME_LEN];
517
518         change_filename_extension(ifile_name, level_name, ".HXM" );
519
520         fp = cfopen(ifile_name,"rb");
521
522         if (!fp)                //no robot replacement file
523                 return;
524
525         t = cfile_read_int(fp);                 //read id "HXM!"
526         if (t!= 0x21584d48)
527                 Error("ID of HXM! file incorrect");
528
529         t = cfile_read_int(fp);                 //read version
530         if (t<1)
531                 Error("HXM! version too old (%d)",t);
532
533         t = cfile_read_int(fp);                 //read number of robots
534         for (j=0;j<t;j++) {
535                 i = cfile_read_int(fp);         //read robot number
536                 if (i<0 || i>=N_robot_types)
537                         Error("Robots number (%d) out of range in (%s).  Range = [0..%d].",i,level_name,N_robot_types-1);
538                 robot_info_read_n(&Robot_info[i], 1, fp);
539         }
540
541         t = cfile_read_int(fp);                 //read number of joints
542         for (j=0;j<t;j++) {
543                 i = cfile_read_int(fp);         //read joint number
544                 if (i<0 || i>=N_robot_joints)
545                         Error("Robots joint (%d) out of range in (%s).  Range = [0..%d].",i,level_name,N_robot_joints-1);
546                 jointpos_read_n(&Robot_joints[i], 1, fp);
547         }
548
549         t = cfile_read_int(fp);                 //read number of polygon models
550         for (j=0;j<t;j++)
551         {
552                 i = cfile_read_int(fp);         //read model number
553                 if (i<0 || i>=N_polygon_models)
554                         Error("Polygon model (%d) out of range in (%s).  Range = [0..%d].",i,level_name,N_polygon_models-1);
555                 polymodel_read(&Polygon_models[i], fp);
556
557                 free_model(&Polygon_models[i]);
558                 polygon_model_data_read(&Polygon_models[i], fp);
559
560                 Dying_modelnums[i] = cfile_read_int(fp);
561                 Dead_modelnums[i] = cfile_read_int(fp);
562         }
563
564         t = cfile_read_int(fp);                 //read number of objbitmaps
565         for (j=0;j<t;j++) {
566                 i = cfile_read_int(fp);         //read objbitmap number
567                 if (i<0 || i>=MAX_OBJ_BITMAPS)
568                         Error("Object bitmap number (%d) out of range in (%s).  Range = [0..%d].",i,level_name,MAX_OBJ_BITMAPS-1);
569                 bitmap_index_read(&ObjBitmaps[i], fp);
570         }
571
572         t = cfile_read_int(fp);                 //read number of objbitmapptrs
573         for (j=0;j<t;j++) {
574                 i = cfile_read_int(fp);         //read objbitmapptr number
575                 if (i<0 || i>=MAX_OBJ_BITMAPS)
576                         Error("Object bitmap pointer (%d) out of range in (%s).  Range = [0..%d].",i,level_name,MAX_OBJ_BITMAPS-1);
577                 ObjBitmapPtrs[i] = cfile_read_short(fp);
578         }
579
580         cfclose(fp);
581 }
582
583
584 /*
585  * Routines for loading exit models
586  *
587  * Used by d1 levels (including some add-ons), and by d2 shareware.
588  * Could potentially be used by d2 add-on levels, but only if they
589  * don't use "extra" robots...
590  */
591
592 // formerly exitmodel_bm_load_sub
593 bitmap_index read_extra_bitmap_iff( char * filename )
594 {
595         bitmap_index bitmap_num;
596         grs_bitmap * new = &GameBitmaps[extra_bitmap_num];
597         ubyte newpal[256*3];
598         int iff_error;          //reference parm to avoid warning message
599
600         bitmap_num.index = 0;
601
602         //MALLOC( new, grs_bitmap, 1 );
603         iff_error = iff_read_bitmap(filename,new,BM_LINEAR,newpal);
604         new->bm_handle=0;
605         if (iff_error != IFF_NO_ERROR)          {
606                 con_printf(CON_DEBUG, "Error loading exit model bitmap <%s> - IFF error: %s\n", filename, iff_errormsg(iff_error));
607                 return bitmap_num;
608         }
609
610         if ( iff_has_transparency )
611                 gr_remap_bitmap_good( new, newpal, iff_transparent_color, 254 );
612         else
613                 gr_remap_bitmap_good( new, newpal, -1, 254 );
614
615         new->avg_color = 0;     //compute_average_pixel(new);
616
617         bitmap_num.index = extra_bitmap_num;
618
619         GameBitmaps[extra_bitmap_num++] = *new;
620
621         //d_free( new );
622         return bitmap_num;
623 }
624
625 // formerly load_exit_model_bitmap
626 grs_bitmap *bm_load_extra_objbitmap(char *name)
627 {
628         Assert(N_ObjBitmaps < MAX_OBJ_BITMAPS);
629
630         {
631                 ObjBitmaps[N_ObjBitmaps] = read_extra_bitmap_iff(name);
632
633                 if (ObjBitmaps[N_ObjBitmaps].index == 0)
634                 {
635                         char *name2 = d_strdup(name);
636                         *strrchr(name2, '.') = '\0';
637                         ObjBitmaps[N_ObjBitmaps] = read_extra_bitmap_d1_pig(name2);
638                         d_free(name2);
639                 }
640                 if (ObjBitmaps[N_ObjBitmaps].index == 0)
641                         return NULL;
642
643                 if (GameBitmaps[ObjBitmaps[N_ObjBitmaps].index].bm_w!=64 || GameBitmaps[ObjBitmaps[N_ObjBitmaps].index].bm_h!=64)
644                         Error("Bitmap <%s> is not 64x64",name);
645                 ObjBitmapPtrs[N_ObjBitmaps] = N_ObjBitmaps;
646                 N_ObjBitmaps++;
647                 Assert(N_ObjBitmaps < MAX_OBJ_BITMAPS);
648                 return &GameBitmaps[ObjBitmaps[N_ObjBitmaps-1].index];
649         }
650 }
651
652 #ifdef OGL
653 void ogl_cache_polymodel_textures(int model_num);
654 #endif
655
656 int load_exit_models()
657 {
658         CFILE *exit_hamfile;
659         int start_num;
660
661         bm_free_extra_models();
662         bm_free_extra_objbitmaps();
663
664         start_num = N_ObjBitmaps;
665         if (!bm_load_extra_objbitmap("steel1.bbm") ||
666                 !bm_load_extra_objbitmap("rbot061.bbm") ||
667                 !bm_load_extra_objbitmap("rbot062.bbm") ||
668                 !bm_load_extra_objbitmap("steel1.bbm") ||
669                 !bm_load_extra_objbitmap("rbot061.bbm") ||
670                 !bm_load_extra_objbitmap("rbot063.bbm"))
671         {
672                 con_printf(CON_NORMAL, "Can't load exit models!\n");
673                 return 0;
674         }
675
676 #ifndef MACINTOSH
677         exit_hamfile = cfopen("exit.ham","rb");
678 #else
679         exit_hamfile = cfopen(":Data:exit.ham","rb");
680 #endif
681         if (exit_hamfile) {
682                 exit_modelnum = N_polygon_models++;
683                 destroyed_exit_modelnum = N_polygon_models++;
684                 polymodel_read(&Polygon_models[exit_modelnum], exit_hamfile);
685                 polymodel_read(&Polygon_models[destroyed_exit_modelnum], exit_hamfile);
686                 Polygon_models[exit_modelnum].first_texture = start_num;
687                 Polygon_models[destroyed_exit_modelnum].first_texture = start_num+3;
688
689                 polygon_model_data_read(&Polygon_models[exit_modelnum], exit_hamfile);
690
691                 polygon_model_data_read(&Polygon_models[destroyed_exit_modelnum], exit_hamfile);
692
693                 cfclose(exit_hamfile);
694
695         } else if (cfexist("exit01.pof") && cfexist("exit01d.pof")) {
696
697                 exit_modelnum = load_polygon_model("exit01.pof", 3, start_num, NULL);
698                 destroyed_exit_modelnum = load_polygon_model("exit01d.pof", 3, start_num + 3, NULL);
699
700 #ifdef OGL
701                 ogl_cache_polymodel_textures(exit_modelnum);
702                 ogl_cache_polymodel_textures(destroyed_exit_modelnum);
703 #endif
704         }
705         else if (cfexist(D1_PIGFILE))
706         {
707                 int offset, offset2;
708
709                 exit_hamfile = cfopen(D1_PIGFILE, "rb");
710                 switch (cfilelength(exit_hamfile)) { //total hack for loading models
711                 case D1_PIGSIZE:
712                         offset = 91848;     /* and 92582  */
713                         offset2 = 383390;   /* and 394022 */
714                         break;
715                 default:
716                 case D1_SHAREWARE_10_PIGSIZE:
717                 case D1_SHAREWARE_PIGSIZE:
718                         Int3();             /* exit models should be in .pofs */
719                 case D1_OEM_PIGSIZE:
720                 case D1_MAC_PIGSIZE:
721                 case D1_MAC_SHARE_PIGSIZE:
722                         con_printf(CON_NORMAL, "Can't load exit models!\n");
723                         return 0;
724                 }
725                 cfseek(exit_hamfile, offset, SEEK_SET);
726                 exit_modelnum = N_polygon_models++;
727                 destroyed_exit_modelnum = N_polygon_models++;
728                 polymodel_read(&Polygon_models[exit_modelnum], exit_hamfile);
729                 polymodel_read(&Polygon_models[destroyed_exit_modelnum], exit_hamfile);
730                 Polygon_models[exit_modelnum].first_texture = start_num;
731                 Polygon_models[destroyed_exit_modelnum].first_texture = start_num+3;
732
733                 cfseek(exit_hamfile, offset2, SEEK_SET);
734                 polygon_model_data_read(&Polygon_models[exit_modelnum], exit_hamfile);
735                 polygon_model_data_read(&Polygon_models[destroyed_exit_modelnum], exit_hamfile);
736
737                 cfclose(exit_hamfile);
738         } else {
739                 con_printf(CON_NORMAL, "Can't load exit models!\n");
740                 return 0;
741         }
742
743         atexit(bm_free_extra_objbitmaps);
744
745         return 1;
746 }