script_name('Hitman Helper for CRP')
script_author('arcanum')
script_description('Hitman Helper is a script created specially for Capital Role Play')

require "lib.moonloader"
local imgui  = require 'imgui'
local sampev = require 'lib.samp.events'
local encoding = require 'encoding'
local inicfg = require 'inicfg'
encoding.default = 'CP1251'
u8 = encoding.UTF8

-- IMGUI
local find_bool = imgui.ImBool(false)
local settings_bool = imgui.ImBool(false)

-- INFO ABOUT ORDER
local find_id = imgui.ImBuffer(256)
local find_name = imgui.ImBuffer(256)
local find_amount = imgui.ImBuffer(256)

-- ETC

local lastwep = 0

-- SETTINGS

local choosed_style = imgui.ImInt(1)
local autoradiowhenreceived = imgui.ImBool(true)
local autoradiowhencanceled = imgui.ImBool(true)
local autoradiowhencompleted = imgui.ImBool(true)
local menuwhenkilled = imgui.ImBool(true)
local autoguns = imgui.ImBool(true)
local hideneworders = imgui.ImBool(false)

local accentcolor = nil

local pathIni = "moonloader\\config\\hitmanhelper.ini"
local mainIni = nil

local mainIniDefault = {
	Settings = {
		Theme = "2",
		AutoRadioWhenReceived = "1",
		AutoRadioWhenCanceled = "1",
		AutoRadioWhenCompleted = "1",
		MenuWhenKilled = "1",
		AutoGuns = "1",
		HideNewOrders = "0"
	}
}

function reloadIni()
	if not doesFileExist(pathIni) then inicfg.save(mainIniDefault, "hitmanhelper.ini") end

	mainIni = inicfg.load(nil, pathIni)

	choosed_style.v = mainIni.Settings.Theme
	if mainIni.Settings.AutoRadioWhenReceived == 1 then autoradiowhenreceived.v = true else autoradiowhenreceived.v = false end
	if mainIni.Settings.AutoRadioWhenCanceled == 1 then autoradiowhencanceled.v = true else autoradiowhencanceled.v = false end
	if mainIni.Settings.AutoRadioWhenCompleted == 1 then autoradiowhencompleted.v = true else autoradiowhencompleted.v = false end
	if mainIni.Settings.AutoGuns == 1 then autoguns.v = true else autoguns.v = false end
	if mainIni.Settings.HideNewOrders == 1 then hideneworders.v = true else hideneworders.v = false end

	if mainIni.Settings.Theme == 1 then
		orangestyle()
		accentcolor = "cc5400"
	end
	if mainIni.Settings.Theme == 2 then
		redstyle()
		accentcolor = "8f2424"
	end
	if mainIni.Settings.Theme == 3 then
		violetstyle()
		accentcolor = "7f43ba"
	end	
	if mainIni.Settings.Theme == 4 then
		blackwhitestyle()
		accentcolor = "b3b3b3"
	end
end

function main()
	if not isSampLoaded() or not isSampfuncsLoaded() then return end
	while not isSampAvailable() do wait(100) end
		reloadIni()
		sampAddChatMessage(string.format("{%s}Hitman Helper {FFFFFF}загружен. Автор: {%s}arcanum", accentcolor, accentcolor), -1)
		sampRegisterChatCommand("hsettings", settings_switcher)
		if mainIni.Settings.AutoGuns == 1 then lua_thread.create(rpguns) end
	while true do
		wait(0)
		if isKeyJustPressed(VK_F2) then
			refind()
		end
	end
end

function rpguns()
	while true do
		wait(1000)
		if lastwep ~= getCurrentCharWeapon(PLAYER_PED) and mainIni.Settings.AutoGuns == 1 then
			local wep = getCurrentCharWeapon(PLAYER_PED)
			if wep == 0 then -- FIST
				sampSendChat("/me спрятал оружие")
				wait(1000)
			end
			if wep == 24 then -- DEAGLE
				sampSendChat("/me достал пистолет Desert Eagle из кобуры")
				wait(1000)
				sampSendChat("/do Пистолет Desert Eagle находится в руках.")
			end
			if wep == 25 then -- SHOTGUN
				sampSendChat("/me достал из-за спины дробовик марки \"Shotgun\"")
				wait(1000)
				sampSendChat("/do Дробовик находится в руках.")
			end
			if wep == 31 then -- M4
				sampSendChat("/me достал из-за спины автомат марки \"M4A1\"")
				wait(1000)
				sampSendChat("/do Автомат находится в руках.")
			end
			if wep == 33 then -- COUNTRY RIFLE
				sampSendChat("/me достал из-за спины охотничью винтовку")
				wait(1000)
				sampSendChat("/do Винтовка находится в руках.")
			end
			if wep == 29 then -- MP5
				sampSendChat("/me снял с поясного держателя пистолет-пулемет марки \"MP5\"")
				wait(1000)
				sampSendChat("/do Пистолет-пулемёт находится в руках.")
			end
			if wep == 34 then -- SNIPER RIFLE
				sampSendChat("/me достал из-за спины профессиональную снайперскую винтовку")
				wait(1000)
				sampSendChat("/do Винтовка находится в руках.")
			end
			if wep == 30 then -- AK-47
				sampSendChat("/me достал из-за спины автомат марки \"AK-47\"")
				wait(1000)
				sampSendChat("/do Автомат находится в руках.")
			end
			lastwep = wep
		end
	end
end

function aboba()
	lua_thread.create(function() 
	sampShowDialog(11588, string.format("{%s}Успешное убийство", accentcolor), string.format("{FFFFFF}Вы убили свою жертву {%s}"..find_name.v.."["..find_id.v.."]\nИ заработали {%s}"..find_amount.v.." {FFFFFF}Если вы хотите {%s}выполнить еще 1 заказ\n{FFFFFF}Нажмите на клавиатуре клавишу {%s}Enter", accentcolor, accentcolor, accentcolor, accentcolor), "Да", "Нет", DIALOG_STYLE_MSGBOX)
	while sampIsDialogActive(11588) do wait(100) end
	local _, button, _, _ = sampHasDialogRespond(11588)
		if button == 1 then
			sampSendChat("/menu")
			while not sampIsDialogActive(7763) do wait(10) end
			sampSendDialogResponse(7763, 1, 0, nil)
		end
	end)
end

function settings_switcher()
	settings_bool.v = not settings_bool.v
	imgui.Process = settings_bool.v
end



function cancelOrder()
	wait(100)
	savedid = find_id.v

	sampSendChat("/menu")
	
	while not sampIsDialogActive(7763) do wait(10) end
	sampSendDialogResponse(7763, 1, 2, nil)
	sampCloseCurrentDialogWithButton(0)
	
	wait(1000)
	
	if mainIni.Settings.AutoRadioWhenCanceled == 1 then
		sampSendChat("/f Я отменил заказ под номером №" .. savedid)
	end
end

