+ float min_a = 0.70710678118654752f;
+ ray_hit *candidate = NULL;
+
+ if( sample_count >= 2 ){
+ for( int i=0; i<sample_count-1; i++ ){
+ ray_hit *s0 = &samples[i],
+ *s1 = &samples[i+1];
+
+ float a = v3_dot( s0->normal, s1->normal );
+
+ if( (a < min_a) && (a >= -0.1f) && (s0->normal[1]>s1->normal[1]) ){
+ min_a = a;
+ candidate = s0;
+ }
+ }
+ }
+
+ if( candidate ){
+ v4f pa, pb, pc;
+
+ ray_hit *s0 = candidate,
+ *s1 = candidate+1;
+
+ vg_line( s0->pos, s1->pos, VG__WHITE );
+
+ v3_copy( s0->normal, pa );
+ v3_copy( s1->normal, pb );
+ v3_cross( localplayer.rb.to_world[1], dir, pc );
+ v3_normalize( pc );
+
+ pa[3] = v3_dot( pa, s0->pos );
+ pb[3] = v3_dot( pb, s1->pos );
+ pc[3] = v3_dot( pc, localplayer.rb.co );
+
+ v3f edge;
+ if( plane_intersect3( pa, pb, pc, edge ) ){
+ v3_copy( edge, player_walk.state.drop_in_target );
+ v3_copy( s1->normal, player_walk.state.drop_in_normal );
+ v3_copy( localplayer.rb.co, player_walk.state.drop_in_start );
+
+ player_walk.state.drop_in_start_angle = player_get_heading_yaw();
+ player_walk.state.drop_in_angle =
+ atan2f( player_walk.state.drop_in_normal[0],
+ player_walk.state.drop_in_normal[2] );
+
+ /* TODO: scan multiple of these? */
+ v3f oco;
+ v4f oq;
+ player_walk_drop_in_overhang_transform( 1.0f, oco, oq );
+
+ v3f va = {0.0f,0.0f,-k_board_length - 0.3f},
+ vb = {0.0f,0.0f, k_board_length + 0.3f};
+
+ q_mulv( oq, va, va );
+ q_mulv( oq, vb, vb );
+ v3_add( oco, va, va );
+ v3_add( oco, vb, vb );
+
+ v3f v0;
+ v3_sub( vb, va, v0 );
+ v3_normalize( v0 );
+
+ ray_hit ray;
+ ray.dist = k_board_length*2.0f + 0.6f;
+
+ if( ray_world( world, va, v0, &ray, 0 ) ){
+ vg_line( va, vb, VG__RED );
+ vg_line_point( ray.pos, 0.1f, VG__RED );
+ vg_error( "invalidated\n" );
+ return 0;
+ }
+
+ v3_muls( v0, -1.0f, v0 );
+ if( ray_world( world, vb, v0, &ray, 0 ) ){
+ vg_line( va, vb, VG__RED );
+ vg_line_point( ray.pos, 0.1f, VG__RED );
+ vg_error( "invalidated\n" );
+ return 0;
+ }
+
+ player_walk_drop_in_vector( localplayer.rb.v );
+ return 1;
+ }
+ else{
+ vg_error( "failed to find intersection of drop in\n" );
+ }
+ }
+
+ return 0;
+}
+
+static struct skeleton_anim *player_walk_outro_anim( enum walk_outro type ){
+ struct player_walk *w = &player_walk;
+
+ if( type >= k_walk_outro_max )
+ return NULL;
+
+ return (struct skeleton_anim *[]){
+ [k_walk_outro_none] = NULL,
+ [k_walk_outro_jump_to_air] = w->anim_jump_to_air,
+ [k_walk_outro_regular] = w->anim_intro,
+ [k_walk_outro_drop_in] = w->anim_drop_in
+ }[ type ];
+}
+
+
+static void player__walk_pre_update(void){
+ struct player_walk *w = &player_walk;
+
+ if( !localplayer.immobile )
+ player_look( localplayer.angles, skaterift.time_rate );
+
+ if( w->state.outro_type ){
+ struct skeleton_anim *anim = player_walk_outro_anim(w->state.outro_type);
+
+ f32 outro_length = (f32)anim->length / anim->rate,
+ outro_time = vg.time - w->state.outro_start_time;
+
+ if( outro_time >= outro_length ){
+ if( w->state.outro_type == k_walk_outro_drop_in ){
+ player_walk_drop_in_to_skate();
+ }
+ else if( w->state.outro_type == k_walk_outro_jump_to_air ){
+ player_walk_generic_to_skate( k_skate_activity_air, 0.0f );
+ }
+ else{
+ player_walk_generic_to_skate( k_skate_activity_ground, 1.0f );
+ }
+
+ w->state.outro_type = k_walk_outro_none;
+ return;
+ }
+ }
+ else if( button_down( k_srbind_use ) && !localplayer.immobile ){
+ if( v3_dist2( localplayer.rb.co, gzoomer.obj.rb.co ) <= 4.0f*4.0f ){
+ localplayer.subsystem = k_player_subsystem_drive;
+ }
+ else{
+ struct player_board *board =
+ addon_cache_item_if_loaded( k_addon_type_board,
+ localplayer.board_view_slot );
+
+ if( !board ) return;
+
+ if( w->state.activity == k_walk_activity_ground ){
+ if( player_walk_scan_for_drop_in() ){
+ w->state.outro_type = k_walk_outro_drop_in;
+ w->state.outro_start_time = vg.time;
+ localplayer.immobile = 1;
+
+ struct player_avatar *av = localplayer.playeravatar;
+ m4x3_mulv( localplayer.final_mtx[ av->id_ik_foot_r ],
+ av->sk.bones[ av->id_ik_foot_r ].co,
+ w->state.drop_in_foot_anchor );
+ }
+ else{
+ w->state.outro_type = k_walk_outro_regular;
+ w->state.outro_start_time = vg.time;
+ w->state.activity = k_walk_activity_lockedmove;
+
+ if( player_xyspeed2() < 0.1f * 0.1f ){
+ q_mulv( localplayer.rb.q, (v3f){0.0f,0.0f,1.6f},
+ localplayer.rb.v );
+ }
+ }
+ }
+ else{
+ w->state.outro_type = k_walk_outro_jump_to_air;
+ w->state.outro_start_time = vg.time;
+ return;
+ }
+ }
+ }
+ else if( button_down( k_srbind_jump ) && !localplayer.immobile ){
+ w->state.jump_queued = 1;
+ w->state.jump_input_time = vg.time;