AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Player walk distance (https://forums.alliedmods.net/showthread.php?t=206899)

Backstabnoob 01-27-2013 07:08

Player walk distance
 
How would you make a function that gets called for example each time player walks 100 units? I've tried saving their old origin and comparing it to the new in a task, but that is not a great solution as when the player falls, uses teleport of any kind or something similar, it shows greatly incorrect values.

I'm creating an achievement that gets unlocked after player moves 50000 units, therefore this should be called each 100 units so I can save accordingly.

Thanks in advance.

Xellath 01-28-2013 08:50

Re: Player walk distance
 
Heres how I did it a while back, if it's of any help (you get the idea, it's pretty self-explanatory);

Code:
#define UnitsToMeters(%1) ( %1 * 0.0254 ) const FL_ONGROUND2 = ( FL_ONGROUND | FL_PARTIALGROUND | FL_INWATER | FL_CONVEYOR | FL_FLOAT ); new Float:Meters[ MaxClients + 1 ]; new Float:OldOrigin[ MaxClients + 1 ][ VectorSize ]; new bool:OnSurf[ MaxClients + 1 ]; // plugin_init register_forward( FM_PlayerPreThink, "Forward_FMPlayerPreThink" ); register_forward( FM_Touch, "Forward_FMTouch" ); public Forward_FMPlayerPreThink( Client ) {     if( !is_user_alive( Client ) || AchievementCompleted[ Client ][ _Runner ] )     {         return;     }         new Float:Origin[ VectorSize ], Float:Gravity;     new bool:OnGround = IsUserOnGround( Client );         if( OnSurf[ Client ] )     {         OnSurf[ Client ] = false;                 return;     }         pev( Client, pev_origin, Origin );     pev( Client, pev_gravity, Gravity );         if( ( pev( Client, pev_movetype ) != MOVETYPE_WALK )     || ( Gravity != 1.0 )     || ( pev( Client, pev_waterlevel ) > 0 )     || ( OldOrigin[ Client ][ 0 ] == Origin[ 0 ] && OldOrigin[ Client ][ 1 ] == Origin[ 1 ] && OldOrigin[ Client ][ 2 ] == Origin[ 2 ] ) )     {         return;     }         if( OnGround )     {         Meters[ Client ] += get_distance_f( Origin, OldOrigin[ Client ] );                 pev( Client, pev_origin, OldOrigin[ Client ] );                 //client_print( Client, print_chat, "debug: on ground %f %f", UnitsToMeters( floatround( Meters[ Client ] ) ), Meters[ Client ] );                 if( ( UnitsToMeters( floatround( Meters[ Client ] ) ) >= 50000 ) && !AchievementCompleted[ Client ][ _Runner ] )         {             ForwardAchievementStatus( Client, _Runner );         }     } } public Forward_FMTouch( Player, Entity ) {     if( ( 1 <= Player <= MaxPlayers ) && !IsUserOnGround( Entity ) )     {         OnSurf[ Player ] = true;     }         if( ( 1 <= Entity <= MaxPlayers ) && !IsUserOnGround( Entity ) )     {         OnSurf[ Entity ] = true;     } } bool:IsUserOnGround( Client ) {     return !!( pev( Client, pev_flags ) & FL_ONGROUND2 ); }

Backstabnoob 01-28-2013 11:55

Re: Player walk distance
 
Thanks, however that doesn't solve the problem with things such as teleports. Checking whether the difference in origins is higher than X should work though, as it's called too often.

AngeIII 01-28-2013 12:17

Re: Player walk distance
 
store as static value oldorigins
and before do increement check if distance(old origin,new origin)<=10
if yes you can increement
if no do nothing..

Backstabnoob 01-28-2013 12:17

Re: Player walk distance
 
That's exactly what I've just said.

Sylwester 01-28-2013 12:57

Re: Player walk distance
 
you can do it with task instead of using prethink like this:
PHP Code:

#define MAX_SPEED 320.0

#define UPDATE_INTERVAL 0.1
#define TID_TIMER 350983

new Float:g_p_distance[33]

public 
plugin_init(){
    
set_task(UPDATE_INTERVAL"timer_cycle"TID_TIMER__"b")    
}

public 
client_disconnect(id){
    
//save distance...
    
    
g_p_distance[id] = 0.0
}

public 
timer_cycle(){
    static 
players[32], pnumplayer_idFloat:p_last_origin[33][3], Float:tmp_origin[3], Float:tmp_distance
    get_players
(playerspnum)
    for(new 
i=0i<pnumi++){
        
player_id players[i]
        if(!
is_user_alive(player_id))
            continue
        
pev(player_idpev_origintmp_origin)
        
tmp_origin[player_id][2] = 0.0
        tmp_distance 
get_distance_f(tmp_originp_last_origin[player_id])
        if(
tmp_distance MAX_SPEED UPDATE_INTERVAL)
            
g_p_distance[player_id] += tmp_distance
        p_last_origin
[player_id][0] = tmp_origin[0]
        
p_last_origin[player_id][1] = tmp_origin[1]
    }    



AngeIII 01-28-2013 13:11

Re: Player walk distance
 
task for loop through 32 players... every 0.1 seconds?
.. witch uses 5-6natives?

better to use one more variable and native get_time(halftime)..
but why not to use only origins..

Backstabnoob 01-28-2013 13:30

Re: Player walk distance
 
Quote:

task for loop through 32 players... every 0.1 seconds?
That's still about 6x less than using PreThink.

@Sylwester: Thank you, this is exactly what I need.

Sylwester 01-28-2013 13:40

Re: Player walk distance
 
Quote:

Originally Posted by AngeIII (Post 1882367)
better to use one more variable and native get_time(halftime)..
but why not to use only origins..

What?

AngeIII 01-28-2013 15:34

Re: Player walk distance
 
if(timedifference(current,old)<0.1) skip;
else
{
....
calculate meters
old_time=current.
}


All times are GMT -4. The time now is 20:33.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.