How to store distance while driving?
#1

I want to save the distance while you are moving, for example a player can have 100meters, or 50kilometers. And then something if player has 100kilometers then he can do a command like /countdown(just an example). I was thinking having a timer per second and then getting the player's speed(if he is in a vehicle), but what should i do after this?
Reply
#2

Make a global variable for players and then add the distances to it every second.
Reply
#3

Can you give me an example? I just need to know what to put after getting the player's speed.
pawn Код:
new Distance[MAX_PLAYERS];
DistanceTimer()//This timer will be called per second
{
    for(new i; i<MAX_PLAYERS; i++)
    {
        if(!IsPlayerInAnyVehicle(i)) continue;
        new speed = GetPlayerSpeed(i, true);//getting the speed in KM/H and storing at "speed"
        Distance += //What should be here?
    }
}
GetPlayerSpeed function
pawn Код:
stock GetPlayerSpeed(playerid,bool:kmh) // by misco
{
    new Float:Vx,Float:Vy,Float:Vz,Float:rtn;
    if(IsPlayerInAnyVehicle(playerid)) GetVehicleVelocity(GetPlayerVehicleID(playerid),Vx,Vy,Vz); else GetPlayerVelocity(playerid,Vx,Vy,Vz);
    rtn = floatsqroot(floatabs(floatpower(Vx + Vy + Vz,2)));
    return kmh?floatround(rtn * 100 * 1.61):floatround(rtn * 100);
}
EDIT: Added if(!IsPlayerInAnyVehicle(i)) continue; at the timer
Reply
#4

Something like this I guess

pawn Код:
#define TRAVEL_TYPES 2

#define TRAVEL_ONFOOT 0

#define TRAVEL_DRIVER 1

#define INVALID_POS -10000.00

forward UpdateTravel();

new Float:pTravelDist[MAX_PLAYERS][TRAVEL_TYPES];
new Float:pOldPos[MAX_PLAYERS][3];

stock PlayerDistanceToPoint(playerid,Float:x,Float:y,Float:z)
{
  new Float:px,Float:py,Float:pz;
  GetPlayerPos(playerid,py,px,pz);
  return floatsqroot( ( (x-px)*(x-px) ) + ( (y-py)*(y-py) ) + ( (z-pz)*(z-pz) ) );
}

public OnGameModeInit()
{
  SetTimer("UpdateTravel",1000,1);
  return 1;
}

public OnPlayerConnect(playerid)
{
  pOldPos[playerid]={INVALID_POS,...};
  return 1;
}

public OnPlayerSpawn(playerid)
{
  pOldPos[playerid]={INVALID_POS,...};
  return 1;
}

public UpdateTravel()
{
  new Float:x,Float:y,Float:z;
  for(new playerid;playerid<MAX_PLAYERS;playerid++) //Suggested to use foreach
  {
    if(!IsPlayerConnected(playerid))continue;
    GetPlayerPos(playerid,x,y,z);
    if(pOldDist[playerid][0]=INVALID_POS)
    {
      pOldDist[playerid][0]=x;
      pOldDist[playerid][1]=y;
      pOldDist[playerid][2]=z;
    }else{
      switch(GetPlayerState(playerid))
      {
        case PLAYER_STATE_ONFOOT: pTravelDist[playerid][TRAVEL_ONFOOT]+=PlayerDistanceToPoint(playerid,pOldDist[playerid][0],pOldDist[playerid][1],pOldDist[playerid][2]);
        case PLAYER_STATE_DRIVER: pTravelDist[playerid][TRAVEL_DRIVER]+=PlayerDistanceToPoint(playerid,pOldDist[playerid][0],pOldDist[playerid][1],pOldDist[playerid][2]);
      }
      pOldDist[playerid][0]=x;
      pOldDist[playerid][1]=y;
      pOldDist[playerid][2]=z;
    }
  }
}
With that script, the variables pTravelDist[playerid][TRAVEL_ONFOOT] and pTravelDist[playerid][TRAVEL_DRIVER] will keep tracking of your distance traveled on foot and whilst driver, respectively.

EDIT* Note that those values are in meters.
Reply
#5

Thanks! It looks good. I am going to try it.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)