perl: using if with 4 variables.

Posted by Nid on Thu 24 Aug 2006 06:39 PM — 7 posts, 25,960 views.

Sweden #0
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.
Sweden #1
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);
}
Australia Forum Administrator #2
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. :)

Sweden #3
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 ;))
Amended on Thu 24 Aug 2006 10:38 PM by Nid
Sweden #4
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!
Australia Forum Administrator #5
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


Sweden #6
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.