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