Wait and Wait Line

Share your scripts and packages with other Mudlet users.
zhenzh
Posts: 68
Joined: Fri Apr 17, 2020 2:23 am

Wait and Wait Line

Post by zhenzh »

The script is to implement the result that suspend current function and waiting specified time or pattern coming.
It helps combining simple actions to a set of action chain which get your functions modular.
As the functions uses coroutine yield/resume, you have to wrap all your implemention in a coroutine function.

Syntax:

wait(time)
Get your function flow being suspended for specified time.
Parameters
  • time: the time in seconds for which to wait
wait_line(pattern, timeout[, action])
Get your function flow being suspended until the specified pattern line coming. Matche values from regex will be returned.
Parameters
  • pattern: string or table regular expression that lines will be matched on, function flow will not go ahead until this pattern is matched.
    When using table pattern format, multiple patterns can be specified to simulate waitting multi-line.
    Lines will be compared one after another as the sequence of the table. The flow will not go ahead until all patterns are matched.
  • timeout: seconds to wait for the pattern line, will resume the coroutine with false returned. 0 for no timeout.
  • action: any behavior commands to be sent to server, multiple commands can be accepted with command separator.

Example

Code: Select all

coroutine.wrap(function()
    send("say this is a test")
    wait(3)
    send("say 3 seconds later")
end)()

function kill_enemy(target)
    local result = wait_line([[^(\S+) die]], "kill "..target)
    if result[2] == target then
        return true
    else
        return false
    end
end

coroutine.wrap(function()
    if kill_enemy("monster") == true then
        send("say job success")
    else
        send("say job fail")
    end
end)()
Attachments
wait-and-wait-line-v4.xml
(3.6 KiB) Downloaded 700 times
Last edited by zhenzh on Tue Mar 16, 2021 2:57 pm, edited 3 times in total.

User avatar
Vadi
Posts: 5035
Joined: Sat Mar 14, 2009 3:13 pm

Re: Wait and Wait Line

Post by Vadi »

It's important that your code goes inside:

Code: Select all

coroutine.wrap(
  function()
    -- your code here
  end
)()
This is working well :) unlike zMUD, running many of these shouldn't be a problem either because it uses tempTimer() and tempRegexTrigger() that you might already know, but this is new tech - so caveat emptor.

Some more examples in addition to the ones on top:

Code: Select all

coroutine.wrap(function()
    print("one!")
    wait(1)
    print("two!")
    wait(2)
    print("three!")
end)()
Selection_547.png

Code: Select all

coroutine.wrap(
  function()
    cecho("<green>waiting for exits line...\n")
    wait_line([[^You see a single exit leading west\.$]])
    cecho("\n<green>found it!")
  end
)()
Selection_549.png

Code: Select all

coroutine.wrap(
  function()
    cecho("<green>waiting for the line...\n")
    local matches = wait_line([[^You see exits leading (\w+), (\w+), (\w+), and (\w+)\.$]])
    cecho("\n<green>First three matches are: "..matches[2]..", "..matches[3]..", and "..matches[4])
  end
)()
Selection_550.png

kotokz
Posts: 2
Joined: Sun Apr 05, 2020 1:14 pm

Re: Wait and Wait Line

Post by kotokz »

i'd suggest the wait_line should have timeout option, which is quite useful IMO.

zhenzh
Posts: 68
Joined: Fri Apr 17, 2020 2:23 am

Re: Wait and Wait Line

Post by zhenzh »

Here's the v2 update for improving error reporting and timeout introduced

Saros
Posts: 12
Joined: Sat Oct 15, 2016 10:10 pm

Re: Wait and Wait Line

Post by Saros »

Thanks for this!

I don’t see v2 attached, can you try again?

User avatar
Vadi
Posts: 5035
Joined: Sat Mar 14, 2009 3:13 pm

Re: Wait and Wait Line

Post by Vadi »

Check out the first post.

kotokz
Posts: 2
Joined: Sun Apr 05, 2020 1:14 pm

Re: Wait and Wait Line

Post by kotokz »

This looks pretty good so far, few suggestions tho
1. Better wrap the thread and functions under a module, it's using global map which name is threads. i think it's a very common name to cause collision problem
2. The timeout timer killing function is saved in thread array, but no one actually calls it. Probably fine given the regex tirgger only fires once.

also i'm trying to put in loop for mutiple lines capture with same pattern, this could cause mudlet hung during my testing (win10 pc)
i don't quite get why still, don't think it's package problem, but just want to post here for awareness

for exampl:
In-mud text to testing:
You are using:
{Worn On Body} a set of simple cloth robes
{Worn On Legs} a pair of cloth pants
{Worn On Arms} a pair of cloth sleeves
{Worn About Body} a leather backpack
{Wielded} a small iron dagger
{Worn As Ammo Pouch} a small leather quiverending loop
Armr: None, Shld: None, Ench: +0, Pnlty: 0, MaxDex: No-Max, SpellFail: 0.

Code: Select all

coroutine.wrap(function()
    send("say this is a test")
    send("eq")      --  this cmd to trigger the above text. so it's a simple test to print each worn body part only.
    while true do
       local result = wait_line([[Worn on ([^\}]+)|Worn As]], 2)
       if result and result[2] then
        print("part = ".. result[2])
       else
        print("ending loop")
        break
       end       
    end
end)()
this will cause mudlet hung forever

zhenzh
Posts: 68
Joined: Fri Apr 17, 2020 2:23 am

Re: Wait and Wait Line

Post by zhenzh »

#1 I have set the threads local variable which can not be reached from global at all. You can try the v3 script in #1 post edited.
#2 The timer killing function do be called by trigger execuation, "threads[name][3]()" which is to stop effect of timeout at the point of time that the trigger is fired. The trigger and the timer contains killing the other elements each other to confirm any one of them can get ride of the operation from the others when it meet the execution condition.
#3 I'm not sure why your mudlet hung by running you while loop as I can not re-produce from my env. What the sympton of hung as you mentioned? console no response or mudlet crash? I have had the wait_line enhanced in v3 which supports multi-line wait, maybe you can have a try for your case.

zhenzh
Posts: 68
Joined: Fri Apr 17, 2020 2:23 am

Re: Wait and Wait Line

Post by zhenzh »

v3 script enhancement:
1. localize variable threads to avoid getting conflict from global env.
2. wait_line supports multi-line by defining pattern as a table format like {pattern1, pattern2, pattern3, ...}. Captured values will be returned as what the mudlet multimatches variable. Single line match can still be used by defining pattern as a string format.

See example for multi-line wait from the attached picture
Attachments
捕获.PNG

User avatar
Vadi
Posts: 5035
Joined: Sat Mar 14, 2009 3:13 pm

Re: Wait and Wait Line

Post by Vadi »

How is it working out for you? Going well?

Post Reply