added -udp documentation from d1x
[btb/d2x.git] / main / robot.c
1 /* $Id: robot.c,v 1.5 2003-10-10 09:36:35 btb Exp $ */
2 /*
3 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
4 SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
5 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
6 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
7 IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
8 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
9 FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
10 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
11 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
12 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
13 */
14
15 /*
16  *
17  * Code for handling robots
18  *
19  * Old Log:
20  * Revision 1.1  1995/05/16  15:30:34  allender
21  * Initial revision
22  *
23  * Revision 2.1  1995/03/07  16:52:02  john
24  * Fixed robots not moving without edtiro bug.
25  *
26  * Revision 2.0  1995/02/27  11:31:11  john
27  * New version 2.0, which has no anonymous unions, builds with
28  * Watcom 10.0, and doesn't require parsing BITMAPS.TBL.
29  *
30  * Revision 1.19  1995/02/22  13:58:09  allender
31  * remove anonymous unions from object structure
32  *
33  * Revision 1.18  1995/01/27  11:17:06  rob
34  * Avoid problems with illegal gun num.
35  *
36  * Revision 1.17  1994/11/19  15:15:02  mike
37  * remove unused code and data
38  *
39  * Revision 1.16  1994/11/05  16:41:31  adam
40  * upped MAX_ROBOT_JOINTS
41  *
42  * Revision 1.15  1994/09/26  15:29:29  matt
43  * Allow morphing objects to fire
44  *
45  * Revision 1.14  1994/06/20  14:31:02  matt
46  * Don't include joint zero in animation data
47  *
48  * Revision 1.13  1994/06/10  14:39:58  matt
49  * Increased limit of robot joints
50  *
51  * Revision 1.12  1994/06/10  10:59:18  matt
52  * Do error checking on list of angles
53  *
54  * Revision 1.11  1994/06/09  16:21:32  matt
55  * Took out special-case and test code.
56  *
57  * Revision 1.10  1994/06/07  13:21:14  matt
58  * Added support for new chunk-based POF files, with robot animation data.
59  *
60  * Revision 1.9  1994/06/01  17:58:24  mike
61  * Greater flinch effect.
62  *
63  * Revision 1.8  1994/06/01  14:59:25  matt
64  * Fixed calc_gun_position(), which was rotating the wrong way for the
65  * object orientation.
66  *
67  * Revision 1.7  1994/06/01  12:44:04  matt
68  * Added flinch state for test robot
69  *
70  * Revision 1.6  1994/05/31  19:17:24  matt
71  * Fixed test robot angles
72  *
73  * Revision 1.5  1994/05/30  19:43:50  mike
74  * Call set_test_robot.
75  *
76  *
77  * Revision 1.4  1994/05/30  00:02:44  matt
78  * Got rid of robot render type, and generally cleaned up polygon model
79  * render objects.
80  *
81  * Revision 1.3  1994/05/29  18:46:15  matt
82  * Added stuff for getting robot animation info for different states
83  *
84  * Revision 1.2  1994/05/26  21:09:15  matt
85  * Moved robot stuff out of polygon model and into robot_info struct
86  * Made new file, robot.c, to deal with robots
87  *
88  * Revision 1.1  1994/05/26  18:02:04  matt
89  * Initial revision
90  *
91  *
92  */
93
94
95 #ifdef HAVE_CONFIG_H
96 #include <conf.h>
97 #endif
98
99 #include <stdio.h>
100
101 #include "error.h"
102
103 #include "inferno.h"
104
105 #include "robot.h"
106 #include "object.h"
107 #include "polyobj.h"
108 #include "mono.h"
109
110 int     N_robot_types = 0;
111 int     N_robot_joints = 0;
112
113 //      Robot stuff
114 robot_info Robot_info[MAX_ROBOT_TYPES];
115
116 //Big array of joint positions.  All robots index into this array
117
118 #define deg(a) ((int) (a) * 32768 / 180)
119
120 //test data for one robot
121 jointpos Robot_joints[MAX_ROBOT_JOINTS] = {
122
123 //gun 0
124         {2,{deg(-30),0,0}},         //rest (2 joints)
125         {3,{deg(-40),0,0}},
126
127         {2,{deg(0),0,0}},           //alert
128         {3,{deg(0),0,0}},
129
130         {2,{deg(0),0,0}},           //fire
131         {3,{deg(0),0,0}},
132
133         {2,{deg(50),0,0}},          //recoil
134         {3,{deg(-50),0,0}},
135
136         {2,{deg(10),0,deg(70)}},    //flinch
137         {3,{deg(0),deg(20),0}},
138
139 //gun 1
140         {4,{deg(-30),0,0}},         //rest (2 joints)
141         {5,{deg(-40),0,0}},
142
143         {4,{deg(0),0,0}},           //alert
144         {5,{deg(0),0,0}},
145
146         {4,{deg(0),0,0}},           //fire
147         {5,{deg(0),0,0}},
148
149         {4,{deg(50),0,0}},          //recoil
150         {5,{deg(-50),0,0}},
151
152         {4,{deg(20),0,deg(-50)}},   //flinch
153         {5,{deg(0),0,deg(20)}},
154
155 //rest of body (the head)
156
157         {1,{deg(70),0,0}},          //rest (1 joint, head)
158
159         {1,{deg(0),0,0}},           //alert
160
161         {1,{deg(0),0,0}},           //fire
162
163         {1,{deg(0),0,0}},           //recoil
164
165         {1,{deg(-20),deg(15),0}},   //flinch
166
167 };
168
169 //given an object and a gun number, return position in 3-space of gun
170 //fills in gun_point
171 void calc_gun_point(vms_vector *gun_point,object *obj,int gun_num)
172 {
173         polymodel *pm;
174         robot_info *r;
175         vms_vector pnt;
176         vms_matrix m;
177         int mn;                         //submodel number
178
179         Assert(obj->render_type==RT_POLYOBJ || obj->render_type==RT_MORPH);
180         Assert(obj->id < N_robot_types);
181
182         r = &Robot_info[obj->id];
183         pm =&Polygon_models[r->model_num];
184
185         if (gun_num >= r->n_guns)
186         {
187                 mprintf((1, "Bashing gun num %d to 0.\n", gun_num));
188                 //Int3();
189                 gun_num = 0;
190         }
191
192 //      Assert(gun_num < r->n_guns);
193
194         pnt = r->gun_points[gun_num];
195         mn = r->gun_submodels[gun_num];
196
197         //instance up the tree for this gun
198         while (mn != 0) {
199                 vms_vector tpnt;
200
201                 vm_angles_2_matrix(&m,&obj->rtype.pobj_info.anim_angles[mn]);
202                 vm_transpose_matrix(&m);
203                 vm_vec_rotate(&tpnt,&pnt,&m);
204
205                 vm_vec_add(&pnt,&tpnt,&pm->submodel_offsets[mn]);
206
207                 mn = pm->submodel_parents[mn];
208         }
209
210         //now instance for the entire object
211
212         vm_copy_transpose_matrix(&m,&obj->orient);
213         vm_vec_rotate(gun_point,&pnt,&m);
214         vm_vec_add2(gun_point,&obj->pos);
215
216 }
217
218 //fills in ptr to list of joints, and returns the number of joints in list
219 //takes the robot type (object id), gun number, and desired state
220 int robot_get_anim_state(jointpos **jp_list_ptr,int robot_type,int gun_num,int state)
221 {
222
223         Assert(gun_num <= Robot_info[robot_type].n_guns);
224
225         *jp_list_ptr = &Robot_joints[Robot_info[robot_type].anim_states[gun_num][state].offset];
226
227         return Robot_info[robot_type].anim_states[gun_num][state].n_joints;
228
229 }
230
231
232 //for test, set a robot to a specific state
233 void set_robot_state(object *obj,int state)
234 {
235         int g,j,jo;
236         robot_info *ri;
237         jointlist *jl;
238
239         Assert(obj->type == OBJ_ROBOT);
240
241         ri = &Robot_info[obj->id];
242
243         for (g=0;g<ri->n_guns+1;g++) {
244
245                 jl = &ri->anim_states[g][state];
246
247                 jo = jl->offset;
248
249                 for (j=0;j<jl->n_joints;j++,jo++) {
250                         int jn;
251
252                         jn = Robot_joints[jo].jointnum;
253
254                         obj->rtype.pobj_info.anim_angles[jn] = Robot_joints[jo].angles;
255
256                 }
257         }
258 }
259
260 #include "mono.h"
261
262 //--unused-- int cur_state=0;
263
264 //--unused-- test_anim_states()
265 //--unused-- {
266 //--unused--    set_robot_state(&Objects[1],cur_state);
267 //--unused--
268 //--unused--    mprintf(0,"Robot in state %d\n",cur_state);
269 //--unused--
270 //--unused--    cur_state = (cur_state+1)%N_ANIM_STATES;
271 //--unused--
272 //--unused-- }
273
274 //set the animation angles for this robot.  Gun fields of robot info must
275 //be filled in.
276 void robot_set_angles(robot_info *r,polymodel *pm,vms_angvec angs[N_ANIM_STATES][MAX_SUBMODELS])
277 {
278         int m,g,state;
279         int gun_nums[MAX_SUBMODELS];                    //which gun each submodel is part of
280
281         for (m=0;m<pm->n_models;m++)
282                 gun_nums[m] = r->n_guns;                //assume part of body...
283
284         gun_nums[0] = -1;               //body never animates, at least for now
285
286         for (g=0;g<r->n_guns;g++) {
287                 m = r->gun_submodels[g];
288
289                 while (m != 0) {
290                         gun_nums[m] = g;                                //...unless we find it in a gun
291                         m = pm->submodel_parents[m];
292                 }
293         }
294
295         for (g=0;g<r->n_guns+1;g++) {
296
297                 //mprintf(0,"Gun %d:\n",g);
298
299                 for (state=0;state<N_ANIM_STATES;state++) {
300
301                         //mprintf(0," State %d:\n",state);
302
303                         r->anim_states[g][state].n_joints = 0;
304                         r->anim_states[g][state].offset = N_robot_joints;
305
306                         for (m=0;m<pm->n_models;m++) {
307                                 if (gun_nums[m] == g) {
308                                         //mprintf(0,"  Joint %d: %x %x %x\n",m,angs[state][m].pitch,angs[state][m].bank,angs[state][m].head);
309                                         Robot_joints[N_robot_joints].jointnum = m;
310                                         Robot_joints[N_robot_joints].angles = angs[state][m];
311                                         r->anim_states[g][state].n_joints++;
312                                         N_robot_joints++;
313                                         Assert(N_robot_joints < MAX_ROBOT_JOINTS);
314                                 }
315                         }
316                 }
317         }
318
319 }
320
321 #ifndef FAST_FILE_IO
322 /*
323  * reads n jointlist structs from a CFILE
324  */
325 static int jointlist_read_n(jointlist *jl, int n, CFILE *fp)
326 {
327         int i;
328
329         for (i = 0; i < n; i++) {
330                 jl[i].n_joints = cfile_read_short(fp);
331                 jl[i].offset = cfile_read_short(fp);
332         }
333         return i;
334 }
335
336 /*
337  * reads n robot_info structs from a CFILE
338  */
339 int robot_info_read_n(robot_info *ri, int n, CFILE *fp)
340 {
341         int i, j;
342
343         for (i = 0; i < n; i++) {
344                 ri[i].model_num = cfile_read_int(fp);
345                 for (j = 0; j < MAX_GUNS; j++)
346                         cfile_read_vector(&(ri[i].gun_points[j]), fp);
347                 cfread(ri[i].gun_submodels, MAX_GUNS, 1, fp);
348
349                 ri[i].exp1_vclip_num = cfile_read_short(fp);
350                 ri[i].exp1_sound_num = cfile_read_short(fp);
351
352                 ri[i].exp2_vclip_num = cfile_read_short(fp);
353                 ri[i].exp2_sound_num = cfile_read_short(fp);
354
355                 ri[i].weapon_type = cfile_read_byte(fp);
356                 ri[i].weapon_type2 = cfile_read_byte(fp);
357                 ri[i].n_guns = cfile_read_byte(fp);
358                 ri[i].contains_id = cfile_read_byte(fp);
359
360                 ri[i].contains_count = cfile_read_byte(fp);
361                 ri[i].contains_prob = cfile_read_byte(fp);
362                 ri[i].contains_type = cfile_read_byte(fp);
363                 ri[i].kamikaze = cfile_read_byte(fp);
364
365                 ri[i].score_value = cfile_read_short(fp);
366                 ri[i].badass = cfile_read_byte(fp);
367                 ri[i].energy_drain = cfile_read_byte(fp);
368
369                 ri[i].lighting = cfile_read_fix(fp);
370                 ri[i].strength = cfile_read_fix(fp);
371
372                 ri[i].mass = cfile_read_fix(fp);
373                 ri[i].drag = cfile_read_fix(fp);
374
375                 for (j = 0; j < NDL; j++)
376                         ri[i].field_of_view[j] = cfile_read_fix(fp);
377                 for (j = 0; j < NDL; j++)
378                         ri[i].firing_wait[j] = cfile_read_fix(fp);
379                 for (j = 0; j < NDL; j++)
380                         ri[i].firing_wait2[j] = cfile_read_fix(fp);
381                 for (j = 0; j < NDL; j++)
382                         ri[i].turn_time[j] = cfile_read_fix(fp);
383                 for (j = 0; j < NDL; j++)
384                         ri[i].max_speed[j] = cfile_read_fix(fp);
385                 for (j = 0; j < NDL; j++)
386                         ri[i].circle_distance[i] = cfile_read_fix(fp);
387                 cfread(ri[i].rapidfire_count, NDL, 1, fp);
388
389                 cfread(ri[i].evade_speed, NDL, 1, fp);
390
391                 ri[i].cloak_type = cfile_read_byte(fp);
392                 ri[i].attack_type = cfile_read_byte(fp);
393
394                 ri[i].see_sound = cfile_read_byte(fp);
395                 ri[i].attack_sound = cfile_read_byte(fp);
396                 ri[i].claw_sound = cfile_read_byte(fp);
397                 ri[i].taunt_sound = cfile_read_byte(fp);
398
399                 ri[i].boss_flag = cfile_read_byte(fp);
400                 ri[i].companion = cfile_read_byte(fp);
401                 ri[i].smart_blobs = cfile_read_byte(fp);
402                 ri[i].energy_blobs = cfile_read_byte(fp);
403
404                 ri[i].thief = cfile_read_byte(fp);
405                 ri[i].pursuit = cfile_read_byte(fp);
406                 ri[i].lightcast = cfile_read_byte(fp);
407                 ri[i].death_roll = cfile_read_byte(fp);
408
409                 ri[i].flags = cfile_read_byte(fp);
410                 cfread(ri[i].pad, 3, 1, fp);
411
412                 ri[i].deathroll_sound = cfile_read_byte(fp);
413                 ri[i].glow = cfile_read_byte(fp);
414                 ri[i].behavior = cfile_read_byte(fp);
415                 ri[i].aim = cfile_read_byte(fp);
416
417                 for (j = 0; j < MAX_GUNS + 1; j++)
418                         jointlist_read_n(ri[i].anim_states[j], N_ANIM_STATES, fp);
419
420                 ri[i].always_0xabcd = cfile_read_int(fp);
421         }
422         return i;
423 }
424
425 /*
426  * reads n jointpos structs from a CFILE
427  */
428 int jointpos_read_n(jointpos *jp, int n, CFILE *fp)
429 {
430         int i;
431
432         for (i = 0; i < n; i++) {
433                 jp[i].jointnum = cfile_read_short(fp);
434                 cfile_read_angvec(&jp[i].angles, fp);
435         }
436         return i;
437 }
438 #endif