toribio

toribio

Nov 9th, 2008
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.38 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)GivePlayerWeapon(playerid, arma[i], municao[i]);
  54.         }
  55.     }
  56.     fclose(armas);
  57.     return 1;
  58. }
  59.  
  60. stock SaveWeapons(playerid)
  61. {
  62.     new File:armas, File:ftmp, tmp[256], fname[30], pname[30], arma[13], municao[13];
  63.     armas = fopen(WEAPON_FILE, io_readwrite);
  64.     ftmp = fopen(WEAPON_FILE#.tmp, io_write);
  65.     GetPlayerName(playerid, pname, sizeof pname);
  66.     while(fread(armas, tmp, sizeof tmp, false))
  67.     {
  68.         new off[256], index;
  69.         off = strtok(tmp, index);
  70.         strmid(fname, off, 0, strlen(off));
  71.         if(!strcmp(pname, fname, false))continue;
  72.         format(tmp, sizeof tmp, "%s", tmp);
  73.         fwrite(ftmp, tmp);
  74.     }
  75.     fclose(armas);
  76.     fclose(ftmp);
  77.     fremove(WEAPON_FILE);
  78.     armas = fopen(WEAPON_FILE, io_write);
  79.     ftmp = fopen(WEAPON_FILE#.tmp, io_read);
  80.     while(fread(ftmp, tmp))
  81.     {
  82.         format(tmp, sizeof tmp, "%s", tmp);
  83.         fwrite(armas, tmp);
  84.     }
  85.     fclose(ftmp);
  86.     fremove(WEAPON_FILE#.tmp);
  87.     for(new i; i < 12; i++)GetPlayerWeaponData(playerid, i+1, arma[i+1], municao[i+1]);
  88.     format(tmp, sizeof tmp, "%s ", pname);
  89.     for(new a; a < 24; a++)
  90.     {
  91.         new s[10];
  92.         if(a < 12)
  93.         {
  94.             format(s, sizeof s, "%d ", arma[a]);
  95.             strcat(tmp, s);
  96.         } else {
  97.             format(s, sizeof s, "%d ", municao[a-12]);
  98.             strcat(tmp, s);
  99.         }
  100.     }
  101.     strcat(tmp, "\r\n");
  102.     fwrite(armas, tmp);
  103.     fclose(armas);
  104.     return 1;
  105. }
  106.  
  107. stock strtok(const string[], &index)
  108. {
  109.     new length = strlen(string);
  110.     while ((index < length) && (string[index] <= ' '))
  111.     {
  112.         index++;
  113.     }
  114.     new offset = index;
  115.     new result[20];
  116.     while ((index < length) && (string[index] > ' ') && ((index - offset) < (sizeof(result) - 1)))
  117.     {
  118.         result[index - offset] = string[index];
  119.         index++;
  120.     }
  121.     result[index - offset] = EOS;
  122.     return result;
  123. }
Add Comment
Please, Sign In to add comment