script_name("Ap3x Menu")
script_author("Ap3x")
script_version("4.2.0")
local ab = {
	tog = {true,true,true,true,true,true,true,true,true}
}
if getMoonloaderVersion() >= 27 then
	require 'libstd.deps' {
	   'fyp:mimgui',
	   'fyp:fa-icons-4',
	   --'donhomka:mimgui-addons',
	   'donhomka:extensions-lite'
	}
end


function keycheck(k)
    local r = true
    for i = 1, #k.k do
        r = r and PressType[k.t[i]](k.k[i])
    end
    return r
end

function isKeyControlAvailable()
	if not isSampLoaded() then return true end
	if not isSampfuncsLoaded() then return not sampIsChatInputActive() and not sampIsDialogActive() end
	return not sampIsChatInputActive() and not sampIsDialogActive()
end

function getClosestPlayerId_2(maxdist, ArmorCheck)
	local GangSkins = {136, 102, 103, 104, 190, 296, 13, 44, 90, 114, 115, 116, 173, 174, 175, 143, 176, 177, 297, 69, 21}
	local i = -1
	local maxplayerid = sampGetMaxPlayerId(false)
    for i = 0, maxplayerid do
		if sampIsPlayerConnected(i) then
			local result, ped = sampGetCharHandleBySampPlayerId(i)
			if result and not sampIsPlayerPaused(i) then
				local dist = get_distance_to_player(i)
				if (dist < maxdist and sampGetPlayerArmor(i) < 48) then
					if GangSkins then
						if has_value(GangSkins, getCharModel(ped)) then
							return i
						end
					else
						return i
					end
				end
			end
		end
    end
	return i
end

function get_distance_to_player(playerId)
	local dist = -0
	if sampIsPlayerConnected(playerId) then
		local result, ped = sampGetCharHandleBySampPlayerId(playerId)
		if result then
			local myX, myY, myZ = getCharCoordinates(playerPed)
			local playerX, playerY, playerZ = getCharCoordinates(ped)
			dist = getDistanceBetweenCoords3d(myX, myY, myZ, playerX, playerY, playerZ)
			return dist
		end
	end
	return dist
end

function has_value (tab, val)
    for index, value in ipairs(tab) do
        if value == val then
            return true
        end
    end

    return false
end

 -- IDAN END

require"lib.moonloader"
require"lib.sampfuncs"
require 'extensions-lite'


--sampAddChatMessage("{005559}[ A{01c28b}p{a9d878}3{fec063}x {f46c45}M{f3124b}e{a42d5d}n{73035e}u ]    {5ebd3e}---LOADED---")


local imgui, ffi = require 'mimgui', require 'ffi'
local new, str = imgui.new, ffi.string
local ped, h = playerPed, playerHandle
--local vk = require 'vkeys'
local keys  = require 'game.keys'
local sampev = require 'lib.samp.events'
local encoding = require 'encoding'
encoding.default = 'CP1251'
local u8 = encoding.UTF8
--local mimgui_addons = require 'mimgui_addons'
local faicons = require 'fa-icons'
local mainc = imgui.ImVec4(0.00, 0.00, 0.00, 1.0) -- Pink

local blank = {}

local show = new.bool()
local captog = false
local autofind, cooldown = false, false


local items = {'first','second','third','fourth'} -- List
local chosen_items = {} -- An array where the selected elements of the list will be written

local chosen = new.bool(false) -- Needed to show whether a certain item is selected or not


path = getWorkingDirectory() .. '\\config\\' 
cfg = path .. 'autobind.ini'