function imgui.OnDrawFrame()

	if find_bool.v then
		local sw, sh = getScreenResolution()
		imgui.SetNextWindowPos(imgui.ImVec2(sw-135, sh / 2), imgui.Cond.FirstUseEver, imgui.ImVec2(0.5, 0.5))
		imgui.SetNextWindowSize(imgui.ImVec2(250, 150), imgui.Cond.FirstUseEver)
		imgui.Begin("Hitman Helper", false, imgui.WindowFlags.NoResize + imgui.WindowFlags.NoMove + imgui.WindowFlags.NoCollapse + imgui.WindowFlags.NoBringToFrontOnFocus)
		imgui.ShowCursor = false
		
		imgui.Text(u8"Игрок: ".. find_name.v .. "[" .. find_id.v .. "]")
		imgui.Text(u8"Уровень: ".. sampGetPlayerScore(find_id.v))
		imgui.Text(u8"Оплата: ".. find_amount.v .."$")
		
		imgui.Separator()
		
		imgui.SetCursorPos(imgui.ImVec2(30, 90))
		imgui.Text(u8"Обновить местоположение - F2")
		
		imgui.SetCursorPos(imgui.ImVec2(78, 110))
		if imgui.Button(u8"Отменить заказ") then
			lua_thread.create(cancelOrder)
			find_bool.v = false
		end
		
		imgui.End()
	end
	
	if settings_bool.v then
		local sw, sh = getScreenResolution()
		imgui.SetNextWindowPos(imgui.ImVec2(sw / 2, sh / 2), imgui.Cond.FirstUseEver, imgui.ImVec2(0.5, 0.5))
		imgui.SetNextWindowSize(imgui.ImVec2(455, 203), imgui.Cond.FirstUseEver)
		imgui.Begin("Hitman Helper Settings", settings_bool, imgui.WindowFlags.NoResize + imgui.WindowFlags.NoMove + imgui.WindowFlags.NoCollapse + imgui.WindowFlags.NoBringToFrontOnFocus)
		imgui.Text(u8"Цвет окна:")
		
		imgui.SameLine()
		if imgui.RadioButton(u8"Оранжевый", choosed_style, 1) then
			mainIni.Settings.Theme = 1
			inicfg.save(mainIni, pathIni)
			reloadIni()
		end
		
		imgui.SameLine()
		if imgui.RadioButton(u8"Красный", choosed_style, 2) then
			mainIni.Settings.Theme = 2
			inicfg.save(mainIni, pathIni)
			reloadIni()
		end
		
		imgui.SameLine()
		if imgui.RadioButton(u8"Фиолетовый", choosed_style, 3) then
			mainIni.Settings.Theme = 3
			inicfg.save(mainIni, pathIni)
			reloadIni()
		end
		
		imgui.SameLine()
		if imgui.RadioButton(u8"Чёрно-белый", choosed_style, 4) then
			mainIni.Settings.Theme = 4
			inicfg.save(mainIni, pathIni)
			reloadIni()
		end
		
		imgui.Separator()

		if imgui.Checkbox(u8"Автоматически писать в рацию при взятии заказа", autoradiowhenreceived) then
			if autoradiowhenreceived.v == true then mainIni.Settings.AutoRadioWhenReceived = 1 else mainIni.Settings.AutoRadioWhenReceived = false end
			inicfg.save(mainIni, pathIni)
			reloadIni()
		end
		
		if imgui.Checkbox(u8"Автоматически писать в рацию при отмене заказа", autoradiowhencanceled) then
			if autoradiowhencanceled.v == true then mainIni.Settings.AutoRadioWhenCanceled = 1 else mainIni.Settings.AutoRadioWhenCanceled = false end
			inicfg.save(mainIni, pathIni)
			reloadIni()
		end
		
		if imgui.Checkbox(u8"Автоматически писать в рацию при выполнении заказа", autoradiowhencompleted) then
			if autoradiowhencompleted.v == true then mainIni.Settings.AutoRadioWhenCompleted = 1 else mainIni.Settings.AutoRadioWhenCompleted = false end
			inicfg.save(mainIni, pathIni)
			reloadIni()
		end
		
		if imgui.Checkbox(u8"Показывать меню о заказе после убийства жертвы", menuwhenkilled) then
			if menuwhenkilled.v == true then mainIni.Settings.MenuWhenKilled = 1 else mainIni.Settings.MenuWhenKilled = false end
			inicfg.save(mainIni, pathIni)
			reloadIni()
		end
		
		if imgui.Checkbox(u8"Автоматическая отыгровка оружия в руках", autoguns) then
			if autoguns.v == true then mainIni.Settings.AutoGuns = 1 else mainIni.Settings.AutoGuns = false end
			inicfg.save(mainIni, pathIni)
			reloadIni()
		end
		
		if imgui.Checkbox(u8"Автоматически скрывать новые заказы", hideneworders) then
			if hideneworders.v == true then mainIni.Settings.HideNewOrders = 1 else mainIni.Settings.HideNewOrders = false end
			inicfg.save(mainIni, pathIni)
			reloadIni()
		end
		
		imgui.End()
	end
end

function refind()
	if find_bool.v then
		sampSendChat("/find " .. find_id.v)
	else
		sampAddChatMessage(string.format("{%s}[Hitman Helper] {FFFFFF}В данный момент у вас нету жертвы.", accentcolor), -1)
	end
end

--[[function autofind()
	while find_bool.v == true and mainIni.Settings.AutoFind == 1 do
		wait(1000)
		sampSendChat("/find " .. find_id.v)
		wait(5000)
		while sampIsChatInputActive() do wait(1000) end
		sampSendChat("/find " .. find_id.v)
	end
end--]]

