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