local moves, openers = {}, {}
local whitebox = load_texture("invalid")
-- table stringification
table.tostring = (function ()
local function value_to_string (value)
if "string" == type(value) then
-- escape escaped little Ns, if they exist
value = string.gsub(value, "\n", "\\n")
if string.match(string.gsub(value, "[^'\"]", ""), '"') then
-- contains double quotes, but no single quotes
-- quote with single quotes
return "'" .. value .. "'"
end
-- contains double quotes, escape them
return '"' .. string.gsub(value, '"', '\\"') .. '"'
end
return "table" == type(value) and table_to_string(value) or tostring(value)
end
local function table_to_string (tbl)
local result, done = {}, {}
for k, v in ipairs(tbl) do
table.insert(result, table_val2str(v))
table.insert(done, k)
end
for k, v in pairs(tbl) do if not done[k] then
-- table key may be perfectly valid (alphanumeric + underscores, starts
-- with a letter) but may still be a reserved word. the table to check
-- would be large, so for now, just quote it regardless
table.insert(result,
"[" .. value_to_string(k) .. "] =" .. value_to_string(v)
)
end end
return "{" .. table.concat(result, ",") .. "}"
end
return table_to_string
end)()
-- move compression
local encode, decode = (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, decode
end)()
-- get/set move
local function get_move (player)
local move = 0
for joint = 0, 19 do
-- two bits per joint
local s = 2 ^ (2 * joint)
move = move + s * (get_joint_info(player, joint).state - 1)
end
-- one bit per hand
move = move + 2 ^ 41 * get_grip_info(player, 11)
move = move + 2 ^ 42 * get_grip_info(player, 12)
-- and then we encode it
return encode(move)
end
local function set_move (move)
local move = table.concat(move)
move = decode(move)
local player = get_world_state().selected_player
if 0 > player then return end
for joint = 0, 19 do
local state = math.floor(move / 2 ^ (2 * joint)) % 4
set_joint_state(player, joint, state + 1)
end
set_grip_info(player, 11, math.floor(move / 2 ^ 40) % 2)
set_grip_info(player, 12, math.floor(move / 2 ^ 41) % 2)
end
-- commands
local commands = { ["load"] = set_move }
local function command (raw)
local input = raw:gmatch("%S+")
local cmd, args = input(), {}
for arg in input do table.insert(args, arg) end
if commands[cmd] then
commands[cmd](args)
return 1
end
end
add_hook("command", "movememory2", command)
-- hotkeys
-- mouse
local function mouse_button_down (button, X, Y)
if 1 ~= button then return end
echo("click! X: " .. X .. ", Y: " .. Y)
end
add_hook("mouse_button_down", "movememory2", mouse_button_down)
-- brains
local moving = false
local function enter_frame ()
if moving then return end
moving = true
moves[0] = get_move(0)
moves[1] = get_move(1)
if 0 == #openers then
openers[0] = moves[0]
openers[1] = moves[1]
end
end
add_hook("enter_frame", "movememory2", enter_frame)
local function enter_freeze ()
moving = false
end
add_hook("enter_freeze", "movememory2", enter_freeze)
local function match_begin ()
moving = false
openers = {}
end
add_hook("match_begin", "movememory2", match_begin)
-- gui
local function draw2d ()
if 0 == #openers then return end
if chat_input_is_active() then return end
local x, y = get_window_size()
set_color(0, 0, 0, 1)
draw_quad(x * .38 - 5, y - 110, x * .24 + 10, 48)
set_color(1/3, 1, 1/3, 1)
draw_centered_text("opener", y - 109, 2)
draw_centered_text("current", y - 89, 2)
set_color(1, 1/3, 1/3, 1)
draw_text(openers[0], x * .54, y - 105, 1)
draw_text(moves[0], x * .54, y - 85, 1)
set_color(1/3, 1/3, 1, 1)
draw_right_text(openers[1], x * .54, y - 105, 1)
draw_right_text(moves[1], x * .54, y - 85, 1)
end
add_hook("draw2d", "movememory2", draw2d)
-- end
echo "movememory2.lua by Pegasus Epsilon <pegasus@pimpninjas.org>"
-- (C) 2014 Distribute Unmodified -- http://pegasus.pimpninjas.org/license