SA-MP Forums Archive
[Tutorial] How to make Capture Zones! - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+---- Forum: Tutorials (https://sampforum.blast.hk/forumdisplay.php?fid=70)
+---- Thread: [Tutorial] How to make Capture Zones! (/showthread.php?tid=453013)

Pages: 1 2


How to make Capture Zones! - JimmyCh - 23.07.2013

Hey guys, I'm back with a new tutorial.
I'm pretty sure you're gonna like this one, because the ones I've seen aren't the best, so this tutorial will help you make fully working capture zones!
Let's get started!
Before we start, make sure you do the following include under #include <a_samp>:
pawn Код:
#include <streamer>
First thing we're gonna do is defining the gang zone's name.
Let's call it CAPZONE.
pawn Код:
#define CAPZONE 0
I will make two teams so you can understand more.
pawn Код:
#define TEAM1 0
#define TEAM2 1
#define NONE  2
NONE will be used OnGameModeInIt to show it's not captured by anyone.

Some color defines for the teams and others we will use.
pawn Код:
#define     TEAM1_COLOR     0x0011FF93
#define     TEAM2_COLOR     0xFF0000C8
#define     COLOR_WHITE     0xFFFFFFFF
#define     COLOR_RED       0xFF0000FF
#define     COLOR_GREEN     0x33AA33AA
#define     COLOR_ORANGE    0xFF560AFF
#define     STEALTH_BLUE    0x0077BB00
Now we need some stuff to make our script work, add this on top of the script:
pawn Код:
new tCP[30];
new UnderAttack[30] = 0;
new CP[30];
new Zone[30];
new Captured[MAX_PLAYERS][30];
new timer[MAX_PLAYERS][30];
new CountVar[MAX_PLAYERS][30];
new Text:CountText[MAX_PLAYERS];
new IsPlayerCapturing[MAX_PLAYERS][30];
We will use those later.

Alright we're done with that now!
Let's move on!
Now OnGameModeInIt, we're gonna put that the zone is not captured by anyone, and isn't under attack, and we're gonna create the gang zone and dynamic CP!
We will add the following:
pawn Код:
tCP[CAPZONE] = NONE;
    UnderAttack[CAPZONE] = 0;
    Zone[CAPZONE] = GangZoneCreate(MinX, MinY, MaxX, MaxY);
    CP[CAPZONE] = CreateDynamicCP(X,Y,Z, radius, -1, -1, -1, 100.0);
Let me explain what I did.
First the tCP[CAPZONE] = NONE; it's to show that the zone is not captured by anyone OnGameModeInIt.
It's not UnderAttack too( = 0), we also created the GangZone and The Dynamic CP.
Do not forget to change the coordinates to your zone location.

Now OnPlayerConnect, we need to add some stuff too:
Put the following:
pawn Код:
SetPlayerMapIcon(playerid, 2, X,Y,Z, 19, MAPICON_GLOBAL);
    GangZoneShowForAll(Zone[CAPZONE], 0x00000043);
    IsPlayerCapturing[playerid][CAPZONE] = 0;
    CountVar[playerid][CAPZONE] = 25;
    if(tCP[CAPZONE] == NONE) GangZoneShowForAll(Zone[CAPZONE], -66);
    else if(tCP[CAPZONE] == TEAM1) GangZoneShowForAll(Zone[CAPZONE], TEAM1_COLOR);
    else if(tCP[CAPZONE] == TEAM2) GangZoneShowForAll(Zone[CAPZONE], TEAM2_COLOR);
So first of all, we're creating a map icon on the capture zone, change the coordinates of it(It's a flag by the way).
We made the Zone[CAPZONE] show as white color because it's not captured.
Since this is OnPlayerConnect, so we're checking if the zone if captured so we show the player who captured the zone.
We put that if the zone is captured by TEAM1, it shows as TEAM1 color.
And for the TEAM2 the same thing, else if it's not captured it shows as white.

Let's move to OnPlayerDisconnect now, we will put the following:
pawn Код:
if(Captured[playerid][CAPZONE] == 0 && IsPlayerCapturing[playerid][CAPZONE] == 1)
    {
    LeavingCAPZONE(playerid);
    }
