How to store distance while driving? - Printable Version
+- SA-MP Forums Archive (
https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (
https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (
https://sampforum.blast.hk/forumdisplay.php?fid=12)
+---- Forum: Help Archive (
https://sampforum.blast.hk/forumdisplay.php?fid=89)
+---- Thread: How to store distance while driving? (
/showthread.php?tid=121403)
How to store distance while driving? -
Dark_Kostas - 16.01.2010
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?
Re: How to store distance while driving? -
MadeMan - 16.01.2010
Make a global variable for players and then add the distances to it every second.
Re: How to store distance while driving? -
Dark_Kostas - 16.01.2010
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
Re: How to store distance while driving? -
Joe Staff - 16.01.2010
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.
Re: How to store distance while driving? -
Dark_Kostas - 16.01.2010
Thanks! It looks good. I am going to try it.