--kari.yaw
local ui_reference, ui_new_checkbox, ui_new_hotkey, ui_get, ui_new_slider, ui_new_combobox, ui_new_multiselect, ui_set, ui_new_color_picker, ui_set_visible =
    ui.reference, ui.new_checkbox, ui.new_hotkey, ui.get, ui.new_slider, ui.new_combobox, ui.new_multiselect, ui.set, ui.new_color_picker, ui.set_visible
local math_sqrt, math_deg, math_atan2, math_rad, math_floor, math_random, math_sin, math_abs, math_max, math_min, math_cos, math_atan =
    math.sqrt, math.deg, math.atan2, math.rad, math.floor, math.random, math.sin, math.abs, math.max, math.min, math.cos, math.atan
local entity_get_prop, entity_get_local_player, entity_is_alive, entity_get_origin, client_trace_bullet, entity_is_dormant, entity_set_prop, entity_get_esp_data,
    entity_get_players, entity_get_classname, entity_get_player_weapon, entity_hitbox_position, entity_get_player_name =
    entity.get_prop, entity.get_local_player, entity.is_alive, entity.get_origin, client.trace_bullet, entity.is_dormant, entity.set_prop, entity.get_esp_data,
    entity.get_players, entity.get_classname, entity.get_player_weapon, entity.hitbox_position, entity.get_player_name
local globals_tickinterval, globals_tickcount, globals_curtime, globals_chokedcommands, globals_lastoutgoingcommand, globals_frametime =
    globals.tickinterval, globals.tickcount, globals.curtime, globals.chokedcommands, globals.lastoutgoingcommand, globals.frametime
local client_trace_line, client_set_event_callback,  client_get_cvar, client_current_threat, client_random_int, client_random_float, client_visible, client_scale_damage =
    client.trace_line, client.set_event_callback,  client.get_cvar, client.current_threat, client.random_int, client.random_float, client.visible, client.scale_damage
local renderer_text, renderer_measure_text, renderer_indicator, renderer_rectangle, renderer_circle, renderer_circle_outline, renderer_gradient =
    renderer.text, renderer.measure_text, renderer.indicator, renderer.rectangle, renderer.circle, renderer.circle_outline, renderer.gradient
local bit_band, bit_lshift = bit.band, bit.lshift
local client_screen_size, client_camera_angles, client_userid_to_entindex, client_latency, plist_set =
    client.screen_size, client.camera_angles, client.userid_to_entindex, client.latency, plist.set

local ffi = require('ffi')
local anti_aim = require ("gamesense/antiaim_funcs")
local ent = require ("gamesense/entity")
local clipboard = require("gamesense/clipboard")
local base64 = require("gamesense/base64")
local vector = require('vector')
local weapons = require("gamesense/csgo_weapons")

local native_GetClientEntity = vtable_bind("client.dll", "VClientEntityList003", 3, "uintptr_t(__thiscall*)(void*, int)")

local gui = {}

local function dist(v1, v2, v3)
	local d = (v1 - v2) / v1:dist(v2)
	local v = v3 - v2
	local t = v:dot(d)
	local P = v2 + d:scaled(t)
	return P:dist(v3)
end

