Race position
#1

SOLVED

I have been searching for a while now about this "Race Position" thing but I didn't find anything. How do I determine the player's position in the race or list the Top 5 Leading Players in the Race?

For these who are looking for answer, Use this code (modify it a little bit to fit your needs);

PHP код:
stock GetPlayerRacePosition(playerid)
{
    new 
countcp gCheckpoint[playerid];
    foreach(new 
Character)
    {
        if(
gCheckpoint[i] > gCheckpoint[playerid])
        {
            
count ++;
        }
        else if (
gCheckpoint[i] == gCheckpoint[playerid])
        {
            if (
GetPlayerDistanceFromPoint(ig_arrCheckpoints[cp][0], g_arrCheckpoints[cp][1], g_arrCheckpoints[cp][2]) < GetPlayerDistanceFromPoint(playeridg_arrCheckpoints[cp][0], g_arrCheckpoints[cp][1], g_arrCheckpoints[cp][2]))
            {
                
count ++;
            }
        }
    }
    return 
count 1;

Reply
#2

1) Create a variable to count.
2) Loop through players.
3) Skip any player is not in race or if iterator variable is equal to playerid.
4) If current checkpoint of participant is greater to checkpoint of playerid's, increase by 1.
5) If current checkpoint of participant is equal to checkpoint of playerid's and the distance (GetPlayerDistanceFromPoint) from the checkpoint's position of the participant is lower than the playerid's, then increase by 1.
6) Out of the loop, return counter + 1.
Reply
#3

