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