Need help with jscript for checking my health...

Posted by Phatj on Mon 24 Feb 2003 01:20 AM — 5 posts, 17,243 views.

#0
Im having a problem with the below script. Everytime I call it with an alias, it returns the same amount of health and mana, even after a battle when my health has changed. Can anyone help me with this problem on updating my stats? Thanks.


////////////////////////////////////////////////////
var line, total_lines;

total_lines = world.GetLinesInBufferCount ();


//Determines health!
function GetHealthandMana(){

stats = world.GetLineInfo (total_lines, 1);
actualHealth = stats.slice(0,3);
tempstring = stats.slice(6,13);
actualMana = tempstring.slice(0,3);

}


//puts everything together
function Main(){

//start by checking health and mana
GetHealthandMana();
world.note (actualHealth + " " + actualMana);
if(actualHealth != 888){
world.send (sleep);
world.DoAfter +24 (wake);
world.DoAfter +6 (stand);
}else{
world.send ("laugh");
}
}

#1
BTW- here is some output from the mud

///////////////////////////////

Meihua arrives from the north.
888h, 642m ex-
Meihua leaves to the southeast.
888h, 642m ex-
With a flick of its small whiskers, a young rat dashes out of view.
888h, 642m ex-
Zech, Dedicated of Light asks, in Xorani "What is so funny?"
888h, 642m ex-

//////////////////////////////

every newline begins with the health/mana
Australia Forum Administrator #2
This is a very roundabout way of doing it. I would use a trigger, and simply set up two wildcards. eg


<triggers>
  <trigger
   custom_colour="2"
   enabled="y"
   match="*h, *m ex-"
   name="process_health"
   script="DoHealth"
   sequence="100"
  >
  </trigger>
</triggers>


Then wildcard 1 is your health and wildcard 2 is your mana. Then extract them in a simple trigger script. There are dozens of examples on the forum here.
Russia #3
Although, Nick's suggestion wouldn't really work I'd still go with it, Phatj. In fact I already did. :P

Reason it won't work is that the '*h, *m ex-' trigger wouldn't match, since 1) that form of prompt appears with no newline; 2) the 'ex' portion changes all the time, so you need to account for that also. But that's just details, the approach itself is perfectly valid.

The only situation where you'd want to know how much health/mana you have is during combat (or bashing) and in both of those, things happen so fast that a prompt without a newline rarely ever hangs on the screen for even a second, so you can safely use a trigger to pull the stats directly from wildcards. Here's a regexp trigger that I use to account for the 'ex' portion and pull out 2 wilcards with stats:


<triggers>
  <trigger
   enabled="y"
   regexp="y"
   match="^(\d+)h\, (\d+)m (\D{0,6})-.*$"
   name="process_health"
   script="DoHealth"
   sequence="100"
  >
  </trigger>
</triggers>


That'll return 3 wildcards, but you only need the first 2 really. Accounting for the 'ex' (cexkdb - in full) is safer done with using a character class - something that'll match on any combination of characters 'cexkdb' but no more than 6 of them and in that order (ceb|exk|ed...) - but I am too lazy to figure it out, so I just use the much broader (\D{0,6}).

Russia #4
Silly me - that trigger would screw up your entire list of them in worst case scenario or just wont match in the best one. Here's the one you really need:

 
<triggers>
  <trigger
   enabled="y"
   regexp="y"
   keep_evaluating="y"
   match="^(\d+)h\, (\d+)m (\D{0,6})-.*$"
   name="process_health"
   script="DoHealth"
   sequence="1"
  >
  </trigger>
</triggers  


As you can see, it is the first trigger to be matched on any line you recieve from the MUD, but if it matches then all other triggers aren't ignored and attempts to match them continue on the same string (because of the Keep evaluating option being selected)