home *** CD-ROM | disk | FTP | other *** search
/ Amiga ACS 1998 #6 / amigaacscoverdisc1998-061998.iso / games / descent / source / main / robot.c < prev    next >
Text File  |  1998-06-08  |  8KB  |  321 lines

  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-1998 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
  12. */
  13. /*
  14.  * $Source: f:/miner/source/main/rcs/robot.c $
  15.  * $Revision: 2.1 $
  16.  * $Author: john $
  17.  * $Date: 1995/03/07 16:52:02 $
  18.  * 
  19.  * Code for handling robots
  20.  * 
  21.  * $Log: robot.c $
  22.  * Revision 2.1  1995/03/07  16:52:02  john
  23.  * Fixed robots not moving without edtiro bug.
  24.  * 
  25.  * Revision 2.0  1995/02/27  11:31:11  john
  26.  * New version 2.0, which has no anonymous unions, builds with
  27.  * Watcom 10.0, and doesn't require parsing BITMAPS.TBL.
  28.  * 
  29.  * Revision 1.19  1995/02/22  13:58:09  allender
  30.  * remove anonymous unions from object structure
  31.  * 
  32.  * Revision 1.18  1995/01/27  11:17:06  rob
  33.  * Avoid problems with illegal gun num.
  34.  * 
  35.  * Revision 1.17  1994/11/19  15:15:02  mike
  36.  * remove unused code and data
  37.  * 
  38.  * Revision 1.16  1994/11/05  16:41:31  adam
  39.  * upped MAX_ROBOT_JOINTS
  40.  * 
  41.  * Revision 1.15  1994/09/26  15:29:29  matt
  42.  * Allow morphing objects to fire
  43.  * 
  44.  * Revision 1.14  1994/06/20  14:31:02  matt
  45.  * Don't include joint zero in animation data
  46.  * 
  47.  * Revision 1.13  1994/06/10  14:39:58  matt
  48.  * Increased limit of robot joints
  49.  * 
  50.  * Revision 1.12  1994/06/10  10:59:18  matt
  51.  * Do error checking on list of angles
  52.  * 
  53.  * Revision 1.11  1994/06/09  16:21:32  matt
  54.  * Took out special-case and test code.
  55.  * 
  56.  * Revision 1.10  1994/06/07  13:21:14  matt
  57.  * Added support for new chunk-based POF files, with robot animation data.
  58.  * 
  59.  * Revision 1.9  1994/06/01  17:58:24  mike
  60.  * Greater flinch effect.
  61.  * 
  62.  * Revision 1.8  1994/06/01  14:59:25  matt
  63.  * Fixed calc_gun_position(), which was rotating the wrong way for the
  64.  * object orientation.
  65.  * 
  66.  * Revision 1.7  1994/06/01  12:44:04  matt
  67.  * Added flinch state for test robot
  68.  * 
  69.  * Revision 1.6  1994/05/31  19:17:24  matt
  70.  * Fixed test robot angles
  71.  * 
  72.  * Revision 1.5  1994/05/30  19:43:50  mike
  73.  * Call set_test_robot.
  74.  * 
  75.  * 
  76.  * Revision 1.4  1994/05/30  00:02:44  matt
  77.  * Got rid of robot render type, and generally cleaned up polygon model
  78.  * render objects.
  79.  * 
  80.  * Revision 1.3  1994/05/29  18:46:15  matt
  81.  * Added stuff for getting robot animation info for different states
  82.  * 
  83.  * Revision 1.2  1994/05/26  21:09:15  matt
  84.  * Moved robot stuff out of polygon model and into robot_info struct
  85.  * Made new file, robot.c, to deal with robots
  86.  * 
  87.  * Revision 1.1  1994/05/26  18:02:04  matt
  88.  * Initial revision
  89.  * 
  90.  * 
  91.  */
  92.  
  93.  
  94. #pragma off (unreferenced)
  95. static char rcsid[] = "$Id: robot.c 2.1 1995/03/07 16:52:02 john Exp $";
  96. #pragma on (unreferenced)
  97.  
  98. #include "error.h"
  99.  
  100. #include "inferno.h"
  101.  
  102. #include "robot.h"
  103. #include "object.h"
  104. #include "polyobj.h"
  105. #include "mono.h"
  106.  
  107. int    N_robot_types = 0;
  108. int    N_robot_joints = 0;
  109.  
  110. //    Robot stuff
  111. robot_info Robot_info[MAX_ROBOT_TYPES];
  112.  
  113. //Big array of joint positions.  All robots index into this array
  114.  
  115. #define deg(a) ((int) (a) * 32768 / 180)
  116.  
  117. //test data for one robot
  118. jointpos Robot_joints[MAX_ROBOT_JOINTS] = {
  119.  
  120. //gun 0
  121.                     {2,{deg(-30),0,0}},        //rest (2 joints)
  122.                     {3,{deg(-40),0,0}},
  123.  
  124.                     {2,{deg(0),0,0}},            //alert
  125.                     {3,{deg(0),0,0}},
  126.         
  127.                     {2,{deg(0),0,0}},            //fire
  128.                     {3,{deg(0),0,0}},
  129.         
  130.                     {2,{deg(50),0,0}},        //recoil
  131.                     {3,{deg(-50),0,0}},
  132.         
  133.                     {2,{deg(10),0,deg(70)}},        //flinch
  134.                     {3,{deg(0),deg(20),0}},
  135.         
  136. //gun 1
  137.                     {4,{deg(-30),0,0}},        //rest (2 joints)
  138.                     {5,{deg(-40),0,0}},
  139.  
  140.                     {4,{deg(0),0,0}},            //alert
  141.                     {5,{deg(0),0,0}},
  142.         
  143.                     {4,{deg(0),0,0}},            //fire
  144.                     {5,{deg(0),0,0}},
  145.         
  146.                     {4,{deg(50),0,0}},        //recoil
  147.                     {5,{deg(-50),0,0}},
  148.         
  149.                     {4,{deg(20),0,deg(-50)}},    //flinch
  150.                     {5,{deg(0),0,deg(20)}},
  151.         
  152. //rest of body (the head)
  153.  
  154.                     {1,{deg(70),0,0}},        //rest (1 joint, head)
  155.  
  156.                     {1,{deg(0),0,0}},            //alert
  157.         
  158.                     {1,{deg(0),0,0}},            //fire
  159.         
  160.                     {1,{deg(0),0,0}},            //recoil
  161.  
  162.                     {1,{deg(-20),deg(15),0}},            //flinch
  163.  
  164.  
  165. };
  166.  
  167. //given an object and a gun number, return position in 3-space of gun
  168. //fills in gun_point
  169. void calc_gun_point(vms_vector *gun_point,object *obj,int gun_num)
  170. {
  171.     polymodel *pm;
  172.     robot_info *r;
  173.     vms_vector pnt;
  174.     vms_matrix m;
  175.     int mn;                //submodel number
  176.  
  177.     Assert(obj->render_type==RT_POLYOBJ || obj->render_type==RT_MORPH);
  178.     Assert(obj->id < N_robot_types);
  179.  
  180.     r = &Robot_info[obj->id];
  181.     pm =&Polygon_models[r->model_num];
  182.  
  183.     if (gun_num >= r->n_guns)
  184.     {
  185.         mprintf((1, "Bashing gun num %d to 0.\n", gun_num));
  186.         Int3();
  187.         gun_num = 0;
  188.     }
  189.  
  190. //    Assert(gun_num < r->n_guns);
  191.  
  192.     pnt = r->gun_points[gun_num];
  193.     mn = r->gun_submodels[gun_num];
  194.  
  195.     //instance up the tree for this gun
  196.     while (mn != 0) {
  197.         vms_vector tpnt;
  198.  
  199.         vm_angles_2_matrix(&m,&obj->rtype.pobj_info.anim_angles[mn]);
  200.         vm_transpose_matrix(&m);
  201.         vm_vec_rotate(&tpnt,&pnt,&m);
  202.  
  203.         vm_vec_add(&pnt,&tpnt,&pm->submodel_offsets[mn]);
  204.  
  205.         mn = pm->submodel_parents[mn];
  206.     }
  207.  
  208.     //now instance for the entire object
  209.  
  210.     vm_copy_transpose_matrix(&m,&obj->orient);
  211.     vm_vec_rotate(gun_point,&pnt,&m);
  212.     vm_vec_add2(gun_point,&obj->pos);
  213.     
  214. }
  215.  
  216. //fills in ptr to list of joints, and returns the number of joints in list
  217. //takes the robot type (object id), gun number, and desired state
  218. int robot_get_anim_state(jointpos **jp_list_ptr,int robot_type,int gun_num,int state)
  219. {
  220.  
  221.     Assert(gun_num <= Robot_info[robot_type].n_guns);
  222.  
  223.     *jp_list_ptr = &Robot_joints[Robot_info[robot_type].anim_states[gun_num][state].offset];
  224.  
  225.     return Robot_info[robot_type].anim_states[gun_num][state].n_joints;
  226.  
  227. }
  228.  
  229.  
  230. //for test, set a robot to a specific state
  231. set_robot_state(object *obj,int state)
  232. {
  233.     int g,j,jo;
  234.     robot_info *ri;
  235.     jointlist *jl;
  236.  
  237.     Assert(obj->type == OBJ_ROBOT);
  238.  
  239.     ri = &Robot_info[obj->id];
  240.  
  241.     for (g=0;g<ri->n_guns+1;g++) {
  242.  
  243.         jl = &ri->anim_states[g][state];
  244.  
  245.         jo = jl->offset;
  246.  
  247.         for (j=0;j<jl->n_joints;j++,jo++) {
  248.             int jn;
  249.  
  250.             jn = Robot_joints[jo].jointnum;
  251.  
  252.             obj->rtype.pobj_info.anim_angles[jn] = Robot_joints[jo].angles;
  253.  
  254.         }
  255.     }
  256. }
  257.  
  258. #include "mono.h"
  259.  
  260. //--unused-- int cur_state=0;
  261.  
  262. //--unused-- test_anim_states()
  263. //--unused-- {
  264. //--unused--     set_robot_state(&Objects[1],cur_state);
  265. //--unused-- 
  266. //--unused--     mprintf(0,"Robot in state %d\n",cur_state);
  267. //--unused-- 
  268. //--unused--     cur_state = (cur_state+1)%N_ANIM_STATES;
  269. //--unused-- 
  270. //--unused-- }
  271.  
  272. //set the animation angles for this robot.  Gun fields of robot info must
  273. //be filled in.
  274. robot_set_angles(robot_info *r,polymodel *pm,vms_angvec angs[N_ANIM_STATES][MAX_SUBMODELS])
  275. {
  276.     int m,g,state;
  277.     int gun_nums[MAX_SUBMODELS];            //which gun each submodel is part of
  278.  
  279.     for (m=0;m<pm->n_models;m++)
  280.         gun_nums[m] = r->n_guns;        //assume part of body...
  281.  
  282.     gun_nums[0] = -1;        //body never animates, at least for now
  283.  
  284.     for (g=0;g<r->n_guns;g++) {
  285.         m = r->gun_submodels[g];
  286.  
  287.         while (m != 0) {
  288.             gun_nums[m] = g;                //...unless we find it in a gun
  289.             m = pm->submodel_parents[m];
  290.         }
  291.     }
  292.  
  293.     for (g=0;g<r->n_guns+1;g++) {
  294.  
  295.         //mprintf(0,"Gun %d:\n",g);
  296.  
  297.         for (state=0;state<N_ANIM_STATES;state++) {
  298.  
  299.             //mprintf(0," State %d:\n",state);
  300.  
  301.             r->anim_states[g][state].n_joints = 0;
  302.             r->anim_states[g][state].offset = N_robot_joints;
  303.  
  304.             for (m=0;m<pm->n_models;m++) {
  305.                 if (gun_nums[m] == g) {
  306.                     //mprintf(0,"  Joint %d: %x %x %x\n",m,angs[state][m].pitch,angs[state][m].bank,angs[state][m].head);
  307.                     Robot_joints[N_robot_joints].jointnum = m;
  308.                     Robot_joints[N_robot_joints].angles = angs[state][m];
  309.                     r->anim_states[g][state].n_joints++;
  310.                     N_robot_joints++;
  311.                     Assert(N_robot_joints < MAX_ROBOT_JOINTS);
  312.                 }
  313.             }
  314.         }
  315.     }
  316.  
  317. }
  318.  
  319.  
  320. 
  321.