local function spiral_wrap_signed(value, bits)
    local modulus = 2 ^ bits
    value = value % modulus
    if value >= modulus / 2 then value = value - modulus end
    return value
end
local Mut0, Mut1, Us0_Some, Us0_None, Us1_Some, Us1_None, get_environment_variable_1, env_text_0, disable_client_bot_2, mute_3, closure0, file_exists_5, log_flush_7, write_log_line_6, closure3, typed_c2, closure5, typed_c4, format_real_8, player_present_10, live_monster_12, scan_11, closure8, go_9, closure7, ready_c6, closure9, poll_4, closure1, closure10
Mut0 = function(l0) return { __tag = "Mut0", l0 = l0 } end

Mut1 = function(l0) return { __tag = "Mut1", l0 = l0 } end

function Us0_Some(v0, v1, v2) return { tag = "Us0_Some",  _1 = v0,  _2 = v1,  _3 = v2 } end
function Us0_None() return { tag = "Us0_None" } end

function Us1_Some(v0, v1) return { tag = "Us1_Some",  _1 = v0,  _2 = v1 } end
function Us1_None() return { tag = "Us1_None" } end

function get_environment_variable_1(v0)
    local v1 = os.getenv(v0) or ""
    return v1
end

function env_text_0(v0, v1)
    local v2 = get_environment_variable_1(v0)
    local v3 = v2 == ""
    if v3 then
        return v1
    else
        return v2
    end
end

function disable_client_bot_2(v0)
    local v1 = g_settings.getNode("bot")
    local v2 = (v1) ~= nil
    local getv4 = function()
        if v2 then
            return v1
        else
            local v3 = {}
            return v3
        end
    end
    local v4 = getv4()
    do (v4)[v0] = { enabled = false } end
    g_settings.setNode("bot", v4)
    g_settings.save()
    return nil
end

function mute_3()
    g_settings.set("enableAudio", false)
    local v0 = g_sounds ~= nil
    if v0 then
        g_sounds.setAudioEnabled(false)
        g_sounds.stopAll()
        return nil
    else
    end
end

function closure0(capt)
    return function(dom)
        local v0 = "TIBIA_PROBE_HIDDEN"
        local v1 = get_environment_variable_1(v0)
        local v2 = v1 == "1"
        if v2 then
            g_window.move({ x = -32000, y = -32000 })
            g_window.hide()
        else
        end
        return mute_3()
    end
end

function file_exists_5(v0)
    local v1 = "r"
    local v2 = io.open(v0, v1)
    local v3 = (v2) ~= nil
    if v3 then
        do (v2):close() end
        return true
    else
        return false
    end
end

function log_flush_7()
    local v0 = table.concat(TibiaProbeLog, string.char(10))
    local v1 = "\n"
    local v2 = v0 .. v1 
    g_resources.writeFileContents("/tibia-probe.log", v2)
    return nil
end

function write_log_line_6(v0)
    local v1 = TibiaProbeLog == nil
    if v1 then
        TibiaProbeLog = {}
    else
    end
    table.insert(TibiaProbeLog, v0)
    g_logger.info(v0)
    return log_flush_7()
end

function closure3(capt)
    return function(dom)
        g_app.exit()
        return nil
    end
end

function typed_c2(capt)
    return function(v0)
        local v1 = tostring(v0)
        local v2 = "game login error: "
        local v3 = v2 .. v1 
        local v4 = "[tibia-probe] "
        local v5 = v4 .. v3 
        write_log_line_6(v5)
        local v6 = "finish: "
        local v7 = "game login error"
        local v8 = v6 .. v7 
        local v9 = v4 .. v8 
        write_log_line_6(v9)
        local v10 = g_game.isOnline()
        if v10 then
            g_game.safeLogout()
        else
        end
        local v11 = closure3(nil)
        scheduleEvent(v11, 2000)
        return nil
    end
end

function closure5(capt)
    local v0 = (table.unpack or unpack)(capt)
    return function(v1)
        local v2 = tostring(v0)
        local v3 = tostring(v1)
        local v4 = "server text ["
        local v5 = v4 .. v2 
        local v6 = "]: "
        local v7 = v5 .. v6 
        local v8 = v7 .. v3 
        local v9 = "[tibia-probe] "
        local v10 = v9 .. v8 
        return write_log_line_6(v10)
    end
end

function typed_c4(capt)
    return function(v0)
        return closure5({ v0 })
    end
end

function format_real_8(v0)
    local v1 = ""
    local v2 = { __tag = "Mut1", l0 = v1 }
    local v3 = tostring(v0)       
    local v4 = v2 ~= nil and v2.l0
    local v5 = v4 .. v3 
    v2.l0 = v5
    local v6 = v2 ~= nil and v2.l0
    return v6
