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 ➜ Perlscript ➜ perl: using if with 4 variables.

perl: using if with 4 variables.

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


Posted by Nid   Sweden  (9 posts)  Bio
Date Thu 24 Aug 2006 06:39 PM (UTC)
Message
Hey.
I have 4 variables for summon health:
summ1health
summ2health
summ3health
summ4health
I also have 4 variables with summon names:
summon1, summon2, summon3 and summon4

These are set auto by my script/triggers.

Now:

I want to have an if-script to automatically send "say, $summon2, defend me" if $summ1health drops below 4.
Well, what I really want is for it to send "say, (summon with highest health), defend me, and then set a variable, say $tanker to (whichever $summon1-4) is tanking.


I probably don't make much sense, the script is really long (since I don't really know perl well yet, and made it the "simple" way. I guess I can paste the whole thing here, it's like 100 lines or something.

Well thanks in advance.
Top

Posted by Nid   Sweden  (9 posts)  Bio
Date Reply #1 on Thu 24 Aug 2006 06:43 PM (UTC)
Message
Actually, here's what I've done so far, maby it'll make more sense. What's left to do is the rotate-thingy, where the summon with most health is tanking, and switch if it gets low.

# Rotate-tanking for 1-4 summons
# The trigger:
# ^(.*?)\-\-\> ([a-zA-Z ]+):([^,]+),? ?([a-zA-Z ]+)?:?([^,]+)?,? ?([a-zA-Z ]+)?:?([^,]+)?,? ?([a-zA-Z ]+)?:?([^,]+)?(.*?)$
#Original line: --> Giant weasel:+, Wererat:+, Huge spider:+, Orc:+
#Note: summon names are random