So what I did now that if the player is capturing the capture zone and disconnects, it will check the stock that we will make later, the LeavingCAPZONE stock.

Now OnPlayerSpawn, we will also put this:
pawn Код:
if(Captured[playerid][CAPZONE] == 0 && IsPlayerCapturing[playerid][CAPZONE] == 1)
    {
    LeavingCAPZONE(playerid);
    }
We checked the same, if he's capturing and spawns, it will stop capturing, else it will bug and keep capturing EVEN if he left the area.

OnPlayerDeath, we will do the same:
pawn Код:
if(Captured[playerid][CAPZONE] == 0 && IsPlayerCapturing[playerid][CAPZONE] == 1)
        {
        LeavingCAPZONE(playerid);
        }
We also checked that if he died while capturing, it will return to the stock LeavingCAPZONE which we'll check later.

Now let's move on to the OnPlayerEnterDynamicCP.
Let's start with this:
pawn Код:
forward OnPlayerEnterDynamicCP(playerid, checkpointid);
public OnPlayerEnterDynamicCP(playerid, checkpointid)
Now that we have this, we will check that if the checkpoint is called CAPZONE, it will start capturing(Also a stock).
So OnPlayerEnterDynamicCP we got the following:
pawn Код:
forward OnPlayerEnterDynamicCP(playerid, checkpointid);
public OnPlayerEnterDynamicCP(playerid, checkpointid)
{
    if(checkpointid == CP[CAPZONE])
    {
        if(UnderAttack[CAPZONE] == 0)
        {
            if(tCP[CAPZONE] != gTeam[playerid])
            {
                CountVar[playerid][CAPZONE] = 25;
                ActiveCAPZONE(playerid);

            } else return SendClientMessage(playerid, COLOR_RED,"*This zone is already captured by your team!");
        } else return CaptureZoneMessage(playerid, 2);
    }
}
We checked that if he entered the zone and it's not under attack already, it will start capturing, else it will show a message that it's under attack.
And we checked that if his team already captured it, he can't capture it.

Now, the same for OnPlayerLeaveDynamicCP, we will put this:
pawn Код:
forward OnPlayerLeaveDynamicCP(playerid, checkpointid);
public OnPlayerLeaveDynamicCP(playerid, checkpointid)
{
    if(checkpointid == CP[CAPZONE] && Captured[playerid][CAPZONE] == 0 && IsPlayerCapturing[playerid][CAPZONE] == 1 && !IsPlayerInDynamicCP(playerid, CP[CAPZONE]))
    {
        LeavingCAPZONE(playerid);
    }
}
We just checked that if the CP is the CAPZONE checkpoint, and if he's capturing and if he left the checkpoint, it will also stop capturing( From the stock which we're gonna put).

Now let's make the stock for the CaptureZoneMessage before we continue:
pawn Код:
stock CaptureZoneMessage(playerid, messageid)
{
    switch(messageid)
    {
       case 1:
       {
           SendClientMessage(playerid, COLOR_RED,"You cannot capture while in a vehicle!");
       }
       case 2:
       {
           SendClientMessage(playerid, COLOR_RED,"This zone is already being taken over!");
       }
    }
    return 1;
}
Alright, we're good to start the real work!
Let's start with the stock, ActiveCAPZONE.
pawn Код:
stock ActiveCAPZONE(playerid)
{
        if(UnderAttack[CAPZONE] == 0)
        {
            if(!IsPlayerInAnyVehicle(playerid))
            {
                UnderAttack[CAPZONE] = 1;
                timer[playerid][CAPZONE] = SetTimerEx("CAPZONETimer", 25000, false,"i",playerid);
                Captured[playerid][CAPZONE] = 0;
                SendClientMessage(playerid, 0xFFFFFFFF,"| Stay in this checkpoint for 25 seconds to capture it! |");
                if(gTeam[playerid] == TEAM1)
                {
                  GangZoneFlashForAll(Zone[CAPZONE], TEAM1_COLOR);
                }
                else if(gTeam[playerid] == TEAM2)
                {
                  GangZoneFlashForAll(Zone[CAPZONE], TEAM2_COLOR);
                }

                //------Message-----
                if(tCP[CAPZONE] == TEAM1)
                {
                  SendClientMessage(playerid, COLOR_WHITE,"This flag is controlled by team 1!");
                  SendClientMessageToAll(STEALTH_BLUE,"*Capture Zone is under attack!");
                }
                else if(tCP[CAPZONE] == TEAM2)
                {
                  SendClientMessage(playerid, COLOR_WHITE,"This flag is controlled by team 2!");
                  SendClientMessageToAll(STEALTH_BLUE,"*Capture Zone is under attack!");
                }

                else if(tCP[CAPZONE] == NONE)
                {
                  SendClientMessage(playerid, COLOR_WHITE,"This flag is not controlled by any team!");
                }
                //---------loop-------//
                for(new i = 0; i < MAX_PLAYERS; i ++)
                {
                   IsPlayerCapturing[i][CAPZONE] = 1;
                }
            }
            else return CaptureZoneMessage(playerid, 1);
        }
        else return CaptureZoneMessage(playerid, 2);
        return 1;
}
Don't worry, gonna explain it.
First of all, to start capturing we checked if it's not already getting captured.
Secondly, we checked if he's in a vehicle, if so, it will return CaptureZoneMessage 1, which says that he can't capture in a vehicle.
If he follows the requirements, it starts the timer which is 25 seconds to capture(You can change it) and it will start flashing the zone with the team's color.
It will also send a message that the zone is under attack.
It will also tell the player who already controls the flag, Team1, Team2, or NONE.


Let's move on, shall we?
Now let's see CAPZONECaptured, where we have done capturing the zone.
We will make a system to send his team 1 score and the player himself some score and cash.
pawn Код:
stock CAPZONECaptured(playerid)
{

    Captured[playerid][CAPZONE] = 1;
    UnderAttack[CAPZONE] = 0;
    KillTimer(timer[playerid][CAPZONE]);
    CountVar[playerid][CAPZONE] = 25;
    GivePlayerScore(playerid, 5);
    GivePlayerMoney(playerid, 5000);
    SendClientMessage(playerid, COLOR_GREEN,"Congratulations! You have captured the area! You received +5 scores and +$5000 cash!");
    //==========================================================================
    for(new i = 0; i < MAX_PLAYERS; i++)
    {
       IsPlayerCapturing[i][CAPZONE] = 0;
       if(gTeam[i] == gTeam[playerid])
       {
           SendClientMessage(i, 0xFFFFFFFF,"*Your team has captured the area! You received +1 score for it!");
           GivePlayerScore(i, 1);
       }
    }
    //==========================================================================
    tCP[CAPZONE] = gTeam[playerid];
    GangZoneStopFlashForAll(Zone[CAPZONE]);
    //==========================================================================
    if(gTeam[playerid] == USA)
    {
       GangZoneShowForAll(Zone[CAPZONE], TEAM1_COLOR);
    }
    else if(gTeam[playerid] == CHINA)
    {
    GangZoneShowForAll(Zone[CAPZONE], TEAM2_COLOR);
    }
    //==========================================================================
    new str[128];
    format(str, sizeof(str),"%s has captured the capture zone!", GetName(playerid));
    SendClientMessageToAll(COLOR_ORANGE, str);
    return 1;
}
Well it's pretty simple, if he captures it, he gets some score, and we did a loop to see if a player is in his team, it will give him 1 score.
We also sent a message to everyone saying the player captured the zone.

Now let's make the stock, LeavingCAPZONE.
pawn Код:
stock LeavingCAPZONE(playerid)
{
    Captured[playerid][CAPZONE] = 0;
    UnderAttack[CAPZONE] = 0;
    KillTimer(timer[playerid][CAPZONE]);
    CountVar[playerid][CAPZONE] = 25;
    GangZoneStopFlashForAll(Zone[CAPZONE]);
    for(new i = 0; i < MAX_PLAYERS; i++)
    {
       IsPlayerCapturing[i][CAPZONE] = 0;
    }
    SendClientMessage(playerid, COLOR_RED,"*You have failed to capture this zone!");
    return 1;
}
It's as simple as before, if he leaves the CAPZONE, it stops capturing, and isn't under attack anymore.
The timer stops, and the zone stops flashing.
We made a loop to see who is capturing, and tell him he failed to capture the zone.

