Register to post in forums, or Log in to your existing account
 

Play RetroMUD
Post new topic  Reply to topic     Home » Forums » CMUD General Discussion
complex
Apprentice


Joined: 12 Apr 2004
Posts: 134
Location: USA

PostPosted: Fri Oct 01, 2010 9:33 pm   

Merge multiple triggers
 
I currently have two two triggers doing the work that one should be able to do, the reason for this is items can be represented in my inventory in two different ways, if there is a single of that item, or multiple.

IE:
Code:

( 3) a sword of the hero
     a flail of destruction


Is there a way to capture both inputs with one trigger? Currently this is my set up
Code:

<trigger name="matchWeapon" id="64">
  <pattern>*({@WeaponList})</pattern>
  <value>SellQueue = %addItem(@SellWeaponCMD.%2,@SellQueue)</value>
</trigger>

<trigger name="matchWeapons" priority="940" id="94">
  <pattern>(%d)*({@WeaponList})</pattern>
  <value>#LOOP %1 {SellQueue = %addItem(@SellWeaponCMD.%2,@SellQueue)}
</value>
</trigger>

but the first trigger will *sometimes* match the properties of the second trigger. By that I mean...
Code:

0.0011 |
0.0011 | f  Kingdoms |  Pattern: *({@WeaponList}) : (%1="a sword of the hero")
0.0013 | c  Kingdoms |  exec : Pattern "matchWeapon" : SellQueue = %addItem(@SellWeaponC...
0.0010 | n  Kingdoms |  Exec Trigger "matchWeapon"
0.0010 | k  Kingdoms |  Var "SellQueue" changed from "" to "|"
0.0010 | f  Kingdoms |  Pattern: (%d)*({@WeaponList}) : (%1=3, %2="a sword of the hero")
0.0015 | c  Kingdoms |  exec : Pattern "matchWeapons" : #LOOP %1 {SellQueue = %addItem(@...
0.0010 | n  Kingdoms |  Exec Trigger "matchWeapons"
0.0010 | k  Kingdoms |  Var "SellQueue" changed from "|" to "||sell hero"
0.0011 | k  Kingdoms |  Var "SellQueue" changed from "||sell hero" to "||sell hero|sell hero"
0.0011 | k  Kingdoms |  Var "SellQueue" changed from "||sell hero|sell hero" to "||sell hero|sell hero|sell hero"
_________________
<Clever text here>
Reply with quote
chamenas
Wizard


Joined: 26 Mar 2008
Posts: 1547

PostPosted: Fri Oct 01, 2010 9:42 pm   
 
I don't know if there's a way to make things optional in zscript, if there is, then use that. If no one suggests one, I can give you a REGEX pattern which will match both.
_________________
Listen to my Guitar - If you like it, listen to more
Reply with quote
complex
Apprentice


Joined: 12 Apr 2004
Posts: 134
Location: USA

PostPosted: Fri Oct 01, 2010 9:47 pm   
 
Tell me about these REGEX patterns you keep mentioning.
_________________
<Clever text here>
Reply with quote
MattLofton
GURU


Joined: 23 Dec 2000
Posts: 4834
Location: USA

PostPosted: Fri Oct 01, 2010 10:21 pm   
 
({~(%d~)|%s}) (*)

While that is all the pattern you really need, you could probably better refine the * with something tighter--depending on what symbolics are allowed to appear in an item name (if it's just letters and spaces, then you could use [%s%w]). In both Zscript and Regex.
_________________
EDIT: I didn't like my old signature
Reply with quote
chamenas
Wizard


Joined: 26 Mar 2008
Posts: 1547

PostPosted: Fri Oct 01, 2010 10:23 pm   
 
REGEX is short for regular expressions. Ultimately, zScript patterns are translated into Regular Expressions for purposes of matching. However, zScript is MUCH simpler than Regular Expressions. Still, Regular Expressions are significantly more powerful and, to my knowledge, can do things which zScript can't do. I've fallen into the habit of using Regular Expressions even when there is a perfectly acceptable zScript pattern, which some frown upon, but there are many scripts that I use which would require Regular Expressions to work.

The REGEX pattern you would want would be:
Code:

^(?:\((?:\s+)?(\d+)\)\s|\s+)([\w ]+)


You can change:
[\w ]+ to: [\w '-,]+ if there's ever commas, apostrophes or hyphens in the item names.

() Parentheses - When you put () around something it captures it, just like in zScript. However, in REGEX, parentheses can also be used to surround a list of items for an OR list in the pattern matching. In order to separate items in a list, you use |, just like in a stringlist: (item1|item2), you can see this above in the following: (?:\((?:\s+)?(\d+)\)\s|\s+) this says to match any of the following: any number of spaces, ( 1), or (11) where the 1s are interchangeable for any number. Thus, it can match either of the two patterns you gave before.

But parentheses don't just pull double duty, they also pull triple duty! You can use them as a container to make something optional. Anything encompassed in () and followed by a ?, aka (match)? means that the item is optional. If it's there, it's matched, otherwise the pattern matcher ignores it and continues trying to see if the pattern matches otherwise.

It should also be noted that since parentheses are used to capture in CMUD, you can use ?: just inside the parentheses to tell it NOT to capture something, aka (?:match).

\s, \d, \w - These should all work similar to their zScript companions, %s, %d, and %w. However, unlike their zScript companions, these little values only match 1 space, 1 digit, or 1 alpha character respectively. In order to get them to match a variable number of spaces, numbers or characters you must use a + sign: \s+, \d+, \w+.

[] - Square brackets in regex represent a range-class, matching anything within the class. Like the \s, \d, and \w values, they will only match 1 item in the class and then move on unless a + sign immediately follows. Thus, [\w ]+ will match any number of words until it runs upon something which is non-alpha or non-space, which is great for matching a number of words. But you can also expand it, as mentioned. Maybe some of the strings you need to match will have commas, by adding a comma to the range class [\w ,]+ you tell it to match any commas as well.

Edit:
And Matt posted a perfectly acceptable zScript instead ;), the one benefit of mine is that it will, in theory, be a much tighter match, but that is usually not a very meaningful statement, unless you find yourself running into similar patterns that you don't want to match but happen to match it incidentally.
_________________
Listen to my Guitar - If you like it, listen to more
Reply with quote
MattLofton
GURU


Joined: 23 Dec 2000
Posts: 4834
Location: USA

PostPosted: Fri Oct 01, 2010 10:45 pm   
 
Quote:

However, zScript is MUCH simpler than Regular Expressions. Still, Regular Expressions are significantly more powerful and, to my knowledge, can do things which zScript can't do.


In CMud Pro (the basic version I believe only allows one format), Zugg uses a handy 3rd-party database component that lets you open up any database format without having to worry about the details. This component is a convenience wrapper that makes databases easy for the user.

Zscript patternmatching is pretty the same thing for Regex. Sure, there are a handful of features on either side that the other cannot do as well or at all, but the vast majority of situations will not show any significant advantage between zscript and regex. They are functionally similar enough that you could get away with calling them identical.
_________________
EDIT: I didn't like my old signature
Reply with quote
chamenas
Wizard


Joined: 26 Mar 2008
Posts: 1547

PostPosted: Fri Oct 01, 2010 10:52 pm   
 
For the most part, maybe. But I definitely have a number of patterns which would be extremely difficult or impossible with zScript, which is why I stick to REGEX. However, I often shy away from helping out with patterns because I don't often know enough about zScript to say when it's not going to be enough anymore, and I certainly would rather people be able to use something which is far simpler to understand. Smile
_________________
Listen to my Guitar - If you like it, listen to more
Reply with quote
complex
Apprentice


Joined: 12 Apr 2004
Posts: 134
Location: USA

PostPosted: Sat Oct 02, 2010 2:04 am   
 
Thank you guys for help, I don't know if I am ready to jump into REGEX considering I am still learning zscript. Though I can see where knowing both will be very handy.
_________________
<Clever text here>
Reply with quote
complex
Apprentice


Joined: 12 Apr 2004
Posts: 134
Location: USA

PostPosted: Sat Oct 02, 2010 3:32 am   
 
Neither of the about options seem to word, the reason I did not use the ({~(%d~)|%s}) before is it does not allow for wild cards inside, and for my trigger to work I need the %d to be a wild card when a number is present. Anyway thanks =)
_________________
<Clever text here>
Reply with quote
chamenas
Wizard


Joined: 26 Mar 2008
Posts: 1547

PostPosted: Sat Oct 02, 2010 3:43 am   
 
Why does the number need to be a wild card? If it just a digit then %d or \d+ will work. We need more info, if there's things you're not telling us, we need to know about it or we cant help.
_________________
Listen to my Guitar - If you like it, listen to more
Reply with quote
complex
Apprentice


Joined: 12 Apr 2004
Posts: 134
Location: USA

PostPosted: Sat Oct 02, 2010 4:41 am   
 
My bad, I thought I went over that in the first post. So in the case when there are multiples of an item the trigger loops the number of need times, entering the command to sell the item to the queue each time.

Code:

( 3) a sword of the hero


should and does result in
Code:

0.0010 | f  Kingdoms |  Pattern: (%d)*({@WeaponList}) : (%1=3, %2="a sword of the hero")
0.0015 | c  Kingdoms |  exec : Pattern "matchWeapons" : #LOOP %1 {SellQueue = %addItem(@...
0.0010 | n  Kingdoms |  Exec Trigger "matchWeapons"
0.0010 | k  Kingdoms |  Var "SellQueue" changed from "|" to "||sell hero"
0.0011 | k  Kingdoms |  Var "SellQueue" changed from "||sell hero" to "||sell hero|sell hero"
0.0011 | k  Kingdoms |  Var "SellQueue" changed from "||sell hero|sell hero" to "||sell hero|sell hero|sell hero"


In the case where there is only one item, the above trigger does not word because %2 is no longer the name of the item.

I did modify the triggers, to stop using * which removed the false positive, but I cant think a way around the two trigger situation
Code:

<pattern>%s({@armorList})</pattern>
sellQueue = %addItem(@sellArmorCMD.%1,@sellQueue)


(%d)*({@armorList})
#LOOP %1 {SellQueue = %addItem(@SellArmorCMD.%2,@sellQueue)}


At this point its working, and its just a matter of cleaning things up a bit.
_________________
<Clever text here>
Reply with quote
chamenas
Wizard


Joined: 26 Mar 2008
Posts: 1547

PostPosted: Sat Oct 02, 2010 5:04 am   
 
My pattern differentiates the two

%1 is equal to a number, if it exists. If it doesn't exist then %1 is null and %2 is always equal to the item name. It matches both patterns you gave, for the first one %1=3 and %2="a sword of the hero" for the second %1=null and %2="a flail of destruction"
_________________
Listen to my Guitar - If you like it, listen to more
Reply with quote
complex
Apprentice


Joined: 12 Apr 2004
Posts: 134
Location: USA

PostPosted: Sat Oct 02, 2010 5:35 am   
 
I must be using it wrong I set the pattern as


^(?:\((?:\s+)?(\d+)\)\s|\s+)([\w ]+)({@armorList})
_________________
<Clever text here>
Reply with quote
Fizgar
Magician


Joined: 07 Feb 2002
Posts: 333
Location: Central Virginia

PostPosted: Sat Oct 02, 2010 7:49 am   
 
In zscript a pattern of
Code:
(*)a sword of the hero

with a script of
Code:
#switch (%match(%1,"~((%d)~) ",Swords)) {}
  (%null(%1)) {#var Swords 1}
  {}


would set the Swords variable to one if:
Code:
a sword of the hero

was displayed and 3 if
Code:
(3) a sword of the hero

was displayed.

I know that won't work for the ( 3) a sword of the hero you have as an example due to the space between the parenthesis without modification, but should be doable. I'd test it out now but I just finished a cup of coffee and am about to head out with hopes of bagging my first deer of the year this morning. Anyway, hope that is of some use.
_________________
Windows Vista Home Premium SP2 32-bit
AMD Athlon Dual Core 4400+ 2.31 GHz
3 GB RAM
CMUD 3.34
Reply with quote
complex
Apprentice


Joined: 12 Apr 2004
Posts: 134
Location: USA

PostPosted: Sat Oct 02, 2010 3:50 pm   
 
Fizgar that got me very close, thank you; however it does not match quite right here is how it looks, I do not know how to make the spaces copy correctly in the forums, in either of the patterns, but...

a sword of the hero = 5 leading white spaces, then the pattern to match
( 3) a sword of the hero = a "(" "1 white space, or none if there are more then 9 items, then the number, then a space then the pattern

When I put this pattern together
Code:

(*)({@armorList})


The following string then produces
( 3) a sword of the hero
%1 = 3)
%2 = a sword of the hero

I have played around with variations of ~( and (*~) but neither of them result in a pattern match
_________________
<Clever text here>
Reply with quote
chamenas
Wizard


Joined: 26 Mar 2008
Posts: 1547

PostPosted: Sat Oct 02, 2010 4:17 pm   
 
I mentioned that the ([\w ]+) was to match the item string, since you added something else to match the item string, you would need to replace it, not add something to it, it would be:
Code:

^(?:\((?:\s+)?(\d+)\)\s|\s+)(@armorList)
_________________
Listen to my Guitar - If you like it, listen to more
Reply with quote
complex
Apprentice


Joined: 12 Apr 2004
Posts: 134
Location: USA

PostPosted: Sat Oct 02, 2010 4:47 pm   
 
Touche' I misunderstood the way that the regex pattern there work, I thought the ([\w]+) was to match the script, BUT I thought I could pipe that value against the @armorList. It did not occur to me that I should drop the string match. Thanks again chamenas.
_________________
<Clever text here>
Reply with quote
MattLofton
GURU


Joined: 23 Dec 2000
Posts: 4834
Location: USA

PostPosted: Sat Oct 02, 2010 6:16 pm   
 
I missed the padding, but that's easy to match for in zscript:

{~(|}([%d%s]){~)|} ({@armorlist})
_________________
EDIT: I didn't like my old signature
Reply with quote
chamenas
Wizard


Joined: 26 Mar 2008
Posts: 1547

PostPosted: Sat Oct 02, 2010 7:06 pm   
 
Well struck, sir. Where's the page for zScript pattern matching? I forget...
_________________
Listen to my Guitar - If you like it, listen to more
Reply with quote
complex
Apprentice


Joined: 12 Apr 2004
Posts: 134
Location: USA

PostPosted: Sat Oct 02, 2010 7:13 pm   
 
This page? http://forums.zuggsoft.com/modules/mx_kb/kb.php?mode=doc&page=3&refpage=3&a=cmud_Pattern_Match
_________________
<Clever text here>
Reply with quote
Fizgar
Magician


Joined: 07 Feb 2002
Posts: 333
Location: Central Virginia

PostPosted: Mon Oct 04, 2010 5:11 am   
 
From what i'm using to test with this seems to work.

Code:
<trigger priority="10" id="1">
  <pattern>(*){@armorList}</pattern>
  <value>#switch (%match(%1,"~(( %d)~) ",Swords) or %match(%1,"~((%d)~) ",Swords)) {}
    (%match(%1,"%s")) {#var swords 1}
  {} </value>
</trigger>


Code:
<var name="armorList" type="StringList" id="3">
  <value>a sword of the hero</value>
  <json>["a sword of the hero"]</json>
</var>


Below is what i'm putting on the command line and changes that are made to the sword variable.


Code:
#show {( 5) a sword of the hero}
#var swords

@swords is changed to 5

Code:
 #show {     a sword of the hero}
#var swords

@swords is changed to 1

Code:
#show {(13) a sword of the hero}
#var swords

@swords is changed to 13

I know you aren't using @swords as a variable but if it will assign that variable correctly you should be able to make it work with what you are doing. Sorry for the delay in response time. Had a bit of an accident Saturday morning when my tree stand, I think someone tampered with broke, and I fell about 20 feet to the ground. Been liquored up all weekend for numbness.
_________________
Windows Vista Home Premium SP2 32-bit
AMD Athlon Dual Core 4400+ 2.31 GHz
3 GB RAM
CMUD 3.34
Reply with quote
complex
Apprentice


Joined: 12 Apr 2004
Posts: 134
Location: USA

PostPosted: Mon Oct 04, 2010 6:50 am   
 
Well the pattern works like a charm, I will have to play with to some tomorrow for looping, but is there a check for null? like
#if %1==NULL {add %2 once} {add %2 %1} ?
_________________
<Clever text here>
Reply with quote
Fizgar
Magician


Joined: 07 Feb 2002
Posts: 333
Location: Central Virginia

PostPosted: Mon Oct 04, 2010 10:22 am   
 
Code:
#if (%null(%1)) {add %2 once} {add %2 %1}

should work.
_________________
Windows Vista Home Premium SP2 32-bit
AMD Athlon Dual Core 4400+ 2.31 GHz
3 GB RAM
CMUD 3.34
Reply with quote
Display posts from previous:   
Post new topic   Reply to topic     Home » Forums » CMUD General Discussion All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

© 2009 Zugg Software. Hosted by Wolfpaw.net