function sampev.onServerMessage(clr, text)
	if string.find(text, "Вы выбрали жертву%. (.+)%[(%d+)%] %((%d+) вирт%).") then
		find_name.v, find_id.v, find_amount.v = string.match(text, "Вы выбрали жертву%. (.+)%[(%d+)%] %((%d+) вирт%).")
		if mainIni.Settings.AutoRadioWhenReceived == 1 then
			sampSendChat("/f Я взял заказ под номером №" .. find_id.v)
		end
		find_bool.v = true
		imgui.Process = find_bool.v
		--lua_thread.create(autofind)
		return false
	end
	
	_, myid = sampGetPlayerIdByCharHandle(PLAYER_PED)
	if string.find(text, "Хитман ".. sampGetPlayerNickname(myid) .."%[(%d+)%] отказался от выполнения заказа на (.+)%[(%d+)%]") then
		find_bool.v = false
		return false
	end
	
	if string.find(text, "<< Хитман "..sampGetPlayerNickname(myid).."%[(%d+)%] выполнил%(а%) контракт на (.+)%[(%d+)%] и получил %$(%d+) >>") then
		find_bool.v = false
		lua_thread.create(function() 
		
		wait(150)
		if mainIni.Settings.AutoRadioWhenCompleted == 1 then sampSendChat("/f Я успешно выполнил заказ под номером №"..find_id.v) end
		wait(150)
		
		if mainIni.Settings.MenuWhenKilled == 1 then
			sampShowDialog(11588, string.format("{%s}Успешное убийство", accentcolor), string.format("{FFFFFF}Вы убили свою жертву {%s}"..find_name.v.."["..find_id.v.."]\nИ заработали {%s}"..find_amount.v.." {FFFFFF}Если вы хотите {%s}выполнить еще 1 заказ\n{FFFFFF}Нажмите на клавиатуре клавишу {%s}Enter", accentcolor, accentcolor, accentcolor, accentcolor), "Да", "Нет", DIALOG_STYLE_MSGBOX)
			while sampIsDialogActive(11588) do wait(100) end
			local _, button, _, _ = sampHasDialogRespond(11588)
			if button == 1 then
				sampSendChat("/menu")
				while not sampIsDialogActive(7763) do wait(10) end
				sampSendDialogResponse(7763, 1, 0, nil)
			end
		end
		end)
	end
	
	if string.find(text, "Жертва (.+)%[(%d+)%] вышел из игры.") then
		find_bool.v = false
		sampAddChatMessage(string.format("{%s}[Hitman Helper] {FFFFFF}Заказ был автоматически отменён, так как жертва {%s}%s[%s] вышел(а) из игры", accentcolor, accentcolor, find_name.v, find_id.v), -1)
		return false
	end
	
	if string.find(text, "Внимание, поступил новый заказ цена головы заказчика (%d+)%$.") and mainIni.Settings.HideNewOrders == 1 then
		return false
	end
	
	
	if string.find(text, "Метка отключена", 1, true) and find_bool.v == true then
		return false
	end
	if string.find(text, "Метка установлена", 1, true) and find_bool.v == true then
		return false
	end
end

