Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*----------------------------------------------------------------------------*\
- Base Race Script - FilterScript
- @Description:
- A base of a race-system with player positions.
- @Legal:
- This Source Code Form is subject to the terms of the Mozilla Public
- License, v. 2.0. If a copy of the MPL was not distributed with this
- file, You can obtain one at http://mozilla.org/MPL/2.0/.
- The Initial Developer of this Code is Lucas "Larceny" Godoy.
- Codes created by the Initial Developer are Copyright (C) 2012
- the Initial Developer.
- All Rights Reserved.
- @Version:
- 1.0 - 19:47 14/11/2012 - lines: 246
- 1.1 - 23:57 14/11/2012 - lines: 287
- 1.2 - 12:58 15/11/2012 - lines: 315
- 1.3 - 14:52 16/11/2012 - lines: 359
- 1.4 - 16:36 16/11/2012 - lines: 478
- @Change-log:
- 1.1:
- Message to all of what position the player finished and bugs fixed.
- 1.2:
- Command to reset race.
- 1.3:
- MAX_CHECKPOINT_DISTANCE definition, improvements at checkings and
- bonus to the first three winners.
- 1.4:
- Show how much time the player took to end the race and
- minor changes.
- @Author:
- Lucas "Larceny" Godoy
- @Thanks-to:
- Slice, ----------> md-sort and some codes from his post
- Yagu,------------> SetPlayerCheckpoint code
- Y_Less,----------> y_commands and y_timers includes
- Gustavo Araújo, -> Beta-testing and hosting the server to testing
- Lós, ------------> Beta-testing
- Lucas Silva, ----> Beta-testing
- Murilo Sousa, ---> Giving me the idea of making this script
- SA-MP Team ------> (c) SA-MP
- \*----------------------------------------------------------------------------*/
- /*----------------------------------------------------------------------------*\
- This is a filterscript.
- \*----------------------------------------------------------------------------*/
- #define FILTERSCRIPT
- /*----------------------------------------------------------------------------*\
- More definitions
- \*----------------------------------------------------------------------------*/
- static const Airrace = 0;
- static const Float:CPsize = 3.5;
- #define IsPlayerInRace(%0) g_ePlayerRaceData[%0][isInRace]
- // The maximum distance that a checkpoint can be from other one
- #define MAX_CHECKPOINT_DISTANCE 1000.0
- /*----------------------------------------------------------------------------*\
- Variables declarations
- \*----------------------------------------------------------------------------*/
- new g_players_in_race;
- new g_players_win_pos;
- /*----------------------------------------------------------------------------*\
- Importing inc files...
- \*----------------------------------------------------------------------------*/
- #include <a_samp>
- #include <md-sort>
- #include <YSI\y_timers>
- #include <YSI\y_iterate>
- #include <YSI\y_commands>
- /*----------------------------------------------------------------------------*\
- Forward declarations
- \*----------------------------------------------------------------------------*/
- forward SetRaceCheckpoint(playerid, target, next);
- /*----------------------------------------------------------------------------*\
- Checkpoint positions
- \*----------------------------------------------------------------------------*/
- new Float:g_fCheckpointPositions[][] = {
- {2045.9878, 1255.0852, 10.3989},
- {2045.6395, 1143.7676, 10.3990},
- {2045.2872, 1022.4197, 10.3990},
- {2045.5452, 874.15550, 6.70830},
- {2066.6753, 869.71150, 6.60710},
- {2068.1384, 990.57460, 10.3970},
- {2069.2429, 1156.9362, 10.4019},
- {2069.7380, 1361.6147, 10.3986},
- {2070.2107, 1587.7080, 10.4068},
- {2128.6960, 1828.0879, 10.3990},
- {2146.8923, 2037.6427, 10.3990},
- {2146.8491, 2213.1565, 10.3990},
- {2179.2429, 2344.7166, 10.3989},
- {2106.0913, 2285.1643, 10.3989},
- {2128.9253, 2082.8274, 10.3990},
- {2083.6333, 1775.9579, 10.3990},
- {2048.1340, 1524.6210, 10.3990},
- {2047.9237, 1390.8652, 10.3990}
- };
- /*----------------------------------------------------------------------------*\
- Player definitions
- \*----------------------------------------------------------------------------*/
- enum E_RACE_DATA
- {
- PlayerID,
- PlayerCP,
- isInRace,
- PlayerPos,
- PlayerTime,
- Float:DistanceToFinish
- }
- new g_ePlayerRaceData[MAX_PLAYERS][E_RACE_DATA];
- /*----------------------------------------------------------------------------*\
- This callback is called when the filterscript is initialized.
- Param:
- -
- \*----------------------------------------------------------------------------*/
- public OnFilterScriptInit()
- {
- print ("\n");
- print ("|-------------------------------------------|");
- print ("| |");
- print ("| Base Race Script Filterscript by (c) Larc'|");
- printf ("| Running Version: 1.4 |");
- print ("| |");
- print ("|-------------------------------------------|\n");
- return 1;
- }
- /*----------------------------------------------------------------------------*\
- This callback is called when the filterscript is turned off.
- Param:
- -
- \*----------------------------------------------------------------------------*/
- public OnFilterScriptExit()
- {
- return 1;
- }
- /*----------------------------------------------------------------------------*\
- This callback is called when a player connects to the server.
- Param:
- - playerid: ID of the player that has connected.
- \*----------------------------------------------------------------------------*/
- public OnPlayerConnect(playerid)
- {
- g_ePlayerRaceData[playerid][isInRace] = 0;
- g_ePlayerRaceData[playerid][DistanceToFinish] =
- (2147482646.0 + float(playerid));
- return 1;
- }
- /*----------------------------------------------------------------------------*\
- This callback is called when a player disconnects of the server.
- Param:
- - playerid: ID of the player that has connected.
- \*----------------------------------------------------------------------------*/
- public OnPlayerDisconnect(playerid, reason)
- {
- if(g_ePlayerRaceData[playerid][isInRace] == 1)
- {
- g_players_in_race--;
- g_ePlayerRaceData[playerid][isInRace] = 0;
- g_ePlayerRaceData[playerid][DistanceToFinish] =
- (2147482646.0 + float(playerid));
- }
- return 1;
- }
- /*----------------------------------------------------------------------------*\
- This callback is called every-time a client/player updates the server with
- their status.
- Param:
- - playerid: ID of the player whom has been updated.
- \*----------------------------------------------------------------------------*/
- public OnPlayerUpdate(playerid)
- {
- if(g_ePlayerRaceData[playerid][isInRace] == 1)
- {
- new Float:fDistance = GetPlayerDistanceFromPoint(playerid,
- g_fCheckpointPositions[g_ePlayerRaceData[playerid][PlayerCP]][0],
- g_fCheckpointPositions[g_ePlayerRaceData[playerid][PlayerCP]][1],
- g_fCheckpointPositions[g_ePlayerRaceData[playerid][PlayerCP]][2]);
- g_ePlayerRaceData[playerid][DistanceToFinish] =
- ( fDistance + MAX_CHECKPOINT_DISTANCE ) +
- ((sizeof(g_fCheckpointPositions) * MAX_CHECKPOINT_DISTANCE) -
- (g_ePlayerRaceData[playerid][PlayerCP] * MAX_CHECKPOINT_DISTANCE));
- }
- return 1;
- }
- /*----------------------------------------------------------------------------*\
- from Yagu's race filterscript, (c) by Yagu
- Param:
- -
- \*----------------------------------------------------------------------------*/
- public SetRaceCheckpoint(playerid, target, next)
- {
- if(next == -1 && Airrace == 0)
- SetPlayerRaceCheckpoint(playerid, 1, g_fCheckpointPositions[target][0],
- g_fCheckpointPositions[target][1],g_fCheckpointPositions[target][2],
- 0.0, 0.0, 0.0, CPsize);
- else if(next == -1 && Airrace == 1)
- SetPlayerRaceCheckpoint(playerid, 4, g_fCheckpointPositions[target][0],
- g_fCheckpointPositions[target][1],g_fCheckpointPositions[target][2],
- 0.0, 0.0, 0.0, CPsize);
- else if(Airrace == 1)
- SetPlayerRaceCheckpoint(playerid, 3, g_fCheckpointPositions[target][0],
- g_fCheckpointPositions[target][1],g_fCheckpointPositions[target][2],
- g_fCheckpointPositions[next][0], g_fCheckpointPositions[next][1],
- g_fCheckpointPositions[next][2], CPsize);
- else
- SetPlayerRaceCheckpoint(playerid, 0, g_fCheckpointPositions[target][0],
- g_fCheckpointPositions[target][1],g_fCheckpointPositions[target][2],
- g_fCheckpointPositions[next][0],g_fCheckpointPositions[next][1],
- g_fCheckpointPositions[next][2], CPsize);
- }
- /*----------------------------------------------------------------------------*\
- This callback is called when a player enter in a race checkpoint.
- Param:
- - playerid: ID of the player whom has entered in a r-cp.
- \*----------------------------------------------------------------------------*/
- public OnPlayerEnterRaceCheckpoint(playerid)
- {
- g_ePlayerRaceData[playerid][PlayerCP]++;
- if(g_ePlayerRaceData[playerid][PlayerCP] ==
- sizeof(g_fCheckpointPositions)-1)
- {
- SetRaceCheckpoint(playerid, g_ePlayerRaceData[playerid][PlayerCP], -1);
- }
- else if(g_ePlayerRaceData[playerid][PlayerCP] ==
- sizeof(g_fCheckpointPositions))
- {
- g_players_win_pos++;
- DisablePlayerRaceCheckpoint(playerid);
- g_ePlayerRaceData[playerid][isInRace] = 0;
- g_ePlayerRaceData[playerid][DistanceToFinish] = g_players_win_pos;
- new sTextToSend[144], sPlayerName[MAX_PLAYER_NAME], nWinnerBonus;
- GetPlayerName(playerid, sPlayerName, MAX_PLAYER_NAME);
- nWinnerBonus = 4000 - (g_players_win_pos * 1000);
- if(g_players_win_pos < 4)
- {
- format(sTextToSend, sizeof(sTextToSend),
- "- %s finalizou a corrida em %iº lugar e ganhou $%i. \
- Tempo: %s", sPlayerName, g_players_win_pos, nWinnerBonus,
- convertTime(gettime() - g_ePlayerRaceData[playerid][PlayerTime]));
- GivePlayerMoney(playerid, nWinnerBonus);
- }
- else
- {
- format(sTextToSend, sizeof(sTextToSend),
- "- %s finalizou a corrida em %iº lugar.",
- sPlayerName, g_players_win_pos);
- }
- SendClientMessageToAll(-1, sTextToSend);
- }
- else
- {
- SetRaceCheckpoint(playerid, g_ePlayerRaceData[playerid][PlayerCP],
- g_ePlayerRaceData[playerid][PlayerCP]+1);
- }
- return 1;
- }
- /*----------------------------------------------------------------------------*\
- from Slice's md-sort post, (c) by Slice
- Param:
- -
- \*----------------------------------------------------------------------------*/
- Comparator:CompareDistanceToFinish(left[E_RACE_DATA], right[E_RACE_DATA])
- {
- // Returning negative means "left goes above"
- // Positive means "left goes below"
- // floatcmp returns -1 if right > left, 0 if equal, and 1 if left > right
- return floatcmp(left[DistanceToFinish], right[DistanceToFinish]);
- }
- /*----------------------------------------------------------------------------*\
- This task is called every second.
- Param:
- -
- \*----------------------------------------------------------------------------*/
- task OnClientUpdate[1000]()
- {
- new sorted_racers[MAX_PLAYERS];
- SortArrayUsingComparator(g_ePlayerRaceData, CompareDistanceToFinish, SORT_IS_PLAYERS) => sorted_racers;
- for (new i = 0; i < sizeof(sorted_racers); i++)
- {
- new playerid = sorted_racers[i];
- // All valid player IDs are at the start of the array,
- // so the first invalid one means the rest will be, too.
- if (playerid == INVALID_PLAYER_ID)
- break;
- // do stuff with "playerid"
- if(g_ePlayerRaceData[playerid][isInRace] == 1)
- {
- new sTextToSend[96];
- format(sTextToSend, sizeof(sTextToSend),
- "~n~~n~~n~~n~~n~~n~~n~~n~ \
- ~b~~h~%i ~w~/ %i",
- i+1, g_players_in_race);
- GameTextForPlayer(playerid, sTextToSend, 1111, 3);
- g_ePlayerRaceData[playerid][PlayerPos] = i+1;
- }
- }
- }
- /*----------------------------------------------------------------------------*\
- This function convert unix time stamp to human-readable date
- Params:
- - nTimeStamp: Unixtime to be converted
- (c) Larceny
- \*----------------------------------------------------------------------------*/
- convertTime(nTimeStamp)
- {
- new
- nYears = 0,
- nDays = 0,
- nMonths = 0,
- nHours = 0,
- nMins = 0,
- nSecs = 0,
- sFormattedString[80];
- while(nTimeStamp > 31622400)
- {
- nTimeStamp -= 31536000;
- nYears++;
- }
- while(nTimeStamp > 86400)
- {
- nTimeStamp -= 86400;
- nDays++;
- if(nDays == 31) nDays = 0, nMonths++;
- }
- while(nTimeStamp > 60)
- {
- nTimeStamp -= 60;
- nMins++;
- if(nMins == 60) nMins = 0, nHours++;
- }
- nSecs = nTimeStamp;
- if(nYears > 0)
- {
- format(sFormattedString, sizeof sFormattedString, "%02d anos, \
- %02d meses, %02d dias, %02d horas, %02d minutos, %02d segundos",
- nYears, nMonths, nDays, nHours, nMins, nSecs);
- }
- else if(nYears == 0 && nMonths > 0)
- {
- format(sFormattedString, sizeof sFormattedString, "\
- %02d meses, %02d dias, %02d horas, %02d minutos, %02d segundos",
- nMonths, nDays, nHours, nMins, nSecs);
- }
- else if(nYears == 0 && nMonths == 0 && nDays > 0)
- {
- format(sFormattedString, sizeof sFormattedString, "\
- %02d dias, %02d horas, %02d minutos, %02d segundos",
- nDays, nHours, nMins, nSecs);
- }
- else
- {
- format(sFormattedString, sizeof sFormattedString, "\
- %02d horas, %02d minutos, %02d segundos",
- nHours, nMins, nSecs);
- }
- return sFormattedString;
- }
- /*----------------------------------------------------------------------------*\
- This command put the player in the race.
- Param:
- -
- \*----------------------------------------------------------------------------*/
- YCMD:entrarcorrida(playerid, params[], help)
- {
- if(IsPlayerInRace(playerid))
- return SendClientMessage(playerid, -1, "- Você já está na corrida."), 1;
- if(g_ePlayerRaceData[playerid][DistanceToFinish] < MAX_CHECKPOINT_DISTANCE)
- return SendClientMessage(playerid, -1,
- "- Você não pode mais entrar em corridas."), 1;
- g_players_in_race++;
- g_ePlayerRaceData[playerid][PlayerCP] = 0;
- g_ePlayerRaceData[playerid][PlayerTime] = gettime();
- new Float:fDistance = GetPlayerDistanceFromPoint(playerid,
- g_fCheckpointPositions[g_ePlayerRaceData[playerid][PlayerCP]][0],
- g_fCheckpointPositions[g_ePlayerRaceData[playerid][PlayerCP]][1],
- g_fCheckpointPositions[g_ePlayerRaceData[playerid][PlayerCP]][2]);
- g_ePlayerRaceData[playerid][DistanceToFinish] =
- (fDistance + MAX_CHECKPOINT_DISTANCE) +
- (sizeof(g_fCheckpointPositions) * MAX_CHECKPOINT_DISTANCE);
- g_ePlayerRaceData[playerid][isInRace] = 1;
- SetRaceCheckpoint(playerid, g_ePlayerRaceData[playerid][PlayerCP],
- g_ePlayerRaceData[playerid][PlayerCP]+1);
- return 1;
- }
- /*----------------------------------------------------------------------------*\
- This command put the player out of the race.
- Param:
- -
- \*----------------------------------------------------------------------------*/
- YCMD:saircorrida(playerid, params[], help)
- {
- g_players_in_race--;
- g_ePlayerRaceData[playerid][isInRace] = 0;
- g_ePlayerRaceData[playerid][DistanceToFinish] =
- (2147482646.0 + float(playerid));
- DisablePlayerRaceCheckpoint(playerid);
- return 1;
- }
- /*----------------------------------------------------------------------------*\
- This command reset the race.
- Param:
- -
- \*----------------------------------------------------------------------------*/
- YCMD:resetarcorrida(playerid, params[], help)
- {
- g_players_in_race = 0;
- foreach(new i: Player)
- {
- g_ePlayerRaceData[i][isInRace] = 0;
- g_ePlayerRaceData[i][DistanceToFinish] =
- (2147482646.0 + float(playerid));
- DisablePlayerRaceCheckpoint(i);
- }
- SendClientMessageToAll(-1,
- "- Corrida reiniciada, /entrarcorrida para entrar.");
- return 1;
- }
- /*----------------------------------------------------------------------------*\
- END OF FILE - (c) L "S" G
- \*----------------------------------------------------------------------------*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement