Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ MUSHclient ➜ Lua ➜ Saving player stats to variables

Saving player stats to variables

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Blixel   (80 posts)  Bio
Date Wed 09 Aug 2017 03:17 PM (UTC)

Amended on Wed 09 Aug 2017 03:18 PM (UTC) by Blixel

Message
In the game I'm playing, there is a "stats" command that will return the player's statistics. I'm trying to create a combination of triggers that will capture these stats as variables for later use.

The output of the stats command is as follows:

>stats

Strength:     18  (+2)
Dexterity:    7   (+1)
Intelligence: 6 
Wisdom:       8 
Charisma:     8 
Constitution: 18

Hit Points:   79    Hit Max: 82 
ArmorClass:   4     Level:   9  

You are a old chaotic-evil male dwarf fighter. You are wearing 0 rings. You are
wearing platemail and are armed with a greatsword.
Your rank is Lord.


Then I'll save several of these values as playerStr, playerDex, playerInt, and so on. I'd also like to be able to save playerLevel and playerClass from the other section.

So far I've just been focusing on capturing playerStr. I assume if I can figure that out, then getting the other 5 primary stats will be a copy/paste.

I've attempted a dozen or more regular expressions but I really struggle with these. This is as far as I've gotten, which I know is wrong, but this is the only thing I've done so far that gives me any thing at all:

^Strength:*(.+)


This captures...

     18  (+2)


...which of course I can't use. I can't figure out how to get just the 18.

So my specific question is, how do I capture the output of that stats command so that I can save the values as variables?

Thanks for any help.
Top

Posted by Nick Gammon   Australia  (23,140 posts)  Bio   Forum Administrator
Date Reply #1 on Wed 09 Aug 2017 09:46 PM (UTC)
Message
Template:regexp Regular expressions
  • Regular expressions (as used in triggers and aliases) are documented on the Regular expression tips forum page.
  • Also see how Lua string matching patterns work, as documented on the Lua string.find page.


Stay calm and just look at what the string contains. In your case try:


^Strength:\s+(\d+)


This is matching:


  • ^ --> start of line
  • Strength: --> literally that word and the colon
  • \s+ --> one or more spaces
  • (\d+) --> a capture group, consisting of digits only

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Blixel   (80 posts)  Bio
Date Reply #2 on Thu 10 Aug 2017 01:10 PM (UTC)
Message
Ok that worked. Thank you, I appreciate the fast reply. I keep thinking that the asterisks is the wildcard to match any number of arbitrary characters. At least that's how it works at the command line. ls *.txt for example.
Top

Posted by Blixel   (80 posts)  Bio
Date Reply #3 on Thu 10 Aug 2017 06:35 PM (UTC)
Message
I have another question relating to this same topic. I'm also wanting to setup a playerClass variable. The information from the game comes from the paragraph underneath of the stats.

You are a old chaotic-evil male dwarf fighter. You are wearing 0 rings. You are
wearing platemail and are armed with a greatsword.
Your rank is Lord.


The keywords for the class can be one of these words:
fighter, magic-user, cleric, ninja, assassin, fighter/cleric, or fighter/magic-user


My regular expression is working, but if the player is a fighter/cleric, it seems to find the word "cleric" first. And it doesn't help if I place the fighter/cleric class before the word cleric in the regular expression. It will still find "cleric" first.

So my question is, how can I prioritize the multi-class characters so that the regular expression will look for those first?

^You are a .*(fighter\/cleric|fighter\/magic-user|fighter|magic-user|cleric|ninja|assassin)
Top

Posted by Nick Gammon   Australia  (23,140 posts)  Bio   Forum Administrator
Date Reply #4 on Thu 10 Aug 2017 09:25 PM (UTC)

Amended on Thu 10 Aug 2017 09:27 PM (UTC) by Nick Gammon

Message
Your problem here is that the .* is "greedy". See the link above about what greedy means.

When it sees ".*(fighter\/cleric|cleric" it "greedily" consumes "fighter" as part of .* and can still match on "cleric" thus satisfying the regexp.

Make it non-greedy and it works:


^You are a .*?(fighter\/cleric|fighter\/magic-user|fighter|magic-user|cleric|ninja|assassin)


Note that ".*" is now ".*?"

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


18,284 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.