05.09.2010, 16:12
Hello dear users, I browsed the forums a little bit and found out that there is only one race making tutorial,the problem is, it was in Portuguese D:
Well anyway, lets begin!
Firstly,I'm giving a code,and only then deeply explain what I made.
Probably the most important include you'll need,you cannot do 99% of the actions in PAWN(sa-mp's case) without it .
This is the money that the winning player will be given when he reaches the last checkpoint.
Well,this is the checkpoints variable.
It's the order of the checkpoints placement.
10 is the amount of checkpoints that would be in your race,3 is the amount of information each checkpoint includes, the 3 information pieces are of course,the X,the Y,and the Z.
Player_CheckPoint will be the variable that defines how much checkpoints the player has picked up. (We will need that). Well,why MAX_PLAYERS? Because we need an individual variable for each player because every player is in a different checkpoint.
bool:IsRaceRunning is a Boolean( a variable that usually used on true/false statements) to check if the race is running. It will be used for the race cancellation when the first player picks up the final checkpoint.
WinnerName is just a variable to hold the winners' name.
stringwin will be the string that we'll use to format a message to all of the other competitors announcing them that the race has ended.
Moving on!
Now, we're using the OnGameModeInit() callback,which is called when the gamemode is loaded on 'samp-server.exe'.
We're looping through all the connected players,setting their current checkpoint to the first one,and setting them a race checkpoint with the race checkpoints' information, now we'll go trough every parameter in the SetPlayerRaceCheckpoint.
'i' - Making sure the 'playerid' parameter equals to the looped players.
'0' - The race checkpoint type. '0' is the normal type which includes a nice arrow directing you to the next checkpoint.
CheckPoints[0][0] - This means,we're setting to the players the FIRST checkpoint in the CheckPoints variable,the count starts from zero. The first [0] is the ID of the checkpoint,it's 0 because it's the first checkpoint. The second [0] is the first information piece of the first checkpoint ID. In our case,the X.
CheckPoints[0][1] - Again as the previous checkpoint,just this time it's the Y we're setting.
CheckPoints[0][2] - And again,this time, it's the Z of the first checkpoint.
CheckPoints[1][0] - As I explained before,this parameter is the nextx parameter,which is actually the X position of the following checkpoint,it's only used for the arrow pointing position.
CheckPoints[1][1] - Again,the nexty parameter.
CheckPoints[1][2] - And again,the nextz parameter.
'9.7' - The size of the race checkpoint.
A public that is called every time a player enters a race checkpoint.
We're checking if the race is still running,then enlarging the player who entered the checkpoints' variable to +1.
Okay,this part is a bit tricky, we're checking if the player is in any checkpoint that is previous to the tenth one,and then checking if the race is still running.
Sounds tricky? Well, remember that in here, 9, is actually the 10th checkpoint,since the count starts from 0.
And we're checking if he is not in the 10th checkpoint because if he is in the 10th checkpoint,he will be in one checkpoint before the last one,which means we'll need to set a different type of race checkpoint,which is 1.
CheckPoints[Player_CheckPoint[playerid]][0 - 2] Again,setting his checkpoint to the next one. Why do we have [Player_CheckPoint[playerid]] as the checkpoint ID? Because we've increased the variable previously,remember?
CheckPoints[Player_CheckPoint[playerid]+1][0 -2] Okay,this one is simpler than it looks.
We're adding '+1' to set the following checkpoint AFTER the next checkpoint,again,for arrow pointing directions.
Again,now,checking if the players' checkpoint counter counted 10 picked up checkpoints,and if the race is still running,and then setting his checkpoint to the last one,just with type 1,which is a race checkpoint with a finish line drawing in it.
Now,checking if the player is has picked up the last checkpoint.
Then loops trough all players,and disabling their checkpoints.
Then we're getting the players' name using the WinnerName variable from the start,remember?
Then,formatting to the winstring we've also declared at the start,the message with the winners' name,and the amount of money he won.
Then,sending everyone the formatted string,and giving the winner the money we've defined in the begging,and canceling the race.
Ending up,your script should look something like this:
http://pastebin.com/rfVyvfDb
If something was misunderstood, just ask, if they are bugs, I'll fix them right away.
Well anyway, lets begin!
Firstly,I'm giving a code,and only then deeply explain what I made.
pawn Код:
#include <a_samp>
pawn Код:
#define winner_money 10000
pawn Код:
new CheckPoints[10][3] =
{
{x,y,z},
{x,y,z},
{x,y,z},
{x,y,z},
{x,y,z},
{x,y,z},
{x,y,z},
{x,y,z},
{x,y,z},
{x,y,z}
};
It's the order of the checkpoints placement.
10 is the amount of checkpoints that would be in your race,3 is the amount of information each checkpoint includes, the 3 information pieces are of course,the X,the Y,and the Z.
pawn Код:
new Player_CheckPoint[MAX_PLAYERS];
new bool:IsRaceRunning = false;
new WinnerName[MAX_PLAYER_NAME];
new stringwin[64];
bool:IsRaceRunning is a Boolean( a variable that usually used on true/false statements) to check if the race is running. It will be used for the race cancellation when the first player picks up the final checkpoint.
WinnerName is just a variable to hold the winners' name.
stringwin will be the string that we'll use to format a message to all of the other competitors announcing them that the race has ended.
Moving on!
pawn Код:
public OnGameModeInit()
{
for(new i = 0; i < MAX_PLAYERS; i++)
{
if(IsPlayerConnected(i))
{
Player_CheckPoint[i] = 0;
SetPlayerRaceCheckpoint(i,0,CheckPoints[0][0],CheckPoints[0][1],CheckPoints[0][2],CheckPoints[1][0],CheckPoints[1][1],CheckPoints[1][2],9.7);
IsRaceRunning = true;
}
}
return 1;
}
We're looping through all the connected players,setting their current checkpoint to the first one,and setting them a race checkpoint with the race checkpoints' information, now we'll go trough every parameter in the SetPlayerRaceCheckpoint.
'i' - Making sure the 'playerid' parameter equals to the looped players.
'0' - The race checkpoint type. '0' is the normal type which includes a nice arrow directing you to the next checkpoint.
CheckPoints[0][0] - This means,we're setting to the players the FIRST checkpoint in the CheckPoints variable,the count starts from zero. The first [0] is the ID of the checkpoint,it's 0 because it's the first checkpoint. The second [0] is the first information piece of the first checkpoint ID. In our case,the X.
CheckPoints[0][1] - Again as the previous checkpoint,just this time it's the Y we're setting.
CheckPoints[0][2] - And again,this time, it's the Z of the first checkpoint.
CheckPoints[1][0] - As I explained before,this parameter is the nextx parameter,which is actually the X position of the following checkpoint,it's only used for the arrow pointing position.
CheckPoints[1][1] - Again,the nexty parameter.
CheckPoints[1][2] - And again,the nextz parameter.
'9.7' - The size of the race checkpoint.
pawn Код:
public OnPlayerEnterRaceCheckpoint(playerid)
pawn Код:
if(IsRaceRunning == true) { Player_CheckPoint[playerid]++; }
pawn Код:
if(Player_CheckPoint[playerid] < 9 && IsRaceRunning == true)
{
DisablePlayerRaceCheckpoint(playerid);
SetPlayerRaceCheckpoint(playerid,0,CheckPoints[Player_CheckPoint[playerid]][0],CheckPoints[Player_CheckPoint[playerid]][1],CheckPoints[Player_CheckPoint[playerid]][2],CheckPoints[Player_CheckPoint[playerid]+1][0],CheckPoints[Player_CheckPoint[playerid]+1][1],CheckPoints[Player_CheckPoint[playerid]+1][2],9.7);
return 1;
}
Sounds tricky? Well, remember that in here, 9, is actually the 10th checkpoint,since the count starts from 0.
And we're checking if he is not in the 10th checkpoint because if he is in the 10th checkpoint,he will be in one checkpoint before the last one,which means we'll need to set a different type of race checkpoint,which is 1.
CheckPoints[Player_CheckPoint[playerid]][0 - 2] Again,setting his checkpoint to the next one. Why do we have [Player_CheckPoint[playerid]] as the checkpoint ID? Because we've increased the variable previously,remember?
CheckPoints[Player_CheckPoint[playerid]+1][0 -2] Okay,this one is simpler than it looks.
We're adding '+1' to set the following checkpoint AFTER the next checkpoint,again,for arrow pointing directions.
pawn Код:
if(Player_CheckPoint[playerid] == 9 && IsRaceRunning == true)
{
DisablePlayerRaceCheckpoint(playerid);
SetPlayerRaceCheckpoint(playerid,0,CheckPoints[Player_CheckPoint[playerid]][0],CheckPoints[Player_CheckPoint[playerid]][1],CheckPoints[Player_CheckPoint[playerid]][2],CheckPoints[Player_CheckPoint[playerid]+1][0],CheckPoints[Player_CheckPoint[playerid]+1][1],CheckPoints[Player_CheckPoint[playerid]+1][2],9.7);
return 1;
}
pawn Код:
if(Player_CheckPoint[playerid] == 10 && IsRaceRunning == true)
{
for(new i = 0; i < MAX_PLAYERS; i++)
{
DisablePlayerRaceCheckpoint(i);
}
GetPlayerName(playerid,WinnerName,sizeof(WinnerName));
format(stringwin,sizeof(stringwin),"[RACE] The race is over, %s has won and achieved $%d!",WinnerName,winner_money);
SendClientMessageToAll(0xFF0000FF,stringwin);
GivePlayerMoney(playerid,winner_money);
IsRaceRunning = false;
return 1;
}
Then loops trough all players,and disabling their checkpoints.
Then we're getting the players' name using the WinnerName variable from the start,remember?
Then,formatting to the winstring we've also declared at the start,the message with the winners' name,and the amount of money he won.
Then,sending everyone the formatted string,and giving the winner the money we've defined in the begging,and canceling the race.
Ending up,your script should look something like this:
http://pastebin.com/rfVyvfDb
If something was misunderstood, just ask, if they are bugs, I'll fix them right away.