input update 1
[carveJwlIkooP6JGAAIwe30JlM.git] / player.c
1 #ifndef PLAYER_C
2 #define PLAYER_C
3
4 #include "player.h"
5 #include "camera.h"
6 #include "player_model.h"
7 #include "input.h"
8
9 VG_STATIC int localplayer_cmd_respawn( int argc, const char *argv[] )
10 {
11 ent_spawn *rp = NULL, *r;
12 world_instance *world = localplayer.viewable_world;
13
14 if( argc == 1 ){
15 rp = world_find_spawn_by_name( world, argv[0] );
16 }
17 else if( argc == 0 ){
18 rp = world_find_closest_spawn( world, localplayer.rb.co );
19 }
20
21 if( !rp )
22 return 0;
23
24 player__spawn( &localplayer, rp );
25 return 1;
26 }
27
28 VG_STATIC void player_init(void)
29 {
30 for( u32 i=0; i<vg_list_size(_player_system_register); i++ ){
31 if( _player_system_register[i] )
32 _player_system_register[i]();
33 }
34
35 vg_console_reg_cmd( "respawn", localplayer_cmd_respawn, NULL );
36 }
37
38 PLAYER_API
39 void player__debugtext( int size, const char *fmt, ... )
40 {
41 char buffer[ 1024 ];
42
43 va_list args;
44 va_start( args, fmt );
45 vsnprintf( buffer, 1024, fmt, args );
46 va_end( args );
47
48 ui_text( vg_uictx.cursor, buffer, size, k_text_align_right );
49 vg_uictx.cursor[1] += 14*size;
50 }
51
52 /*
53 * Init
54 */
55 PLAYER_API
56 void player__create( player_instance *inst )
57 {
58 static int only_once = 0;
59 assert( only_once == 0 );
60 only_once ++;
61
62 v3_zero( inst->rb.co );
63 v3_zero( inst->rb.w );
64 v3_zero( inst->rb.v );
65 q_identity( inst->rb.q );
66 m4x3_identity( inst->rb.to_world );
67 m4x3_identity( inst->rb.to_local );
68
69 inst->rewind_length = 0;
70 inst->rewind_buffer =
71 vg_linear_alloc( vg_mem.rtmemory,
72 sizeof(struct rewind_frame) * PLAYER_REWIND_FRAMES );
73
74 }
75
76 /*
77 * Appearence
78 */
79 PLAYER_API
80 void player__use_avatar( player_instance *player, struct player_avatar *av )
81 {
82 player->playeravatar = av;
83 player_setup_ragdoll_from_avatar( &player->ragdoll, av );
84 }
85
86 PLAYER_API
87 void player__use_model( player_instance *player, struct player_model *mdl )
88 {
89 player->playermodel = mdl;
90 }
91
92 PLAYER_API
93 void player__bind( player_instance *player )
94 {
95 for( u32 i=0; i<vg_list_size(_player_bind); i++ ){
96 if( _player_bind[i] )
97 _player_bind[i]( player );
98 }
99 }
100
101 /*
102 * Gameloop events
103 * ----------------------------------------------------------------------------
104 */
105
106 VG_STATIC void player_save_rewind_frame( player_instance *player )
107 {
108 if( player->rewind_length < PLAYER_REWIND_FRAMES ){
109 struct rewind_frame *fr =
110 &player->rewind_buffer[ player->rewind_length ++ ];
111
112 v2_copy( player->cam.angles, fr->ang );
113 v3_copy( player->cam.pos, fr->pos );
114
115 if( player->rewind_length >= 2 ){
116 player->rewind_total_length +=
117 v3_dist( player->rewind_buffer[player->rewind_length-1].pos,
118 player->rewind_buffer[player->rewind_length-2].pos );
119 }
120 }
121 }
122
123 PLAYER_API
124 void player__pre_update( player_instance *player )
125 {
126 if( player->rewinding ){
127 return;
128 }
129
130 if( button_down( k_srbind_reset ) && !player->immobile ){
131 double delta = world_global.time - world_global.last_use;
132
133 if( (delta <= RESET_MAX_TIME) && (world_global.last_use != 0.0) ){
134 player->rewinding = 1;
135 player->rewind_sound_wait = 1;
136 player->rewind_time = (double)player->rewind_length - 0.0001;
137 player_save_rewind_frame( player );
138
139 audio_lock();
140 audio_oneshot( &audio_rewind[0], 1.0f, 0.0f );
141 audio_unlock();
142
143 /* based on testing. DONT CHANGE!
144 *
145 * time taken: y = (x^(4/5)) * 74.5
146 * inverse : x = (2/149)^(4/5) * y^(4/5)
147 */
148
149 float constant = powf( 2.0f/149.0f, 4.0f/5.0f ),
150 curve = powf( player->rewind_total_length, 4.0f/5.0f );
151
152 player->rewind_predicted_time = constant * curve;
153 player->rewind_start = vg.time;
154 player->subsystem = player->subsystem_gate;
155 player->rb = player->rb_gate_storage;
156 v3_copy( player->angles_storage, player->angles );
157
158 if( _player_restore[ player->subsystem ] )
159 _player_restore[ player->subsystem ]( player );
160 }
161 else{
162 if( player->subsystem == k_player_subsystem_dead ){
163 localplayer_cmd_respawn( 0, NULL );
164 }
165 else{
166 /* cant do that */
167 audio_lock();
168 audio_oneshot( &audio_rewind[4], 1.0f, 0.0f );
169 audio_unlock();
170 }
171 }
172 }
173
174 if( button_down( k_srbind_camera ) && !player->immobile ){
175 if( player->camera_mode == k_cam_firstperson )
176 player->camera_mode = k_cam_thirdperson;
177 else
178 player->camera_mode = k_cam_firstperson;
179 }
180
181 if( _player_pre_update[ player->subsystem ] )
182 _player_pre_update[ player->subsystem ]( player );
183 }
184
185 PLAYER_API
186 void player__update( player_instance *player )
187 {
188 if( player->rewinding )
189 return;
190
191 if( _player_update[ player->subsystem ] )
192 _player_update[ player->subsystem ]( player );
193 }
194
195 PLAYER_API
196 void player__post_update( player_instance *player )
197 {
198 if( player->rewinding )
199 return;
200
201 if( _player_post_update[ player->subsystem ] )
202 _player_post_update[ player->subsystem ]( player );
203
204 if((player->subsystem != k_player_subsystem_dead) && !player->gate_waiting){
205 player->rewind_accum += vg.time_frame_delta;
206
207 if( player->rewind_accum > 0.25f ){
208 player->rewind_accum -= 0.25f;
209 player_save_rewind_frame( player );
210 }
211 }
212 }
213
214 /*
215 * Applies gate transport to a player_interface
216 */
217 PLAYER_API
218 void player__pass_gate( player_instance *player, ent_gate *gate )
219 {
220 world_routes_fracture( get_active_world(), gate,
221 player->rb.co, player->rb.v );
222
223 player->gate_waiting = gate;
224 world_routes_activate_entry_gate( get_active_world(), gate );
225
226 m4x3_mulv( gate->transport, player->tpv_lpf, player->tpv_lpf );
227 m3x3_mulv( gate->transport, player->cam_velocity_smooth,
228 player->cam_velocity_smooth );
229
230 m3x3_copy( player->basis, player->basis_gate );
231
232 v4f q;
233 m3x3_q( gate->transport, q );
234 q_mul( q, player->qbasis, player->qbasis );
235 q_normalize( player->qbasis );
236 q_m3x3( player->qbasis, player->basis );
237 m3x3_transpose( player->basis, player->invbasis );
238
239 player->subsystem_gate = player->subsystem;
240 player->rb_gate_storage = player->rb;
241 v3_copy( player->angles, player->angles_storage );
242 player->rewind_length = 0;
243 player->rewind_total_length = 0.0f;
244 player->rewind_gate = gate;
245 player->rewind_accum = 0.0f;
246
247 m4x3_mulv( gate->transport, player->cam.pos, player->cam.pos );
248 player_save_rewind_frame( player );
249
250 if( gate->type == k_gate_type_nonlocel )
251 world_global.active_world = gate->target;
252
253 world_global.in_volume = 0;
254
255 audio_lock();
256 audio_oneshot( &audio_gate_pass, 1.0f, 0.0f );
257 audio_unlock();
258 }
259
260 VG_STATIC void player_apply_transport_to_cam( m4x3f transport )
261 {
262 /* FIXME: Applies to main_camera directly! */
263
264 /* Pre-emptively edit the camera matrices so that the motion vectors
265 * are correct */
266 m4x3f transport_i;
267 m4x4f transport_4;
268 m4x3_invert_affine( transport, transport_i );
269 m4x3_expand( transport_i, transport_4 );
270 m4x4_mul( main_camera.mtx.pv, transport_4, main_camera.mtx.pv );
271 m4x4_mul( main_camera.mtx.v, transport_4, main_camera.mtx.v );
272
273 /* we want the regular transform here no the inversion */
274 m4x3_expand( transport, transport_4 );
275 m4x4_mul( gate_camera.mtx.pv, transport_4, gate_camera.mtx.pv );
276 m4x4_mul( gate_camera.mtx.v, transport_4, gate_camera.mtx.v );
277 }
278
279 __attribute__ ((deprecated))
280 VG_STATIC void gate_rotate_angles( ent_gate *gate, v3f angles, v3f d )
281 {
282 v3_copy( angles, d );
283 return;
284
285 v3f fwd_dir = { cosf(angles[0]),
286 0.0f,
287 sinf(angles[0])};
288 m3x3_mulv( gate->transport, fwd_dir, fwd_dir );
289
290 v3_copy( angles, d );
291 d[0] = atan2f( fwd_dir[2], fwd_dir[0] );
292 }
293
294 PLAYER_API void player__im_gui( player_instance *player )
295 {
296 vg_uictx.cursor[0] = vg.window_x - 200;
297 vg_uictx.cursor[1] = 0;
298 vg_uictx.cursor[2] = 200;
299 vg_uictx.cursor[3] = 200;
300
301 struct ui_vert *b = ui_fill_rect( vg_uictx.cursor, 0x70000000 );
302
303 vg_uictx.cursor[0] = vg.window_x;
304
305 player__debugtext( 1, "angles: " PRINTF_v3f( player->cam.angles ) );
306 player__debugtext( 1, "basis: " PRINTF_v4f( player->qbasis ) );
307
308 if( _player_im_gui[ player->subsystem ] )
309 _player_im_gui[ player->subsystem ]( player );
310
311 b[2].co[1] = vg_uictx.cursor[1];
312 b[3].co[1] = vg_uictx.cursor[1];
313 }
314
315 VG_STATIC void global_skateshop_exit(void);
316 PLAYER_API void player__spawn( player_instance *player,
317 ent_spawn *rp )
318 {
319 v3_copy( rp->transform.co, player->rb.co );
320 v3_zero( player->rb.v );
321 v3_zero( player->rb.w );
322 q_identity( player->rb.q );
323 rb_update_transform( &player->rb );
324
325 q_identity( player->qbasis );
326 m3x3_identity( player->basis );
327 m3x3_identity( player->invbasis );
328
329 player->subsystem = k_player_subsystem_walk;
330 player->immobile = 0;
331 player->gate_waiting = NULL;
332
333 global_skateshop_exit();
334
335 if( _player_reset[ player->subsystem ] )
336 _player_reset[ player->subsystem ]( player, rp );
337 }
338
339
340 PLAYER_API void player__kill( player_instance *player )
341 {
342
343 }
344
345 #endif /* PLAYER_C */