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