Now all left is the timer.
pawn Код:
forward CAPZONETimer(playerid);
public CAPZONETimer(playerid)
{
    CAPZONECaptured(playerid);
    return 1;
}
If you scroll up, you can see a GetName which we will create a stock for.
And here it is:
pawn Код:
stock GetName(playerid)
{
 new pnameid[24];
 GetPlayerName(playerid,pnameid,24);
 return pnameid;
}
One last thing, in case you got an error on "Undefined symbol ""GivePlayerScore"" ", we will create a stock on that to define what it is supposed to do:
Simply add the following:
pawn Код:
stock GivePlayerScore(playerid, score) // creating our stock
{
SetPlayerScore(playerid, GetPlayerScore(playerid) + score); // we're defining it as it will SetPlayerScore, since it's not known like GivePlayerMoney, there is just SetPlayerScore for the scores.
return 1;
}
I guess we're done! Hope I helped you guys, it took me ALOT of time to do this tutorial.
I hope I get some positive feedback, if you encounter any bug or see any mistake in the script, or got any questions, do not hesitate to post!
Thank you for reading!


Re: How to make Capture Zones! - Nofear192 - 23.07.2013

Nice Job bro ;]


Re: How to make Capture Zones! - Blademaster680 - 23.07.2013

Welldone Jimmy


Re: How to make Capture Zones! - JimmyCh - 23.07.2013

Thank you guys


Re: How to make Capture Zones! - Mckarlis - 23.07.2013

Thanks this is really good +REP


Re: How to make Capture Zones! - JimmyCh - 23.07.2013

Thank you Karlis


Re: How to make Capture Zones! - Necip - 23.07.2013

Such a nice tutorial.Explained and helpful.Gave you a +REP.


Re: How to make Capture Zones! - JimmyCh - 23.07.2013

Thank you Necip for your comment


Re: How to make Capture Zones! - Areax - 23.07.2013

Thank you for this NICE tutorial! REP+


Re: How to make Capture Zones! - JimmyCh - 23.07.2013

Thank you for your feedback! If you guys need more tutorials, I have nothing to do and looking for ideas!


Re: How to make Capture Zones! - Nirzor - 24.07.2013

Jimmy this Capture Zone system you got there was created by me and Jarnu then slowly it spread and you have

explained our capture zones better than us.For that thanks and you could give our credits.Anyways the tutorial

was great +REP. Give us credit xD.


Re: How to make Capture Zones! - CrewMaster - 24.07.2013

nice tutorial bro...


Re: How to make Capture Zones! - JimmyCh - 24.07.2013

Thanks guys.


Re: How to make Capture Zones! - ThePhenix - 24.07.2013

Why new tCP[30];??

If you are just creating one capture zone?


Re: How to make Capture Zones! - JimmyCh - 24.07.2013

Because I'm pretty sure people who are gonna use capture zones won't just create one capture zone, they're going to create atleast 3-4 capture zones(If not more).


Re: How to make Capture Zones! - ThePhenix - 24.07.2013

You just need 1 to create one capture zone - 2 to create 2...


Re: How to make Capture Zones! - Nirzor - 24.07.2013

Phenix people don't understand that okay? My server has 20 + capture zones.

Thanks for the credits and Rep + for you as you did alot of work on this tutorial and pretty thanks for it for people to

know it.


Re: How to make Capture Zones! - JimmyCh - 24.07.2013

Thanks for acting like you +repped while you -repped me
What credits? I ain't giving credits


Re: How to make Capture Zones! - Nirzor - 24.07.2013

Bro I didn't -rep you promise and you could give credits to real author it says I need to spread more rep before

giving it to you again!


Re: How to make Capture Zones! - JimmyCh - 24.07.2013

That's because you just -repped me, Einstein.
I swear if you keep -repping for fun I will report you, you're just getting my reputation lower on this great tutorial.
Don't start acting stupid now, will ya'?