]> icculus.org git repositories - btb/d2x.git/blob - main/bm.c
move PhysicsFS initialisation, search path setup and argument reading to physfsx.h
[btb/d2x.git] / main / bm.c
1 /* $Id: bm.c,v 1.50 2006-02-26 02:29:06 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 int Robot_replacements_loaded = 0;
494
495 void load_robot_replacements(char *level_name)
496 {
497         CFILE *fp;
498         int t,i,j;
499         char ifile_name[FILENAME_LEN];
500
501         change_filename_extension(ifile_name, level_name, ".HXM" );
502
503         fp = cfopen(ifile_name,"rb");
504
505         if (!fp)                //no robot replacement file
506                 return;
507
508         t = cfile_read_int(fp);                 //read id "HXM!"
509         if (t!= 0x21584d48)
510                 Error("ID of HXM! file incorrect");
511
512         t = cfile_read_int(fp);                 //read version
513         if (t<1)
514                 Error("HXM! version too old (%d)",t);
515
516         t = cfile_read_int(fp);                 //read number of robots
517         for (j=0;j<t;j++) {
518                 i = cfile_read_int(fp);         //read robot number
519                 if (i<0 || i>=N_robot_types)
520                         Error("Robots number (%d) out of range in (%s).  Range = [0..%d].",i,level_name,N_robot_types-1);
521                 robot_info_read_n(&Robot_info[i], 1, fp);
522         }
523
524         t = cfile_read_int(fp);                 //read number of joints
525         for (j=0;j<t;j++) {
526                 i = cfile_read_int(fp);         //read joint number
527                 if (i<0 || i>=N_robot_joints)
528                         Error("Robots joint (%d) out of range in (%s).  Range = [0..%d].",i,level_name,N_robot_joints-1);
529                 jointpos_read_n(&Robot_joints[i], 1, fp);
530         }
531
532         t = cfile_read_int(fp);                 //read number of polygon models
533         for (j=0;j<t;j++)
534         {
535                 i = cfile_read_int(fp);         //read model number
536                 if (i<0 || i>=N_polygon_models)
537                         Error("Polygon model (%d) out of range in (%s).  Range = [0..%d].",i,level_name,N_polygon_models-1);
538
539                 free_model(&Polygon_models[i]);
540                 polymodel_read(&Polygon_models[i], fp);
541                 polygon_model_data_read(&Polygon_models[i], fp);
542
543                 Dying_modelnums[i] = cfile_read_int(fp);
544                 Dead_modelnums[i] = cfile_read_int(fp);
545         }
546
547         t = cfile_read_int(fp);                 //read number of objbitmaps
548         for (j=0;j<t;j++) {
549                 i = cfile_read_int(fp);         //read objbitmap number
550                 if (i<0 || i>=MAX_OBJ_BITMAPS)
551                         Error("Object bitmap number (%d) out of range in (%s).  Range = [0..%d].",i,level_name,MAX_OBJ_BITMAPS-1);
552                 bitmap_index_read(&ObjBitmaps[i], fp);
553         }
554
555         t = cfile_read_int(fp);                 //read number of objbitmapptrs
556         for (j=0;j<t;j++) {
557                 i = cfile_read_int(fp);         //read objbitmapptr number
558                 if (i<0 || i>=MAX_OBJ_BITMAPS)
559                         Error("Object bitmap pointer (%d) out of range in (%s).  Range = [0..%d].",i,level_name,MAX_OBJ_BITMAPS-1);
560                 ObjBitmapPtrs[i] = cfile_read_short(fp);
561         }
562
563         cfclose(fp);
564         Robot_replacements_loaded = 1;
565 }
566
567
568 /*
569  * Routines for loading exit models
570  *
571  * Used by d1 levels (including some add-ons), and by d2 shareware.
572  * Could potentially be used by d2 add-on levels, but only if they
573  * don't use "extra" robots...
574  */
575
576 // formerly exitmodel_bm_load_sub
577 bitmap_index read_extra_bitmap_iff( char * filename )
578 {
579         bitmap_index bitmap_num;
580         grs_bitmap * new = &GameBitmaps[extra_bitmap_num];
581         ubyte newpal[256*3];
582         int iff_error;          //reference parm to avoid warning message
583
584         bitmap_num.index = 0;
585
586         //MALLOC( new, grs_bitmap, 1 );
587         iff_error = iff_read_bitmap(filename,new,BM_LINEAR,newpal);
588         new->bm_handle=0;
589         if (iff_error != IFF_NO_ERROR)          {
590                 con_printf(CON_DEBUG, "Error loading exit model bitmap <%s> - IFF error: %s\n", filename, iff_errormsg(iff_error));
591                 return bitmap_num;
592         }
593
594         if ( iff_has_transparency )
595                 gr_remap_bitmap_good( new, newpal, iff_transparent_color, 254 );
596         else
597                 gr_remap_bitmap_good( new, newpal, -1, 254 );
598
599         new->avg_color = 0;     //compute_average_pixel(new);
600
601         bitmap_num.index = extra_bitmap_num;
602
603         GameBitmaps[extra_bitmap_num++] = *new;
604
605         //d_free( new );
606         return bitmap_num;
607 }
608
609 // formerly load_exit_model_bitmap
610 grs_bitmap *bm_load_extra_objbitmap(char *name)
611 {
612         Assert(N_ObjBitmaps < MAX_OBJ_BITMAPS);
613
614         {
615                 ObjBitmaps[N_ObjBitmaps] = read_extra_bitmap_iff(name);
616
617                 if (ObjBitmaps[N_ObjBitmaps].index == 0)
618                 {
619                         char *name2 = d_strdup(name);
620                         *strrchr(name2, '.') = '\0';
621                         ObjBitmaps[N_ObjBitmaps] = read_extra_bitmap_d1_pig(name2);
622                         d_free(name2);
623                 }
624                 if (ObjBitmaps[N_ObjBitmaps].index == 0)
625                         return NULL;
626
627                 if (GameBitmaps[ObjBitmaps[N_ObjBitmaps].index].bm_w!=64 || GameBitmaps[ObjBitmaps[N_ObjBitmaps].index].bm_h!=64)
628                         Error("Bitmap <%s> is not 64x64",name);
629                 ObjBitmapPtrs[N_ObjBitmaps] = N_ObjBitmaps;
630                 N_ObjBitmaps++;
631                 Assert(N_ObjBitmaps < MAX_OBJ_BITMAPS);
632                 return &GameBitmaps[ObjBitmaps[N_ObjBitmaps-1].index];
633         }
634 }
635
636 #ifdef OGL
637 void ogl_cache_polymodel_textures(int model_num);
638 #endif
639
640 int load_exit_models()
641 {
642         CFILE *exit_hamfile;
643         int start_num;
644
645         bm_free_extra_models();
646         bm_free_extra_objbitmaps();
647
648         start_num = N_ObjBitmaps;
649         if (!bm_load_extra_objbitmap("steel1.bbm") ||
650                 !bm_load_extra_objbitmap("rbot061.bbm") ||
651                 !bm_load_extra_objbitmap("rbot062.bbm") ||
652                 !bm_load_extra_objbitmap("steel1.bbm") ||
653                 !bm_load_extra_objbitmap("rbot061.bbm") ||
654                 !bm_load_extra_objbitmap("rbot063.bbm"))
655         {
656                 con_printf(CON_NORMAL, "Can't load exit models!\n");
657                 return 0;
658         }
659
660 #ifndef MACINTOSH
661         exit_hamfile = cfopen("exit.ham","rb");
662 #else
663         exit_hamfile = cfopen(":Data:exit.ham","rb");
664 #endif
665         if (exit_hamfile) {
666                 exit_modelnum = N_polygon_models++;
667                 destroyed_exit_modelnum = N_polygon_models++;
668                 polymodel_read(&Polygon_models[exit_modelnum], exit_hamfile);
669                 polymodel_read(&Polygon_models[destroyed_exit_modelnum], exit_hamfile);
670                 Polygon_models[exit_modelnum].first_texture = start_num;
671                 Polygon_models[destroyed_exit_modelnum].first_texture = start_num+3;
672
673                 polygon_model_data_read(&Polygon_models[exit_modelnum], exit_hamfile);
674
675                 polygon_model_data_read(&Polygon_models[destroyed_exit_modelnum], exit_hamfile);
676
677                 cfclose(exit_hamfile);
678
679         } else if (cfexist("exit01.pof") && cfexist("exit01d.pof")) {
680
681                 exit_modelnum = load_polygon_model("exit01.pof", 3, start_num, NULL);
682                 destroyed_exit_modelnum = load_polygon_model("exit01d.pof", 3, start_num + 3, NULL);
683
684 #ifdef OGL
685                 ogl_cache_polymodel_textures(exit_modelnum);
686                 ogl_cache_polymodel_textures(destroyed_exit_modelnum);
687 #endif
688         }
689         else if (cfexist(D1_PIGFILE))
690         {
691                 int offset, offset2;
692                 int hamsize;
693
694                 exit_hamfile = cfopen(D1_PIGFILE, "rb");
695                 hamsize = cfilelength(exit_hamfile);
696                 switch (hamsize) { //total hack for loading models
697                 case D1_PIGSIZE:
698                         offset = 91848;     /* and 92582  */
699                         offset2 = 383390;   /* and 394022 */
700                         break;
701                 default:
702                 case D1_SHARE_BIG_PIGSIZE:
703                 case D1_SHARE_10_PIGSIZE:
704                 case D1_SHARE_PIGSIZE:
705                 case D1_10_BIG_PIGSIZE:
706                 case D1_10_PIGSIZE:
707                         Int3();             /* exit models should be in .pofs */
708                 case D1_OEM_PIGSIZE:
709                 case D1_MAC_PIGSIZE:
710                 case D1_MAC_SHARE_PIGSIZE:
711                         con_printf(CON_NORMAL, "Can't load exit models!\n");
712                         return 0;
713                 }
714                 cfseek(exit_hamfile, offset, SEEK_SET);
715                 exit_modelnum = N_polygon_models++;
716                 destroyed_exit_modelnum = N_polygon_models++;
717                 polymodel_read(&Polygon_models[exit_modelnum], exit_hamfile);
718                 polymodel_read(&Polygon_models[destroyed_exit_modelnum], exit_hamfile);
719                 Polygon_models[exit_modelnum].first_texture = start_num;
720                 Polygon_models[destroyed_exit_modelnum].first_texture = start_num+3;
721
722                 cfseek(exit_hamfile, offset2, SEEK_SET);
723                 polygon_model_data_read(&Polygon_models[exit_modelnum], exit_hamfile);
724                 polygon_model_data_read(&Polygon_models[destroyed_exit_modelnum], exit_hamfile);
725
726                 cfclose(exit_hamfile);
727         } else {
728                 con_printf(CON_NORMAL, "Can't load exit models!\n");
729                 return 0;
730         }
731
732         atexit(bm_free_extra_objbitmaps);
733
734         return 1;
735 }