use portable loader if bigendian
[btb/d2x.git] / main / bm.c
1 /*
2 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
3 SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
4 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
5 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
6 IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
7 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
8 FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
9 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
10 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.  
11 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
12 */
13
14 /*
15  * $Source: /cvs/cvsroot/d2x/main/bm.c,v $
16  * $Revision: 1.8 $
17  * $Author: bradleyb $
18  * $Date: 2002-02-11 07:44:44 $
19  *
20  * Bitmap and palette loading functions.
21  *
22  * $Log: not supported by cvs2svn $
23  * Revision 1.7  2001/11/14 09:34:32  bradleyb
24  * use MAKE_SIG
25  *
26  * Revision 1.6  2001/11/02 02:22:25  bradleyb
27  * formatting
28  *
29  * Revision 1.5  2001/10/25 02:15:55  bradleyb
30  * conditionalize including multi.h and network.h, fix backslashes
31  *
32  * Revision 1.4  2001/10/19 08:06:20  bradleyb
33  * Partial application of linux/alpha patch.  Courtesy of Falk Hueffner <falk.hueffner@student.uni-tuebingen.de>
34  *
35  *
36  */
37
38 #ifdef HAVE_CONFIG_H
39 #include <conf.h>
40 #endif
41
42 #ifdef WORDS_BIGENDIAN
43 #define PORTABLE_LOADER
44 #endif
45
46 #include <stdio.h>
47 #include <stdlib.h>
48
49 #include "pstypes.h"
50 #include "inferno.h"
51 #include "gr.h"
52 #include "bm.h"
53 #include "u_mem.h"
54 #include "mono.h"
55 #include "error.h"
56 #include "object.h"
57 #include "vclip.h"
58 #include "effects.h"
59 #include "polyobj.h"
60 #include "wall.h"
61 #include "textures.h"
62 #include "game.h"
63 #ifdef NETWORK
64 #include "multi.h"
65 #endif
66 #include "iff.h"
67 #include "cfile.h"
68 #include "powerup.h"
69 #include "sounds.h"
70 #include "piggy.h"
71 #include "aistruct.h"
72 #include "robot.h"
73 #include "weapon.h"
74 #include "gauges.h"
75 #include "player.h"
76 #include "endlevel.h"
77 #include "cntrlcen.h"
78 #include "byteswap.h"
79 #include "makesig.h"
80
81 ubyte Sounds[MAX_SOUNDS];
82 ubyte AltSounds[MAX_SOUNDS];
83
84 #ifdef EDITOR
85 int Num_total_object_types;
86 byte    ObjType[MAX_OBJTYPE];
87 byte    ObjId[MAX_OBJTYPE];
88 fix     ObjStrength[MAX_OBJTYPE];
89 #endif
90
91 //for each model, a model number for dying & dead variants, or -1 if none
92 int Dying_modelnums[MAX_POLYGON_MODELS];
93 int Dead_modelnums[MAX_POLYGON_MODELS];
94
95 //the polygon model number to use for the marker
96 int     Marker_model_num = -1;
97
98 //right now there's only one player ship, but we can have another by 
99 //adding an array and setting the pointer to the active ship.
100 player_ship only_player_ship,*Player_ship=&only_player_ship;
101
102 //----------------- Miscellaneous bitmap pointers ---------------
103 int                                     Num_cockpits = 0;
104 bitmap_index            cockpit_bitmap[N_COCKPIT_BITMAPS];
105
106 //---------------- Variables for wall textures ------------------
107 int                                     Num_tmaps;
108 tmap_info                       TmapInfo[MAX_TEXTURES];
109
110 //---------------- Variables for object textures ----------------
111
112 int                                     First_multi_bitmap_num=-1;
113
114 bitmap_index            ObjBitmaps[MAX_OBJ_BITMAPS];
115 ushort                          ObjBitmapPtrs[MAX_OBJ_BITMAPS];         // These point back into ObjBitmaps, since some are used twice.
116
117 #ifdef PORTABLE_LOADER
118 void read_tmap_info(CFILE *fp, int inNumTexturesToRead, int inOffset)
119 {
120         int i;
121         
122         for (i = inOffset; i < (inNumTexturesToRead + inOffset); i++)
123         {
124                 TmapInfo[i].flags = cfile_read_byte(fp);
125                 TmapInfo[i].pad[0] = cfile_read_byte(fp);
126                 TmapInfo[i].pad[1] = cfile_read_byte(fp);
127                 TmapInfo[i].pad[2] = cfile_read_byte(fp);
128                 TmapInfo[i].lighting = cfile_read_fix(fp);
129                 TmapInfo[i].damage = cfile_read_fix(fp);
130                 TmapInfo[i].eclip_num = cfile_read_short(fp);
131                 TmapInfo[i].destroyed = cfile_read_short(fp);
132                 TmapInfo[i].slide_u = cfile_read_short(fp);
133                 TmapInfo[i].slide_v = cfile_read_short(fp);
134         }
135 }
136
137 void read_vclip_info(CFILE *fp, int inNumVClipsToRead, int inOffset)
138 {
139         int i, j;
140         
141         for (i = inOffset; i < (inNumVClipsToRead + inOffset); i++)
142         {
143                 Vclip[i].play_time = cfile_read_fix(fp);
144                 Vclip[i].num_frames = cfile_read_int(fp);
145                 Vclip[i].frame_time = cfile_read_fix(fp);
146                 Vclip[i].flags = cfile_read_int(fp);
147                 Vclip[i].sound_num = cfile_read_short(fp);
148                 for (j = 0; j < VCLIP_MAX_FRAMES; j++)
149                         Vclip[i].frames[j].index = cfile_read_short(fp);
150                 Vclip[i].light_value = cfile_read_fix(fp);
151         }
152 }
153
154 void read_effect_info(CFILE *fp, int inNumEffectsToRead, int inOffset)
155 {
156         int i, j;
157
158
159         for (i = inOffset; i < (inNumEffectsToRead + inOffset); i++)
160         {
161                 Effects[i].vc.play_time = cfile_read_fix(fp);
162                 Effects[i].vc.num_frames = cfile_read_int(fp);
163                 Effects[i].vc.frame_time = cfile_read_fix(fp);
164                 Effects[i].vc.flags = cfile_read_int(fp);
165                 Effects[i].vc.sound_num = cfile_read_short(fp);
166                 for (j = 0; j < VCLIP_MAX_FRAMES; j++)
167                         Effects[i].vc.frames[j].index = cfile_read_short(fp);
168                 Effects[i].vc.light_value = cfile_read_fix(fp);
169                 Effects[i].time_left = cfile_read_fix(fp);
170                 Effects[i].frame_count = cfile_read_int(fp);
171                 Effects[i].changing_wall_texture = cfile_read_short(fp);
172                 Effects[i].changing_object_texture = cfile_read_short(fp);
173                 Effects[i].flags = cfile_read_int(fp);
174                 Effects[i].crit_clip = cfile_read_int(fp);
175                 Effects[i].dest_bm_num = cfile_read_int(fp);
176                 Effects[i].dest_vclip = cfile_read_int(fp);
177                 Effects[i].dest_eclip = cfile_read_int(fp);
178                 Effects[i].dest_size = cfile_read_fix(fp);
179                 Effects[i].sound_num = cfile_read_int(fp);
180                 Effects[i].segnum = cfile_read_int(fp);
181                 Effects[i].sidenum = cfile_read_int(fp);
182         }
183 }
184
185 void read_wallanim_info(CFILE *fp, int inNumWallAnimsToRead, int inOffset)
186 {
187         int i, j;
188         
189         for (i = inOffset; i < (inNumWallAnimsToRead + inOffset); i++)
190         {
191                 WallAnims[i].play_time = cfile_read_fix(fp);;
192                 WallAnims[i].num_frames = cfile_read_short(fp);;
193                 for (j = 0; j < MAX_CLIP_FRAMES; j++)
194                         WallAnims[i].frames[j] = cfile_read_short(fp);
195                 WallAnims[i].open_sound = cfile_read_short(fp);
196                 WallAnims[i].close_sound = cfile_read_short(fp);
197                 WallAnims[i].flags = cfile_read_short(fp);
198                 cfread(WallAnims[i].filename, 13, 1, fp);
199                 WallAnims[i].pad = cfile_read_byte(fp);
200         }               
201 }
202
203 void read_robot_info(CFILE *fp, int inNumRobotsToRead, int inOffset)
204 {
205         int i, j, k;
206         
207         for (i = inOffset; i < (inNumRobotsToRead + inOffset); i++)
208         {
209                 Robot_info[i].model_num = cfile_read_int(fp);
210                 for (j = 0; j < MAX_GUNS; j++)
211                         cfile_read_vector(&(Robot_info[i].gun_points[j]), fp);
212                 for (j = 0; j < MAX_GUNS; j++)
213                         Robot_info[i].gun_submodels[j] = cfile_read_byte(fp);
214
215                 Robot_info[i].exp1_vclip_num = cfile_read_short(fp);
216                 Robot_info[i].exp1_sound_num = cfile_read_short(fp);
217
218                 Robot_info[i].exp2_vclip_num = cfile_read_short(fp);
219                 Robot_info[i].exp2_sound_num = cfile_read_short(fp);
220
221                 Robot_info[i].weapon_type = cfile_read_byte(fp);
222                 Robot_info[i].weapon_type2 = cfile_read_byte(fp);
223                 Robot_info[i].n_guns = cfile_read_byte(fp);
224                 Robot_info[i].contains_id = cfile_read_byte(fp);
225
226                 Robot_info[i].contains_count = cfile_read_byte(fp);
227                 Robot_info[i].contains_prob = cfile_read_byte(fp);
228                 Robot_info[i].contains_type = cfile_read_byte(fp);
229                 Robot_info[i].kamikaze = cfile_read_byte(fp);
230
231                 Robot_info[i].score_value = cfile_read_short(fp);
232                 Robot_info[i].badass = cfile_read_byte(fp);
233                 Robot_info[i].energy_drain = cfile_read_byte(fp);
234                 
235                 Robot_info[i].lighting = cfile_read_fix(fp);
236                 Robot_info[i].strength = cfile_read_fix(fp);
237
238                 Robot_info[i].mass = cfile_read_fix(fp);
239                 Robot_info[i].drag = cfile_read_fix(fp);
240
241                 for (j = 0; j < NDL; j++)
242                         Robot_info[i].field_of_view[j] = cfile_read_fix(fp);
243                 for (j = 0; j < NDL; j++)
244                         Robot_info[i].firing_wait[j] = cfile_read_fix(fp);
245                 for (j = 0; j < NDL; j++)
246                         Robot_info[i].firing_wait2[j] = cfile_read_fix(fp);
247                 for (j = 0; j < NDL; j++)
248                         Robot_info[i].turn_time[j] = cfile_read_fix(fp);
249                 for (j = 0; j < NDL; j++)
250                         Robot_info[i].max_speed[j] = cfile_read_fix(fp);
251                 for (j = 0; j < NDL; j++)
252                         Robot_info[i].circle_distance[j] = cfile_read_fix(fp);
253                 for (j = 0; j < NDL; j++)
254                         cfread(&(Robot_info[i].rapidfire_count[j]), sizeof(byte), 1, fp);
255                 for (j = 0; j < NDL; j++)
256                         cfread(&(Robot_info[i].evade_speed[j]), sizeof(byte), 1, fp);
257                 Robot_info[i].cloak_type = cfile_read_byte(fp);
258                 Robot_info[i].attack_type = cfile_read_byte(fp);
259
260                 Robot_info[i].see_sound = cfile_read_byte(fp);
261                 Robot_info[i].attack_sound = cfile_read_byte(fp);
262                 Robot_info[i].claw_sound = cfile_read_byte(fp);
263                 Robot_info[i].taunt_sound = cfile_read_byte(fp);
264
265                 Robot_info[i].boss_flag = cfile_read_byte(fp);
266                 Robot_info[i].companion = cfile_read_byte(fp);
267                 Robot_info[i].smart_blobs = cfile_read_byte(fp);
268                 Robot_info[i].energy_blobs = cfile_read_byte(fp);
269
270                 Robot_info[i].thief = cfile_read_byte(fp);
271                 Robot_info[i].pursuit = cfile_read_byte(fp);
272                 Robot_info[i].lightcast = cfile_read_byte(fp);
273                 Robot_info[i].death_roll = cfile_read_byte(fp);
274
275                 Robot_info[i].flags = cfile_read_byte(fp);
276                 Robot_info[i].pad[0] = cfile_read_byte(fp);
277                 Robot_info[i].pad[1] = cfile_read_byte(fp);
278                 Robot_info[i].pad[2] = cfile_read_byte(fp);
279
280                 Robot_info[i].deathroll_sound = cfile_read_byte(fp);
281                 Robot_info[i].glow = cfile_read_byte(fp);
282                 Robot_info[i].behavior = cfile_read_byte(fp);
283                 Robot_info[i].aim = cfile_read_byte(fp);
284
285                 for (j = 0; j < MAX_GUNS + 1; j++) {
286                         for (k = 0; k < N_ANIM_STATES; k++) {
287                                 Robot_info[i].anim_states[j][k].n_joints = cfile_read_short(fp);
288                                 Robot_info[i].anim_states[j][k].offset = cfile_read_short(fp);
289                         }
290                 }
291
292                 Robot_info[i].always_0xabcd = cfile_read_int(fp);
293         }
294 }
295
296 void read_robot_joint_info(CFILE *fp, int inNumRobotJointsToRead, int inOffset)
297 {
298         int i;
299
300         for (i = inOffset; i < (inNumRobotJointsToRead + inOffset); i++)
301         {
302                 Robot_joints[i].jointnum = cfile_read_short(fp);
303                 cfile_read_angvec(&(Robot_joints[i].angles), fp);
304         }
305 }
306
307 void read_weapon_info(CFILE *fp, int inNumWeaponsToRead, int inOffset)
308 {
309         int i, j;
310         
311         for (i = inOffset; i < (inNumWeaponsToRead + inOffset); i++)
312         {
313                 Weapon_info[i].render_type = cfile_read_byte(fp);
314                 Weapon_info[i].persistent = cfile_read_byte(fp);
315                 Weapon_info[i].model_num = cfile_read_short(fp);
316                 Weapon_info[i].model_num_inner = cfile_read_short(fp);
317
318                 Weapon_info[i].flash_vclip = cfile_read_byte(fp);
319                 Weapon_info[i].robot_hit_vclip = cfile_read_byte(fp);
320                 Weapon_info[i].flash_sound = cfile_read_short(fp);              
321
322                 Weapon_info[i].wall_hit_vclip = cfile_read_byte(fp);
323                 Weapon_info[i].fire_count = cfile_read_byte(fp);
324                 Weapon_info[i].robot_hit_sound = cfile_read_short(fp);
325                 
326                 Weapon_info[i].ammo_usage = cfile_read_byte(fp);
327                 Weapon_info[i].weapon_vclip = cfile_read_byte(fp);
328                 Weapon_info[i].wall_hit_sound = cfile_read_short(fp);           
329
330                 Weapon_info[i].destroyable = cfile_read_byte(fp);
331                 Weapon_info[i].matter = cfile_read_byte(fp);
332                 Weapon_info[i].bounce = cfile_read_byte(fp);
333                 Weapon_info[i].homing_flag = cfile_read_byte(fp);
334
335                 Weapon_info[i].speedvar = cfile_read_byte(fp);
336                 Weapon_info[i].flags = cfile_read_byte(fp);
337                 Weapon_info[i].flash = cfile_read_byte(fp);
338                 Weapon_info[i].afterburner_size = cfile_read_byte(fp);
339                 
340                 Weapon_info[i].children = cfile_read_byte(fp);
341
342                 Weapon_info[i].energy_usage = cfile_read_fix(fp);
343                 Weapon_info[i].fire_wait = cfile_read_fix(fp);
344                 
345                 Weapon_info[i].multi_damage_scale = cfile_read_fix(fp);
346                 
347                 Weapon_info[i].bitmap.index = cfile_read_short(fp);     // bitmap_index = short
348
349                 Weapon_info[i].blob_size = cfile_read_fix(fp);
350                 Weapon_info[i].flash_size = cfile_read_fix(fp);
351                 Weapon_info[i].impact_size = cfile_read_fix(fp);
352                 for (j = 0; j < NDL; j++)
353                         Weapon_info[i].strength[j] = cfile_read_fix(fp);
354                 for (j = 0; j < NDL; j++)
355                         Weapon_info[i].speed[j] = cfile_read_fix(fp);
356                 Weapon_info[i].mass = cfile_read_fix(fp);
357                 Weapon_info[i].drag = cfile_read_fix(fp);
358                 Weapon_info[i].thrust = cfile_read_fix(fp);
359                 Weapon_info[i].po_len_to_width_ratio = cfile_read_fix(fp);
360                 Weapon_info[i].light = cfile_read_fix(fp);
361                 Weapon_info[i].lifetime = cfile_read_fix(fp);
362                 Weapon_info[i].damage_radius = cfile_read_fix(fp);
363                 Weapon_info[i].picture.index = cfile_read_short(fp);            // bitmap_index is a short
364                 Weapon_info[i].hires_picture.index = cfile_read_short(fp);              // bitmap_index is a short
365         }
366 }
367
368 void read_powerup_info(CFILE *fp, int inNumPowerupsToRead, int inOffset)
369 {
370         int i;
371         
372         for (i = inOffset; i < (inNumPowerupsToRead + inOffset); i++)
373         {
374                 Powerup_info[i].vclip_num = cfile_read_int(fp);
375                 Powerup_info[i].hit_sound = cfile_read_int(fp);
376                 Powerup_info[i].size = cfile_read_fix(fp);
377                 Powerup_info[i].light = cfile_read_fix(fp);
378         }
379 }
380
381 void read_polygon_models(CFILE *fp, int inNumPolygonModelsToRead, int inOffset)
382 {
383         int i, j;
384
385         for (i = inOffset; i < (inNumPolygonModelsToRead + inOffset); i++)
386         {
387                 Polygon_models[i].n_models = cfile_read_int(fp);
388                 Polygon_models[i].model_data_size = cfile_read_int(fp);
389                 Polygon_models[i].model_data = (ubyte *) cfile_read_int(fp);
390                 for (j = 0; j < MAX_SUBMODELS; j++)
391                         Polygon_models[i].submodel_ptrs[j] = cfile_read_int(fp);
392                 for (j = 0; j < MAX_SUBMODELS; j++)
393                         cfile_read_vector(&(Polygon_models[i].submodel_offsets[j]), fp);
394                 for (j = 0; j < MAX_SUBMODELS; j++)
395                         cfile_read_vector(&(Polygon_models[i].submodel_norms[j]), fp);
396                 for (j = 0; j < MAX_SUBMODELS; j++)
397                         cfile_read_vector(&(Polygon_models[i].submodel_pnts[j]), fp);
398                 for (j = 0; j < MAX_SUBMODELS; j++)
399                         Polygon_models[i].submodel_rads[j] = cfile_read_fix(fp);
400                 for (j = 0; j < MAX_SUBMODELS; j++)
401                         Polygon_models[i].submodel_parents[j] = cfile_read_byte(fp);
402                 for (j = 0; j < MAX_SUBMODELS; j++)
403                         cfile_read_vector(&(Polygon_models[i].submodel_mins[j]), fp);
404                 for (j = 0; j < MAX_SUBMODELS; j++)
405                         cfile_read_vector(&(Polygon_models[i].submodel_maxs[j]), fp);
406                 cfile_read_vector(&(Polygon_models[i].mins), fp);
407                 cfile_read_vector(&(Polygon_models[i].maxs), fp);
408                 Polygon_models[i].rad = cfile_read_fix(fp);             
409                 Polygon_models[i].n_textures = cfile_read_byte(fp);
410                 Polygon_models[i].first_texture = cfile_read_short(fp);
411                 Polygon_models[i].simpler_model = cfile_read_byte(fp);
412         }
413 }
414
415 void read_player_ship(CFILE *fp)
416 {
417         int i;
418
419         only_player_ship.model_num = cfile_read_int(fp);
420         only_player_ship.expl_vclip_num = cfile_read_int(fp);
421         only_player_ship.mass = cfile_read_fix(fp);
422         only_player_ship.drag = cfile_read_fix(fp);
423         only_player_ship.max_thrust = cfile_read_fix(fp);
424         only_player_ship.reverse_thrust = cfile_read_fix(fp);
425         only_player_ship.brakes = cfile_read_fix(fp);
426         only_player_ship.wiggle = cfile_read_fix(fp);
427         only_player_ship.max_rotthrust = cfile_read_fix(fp);
428         for (i = 0; i < N_PLAYER_GUNS; i++)
429                 cfile_read_vector(&(only_player_ship.gun_points[i]), fp);
430 }
431
432 void read_reactor_info(CFILE *fp, int inNumReactorsToRead, int inOffset)
433 {
434         int i, j;
435         
436         for (i = inOffset; i < (inNumReactorsToRead + inOffset); i++)
437         {
438                 Reactors[i].model_num = cfile_read_int(fp);
439                 Reactors[i].n_guns = cfile_read_int(fp);
440                 for (j = 0; j < MAX_CONTROLCEN_GUNS; j++)
441                         cfile_read_vector(&(Reactors[i].gun_points[j]), fp);
442                 for (j = 0; j < MAX_CONTROLCEN_GUNS; j++)
443                         cfile_read_vector(&(Reactors[i].gun_dirs[j]), fp);
444         }
445 }
446
447 #ifdef SHAREWARE
448 extern int exit_modelnum,destroyed_exit_modelnum, Num_bitmap_files;
449 int N_ObjBitmaps, extra_bitmap_num;
450
451 bitmap_index exitmodel_bm_load_sub( char * filename )
452 {
453         bitmap_index bitmap_num;
454         grs_bitmap * new;
455         ubyte newpal[256*3];
456         int i, iff_error;               //reference parm to avoid warning message
457
458         bitmap_num.index = 0;
459
460         MALLOC( new, grs_bitmap, 1 );
461         iff_error = iff_read_bitmap(filename,new,BM_LINEAR,newpal);
462         new->bm_handle=0;
463         if (iff_error != IFF_NO_ERROR)          {
464                 Error("Error loading exit model bitmap <%s> - IFF error: %s",filename,iff_errormsg(iff_error));
465         }
466         
467         if ( iff_has_transparency )
468                 gr_remap_bitmap_good( new, newpal, iff_transparent_color, 254 );
469         else
470                 gr_remap_bitmap_good( new, newpal, -1, 254 );
471
472         new->avg_color = 0;     //compute_average_pixel(new);
473
474         bitmap_num.index = extra_bitmap_num;
475
476         GameBitmaps[extra_bitmap_num++] = *new;
477         
478         d_free( new );
479         return bitmap_num;
480 }
481
482 grs_bitmap *load_exit_model_bitmap(char *name)
483 {
484         Assert(N_ObjBitmaps < MAX_OBJ_BITMAPS);
485
486         {
487                 ObjBitmaps[N_ObjBitmaps] = exitmodel_bm_load_sub(name);
488                 if (GameBitmaps[ObjBitmaps[N_ObjBitmaps].index].bm_w!=64 || GameBitmaps[ObjBitmaps[N_ObjBitmaps].index].bm_h!=64)
489                         Error("Bitmap <%s> is not 64x64",name);
490                 ObjBitmapPtrs[N_ObjBitmaps] = N_ObjBitmaps;
491                 N_ObjBitmaps++;
492                 Assert(N_ObjBitmaps < MAX_OBJ_BITMAPS);
493                 return &GameBitmaps[ObjBitmaps[N_ObjBitmaps-1].index];
494         }
495 }
496
497 void load_exit_models()
498 {
499         CFILE *exit_hamfile;
500         int i, j;
501         ubyte pal[768];
502         int start_num;
503
504         start_num = N_ObjBitmaps;
505         extra_bitmap_num = Num_bitmap_files;
506         load_exit_model_bitmap("steel1.bbm");
507         load_exit_model_bitmap("rbot061.bbm");
508         load_exit_model_bitmap("rbot062.bbm");
509
510         load_exit_model_bitmap("steel1.bbm");
511         load_exit_model_bitmap("rbot061.bbm");
512         load_exit_model_bitmap("rbot063.bbm");
513
514         exit_hamfile = cfopen(":Data:exit.ham","rb");
515
516         exit_modelnum = N_polygon_models++;
517         destroyed_exit_modelnum = N_polygon_models++;
518
519 #ifndef PORTABLE_LOADER
520         cfread( &Polygon_models[exit_modelnum], sizeof(polymodel), 1, exit_hamfile );
521         cfread( &Polygon_models[destroyed_exit_modelnum], sizeof(polymodel), 1, exit_hamfile );
522 #else
523         for (i = exit_modelnum; i <= destroyed_exit_modelnum; i++) {
524                 Polygon_models[i].n_models = cfile_read_int(exit_hamfile);
525                 Polygon_models[i].model_data_size = cfile_read_int(exit_hamfile);
526                 Polygon_models[i].model_data = (ubyte *)read_int_swap(exit_hamfile);
527                 for (j = 0; j < MAX_SUBMODELS; j++)
528                         Polygon_models[i].submodel_ptrs[j] = cfile_read_int(exit_hamfile);
529                 for (j = 0; j < MAX_SUBMODELS; j++)
530                         cfile_read_vector(&(Polygon_models[i].submodel_offsets), exit_hamfile);
531                 for (j = 0; j < MAX_SUBMODELS; j++)
532                         cfile_read_vector(&(Polygon_models[i].submodel_norms), exit_hamfile);
533                 for (j = 0; j < MAX_SUBMODELS; j++)
534                         cfile_read_vector(&(Polygon_models[i].submodel_pnts), exit_hamfile);
535                 for (j = 0; j < MAX_SUBMODELS; j++)
536                         Polygon_models[i].submodel_rads[j] = cfile_read_fix(exit_hamfile);
537                 for (j = 0; j < MAX_SUBMODELS; j++)
538                         Polygon_models[i].submodel_parents[j] = cfile_read_byte(exit_hamfile);
539                 for (j = 0; j < MAX_SUBMODELS; j++)
540                         cfile_read_vector(&(Polygon_models[i].submodel_mins), exit_hamfile);
541                 for (j = 0; j < MAX_SUBMODELS; j++)
542                         cfile_read_vector(&(Polygon_models[i].submodel_maxs), exit_hamfile);
543                 cfile_read_vector(&(Polygon_models[i].mins), exit_hamfile);
544                 cfile_read_vector(&(Polygon_models[i].maxs), exit_hamfile);
545                 Polygon_models[i].rad = cfile_read_fix(exit_hamfile);           
546                 Polygon_models[i].n_textures = cfile_read_byte(exit_hamfile);
547                 Polygon_models[i].first_texture = cfile_read_short(exit_hamfile);
548                 Polygon_models[i].simpler_model = cfile_read_byte(exit_hamfile);
549         }
550         Polygon_models[exit_modelnum].first_texture = start_num;
551         Polygon_models[destroyed_exit_modelnum].first_texture = start_num+3;
552 #endif
553
554         Polygon_models[exit_modelnum].model_data = d_malloc(Polygon_models[exit_modelnum].model_data_size);
555         Assert( Polygon_models[exit_modelnum].model_data != NULL );
556         cfread( Polygon_models[exit_modelnum].model_data, sizeof(ubyte), Polygon_models[exit_modelnum].model_data_size, exit_hamfile );
557         #ifdef MACINTOSH
558         swap_polygon_model_data(Polygon_models[exit_modelnum].model_data);
559         #endif
560         g3_init_polygon_model(Polygon_models[exit_modelnum].model_data);
561
562         Polygon_models[destroyed_exit_modelnum].model_data = d_malloc(Polygon_models[destroyed_exit_modelnum].model_data_size);
563         Assert( Polygon_models[destroyed_exit_modelnum].model_data != NULL );
564         cfread( Polygon_models[destroyed_exit_modelnum].model_data, sizeof(ubyte), Polygon_models[destroyed_exit_modelnum].model_data_size, exit_hamfile );
565         #ifdef MACINTOSH
566         swap_polygon_model_data(Polygon_models[destroyed_exit_modelnum].model_data);
567         #endif
568         g3_init_polygon_model(Polygon_models[destroyed_exit_modelnum].model_data);
569
570         cfclose(exit_hamfile);
571
572 }
573 #endif          // SHAREWARE
574
575 #endif          // PORTABLE_LOADER
576
577 //-----------------------------------------------------------------
578 // Read data from piggy.
579 // This is called when the editor is OUT.  
580 // If editor is in, bm_init_use_table() is called.
581 int bm_init()
582 {
583         init_polygon_models();
584         if (! piggy_init())                             // This calls bm_read_all
585                 Error("Cannot open pig and/or ham file");
586
587         piggy_read_sounds();
588
589         #ifdef SHAREWARE
590         init_endlevel();                //this is in bm_init_use_tbl(), so I gues it goes here
591         #endif
592
593         return 0;
594 }
595
596 void bm_read_all(CFILE * fp)
597 {
598         int i,t;
599
600         NumTextures = cfile_read_int(fp);
601 #ifndef PORTABLE_LOADER
602         cfread( Textures, sizeof(bitmap_index), NumTextures, fp );
603         cfread( TmapInfo, sizeof(tmap_info), NumTextures, fp );
604 #else
605         for (i = 0; i < NumTextures; i++)
606                 Textures[i].index = cfile_read_short(fp);
607         read_tmap_info(fp, NumTextures, 0);
608 #endif
609
610         t = cfile_read_int(fp); 
611         cfread( Sounds, sizeof(ubyte), t, fp );
612         cfread( AltSounds, sizeof(ubyte), t, fp );
613
614         Num_vclips = cfile_read_int(fp);
615 #ifndef PORTABLE_LOADER
616         cfread( Vclip, sizeof(vclip), Num_vclips, fp );
617 #else
618         read_vclip_info(fp, Num_vclips, 0);
619 #endif
620
621         Num_effects = cfile_read_int(fp);
622 #ifndef PORTABLE_LOADER
623         cfread( Effects, sizeof(eclip), Num_effects, fp );
624 #else   
625         read_effect_info(fp, Num_effects, 0);
626 #endif
627
628         Num_wall_anims = cfile_read_int(fp);
629 #ifndef PORTABLE_LOADER
630         cfread( WallAnims, sizeof(wclip), Num_wall_anims, fp );
631 #else
632         read_wallanim_info(fp, Num_wall_anims, 0);
633 #endif
634
635         N_robot_types = cfile_read_int(fp);
636 #ifndef PORTABLE_LOADER
637         cfread( Robot_info, sizeof(robot_info), N_robot_types, fp );
638 #else
639         read_robot_info(fp, N_robot_types, 0);
640 #endif
641         N_robot_joints = cfile_read_int(fp);
642 #ifndef PORTABLE_LOADER
643         cfread( Robot_joints, sizeof(jointpos), N_robot_joints, fp );
644 #else
645         read_robot_joint_info(fp, N_robot_joints, 0);
646 #endif
647
648         N_weapon_types = cfile_read_int(fp);
649 #ifndef PORTABLE_LOADER
650         cfread( Weapon_info, sizeof(weapon_info), N_weapon_types, fp );
651 #else
652         read_weapon_info(fp, N_weapon_types, 0);
653 #endif
654
655         N_powerup_types = cfile_read_int(fp);
656 #ifndef PORTABLE_LOADER
657         cfread( Powerup_info, sizeof(powerup_type_info), N_powerup_types, fp );
658 #else
659         read_powerup_info(fp, N_powerup_types, 0);
660 #endif
661         
662         N_polygon_models = cfile_read_int(fp);
663 #ifndef PORTABLE_LOADER
664         cfread( Polygon_models, sizeof(polymodel), N_polygon_models, fp );
665 #else
666         read_polygon_models(fp, N_polygon_models, 0);
667 #endif
668
669         for (i=0; i<N_polygon_models; i++ )     {
670                 Polygon_models[i].model_data = d_malloc(Polygon_models[i].model_data_size);
671                 Assert( Polygon_models[i].model_data != NULL );
672                 cfread( Polygon_models[i].model_data, sizeof(ubyte), Polygon_models[i].model_data_size, fp );
673 #ifdef MACINTOSH
674                 swap_polygon_model_data(Polygon_models[i].model_data);
675 #endif
676                 g3_init_polygon_model(Polygon_models[i].model_data);
677         }
678
679         cfread( Dying_modelnums, sizeof(int), N_polygon_models, fp );
680         cfread( Dead_modelnums, sizeof(int), N_polygon_models, fp );
681 #ifdef MACINTOSH
682         for (i = 0; i < N_polygon_models; i++)
683                 Dying_modelnums[i]= SWAPINT(Dying_modelnums[i]);
684         for (i = 0; i < N_polygon_models; i++)
685                 Dead_modelnums[i]= SWAPINT(Dead_modelnums[i]);
686 #endif
687
688         t = cfile_read_int(fp);
689         cfread( Gauges, sizeof(bitmap_index), t, fp );
690         cfread( Gauges_hires, sizeof(bitmap_index), t, fp );
691 #ifdef MACINTOSH
692         for (i = 0; i < t; i++) {
693                 Gauges[i].index = SWAPSHORT(Gauges[i].index);
694                 Gauges_hires[i].index = SWAPSHORT(Gauges_hires[i].index);
695         }
696 #endif
697
698         t = cfile_read_int(fp);
699         cfread( ObjBitmaps, sizeof(bitmap_index), t, fp );
700         cfread( ObjBitmapPtrs, sizeof(ushort), t, fp );
701
702 #ifdef MACINTOSH
703 #ifdef SHAREWARE
704         N_ObjBitmaps = t;
705 #endif
706         for (i = 0; i < t; i++) {
707                 ObjBitmaps[i].index = SWAPSHORT(ObjBitmaps[i].index);
708                 ObjBitmapPtrs[i] = SWAPSHORT(ObjBitmapPtrs[i]);
709         }
710 #endif
711
712 #ifndef PORTABLE_LOADER
713         cfread( &only_player_ship, sizeof(player_ship), 1, fp );
714 #else
715         read_player_ship(fp);
716 #endif
717
718         Num_cockpits = cfile_read_int(fp);
719         cfread( cockpit_bitmap, sizeof(bitmap_index), Num_cockpits, fp );
720 #ifdef PORTABLE_LOADER
721         for (i = 0; i < Num_cockpits; i++)
722                 cockpit_bitmap[i].index = SWAPSHORT(cockpit_bitmap[i].index);
723 #endif
724
725 //@@    cfread( &Num_total_object_types, sizeof(int), 1, fp );
726 //@@    cfread( ObjType, sizeof(byte), Num_total_object_types, fp );
727 //@@    cfread( ObjId, sizeof(byte), Num_total_object_types, fp );
728 //@@    cfread( ObjStrength, sizeof(fix), Num_total_object_types, fp );
729
730         First_multi_bitmap_num = cfile_read_int(fp);
731
732         Num_reactors = cfile_read_int(fp);
733 #ifndef MACINTOSH
734         cfread( Reactors, sizeof(*Reactors), Num_reactors, fp);
735 #else
736         read_reactor_info(fp, Num_reactors, 0);
737 #endif
738
739         Marker_model_num = cfile_read_int(fp);
740
741         //@@cfread( &N_controlcen_guns, sizeof(int), 1, fp );
742         //@@cfread( controlcen_gun_points, sizeof(vms_vector), N_controlcen_guns, fp );
743         //@@cfread( controlcen_gun_dirs, sizeof(vms_vector), N_controlcen_guns, fp );
744
745         #ifdef SHAREWARE
746         exit_modelnum = cfile_read_int(fp);
747         destroyed_exit_modelnum = cfile_read_int(fp);
748         #endif
749
750 }
751
752
753 //these values are the number of each item in the release of d2
754 //extra items added after the release get written in an additional hamfile
755 #define N_D2_ROBOT_TYPES                66
756 #define N_D2_ROBOT_JOINTS               1145
757 #define N_D2_POLYGON_MODELS             166
758 #define N_D2_OBJBITMAPS                 422
759 #define N_D2_OBJBITMAPPTRS              502
760 #define N_D2_WEAPON_TYPES               62
761
762 //type==1 means 1.1, type==2 means 1.2 (with weaons)
763 void bm_read_extra_robots(char *fname,int type)
764 {
765         CFILE *fp;
766         int t,i;
767         int version;
768         
769         #ifdef MACINTOSH
770                 ulong varSave = 0;
771         #endif
772
773         fp = cfopen(fname,"rb");
774
775         if (type == 2) {
776                 int sig;
777
778                 sig = cfile_read_int(fp);
779                 if (sig != MAKE_SIG('X','H','A','M'))
780                         return;
781                 version = cfile_read_int(fp);
782         }
783         else
784                 version = 0;
785
786         //read extra weapons
787
788         t = cfile_read_int(fp);
789         N_weapon_types = N_D2_WEAPON_TYPES+t;
790         if (N_weapon_types >= MAX_WEAPON_TYPES)
791                 Error("Too many weapons (%d) in <%s>.  Max is %d.",t,fname,MAX_WEAPON_TYPES-N_D2_WEAPON_TYPES);
792         #ifdef MACINTOSH
793                 read_weapon_info(fp, t, N_D2_WEAPON_TYPES);
794         #else
795                 cfread( &Weapon_info[N_D2_WEAPON_TYPES], sizeof(weapon_info), t, fp );
796         #endif 
797         
798         //now read robot info
799
800         t = cfile_read_int(fp);
801         N_robot_types = N_D2_ROBOT_TYPES+t;
802         if (N_robot_types >= MAX_ROBOT_TYPES)
803                 Error("Too many robots (%d) in <%s>.  Max is %d.",t,fname,MAX_ROBOT_TYPES-N_D2_ROBOT_TYPES);
804         #ifdef MACINTOSH
805                 read_robot_info(fp, t, N_D2_ROBOT_TYPES);
806         #else
807                 cfread( &Robot_info[N_D2_ROBOT_TYPES], sizeof(robot_info), t, fp );
808         #endif
809         
810         t = cfile_read_int(fp);
811         N_robot_joints = N_D2_ROBOT_JOINTS+t;
812         if (N_robot_joints >= MAX_ROBOT_JOINTS)
813                 Error("Too many robot joints (%d) in <%s>.  Max is %d.",t,fname,MAX_ROBOT_JOINTS-N_D2_ROBOT_JOINTS);
814         #ifdef MACINTOSH
815                 read_robot_joint_info(fp, t, N_D2_ROBOT_JOINTS);
816         #else
817                 cfread( &Robot_joints[N_D2_ROBOT_JOINTS], sizeof(jointpos), t, fp );
818         #endif
819         
820         t = cfile_read_int(fp);
821         N_polygon_models = N_D2_POLYGON_MODELS+t;
822         if (N_polygon_models >= MAX_POLYGON_MODELS)
823                 Error("Too many polygon models (%d) in <%s>.  Max is %d.",t,fname,MAX_POLYGON_MODELS-N_D2_POLYGON_MODELS);
824         #ifdef MACINTOSH
825                 read_polygon_models(fp, t, N_D2_POLYGON_MODELS);
826         #else
827                 cfread( &Polygon_models[N_D2_POLYGON_MODELS], sizeof(polymodel), t, fp );
828         #endif
829         
830         for (i=N_D2_POLYGON_MODELS; i<N_polygon_models; i++ )
831         {
832                 Polygon_models[i].model_data = d_malloc(Polygon_models[i].model_data_size);
833                 Assert( Polygon_models[i].model_data != NULL );
834                 cfread( Polygon_models[i].model_data, sizeof(ubyte), Polygon_models[i].model_data_size, fp );
835                 
836                 #ifdef MACINTOSH
837                         swap_polygon_model_data(Polygon_models[i].model_data);
838                 #endif
839                 
840                 g3_init_polygon_model(Polygon_models[i].model_data);
841         }
842
843         cfread( &Dying_modelnums[N_D2_POLYGON_MODELS], sizeof(int), t, fp );
844         cfread( &Dead_modelnums[N_D2_POLYGON_MODELS], sizeof(int), t, fp );
845
846         #ifdef MACINTOSH
847                 for (i = N_D2_POLYGON_MODELS; i < N_polygon_models; i++)
848                 {
849                         Dying_modelnums[i]= SWAPINT(Dying_modelnums[i]);
850                         Dead_modelnums[i]= SWAPINT(Dead_modelnums[i]);
851                 }
852         #endif
853
854         t = cfile_read_int(fp);
855         if (N_D2_OBJBITMAPS+t >= MAX_OBJ_BITMAPS)
856                 Error("Too many object bitmaps (%d) in <%s>.  Max is %d.",t,fname,MAX_OBJ_BITMAPS-N_D2_OBJBITMAPS);
857         cfread( &ObjBitmaps[N_D2_OBJBITMAPS], sizeof(bitmap_index), t, fp );
858         #ifdef MACINTOSH
859                 for (i = N_D2_OBJBITMAPS; i < (N_D2_OBJBITMAPS + t); i++)
860                 {
861                         ObjBitmaps[i].index = SWAPSHORT(ObjBitmaps[i].index);
862                 }
863         #endif
864
865         t = cfile_read_int(fp);
866         if (N_D2_OBJBITMAPPTRS+t >= MAX_OBJ_BITMAPS)
867                 Error("Too many object bitmap pointers (%d) in <%s>.  Max is %d.",t,fname,MAX_OBJ_BITMAPS-N_D2_OBJBITMAPPTRS);
868         cfread( &ObjBitmapPtrs[N_D2_OBJBITMAPPTRS], sizeof(ushort), t, fp );
869         #ifdef MACINTOSH
870                 for (i = N_D2_OBJBITMAPPTRS; i < (N_D2_OBJBITMAPPTRS + t); i++)
871                 {
872                         ObjBitmapPtrs[i] = SWAPSHORT(ObjBitmapPtrs[i]);
873                 }
874         #endif
875
876         cfclose(fp);
877 }
878
879 extern void change_filename_extension( char *dest, char *src, char *new_ext );
880
881 int Robot_replacements_loaded = 0;
882
883 void load_robot_replacements(char *level_name)
884 {
885         CFILE *fp;
886         int t,i,j;
887         char ifile_name[FILENAME_LEN];
888
889         change_filename_extension(ifile_name, level_name, ".HXM" );
890         
891         fp = cfopen(ifile_name,"rb");
892
893         if (!fp)                //no robot replacement file
894                 return;
895
896         t = cfile_read_int(fp);                 //read id "HXM!"
897         if (t!= 0x21584d48) 
898                 Error("ID of HXM! file incorrect");
899
900         t = cfile_read_int(fp);                 //read version
901         if (t<1)
902                 Error("HXM! version too old (%d)",t); 
903
904         t = cfile_read_int(fp);                 //read number of robots
905         for (j=0;j<t;j++) {
906                 i = cfile_read_int(fp);         //read robot number
907            if (i<0 || i>=N_robot_types)
908                         Error("Robots number (%d) out of range in (%s).  Range = [0..%d].",i,level_name,N_robot_types-1);
909                 #ifdef MACINTOSH
910                         read_robot_info(fp, 1, i);
911                 #else
912                         cfread( &Robot_info[i], sizeof(robot_info), 1, fp );
913                 #endif
914         }
915
916         t = cfile_read_int(fp);                 //read number of joints
917         for (j=0;j<t;j++) {
918                 i = cfile_read_int(fp);         //read joint number
919                 if (i<0 || i>=N_robot_joints)
920                         Error("Robots joint (%d) out of range in (%s).  Range = [0..%d].",i,level_name,N_robot_joints-1);
921                 #ifdef MACINTOSH
922                         read_robot_joint_info(fp, 1, i);
923                 #else
924                         cfread( &Robot_joints[i], sizeof(jointpos), 1, fp );
925                 #endif
926         }
927
928         t = cfile_read_int(fp);                 //read number of polygon models
929         for (j=0;j<t;j++)
930         {
931                 i = cfile_read_int(fp);         //read model number
932                 if (i<0 || i>=N_polygon_models)
933                         Error("Polygon model (%d) out of range in (%s).  Range = [0..%d].",i,level_name,N_polygon_models-1);
934         
935                 #ifdef MACINTOSH
936                         read_polygon_models(fp, 1, i);
937                 #else
938                         cfread( &Polygon_models[i], sizeof(polymodel), 1, fp );
939                 #endif
940         
941                 d_free(Polygon_models[i].model_data);
942                 Polygon_models[i].model_data = d_malloc(Polygon_models[i].model_data_size);
943                 Assert( Polygon_models[i].model_data != NULL );
944
945                 cfread( Polygon_models[i].model_data, sizeof(ubyte), Polygon_models[i].model_data_size, fp );
946                 #ifdef MACINTOSH
947                         swap_polygon_model_data(Polygon_models[i].model_data);
948                 #endif
949                 g3_init_polygon_model(Polygon_models[i].model_data);
950
951                 Dying_modelnums[i] = cfile_read_int(fp);
952                 Dead_modelnums[i] = cfile_read_int(fp);
953         }
954
955         t = cfile_read_int(fp);                 //read number of objbitmaps
956         for (j=0;j<t;j++) {
957                 i = cfile_read_int(fp);         //read objbitmap number
958                 if (i<0 || i>=MAX_OBJ_BITMAPS)
959                         Error("Object bitmap number (%d) out of range in (%s).  Range = [0..%d].",i,level_name,MAX_OBJ_BITMAPS-1);
960                 ObjBitmaps[i].index = cfile_read_short(fp);
961         }
962
963         t = cfile_read_int(fp);                 //read number of objbitmapptrs
964         for (j=0;j<t;j++) {
965                 i = cfile_read_int(fp);         //read objbitmapptr number
966                 if (i<0 || i>=MAX_OBJ_BITMAPS)
967                         Error("Object bitmap pointer (%d) out of range in (%s).  Range = [0..%d].",i,level_name,MAX_OBJ_BITMAPS-1);
968                 ObjBitmapPtrs[i] = cfile_read_short(fp);
969         }
970
971         cfclose(fp);
972 }