<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE muclient>
<!--
  Six Swords for MUSHclient (#245). Save this file, then File > Plugins > Add.
  Ctrl+Space completes from the game itself, so it knows what each word should be ("cast l"
  completes a spell); when it can't get further it lists the choices. Tab stays MUSHclient's own.
  Ctrl+U clears the line. Ctrl+R shows the last reply again.
  Sound: MUSHclient plays only local WAV files and the game's clips are MP3 on the web, so this
  version leaves MSP off (no raw sound lines for the screen reader); see the soundpack (#246).
-->
<muclient>
<plugin
   name="Six_Swords"
   author="Six Swords"
   id="31a7f16dea6b27c1f9e3442a"
   language="Lua"
   purpose="Six Swords: Ctrl+Space completes from the game, Ctrl+U and Ctrl+R"
   date_written="2026-11-28"
   requires="4.73"
   version="1.2"
   save_state="n"
   >
<description trim="y">
<![CDATA[
Six Swords for MUSHclient.

Ctrl+Space completes from the game: "cast l" completes a spell. When it can't get further, it lists
the choices.
Ctrl+U clears the line.
Ctrl+R shows the last reply again.
]]>
</description>
</plugin>

<script>
<![CDATA[
local IAC, SB, SE = string.char(255), string.char(250), string.char(240)
local GMCP = 201

local function gmcpSend(text)
  SendPkt(IAC .. SB .. string.char(GMCP) .. text .. IAC .. SE)
end

local ok, json = pcall(require, "json")

-- the command window's text replaced (SetCommand only works on an empty one)
local function setLine(text)
  SelectCommand()
  PasteCommand(text)
end

-- Ctrl+Space: the line goes to the game, which completes it the way it reads it
function SixSwordsTab()
  local line = GetCommand()
  if line == "" or not ok then
    return
  end
  gmcpSend("Game.Complete " .. json.encode({ line = line }))
end

function SixSwordsClear()
  setLine("")
end

function OnPluginInstall()
  AcceleratorTo("Ctrl+Space", "SixSwordsTab()", sendto.script)
  AcceleratorTo("Ctrl+U", "SixSwordsClear()", sendto.script)
  Accelerator("Ctrl+R", "repeat")
end

-- agree to GMCP when the game offers it, then say who we are and what we take
function OnPluginTelnetRequest(option, data)
  if option ~= GMCP then
    return false
  end
  if data == "WILL" then
    return true
  end
  if data == "SENT_DO" then
    gmcpSend('Core.Hello { "client": "MUSHclient", "version": "' .. Version() .. '" }')
    gmcpSend('Core.Supports.Set [ "Game 1" ]')
    return true
  end
  return false
end

-- the game's answer: completed as far as the choices agree, or, when that gets no further, the
-- choices on one line (a screen reader reads it)
function OnPluginTelnetSubnegotiation(option, data)
  if option ~= GMCP or not ok then
    return
  end
  local name, body = string.match(data, "^%s*(%S+)%s*(.*)$")
  if not name or string.lower(name) ~= "game.completion" then
    return
  end
  local c = json.decode(body)
  if not c or c["for"] ~= GetCommand() then
    return -- typed on since: the answer is stale
  end
  if c.line ~= c["for"] then
    setLine(c.line)
  elseif c.options and #c.options > 1 then
    Note(table.concat(c.options, ", "))
  end
end
]]>
</script>
</muclient>
