Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --LUA Lag Compensation 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.
- weapons = {}
- weapons[30] = {range = 300, damage = 22, name = "AK-47"}
- weapons[32] = {range = 300, damage = 22, name = "M4A1"}
- weapons[50] = {range = 8, damage = 50, name = "Knife"}
- weapons[3] = {range = 300, damage = 34, name = "Deagle"}
- for i = 1, 100 do
- if weapons[i] then
- parse("mp_wpndmg "..weapons[i]["name"].." 0")
- end
- end
- 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("attack", "onattack")
- function onattack(id)
- local wpn = player(id, "weapon")
- if not weapons[wpn] then
- return
- end
- local rot = player(id, "rot")
- local start_x = player(id, "x")
- local start_y = player(id, "y")
- local end_x = start_x + (3 * weapons[wpn]["range"]) * math.sin(math.rad(rot))
- local end_y = start_y - (3 * weapons[wpn]["range"]) * math.cos(math.rad(rot))
- local tile_x = player(id, "tilex")
- local tile_y = player(id, "tiley")
- --the following huge if statement will set tile_x and tile_y to the first wall the bullet hits.
- if rot > 0 and rot < 90 then
- while (not tile(tile_x, tile_y, "wall")) and topixel(tile_x) <= end_x and topixel(tile_y) >= end_y do
- if intersect(start_x, start_y, end_x, end_y, topixel(tile_x + 1), topixel(tile_y - 1), 32) then
- tile_x, tile_y = tile_x + 1, tile_y - 1
- elseif intersect(start_x, start_y, end_x, end_y, topixel(tile_x + 1), topixel(tile_y), 32) then
- tile_x = tile_x + 1
- elseif intersect(start_x, start_y, end_x, end_y, topixel(tile_x), topixel(tile_y - 1), 32) then
- tile_y = tile_y - 1
- else --unlikely or impossible, but here for safety.
- break
- end
- end
- elseif rot > 90 and rot < 180 then
- while (not tile(tile_x, tile_y, "wall")) and topixel(tile_x) <= end_x and topixel(tile_y) <= end_y do
- if intersect(start_x, start_y, end_x, end_y, topixel(tile_x + 1), topixel(tile_y + 1), 32) then
- tile_x, tile_y = tile_x + 1, tile_y + 1
- elseif intersect(start_x, start_y, end_x, end_y, topixel(tile_x + 1), topixel(tile_y), 32) then
- tile_x = tile_x + 1
- elseif intersect(start_x, start_y, end_x, end_y, topixel(tile_x), topixel(tile_y + 1), 32) then
- tile_y = tile_y + 1
- else
- break
- end
- end
- elseif rot > -180 and rot < -90 then
- while (not tile(tile_x, tile_y, "wall")) and topixel(tile_x) >= end_x and topixel(tile_y) <= end_y do
- if intersect(start_x, start_y, end_x, end_y, topixel(tile_x - 1), topixel(tile_y + 1), 32) then
- tile_x, tile_y = tile_x - 1, tile_y + 1
- elseif intersect(start_x, start_y, end_x, end_y, topixel(tile_x - 1), topixel(tile_y), 32) then
- tile_x = tile_x - 1
- elseif intersect(start_x, start_y, end_x, end_y, topixel(tile_x), topixel(tile_y + 1), 32) then
- tile_y = tile_y + 1
- else
- break
- end
- end
- elseif rot > -90 and rot < 0 then
- while (not tile(tile_x, tile_y, "wall")) and topixel(tile_x) >= end_x and topixel(tile_y) >= end_y do
- if intersect(start_x, start_y, end_x, end_y, topixel(tile_x - 1), topixel(tile_y - 1), 32) then
- tile_x, tile_y = tile_x - 1, tile_y - 1
- elseif intersect(start_x, start_y, end_x, end_y, topixel(tile_x - 1), topixel(tile_y), 32) then
- tile_x = tile_x - 1
- elseif intersect(start_x, start_y, end_x, end_y, topixel(tile_x), topixel(tile_y - 1), 32) then
- tile_y = tile_y - 1
- else
- break
- end
- end
- elseif rot == 0 then
- while not tile(tile_x, tile_y, "wall") and topixel(tile_y) >= end_y do
- tile_y = tile_y - 1
- end
- elseif rot == 90 then
- while not tile(tile_x, tile_y, "wall") and topixel(tile_x) <= end_x do
- tile_x = tile_x + 1
- end
- elseif rot == 180 then
- while not tile(tile_x, tile_y, "wall") and topixel(tile_y) <= end_y do
- tile_y = tile_y + 1
- end
- elseif rot == -90 then
- while not tile(tile_x, tile_y, "wall") and topixel(tile_x) >= end_x do
- tile_x = tile_x - 1
- end
- end
- local temp_x, temp_y = end_x, end_y
- end_x, end_y = intersect(start_x, start_y, temp_x, temp_y, topixel(tile_x), topixel(tile_y), 32)
- if not (end_x or end_y) then --unlikely or impossible, but here for safety.
- end_x, end_y = temp_x, temp_y
- end
- 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") - weapons[wpn]["damage"]
- if newarmor < 0 then
- newarmor = 0
- end
- local newhealth = player(v, "health") - (weapons[wpn]["damage"] - 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.." "..weapons[wpn]["name"].." "..v)
- end
- end
- end
- end
- function topixel(tile)
- return (tile * 32) + 16
- end
- function isinorder(s, i, e)
- return (e >= i and i >= s) or (e <= i and i <= s)
- end
- 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 return sx, sy end
- local i_x, i_y
- if ey > sy then
- i_x = ((ex - sx) * (by - bl) + (sx * ey - sy * ex)) / (ey - sy)
- elseif ey < sy then
- i_x = ((ex - sx) * (by + bl) + (sx * ey - sy * ex)) / (ey - sy)
- end
- if i_x and math.abs(i_x - bx) <= bl and isinorder(sx, i_x, ex) then
- return i_x, (((ey > sy) and (by - bl)) or (by + bl))
- end
- if ex > sx then
- i_y = ((ey - sy) * (bx - bl) + (sy * ex - sx * ey)) / (ex - sx)
- elseif ex < sx then
- i_y = ((ey - sy) * (bx + bl) + (sy * ex - sx * ey)) / (ex - sx)
- end
- if i_y and math.abs(i_y - by) <= bl and isinorder(sy, i_y, ey) then
- return (((ex > sx) and (bx - bl)) or (bx + bl)), i_y
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement