Centimeters distance
#1

I am new to scripting, and I have a question for developing future systems.

How I can do to calculate the exact distance of centimeters between two players?

I need some way to calculate inches away, to use that OnPlayerTakeDamage.
Reply
#2

So how would the code? explanation is that the math level I understood, but I can not apply it in a script, as well, I'm new, and I can hardly theme applied coordinate axes at a distance.
Reply
#3

Help me, please.
Reply
#4

I made a little something for you, untested though.

pawn Код:
stock Float: GetDistanceBetweenPlayers(playerid, toplayerid, UNITS = UNIT_METERS)
{
    #define UNIT_METERS         0
    #define UNIT_KILOMETERS     1
    #define UNIT_MILES          2
    #define UNIT_CENTIMETERS    3
    #define UNIT_INCHES         4
   
    new Float: x,
        Float: y,
        Float: z,
        Float: toX,
        Float: toY,
        Float: toZ,
        Float: modifier
    ;
    GetPlayerPos(playerid, x, y, z);
    GetPlayerPos(toplayerid, toX, toY, toZ);
   
    x-=toX;
    y-=toY;
    z-=toZ;
   
    switch(UNITS) {
        case UNIT_METERS: modifier = 1.0;
        case UNIT_KILOMETERS: modifier = 1.0/1000.0;
        case UNIT_MILES: modifier = (1.0/1000.0)/1.6;
        case UNIT_CENTIMETERS: modifier = 100.0;
        case UNIT_INCHES: modifier = 39.3701;
    }
    return floatsqroot( (x*x) + (y*y) + (z*z) ) * modifier;
}
In UNITS, you put UNIT_KILOMETERS, UNIT_METERS, or miles or inches or whatever.
Reply
#5

I tried your function this way (to determine the distance in centimeters between two players) but I get errors:

Code:

pawn Код:
#include <a_samp>

public OnPlayerTakeDamage(playerid, issuerid, Float: amount, weaponid, bodypart)
{
    if(issuerid != INVALID_PLAYER_ID)
    {
        if(weaponid > 21 && weaponid < 52)
        {
            new string[128];
            new distance = GetDistanceBetweenPlayers(playerid, issuerid, UNIT_CENTIMETERS); // LINE 10
            format(string, sizeof(string), "Shooting distance: %f centimeters.", distance);
            SendClientMessageToAll(-1, string);
        }
    }
    return 1;
}

stock Float:GetDistanceBetweenPlayers(playerid, toplayerid, UNITS = UNIT_CENTIMETERS) // LINE 18
{
    #define UNIT_METERS         0
    #define UNIT_KILOMETERS     1
    #define UNIT_MILES          2
    #define UNIT_CENTIMETERS    3
    #define UNIT_INCHES         4

    new Float: x,
        Float: y,
        Float: z,
        Float: toX,
        Float: toY,
        Float: toZ,
        Float: modifier
    ;
    GetPlayerPos(playerid, x, y, z);
    GetPlayerPos(toplayerid, toX, toY, toZ);

    x-=toX;
    y-=toY;
    z-=toZ;

    switch(UNITS) {
        case UNIT_METERS: modifier = 1.0;
        case UNIT_KILOMETERS: modifier = 1.0/1000.0;
        case UNIT_MILES: modifier = (1.0/1000.0)/1.6;
        case UNIT_CENTIMETERS: modifier = 100.0;
        case UNIT_INCHES: modifier = 39.3701;
    }
    return floatsqroot( (x*x) + (y*y) + (z*z) ) * modifier;
}
Errors:
pawn Код:
C:\Users\Happiness\Desktop\Scripting\Testing\Distance.pwn(18) : warning 208: function with tag result used before definition, forcing reparse
C:\Users\Happiness\Desktop\Scripting\Testing\Distance.pwn(10) : error 017: undefined symbol "UNIT_CENTIMETERS"
C:\Users\Happiness\Desktop\Scripting\Testing\Distance.pwn(10) : warning 213: tag mismatch
C:\Users\Happiness\Desktop\Scripting\Testing\Distance.pwn(18) : error 017: undefined symbol "UNIT_CENTIMETERS"
Pawn compiler 3.2.3664          Copyright (c) 1997-2006, ITB CompuPhase


2 Errors.
Reply
#6

Define the function before using it.
pawn Код:
stock Float:GetDistanceBetweenPlayers(playerid, toplayerid, UNITS = 3)
{
    #define UNIT_METERS         0
    #define UNIT_KILOMETERS     1
    #define UNIT_MILES          2
    #define UNIT_CENTIMETERS    3
    #define UNIT_INCHES         4

    new Float: x,
        Float: y,
        Float: z,
        Float: toX,
        Float: toY,
        Float: toZ,
        Float: modifier
    ;
    GetPlayerPos(playerid, x, y, z);
    GetPlayerPos(toplayerid, toX, toY, toZ);

    x-=toX;
    y-=toY;
    z-=toZ;

    switch(UNITS) {
        case UNIT_METERS: modifier = 1.0;
        case UNIT_KILOMETERS: modifier = 1.0/1000.0;
        case UNIT_MILES: modifier = (1.0/1000.0)/1.6;
        case UNIT_CENTIMETERS: modifier = 100.0;
        case UNIT_INCHES: modifier = 39.3701;
    }
    return floatsqroot( (x*x) + (y*y) + (z*z) ) * modifier;
}

public OnPlayerTakeDamage(playerid, issuerid, Float: amount, weaponid, bodypart)
{
    if(issuerid != INVALID_PLAYER_ID)
    {
        if(weaponid > 21 && weaponid < 52)
        {
            new string[128];
            new distance = GetDistanceBetweenPlayers(playerid, issuerid, UNIT_CENTIMETERS); // LINE 10
            format(string, sizeof(string), "Shooting distance: %f centimeters.", distance);
            SendClientMessageToAll(-1, string);
        }
    }
    return 1;
}
Reply
#7

pawn Код:
Distance.pwn(3) : error 017: undefined symbol "UNIT_CENTIMETERS"
Distance.pwn(43) : warning 213: tag mismatch
Pawn compiler 3.2.3664          Copyright (c) 1997-2006, ITB CompuPhase


1 Error.
pawn Код:
#include <a_samp>

stock Float:GetDistanceBetweenPlayers(playerid, toplayerid, UNITS = UNIT_CENTIMETERS) // LINE 3
{
    #define UNIT_METERS         0
    #define UNIT_KILOMETERS     1
    #define UNIT_MILES          2
    #define UNIT_CENTIMETERS    3
    #define UNIT_INCHES         4

    new Float: x,
        Float: y,
        Float: z,
        Float: toX,
        Float: toY,
        Float: toZ,
        Float: modifier
    ;
    GetPlayerPos(playerid, x, y, z);
    GetPlayerPos(toplayerid, toX, toY, toZ);

    x-=toX;
    y-=toY;
    z-=toZ;

    switch(UNITS) {
        case UNIT_METERS: modifier = 1.0;
        case UNIT_KILOMETERS: modifier = 1.0/1000.0;
        case UNIT_MILES: modifier = (1.0/1000.0)/1.6;
        case UNIT_CENTIMETERS: modifier = 100.0;
        case UNIT_INCHES: modifier = 39.3701;
    }
    return floatsqroot( (x*x) + (y*y) + (z*z) ) * modifier;
}

public OnPlayerTakeDamage(playerid, issuerid, Float: amount, weaponid, bodypart)
{
    if(issuerid != INVALID_PLAYER_ID)
    {
        if(weaponid > 21 && weaponid < 52)
        {
            new string[128];
            new distance = GetDistanceBetweenPlayers(playerid, issuerid, UNIT_CENTIMETERS); // LINE 43
            format(string, sizeof(string), "Shooting distance: %f centimeters.", distance);
            SendClientMessageToAll(-1, string);
        }
    }
    return 1;
}
Reply
#8

Put the defines under the includes.
Reply
#9

distance should be a float.
pawn Код:
#include <a_samp>
#define UNIT_METERS         0
#define UNIT_KILOMETERS     1
#define UNIT_MILES          2
#define UNIT_CENTIMETERS    3
#define UNIT_INCHES         4

stock Float:GetDistanceBetweenPlayers(playerid, toplayerid, UNITS = UNIT_CENTIMETERS) // LINE 3
{


    new Float: x,
        Float: y,
        Float: z,
        Float: toX,
        Float: toY,
        Float: toZ,
        Float: modifier
    ;
    GetPlayerPos(playerid, x, y, z);
    GetPlayerPos(toplayerid, toX, toY, toZ);

    x-=toX;
    y-=toY;
    z-=toZ;

    switch(UNITS) {
        case UNIT_METERS: modifier = 1.0;
        case UNIT_KILOMETERS: modifier = 1.0/1000.0;
        case UNIT_MILES: modifier = (1.0/1000.0)/1.6;
        case UNIT_CENTIMETERS: modifier = 100.0;
        case UNIT_INCHES: modifier = 39.3701;
    }
    return floatsqroot( (x*x) + (y*y) + (z*z) ) * modifier;
}

public OnPlayerTakeDamage(playerid, issuerid, Float: amount, weaponid, bodypart)
{
    if(issuerid != INVALID_PLAYER_ID)
    {
        if(weaponid > 21 && weaponid < 52)
        {
            new string[128];
            new Float:distance = GetDistanceBetweenPlayers(playerid, issuerid, UNIT_CENTIMETERS); // LINE 43
            format(string, sizeof(string), "Shooting distance: %f centimeters.", distance);
            SendClientMessageToAll(-1, string);
        }
    }
    return 1;
}
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)