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