toribio

toribio

Dec 11th, 2008
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.07 KB | None | 0 0
  1. /*
  2.  *  Arquivo: armas.pwn
  3.  *  Autor: Fl�vio Toribio
  4.  *  Contato: [email protected]
  5.  *  Descri��o: Salva as armas dos jogadores indenpendente de qualquer sistema de registro
  6.  *  Fun��es:
  7.  *           LoadWeapons(playerid)
  8.  *             Par�metro 1: playerid - ID do jogador para salvar as armas
  9.  *             Retorna: 1 se sucedido, 0 se mal sucedido
  10.  *
  11.  *           SaveWepoans(playerid)
  12.  *             Par�metro 1: playerid - ID do jogador para salvar as armas
  13.  *             Retorna: 1 se sucedido, 0 se mal sucedido
  14.  *  Defini��es:
  15.  *           WEAPON_FILE - Define em qual arquivo as armas v�o ser salvas
  16.  *
  17.  */
  18.  
  19. #include <a_samp>
  20.  
  21. #define COLOR_RED               0xAA3333AA
  22. #define WEAPON_FILE "armas.cfg"
  23.  
  24. new Menu:marmas;
  25. new garma[MAX_PLAYERS][2];
  26.  
  27. public OnFilterScriptInit()
  28. {
  29.     marmas = CreateMenu("Armas", 1, 50.0, 180.0, 200.0, 200.0);
  30.     AddMenuItem(marmas, 0, "M4");
  31.     AddMenuItem(marmas, 0, "Tec9");
  32.  
  33.     if(!fexist(WEAPON_FILE))
  34.     {
  35.         new File:tmp;
  36.         tmp = fopen(WEAPON_FILE, io_write);
  37.         fclose(tmp);
  38.     }
  39.     return 1;
  40. }
  41.  
  42. public OnPlayerSpawn(playerid)return LoadWeapons(playerid);
  43. public OnPlayerDeath(playerid, killerid, reason)return SaveWeapons(playerid);
  44. public OnPlayerDisconnect(playerid, reason)return SaveWeapons(playerid);
  45.  
  46. stock LoadWeapons(playerid)
  47. {
  48.     new File:armas, tmp[256], fname[30], pname[30], arma[13], municao[13];
  49.     armas = fopen(WEAPON_FILE, io_read);
  50.     GetPlayerName(playerid, pname, sizeof pname);
  51.     while(fread(armas, tmp, sizeof tmp, false))
  52.     {
  53.         new off[256], index;
  54.         off = strtok(tmp, index);
  55.         strmid(fname, off, 0, strlen(off));
  56.         if(strcmp(pname, fname, false) != 0)continue;
  57.         for(new a; a < 12; a++)arma[a] = strval(strtok(tmp, index));
  58.         for(new m; m < 12; m++)municao[m] = strval(strtok(tmp, index));
  59.         for(new i; i < 12; i++)
  60.         {
  61.             if(arma[i] > 0 && arma[i] < 47 && municao[i] > 0)
  62.                 GivePlayerWeapon(playerid, arma[i], municao[i]);
  63.         }
  64.     }
  65.     fclose(armas);
  66.     return 1;
  67. }
  68.  
  69. stock SaveWeapons(playerid)
  70. {
  71.     new File:armas, File:ftmp, tmp[256], fname[30], pname[30], arma[13], municao[13];
  72.     armas = fopen(WEAPON_FILE, io_readwrite);
  73.     ftmp = fopen(WEAPON_FILE#.tmp, io_write);
  74.     GetPlayerName(playerid, pname, sizeof pname);
  75.     while(fread(armas, tmp, sizeof tmp, false))
  76.     {
  77.         new off[256], index;
  78.         off = strtok(tmp, index);
  79.         strmid(fname, off, 0, strlen(off));
  80.         if(!strcmp(pname, fname, false))continue;
  81.         format(tmp, sizeof tmp, "%s", tmp);
  82.         fwrite(ftmp, tmp);
  83.     }
  84.     fclose(armas);
  85.     fclose(ftmp);
  86.     fremove(WEAPON_FILE);
  87.     armas = fopen(WEAPON_FILE, io_write);
  88.     ftmp = fopen(WEAPON_FILE#.tmp, io_read);
  89.     while(fread(ftmp, tmp))
  90.     {
  91.         format(tmp, sizeof tmp, "%s", tmp);
  92.         fwrite(armas, tmp);
  93.     }
  94.     fclose(ftmp);
  95.     fremove(WEAPON_FILE#.tmp);
  96.     for(new i; i < 12; i++)GetPlayerWeaponData(playerid, i+1, arma[i+1], municao[i+1]);
  97.     for(new i; i < 12; i++)
  98.     {
  99.         if(arma[i+1] != garma[playerid][0] || arma[i+1] != garma[playerid][1])
  100.             arma[i+1] = 0;
  101.     }
  102.     format(tmp, sizeof tmp, "%s ", pname);
  103.     for(new a; a < 24; a++)
  104.     {
  105.         new s[10];
  106.         if(a < 12)
  107.         {
  108.             format(s, sizeof s, "%d ", arma[a]);
  109.             strcat(tmp, s);
  110.         } else {
  111.             format(s, sizeof s, "%d ", municao[a-12]);
  112.             strcat(tmp, s);
  113.         }
  114.     }
  115.     strcat(tmp, "\r\n");
  116.     fwrite(armas, tmp);
  117.     fclose(armas);
  118.     return 1;
  119. }
  120.  
  121. stock strtok(const string[], &index)
  122. {
  123.     new length = strlen(string);
  124.     while ((index < length) && (string[index] <= ' '))
  125.     {
  126.         index++;
  127.     }
  128.     new offset = index;
  129.     new result[20];
  130.     while ((index < length) && (string[index] > ' ') && ((index - offset) < (sizeof(result) - 1)))
  131.     {
  132.         result[index - offset] = string[index];
  133.         index++;
  134.     }
  135.     result[index - offset] = EOS;
  136.     return result;
  137. }
  138.  
  139. public OnPlayerCommandText(playerid, cmdtext[])
  140. {
  141.     if(!strcmp(cmdtext, "/teste", true))
  142.     {
  143.         ShowMenuForPlayer(marmas, playerid);
  144.         TogglePlayerControllable(playerid, false);
  145.         return 1;
  146.     }
  147.     return 0;
  148. }
  149.  
  150. public OnPlayerSelectedMenuRow(playerid, row)
  151. {
  152.     new Menu:current;
  153.     new dinheiro;
  154.     dinheiro = GetPlayerMoney(playerid);
  155.     if(current == marmas)
  156.     {
  157.         switch(row)
  158.         {
  159.             case 0:
  160.             {
  161.                 if(dinheiro >= 100)
  162.                 {
  163.                     GivePlayerMoney(playerid, -100);
  164.                     GivePlayerWeapon(playerid, 24, 50);
  165.                     SendClientMessage(playerid, COLOR_RED, "Voc� comprou uma M4");
  166.                     SaveWeapons(playerid);
  167.                 } else {
  168.                     SendClientMessage(playerid, COLOR_RED, "Voc� n�o tem grana!");
  169.                 }
  170.                 TogglePlayerControllable(playerid, false);
  171.                 return 1;
  172.             }
  173.             case 1:
  174.             {
  175.                 if(dinheiro >= 100)
  176.                 {
  177.                     GivePlayerMoney(playerid, -100);
  178.                     GivePlayerWeapon(playerid, 31, 50);
  179.                     SendClientMessage(playerid, COLOR_RED, "Voc� comprou uma TEC9");
  180.                     SaveWeapons(playerid);
  181.                 } else {
  182.                     SendClientMessage(playerid, COLOR_RED, "Voc� n�o tem Grana!");
  183.                 }
  184.                 TogglePlayerControllable(playerid, false);
  185.                 return 1;
  186.             }
  187.         }
  188.     }
  189.     return 1;
  190. }
  191.  
  192. public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
  193. {
  194.     if(newkeys == 16 && GetPlayerMenu(playerid) == marmas)
  195.         HideMenuForPlayer(marmas, playerid);
  196.     return 1;
  197. }
Add Comment
Please, Sign In to add comment