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