here ya go
just save as JSpeedo.inc
and add to your mode and your done.
it includes the ALS Hooks so you dont have to add any code to your mode.
I have also made the mph/kmph an option
just read the top comments in the code.
also adjust the time update time for you own personal needs.
regards,
pawn Код:
/*
JSpeedo.inc
By: Jonny5 2012
Version: 0.1
Use at your own risk!
*/
//#define JSPEEDO_KMPH //uncomment to use KMPH , default is MPH
#define JSPEEDO_TIMER_UPDATE 800 //time in MS that the timer updates.
new PlayerText:ptdSpeedo[MAX_PLAYERS];
new bool:pSpeedo_Shown[MAX_PLAYERS];
stock GetVehicleSpeed( vehicleid, bool:kmph=false )
{
new Float:x, Float:y, Float:z;GetVehicleVelocity( vehicleid, x, y, z );
if(kmph) return floatround( floatsqroot( x*x + y*y + z*z ) * 180 ); // RETURN kmph
return floatround( floatsqroot( x*x + y*y + z*z ) * 180 / 1.609344 ); //RETURN mph
}
forward SpeedoUpdate();
public SpeedoUpdate()
{
new vehicleid;
new str[12];
new playerid;
for(playerid = 0;playerid<MAX_PLAYERS;playerid++) //if you use for each then replace this line.
{
vehicleid = GetPlayerVehicleID(playerid);
if((vehicleid > 0) && (GetPlayerState(playerid) == PLAYER_STATE_DRIVER))
{
#if !defined JSPEEDO_KMPH
format(str, 12, "%i", GetVehicleSpeed(vehicleid));
#else
format(str, 12, "%i", GetVehicleSpeed(vehicleid,true));
#endif
PlayerTextDrawSetString(playerid,ptdSpeedo[playerid],str);
if (!pSpeedo_Shown[playerid])
{
PlayerTextDrawShow(playerid,ptdSpeedo[playerid]);
pSpeedo_Shown[playerid]=true;
}
}
else
{
if (pSpeedo_Shown[playerid])
{
PlayerTextDrawSetString(playerid,ptdSpeedo[playerid]," ");
PlayerTextDrawHide(playerid,ptdSpeedo[playerid]);
pSpeedo_Shown[playerid] = false;
}
}
}
return 1;
}
//For more info on ALS Hooking check
//https://sampforum.blast.hk/showthread.php?tid=85907
static gSpeedo_HasCB[2];
//--------------OnGameModeInit Hook-------------------
public OnGameModeInit()
{
SetTimer("SpeedoUpdate",true,JSPEEDO_TIMER_UPDATE);
gSpeedo_HasCB[0] = funcidx("Speedo_OnPlayerConnect") != -1;
gSpeedo_HasCB[1] = funcidx("Speedo_OnPlayerDisconnect") != -1;
if (funcidx("Speedo_OnGameModeInit") != -1) CallLocalFunction("Speedo_OnGameModeInit", "");
return 1;
}
#if defined _ALS_OnGameModeInit
#undef OnGameModeInit
#else
#define _ALS_OnGameModeInit
#endif
#define OnGameModeInit Speedo_OnGameModeInit
forward Speedo_OnGameModeInit();
//--------------OnPlayerConnect Hook-------------------
public OnPlayerConnect(playerid)
{
ptdSpeedo[playerid] =
CreatePlayerTextDraw (playerid, 630.000000, 370.000000, " ");
PlayerTextDrawAlignment (playerid, ptdSpeedo[playerid], 3);
PlayerTextDrawBackgroundColor (playerid, ptdSpeedo[playerid], 255);
PlayerTextDrawFont (playerid, ptdSpeedo[playerid], 3);
PlayerTextDrawLetterSize (playerid, ptdSpeedo[playerid], 0.800000, 4.000000);
PlayerTextDrawColor (playerid, ptdSpeedo[playerid], 16711935);
PlayerTextDrawSetOutline (playerid, ptdSpeedo[playerid], 1);
PlayerTextDrawSetProportional (playerid, ptdSpeedo[playerid], 1);
if (gSpeedo_HasCB[0]) CallLocalFunction("Speedo_OnPlayerConnect", "i", playerid);
return 1;
}
#if defined _ALS_OnPlayerConnect
#undef OnPlayerConnect
#else
#define _ALS_OnPlayerConnect
#endif
#define OnPlayerConnect Speedo_OnPlayerConnect
forward Speedo_OnPlayerConnect(playerid);
//--------------OnPlayerDisconnect Hook-------------------
public OnPlayerDisconnect(playerid, reason)
{
PlayerTextDrawDestroy(playerid, ptdSpeedo[playerid]);
if (gSpeedo_HasCB[1]) CallLocalFunction("Speedo_OnPlayerDisconnect", "ii", playerid, reason);
return 1;
}
#if defined _ALS_OnPlayerDisconnect
#undef OnPlayerDisconnect
#else
#define _ALS_OnPlayerDisconnect
#endif
#define OnPlayerDisconnect Speedo_OnPlayerDisconnect
forward Speedo_OnPlayerDisconnect(playerid, reason);