Fix crash if Num_walls=0
[btb/d2x.git] / main / slew.c
1 /* $Id: slew.c,v 1.5 2004-08-28 23:17:45 schaffner Exp $ */
2 /*
3 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
4 SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
5 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
6 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
7 IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
8 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
9 FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
10 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
11 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
12 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
13 */
14
15 /*
16  *
17  * Basic slew system for moving around the mine
18  *
19  */
20
21
22 #ifdef HAVE_CONFIG_H
23 #include <conf.h>
24 #endif
25
26 #ifdef RCS
27 static char rcsid[] = "$Id: slew.c,v 1.5 2004-08-28 23:17:45 schaffner Exp $";
28 #endif
29
30 #include <stdlib.h>
31
32 #include "inferno.h"
33 #include "game.h"
34 #include "vecmat.h"
35 #include "key.h"
36 #include "joy.h"
37 #include "object.h"
38 #include "error.h"
39 #include "physics.h"
40 #include "joydefs.h"
41 #include "kconfig.h"
42 #include "args.h"
43
44 //variables for slew system
45
46 object *slew_obj=NULL;  //what object is slewing, or NULL if none
47
48 #define JOY_NULL 15
49 #define ROT_SPEED 8             //rate of rotation while key held down
50 #define VEL_SPEED (2*55)        //rate of acceleration while key held down
51
52 short old_joy_x,old_joy_y;      //position last time around
53
54 //      Function Prototypes
55 int slew_stop(void);
56
57
58 // -------------------------------------------------------------------
59 //say start slewing with this object
60 void slew_init(object *obj)
61 {
62         slew_obj = obj;
63         
64         slew_obj->control_type = CT_SLEW;
65         slew_obj->movement_type = MT_NONE;
66
67         slew_stop();            //make sure not moving
68 }
69
70
71 int slew_stop()
72 {
73         if (!slew_obj || slew_obj->control_type!=CT_SLEW) return 0;
74
75         vm_vec_zero(&slew_obj->mtype.phys_info.velocity);
76         return 1;
77 }
78
79 void slew_reset_orient()
80 {
81         if (!slew_obj || slew_obj->control_type!=CT_SLEW) return;
82
83         slew_obj->orient.rvec.x = slew_obj->orient.uvec.y = slew_obj->orient.fvec.z = f1_0;
84
85         slew_obj->orient.rvec.y = slew_obj->orient.rvec.z = slew_obj->orient.uvec.x =
86    slew_obj->orient.uvec.z = slew_obj->orient.fvec.x = slew_obj->orient.fvec.y = 0;
87
88 }
89
90 int do_slew_movement(object *obj, int check_keys, int check_joy )
91 {
92         int moved = 0;
93         vms_vector svel, movement;                              //scaled velocity (per this frame)
94         vms_matrix rotmat,new_pm;
95         int joy_x,joy_y,btns;
96         int joyx_moved,joyy_moved;
97         vms_angvec rotang;
98
99         if (!slew_obj || slew_obj->control_type!=CT_SLEW) return 0;
100
101         if (check_keys) {
102                 if (Function_mode == FMODE_EDITOR) {
103                         if (FindArg("-jasen"))
104                                 obj->mtype.phys_info.velocity.x += VEL_SPEED * (key_down_time(KEY_PAD3) - key_down_time(KEY_PAD1));
105                         else
106                                 obj->mtype.phys_info.velocity.x += VEL_SPEED * (key_down_time(KEY_PAD9) - key_down_time(KEY_PAD7));
107                         obj->mtype.phys_info.velocity.y += VEL_SPEED * (key_down_time(KEY_PADMINUS) - key_down_time(KEY_PADPLUS));
108                         obj->mtype.phys_info.velocity.z += VEL_SPEED * (key_down_time(KEY_PAD8) - key_down_time(KEY_PAD2));
109
110                         rotang.p = (key_down_time(KEY_LBRACKET) - key_down_time(KEY_RBRACKET))/ROT_SPEED ;
111                         if (FindArg("-jasen"))
112                                 rotang.b  = (key_down_time(KEY_PAD7) - key_down_time(KEY_PAD9))/ROT_SPEED;
113                         else
114                                 rotang.b  = (key_down_time(KEY_PAD1) - key_down_time(KEY_PAD3))/ROT_SPEED;
115                         rotang.h  = (key_down_time(KEY_PAD6) - key_down_time(KEY_PAD4))/ROT_SPEED;
116                 }
117                 else {
118                         obj->mtype.phys_info.velocity.x += VEL_SPEED * Controls.sideways_thrust_time;
119                         obj->mtype.phys_info.velocity.y += VEL_SPEED * Controls.vertical_thrust_time;
120                         obj->mtype.phys_info.velocity.z += VEL_SPEED * Controls.forward_thrust_time;
121
122                         rotang.p = Controls.pitch_time/ROT_SPEED ;
123                         rotang.b  = Controls.bank_time/ROT_SPEED;
124                         rotang.h  = Controls.heading_time/ROT_SPEED;
125                 }
126         }
127         else
128                 rotang.p = rotang.b  = rotang.h  = 0;
129
130         //check for joystick movement
131
132         if (check_joy && joy_present && (Function_mode == FMODE_EDITOR) )       {
133                 joy_get_pos(&joy_x,&joy_y);
134                 btns=joy_get_btns();
135         
136                 joyx_moved = (abs(joy_x - old_joy_x)>JOY_NULL);
137                 joyy_moved = (abs(joy_y - old_joy_y)>JOY_NULL);
138         
139                 if (abs(joy_x) < JOY_NULL) joy_x = 0;
140                 if (abs(joy_y) < JOY_NULL) joy_y = 0;
141         
142                 if (btns)
143                         if (!rotang.p) rotang.p = fixmul(-joy_y * 512,FrameTime); else;
144                 else
145                         if (joyy_moved) obj->mtype.phys_info.velocity.z = -joy_y * 8192;
146         
147                 if (!rotang.h) rotang.h = fixmul(joy_x * 512,FrameTime);
148         
149                 if (joyx_moved) old_joy_x = joy_x;
150                 if (joyy_moved) old_joy_y = joy_y;
151         }
152
153         moved = rotang.p | rotang.b | rotang.h;
154
155         vm_angles_2_matrix(&rotmat,&rotang);
156         vm_matrix_x_matrix(&new_pm,&obj->orient,&rotmat);
157         obj->orient = new_pm;
158         vm_transpose_matrix(&new_pm);           //make those columns rows
159
160         moved |= obj->mtype.phys_info.velocity.x | obj->mtype.phys_info.velocity.y | obj->mtype.phys_info.velocity.z;
161
162         svel = obj->mtype.phys_info.velocity;
163         vm_vec_scale(&svel,FrameTime);          //movement in this frame
164         vm_vec_rotate(&movement,&svel,&new_pm);
165
166 //      obj->last_pos = obj->pos;
167         vm_vec_add2(&obj->pos,&movement);
168
169         moved |= (movement.x || movement.y || movement.z);
170
171         if (moved)
172                 update_object_seg(obj); //update segment id
173
174         return moved;
175 }
176
177 //do slew for this frame
178 int slew_frame(int check_keys)
179 {
180         return do_slew_movement( slew_obj, !check_keys, 1 );
181
182 }
183