function apply_custom_style()
   local style = imgui.GetStyle()
   local colors = style.Colors
   local clr = imgui.Col
   local ImVec4 = imgui.ImVec4
   style.WindowRounding = 1.5
   style.WindowTitleAlign = imgui.ImVec2(0.5, 0.5)
   style.FrameRounding = 1.0
   style.ItemSpacing = imgui.ImVec2(4.0, 4.0)
   style.ScrollbarSize = 13.0
   style.ScrollbarRounding = 0
   style.GrabMinSize = 8.0
   style.GrabRounding = 1.0
   style.WindowBorderSize = 0.0
   style.WindowPadding = imgui.ImVec2(5.0, 5.0)
   style.FramePadding = imgui.ImVec2(2.0, 2.0)
   style.ButtonTextAlign = imgui.ImVec2(0.5, 0.35)
 
   colors[clr.Text]                   = ImVec4(1.00, 1.00, 1.00, 1.00)
   colors[clr.TextDisabled]           = ImVec4(0.7, 0.7, 0.7, 1.0)
   colors[clr.WindowBg]               = ImVec4(0.05, 0.05, 0.05, 0.7)
   colors[clr.PopupBg]                = ImVec4(0.08, 0.08, 0.08, 0.94)
   colors[clr.Border]                 = ImVec4(mainc.x, mainc.y, mainc.z, 0.4)
   colors[clr.BorderShadow]           = ImVec4(0.00, 0.00, 0.00, 0.00)
   colors[clr.FrameBg]                = ImVec4(mainc.x, mainc.y, mainc.z, 0.7)
   colors[clr.FrameBgHovered]         = ImVec4(mainc.x, mainc.y, mainc.z, 0.4)
   colors[clr.FrameBgActive]          = ImVec4(mainc.x, mainc.y, mainc.z, 0.9)
   colors[clr.TitleBg]                = ImVec4(mainc.x, mainc.y, mainc.z, 1.0)
   colors[clr.TitleBgActive]          = ImVec4(mainc.x, mainc.y, mainc.z, 1.0)
   colors[clr.TitleBgCollapsed]       = ImVec4(mainc.x, mainc.y, mainc.z, 0.79)
   colors[clr.MenuBarBg]              = ImVec4(0.14, 0.14, 0.14, 1.00)
   colors[clr.ScrollbarBg]            = ImVec4(0.02, 0.02, 0.02, 0.53)
   colors[clr.ScrollbarGrab]          = ImVec4(mainc.x, mainc.y, mainc.z, 0.8)
   colors[clr.ScrollbarGrabHovered]   = ImVec4(0.41, 0.41, 0.41, 1.00)
   colors[clr.ScrollbarGrabActive]    = ImVec4(0.51, 0.51, 0.51, 1.00)
   colors[clr.CheckMark]              = ImVec4(mainc.x + 0.13, mainc.y + 0.13, mainc.z + 0.13, 1.00)
   colors[clr.SliderGrab]             = ImVec4(0.28, 0.28, 0.28, 1.00)
   colors[clr.SliderGrabActive]       = ImVec4(0.35, 0.35, 0.35, 1.00)
   colors[clr.Button]                 = ImVec4(mainc.x, mainc.y, mainc.z, 0.8)
   colors[clr.ButtonHovered]          = ImVec4(mainc.x, mainc.y, mainc.z, 0.63)
   colors[clr.ButtonActive]           = ImVec4(mainc.x, mainc.y, mainc.z, 1.0)
   colors[clr.Header]                 = ImVec4(mainc.x, mainc.y, mainc.z, 0.6)
   colors[clr.HeaderHovered]          = ImVec4(mainc.x, mainc.y, mainc.z, 0.43)
   colors[clr.HeaderActive]           = ImVec4(mainc.x, mainc.y, mainc.z, 0.8)
   colors[clr.Separator]              = colors[clr.Border]
   colors[clr.SeparatorHovered]       = ImVec4(0.26, 0.59, 0.98, 0.78)
   colors[clr.SeparatorActive]        = ImVec4(0.26, 0.59, 0.98, 1.00)
   colors[clr.ResizeGrip]             = ImVec4(mainc.x, mainc.y, mainc.z, 0.8)
   colors[clr.ResizeGripHovered]      = ImVec4(mainc.x, mainc.y, mainc.z, 0.63)
   colors[clr.ResizeGripActive]       = ImVec4(mainc.x, mainc.y, mainc.z, 1.0)
   colors[clr.PlotLines]              = ImVec4(0.61, 0.61, 0.61, 1.00)
   colors[clr.PlotLinesHovered]       = ImVec4(1.00, 0.43, 0.35, 1.00)
   colors[clr.PlotHistogram]          = ImVec4(0.90, 0.70, 0.00, 1.00)
   colors[clr.PlotHistogramHovered]   = ImVec4(1.00, 0.60, 0.00, 1.00)
   colors[clr.TextSelectedBg]         = ImVec4(0.26, 0.59, 0.98, 0.35)
 end

