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.
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<item>.+) on (?P<hand>left|right) hand\.$"
match_inverse="y"
regexp="y"
send_to="12"
sequence="100"
>
<send>
hands = hands or {} -- make sure table exists
hands.%<hand> = "%<item>" -- now wielded
-- inform us what we did
ColourNote ("white", "green", "Now wielding %<item> in %<hand> 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<item>.+) on (?P<hand>left|right) hand\.$"
match_inverse="y"
regexp="y"
send_to="12"
sequence="100"
>
<send>
hands = hands or {} -- make sure table exists
hands.%<hand> = nil -- not wielded now
-- inform us what we did
ColourNote ("white", "maroon", "Now wielding nothing in %<hand> 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 < and so on. See:
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")
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.
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<item>[%a ]+) on (?P<hand>left|right) hand\.$"
regexp="y"
send_to="12"
sequence="100"
>
<send>
hands = hands or {} -- make sure table exists
for w in string.gmatch ("%<item>", "%a+") do
hands.%<hand> = w
end -- for
-- inform us what we did
ColourNote ("white", "green", "Now wielding " .. hands.%<hand> .. " in %<hand> 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.
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
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.