]> icculus.org git repositories - btb/d2x.git/blob - main/bm.c
needed for tolower()
[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         for (i = N_D2_ROBOT_TYPES; i < N_robot_types; i++)
436                 entity_add(OBJ_ROBOT, i, Robot_names[i]);
437
438         t = cfile_read_int(fp);
439         N_robot_joints = N_D2_ROBOT_JOINTS+t;
440         if (N_robot_joints >= MAX_ROBOT_JOINTS)
441                 Error("Too many robot joints (%d) in <%s>.  Max is %d.",t,fname,MAX_ROBOT_JOINTS-N_D2_ROBOT_JOINTS);
442         jointpos_read_n(&Robot_joints[N_D2_ROBOT_JOINTS], t, fp);
443
444         t = cfile_read_int(fp);
445         N_polygon_models = N_D2_POLYGON_MODELS+t;
446         if (N_polygon_models >= MAX_POLYGON_MODELS)
447                 Error("Too many polygon models (%d) in <%s>.  Max is %d.",t,fname,MAX_POLYGON_MODELS-N_D2_POLYGON_MODELS);
448         polymodel_read_n(&Polygon_models[N_D2_POLYGON_MODELS], t, fp);
449
450         for (i=N_D2_POLYGON_MODELS; i<N_polygon_models; i++ )
451                 polygon_model_data_read(&Polygon_models[i], fp);
452
453         for (i = N_D2_POLYGON_MODELS; i < N_polygon_models; i++)
454                 Dying_modelnums[i] = cfile_read_int(fp);
455         for (i = N_D2_POLYGON_MODELS; i < N_polygon_models; i++)
456                 Dead_modelnums[i] = cfile_read_int(fp);
457
458         t = cfile_read_int(fp);
459         if (N_D2_OBJBITMAPS+t >= MAX_OBJ_BITMAPS)
460                 Error("Too many object bitmaps (%d) in <%s>.  Max is %d.",t,fname,MAX_OBJ_BITMAPS-N_D2_OBJBITMAPS);
461         bitmap_index_read_n(&ObjBitmaps[N_D2_OBJBITMAPS], t, fp);
462
463         t = cfile_read_int(fp);
464         if (N_D2_OBJBITMAPPTRS+t >= MAX_OBJ_BITMAPS)
465                 Error("Too many object bitmap pointers (%d) in <%s>.  Max is %d.",t,fname,MAX_OBJ_BITMAPS-N_D2_OBJBITMAPPTRS);
466         for (i = N_D2_OBJBITMAPPTRS; i < (N_D2_OBJBITMAPPTRS + t); i++)
467                 ObjBitmapPtrs[i] = cfile_read_short(fp);
468
469         cfclose(fp);
470 }
471
472 int Robot_replacements_loaded = 0;
473
474 void load_robot_replacements(char *level_name)
475 {
476         CFILE *fp;
477         int t,i,j;
478         char ifile_name[FILENAME_LEN];
479
480         change_filename_extension(ifile_name, level_name, ".HXM" );
481
482         fp = cfopen(ifile_name,"rb");
483
484         if (!fp)                //no robot replacement file
485                 return;
486
487         t = cfile_read_int(fp);                 //read id "HXM!"
488         if (t!= 0x21584d48)
489                 Error("ID of HXM! file incorrect");
490
491         t = cfile_read_int(fp);                 //read version
492         if (t<1)
493                 Error("HXM! version too old (%d)",t);
494
495         t = cfile_read_int(fp);                 //read number of robots
496         for (j=0;j<t;j++) {
497                 i = cfile_read_int(fp);         //read robot number
498                 if (i<0 || i>=N_robot_types)
499                         Error("Robots number (%d) out of range in (%s).  Range = [0..%d].",i,level_name,N_robot_types-1);
500                 robot_info_read_n(&Robot_info[i], 1, fp);
501         }
502
503         t = cfile_read_int(fp);                 //read number of joints
504         for (j=0;j<t;j++) {
505                 i = cfile_read_int(fp);         //read joint number
506                 if (i<0 || i>=N_robot_joints)
507                         Error("Robots joint (%d) out of range in (%s).  Range = [0..%d].",i,level_name,N_robot_joints-1);
508                 jointpos_read_n(&Robot_joints[i], 1, fp);
509         }
510
511         t = cfile_read_int(fp);                 //read number of polygon models
512         for (j=0;j<t;j++)
513         {
514                 i = cfile_read_int(fp);         //read model number
515                 if (i<0 || i>=N_polygon_models)
516                         Error("Polygon model (%d) out of range in (%s).  Range = [0..%d].",i,level_name,N_polygon_models-1);
517
518                 free_model(&Polygon_models[i]);
519                 polymodel_read(&Polygon_models[i], fp);
520                 polygon_model_data_read(&Polygon_models[i], fp);
521
522                 Dying_modelnums[i] = cfile_read_int(fp);
523                 Dead_modelnums[i] = cfile_read_int(fp);
524         }
525
526         t = cfile_read_int(fp);                 //read number of objbitmaps
527         for (j=0;j<t;j++) {
528                 i = cfile_read_int(fp);         //read objbitmap number
529                 if (i<0 || i>=MAX_OBJ_BITMAPS)
530                         Error("Object bitmap number (%d) out of range in (%s).  Range = [0..%d].",i,level_name,MAX_OBJ_BITMAPS-1);
531                 bitmap_index_read(&ObjBitmaps[i], fp);
532         }
533
534         t = cfile_read_int(fp);                 //read number of objbitmapptrs
535         for (j=0;j<t;j++) {
536                 i = cfile_read_int(fp);         //read objbitmapptr number
537                 if (i<0 || i>=MAX_OBJ_BITMAPS)
538                         Error("Object bitmap pointer (%d) out of range in (%s).  Range = [0..%d].",i,level_name,MAX_OBJ_BITMAPS-1);
539                 ObjBitmapPtrs[i] = cfile_read_short(fp);
540         }
541
542         cfclose(fp);
543         Robot_replacements_loaded = 1;
544 }
545
546
547 /*
548  * Routines for loading exit models
549  *
550  * Used by d1 levels (including some add-ons), and by d2 shareware.
551  * Could potentially be used by d2 add-on levels, but only if they
552  * don't use "extra" robots...
553  */
554
555 // formerly exitmodel_bm_load_sub
556 bitmap_index read_extra_bitmap_iff( char * filename )
557 {
558         bitmap_index bitmap_num;
559         grs_bitmap * new = &GameBitmaps[extra_bitmap_num];
560         ubyte newpal[256*3];
561         int iff_error;          //reference parm to avoid warning message
562
563         bitmap_num.index = 0;
564
565         //MALLOC( new, grs_bitmap, 1 );
566         iff_error = iff_read_bitmap(filename,new,BM_LINEAR,newpal);
567         new->bm_handle=0;
568         if (iff_error != IFF_NO_ERROR)          {
569                 con_printf(CON_DEBUG, "Error loading exit model bitmap <%s> - IFF error: %s\n", filename, iff_errormsg(iff_error));
570                 return bitmap_num;
571         }
572
573         if ( iff_has_transparency )
574                 gr_remap_bitmap_good( new, newpal, iff_transparent_color, 254 );
575         else
576                 gr_remap_bitmap_good( new, newpal, -1, 254 );
577
578         new->avg_color = 0;     //compute_average_pixel(new);
579
580         bitmap_num.index = extra_bitmap_num;
581
582         GameBitmaps[extra_bitmap_num++] = *new;
583
584         //d_free( new );
585         return bitmap_num;
586 }
587
588 // formerly load_exit_model_bitmap
589 grs_bitmap *bm_load_extra_objbitmap(char *name)
590 {
591         Assert(N_ObjBitmaps < MAX_OBJ_BITMAPS);
592
593         {
594                 ObjBitmaps[N_ObjBitmaps] = read_extra_bitmap_iff(name);
595
596                 if (ObjBitmaps[N_ObjBitmaps].index == 0)
597                 {
598                         char *name2 = d_strdup(name);
599                         *strrchr(name2, '.') = '\0';
600                         ObjBitmaps[N_ObjBitmaps] = read_extra_bitmap_d1_pig(name2);
601                         d_free(name2);
602                 }
603                 if (ObjBitmaps[N_ObjBitmaps].index == 0)
604                         return NULL;
605
606                 if (GameBitmaps[ObjBitmaps[N_ObjBitmaps].index].bm_w!=64 || GameBitmaps[ObjBitmaps[N_ObjBitmaps].index].bm_h!=64)
607                         Error("Bitmap <%s> is not 64x64",name);
608                 ObjBitmapPtrs[N_ObjBitmaps] = N_ObjBitmaps;
609                 N_ObjBitmaps++;
610                 Assert(N_ObjBitmaps < MAX_OBJ_BITMAPS);
611                 return &GameBitmaps[ObjBitmaps[N_ObjBitmaps-1].index];
612         }
613 }
614
615 #ifdef OGL
616 void ogl_cache_polymodel_textures(int model_num);
617 #endif
618
619 int load_exit_models()
620 {
621         CFILE *exit_hamfile;
622         int start_num;
623
624         bm_free_extra_models();
625         bm_free_extra_objbitmaps();
626
627         start_num = N_ObjBitmaps;
628         if (!bm_load_extra_objbitmap("steel1.bbm") ||
629                 !bm_load_extra_objbitmap("rbot061.bbm") ||
630                 !bm_load_extra_objbitmap("rbot062.bbm") ||
631                 !bm_load_extra_objbitmap("steel1.bbm") ||
632                 !bm_load_extra_objbitmap("rbot061.bbm") ||
633                 !bm_load_extra_objbitmap("rbot063.bbm"))
634         {
635                 con_printf(CON_NORMAL, "Can't load exit models!\n");
636                 return 0;
637         }
638
639         exit_hamfile = cfopen("exit.ham","rb");
640         if (exit_hamfile) {
641                 exit_modelnum = N_polygon_models++;
642                 destroyed_exit_modelnum = N_polygon_models++;
643                 polymodel_read(&Polygon_models[exit_modelnum], exit_hamfile);
644                 polymodel_read(&Polygon_models[destroyed_exit_modelnum], exit_hamfile);
645                 Polygon_models[exit_modelnum].first_texture = start_num;
646                 Polygon_models[destroyed_exit_modelnum].first_texture = start_num+3;
647
648                 polygon_model_data_read(&Polygon_models[exit_modelnum], exit_hamfile);
649
650                 polygon_model_data_read(&Polygon_models[destroyed_exit_modelnum], exit_hamfile);
651
652                 cfclose(exit_hamfile);
653
654         } else if (cfexist("exit01.pof") && cfexist("exit01d.pof")) {
655
656                 exit_modelnum = load_polygon_model("exit01.pof", 3, start_num, NULL);
657                 destroyed_exit_modelnum = load_polygon_model("exit01d.pof", 3, start_num + 3, NULL);
658
659 #ifdef OGL
660                 ogl_cache_polymodel_textures(exit_modelnum);
661                 ogl_cache_polymodel_textures(destroyed_exit_modelnum);
662 #endif
663         }
664         else if (cfexist(D1_PIGFILE))
665         {
666                 int offset, offset2;
667                 int hamsize;
668
669                 exit_hamfile = cfopen(D1_PIGFILE, "rb");
670                 hamsize = cfilelength(exit_hamfile);
671                 switch (hamsize) { //total hack for loading models
672                 case D1_PIGSIZE:
673                         offset = 91848;     /* and 92582  */
674                         offset2 = 383390;   /* and 394022 */
675                         break;
676                 default:
677                 case D1_SHARE_BIG_PIGSIZE:
678                 case D1_SHARE_10_PIGSIZE:
679                 case D1_SHARE_PIGSIZE:
680                 case D1_10_BIG_PIGSIZE:
681                 case D1_10_PIGSIZE:
682                         Int3();             /* exit models should be in .pofs */
683                 case D1_OEM_PIGSIZE:
684                 case D1_MAC_PIGSIZE:
685                 case D1_MAC_SHARE_PIGSIZE:
686                         con_printf(CON_NORMAL, "Can't load exit models!\n");
687                         return 0;
688                 }
689                 cfseek(exit_hamfile, offset, SEEK_SET);
690                 exit_modelnum = N_polygon_models++;
691                 destroyed_exit_modelnum = N_polygon_models++;
692                 polymodel_read(&Polygon_models[exit_modelnum], exit_hamfile);
693                 polymodel_read(&Polygon_models[destroyed_exit_modelnum], exit_hamfile);
694                 Polygon_models[exit_modelnum].first_texture = start_num;
695                 Polygon_models[destroyed_exit_modelnum].first_texture = start_num+3;
696
697                 cfseek(exit_hamfile, offset2, SEEK_SET);
698                 polygon_model_data_read(&Polygon_models[exit_modelnum], exit_hamfile);
699                 polygon_model_data_read(&Polygon_models[destroyed_exit_modelnum], exit_hamfile);
700
701                 cfclose(exit_hamfile);
702         } else {
703                 con_printf(CON_NORMAL, "Can't load exit models!\n");
704                 return 0;
705         }
706
707         atexit(bm_free_extra_objbitmaps);
708
709         return 1;
710 }