Advertisement
Lenny

LennyRace

Jun 16th, 2011
1,328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 25.78 KB | None | 0 0
  1. /******************************************************************************\
  2. *       ,-----,                                                               *
  3. *      /----/ |                                           ,---,    ,----,     *
  4. *      |    | |       ,------, ,----,  ,----,            /---/ \  /---/ |     *
  5. *      |    | |      /-----/ |/----/ \/---/ |            \   \  \/   / /      *
  6. *      |    | |      |  ,__|/ |     \|    | |             \   \ /   / /       *
  7. *      |    | |____  |  ---,/||      \    | |              \   v   / /        *
  8. *      |    |____/ | |  ,__|/ |   ^\      | | ,----, ,----, \     / /         *
  9. *      |         | | |  ---,/||   | \     | /----/ \/---/ |  |   | |          *
  10. *      |_________|/  |_____|/ |___,/ \____,/|     \|    | |  |   | |          *
  11. *                                           |      \    | |  |   | |          *
  12. *      ,--------------------------,         |   ^\      | |  |___,/           *
  13. *     /-------------------------/ |         |   | \     | |                   *
  14. *     |/\/\/\/\/\/\/\/\/\/\/\/\/| |         |___,/ \____,/                    *
  15. *     |lenny.carlson@hotmail.com| |                                           *
  16. *     |/\/\/\/\/\/\/\/\/\/\/\/\/| |                                           *
  17. *     |_________________________,/                                            *
  18. *                                                                             *
  19. * Information about this file:                                                *
  20. *                                                                             *
  21. * Race system written by Lenny, thread here:                                  *
  22. * http://forum.sa-mp.com/showthread.php?p=1249184                             *
  23. *                                                                             *
  24. * Feel free to edit/republish but please give me credits :)                   *
  25. *                                                                             *
  26. \*****************************************************************************/
  27.  
  28.  
  29. #define MAX_RACES               60          // The maximum amount of races that can be stored
  30. #define MAX_RACE_NAME           20          // The longest name of a race that can be allowed
  31. #define MAX_RACE_CHECKPOINTS    500         // The maximum amount of checkpoints in total that can be stored
  32.  
  33. #define DIALOG_RACES            100         // The dialog for /loadrace
  34. #define DIALOG_RACES2           101         // The dialog for /loadrace
  35.  
  36. #define LR_USE_VARNAMES         0           // If you don't know what it is, keep it at 0 :)
  37.  
  38. #if LR_USE_VARNAMES == 1
  39.     #define PlayerName[%1] PlayerInfo[%1][pName]    // Here is where you define the varname if you have it!
  40. #else
  41.     #define PlayerName[%1] LR_GetName(%1)
  42. #endif
  43.  
  44. #define COLOR_RACE_GREY 0xAFAFAF00
  45. #define COLOR_RACE_GREEN 0x33AA33AA
  46. #define COLOR_RACE_RED 0xAA3333AA
  47.  
  48. #define MAX_PLAYER_NAME2 MAX_PLAYER_NAME
  49. #undef MAX_PLAYER_NAME
  50. #define MAX_PLAYER_NAME 24
  51.  
  52. forward LR_LoadRaces();
  53. forward LR_OnPlayerEnterRaceCheckpoint(playerid);
  54. forward LR_OnDialogResponse(playerid, dialogid, response, listitem);
  55.  
  56. forward LR_RaceCountdown(playerid);
  57.  
  58. enum RaceInfo
  59. {
  60.     rID,
  61.     rName[MAX_RACE_NAME],
  62.     rVehicles[41],
  63.     rFastest[MAX_PLAYER_NAME],
  64.     rRecord
  65. }
  66. new Race[MAX_RACES][RaceInfo];
  67.  
  68. enum RaceCheckPointInfo
  69. {
  70.             rcID,
  71.             rcRaceID,
  72.     Float:  rcX,
  73.     Float:  rcY,
  74.     Float:  rcZ,
  75.             rcType,
  76.     Float:  rcSize
  77. }
  78. new RaceCheckPoint[MAX_RACE_CHECKPOINTS][RaceCheckPointInfo];
  79.  
  80. public LR_LoadRaces()
  81. {
  82.     print("  LoadRaces initalized...");
  83.  
  84.     new
  85.         szString[170],
  86.         i;
  87.    
  88.     while(i < MAX_RACES) // Resetting variables
  89.     {
  90.         sscanf("-1%""%""%""%0", "p<%>e<is[" #MAX_RACE_NAME "]s[41]s[" #MAX_PLAYER_NAME "]i>", Race[i]);
  91.         i++;
  92.     }
  93.    
  94.     i = 0;
  95.    
  96.     format(szString, sizeof(szString), "SELECT `id`,`name`,`vehicles`,`fastest`,`record` FROM `races` ORDER BY `name` ASC LIMIT %d", MAX_RACES);
  97.     mysql_query(szString);
  98.     mysql_store_result();
  99.    
  100.     while(mysql_fetch_row_format(szString, "%"))
  101.     {
  102.         sscanf(szString, "p<%>e<is[" #MAX_RACE_NAME "]s[41]s[" #MAX_PLAYER_NAME "]i>", Race[i]);
  103.         i++;
  104.     }
  105.    
  106.     mysql_free_result();
  107.    
  108.     i = 0;
  109.    
  110.     while(i < MAX_RACE_CHECKPOINTS) // Resetting variables
  111.     {
  112.         sscanf("-1%-1%-1.0%-1.0%-1.0%-1%-1.0", "p<%>e<iifffif>", RaceCheckPoint[i]);
  113.         i++;
  114.     }
  115.    
  116.     i = 0;
  117.    
  118.     format(szString, sizeof(szString), "SELECT `id`,`raceid`,`x`,`y`,`z`,`type`,`size` FROM `race_checkpoints` ORDER BY `id` ASC LIMIT %d", MAX_RACE_CHECKPOINTS);
  119.     mysql_query(szString);
  120.     mysql_store_result();
  121.    
  122.     while(mysql_fetch_row_format(szString, "|"))
  123.     {
  124.         sscanf(szString, "p<|>iifffif", RaceCheckPoint[i][rcID], RaceCheckPoint[i][rcRaceID], RaceCheckPoint[i][rcX], RaceCheckPoint[i][rcY], RaceCheckPoint[i][rcZ], RaceCheckPoint[i][rcType], RaceCheckPoint[i][rcSize]);
  125.         i++;
  126.     }
  127.  
  128.     mysql_free_result();
  129. }
  130.  
  131. public LR_OnPlayerEnterRaceCheckpoint(playerid)
  132. {
  133.     if(GetPlayerVehicleSeat(playerid) != 0)
  134.     {
  135.         SendClientMessage(playerid, COLOR_RACE_RED, "You need to be driving in order to compete in a race.");
  136.         return 1;
  137.     }
  138.    
  139.     new
  140.         iRace = GetPVarInt(playerid, "Race")-1;
  141.    
  142.     if(iRace == -1)
  143.     {
  144.         SendClientMessage(playerid, COLOR_RACE_RED, "You're not in a race.");
  145.         DisablePlayerRaceCheckpoint(playerid);
  146.         return 1;
  147.     }
  148.    
  149.     new
  150.             a_iAllowedVehicles[10],
  151.             iPlayerVehicleModel = GetVehicleModel(GetPlayerVehicleID(playerid)),
  152.     bool:   VehicleIsAllowed = false;
  153.        
  154.     sscanf(Race[iRace][rVehicles], "p</>A<d>(0)[10]", a_iAllowedVehicles);
  155.    
  156.     for(new i; i < 10; i++)
  157.     {
  158.         if(a_iAllowedVehicles[i] == iPlayerVehicleModel)
  159.         {
  160.             VehicleIsAllowed = true;
  161.             break;
  162.         }
  163.     }
  164.    
  165.     if(!VehicleIsAllowed)
  166.     {
  167.         SendClientMessage(playerid, COLOR_RACE_GREY, "This vehicle isn't allowed. Type \"/racecars\" to see which vehicles are allowed for this race.");
  168.         return 1;
  169.     }
  170.    
  171.     new
  172.         iRacing = GetPVarInt(playerid, "Racing");
  173.    
  174.     if(!iRacing)
  175.     {
  176.         SendClientMessage(playerid, COLOR_RACE_GREEN, "You can now start the race with \"/ready\".");
  177.         return 1;
  178.     }
  179.    
  180.     DisablePlayerRaceCheckpoint(playerid);
  181.     PlayerPlaySound(playerid, 1139, 0, 0, 0);
  182.    
  183.     new
  184.         iRaceID = Race[iRace][rID];
  185.    
  186.     new iCPNumber = GetPVarInt(playerid, "Checkpoint");
  187.     new iCurrentCP;
  188.     new iNextCP;
  189.        
  190.     if(GetPVarInt(playerid, "FinalCheckpoint"))
  191.     {      
  192.         new
  193.             iTime = gettime() - GetPVarInt(playerid, "RaceTimer"),
  194.             szString[MAX_PLAYER_NAME + MAX_RACE_NAME + 9 + 40];
  195.        
  196.         format(szString, sizeof(szString), "%s finished the race \"%s\". His time was %s.", PlayerName[playerid], Race[iRace][rName], TimeToRaceTime(iTime));
  197.         SendClientMessageToAll(COLOR_RACE_GREEN, szString);
  198.        
  199.         format(szString, sizeof(szString), "INSERT INTO `race_times` (raceid,name,time,vehicle) VALUES (%d,'%s',%d,%d)", iRaceID, PlayerName[playerid], iTime, GetVehicleModel(GetPlayerVehicleID(playerid)));
  200.         mysql_query(szString);
  201.        
  202.         if(iTime < Race[iRace][rRecord])
  203.         {
  204.             format(szString, sizeof(szString), "%s beat the old record of %s!", PlayerName[playerid], TimeToRaceTime(Race[iRace][rRecord]));
  205.             SendClientMessageToAll(COLOR_RACE_GREEN, szString);
  206.            
  207.             format(Race[iRace][rFastest], MAX_PLAYER_NAME, PlayerName[playerid]);
  208.             Race[iRace][rRecord] = iTime;
  209.            
  210.             format(szString, sizeof(szString), "UPDATE `races` SET fastest='%s',record=%d WHERE id=%d", PlayerName[playerid], iTime, Race[iRace][rID]);
  211.             mysql_query(szString);
  212.         }
  213.  
  214.         DeletePVar(playerid, "Checkpoint");
  215.         DeletePVar(playerid, "Race");
  216.         DeletePVar(playerid, "Racing");
  217.         DeletePVar(playerid, "RaceTimer");
  218.        
  219.         return 1;
  220.     }  
  221.    
  222.     for(new i; i < MAX_RACE_CHECKPOINTS; i++)
  223.     {
  224.         if(RaceCheckPoint[i][rcRaceID] == iRaceID)
  225.         {
  226.             if(iCPNumber == 0)
  227.             {
  228.                 if(!iCurrentCP)
  229.                 {
  230.                     iCurrentCP = i;
  231.                     continue;
  232.                 }
  233.                 else
  234.                 {
  235.                     iNextCP = i;
  236.                     break;
  237.                 }
  238.             }
  239.             iCPNumber--;
  240.         }
  241.     }
  242.    
  243.     if(iNextCP)
  244.     {
  245.         SetPVarInt(playerid, "Checkpoint", GetPVarInt(playerid, "Checkpoint") + 1);
  246.        
  247.         SetPlayerRaceCheckpoint(playerid, RaceCheckPoint[iCurrentCP][rcType], RaceCheckPoint[iCurrentCP][rcX], RaceCheckPoint[iCurrentCP][rcY], RaceCheckPoint[iCurrentCP][rcZ], RaceCheckPoint[iNextCP][rcX], RaceCheckPoint[iNextCP][rcY], RaceCheckPoint[iNextCP][rcZ], RaceCheckPoint[iCurrentCP][rcSize]);
  248.     }
  249.    
  250.     else
  251.     {
  252.         SetPVarInt(playerid, "FinalCheckpoint", 1);
  253.        
  254.         DeletePVar(playerid, "Checkpoint");
  255.         if(RaceCheckPoint[iCurrentCP][rcType] < 3)
  256.             SetPlayerRaceCheckpoint(playerid, 1, RaceCheckPoint[iCurrentCP][rcX], RaceCheckPoint[iCurrentCP][rcY], RaceCheckPoint[iCurrentCP][rcZ], 0, 0, 0, RaceCheckPoint[iCurrentCP][rcSize]);
  257.         else
  258.             SetPlayerRaceCheckpoint(playerid, 4, RaceCheckPoint[iCurrentCP][rcX], RaceCheckPoint[iCurrentCP][rcY], RaceCheckPoint[iCurrentCP][rcZ], 0, 0, 0, RaceCheckPoint[iCurrentCP][rcSize]);
  259.     }
  260.  
  261.     return 1;
  262. }
  263.  
  264. public LR_OnDialogResponse(playerid, dialogid, response, listitem)
  265. {
  266.     if(dialogid != DIALOG_RACES && dialogid != DIALOG_RACES2)
  267.     {
  268.         return 1;
  269.     }
  270.    
  271.     if(!response)
  272.     {
  273.         return 1;
  274.     }
  275.    
  276.     if(dialogid == DIALOG_RACES)
  277.     {
  278.         foreach(Player, i)
  279.         {
  280.             DisablePlayerRaceCheckpoint(i);
  281.             SetPVarInt(i, "Race", listitem+1);
  282.             DeletePVar(i, "Racing");
  283.             DeletePVar(i, "RaceTimer");
  284.             DeletePVar(i, "FinalCheckpoint");
  285.         }
  286.     }
  287.     else
  288.     {
  289.         DisablePlayerRaceCheckpoint(playerid);
  290.         SetPVarInt(playerid, "Race", listitem+1);
  291.         DeletePVar(playerid, "Racing");
  292.         DeletePVar(playerid, "RaceTimer");
  293.         DeletePVar(playerid, "FinalCheckpoint");
  294.     }
  295.    
  296.     #define SendRaceMessage(%1);                        \
  297.     if(dialogid == DIALOG_RACES)                        \
  298.         SendClientMessageToAll(COLOR_RACE_GREEN, %1);       \
  299.     else                                                \
  300.         SendClientMessage(playerid, COLOR_RACE_GREEN, %1);
  301.    
  302.     new
  303.         szString[128],
  304.         a_iAllowedVehicles[10],
  305.         szVehicleName[20];
  306.        
  307.     format(szString, sizeof(szString), "[RACE] %s loaded the race \"%s\".", PlayerName[playerid], Race[listitem][rName]);
  308.     SendRaceMessage(szString);
  309.    
  310.     sscanf(Race[listitem][rVehicles], "p</>A<d>(0)[10]", a_iAllowedVehicles);
  311.                
  312.     for(new i; i < 10; i++)
  313.     {
  314.         if(!a_iAllowedVehicles[i])
  315.             break;
  316.         GetVehicleName(a_iAllowedVehicles[i], szVehicleName, sizeof(szVehicleName));
  317.        
  318.         if(i == 0)
  319.             format(szString, sizeof(szString), "Allowed vehicle(s): %s", szVehicleName);
  320.         else if(i == 5)
  321.         {
  322.             SendRaceMessage(szString);
  323.             format(szString, sizeof(szString), "Additional vehicle(s): %s", szVehicleName);
  324.         }
  325.         else
  326.             format(szString, sizeof(szString), "%s, %s", szString, szVehicleName);
  327.     }
  328.     SendRaceMessage(szString);
  329.    
  330.     format(szString, sizeof(szString), "Current recordholder and time: %s - %s", Race[listitem][rFastest], TimeToRaceTime(Race[listitem][rRecord]));
  331.     SendRaceMessage(szString);
  332.    
  333.     SendRaceMessage("Type \"/join\" to join the race.");
  334.    
  335.     #undef SendRaceMessage
  336.    
  337.     return 1;
  338. }
  339.  
  340. stock LR_GetName(playerid)
  341. {
  342.     new
  343.         szName[MAX_PLAYER_NAME];
  344.    
  345.     GetPlayerName(playerid, szName, MAX_PLAYER_NAME);
  346.    
  347.     return szName;
  348. }
  349.  
  350. stock IsVehicleAllowedInRace(model, raceid)
  351. {
  352.     new
  353.         a_iAllowedVehicles[10];
  354.    
  355.     sscanf(Race[raceid][rVehicles], "p</>A<d>(0)[10]", a_iAllowedVehicles);
  356.    
  357.     for(new i; a_iAllowedVehicles[i]; i++)
  358.     {
  359.         if(a_iAllowedVehicles[i] == model)
  360.             return 1;
  361.     }
  362.    
  363.     return 0;
  364. }
  365.  
  366. stock GetVehicleName(modelid,name[],namesize)
  367. {
  368.     new VehicleNames[][] =
  369.     {
  370.         "Landstalker","Bravura","Buffalo","Linerunner","Pereniel","Sentinel","Dumper","Firetruck","Trashmaster","Stretch","Manana","Infernus","Voodoo","Pony","Mule","Cheetah","Ambulance","Leviathan","Moonbeam","Esperanto",
  371.         "Taxi","Washington","Bobcat","Mr Whoopee","BF Injection","Hunter","Premier","Enforcer","Securicar","Banshee","Predator","Bus","Rhino","Barracks","Hotknife","Trailer","Previon","Coach","Cabbie","Stallion",
  372.         "Rumpo","RC Bandit","Romero","Packer","Monster","Admiral","Squalo","Seasparrow","Pizzaboy","Tram","Trailer","Turismo","Speeder","Reefer","Tropic","Flatbed","Yankee","Caddy","Solair","Berkley's RC Van",
  373.         "Skimmer","PCJ-600","Faggio","Freeway","RC Baron","RC Raider","Glendale","Oceanic","Sanchez","Sparrow","Patriot","Quad","Coastguard","Dinghy","Hermes","Sabre","Rustler","ZR3 50","Walton","Regina",
  374.         "Comet","BMX","Burrito","Camper","Marquis","Baggage","Dozer","Maverick","News Chopper","Rancher","FBI Rancher","Virgo","Greenwood","Jetmax","Hotring","Sandking","Blista Compact","Police Maverick","Boxville","Benson",
  375.         "Mesa","RC Goblin","Hotring Racer","Hotring Racer","Bloodring Banger","Rancher","Super GT","Elegant","Journey","Bike","Mountain Bike","Beagle","Cropdust","Stunt","Tanker","RoadTrain","Nebula","Majestic","Buccaneer","Shamal",
  376.         "Hydra","FCR-900","NRG-500","HPV1000","Cement Truck","Tow Truck","Fortune","Cadrona","FBI Truck","Willard","Forklift","Tractor","Combine","Feltzer","Remington","Slamvan","Blade","Freight","Streak","Vortex",
  377.         "Vincent","Bullet","Clover","Sadler","Firetruck","Hustler","Intruder","Primo","Cargobob","Tampa","Sunrise","Merit","Utility","Nevada","Yosemite","Windsor","Monster","Monster","Uranus","Jester",
  378.         "Sultan","Stratum","Elegy","Raindance","RC Tiger","Flash","Tahoma","Savanna","Bandito","Freight","Trailer","Kart","Mower","Duneride","Sweeper","Broadway","Tornado","AT-400","DFT-30","Huntley",
  379.         "Stafford","BF-400","Newsvan","Tug","Trailer","Emperor","Wayfarer","Euros","Hotdog","Club","Trailer","Trailer","Andromada","Dodo","RC Cam","Launch","Police Car (LSPD)","Police Car (SFPD)","Police Car (LVPD)","Police Ranger",
  380.         "Picador","S.W.A.T. Van","Alpha","Phoenix","Glendale","Sadler","Luggage Trailer","Luggage Trailer","Stair Trailer","Boxville","Farm Plow","Utility Trailer"
  381.     };
  382.     if(modelid < 400 || modelid > 611) return 0;
  383.     modelid -= 400;
  384.     for(new i = 0, len = strlen(VehicleNames[modelid]); i < len && i < namesize; i++)
  385.         name[i] = VehicleNames[modelid][i];
  386.     name[min(namesize, strlen(VehicleNames[modelid]))] = '\0';
  387.     return 1;
  388. }
  389.  
  390. stock TimeToRaceTime(iTime)
  391. {
  392.     new
  393.         szTime[9],
  394.         a_iTime[3];
  395.    
  396.     a_iTime[0] = iTime % 60; // Seconds
  397.     iTime /= 60;
  398.     a_iTime[1] = iTime % 60; // Minutes
  399.     iTime /= 60;
  400.     a_iTime[2] = iTime % 24; // Hours
  401.    
  402.     format(szTime, sizeof(szTime), "%02d:%02d:%02d", a_iTime[2], a_iTime[1], a_iTime[0]);
  403.    
  404.     return szTime;
  405. }
  406.  
  407. stock Freeze(playerid, op)
  408.     TogglePlayerControllable(playerid, 1 - op);
  409.  
  410. stock freeze(playerid,option)
  411. {
  412.     if(option)
  413.     {
  414.         SetPVarInt(playerid, "Frozen", GetPVarInt(playerid, "Frozen") + 1);
  415.         Freeze(playerid, 1);
  416.     }
  417.     else
  418.         SetPVarInt(playerid, "Frozen", GetPVarInt(playerid, "Frozen") - 1);
  419.        
  420.     if(GetPVarInt(playerid, "Frozen") < 1)
  421.     {
  422.         DeletePVar(playerid, "Frozen"); // In case of some bug that people will have minus
  423.         Freeze(playerid, 0);
  424.     }
  425. }
  426.  
  427. public LR_RaceCountdown(playerid)
  428. {
  429.     new
  430.         iCount = GetPVarInt(playerid, "RaceCountdown");
  431.        
  432.     SetVehicleVelocity(GetPlayerVehicleID(playerid), 0.0, 0.0, 0.0);
  433.     if(!iCount)
  434.     {
  435.         SetTimerEx("OnPlayerEnterRaceCheckpoint",0,0,"d",playerid);
  436.         DeletePVar(playerid, "RaceCountdown");
  437.         GameTextForPlayer(playerid, "~g~Go!", 2000, 3);
  438.         freeze(playerid, false);
  439.         SetPVarInt(playerid, "RaceTimer", gettime());
  440.         KillTimer(GetPVarInt(playerid, "RaceCountdownID"));
  441.     }
  442.     else
  443.     {
  444.         new
  445.                 szString[5],
  446.         Float:  fPos[3];
  447.        
  448.         GetPlayerPos(playerid, fPos[0], fPos[1], fPos[2]);
  449.         switch(iCount)
  450.         {
  451.             case 1:
  452.             {
  453.                 SetCameraBehindPlayer(playerid);
  454.                 format(szString, sizeof(szString), "~r~%d", iCount);
  455.             }
  456.             case 2:
  457.             {
  458.                 SetPlayerCameraPos(playerid, fPos[0], fPos[1] - 10, fPos[2] + 5);
  459.                 SetPlayerCameraLookAt(playerid, fPos[0], fPos[1] + 10, fPos[2]);
  460.                 format(szString, sizeof(szString), "~g~%d", iCount);
  461.             }
  462.             case 3:
  463.             {
  464.                 SetPlayerCameraPos(playerid, fPos[0] - 10, fPos[1], fPos[2] + 5);
  465.                 SetPlayerCameraLookAt(playerid, fPos[0] + 10, fPos[1], fPos[2]);
  466.                 format(szString, sizeof(szString), "~y~%d", iCount);
  467.             }
  468.             case 4:
  469.             {
  470.                 SetPlayerCameraPos(playerid, fPos[0], fPos[1], fPos[2] + 5);
  471.                 SetPlayerCameraLookAt(playerid, fPos[0], fPos[1], fPos[2] - 10);
  472.                 format(szString, sizeof(szString), "~b~%d", iCount);
  473.             }
  474.             default:
  475.             {
  476.                 SetPlayerCameraLookAt(playerid, fPos[0], fPos[1], fPos[2]);
  477.                 format(szString, sizeof(szString), "~r~%d", iCount);
  478.             }
  479.         }
  480.         GameTextForPlayer(playerid, szString, 2000, 3);
  481.         SetPVarInt(playerid, "RaceCountdown", iCount-1);
  482.     }
  483. }
  484.  
  485. // COMMANDS
  486.  
  487. stock LR_CMD_ADDRACE(playerid, params[])
  488. {
  489.     new
  490.         szRaceName[MAX_RACE_NAME],
  491.         szString[191],
  492.         szRaceVehicles[41],
  493.         a_iVehicleID[10];
  494.    
  495.     if(sscanf(params, "s[" #MAX_RACE_NAME "]A<i>(-1)[10]", szRaceName, a_iVehicleID))
  496.     {
  497.         SendClientMessage(playerid, COLOR_RACE_GREY, "USAGE: /createrace [Racename] [Vehiclemodel ID] (Up to 9 additional Vehiclemodel IDs)");
  498.         return 1;
  499.     }
  500.    
  501.     if(a_iVehicleID[0] == -1)
  502.     {
  503.         SendClientMessage(playerid, COLOR_RACE_GREY, "You need to provide at least one vehicle ID.");
  504.         return 1;
  505.     }
  506.    
  507.     if(!IsValidvehicleModel(a_iVehicleID[0]))
  508.     {
  509.         format(szString, sizeof(szString), "%d is not a valid vehicle model.", a_iVehicleID[0]);
  510.         SendClientMessage(playerid, COLOR_RACE_GREY, szString);
  511.         return 1;
  512.     }
  513.    
  514.     format(szRaceVehicles, sizeof(szRaceVehicles), "%d", a_iVehicleID[0]);
  515.    
  516.     for(new i = 1; a_iVehicleID[i] && i < 10; i++)
  517.     {
  518.         if(a_iVehicleID[i] == -1)
  519.             break;
  520.            
  521.         if(!IsValidvehicleModel(a_iVehicleID[i]))
  522.         {
  523.             format(szString, sizeof(szString), "%d is not a valid vehicle model.", a_iVehicleID[i]);
  524.             SendClientMessage(playerid, COLOR_RACE_GREY, szString);
  525.             return 1;
  526.         }
  527.         format(szRaceVehicles, sizeof(szRaceVehicles), "%s/%d", szRaceVehicles, a_iVehicleID[i]);
  528.     }
  529.    
  530.     mysql_real_escape_string(szRaceName, szRaceName);
  531.    
  532.     format(szString, sizeof(szString), "SELECT `id` FROM `races` WHERE `name`='%s' LIMIT 1", szRaceName);
  533.    
  534.     mysql_query(szString);
  535.     mysql_store_result();
  536.    
  537.     if(mysql_num_rows())
  538.     {
  539.         mysql_free_result();
  540.         SendClientMessage(playerid, COLOR_RACE_GREY, "This race already exists. Try a different name.");
  541.         return 1;
  542.     }
  543.     mysql_free_result();
  544.  
  545.     mysql_real_escape_string(szRaceVehicles, szRaceVehicles);
  546.    
  547.     format(szString, sizeof(szString), "INSERT INTO `races` (`name`,`vehicles`,`creator`,`fastest`,`record`) VALUES ('%s','%s','%s','Noone','360000')", szRaceName, szRaceVehicles, PlayerName[playerid]);
  548.     mysql_query(szString);
  549.    
  550.     LR_LoadRaces();
  551.    
  552.     format(szString, sizeof(szString), "You have successfully created the race \"%s\".", szRaceName);
  553.     SendClientMessage(playerid, COLOR_RACE_GREEN, szString);
  554.    
  555.     return 1;
  556. }
  557.  
  558. stock LR_CMD_ADDCHECKPOINT(playerid, params[])
  559. {
  560.     new
  561.             szRaceName[MAX_RACE_NAME],
  562.             iRaceID,
  563.             iCheckType,
  564.     Float:  fSize,
  565.             szString[150],
  566.             iCPID = 1,
  567.     Float:  fPos[3];
  568.    
  569.     if(sscanf(params, "s[" #MAX_RACE_NAME "]I(0)F(5.0)", szRaceName, iCheckType, fSize))
  570.     {
  571.         SendClientMessage(playerid, COLOR_RACE_GREY, "USAGE: /a(dd)c(heckpoint) [Racename] (Checkpoint type) (Size)");
  572.         SendClientMessage(playerid, COLOR_RACE_GREY, "TYPES: 0-Normal, 1-Finish, 2-Nothing, 3-Air normal, 4-Air finish");
  573.         return 1;
  574.     }
  575.    
  576.     mysql_real_escape_string(szRaceName, szRaceName);
  577.  
  578.     while(iRaceID < MAX_RACES)
  579.     {
  580.         if(Race[iRaceID][rID] == -1)
  581.         {
  582.             iRaceID = MAX_RACES - 1;
  583.             break;
  584.         }
  585.        
  586.         if(!strcmp(szRaceName, Race[iRaceID][rName]))
  587.         {
  588.             break;
  589.         }      
  590.        
  591.         iRaceID++;
  592.     }
  593.    
  594.     if(iRaceID == MAX_RACES - 1)
  595.     {
  596.         SendClientMessage(playerid, COLOR_RACE_GREY, "This race doesn't exist.");
  597.         return 1;
  598.     }
  599.    
  600.     while(iCPID < MAX_RACE_CHECKPOINTS)
  601.     {
  602.         if(RaceCheckPoint[iCPID][rcID] == -1)
  603.         {
  604.             break;
  605.         }
  606.         iCPID++;
  607.     }
  608.    
  609.     if(iCPID == MAX_RACE_CHECKPOINTS)
  610.     {
  611.         SendClientMessage(playerid, COLOR_RACE_RED, "Maxmimum number of checkpoints allowed on the server reached. Contact a developer.");
  612.         return 1;
  613.     }
  614.    
  615.     GetPlayerPos(playerid, fPos[0], fPos[1], fPos[2]);
  616.  
  617.     format(szString, sizeof(szString), "INSERT INTO `race_checkpoints` (`raceid`,`x`,`y`,`z`,`type`,`size`) VALUES (%d, '%f', '%f', '%f', %d, '%f')", Race[iRaceID][rID], fPos[0], fPos[1], fPos[2], iCheckType, fSize);
  618.     mysql_query(szString);
  619.    
  620.     format(szString, sizeof(szString), "%d>%d>%f>%f>%f>%d>%f", mysql_insert_id(), iRaceID, fPos[0], fPos[1], fPos[2], iCheckType, fSize);
  621.  
  622.     sscanf(szString, "p<>>e<iifffif>", RaceCheckPoint[iCPID]);
  623.    
  624.     sscanf("Reset, race modified%600000", "p<%>s[" #MAX_PLAYER_NAME "]i", Race[iRaceID][rFastest], Race[iRaceID][rRecord]);
  625.    
  626.     format(szString, sizeof(szString), "UPDATE `races` SET `fastest`='%s', `record`='%s' WHERE `id`='%d'", Race[iRaceID][rFastest], Race[iRaceID][rRecord], Race[iRaceID][rID]);
  627.     mysql_query(szString);
  628.    
  629.     LR_LoadRaces();
  630.    
  631.     format(szString, sizeof(szString), "You have created a checkpoint in the race \"%s\".", szRaceName);
  632.     SendClientMessage(playerid, COLOR_RACE_GREY, szString);
  633.    
  634.     return 1;
  635. }
  636.  
  637. stock LR_CMD_LOADRACEFORALL(playerid)
  638. {
  639.     new
  640.         szDialogString[MAX_RACES*(MAX_RACE_NAME+2)];
  641.    
  642.     for(new i; i < MAX_RACES; i++)
  643.     {
  644.         if(Race[i][rID] == -1)
  645.             break;
  646.         format(szDialogString, sizeof(szDialogString), "%s\n%s", szDialogString, Race[i][rName]);
  647.     }
  648.    
  649.     if(isnull(szDialogString))
  650.     {
  651.         SendClientMessage(playerid, COLOR_RACE_GREY, "There aren't any races yet.");
  652.         return 1;
  653.     }
  654.    
  655.     ShowPlayerDialog(playerid, DIALOG_RACES, DIALOG_STYLE_LIST, "Race list", szDialogString, "Load", "Quit");
  656.  
  657.     return 1;
  658. }
  659.  
  660. stock LR_CMD_LOADRACEFORPLAYER(playerid)
  661. {
  662.     new
  663.         szDialogString[MAX_RACES*(MAX_RACE_NAME+2)];
  664.    
  665.     for(new i; i < MAX_RACES && Race[i][rID] != -1; i++)
  666.     {
  667.         format(szDialogString, sizeof(szDialogString), "%s\n%s", szDialogString, Race[i][rName]);
  668.     }
  669.    
  670.     if(isnull(szDialogString))
  671.     {
  672.         SendClientMessage(playerid, COLOR_RACE_GREY, "There aren't any race yet.");
  673.         return 1;
  674.     }
  675.    
  676.     ShowPlayerDialog(playerid, DIALOG_RACES2, DIALOG_STYLE_LIST, "Races list", szDialogString, "Load", "Quit");
  677.  
  678.     return 1;
  679. }
  680.  
  681. stock LR_CMD_JOINRACE(playerid)
  682. {
  683.     new
  684.         iRaceID,
  685.         iCurrentCP = -1,
  686.         iNextCP;
  687.        
  688.     if(!GetPVarInt(playerid, "Race"))
  689.     {
  690.         SendClientMessage(playerid, COLOR_RACE_GREY, "No race has been loaded.");
  691.         return 1;
  692.     }
  693.    
  694.     DeletePVar(playerid, "Checkpoint");
  695.    
  696.     iRaceID = Race[GetPVarInt(playerid, "Race") - 1][rID];
  697.    
  698.     while(iNextCP < MAX_RACE_CHECKPOINTS)
  699.     {
  700.         if(RaceCheckPoint[iNextCP][rcRaceID] == iRaceID)
  701.         {
  702.             if(iCurrentCP == -1)
  703.             {
  704.                 iCurrentCP = iNextCP;
  705.             }
  706.             else
  707.             {
  708.                 break;
  709.             }
  710.         }
  711.         iNextCP++;
  712.     }
  713.    
  714.     if(iNextCP == MAX_RACE_CHECKPOINTS)
  715.     {
  716.         SendClientMessage(playerid, COLOR_RACE_GREY, "This race doesn't have more than one checkpoint yet.");
  717.         return 1;
  718.     }
  719.    
  720.     new
  721.     Float:  fPos[3];
  722.    
  723.     GetPlayerPos(playerid, fPos[0], fPos[1], fPos[2]);
  724.    
  725.     if(IsPlayerInAnyVehicle(playerid))
  726.     {
  727.         SetVehiclePos(GetPlayerVehicleID(playerid), RaceCheckPoint[iCurrentCP][rcX], RaceCheckPoint[iCurrentCP][rcY], RaceCheckPoint[iCurrentCP][rcZ]);
  728.     }
  729.     else
  730.     {
  731.         SetPlayerPos(playerid, RaceCheckPoint[iCurrentCP][rcX], RaceCheckPoint[iCurrentCP][rcY], RaceCheckPoint[iCurrentCP][rcZ]);
  732.     }
  733.    
  734.     SetPlayerRaceCheckpoint(playerid, RaceCheckPoint[iCurrentCP][rcType], RaceCheckPoint[iCurrentCP][rcX], RaceCheckPoint[iCurrentCP][rcY], RaceCheckPoint[iCurrentCP][rcZ], RaceCheckPoint[iNextCP][rcX], RaceCheckPoint[iNextCP][rcY], RaceCheckPoint[iNextCP][rcZ], RaceCheckPoint[iCurrentCP][rcSize]);
  735.    
  736.     return 1;
  737. }
  738.  
  739. stock LR_CMD_LEAVERACE(playerid)
  740. {
  741.     if(!GetPVarInt(playerid, "Racing"))
  742.     {
  743.         SendClientMessage(playerid, COLOR_RACE_GREY, "You are not racing.");
  744.         return 1;
  745.     }
  746.    
  747.     DeletePVar(playerid, "Checkpoint");
  748.     DeletePVar(playerid, "Racing");
  749.     DeletePVar(playerid, "RaceTimer");
  750.    
  751.     DisablePlayerRaceCheckpoint(playerid);
  752.    
  753.     SendClientMessage(playerid, COLOR_RACE_GREEN, "You have left the race.");
  754.    
  755.     return 1;
  756. }
  757.  
  758. stock LR_CMD_READY(playerid)
  759. {
  760.     if(!GetPVarInt(playerid, "Race"))
  761.     {
  762.         SendClientMessage(playerid, COLOR_RACE_GREY, "No race has been loaded.");
  763.         return 1;
  764.     }
  765.    
  766.     if(GetPVarInt(playerid, "Racing"))
  767.     {
  768.         SendClientMessage(playerid, COLOR_RACE_GREY, "You are already racing.");
  769.         return 1;
  770.     }
  771.    
  772.     if(!IsPlayerInRaceCheckpoint(playerid))
  773.     {
  774.         SendClientMessage(playerid, COLOR_RACE_GREY, "You are not inside the race checkpoint.");
  775.         return 1;
  776.     }
  777.    
  778.     if(!IsVehicleAllowedInRace(GetVehicleModel(GetPlayerVehicleID(playerid)), GetPVarInt(playerid, "Race") - 1))
  779.     {
  780.         SendClientMessage(playerid, COLOR_RACE_GREY, "This vehicle is not allowed. To show which vehicles are allowed, type \"/racecars\".");
  781.         return 1;
  782.     }
  783.    
  784.     SetPVarInt(playerid, "Racing", 1);
  785.     SetPVarInt(playerid, "RaceCountdown", 5);
  786.     SetPVarInt(playerid, "RaceCountdownID", SetTimerEx("LR_RaceCountdown", 1000, 1, "d", playerid));
  787.     freeze(playerid, true);
  788.     SetVehicleVelocity(GetPlayerVehicleID(playerid), 0.0, 0.0, 0.0);
  789.    
  790.     return 1;
  791. }
  792.  
  793. stock LR_CMD_READYALL()
  794. {
  795.     foreach(Player, i)
  796.     {
  797.         if(!GetPVarInt(i, "Race") || GetPVarInt(i, "Racing") || !IsPlayerInRaceCheckpoint(i) || !IsVehicleAllowedInRace(GetVehicleModel(GetPlayerVehicleID(i)), GetPVarInt(i, "Race") - 1))
  798.         {
  799.             continue;
  800.         }
  801.        
  802.         SetPVarInt(i, "Racing", 1);
  803.         SetPVarInt(i, "RaceCountdown", 5);
  804.         SetPVarInt(i, "RaceCountdownID", SetTimerEx("LR_RaceCountdown", 1000, 1, "d", i));
  805.         freeze(i, true);
  806.         SetVehicleVelocity(GetPlayerVehicleID(i), 0.0, 0.0, 0.0);
  807.        
  808.     }
  809.    
  810.     return 1;
  811. }
  812.  
  813. stock LR_CMD_RACECARS(playerid)
  814. {
  815.     new
  816.         iRaceID = GetPVarInt(playerid, "Race") - 1,
  817.         a_iAllowedVehicles[10],
  818.         szString[128],
  819.         szVehicleName[20];
  820.    
  821.     if(iRaceID == -1)
  822.     {
  823.         SendClientMessage(playerid, COLOR_RACE_GREY, "No race has been loaded.");
  824.         return 1;
  825.     }
  826.    
  827.     sscanf(Race[iRaceID][rVehicles], "p</>A<d>(0)[10]", a_iAllowedVehicles);
  828.                            
  829.     for(new i; i < 10; i++)
  830.     {
  831.         if(!a_iAllowedVehicles[i])
  832.             break;
  833.  
  834.         GetVehicleName(a_iAllowedVehicles[i], szVehicleName, sizeof(szVehicleName));
  835.        
  836.         if(i == 0)
  837.             format(szString, sizeof(szString), "Allowed vehicle(s): %s", szVehicleName);
  838.         else if(i == 5)
  839.         {
  840.             SendClientMessage(playerid, COLOR_RACE_GREY, szString);
  841.             format(szString, sizeof(szString), "Additional vehicle(s): %s", szVehicleName);
  842.         }
  843.         else
  844.             format(szString, sizeof(szString), "%s, %s", szString, szVehicleName);
  845.     }
  846.    
  847.     SendClientMessage(playerid, COLOR_RACE_GREY, szString);
  848.     return 1;
  849. }
  850.  
  851. #undef MAX_PLAYER_NAME
  852. #define MAX_PLAYER_NAME MAX_PLAYER_NAME2
  853. #undef MAX_PLAYER_NAME2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement