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