Exit Capturing, the stupid way.

Post Reply
User avatar
Alexander Divine
Posts: 65
Joined: Mon Dec 21, 2009 7:01 pm

Exit Capturing, the stupid way.

Post by Alexander Divine »

It's me again! Ever-inquisitive.

Trigger filtering's pretty easy, I've already got it capturing my exits and later I'll make a purdy compass to go with.

However, for the sake of learning Lua, I'd like to know if there's a way I can do it without Mudlet's unique trigger-filter functionality.

In zMud, we would capture exits like this:

Code: Select all

Trigger: ^You see {a single exit|exits} leading (*).$

#var Exits {%1}
#var Exits {%replace( @Exits, ", ", "|")}
#var Exits {%replace( @Exits, "and ", "|")
Basically you capture the entire "north, west, southwest, and down" line, and then using replace functions, you tell it to replace the ", " and "and " bits with a pipe, which is the character zMud uses to separate items in a variable list.

Is there any way to do something similar to the above in Lua, or perhaps a clean, alternative method that doesn't require trigger filtering? (which I love, by the way, don't get me wrong)

Denarii
Posts: 111
Joined: Thu Dec 03, 2009 10:54 pm

Re: Exit Capturing, the stupid way.

Post by Denarii »

The first example is assuming you have the lrexlib library, which you should if you're running Mudlet 1.0.5 on Windows or Linux (and the Mac version is coming soon). If lrexlib is present you should see "[INFO] found Lua module rex_pcre" when connecting to a profile.

Code: Select all

Pattern: ^You see (?:a single )?exits? leading ([a-z, ]+)\.$

exits = {}
for s in rex.split(matches[2], [[, |and ]]) do
	exits:insert(s)
end
If lrexlib is unavailable:

Code: Select all

Pattern: ^You see (?:a single )?exits? leading ([a-z, ]+)\.$

exits = {}
do
	local i = 1
	local j
	local match = matches[2]:gsub(" and ", ","):gsub(", ", ",")
	while match:find(",", i) do
		j = match:find(",", i)
		table.insert(exits, match:sub(i, j-1))
		i = j+1
	end
	table.insert(exits, match:sub(i))
end

Iocun
Posts: 174
Joined: Wed Dec 02, 2009 1:45 am

Re: Exit Capturing, the stupid way.

Post by Iocun »

Just as a little additional note: Even without lrexlib you can do it quite as shortly, with string.gmatch():

Code: Select all

exits = {}
for s in string.gmatch(matches[2], "%w+") do
	exits[#exits+1] = s
end
(You'll still have to filter out words like "and", "open", "closed", "door", but that's relatively easy.)

User avatar
Alexander Divine
Posts: 65
Joined: Mon Dec 21, 2009 7:01 pm

Re: Exit Capturing, the stupid way.

Post by Alexander Divine »

Great, I think that might look like a very simple solution. I also do want it to capture doors so that oughta work nicely. You guys have been a great help, I appreciate it. At the rate I'm going I might just need to make myself a dedicated thread. XD

Post Reply