Advertisement
Guest User

LUA LAG COMPENSATION

a guest
Nov 16th, 2010
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.12 KB | None | 0 0
  1. --LUA Lag Compensation
  2. --by FlooD
  3. --thx to Jermuk for his custom server
  4. --and lasthope and def3ct for initial testing.
  5.  
  6. BUFFER_SIZE = 25
  7.  
  8. buffer = {}
  9. for i = 0, (BUFFER_SIZE - 1) do
  10.     buffer[i] = {}
  11.     for j = 1, 32 do
  12.         buffer[i][j] = {0, 0}
  13.     end
  14. end
  15.  
  16. weapons = {}
  17. weapons[30] = {range = 300, damage = 22, name = "AK-47"}
  18. weapons[32] = {range = 300, damage = 22, name = "M4A1"}
  19. weapons[50] = {range = 8, damage = 50, name = "Knife"}
  20. weapons[3] = {range = 300, damage = 34, name = "Deagle"}
  21.  
  22. for i = 1, 100 do
  23.     if weapons[i] then
  24.         parse("mp_wpndmg "..weapons[i]["name"].." 0")
  25.     end
  26. end
  27.  
  28. addhook("always", "updatebuffer")
  29. function updatebuffer()
  30.     for i = 0, (BUFFER_SIZE - 2) do
  31.         buffer[BUFFER_SIZE - 1 - i] = deepcopy(buffer[BUFFER_SIZE - 2 - i])
  32.     end
  33.     for i = 1, 32 do
  34.         buffer[0][i][1], buffer[0][i][2] = player(i,"x"), player(i,"y")
  35.     end
  36. end
  37.  
  38. addhook("leave", "clearbuffer")
  39. addhook("die", "clearbuffer")
  40. function clearbuffer(id)
  41.     for i = 0, (BUFFER_SIZE - 1) do
  42.         buffer[i][id] = {0, 0}
  43.     end
  44. end
  45.  
  46. addhook("attack", "onattack")
  47. function onattack(id)
  48.     local wpn = player(id, "weapon")
  49.     local frames = math.floor(player(id, "ping") / 20)
  50.     if frames > (BUFFER_SIZE - 1) then
  51.         frames = (BUFFER_SIZE -1)
  52.     end
  53.  
  54.     local rot = player(id, "rot")
  55.     local increment_x = math.sin(math.pi * rot / 180)
  56.     local increment_y = math.cos(math.pi * rot / 180)
  57.  
  58.     local current_x = player(id, "x")
  59.     local current_y = player(id, "y")
  60.  
  61.     local targets = player(0, "tableliving")
  62.  
  63.     local hit = {}
  64.     for i = 1, 32 do
  65.         hit[i] = 0
  66.     end
  67.  
  68.     for i = 1, (3 * weapons[wpn]["range"]) do
  69.         current_x = current_x + increment_x
  70.         current_y = current_y - increment_y
  71.  
  72.         local tile_x = math.floor(current_x / 32)
  73.         local tile_y = math.floor(current_y / 32)
  74.         if tile(tile_x, tile_y, "wall") then
  75.             return
  76.         end
  77.  
  78.         for j = 1, #targets do
  79.             local tid=targets[j]
  80.             if ((game("sv_friendlyfire") == 1) or (player(tid, "team") ~= player(id, "team"))) and (tid ~= id) and (hit[tid] == 0) and (math.abs(buffer[frames][tid][1] - current_x) <= 12) and (math.abs(buffer[frames][tid][2] - current_y) <= 12) then
  81.                 hit[tid] = 1
  82.                 local newarmor = player(tid, "armor") - weapons[wpn]["damage"]
  83.                 if newarmor < 0 then
  84.                     newarmor = 0
  85.                 end
  86.                 local newhealth = player(tid, "health") - (weapons[wpn]["damage"] - math.floor(game("mp_kevlar") * (player(tid, "armor") - newarmor)))
  87.                 if newhealth > 0 then
  88.                     parse("sethealth "..tid.." "..newhealth)
  89.                     parse("setarmor "..tid.." "..newarmor)
  90.                 else
  91.                     parse("customkill "..id.." "..weapons[wpn]["name"].." "..tid)
  92.                 end
  93.             end
  94.         end
  95.     end
  96. end
  97.  
  98. function deepcopy(object)
  99.     local lookup_table = {}
  100.     local function _copy(object)
  101.         if type(object) ~= "table" then
  102.             return object
  103.         elseif lookup_table[object] then
  104.             return lookup_table[object]
  105.         end
  106.         local new_table = {}
  107.         lookup_table[object] = new_table
  108.         for index, value in pairs(object) do
  109.             new_table[_copy(index)] = _copy(value)
  110.         end
  111.         return setmetatable(new_table, getmetatable(object))
  112.     end
  113.     return _copy(object)
  114. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement