local tag, limp, score, state, move = "[gadget] ", {}, {}, {}, {}
function dumpkeys (tbl) for k in pairs(tbl) do echo(k) end end
function dumptable (tbl) for k, v in pairs(tbl) do echo(k .. ": " .. v) end end
local base64 = (function ()
local encdata, decdata = (function (str)
local iter = str:gmatch("%S")
local enctbl, dectbl = { [0] = iter() }, {}
for c in iter do table.insert(enctbl, c) end
for k, v in pairs(enctbl) do dectbl[v] = k end
return enctbl, dectbl
end)("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/")
local function encode (input)
local output = ""
for i = 1, 7 do
output = encdata[input % 64] .. output
input = math.floor(input / 64)
end
return output
end
local function decode (input)
local output = 0
for i = 1, 7 do
output = output + 64 ^ (7 - i) * decdata[input:sub(i, i)]
end
return output
end
return { encode = encode, decode = decode }
end)()
local function call_str (args)
local win, fail = pcall(loadstring(table.concat(args, " ")))
if (not win) then echo(tag .. "Error: " .. fail) end
end
local function reload ()
local fh = io.open("gadget.lua", "r")
if (nil == fh) then return echo(tag .. "can't find myself?") end
local c = loadstring("do\n" .. fh:read("*a") .. "\nend")
fh:close()
local win, fail = pcall(c)
if (not win) then echo(tag .. fail) end
end
local function colors ()
for a = 0, 9 do
local c = {}
for b = 0, 9 do table.insert(c, "^" .. a .. b .. a .. b) end
echo(table.concat(c, ", "))
end
end
local function get_joints (player)
local joints = 0
for joint = 0, 19 do
local s = 2 ^ (2 * joint) -- two bits of data per joint
joints = joints + s * (get_joint_info(player, joint).state - 1)
end
return joints
end
local function get_grips (player)
-- one bit of data per hand
local state = 2 ^ 41 * get_grip_info(player, 11)
return state + 2 ^ 42 * get_grip_info(player, 12)
end
local function randomize (player)
for joint = 0, 19 do
set_joint_state(player, joint, math.random(4))
end
for hand = 11, 12 do
set_grip_info(player, hand, math.random(2) - 1)
end
end
local function calculate_damage ()
for player = 0, 1 do
local newscore = get_player_info(1 - player).injury
if newscore ~= score[player] then
echo(
move[player] .. " earned "
.. (newscore - score[player])
.. " damage for " .. get_player_info(player).name
)
score[player] = newscore
end
end
move = {}
end
local function auto_stop ()
remove_hook("match_begin", tag .. "auto")
remove_hook("enter_freeze", tag .. "auto")
remove_hook("end_game", tag .. "auto")
enable_camera_movement()
enable_sound()
return echo(tag .. "stopped.")
end
local function practice_step ()
for player = 0, 1 do randomize(player) end
step_game()
end
local function practice_end_game () return start_new_game() end
local function practice ()
add_hook("match_begin", tag .. "auto", practice_step)
add_hook("enter_freeze", tag .. "auto", practice_step)
add_hook("end_game", tag .. "auto", practice_end_game)
start_new_game()
return echo(tag .. "practicing...")
end
local bot_messages = {
"i'm a bot - i do random moves",
"they aren't always good",
"one day i'll be better at this",
"until then, enjoy the random"
}
local move_number = 1
local function fight_step ()
local id = get_world_state().selected_player
if 0 > id then return false end
randomize(id)
step_game()
if bot_messages[move_number] then
run_cmd("em " .. bot_messages[move_number])
end
move_number = move_number + 1
end
local function fight_begin ()
move_number = 1
fight_step()
end
local function fight ()
add_hook("match_begin", tag .. "auto", fight_begin)
add_hook("enter_freeze", tag .. "auto", fight_step)
return echo(tag .. "FIGHTING!")
end
local commands = {
lua = call_str,
reload = reload,
colors = colors,
learn = auto_learn,
stop = auto_stop,
practice = practice,
fight = fight
}
local function command (raw)
local i = raw:gmatch("%S+")
local cmd, args = i(), {}
for arg in i do table.insert(args, arg) end
if commands[cmd] then
commands[cmd](args)
return 1
end
end
add_hook("command", tag, command)
local JOINTS_LIMP = 2 ^ 40 - 1
local function frame_update (player)
local joints = get_joints(player)
if JOINTS_LIMP == joints then
if nil == limp[player] then
echo(tag .. get_player_info(player).name .. " has gone limp!")
end
limp[player] = true
else limp[player] = nil end
return base64.encode(joints + get_grips(player))
end
local function enter_frame ()
for player = 0, 1 do state[player] = frame_update(player) end
if 0 == #move then for player = 0, 1 do move[player] = state[player] end end
end
add_hook("enter_frame", tag, enter_frame)
local function enter_freeze () calculate_damage() end
add_hook("enter_freeze", tag, enter_freeze)
local function end_game ()
calculate_damage()
local winner = get_world_state().winner
if (0 > winner) then echo(tag .. "draw!")
else echo(tag .. get_player_info(winner).name .. " wins!") end
end
add_hook("end_game", tag, end_game)
local function match_begin ()
score, state, move = { [0] = 0, 0 }, {}, {}
set_option("smoothcam", 0)
set_camera_angle(0)
echo(tag .. get_player_info(0).name .. " vs " .. get_player_info(1).name)
end
add_hook("match_begin", tag, match_begin)
--[[
function decode (arg1, arg2, arg3)
echo(arg1) echo(arg2) echo(arg3)
end
add_hook("console", tag, decode)
--]]
math.randomseed(os.time())
echo(tag .. "locked and loaded")