end

function player_present_10()
    local v0 = g_game.getLocalPlayer() ~= nil
    if v0 then
        local v1 = g_game.isOnline()
        return v1
    else
        return false
    end
end

function live_monster_12(v0)
    local v1 = (v0):isMonster()
    local getv4 = function()
        if v1 then
            local v2 = (v0):isDead()
            local v3 = v2 == false
            return v3
        else
            return false
        end
    end
    local v4 = getv4()
    if v4 then
        local v5 = (v0):getPosition() ~= nil
        return v5
    else
        return false
    end
end

function scan_11(v0, v1, v2, v3, v4, v5, v6)
    local v7 = v5 > v4
    if v7 then
        return v6
    else
        local v8 = spiral_wrap_signed((v5 + 1), 32)
        local v9 = (v3)[v5]
        local v10 = live_monster_12(v9)
        local getv33 = function()
            if v10 then
                local v11 = (v9):getPosition()
                local v12 = v11.x
                local v13 = v11.y
                local v14 = v11.z
                local v15 = v12 > v0
                local getv18 = function()
                    if v15 then
                        local v16 = spiral_wrap_signed((v12 - v0), 32)
                        return v16
                    else
                        local v17 = spiral_wrap_signed((v0 - v12), 32)
                        return v17
                    end
                end
                local v18 = getv18()
                local v19 = v13 > v1
                local getv22 = function()
                    if v19 then
                        local v20 = spiral_wrap_signed((v13 - v1), 32)
                        return v20
                    else
                        local v21 = spiral_wrap_signed((v1 - v13), 32)
                        return v21
                    end
                end
                local v22 = getv22()
                local v23 = v18 > v22
                local getv24 = function()
                    if v23 then
                        return v18
                    else
                        return v22
                    end
                end
                local v24 = getv24()
                local getv30 = function()
                    if v6 ~= nil and v6.tag == "Us1_None" then
                        local v28 = v24 < 999
                        return v28
                    elseif v6 ~= nil and v6.tag == "Us1_Some" then
                        local v25 = v6._1
                        local v26 = v6._2
                        local v27 = v24 < v26
                        return v27
                    end
                end
                local v30 = getv30()
                if v30 then
                    return Us1_Some(v5, v24)
                else
                    return v6
                end
            else
                return v6
            end
        end
        local v33 = getv33()
        return scan_11(v0, v1, v2, v3, v4, v8, v33)
    end
end

function closure8(capt)
    local v0, v1, v2, v3 = (table.unpack or unpack)(capt)
    return function(dom)
        return go_9(v0, v1, v2, v3)
    end
end

function go_9(v0, v1, v2, v3)
    local v4 = player_present_10()
    local v5 = v4 == false
    if v5 then
        local v6 = v0 ~= nil and v0.l0
        local v7 = v6 == 0
        if v7 then
            v0.l0 = 1
            local v8 = "finish: "
            local v9 = "offline"
            local v10 = v8 .. v9 
            local v11 = "[tibia-probe] "
            local v12 = v11 .. v10 
            write_log_line_6(v12)
            local v13 = g_game.isOnline()
            if v13 then
                g_game.safeLogout()
            else
            end
            local v14 = closure3(nil)
            scheduleEvent(v14, 2000)
            return nil
        else
        end
    else
        local v15 = g_clock.millis()
        local v16 = spiral_wrap_signed((v15 - v1), 32)
        local v17 = g_game.getLocalPlayer():getPosition()
        local v18 = v17.x
        local v19 = v17.y
        local v20 = v17.z
        local v21 = g_map.getSpectators({ x = v18, y = v19, z = v20 }, false)
        local v22 = #(v21)
        local v23 = 1
        local v24 = Us1_None()
        local v25 = scan_11(v18, v19, v20, v21, v22, v23, v24)
        local getv77 = function()
            if v25 ~= nil and v25.tag == "Us1_None" then
                return { [1]=false, [2]=v2, [3]=v3 }
            elseif v25 ~= nil and v25.tag == "Us1_Some" then
                local v26 = v25._1
                local v27 = v25._2
                local v28 = v27 <= 1
                local v29 = (v21)[v26]
                local v30 = (v29):getPosition()
                local v31 = v30.x
                local v32 = v30.y
                local v33 = v30.z
                local getv44 = function()
                    if v2 ~= nil and v2.tag == "Us0_None" then
                        return { [1]=true, [2]=v3 }
                    elseif v2 ~= nil and v2.tag == "Us0_Some" then
                        local v34 = v2._1
                        local v35 = v2._2
                        local v36 = v2._3
                        local v37 = v31 ~= v34
                        local getv39 = function()
                            if v37 then
                                return true
                            else
                                local v38 = v32 ~= v35
                                return v38
                            end
                        end
                        local v39 = getv39()
                        local getv41 = function()
                            if v39 then
                                local v40 = spiral_wrap_signed((v3 + 1), 32)
                                return v40
                            else
                                return v3
                            end
                        end
                        local v41 = getv41()
                        return { [1]=v39, [2]=v41 }
                    end
                end
                local v44, v45 = (table.unpack or unpack)(getv44())
                if v44 then
                    local v46 = format_real_8(v31)
                    local v47 = ""
                    local v48 = v47 .. v46 
                    local v49 = ","
                    local v50 = v48 .. v49 
                    local v51 = format_real_8(v32)
                    local v52 = v50 .. v51 
                    local v53 = v52 .. v49 
                    local v54 = format_real_8(v33)
                    local v55 = v53 .. v54 
                    local v56 = format_real_8(v16)
                    local v57 = "path t="
                    local v58 = v57 .. v56 
                    local v59 = " ms monster at "
                    local v60 = v58 .. v59 
                    local v61 = v60 .. v55 
                    local v62 = " distance "
                    local v63 = v61 .. v62 
                    local v64 = format_real_8(v27)
                    local v65 = v63 .. v64 
                    local v66 = " step "
                    local v67 = v65 .. v66 
                    local v68 = format_real_8(v45)
                    local v69 = v67 .. v68 
                    local v70 = "[tibia-probe] "
                    local v71 = v70 .. v69 
                    write_log_line_6(v71)
                else
                end
                local getv73 = function()
                    if v44 then
                        return Us0_Some(v31, v32, v33)
                    else
                        return v2
                    end
                end
                local v73 = getv73()
                return { [1]=v28, [2]=v73, [3]=v45 }
            end
        end
        local v77, v78, v79 = (table.unpack or unpack)(getv77())
        local getv131 = function()
            if v77 then
                local v80 = format_real_8(v18)
                local v81 = ""
                local v82 = v81 .. v80 
                local v83 = ","
                local v84 = v82 .. v83 
                local v85 = format_real_8(v19)
                local v86 = v84 .. v85 
                local v87 = v86 .. v83 
                local v88 = format_real_8(v20)
                local v89 = v87 .. v88 
                local v90 = format_real_8(v16)
                local v91 = "arrived at "
                local v92 = v91 .. v90 
                local v93 = " ms after "
                local v94 = v92 .. v93 
                local v95 = format_real_8(v79)
                local v96 = v94 .. v95 
                local v97 = " steps, knight at "
                local v98 = v96 .. v97 
                local v99 = v98 .. v89 
                local v100 = "[tibia-probe] "
                local v101 = v100 .. v99 
                write_log_line_6(v101)
                local v102 = v0 ~= nil and v0.l0
                local v103 = v102 == 0
                if v103 then
                    v0.l0 = 1
                    local v104 = "finish: "
                    local v105 = "arrived"
                    local v106 = v104 .. v105 
                    local v107 = v100 .. v106 
                    write_log_line_6(v107)
                    local v108 = g_game.isOnline()
                    if v108 then
                        g_game.safeLogout()
                    else
                    end
                    local v109 = closure3(nil)
                    scheduleEvent(v109, 2000)
                else
                end
                return true
            else
                local v110 = v16 > 30000
                if v110 then
                    local v111 = format_real_8(v16)
                    local v112 = "not arrived after "
                    local v113 = v112 .. v111 
                    local v114 = " ms, "
                    local v115 = v113 .. v114 
                    local v116 = format_real_8(v79)
                    local v117 = v115 .. v116 
                    local v118 = " steps"
                    local v119 = v117 .. v118 
                    local v120 = "[tibia-probe] "
                    local v121 = v120 .. v119 
                    write_log_line_6(v121)
                    local v122 = v0 ~= nil and v0.l0
                    local v123 = v122 == 0
                    if v123 then
                        v0.l0 = 1
                        local v124 = "finish: "
                        local v125 = "not arrived"
                        local v126 = v124 .. v125 
                        local v127 = v120 .. v126 
                        write_log_line_6(v127)
                        local v128 = g_game.isOnline()
                        if v128 then
                            g_game.safeLogout()
                        else
                        end
                        local v129 = closure3(nil)
                        scheduleEvent(v129, 2000)
                    else
                    end
                    return true
                else
                    return false
                end
            end
        end
        local v131 = getv131()
        local v132 = v131 == false
        if v132 then
            local v133 = closure8({ v0, v1, v78, v79 })
            scheduleEvent(v133, 50)
            return nil
        else
        end
    end
end

function closure7(capt)
    local v0 = (table.unpack or unpack)(capt)
    return function(dom)
        local v1 = g_game.getLocalPlayer():getName()
        local v2 = g_game.getLocalPlayer():getPosition()
        local v3 = v2.x
        local v4 = v2.y
        local v5 = v2.z
        local v6 = format_real_8(v3)
        local v7 = ""
        local v8 = v7 .. v6 
        local v9 = ","
        local v10 = v8 .. v9 
        local v11 = format_real_8(v4)
        local v12 = v10 .. v11 
        local v13 = v12 .. v9 
        local v14 = format_real_8(v5)
        local v15 = v13 .. v14 
        local v16 = "in game as "
        local v17 = v16 .. v1 
        local v18 = " at "
        local v19 = v17 .. v18 
        local v20 = v19 .. v15 
        local v21 = " (path check, standing, no attack)"
        local v22 = v20 .. v21 
        local v23 = "[tibia-probe] "
        local v24 = v23 .. v22 
        write_log_line_6(v24)
        local v25 = g_clock.millis()
        local v26 = Us0_None()
        local v27 = 0
        return go_9(v0, v25, v26, v27)
    end
end

function ready_c6(capt)
    local v0 = (table.unpack or unpack)(capt)
    return function(dom)
        local v1 = closure7({ v0 })
        scheduleEvent(v1, 1500)
        return nil
    end
end

function closure9(capt)
    local v0, v1, v2 = (table.unpack or unpack)(capt)
    return function(dom)
        return poll_4(v0, v1, v2)
    end
end

function poll_4(v0, v1, v2)
    local v3 = v0 == ""
    local getv5 = function()
        if v3 then
            return true
        else
            return file_exists_5(v0)
        end
    end
    local v5 = getv5()
    if v5 then
        g_game.setClientVersion(1511)
        g_game.setProtocolVersion(g_game.getClientProtocolVersion(1511))
        g_game.chooseRsa("127.0.0.1")
        local v6 = typed_c2(nil)
        local v7 = function(message) (v6)(message) end
        local v8 = typed_c4(nil)
        local v9 = function(mode, text) (v8)(mode)(text) end
        local v10 = ready_c6({ v2 })
        connect(g_game, { onGameStart = v10, onLoginError = v7, onTextMessage = v9 })
        local v11 = "@god"
        local v12 = "@god\ngod"
        g_game.loginWorld(v11, "god", "OTServBR-Global", "127.0.0.1", 7172, v1, "", v12)
        return nil
    else
        local v13 = closure9({ v0, v1, v2 })
        scheduleEvent(v13, 50)
        return nil
    end
end

function closure1(capt)
    local v0, v1 = (table.unpack or unpack)(capt)
    return function(dom)
        local v2 = "TIBIA_LOGIN_GATE"
        local v3 = ""
        local v4 = env_text_0(v2, v3)
        return poll_4(v4, v0, v1)
    end
end

function closure10(capt)
    local v0 = (table.unpack or unpack)(capt)
    return function(dom)
        local v1 = v0 ~= nil and v0.l0
        local v2 = v1 == 0
        if v2 then
            v0.l0 = 1
            local v3 = "finish: "
            local v4 = "timeout"
            local v5 = v3 .. v4 
            local v6 = "[tibia-probe] "
            local v7 = v6 .. v5 
            write_log_line_6(v7)
            local v8 = g_game.isOnline()
            if v8 then
                g_game.safeLogout()
            else
            end
            local v9 = closure3(nil)
            scheduleEvent(v9, 2000)
            return nil
        else
        end
    end
end

local v0 = { __tag = "Mut0", l0 = 0 }
local v1 = "TIBIA_ARENA_CHARACTER"
local v2 = "Knight Sample"
local v3 = env_text_0(v1, v2)
local v4 = "_1511"
local v5 = v3 .. v4 
disable_client_bot_2(v5)
local v6 = env_text_0(v1, v2)
local v7 = "TIBIA_PROBE_HIDDEN"
local v8 = get_environment_variable_1(v7)
local v9 = v8 == "1"
if v9 then
    g_window.move({ x = -32000, y = -32000 })
    g_window.hide()
else
end
mute_3()
local v10 = closure0(nil)
scheduleEvent(v10, 200)
scheduleEvent(v10, 1000)
local v11 = closure1({ v6, v0 })
scheduleEvent(v11, 3000)
local v12 = closure10({ v0 })
scheduleEvent(v12, 60000)
return 0