--imgui.OnInitialize() called only once, before the first render
imgui.OnInitialize(function()
	apply_custom_style() -- apply custom style
	local defGlyph = imgui.GetIO().Fonts.ConfigData.Data[0].GlyphRanges
	imgui.GetIO().Fonts:Clear() -- clear the fonts
	local font_config = imgui.ImFontConfig() -- each font has its own config
	font_config.SizePixels = 15.0;
	font_config.GlyphExtraSpacing.x = 0.1
	-- main font
	local def = imgui.GetIO().Fonts:AddFontFromFileTTF(getFolderPath(0x14) .. '\\Gisha.ttf', font_config.SizePixels, font_config, defGlyph)
   
	local config = imgui.ImFontConfig()
	config.MergeMode = true
	config.PixelSnapH = true
	config.FontDataOwnedByAtlas = false
	config.GlyphOffset.y = 1.0 -- offset 1 pixel from down
	local fa_glyph_ranges = new.ImWchar[3]({ faicons.min_range, faicons.max_range, 0 })
	-- icons
	local faicon = imgui.GetIO().Fonts:AddFontFromMemoryCompressedBase85TTF(faicons.get_font_data_base85(), font_config.SizePixels, config, fa_glyph_ranges)

	imgui.GetIO().ConfigWindowsMoveFromTitleBarOnly = true
	imgui.GetIO().IniFilename = nil
end)

imgui.OnFrame(function() return show[0] end,
function()
	local width, height = getScreenResolution()
	imgui.SetNextWindowPos(imgui.ImVec2(width / 2, height / 2), imgui.Cond.FirstUseEver, imgui.ImVec2(0.5, 0.5))
	--imgui.SetNextWindowSize(imgui.ImVec2(290, 250), imgui.Cond.FirstUseEver)
	imgui.Begin(faicons.ICON_TASKS .. ' Ap3xMenu', show, imgui.WindowFlags.NoCollapse + imgui.WindowFlags.NoResize)
		if imgui.Button('Default') then blankIni() end
		imgui.SameLine()
		if imgui.Button('Save') then saveIni() end
		imgui.SameLine()
		if imgui.Checkbox('Radio', new.bool(ab.tog[6])) then ab.tog[6] = not ab.tog[6] end
		if imgui.Checkbox('Turf Capture (/tcap)', new.bool(ab.tog[1])) then ab.tog[1] = not ab.tog[1] end
		imgui.SameLine()
		if imgui.Checkbox('Turf Capture Spam (/tcs)', new.bool(captog)) then captog = not captog end
		if imgui.Checkbox('Point Capture (/pcap)', new.bool(ab.tog[2])) then cmd_pcap() end
		if imgui.Checkbox('Auto Accept Bodyguard (/av)', new.bool(ab.tog[3])) then cmd_av() end
		if imgui.Checkbox('Auto Send Bodyguard (/asv)', new.bool(ab.tog[9])) then cmd_asv() end
		if imgui.Checkbox('Auto Accept Sex (/aas)', new.bool(ab.tog[4])) then cmd_aas() end
		if imgui.Checkbox('Auto Accept Repair (/aar)', new.bool(ab.tog[5])) then cmd_aar() end
		if imgui.Checkbox('Auto Accept Weapon (/aaw)', new.bool(ab.tog[7])) then cmd_aaw() end
		if imgui.Checkbox('Diamond Donator Vest (/ddv)', new.bool(ab.tog[8])) then ab.tog[8] = not ab.tog[8] end
	imgui.End()
end)

--Turf Cap
function cmd_tcap()
	if ab.tog[1] == false then
		ab.tog[1] = true
		sampAddChatMessage("{005559}[ A{01c28b}p{a9d878}3{fec063}x {f46c45}M{f3124b}e{a42d5d}n{73035e}u ] {FFFF00} Auto Turf Capture is - {35fc03}[ON]")
		else
		ab.tog[1] = false
		sampAddChatMessage("{005559}[ A{01c28b}p{a9d878}3{fec063}x {f46c45}M{f3124b}e{a42d5d}n{73035e}u ] {FFFF00} Auto Turf Capture is - {fc033d}[OFF]")
	end
end

--Point Cap
function cmd_pcap()
	if ab.tog[2] == false then
		ab.tog[2] = true
		sampAddChatMessage("{005559}[ A{01c28b}p{a9d878}3{fec063}x {f46c45}M{f3124b}e{a42d5d}n{73035e}u ] {FFFF00} Auto Point Capture is - {35fc03}[ON]")
		else
		ab.tog[2] = false
		sampAddChatMessage("{005559}[ A{01c28b}p{a9d878}3{fec063}x {f46c45}M{f3124b}e{a42d5d}n{73035e}u ] {FFFF00} Auto Point Capture is - {fc033d}[OFF]")
	end
end

--Accept Vest
function cmd_av()
	if ab.tog[3] == false then
		ab.tog[3] = true
		sampAddChatMessage("{005559}[ A{01c28b}p{a9d878}3{fec063}x {f46c45}M{f3124b}e{a42d5d}n{73035e}u ] {33ccff} Auto Accept Vest is - {35fc03}[ON]")
		else
		ab.tog[3] = false
		sampAddChatMessage("{005559}[ A{01c28b}p{a9d878}3{fec063}x {f46c45}M{f3124b}e{a42d5d}n{73035e}u ] {33ccff} Auto Accept Vest is - {fc033d}[OFF]")
	end
end

-- Send Vest
function cmd_asv()
    ab.tog[9] = not ab.tog[9]
	if ab.tog[9] then
		sampAddChatMessage("{005559}[ A{01c28b}p{a9d878}3{fec063}x {f46c45}M{f3124b}e{a42d5d}n{73035e}u ] {33ccff} Auto Send Vest is - {35fc03}[ON]", -1)
	else
		sampAddChatMessage("{005559}[ A{01c28b}p{a9d878}3{fec063}x {f46c45}M{f3124b}e{a42d5d}n{73035e}u ] {33ccff} Auto Send Vest is - {fc033d}[OFF]", -1)
	end
end

-- Accept Sex
function cmd_aas()
    ab.tog[8] = not ab.tog[8]
	if ab.tog[8] then
		sampAddChatMessage("{005559}[ A{01c28b}p{a9d878}3{fec063}x {f46c45}M{f3124b}e{a42d5d}n{73035e}u ] {33ccff} Auto Accept Sex is - {35fc03}[ON]", -1)
	else
		sampAddChatMessage("{005559}[ A{01c28b}p{a9d878}3{fec063}x {f46c45}M{f3124b}e{a42d5d}n{73035e}u ] {33ccff} Auto Accept Sex is - {fc033d}[OFF]", -1)
	end
end

-- Accept Repair
function cmd_aar()
    ab.tog[5] = not ab.tog[5]
	if ab.tog[5] then
		sampAddChatMessage("{005559}[ A{01c28b}p{a9d878}3{fec063}x {f46c45}M{f3124b}e{a42d5d}n{73035e}u ] {33ccff} Auto Accept Car Repair is - {35fc03}[ON]", -1)
	else
		sampAddChatMessage("{005559}[ A{01c28b}p{a9d878}3{fec063}x {f46c45}M{f3124b}e{a42d5d}n{73035e}u ] {33ccff} Auto Accept Car Repair is - {fc033d}[OFF]", -1)
	end
end

-- Accept Weapon
function cmd_aaw()
    ab.tog[7] = not ab.tog[7]
	if ab.tog[7] then
		sampAddChatMessage("{005559}[ A{01c28b}p{a9d878}3{fec063}x {f46c45}M{f3124b}e{a42d5d}n{73035e}u ] {33ccff} Auto Accept Weapon is - {35fc03}[ON]", -1)
	else
		sampAddChatMessage("{005559}[ A{01c28b}p{a9d878}3{fec063}x {f46c45}M{f3124b}e{a42d5d}n{73035e}u ] {33ccff} Auto Accept Weapon is - {fc033d}[OFF]", -1)
	end
end

-- Diamond Donators Send Vest
function cmd_ddv()
    ab.tog[8] = not ab.tog[8]
	if ab.tog[8] then
		sampAddChatMessage("{005559}[ A{01c28b}p{a9d878}3{fec063}x {f46c45}M{f3124b}e{a42d5d}n{73035e}u ] {33ccff}Auto Send Vest {7aa8d7}(DD Only) is - {35fc03}[ON]", -1)
	else
		sampAddChatMessage("{005559}[ A{01c28b}p{a9d878}3{fec063}x {f46c45}M{f3124b}e{a42d5d}n{73035e}u ] {33ccff}Auto Send Vest {7aa8d7}(DD Only) is - {fc033d}[OFF]")
	end
end

