Advertisement
Guest User

[SAMP] Deer Hunting by Dinnozor - translated version

a guest
Dec 25th, 2012
761
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 19.62 KB | None | 0 0
  1. #include <OPVD>
  2.  
  3. #define MAX_ANIM 20//Define here the maximum number of deers (if one dies, another will spawn). For example, here there will always be 20 deers near LS
  4.  
  5. //Function prototypes and variables
  6.  
  7. forward MovingDeer(DeerID);
  8. forward DestroyDeer(DeerID);
  9. forward FinDeer(DeerID,playerid);// Function to pick up the deer meat
  10.  
  11. new Deers[MAX_ANIM],DeerKO[MAX_ANIM],DeerObject[MAX_ANIM];
  12. new DeerMeat[MAX_ANIM],InfectedDeer[MAX_ANIM],DeerMove[MAX_ANIM];
  13.  
  14.  
  15. //Fonction MapAndreas Include to get the Z of each X Y
  16.  
  17. stock Float: GetPointZPos(const Float: fX, const Float: fY, &Float: fZ = 0.0)
  18. {
  19.         if(!((-3000.0 < fX < 3000.0) && (-3000.0 < fY < 3000.0)))
  20.         {
  21.                 return 0.0;
  22.                
  23.         }
  24.         static
  25.         File: s_hMap
  26.         ;
  27.         if(!s_hMap)
  28.         {
  29.                 s_hMap = fopen("SAfull.hmap", io_read);
  30.                
  31.                 if(!s_hMap)
  32.                 {
  33.                         return 0.0;
  34.                        
  35.                 }
  36.                
  37.         }
  38.         new
  39.         afZ[1]
  40.         ;
  41.         fseek(s_hMap, ((6000 * (-floatround(fY, floatround_tozero) + 3000) + (floatround(fX, floatround_tozero) + 3000)) << 1));
  42.         fblockread(s_hMap, afZ);
  43.        
  44.         return (fZ = ((afZ[0] >>> 16) * 0.01));
  45. }
  46.  
  47.  
  48. //IsPlayerAimingObject lets you know if you are aiming at an object
  49.  
  50. stock IsPlayerAimingObject(playerid, aimid)
  51. {
  52.         new Float:X1, Float:Y1, Float:Z1, Float:X2, Float:Y2, Float:Z2;
  53.         GetPlayerPos(playerid, X1, Y1, Z1);
  54.         GetDynamicObjectPos(aimid, X2, Y2, Z2);
  55.         new Float:Dist = floatsqroot(floatpower(floatabs(X1-X2), 2) + floatpower(floatabs(Y1-Y2), 2));
  56.         if(Dist < 100)
  57.        
  58.         {
  59.                 new Float:A;
  60.                 GetPlayerFacingAngle(playerid, A);
  61.                 X1 += (Dist * floatsin(-A, degrees));
  62.                 Y1 += (Dist * floatcos(-A, degrees));
  63.                 Dist = floatsqroot(floatpower(floatabs(X1-X2), 2) + floatpower(floatabs(Y1-Y2), 2));
  64.                 if(Dist < 0.5)
  65.                
  66.                 {
  67.                         return true;
  68.                        
  69.                 }
  70.                
  71.         }
  72.         return false;
  73. }
  74.  
  75.  
  76. //Functions
  77.  
  78. stock CreateDeer(Float:X,Float:Y)
  79. {
  80.         new DeerID=-1;
  81.         for (new i=0;i<MAX_ANIM;i++)
  82.         {
  83.                 if(Deers[i]==0)
  84.                 {
  85.                         DeerID=i;
  86.                         i=MAX_ANIM;
  87.                        
  88.                 }
  89.                
  90.         }
  91.         if(DeerID!=-1)
  92.         {
  93.                 new Float:Z;
  94.                 GetPointZPos(X,Y,Z);
  95.                 DeerObject[DeerID]=CreateDynamicObject(19315,X,Y,Z+1,0,0,0,-1,-1,-1,300);
  96.                 Deers[DeerID]=1;
  97.                 DeerMeat[DeerID]=5+random(10);
  98.                 DeerKO[DeerID]=0;
  99.                 MovingDeer(DeerID);
  100.                 if(random(100)<3) InfectedDeer[DeerID]=1;//Here it will "infect" 3% of the deers. If you don't want any "crazy" deer, just replace these lines with "InfectedDeer[DeerID]=0;". You can also change the 3%, just replace the 3...
  101.                 else InfectedDeer[DeerID]=0;
  102.                
  103.         }
  104. }
  105.  
  106. public DestroyDeer(DeerID)
  107. {
  108.         Deers[DeerID]=0;
  109.         DestroyDynamicObject(DeerObject[DeerID]);
  110.         return 1;
  111. }
  112.  
  113. public MovingDeer(DeerID)
  114. {
  115.         if(DeerKO[DeerID]==0&&DeerMove[DeerID]==0)
  116.         {
  117.                 new Float:X,Float:Y,Float:Z,Float:Xrand,Float:Yrand,Float:Zrand,Float:speedRand,Float:Angle,Float:coef,Float:X2,Float:Y2;
  118.                 GetDynamicObjectPos(DeerObject[DeerID],X,Y,Z);
  119.                 new rand1=random(2),rand2=random(2);
  120.                 if(rand1==0) Xrand=X+10+float(random(10));
  121.                 else Xrand=X-10-float(random(10));
  122.                 if(rand2==0) Yrand=Y+10+float(random(10));
  123.                 else Yrand=Y-10-float(random(10));
  124.                 Zrand=GetPointZPos(Xrand,Yrand,Zrand);
  125.                 speedRand=7+float(random(9));
  126.                 Angle=atan((Yrand-Y)/(Xrand-X));
  127.                
  128.                 if((Zrand-Z)<2.0||(Z-Zrand)<2.0)
  129.                 {
  130.                         if(IsPosInDeerZone(Xrand,Yrand))
  131.                         {
  132.                                 GetPointZPos(Xrand,Yrand,Zrand);
  133.                                 SetDynamicObjectRot(DeerObject[DeerID],0,0,Angle);
  134.                                 MoveDynamicObject(DeerObject[DeerID],Xrand,Yrand,Zrand+0.3,speedRand,0,0,Angle);
  135.                                 DeerMove[DeerID]=1;
  136.                                 SetTimerEx("MovingDeer",10000+random(40000),false,"i",DeerID);//On peut changer la fréquence des mouvements
  137.                                
  138.                         }
  139.                         else MovingDeer(DeerID);
  140.                        
  141.                 }
  142.                 else MovingDeer(DeerID);
  143.                
  144.         }
  145.         return 1;
  146. }
  147.  
  148. stock IsPosInDeerZone(Float:X,Float:Y)//Areas where the deers can walk/run ; they will not exit them. It is easy to add some areas : take the coords Xmax, Xmin, Ymax and Ymin of the area, and you are done. Note : These zones are around Los Santos, there are none around LV or SF so just add some if you want to.
  149. {
  150.         if(X>-1590.561645&&X<-395.603057&&Y<-816.069274&&Y>-1717.792480) return 1;
  151.         else if(X>-689.845947&&X<197.757766&&Y<138.800338&&Y>-258.169403) return 1;
  152.         else if(X>567.683715&&X<1504.512084&&Y<376.543304&&Y>-37.922641) return 1;
  153.         else if(X>1787.830322&&X<2753.750976&&Y<-310.041870&&Y>-954.100341) return 1;
  154.         else if(X>-2527.260498&&X<-1637.774780&&Y<-1732.938232&&Y>-2647.622802) return 1;
  155.         else return 0;
  156. }
  157.  
  158.  
  159. //Spawn Locations for deers. Of course, if you add areas above, add at least a spawning location for every added area, or the deers will never be in them... Also remind to change the '21' with the actual number of locations after modification.
  160.  
  161. new Float:DeerSpawn[21][3]=
  162. {
  163.        
  164.         {
  165.                 -604.803588,-1308.452636,22.106567
  166.         }
  167.         ,
  168.        
  169.         {
  170.                 -702.036621,-1309.305297,63.694377
  171.         }
  172.         ,
  173.        
  174.         {
  175.                 -630.591491,-889.400268,108.452827
  176.         }
  177.         ,
  178.        
  179.         {
  180.                 -992.249938,-930.179138,129.602951
  181.         }
  182.         ,
  183.        
  184.         {
  185.                 -935.597717,-1146.799194,129.202728
  186.         }
  187.         ,
  188.        
  189.         {
  190.                 -394.224884,87.232124,28.368480
  191.         }
  192.         ,
  193.        
  194.         {
  195.                 -636.897583,-92.761047,64.660194
  196.         }
  197.         ,
  198.        
  199.         {
  200.                 -2447.039794,-2192.797851,28.498489
  201.         }
  202.         ,
  203.        
  204.         {
  205.                 -2224.478759,-2367.201416,32.494483
  206.         }
  207.         ,
  208.        
  209.         {
  210.                 -1803.133300,-2423.008789,26.044141
  211.         }
  212.         ,
  213.        
  214.         {
  215.                 -1690.161499,-2070.091308,42.189098
  216.         }
  217.         ,
  218.        
  219.         {
  220.                 2006.325439,-766.786865,131.285568
  221.         }
  222.         ,
  223.        
  224.         {
  225.                 -1817.458251,-1867.854980,86.802558
  226.         }
  227.         ,
  228.        
  229.         {
  230.                 2176.190429,-897.414916,84.565956
  231.         }
  232.         ,
  233.        
  234.         {
  235.                 2647.276855,-840.732543,70.997520
  236.         }
  237.         ,
  238.        
  239.         {
  240.                 2601.860351,-436.280792,73.739181
  241.         }
  242.         ,
  243.        
  244.         {
  245.                 2365.430664,-356.828186,59.507225
  246.         }
  247.         ,
  248.        
  249.         {
  250.                 927.202453,-10.505444,91.440376
  251.         }
  252.         ,
  253.        
  254.         {
  255.                 1092.987304,15.023153,68.779556
  256.         }
  257.         ,
  258.        
  259.         {
  260.                 1504.512084,192.802429,22.368137
  261.         }
  262.         ,
  263.        
  264.         {
  265.                 1083.728637,362.787322,25.921319
  266.         }
  267. }
  268. ;
  269.  
  270.  
  271. //OnGameModeInit - spawn the deers. If you've added some spawning locations, don't forget to change the '21' in the random(21)
  272.  
  273. for(new i=0;i<MAX_ANIM;i++)
  274. {
  275.         new rand=random(21);
  276.         new Float:X=DeerSpawn[rand][0];
  277.         new Float:Y=DeerSpawn[rand][1];
  278.         CreateDeer(X,Y);
  279.        
  280. }
  281.  
  282.  
  283. //OnPlayerUpdate - or equivalent function. But don't forget that the more you refresh the updates (OnPlayerUpdate is the fastest, I guess), the more accurate the script will be
  284.  
  285. for(new i=0;i<MAX_ANIM;i++)
  286. {
  287.         if(IsValidDynamicObject(DeerObject[i])&&Deers[i]>0)
  288.         {
  289.                 new Float:xd,Float:yd,Float:zd;
  290.                 GetDynamicObjectPos(DeerObject[i],xd,yd,zd);
  291.                 if(IsPlayerInAnyVehicle(playerid))
  292.                 {
  293.                         if(GetVehicleCategory(GetPlayerVehicleID(playerid))!=21)
  294.                         {
  295.                                 if(IsPlayerInRangeOfPoint(playerid,25,xd,yd,zd))
  296.                                 {
  297.                                         MovingDeer(i);
  298.                                        
  299.                                 }
  300.                                
  301.                         }
  302.                         else
  303.                         {
  304.                                 if(IsPlayerInRangeOfPoint(playerid,10,xd,yd,zd))
  305.                                 {
  306.                                         MovingDeer(i);
  307.                                        
  308.                                 }
  309.                                
  310.                         }
  311.                        
  312.                 }
  313.                 else if(IsPlayerInRangeOfPoint(playerid,2,xd,yd,zd)&&InfectedDeer[i]==1&&DeerKO[i]==0&&random(15)==0)
  314.                 {
  315.                         new Float:phealth;
  316.                         GetPlayerHealth(playerid,phealth);
  317.                         SetPlayerHealth(playerid,floatround(phealth-1,floatround_round));
  318.                         OnePlayAnim(playerid,"ped","HIT_wall",4,0,1,1,0,0);
  319.                         GameTextForPlayer(playerid,"A deer is attacking you !",3000,6);
  320.                        
  321.                 }
  322.                 else
  323.                 {
  324.                         if(IsPlayerInRangeOfPoint(playerid,15,xd,yd,zd))
  325.                         {
  326.                                
  327.                                 if(InfectedDeer[i]==1&&DeerKO[i]==0&&DeerMove[i]==0)
  328.                                 {
  329.                                         //Concerne les cerfs infectés, eh eh
  330.                                         new Float:xp,Float:yp,Float:zp;
  331.                                         GetPlayerPos(playerid,xp,yp,zp);
  332.                                         GetPointZPos(xp,yp,zp);
  333.                                         if(IsPosInDeerZone(xp,yp))
  334.                                         {
  335.                                                 DeerMove[i]=1;
  336.                                                 switch(random(2))
  337.                                                 {
  338.                                                         case 0:MoveDynamicObject(DeerObject[i],xp-1,yp,zp+0.3,8,0,0,atan((yp-yd)/(xp-xd)));
  339.                                                         case 1:MoveDynamicObject(DeerObject[i],xp,yp-1,zp+0.3,10,0,0,atan((yp-yd)/(xp-xd)));
  340.                                                        
  341.                                                 }
  342.                                                
  343.                                         }
  344.                                         else MovingDeer(i);
  345.                                        
  346.                                 }
  347.                                 else MovingDeer(i);
  348.                                
  349.                         }
  350.                        
  351.                 }
  352.                
  353.         }
  354.        
  355. }
  356.  
  357. //OnPlayerShoot callback. Still in the OnPlayerUpdate ;)
  358. new iCurWeap = GetPlayerWeapon(playerid); // Return the player's current weapon
  359. new iCurAmmo = GetPlayerAmmo(playerid);
  360. if(iCurWeap==GetPVarInt(playerid,"iCurrentWeapon")&&iCurAmmo!=GetPVarInt(playerid,"iCurrentAmmo"))
  361. {
  362.         OnPlayerShoot(playerid,GetPVarInt(playerid, "iCurrentAmmo"), iCurAmmo,iCurWeap);
  363.         SetPVarInt(playerid,"iCurrentAmmo",iCurAmmo);
  364.        
  365. }
  366.  
  367. //Now you are out of the OnPlayerUpdate callback. Add this one
  368. stock OnPlayerShoot(playerid,oldammo,newammo,weapon)
  369. {
  370.         for (new i=0;i<MAX_ANIM;i++)
  371.         {
  372.                 new Float:X,Float:Y,Float:Z,Float:rx,Float:ry,Float:rz,Float:Z2;
  373.                 GetDynamicObjectPos(DeerObject[i],X,Y,Z);
  374.                 if(IsPlayerAimingObject(playerid,DeerObject[i]))
  375.                 {
  376.                         GetDynamicObjectRot(DeerObject[i],rx,ry,rz);
  377.                         GetPointZPos(X,Y,Z2);
  378.                         MoveDynamicObject(DeerObject[i],X+0.1,Y,Z2+0.15,0.3,90,0,rz);
  379.                         switch(GetPlayerWeapon(playerid))
  380.                         {
  381.                                 case 22,23,33,34:DeerMeat[i]--;
  382.                                 case 25..27:DeerMeat[i]-=3;
  383.                                 default:DeerMeat[i]-=2;
  384.                                
  385.                         }
  386.                         DeerKO[i]=1;
  387.                        
  388.                 }
  389.                 else
  390.                 {
  391.                         if(IsPlayerInRangeOfPoint(playerid,45,X,Y,Z)) MovingDeer(i);//Shooting will frighten near deers
  392.                 }
  393.                
  394.         }
  395. }
  396.  
  397.  
  398. //OnPlayerCOmmandText - Let the admins respawn the deers
  399.  
  400. if (!strcmp(cmdtext,"/resetdeers",true))
  401.  
  402. {
  403.         if (AdminLvl[playerid]<1) SendClientMessage(playerid,COLOR_RED,"You are not allowed to use this command.");
  404.         else
  405.         {
  406.                 for(new i=0;i<MAX_ANIM;i++)
  407.                 {
  408.                         DestroyDeer(i);
  409.                         new rand=random(21);//If you've added spawn locations, change the '21',like you should have done before
  410.                         new Float:X=DeerSpawn[rand][0];
  411.                         new Float:Y=DeerSpawn[rand][1];
  412.                         CreateDeer(X,Y);
  413.                        
  414.                 }
  415.                
  416.         }
  417.         return 1;
  418.        
  419. }
  420.  
  421.  
  422. //OnPlayerKeyStateChange - let's get the meat !
  423.  
  424. if (GetPlayerState(playerid)==1)
  425. {
  426.         if(GetPlayerWeapon(playerid)==4||GetPlayerWeapon(playerid)==8||GetPlayerWeapon(playerid)==9)
  427.         {
  428.                 //Knive, katana, chainsaw
  429.                 if(HOLDING(KEY_FIRE))
  430.                 {
  431.                         for(new i=0;i<MAX_ANIM;i++)
  432.                         {
  433.                                 new Float:xd,Float:yd,Float:zd;
  434.                                 GetDynamicObjectPos(DeerObject[i],xd,yd,zd);
  435.                                 if(IsPlayerInRangeOfPoint(playerid,3,xd,yd,zd))
  436.                                 {
  437.                                         if(IsPlayerAimingObject(playerid,DeerObject[i]))
  438.                                         {
  439.                                                 if(DeerMeat[i]>0&&InfectedDeer[i]==0)
  440.                                                 {
  441.                                                         if(DeerKO[i]==0)
  442.                                                         {
  443.                                                                 new Float:X,Float:Y,Float:Z,Float:rx,Float:ry,Float:rz;
  444.                                                                 GetDynamicObjectPos(DeerObject[i],X,Y,Z);
  445.                                                                 GetDynamicObjectRot(DeerObject[i],rx,ry,rz);
  446.                                                                 MoveDynamicObject(DeerObject[i],X,Y,Z,90,ry,0);
  447.                                                                 DeerKO[i]=1;
  448.                                                                
  449.                                                         }
  450.                                                         else
  451.                                                         {
  452.                                                                 LoopingAnim(playerid,"BOMBER","BOM_Plant_Loop",4,1,1,1,0,DeerMeat[i]*1000);
  453.                                                                 GameTextForPlayer(playerid,"Picking up the meat...",3000,6);
  454.                                                                 SetTimerEx("FinDeer",1000,false,"ii",i,playerid);
  455.                                                                
  456.                                                                
  457.                                                         }
  458.                                                        
  459.                                                 }
  460.                                                 else
  461.                                                 {
  462.                                                         DestroyDeer(i);
  463.                                                         SendClientMessage(playerid,COLOR_GREEN,"You could not find any edible meat on this animal.");
  464.                                                         new rand=random(21);//if you've added any spawn locations, replace the 21
  465.                                                         new Float:X=DeerSpawn[rand][0];
  466.                                                         new Float:Y=DeerSpawn[rand][1];
  467.                                                         CreateDeer(X,Y);
  468.                                                        
  469.                                                 }
  470.                                                
  471.                                         }
  472.                                        
  473.                                 }
  474.                                
  475.                         }
  476.                        
  477.                 }
  478.                
  479.         }
  480. }
  481.  
  482. //function to take the meat
  483. public FinDeer(DeerID,playerid)
  484. {
  485.         new str[128];
  486.         PlayerInfo[playerid][Viande]++;//Add this variable to the PlayerInfo. 'viande' means 'meat' FYI ;)
  487.         DeerMeat[DeerID]--;
  488.         if(DeerMeat[DeerID]<1)
  489.         {
  490.                 DestroyDeer(DeerID);
  491.                 format(str,sizeof(str),"You picked up %dkg of meat.",PlayerInfo[playerid][Viande]);
  492.                 SendClientMessage(playerid,COLOR_GREEN,str);
  493.                 PlayerInfo[playerid][Viande]=0;
  494.                 new rand=random(21);//Replace the 21 if you've added locations...
  495.                 new Float:X=DeerSpawn[rand][0];
  496.                 new Float:Y=DeerSpawn[rand][1];
  497.                 CreateDeer(X,Y);
  498.                
  499.         }
  500.         else SetTimerEx("FinDeer",1000,false,"ii",DeerID,playerid);
  501.         return 1;
  502. }
  503.  
  504.  
  505. public OnDynamicObjectMoved(objectid)
  506. {
  507.         for(new i=0;i<MAX_ANIM;i++)
  508.         {
  509.                 if(IsValidDynamicObject(DeerObject[i]))
  510.                 {
  511.                         DeerMove[i]=0;
  512.                        
  513.                 }
  514.                
  515.         }
  516.         return 1;
  517. }
  518.  
  519. public OnPlayerVehicleDamage(playerid,vehicleid,Float:Damage)//To hurt the deers with vehicles
  520. {
  521.         for(new i=0;i<MAX_ANIM;i++)
  522.         {
  523.                 new Float:xd,Float:yd,Float:zd;
  524.                 GetDynamicObjectPos(DeerObject[i],xd,yd,zd);
  525.                 if(IsPlayerInRangeOfPoint(playerid,5,xd,yd,zd))
  526.                 {
  527.                         new Float:rx,Float:ry,Float:rz;
  528.                         GetDynamicObjectRot(DeerObject[i],rx,ry,rz);
  529.                         GetPointZPos(xd,yd,zd);
  530.                         MoveDynamicObject(DeerObject[i],xd+0.1,yd,zd+0.15,0.3,90,0,rz);
  531.                         DeerMeat[i]-=4;
  532.                         DeerKO[i]=1;
  533.                        
  534.                 }
  535.                
  536.         }
  537.         return 1;
  538. }
  539.  
  540.  
  541. //OnePLayAnim
  542.  
  543. OnePlayAnim(playerid,animlib[],animname[], Float:Speed, looping, lockx, locky, lockz, lp)
  544. {
  545.         ApplyAnimation(playerid, animlib, animname, Speed, looping, lockx, locky, lockz, lp,1);
  546. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement