SA-MP Forums

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

Reply
 
Thread Tools Display Modes
Old 08/07/2012, 01:49 PM   #1
SomebodyAndMe
Huge Clucker
 
SomebodyAndMe's Avatar
 
Join Date: Feb 2010
Location: 52.511938,6.092434 find it.
Posts: 473
Reputation: 21
Default Check player speeding on-foot and on-car

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 Code:
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:
Code:
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
SomebodyAndMe is offline   Reply With Quote
Old 08/07/2012, 01:55 PM   #2
AndreT
High-roller
 
AndreT's Avatar
 
Join Date: Jul 2011
Posts: 1,014
Reputation: 392
Default Re: Check player speeding on-foot and on-car

Can you explain what's this:
pawn Code:
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 Code:
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 Code:
if(GetPlayerVehicleSeat(playerid) == 0 && APlayerData[playerid][PlayerSpeed] > 130)
{
}

Also look at this:
pawn Code:
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 Code:
if(APlayerData[playerid][LoggedIn] == false) continue;
GetPlayerPos(playerid, PolicePos[1], PolicePos[2], PolicePos[3]);
AndreT is offline   Reply With Quote
Old 08/07/2012, 02:00 PM   #3
SomebodyAndMe
Huge Clucker
 
SomebodyAndMe's Avatar
 
Join Date: Feb 2010
Location: 52.511938,6.092434 find it.
Posts: 473
Reputation: 21
Default Re: Check player speeding on-foot and on-car

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?
SomebodyAndMe is offline   Reply With Quote
Old 08/07/2012, 02:04 PM   #4
AndreT
High-roller
 
AndreT's Avatar
 
Join Date: Jul 2011
Posts: 1,014
Reputation: 392
Default Re: Check player speeding on-foot and on-car

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.
AndreT is offline   Reply With Quote
Old 08/07/2012, 02:17 PM   #5
SomebodyAndMe
Huge Clucker
 
SomebodyAndMe's Avatar
 
Join Date: Feb 2010
Location: 52.511938,6.092434 find it.
Posts: 473
Reputation: 21
Default Re: Check player speeding on-foot and on-car

Code:
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 Code:
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?
SomebodyAndMe is offline   Reply With Quote
Old 08/07/2012, 02:35 PM   #6
Vince
Godfather
 
Vince's Avatar
 
Join Date: Sep 2007
Location: Belgium
Posts: 6,104
Reputation: 1623
Default Re: Check player speeding on-foot and on-car

pawn Code:
public LaserPolice(); // <<< remove this semicolon
Start of function body without function header nearly always means that you placed a semicolon after the function header.
__________________
I will rarely post a ready made solution in your thread, but rather I try to make you realize your own mistakes so you can fix them yourself.
Give a man a fish and you feed him for a day; teach a man to fish and you feed him for a lifetime.
Vince is offline   Reply With Quote
Old 08/07/2012, 02:43 PM   #7
AndreT
High-roller
 
AndreT's Avatar
 
Join Date: Jul 2011
Posts: 1,014
Reputation: 392
Default Re: Check player speeding on-foot and on-car

What seems to be generating some errors is the semi-colon after the public header.
pawn Code:
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 Code:
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 Code:
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 Code:
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 Code:
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 Code:
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!
AndreT is offline   Reply With Quote
Old 08/07/2012, 02:52 PM   #8
SomebodyAndMe
Huge Clucker
 
SomebodyAndMe's Avatar
 
Join Date: Feb 2010
Location: 52.511938,6.092434 find it.
Posts: 473
Reputation: 21
Default Re: Check player speeding on-foot and on-car

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

Last edited by SomebodyAndMe; 20/11/2013 at 05:25 PM.
SomebodyAndMe is offline   Reply With Quote
Old 08/07/2012, 03:18 PM   #9
AndreT
High-roller
 
AndreT's Avatar
 
Join Date: Jul 2011
Posts: 1,014
Reputation: 392
Default Re: Check player speeding on-foot and on-car

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.
AndreT is offline   Reply With Quote
Old 08/07/2012, 03:22 PM   #10
SomebodyAndMe
Huge Clucker
 
SomebodyAndMe's Avatar
 
Join Date: Feb 2010
Location: 52.511938,6.092434 find it.
Posts: 473
Reputation: 21
Default Re: Check player speeding on-foot and on-car

I'll look on it tonight, thanks! Keep in touch!
SomebodyAndMe 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
check for player pos (+rep) WardenCS Scripting Help 7 09/04/2012 07:25 PM
/enter that brings you in the car if in a car, or on foot if on foot. Garc1a Help Archive 2 16/09/2010 06:23 PM
[HELP] IS PLAYER ON FOOT COMMAND akis_tze Help Archive 2 30/12/2009 08:53 PM
Need help speeding up my computer? DeltaAirlines12 Everything and Nothing 28 24/11/2009 11:51 PM
is player on foot hipy Help Archive 5 22/06/2009 06:05 PM


All times are GMT. The time now is 04:06 AM.


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