Advertisement
ZumeZero

zRaces INC beta

Jan 23rd, 2015
499
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 15.84 KB | None | 0 0
  1. #if defined _zRace
  2.     #endinput
  3. #endif
  4. #define _zRace
  5.  
  6. #if !defined mysql_connect
  7.     #error "Es necesario para que zRace funcione que sea agregado el include MySQL"
  8. #endif
  9.  
  10. #define MAX_RACES 20
  11. #define MAX_STEPS 50
  12.  
  13. #define MESSAGE_PRINTABLE (true)
  14. #define MESSAGE_NO_PRINTABLE (false)
  15.  
  16. #define INVALID_RACE_ID (-1)
  17. enum RacePlayerEN{
  18.     pCount,
  19.     pRace,
  20. };
  21. enum RaceEnum{
  22.     rID,
  23.     rName,
  24.     rExist,
  25.     rStart,
  26.     Float:rPos[4],
  27.     rVW,
  28.     rINT
  29. };
  30. enum ARRAY_TRACK_RACEs{
  31.     rID,
  32.     rExist,
  33.     rOwner,
  34.     Float:rPos[4]
  35. };
  36.  
  37. new ARRAY_TRACK_RACE[MAX_RACES][MAX_STEPS][ARRAY_TRACK_RACEs],
  38.     n@dRace[MAX_RACES][RaceEnum],
  39.     DataRace[MAX_PLAYERS][RacePlayerEN],
  40.     gRaceConections;
  41.  
  42. #define RaceExist(%1) (0 <= (%1) < MAX_RACES && _Valid@Race((%1)))
  43.  
  44. #define rDATA_SV    "localhost"
  45. #define rDATA_USER  "root"
  46. #define rDATA_SQL   "x367"
  47. #define rDATA_PASS  "holamundo12345"
  48.  
  49. stock Float:cache_get_field_floatr(row, const field_name[])
  50. {
  51.     new
  52.         str[16];
  53.  
  54.     cache_get_field_content(row, field_name, str, gRaceConections);
  55.     return floatstr(str);
  56. }
  57.  
  58. stock sql@ConnectRace(){
  59.         gRaceConections = mysql_connect(rDATA_SV,
  60.                                         rDATA_USER,
  61.                                         rDATA_SQL,
  62.                                         rDATA_PASS);
  63. }
  64.  
  65. cache_get_field_intr(row, const field_name[]){
  66.     new
  67.         str[12];
  68.  
  69.     cache_get_field_content(row, field_name, str, gRaceConections);
  70.     return strval(str);
  71. }
  72.  
  73. public OnFilterScriptInit()
  74. {
  75.     c@_InitRace();
  76.  
  77.         #if defined RX_OnFilterScriptInit
  78.             return RX_OnFilterScriptInit();
  79.         #else
  80.                 return 1;
  81.         #endif
  82. }
  83.  
  84. public OnGameModeInit()
  85. {
  86.     c@_InitRace();
  87.  
  88.     #if defined RX_OnGameModeInit
  89.        return RX_OnGameModeInit();
  90.     #else
  91.        return 1;
  92.     #endif
  93. }
  94.  
  95. public OnPlayerEnterRaceCheckpoint(playerid)
  96. {
  97.     c@EnterPlayerCP(playerid);
  98.  
  99.     #if defined RX_OnPlayerEnterRaceCheckpoint
  100.         return RX_OnPlayerEnterRaceCheckpoint(playerid);
  101.     #else
  102.         return 1;
  103.     #endif
  104. }
  105.  
  106. stock GetTrackRestants(playerid, raceid)
  107. {
  108.     new count;
  109.     if(RaceExist(raceid))
  110.     {
  111.         for(new i = GetPlayerStep(playerid); i != MAX_STEPS; i ++)
  112.         {
  113.             if(TrackExist(raceid, i) && ARRAY_TRACK_RACE[raceid][i][rOwner] == n@dRace[raceid][rID])
  114.             {
  115.                 count ++;
  116.             }
  117.         }
  118.     }
  119.     return (count-1);
  120. }
  121.  
  122. stock GetNextFinish(playerid){
  123.     new raceid = GetPlayerRace(playerid);
  124.     if(!RaceExist(raceid))
  125.         return 0;
  126.  
  127.     new
  128.         count,
  129.         total = GetPlayerStep(playerid);
  130.  
  131.     for(new i; i != MAX_STEPS; i ++)
  132.     {
  133.         if(ARRAY_TRACK_RACE[raceid][i][rExist] && ARRAY_TRACK_RACE[raceid][i][rOwner] == n@dRace[raceid][rID])
  134.         {
  135.             if(count > total)
  136.             {
  137.                 return i;
  138.             }
  139.             count ++;
  140.         }
  141.     }
  142.     return 0;
  143. }
  144.  
  145. stock c@EnterPlayerCP(playerid)
  146. {  
  147.     new
  148.         raceid = GetPlayerRace(playerid),
  149.         step = GetPlayerStep(playerid),
  150.         left = GetTrackRestants(playerid, raceid),
  151.         next = GetNextFinish(playerid);
  152.     if(left < 1)
  153.     {
  154.         CallLocalFunction("OnPlayerFinishRace", "d", playerid);
  155.         DataRace[playerid][pCount] = 0;
  156.     }
  157.     else
  158.     {
  159.         DataRace[playerid][pCount]++;
  160.         PlayerPlaySound(playerid, 1057, 0.0, 0.0, 0.0);
  161.         CallLocalFunction("OnPlayerTrackRace", "dddd", playerid, raceid, left, left);
  162.         SetRaceCPEx(playerid, raceid, ((step > 0) ? (step+1) : (step)), next);
  163.     }
  164. }
  165.  
  166. forward OnPlayerFinishRace(playerid, raceid);
  167.  
  168. forward OnPlayerTrackRace(playerid, raceid, steps, left);
  169.  
  170. #if defined _ALS_OnPlayerEnterRaceCP
  171.         #undef OnPlayerEnterRaceCheckpoint
  172. #else
  173.         #define _ALS_OnPlayerEnterRaceCP
  174. #endif
  175.  
  176. #define OnPlayerEnterRaceCheckpoint     RX_OnPlayerEnterRaceCheckpoint
  177.  
  178. #if defined RX_OnPlayerEnterRaceCheckpoint
  179.         forward RX_OnPlayerEnterRaceCheckpoint(playerid);
  180. #endif
  181.  
  182. #if defined _ALS_OnFilterScriptInit
  183.         #undef OnFilterScriptInit
  184. #else
  185.         #define _ALS_OnFilterScriptInit
  186. #endif
  187.  
  188. #if defined _ALS_OnGameModeInit
  189.         #undef OnGameModeInit
  190. #else
  191.         #define _ALS_OnGameModeInit
  192. #endif
  193.  
  194. #define OnFilterScriptInit      RX_OnFilterScriptInit
  195. #define OnGameModeInit          RX_OnGameModeInit
  196.  
  197. #if defined RX_OnFilterScriptInit
  198.         forward RX_OnFilterScriptInit();
  199. #endif
  200.  
  201. #if defined RX_OnGameModeInit
  202.         forward RX_OnGameModeInit();
  203. #endif
  204.  
  205. stock static c@_InitRace()
  206. {
  207.     sql@ConnectRace();
  208.  
  209.     if(mysql_errno(gRaceConections) != 0)
  210.         return print("DATABASE 'RACES' no conected");
  211.  
  212.     mysql_function_query(gRaceConections, "SELECT * FROM `races`", true, "sql@LoadRace_", "");
  213.     return 1;
  214. }
  215.  
  216. forward sql@LoadRace_(); public sql@LoadRace_()
  217. {
  218.     if(mysql_errno(gRaceConections) != 0)
  219.         return 0;
  220.  
  221.     new rows, fields;
  222.     cache_get_data(rows, fields, gRaceConections);
  223.     for (new i = 0; i < rows; i ++) if (i < MAX_RACES)
  224.     {
  225.         n@dRace[i][rExist] = true;
  226.         n@dRace[i][rID] = cache_get_field_intr(i, "id");
  227.         n@dRace[i][rINT] = cache_get_field_intr(i, "int");
  228.         n@dRace[i][rVW] = cache_get_field_intr(i, "vw");
  229.         n@dRace[i][rPos][0] = cache_get_field_floatr(i, "pos0");
  230.         n@dRace[i][rPos][1] = cache_get_field_floatr(i, "pos1");
  231.         n@dRace[i][rPos][2] = cache_get_field_floatr(i, "pos2");
  232.         n@dRace[i][rPos][3] = cache_get_field_floatr(i, "pos3");
  233.         cache_get_field_content(i, "name", n@dRace[i][rName], gRaceConections);
  234.     }
  235.     new
  236.         str[64];
  237.     for (new i = 0; i < MAX_RACES; i ++) if (n@dRace[i][rExist])
  238.     {
  239.         format(str, sizeof(str), "SELECT * FROM `tracks` WHERE `raceowner` = '%d'", n@dRace[i][rID]);
  240.         mysql_function_query(gRaceConections, str, true, "sql@LoadTracks", "d", i);
  241.     }
  242.     return 1;
  243. }
  244.  
  245. forward sql@LoadTracks(track);
  246. public sql@LoadTracks(track)
  247. {
  248.     new
  249.         rows,
  250.         fields;
  251.  
  252.     cache_get_data(rows, fields, gRaceConections);
  253.  
  254.     for (new i = 0; i != rows; i ++)
  255.     {
  256.         ARRAY_TRACK_RACE[track][i][rExist] = true;
  257.         ARRAY_TRACK_RACE[track][i][rID] = cache_get_field_intr(i, "ID");
  258.         ARRAY_TRACK_RACE[track][i][rOwner] = cache_get_field_intr(i, "raceowner");
  259.         ARRAY_TRACK_RACE[track][i][rPos][0] = cache_get_field_floatr(i, "pos0");
  260.         ARRAY_TRACK_RACE[track][i][rPos][1] = cache_get_field_floatr(i, "pos1");
  261.         ARRAY_TRACK_RACE[track][i][rPos][2] = cache_get_field_floatr(i, "pos2");
  262.         printf("i: %d, Owner %d (rID: %d)", i , ARRAY_TRACK_RACE[track][i][rOwner], ARRAY_TRACK_RACE[track][i][rID]);
  263.     }
  264.     return 1;
  265. }
  266.  
  267. stock TrackCountOfRace(raceid)
  268. {
  269.     new
  270.         c,
  271.         i;
  272.  
  273.     while(i < MAX_STEPS)
  274.     {
  275.         printf("i: %d, Owner %d, IDRace %d", i, ARRAY_TRACK_RACE[raceid][i][rOwner], n@dRace[raceid][rID]);
  276.         if(TrackExist(raceid, i) && ARRAY_TRACK_RACE[raceid][i][rOwner] == n@dRace[raceid][rID])
  277.         {
  278.             printf("TrackCountOfRace: %d", i);
  279.             c ++;
  280.         }
  281.         i ++;
  282.     }
  283.     return (c);
  284. }
  285.  
  286. stock StartRace(playerid, raceid)
  287. {
  288.     if(RaceExist(raceid)){
  289.         DataRace[playerid][pRace] = raceid;
  290.         DataRace[playerid][pCount] = 0;
  291.  
  292.         new step = GetPlayerStep(playerid),
  293.             next = step+1;
  294.  
  295.         SetRaceCPEx(playerid, raceid, step, next);
  296.     }
  297.     return 1;
  298. }
  299.  
  300. stock GetTrackPos(raceid, trackid, &Float:X, &Float:Y, &Float:Z)
  301. {
  302.     if((RaceExist(raceid) || TrackExist(raceid, trackid)))
  303.     {
  304.         X = ARRAY_TRACK_RACE[raceid][trackid][rPos][0];
  305.         Y = ARRAY_TRACK_RACE[raceid][trackid][rPos][1];
  306.         Z = ARRAY_TRACK_RACE[raceid][trackid][rPos][2];
  307.         return 1;
  308.     }
  309.     return 0;
  310. }
  311.  
  312. stock GetPlayerStep(playerid)
  313. {
  314.     if(IsPlayerConnected(playerid))
  315.     {
  316.         if(RaceExist(GetPlayerRace(playerid)))
  317.         {
  318.             return (DataRace[playerid][pCount]);
  319.         }
  320.     }
  321.     return 0;
  322. }
  323.  
  324. stock GetPlayerRace(playerid)
  325. {
  326.     if(IsPlayerConnected(playerid))
  327.     {
  328.         if(RaceExist(DataRace[playerid][pRace]))
  329.         {
  330.             return (DataRace[playerid][pRace]);
  331.         }
  332.     }
  333.     return 0;
  334. }
  335.  
  336. stock SetRaceCPEx(playerid, raceid, step, next)
  337. {
  338.     if(IsPlayerConnected(playerid))
  339.     {
  340.         if(RaceExist(DataRace[playerid][pRace]))
  341.         {  
  342.             SetPlayerRaceCheckpoint(playerid,0, ARRAY_TRACK_RACE[raceid][step][rPos][0], ARRAY_TRACK_RACE[raceid][step][rPos][1], ARRAY_TRACK_RACE[raceid][step][rPos][2],
  343.             ARRAY_TRACK_RACE[raceid][next][rPos][0], ARRAY_TRACK_RACE[raceid][next][rPos][1], ARRAY_TRACK_RACE[raceid][next][rPos][2], 5.0);
  344.         }
  345.     }
  346.     return 1;
  347. }
  348.  
  349. stock CreateTrack(raceid, Float:sX, Float:sY, Float:sZ)
  350. {
  351.     if(RaceExist(raceid))
  352.     {
  353.         new query[128];
  354.         for(new i; i != MAX_STEPS; i++) if(!ARRAY_TRACK_RACE[raceid][i][rExist])
  355.         {
  356.             ARRAY_TRACK_RACE[raceid][i][rExist] = true;
  357.             ARRAY_TRACK_RACE[raceid][i][rOwner] = n@dRace[raceid][rID];
  358.  
  359.             format(query, sizeof(query), "INSERT INTO `tracks` (`raceowner`) VALUES('%d')", raceid);
  360.             mysql_tquery(gRaceConections, query, "SQLCreatetRack", "iifff", raceid, i, sX, sY, sZ);
  361.             return i;
  362.         }  
  363.     }
  364.     return -1;
  365. }
  366.  
  367. forward SQLCreatetRack(raceid, id, Float:x, Float:y, Float:z); public SQLCreatetRack(raceid, id, Float:x, Float:y, Float:z)
  368. {
  369.     if(TrackExist(raceid, id))
  370.     {
  371.         ARRAY_TRACK_RACE[raceid][id][rID] = cache_insert_id(gRaceConections);
  372.         SetTrackPos(raceid, id, x, y, z);
  373.         return 1;  
  374.     }
  375.     return 0;
  376. }
  377.  
  378. stock StrAvailableRaces()
  379. {
  380.     new
  381.         stri[34], c;
  382.  
  383.     for(new i; i != MAX_RACES; i ++) if(RaceExist(i))
  384.     {
  385.         format(stri, sizeof(stri), "%s%d, ", stri, i);
  386.         c++;
  387.     }
  388.     if(c < 1){
  389.         format(stri, sizeof(stri), "Nothing ..");
  390.     }
  391.     return stri;
  392. }
  393.  
  394. stock SetRacePos(raceid, Float:X, Float:Y, Float:Z)
  395. {
  396.     if(RaceExist(raceid))
  397.     {
  398.         n@dRace[raceid][rPos][0] = X;
  399.         n@dRace[raceid][rPos][1] = Y;
  400.         n@dRace[raceid][rPos][2] = Z;
  401.         return 1;
  402.     }
  403.     return 0;
  404. }
  405.  
  406. stock GetRacePos(raceid, &Float:X, &Float:Y, &Float:Z)
  407. {
  408.     if(RaceExist(raceid)){
  409.         X = n@dRace[raceid][rPos][0];
  410.         Y = n@dRace[raceid][rPos][1];
  411.         Z = n@dRace[raceid][rPos][2];
  412.         return 1;
  413.     }
  414.     return 0;
  415. }
  416.  
  417. stock GetRaceFacingAngle(raceid, &Float:R)
  418. {
  419.     if(RaceExist(raceid)){
  420.         R = n@dRace[raceid][rPos][3];
  421.         return 1;
  422.     }
  423.     return 0;
  424. }
  425.  
  426. stock SetRaceFacingAngle(raceid, Float:R)
  427. {
  428.     if(RaceExist(raceid)){
  429.         n@dRace[raceid][rPos][3] = R;
  430.         return 1;
  431.     }
  432.     return 0;
  433. }
  434.  
  435. stock GetRaceVirtualWorld(raceid)
  436. {
  437.     if(RaceExist(raceid)){
  438.         return (n@dRace[raceid][rVW]);
  439.     }
  440.     return 0;
  441. }
  442.  
  443. stock GetRaceInterior(raceid)
  444. {
  445.     if(RaceExist(raceid)){
  446.         return (n@dRace[raceid][rINT]);
  447.     }
  448.     return 0;
  449. }
  450.  
  451. stock SetRaceVirtualWorld(raceid, newvw)
  452. {
  453.     if(RaceExist(raceid)){
  454.         n@dRace[raceid][rVW] = newvw;
  455.     }
  456.     return 0;
  457. }
  458.  
  459. stock SetRaceInterior(raceid, newint){
  460.     if(RaceExist(raceid)){
  461.         n@dRace[raceid][rINT] = newint;
  462.     }
  463.     return 0;
  464. }
  465.  
  466. stock SetRaceName(raceid, name[]){
  467.     if(RaceExist(raceid)){
  468.         format(n@dRace[raceid][rName], 24, "%s", name);
  469.     }
  470.     return 0;
  471. }
  472. stock CreateRace(Float:sX, Float:sY, Float:sZ, interior, virtualworld, name[] = "Untitled")
  473. {
  474.     new
  475.         query[128];
  476.     for(new i; i != MAX_RACES; i++) if(!n@dRace[i][rExist])
  477.     {
  478.         n@dRace[i][rExist] = true;
  479.         SetRaceName(i, name);
  480.         format(query, sizeof(query), "INSERT INTO `races` (`name`) VALUES('%s')", SQL_ReturnEscapedR(n@dRace[i][rName]));
  481.         mysql_tquery(gRaceConections, query, "SqlCreateRC", "ifffii", i, sX, sY, sZ, interior, virtualworld);
  482.         return i;
  483.     }  
  484.     return -1;
  485. }
  486.  
  487. forward SqlCreateRC(id, Float:sX, Float:sY, Float:sZ, interior, virtualworld); public SqlCreateRC(id, Float:sX, Float:sY, Float:sZ, interior, virtualworld)
  488. {
  489.     if(RaceExist(id)){
  490.         n@dRace[id][rID] = cache_insert_id(gRaceConections);
  491.         n@dRace[id][rPos][0] = sX;
  492.         n@dRace[id][rPos][1] = sY;
  493.         n@dRace[id][rPos][2] = sZ;
  494.         n@dRace[id][rINT] = interior;
  495.         n@dRace[id][rVW] = virtualworld;
  496.         UpdateRaceDatabaseAll(id);
  497.         return 1;
  498.     }
  499.     return 0;
  500. }
  501.  
  502. stock UpdateRaceDatabaseAll(raceid){
  503.     if(!n@dRace[raceid][rExist])
  504.         return 0;
  505.        
  506.     static
  507.         Float:pPos[4],
  508.         str[64];
  509.  
  510.     GetRacePos(raceid, pPos[0], pPos[1], pPos[2]);
  511.     GetRaceFacingAngle(raceid, pPos[3]);
  512.  
  513.     for(new i = 0; i != 4; i ++){
  514.         format(str, sizeof(str), "pos%d", i);
  515.         UpdateRowRaceFloat(raceid, str, pPos[i]);  
  516.     }
  517.  
  518.     UpdateRowRaceInt(raceid, "vw", GetRaceVirtualWorld(raceid));    
  519.     UpdateRowRaceInt(raceid, "int", GetRaceInterior(raceid));  
  520.     return 1;
  521. }
  522.  
  523. stock UpdateRowRaceStr(id, rUpdate[], variable[])
  524. {
  525.     if(RaceExist(id)){
  526.         new query[200];
  527.         format(query, sizeof(query), "UPDATE `races` SET `%s` = '%s' WHERE `id` = '%d'",
  528.             rUpdate,
  529.             variable,
  530.             n@dRace[id][rID]);
  531.         mysql_tquery(gRaceConections, query, "", "");
  532.         return 1;
  533.     }
  534.     return 0;
  535. }
  536.  
  537.  
  538. stock SetTrackPos(raceid, trackid, Float:tX, Float:tY, Float:tZ){
  539.     if(!ARRAY_TRACK_RACE[raceid][trackid][rExist])
  540.         return 0;
  541.  
  542.     ARRAY_TRACK_RACE[raceid][trackid][rPos][0] = tX;
  543.     ARRAY_TRACK_RACE[raceid][trackid][rPos][1] = tY;
  544.     ARRAY_TRACK_RACE[raceid][trackid][rPos][2] = tZ;
  545.    
  546.     return 1;
  547. }
  548.  
  549. stock cSQL@track(id@race, id@track){
  550.     if(RaceExist(raceid)){
  551.         new query[128];
  552.         format(query, sizeof(query), "UPDATE `tracks` SET `pos0` = '%.4f', `pos1` = '%.4f', `pos2` = '%.4f' WHERE `ID` = '%d'",
  553.             ARRAY_TRACK_RACE[raceid][trackid][rPos][0],
  554.             ARRAY_TRACK_RACE[raceid][trackid][rPos][1],
  555.             ARRAY_TRACK_RACE[raceid][trackid][rPos][2],
  556.             ARRAY_TRACK_RACE[raceid][trackid][rID]);
  557.         mysql_tquery(gRaceConections, query, "", "");
  558.         return 1;
  559.     }
  560.     return 0;
  561. }
  562.  
  563. stock TrackExist(r_@i, r_@t){
  564.     return ((0 <= (r_@i) < MAX_STEPS && (ARRAY_TRACK_RACE[r_@i][r_@t][rExist])));
  565. }
  566.  
  567. stock _Valid@Race(r_@i){
  568.     return (n@dRace[r_@i][rExist]);
  569. }
  570.  
  571. stock UpdateRowRaceFloat(id, rUpdate[], Float:variable)
  572. {
  573.     if(RaceExist(id)){
  574.         new query[200];
  575.         format(query, sizeof(query), "UPDATE `races` SET `%s` = '%f' WHERE `id` = '%d'",
  576.             rUpdate,
  577.             variable,
  578.             n@dRace[id][rID]);
  579.         mysql_tquery(gRaceConections, query, "", "");
  580.         return 1;
  581.     }
  582.     return 0;
  583. }
  584.  
  585. stock UpdateRowRaceInt(id, rUpdate[], variable)
  586. {
  587.     if(RaceExist(id)){
  588.         new query[200];
  589.         format(query, sizeof(query), "UPDATE `races` SET `%s` = '%d' WHERE `id` = '%d'",
  590.             rUpdate,
  591.             variable,
  592.             n@dRace[id][rID]);
  593.         mysql_tquery(gRaceConections, query, "", "");
  594.     }
  595.     return 1;
  596. }
  597.  
  598. stock SQL_ReturnEscapedR(string[])
  599. {
  600.     new
  601.         entry[256];
  602.  
  603.     mysql_real_escape_string(string, entry, gRaceConections);
  604.     return entry;
  605. }
  606.  
  607. stock GetParticipants(raceid = 0){
  608.     if(RaceExist(id))
  609.     {
  610.         new c_@,
  611.             i;
  612.  
  613.         while(i < GetMaxPlayers())
  614.         {
  615.             if(IsPlayerConnected(playerid) && Participante[i] == raceid){
  616.                 c_@ ++;
  617.             }
  618.             i ++;
  619.         }
  620.         return (c_@);
  621.     }
  622.     return 0;
  623. }
  624.  
  625. stock GetActiveRaces()
  626. {
  627.     new
  628.         _@c,
  629.         i;
  630.  
  631.     while(i < (MAX_RACES))
  632.     {
  633.         if(RaceExist(i)){
  634.             _@c ++;
  635.         }
  636.         i++;
  637.     }
  638.     return (_@c);
  639. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement