configuration fixes
[btb/d2x.git] / main / controls.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
15 #ifdef RCS
16 static char rcsid[] = "$Id: controls.c,v 1.1.1.1 2001-01-19 03:30:01 bradleyb Exp $";
17 #endif
18
19 #include <conf.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22
23 #include "pstypes.h"
24 #include "mono.h"
25 #include "key.h"
26 #include "joy.h"
27 #include "timer.h"
28 #include "error.h"
29
30 #include "inferno.h"
31 #include "game.h"
32 #include "object.h"
33 #include "player.h"
34
35 #include "controls.h"
36 #include "joydefs.h"
37 #include "render.h"
38 #include "args.h"
39 #include "palette.h"
40 #include "mouse.h"
41 #include "kconfig.h"
42 #include "laser.h"
43 #include "multi.h"
44 #include "vclip.h"
45 #include "fireball.h"
46
47 //look at keyboard, mouse, joystick, CyberMan, whatever, and set 
48 //physics vars rotvel, velocity
49
50 fix Afterburner_charge=f1_0;
51
52 #define AFTERBURNER_USE_SECS    3                               //use up in 3 seconds
53 #define DROP_DELTA_TIME                 (f1_0/15)       //drop 3 per second
54
55 extern int Drop_afterburner_blob_flag;          //ugly hack
56
57 extern fix      Seismic_tremor_magnitude;
58
59 void read_flying_controls( object * obj )
60 {
61         fix     forward_thrust_time;
62
63         Assert(FrameTime > 0);          //Get MATT if hit this!
64
65         if (Player_is_dead) {
66                 vm_vec_zero(&obj->mtype.phys_info.rotthrust);
67                 vm_vec_zero(&obj->mtype.phys_info.thrust);
68                 return;
69         }
70
71         if ((obj->type!=OBJ_PLAYER) || (obj->id!=Player_num)) return;   //references to player_ship require that this obj be the player
72
73         if (Guided_missile[Player_num] && Guided_missile[Player_num]->signature==Guided_missile_sig[Player_num]) {
74                 vms_angvec rotangs;
75                 vms_matrix rotmat,tempm;
76                 fix speed;
77
78                 //this is a horrible hack.  guided missile stuff should not be
79                 //handled in the middle of a routine that is dealing with the player
80
81                 vm_vec_zero(&obj->mtype.phys_info.rotthrust);
82
83                 rotangs.p = Controls.pitch_time / 2 + Seismic_tremor_magnitude/64;
84                 rotangs.b = Controls.bank_time / 2 + Seismic_tremor_magnitude/16;
85                 rotangs.h = Controls.heading_time / 2 + Seismic_tremor_magnitude/64;
86
87                 vm_angles_2_matrix(&rotmat,&rotangs);
88
89                 vm_matrix_x_matrix(&tempm,&Guided_missile[Player_num]->orient,&rotmat);
90
91                 Guided_missile[Player_num]->orient = tempm;
92
93                 speed = Weapon_info[Guided_missile[Player_num]->id].speed[Difficulty_level];
94
95                 vm_vec_copy_scale(&Guided_missile[Player_num]->mtype.phys_info.velocity,&Guided_missile[Player_num]->orient.fvec,speed);
96 #ifdef NETWORK
97                 if (Game_mode & GM_MULTI)
98                         multi_send_guided_info (Guided_missile[Player_num],0);
99 #endif
100
101         }
102         else {
103                 obj->mtype.phys_info.rotthrust.x = Controls.pitch_time;
104                 obj->mtype.phys_info.rotthrust.y = Controls.heading_time;
105                 obj->mtype.phys_info.rotthrust.z = Controls.bank_time;
106         }
107
108 //      mprintf( (0, "Rot thrust = %.3f,%.3f,%.3f\n", f2fl(obj->mtype.phys_info.rotthrust.x),f2fl(obj->mtype.phys_info.rotthrust.y), f2fl(obj->mtype.phys_info.rotthrust.z) ));
109
110         forward_thrust_time = Controls.forward_thrust_time;
111
112         if (Players[Player_num].flags & PLAYER_FLAGS_AFTERBURNER)
113         {
114                 if (Controls.afterburner_state) {                       //player has key down
115                         //if (forward_thrust_time >= 0) {               //..and isn't moving backward
116                         {
117                                 fix afterburner_scale;
118                                 int old_count,new_count;
119         
120                                 //add in value from 0..1
121                                 afterburner_scale = f1_0 + min(f1_0/2,Afterburner_charge) * 2;
122         
123                                 forward_thrust_time = fixmul(FrameTime,afterburner_scale);      //based on full thrust
124         
125                                 old_count = (Afterburner_charge / (DROP_DELTA_TIME/AFTERBURNER_USE_SECS));
126
127                                 Afterburner_charge -= FrameTime/AFTERBURNER_USE_SECS;
128
129                                 if (Afterburner_charge < 0)
130                                         Afterburner_charge = 0;
131
132                                 new_count = (Afterburner_charge / (DROP_DELTA_TIME/AFTERBURNER_USE_SECS));
133
134                                 if (old_count != new_count)
135                                         Drop_afterburner_blob_flag = 1; //drop blob (after physics called)
136                         }
137                 }
138                 else {
139                         fix cur_energy,charge_up;
140         
141                         //charge up to full
142                         charge_up = min(FrameTime/8,f1_0 - Afterburner_charge); //recharge over 8 seconds
143         
144                         cur_energy = max(Players[Player_num].energy-i2f(10),0); //don't drop below 10
145
146                         //maybe limit charge up by energy
147                         charge_up = min(charge_up,cur_energy/10);
148         
149                         Afterburner_charge += charge_up;
150         
151                         Players[Player_num].energy -= charge_up * 100 / 10;     //full charge uses 10% of energy
152                 }
153         }
154
155         // Set object's thrust vector for forward/backward
156         vm_vec_copy_scale(&obj->mtype.phys_info.thrust,&obj->orient.fvec, forward_thrust_time );
157         
158         // slide left/right
159         vm_vec_scale_add2(&obj->mtype.phys_info.thrust,&obj->orient.rvec, Controls.sideways_thrust_time );
160
161         // slide up/down
162         vm_vec_scale_add2(&obj->mtype.phys_info.thrust,&obj->orient.uvec, Controls.vertical_thrust_time );
163
164         if (obj->mtype.phys_info.flags & PF_WIGGLE) {
165                 fix swiggle;
166                 fix_fastsincos(GameTime, &swiggle, NULL);
167                 vm_vec_scale_add2(&obj->mtype.phys_info.velocity,&obj->orient.uvec,fixmul(swiggle,Player_ship->wiggle));
168         }
169
170         // As of now, obj->mtype.phys_info.thrust & obj->mtype.phys_info.rotthrust are 
171         // in units of time... In other words, if thrust==FrameTime, that
172         // means that the user was holding down the Max_thrust key for the
173         // whole frame.  So we just scale them up by the max, and divide by
174         // FrameTime to make them independant of framerate
175
176         //      Prevent divide overflows on high frame rates.
177         //      In a signed divide, you get an overflow if num >= div<<15
178         {
179                 fix     ft = FrameTime;
180
181                 //      Note, you must check for ft < F1_0/2, else you can get an overflow  on the << 15.
182                 if ((ft < F1_0/2) && (ft << 15 <= Player_ship->max_thrust)) {
183                         mprintf((0, "Preventing divide overflow in controls.c for Max_thrust!\n"));
184                         ft = (Player_ship->max_thrust >> 15) + 1;
185                 }
186
187                 vm_vec_scale( &obj->mtype.phys_info.thrust, fixdiv(Player_ship->max_thrust,ft) );
188
189                 if ((ft < F1_0/2) && (ft << 15 <= Player_ship->max_rotthrust)) {
190                         mprintf((0, "Preventing divide overflow in controls.c for max_rotthrust!\n"));
191                         ft = (Player_ship->max_thrust >> 15) + 1;
192                 }
193
194                 vm_vec_scale( &obj->mtype.phys_info.rotthrust, fixdiv(Player_ship->max_rotthrust,ft) );
195         }
196
197 }