SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help

Reply
 
Thread Tools Display Modes
Old 16/02/2020, 08:08 PM   #11
Symon
Gangsta
 
Symon's Avatar
 
Join Date: Apr 2019
Location: Italy
Posts: 819
Reputation: 68
Default Re: Kilometers/meters conversion

I do prefer the first, seems more readable and fast to achieve.
__________________


"A server owner shouldn't be embarrassed or have fear to request help, because we're humans and we never stop learning."
Symon is offline   Reply With Quote
Old 17/02/2020, 08:09 AM   #12
jasperschellekens
Gangsta
 
jasperschellekens's Avatar
 
Join Date: Dec 2016
Location: Gehenna
Posts: 813
Reputation: 79
Default Re: Kilometers/meters conversion

Misunderstood you there. It works like Calisthenics did but if there are more than a 1000 KM's it will not.
You could try to do it like this tho:
PHP Code:
CalculateDistance(distance)
{
     new 
str[128], value;
     if (
distance >= 1000)
    {
        
value=distance/=1000
        
format(str,sizeof(str),"%d kilometers"value);
    } else {
       
value=distance*=1000
       
format(str,sizeof(str),"%d meters"value);
    }
    return 
str;
}
SCMEX(playerid, -1"Distance left: %s"CalculateDistance(randomDistance)); 

Pure out of curiousity; How do you actually calculate the distance in meters to a specific set of XYZ coords?
__________________
Coming soon (5-21-2020):

Undead Wasteland Roleplay - The year is 1996. San Andreas is a wasteland ruled by the undead. the once so glorius landscape of San Andreas, is now a shattered decayed place that is only a vague memory of the world as we used to know it.
Official Live Beta Release Date: 21th of May, 2020
Discord
jasperschellekens is online now   Reply With Quote
Old 17/02/2020, 08:24 AM   #13
Symon
Gangsta
 
Symon's Avatar
 
Join Date: Apr 2019
Location: Italy
Posts: 819
Reputation: 68
Default Re: Kilometers/meters conversion

Quote:
Originally Posted by jasperschellekens View Post
Misunderstood you there. It works like Calisthenics did but if there are more than a 1000 KM's it will not.
You could try to do it like this tho:
PHP Code:
CalculateDistance(distance)
{
     new 
str[128], value;
     if (
distance >= 1000)
    {
        
value=distance/=1000
        
format(str,sizeof(str),"%d kilometers"value);
    } else {
       
value=distance*=1000
       
format(str,sizeof(str),"%d meters"value);
    }
    return 
str;
}
SCMEX(playerid, -1"Distance left: %s"CalculateDistance(randomDistance)); 
Thanks, i'll try it.

Quote:
Originally Posted by jasperschellekens View Post
Pure out of curiousity; How do you actually calculate the distance in meters to a specific set of XYZ coords?
GetPlayerDistanceFromPoint.
__________________


"A server owner shouldn't be embarrassed or have fear to request help, because we're humans and we never stop learning."
Symon is offline   Reply With Quote
Old 17/02/2020, 09:24 AM   #14
Calisthenics
High-roller
 
Join Date: May 2018
Posts: 1,085
Reputation: 174
Default Re: Kilometers/meters conversion

@jasperschellekens: input is in meters so multiplying with 1000 will give different result.

Diving with 1000 only for distance such as 1736 meters, it will display 1 km. If you want more precision such as 1.7 km:
pawn Code:
CalculateDistance(distance)
{
    new str[13];
   
    if (distance >= 1000)
    {
        format(str, sizeof (str), "%d.%d km", distance / 1000, (distance % 1000) / 100);
    }
    else
    {
       format(str, sizeof (str), "%d meters", distance);
    }
    return str;
}
(distance % 1000) returns remaining -> 1736 % 1000 = 736 / 100 = 7
__________________
Calisthenics is offline   Reply With Quote
Old 17/02/2020, 09:44 AM   #15
Symon
Gangsta
 
Symon's Avatar
 
