Advertisement
Ayuto

SPE Trace examples

May 17th, 2013
369
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.59 KB | None | 0 0
  1. '''
  2. # 16.05.13
  3. # UTIL_TraceLine(Vector  const&, Vector  const&, unsigned int, IHandleEntity  const*, int, CGameTrace *)
  4. [UTIL_TraceLine]
  5. shortname = TraceLine
  6. sig = 53 8B DC 83 EC 08 83 E4 F0 83 C4 04 55 8B 6B 04 89 6C 24 04 8B EC 83 EC 6C 56
  7. symbol = _Z14UTIL_TraceLineRK6VectorS1_jPK13IHandleEntityiP10CGameTrace
  8. param = ppipip)v
  9. convention = cdecl
  10.  
  11. # 17.05.13
  12. # CTraceFilterSimple::ShouldHitEntity(IHandleEntity *, int)
  13. [CTraceFilterSimple::ShouldHitEntity]
  14. shortname = ShouldHitEntity
  15. sig = 55 8B EC 8B 45 0C 53 8B 5D 08 57
  16. symbol = _ZN18CTraceFilterSimple15ShouldHitEntityEP13IHandleEntityi
  17. param = ppi)b
  18. convention = thiscall
  19. '''
  20.  
  21. # =============================================================================
  22. # >> IMPORTS
  23. # =============================================================================
  24. # EventScripts
  25. import es
  26. import playerlib
  27.  
  28. from vecmath import Vector
  29.  
  30. # Source-Python-Extensions
  31. import spe
  32.  
  33. from spe import HookAction
  34. from spe import HookType
  35.  
  36.  
  37. # =============================================================================
  38. # >> GLOBAL VARIABLES
  39. # =============================================================================
  40. SIZE_VECTOR     = 12
  41. SIZE_TRACE_T    = 84
  42. MAX_COORD_RANGE = 16384
  43.  
  44. spe.parseINI('signatures.ini')
  45.  
  46.  
  47. # =============================================================================
  48. # >> GAME EVENTS
  49. # =============================================================================
  50. def player_say(ev):
  51.     userid = int(ev['userid'])
  52.  
  53.     """
  54.    # enumerateEntities()
  55.    player   = playerlib.getPlayer(userid)
  56.    startvec = player.getEyeLocation()
  57.    endvec   = list(Vector(startvec) + Vector(player.viewvector) * \
  58.        MAX_COORD_RANGE)
  59.  
  60.    entities = enumerateEntities(startvec, endvec)
  61.    es.msg(map(lambda x: es.entitygetvalue(x, 'classname'), entities))
  62.    """
  63.  
  64.     # isWallBetween()
  65.     player = playerlib.getPlayer(userid)
  66.     for player2 in playerlib.getPlayerList('#alive'):
  67.         es.msg('Is wall between %s & %s? %s!'% (player.name, player2.name,
  68.             isWallBetween(player.getEyeLocation(), player2.getEyeLocation())))
  69.  
  70.  
  71. # =============================================================================
  72. # >> CLASSES
  73. # =============================================================================
  74. class EnumHelper:
  75.     entities = set()
  76.  
  77.     @staticmethod
  78.     def startTracing():
  79.         EnumHelper.entities.clear()
  80.         spe.detourFunction('ShouldHitEntity', HookType.Pre,
  81.             preShouldHitEntity)
  82.  
  83.     @staticmethod
  84.     def stopTracing():
  85.         spe.undetourFunction('ShouldHitEntity', HookType.Pre,
  86.             preShouldHitEntity)
  87.  
  88.         return EnumHelper.entities
  89.  
  90.  
  91. # =============================================================================
  92. # >> FUNCTIONS
  93. # =============================================================================
  94. def isWallBetween(start, end):
  95.     '''
  96.    Returns True if a wall is between the start and end vector.
  97.    '''
  98.  
  99.     # Create start and end vector pointers
  100.     pStart = createVector(*start)
  101.     pEnd   = createVector(*end)
  102.  
  103.     # Allocate space for the CGameTrace object
  104.     ptr = spe.alloc(SIZE_TRACE_T)
  105.  
  106.     # Call UTIL_TraceLine()
  107.     spe.call('TraceLine', pStart, pEnd, 0x1, 0, 0, ptr)
  108.  
  109.     # Get m_pEnt
  110.     result = bool(spe.getLocVal('i', ptr + 76))
  111.  
  112.     # Deallocate reserved space
  113.     spe.dealloc(pStart)
  114.     spe.dealloc(pEnd)
  115.     spe.dealloc(ptr)
  116.  
  117.     return result
  118.  
  119. def enumerateEntities(start, end, mask=0x1, collisiongroup=0):
  120.     '''
  121.    Returns all entity indexes between the start and end vector.
  122.    '''
  123.  
  124.     # Create start and end vector pointers
  125.     pStart = createVector(*start)
  126.     pEnd   = createVector(*end)
  127.  
  128.     # Allocate space for the CGameTrace object
  129.     ptr = spe.alloc(SIZE_TRACE_T)
  130.  
  131.     # Start the trace
  132.     EnumHelper.startTracing()
  133.  
  134.     # Call UTIL_TraceLine()
  135.     spe.call('TraceLine', pStart, pEnd, mask, 0, collisiongroup, ptr)
  136.  
  137.     # Stop the trace
  138.     result = EnumHelper.stopTracing()
  139.  
  140.     # Deallocate reserved space
  141.     spe.dealloc(pStart)
  142.     spe.dealloc(pEnd)
  143.     spe.dealloc(ptr)
  144.  
  145.     return result
  146.  
  147. def getViewCoords(userid, mask=0x1, collisiongroup=0):
  148.     '''
  149.    Returns the coordinates the given player is currently looking at.
  150.    '''
  151.  
  152.     player   = playerlib.getPlayer(userid)
  153.     startvec = player.getEyeLocation()
  154.  
  155.     # Create start and end vector pointers
  156.     pStart = createVector(*startvec)
  157.     pEnd   = createVector(*list(Vector(startvec) + Vector(player.viewvector) \
  158.         * MAX_COORD_RANGE))
  159.  
  160.     # Allocate space for the CGameTrace object
  161.     ptr = spe.alloc(SIZE_TRACE_T)
  162.  
  163.     # Call UTIL_TraceLine()
  164.     spe.call('TraceLine', pStart, pEnd, mask, spe.getPlayer(int(userid)),
  165.         collisiongroup, ptr)
  166.  
  167.     # Wrap the end vector...
  168.     x = spe.makeObject('Vector', ptr + 12)
  169.  
  170.     # ... and save the result
  171.     result = x.x, x.y, x.z
  172.  
  173.     # Deallocate reserved space
  174.     spe.dealloc(pStart)
  175.     spe.dealloc(pEnd)
  176.     spe.dealloc(ptr)
  177.  
  178.     return result
  179.  
  180. def getGroundMaterial(userid, mask=0x1, collisiongroup=0):
  181.     '''
  182.    Returns the name of the material on which the given player is currently
  183.    standing.
  184.    '''
  185.  
  186.     x, y, z = es.getplayerlocation(userid)
  187.  
  188.     # Create start and end vector pointers
  189.     pStart = createVector(x, y, z)
  190.     pEnd   = createVector(x, y, -MAX_COORD_RANGE)
  191.  
  192.     # Allocate space for the CGameTrace object
  193.     ptr = spe.alloc(SIZE_TRACE_T)
  194.  
  195.     # Call UTIL_TraceLine()
  196.     spe.call('TraceLine', pStart, pEnd, mask, 0, collisiongroup, ptr)
  197.  
  198.     # TODO: Explore IPhysicsSurfaceProps::GetSurfaceData() to get the material
  199.     # of **studio** or **displacement**
  200.     result = spe.getLocVal('s', spe.getLocVal('i', ptr + 60))
  201.  
  202.     # Deallocate reserved space
  203.     spe.dealloc(pStart)
  204.     spe.dealloc(pEnd)
  205.     spe.dealloc(ptr)
  206.  
  207.     return result
  208.  
  209. def createVector(x, y, z):
  210.     obj = spe.makeObject('Vector', spe.alloc(SIZE_VECTOR))
  211.     obj.x = x
  212.     obj.y = y
  213.     obj.z = z
  214.     return obj.base
  215.  
  216.  
  217. # =============================================================================
  218. # >> CALLBACKS
  219. # =============================================================================
  220. def preShouldHitEntity(args):
  221.     spe.setCallingConvention('thiscall')
  222.  
  223.     # Call IHandleEntity::GetRefEHandle() and read m_Index of the return
  224.     # value (CBaseHandle)
  225.     spe.setCallingConvention('thiscall')
  226.     handle = spe.getLocVal('i', spe.callFunction(spe.findVirtualFunc(
  227.         args[0], 2), 'p)p', (args[0],)))
  228.  
  229.     EnumHelper.entities.add(es.getindexfromhandle(handle))
  230.     return (HookAction.Continue, 0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement