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, 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.
 Entire forum ➜ MUSHclient ➜ Lua ➜ Keeping track and switching items held

Keeping track and switching items held

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


Posted by Thalir   (13 posts)  Bio
Date Fri 03 Aug 2007 03:49 PM (UTC)
Message
I've just started using mushclient, so I'll fit multiple questions here in order to learn different techniques.
Hope I lay down my concepts clearly, any advice or suggestion is much appreciated.

Background:
blue-green knife
sharp sword
small axe

atk1 requires knife and sword, atk2 requires sword and axe, atk3 requires knife, etc...
Need to keep track of what is held on my LEFT or RIGHT hand.
So when I use alias atk1, I will hold knife and sword, when I do atk2, axe will replace knife,
on whatever hand it was. Hope you get the idea.

My plan:
-Variable-
item = (blue-green knife|sharp sword|small axe)

-triggers-
^You wield (@!item) on left hand.$
^You remove the (@!item) on right hand.$

-script (left hand example, called by triggers when holding or removing weapons)-
hands = {}
hands.left.held = 0 -- when hand is empty
hands.left = {knife = "blue-green knife", sword = "sharp sword", axe = "small axe"}

Scenario (what I hope to do):
Recieve trigger --> You wield sharp sword on left hand.
Calls function in script, using matching variable from trigger ("sharp sword"), determines it matches
--> hands.left.sword = "sharp sword"
Q1) How do I link the matching variable from the trigger to my script, in order to match it against my table?

Now takes the key of (hands.left.sword = "sharp sword"), which is sword, and stores it in (hands.left.held = sword)
Q2) How can I isolate the key from here?

If I use
^You wield (@!item) on (@!handside) hand.$
(where I'll make @handside = left|right)
Q3) How can I get the first and second matching variables in a script? Like how can I do World.Note (first variable, second variable)

(So from then on, I can directly use a function to unwield the (hands.left.held = sword) value. When I use an alias, it will do a series of IF statements to check and wield, from the values in the tables. I've got this bit covered I think)

I'm sure I'll have more questions stemming from this, as I try to implement it. I hope this isn't too confusing.
Top

Posted by Nick Gammon   Australia  (23,046 posts)  Bio   Forum Administrator
Date Reply #1 on Sat 04 Aug 2007 12:02 AM (UTC)

Amended on Sat 04 Aug 2007 12:19 AM (UTC) by Nick Gammon

Message
Well, let's start simply. We'll make a trigger that keeps track of what you are wielding:


<triggers>
  <trigger
   enabled="y"
   match="^You wield (?P&lt;item&gt;.+) on (?P&lt;hand&gt;left|right) hand\.$"
   match_inverse="y"
   regexp="y"
   send_to="12"
   sequence="100"
  >
  <send>

hands = hands or {}  -- make sure table exists
hands.%&lt;hand&gt; = "%&lt;item&gt;"  -- now wielded

-- inform us what we did
ColourNote ("white", "green", "Now wielding %&lt;item&gt; in %&lt;hand&gt; hand")

</send>
  </trigger>
</triggers>


I didn't use a table of what you might wield, I presume if you get the message, you wielded something valid.

We also need a trigger to handle removing the item:


<triggers>
  <trigger
   enabled="y"
   match="^You remove the (?P&lt;item&gt;.+) on (?P&lt;hand&gt;left|right) hand\.$"
   match_inverse="y"
   regexp="y"
   send_to="12"
   sequence="100"
  >
  <send>

hands = hands or {}  -- make sure table exists
hands.%&lt;hand&gt; = nil  -- not wielded now

-- inform us what we did
ColourNote ("white", "maroon", "Now wielding nothing in %&lt;hand&gt; hand")

</send>
  </trigger>
</triggers>


Now we need an alias to handle wielding what you want. I am not sure of your exact MUD syntax, but this will give the general idea:


<aliases>
  <alias
   match="atk1"
   enabled="y"
   send_to="12"
   sequence="100"
  >
  <send>

-- assume we want knife in left hand and sword in right hand

hands = hands or {}  -- ensure table exists

-- wield knife in left hand, if not there already
if hands.left ~= "knife" then  -- not what we want
  if hands.left then
    Send ("unwield " .. hands.left)
  end -- if not nothing
  Send ("wield knife on left hand")
end -- wielding something else

-- wield sword in right hand, if not there already
if hands.right ~= "sword" then  -- not what we want
  if hands.right then
    Send ("unwield " .. hands.right)
  end -- if not nothing
  Send ("wield sword on right hand")
end -- wielding something else

</send>
  </alias>
</aliases>


These will be easier to read if you copy and paste into MUSHclient, that will convert the &lt; and so on. See:

http://www.mushclient.com/pasting

- Nick Gammon

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

Posted by Nick Gammon   Australia  (23,046 posts)  Bio   Forum Administrator
Date Reply #2 on Sat 04 Aug 2007 12:23 AM (UTC)
Message
You could generalize the whole wielding stuff into a function (which you might put in your script file), so that you don't need to have nearly identical code in lots of aliases, eg.



function wield_stuff (left, right)

  hands = hands or {}  -- ensure table exists
  
  -- wield 'left' item in left hand, if not there already
  if hands.left ~= left then  -- not what we want
    if hands.left then
      Send ("unwield " .. hands.left)
    end -- if not nothing
    Send ("wield " .. left .. " on left hand")
  end -- wielding something else
  
  -- wield 'right' item in right hand, if not there already
  if hands.right ~= right then  -- not what we want
    if hands.right then
      Send ("unwield " .. hands.right)
    end -- if not nothing
    Send ("wield " .. right .. " on right hand")
  end -- wielding something else

end -- wield_stuff 



Now in your alias you just have to do this:


-- assume we want knife in left hand and sword in right hand
wield_stuff ("knife", "sword")


- Nick Gammon

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

Posted by Thalir   (13 posts)  Bio
Date Reply #3 on Sat 04 Aug 2007 02:26 AM (UTC)
Message
Thank you very much, I've understood the ideas so far!

A problem:
The weapons, be it small axe, large sword...
all have an adjective in front of the actual noun I need.

I can't send the command to hold the small axe, large sword.
I can send the command to hold the axe, sword.

My ideas:
1) Somehow capture the last word (I've checked, the last word is always they keyword/noun) of that variable
OR
2) Use a preset table e.g. hands.left = {axe = "small axe"}
and when the variable captured matches, I can take the key "axe" as the value for when I hold the item.

