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