Advertisement
Guest User

Killable Cows v0.1

a guest
Dec 20th, 2010
435
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 6.94 KB | None | 0 0
  1. /*
  2.     "Killable Cows" v0.1 by ~AWESOME~
  3.     17.12.2010
  4.    
  5.     Changelog:
  6.     o Added Counters
  7.    
  8. */
  9.  
  10. #include <a_samp>
  11.  
  12. #define SCRIPT_VERSION      "0.1"
  13. #define MAX_COWS            3
  14. #define FILE_COWS           "KillableCows.txt"
  15.  
  16. #define COLOR_RED           0xFF0000AA
  17. #define COLOR_ORANGE        0xFF9900AA
  18.  
  19. #define IsPlayerSpawned(%1) IsPlayerSpawned[%1]
  20. #define IsCowConnected(%1)  CowInfo[%1][IsValid]
  21.  
  22. #define GetDistanceBetweenCoords(%1,%2,%3,%4,%5,%6) floatsqroot((%4 - %1)*(%4 - %1) + (%5 - %2)*(%5 - %2) + (%6 - %3)*(%6 - %3))
  23.  
  24. #if !defined dcmd
  25.     #define dcmd(%1,%2,%3) if((strcmp((%3)[1], #%1, true, (%2)) == 0) && ((((%3)[(%2) + 1] == 0) && (dcmd_%1(playerid, ""))) || (((%3)[(%2) + 1] == 32) && (dcmd_%1(playerid, (%3)[(%2) + 2]))))) return 1
  26. #endif
  27.  
  28. enum cowinfos
  29. {
  30.     iObjectID,
  31.     Float:sX,
  32.     Float:sY,
  33.     Float:sZ,
  34.     Float:Angle[3],
  35.     IsDead,
  36.     IsValid
  37. }
  38.  
  39. new CowInfo[MAX_COWS][cowinfos];
  40. new bool:IsPlayerSpawned[MAX_PLAYERS];
  41.  
  42. forward LoadCows();
  43. forward OnCowConnect(CowID);
  44.  
  45. stock Float:GetDistanceToObject(playerid,objectid)
  46. {
  47.     new Float:x1,Float:y1,Float:z1,Float:x2,Float:y2,Float:z2;
  48.     GetPlayerPos(playerid,x1,y1,z1);
  49.     GetObjectPos(objectid,x2,y2,z2);
  50.     return floatsqroot(floatpower(floatabs(floatsub(x2,x1)),2)+floatpower(floatabs(floatsub(y2,y1)),2)+floatpower(floatabs(floatsub(z2,z1)),2));
  51. }
  52.  
  53. public OnFilterScriptInit()
  54. {
  55.     printf(".:: Killable Cows v%s by ~AWESOME~ loaded ::.",SCRIPT_VERSION);
  56.     LoadCows();
  57.     return 1;
  58. }
  59.  
  60. public OnFilterScriptExit()
  61. {
  62.     for(new id = 0; id < MAX_PLAYERS; id++)
  63.     {
  64.         DestroyObject(CowInfo[id][iObjectID]);
  65.     }
  66.     return 1;
  67. }
  68.  
  69. public LoadCows()
  70. {
  71.     new File:cowfile, string[128], data[26][32], count;
  72.     if(fexist(FILE_COWS))
  73.     {
  74.         cowfile = fopen(FILE_COWS,io_read);
  75.         for(new id = 0; id < MAX_COWS; id++)
  76.         {
  77.             fread(cowfile,string);
  78.             OnCowConnect(id);
  79.             if(!strlen(string))
  80.             {
  81.                 CowInfo[id][IsValid] = 0;
  82.                 continue;
  83.             }
  84.             split(string,data,',');
  85.             CowInfo[id][IsValid] = 1;
  86.             CowInfo[id][IsDead] = 0;
  87.             CowInfo[id][sX] = floatstr(data[0]);
  88.             CowInfo[id][sY] = floatstr(data[1]);
  89.             CowInfo[id][sZ] = floatstr(data[2]);
  90.             CowInfo[id][Angle][0] = floatstr(data[3]);
  91.             CowInfo[id][Angle][1] = floatstr(data[4]);
  92.             CowInfo[id][Angle][2] = floatstr(data[5]);
  93.             CowInfo[id][iObjectID] = CreateObject(16442,CowInfo[id][sX],CowInfo[id][sY],CowInfo[id][sZ],CowInfo[id][Angle][0],CowInfo[id][Angle][1],CowInfo[id][Angle][2]);
  94.             count++;
  95.         }
  96.         if(!count) print("» ERROR: Cows couldn't be loaded");
  97.         else printf("» %d Cows loaded.",count);
  98.         fclose(cowfile);
  99.     }
  100.     else print("» ERROR: Cows couldn't be loaded");
  101.     return 1;
  102. }
  103.  
  104. public OnCowConnect(CowID)
  105. {
  106.     CowInfo[CowID][iObjectID] = -1;
  107.     return 1;
  108. }
  109.  
  110. public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
  111. {
  112.     new wstate = GetPlayerWeaponState(playerid);
  113.     if(newkeys & KEY_FIRE && GetPlayerWeapon(playerid) != 0 && IsPlayerSpawned(playerid))
  114.     {
  115.         if(wstate != WEAPONSTATE_RELOADING || wstate != WEAPONSTATE_NO_BULLETS)
  116.         {
  117.             new Float:X,
  118.                 Float:Y,
  119.                 Float:Z,
  120.                 Float:Distance,
  121.                 CowID = GetPlayerClosestCow(playerid);
  122.             Distance = GetDistanceToObject(playerid,CowInfo[CowID][iObjectID]);
  123.             GetPosFromView(playerid,Distance,X,Y,Z);
  124.             if(ObjectToPoint(3.0,CowInfo[CowID][iObjectID],X,Y,Z) != 0)
  125.             {
  126.                 KillCow(CowID);
  127.             }
  128.         }
  129.     }
  130.     return 0;
  131. }
  132.  
  133. public OnPlayerCommandText(playerid,cmdtext[])
  134. {
  135.     dcmd(respawncows,11,cmdtext);
  136.     return 0;
  137. }
  138.  
  139. dcmd_respawncows(playerid,params[])
  140. {
  141.     #pragma unused params
  142.     new string[100], count = 0;
  143.     if(IsPlayerAdmin(playerid))
  144.     {
  145.         if(CountCows() == 0)
  146.         {
  147.             SendClientMessage(playerid,COLOR_RED,"» There aren't any cows connected!");
  148.             return 1;
  149.         }
  150.         for(new id = 0; id < MAX_COWS; id++)
  151.         {
  152.             if(IsCowConnected(id))
  153.             {
  154.                 if(CowInfo[id][IsDead])
  155.                 {
  156.                     SetObjectPos(CowInfo[id][iObjectID], CowInfo[id][sX], CowInfo[id][sY], CowInfo[id][sZ]);
  157.                     SetObjectRot(CowInfo[id][iObjectID], CowInfo[id][Angle][0], CowInfo[id][Angle][1], CowInfo[id][Angle][2]);
  158.                     CowInfo[id][IsDead] = 0;
  159.                     count++;
  160.                 }
  161.             }
  162.         }
  163.         if(!count)
  164.         {
  165.             SendClientMessage(playerid,COLOR_ORANGE,"» There is no dead cow!");
  166.             return 1;
  167.         }
  168.         format(string,sizeof(string),"» %d Cows respawned.",count);
  169.         SendClientMessage(playerid,COLOR_ORANGE,string);
  170.     }
  171.     else SendClientMessage(playerid,COLOR_RED,"» You aren't an administrator!");
  172.     return 1;
  173. }
  174.  
  175.  
  176. public OnPlayerConnect(playerid)
  177. {
  178.     IsPlayerSpawned(playerid) = false;
  179.     return 1;
  180. }
  181.  
  182. public OnPlayerSpawn(playerid)
  183. {
  184.     IsPlayerSpawned(playerid) = true;
  185.     return 1;
  186. }
  187.  
  188. public OnPlayerDeath(playerid,killerid,reason)
  189. {
  190.     IsPlayerSpawned(playerid) = false;
  191.     return 1;
  192. }
  193.  
  194. stock KillCow(CowID)
  195. {
  196.     CowInfo[CowID][IsDead] = 1;
  197.     if(wait(3))
  198.     {
  199.         SetObjectRot(CowInfo[CowID][iObjectID], 90.0, CowInfo[CowID][Angle][1], CowInfo[CowID][Angle][2]);
  200.         SetObjectPos(CowInfo[CowID][iObjectID], CowInfo[CowID][sX], CowInfo[CowID][sY], CowInfo[CowID][sZ] - 1.25);
  201.     }
  202.     return 1;
  203. }
  204.  
  205. stock CountCows()
  206. {
  207.     new cowcount = 0;
  208.     for(new i = 0; i < MAX_COWS; i++)
  209.     {
  210.         if(IsCowConnected(i))
  211.         {
  212.             cowcount++;
  213.         }
  214.     }
  215.     return cowcount;
  216. }
  217.  
  218. stock ObjectToPoint(Float:Scale, ObjectID, Float:X, Float:Y, Float:Z)
  219. {
  220.    new Float:oldposx, Float:oldposy, Float:oldposz, Float:tempposx, Float:tempposy, Float:tempposz;
  221.    GetObjectPos(ObjectID, oldposx, oldposy, oldposz);
  222.    tempposx = (oldposx -X);
  223.    tempposy = (oldposy -Y);
  224.    tempposz = (oldposz -Z);
  225.    if(((tempposx < Scale) && (tempposx > -Scale)) && ((tempposy < Scale) && (tempposy > -Scale)) && ((tempposz < Scale) && (tempposz > -Scale)))
  226.    {
  227.       return 1;
  228.    }
  229.    return 0;
  230. }
  231.  
  232. stock GetPosFromView(playerid, Float:Distance, &Float:X, &Float:Y, &Float:Z)
  233. {
  234.     new Float:cx,
  235.         Float:cy,
  236.         Float:cz,
  237.         Float:fx,
  238.         Float:fy,
  239.         Float:fz;
  240.     GetPlayerCameraPos(playerid,cx,cy,cz);
  241.     GetPlayerCameraFrontVector(playerid,fx,fy,fz);
  242.     X = fx * Distance + cx;
  243.     Y = fy * Distance + cy;
  244.     Z = fz * Distance + cz;
  245.     return 1;
  246. }
  247.  
  248. stock split(const strsrc[], strdest[][], delimiter)
  249. {
  250.     new i, li;
  251.     new aNum;
  252.     new len;
  253.     while(i <= strlen(strsrc))
  254.     {
  255.         if(strsrc[i] == delimiter || i == strlen(strsrc))
  256.         {
  257.             len = strmid(strdest[aNum], strsrc, li, i, 128);
  258.             strdest[aNum][len] = 0;
  259.             li = i+1;
  260.             aNum++;
  261.         }
  262.         i++;
  263.     }
  264.     return 1;
  265. }
  266.  
  267. stock wait(time)
  268. {
  269.     new stamp = tickcount();
  270.     while (tickcount() - stamp < time){}
  271.     return 1;
  272. }
  273.  
  274. stock GetPlayerClosestCow(playerid)
  275. {
  276.     new cid = -1,
  277.         Float:distance = 69000.0,
  278.         Float:tmp[7];
  279.     GetPlayerPos(playerid,tmp[0],tmp[1],tmp[2]);
  280.     for(new id = 0; id < MAX_COWS; id++)
  281.     {
  282.         if(!IsCowConnected(id)) continue;
  283.         if(CowInfo[id][IsDead]) continue;
  284.         tmp[3] = CowInfo[id][sX];
  285.         tmp[4] = CowInfo[id][sY];
  286.         tmp[5] = CowInfo[id][sZ];
  287.         tmp[6] = GetDistanceBetweenCoords(tmp[0],tmp[1],tmp[2],tmp[3],tmp[4],tmp[5]);
  288.         if(distance < tmp[6]) continue;
  289.         distance = tmp[6];
  290.         cid = id;
  291.     }
  292.     return cid;
  293. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement