Check player speeding on-foot and on-car
#1

Hey, I'm trying to create a script that when a player passes a police with his car, and going over 130. It gives an +1 star and the police and SRT are getting informed about it.

The script so far:
pawn Код:
forward LaserPolice();
public LaserPolice();
{
    // local variables
    new PolicePos[3];
    for(new i; i < MAX_PLAYERS; i++)
    for(new playerid; playerid < MAX_PLAYERS; playerid++)
    {
        GetPlayerPos(playerid, PolicePos[1], PolicePos[2], PolicePos[3]);
        if(APlayerData[playerid][LoggedIn] == false) continue;
        {
            if((APlayerData[playerid][PlayerClass] == ClassPolice) || (APlayerData[playerid][PlayerClass] == ClassSRT))
            {
            if(IsPlayerInRangeOfPoint(i, 50.0, PolicePos[1], PolicePos[2], PolicePos[3]);
            {
                if ((APlayerData[i][PlayerClass] == ClassBusDriver) || (APlayerData[i][PlayerClass] == ClassTruckDriver) || (APlayerData[i][PlayerClass] == ClassPilot) || (APlayerData[i][PlayerClass] == ClassMafia) || (APlayerData[i][PlayerClass] == ClassCourier) || (APlayerData[i][PlayerClass] == ClassAssistance) ||(APlayerData[i][PlayerClass] == ClassRoadWorker))
                {
                    CheckPoliceSpeeding(i);
                }
                else return 0;
            }
            else return 0;
            }
            else return 0;
        }
    }

    return 1;
}

CheckPoliceSpeeding(playerid)
{
    // Setup local variables
    new Name[24], Msg[128];

    // Check if the player hasn't been caught speeding recently
    if (APlayerData[playerid][PlayerCaughtSpeeding] == 0)
    {
                // Check if the player is the driver of the vehicle
                if (GetPlayerVehicleSeat(playerid) == 0)
                {
                    // Check if the player's speed is greater than the speed allowed by this camera (no need to process a distance-check if not speeding)
                    if (APlayerData[playerid][PlayerSpeed] > 130)
                    {
                            // Prevent the player being caught multiple times by the same speed-camera
                            APlayerData[playerid][PlayerCaughtSpeeding] = 20;
                            // Increase the wanted-level of this player by 1 star
                            SetPlayerWantedLevel(playerid, GetPlayerWantedLevel(playerid) + 1);
                            // Let the player know he's been caught speeding
                            SendClientMessage(playerid, 0xFFFFFFFF, TXT_PlayerCaughtSpeeding);

                            // Get the name of the player
                            GetPlayerName(playerid, Name, sizeof(Name));
                            // Also inform all police players that this player is caught speeding
                            format(Msg, 128, "{00FF00}Player {FFFF00}%s{00FF00} is caught speeding, pursue and fine him", Name);
                            Police_SendMessage(Msg);
                            SRT_SendMessage(Msg);

                    }
                }

    }
    else // If the player has been caught before, reduce the value until it's 0 again, then he can be caught again
        APlayerData[playerid][PlayerCaughtSpeeding]--;
}
The errors:
Код:
C:\Users\Barbara\Desktop\SAMP Kevin\hoi\gamemodes\PPC_Trucking.pwn(214) : error 055: start of function body without function header
C:\Users\Barbara\Desktop\SAMP Kevin\hoi\gamemodes\PPC_Trucking.pwn(217) : error 010: invalid function or declaration
C:\Users\Barbara\Desktop\SAMP Kevin\hoi\gamemodes\PPC_Trucking.pwn(218) : error 010: invalid function or declaration
C:\Users\Barbara\Desktop\SAMP Kevin\hoi\gamemodes\PPC_Trucking.pwn(221) : error 010: invalid function or declaration
C:\Users\Barbara\Desktop\SAMP Kevin\hoi\gamemodes\PPC_Trucking.pwn(223) : error 010: invalid function or declaration
C:\Users\Barbara\Desktop\SAMP Kevin\hoi\gamemodes\PPC_Trucking.pwn(225) : error 010: invalid function or declaration
C:\Users\Barbara\Desktop\SAMP Kevin\hoi\gamemodes\PPC_Trucking.pwn(227) : error 010: invalid function or declaration
C:\Users\Barbara\Desktop\SAMP Kevin\hoi\gamemodes\PPC_Trucking.pwn(230) : error 054: unmatched closing brace ("}")
C:\Users\Barbara\Desktop\SAMP Kevin\hoi\gamemodes\PPC_Trucking.pwn(231) : error 010: invalid function or declaration
C:\Users\Barbara\Desktop\SAMP Kevin\hoi\gamemodes\PPC_Trucking.pwn(233) : error 010: invalid function or declaration
C:\Users\Barbara\Desktop\SAMP Kevin\hoi\gamemodes\PPC_Trucking.pwn(235) : error 010: invalid function or declaration
C:\Users\Barbara\Desktop\SAMP Kevin\hoi\gamemodes\PPC_Trucking.pwn(239) : error 010: invalid function or declaration
C:\Users\Barbara\Desktop\SAMP Kevin\hoi\gamemodes\PPC_Trucking.pwn(242) : error 025: function heading differs from prototype
As my opinion I scripted as it should be, but apparently not.

Someone can help me out?
Thanks in advance!

-Kevin
Reply
#2

Can you explain what's this:
pawn Код:
for(new i; i < MAX_PLAYERS; i++)
for(new playerid; playerid < MAX_PLAYERS; playerid++)
You only need one loop there. And replace i with playerid in the rest of the loop.

Additionally I can spot a lot of parts which could be shortened and by that, improved a little efficiency-wise.
pawn Код:
if (GetPlayerVehicleSeat(playerid) == 0)
{
    // Check if the player's speed is greater than the speed allowed by this camera (no need to process a distance-check if not speeding)
    if (APlayerData[playerid][PlayerSpeed] > 130)
    {
    }
}
->
pawn Код:
if(GetPlayerVehicleSeat(playerid) == 0 && APlayerData[playerid][PlayerSpeed] > 130)
{
}
Also look at this:
pawn Код:
GetPlayerPos(playerid, PolicePos[1], PolicePos[2], PolicePos[3]);
if(APlayerData[playerid][LoggedIn] == false) continue;
What you do here is: 1. Get the player's position, 2. See if we even need to get the player's position.
This could be improved to:
pawn Код:
if(APlayerData[playerid][LoggedIn] == false) continue;
GetPlayerPos(playerid, PolicePos[1], PolicePos[2], PolicePos[3]);
Reply
#3

playerid would be only for the police, and the i is for if someone is passing by the police.

I thought it was wrong but how do I make it for 2 classes that they won't get caught?
Reply
#4

Oh, sorry for the confusement. But your code itself is quite confusing as well.

What it should be like is:
1. Start the first loop
2. Check if the looped player is firstly in the group that can be punished for speeding and check if they are speeding (going over 130).
3. If the player is speeding, get the speed of the speeding player and loop through all police officers
4. If the police officer is within the range of the speeder, report the player as the speeder

Your code at the current moment confuses me.
Reply
#5

Код:
C:\Users\Barbara\Desktop\SAMP Kevin\hoi\gamemodes\PPC_Trucking.pwn(214) : error 055: start of function body without function header
C:\Users\Barbara\Desktop\SAMP Kevin\hoi\gamemodes\PPC_Trucking.pwn(217) : error 010: invalid function or declaration
C:\Users\Barbara\Desktop\SAMP Kevin\hoi\gamemodes\PPC_Trucking.pwn(219) : error 010: invalid function or declaration
C:\Users\Barbara\Desktop\SAMP Kevin\hoi\gamemodes\PPC_Trucking.pwn(221) : error 010: invalid function or declaration
C:\Users\Barbara\Desktop\SAMP Kevin\hoi\gamemodes\PPC_Trucking.pwn(223) : error 010: invalid function or declaration
C:\Users\Barbara\Desktop\SAMP Kevin\hoi\gamemodes\PPC_Trucking.pwn(225) : error 010: invalid function or declaration
C:\Users\Barbara\Desktop\SAMP Kevin\hoi\gamemodes\PPC_Trucking.pwn(227) : error 010: invalid function or declaration
C:\Users\Barbara\Desktop\SAMP Kevin\hoi\gamemodes\PPC_Trucking.pwn(231) : error 010: invalid function or declaration
C:\Users\Barbara\Desktop\SAMP Kevin\hoi\gamemodes\PPC_Trucking.pwn(234) : error 010: invalid function or declaration
C:\Users\Barbara\Desktop\SAMP Kevin\hoi\gamemodes\PPC_Trucking.pwn(236) : error 021: symbol already defined: "SetPlayerWantedLevel"
C:\Users\Barbara\Desktop\SAMP Kevin\hoi\gamemodes\PPC_Trucking.pwn(238) : error 021: symbol already defined: "SendClientMessage"
C:\Users\Barbara\Desktop\SAMP Kevin\hoi\gamemodes\PPC_Trucking.pwn(241) : error 021: symbol already defined: "GetPlayerName"
C:\Users\Barbara\Desktop\SAMP Kevin\hoi\gamemodes\PPC_Trucking.pwn(243) : error 021: symbol already defined: "format"
C:\Users\Barbara\Desktop\SAMP Kevin\hoi\gamemodes\PPC_Trucking.pwn(244) : error 021: symbol already defined: "Police_SendMessage"
C:\Users\Barbara\Desktop\SAMP Kevin\hoi\gamemodes\PPC_Trucking.pwn(245) : error 021: symbol already defined: "SRT_SendMessage"
C:\Users\Barbara\Desktop\SAMP Kevin\hoi\gamemodes\PPC_Trucking.pwn(247) : error 010: invalid function or declaration
C:\Users\Barbara\Desktop\SAMP Kevin\hoi\gamemodes\PPC_Trucking.pwn(250) : error 010: invalid function or declaration
C:\Users\Barbara\Desktop\SAMP Kevin\hoi\gamemodes\PPC_Trucking.pwn(253) : error 010: invalid function or declaration
C:\Users\Barbara\Desktop\SAMP Kevin\hoi\gamemodes\PPC_Trucking.pwn(257) : error 010: invalid function or declaration
Current script:
pawn Код:
forward LaserPolice();
public LaserPolice();
{
    // local variables
    new PolicePos[3];
    for(new playerid; playerid < MAX_PLAYERS; playerid++)
    {
        if (APlayerData[playerid][PlayerCaughtSpeeding] == 0)
        {
                if(GetPlayerVehicleSeat(playerid) == 0 && APlayerData[playerid][PlayerSpeed] > 130)
                {
                    if ((APlayerData[playerid][PlayerClass] == ClassBusDriver) || (APlayerData[playerid][PlayerClass] == ClassTruckDriver) || (APlayerData[playerid][PlayerClass] == ClassPilot) || (APlayerData[playerid][PlayerClass] == ClassMafia) || (APlayerData[playerid][PlayerClass] == ClassCourier) || (APlayerData[playerid][PlayerClass] == ClassAssistance) ||(APlayerData[playerid][PlayerClass] == ClassRoadWorker))
                    {
                            for(new i; i < MAX_PLAYERS; i++)
                            {
                                if ((APlayerData[i][PlayerClass] == ClassPolice) || (APlayerData[playerid][PlayerClass] == ClassSRT))
                                {
                                    GetPlayerPos(i, PolicePos[1], PolicePos[2], PolicePos[3]);
                                }
                                if(IsPlayerInRangeOfPoint(playerid, 50.0, PolicePos[1], PolicePos[2], PolicePos[3]);
                                {
                                    // Prevent the player being caught multiple times by the same speed-camera
                                    APlayerData[playerid][PlayerCaughtSpeeding] = 20;
                                    // Increase the wanted-level of this player by 1 star
                                    SetPlayerWantedLevel(playerid, GetPlayerWantedLevel(playerid) + 1);
                                    // Let the player know he's been caught speeding
                                    SendClientMessage(playerid, 0xFFFFFFFF, TXT_PlayerCaughtSpeeding);

                                    // Get the name of the player
                                    GetPlayerName(playerid, Name, sizeof(Name));
                                    // Also inform all police players that this player is caught speeding
                                    format(Msg, 128, "{00FF00}Player {FFFF00}%s{00FF00} is caught speeding, pursue and fine him", Name);
                                    Police_SendMessage(Msg);
                                    SRT_SendMessage(Msg);
                                }
                                else return 0;
                            }
                    }
                    else return 0;
                }
        }
        else
            APlayerData[playerid][PlayerCaughtSpeeding]--;
    }

    return 1;
}
You meant it like that?
Reply
#6

pawn Код:
public LaserPolice(); // <<< remove this semicolon
Start of function body without function header nearly always means that you placed a semicolon after the function header.
Reply
#7

What seems to be generating some errors is the semi-colon after the public header.
pawn Код:
forward LaserPolice();
public LaserPolice() // No semicolon here!
Also, I see you're returning 0 in various places. Why is this? Seems unnecessary to me. You can also combine 2 if-checks, these:
pawn Код:
if(GetPlayerVehicleSeat(playerid) == 0 && APlayerData[playerid][PlayerSpeed] > 130)
    {
    if ((APlayerData[playerid][PlayerClass] == ClassBusDriver) || (APlayerData[playerid][PlayerClass] == ClassTruckDriver) || (APlayerData[playerid][PlayerClass] == ClassPilot) || (APlayerData[playerid][PlayerClass] == ClassMafia) || (APlayerData[playerid][PlayerClass] == ClassCourier) || (APlayerData[playerid][PlayerClass] == ClassAssistance) ||(APlayerData[playerid][PlayerClass] == ClassRoadWorker))
    {
    }
    // no else return needed here!
}
Should become:
pawn Код:
if(GetPlayerVehicleSeat(playerid) == 0 && APlayerData[playerid][PlayerSpeed] > 130 && (APlayerData[playerid][PlayerClass] == ClassBusDriver || APlayerData[playerid][PlayerClass] == ClassTruckDriver || APlayerData[playerid][PlayerClass] == ClassPilot || APlayerData[playerid][PlayerClass] == ClassMafia || APlayerData[playerid][PlayerClass] == ClassCourier || APlayerData[playerid][PlayerClass] == ClassAssistance || APlayerData[playerid][PlayerClass] == ClassRoadWorker))
{
}
Also, depending on how your class hierarchy is built, I don't see why not do:
pawn Код:
if(GetPlayerVehicleSeat(playerid) == 0 && APlayerData[playerid][PlayerSpeed] > 130 && APlayerData[playerid][PlayerClass] != ClassPolice && APlayerData[playerid][PlayerClass] != ClassSRT)
This way, all classes except for ClassPolice and ClassSRT can be labeled speeders!

Continuing further down in your code, I see this:
pawn Код:
for(new i; i < MAX_PLAYERS; i++)
                            {
                                if ((APlayerData[i][PlayerClass] == ClassPolice) || (APlayerData[playerid][PlayerClass] == ClassSRT))
                                {
                                    GetPlayerPos(i, PolicePos[1], PolicePos[2], PolicePos[3]);
                                }
                                if(IsPlayerInRangeOfPoint(playerid, 50.0, PolicePos[1], PolicePos[2], PolicePos[3]);
                                {
                                    // Prevent the player being caught multiple times by the same speed-camera
                                    APlayerData[playerid][PlayerCaughtSpeeding] = 20;
                                    // Increase the wanted-level of this player by 1 star
                                    SetPlayerWantedLevel(playerid, GetPlayerWantedLevel(playerid) + 1);
                                    // Let the player know he's been caught speeding
                                    SendClientMessage(playerid, 0xFFFFFFFF, TXT_PlayerCaughtSpeeding);

                                    // Get the name of the player
                                    GetPlayerName(playerid, Name, sizeof(Name));
                                    // Also inform all police players that this player is caught speeding
                                    format(Msg, 128, "{00FF00}Player {FFFF00}%s{00FF00} is caught speeding, pursue and fine him", Name);
                                    Police_SendMessage(Msg);
                                    SRT_SendMessage(Msg);
                                }
                                else return 0;
                            }
But there's something in your logic that will definitely not benefit you. What you currently do is (the code itself is a bit bugged as well):
1. Get the position of the police officer (one GetPlayerPos per police officer)
2. See if the player is near the police officer (one IsPlayerInRangeOfPoint per police officer)
What you could do is:
1. Get the position of the speeding player (ONCE!!!)
2. See if the police officer is near the speeding player (one IsPlayerInRangeOfPoint per police officer).
This way things would be much more efficient.
pawn Код:
GetPlayerPos(i, PlayerPOS[1], PlayerPOS[2], PlayerPOS[3]);
for(new i; i != MAX_PLAYERS; i++) // changed < to != because the instruction should be a bit faster(?)
{
    // See if the player "i" is a police officer or SRT and if they're near the playerid that is speeding.
    if(APlayerData[i][PlayerClass] == ClassPolice || APlayerData[i][PlayerClass] == ClassSRT
    && IsPlayerInRangeOfPoint(i, 50.0, PlayerPOS[1], PlayerPOS[2], PlayerPOS[3]))
    {
        // The player is speeding!
    }
}
(define Float:PlayerPOS[3] in the upper part of the function)

Once you post the new errors and your questions, I can continue helping you!
Reply
#8

I'm having some trouble understanding the last part you posted, maybe if you are in for it, you can help me via teamviewer.
Reply
#9

I am on vacation and I don't have such tools on my laptop. And I don't see what's so complicated. Read what I said through a few more times and try to understand how loops work.

Then go and simply replace the inner loop (looping through i) with the code that I posted. Copying and pasting is a hassle for me right now so I'll leave you the task of doing that yourself.
Reply
#10

I'll look on it tonight, thanks! Keep in touch!
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)