Advertisement
Guest User

"vla.inc" V1 - iPLEOMAX

a guest
Jan 12th, 2012
331
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 5.54 KB | None | 0 0
  1.  
  2. /******************************************************************************/
  3. /******** OnPlayerLookAtVehicle Include by iPLEOMAX, (c) 2012. ****************/
  4. /******** Credit me, if you use anything from here.            ****************/
  5. /******** Thanks to:                                           ****************/
  6. /******** JernejL - DistanceCameraTargetToLocation.            ****************/
  7. /******** CracK - Trignometry Help (because of his post).      ****************/
  8. /******** Thank you for Downloading! ;)                        ****************/
  9. /******************************************************************************/
  10.  
  11. #if defined _inc_vlook
  12.     #endinput
  13. #endif
  14.  
  15. #define _inc_vlook
  16.  
  17. #include <a_samp>
  18.  
  19. #define VLA_UPDATE_RATE 200 //in Milliseconds.
  20. #define MAX_VLA_DISTANCE 200 //in San Andreas Map Unit
  21.  
  22. //native EnableVehicleLookAt(forplayerid);
  23. //native DisableVehicleLookAt(forplayerid);
  24. //native Callback - OnPlayerLookAtVehicle(playerid, oldvehicle, newvehicle);
  25.  
  26. new
  27.     bool:VLA_ENABLED[MAX_PLAYERS],
  28.     bool:VEH_ON_SIGHT[MAX_PLAYERS],
  29.     VLA_CURRENT[MAX_PLAYERS],
  30.     VLA_TEMP[MAX_PLAYERS],
  31.     VLA_Timer,
  32.    
  33.     Float:VLA_VX[MAX_VEHICLES],
  34.     Float:VLA_VY[MAX_VEHICLES],
  35.     Float:VLA_VZ[MAX_VEHICLES],
  36.    
  37.     Float:VLA_Dis[MAX_PLAYERS],
  38.     Float:VLA_PX[MAX_PLAYERS],
  39.     Float:VLA_PY[MAX_PLAYERS],
  40.     Float:VLA_PZ[MAX_PLAYERS]
  41. ;
  42.  
  43. public OnPlayerDisconnect(playerid, reason)
  44. {
  45.     VEH_ON_SIGHT[playerid] = false;
  46.     VLA_CURRENT[playerid] = 0;
  47.     VLA_TEMP[playerid] = 0;
  48.     VLA_Dis[playerid] = 0;
  49.     VLA_PX[playerid] = 0;
  50.     VLA_PY[playerid] = 0;
  51.     VLA_PZ[playerid] = 0;
  52.     if (funcidx("vla_OnPlayerDisconnect") != -1) return CallLocalFunction("vla_OnPlayerDisconnect", "ii", playerid, reason);
  53.     return true;
  54. }
  55.  
  56. #if defined _ALS_OnPlayerDisconnect
  57.     #undef OnPlayerDisconnect
  58. #else
  59.     #define _ALS_OnPlayerDisconnect
  60. #endif
  61. #define OnPlayerDisconnect vla_OnPlayerDisconnect
  62.  
  63. public OnFilterScriptInit()
  64. {
  65.     printf("  * OnPlayerLookAtVehicle (c) iPLEOMAX 2012.");
  66.     VLA_Timer = SetTimer("ScanVehicleLookAt", VLA_UPDATE_RATE, true);
  67.     if (funcidx("vla_OnFilterScriptInit") != -1) return CallLocalFunction("vla_OnFilterScriptInit", "");
  68.     return true;
  69. }
  70.  
  71. #if defined _ALS_OnFilterScriptInit
  72.     #undef OnFilterScriptInit
  73. #else
  74.     #define _ALS_OnFilterScriptInit
  75. #endif
  76. #define OnFilterScriptInit vla_OnFilterScriptInit
  77.  
  78. public OnFilterScriptExit()
  79. {
  80.     KillTimer(VLA_Timer);
  81.     if (funcidx("vla_OnFilterScriptExit") != -1) return CallLocalFunction("vla_OnFilterScriptExit", "");
  82.     return true;
  83. }
  84.  
  85. #if defined _ALS_OnFilterScriptExit
  86.     #undef OnFilterScriptExit
  87. #else
  88.     #define _ALS_OnFilterScriptExit
  89. #endif
  90. #define OnFilterScriptExit vla_OnFilterScriptExit
  91.  
  92.  
  93. forward ScanVehicleLookAt();
  94. public ScanVehicleLookAt()
  95. {
  96.     for(new i=0; i<MAX_PLAYERS; i++)
  97.     {
  98.         if(!IsPlayerConnected(i)) continue;
  99.        
  100.         VEH_ON_SIGHT[i] = false;
  101.         if(VLA_ENABLED[i])
  102.         {
  103.             VLA_Dis[i] = MAX_VLA_DISTANCE;
  104.             for(new v=0; v<MAX_VEHICLES; v++)
  105.             {
  106.                 if(!GetVehicleModel(v)) continue;
  107.                 if(GetPlayerVehicleID(i) == v) continue;
  108.                 if(!IsVehicleStreamedIn(v, i)) continue;
  109.                
  110.                 GetVehiclePos(v, VLA_VX[v],  VLA_VY[v],  VLA_VZ[v]);
  111.                 GetPlayerPos(i, VLA_PX[i], VLA_PY[i], VLA_PZ[i]);
  112.                
  113.                 new Float:radius = 2.0, Float:Distance =
  114.                 floatround
  115.                 (
  116.                     floatsqroot
  117.                     (
  118.                         floatpower(floatabs(floatsub(VLA_PX[i],VLA_VX[v])),2)+
  119.                         floatpower(floatabs(floatsub(VLA_PY[i],VLA_VY[v])),2)+
  120.                         floatpower(floatabs(floatsub(VLA_PZ[i],VLA_VZ[v])),2)
  121.                     )
  122.                 )
  123.                 ;
  124.  
  125.                 if (Distance > MAX_VLA_DISTANCE) continue;
  126.                 if (Distance < 5) radius = 3.5;
  127.                
  128.                 if(VehicleAimed(i, v, radius))
  129.                 {
  130.                     VEH_ON_SIGHT[i] = true;
  131.                     if(Distance < VLA_Dis[i]) VLA_TEMP[i] = v;
  132.                     VLA_Dis[i] = Distance;
  133.                 }
  134.             }
  135.         }
  136.        
  137.         switch(VEH_ON_SIGHT[i])
  138.         {
  139.             case true: {
  140.                 if(VLA_TEMP[i] != VLA_CURRENT[i])
  141.                 {
  142.                     CallLocalFunction("OnPlayerLookAtVehicle", "iii", i, VLA_CURRENT[i], VLA_TEMP[i]);
  143.                     VLA_CURRENT[i] = VLA_TEMP[i];
  144.                 }
  145.             }
  146.             case false: {
  147.                 if(VLA_CURRENT[i]) CallLocalFunction("OnPlayerLookAtVehicle", "iii", i, VLA_CURRENT[i], 0);
  148.                 VLA_CURRENT[i] = 0;
  149.             }
  150.         }
  151.     }
  152.     return true;
  153. }
  154.  
  155. Float:DistanceCameraTargetToLocation(Float:CamX, Float:CamY, Float:CamZ,  Float:ObjX, Float:ObjY, Float:ObjZ,  Float:FrX, Float:FrY, Float:FrZ) {
  156.  
  157.     new Float:TGTDistance;
  158.     TGTDistance = floatsqroot((CamX - ObjX) * (CamX - ObjX) + (CamY - ObjY) * (CamY - ObjY) + (CamZ - ObjZ) * (CamZ - ObjZ));
  159.     new Float:tmpX, Float:tmpY, Float:tmpZ;
  160.     tmpX = FrX * TGTDistance + CamX;
  161.     tmpY = FrY * TGTDistance + CamY;
  162.     tmpZ = FrZ * TGTDistance + CamZ;
  163.     return floatsqroot((tmpX - ObjX) * (tmpX - ObjX) + (tmpY - ObjY) * (tmpY - ObjY) + (tmpZ - ObjZ) * (tmpZ - ObjZ));
  164. }
  165.  
  166. stock VehicleAimed(playerid, vehicleid, Float:radius)
  167. {
  168.     new
  169.         Float:CamX, Float:CamY, Float:CamZ,
  170.         Float:VecX, Float:VecY, Float:VecZ
  171.     ;
  172.    
  173.     GetPlayerCameraPos(playerid, CamX, CamY, CamZ);
  174.     GetPlayerCameraFrontVector(playerid, VecX, VecY, VecZ);
  175.     return (radius >= DistanceCameraTargetToLocation(CamX, CamY, CamZ,
  176.     VLA_VX[vehicleid],  VLA_VY[vehicleid],  VLA_VZ[vehicleid], VecX, VecY, VecZ));
  177. }
  178.  
  179. stock EnableVehicleLookAt(forplayerid) VLA_ENABLED[forplayerid] = true;
  180. stock DisableVehicleLookAt(forplayerid) VLA_ENABLED[forplayerid] = false;
  181. forward OnPlayerLookAtVehicle(playerid, oldvehicle, newvehicle);
  182.  
  183. /*
  184. native EnableVehicleLookAt(forplayerid);
  185. native DisableVehicleLookAt(forplayerid);
  186. native Callback - OnPlayerLookAtVehicle(playerid, oldvehicle, newvehicle);
  187. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement