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