Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --LUA Lag Compensation version 0.4 by FlooD
- --thx to Jermuk for his custom server, which first encouraged me to make lag compensation.
- --thx to lasthope and def3ct for initial testing.
- --thx to 3rr0r for ideas and suggestions for refining the code.
- BUFFER_SIZE = 25
- frame = 0
- buffer = {}
- buffer[1] = {} --x buffer
- buffer[2] = {} --y buffer
- function clearbuffer(id)
- buffer[1][id] = {}
- buffer[2][id] = {}
- end
- for i = 1, 32 do
- clearbuffer(i)
- end
- addhook("leave", "clearbuffer")
- addhook("die", "clearbuffer")
- addhook("always", "updatebuffer")
- function updatebuffer()
- frame = frame + 1
- for i = 1, 32 do
- buffer[1][i][frame], buffer[2][i][frame] = player(i, "x"), player(i, "y")
- buffer[1][i][frame - BUFFER_SIZE], buffer[2][i][frame - BUFFER_SIZE] = nil, nil
- end
- end
- addhook("hit", "onhit")
- function onhit(v,id,wpn)
- if wpn ~= 51 then
- return 1
- end
- end
- addhook("attack", "onattack")
- function onattack(id)
- local wpn = player(id, "weapon")
- if wpn == 51 then
- return
- end
- simulate_attack(id, wpn)
- end
- addhook("attack2", "onattack2")
- function onattack2(id, mode)
- local wpn = player(id, "weapon")
- if wpn == 50 then
- simulate_attack(id, wpn, itemtype(wpn, "dmg_z1") * game("mp_damagefactor"))
- end
- end
- --[[simulates the shooting of a bullet of damage (dmg) from (wpn) by (id) with
- angle (rot). it has two parts. part 1 finds bullet's path before hitting
- a wall; part 2 calculates hits on other players (with lag compensation).]]
- function simulate_attack(id, wpn, dmg, rot, range)
- if not wpn then wpn = player(id, "weapon") end
- if not dmg then dmg = itemtype(wpn, "dmg") * game("mp_damagefactor") end
- if not rot then rot = player(id, "rot") end
- if not range then range = itemtype(wpn, "range") end
- local start_x = player(id, "x")
- local start_y = player(id, "y")
- local end_x = start_x + (3 * range) * math.sin(math.rad(rot))
- local end_y = start_y - (3 * range) * math.cos(math.rad(rot))
- local tile_x = math.floor(start_x / 32)
- local tile_y = math.floor(start_y / 32)
- --part 1 - find the intersection of the bullet with the first wall it hits.
- local inc_x, inc_y
- if rot < 0 then
- inc_x = -1
- elseif rot > 0 and rot ~= 180 then
- inc_x = 1
- end
- if math.abs(rot) > 90 then
- inc_y = 1
- elseif math.abs(rot) < 90 then
- inc_y = -1
- end
- while not tile(tile_x, tile_y, "wall") do
- local temp_x, temp_y = tile_x, tile_y
- if inc_x and intersect(start_x, start_y, end_x, end_y, topixel(temp_x + inc_x), topixel(temp_y), 32) then
- tile_x = temp_x + inc_x
- end
- if inc_y and intersect(start_x, start_y, end_x, end_y, topixel(temp_x), topixel(temp_y + inc_y), 32) then
- tile_y = temp_y + inc_y
- end
- if tile_x == temp_x and tile_y == temp_y then
- break
- end
- end
- end_x, end_y = intersect(start_x, start_y, end_x, end_y, topixel(tile_x), topixel(tile_y), 32)
- --part 2 - detect hits
- local frames = math.floor(player(id, "ping") / 20)
- if frames > (BUFFER_SIZE - 1) then
- frames = (BUFFER_SIZE - 1)
- end
- local alive = player(0, "tableliving")
- for _, v in ipairs(alive) do
- 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
- local newarmor = player(v, "armor") - dmg
- if newarmor < 0 then
- newarmor = 0
- end
- local newhealth = player(v, "health") - (dmg - math.floor(game("mp_kevlar") * (player(v, "armor") - newarmor)))
- if newhealth > 0 then
- parse("sethealth "..v.." "..newhealth)
- parse("setarmor "..v.." "..newarmor)
- else
- parse("customkill "..id.." "..itemtype(wpn, "name").." "..v)
- end
- end
- end
- end
- --the following three functions are used in simulate_attack.
- --converts a tile number to the tile's center pixel.
- function topixel(tile)
- return (tile * 32) + 16
- end
- --quick test to see if i is in between s and e. used in intersect().
- function isinorder(s, i, e)
- return (e >= i and i >= s) or (e <= i and i <= s)
- end
- --[[returns the first point of intersection between a box centered at (bx, by) with
- side length (bl) and a line segment starting from (sx, sy) and ending at (ex, ey).
- if the line segment is enclosed by the box, (ex, ey) is returned.]]
- function intersect(sx, sy, ex, ey, bx, by, bl)
- bl = bl / 2
- if math.abs(sx - bx) <= bl and math.abs(sy - by) <= bl then
- if math.abs(ex - bx) <= bl and math.abs(ey - by) <= bl then
- return ex, ey
- else
- return intersect(ex, ey, sx, sy, bx, by, bl)
- end
- end
- local i_x, i_y
- if ey > sy then
- i_y = by - bl
- elseif ey < sy then
- i_y = by + bl
- end
- if i_y and isinorder(sy, i_y, ey) then
- i_x = ((ex - sx) * i_y + (sx * ey - sy * ex)) / (ey - sy)
- if math.abs(i_x - bx) <= bl and isinorder(sx, i_x, ex) then
- return i_x, i_y
- end
- end
- if ex > sx then
- i_x = bx - bl
- elseif ex < sx then
- i_x = bx + bl
- end
- if i_x and isinorder(sx, i_x, ex) then
- i_y = ((ey - sy) * i_x + (sy * ex - sx * ey)) / (ex - sx)
- if math.abs(i_y - by) <= bl and isinorder(sy, i_y, ey) then
- return i_x, i_y
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement