Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. #teleport_behind_test.py
  2. import math
  3. from mathlib import Vector
  4. from commands.say import SayCommand
  5. from players.entity import Player
  6.  
  7. from entities import BaseEntityGenerator
  8. from engines.trace import engine_trace
  9. from engines.trace import ContentMasks
  10. from engines.trace import GameTrace
  11. from engines.trace import Ray
  12. from engines.trace import TraceFilterSimple
  13.  
  14.  
  15. @SayCommand('test')
  16. def test(command, index, teamOnly):
  17.     player = Player(index)
  18.     #message(player.origin)
  19.     target = player.get_view_player()
  20.     if target:
  21.         angles = target.get_view_angle()
  22.         angle = math.radians(target.get_view_angle()[1])  # (0, angle)
  23.         x = -40 * math.cos(angle)
  24.         y = -40 * math.sin(angle)
  25.  
  26.         new_position = Vector(
  27.             target.origin[0] + x,
  28.             target.origin[1] + y,
  29.             target.origin[2])
  30.  
  31.         # Check if there's enough space behind the target.
  32.         trace = check_space(new_position, player.mins, player.maxs)
  33.        
  34.         # Did the trace hit something solid?
  35.         if trace.did_hit():
  36.             # Increase the height(z) of the new position,
  37.             # in case the target was on sloped terrain.
  38.             new_position[2] += 20
  39.  
  40.             # Is there enough space now?
  41.             trace2 = check_space(new_position, player.mins, player.maxs)
  42.             if trace2.did_hit():
  43.                 # There's still something solid behind the target.
  44.                 # Could be a wall or some other object.
  45.                 return
  46.  
  47.         # teleport(position, rotation, velocity)
  48.         player.teleport(new_position, None, None)
  49.         player.set_view_angle(angles)
  50.  
  51.  
  52. def check_space(position, mins, maxs):
  53.     mask=ContentMasks.ALL
  54.     generator=BaseEntityGenerator
  55.     ray = Ray(position, position, mins, maxs)
  56.  
  57.     trace = GameTrace()
  58.     engine_trace.trace_ray(ray, mask, TraceFilterSimple(generator()), trace)
  59.     return trace