Advertisement
Guest User

Draft bomb system

a guest
Aug 28th, 2012
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.16 KB | None | 0 0
  1. #include <Vectoral>
  2. #include <streamer>
  3. #include <MapAndreas>
  4. #include <YSI\y_hooks>
  5.  
  6. #define Blanco 0xFFFFFFFF
  7.  
  8. //==============================================================================
  9. // Key State
  10. //==============================================================================
  11. #define RELEASED(%0) \
  12.     (((newkeys & (%0)) != (%0)) && ((oldkeys & (%0)) == (%0)))
  13. #define PRESSED(%0) \
  14. (((newkeys & (%0)) == (%0)) && ((oldkeys & (%0)) != (%0)))
  15. #define HOLDING(%0) \
  16.     ((newkeys & (%0)) == (%0))
  17.  
  18. new VectorBombas;  
  19.    
  20. hook OnFilterScriptInit()
  21. {
  22.     VectorBombas = cvector_g();
  23. }  
  24.    
  25. hook OnPlayerKeyStateChange(playerid,newkeys,oldkeys)
  26. {
  27.     if(PRESSED(KEY_YES) && GetPlayerWeapon(playerid) == 40)
  28.     {
  29.         PlayerPlaySound(playerid, 6400, 0.0, 0.0, 0.0);
  30.         for(new i=0; i<cvector_g_size(VectorBombas); i++)
  31.         {
  32.             new MapaBomba = cvector_g_get(VectorBombas, i);
  33.             if(cmap_get(MapaBomba, "jugador") == playerid)
  34.             {
  35.                 KillTimer(cmap_get(MapaBomba, "timer"));
  36.                 DetonarBomba(MapaBomba);
  37.             }
  38.         }
  39.     }
  40.    
  41.     if(PRESSED(KEY_FIRE) && HOLDING(KEY_HANDBRAKE))
  42.     {
  43.         for(new i=0; i<cvector_g_size(VectorBombas); i++)
  44.         {
  45.             new MapaBomba = cvector_g_get(VectorBombas, i);
  46.             new objeto = cmap_get(MapaBomba, "objeto");
  47.             new Float:px, Float:py, Float:pz;
  48.             GetDynamicObjectPos(objeto, px, py, pz);
  49.             if(IsPlayerAimingAt(playerid, px, py, pz, 0.2))
  50.             {
  51.                 KillTimer(cmap_get(MapaBomba, "timer"));
  52.                 DetonarBomba(MapaBomba);
  53.             }
  54.         }
  55.     }
  56. }
  57.  
  58. hook OnPlayerCommandText(playerid, cmdtext[])
  59. {
  60.     new cmd[128];
  61.     new tmp[128];
  62.     new idx;
  63.     cmd = strtokex(cmdtext, idx);
  64.    
  65.     if(strcmp(cmd, "/plantbomb", true) == 0)
  66.     {
  67.         tmp = strtokex(cmdtext, idx);
  68.         if(!strlen(tmp) || !IsNumeric(tmp))
  69.         {
  70.             return SendClientMessage(playerid, Blanco, "USE: /plantbomb [Delay] [Car]");
  71.         }
  72.         new segundos = strval(tmp);
  73.        
  74.         tmp = strtokex(cmdtext, idx);
  75.         if(!strlen(tmp) || !IsNumeric(tmp))
  76.         {
  77.             return SendClientMessage(playerid, Blanco, "USE: /plantbomb [Delay] [Car]");
  78.         }
  79.         new coche = strval(tmp);
  80.        
  81.         new Float:px, Float:py, Float:pz;
  82.         GetPlayerPos(playerid, px, py, pz);
  83.         MapAndreas_FindZ_For2DCoord(px, py, pz);
  84.         pz = pz+0.4;
  85.         new MapaBomba = cmap();
  86.         cmap_insert_float(MapaBomba, "posX", px);
  87.         cmap_insert_float(MapaBomba, "posY", py);
  88.         cmap_insert_float(MapaBomba, "posZ", pz);
  89.         cmap_insert(MapaBomba, "objeto", CreateDynamicObject(363, px, py, pz, 0.0, 90.0, 0.0));
  90.         cmap_insert(MapaBomba, "jugador", playerid);
  91.         cvector_g_push_back(VectorBombas, MapaBomba);
  92.         if(coche > 0)
  93.         {
  94.             cmap_insert(MapaBomba, "coche", coche);
  95.             AttachDynamicObjectToVehicle(cmap_get(MapaBomba, "objeto"), coche, 0.0, 0.0, -0.6, 90.0, 0.0, 0.0);
  96.         }
  97.         cmap_insert(MapaBomba, "timer", SetTimerEx("DetonarBomba", segundos*1000, 0, "i", MapaBomba));
  98.         return 1;
  99.     }
  100.    
  101.     return 0;
  102. }
  103.  
  104. forward DetonarBomba(bombamap);
  105. public DetonarBomba(bombamap)
  106. {
  107.     new objeto = cmap_get(bombamap, "objeto");
  108.     new Float:px, Float:py, Float:pz;
  109.     new Float:pxa, Float:pya, Float:pza;
  110.     new coche = cmap_get(bombamap, "coche");
  111.     if(coche > 0)
  112.     {
  113.         GetVehicleVelocity(coche, pxa, pya, pza);
  114.         GetVehiclePos(coche, px, py, pz);
  115.     }
  116.     else
  117.     {
  118.         GetDynamicObjectPos(objeto, px, py, pz);
  119.     }
  120.     CreateExplosion(px+pxa*10, py+pya*10, pz+pza*10, 0, 12.0);
  121.     DestroyDynamicObject(cmap_get(bombamap, "objeto"));
  122.     cvector_g_remove_value(VectorBombas, bombamap);
  123.     for(new i=0; i<cvector_g_size(VectorBombas); i++)
  124.     {
  125.         new MapaBomba = cvector_g_get(VectorBombas, i);
  126.         new objetoa = cmap_get(MapaBomba, "objeto");
  127.         GetDynamicObjectPos(objetoa, pxa, pya, pza);
  128.         if(PointToPoint2D(8.0, px, py, pxa, pya))
  129.         {
  130.             KillTimer(cmap_get(MapaBomba, "timer"));
  131.             SetTimerEx("DetonarBomba", 500, 0, "i", MapaBomba);
  132.         }
  133.     }
  134. }
  135.  
  136. stock IsNumeric(stringer[]) { for (new i = 0, j = strlen(stringer); i < j; i++) if (stringer[i] > '9' || stringer[i] < '0') return 0; return 1; }
  137.  
  138. stock strtokex(const stringe[], &index)
  139. {
  140.     new length = strlen(stringe);
  141.     while ((index < length) && (stringe[index] <= ' '))
  142.     {
  143.         index++;
  144.     }
  145.  
  146.     new offset = index;
  147.     new result[20];
  148.     while ((index < length) && (stringe[index] > ' ') && ((index - offset) < (sizeof(result) - 1)))
  149.     {
  150.         result[index - offset] = stringe[index];
  151.         index++;
  152.     }
  153.     result[index - offset] = EOS;
  154.     return result;
  155. }
  156.  
  157. Float:DistanceCameraTargetToLocation(Float:CamX, Float:CamY, Float:CamZ, Float:ObjX, Float:ObjY, Float:ObjZ, Float:FrX, Float:FrY, Float:FrZ) {
  158.  
  159.     new Float:TGTDistance;
  160.  
  161.     TGTDistance = floatsqroot((CamX - ObjX) * (CamX - ObjX) + (CamY - ObjY) * (CamY - ObjY) + (CamZ - ObjZ) * (CamZ - ObjZ));
  162.  
  163.     new Float:tmpX, Float:tmpY, Float:tmpZ;
  164.  
  165.     tmpX = FrX * TGTDistance + CamX;
  166.     tmpY = FrY * TGTDistance + CamY;
  167.     tmpZ = FrZ * TGTDistance + CamZ;
  168.  
  169.     return floatsqroot((tmpX - ObjX) * (tmpX - ObjX) + (tmpY - ObjY) * (tmpY - ObjY) + (tmpZ - ObjZ) * (tmpZ - ObjZ));
  170. }
  171.  
  172. stock Float:GetPointAngleToPoint(Float:x2, Float:y2, Float:X, Float:Y) {
  173.  
  174.     new Float:DX, Float:DY;
  175.     new Float:angle;
  176.        
  177.     DX = floatabs(floatsub(x2,X));
  178.     DY = floatabs(floatsub(y2,Y));
  179.    
  180.     if (DY == 0.0 || DX == 0.0) {
  181.         if(DY == 0 && DX > 0) angle = 0.0;
  182.         else if(DY == 0 && DX < 0) angle = 180.0;
  183.         else if(DY > 0 && DX == 0) angle = 90.0;
  184.         else if(DY < 0 && DX == 0) angle = 270.0;
  185.         else if(DY == 0 && DX == 0) angle = 0.0;
  186.     }
  187.     else {
  188.         angle = atan(DX/DY);
  189.    
  190.         if(X > x2 && Y <= y2) angle += 90.0;
  191.         else if(X <= x2 && Y < y2) angle = floatsub(90.0, angle);
  192.         else if(X < x2 && Y >= y2) angle -= 90.0;
  193.         else if(X >= x2 && Y > y2) angle = floatsub(270.0, angle);
  194.     }          
  195.  
  196.     return floatadd(angle, 90.0);
  197. }
  198.  
  199. stock GetXYInFrontOfPoint(&Float:x, &Float:y, Float:angle, Float:distance) {
  200.     x += (distance * floatsin(-angle, degrees));
  201.     y += (distance * floatcos(-angle, degrees));
  202. }
  203.  
  204. stock IsPlayerAimingAt(playerid, Float:x, Float:y, Float:z, Float:radius) {
  205.     new Float:camera_x,Float:camera_y,Float:camera_z,Float:vector_x,Float:vector_y,Float:vector_z;
  206.     GetPlayerCameraPos(playerid, camera_x, camera_y, camera_z);
  207.     GetPlayerCameraFrontVector(playerid, vector_x, vector_y, vector_z);
  208.  
  209.     new Float:vertical, Float:horizontal;
  210.  
  211.     switch (GetPlayerWeapon(playerid)) {
  212.         case 34,35,36: {
  213.             if (DistanceCameraTargetToLocation(camera_x, camera_y, camera_z, x, y, z, vector_x, vector_y, vector_z) < radius) return true;
  214.             return false;
  215.         }
  216.         case 30,31: {vertical = 4.0; horizontal = -1.6;}
  217.         case 33: {vertical = 2.7; horizontal = -1.0;}
  218.         default: {vertical = 6.0; horizontal = -2.2;}
  219.     }
  220.  
  221.     new Float:angle = GetPointAngleToPoint(0, 0, floatsqroot(vector_x*vector_x+vector_y*vector_y), vector_z) - 270.0;
  222.     new Float:resize_x, Float:resize_y, Float:resize_z = floatsin(angle+vertical, degrees);
  223.     GetXYInFrontOfPoint(resize_x, resize_y, GetPointAngleToPoint(0, 0, vector_x, vector_y)+horizontal, floatcos(angle+vertical, degrees));
  224.    
  225.     if (DistanceCameraTargetToLocation(camera_x, camera_y, camera_z, x, y, z, resize_x, resize_y, resize_z) < radius) return true;
  226.     return false;
  227. }
  228.  
  229. stock PointToPoint2D(Float:radi, Float:x, Float:y, Float:x2, Float:y2)
  230. {
  231.     new Float:distance;
  232.     distance= floatsqroot( floatadd ( floatpower( floatsub ( x2 , x) , 2) , floatpower( floatsub ( y2 , y), 2) ) );
  233.    
  234.     if (distance < radi)
  235.     {
  236.         return 1;
  237.     }
  238.     return 0;
  239. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement