Speedwalk conversion

Posted by Hex1 on Fri 18 May 2007 07:54 AM — 3 posts, 14,278 views.

Australia #0
I've quite a few speedwalks in the old-fashioned
"
west
north
north
north
east
"
alias-type format (along with standard abbreviations [e ne n nw w sw s se] as well as other directions/commands such as 'out'), is there any automated way to convert them to the MUSHclient "w 3n e" speedwalk-type format?

I tried to write a Delphi application to do this, but unfortunately I had to format my PC recently so all my hard (and n00by, I'm sure a pro would have it done in the blink of an eye) work was lost.
Amended on Fri 18 May 2007 07:59 AM by Hex1
USA #1
Here you go, in Lua:

local dirToLetter = {
    north = "n", n = "n",
    northwest = "nw", nw = "nw",
    west = "w", w = "w",
    southwest = "sw", sw = "sw",
    south = "s", s = "s",
    southeast = "se", se = "se",
    east = "e", e = "e",
    northeast = "ne", ne = "ne",
}

local commands = {}
local count = 0

for line in io.lines() do

    -- only act on non-blank lines
    if string.match(line, "%S") then

        local last = commands[#commands]

        local letter = dirToLetter[line]

        assert(letter)

        -- first?
        if not last then
            commands[1] = {letter=letter,count=1}
        else
            -- same as previous?
            if last.letter == letter then
                last.count = last.count + 1
            else
                commands[#commands+1] = {letter=letter, count=1}
            end
        end
    end
end

for _,cmd in ipairs(commands) do
    if cmd.count > 1 then
        io.write(cmd.count)
    end
    io.write(cmd.letter, " ")
end

-- blank line
print()




Here's a demo of it working:


[david@thebalrog:~]$ cat tmp.txt
north
north
east
east
sw
sw
n
n
s
s

[david@thebalrog:~]$ lua converter.lua < tmp.txt
2n 2e 2sw 2n 2s 
Australia #2
Wow, thank you so much! :)
Just one more tiny thing, how would you implement two word commands, notably "journey <dir>" (eg. "journey ne"). I've already incorporated a few of my common phrases that are one word (up/down/out/backwards) but I can't seem to get that to work :(

EDIT: Never mind, I got it:

local dirToLetter = {
    north = "n", n = "n",
    northwest = "(nw)", nw = "(nw)",
    west = "w", w = "w",
    southwest = "(sw)", sw = "(sw)",
    south = "s", s = "s",
    southeast = "(se)", se = "(se)",
    east = "e", e = "e",
    northeast = "(ne)", ne = "(ne)",
    up = "(up)", u = "(up)",
    down = "(down)", d = "(down)",
    out = "(out)",
    backwards = "(backwards)", bw = "(backwards)",
    journeyqn = "(journey n)",
    journeyqs = "(journey s)",
    journeyqe = "(journey e)",
    journeyqw = "(journey w)",
    journeyqne = "(journey ne)",
    journeyqnw = "(journey nw)",
    journeyqse = "(journey se)",
    journeyqsw = "(journey sw)"
}

local commands = {}
local count = 0

for line in io.lines() do

    -- only act on non-blank lines
    if string.match(line, "%S") then

        local last = commands[#commands]

         line = string.gsub(line, " ", "q")

        local letter = dirToLetter[line]

        assert(letter)

        -- first?
        if not last then
            commands[1] = {letter=letter,count=1}
        else
            -- same as previous?
            if last.letter == letter then
                last.count = last.count + 1
            else
                commands[#commands+1] = {letter=letter, count=1}
            end
        end
    end
end

for _,cmd in ipairs(commands) do
    if cmd.count > 1 then
        io.write(cmd.count)
    end
    io.write(cmd.letter, " ")
end

-- blank line
print()