sub rotatescript
{
my ($thename, $theoutput, $wildcards) = @_;

## Set variables and name summons ##

$world->DeleteVariable("summ1");
$world->DeleteVariable("summ2");
$world->DeleteVariable("summ3");
$world->DeleteVariable("summ4");
$world->DeleteVariable("summ1health");
$world->DeleteVariable("summ2health");
$world->DeleteVariable("summ3health");
$world->DeleteVariable("summ4health");

$world->setvariable("summ1counter", 1);
$world->setvariable("summ2counter", 1);
$world->setvariable("summ3counter", 1);
$world->setvariable("summ4counter", 1);
$world->setvariable("summcount", 0);

$ettan = $world->trim ($world->GetTriggerInfo ($thename, 103));
$tvaan = $world->trim ($world->GetTriggerInfo ($thename, 105));
$trean = $world->trim ($world->GetTriggerInfo ($thename, 107));
$fyran = $world->trim ($world->GetTriggerInfo ($thename, 109));


$summon1 = $world->trim ($world->GetTriggerInfo ($thename, 102));
$summon2 = $world->trim ($world->GetTriggerInfo ($thename, 104));
$summon3 = $world->trim ($world->GetTriggerInfo ($thename, 106));
$summon4 = $world->trim ($world->GetTriggerInfo ($thename, 108));

$world->setvariable("summon1", $summon1);
$world->setvariable("summon2", $summon2);
$world->setvariable("summon3", $summon3);
$world->setvariable("summon4", $summon4);



## Change the mob status into numbers 1-6 and variables ##

if( $ettan =~ "[+]")
{
$world->setvariable("summ1health", 6);
}
if( $ettan =~ "A")
{
$world->setvariable("summ1health", 5);
}
if( $ettan =~ "~A")
{
$world->setvariable("summ1health", 5);
}
if( $ettan =~ "B")
{
$world->setvariable("summ1health", 4);
}
if( $ettan =~ "~B")
{
$world->setvariable("summ1health", 4);
}
if( $ettan =~ "C")
{
$world->setvariable("summ1health", 3);
}
if( $ettan =~ "~C")
{
$world->setvariable("summ1health", 3);
}
if( $ettan =~ "D")
{
$world->setvariable("summ1health", 2);
}
if( $ettan =~ "~D")
{
$world->setvariable("summ1health", 2);
}
if( $ettan =~ "E")
{
$world->setvariable("summ1health", 1);
}
if( $ettan =~ "~E")
{
$world->setvariable("summ1health", 1);
}
##
if( $tvaan =~ "[+]")
{
$world->setvariable("summ2health", 6);
}
if( $tvaan =~ "A")
{
$world->setvariable("summ2health", 5);
}
if( $tvaan =~ "~A")
{
$world->setvariable("summ2health", 5);
}
if( $tvaan =~ "B")
{
$world->setvariable("summ2health", 4);
}
if( $tvaan =~ "~B")
{
$world->setvariable("summ2health", 4);
}
if( $tvaan =~ "C")
{
$world->setvariable("summ2health", 3);
}
if( $tvaan =~ "~C")
{
$world->setvariable("summ2health", 3);
}
if( $tvaan =~ "D")
{
$world->setvariable("summ2health", 2);
}
if( $tvaan =~ "~D")
{
$world->setvariable("summ2health", 2);
}
if( $tvaan =~ "E")
{
$world->setvariable("summ2health", 1);
}
if( $tvaan =~ "~E")
{
$world->setvariable("summ2health", 1);
}
##
if( $trean =~ "[+]")
{
$world->setvariable("summ3health", 6);
}
if( $trean =~ "A")
{
$world->setvariable("summ3health", 5);
}
if( $trean =~ "~A")
{
$world->setvariable("summ3health", 5);
}
if( $trean =~ "B")
{
$world->setvariable("summ3health", 4);
}
if( $trean =~ "~B")
{
$world->setvariable("summ3health", 4);
}
if( $trean =~ "C")
{
$world->setvariable("summ3health", 3);
}
if( $trean =~ "~C")
{
$world->setvariable("summ3health", 3);
}
if( $trean =~ "D")
{
$world->setvariable("summ3health", 2);
}
if( $trean =~ "~D")
{
$world->setvariable("summ3health", 2);
}
if( $trean =~ "E")
{
$world->setvariable("summ3health", 1);
}
if( $trean =~ "~E")
{
$world->setvariable("summ3health", 1);
}
##
if( $fyran =~ "[+]")
{
$world->setvariable("summ4health", 6);
}
if( $fyran =~ "A")
{
$world->setvariable("summ4health", 5);
}
if( $fyran =~ "~A")
{
$world->setvariable("summ4health", 5);
}
if( $fyran =~ "B")
{
$world->setvariable("summ4health", 4);
}
if( $fyran =~ "~B")
{
$world->setvariable("summ4health", 4);
}
if( $fyran =~ "C")
{
$world->setvariable("summ4health", 3);
}
if( $fyran =~ "~C")
{
$world->setvariable("summ4health", 3);
}
if( $fyran =~ "D")
{
$world->setvariable("summ4health", 2);
}
if( $fyran =~ "~D")
{
$world->setvariable("summ4health", 2);
}
if( $fyran =~ "E")
{
$world->setvariable("summ4health", 1);
}
if( $fyran =~ "~E")
{
$world->setvariable("summ4health", 1);
}
Top

Posted by Nick Gammon   Australia  (23,173 posts)  Bio   Forum Administrator
Date Reply #2 on Thu 24 Aug 2006 10:14 PM (UTC)
Message
That is an incredibly long script. My first reaction when I see virtually the same coded repeated four times is, that this should be a subroutine. Copy and paste is tedious and makes it hard to debug.

I'm not a Perl expert so I'll do my solution in Lua. If you want to stick to Perl, you can take the ideas from it.


health_levels = {
  ["+"] = 6,
  ["A"] = 5,
  ["B"] = 4,
  ["C"] = 3,
  ["D"] = 2,
  ["E"] = 1  }

-- look up health in table, convert to a number
function convert_health (which)

  -- get rid of tilde (~) from string
  which = string.gsub (which, "%~", "")

  return health_levels [which] or 0

end -- convert_health 

function rotatescript (thename, theoutput, wildcards)

local summon1, summ1health, 
      summon2, summ2health, 
      summon3, summ3health, 
      summon4, summ4health

  summon1 = GetTriggerInfo (thename, 102)  -- name of first summon
  summ1health = convert_health (GetTriggerInfo (thename, 103)) -- health of first summon

  summon2 = GetTriggerInfo (thename, 104)  -- name of 2nd summon
  summ2health = convert_health (GetTriggerInfo (thename, 105)) -- health of 2nd summon

  summon3 = GetTriggerInfo (thename, 106)  -- name of 3rd summon
  summ3health = convert_health (GetTriggerInfo (thename, 107)) -- health of 3rd summon

  summon4 = GetTriggerInfo (thename, 108)  -- name of 4th summon
  summ4health = convert_health (GetTriggerInfo (thename, 109)) -- health of 4th summon

local highestnumber, highestname 

  -- assume 1st summon is highest first
  highestnumber = summ1health
  highestname = summon1 

  -- see if 2nd summon has more health
  if summ2health > highestnumber then
    highestnumber = summ2health 
    highestname = summon2
  end -- 2nd higher than first

  -- see if 3rd summon has more health
  if summ3health > highestnumber then
    highestnumber = summ3health 
    highestname = summon3
  end -- 3rd higher than others

  -- see if 4th summon has more health
  if summ4health > highestnumber then
    highestnumber = summ4health 
    highestname = summon4
  end -- 4th higher than others

  -- the highest name is our tanker
  SetVariable ("tanker", highestname)
  Send ("say, " .. highestname .. ", defend me")

end -- rotatescript 



First thing is to make turning the health letter into a number into a separate function, and use a look-up table for the conversion.

Then call that function for each of the 4 summons, remembering its health.

Then we keep track of the one with highest health so far, and with a few ifs, work out which one is to be the tank. If there were more than 4 I would be doing a sort, but this isn't too bad like this.

Then we set the variable, and do the "say", and we are done. :)


