Unexpected script performance

Posted by BrainFRZ on Sat 27 Aug 2011 10:25 PM — 12 posts, 35,019 views.

USA #0
I am trying to write a script that will allow me to detect an enemy's style and change my style accordingly. The script is as follows:
<aliases>
  <alias
   name="astyle"
   match="^(c|r)?as(d|t|ty|tyl|tyle)(?: )?([kar|kick|aik|kung])?(?: )?([0-9]{1,3})?(?: )?([0-9]{1,3})?$"
   enabled="y"
   group="combat"
   regexp="y"
   send_to="12"
   sequence="100"
  >
  <send>-- This chunk is to assert all the variables haven't somehow been corrupted between uses.
if (GetVariable("style") == nil) or (GetVariable("style") ~= "kar" and GetVariable("style") ~= "kick" and GetVariable("style") ~= "aik" and GetVariable(")style ~= "kung") then
  SetVariable("style", "kar")
end -- if

if GetVariable("strength") == nil or tonumber(GetVariable("str")) &gt; 200 or tonumber(GetVariable("str")) &lt; 1 then
  SetVariable("strength", 100)
end -- if

if GetVariable("speed") == nil or tonumber(GetVariable("spd")) &gt; 200 or tonumber(GetVariable("spd")) &lt; 1 then
  SetVariable("speed", 100)
end -- if



if "$1" == "c" then -- see "current" astyle
  Note("You are now using " .. GetVariable("style") .. " at " .. GetVariable("strength") .. "% strength and " .. GetVariable("speed") .. "% speed.")
elseif "$1" == "r" then -- "reset" style to defaults
  SetVariable("style", "kar")
  SetVariable("strength", 100)
  SetVariable("speed", 100)
  Note("Style has been reset to defaults.")
elseif "%2" == "d" then -- "detect" opponent's style
  Send("astyle detect strength")
  Send("astyle detect speed")


else -- actually change your style
  if "%3" == "" then -- use default style if none is provided
    SetVariable("style", "kar")
  elseif "%3" ~= GetVariable("style") then -- update style
    SetVariable("style", "%3")
  end -- if

  if "%5" ~= "" and "%5" ~= nil then -- require both str and spd to be used to change them
    SetVariable("strength", tonumber("%4"))
    SetVariable("speed", tonumber("%5"))
  end -- if

  Execute("castyle")
  Send("astyle " .. GetVariable("style") .. " " .. GetVariable("str") .. " " .. GetVariable("spd"))
end -- if
</send>
  </alias>
</aliases>


When I type castyle, instead of showing me my current style, it changes my style to default 20 times (I counted). Furthermore, it doesn't recognize anything other than castyle (which it mishandles) and asd (which it actually handles correctly). Can anyone explain this and possibly help me to fix it?
Amended on Sat 03 Sep 2011 09:55 PM by BrainFRZ
Australia Forum Administrator #1
This line:


  Execute("castyle")


is re-calling the alias. So it is a wonder it didn't loop forever.


if "$1" == "c" then -- c for "see"


What is $1? That line will never be true. Do you mean %1?
USA #2
Oh!!! That's what the problem was. I did mean "%1". Thanks! But it still doesn't seem to accept any arguments about style, strength or speed, nor remember them.

Edit:
As for Execute("castyle"), that was intended recursion. It'll only repeat once though. The problem was the condition wasn't screened out, because of my typo.
Amended on Sun 28 Aug 2011 12:23 AM by BrainFRZ
USA #3
Does anyone have any ideas on how I can fix it? I think I must've made a problem with the regex (either in the expression itself or related to it). I'd really appreciate any help anyone could give me.
USA Global Moderator #4
BrainFRZ said:
But it still doesn't seem to accept any arguments about style, strength or speed, nor remember them.

Is there a reason why you've tried to combine a bunch of functionally different concepts into a single alias? I bet if you split into multiple aliases you'd get it to work. Beyond that, in order to tell you what to fix we need to know what you're typing.
USA #5
Well, the first half seems to work just fine. To test it, I ran the following.

Execute("castyle")
SetVariable("style", "kick")
Execute("castyle")
SetVariable("strength", 50)
Execute("castyle")
SetVariable("speed", 150)
Execute("castyle")
Execute("rastyle")
Execute("castyle")


The output was, as expected:

You are now using kar at 100% strength and 100% speed.
You are now using kick at 100% strength and 100% speed.
You are now using kick at 50% strength and 100% speed.
You are now using kick at 50% strength and 150% speed.
Style has been reset to defaults.
You are now using kar at 100% strength and 100% speed.



However, when I reset everything, again, and then type astyle kick 50 150, it's as if I never typed any of it. The reason why I wish it all to be in the same alias is because it's all related to the same thing, and I don't really like having a whole bunch of aliases overloading my alias list. :P I guess I could work it into a script, but I still don't see how that'd help matters in the end. In fact, I think it might leave room for more errors. :| Thanks for the willingness to help.



Edit:
I also changed if "%5" ~= "" then to if "%5" ~= "" and "%5" ~= nil then just in case that would help, but it didn't.


Also, I discovered astyle by itself will send astyle kar 100 100. Out of curiosity, I messed with the variable values, and astyle acted appropriately.
Amended on Thu 01 Sep 2011 06:30 PM by BrainFRZ
Australia Forum Administrator #6
Can you explain the problem again? I'm confused about your alias - you seem to be using Lua variables and MUSHclient variables everywhere (together). How about making up your mind which one you really want?

Lua variables (eg. foo = 42) are easier to use, but only last for this session.

MUSHclient variables (eg. SetVariable ("foo", 42) ) persist in the world file (if you save it).

Quote:

However, when I reset everything, again, ...


Can you describe what you mean by "reset everything"?
USA #7
My problem seems to be with my implementation of the regex. For some reason, the alias doesn't seem to accept any styles, strengths or speeds.

As for using both Lua and MUSHclient variables, I was just using the MUSHclient variables for storage, and working with the Lua variables inside the alias. (It was a silly over-engineering idea of mine. I'm changing it, and will update the original post's code to reflect the changes.)

As far as the last part... By "reset all the variables", I mean when I type rastyle. This resets all the variables back to the defaults.


Also, if it helps any to understand better what I'm trying to do, I've quoted the help file for the command below. I probably should've done that in the first post, heh. I really want to apologize for constantly changing up the script, but I'm just trying to solve the problem. I also want to thank everyone who's been trying to help me.

Quote:
This command will automatically pick a style based on your selection.
Syntax: astyle <family> <strength> <speed>

family is kungfu, karate, kickbox, or aikido

strength and speed are your estimations of your enemies statistics.
for instance astyle kungfu offensive 200 50 would find a kung fu
offensive stance best used against an opponent twice as strong and half
as fast as you.

You can also omit some arguments, if you don't put in a family it will
find a style in any family and if you don't put in targets it will assume them
to be 100 100.

Karate will Negate any Aikido styles
Aikido will Negate any Kick Boxing styles
Kick Boxint will Negate any Kung Fu styles
Kung Fu will Negate any Karate styles

You can also use astyle detect <strength/speed/both> for an estimate of your
opponents statistics, but be warned, if their martial arts skill is much
higher than yours, your estimation may be thrown off.
Amended on Sat 03 Sep 2011 10:13 PM by BrainFRZ
USA #8
I chopped up the alias into the following plugin:

<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE muclient>
<!-- Saved on Sunday, September 04, 2011, 12:03 PM -->
<!-- MuClient version 4.59 -->

<!-- Plugin "Autostyle" created by BrainFRZ -->
<muclient>
<plugin
   name="Autostyle"
   author="BrainFRZ"
   id="388f9018448abf52a84bbf03"
   language="Lua"
   save_state="y"
   date_written="2011-09-04 12:02:22"
   requires="3.81"
   version="1.00"
   >
</plugin>


<!--  Get our standard constants -->
<include name="constants.lua"/>

<!--  Aliases  -->
<aliases>
  <!-- detect opponent's style -->
  <alias
   name="detectstyle"
   match="^asd(?:e|et|ete|etec|etect)?$"
   enabled="y"
   group="combat"
   regexp="y"
   sequence="100"
  >
  <send>astyle detect strength
astyle detect speed</send>
  </alias>

  <!-- display your current style settings -->
  <alias
   name="currentstyle"
   match="^cas(t|ty|tyl|tyle)?$"
   enabled="y"
   group="combat"
   regexp="y"
   send_to="12"
   sequence="100"
  >
  <send>assert_style()

Note("You are now using " .. GetVariable("style") .. " at " .. GetVariable("strength") .. "% strength and " .. GetVariable("speed") .. "% speed.")
  </send>
  </alias>

  <!-- reset your style to default -->
  <alias
   name="resetstyle"
   match="^ras(t|ty|tyl|tyle)?$"
   enabled="y"
   group="combat"
   regexp="y"
   send_to="12"
   sequence="100"
  >
  <send>SetVariable("style", "kar")
SetVariable("strength", 100)
SetVariable("speed", 100)
Note("Style has been reset to defaults.")
  </send>
  </alias>


  <!-- change your style. if the speed and strength aren't given, use saved settings -->
  <alias
   name="astyle"
   match="^as(?:t|ty|tyl|tyle)(?: )?([kar|kick|aik|kung])?(?: )?([0-9]{1,3})?(?: )?({0-9]{1,3})?$"
   enabled="y"
   group="combat"
   regexp="y"
   send_to="12"
   sequence="100"
  >
  <send>assert_style()

if "%1" == "" then -- use default style if none is provided
  SetVariable("style", "kar")
elseif "%1" ~= GetVariable("style") then -- update style
  SetVariable("style", "%1")
end -- if

if "%3" ~= "" or "%3" ~= nil then -- require both str and spd to be used to change them
  SetVariable("strength", "%2")
  SetVariable("speed", "%3")
end -- if

Execute("castyle")
Send("astyle " .. GetVariable("style") .. " " .. GetVariable("strength") .. " " .. GetVariable("spd"))
  </send>
  </alias>
</aliases>


<script>
<![CDATA[
-- This function is to assert all the variables haven't somehow been corrupted between uses.
function assert_style()
  if (GetVariable("style") == nil) or (GetVariable("style") ~= "kar" and GetVariable("style") ~= "kick" and
                                       GetVariable("style") ~= "aik" and GetVariable("style") ~= "kung")
  then
    SetVariable("style", "kar")
  end -- if

  if GetVariable("strength") == nil or (tonumber(GetVariable("strength")) < 1 or
                                        tonumber(GetVariable("strength")) > 400)
  then
    SetVariable("strength", 100)
  end -- if

  if GetVariable("speed") == nil or (tonumber(GetVariable("speed")) < 1 or
                                     tonumber(GetVariable("speed")) > 400)
  then
    SetVariable("speed", 100)
  end -- if
end -- function assert_style
]]>
</script>

</muclient>



However, I'm now getting an error whenever I save the world:
Quote:
Unable to create the plugin save state file: C:\Users\Terry\AppData\Roaming\Mirosoft\Windows\Star Menu\Programs\MUSHclient\state\b7362388671358c60e83f0a7-388f9018448abf52a84bf03-state.xml
I searched for it it in the forums, and moved the plugins directory to My Documents and updated the Global Preferences. I also turned off UAC. It just seems like everything I do with this thing, it just makes things worse. :P
Amended on Mon 05 Sep 2011 05:48 PM by BrainFRZ
Australia Forum Administrator #9
Is this Windows 7?

Try just installing MUSHclient into My Documents and not anywhere near Program Files.

You can probably simplify this sort of stuff:


if GetVariable("strength") == nil or (tonumber(GetVariable("strength")) < 1 or
                                        tonumber(GetVariable("strength")) > 400)
  then
    SetVariable("strength", 100)
  end -- if


to:


local str = tonumber (GetVariable("strength"))

if not str or (str < 1 or str > 400) then
    SetVariable("strength", 100)
  end -- if



Australia Forum Administrator #10
BrainFRZ said:

My problem seems to be with my implementation of the regex. For some reason, the alias doesn't seem to accept any styles, strengths or speeds.


Did you resolve the regex issue?
USA #11
I implemented your suggestion, but the plugin overall still won't work. The only thing it's useful for is the asd alias. Did I make a mistake in the script itself that's making it so I can't save the plugin state? Would that cause it not to work?