HLMOD.HU Forrás Megtekintés - www.hlmod.hu
  1. #include <amxmodx>
  2. #include <fakemeta>
  3. #include <xs>
  4.  
  5. #define PLUGIN "Lie Flat"
  6. #define VERSION "1.1"
  7. #define AUTHOR "Nomexous"
  8.  
  9. /*
  10.  
  11. Version 1.0
  12.  - Initial release
  13.  
  14. Version 1.1
  15.  - Fixed error message when 0 was passed as the weapon entity. (Reported by Voi)
  16.  
  17. */
  18.  
  19. public plugin_init()
  20. {
  21. register_plugin(PLUGIN, VERSION, AUTHOR)
  22.  
  23. register_forward(FM_Touch, "fw_touch")
  24. }
  25.  
  26. public fw_touch(touched, weapon)
  27. {
  28. if (!pev_valid(weapon)) return FMRES_IGNORED
  29.  
  30. static class[32]
  31. pev(weapon, pev_classname, class, 31)
  32.  
  33. if (equal(class, "weaponbox") || equal(class, "weapon_shield") || equal(class, "grenade") || equal(class, "item_thighpack"))
  34. {
  35. lie_flat(weapon)
  36. }
  37.  
  38. return FMRES_IGNORED
  39. }
  40.  
  41. stock lie_flat(ent)
  42. {
  43. // If the entity is not on the ground, don't bother continuing.
  44. if (pev(ent, pev_flags) & ~FL_ONGROUND) return
  45.  
  46. // I decided to make all the variables static; suprisingly, the touch function can be called upwards of 5 times per drop.
  47. // I dunno why, but I suspect it's because the item "skips" on the ground.
  48. static Float:origin[3], Float:traceto[3], trace = 0, Float:fraction, Float:angles[3], Float:angles2[3]
  49.  
  50. pev(ent, pev_origin, origin)
  51. pev(ent, pev_angles, angles)
  52.  
  53. // We want to trace downwards 10 units.
  54. xs_vec_sub(origin, Float:{0.0, 0.0, 10.0}, traceto)
  55.  
  56. engfunc(EngFunc_TraceLine, origin, traceto, IGNORE_MONSTERS, ent, trace)
  57.  
  58. // Most likely if the entity has the FL_ONGROUND flag, flFraction will be less than 1.0, but we need to make sure.
  59. get_tr2(trace, TR_flFraction, fraction)
  60. if (fraction == 1.0) return
  61.  
  62. // Normally, once an item is dropped, the X and Y-axis rotations (aka roll and pitch) are set to 0, making them lie "flat."
  63. // We find the forward vector: the direction the ent is facing before we mess with its angles.
  64. static Float:original_forward[3]
  65. angle_vector(angles, ANGLEVECTOR_FORWARD, original_forward)
  66.  
  67. // If your head was an entity, no matter which direction you face, these vectors would be sticking out of your right ear,
  68. // up out the top of your head, and forward out from your nose.
  69. static Float:right[3], Float:up[3], Float:fwd[3]
  70.  
  71. // The plane's normal line will be our new ANGLEVECTOR_UP.
  72. get_tr2(trace, TR_vecPlaneNormal, up)
  73.  
  74. // This checks to see if the ground is flat. If it is, don't bother continuing.
  75. if (up[2] == 1.0) return
  76.  
  77. // The cross product (aka vector product) will give us a vector, which is in essence our ANGLEVECTOR_RIGHT.
  78. xs_vec_cross(original_forward, up, right)
  79. // And this cross product will give us our new ANGLEVECTOR_FORWARD.
  80. xs_vec_cross(up, right, fwd)
  81.  
  82. // Converts from the forward vector to angles. Unfortunately, vectors don't provide enough info to determine X-axis rotation (roll),
  83. // so we have to find it by pretending our right anglevector is a forward, calculating the angles, and pulling the corresponding value
  84. // that would be the roll.
  85. vector_to_angle(fwd, angles)
  86. vector_to_angle(right, angles2)
  87.  
  88. // Multiply by -1 because pitch increases as we look down.
  89. angles[2] = -1.0 * angles2[0]
  90.  
  91. // Finally, we turn our entity to lie flat.
  92. set_pev(ent, pev_angles, angles)
  93. }
  94.