]> icculus.org git repositories - btb/d2x.git/blob - main/bm.c
comments
[btb/d2x.git] / main / bm.c
1 /* $Id: bm.c,v 1.22 2003-03-19 22:44:15 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
22 #ifdef HAVE_CONFIG_H
23 #include <conf.h>
24 #endif
25
26 #include <stdio.h>
27 #include <stdlib.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 byte    ObjType[MAX_OBJTYPE];
67 byte    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 extern int Num_bitmap_files;
139 int extra_bitmap_num;
140
141 bitmap_index exitmodel_bm_load_sub( char * filename )
142 {
143         bitmap_index bitmap_num;
144         grs_bitmap * new = &GameBitmaps[extra_bitmap_num];
145         ubyte newpal[256*3];
146         int iff_error;          //reference parm to avoid warning message
147
148         bitmap_num.index = 0;
149
150         //MALLOC( new, grs_bitmap, 1 );
151         iff_error = iff_read_bitmap(filename,new,BM_LINEAR,newpal);
152         new->bm_handle=0;
153         if (iff_error != IFF_NO_ERROR)          {
154                 Error("Error loading exit model bitmap <%s> - IFF error: %s",filename,iff_errormsg(iff_error));
155         }
156
157         if ( iff_has_transparency )
158                 gr_remap_bitmap_good( new, newpal, iff_transparent_color, 254 );
159         else
160                 gr_remap_bitmap_good( new, newpal, -1, 254 );
161
162         new->avg_color = 0;     //compute_average_pixel(new);
163
164         bitmap_num.index = extra_bitmap_num;
165
166         GameBitmaps[extra_bitmap_num++] = *new;
167
168         //d_free( new );
169         return bitmap_num;
170 }
171
172 grs_bitmap *load_exit_model_bitmap(char *name)
173 {
174         Assert(N_ObjBitmaps < MAX_OBJ_BITMAPS);
175
176         {
177                 ObjBitmaps[N_ObjBitmaps] = exitmodel_bm_load_sub(name);
178                 if (GameBitmaps[ObjBitmaps[N_ObjBitmaps].index].bm_w!=64 || GameBitmaps[ObjBitmaps[N_ObjBitmaps].index].bm_h!=64)
179                         Error("Bitmap <%s> is not 64x64",name);
180                 ObjBitmapPtrs[N_ObjBitmaps] = N_ObjBitmaps;
181                 N_ObjBitmaps++;
182                 Assert(N_ObjBitmaps < MAX_OBJ_BITMAPS);
183                 return &GameBitmaps[ObjBitmaps[N_ObjBitmaps-1].index];
184         }
185 }
186
187 void
188 free_exit_model_data()
189 {
190         int i;
191
192         for (i = Num_bitmap_files; i < extra_bitmap_num; i++)
193                 d_free(GameBitmaps[i].bm_data);
194         extra_bitmap_num = Num_bitmap_files;
195 }
196
197
198 #ifdef OGL
199 void ogl_cache_polymodel_textures(int model_num);
200 #endif
201
202 int load_exit_models()
203 {
204         CFILE *exit_hamfile;
205         int start_num;
206
207         if (!cfexist("steel1.bbm") ||
208             !cfexist("rbot061.bbm") ||
209             !cfexist("rbot062.bbm") ||
210             !cfexist("rbot063.bbm")) {
211                 Warning("Can't load exit models!\n");
212                 return 0;
213         }
214
215         start_num = N_ObjBitmaps;
216         extra_bitmap_num = Num_bitmap_files;
217         load_exit_model_bitmap("steel1.bbm");
218         load_exit_model_bitmap("rbot061.bbm");
219         load_exit_model_bitmap("rbot062.bbm");
220
221         load_exit_model_bitmap("steel1.bbm");
222         load_exit_model_bitmap("rbot061.bbm");
223         load_exit_model_bitmap("rbot063.bbm");
224
225         if (cfexist("exit.ham")) {
226 #ifndef MACINTOSH
227                 exit_hamfile = cfopen("exit.ham","rb");
228 #else
229                 exit_hamfile = cfopen(":Data:exit.ham","rb");
230 #endif
231
232                 exit_modelnum = N_polygon_models++;
233                 destroyed_exit_modelnum = N_polygon_models++;
234                 polymodel_read(&Polygon_models[exit_modelnum], exit_hamfile);
235                 polymodel_read(&Polygon_models[destroyed_exit_modelnum], exit_hamfile);
236                 Polygon_models[exit_modelnum].first_texture = start_num;
237                 Polygon_models[destroyed_exit_modelnum].first_texture = start_num+3;
238
239                 polygon_model_data_read(&Polygon_models[exit_modelnum], exit_hamfile);
240
241                 polygon_model_data_read(&Polygon_models[destroyed_exit_modelnum], exit_hamfile);
242
243                 cfclose(exit_hamfile);
244
245         } else if (cfexist("exit01.pof") && cfexist("exit01d.pof")) {
246
247                 exit_modelnum = load_polygon_model("exit01.pof", 3, start_num, NULL);
248                 destroyed_exit_modelnum = load_polygon_model("exit01d.pof", 3, start_num + 3, NULL);
249
250 #ifdef OGL
251                 ogl_cache_polymodel_textures(exit_modelnum);
252                 ogl_cache_polymodel_textures(destroyed_exit_modelnum);
253 #endif
254
255         } else {
256                 Warning("Can't load exit models!\n");
257                 return 0;
258         }
259
260         return 1;
261 }
262
263
264 //-----------------------------------------------------------------
265 // Read data from piggy.
266 // This is called when the editor is OUT.
267 // If editor is in, bm_init_use_table() is called.
268 int bm_init()
269 {
270         init_polygon_models();
271         if (! piggy_init())                             // This calls bm_read_all
272                 Error("Cannot open pig and/or ham file");
273
274         piggy_read_sounds();
275
276         init_endlevel();                //this is in bm_init_use_tbl(), so I gues it goes here
277
278         return 0;
279 }
280
281 void bm_read_all(CFILE * fp)
282 {
283         int i,t;
284
285         NumTextures = cfile_read_int(fp);
286         bitmap_index_read_n(Textures, NumTextures, fp );
287         tmap_info_read_n(TmapInfo, NumTextures, fp);
288
289         t = cfile_read_int(fp);
290         cfread( Sounds, sizeof(ubyte), t, fp );
291         cfread( AltSounds, sizeof(ubyte), t, fp );
292
293         Num_vclips = cfile_read_int(fp);
294         vclip_read_n(Vclip, Num_vclips, fp);
295
296         Num_effects = cfile_read_int(fp);
297         eclip_read_n(Effects, Num_effects, fp);
298
299         Num_wall_anims = cfile_read_int(fp);
300         wclip_read_n(WallAnims, Num_wall_anims, fp);
301
302         N_robot_types = cfile_read_int(fp);
303         robot_info_read_n(Robot_info, N_robot_types, fp);
304
305         N_robot_joints = cfile_read_int(fp);
306         jointpos_read_n(Robot_joints, N_robot_joints, fp);
307
308         N_weapon_types = cfile_read_int(fp);
309         weapon_info_read_n(Weapon_info, N_weapon_types, fp, Piggy_hamfile_version);
310
311         N_powerup_types = cfile_read_int(fp);
312         powerup_type_info_read_n(Powerup_info, N_powerup_types, fp);
313
314         N_polygon_models = cfile_read_int(fp);
315         polymodel_read_n(Polygon_models, N_polygon_models, fp);
316
317         for (i=0; i<N_polygon_models; i++ )
318                 polygon_model_data_read(&Polygon_models[i], fp);
319
320         for (i = 0; i < N_polygon_models; i++)
321                 Dying_modelnums[i] = cfile_read_int(fp);
322         for (i = 0; i < N_polygon_models; i++)
323                 Dead_modelnums[i] = cfile_read_int(fp);
324
325         t = cfile_read_int(fp);
326         bitmap_index_read_n(Gauges, t, fp);
327         bitmap_index_read_n(Gauges_hires, t, fp);
328
329         N_ObjBitmaps = cfile_read_int(fp);
330         bitmap_index_read_n(ObjBitmaps, N_ObjBitmaps, fp);
331         for (i = 0; i < N_ObjBitmaps; i++)
332                 ObjBitmapPtrs[i] = cfile_read_short(fp);
333
334         player_ship_read(&only_player_ship, fp);
335
336         Num_cockpits = cfile_read_int(fp);
337         bitmap_index_read_n(cockpit_bitmap, Num_cockpits, fp);
338
339 //@@    cfread( &Num_total_object_types, sizeof(int), 1, fp );
340 //@@    cfread( ObjType, sizeof(byte), Num_total_object_types, fp );
341 //@@    cfread( ObjId, sizeof(byte), Num_total_object_types, fp );
342 //@@    cfread( ObjStrength, sizeof(fix), Num_total_object_types, fp );
343
344         First_multi_bitmap_num = cfile_read_int(fp);
345
346         Num_reactors = cfile_read_int(fp);
347         reactor_read_n(Reactors, Num_reactors, fp);
348
349         Marker_model_num = cfile_read_int(fp);
350
351         //@@cfread( &N_controlcen_guns, sizeof(int), 1, fp );
352         //@@cfread( controlcen_gun_points, sizeof(vms_vector), N_controlcen_guns, fp );
353         //@@cfread( controlcen_gun_dirs, sizeof(vms_vector), N_controlcen_guns, fp );
354
355         if (Piggy_hamfile_version < 3) {
356                 exit_modelnum = cfile_read_int(fp);
357                 destroyed_exit_modelnum = cfile_read_int(fp);
358         }
359
360 }
361
362 #define D1_MAX_TEXTURES 800
363 #define D1_MAX_SOUNDS 250
364 #define D1_MAX_VCLIPS 70
365 #define D1_MAX_EFFECTS 60
366 #define D1_MAX_WALL_ANIMS 30
367 #define D1_MAX_ROBOT_TYPES 30
368 #define D1_MAX_ROBOT_JOINTS 600
369 #define D1_MAX_WEAPON_TYPES 30
370 #define D1_MAX_POWERUP_TYPES 29
371 #define D1_MAX_GAUGE_BMS 80
372 #define D1_MAX_OBJ_BITMAPS 210
373 #define D1_MAX_COCKPIT_BITMAPS 4
374 #define D1_MAX_OBJTYPE 100
375 #define D1_MAX_POLYGON_MODELS 85
376
377 #define D1_TMAP_INFO_SIZE 26
378 #define D1_VCLIP_SIZE 66
379 #define D1_ROBOT_INFO_SIZE 486
380 #define D1_WEAPON_INFO_SIZE 115
381
382 #define D1_LAST_STATIC_TMAP_NUM 324
383 short *d2_Textures_backup = NULL;
384
385 void undo_bm_read_all_d1() {
386         if (d2_Textures_backup) {
387                 int i;
388                 for (i = 0; i < D1_LAST_STATIC_TMAP_NUM; i++)
389                         Textures[i].index = d2_Textures_backup[i];
390                 d_free(d2_Textures_backup);
391                 d2_Textures_backup = NULL;
392         }
393 }
394
395 /*
396  * used by piggy_d1_init to read in descent 1 pigfile
397  */
398 void bm_read_all_d1(CFILE * fp)
399 {
400         int i;
401
402         /*NumTextures = */ cfile_read_int(fp);
403         //bitmap_index_read_n(Textures, D1_MAX_TEXTURES, fp );
404         //for (i = 0; i < D1_MAX_TEXTURES; i++)
405         //      Textures[i].index = cfile_read_short(fp) + 600;  
406         //cfseek(fp, D1_MAX_TEXTURES * sizeof(short), SEEK_CUR);
407         MALLOC(d2_Textures_backup, short, D1_LAST_STATIC_TMAP_NUM);
408         for (i = 0; i < D1_LAST_STATIC_TMAP_NUM; i++) {
409                 d2_Textures_backup[i] = Textures[i].index;
410                 Textures[i].index = cfile_read_short(fp) + 521;
411         }
412         cfseek(fp, (D1_MAX_TEXTURES - D1_LAST_STATIC_TMAP_NUM) * sizeof(short), SEEK_CUR);
413
414         //tmap_info_read_n_d1(TmapInfo, D1_MAX_TEXTURES, fp);
415         cfseek(fp, D1_MAX_TEXTURES * D1_TMAP_INFO_SIZE, SEEK_CUR);
416
417         /*
418         cfread( Sounds, sizeof(ubyte), D1_MAX_SOUNDS, fp );
419         cfread( AltSounds, sizeof(ubyte), D1_MAX_SOUNDS, fp );
420         */cfseek(fp, D1_MAX_SOUNDS * 2, SEEK_CUR);
421
422         /*Num_vclips = */ cfile_read_int(fp);
423         //vclip_read_n(Vclip, D1_MAX_VCLIPS, fp);
424         cfseek(fp, D1_MAX_VCLIPS * D1_VCLIP_SIZE, SEEK_CUR);
425
426         /*
427         Num_effects = cfile_read_int(fp);
428         eclip_read_n(Effects, D1_MAX_EFFECTS, fp);
429
430         Num_wall_anims = cfile_read_int(fp);
431         wclip_read_n_d1(WallAnims, D1_MAX_WALL_ANIMS, fp);
432         */
433
434         /*
435         N_robot_types = cfile_read_int(fp);
436         //robot_info_read_n(Robot_info, D1_MAX_ROBOT_TYPES, fp);
437         cfseek(fp, D1_MAX_ROBOT_TYPES * D1_ROBOT_INFO_SIZE, SEEK_CUR);
438
439         N_robot_joints = cfile_read_int(fp);
440         jointpos_read_n(Robot_joints, D1_MAX_ROBOT_JOINTS, fp);
441
442         N_weapon_types = cfile_read_int(fp);
443         //weapon_info_read_n(Weapon_info, D1_MAX_WEAPON_TYPES, fp, Piggy_hamfile_version);
444         cfseek(fp, D1_MAX_WEAPON_TYPES * D1_WEAPON_INFO_SIZE, SEEK_CUR);
445
446         N_powerup_types = cfile_read_int(fp);
447         powerup_type_info_read_n(Powerup_info, D1_MAX_POWERUP_TYPES, fp);
448         */
449
450         /* in the following code are bugs, solved by hack
451         N_polygon_models = cfile_read_int(fp);
452         polymodel_read_n(Polygon_models, N_polygon_models, fp);
453         for (i=0; i<N_polygon_models; i++ )
454                 polygon_model_data_read(&Polygon_models[i], fp);
455         */cfseek(fp, 521490-160, SEEK_SET); // OK, I admit, this is a dirty hack
456         //bitmap_index_read_n(Gauges, D1_MAX_GAUGE_BMS, fp);
457         cfseek(fp, D1_MAX_GAUGE_BMS * sizeof(bitmap_index), SEEK_CUR);
458
459         /*
460         for (i = 0; i < D1_MAX_POLYGON_MODELS; i++)
461                 Dying_modelnums[i] = cfile_read_int(fp);
462         for (i = 0; i < D1_MAX_POLYGON_MODELS; i++)
463                 Dead_modelnums[i] = cfile_read_int(fp);
464         */ cfseek(fp, D1_MAX_POLYGON_MODELS * 8, SEEK_CUR);
465
466         //bitmap_index_read_n(ObjBitmaps, D1_MAX_OBJ_BITMAPS, fp);
467         cfseek(fp, D1_MAX_OBJ_BITMAPS * sizeof(bitmap_index), SEEK_CUR);
468         for (i = 0; i < D1_MAX_OBJ_BITMAPS; i++)
469                 cfseek(fp, 2, SEEK_CUR);//ObjBitmapPtrs[i] = cfile_read_short(fp);
470
471         //player_ship_read(&only_player_ship, fp);
472         cfseek(fp, sizeof(player_ship), SEEK_CUR);
473
474         /*Num_cockpits = */ cfile_read_int(fp);
475         //bitmap_index_read_n(cockpit_bitmap, D1_MAX_COCKPIT_BITMAPS, fp);
476         cfseek(fp, D1_MAX_COCKPIT_BITMAPS * sizeof(bitmap_index), SEEK_CUR);
477
478         /*
479         cfread( Sounds, sizeof(ubyte), D1_MAX_SOUNDS, fp );
480         cfread( AltSounds, sizeof(ubyte), D1_MAX_SOUNDS, fp );
481         */cfseek(fp, D1_MAX_SOUNDS * 2, SEEK_CUR);
482
483         /*Num_total_object_types = */ cfile_read_int( fp );
484         /*
485         cfread( ObjType, sizeof(byte), D1_MAX_OBJTYPE, fp );
486         cfread( ObjId, sizeof(byte), D1_MAX_OBJTYPE, fp );
487         for (i=0; i<D1_MAX_OBJTYPE; i++ )
488                 ObjStrength[i] = cfile_read_int( fp );
489         */ cfseek(fp, D1_MAX_OBJTYPE * 6, SEEK_CUR);
490
491         /*First_multi_bitmap_num =*/ cfile_read_int(fp);
492         /*Reactors[0].n_guns = */ cfile_read_int( fp );
493         /*for (i=0; i<4; i++)
494                 cfile_read_vector(&(Reactors[0].gun_points[i]), fp);
495         for (i=0; i<4; i++)
496                 cfile_read_vector(&(Reactors[0].gun_dirs[i]), fp);
497         */cfseek(fp, 8 * 12, SEEK_CUR);
498
499         /*exit_modelnum = */ cfile_read_int(fp);
500         /*destroyed_exit_modelnum = */ cfile_read_int(fp);
501 }
502
503 //these values are the number of each item in the release of d2
504 //extra items added after the release get written in an additional hamfile
505 #define N_D2_ROBOT_TYPES                66
506 #define N_D2_ROBOT_JOINTS               1145
507 #define N_D2_POLYGON_MODELS             166
508 #define N_D2_OBJBITMAPS                 422
509 #define N_D2_OBJBITMAPPTRS              502
510 #define N_D2_WEAPON_TYPES               62
511
512 //type==1 means 1.1, type==2 means 1.2 (with weapons)
513 void bm_read_extra_robots(char *fname,int type)
514 {
515         CFILE *fp;
516         int t,i;
517         int version;
518
519         fp = cfopen(fname,"rb");
520
521         if (type == 2) {
522                 int sig;
523
524                 sig = cfile_read_int(fp);
525                 if (sig != MAKE_SIG('X','H','A','M'))
526                         return;
527                 version = cfile_read_int(fp);
528         }
529         else
530                 version = 0;
531
532         //read extra weapons
533
534         t = cfile_read_int(fp);
535         N_weapon_types = N_D2_WEAPON_TYPES+t;
536         if (N_weapon_types >= MAX_WEAPON_TYPES)
537                 Error("Too many weapons (%d) in <%s>.  Max is %d.",t,fname,MAX_WEAPON_TYPES-N_D2_WEAPON_TYPES);
538         weapon_info_read_n(&Weapon_info[N_D2_WEAPON_TYPES], t, fp, 3);
539
540         //now read robot info
541
542         t = cfile_read_int(fp);
543         N_robot_types = N_D2_ROBOT_TYPES+t;
544         if (N_robot_types >= MAX_ROBOT_TYPES)
545                 Error("Too many robots (%d) in <%s>.  Max is %d.",t,fname,MAX_ROBOT_TYPES-N_D2_ROBOT_TYPES);
546         robot_info_read_n(&Robot_info[N_D2_ROBOT_TYPES], t, fp);
547
548         t = cfile_read_int(fp);
549         N_robot_joints = N_D2_ROBOT_JOINTS+t;
550         if (N_robot_joints >= MAX_ROBOT_JOINTS)
551                 Error("Too many robot joints (%d) in <%s>.  Max is %d.",t,fname,MAX_ROBOT_JOINTS-N_D2_ROBOT_JOINTS);
552         jointpos_read_n(&Robot_joints[N_D2_ROBOT_JOINTS], t, fp);
553
554         t = cfile_read_int(fp);
555         N_polygon_models = N_D2_POLYGON_MODELS+t;
556         if (N_polygon_models >= MAX_POLYGON_MODELS)
557                 Error("Too many polygon models (%d) in <%s>.  Max is %d.",t,fname,MAX_POLYGON_MODELS-N_D2_POLYGON_MODELS);
558         polymodel_read_n(&Polygon_models[N_D2_POLYGON_MODELS], t, fp);
559
560         for (i=N_D2_POLYGON_MODELS; i<N_polygon_models; i++ )
561         {
562                 polygon_model_data_read(&Polygon_models[i], fp);
563         }
564
565         for (i = N_D2_POLYGON_MODELS; i < N_polygon_models; i++)
566                 Dying_modelnums[i] = cfile_read_int(fp);
567         for (i = N_D2_POLYGON_MODELS; i < N_polygon_models; i++)
568                 Dead_modelnums[i] = cfile_read_int(fp);
569
570         t = cfile_read_int(fp);
571         if (N_D2_OBJBITMAPS+t >= MAX_OBJ_BITMAPS)
572                 Error("Too many object bitmaps (%d) in <%s>.  Max is %d.",t,fname,MAX_OBJ_BITMAPS-N_D2_OBJBITMAPS);
573         bitmap_index_read_n(&ObjBitmaps[N_D2_OBJBITMAPS], t, fp);
574
575         t = cfile_read_int(fp);
576         if (N_D2_OBJBITMAPPTRS+t >= MAX_OBJ_BITMAPS)
577                 Error("Too many object bitmap pointers (%d) in <%s>.  Max is %d.",t,fname,MAX_OBJ_BITMAPS-N_D2_OBJBITMAPPTRS);
578         for (i = N_D2_OBJBITMAPPTRS; i < (N_D2_OBJBITMAPPTRS + t); i++)
579                 ObjBitmapPtrs[i] = cfile_read_short(fp);
580
581         cfclose(fp);
582 }
583
584 extern void change_filename_extension( char *dest, char *src, char *new_ext );
585
586 int Robot_replacements_loaded = 0;
587
588 void load_robot_replacements(char *level_name)
589 {
590         CFILE *fp;
591         int t,i,j;
592         char ifile_name[FILENAME_LEN];
593
594         change_filename_extension(ifile_name, level_name, ".HXM" );
595
596         fp = cfopen(ifile_name,"rb");
597
598         if (!fp)                //no robot replacement file
599                 return;
600
601         t = cfile_read_int(fp);                 //read id "HXM!"
602         if (t!= 0x21584d48)
603                 Error("ID of HXM! file incorrect");
604
605         t = cfile_read_int(fp);                 //read version
606         if (t<1)
607                 Error("HXM! version too old (%d)",t);
608
609         t = cfile_read_int(fp);                 //read number of robots
610         for (j=0;j<t;j++) {
611                 i = cfile_read_int(fp);         //read robot number
612                 if (i<0 || i>=N_robot_types)
613                         Error("Robots number (%d) out of range in (%s).  Range = [0..%d].",i,level_name,N_robot_types-1);
614                 robot_info_read_n(&Robot_info[i], 1, fp);
615         }
616
617         t = cfile_read_int(fp);                 //read number of joints
618         for (j=0;j<t;j++) {
619                 i = cfile_read_int(fp);         //read joint number
620                 if (i<0 || i>=N_robot_joints)
621                         Error("Robots joint (%d) out of range in (%s).  Range = [0..%d].",i,level_name,N_robot_joints-1);
622                 jointpos_read_n(&Robot_joints[i], 1, fp);
623         }
624
625         t = cfile_read_int(fp);                 //read number of polygon models
626         for (j=0;j<t;j++)
627         {
628                 i = cfile_read_int(fp);         //read model number
629                 if (i<0 || i>=N_polygon_models)
630                         Error("Polygon model (%d) out of range in (%s).  Range = [0..%d].",i,level_name,N_polygon_models-1);
631                 polymodel_read(&Polygon_models[i], fp);
632
633                 d_free(Polygon_models[i].model_data);
634                 polygon_model_data_read(&Polygon_models[i], fp);
635
636                 Dying_modelnums[i] = cfile_read_int(fp);
637                 Dead_modelnums[i] = cfile_read_int(fp);
638         }
639
640         t = cfile_read_int(fp);                 //read number of objbitmaps
641         for (j=0;j<t;j++) {
642                 i = cfile_read_int(fp);         //read objbitmap number
643                 if (i<0 || i>=MAX_OBJ_BITMAPS)
644                         Error("Object bitmap number (%d) out of range in (%s).  Range = [0..%d].",i,level_name,MAX_OBJ_BITMAPS-1);
645                 bitmap_index_read(&ObjBitmaps[i], fp);
646         }
647
648         t = cfile_read_int(fp);                 //read number of objbitmapptrs
649         for (j=0;j<t;j++) {
650                 i = cfile_read_int(fp);         //read objbitmapptr number
651                 if (i<0 || i>=MAX_OBJ_BITMAPS)
652                         Error("Object bitmap pointer (%d) out of range in (%s).  Range = [0..%d].",i,level_name,MAX_OBJ_BITMAPS-1);
653                 ObjBitmapPtrs[i] = cfile_read_short(fp);
654         }
655
656         cfclose(fp);
657 }