Join Date: Apr 2019
Location: Italy
Posts: 819
Reputation: 68
Default Re: Kilometers/meters conversion

Ok, tried to implement the Calisthenics's option and getting tag mismatch:

pawn Code:
PlayerTextDrawSetString(playerid, GpsTD[playerid], sprintf("        GPS~n~~b~%s~n~Distance: ~w~%s", GPSDestinationName[playerid], CalculateDistance(distance)));

This is how i calculate the distance:

pawn Code:
new Float: distance = GetPlayerDistanceFromPoint(playerid, destX, destY, destZ);
__________________


"A server owner shouldn't be embarrassed or have fear to request help, because we're humans and we never stop learning."
Symon is offline   Reply With Quote
Old 17/02/2020, 09:47 AM   #16
Calisthenics
High-roller
 
Join Date: May 2018
Posts: 1,085
Reputation: 174
Default Re: Kilometers/meters conversion

`GetPlayerDistanceFromPoint` returns float and the input of `CalculateDistance` is integer.
__________________
Calisthenics is offline   Reply With Quote
Old 17/02/2020, 09:50 AM   #17
Symon
Gangsta
 
Symon's Avatar
 
Join Date: Apr 2019
Location: Italy
Posts: 819
Reputation: 68
Default Re: Kilometers/meters conversion

Quote:
Originally Posted by Calisthenics View Post
`GetPlayerDistanceFromPoint` returns float and the input of `CalculateDistance` is integer.
So what i have to do? Use floatround? If, so how?
__________________


"A server owner shouldn't be embarrassed or have fear to request help, because we're humans and we never stop learning."
Symon is offline   Reply With Quote
Old 17/02/2020, 09:52 AM   #18
Calisthenics
High-roller
 
Join Date: May 2018
Posts: 1,085
Reputation: 174
Default Re: Kilometers/meters conversion

https://wiki.sa-mp.com/wiki/Function:floatround

First parameter is what `GetPlayerDistanceFromPoint` returns.
__________________
Calisthenics is offline   Reply With Quote
Old 17/02/2020, 10:18 AM   #19
Symon
Gangsta
 
Symon's Avatar
 
Join Date: Apr 2019
Location: Italy
Posts: 819
Reputation: 68
Default Re: Kilometers/meters conversion

So i'll not touch the normal float, but i'll do:

pawn Code:
new frdistance = floatround(distance);

Then CalculateDistance(frdistance), right?

EDIT: Works perfectly, thanks to everyone.
__________________


"A server owner shouldn't be embarrassed or have fear to request help, because we're humans and we never stop learning."
Symon is offline   Reply With Quote
Old 17/02/2020, 10:34 AM   #20
Calisthenics
High-roller
 
Join Date: May 2018
Posts: 1,085
Reputation: 174
Default Re: Kilometers/meters conversion

On second thought, converting to an integer is just extra work. Working with float is easier:
pawn Code:
CalculateDistance(Float: distance)
{
    new str[13];
   
    if (distance >= 1000.0)
    {
        format(str, sizeof (str), "%.1f km", distance / 1000.0);
    }
    else
    {
       format(str, sizeof (str), "%.0f meters", distance);
    }
    return str;
}
Now you can use it directly with:
pawn Code:
new Float: distance = GetPlayerDistanceFromPoint(playerid, destX, destY, destZ);

PlayerTextDrawSetString(playerid, GpsTD[playerid], sprintf("        GPS~n~~b~%s~n~Distance: ~w~%s", GPSDestinationName[playerid], CalculateDistance(distance)));
__________________
Calisthenics is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Kilometers... NeroX98 Scripting Help 8 07/12/2012 09:34 PM
[Include] Get/Set Distance in Kilometers/Meters. TheArcher Includes 7 16/09/2011 05:37 PM
kilometers passed? 77ther Help Archive 1 05/03/2011 01:53 PM
Kilometers whitedragon Help Archive 2 12/07/2010 11:33 AM


All times are GMT. The time now is 08:02 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.