- Nick Gammon

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

Posted by Nid   Sweden  (9 posts)  Bio
Date Reply #3 on Thu 24 Aug 2006 10:35 PM (UTC)

Amended on Thu 24 Aug 2006 10:38 PM (UTC) by Nid

Message
Thank you Nick!

I think I will try to redo that into perl, since I have no idea how Lua works.

You rock :)

(Oh yeah, if you or anyone have any idea on how to turn this into a perl-script, please post, it'll save me about 14 hours of agonizing pain ;))
Top

Posted by Nid   Sweden  (9 posts)  Bio
Date Reply #4 on Fri 25 Aug 2006 12:26 AM (UTC)
Message
Hey again ;)

Okay I tried out Lua some and it looks really nice.

There are however some issues with your script that I wonder if you could help me out with.

First off, there's trouble if 2 summons have the same name, for instance "large rat", or say that all 4 is named "large rat", the script will just keep saying "say large rat, defend me".

And secondly, This trigger runs every round. Is there a way to only send "say <summon>, defend me" when we get a new tank?

Thanks in advance!
Top

Posted by Nick Gammon   Australia  (23,173 posts)  Bio   Forum Administrator
Date Reply #5 on Fri 25 Aug 2006 02:34 AM (UTC)
Message
Quote:

First off, there's trouble if 2 summons have the same name, for instance "large rat", or say that all 4 is named "large rat", the script will just keep saying "say large rat, defend me".


Well, what would you type if you weren't using a script?

Quote:

And secondly, This trigger runs every round. Is there a way to only send "say <summon>, defend me" when we get a new tank?


Change the last couple of lines to see if the tanker has changed:


-- the highest name is our tanker

  if GetVariable ("tanker") ~= highestname then
    SetVariable ("tanker", highestname)
    Send ("say, " .. highestname .. ", defend me")
  end -- if tank has changed



- Nick Gammon

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

Posted by Nid   Sweden  (9 posts)  Bio
Date Reply #6 on Fri 25 Aug 2006 10:00 AM (UTC)
Message
If I were not using a script I would type:
(If say <summon1> and <summon2> have the same name:
"say, <summonname> 2, defend me"
and if 3 have the same name:
or "say, <summonname> 3, defend me" (if 3 has the most life of course) :)

--

The other part of the script worked fine, no spammy spam now, thanks Nick.
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.


25,001 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.