function str_to_sub(input, sep)
    local t = {}
    for str in string.gmatch(input, "([^"..sep.."]+)") do
        t[#t + 1] = string.gsub(str, "\n", "")
    end
    return t
end

function arr_to_string(arr)
	arr = ui_get(arr)
	local str = ""
	for i=1, #arr do
		str = str .. arr[i] .. (i == #arr and "" or ",")
	end
	if str == "" then
		str = "-"
	end
	return str
end

function to_boolean(str)
    if str == "true" or str == "false" then
        return (str == "true")
    else
        return str
    end
end

local function table_contains(tbl, val)
    for i=1,#tbl do
        if tbl[i] == val then
            return true
        end
    end
    return false
end

function menu(register,need_export,... )
    local number_ = register
    if type(number_) == 'number' then
        table.insert(gui.callback,number_)
    end
    if need_export then
        if type(number_) == 'number' then
            table.insert(gui.export[type(ui_get(number_))],number_)
        end
    end
    return number_
end

gui.callback = {}
gui.export = {
    ['number'] = {},
    ['boolean'] = {},
    ['table'] = {},
    ['string'] = {}
}
local function expd()
	local str = ""
    for i,o in pairs(gui.export['number']) do
        str = str .. tostring(ui_get(o)) .. '|'
    end
    for i,o in pairs(gui.export['string']) do
        str = str .. (ui_get(o)) .. '|'
    end
    for i,o in pairs(gui.export['boolean']) do
        str = str .. tostring(ui_get(o)) .. '|'
    end
    for i,o in pairs(gui.export['table']) do
        str = str .. arr_to_string(o) .. '|'
    end
    clipboard.set(base64.encode(str, 'base64'))
end

local function loadd()
    local tbl = str_to_sub(base64.decode(clipboard.get(), 'base64'), "|")
    local p = 1
    for i,o in pairs(gui.export['number']) do

        ui_set(o,tonumber(tbl[p]))
        p = p + 1
    end
    for i,o in pairs(gui.export['string']) do
        ui_set(o,tbl[p])
        p = p + 1
    end
    for i,o in pairs(gui.export['boolean']) do
        ui_set(o,to_boolean(tbl[p]))
        p = p + 1
    end
    for i,o in pairs(gui.export['table']) do
        ui_set(o,str_to_sub(tbl[p],','))
        p = p + 1
    end
end

local refer = {
    enabled = ui_reference("AA", "Anti-aimbot angles", "Enabled"),
    pitch = {ui_reference("AA", "Anti-aimbot angles", "pitch")},
    roll = ui_reference("AA", "Anti-aimbot angles", "roll"),
    yawbase = ui_reference("AA", "Anti-aimbot angles", "Yaw base"),
    yaw = {ui_reference("AA", "Anti-aimbot angles", "Yaw")},
    fsbodyyaw = ui_reference("AA", "anti-aimbot angles", "Freestanding body yaw"),
    edgeyaw = ui_reference("AA", "Anti-aimbot angles", "Edge yaw"),
    os = {ui_reference("AA", "Other", "On shot anti-aim")},
    dt = {ui_reference("RAGE", "Aimbot", "Double tap")},
    dt_limit = ui_reference("RAGE", "Aimbot", "Double tap fake lag limit"),
    fakelaglimit = ui_reference("AA", "Fake lag", "Limit"),
    forcebaim = ui_reference("RAGE", "Aimbot", "Force body aim"),
    quickpeek = {ui_reference("RAGE", "Other", "Quick peek assist")},
    yawjitter = {ui_reference("AA", "Anti-aimbot angles", "Yaw jitter")},
    bodyyaw = {ui_reference("AA", "Anti-aimbot angles", "Body yaw")},
    freestand = {ui_reference("AA", "Anti-aimbot angles", "Freestanding")},
    slow = {ui_reference("AA", "Other", "Slow motion")},
    leg_movement = ui_reference("AA", "Other", "Leg movement"),
    SP = ui_reference("Rage", "Aimbot", "Force safe point"),
    fakelagamout = ui_reference("AA", "Fake lag", "Amount"),
    fakelagva = ui_reference("AA", "Fake lag", "Variance"),
    fakeduck = {ui_reference("RAGE", "Other", "Duck peek assist")},
    fakelag = {ui_reference("AA", "Fake lag", "Enabled")},
    dmg = ui_reference("RAGE", "Aimbot", "Minimum damage"),
    dmgoverride = {ui_reference("RAGE", "Aimbot", "Minimum damage override")},
    fd = ui_reference("RAGE", "Other", "Duck peek assist"),
    maxusrcmdprocessticks = ui_reference("misc", "settings", "sv_maxusrcmdprocessticks2"),
    pingspike = {ui_reference("misc", "miscellaneous", "ping spike")},
}

gui.menu = {
    lua_enable = menu(ui_new_checkbox("AA", "Anti-aimbot angles","\a757575FFKARI.YAW"),true),
    lua_tab = menu(ui_new_combobox("AA", "Anti-aimbot angles","Swicther[\a757575FFMain\aFFFFFFCC]",{"Anti aim","Visuals","Misc","Settings"}),true),
    aa_option = menu(ui_new_multiselect("AA", "Anti-aimbot angles","Option",{"Anti backstab","Shift tick",""}),true),
    freestanding_key = menu(ui_new_hotkey("AA", "Anti-aimbot angles", "Freestanding key\n"),true),
    indicator = menu(ui_new_multiselect("AA", "Anti-aimbot angles","Indicator",{"Crosshair","Defensive"}),true),
    animate= menu(ui_new_multiselect("AA", "Anti-aimbot angles", "Animation",{"Static legs","Pitch 0 on land","Leg breaker"}),true),
    air_walk = menu(ui_new_combobox("AA", "Anti-aimbot angles", "Air animation",{"Walk","Static","Normal"}),true),
    auto_tp = menu(ui_new_checkbox("AA", "Anti-aimbot angles" ,"Enable"),true),
    auto_tp_key = menu(ui_new_hotkey("AA", "Anti-aimbot angles" ,"Auto teleport"),true),
    trails = menu(ui_new_checkbox("AA", "Anti-aimbot angles", "Enemy trails"),true),
    color = menu(ui_new_color_picker("AA", "Anti-aimbot angles", "\n Color picker", 255, 255, 255, 255),true),
    enable_fl = menu(ui_new_checkbox("AA", "Fake lag", "Enable Fakelag"),true),
    reset_onshot = menu(ui_new_checkbox("AA", "Fake lag", "Reset fakelag on shot"),true),
    triggerlimit = menu(ui.new_slider("AA", "Fake lag", "Limit",1,15,15)),
	sendlimit = menu(ui.new_slider("AA", "Fake lag", "Send limit",1,15,13)),
    defensive = menu(ui_new_multiselect("AA", "Anti-aimbot angles","Defensive trigger",{"In air","In duck","Always"}),true),
    def_slider = menu(ui_new_slider("AA", "Anti-aimbot angles", "Defensive tick\n", 2, 11, 0, true, "t", 1, {[2] = "Instant"}),true),
    resolver = menu(ui_new_checkbox("AA", "Anti-aimbot angles" ,"Jitter correction heler"),true),
    lethal_baim = menu(ui_new_checkbox("AA", "Anti-aimbot angles" ,"Auto lethal baim"),true),
}

local antiaim = {
    state_name = {
        "Stand", 
        "Move", 
        "Air", 
        "Air duck", 
        "Slow Walke",
        "Duck",
    },
    state_to_idex = {
        ["Stand"] = 1, 
        ["Move"] = 2, 
        ["Air"] = 3, 
        ["Air duck"] = 4, 
        ["Slow Walke"] = 5,
        ["Duck"] = 6,
    },
    state_index = 1,
    active_index = 1
}

gui.menu.custom = {}
gui.menu.custom[0] = {
    player_state = menu(ui_new_combobox("AA", "Anti-aimbot angles", "Condition".."[\a757575FF".. "Anti aim" .."\aFFFFFFCC]", antiaim.state_name),true),
}

for i = 1, 6 do
    gui.menu.custom[i] = {
        yawtype = menu(ui_new_combobox( "AA","Anti-aimbot angles","Yaw type\n" .. antiaim.state_name[i],{"Off","Jitter","Sway","Random","3-way","5-way","Choke"}),true),
        yawadd = menu(ui_new_slider("AA", "Anti-aimbot angles", "Left\n" .. antiaim.state_name[i], -180, 180, 0),true),
        yawadd1 = menu(ui_new_slider("AA", "Anti-aimbot angles", "Right\n" .. antiaim.state_name[i], -180, 180, 0),true),
        jittertype = menu(ui_new_combobox( "AA","Anti-aimbot angles","Jitter type\n" .. antiaim.state_name[i],{"Off","Offset","Center","Random","Skitter"}),true),
        jitteradd = menu(ui_new_slider("AA", "Anti-aimbot angles", "Jitter amount\n" .. antiaim.state_name[i], -180, 180, 0),true),
        bodyyaw = menu(ui_new_combobox( "AA","Anti-aimbot angles","Body yaw\n" .. antiaim.state_name[i],{"Off","Opposite","Jitter","Static"}),true),
        delay = menu(ui_new_slider("AA", "Anti-aimbot angles", "Delay\n" .. antiaim.state_name[i], 0, 20, 0),true),
        flicker = menu(ui_new_checkbox("AA", "Anti-aimbot angles" ,"Flicker\n".. antiaim.state_name[i]),true),
        flicke_limit = menu(ui_new_slider("AA", "Anti-aimbot angles", "Limit\n" .. antiaim.state_name[i], -60,60, 0),true),
        flicke_speed = menu(ui_new_slider("AA", "Anti-aimbot angles", "speed\n" .. antiaim.state_name[i], 1, 64, 0),true),
        def = menu(ui_new_checkbox("AA", "Anti-aimbot angles" ,"Defensive antiaim\n".. antiaim.state_name[i]),true),
        def_pitch = menu(ui_new_combobox("AA","Anti-aimbot angles","Pitch\n" .. antiaim.state_name[i],{"Off","Random","Increase","Decrease"}),true),
        def_pitch_add = menu(ui_new_slider("AA", "Anti-aimbot angles", "\nPitch_va" .. antiaim.state_name[i], -89, 89, 0),true),
        def_pitch_add1 = menu(ui_new_slider("AA", "Anti-aimbot angles", "\nPitch_va1" .. antiaim.state_name[i], -89, 89, 0),true),
        def_yaw = menu(ui_new_combobox( "AA","Anti-aimbot angles","Yaw option\n" .. antiaim.state_name[i],{"Off","Spin","Increase","Jitter"}),true),
        def_yaw_add = menu(ui_new_slider("AA", "Anti-aimbot angles", "\nyaw_va" .. antiaim.state_name[i], 0, 180, 0),true),
    }
end

local export = ui.new_button("AA","Anti-aimbot angles",'Export settings to clipboard',expd)
local load = ui.new_button("AA","Anti-aimbot angles",'Import settings from clipboard',loadd)

local function set_og_menu(state)
    ui_set_visible(refer.enabled,state)
    ui_set_visible(refer.pitch[1], state)
    ui_set_visible(refer.pitch[2], state)
    ui_set_visible(refer.roll, state)
    ui_set_visible(refer.yawbase, state)
    ui_set_visible(refer.yaw[1], state)
    ui_set_visible(refer.yaw[2], state)
    ui_set_visible(refer.yawjitter[1], state)
    ui_set_visible(refer.yawjitter[2], state)
    ui_set_visible(refer.bodyyaw[1], state)
    ui_set_visible(refer.bodyyaw[2], state)
    ui_set_visible(refer.freestand[1], state)
    ui_set_visible(refer.freestand[2], state)
    ui_set_visible(refer.fsbodyyaw, state)
    ui_set_visible(refer.edgeyaw, state)
    ui_set_visible(refer.fakelagamout, state)
    ui_set_visible(refer.fakelag[1], state)
    ui_set_visible(refer.fakelaglimit,state)
    ui_set_visible(refer.fakelagva, state)
end
local function hide_controls()
    set_og_menu(not ui_get(gui.menu.lua_enable))
    ui_set_visible(gui.menu.lua_tab,ui_get(gui.menu.lua_enable) and true or false)
    ui_set_visible(gui.menu.enable_fl,ui_get(gui.menu.lua_enable))
    ui_set_visible(gui.menu.reset_onshot,ui_get(gui.menu.lua_enable))
    if ui_get(gui.menu.enable_fl) and ui_get(gui.menu.lua_enable) then
        ui_set_visible(refer.fakelagamout, false)
        ui_set_visible(refer.fakelag[1], false)
        ui_set_visible(refer.fakelaglimit, false)
        ui_set_visible(refer.fakelagva, false)
        ui_set_visible(gui.menu.triggerlimit, true)
        ui_set_visible(gui.menu.sendlimit, true)
    else
        ui_set_visible(refer.fakelagamout, true)
        ui_set_visible(refer.fakelag[1], true)
        ui_set_visible(refer.fakelaglimit, true)
        ui_set_visible(refer.fakelagva, true)
        ui_set_visible(gui.menu.triggerlimit, false)
        ui_set_visible(gui.menu.sendlimit, false)
    end
    ui_set_visible(gui.menu.air_walk,ui_get(gui.menu.lua_enable) and ui_get(gui.menu.lua_tab) == "Misc")
    ui_set_visible(gui.menu.custom[0].player_state,ui_get(gui.menu.lua_enable) and ui_get(gui.menu.lua_tab) == "Anti aim")
    ui_set_visible(export,ui_get(gui.menu.lua_enable) and ui_get(gui.menu.lua_tab) == "Settings")
    ui_set_visible(load,ui_get(gui.menu.lua_enable) and ui_get(gui.menu.lua_tab) == "Settings")
    ui_set_visible(gui.menu.aa_option,ui_get(gui.menu.lua_enable) and ui_get(gui.menu.lua_tab) == "Anti aim")
    ui_set_visible(gui.menu.defensive,ui_get(gui.menu.lua_enable) and table_contains(ui_get(gui.menu.aa_option), "Shift tick") and ui_get(gui.menu.lua_tab) == "Anti aim")
    ui_set_visible(gui.menu.indicator,ui_get(gui.menu.lua_enable) and ui_get(gui.menu.lua_tab) == "Visuals")
    ui_set_visible(gui.menu.animate,ui_get(gui.menu.lua_enable) and ui_get(gui.menu.lua_tab) == "Misc")
    ui_set_visible(gui.menu.auto_tp,ui_get(gui.menu.lua_enable) and ui_get(gui.menu.lua_tab) == "Misc")
    ui_set_visible(gui.menu.resolver,ui_get(gui.menu.lua_enable) and ui_get(gui.menu.lua_tab) == "Misc")
    ui_set_visible(gui.menu.auto_tp_key,ui_get(gui.menu.lua_enable) and ui_get(gui.menu.lua_tab) == "Misc")
    ui_set_visible(gui.menu.trails,ui_get(gui.menu.lua_enable) and ui_get(gui.menu.lua_tab) == "Misc")
    ui_set_visible(gui.menu.color,ui_get(gui.menu.lua_enable) and ui_get(gui.menu.lua_tab) == "Misc")
    ui_set_visible(gui.menu.lethal_baim,ui_get(gui.menu.lua_enable) and ui_get(gui.menu.lua_tab) == "Misc")
    ui_set_visible(gui.menu.freestanding_key,ui_get(gui.menu.lua_enable) and ui_get(gui.menu.lua_tab) == "Anti aim")
    ui_set_visible(gui.menu.def_slider,table_contains(ui_get(gui.menu.aa_option), "Shift tick") and ui_get(gui.menu.lua_enable) and ui_get(gui.menu.lua_tab) == "Anti aim")
    antiaim.active_index = antiaim.state_to_idex[ui_get(gui.menu.custom[0].player_state)]
    for i = 1, 6 do
        ui_set_visible(gui.menu.custom[i].yawadd,antiaim.active_index == i  and ui_get(gui.menu.lua_enable) and ui_get(gui.menu.lua_tab) == "Anti aim" )
        ui_set_visible(gui.menu.custom[i].yawadd1,antiaim.active_index == i  and ui_get(gui.menu.lua_enable) and ui_get(gui.menu.lua_tab) == "Anti aim" )
        ui_set_visible(gui.menu.custom[i].yawtype, antiaim.active_index == i and ui_get(gui.menu.lua_enable) and ui_get(gui.menu.lua_tab) == "Anti aim")
        ui_set_visible(gui.menu.custom[i].jittertype, antiaim.active_index == i and ui_get(gui.menu.lua_enable) and ui_get(gui.menu.lua_tab) == "Anti aim")
        ui_set_visible(gui.menu.custom[i].jitteradd,antiaim.active_index == i and ui_get(gui.menu.custom[antiaim.active_index].jittertype) ~= "Off"  and ui_get(gui.menu.lua_enable) and ui_get(gui.menu.lua_tab) == "Anti aim")
        ui_set_visible(gui.menu.custom[i].bodyyaw, antiaim.active_index == i and ui_get(gui.menu.lua_enable) and ui_get(gui.menu.lua_tab) == "Anti aim")
        ui_set_visible(gui.menu.custom[i].delay,antiaim.active_index == i  and ui_get(gui.menu.lua_enable) and ui_get(gui.menu.lua_tab) == "Anti aim")
        ui_set_visible(gui.menu.custom[i].flicker,antiaim.active_index == i and ui_get(gui.menu.lua_enable) and ui_get(gui.menu.lua_tab) == "Anti aim")
        ui_set_visible(gui.menu.custom[i].flicke_limit,antiaim.active_index == i and ui_get(gui.menu.custom[i].flicker) and ui_get(gui.menu.lua_enable) and ui_get(gui.menu.lua_tab) == "Anti aim")
        ui_set_visible(gui.menu.custom[i].flicke_speed,antiaim.active_index == i and ui_get(gui.menu.custom[i].flicker) and ui_get(gui.menu.lua_enable) and ui_get(gui.menu.lua_tab) == "Anti aim")
        ui_set_visible(gui.menu.custom[i].def, antiaim.active_index == i and ui_get(gui.menu.lua_enable) and ui_get(gui.menu.lua_tab) == "Anti aim")
        ui_set_visible(gui.menu.custom[i].def_pitch, antiaim.active_index == i and ui_get(gui.menu.custom[i].def) and ui_get(gui.menu.lua_enable) and ui_get(gui.menu.lua_tab) == "Anti aim")
        ui_set_visible(gui.menu.custom[i].def_pitch_add, antiaim.active_index == i and ui_get(gui.menu.custom[i].def) and ui_get(gui.menu.lua_enable) and ui_get(gui.menu.lua_tab) == "Anti aim")
        ui_set_visible(gui.menu.custom[i].def_pitch_add1, antiaim.active_index == i and ui_get(gui.menu.custom[i].def) and ui_get(gui.menu.lua_enable) and ui_get(gui.menu.lua_tab) == "Anti aim")
        ui_set_visible(gui.menu.custom[i].def_yaw, antiaim.active_index == i and ui_get(gui.menu.custom[i].def) and ui_get(gui.menu.lua_enable) and ui_get(gui.menu.lua_tab) == "Anti aim")
        ui_set_visible(gui.menu.custom[i].def_yaw_add, antiaim.active_index == i and ui_get(gui.menu.custom[i].def) and ui_get(gui.menu.lua_enable) and ui_get(gui.menu.lua_tab) == "Anti aim")
    end
end

local vars = {
    vulnerable_ticks = 0,
    tick_var1 = 0,
    tick_var2 = 0,
    tick_var3 = 0,
    tick_var4 = 0,
    tick_var5 = 0,
    tick_var6 = 0,
    tick_var7 = 0,
    sway_stage = 1,
    bsway_stage = 1,
    center_stage = 1,
    switch = false,
    bcenter_stage = 1,
    ycenter_stage = 1,
    way_stage = 1,
    way_stage5 = 1,

    yaw_add = 0,
    nade = 0,

    fsside = nil,
    

    peeked = false,
    ran_last_tick = false,
    vulnerable1 = false,
    fsyaw = 180,
    fsside = "no",

   

    Conditi = nil,

    diff_sim = 0,
    prev_simulation_time = 0,

    can_attack = false,

    ground_ticks = 1, 
    ground_tick = 1, 
    end_time = 0,
    shooting_ticks = 0,
    last_origin = vector(0, 0, 0),
    
    h = 0,
    g = 0,

    shoot = false,
    body_yaw = 0,
    store ={
        fakeyaw = 0,
        realyaw = 0,
        s_fakeyaw = 0,
        s_realyaw = 0,
    },
    enemy_shoot = false,
    pitch = 0,
    def_yaw = 0,

    side = false,
    peeked = false,
    vulnerable = false,

    ran_last_tick = false,
    lagcomp = false,
    positions = {},

    defensive_until = 0,
    defensive_ticks = 0,
    is_defensive = false,
    last_sim_time = 0,

    flicked = false,
    flicked_ticks = 0,

    bodyside =false,
    trigger = 0,

    cmd = 0,
	tickbase_max = 0,
	defensive = 0,

    interpolationParameter = 0,
    der = 1,

    choked = 0,
    pitch_increase = 0,
    pitch_decrease = 0,
    yaw_increase = 0,

    teleport = false,
}


get_velocity = function(player)
    local x, y, z = entity_get_prop(player, "m_vecVelocity")
    if x == nil then
        return
    end
    return math_sqrt(x * x + y * y + z * z)
end

-- Get if the player is in air
in_air = function(player)
    local flags = entity_get_prop(player, "m_fFlags")

    if bit.band(flags, 1) == 0 then
        return true
    end

    return false
end

time_to_ticks = function(t)
    return math_floor(0.5 + (t / globals_tickinterval()))
end

rec = function(x, y, w, h, radius, color)
    radius = math.min(x/2, y/2, radius)
    local r, g, b, a = unpack(color)
    renderer_rectangle(x, y + radius, w, h - radius*2, r, g, b, a)
    renderer_rectangle(x + radius, y, w - radius*2, radius, r, g, b, a)
    renderer_rectangle(x + radius, y + h - radius, w - radius*2, radius, r, g, b, a)
    renderer_circle(x + radius, y + radius, r, g, b, a, radius, 180, 0.25)
    renderer_circle(x - radius + w, y + radius, r, g, b, a, radius, 90, 0.25)
    renderer_circle(x - radius + w, y - radius + h, r, g, b, a, radius, 0, 0.25)
    renderer_circle(x + radius, y - radius + h, r, g, b, a, radius, -90, 0.25)
end
rec_outline = function(x, y, w, h, radius, thickness, color)
    radius = math.min(w/2, h/2, radius)
    local r, g, b, a = unpack(color)
    if radius == 1 then
        renderer_rectangle(x, y, w, thickness, r, g, b, a)
        renderer_rectangle(x, y + h - thickness, w , thickness, r, g, b, a)
    else
        renderer_rectangle(x + radius, y, w - radius*2, thickness, r, g, b, a)
        renderer_rectangle(x + radius, y + h - thickness, w - radius*2, thickness, r, g, b, a)
        renderer_rectangle(x, y + radius, thickness, h - radius*2, r, g, b, a)
        renderer_rectangle(x + w - thickness, y + radius, thickness, h - radius*2, r, g, b, a)
        renderer_circle_outline(x + radius, y + radius, r, g, b, a, radius, 180, 0.25, thickness)
        renderer_circle_outline(x + radius, y + h - radius, r, g, b, a, radius, 90, 0.25, thickness)
        renderer_circle_outline(x + w - radius, y + radius, r, g, b, a, radius, -90, 0.25, thickness)
        renderer_circle_outline(x + w - radius, y + h - radius, r, g, b, a, radius, 0, 0.25, thickness)
    end
end
render_rectangle = function(x, y, w, h, r, g, b, a, roundness)
    local adder = 0
    local radius = 4
    local glow = 15
    if a == 0 then return end
    renderer_rectangle(x + roundness + adder, y + roundness + adder, w - roundness * 2 - adder * 2, h - roundness * 2 - adder * 2, r, g, b, a) --background
    renderer_circle(x + w - roundness - adder, y + roundness + adder, r, g, b, a, roundness, 90, 0.25) -- right top corner
    renderer_circle(x + w - roundness - adder, y + h - roundness - adder, r, g, b, a, roundness, 360, 0.25) --right bottom corner
    renderer_circle(x + roundness + adder, y + h - roundness - adder, r, g, b, a, roundness, 270, 0.25) -- left bottom corner
    renderer_circle(x + roundness + adder, y + roundness + adder, r, g, b, a, roundness, 180, 0.25) -- left top corner
    renderer_rectangle(x + roundness + adder, y + adder, w - roundness * 2 - adder * 2, roundness, r, g, b, a)
    renderer_rectangle(x + w - roundness - adder, y + roundness + adder, roundness, h - roundness * 2 - adder * 2, r, g, b, a)
    renderer_rectangle(x + roundness + adder, y + h - roundness - adder, w - roundness * 2 - adder * 2, roundness, r, g, b, a)
    renderer_rectangle(x + adder, y + roundness + adder, roundness, h - roundness * 2 - adder * 2, r, g, b, a)
end
glow_module = function(x, y, w, h, width, rounding, accent, accent_inner)
    local thickness = 1
    local offset = 1
    local r, g, b, a = unpack(accent)
    if accent_inner then
        rec(x, y, w, h + 1, 50, accent_inner)
    end
    for k = 0, width do
        if a * (k/width)^(1) > 5 then
            local accent = {r, g, b, a * (k/width)^(2)}
            rec_outline(x + (k - width - offset)*thickness, y + (k - width - offset) * thickness, w - (k - width - offset)*thickness*2, h + 1 - (k - width - offset)*thickness*2, rounding + thickness * (width - k + offset), thickness, accent)
        end
    end
end
is_vulnerable = function()
    for _, v in ipairs(entity_get_players(true)) do
        local flags = (entity_get_esp_data(v)).flags

        if bit_band(flags, bit_lshift(1, 11)) ~= 0 then
            vars.vulnerable_ticks = vars.vulnerable_ticks + 1
            return true
        end
    end
    vars.vulnerable_ticks = 0
    return false
end

count = function(cmd,plocal)
    --if not entity_is_alive(me) then return end
    local tickbase = globals.tickcount()
    local delay = ui_get(gui.menu.custom[antiaim.state_index].delay)
    local chokedcommands = cmd.chokedcommands == 0
    local updates = {
        {
            condition = function() return tickbase > vars.tick_var3 + delay end,
            update = function()
                if chokedcommands then
                    vars.center_stage = not vars.center_stage
                    vars.tick_var3 = tickbase
                end
            end,
            reset = function() 
                if tickbase < vars.tick_var3 then
                    vars.tick_var3 = tickbase
                end
            end
        },
        {
            condition = function() return tickbase > vars.tick_var4 + delay end,
            update = function()
                if chokedcommands then
                    if vars.way_stage < 3 then
                        vars.way_stage = vars.way_stage + 1
                    else
                        vars.way_stage = 1
                    end
                    vars.tick_var4 = tickbase
                end
            end,
            reset = function() 
                if tickbase < vars.tick_var4 then
                    vars.tick_var4 = tickbase
                end
            end
        },
        {
            condition = function() return tickbase > vars.tick_var2 + delay end,
            update = function()
                if chokedcommands then
                    if vars.way_stage5 < 5 then
                        vars.way_stage5 = vars.way_stage5 + 1
                    else
                        vars.way_stage5 = 1
                    end
                    vars.tick_var2 = tickbase
                end
            end,
            reset = function() 
                if tickbase < vars.tick_var2 then
                    vars.tick_var2 = tickbase
                end
            end
        },
        
    }
    
    for _, update in ipairs(updates) do
        if update.condition() then
            update.update()
        else
            update.reset()
        end
    end
    if chokedcommands then
        vars.switch = not vars.switch
        if vars.choked >= 8 then
            vars.choked = 1
        else
            vars.choked = vars.choked + 1
        end
        if vars.pitch_increase >= 85 then
            vars.pitch_increase = -89
        else
            vars.pitch_increase = vars.pitch_increase + 1
        end
        if vars.pitch_decrease <= -85 then
            vars.pitch_decrease = 89
        else
            vars.pitch_decrease = vars.pitch_decrease - 1
        end
        if vars.yaw_increase >= 175 then
            vars.yaw_increase = -180
        else
            vars.yaw_increase = vars.yaw_increase + 2
        end
    end
end
sway_manager = (function()
    local a = { data = {} }
    function a:new_frame(b)
        for c, d in pairs(self.data) do
            if b - d.last_tick >= d.speed then
                local e = d.times; for f = 1, e do
                    if d.forward then
                        if d.value < d.max then d.value = d.value + 1 else d.forward = false end
                    else
                        if d.value > d.min then
                            d.value =
                                d.value - 1
                        else
                            d.forward = true
                        end
                    end
                end
                ; d.last_tick = b
            end
        end
    end

    ; function a:sway(g, h, i, j, e)
        local k = math.min(h, i)
        local l = math.max(h, i)
        local e = e or 1; if not self.data[g] then
            self.data[g] = {
                forward = true,
                value = k,
                min = k,
                max = l,
                last_tick = 0,
                speed = j,
                times = e
            }
        end
        ; local m = self.data[g]
        if m.min ~= k or m.max ~= l then
            self.data[g] = {
                forward = true,
                value = k,
                min = k,
                max = l,
                last_tick = 0,
                speed = j,
                times = e
            }
        end
        ; if self.data[g].speed ~= j then self.data[g].speed = j end
        ; if self.data[g].times ~= e then self.data[g].times = e end
        ; return self.data[g].value
    end

    ; return a
end)()
flick_manager = (function()
    local a = { data = {} }
    function a:new_frame(b)
        for c, d in pairs(self.data) do
            if b - d.last_tick >= d.speed then
                d.flicked = not d.flicked; d.last_tick = b
            end
        end
    end

    ; function a:flick(e, f, g, h)
        local i = math.min(f, g)
        local j = math.max(f, g)
        if not self.data[e] then self.data[e] = { flicked = false, last_tick = 0, speed = h } end
        ; if self.data[e].speed ~= h then self.data[e].speed = h end
        ; return self.data[e].flicked and j or i
    end

    ; return a
end)()


draw_3d_line = function(screen, dest, color)
    local start = {screen[1] / 2, screen[2]}
    local ends = {renderer.world_to_screen(dest.x, dest.y, dest.z)}
    renderer.line(start[1], start[2], ends[1], ends[2], color[1], color[2], color[3], color[4])
end
on_shot = function()
    local weapon = entity_get_prop(entity_get_local_player(), "m_hActiveWeapon")
    if weapon == nil then return end
    local last_shot_time = entity_get_prop(weapon, "m_fLastShotTime")
    if last_shot_time == nil then return end
    local time_difference = globals_curtime() - last_shot_time
    return time_difference <= 0.035
end

GetClosestKnife = function ()
    local LocalPlayer = entity_get_local_player();
    local MyOrigin = vector(entity.get_origin(LocalPlayer));
    local EnemyList = entity.get_players(true);
    local ClosestKnife = nil;
    local MinDist = math.huge;

    local get_origin = entity.get_origin;
    local get_player_weapon = entity.get_player_weapon;
    local get_classname = entity.get_classname;
    local is_alive = entity.is_alive;
    local is_dormant = entity.is_dormant;

    for i = 1, #EnemyList do
        local idx = EnemyList[i];
        if is_alive(idx) and not is_dormant(idx) then
            local Weapon = get_player_weapon(idx);

            if Weapon ~= nullptr then
                local WeaponClassName = get_classname(Weapon);
                if WeaponClassName:match("CKnife") then
                    local Dist = MyOrigin:dist(vector(get_origin(idx)));
                    if Dist < MinDist then
                        MinDist = Dist;
                        ClosestKnife = idx;
                    end
                end
            end
        end
    end

    return ClosestKnife, MinDist;
end

local alpha_pulse = 0
defensive_indicator_paint = function()
    local X,Y = client_screen_size()
    local defensive = vars.is_defensive
    if table_contains(ui_get(gui.menu.indicator),"Defensive") and defensive then
        alpha_pulse = math_sin(math_abs(-math.pi + (globals_curtime() * (1 / 0.45)) % (math.pi * 2))) * 200
    else
        alpha_pulse = 0
    end
    local tx, ty = renderer_measure_text("", "- DEFENSIVE -")
    renderer.text(X/2, Y/2 - 400, 255, 255, 255, alpha_pulse, "c", 0, "- DEFENSIVE -")    
end
tp_vl = function(cmd,plocal,threat)
    if ui_get(gui.menu.auto_tp) and ui_get(gui.menu.auto_tp_key) then
        if plocal == nil or entity_is_alive(plocal) == false then
            return
        end
        local weapon = entity_get_player_weapon(plocal)
        if entity_get_classname(weapon) == "CKnife" or entity_get_classname(weapon) == "CWeaponTaser" then
            return
        end
        local velo = get_velocity(plocal)
        local vulnerable = is_vulnerable()
        local on_ground = bit_band(entity_get_prop(plocal, "m_fFlags"), 1) == 1
        if threat == nil then return end
        if vulnerable and velo > 4 and not on_ground then
            cmd.force_defensive = true
            if vars.is_defensive then
                cmd.in_jump = 0
                cmd.discharge_pending = 1
            else
                cmd.discharge_pending = 0
                vars.teleport = false
            end
        end
    end
end
get_conditions = function(cmd, plocal)
    if not plocal then return end
    local vx, vy, _ = entity_get_prop(plocal, "m_vecVelocity")
    local stand = math.sqrt(vx^2 + vy^2) < 5
    local on_ground = bit.band(entity_get_prop(plocal, "m_fFlags"), 1) == 1 and cmd.in_jump == 0
    local slow = ui_get(refer.slow[1]) and ui_get(refer.slow[2])
    --print(defensive_condition and "1" or "0")
    if on_ground then
        if cmd.in_duck == 1 then
            antiaim.state_index = 6
        elseif slow then
            antiaim.state_index = 5
        elseif stand then
            antiaim.state_index = 1
        else
            antiaim.state_index = 2
        end
    else -- not on_ground
        if cmd.in_duck == 1 then
            antiaim.state_index = 4
        else
            antiaim.state_index = 3
        end
    end
end


aa_handler = function(c,plocal,threat,origin)
    if not plocal then return end
    local ClosestEnemy, ClosestEnemyDistance = GetClosestKnife()
    local delay = ui_get(gui.menu.custom[antiaim.state_index].delay)
    local desync = (entity_get_prop(entity_get_local_player(),"m_flPoseParameter" , 11) * 120 - 60) < 0 
    local velocity = get_velocity(plocal)
    local not_fakelag = (ui_get(refer.dt[1]) and ui_get(refer.dt[2])) == true or (ui_get(refer.os[1]) and ui_get(refer.os[2])) == true 
    local defensive_condition = vars.is_defensive and not_fakelag

    sway_manager:new_frame(globals_tickcount())
   
    local yawtype = ui_get(gui.menu.custom[antiaim.state_index].yawtype)
    local yawadd = ui_get(gui.menu.custom[antiaim.state_index].yawadd)
    local yawadd1 = ui_get(gui.menu.custom[antiaim.state_index].yawadd1)
    local bodyyaw = ui_get(gui.menu.custom[antiaim.state_index].bodyyaw)
    --local desync = ui_get(gui.menu.custom[antiaim.state_index].limit)
    local jittertype = ui_get(gui.menu.custom[antiaim.state_index].jittertype)
    local jitteradd = ui_get(gui.menu.custom[antiaim.state_index].jitteradd)
    local def = ui_get(gui.menu.custom[antiaim.state_index].def)
    local def_pitch = ui_get(gui.menu.custom[antiaim.state_index].def_pitch)
    local def_pitch_add = ui_get(gui.menu.custom[antiaim.state_index].def_pitch_add)
    local def_pitch_add1 = ui_get(gui.menu.custom[antiaim.state_index].def_pitch_add1)
    local def_yaw = ui_get(gui.menu.custom[antiaim.state_index].def_yaw)
    local def_yaw_add = ui_get(gui.menu.custom[antiaim.state_index].def_yaw_add)

    local yawadd_values = {yawadd1, yawadd, yawadd, yawadd, yawadd1, yawadd, yawadd1, yawadd1}
    local stage_values = {yawadd, yawadd/2, 0, yawadd1/2, yawadd1}
    local stage = {yawadd, 0, yawadd1}

    if bodyyaw then
        ui_set(refer.bodyyaw[1], bodyyaw)
        if bodyyaw == "Jitter" then
            vars.h = desync and -123 or 123
        elseif bodyyaw == "Static" then
            vars.h = desync and 1 or -1
        end
    end

    if yawtype then
        if yawtype == "Off" then
            vars.g = yawadd
        elseif yawtype == "Jitter" then
            vars.g = vars.center_stage and yawadd or yawadd1
            if delay > 0 and bodyyaw ~= "Opposite" then
                ui_set(refer.bodyyaw[1],"Static")
                vars.h = vars.g
            else
                vars.h = vars.g > 0 and 123 or -123
            end
        elseif yawtype == "Random" then
            vars.g = client_random_int(-math_abs(yawadd),math_abs(yawadd1))
        elseif yawtype == "Sway" then
            vars.g = sway_manager:sway("sway",yawadd,yawadd1,delay,10)
        elseif yawtype == "3-way" then
            vars.g = stage[vars.way_stage] or 0
        elseif yawtype == "5-way" then
            vars.g = stage_values[vars.way_stage5] or 0
        elseif yawtype == "Choke" then
            if bodyyaw ~= "Opposite" then
                ui_set(refer.bodyyaw[1],"Static")
            end
            vars.g = yawadd_values[vars.choked]
            vars.h = vars.g
        end
    end


    if jittertype == "Off" then
        ui_set(refer.yawjitter[1],"Off")         
    elseif jittertype == "Random" then
        ui_set(refer.yawjitter[1],"Random")
    elseif jittertype == "Center" then
        ui_set(refer.yawjitter[1],"Center")
    elseif jittertype == "Offset" then
        ui_set(refer.yawjitter[1],"Offset")
    elseif jittertype == "Skitter" then
        ui_set(refer.yawjitter[1],"Skitter")
    end
    ui_set(refer.yawjitter[2],jitteradd)

    if ui_get(gui.menu.custom[antiaim.state_index].flicker) then
        flick_manager:new_frame(globals_tickcount())
        local yaw_to,yaw_start  = -math_abs(ui_get(gui.menu.custom[antiaim.state_index].flicke_limit)),math_abs(ui_get(gui.menu.custom[antiaim.state_index].flicke_limit))
        local yaw_speed = ui_get(gui.menu.custom[antiaim.state_index].flicke_speed)
        local a = flick_manager:flick("flick1",yaw_start , yaw_to , yaw_speed)
        local flick = flick_manager.data["flick1"].flicked
        if vars.flicked_ticks <= yaw_speed/7 then
            vars.g = vars.g + a
            vars.flicked_ticks = vars.flicked_ticks + 1
            vars.flicked = flick_manager.data["flick1"].flicked
        end
        if flick ~= vars.flicked then
            vars.flicked_ticks = 0
        end
    end

    if threat and not entity_is_dormant(threat) then
        local vulnerable = is_vulnerable()
        local threat_origin = vector(entity_get_origin(threat))
        local distance = origin:dist(threat_origin)
        local height_diff = origin.z - threat_origin.z
        local is_height = height_diff > 64 and vulnerable
        local weapon = entity_get_player_weapon(plocal)
        local is_knife = weapon and entity_get_classname(weapon) == "CKnife" and antiaim.state_index == 4
        if is_knife or is_height then
            vars.g = 20
            vars.h = 1
            ui_set(refer.bodyyaw[1],"Static")
        end
    end
    
    vars.h = math.min(180, math.max(-180, vars.h))
    vars.g = math.min(180, math.max(-180, vars.g))
    
    ui_set(refer.yaw[2],vars.g)
    ui_set(refer.bodyyaw[2], vars.h)   
    
    ui_set(refer.fsbodyyaw,false)
    ui_set(refer.freestand[1],ui_get(gui.menu.freestanding_key))
    ui_set(refer.freestand[2],ui_get(gui.menu.freestanding_key) and "Always on" or "On hotkey")

    if defensive_condition and not_fakelag and def then
        ui_set(refer.fsbodyyaw,true)
        if def_pitch == "Off" then
            ui_set(refer.pitch[1],"Default")
        elseif def_pitch == "Random" then
            ui_set(refer.pitch[1],"Custom")
            ui_set(refer.pitch[2],math_random(def_pitch_add,def_pitch_add1))  
        elseif def_pitch == "Increase" then
            ui_set(refer.pitch[1],"Custom") 
            ui_set(refer.pitch[2],vars.pitch_increase)  
        elseif def_pitch == "Decrease" then
            ui_set(refer.pitch[1],"Custom")  
            ui_set(refer.pitch[2],vars.pitch_decrease)    
        end
        if def_yaw == "Off" then
            ui_set(refer.yaw[1],"180") 
        elseif def_yaw == "Jitter" then
            ui_set(refer.yaw[1],"180")
            ui_set(refer.yaw[2],vars.switch and -def_yaw_add or def_yaw_add)
        elseif def_yaw == "Spin" then
            ui_set(refer.yaw[1],"Spin")
            ui_set(refer.yaw[2],def_yaw_add)
        elseif def_yaw == "Increase" then
            ui_set(refer.yaw[1],"180")
            ui_set(refer.yaw[2],vars.yaw_increase)
        end
    else
        ui_set(refer.yaw[1],"180")
        ui_set(refer.pitch[1],"Custom")
        ui_set(refer.pitch[2],89)
    end
    if ClosestEnemyDistance < 200 and table_contains(ui_get(gui.menu.aa_option),"Anti backstab") then
        ui_set(refer.yawjitter[1],"Center")
        ui_set(refer.yawjitter[2],66)
        ui_set(refer.yaw[2],180)
        ui_set(refer.bodyyaw[1],"Off")
    end
end

fakelag_on = function(cmd,plocal)
    if not entity_is_alive(plocal) then return end
    local on_ground = bit_band(entity_get_prop(plocal, "m_fFlags"), 1) == 1
    local velocity = get_velocity(plocal)
    local a = client_random_int(0,10)
    local on_shot = on_shot()
    fl = ( a <= 5 and ui_get(gui.menu.triggerlimit) or ui_get(gui.menu.sendlimit))
    vars.ground_tick = on_ground and vars.ground_tick + 1 or 0
    if vars.ground_tick < 13 and on_ground then
        fl =1
    end
    if ui_get(gui.menu.reset_onshot) then
        if on_shot or vars.shoot == true then
            cmd.no_choke = true
            vars.shoot = false
        end
    end

    if vars.is_defensive then
        cmd.no_choke = true
    end
    
    if ui_get(gui.menu.enable_fl) then
        ui_set(refer.fakelag[1],true)
        ui_set(refer.fakelagamout,"Dynamic")
        ui_set(refer.fakelaglimit,fl)
        ui_set(refer.fakelagva,0)
    end
end

force_defensive = function(cmd,plocal)
    if not table_contains(ui_get(gui.menu.aa_option),"Shift tick") then return end
    local d = (cmd.quick_stop) and ui_get(refer.quickpeek[2])
    if ui_get(refer.dt[2]) then 
        local on_ground = bit.band(entity_get_prop(plocal, "m_fFlags"), 1) == 1
        local b = (antiaim.state_index == 3 or antiaim.state_index == 4) and table_contains(ui_get(gui.menu.defensive),"In air") 
        local a = (antiaim.state_index == 6) and table_contains(ui_get(gui.menu.defensive),"In duck")
        local c = table_contains(ui_get(gui.menu.defensive),"Always") 
        if a or b or c or d  then
            cmd.force_defensive = true
        end
    end
end
is_lethal = function(player)
    local local_player = entity_get_local_player()
    if local_player == nil or not entity_is_alive(local_player) then return end
    local local_origin = vector(entity_get_prop(local_player, "m_vecAbsOrigin"))
    local distance = local_origin:dist(vector(entity_get_prop(player, "m_vecOrigin")))
    local enemy_health = entity_get_prop(player, "m_iHealth")
    local weapon_ent = entity_get_player_weapon(entity.get_local_player())
    if weapon_ent == nil then return end
    local weapon_idx = entity_get_prop(weapon_ent, "m_iItemDefinitionIndex")
    if weapon_idx == nil then return end
    local weapon = weapons[weapon_idx]
    if weapon == nil then return end
    local dmg_after_range = (weapon.damage * math.pow(weapon.range_modifier, (distance * 0.002))) * 1.25
    local armor = entity_get_prop(player,"m_ArmorValue")
    local newdmg = dmg_after_range * (weapon.armor_ratio * 0.5)
    if dmg_after_range - (dmg_after_range * (weapon.armor_ratio * 0.5)) * 0.5 > armor then
        newdmg = dmg_after_range - (armor / 0.5)
    end
    return newdmg >= enemy_health
end
force_baim = function()
    if ui_get(gui.menu.lethal_baim) then
        local enemies = entity_get_players(true)
        for i = 1, #enemies do
            if enemies[i] == nil then return end
            local value = is_lethal(enemies[i]) and "Force" or "-"
            plist_set(enemies[i], "Override prefer body aim", value)
        end
    end
end

trails = function(plocal,threat)
    if not entity_is_alive(plocal) then return end
    if plocal ~= nil and threat ~= nil and ui.get(gui.menu.trails) then
        local start_pos = {client.screen_size()}
        local enemy_pos = vector(entity.hitbox_position(threat, 2))
        draw_3d_line(start_pos, enemy_pos, {ui_get(gui.menu.color)})
    end
end
    
---------------------- INDICATORS ----------------------
old_indicator_spacing = 0
indicator_fade = 0
local scoped_offset=0
local dt_scoped_offset=0
local screen = {client_screen_size()}
local x_offset, y_offset = screen[1], screen[2]
local x, y =  x_offset/2,y_offset/2

local rgba_to_hex = function(b, c, d, e)
    return string.format('%02x%02x%02x%02x', b, c, d, e)
end
function lerp(a, b, t)
    return a + (b - a) * t
end
function clamp(x, minval, maxval)
    if x < minval then
        return minval
    elseif x > maxval then
        return maxval
    else
        return x
    end
end
local function text_fade_animation(x, y, speed, color1, color2, text)
    local final_text = ''
    local curtime = globals_curtime()
    for i = 0, #text do
        local x = i * 10
        local wave = math_cos(2 * speed * curtime / 4 + x / 30)
        local color = rgba_to_hex(
            lerp(color1.r, color2.r, clamp(wave, 0, 1)),
            lerp(color1.g, color2.g, clamp(wave, 0, 1)),
            lerp(color1.b, color2.b, clamp(wave, 0, 1)),
            color1.a
        )
        final_text = final_text .. '\a' .. color .. text:sub(i, i)
    end

    renderer_text(x, y, color1.r, color1.g, color1.b, color1.a, "c-", nil, final_text)
end

local value = 0
local value_clamped = 0
speed_threshold = 60/8
local indicator_alpha = 255
local indicator_alpha_200 = 200
local indicator_alpha_150= 150
local indicator_alpha_50= 150
indicators = function(plocal)
	X,Y = client_screen_size()
	indicator_spacing = 0

    local scoped = entity_get_prop(plocal, "m_bIsScoped") == 1
    if scoped then
        scoped_offset=scoped_offset+(30-scoped_offset)*globals_frametime() * 15
    else
        scoped_offset=scoped_offset+(0-scoped_offset)*globals_frametime() * 15
    end

    if not entity_is_alive(plocal) then return end
    if not table_contains(ui_get(gui.menu.indicator),"Crosshair") then return end
    -------------------> LUA NAME <-------------------
    local pulse = math.sin(math.abs((math.pi * -1) + (globals.curtime() * (1 / 0.7)) % (math.pi * 2))) * 255
    local name_text_width, name_text_height = renderer_measure_text("c-", "KARI YAW")
    --renderer_text(X / 2+name_scoped_offset+2, Y / 2 +40, 255, 255, 255, 255,"cb",0,"\aFFFFFFFFG\aE4FFE2FFA\aCAFFC6FFM\aAFFFA9FFE\a95FF8DFFS\a7AFF71FFE\a60FF54FFN\a45FF38FFS\a2BFF1CFFE\aFFFFFFFF")
    --text_fade_animation(X / 2+name_scoped_offset+2, Y / 2 +40, 5,  {255, 255, 255, 255}, {43,255,28,255}, "text")
    text_fade_animation(X / 2+scoped_offset-10, Y / 2 +42, 3,{r=200, g=200, b=200, a=255} ,{r=117, g=117, b=117, a=200},  "KARI.YAW")
    renderer_text(X / 2+scoped_offset + name_text_width-10 , Y / 2 +42, 200, 200, 200, pulse, "c-", nil, "[PRIVA]")
    local speed = math_floor(get_velocity(plocal))/7
    local start_v = 0
    local end_v = 0
    local rect_weight = speed
    if speed <= speed_threshold then
        indicator_alpha = math_max(indicator_alpha - globals_frametime() * 2000, 0)
        indicator_alpha_150 = math_max(indicator_alpha_150 - globals_frametime() * 2000, 0)
        indicator_alpha_200 = math_max(indicator_alpha_200 - globals_frametime() * 2000, 0)
        indicator_alpha_50 = math_max(indicator_alpha_50 - globals_frametime() * 2000, 0)
    else
        indicator_alpha = math_min(indicator_alpha + globals_frametime() * 800, 255)
        indicator_alpha_150 = math_min(indicator_alpha_150 + globals_frametime() * 800, 150)
        indicator_alpha_200 = math_min(indicator_alpha_200 + globals_frametime() * 800, 200)
        indicator_alpha_50 = math_min(indicator_alpha_50 + globals_frametime() * 800, 50)
    end
    if rect_weight >= 1 then
        indicator_fade = lerp(indicator_fade,10,globals_frametime() * 15)
        renderer_gradient(X / 2 + scoped_offset - 15-10, Y / 2 + 60, rect_weight+2, 2, 117, 117, 117, indicator_alpha, 117, 117, 117, indicator_alpha, true)
        glow_module(X / 2 + scoped_offset - 15-10, Y / 2 + 60, rect_weight, 3, 4, 2, {117, 117, 117, indicator_alpha_200}, {r, g, b, 0})
        renderer_gradient(X / 2 + scoped_offset - 15-10, Y / 2 + 63, rect_weight+2, 1, 117, 117, 117, indicator_alpha_150, 117, 117, 117, indicator_alpha_150, true)
        renderer_gradient(X / 2 + scoped_offset - 15-10, Y / 2 + 61, 2, 2, 117, 117, 117, indicator_alpha, 117, 117, 117, indicator_alpha, true)
        renderer_gradient(X / 2 + scoped_offset - 15+ rect_weight -2-10, Y / 2 + 61 , 2, 2, 117, 117, 117, indicator_alpha, 117, 117, 117, indicator_alpha, true)
        renderer_gradient(X / 2 + scoped_offset - 15-10, Y / 2 + 62, rect_weight+2, 1, 117, 117, 117, indicator_alpha_50, 117, 117, 117, indicator_alpha_50, true)
        renderer_text(X / 2 + scoped_offset - 8 + rect_weight-10, Y / 2 + 61, 150, 150, 117, indicator_alpha_200, "c-", nil, math_floor(get_velocity(plocal)))
        indicator_spacing = indicator_spacing + 10
    else
        indicator_fade = lerp(indicator_fade,0,globals_frametime() * 15)
    end
    local stateMap = {
        [6] = "DUCK",
        [4] = "AIR+D",
        [3] = "AIR",
        [5] = "SLOW",
        [1] = "STAND",
        [2] = "MOVING",
    }
    
    vars.Conditi = stateMap[antiaim.state_index] or "UNKNOWN"
    local dd,ff = renderer.measure_text("-","RAPID")
    renderer.text(X / 2 + scoped_offset -17-10, Y / 2 +47 , 117,117,117,200,"-",0,"RAPID")
    renderer.text(X / 2 + scoped_offset -16+dd-10, Y / 2 +47 , 255,255,255,200,"-",0,"   |   "..vars.Conditi)
    -------------------> DT INDICATOR <-------------------
	if ui.get(refer.dt[1]) and ui.get(refer.dt[2]) then
		renderer.text(X / 2 - 13 + scoped_offset-10, Y / 2 +62 +indicator_fade, 255,255,255,255,"-c",0,"DT")
	elseif not ui.get(refer.dt[1]) or not ui.get(refer.dt[2]) then
        renderer.text(X / 2 - 13 + scoped_offset-10, Y / 2 +62 +indicator_fade, 255,255,255,50,"-c",0,"DT")
	end
     -------------------> QP INDICATOR <-------------------
	if ui.get(refer.quickpeek[2]) then
        renderer.text(X / 2 + 24 + scoped_offset-10, Y / 2 +62 +indicator_fade , 255, 255, 255, 255,"-c",0,"QP")
    else
        renderer.text(X / 2 + 24 + scoped_offset-10, Y / 2 +62 +indicator_fade , 255, 255, 255, 50,"-c",0,"QP")
    end

    -------------------> SP INDICATOR <-------------------
    if ui.get(refer.SP) then
    	renderer.text(X / 2  + scoped_offset-10, Y / 2 +62 +indicator_fade , 255, 255, 255, 255,"-c",0,"SP")
    else
     	renderer.text(X / 2 + scoped_offset-10, Y / 2 +62 +indicator_fade , 255, 255, 255, 50,"-c",0,"SP")
    end

    -------------------> BAIM INDICATOR <-------------------
    if ui.get(refer.forcebaim) then
     	renderer.text(X / 2 +11 + scoped_offset-10, Y / 2 +62 +indicator_fade , 255, 255, 255, 255,"-c",0,"BM")
    else
     	renderer.text(X / 2 +11 + scoped_offset-10, Y / 2 +62 +indicator_fade , 255, 255, 255, 50,"-c",0,"BM")
    end

    renderer.text(X / 2 +11 + scoped_offset-10, Y / 2 +80 , 255, 255, 255, 255,"c",0,anti_aim.get_tickbase_shifting())
end


---------------------- AIM EVENTS ----------------------


hit_counter = 0
miss_counter = 0
reason_counter = {}
reason_counter.spread = 0
reason_counter.death = 0
reason_counter.prediction_error = 0
reason_counter.unknown = 0
local chance,bt,predicted_damage,predicted_hitgroup
local hitgroup_names = {"Body", "Head", "Chest", "Stomach", "Left Arm", "Right Arm", "Left Leg", "Right Leg", "Neck", "?", "Gear"}
local pi, max = math.pi, math_max

local dynamic = {}
dynamic.__index = dynamic

function dynamic.new(f, z, r, xi)
   f = max(f, 0.001)
   z = max(z, 0)

   local pif = pi * f
   local twopif = 2 * pif

   local a = z / pif
   local b = 1 / ( twopif * twopif )
   local c = r * z / twopif

   return setmetatable({
      a = a,
      b = b,
      c = c,

      px = xi,
      y = xi,
      dy = 0
   }, dynamic)
end

function dynamic:update(dt, x, dx)
   if dx == nil then
      dx = ( x - self.px ) / dt
      self.px = x
   end

   self.y  = self.y + dt * self.dy
   self.dy = self.dy + dt * ( x + self.c * dx - self.y - self.a * self.dy ) / self.b
   return self
end

function dynamic:get()
   return self.y
end


function aim_fire(e)
    vars.shoot = true
    chance = math_floor(e.hit_chance)
    bt = globals_tickcount() - e.tick
    predicted_damage = e.damage
    predicted_hitgroup = e.hitgroup
end



-------------------> LOCAL ANIMATIONS <------------------- 
animate = function (plocal)
    if not ui_get(gui.menu.lua_enable) then return end
    if not entity_is_alive(plocal) then return end
    local me = ent.get_local_player()
    local m_fFlags = me:get_prop("m_fFlags")
    local is_onground = bit.band(m_fFlags, 1) ~= 0
    local velocity = get_velocity(plocal)
    local on_ground = bit_band(entity_get_prop(plocal, "m_fFlags"), 1) == 1
    if ui_get(gui.menu.air_walk)=="Walk" then
        entity_set_prop(plocal, "m_flPoseParameter", 0, 7)
        if velocity > 4 then
            local my_animlayer = me:get_anim_overlay(6) -- MOVEMENT_MOVE

            my_animlayer.weight = 1
        end
        
    elseif ui_get(gui.menu.air_walk) =="Static" then
        local my_animlayer = me:get_anim_overlay(5) -- MOVEMENT_MOVE
        my_animlayer.weight = 1
    elseif ui_get(gui.menu.air_walk) =="Normal" then

    end
    if table_contains(ui_get(gui.menu.animate),"Static legs") then
        entity_set_prop(plocal, "m_flPoseParameter", 1, 6)
    end
    if table_contains(ui_get(gui.menu.animate),"Pitch 0 on land") then
            if on_ground then
                vars.ground_ticks = vars.ground_ticks + 1
            else
                vars.ground_ticks = 0
                vars.end_time = globals_curtime() + 1
            end
            if vars.ground_ticks > 15 + 1 and vars.end_time > globals_curtime() then
                entity_set_prop(plocal, "m_flPoseParameter", 0.5, 12)
            end
    end
    if table_contains(ui_get(gui.menu.animate),"Leg breaker") then
        if on_ground then
            entity.set_prop(plocal, "m_flPoseParameter", ui_get(refer.quickpeek[2]) and 0 or (globals_tickcount() % 4 > 1 and 0.9 or 1), 0)
        end
    end
end

local native_get_client_entity = vtable_bind("client.dll", "VClientEntityList003", 3, "void*(__thiscall*)(void*, int)")
local animstate_t = ffi.typeof 'struct { char pad0[0x18]; float anim_update_timer; char pad1[0xC]; float started_moving_time; float last_move_time; char pad2[0x10]; float last_lby_time; char pad3[0x8]; float run_amount; char pad4[0x10]; void* entity; void* active_weapon; void* last_active_weapon; float last_client_side_animation_update_time; int	 last_client_side_animation_update_framecount; float eye_timer; float eye_angles_y; float eye_angles_x; float goal_feet_yaw; float current_feet_yaw; float torso_yaw; float last_move_yaw; float lean_amount; char pad5[0x4]; float feet_cycle; float feet_yaw_rate; char pad6[0x4]; float duck_amount; float landing_duck_amount; char pad7[0x4]; float current_origin[3]; float last_origin[3]; float velocity_x; float velocity_y; char pad8[0x4]; float unknown_float1; char pad9[0x8]; float unknown_float2; float unknown_float3; float unknown; float m_velocity; float jump_fall_velocity; float clamped_velocity; float feet_speed_forwards_or_sideways; float feet_speed_unknown_forwards_or_sideways; float last_time_started_moving; float last_time_stopped_moving; bool on_ground; bool hit_in_ground_animation; char pad10[0x4]; float time_since_in_air; float last_origin_z; float head_from_ground_distance_standing; float stop_to_full_running_fraction; char pad11[0x4]; float magic_fraction; char pad12[0x3C]; float world_force; char pad13[0x1CA]; float min_yaw; float max_yaw; } **'

entity.get_animstate = function (ent)
    local pointer = native_get_client_entity(ent)
    if pointer then return ffi.cast(animstate_t, ffi.cast("char*", ffi.cast("void***", pointer)) + 0x9960)[0] end
end
entity.get_simtime = function (ent)
    local pointer = native_get_client_entity(ent)
    if pointer then return entity.get_prop(ent, "m_flSimulationTime"), ffi.cast("float*", ffi.cast("uintptr_t", pointer) + 0x26C)[0] else return 0 end
end
entity.get_max_desync = function (animstate)
    local speedfactor = clamp(animstate.feet_speed_forwards_or_sideways, 0, 1)
    local avg_speedfactor = (animstate.stop_to_full_running_fraction * -0.3 - 0.2) * speedfactor + 1

    local duck_amount = animstate.duck_amount
    if duck_amount > 0 then
        local duck_speed = duck_amount * speedfactor

        avg_speedfactor = avg_speedfactor + (duck_speed * (0.5 - avg_speedfactor))
    end

    return clamp(avg_speedfactor, .5, 1)
end
normalize_yaw = function (yaw) return (yaw + 180) % -360 + 180 end
local player = {}
local resolver = {}
resolver = {
	records = {},
	work = function ()
        local self = resolver
        client.update_player_list()
    
        for i = 1, #players do
            local v = players[i]
            if entity.is_enemy(v) then
                local st_cur, st_pre = entity.get_simtime(v)
                st_cur, st_pre = toticks(st_cur), toticks(st_pre)
    
                local slot = self.records[v] or setmetatable({}, {__mode = "kv"})
                self.records[v] = slot
    
                slot[st_cur] = {
                    pose = entity.get_prop(v, "m_flPoseParameter", 11) * 120 - 60,
                    eye = select(2, entity.get_prop(v, "m_angEyeAngles"))
                }
    
                local value
                local allow = (slot[st_pre] and slot[st_cur]) ~= nil
    
                if allow then
                    local animstate = entity.get_animstate(v)
                    local max_desync = entity.get_max_desync(animstate)
    
                    if (slot[st_pre] and slot[st_cur]) and max_desync < .85 and (st_cur - st_pre < 2) then
                        local side = clamp(normalize_yaw(animstate.goal_feet_yaw - slot[st_cur].eye), -1, 1)
                        value = slot[st_pre] and (slot[st_pre].pose * side * max_desync) or nil
                    end
    
                    if value then plist.set(v, "Force body yaw value", value) end
                end
    
                plist.set(v, "Force body yaw", value ~= nil)
                plist.set(v, "Correction active", true)
            end
        end
    end,
    
	restore = function ()
		local self = resolver
		for i = 1, 64 do
			plist.set(i, "Force body yaw", false)
		end
		self.records = {}
	end,
    client_set_event_callback("resolver", function (this)
        if not this.value then self.restore() end
    end)
}
local callbacks ={
    run_command = function(cmd)
        local plocal = entity_get_local_player()
        local valid = entity_is_alive(plocal)
        if valid then
            vars.defensive = cmd.command_number
            players = entity_get_players()
        end
    end,
    predict_command = function(cmd)
        local plocal = entity_get_local_player()
        local valid = entity_is_alive(plocal)
       
        if ui_get(gui.menu.lua_enable) and valid then
            local threat = client_current_threat()
            local ticks = -ui_get(gui.menu.def_slider)
            if cmd.command_number == vars.defensive then
                local tickbase = entity_get_prop(plocal, "m_nTickBase")
                if vars.tickbase_max ~= nil then
                    vars.defensive_ticks = tickbase - vars.tickbase_max
                    vars.is_defensive = vars.defensive_ticks < ticks

                    if math_abs(tickbase - vars.tickbase_max) > 64 then vars.tickbase_max = 0 end
                end
                vars.tickbase_max = math.max(tickbase, vars.tickbase_max or 0)
            end
        end
    end,
    setup_command = function(cmd)
        local plocal = entity_get_local_player()
        local valid = entity_is_alive(plocal)
       
        if ui_get(gui.menu.lua_enable) and valid then
            local threat = client_current_threat()
            local origin = vector(entity_get_prop(plocal, "m_vecOrigin"))
            tp_vl(cmd,plocal,threat)
            force_defensive(cmd,plocal)
            get_conditions(cmd,plocal)
            count(cmd,plocal)
            aa_handler(cmd,plocal,threat,origin)
            fakelag_on(cmd,plocal)
            force_baim()
        end
        
    end,
    paint = function()
        local plocal = entity_get_local_player()
        local threat = client_current_threat()
        if ui_get(gui.menu.lua_enable) then
            
            trails(plocal,threat)
            if ui_get(gui.menu.auto_tp) and ui_get(gui.menu.auto_tp_key) then
                renderer_indicator(255, 255, 255, 200, "TP")
            end
            
            indicators(plocal)
            defensive_indicator_paint()
        end
    end,
    paint_ui = function()
        hide_controls()
    end,
    shutdown = function()
        set_og_menu(true)
    end,
    pre_render = function()
        local plocal = entity_get_local_player()
        if ui_get(gui.menu.lua_enable) then
        animate(plocal)
        end
    end,
    level_init = function()
        flick_manager.data = {}
        vars.tick_var1 = 0
        vars.tick_var2 = 0
        vars.tick_var3 = 0
        vars.tick_var4 = 0
        vars.tick_var5 = 0
        vars.tick_var6 = 0
        vars.tick_var7 = 0
        vars.ground_ticks = 1
        vars.defensive_ticks = 0
        vars.tickbase_max, vars.defensive = 0, 0
        cheked_ticks = 0
    end,
    net_update_start = function()
        local plocal = entity_get_local_player()
    end,
    net_update_end = function()
        local plocal = entity_get_local_player()
        local valid = entity_is_alive(plocal)
        if ui_get(gui.menu.lua_enable) and valid then
            resolver.work()
        end
    end,
}
local function on_init()
    client_set_event_callback("setup_command", callbacks.setup_command)
    client_set_event_callback("run_command", callbacks.run_command)
    client_set_event_callback("predict_command", callbacks.predict_command)
    --client_set_event_callback("net_update_start", callbacks.net_update_start)
    client_set_event_callback("net_update_end", callbacks.net_update_end)
    client_set_event_callback("level_init", callbacks.level_init)
    client_set_event_callback("paint_ui", callbacks.paint_ui)
    client_set_event_callback("paint", callbacks.paint)
    client_set_event_callback("shutdown", callbacks.shutdown)
    client_set_event_callback("pre_render", callbacks.pre_render)
    client_set_event_callback("aim_fire", aim_fire)
end
on_init() 

Lua online compiler

Write, Run & Share Lua code online using OneCompiler's Lua online compiler for free. It's one of the robust, feature-rich online compilers for Lua language, running the latest Lua version 5.4. Getting started with the OneCompiler's Lua editor is easy and fast. The editor shows sample boilerplate code when you choose language as Lua and start coding.

Taking inputs (stdin)

OneCompiler's Lua online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample Lua program which takes name as input and prints hello message with your name.

name = io.read("*a")
print ("Hello ", name)

About Lua

Lua is a light weight embeddable scripting language which is built on top of C. It is used in almost all kind of applications like games, web applications, mobile applications, image processing etc. It's a very powerful, fast, easy to learn, open-source scripting language.

Syntax help

Variables

  • By default all the variables declared are global variables
  • If the variables are explicitly mentioned as local then they are local variables.
  • Lua is a dynamically typed language and hence only the values will have types not the variables.

Examples

-- global variables
a = 10

-- local variables

local x = 30
Value TypeDescription
numberRepresents numbers
stringRepresents text
nilDifferentiates values whether it has data or not
booleanValue can be either true or false
functionRepresents a sub-routine
userdataRepresents arbitary C data
threadRepresents independent threads of execution.
tableCan hold any value except nil

Loops

1. While:

While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.

while(condition)
do
--code
end

2. Repeat-Until:

Repeat-Until is also used to iterate a set of statements based on a condition. It is very similar to Do-While, it is mostly used when you need to execute the statements atleast once.

repeat
   --code
until( condition )

3. For:

For loop is used to iterate a set of statements based on a condition.

for init,max/min value, increment
do
   --code
end

Functions

Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increase re-usuability and modularity.

optional_function_scope function function_name( argument1, argument2, argument3........, argumentn)
--code
return params with comma seperated
end