Companion = require("Companion")
thready = require("thready")
local name_file = fs.open("name.txt", "r")
local name = name_file.readAll()
ccstr = require(cc.strings)
companion = Companion:new(name)
lastcmd = {}
function copy(orig)
if type(orig) ~= 'table' then
return orig
end
local copy_table = {}
for k, v in pairs(orig) do
copy_table[k] = copy(v)
end
return copy_table
end
cmd_array = { guard=next_guard,
move = android.moveTo,
follow = companion:follow,
spelunk = companion:spelunk,
equip = companion:equip_from_stash,
unequip = companion:stash_in_any,
resume = resume_func,
stop = stop_func,
focus = focus_func,
unfocus = unfocus_func,
refuel = companion:refuel,
stayclose = stay_close_func,
attack = attack_func,
stand_down = stand_down_func,
clear_hostiles = clear_hostiles_func,
}
fnc_array ={guard=nil,
move = next_move,
follow = next_follow,
spelunk = next_spelunk,
equip = next_equip,
unequip = next_unequip,
resume = nil,
stop = nil,
focus = nil,
unfocus = nil,
refuel = next_refuel,
stayclose = nil,
attack = nil,
stand_down = nil,
clear_hostiles = nil,
}
function command_loop(cmd)
thready.main_loop()
while true do
_, message, sender, uuid = os.pullEvent("onChatMessage")
if sender ~= companion.player["name"] or not companion.focused then
goto continue
end
local parsed_cmd
if cmd ~= nil then
parsed_cmd = copy(cmd)
else
parsed_cmd = ccstr.split(message, "%s+")
end
local seed_str = parsed_cmd[1]
local seed_cmd = nil
if seed_str then
seed_cmd = cmd_array[seed_str]
end
if seed_cmd then
if seed_str ~= "stop" then
android.sendMessage("Changing commands...")
stop_func()
thready.spawn("cmd", run_command, seed_cmd, seed_str, parsed_cmd)
android.sendMessage("Engaging new command.")
else
android.sendMessage("Stopping current objective...")
stop_func()
android.sendMessage("Ready for next command.")
end
else
goto continue
end
last_cmd = copy(parsed_cmd)
::continue::
end
end
function run_command(seed_cmd, seed_str, parsed_cmd)
if fnc_array[seed_str] ~= nil then
seed_cmd(unpack(fnc_array[seed_str](parsed_cmd)))
else
seed_cmd(parsed_cmd)
end
end
function next_guard(list)
if #list == 4 then
companion:guard_location({posX=tonumber(list[2]), posY = tonumber(list[3]), posZ=tonumber(list[4])})
else
companion:guard_entity(list[2])
end
end
function next_move(list)
return tonumber(list[2]), tonumber(list[3]), tonumber(list[4])
end
function next_follow(list)
return list[2]
end
function next_spelunk(list)
return list[2]
end
function next_refuel(list)
return list[2]
end
function next_equip(list)
return list[2], list[3]
end
function resume_func(list)
if lastcmd[1] ~= "resume" then
command_loop(lastcmd)
else
android.sendMessage("You cannot use resume twice in a row")
end
function stop_func(list)
android.cancelTask()
companion.stop = true
while True do
local running = companion.running_func
sleep(.005)
local still_running = companion.running_func
sleep(.2)
if not running and not still_running then
break
end
end
companion.stop = false
end
function focus_func(list)
if list[1] == companion.name then
companion.focused = true
end
end
function unfocus_func(list)
if list[1] == companion.name then
companion.focused = false
end
end
function stay_close_func(list)
companion.stayclose = list[2] == "true"
end
function attack_func(list)
companion.hostiles[list[2]] = true
end
function stand_down_func(list)
companion.hostiles[list[2]] = nil
end
function clear_hostiles_func(list)
companion.hostiles = {}
end