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