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