Fix crash if Num_walls=0
[btb/d2x.git] / main / robot.c
1 /* $Id: robot.c,v 1.6 2004-08-28 23:17:45 schaffner Exp $ */
2 /*
3 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
4 SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
5 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
6 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
7 IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
8 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
9 FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
10 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
11 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
12 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
13 */
14
15 /*
16  *
17  * Code for handling robots
18  *
19  */
20
21
22 #ifdef HAVE_CONFIG_H
23 #include <conf.h>
24 #endif
25
26 #include <stdio.h>
27
28 #include "error.h"
29
30 #include "inferno.h"
31
32 #include "robot.h"
33 #include "object.h"
34 #include "polyobj.h"
35 #include "mono.h"
36
37 int     N_robot_types = 0;
38 int     N_robot_joints = 0;
39
40 //      Robot stuff
41 robot_info Robot_info[MAX_ROBOT_TYPES];
42
43 //Big array of joint positions.  All robots index into this array
44
45 #define deg(a) ((int) (a) * 32768 / 180)
46
47 //test data for one robot
48 jointpos Robot_joints[MAX_ROBOT_JOINTS] = {
49
50 //gun 0
51         {2,{deg(-30),0,0}},         //rest (2 joints)
52         {3,{deg(-40),0,0}},
53
54         {2,{deg(0),0,0}},           //alert
55         {3,{deg(0),0,0}},
56
57         {2,{deg(0),0,0}},           //fire
58         {3,{deg(0),0,0}},
59
60         {2,{deg(50),0,0}},          //recoil
61         {3,{deg(-50),0,0}},
62
63         {2,{deg(10),0,deg(70)}},    //flinch
64         {3,{deg(0),deg(20),0}},
65
66 //gun 1
67         {4,{deg(-30),0,0}},         //rest (2 joints)
68         {5,{deg(-40),0,0}},
69
70         {4,{deg(0),0,0}},           //alert
71         {5,{deg(0),0,0}},
72
73         {4,{deg(0),0,0}},           //fire
74         {5,{deg(0),0,0}},
75
76         {4,{deg(50),0,0}},          //recoil
77         {5,{deg(-50),0,0}},
78
79         {4,{deg(20),0,deg(-50)}},   //flinch
80         {5,{deg(0),0,deg(20)}},
81
82 //rest of body (the head)
83
84         {1,{deg(70),0,0}},          //rest (1 joint, head)
85
86         {1,{deg(0),0,0}},           //alert
87
88         {1,{deg(0),0,0}},           //fire
89
90         {1,{deg(0),0,0}},           //recoil
91
92         {1,{deg(-20),deg(15),0}},   //flinch
93
94 };
95
96 //given an object and a gun number, return position in 3-space of gun
97 //fills in gun_point
98 void calc_gun_point(vms_vector *gun_point,object *obj,int gun_num)
99 {
100         polymodel *pm;
101         robot_info *r;
102         vms_vector pnt;
103         vms_matrix m;
104         int mn;                         //submodel number
105
106         Assert(obj->render_type==RT_POLYOBJ || obj->render_type==RT_MORPH);
107         Assert(obj->id < N_robot_types);
108
109         r = &Robot_info[obj->id];
110         pm =&Polygon_models[r->model_num];
111
112         if (gun_num >= r->n_guns)
113         {
114                 mprintf((1, "Bashing gun num %d to 0.\n", gun_num));
115                 //Int3();
116                 gun_num = 0;
117         }
118
119 //      Assert(gun_num < r->n_guns);
120
121         pnt = r->gun_points[gun_num];
122         mn = r->gun_submodels[gun_num];
123
124         //instance up the tree for this gun
125         while (mn != 0) {
126                 vms_vector tpnt;
127
128                 vm_angles_2_matrix(&m,&obj->rtype.pobj_info.anim_angles[mn]);
129                 vm_transpose_matrix(&m);
130                 vm_vec_rotate(&tpnt,&pnt,&m);
131
132                 vm_vec_add(&pnt,&tpnt,&pm->submodel_offsets[mn]);
133
134                 mn = pm->submodel_parents[mn];
135         }
136
137         //now instance for the entire object
138
139         vm_copy_transpose_matrix(&m,&obj->orient);
140         vm_vec_rotate(gun_point,&pnt,&m);
141         vm_vec_add2(gun_point,&obj->pos);
142
143 }
144
145 //fills in ptr to list of joints, and returns the number of joints in list
146 //takes the robot type (object id), gun number, and desired state
147 int robot_get_anim_state(jointpos **jp_list_ptr,int robot_type,int gun_num,int state)
148 {
149
150         Assert(gun_num <= Robot_info[robot_type].n_guns);
151
152         *jp_list_ptr = &Robot_joints[Robot_info[robot_type].anim_states[gun_num][state].offset];
153
154         return Robot_info[robot_type].anim_states[gun_num][state].n_joints;
155
156 }
157
158
159 //for test, set a robot to a specific state
160 void set_robot_state(object *obj,int state)
161 {
162         int g,j,jo;
163         robot_info *ri;
164         jointlist *jl;
165
166         Assert(obj->type == OBJ_ROBOT);
167
168         ri = &Robot_info[obj->id];
169
170         for (g=0;g<ri->n_guns+1;g++) {
171
172                 jl = &ri->anim_states[g][state];
173
174                 jo = jl->offset;
175
176                 for (j=0;j<jl->n_joints;j++,jo++) {
177                         int jn;
178
179                         jn = Robot_joints[jo].jointnum;
180
181                         obj->rtype.pobj_info.anim_angles[jn] = Robot_joints[jo].angles;
182
183                 }
184         }
185 }
186
187 #include "mono.h"
188
189 //--unused-- int cur_state=0;
190
191 //--unused-- test_anim_states()
192 //--unused-- {
193 //--unused--    set_robot_state(&Objects[1],cur_state);
194 //--unused--
195 //--unused--    mprintf(0,"Robot in state %d\n",cur_state);
196 //--unused--
197 //--unused--    cur_state = (cur_state+1)%N_ANIM_STATES;
198 //--unused--
199 //--unused-- }
200
201 //set the animation angles for this robot.  Gun fields of robot info must
202 //be filled in.
203 void robot_set_angles(robot_info *r,polymodel *pm,vms_angvec angs[N_ANIM_STATES][MAX_SUBMODELS])
204 {
205         int m,g,state;
206         int gun_nums[MAX_SUBMODELS];                    //which gun each submodel is part of
207
208         for (m=0;m<pm->n_models;m++)
209                 gun_nums[m] = r->n_guns;                //assume part of body...
210
211         gun_nums[0] = -1;               //body never animates, at least for now
212
213         for (g=0;g<r->n_guns;g++) {
214                 m = r->gun_submodels[g];
215
216                 while (m != 0) {
217                         gun_nums[m] = g;                                //...unless we find it in a gun
218                         m = pm->submodel_parents[m];
219                 }
220         }
221
222         for (g=0;g<r->n_guns+1;g++) {
223
224                 //mprintf(0,"Gun %d:\n",g);
225
226                 for (state=0;state<N_ANIM_STATES;state++) {
227
228                         //mprintf(0," State %d:\n",state);
229
230                         r->anim_states[g][state].n_joints = 0;
231                         r->anim_states[g][state].offset = N_robot_joints;
232
233                         for (m=0;m<pm->n_models;m++) {
234                                 if (gun_nums[m] == g) {
235                                         //mprintf(0,"  Joint %d: %x %x %x\n",m,angs[state][m].pitch,angs[state][m].bank,angs[state][m].head);
236                                         Robot_joints[N_robot_joints].jointnum = m;
237                                         Robot_joints[N_robot_joints].angles = angs[state][m];
238                                         r->anim_states[g][state].n_joints++;
239                                         N_robot_joints++;
240                                         Assert(N_robot_joints < MAX_ROBOT_JOINTS);
241                                 }
242                         }
243                 }
244         }
245
246 }
247
248 #ifndef FAST_FILE_IO
249 /*
250  * reads n jointlist structs from a CFILE
251  */
252 static int jointlist_read_n(jointlist *jl, int n, CFILE *fp)
253 {
254         int i;
255
256         for (i = 0; i < n; i++) {
257                 jl[i].n_joints = cfile_read_short(fp);
258                 jl[i].offset = cfile_read_short(fp);
259         }
260         return i;
261 }
262
263 /*
264  * reads n robot_info structs from a CFILE
265  */
266 int robot_info_read_n(robot_info *ri, int n, CFILE *fp)
267 {
268         int i, j;
269
270         for (i = 0; i < n; i++) {
271                 ri[i].model_num = cfile_read_int(fp);
272                 for (j = 0; j < MAX_GUNS; j++)
273                         cfile_read_vector(&(ri[i].gun_points[j]), fp);
274                 cfread(ri[i].gun_submodels, MAX_GUNS, 1, fp);
275
276                 ri[i].exp1_vclip_num = cfile_read_short(fp);
277                 ri[i].exp1_sound_num = cfile_read_short(fp);
278
279                 ri[i].exp2_vclip_num = cfile_read_short(fp);
280                 ri[i].exp2_sound_num = cfile_read_short(fp);
281
282                 ri[i].weapon_type = cfile_read_byte(fp);
283                 ri[i].weapon_type2 = cfile_read_byte(fp);
284                 ri[i].n_guns = cfile_read_byte(fp);
285                 ri[i].contains_id = cfile_read_byte(fp);
286
287                 ri[i].contains_count = cfile_read_byte(fp);
288                 ri[i].contains_prob = cfile_read_byte(fp);
289                 ri[i].contains_type = cfile_read_byte(fp);
290                 ri[i].kamikaze = cfile_read_byte(fp);
291
292                 ri[i].score_value = cfile_read_short(fp);
293                 ri[i].badass = cfile_read_byte(fp);
294                 ri[i].energy_drain = cfile_read_byte(fp);
295
296                 ri[i].lighting = cfile_read_fix(fp);
297                 ri[i].strength = cfile_read_fix(fp);
298
299                 ri[i].mass = cfile_read_fix(fp);
300                 ri[i].drag = cfile_read_fix(fp);
301
302                 for (j = 0; j < NDL; j++)
303                         ri[i].field_of_view[j] = cfile_read_fix(fp);
304                 for (j = 0; j < NDL; j++)
305                         ri[i].firing_wait[j] = cfile_read_fix(fp);
306                 for (j = 0; j < NDL; j++)
307                         ri[i].firing_wait2[j] = cfile_read_fix(fp);
308                 for (j = 0; j < NDL; j++)
309                         ri[i].turn_time[j] = cfile_read_fix(fp);
310                 for (j = 0; j < NDL; j++)
311                         ri[i].max_speed[j] = cfile_read_fix(fp);
312                 for (j = 0; j < NDL; j++)
313                         ri[i].circle_distance[i] = cfile_read_fix(fp);
314                 cfread(ri[i].rapidfire_count, NDL, 1, fp);
315
316                 cfread(ri[i].evade_speed, NDL, 1, fp);
317
318                 ri[i].cloak_type = cfile_read_byte(fp);
319                 ri[i].attack_type = cfile_read_byte(fp);
320
321                 ri[i].see_sound = cfile_read_byte(fp);
322                 ri[i].attack_sound = cfile_read_byte(fp);
323                 ri[i].claw_sound = cfile_read_byte(fp);
324                 ri[i].taunt_sound = cfile_read_byte(fp);
325
326                 ri[i].boss_flag = cfile_read_byte(fp);
327                 ri[i].companion = cfile_read_byte(fp);
328                 ri[i].smart_blobs = cfile_read_byte(fp);
329                 ri[i].energy_blobs = cfile_read_byte(fp);
330
331                 ri[i].thief = cfile_read_byte(fp);
332                 ri[i].pursuit = cfile_read_byte(fp);
333                 ri[i].lightcast = cfile_read_byte(fp);
334                 ri[i].death_roll = cfile_read_byte(fp);
335
336                 ri[i].flags = cfile_read_byte(fp);
337                 cfread(ri[i].pad, 3, 1, fp);
338
339                 ri[i].deathroll_sound = cfile_read_byte(fp);
340                 ri[i].glow = cfile_read_byte(fp);
341                 ri[i].behavior = cfile_read_byte(fp);
342                 ri[i].aim = cfile_read_byte(fp);
343
344                 for (j = 0; j < MAX_GUNS + 1; j++)
345                         jointlist_read_n(ri[i].anim_states[j], N_ANIM_STATES, fp);
346
347                 ri[i].always_0xabcd = cfile_read_int(fp);
348         }
349         return i;
350 }
351
352 /*
353  * reads n jointpos structs from a CFILE
354  */
355 int jointpos_read_n(jointpos *jp, int n, CFILE *fp)
356 {
357         int i;
358
359         for (i = 0; i < n; i++) {
360                 jp[i].jointnum = cfile_read_short(fp);
361                 cfile_read_angvec(&jp[i].angles, fp);
362         }
363         return i;
364 }
365 #endif