PHP код:
stock GetPlayerRacePosition(playerid)
{
    new 
countcp gCheckpoint[playerid] + 1;
    foreach(new 
Player)
    {
        if(
gCheckpoint[i] > gCheckpoint[playerid])
        {
            
count ++;
        }
        else if (
gCheckpoint[i] == gCheckpoint[playerid])
        {
            if (
GetPlayerDistanceFromPoint(ig_arrCheckpoint[0][cp], g_arrCheckpoint[1][cp], g_arrCheckpoint[2][cp]) < GetPlayerDistanceFromPoint(playeridg_arrCheckpoint[0][cp], g_arrCheckpoint[1][cp], g_arrCheckpoint[2][cp]))
            {
                
count ++;
            }
        }
    }
    return 
count 1;

Question; Why plus one on the returned value, and would this work on a track where in there is a left and right turns (It is more of like a Temple Run map)

- I have seen your code somewhere and based it on the code above.

How about; Sorting out the list of these players (Top 5 list)
Reply
#4

PHP код:
return count 1
<--- make no sense but if wanna do a code so complied better to use it than this ->
PHP код:
return count
as i know :/
Reply
#5

If you are, let's say in 1st position then count will be 0. You need to add 1 to the result.

The only way it fails is if there is a 180 degrees turn and more than a player is at that specific checkpoint - the player closer to the checkpoint will have a higher position. Unless you do something with nodes, I don't know if somebody has managed to do anything similar.

I don't understand what you want to do with sorting.
Reply
#6

Does the function works with NPC? I will try it out with the NPC.
Reply
#7

PHP код:
stock GetPlayerRacePosition(playerid)
{
    new 
countcp gCheckpoint[playerid] + 1;
    foreach(new 
Player)
    {
        if(
gCheckpoint[i] > gCheckpoint[playerid])
        {
            
count ++;
        }
        else if (
gCheckpoint[i] == gCheckpoint[playerid])
        {
            if (
GetPlayerDistanceFromPoint(ig_arrCheckpoints[0][cp], g_arrCheckpoints[1][cp], g_arrCheckpoints[2][cp]) < GetPlayerDistanceFromPoint(playeridg_arrCheckpoints[0][cp], g_arrCheckpoints[1][cp], g_arrCheckpoints[2][cp]))
            {
                
count ++;
            }
        }
    }
    return 
count 1;

Код:
[22:32:52] [debug] Run time error 4: "Array index out of bounds"
[22:32:52] [debug]  Attempted to read/write array element at index 3 in array of size 3
[22:32:52] [debug] AMX backtrace:
[22:32:52] [debug] #0 00025f78 in GetPlayerRacePosition (playerid=1) at C:\Documents and Settings\Ramos\My Documents\Pawn Scripting\gamemodes\3.pwn:1061
line 1061

PHP код:
if (GetPlayerDistanceFromPoint(ig_arrCheckpoints[0][cp], g_arrCheckpoints[1][cp], g_arrCheckpoints[2][cp]) < GetPlayerDistanceFromPoint(playeridg_arrCheckpoints[0][cp], g_arrCheckpoints[1][cp], g_arrCheckpoints[2][cp])) 
Reply
#8

PHP код:
stock GetPlayerRacePosition(playerid)
{
    new 
count,  cp gCheckpoint[playerid];
    foreach(new 
Player)
    {
        if(
gCheckpoint[i] > gCheckpoint[playerid])
        {
            
count ++;
        }
        else if (
gCheckpoint[i] == gCheckpoint[playerid])
        {
            if (
GetPlayerDistanceFromPoint(ig_arrCheckpoints[0][cp], g_arrCheckpoints[1][cp], g_arrCheckpoints[2][cp]) < GetPlayerDistanceFromPoint(playeridg_arrCheckpoints[0][cp], g_arrCheckpoints[1][cp], g_arrCheckpoints[2][cp]))
            {
                
count ++;
            }
        else if(
gCheckpoint[i] < gCheckpoint[playerid])
        {
            
count --;
        }
        }
    }
    return 
count 1;

Reply
#9

Even though I noticed it I forgot to comment about it. It is the current checkpoint, don't add 1 to cp.

Now that I think again about it when both players are at the same checkpoint, you should check the distance of the next checkpoint (if exists) - it is the only exception.
Reply
#10

Wont it look like this?
correct me if i am wrong!
Код:
new const Float:g_arrCheckpoints[][] = {
    {3199.9063, -1603.0369, 11.0098},
    {3199.8782, -1577.8555, 11.2266},
    {3199.8042, -1554.7965, 11.0993},
    {3199.8364, -1544.1656, 11.0098},
    {3200.2439, -1533.0986, 11.0098},
    {3222.5374, -1533.3177, 11.2266},
    {3241.9172, -1533.0231, 11.0098},
    {3242.0122, -1516.8359, 11.6601},
    {3241.9187, -1501.1729, 6.0084},
    {3242.0002, -1486.0942, 6.2266},
    {3241.7668, -1472.2089, 7.3302},
    {3241.9143, -1463.2189, 10.5974},
    {3241.7896, -1452.3059, 14.5464},
    {3241.8970, -1436.9100, 20.2788},
    {3241.7861, -1427.0548, 11.2403},
    {3241.5625, -1407.2910, 11.7712},
    {3217.9495, -1407.5604, 11.0993},
    {3217.0859, -1394.6508, 11.0084},
    {3217.6970, -1382.2604, 11.0084},
    {3241.9387, -1382.0331, 10.9599}
};
new CheckCPID[MAX_PLAYERS][sizeof(g_arrCheckpoints)];

stock GetPlayerRacePosition(playerid)
{
	new TempCp;
	for(new x = 0; x < sizeof(g_arrCheckpoints); x++)
	{
		if(GetPlayerDistanceFromPoint(playerid, g_arrCheckpoints[0][x], g_arrCheckpoints[1][x], g_arrCheckpoints[2][x]) < GetPlayerDistanceFromPoint(playerid, g_arrCheckpoints[0][x], g_arrCheckpoints[1][x], g_arrCheckpoints[2][x]))
		{
			TempCp = CheckCPID[playerid][x];
			break;
		}
    }
	return TempCp;
}
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)