ZLib -- Zarqon's useful function library

I am also anal about nouns. I try to always refer to mafia's per-character variables as "properties" or "preferences" (they are synonymous in practice), and ZLib's variables as "script settings."
Thank you. Knowing that distinction will help future understanding.

In the case of this function, it sets (and reads) a mafia daily property. These are accessible (and in this caes, created) via CLI's "set"/"get" and ASH's set_property()/get_property(), and live in your settings directory, as opposed to ZLib script settings, which are created by ZLib-empowered ASH scripts at runtime, and live in your data directory in your vars_myname.txt file.
This pretty much clarifies my confusion. For the reader of the future the wiki pages of Kolmafia Properties and a List thereof maybe helpful. Although the list is somewhat dated (based upon r7582 when the code is up to at least r8223) the use of an underscore to flag something that is "daily" can clearly be inferred.

If in doubt, things are established enough now that you can simply look in the appropriate place and follow the apparent convention.
I also admit that I am asking questions rather than just looking at code and files. I can do that but as long as I am not annoying people it is easier to ask than engage in software archaeology.
:D

Thank you.
 
Hah! I note that my poor SmashLib is a poor example, from a property-naming-standards point of view. I suppose my prop string ought to have been smashLib!
 
Hah! I note that my poor SmashLib is a poor example, from a property-naming-standards point of view. I suppose my prop string ought to have been smashLib!

I wouldn't worry about it since the actual property name has two underscores, i.e. _version_smashlib or _version_smashLib and that is zlib's doing ;)
 
ZLib 17 Updates!

The major update I'm excited about here is a little change to excise(). You can now specify empty strings for start and end, which excise() interprets as the start or end of the string. This allows excise() to handily replace even more of those pesky index_of() situations.

PHP:
string s = "This string is about get the heck parsed out of it."

excise(s,""," is");           // returns "This string"
excise(s,"get "," parsed");   // returns "the heck" as before
excise(s,"parsed ","");       // returns "out of it."
excise(s,"","");  // returns "This string is about get the heck parsed out of it." -- but why?
excise(s,"This","nonsense");  // returns "" as before

A couple other updates:

  • The last release's auto-MCD tweaks introduced a small bug for fighting monsters with unknown ML -- a misplaced parenthese meant that it evaluated safemox as unknown_ml + 7 (as a string). Meaning that if your unknown_ml was 170, it thought the safemox was 1707. Oops. In case you were wondering why your MCD was always getting set to 0 despite being able to handle monsters at your unknown_ml, that's why. Fixed now.
  • Sending only meat (no items) using send_gift() was broken. Not anymore.
 
Nifty, new version of ZLib, with error-fixing!

By the way:
Code:
excise(s,"","");  // returns "This string is about get the heck parsed out of it." -- but why?
Is this a question (i.e. are you not understanding why this is the case)? It seems rather obvious, looking at the code of excise().

(Also, your test string is quite possibly missing a "to"?)
 
Haha, no it was "why?" as in "why would you want to do that?" It's the same as

Code:
stringvar = substring(stringvar,0,length(stringvar));

which is pretty obviously useless.
 
Oh, that would make much more sense (you even had documentation explaining this!). Yeah, it doesn't make much sense to excise a string from itself, and odds are that this probably wouldn't happen in the first place under normal circumstances.
 
I don't know if you accept submissions for zlib, but here's a little function to remove spaces at the beginning and end of a string (usefull to parse a comma-separated list of player names while letting the user use as many spaces as he wants before or after the commas).
Code:
string removeSpaces( string source ) {
	while ( index_of(source , " ") == 0 ) source = substring(source, 1) ;
	while ( last_index_of(source , " ") == length(source)-1 ) source = substring(source, 0, length(source)-1) ;
	return source;
}

EDIT: see better solutions further down
 
Last edited:
This is the version I have in my StashBot script.

PHP:
string trim(string s) {
   while (index_of(s," ") == 0) s = substring(s,1);
   if (length(s) > 0)
    while (last_index_of(s," ") == length(s)-1) s = substring(s,0,length(s)-2);
   return s;
}

Who thinks trim() in ZLib is a good idea? Or, put another way, who would use it?
 
Or, you could do something like this, with rather less string allocation and looping and such.

