Guest User

LUA Lag Compensation version 0.4

a guest
Nov 18th, 2010
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.99 KB | None | 0 0
  1. --LUA Lag Compensation version 0.3 by FlooD
  2. --thx to Jermuk for his custom server, which first encouraged me to make lag compensation.
  3. --thx to lasthope and def3ct for initial testing.
  4. --thx to 3rr0r for ideas and suggestions for refining the code.
  5.  
  6. parse("mp_damagefactor 0")
  7.  
  8. BUFFER_SIZE = 25
  9. frame = 0
  10.  
  11. buffer = {}
  12. buffer[1] = {} --x buffer
  13. buffer[2] = {} --y buffer
  14.  
  15. function clearbuffer(id)
  16.     buffer[1][id] = {}
  17.     buffer[2][id] = {}
  18. end
  19.  
  20. for i = 1, 32 do
  21.     clearbuffer(i)
  22. end
  23.  
  24. addhook("leave", "clearbuffer")
  25. addhook("die", "clearbuffer")
  26.  
  27. addhook("always", "updatebuffer")
  28. function updatebuffer()
  29.     frame = frame + 1
  30.     for i = 1, 32 do
  31.         buffer[1][i][frame], buffer[2][i][frame] = player(i, "x"), player(i, "y")
  32.         buffer[1][i][frame - BUFFER_SIZE], buffer[2][i][frame - BUFFER_SIZE] = nil, nil
  33.     end
  34. end
  35.  
  36. addhook("attack", "onattack")
  37. function onattack(id)
  38.     local wpn = player(id, "weapon")
  39.  
  40.     local rot = player(id, "rot")
  41.     local start_x = player(id, "x")
  42.     local start_y = player(id, "y")
  43.     local end_x = start_x + (3 * itemtype(wpn, "range")) * math.sin(math.rad(rot))
  44.     local end_y = start_y - (3 * itemtype(wpn, "range")) * math.cos(math.rad(rot))
  45.     local tile_x = math.floor(start_x / 32)
  46.     local tile_y = math.floor(start_y / 32)
  47.     --the following huge if statement will set tile_x and tile_y to the first wall the bullet hits.
  48.     if rot > 0 and rot < 90 then
  49.         while (not tile(tile_x, tile_y, "wall")) do
  50.             if intersect(start_x, start_y, end_x, end_y, topixel(tile_x + 1), topixel(tile_y), 32) then
  51.                 tile_x = tile_x + 1
  52.             elseif intersect(start_x, start_y, end_x, end_y, topixel(tile_x), topixel(tile_y - 1), 32) then
  53.                 tile_y = tile_y - 1
  54.             elseif intersect(start_x, start_y, end_x, end_y, topixel(tile_x + 1), topixel(tile_y - 1), 32) then
  55.                 tile_x, tile_y = tile_x + 1, tile_y - 1
  56.             else --unlikely or impossible, but here for safety.
  57.                 break
  58.             end
  59.         end
  60.     elseif rot > 90 and rot < 180 then
  61.         while (not tile(tile_x, tile_y, "wall")) do
  62.             if intersect(start_x, start_y, end_x, end_y, topixel(tile_x + 1), topixel(tile_y), 32) then
  63.                 tile_x = tile_x + 1
  64.             elseif intersect(start_x, start_y, end_x, end_y, topixel(tile_x), topixel(tile_y + 1), 32) then
  65.                 tile_y = tile_y + 1
  66.             elseif intersect(start_x, start_y, end_x, end_y, topixel(tile_x + 1), topixel(tile_y + 1), 32) then
  67.                 tile_x, tile_y = tile_x + 1, tile_y + 1
  68.             else
  69.                 break
  70.             end
  71.         end
  72.     elseif rot > -180 and rot < -90 then
  73.         while (not tile(tile_x, tile_y, "wall")) do
  74.             if intersect(start_x, start_y, end_x, end_y, topixel(tile_x - 1), topixel(tile_y), 32) then
  75.                 tile_x = tile_x - 1
  76.             elseif intersect(start_x, start_y, end_x, end_y, topixel(tile_x), topixel(tile_y + 1), 32) then
  77.                 tile_y = tile_y + 1
  78.             elseif intersect(start_x, start_y, end_x, end_y, topixel(tile_x - 1), topixel(tile_y + 1), 32) then
  79.                 tile_x, tile_y = tile_x - 1, tile_y + 1
  80.             else
  81.                 break
  82.             end
  83.         end
  84.     elseif rot > -90 and rot < 0 then
  85.         while (not tile(tile_x, tile_y, "wall")) do
  86.             if intersect(start_x, start_y, end_x, end_y, topixel(tile_x - 1), topixel(tile_y), 32) then
  87.                 tile_x = tile_x - 1
  88.             elseif intersect(start_x, start_y, end_x, end_y, topixel(tile_x), topixel(tile_y - 1), 32) then
  89.                 tile_y = tile_y - 1
  90.             elseif intersect(start_x, start_y, end_x, end_y, topixel(tile_x - 1), topixel(tile_y - 1), 32) then
  91.                 tile_x, tile_y = tile_x - 1, tile_y - 1
  92.             else
  93.                 break
  94.             end
  95.         end
  96.     elseif rot == 0 then
  97.         while not tile(tile_x, tile_y, "wall") and topixel(tile_y) >= end_y do
  98.             tile_y = tile_y - 1
  99.         end
  100.     elseif rot == 90 then
  101.         while not tile(tile_x, tile_y, "wall") and topixel(tile_x) <= end_x do
  102.             tile_x = tile_x + 1
  103.         end
  104.     elseif rot == 180 then
  105.         while not tile(tile_x, tile_y, "wall") and topixel(tile_y) <= end_y do
  106.             tile_y = tile_y + 1
  107.         end
  108.     elseif rot == -90 then
  109.         while not tile(tile_x, tile_y, "wall") and topixel(tile_x) >= end_x do
  110.             tile_x = tile_x - 1
  111.         end
  112.     end
  113.  
  114.     local temp_x, temp_y = end_x, end_y
  115.     end_x, end_y = intersect(start_x, start_y, temp_x, temp_y, topixel(tile_x), topixel(tile_y), 32)
  116.    
  117.     if not (end_x  or end_y) then --unlikely or impossible, but here for safety.
  118.         end_x, end_y = temp_x, temp_y
  119.     end
  120.  
  121.     local frames = math.floor(player(id, "ping") / 20)
  122.     if frames > (BUFFER_SIZE - 1) then
  123.         frames = (BUFFER_SIZE - 1)
  124.     end
  125.  
  126.     local alive = player(0, "tableliving")
  127.     for _, v in ipairs(alive) do
  128.         if ((game("sv_friendlyfire") == 1) or (player(v, "team") ~= player(id, "team"))) and (v ~= id) and intersect(start_x, start_y, end_x, end_y, buffer[1][v][frame - frames], buffer[2][v][frame - frames], 24) then
  129.             local newarmor = player(v, "armor") - itemtype(wpn, "dmg")
  130.             if newarmor < 0 then
  131.                 newarmor = 0
  132.             end
  133.             local newhealth = player(v, "health") - (itemtype(wpn, "dmg") - math.floor(game("mp_kevlar") * (player(v, "armor") - newarmor)))
  134.             if newhealth > 0 then
  135.                 parse("sethealth "..v.." "..newhealth)
  136.                 parse("setarmor "..v.." "..newarmor)
  137.             else
  138.                 parse("customkill "..id.." "..itemtype(wpn, "name").." "..v)
  139.             end
  140.         end
  141.     end
  142. end
  143.  
  144. function topixel(tile)
  145.     return (tile * 32) + 16
  146. end
  147.  
  148. function isinorder(s, i, e)
  149.     return (e >= i and i >= s) or (e <= i and i <= s)
  150. end
  151.  
  152. function intersect(sx, sy, ex, ey, bx, by, bl)
  153.     bl = bl / 2
  154.  
  155.     if math.abs(sx - bx) <= bl and math.abs(sy - by) <= bl then
  156.         if math.abs(ex - bx) <= bl and math.abs(ey - by) <= bl then
  157.             return ex, ey
  158.         else
  159.             return intersect(ex, ey, sx, sy, bx, by, bl)
  160.         end
  161.     end
  162.  
  163.     local i_x, i_y
  164.  
  165.     if ey > sy then
  166.         i_y = by - bl
  167.     elseif ey < sy then
  168.         i_y = by + bl
  169.     end
  170.     if i_y and isinorder(sy, i_y, ey) then
  171.         i_x = ((ex - sx) * i_y + (sx * ey - sy * ex)) / (ey - sy)
  172.         if math.abs(i_x - bx) <= bl and isinorder(sx, i_x, ex) then
  173.             return i_x, i_y
  174.         end
  175.     end
  176.  
  177.     if ex > sx then
  178.         i_x = bx - bl
  179.     elseif ex < sx then
  180.         i_x = bx + bl
  181.     end
  182.     if i_x and isinorder(sx, i_x, ex) then
  183.         i_y = ((ey - sy) * i_x + (sy * ex - sx * ey)) / (ex - sx)
  184.         if math.abs(i_y - by) <= bl and isinorder(sy, i_y, ey) then
  185.             return i_x, i_y
  186.         end
  187.     end
  188. end
Advertisement
Add Comment
Please, Sign In to add comment