function violetstyle()
    imgui.SwitchContext()
    local style = imgui.GetStyle()
    local colors = style.Colors
    local clr = imgui.Col
    local ImVec4 = imgui.ImVec4
    style.WindowRounding = 6.0
    style.WindowTitleAlign = imgui.ImVec2(0.5, 0.5)
    style.ChildWindowRounding = 2.0
    style.FrameRounding = 3
    style.ItemSpacing = imgui.ImVec2(5.0, 4.0)
    style.ScrollbarSize = 13.0
    style.ScrollbarRounding = 0
    style.GrabMinSize = 8.0
    style.GrabRounding = 1.0
    style.WindowPadding = imgui.ImVec2(4.0, 4.0)
    style.FramePadding = imgui.ImVec2(3.5, 3.5)
    style.ButtonTextAlign = imgui.ImVec2(0.0, 0.5)
    colors[clr.WindowBg]              = ImVec4(0.14, 0.12, 0.16, 0.85);
    colors[clr.ChildWindowBg]         = ImVec4(0.30, 0.20, 0.39, 0.00);
    colors[clr.PopupBg]               = ImVec4(0.05, 0.05, 0.10, 0.90);
    colors[clr.Border]                = ImVec4(0.89, 0.85, 0.92, 0.30);
    colors[clr.BorderShadow]          = ImVec4(0.00, 0.00, 0.00, 0.00);
    colors[clr.FrameBg]               = ImVec4(0.30, 0.20, 0.39, 1.00);
    colors[clr.FrameBgHovered]        = ImVec4(0.41, 0.19, 0.63, 0.68);
    colors[clr.FrameBgActive]         = ImVec4(0.41, 0.19, 0.63, 1.00);
    colors[clr.TitleBg]               = ImVec4(0.41, 0.19, 0.63, 0.78);
    colors[clr.TitleBgCollapsed]      = ImVec4(0.41, 0.19, 0.63, 0.78);
    colors[clr.TitleBgActive]         = ImVec4(0.41, 0.19, 0.63, 0.78);
    colors[clr.MenuBarBg]             = ImVec4(0.30, 0.20, 0.39, 0.57);
    colors[clr.ScrollbarBg]           = ImVec4(0.30, 0.20, 0.39, 1.00);
    colors[clr.ScrollbarGrab]         = ImVec4(0.41, 0.19, 0.63, 0.31);
    colors[clr.ScrollbarGrabHovered]  = ImVec4(0.41, 0.19, 0.63, 0.78);
    colors[clr.ScrollbarGrabActive]   = ImVec4(0.41, 0.19, 0.63, 1.00);
    colors[clr.ComboBg]               = ImVec4(0.30, 0.20, 0.39, 1.00);
    colors[clr.CheckMark]             = ImVec4(0.56, 0.61, 1.00, 1.00);
    colors[clr.SliderGrab]            = ImVec4(0.41, 0.19, 0.63, 0.24);
    colors[clr.SliderGrabActive]      = ImVec4(0.41, 0.19, 0.63, 1.00);
    colors[clr.Button]                = ImVec4(0.41, 0.19, 0.63, 0.44);
    colors[clr.ButtonHovered]         = ImVec4(0.41, 0.19, 0.63, 0.86);
    colors[clr.ButtonActive]          = ImVec4(0.64, 0.33, 0.94, 1.00);
    colors[clr.Header]                = ImVec4(0.41, 0.19, 0.63, 0.76);
    colors[clr.HeaderHovered]         = ImVec4(0.41, 0.19, 0.63, 0.86);
    colors[clr.HeaderActive]          = ImVec4(0.41, 0.19, 0.63, 1.00);
    colors[clr.ResizeGrip]            = ImVec4(0.41, 0.19, 0.63, 0.20);
    colors[clr.ResizeGripHovered]     = ImVec4(0.41, 0.19, 0.63, 0.78);
    colors[clr.ResizeGripActive]      = ImVec4(0.41, 0.19, 0.63, 1.00);
    colors[clr.CloseButton]           = ImVec4(1.00, 1.00, 1.00, 0.75);
    colors[clr.CloseButtonHovered]    = ImVec4(0.88, 0.74, 1.00, 0.59);
    colors[clr.CloseButtonActive]     = ImVec4(0.88, 0.85, 0.92, 1.00);
    colors[clr.PlotLines]             = ImVec4(0.89, 0.85, 0.92, 0.63);
    colors[clr.PlotLinesHovered]      = ImVec4(0.41, 0.19, 0.63, 1.00);
    colors[clr.PlotHistogram]         = ImVec4(0.89, 0.85, 0.92, 0.63);
    colors[clr.PlotHistogramHovered]  = ImVec4(0.41, 0.19, 0.63, 1.00);
    colors[clr.TextSelectedBg]        = ImVec4(0.41, 0.19, 0.63, 0.43);
    colors[clr.ModalWindowDarkening]  = ImVec4(0.20, 0.20, 0.20, 0.35);
end