PHP:
string trim( string s )
{
    matcher m = create_matcher( "^[\\s]*(.*?)[\\s]*$", s );
    if ( m.find() )
        return m.group( 1 );
    return s;
}
 
Just wanted to say that you never encounter Dr. Awkward or the Ancient Protector Spirit, unless you intend to encounter them. Seems silly to consider them as part of the zone's safe moxie consideration. Similarly for Cyrpt sub-bosses if they aren't being automatically fought when their choice adventure is encountered.

PHP:
int get_safemox(location wear) {
   int high = 0;
   foreach i,m in get_monsters(wear)
      switch(m) {
	  case $monster[Dr. Awkward]:
	  case $monster[Ancient Protector Spirit]:
         break;
	  case $monster[Conjoined Zmombie]:
         if(get_property("choiceAdventure154") == "1")
            high = max(monster_attack(m),high);
         break;
	  case $monster[Giant Skeelton]:
         if(get_property("choiceAdventure156") == "1")
            high = max(monster_attack(m),high);
         break;
	  case $monster[Gargantulihc]:
         if(get_property("choiceAdventure158") == "1")
            high = max(monster_attack(m),high);
         break;
	  case $monster[Huge Ghuol]:
         if(get_property("choiceAdventure160") == "1")
            high = max(monster_attack(m),high);
         break;
	  default:
         high = max(monster_attack(m),high);
	  }
   if (high == 0 || high == monster_level_adjustment()) return 0;
   return high + 7 - current_mcd();
}

What'cha think zarqon?
 
Funny -- when I'm on my period, it's like I don't even care about my regexphobia. I was even hoping someone would post that. I seriously just don't give a rat's appendix right now. We'll see how I feel about it tomorrow though.

EDIT: Bale, didn't see your post. Like it a lot. Should also include Felonia, no?
 
Last edited:
zarqon, I suspect you completely missed the post I made a minute before you. (Sometimes it is bad being a sneaky ninja.) Please go back and read it.
 
EDIT: Bale, didn't see your post. Like it a lot. Should also include Felonia, no?

Definitely. I didn't think of her, but the same logic as for a Cyrpt sub-boss would be good.

I'm always being troubled by having to turn off automcd in those zones. Finally I said, the heck with it and fixed the problem. Now I might write some code for BBB that controls automatic fighting of Cyrpt sub-bosses depending on ability to hit them. (I'd base it on to-hit since character might be using a melee weapon.)
 
@zarqon, It turns out that Felonia is the same ML as other monsters in her zone. She's actually got a lower safe moxie than the Spooky gravy fairy ninja. There's no reason to make her an exception to the zone.

@heeheehee, Good point about Hobo bosses and the clownlord.

The function now looks like this:
PHP:
int get_safemox(location wear) {
   int high = 0;
   foreach i,m in get_monsters(wear)
      switch(m) {
	  case $monster[Dr. Awkward]:
	  case $monster[Ancient Protector Spirit]:
	  case $monster[Ol' Scratch]:
	  case $monster[Frosty]:
	  case $monster[Oscus]:
	  case $monster[Zombo]:
	  case $monster[Chester]:
	  case $monster[Hodgman, The Hoboverlord]:
         break;
	  case $monster[The Clownlord Beelzebozo]:
         if (get_property("choiceAdventure151") == "1" && get_property("choiceAdventure152") == "1")
            high = max(monster_attack(m),high);
         break;
	  case $monster[Conjoined Zmombie]:
         if (get_property("choiceAdventure154") == "1")
            high = max(monster_attack(m),high);
         break;
	  case $monster[Giant Skeelton]:
         if (get_property("choiceAdventure156") == "1")
            high = max(monster_attack(m),high);
         break;
	  case $monster[Gargantulihc]:
         if (get_property("choiceAdventure158") == "1")
            high = max(monster_attack(m),high);
         break;
	  case $monster[Huge Ghuol]:
         if (get_property("choiceAdventure160") == "1")
            high = max(monster_attack(m),high);
         break;
	  default:
         high = max(monster_attack(m),high);
	  }
   if (high == 0 || high == monster_level_adjustment()) return 0;
   return high + 7 - current_mcd();
}
 
Back
Top