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