Posted by
| Nick Gammon
Australia (23,133 posts) Bio
Forum Administrator |
Message
| Yes, well I was really responding to Peeka, who seemed to want a method to pause a speedwalk temporarily, until the "exits" line arrived to indicate s/he had moved to the next room.
However on re-reading the original post, I see that what this thread is really about is pausing on command (perhaps to chat, or eat/drink etc.).
A modified form of my proposed solution is below. The main alias "! <speedwalk>" now checks a global variable pause_speedwalk and also abort_speedwalk, to see if pausing/aborting the speedwalk is wanted.
If pausing is wanted, it remembers its own thread address in speedwalk_thread, and then calls coroutine.yield to yield execution indefinitely, effectively pausing itself.
Then we have three aliases, which do the following:
- pause speedwalk - this sets the pause_speedwalk flag.
This flag is tested next time "through the loop" by the main speedwalk alias, to pause itself.
- resume speedwalk - this calls coroutine.resume to make the speedwalk thread resume.
- abort speedwalk - if the speewalk is paused it calls coroutine.resume to make the speedwalk thread resume, however sending the "abort" argument to make it wrap itself up.
If the speedwalk is not paused it simply sets the abort_speedwalk flag to cause the speedwalk thread to stop executing.
I took out the bit about checking for the "exits" line, however that was really only for simplicity. By adding that back in you have a quite powerful speedwalk system, one that:
- Sends speedwalks with a time-delay you choose between them
- Waits until you have changed rooms until it sends the next speedwalk direction
- Checks for message like "You cannot go that way" to stop uselessly spamming the MUD with directions that won't work
- Can be paused
- Can be resumed
- Can be abandoned
<aliases>
<alias
match="!*"
enabled="y"
send_to="12"
sequence="100"
>
<send>do local t = coroutine.create (function (t)
local lines = {}
sw = EvaluateSpeedwalk ("%1")
if string.sub (sw, 1, 1) == "*" then
ColourNote ("white", "red", string.sub (sw, 2))
return
end -- if
rex.new ("(.+)"):gmatch (sw,
-- build speedwalk lines into a table
function (m)
table.insert (lines, m)
end)
pause_speedwalk = false -- no pause yet
abort_speedwalk = false -- no abort yet
speedwalk_thread = t -- remember this thread
-- iterate the table, sending each line to the MUD
for i, line in ipairs (lines) do
-- see if pausing wanted (global variable set)
if pause_speedwalk then
ColourNote ("white", "green", "Speedwalk paused.")
ret = coroutine.yield ()
if ret == "abort" then
ColourNote ("black", "yellow", "Speedwalk abandoned.")
speedwalk_thread = nil
return
end -- abort speedwalk wanted
ColourNote ("white", "green", "Speedwalk resumed.")
pause_speedwalk = false
end -- if
-- see if aborting wanted
if abort_speedwalk then
ColourNote ("black", "yellow", "Speedwalk abandoned.")
speedwalk_thread = nil
return
end -- if
-- send the speedwalk
Send (line)
-- wait 1.5 seconds
wait (t, 1.5)
end -- of iterating through each speedwalk line
-- all done!
ColourNote ("white", "blue", "Speedwalk done.")
speedwalk_thread = nil
end) assert (coroutine.resume (t, t)) end
</send>
</alias>
<alias
match="pause speedwalk"
enabled="y"
send_to="12"
sequence="100"
>
<send>if speedwalk_thread then
if pause_speedwalk then
ColourNote ("black", "yellow", "The speedwalk is already paused.")
else
pause_speedwalk = true
end
else
ColourNote ("black", "yellow", "No speedwalk is active.")
end
</send>
</alias>
<alias
match="resume speedwalk"
enabled="y"
send_to="12"
sequence="100"
>
<send>if speedwalk_thread then
if pause_speedwalk then
assert (coroutine.resume (speedwalk_thread, "resume"))
else
ColourNote ("black", "yellow", "The speedwalk is not paused.")
end
else
ColourNote ("black", "yellow", "No speedwalk is active.")
end</send>
</alias>
<alias
match="abort speedwalk"
enabled="y"
send_to="12"
sequence="100"
>
<send>if speedwalk_thread then
if pause_speedwalk then
assert (coroutine.resume (speedwalk_thread, "abort"))
else
abort_speedwalk = true
end
else
ColourNote ("black", "yellow", "No speedwalk is active.")
end</send>
</alias>
</aliases>
|
- Nick Gammon
www.gammon.com.au, www.mushclient.com | Top |
|