Debian Codegraph @

similar/main/slew.cpp

raw
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 * Basic slew system for moving around the mine
23 *
24 */
25
26 #include <stdlib.h>
27
28 #include "inferno.h"
29 #include "game.h"
30 #include "vecmat.h"
31 #include "joy.h"
32 #include "object.h"
33 #include "kconfig.h"
34 #include "slew.h"
35 #include "segment.h"
36 #include "d_levelstate.h"
37
38
39 //variables for slew system
40
41 object *slew_obj=NULL; //what object is slewing, or NULL if none
42
43 // -------------------------------------------------------------------
44 //say start slewing with this object
45 void slew_init(const vmobjptr_t obj)
46 {
47 slew_obj = obj;
48
49 slew_obj->control_source = object::control_type::slew;
50 slew_obj->movement_source = object::movement_type::None;
51
52 slew_stop(); //make sure not moving
53 }
54
55
56 int slew_stop()
57 {
58 if (!slew_obj || slew_obj->control_source != object::control_type::slew)
59 return 0;
60
61 slew_obj->mtype.phys_info.velocity = {};
62 return 1;
63 }
64
65 void slew_reset_orient()
66 {
67 if (!slew_obj || slew_obj->control_source != object::control_type::slew)
68 return;
69
70 slew_obj->orient.rvec.x = slew_obj->orient.uvec.y = slew_obj->orient.fvec.z = f1_0;
71
72 slew_obj->orient.rvec.y = slew_obj->orient.rvec.z = slew_obj->orient.uvec.x =
73 slew_obj->orient.uvec.z = slew_obj->orient.fvec.x = slew_obj->orient.fvec.y = 0;
74
75 }
76
77 namespace dsx {
78
79 namespace {
80
81 static int do_slew_movement(const vmobjptridx_t obj, int check_keys, const control_info &Controls)
82 {
83 auto &Objects = LevelUniqueObjectState.Objects;
84 auto &vmobjptr = Objects.vmptr;
85 constexpr int ROT_SPEED = 2; //rate of rotation while key held down
86 constexpr int SLIDE_SPEED = 700;
87 constexpr int ZOOM_SPEED_FACTOR = 1000; //(1500)
88
89 int moved{0};
90 vms_vector svel; //scaled velocity (per this frame)
91 vms_angvec rotang;
92
93 if (!slew_obj || slew_obj->control_source != object::control_type::slew)
94 return 0;
95
96 if (check_keys) {
97 #if 0 //def EDITOR // might be useful for people with playing keys set to modifiers or such,
98 if (EditorWindow) // or just use a separate player file for the editor
99 {
100 obj->mtype.phys_info.velocity.x += SLIDE_SPEED * keyd_pressed[KEY_PAD9] * FrameTime;
101 obj->mtype.phys_info.velocity.x -= SLIDE_SPEED * keyd_pressed[KEY_PAD7] * FrameTime;
102 obj->mtype.phys_info.velocity.y += SLIDE_SPEED * keyd_pressed[KEY_PADMINUS] * FrameTime;
103 obj->mtype.phys_info.velocity.y -= SLIDE_SPEED * keyd_pressed[KEY_PADPLUS] * FrameTime;
104 obj->mtype.phys_info.velocity.z += ZOOM_SPEED_FACTOR * keyd_pressed[KEY_PAD8] * FrameTime;
105 obj->mtype.phys_info.velocity.z -= ZOOM_SPEED_FACTOR * keyd_pressed[KEY_PAD2] * FrameTime;
106
107 rotang.p = rotang.b = rotang.h = 0;
108 rotang.p += keyd_pressed[KEY_LBRACKET] * FrameTime / ROT_SPEED;
109 rotang.p -= keyd_pressed[KEY_RBRACKET] * FrameTime / ROT_SPEED;
110 rotang.b += keyd_pressed[KEY_PAD1] * FrameTime / ROT_SPEED;
111 rotang.b -= keyd_pressed[KEY_PAD3] * FrameTime / ROT_SPEED;
112 rotang.h += keyd_pressed[KEY_PAD6] * FrameTime / ROT_SPEED;
113 rotang.h -= keyd_pressed[KEY_PAD4] * FrameTime / ROT_SPEED;
114 }
115 else
116 #endif
117 {
118 obj->mtype.phys_info.velocity.x = SLIDE_SPEED * Controls.sideways_thrust_time;
119 obj->mtype.phys_info.velocity.y = SLIDE_SPEED * Controls.vertical_thrust_time;
120 obj->mtype.phys_info.velocity.z = ZOOM_SPEED_FACTOR * 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 moved = rotang.p | rotang.b | rotang.h;
131
132 const auto &&rotmat{vm_angles_2_matrix(rotang)};
133 const auto new_pm = vm_transposed_matrix(obj->orient = vm_matrix_x_matrix(obj->orient,rotmat)); //make those columns rows
134
135 moved |= obj->mtype.phys_info.velocity.x | obj->mtype.phys_info.velocity.y | obj->mtype.phys_info.velocity.z;
136
137 svel = obj->mtype.phys_info.velocity;
138 vm_vec_scale(svel,FrameTime); //movement in this frame
139 const auto movement{vm_vec_build_rotated(svel, new_pm)};
140
141 vm_vec_add2(obj->pos,movement);
142
143 moved |= (movement.x || movement.y || movement.z);
144
145 if (moved)
146 update_object_seg(vmobjptr, LevelSharedSegmentState, LevelUniqueSegmentState, obj); //update segment id
147
148 return moved;
149 }
150
151 }
152
153 //do slew for this frame
154 int slew_frame(int check_keys)
155 {
156 auto &Objects = LevelUniqueObjectState.Objects;
157 auto &vmobjptridx = Objects.vmptridx;
158 return do_slew_movement(vmobjptridx(slew_obj), !check_keys, Controls);
159 }
160
161 }