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