function main()
	blank = table.deepcopy(ab)
	if not doesDirectoryExist(path) then createDirectory(path) end
	if doesFileExist(cfg) then loadIni() else blankIni() end
	while not isSampAvailable() do wait(100) end
	sampRegisterChatCommand("abhelp", function()
		sampAddChatMessage("{005559}==========================  [ A{01c28b}p{a9d878}3{fec063}x {f46c45}M{f3124b}e{a42d5d}n{73035e}u ]  ==========================", -1)
		sampAddChatMessage ("{01c28b}/ab - {f3124b}Opens the menu.",-1)
		sampAddChatMessage ("{01c28b}/av - {f3124b}Auto accepts vest.",-1)
		sampAddChatMessage ("{01c28b}/aas - {f3124b}Auto accepts sex.",-1)
		sampAddChatMessage ("{01c28b}/aar - {f3124b}Auto accepts car repair.",-1)
		sampAddChatMessage ("{01c28b}/aaw - {f3124b}Auto accepts weapon. ({FFFFFF}Deagle, Sniper, SPAS-12, M4 Only{f3124b}).",-1)
		sampAddChatMessage ("{01c28b}/ddv - {f3124b}Auto sends vest to allies with /guardnear ({7aa8d7}For Diamond Donators Only{f3124b}).",-1)
		sampAddChatMessage ("{01c28b}/asv - {f3124b}Auto sends vest to allies.",-1)
		sampAddChatMessage ("{01c28b}/hf - {f3124b}Auto finds players.",-1)
		sampAddChatMessage ("{01c28b}/pcap - {f3124b}Auto captures point.",-1)
		sampAddChatMessage ("{01c28b}/tcap - {f3124b}Auto captures turf.",-1)
		sampAddChatMessage ("{01c28b}/tcs - {f3124b}Spams turf capture.",-1)
		sampAddChatMessage ("{01c28b}/vestnear - {f3124b}Offers a vest to the nearest person for 200$.",-1)
		sampAddChatMessage ("{01c28b}/repairnear - {f3124b}Offers car repair to the nearest person.",-1)
		sampAddChatMessage ("{01c28b}/sexnear - {f3124b}Offers sex to the nearest person.",-1)
		sampAddChatMessage ("{01c28b}/level1s, /level2s, /level3s - {f3124b}Shows online level 1,2,3 players.",-1)
		sampAddChatMessage("{005559}================================{73035e}================================", -1)



	end)

	--sampAddChatMessage("{005559}[ A{01c28b}p{a9d878}3{fec063}x {f46c45}M{f3124b}e{a42d5d}n{73035e}u ]    {5ebd3e}---LOADED---", -1)

	sampAddChatMessage("{005559}======  [ A{01c28b}p{a9d878}3{fec063}x {f46c45}M{f3124b}e{a42d5d}n{73035e}u ]  ====== {01c28b}- /ab to open, /abhelp for commands. ", -1)

	
    sampfuncsLog("(AutoBind: /ab)")
	
	sampRegisterChatCommand("tcap", cmd_tcap)
	sampRegisterChatCommand("pcap", cmd_pcap)
	sampRegisterChatCommand("av", cmd_av)
	sampRegisterChatCommand("asv", cmd_asv)
	sampRegisterChatCommand("aas", cmd_aas)
	sampRegisterChatCommand("aar", cmd_aar)
	sampRegisterChatCommand("aaw", cmd_aaw)
	sampRegisterChatCommand("ddv", cmd_ddv)

	sampRegisterChatCommand('ab', function() show[0] = not show[0] end)
	sampRegisterChatCommand("vestnear", function() 
		i = getClosestPlayerId(7, 1)
		if i ~= -1 then
			sampSendChat(string.format("/guard %d 200", i))
		end 
	end)
	sampRegisterChatCommand("sexnear", function() 
		i = getClosestPlayerId(5, 2)
		if i ~= -1 and isCharInAnyCar(ped) then
			sampSendChat(string.format("/sex %d 1", i))
		end
	end)
	sampRegisterChatCommand("repairnear", function()
		i = getClosestPlayerId(5, 3)
		if i ~= -1 then
			sampSendChat(string.format("/repair %d 1", i))
		end
	end)
	sampRegisterChatCommand("hf", function(params) -- Scumbag Locator
		lua_thread.create(function()
			if string.len(params) > 0 then
				local result, playerid, name = getTarget(params)
				if result then
					if not autofind then
						target = playerid
						autofind = true
						sampAddChatMessage("{005559}[ A{01c28b}p{a9d878}3{fec063}x {f46c45}M{f3124b}e{a42d5d}n{73035e}u ]{FFFFFF} FINDING: {00a2ff}"..name.."{ffffff}. /hf to stop", -1)
						while autofind and not cooldown do
							wait(10)
							if sampIsPlayerConnected(target) then
								cooldown = true
								sampSendChat("/find "..target)
								wait(19000)
								cooldown = false
							else
								autofind = false
								sampAddChatMessage("{005559}[ A{01c28b}p{a9d878}3{fec063}x {f46c45}M{f3124b}e{a42d5d}n{73035e}u ]{FFFFFF} The player you were finding has disconnected, you are no longer finding anyone.", -1)
							end
						end
					elseif autofind then
						target = playerid
						sampAddChatMessage("{005559}[ A{01c28b}p{a9d878}3{fec063}x {f46c45}M{f3124b}e{a42d5d}n{73035e}u ]{FFFFFF} NOW FINDING: {00a2ff}"..name.."{ffffff}.", -1)
					end
				else
					sampAddChatMessage("{005559}[ A{01c28b}p{a9d878}3{fec063}x {f46c45}M{f3124b}e{a42d5d}n{73035e}u ]{FFFFFF} Invalid player specified.", 11645361)
				end
			elseif autofind and string.len(params) == 0 then
				autofind = false
				sampAddChatMessage("{005559}[ A{01c28b}p{a9d878}3{fec063}x {f46c45}M{f3124b}e{a42d5d}n{73035e}u ]{FFFFFF} You are no longer finding anyone.", -1)
			else
				sampAddChatMessage('{005559}[ A{01c28b}p{a9d878}3{fec063}x {f46c45}M{f3124b}e{a42d5d}n{73035e}u ]{FFFFFF} USAGE: /hf [playerid/partofname]', -1)
			end
		end)
	end)
	
	sampRegisterChatCommand("level1s", function() 
		for i = 0, sampGetMaxPlayerId(false) do
			if sampIsPlayerConnected(i) and sampGetPlayerScore(i) == 1 then
				sampAddChatMessage("ID: " ..i.. " | Name: "..sampGetPlayerNickname(i).." | Level: "..sampGetPlayerScore(i).." | Ping: "..sampGetPlayerPing (i), -1)
			end
		end
	end)

	sampRegisterChatCommand("level2s", function() 
		for i = 0, sampGetMaxPlayerId(false) do
			if sampIsPlayerConnected(i) and sampGetPlayerScore(i) == 2 then
				sampAddChatMessage("ID: " ..i.. " | Name: "..sampGetPlayerNickname(i).." | Level: "..sampGetPlayerScore(i).." | Ping: "..sampGetPlayerPing (i), -1)
			end
		end
	end)

	sampRegisterChatCommand("level3s", function() 
		for i = 0, sampGetMaxPlayerId(false) do
			if sampIsPlayerConnected(i) and sampGetPlayerScore(i) == 3 then
				sampAddChatMessage("ID: " ..i.. " | Name: "..sampGetPlayerNickname(i).." | Level: "..sampGetPlayerScore(i).." | Ping: "..sampGetPlayerPing (i), -1)
			end
		end
	end)

	sampRegisterChatCommand("tcs", function() captog = not captog end)
	lua_thread.create(function()
		while true do
			wait(100)
			if isKeyControlAvailable() then
				if ab.tog[9] then -- Auto vest
					playerid = getClosestPlayerId_2(7, true)
					if playerid ~= -1 then
						sampSendChat(string.format("/vest %d 200", playerid))
						wait(12500)
					end
				end
				if ab.tog[8] then -- Guard near (DD)
					local playerid = getClosestPlayerId_2(7, true)
					if playerid ~= -1 then
						sampAddChatMessage("{005559}[ A{01c28b}p{a9d878}3{fec063}x {f46c45}M{f3124b}e{a42d5d}n{73035e}u ] - offered protection to "..sampGetPlayerNickname(playerid).."("..playerid..") for $200.")
						sampSendChat("/guardnear")
						wait(12500)
					end
				end
			end
		end
	end)
	
	while true do wait(0) 
		if captog then 
            sampAddChatMessage("{005559}[ A{01c28b}p{a9d878}3{fec063}x {f46c45}M{f3124b}e{a42d5d}n{73035e}u ] {FFFF00}Starting capture spam... (/tcs to toggle)",-1)
            while captog do
                sampSendChat("/capturf")
                wait(1500) 
            end 
            sampAddChatMessage("{005559}[ A{01c28b}p{a9d878}3{fec063}x {f46c45}M{f3124b}e{a42d5d}n{73035e}u ] {FFFF00}Capture spam ended.",-1)
        end
	
		if not ab.tog[6] and isCharInAnyCar(ped) and getRadioChannel() < 12 then 
			setRadioChannel(12) 
		end 
	end
	
	
end


function sampev.onServerMessage(color, text)
    if string.find(text, "has taken control of the") then
		ab.tog[3] = false
	end
	
	if string.find(text, "The time is now") then 
		if ab.tog[1] then 
			sampSendChat("/capturf") 
			--ab.tog[1] = false 
		end 
		if ab.tog[2] then 
			sampSendChat("/capture") 
			--ab.tog[2] = false 
		end

	--AUTO ACCEPT BODYGUARD--	
	elseif string.find(text, "wants to protect you for $200, type /accept bodyguard to accept.") and ab.tog[3] then 
		sampSendChat("/accept bodyguard")
--		sampSendChat("/me replaced ragged vest with a new one.")

	--AUTO ACCEPT SEX--	
	elseif string.find(text, "has Offered you to have Sex with them, for $1") and ab.tog[4] then 
		sampSendChat("/accept sex")

	--AUTO ACCEPT REPAIR--	
	elseif string.find(text, "wants to repair your car for $1") and ab.tog[5] then 
		sampSendChat("/accept repair") 
	
	--AUTO ACCEPT WEAPON--	
	--Deagle
	elseif string.find(text, " deagle for $0, ") and ab.tog[7] then 
		sampSendChat("/accept weapon") 
	elseif string.find(text, " deagle for $1, ") and ab.tog[7] then 
		sampSendChat("/accept weapon") 
	--Sniper
	elseif string.find(text, " sniper for $0, ") and ab.tog[7] then 
		sampSendChat("/accept weapon") 
	elseif string.find(text, " sniper for $1, ") and ab.tog[7] then 
		sampSendChat("/accept weapon")
	--M4
	elseif string.find(text, " m4 for $0, ") and ab.tog[7] then 
		sampSendChat("/accept weapon") 
	elseif string.find(text, " m4 for $1, ") and ab.tog[7] then 
		sampSendChat("/accept weapon") 
	--Spas12
	elseif string.find(text, " spas12 for $0, ") and ab.tog[7] then 
		sampSendChat("/accept weapon") 
	elseif string.find(text, " spas12 for $1, ") and ab.tog[7] then 
		sampSendChat("/accept weapon") 
	end
	

		if string.find(text, "wants to protect you for $200, type /accept bodyguard to accept.") then
			sampAddChatMessage("{005559}[A{01c28b}p{a9d878}3{fec063}x {f46c45}M{f3124b}e{a42d5d}n{73035e}u] {33ccff}* Bodyguard wants to protect you for 200$, {5ebd3e}ACCEPTED.",-1)		
		end
	
		if string.find(text, " for $0, ") then
			sampAddChatMessage("{005559}[ A{01c28b}p{a9d878}3{fec063}x {f46c45}M{f3124b}e{a42d5d}n{73035e}u ] {33ccff}* Arms Dealer wants to sell you a gun for $0, {5ebd3e}ACCEPTED.",-1)		
		end
	
	return {color, text}
	--return false
	--[[
	-- Removes blank messages
	--if string.len(text) < 1 then
--	end
--]]
end

