]> icculus.org git repositories - btb/d2x.git/blob - main/slew.c
landscape fixes, implemented gr_check_mode
[btb/d2x.git] / main / slew.c
1 /* $Id: slew.c,v 1.4 2003-10-10 09:36:35 btb 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  * Old Log:
20  * Revision 1.1  1995/05/16  15:30:57  allender
21  * Initial revision
22  *
23  * Revision 2.0  1995/02/27  11:29:32  john
24  * New version 2.0, which has no anonymous unions, builds with
25  * Watcom 10.0, and doesn't require parsing BITMAPS.TBL.
26  *
27  * Revision 1.34  1995/02/22  14:23:28  allender
28  * remove anonymous unions from object structure
29  *
30  * Revision 1.33  1995/02/22  13:24:26  john
31  * Removed the vecmat anonymous unions.
32  *
33  * Revision 1.32  1994/09/10  15:46:42  john
34  * First version of new keyboard configuration.
35  *
36  * Revision 1.31  1994/08/31  18:29:58  matt
37  * Made slew work with new key system
38  *
39  * Revision 1.30  1994/08/31  14:10:48  john
40  * Made slew go faster.
41  *
42  * Revision 1.29  1994/08/29  19:16:38  matt
43  * Made slew object not have physics movement type, so slew objects don't
44  * get bumped.
45  *
46  * Revision 1.28  1994/08/24  18:59:59  john
47  * Changed key_down_time to return fixed seconds instead of
48  * milliseconds.
49  *
50  * Revision 1.27  1994/07/01  11:33:05  john
51  * Fixed bug with looking for stick even if one not present.
52  *
53  * Revision 1.26  1994/05/20  11:56:33  matt
54  * Cleaned up find_vector_intersection() interface
55  * Killed check_point_in_seg(), check_player_seg(), check_object_seg()
56  *
57  * Revision 1.25  1994/05/19  12:08:41  matt
58  * Use new vecmat macros and globals
59  *
60  * Revision 1.24  1994/05/14  17:16:18  matt
61  * Got rid of externs in source (non-header) files
62  *
63  * Revision 1.23  1994/05/03  12:26:38  matt
64  * Removed use of physics_info var rotvel, which wasn't used for rotational
65  * velocity at all.
66  *
67  * Revision 1.22  1994/02/17  11:32:34  matt
68  * Changes in object system
69  *
70  * Revision 1.21  1994/01/18  14:03:53  john
71  * made joy_get_pos use the new ints instead of
72  * shorts.
73  *
74  * Revision 1.20  1994/01/10  17:11:35  mike
75  * Add prototype for check_object_seg
76  *
77  * Revision 1.19  1994/01/05  10:53:38  john
78  * New object code by John.
79  *
80  * Revision 1.18  1993/12/22  15:32:50  john
81  * took out previos code that attempted to make
82  * modifiers cancel keydowntime.
83  *
84  * Revision 1.17  1993/12/22  11:41:56  john
85  * Made so that keydowntime recognizes editor special case!
86  *
87  * Revision 1.16  1993/12/14  18:13:52  matt
88  * Made slew work in editor even when game isn't in slew mode
89  *
90  * Revision 1.15  1993/12/07  23:53:39  matt
91  * Made slew work in editor even when game isn't in slew mode
92  *
93  * Revision 1.14  1993/12/05  22:47:49  matt
94  * Reworked include files in an attempt to cut down on build times
95  *
96  * Revision 1.13  1993/12/01  11:44:14  matt
97  * Chagned Frfract to FrameTime
98  *
99  * Revision 1.12  1993/11/08  16:21:42  john
100  * made stop_slew or whatever return an int
101  *
102  * Revision 1.11  1993/11/01  13:59:49  john
103  * more slew experiments.
104  *
105  */
106
107
108 #ifdef HAVE_CONFIG_H
109 #include <conf.h>
110 #endif
111
112 #ifdef RCS
113 static char rcsid[] = "$Id: slew.c,v 1.4 2003-10-10 09:36:35 btb Exp $";
114 #endif
115
116 #include <stdlib.h>
117
118 #include "inferno.h"
119 #include "game.h"
120 #include "vecmat.h"
121 #include "key.h"
122 #include "joy.h"
123 #include "object.h"
124 #include "error.h"
125 #include "physics.h"
126 #include "joydefs.h"
127 #include "kconfig.h"
128 #include "args.h"
129
130 //variables for slew system
131
132 object *slew_obj=NULL;  //what object is slewing, or NULL if none
133
134 #define JOY_NULL 15
135 #define ROT_SPEED 8             //rate of rotation while key held down
136 #define VEL_SPEED (2*55)        //rate of acceleration while key held down
137
138 short old_joy_x,old_joy_y;      //position last time around
139
140 //      Function Prototypes
141 int slew_stop(void);
142
143
144 // -------------------------------------------------------------------
145 //say start slewing with this object
146 void slew_init(object *obj)
147 {
148         slew_obj = obj;
149         
150         slew_obj->control_type = CT_SLEW;
151         slew_obj->movement_type = MT_NONE;
152
153         slew_stop();            //make sure not moving
154 }
155
156
157 int slew_stop()
158 {
159         if (!slew_obj || slew_obj->control_type!=CT_SLEW) return 0;
160
161         vm_vec_zero(&slew_obj->mtype.phys_info.velocity);
162         return 1;
163 }
164
165 void slew_reset_orient()
166 {
167         if (!slew_obj || slew_obj->control_type!=CT_SLEW) return;
168
169         slew_obj->orient.rvec.x = slew_obj->orient.uvec.y = slew_obj->orient.fvec.z = f1_0;
170
171         slew_obj->orient.rvec.y = slew_obj->orient.rvec.z = slew_obj->orient.uvec.x =
172    slew_obj->orient.uvec.z = slew_obj->orient.fvec.x = slew_obj->orient.fvec.y = 0;
173
174 }
175
176 int do_slew_movement(object *obj, int check_keys, int check_joy )
177 {
178         int moved = 0;
179         vms_vector svel, movement;                              //scaled velocity (per this frame)
180         vms_matrix rotmat,new_pm;
181         int joy_x,joy_y,btns;
182         int joyx_moved,joyy_moved;
183         vms_angvec rotang;
184
185         if (!slew_obj || slew_obj->control_type!=CT_SLEW) return 0;
186
187         if (check_keys) {
188                 if (Function_mode == FMODE_EDITOR) {
189                         if (FindArg("-jasen"))
190                                 obj->mtype.phys_info.velocity.x += VEL_SPEED * (key_down_time(KEY_PAD3) - key_down_time(KEY_PAD1));
191                         else
192                                 obj->mtype.phys_info.velocity.x += VEL_SPEED * (key_down_time(KEY_PAD9) - key_down_time(KEY_PAD7));
193                         obj->mtype.phys_info.velocity.y += VEL_SPEED * (key_down_time(KEY_PADMINUS) - key_down_time(KEY_PADPLUS));
194                         obj->mtype.phys_info.velocity.z += VEL_SPEED * (key_down_time(KEY_PAD8) - key_down_time(KEY_PAD2));
195
196                         rotang.p = (key_down_time(KEY_LBRACKET) - key_down_time(KEY_RBRACKET))/ROT_SPEED ;
197                         if (FindArg("-jasen"))
198                                 rotang.b  = (key_down_time(KEY_PAD7) - key_down_time(KEY_PAD9))/ROT_SPEED;
199                         else
200                                 rotang.b  = (key_down_time(KEY_PAD1) - key_down_time(KEY_PAD3))/ROT_SPEED;
201                         rotang.h  = (key_down_time(KEY_PAD6) - key_down_time(KEY_PAD4))/ROT_SPEED;
202                 }
203                 else {
204                         obj->mtype.phys_info.velocity.x += VEL_SPEED * Controls.sideways_thrust_time;
205                         obj->mtype.phys_info.velocity.y += VEL_SPEED * Controls.vertical_thrust_time;
206                         obj->mtype.phys_info.velocity.z += VEL_SPEED * Controls.forward_thrust_time;
207
208                         rotang.p = Controls.pitch_time/ROT_SPEED ;
209                         rotang.b  = Controls.bank_time/ROT_SPEED;
210                         rotang.h  = Controls.heading_time/ROT_SPEED;
211                 }
212         }
213         else
214                 rotang.p = rotang.b  = rotang.h  = 0;
215
216         //check for joystick movement
217
218         if (check_joy && joy_present && (Function_mode == FMODE_EDITOR) )       {
219                 joy_get_pos(&joy_x,&joy_y);
220                 btns=joy_get_btns();
221         
222                 joyx_moved = (abs(joy_x - old_joy_x)>JOY_NULL);
223                 joyy_moved = (abs(joy_y - old_joy_y)>JOY_NULL);
224         
225                 if (abs(joy_x) < JOY_NULL) joy_x = 0;
226                 if (abs(joy_y) < JOY_NULL) joy_y = 0;
227         
228                 if (btns)
229                         if (!rotang.p) rotang.p = fixmul(-joy_y * 512,FrameTime); else;
230                 else
231                         if (joyy_moved) obj->mtype.phys_info.velocity.z = -joy_y * 8192;
232         
233                 if (!rotang.h) rotang.h = fixmul(joy_x * 512,FrameTime);
234         
235                 if (joyx_moved) old_joy_x = joy_x;
236                 if (joyy_moved) old_joy_y = joy_y;
237         }
238
239         moved = rotang.p | rotang.b | rotang.h;
240
241         vm_angles_2_matrix(&rotmat,&rotang);
242         vm_matrix_x_matrix(&new_pm,&obj->orient,&rotmat);
243         obj->orient = new_pm;
244         vm_transpose_matrix(&new_pm);           //make those columns rows
245
246         moved |= obj->mtype.phys_info.velocity.x | obj->mtype.phys_info.velocity.y | obj->mtype.phys_info.velocity.z;
247
248         svel = obj->mtype.phys_info.velocity;
249         vm_vec_scale(&svel,FrameTime);          //movement in this frame
250         vm_vec_rotate(&movement,&svel,&new_pm);
251
252 //      obj->last_pos = obj->pos;
253         vm_vec_add2(&obj->pos,&movement);
254
255         moved |= (movement.x || movement.y || movement.z);
256
257         if (moved)
258                 update_object_seg(obj); //update segment id
259
260         return moved;
261 }
262
263 //do slew for this frame
264 int slew_frame(int check_keys)
265 {
266         return do_slew_movement( slew_obj, !check_keys, 1 );
267
268 }
269