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