toribio

toribio

Dec 11th, 2008
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.43 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 WEAPON_FILE "armas.cfg"
  22.  
  23. public OnFilterScriptInit()
  24. {
  25.     if(!fexist(WEAPON_FILE))
  26.     {
  27.         new File:tmp;
  28.         tmp = fopen(WEAPON_FILE, io_write);
  29.         fclose(tmp);
  30.     }
  31.     return 1;
  32. }
  33.  
  34. public OnPlayerSpawn(playerid)return LoadWeapons(playerid);
  35. public OnPlayerDeath(playerid, killerid, reason)return SaveWeapons(playerid);
  36. public OnPlayerDisconnect(playerid, reason)return SaveWeapons(playerid);
  37.  
  38. stock LoadWeapons(playerid)
  39. {
  40.     new File:armas, tmp[256], fname[30], pname[30], arma[13], municao[13];
  41.     armas = fopen(WEAPON_FILE, io_read);
  42.     GetPlayerName(playerid, pname, sizeof pname);
  43.     while(fread(armas, tmp, sizeof tmp, false))
  44.     {
  45.         new off[256], index;
  46.         off = strtok(tmp, index);
  47.         strmid(fname, off, 0, strlen(off));
  48.         if(strcmp(pname, fname, false) != 0)continue;
  49.         for(new a; a < 12; a++)arma[a] = strval(strtok(tmp, index));
  50.         for(new m; m < 12; m++)municao[m] = strval(strtok(tmp, index));
  51.         for(new i; i < 12; i++)
  52.         {
  53.             if(arma[i] > 0 && arma[i] < 47 && municao[i] > 0)
  54.         {
  55.             if(arma[i] != 38 && arma[i] != 37)
  56.                 GivePlayerWeapon(playerid, arma[i], municao[i]);
  57.         }
  58.         }
  59.     }
  60.     fclose(armas);
  61.     return 1;
  62. }
  63.  
  64. stock SaveWeapons(playerid)
  65. {
  66.     new File:armas, File:ftmp, tmp[256], fname[30], pname[30], arma[13], municao[13];
  67.     armas = fopen(WEAPON_FILE, io_readwrite);
  68.     ftmp = fopen(WEAPON_FILE#.tmp, io_write);
  69.     GetPlayerName(playerid, pname, sizeof pname);
  70.     while(fread(armas, tmp, sizeof tmp, false))
  71.     {
  72.         new off[256], index;
  73.         off = strtok(tmp, index);
  74.         strmid(fname, off, 0, strlen(off));
  75.         if(!strcmp(pname, fname, false))continue;
  76.         format(tmp, sizeof tmp, "%s", tmp);
  77.         fwrite(ftmp, tmp);
  78.     }
  79.     fclose(armas);
  80.     fclose(ftmp);
  81.     fremove(WEAPON_FILE);
  82.     armas = fopen(WEAPON_FILE, io_write);
  83.     ftmp = fopen(WEAPON_FILE#.tmp, io_read);
  84.     while(fread(ftmp, tmp))
  85.     {
  86.         format(tmp, sizeof tmp, "%s", tmp);
  87.         fwrite(armas, tmp);
  88.     }
  89.     fclose(ftmp);
  90.     fremove(WEAPON_FILE#.tmp);
  91.     for(new i; i < 12; i++)GetPlayerWeaponData(playerid, i+1, arma[i+1], municao[i+1]);
  92.     format(tmp, sizeof tmp, "%s ", pname);
  93.     for(new a; a < 24; a++)
  94.     {
  95.         new s[10];
  96.         if(a < 12)
  97.         {
  98.             format(s, sizeof s, "%d ", arma[a]);
  99.             strcat(tmp, s);
  100.         } else {
  101.             format(s, sizeof s, "%d ", municao[a-12]);
  102.             strcat(tmp, s);
  103.         }
  104.     }
  105.     strcat(tmp, "\r\n");
  106.     fwrite(armas, tmp);
  107.     fclose(armas);
  108.     return 1;
  109. }
  110.  
  111. stock strtok(const string[], &index)
  112. {
  113.     new length = strlen(string);
  114.     while ((index < length) && (string[index] <= ' '))
  115.     {
  116.         index++;
  117.     }
  118.     new offset = index;
  119.     new result[20];
  120.     while ((index < length) && (string[index] > ' ') && ((index - offset) < (sizeof(result) - 1)))
  121.     {
  122.         result[index - offset] = string[index];
  123.         index++;
  124.     }
  125.     result[index - offset] = EOS;
  126.     return result;
  127. }
Add Comment
Please, Sign In to add comment