Advertisement
NmChris

Hitmarker

Aug 17th, 2018
928
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.95 KB | None | 0 0
  1. --------------------------------------------------------------------------------
  2. -- Caching common functions
  3. --------------------------------------------------------------------------------
  4. local client_set_event_callback, client_userid_to_entindex, entity_get_local_player, entity_hitbox_position, globals_curtime, globals_tickcount, math_sqrt, renderer_line, renderer_world_to_screen, pairs, ui_get, ui_new_checkbox, ui_new_color_picker, ui_new_slider, ui_set_callback, ui_set_visible = client.set_event_callback, client.userid_to_entindex, entity.get_local_player, entity.hitbox_position, globals.curtime, globals.tickcount, math.sqrt, renderer.line, renderer.world_to_screen, pairs, ui.get, ui.new_checkbox, ui.new_color_picker, ui.new_slider, ui.set_callback, ui.set_visible
  5.  
  6. --------------------------------------------------------------------------------
  7. -- Constants and variables
  8. --------------------------------------------------------------------------------
  9. local hitgroups = {
  10.     [1] = {0, 1},
  11.     [2] = {4, 5, 6},
  12.     [3] = {2, 3},
  13.     [4] = {13, 15, 16},
  14.     [5] = {14, 17, 18},
  15.     [6] = {7, 9, 11},
  16.     [7] = {8, 10, 12}
  17. }
  18.  
  19. local shot_data = {}
  20.  
  21. --------------------------------------------------------------------------------
  22. -- Menu handling
  23. --------------------------------------------------------------------------------
  24. local hit_marker        = ui_new_checkbox("LUA", "A", "Hit marker 3D")
  25. local hit_marker_size   = ui_new_slider("LUA", "A", "\n", 1, 10, 3, true, "px")
  26. local hit_marker_color  = ui_new_color_picker("LUA", "A", "Hit marker color", 255, 255, 255, 255)
  27.  
  28. local function handle_menu()
  29.     local state = ui_get(hit_marker)
  30.     ui_set_visible(hit_marker_size, state)
  31. end
  32.  
  33. handle_menu()
  34. ui_set_callback(hit_marker, handle_menu)
  35.  
  36. --------------------------------------------------------------------------------
  37. -- Game event handling
  38. --------------------------------------------------------------------------------
  39. local function paint()
  40.     if not ui_get(hit_marker) then
  41.         return
  42.     end
  43.     local size      = ui_get(hit_marker_size)
  44.     local r, g, b   = ui_get(hit_marker_color)
  45.     for tick, data in pairs(shot_data) do
  46.         if data.draw then
  47.             if globals_curtime() >= data.time then
  48.                 data.alpha = data.alpha - 1
  49.             end
  50.             if data.alpha <= 0 then
  51.                 data.draw = false
  52.             end
  53.             local sx, sy = renderer_world_to_screen(data.x, data.y, data.z)
  54.             if sx ~= nil then
  55.                 renderer_line(sx + size, sy + size, sx + (size * 2), sy + (size * 2), r, g, b, data.alpha)
  56.                 renderer_line(sx - size, sy + size, sx - (size * 2), sy + (size * 2), r, g, b, data.alpha)
  57.                 renderer_line(sx + size, sy - size, sx + (size * 2), sy - (size * 2), r, g, b, data.alpha)
  58.                 renderer_line(sx - size, sy - size, sx - (size * 2), sy - (size * 2), r, g, b, data.alpha)
  59.             end
  60.         end
  61.     end
  62. end
  63.  
  64. local function player_hurt(e)
  65.     if not ui_get(hit_marker) then
  66.         return
  67.     end
  68.     local victim_entindex   = client_userid_to_entindex(e.userid)
  69.     local attacker_entindex = client_userid_to_entindex(e.attacker)
  70.     if attacker_entindex ~= entity_get_local_player() then
  71.         return
  72.     end
  73.     local tick  = globals_tickcount()
  74.     local data  = shot_data[tick]
  75.     if shot_data[tick] == nil then
  76.         return
  77.     end
  78.     local impacts   = data.impacts
  79.     local hitboxes  = hitgroups[e.hitgroup]
  80.     local hit       = nil
  81.     local closest   = math.huge
  82.     for i=1, #impacts do
  83.         local impact = impacts[i]
  84.         for j=1, #hitboxes do
  85.             local x, y, z   = entity_hitbox_position(victim_entindex, hitboxes[j])
  86.             local distance  = math_sqrt((impact.x - x) ^ 2 + (impact.y - y) ^ 2 + (impact.z - z) ^ 2)
  87.             if distance < closest then
  88.                 hit     = impact
  89.                 closest = distance
  90.             end
  91.         end
  92.     end
  93.     shot_data[tick] = {
  94.         x       = hit.x,
  95.         y       = hit.y,
  96.         z       = hit.z,
  97.         time    = globals_curtime() + 2,
  98.         alpha   = 255,
  99.         draw    = true,
  100.     }
  101. end
  102.  
  103. local function bullet_impact(e)
  104.     if not ui_get(hit_marker) then
  105.         return
  106.     end
  107.     if client_userid_to_entindex(e.userid) ~= entity_get_local_player() then
  108.         return
  109.     end
  110.     local tick = globals_tickcount()
  111.     if shot_data[tick] == nil then
  112.         shot_data[tick] = {
  113.             impacts = {}
  114.         }
  115.     end
  116.     local impacts = shot_data[tick].impacts
  117.     impacts[#impacts + 1] = {
  118.         x = e.x,
  119.         y = e.y,
  120.         z = e.z
  121.     }
  122. end
  123.  
  124. local function round_start()
  125.     if not ui_get(hit_marker) then
  126.         return
  127.     end
  128.     shot_data = {}
  129. end
  130.  
  131. client_set_event_callback("paint", paint)
  132. client_set_event_callback("player_hurt", player_hurt)
  133. client_set_event_callback("round_start", round_start)
  134. client_set_event_callback("bullet_impact", bullet_impact)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement