| 1 |
/* |
| 2 |
* Portions of this file are copyright Rebirth contributors and licensed as |
| 3 |
* described in COPYING.txt. |
| 4 |
* Portions of this file are copyright Parallax Software and licensed |
| 5 |
* according to the Parallax license below. |
| 6 |
* See COPYING.txt for license details. |
| 7 |
|
| 8 |
THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX |
| 9 |
SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO |
| 10 |
END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A |
| 11 |
ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS |
| 12 |
IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS |
| 13 |
SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE |
| 14 |
FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE |
| 15 |
CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS |
| 16 |
AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE. |
| 17 |
COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED. |
| 18 |
*/ |
| 19 |
|
| 20 |
/* |
| 21 |
* |
| 22 |
* Code for controlling player movement |
| 23 |
* |
| 24 |
*/ |
| 25 |
|
| 26 |
|
| 27 |
#include <algorithm> |
| 28 |
#include <stdlib.h> |
| 29 |
#include "joy.h" |
| 30 |
#include "dxxerror.h" |
| 31 |
#include "inferno.h" |
| 32 |
#include "game.h" |
| 33 |
#include "object.h" |
| 34 |
#include "player.h" |
| 35 |
#include "weapon.h" |
| 36 |
#include "bm.h" |
| 37 |
#include "controls.h" |
| 38 |
#include "palette.h" |
| 39 |
#include "kconfig.h" |
| 40 |
#if DXX_BUILD_DESCENT == 2 |
| 41 |
#include "laser.h" |
| 42 |
#include "multi.h" |
| 43 |
#include "vclip.h" |
| 44 |
#include "fireball.h" |
| 45 |
#include "d_levelstate.h" |
| 46 |
|
| 47 |
//look at keyboard, mouse, joystick, CyberMan, whatever, and set |
| 48 |
//physics vars rotvel, velocity |
| 49 |
|
| 50 |
#define AFTERBURNER_USE_SECS 3 //use up in 3 seconds |
| 51 |
#define DROP_DELTA_TIME (f1_0/15) //drop 3 per second |
| 52 |
#endif |
| 53 |
|
| 54 |
using std::min; |
| 55 |
using std::max; |
| 56 |
|
| 57 |
namespace dsx { |
| 58 |
|
| 59 |
#if DXX_BUILD_DESCENT == 2 |
| 60 |
fix Afterburner_charge; |
| 61 |
#endif |
| 62 |
|
| 63 |
void read_flying_controls(object &obj, control_info &Controls) |
| 64 |
{ |
| 65 |
fix forward_thrust_time; |
| 66 |
|
| 67 |
Assert(FrameTime > 0); //Get MATT if hit this! |
| 68 |
|
| 69 |
#if DXX_BUILD_DESCENT == 2 |
| 70 |
if (obj.type != OBJ_PLAYER || get_player_id(obj) != Player_num) |
| 71 |
return; //references to player_ship require that this obj be the player |
| 72 |
|
| 73 |
const auto control_guided_missile = [&] { |
| 74 |
const auto &&gimobj = LevelUniqueObjectState.Guided_missile.get_player_active_guided_missile(LevelUniqueObjectState.get_objects().vmptr, Player_num); |
| 75 |
if (gimobj == nullptr) |
| 76 |
return false; |
| 77 |
auto &gmobj = *gimobj; |
| 78 |
const auto gmid = get_weapon_id(gmobj); |
| 79 |
const auto speed = Weapon_info[gmid].speed[GameUniqueState.Difficulty_level]; |
| 80 |
vms_angvec rotangs; |
| 81 |
//this is a horrible hack. guided missile stuff should not be |
| 82 |
//handled in the middle of a routine that is dealing with the player |
| 83 |
|
| 84 |
obj.mtype.phys_info.rotthrust = {}; |
| 85 |
|
| 86 |
const auto Seismic_tremor_magnitude = LevelUniqueSeismicState.Seismic_tremor_magnitude; |
| 87 |
rotangs.p = Controls.pitch_time / 2 + Seismic_tremor_magnitude/64; |
| 88 |
rotangs.b = Controls.bank_time / 2 + Seismic_tremor_magnitude/16; |
| 89 |
rotangs.h = Controls.heading_time / 2 + Seismic_tremor_magnitude/64; |
| 90 |
|
| 91 |
const auto &&rotmat{vm_angles_2_matrix(rotangs)}; |
| 92 |
gmobj.orient = vm_matrix_x_matrix(gmobj.orient, rotmat); |
| 93 |
|
| 94 |
gmobj.mtype.phys_info.velocity = vm_vec_copy_scale(gmobj.orient.fvec, speed); |
| 95 |
if (Game_mode & GM_MULTI) |
| 96 |
multi_send_guided_info(gmobj, 0); |
| 97 |
return true; |
| 98 |
}; |
| 99 |
if (!control_guided_missile()) |
| 100 |
#endif |
| 101 |
{ |
| 102 |
auto &rotthrust = obj.mtype.phys_info.rotthrust; |
| 103 |
rotthrust.x = Controls.pitch_time; |
| 104 |
rotthrust.y = Controls.heading_time; |
| 105 |
rotthrust.z = Controls.bank_time; |
| 106 |
} |
| 107 |
|
| 108 |
forward_thrust_time = Controls.forward_thrust_time; |
| 109 |
|
| 110 |
#if DXX_BUILD_DESCENT == 2 |
| 111 |
auto &player_info = obj.ctype.player_info; |
| 112 |
if (player_info.powerup_flags & PLAYER_FLAGS_AFTERBURNER) |
| 113 |
{ |
| 114 |
if (Controls.state.afterburner) { //player has key down |
| 115 |
{ |
| 116 |
fix afterburner_scale; |
| 117 |
int old_count,new_count; |
| 118 |
|
| 119 |
//add in value from 0..1 |
| 120 |
afterburner_scale = f1_0 + min(f1_0/2,Afterburner_charge) * 2; |
| 121 |
|
| 122 |
forward_thrust_time = fixmul(FrameTime,afterburner_scale); //based on full thrust |
| 123 |
|
| 124 |
old_count = (Afterburner_charge / (DROP_DELTA_TIME/AFTERBURNER_USE_SECS)); |
| 125 |
|
| 126 |
Afterburner_charge -= FrameTime/AFTERBURNER_USE_SECS; |
| 127 |
|
| 128 |
if (Afterburner_charge < 0) |
| 129 |
Afterburner_charge = 0; |
| 130 |
|
| 131 |
new_count = (Afterburner_charge / (DROP_DELTA_TIME/AFTERBURNER_USE_SECS)); |
| 132 |
|
| 133 |
if (old_count != new_count) |
| 134 |
Drop_afterburner_blob_flag = 1; //drop blob (after physics called) |
| 135 |
} |
| 136 |
} |
| 137 |
else { |
| 138 |
fix cur_energy,charge_up; |
| 139 |
|
| 140 |
//charge up to full |
| 141 |
charge_up = min(FrameTime/8,f1_0 - Afterburner_charge); //recharge over 8 seconds |
| 142 |
|
| 143 |
auto &energy = player_info.energy; |
| 144 |
cur_energy = max(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 |
energy -= charge_up * 100 / 10; //full charge uses 10% of energy |
| 152 |
} |
| 153 |
} |
| 154 |
#endif |
| 155 |
|
| 156 |
// Set object's thrust vector for forward/backward |
| 157 |
obj.mtype.phys_info.thrust = vm_vec_copy_scale(obj.orient.fvec, forward_thrust_time); |
| 158 |
|
| 159 |
// slide left/right |
| 160 |
vm_vec_scale_add2(obj.mtype.phys_info.thrust, obj.orient.rvec, Controls.sideways_thrust_time ); |
| 161 |
|
| 162 |
// slide up/down |
| 163 |
vm_vec_scale_add2(obj.mtype.phys_info.thrust, obj.orient.uvec, Controls.vertical_thrust_time ); |
| 164 |
|
| 165 |
if (obj.mtype.phys_info.flags & PF_WIGGLE) |
| 166 |
{ |
| 167 |
auto swiggle = fix_fastsin(static_cast<fix>(GameTime64)); |
| 168 |
if (FrameTime < F1_0) // Only scale wiggle if getting at least 1 FPS, to avoid causing the opposite problem. |
| 169 |
swiggle = fixmul(swiggle*DESIGNATED_GAME_FPS, FrameTime); //make wiggle fps-independent (based on pre-scaled amount of wiggle at DESIGNATED_GAME_FPS) |
| 170 |
vm_vec_scale_add2(obj.mtype.phys_info.velocity, obj.orient.uvec, fixmul(swiggle, Player_ship->wiggle)); |
| 171 |
} |
| 172 |
|
| 173 |
// As of now, obj.mtype.phys_info.thrust & obj->mtype.phys_info.rotthrust are |
| 174 |
// in units of time... In other words, if thrust==FrameTime, that |
| 175 |
// means that the user was holding down the Max_thrust key for the |
| 176 |
// whole frame. So we just scale them up by the max, and divide by |
| 177 |
// FrameTime to make them independant of framerate |
| 178 |
|
| 179 |
// Prevent divide overflows on high frame rates. |
| 180 |
// In a signed divide, you get an overflow if num >= div<<15 |
| 181 |
{ |
| 182 |
fix ft = FrameTime; |
| 183 |
|
| 184 |
// Note, you must check for ft < F1_0/2, else you can get an overflow on the << 15. |
| 185 |
if ((ft < F1_0/2) && (ft << 15 <= Player_ship->max_thrust)) { |
| 186 |
ft = (Player_ship->max_thrust >> 15) + 1; |
| 187 |
} |
| 188 |
|
| 189 |
vm_vec_scale(obj.mtype.phys_info.thrust, fixdiv(Player_ship->max_thrust, ft)); |
| 190 |
|
| 191 |
if ((ft < F1_0/2) && (ft << 15 <= Player_ship->max_rotthrust)) { |
| 192 |
ft = (Player_ship->max_thrust >> 15) + 1; |
| 193 |
} |
| 194 |
|
| 195 |
vm_vec_scale(obj.mtype.phys_info.rotthrust, fixdiv(Player_ship->max_rotthrust, ft)); |
| 196 |
} |
| 197 |
|
| 198 |
// moved here by WraithX |
| 199 |
if (Player_dead_state != player_dead_state::no) |
| 200 |
{ |
| 201 |
obj.mtype.phys_info.thrust = {}; // don't let dead players move, changed by WraithX |
| 202 |
return; |
| 203 |
}// end if |
| 204 |
|
| 205 |
} |
| 206 |
} |