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