23.11.2010, 16:52
I'm trying to make a simple command that measures the distance between a player's current position, and a previously defined point. The command /defpoint stores their starting position, and starts a looping timer to work out how far from the point they've moved. I'll try and include only the necessary parts;
/defpoint command
Timer
Stock function to calculate distance
The issue is that it doesn't seem to be calculating accurate results. The textdraw only reads "Distance: 1". I added a little debug that would send a client message saying what the 'distance' value was, and it was coming up with 10-figure long numbers. (e.g - 1132197622)
I'm hoping it's just some stupid mistake that some smarty-pants will pick up. Any replies are appreciated.
(Don't bother criticising the fact that it's probably a terrible script - I'm only going to run it alone here and there to work out distances, the looping timer doesn't worry me.)
/defpoint command
pawn Код:
CMD:defpoint(playerid, params[])
{
DistanceTravelled[playerid] = TextDrawCreate(320.000000, 361.650000,"~g~~h~Distance: ~w~~h~ ");
TextDrawSetShadow(DistanceTravelled[playerid],0);
TextDrawSetOutline(DistanceTravelled[playerid],1);
TextDrawLetterSize(DistanceTravelled[playerid],0.3,1.05);
TextDrawFont(DistanceTravelled[playerid],1);
TextDrawAlignment(DistanceTravelled[playerid],2);
TextDrawShowForPlayer(playerid, DistanceTravelled[playerid]);
GetPlayerPos(playerid, DEFx, DEFy, DEFz);
UpdateTimer[playerid] = SetTimerEx("distance_timer", 750, true, "i", playerid);
SendClientMessage(playerid, COLOR_LIGHTBLUE, "Point defined! Type /close to close the calculator.");
return 1;
}
pawn Код:
public distance_timer(playerid)
{
GetPlayerPos(playerid, NEWx, NEWy, NEWz);
new Float:distance = GetPointDistanceToPoint(DEFx,DEFy,NEWx,NEWy);
new Float:distancernd = floatround(distance,floatround_round);
format(DistanceUpdateString[playerid],24,"~g~~h~Distance: ~w~~h~%d",distancernd);
TextDrawSetString(DistanceTravelled[playerid], DistanceUpdateString[playerid]);
TextDrawShowForPlayer(playerid, DistanceTravelled[playerid]);
return 1;
}
pawn Код:
Float:GetPointDistanceToPoint(Float:x1,Float:y1,Float:x2,Float:y2)
{
new Float:x, Float:y;
x = x1-x2;
y = y1-y2;
return floatsqroot(x*x+y*y);
}
I'm hoping it's just some stupid mistake that some smarty-pants will pick up. Any replies are appreciated.
(Don't bother criticising the fact that it's probably a terrible script - I'm only going to run it alone here and there to work out distances, the looping timer doesn't worry me.)