function blackwhitestyle()
    imgui.SwitchContext()
    local style = imgui.GetStyle()
    local colors = style.Colors
    local clr = imgui.Col
    local ImVec4 = imgui.ImVec4
    style.WindowRounding = 6.0
    style.WindowTitleAlign = imgui.ImVec2(0.5, 0.5)
    style.ChildWindowRounding = 2.0
    style.FrameRounding = 3
    style.ItemSpacing = imgui.ImVec2(5.0, 4.0)
    style.ScrollbarSize = 13.0
    style.ScrollbarRounding = 0
    style.GrabMinSize = 8.0
    style.GrabRounding = 1.0
    style.WindowPadding = imgui.ImVec2(4.0, 4.0)
    style.FramePadding = imgui.ImVec2(3.5, 3.5)
	style.ButtonTextAlign = imgui.ImVec2(0.0, 0.5)	
    colors[clr.WindowBg]               = ImVec4(0.00, 0.00, 0.00, 0.85)
    colors[clr.ChildWindowBg]          = ImVec4(0.00, 0.00, 0.00, 1.00)
    colors[clr.Text]                   = ImVec4(0.90, 0.90, 0.90, 1.00)
    colors[clr.TextDisabled]           = ImVec4(1.00, 1.00, 1.00, 1.00)
    colors[clr.PopupBg]                = ImVec4(0.00, 0.00, 0.00, 1.00)
    colors[clr.Border]                 = ImVec4(0.82, 0.77, 0.78, 1.00)
    colors[clr.BorderShadow]           = ImVec4(0.35, 0.35, 0.35, 0.66)
    colors[clr.FrameBg]                = ImVec4(1.00, 1.00, 1.00, 0.28)
    colors[clr.FrameBgHovered]         = ImVec4(0.68, 0.68, 0.68, 0.67)
    colors[clr.FrameBgActive]          = ImVec4(0.79, 0.73, 0.73, 0.62)
    colors[clr.TitleBg]                = ImVec4(0.00, 0.00, 0.00, 0.78)
    colors[clr.TitleBgActive]          = ImVec4(0.46, 0.46, 0.46, 0.78)
    colors[clr.TitleBgCollapsed]       = ImVec4(0.00, 0.00, 0.00, 0.78)
    colors[clr.MenuBarBg]              = ImVec4(0.00, 0.00, 0.00, 0.80)
    colors[clr.ScrollbarBg]            = ImVec4(0.00, 0.00, 0.00, 0.60)
    colors[clr.ScrollbarGrab]          = ImVec4(1.00, 1.00, 1.00, 0.87)
    colors[clr.ScrollbarGrabHovered]   = ImVec4(1.00, 1.00, 1.00, 0.79)
    colors[clr.ScrollbarGrabActive]    = ImVec4(0.80, 0.50, 0.50, 0.40)
    colors[clr.ComboBg]                = ImVec4(0.24, 0.24, 0.24, 0.99)
    colors[clr.CheckMark]              = ImVec4(0.99, 0.99, 0.99, 0.52)
    colors[clr.SliderGrab]             = ImVec4(1.00, 1.00, 1.00, 0.42)
    colors[clr.SliderGrabActive]       = ImVec4(0.76, 0.76, 0.76, 1.00)
    colors[clr.Button]                 = ImVec4(0.51, 0.51, 0.51, 0.60)
    colors[clr.ButtonHovered]          = ImVec4(0.68, 0.68, 0.68, 1.00)
    colors[clr.ButtonActive]           = ImVec4(0.67, 0.67, 0.67, 1.00)
    colors[clr.Header]                 = ImVec4(0.72, 0.72, 0.72, 0.54)
    colors[clr.HeaderHovered]          = ImVec4(0.92, 0.92, 0.95, 0.77)
    colors[clr.HeaderActive]           = ImVec4(0.82, 0.82, 0.82, 0.80)
    colors[clr.Separator]              = ImVec4(0.73, 0.73, 0.73, 1.00)
    colors[clr.SeparatorHovered]       = ImVec4(0.81, 0.81, 0.81, 1.00)
    colors[clr.SeparatorActive]        = ImVec4(0.74, 0.74, 0.74, 1.00)
    colors[clr.ResizeGrip]             = ImVec4(0.80, 0.80, 0.80, 0.30)
    colors[clr.ResizeGripHovered]      = ImVec4(0.95, 0.95, 0.95, 0.60)
    colors[clr.ResizeGripActive]       = ImVec4(1.00, 1.00, 1.00, 0.90)
    colors[clr.CloseButton]            = ImVec4(0.45, 0.45, 0.45, 0.50)
    colors[clr.CloseButtonHovered]     = ImVec4(0.70, 0.70, 0.90, 0.60)
    colors[clr.CloseButtonActive]      = ImVec4(0.70, 0.70, 0.70, 1.00)
    colors[clr.PlotLines]              = ImVec4(1.00, 1.00, 1.00, 1.00)
    colors[clr.PlotLinesHovered]       = ImVec4(1.00, 1.00, 1.00, 1.00)
    colors[clr.PlotHistogram]          = ImVec4(1.00, 1.00, 1.00, 1.00)
    colors[clr.PlotHistogramHovered]   = ImVec4(1.00, 1.00, 1.00, 1.00)
    colors[clr.TextSelectedBg]         = ImVec4(1.00, 1.00, 1.00, 0.35)
    colors[clr.ModalWindowDarkening]   = ImVec4(0.88, 0.88, 0.88, 0.35)
end

