]> icculus.org git repositories - btb/d2x.git/blob - main/bm.c
fix mem leak when reading extra robots
[btb/d2x.git] / main / bm.c
1 /* $Id: bm.c,v 1.24 2003-03-23 22:39:58 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
384 // store the Textures[] array as read from the descent 2 pig.
385 short *d2_Textures_backup = NULL;
386
387 void undo_bm_read_all_d1() {
388         if (d2_Textures_backup) {
389                 int i;
390                 for (i = 0; i < D1_LAST_STATIC_TMAP_NUM; i++)
391                         Textures[i].index = d2_Textures_backup[i];
392                 d_free(d2_Textures_backup);
393                 d2_Textures_backup = NULL;
394         }
395 }
396
397 /*
398  * used by piggy_d1_init to read in descent 1 pigfile
399  */
400 void bm_read_all_d1(CFILE * fp)
401 {
402         int i;
403
404         atexit(undo_bm_read_all_d1);
405
406         /*NumTextures = */ cfile_read_int(fp);
407         //bitmap_index_read_n(Textures, D1_MAX_TEXTURES, fp );
408         //for (i = 0; i < D1_MAX_TEXTURES; i++)
409         //      Textures[i].index = cfile_read_short(fp) + 600;  
410         //cfseek(fp, D1_MAX_TEXTURES * sizeof(short), SEEK_CUR);
411         MALLOC(d2_Textures_backup, short, D1_LAST_STATIC_TMAP_NUM);
412         for (i = 0; i < D1_LAST_STATIC_TMAP_NUM; i++) {
413                 d2_Textures_backup[i] = Textures[i].index;
414                 Textures[i].index = cfile_read_short(fp) + 521;
415         }
416         cfseek(fp, (D1_MAX_TEXTURES - D1_LAST_STATIC_TMAP_NUM) * sizeof(short), SEEK_CUR);
417
418         //tmap_info_read_n_d1(TmapInfo, D1_MAX_TEXTURES, fp);
419         cfseek(fp, D1_MAX_TEXTURES * D1_TMAP_INFO_SIZE, SEEK_CUR);
420
421         /*
422         cfread( Sounds, sizeof(ubyte), D1_MAX_SOUNDS, fp );
423         cfread( AltSounds, sizeof(ubyte), D1_MAX_SOUNDS, fp );
424         */cfseek(fp, D1_MAX_SOUNDS * 2, SEEK_CUR);
425
426         /*Num_vclips = */ cfile_read_int(fp);
427         //vclip_read_n(Vclip, D1_MAX_VCLIPS, fp);
428         cfseek(fp, D1_MAX_VCLIPS * D1_VCLIP_SIZE, SEEK_CUR);
429
430         /*
431         Num_effects = cfile_read_int(fp);
432         eclip_read_n(Effects, D1_MAX_EFFECTS, fp);
433
434         Num_wall_anims = cfile_read_int(fp);
435         wclip_read_n_d1(WallAnims, D1_MAX_WALL_ANIMS, fp);
436         */
437
438         /*
439         N_robot_types = cfile_read_int(fp);
440         //robot_info_read_n(Robot_info, D1_MAX_ROBOT_TYPES, fp);
441         cfseek(fp, D1_MAX_ROBOT_TYPES * D1_ROBOT_INFO_SIZE, SEEK_CUR);
442
443         N_robot_joints = cfile_read_int(fp);
444         jointpos_read_n(Robot_joints, D1_MAX_ROBOT_JOINTS, fp);
445
446         N_weapon_types = cfile_read_int(fp);
447         //weapon_info_read_n(Weapon_info, D1_MAX_WEAPON_TYPES, fp, Piggy_hamfile_version);
448         cfseek(fp, D1_MAX_WEAPON_TYPES * D1_WEAPON_INFO_SIZE, SEEK_CUR);
449
450         N_powerup_types = cfile_read_int(fp);
451         powerup_type_info_read_n(Powerup_info, D1_MAX_POWERUP_TYPES, fp);
452         */
453
454         /* in the following code are bugs, solved by hack
455         N_polygon_models = cfile_read_int(fp);
456         polymodel_read_n(Polygon_models, N_polygon_models, fp);
457         for (i=0; i<N_polygon_models; i++ )
458                 polygon_model_data_read(&Polygon_models[i], fp);
459         */cfseek(fp, 521490-160, SEEK_SET); // OK, I admit, this is a dirty hack
460         //bitmap_index_read_n(Gauges, D1_MAX_GAUGE_BMS, fp);
461         cfseek(fp, D1_MAX_GAUGE_BMS * sizeof(bitmap_index), SEEK_CUR);
462
463         /*
464         for (i = 0; i < D1_MAX_POLYGON_MODELS; i++)
465                 Dying_modelnums[i] = cfile_read_int(fp);
466         for (i = 0; i < D1_MAX_POLYGON_MODELS; i++)
467                 Dead_modelnums[i] = cfile_read_int(fp);
468         */ cfseek(fp, D1_MAX_POLYGON_MODELS * 8, SEEK_CUR);
469
470         //bitmap_index_read_n(ObjBitmaps, D1_MAX_OBJ_BITMAPS, fp);
471         cfseek(fp, D1_MAX_OBJ_BITMAPS * sizeof(bitmap_index), SEEK_CUR);
472         for (i = 0; i < D1_MAX_OBJ_BITMAPS; i++)
473                 cfseek(fp, 2, SEEK_CUR);//ObjBitmapPtrs[i] = cfile_read_short(fp);
474
475         //player_ship_read(&only_player_ship, fp);
476         cfseek(fp, sizeof(player_ship), SEEK_CUR);
477
478         /*Num_cockpits = */ cfile_read_int(fp);
479         //bitmap_index_read_n(cockpit_bitmap, D1_MAX_COCKPIT_BITMAPS, fp);
480         cfseek(fp, D1_MAX_COCKPIT_BITMAPS * sizeof(bitmap_index), SEEK_CUR);
481
482         /*
483         cfread( Sounds, sizeof(ubyte), D1_MAX_SOUNDS, fp );
484         cfread( AltSounds, sizeof(ubyte), D1_MAX_SOUNDS, fp );
485         */cfseek(fp, D1_MAX_SOUNDS * 2, SEEK_CUR);
486
487         /*Num_total_object_types = */ cfile_read_int( fp );
488         /*
489         cfread( ObjType, sizeof(byte), D1_MAX_OBJTYPE, fp );
490         cfread( ObjId, sizeof(byte), D1_MAX_OBJTYPE, fp );
491         for (i=0; i<D1_MAX_OBJTYPE; i++ )
492                 ObjStrength[i] = cfile_read_int( fp );
493         */ cfseek(fp, D1_MAX_OBJTYPE * 6, SEEK_CUR);
494
495         /*First_multi_bitmap_num =*/ cfile_read_int(fp);
496         /*Reactors[0].n_guns = */ cfile_read_int( fp );
497         /*for (i=0; i<4; i++)
498                 cfile_read_vector(&(Reactors[0].gun_points[i]), fp);
499         for (i=0; i<4; i++)
500                 cfile_read_vector(&(Reactors[0].gun_dirs[i]), fp);
501         */cfseek(fp, 8 * 12, SEEK_CUR);
502
503         /*exit_modelnum = */ cfile_read_int(fp);
504         /*destroyed_exit_modelnum = */ cfile_read_int(fp);
505 }
506
507 //these values are the number of each item in the release of d2
508 //extra items added after the release get written in an additional hamfile
509 #define N_D2_ROBOT_TYPES                66
510 #define N_D2_ROBOT_JOINTS               1145
511 #define N_D2_POLYGON_MODELS             166
512 #define N_D2_OBJBITMAPS                 422
513 #define N_D2_OBJBITMAPPTRS              502
514 #define N_D2_WEAPON_TYPES               62
515
516 void bm_free_extra_robots()
517 {
518         while (N_polygon_models > N_D2_POLYGON_MODELS)
519                 free_model(&Polygon_models[--N_polygon_models]);
520 }
521
522 //type==1 means 1.1, type==2 means 1.2 (with weapons)
523 void bm_read_extra_robots(char *fname,int type)
524 {
525         CFILE *fp;
526         int t,i;
527         int version;
528
529         fp = cfopen(fname,"rb");
530
531         if (type == 2) {
532                 int sig;
533
534                 sig = cfile_read_int(fp);
535                 if (sig != MAKE_SIG('X','H','A','M'))
536                         return;
537                 version = cfile_read_int(fp);
538         }
539         else
540                 version = 0;
541
542         bm_free_extra_robots();
543
544         //read extra weapons
545
546         t = cfile_read_int(fp);
547         N_weapon_types = N_D2_WEAPON_TYPES+t;
548         if (N_weapon_types >= MAX_WEAPON_TYPES)
549                 Error("Too many weapons (%d) in <%s>.  Max is %d.",t,fname,MAX_WEAPON_TYPES-N_D2_WEAPON_TYPES);
550         weapon_info_read_n(&Weapon_info[N_D2_WEAPON_TYPES], t, fp, 3);
551
552         //now read robot info
553
554         t = cfile_read_int(fp);
555         N_robot_types = N_D2_ROBOT_TYPES+t;
556         if (N_robot_types >= MAX_ROBOT_TYPES)
557                 Error("Too many robots (%d) in <%s>.  Max is %d.",t,fname,MAX_ROBOT_TYPES-N_D2_ROBOT_TYPES);
558         robot_info_read_n(&Robot_info[N_D2_ROBOT_TYPES], t, fp);
559
560         t = cfile_read_int(fp);
561         N_robot_joints = N_D2_ROBOT_JOINTS+t;
562         if (N_robot_joints >= MAX_ROBOT_JOINTS)
563                 Error("Too many robot joints (%d) in <%s>.  Max is %d.",t,fname,MAX_ROBOT_JOINTS-N_D2_ROBOT_JOINTS);
564         jointpos_read_n(&Robot_joints[N_D2_ROBOT_JOINTS], t, fp);
565
566         t = cfile_read_int(fp);
567         N_polygon_models = N_D2_POLYGON_MODELS+t;
568         if (N_polygon_models >= MAX_POLYGON_MODELS)
569                 Error("Too many polygon models (%d) in <%s>.  Max is %d.",t,fname,MAX_POLYGON_MODELS-N_D2_POLYGON_MODELS);
570         polymodel_read_n(&Polygon_models[N_D2_POLYGON_MODELS], t, fp);
571
572         for (i=N_D2_POLYGON_MODELS; i<N_polygon_models; i++ )
573                 polygon_model_data_read(&Polygon_models[i], fp);
574
575         for (i = N_D2_POLYGON_MODELS; i < N_polygon_models; i++)
576                 Dying_modelnums[i] = cfile_read_int(fp);
577         for (i = N_D2_POLYGON_MODELS; i < N_polygon_models; i++)
578                 Dead_modelnums[i] = cfile_read_int(fp);
579
580         t = cfile_read_int(fp);
581         if (N_D2_OBJBITMAPS+t >= MAX_OBJ_BITMAPS)
582                 Error("Too many object bitmaps (%d) in <%s>.  Max is %d.",t,fname,MAX_OBJ_BITMAPS-N_D2_OBJBITMAPS);
583         bitmap_index_read_n(&ObjBitmaps[N_D2_OBJBITMAPS], t, fp);
584
585         t = cfile_read_int(fp);
586         if (N_D2_OBJBITMAPPTRS+t >= MAX_OBJ_BITMAPS)
587                 Error("Too many object bitmap pointers (%d) in <%s>.  Max is %d.",t,fname,MAX_OBJ_BITMAPS-N_D2_OBJBITMAPPTRS);
588         for (i = N_D2_OBJBITMAPPTRS; i < (N_D2_OBJBITMAPPTRS + t); i++)
589                 ObjBitmapPtrs[i] = cfile_read_short(fp);
590
591         cfclose(fp);
592 }
593
594 extern void change_filename_extension( char *dest, char *src, char *new_ext );
595
596 int Robot_replacements_loaded = 0;
597
598 void load_robot_replacements(char *level_name)
599 {
600         CFILE *fp;
601         int t,i,j;
602         char ifile_name[FILENAME_LEN];
603
604         change_filename_extension(ifile_name, level_name, ".HXM" );
605
606         fp = cfopen(ifile_name,"rb");
607
608         if (!fp)                //no robot replacement file
609                 return;
610
611         t = cfile_read_int(fp);                 //read id "HXM!"
612         if (t!= 0x21584d48)
613                 Error("ID of HXM! file incorrect");
614
615         t = cfile_read_int(fp);                 //read version
616         if (t<1)
617                 Error("HXM! version too old (%d)",t);
618
619         t = cfile_read_int(fp);                 //read number of robots
620         for (j=0;j<t;j++) {
621                 i = cfile_read_int(fp);         //read robot number
622                 if (i<0 || i>=N_robot_types)
623                         Error("Robots number (%d) out of range in (%s).  Range = [0..%d].",i,level_name,N_robot_types-1);
624                 robot_info_read_n(&Robot_info[i], 1, fp);
625         }
626
627         t = cfile_read_int(fp);                 //read number of joints
628         for (j=0;j<t;j++) {
629                 i = cfile_read_int(fp);         //read joint number
630                 if (i<0 || i>=N_robot_joints)
631                         Error("Robots joint (%d) out of range in (%s).  Range = [0..%d].",i,level_name,N_robot_joints-1);
632                 jointpos_read_n(&Robot_joints[i], 1, fp);
633         }
634
635         t = cfile_read_int(fp);                 //read number of polygon models
636         for (j=0;j<t;j++)
637         {
638                 i = cfile_read_int(fp);         //read model number
639                 if (i<0 || i>=N_polygon_models)
640                         Error("Polygon model (%d) out of range in (%s).  Range = [0..%d].",i,level_name,N_polygon_models-1);
641                 polymodel_read(&Polygon_models[i], fp);
642
643                 free_model(Polygon_models[i]);
644                 polygon_model_data_read(&Polygon_models[i], fp);
645
646                 Dying_modelnums[i] = cfile_read_int(fp);
647                 Dead_modelnums[i] = cfile_read_int(fp);
648         }
649
650         t = cfile_read_int(fp);                 //read number of objbitmaps
651         for (j=0;j<t;j++) {
652                 i = cfile_read_int(fp);         //read objbitmap number
653                 if (i<0 || i>=MAX_OBJ_BITMAPS)
654                         Error("Object bitmap number (%d) out of range in (%s).  Range = [0..%d].",i,level_name,MAX_OBJ_BITMAPS-1);
655                 bitmap_index_read(&ObjBitmaps[i], fp);
656         }
657
658         t = cfile_read_int(fp);                 //read number of objbitmapptrs
659         for (j=0;j<t;j++) {
660                 i = cfile_read_int(fp);         //read objbitmapptr number
661                 if (i<0 || i>=MAX_OBJ_BITMAPS)
662                         Error("Object bitmap pointer (%d) out of range in (%s).  Range = [0..%d].",i,level_name,MAX_OBJ_BITMAPS-1);
663                 ObjBitmapPtrs[i] = cfile_read_short(fp);
664         }
665
666         cfclose(fp);
667 }