I'm not sure of the actual scripting or whether these two ideas are viable. My gratitude in advance.
Top

Posted by Nick Gammon   Australia  (23,046 posts)  Bio   Forum Administrator
Date Reply #4 on Sat 04 Aug 2007 04:24 AM (UTC)

Amended on Sat 04 Aug 2007 06:29 AM (UTC) by Nick Gammon

Message
This is fairly easily solved. The trigger that matches wielding can be modified to only save the last word:


<triggers>
  <trigger
   enabled="y"
   match="^You wield (?P&lt;item&gt;[%a ]+) on (?P&lt;hand&gt;left|right) hand\.$"
   regexp="y"
   send_to="12"
   sequence="100"
  >
  <send>

hands = hands or {}  -- make sure table exists

for w in string.gmatch ("%&lt;item&gt;", "%a+") do
  hands.%&lt;hand&gt; = w
end -- for

-- inform us what we did
ColourNote ("white", "green", "Now wielding " .. hands.%&lt;hand&gt; .. " in %&lt;hand&gt; hand")

</send>
  </trigger>
</triggers>


I modified the original regexp a bit to make sure we got a match in string.gmatch. It now matches on %a and a space, so we expect letters or spaces. If there might be things like hyphens you need to change the regexp and the string.gmatch, slightly.

- Nick Gammon

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

Posted by Thalir   (13 posts)  Bio
Date Reply #5 on Sun 05 Aug 2007 02:15 PM (UTC)
Message
How may I alter the regexp to match hyphens?
=========================
I was testing, when I was wielding, my trigger calls
script="wielding"

In my script (xx.lua file)

function wielding (name, line, wildcards)
hands = hands or {}
item = wildcards.item -- store item string (e.g. large axe)
side = wildcards.hand -- store hand side

for w in string.gmatch (item, "%a+") do
hands[side] = w
end

testvariable = "testing"
end

How come I can't access any variables made in this function or hands[side], when I try to make an alias to call another function that refers to them?

e.g. alias calls function in xx.lua script
function checkitems (name, line, wildscards)
world.Note(hands[side])
world.Note(testvariable)
end

this returns nil values!
Top

Posted by Shaun Biggs   USA  (644 posts)  Bio
Date Reply #6 on Sun 05 Aug 2007 07:52 PM (UTC)
Message
Try declaring side,testvariable = "" at the top of the script file. They might be considered local to the wielding function.

It is much easier to fight for one's ideals than to live up to them.
Top

Posted by Nick Gammon   Australia  (23,046 posts)  Bio   Forum Administrator
Date Reply #7 on Sun 05 Aug 2007 10:20 PM (UTC)
Message
Quote:

How may I alter the regexp to match hyphens?


In the trigger, add a hyphen at the start or the end, eg.


^You wield (?P<item>[-%a ]+) on (?P<hand>left|right) hand\.$


Same with the gmatch:


for w in string.gmatch (item, "-%a+") do
  hands [side] = w
end


Quote:

How come I can't access any variables made in this function or hands[side], when I try to make an alias to call another function that refers to them?


You should be able to do that. Unless you put the "local" keyword in front of the variable declaration, it will be globally available. It might be a sequence issue.

Until assign to a variable it is nil. So, if you call the alias first, testvariable will be nil. If you call the trigger function first, it should be assigned OK.

- 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.


21,473 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.