function redstyle()
	imgui.SwitchContext()
    local style = imgui.GetStyle()
    local colors = style.Colors
    local clr = imgui.Col
    local ImVec4 = imgui.ImVec4
    style.WindowRounding = 6.0
    style.WindowTitleAlign = imgui.ImVec2(0.5, 0.5)
    style.ChildWindowRounding = 2.0
    style.FrameRounding = 3
    style.ItemSpacing = imgui.ImVec2(5.0, 4.0)
    style.ScrollbarSize = 13.0
    style.ScrollbarRounding = 0
    style.GrabMinSize = 8.0
    style.GrabRounding = 1.0
    style.WindowPadding = imgui.ImVec2(4.0, 4.0)
    style.FramePadding = imgui.ImVec2(3.5, 3.5)
    style.ButtonTextAlign = imgui.ImVec2(0.0, 0.5)
	colors[clr.Text]                 = ImVec4(1.00, 1.00, 1.00, 1.00)
	colors[clr.TextDisabled]         = ImVec4(1.00, 1.00, 1.00, 1.00)
	colors[clr.WindowBg]             = ImVec4(0.11, 0.15, 0.17, 0.85)
	colors[clr.ChildWindowBg]        = ImVec4(0.15, 0.18, 0.22, 1.00)
	colors[clr.PopupBg]              = ImVec4(0.08, 0.08, 0.08, 0.94)
	colors[clr.Border]               = ImVec4(0.43, 0.43, 0.50, 0.50)
	colors[clr.BorderShadow]         = ImVec4(0.00, 0.00, 0.00, 0.00)
	colors[clr.FrameBg]              = ImVec4(0.20, 0.25, 0.29, 1.00)
	colors[clr.FrameBgHovered]       = ImVec4(0.12, 0.20, 0.28, 1.00)
	colors[clr.FrameBgActive]        = ImVec4(0.09, 0.12, 0.14, 1.00)
	colors[clr.TitleBg]              = ImVec4(0.53, 0.20, 0.16, 0.78)
	colors[clr.TitleBgActive]        = ImVec4(0.56, 0.14, 0.14, 0.78)
	colors[clr.TitleBgCollapsed]     = ImVec4(0.00, 0.00, 0.00, 0.78)
	colors[clr.MenuBarBg]            = ImVec4(0.15, 0.18, 0.22, 1.00)
	colors[clr.ScrollbarBg]          = ImVec4(0.02, 0.02, 0.02, 0.39)
	colors[clr.ScrollbarGrab]        = ImVec4(0.20, 0.25, 0.29, 1.00)
	colors[clr.ScrollbarGrabHovered] = ImVec4(0.18, 0.22, 0.25, 1.00)
	colors[clr.ScrollbarGrabActive]  = ImVec4(0.09, 0.21, 0.31, 1.00)
	colors[clr.ComboBg]              = ImVec4(0.20, 0.25, 0.29, 1.00)
	colors[clr.CheckMark]            = ImVec4(1.00, 0.28, 0.28, 1.00)
	colors[clr.SliderGrab]           = ImVec4(0.64, 0.14, 0.14, 1.00)
	colors[clr.SliderGrabActive]     = ImVec4(1.00, 0.37, 0.37, 1.00)
	colors[clr.Button]               = ImVec4(0.59, 0.13, 0.13, 1.00)
	colors[clr.ButtonHovered]        = ImVec4(0.69, 0.15, 0.15, 1.00)
	colors[clr.ButtonActive]         = ImVec4(0.67, 0.13, 0.07, 1.00)
	colors[clr.Header]               = ImVec4(0.20, 0.25, 0.29, 0.55)
	colors[clr.HeaderHovered]        = ImVec4(0.98, 0.38, 0.26, 0.80)
	colors[clr.HeaderActive]         = ImVec4(0.98, 0.26, 0.26, 1.00)
	colors[clr.Separator]            = ImVec4(0.50, 0.50, 0.50, 1.00)
	colors[clr.SeparatorHovered]     = ImVec4(0.60, 0.60, 0.70, 1.00)
	colors[clr.SeparatorActive]      = ImVec4(0.70, 0.70, 0.90, 1.00)
	colors[clr.ResizeGrip]           = ImVec4(0.26, 0.59, 0.98, 0.25)
	colors[clr.ResizeGripHovered]    = ImVec4(0.26, 0.59, 0.98, 0.67)
	colors[clr.ResizeGripActive]     = ImVec4(0.06, 0.05, 0.07, 1.00)
	colors[clr.CloseButton]          = ImVec4(0.40, 0.39, 0.38, 0.16)
	colors[clr.CloseButtonHovered]   = ImVec4(0.40, 0.39, 0.38, 0.39)
	colors[clr.CloseButtonActive]    = ImVec4(0.40, 0.39, 0.38, 1.00)
	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.25, 1.00, 0.00, 0.43)
	colors[clr.ModalWindowDarkening] = ImVec4(1.00, 0.98, 0.95, 0.73)
end