function getClosestPlayerId(maxdist, type)
	for i = 0, sampGetMaxPlayerId(false) do
        local getted, remotePlayer = sampGetCharHandleBySampPlayerId(i)
        if getted and not sampIsPlayerPaused(i) then
			local remotePlayerX, remotePlayerY, remotePlayerZ = getBodyPartCoordinates(8, remotePlayer);
            local myPosX, myPosY, myPosZ = getCharCoordinates(playerPed)
            local dist = getDistanceBetweenCoords3d(remotePlayerX, remotePlayerY, remotePlayerZ, myPosX, myPosY, myPosZ)
            if dist <= maxdist then
				if type == 1 then
					return i 
				elseif type == 2 and getCharArmour(ped) < 48 then 
					return i
				elseif type == 3 and not isCharInAnyCar(ped) and isCharInAnyCar(remotePlayer) then 
					return i
				end
			end
		end
    end
	return -1
end

function getTarget(str)
	local maxplayerid, players, name, target = sampGetMaxPlayerId(false), {}, nil, nil
	for i = 0, maxplayerid do
		if sampIsPlayerConnected(i) then
   			players[i] = sampGetPlayerNickname(i)
		end
	end
	for k, v in pairs(players) do
		if v:lower():match(str:lower()) or string.match(k, str) then
			playerid = k
			playername = players[k]:gsub("_", " ")
			return true, playerid, playername
		elseif k == maxplayerid then
			return false
		end
	end
end

getBonePosition = ffi.cast("int (__thiscall*)(void*, float*, int, bool)", 0x5E4280)
function getBodyPartCoordinates(id, handle)
	local pedptr = getCharPointer(handle)
	local vec = ffi.new("float[3]")
	getBonePosition(ffi.cast("void*", pedptr), vec, id, true)
	return vec[0], vec[1], vec[2]
end

function blankIni()
	ab = table.deepcopy(blank)
	saveIni()
	loadIni()
end

function loadIni()
	local f = io.open(cfg, "r")
	if f then
		ab = decodeJson(f:read("*all"))
		f:close()
	end
end

function saveIni()
	if type(ab) == "table" then
		local f = io.open(cfg, "w")
		f:close()
		if f then
			f = io.open(cfg, "r+")
			f:write(encodeJson(ab))
			f:close()
		end
	end
end 

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