This commit was generated by cvs2svn to compensate for changes in r2,
[btb/d2x.git] / main / slew.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 #ifdef RCS
15 static char rcsid[] = "$Id: slew.c,v 1.1.1.1 2001-01-19 03:30:01 bradleyb Exp $";
16 #endif
17
18 #include <conf.h>
19 #include <stdlib.h>
20
21 #include "inferno.h"
22 #include "game.h"
23 #include "vecmat.h"
24 #include "key.h"
25 #include "joy.h"
26 #include "object.h"
27 #include "error.h"
28 #include "physics.h"
29 #include "joydefs.h"
30 #include "kconfig.h"
31 #include "args.h"
32
33 //variables for slew system
34
35 object *slew_obj=NULL;  //what object is slewing, or NULL if none
36
37 #define JOY_NULL 15
38 #define ROT_SPEED 8             //rate of rotation while key held down
39 #define VEL_SPEED (2*55)        //rate of acceleration while key held down
40
41 short old_joy_x,old_joy_y;      //position last time around
42
43 //      Function Prototypes
44 int slew_stop(void);
45
46
47 // -------------------------------------------------------------------
48 //say start slewing with this object
49 void slew_init(object *obj)
50 {
51         slew_obj = obj;
52          
53         slew_obj->control_type = CT_SLEW;
54         slew_obj->movement_type = MT_NONE;
55
56         slew_stop();            //make sure not moving
57 }
58
59
60 int slew_stop()
61 {
62         if (!slew_obj || slew_obj->control_type!=CT_SLEW) return 0;
63
64         vm_vec_zero(&slew_obj->mtype.phys_info.velocity);
65         return 1;
66 }
67
68 void slew_reset_orient()
69 {
70         if (!slew_obj || slew_obj->control_type!=CT_SLEW) return;
71
72         slew_obj->orient.rvec.x = slew_obj->orient.uvec.y = slew_obj->orient.fvec.z = f1_0;
73
74         slew_obj->orient.rvec.y = slew_obj->orient.rvec.z = slew_obj->orient.uvec.x = 
75    slew_obj->orient.uvec.z = slew_obj->orient.fvec.x = slew_obj->orient.fvec.y = 0;
76
77 }
78
79 int do_slew_movement(object *obj, int check_keys, int check_joy )
80 {
81         int moved = 0;
82         vms_vector svel, movement;                              //scaled velocity (per this frame)
83         vms_matrix rotmat,new_pm;
84         int joy_x,joy_y,btns;
85         int joyx_moved,joyy_moved;
86         vms_angvec rotang;
87
88         if (!slew_obj || slew_obj->control_type!=CT_SLEW) return 0;
89
90         if (check_keys) {
91                 if (Function_mode == FMODE_EDITOR) {
92                         if (args_find("-jasen"))
93                                 obj->mtype.phys_info.velocity.x += VEL_SPEED * (key_down_time(KEY_PAD3) - key_down_time(KEY_PAD1));
94                         else
95                                 obj->mtype.phys_info.velocity.x += VEL_SPEED * (key_down_time(KEY_PAD9) - key_down_time(KEY_PAD7));
96                         obj->mtype.phys_info.velocity.y += VEL_SPEED * (key_down_time(KEY_PADMINUS) - key_down_time(KEY_PADPLUS));
97                         obj->mtype.phys_info.velocity.z += VEL_SPEED * (key_down_time(KEY_PAD8) - key_down_time(KEY_PAD2));
98
99                         rotang.p = (key_down_time(KEY_LBRACKET) - key_down_time(KEY_RBRACKET))/ROT_SPEED ;
100                         if (args_find("-jasen"))
101                                 rotang.b  = (key_down_time(KEY_PAD7) - key_down_time(KEY_PAD9))/ROT_SPEED;
102                         else
103                                 rotang.b  = (key_down_time(KEY_PAD1) - key_down_time(KEY_PAD3))/ROT_SPEED;
104                         rotang.h  = (key_down_time(KEY_PAD6) - key_down_time(KEY_PAD4))/ROT_SPEED;
105                 }
106                 else {
107                         obj->mtype.phys_info.velocity.x += VEL_SPEED * Controls.sideways_thrust_time;
108                         obj->mtype.phys_info.velocity.y += VEL_SPEED * Controls.vertical_thrust_time;
109                         obj->mtype.phys_info.velocity.z += VEL_SPEED * Controls.forward_thrust_time;
110
111                         rotang.p = Controls.pitch_time/ROT_SPEED ;
112                         rotang.b  = Controls.bank_time/ROT_SPEED;
113                         rotang.h  = Controls.heading_time/ROT_SPEED;
114                 }
115         }
116         else
117                 rotang.p = rotang.b  = rotang.h  = 0;
118
119         //check for joystick movement
120
121         if (check_joy && joy_present && (Function_mode == FMODE_EDITOR) )       {
122                 joy_get_pos(&joy_x,&joy_y);
123                 btns=joy_get_btns();
124         
125                 joyx_moved = (abs(joy_x - old_joy_x)>JOY_NULL);
126                 joyy_moved = (abs(joy_y - old_joy_y)>JOY_NULL);
127         
128                 if (abs(joy_x) < JOY_NULL) joy_x = 0;
129                 if (abs(joy_y) < JOY_NULL) joy_y = 0;
130         
131                 if (btns)
132                         if (!rotang.p) rotang.p = fixmul(-joy_y * 512,FrameTime); else;
133                 else
134                         if (joyy_moved) obj->mtype.phys_info.velocity.z = -joy_y * 8192;
135         
136                 if (!rotang.h) rotang.h = fixmul(joy_x * 512,FrameTime);
137         
138                 if (joyx_moved) old_joy_x = joy_x;
139                 if (joyy_moved) old_joy_y = joy_y;
140         }
141
142         moved = rotang.p | rotang.b | rotang.h;
143
144         vm_angles_2_matrix(&rotmat,&rotang);
145         vm_matrix_x_matrix(&new_pm,&obj->orient,&rotmat);
146         obj->orient = new_pm;
147         vm_transpose_matrix(&new_pm);           //make those columns rows
148
149         moved |= obj->mtype.phys_info.velocity.x | obj->mtype.phys_info.velocity.y | obj->mtype.phys_info.velocity.z;
150
151         svel = obj->mtype.phys_info.velocity;
152         vm_vec_scale(&svel,FrameTime);          //movement in this frame
153         vm_vec_rotate(&movement,&svel,&new_pm);
154
155 //      obj->last_pos = obj->pos;
156         vm_vec_add2(&obj->pos,&movement);
157
158         moved |= (movement.x || movement.y || movement.z);
159
160         if (moved) 
161                 update_object_seg(obj); //update segment id
162
163         return moved;
164 }
165
166 //do slew for this frame
167 int slew_frame(int check_keys)
168 {
169         return do_slew_movement( slew_obj, !check_keys, 1 );
170
171 }
172