function orangestyle()
	imgui.SwitchContext()
    local style = imgui.GetStyle()
    local colors = style.Colors
    local clr = imgui.Col
    local ImVec4 = imgui.ImVec4
    style.WindowRounding = 6.0
    style.WindowTitleAlign = imgui.ImVec2(0.5, 0.5)
    style.ChildWindowRounding = 2.0
    style.FrameRounding = 3
    style.ItemSpacing = imgui.ImVec2(5.0, 4.0)
    style.ScrollbarSize = 13.0
    style.ScrollbarRounding = 0
    style.GrabMinSize = 8.0
    style.GrabRounding = 1.0
    style.WindowPadding = imgui.ImVec2(4.0, 4.0)
    style.FramePadding = imgui.ImVec2(3.5, 3.5)
    style.ButtonTextAlign = imgui.ImVec2(0.0, 0.5)
	colors[clr.Text] = ImVec4(0.80, 0.80, 0.83, 1.00)
	colors[clr.TextDisabled] = ImVec4(0.24, 0.23, 0.29, 1.00)
	colors[clr.WindowBg] = ImVec4(0.06, 0.05, 0.07, 0.85)
	colors[clr.ChildWindowBg] = ImVec4(0.07, 0.07, 0.09, 1.00)
	colors[clr.PopupBg] = ImVec4(0.07, 0.07, 0.09, 0.94)
	colors[clr.Border] = ImVec4(0.80, 0.80, 0.83, 0.88)
	colors[clr.BorderShadow] = ImVec4(0.92, 0.91, 0.88, 0.00)
	colors[clr.FrameBg] = ImVec4(0.10, 0.09, 0.12, 1.00)
	colors[clr.FrameBgHovered] = ImVec4(0.24, 0.23, 0.29, 1.00)
	colors[clr.FrameBgActive] = ImVec4(0.56, 0.56, 0.58, 1.00)
	colors[clr.TitleBg] = ImVec4(0.76, 0.31, 0.00, 0.78)
	colors[clr.TitleBgCollapsed] = ImVec4(1.00, 0.98, 0.95, 0.78)
	colors[clr.TitleBgActive] = ImVec4(0.80, 0.33, 0.00, 0.78)
	colors[clr.MenuBarBg] = ImVec4(0.10, 0.09, 0.12, 1.00)
	colors[clr.ScrollbarBg] = ImVec4(0.10, 0.09, 0.12, 1.00)
	colors[clr.ScrollbarGrab] = ImVec4(0.80, 0.80, 0.83, 0.31)
	colors[clr.ScrollbarGrabHovered] = ImVec4(0.56, 0.56, 0.58, 1.00)
	colors[clr.ScrollbarGrabActive] = ImVec4(0.06, 0.05, 0.07, 1.00)
	colors[clr.ComboBg] = ImVec4(0.19, 0.18, 0.21, 1.00)
	colors[clr.CheckMark] = ImVec4(1.00, 0.42, 0.00, 0.53)
	colors[clr.SliderGrab] = ImVec4(1.00, 0.42, 0.00, 0.53)
	colors[clr.SliderGrabActive] = ImVec4(1.00, 0.42, 0.00, 1.00)
	colors[clr.Button] = ImVec4(0.10, 0.09, 0.12, 1.00)
	colors[clr.ButtonHovered] = ImVec4(0.24, 0.23, 0.29, 1.00)
	colors[clr.ButtonActive] = ImVec4(0.56, 0.56, 0.58, 1.00)
	colors[clr.Header] = ImVec4(0.10, 0.09, 0.12, 1.00)
	colors[clr.HeaderHovered] = ImVec4(0.56, 0.56, 0.58, 1.00)
	colors[clr.HeaderActive] = ImVec4(0.06, 0.05, 0.07, 1.00)
	colors[clr.ResizeGrip] = ImVec4(0.00, 0.00, 0.00, 0.00)
	colors[clr.ResizeGripHovered] = ImVec4(0.56, 0.56, 0.58, 1.00)
	colors[clr.ResizeGripActive] = ImVec4(0.06, 0.05, 0.07, 1.00)
	colors[clr.CloseButton] = ImVec4(0.40, 0.39, 0.38, 0.16)
	colors[clr.CloseButtonHovered] = ImVec4(0.40, 0.39, 0.38, 0.39)
	colors[clr.CloseButtonActive] = ImVec4(0.40, 0.39, 0.38, 1.00)
	colors[clr.PlotLines] = ImVec4(0.40, 0.39, 0.38, 0.63)
	colors[clr.PlotLinesHovered] = ImVec4(0.25, 1.00, 0.00, 1.00)
	colors[clr.PlotHistogram] = ImVec4(0.40, 0.39, 0.38, 0.63)
	colors[clr.PlotHistogramHovered] = ImVec4(0.25, 1.00, 0.00, 1.00)
	colors[clr.TextSelectedBg] = ImVec4(0.25, 1.00, 0.00, 0.43)
	colors[clr.ModalWindowDarkening] = ImVec4(1.00, 0.98, 0.95, 0.73)
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