Difference between pages "Talk:Tips, Tricks and Workarounds" and "Zlib"

From Kolmafia
(Difference between pages)
Jump to navigation Jump to search
imported>PhilmASTErpLus
(→‎Quirks & Hacks from PhilmASTErpLus: How about moving the information about print() to the function's page?)
 
imported>StDoodle
m
 
Line 1: Line 1:
__TOC__
+
{{TOCright}}
 +
{{DISPLAYTITLE:ZLib (zlib.ash)}}
  
== Discussion Recap ==
+
Behold, a single function library containing most of zarqon's most useful functions, which will be used by most of his scripts. He grew tired of tweaking a function that was in multiple scripts and making sure he had changed them all. ''It would be nice'', he thought in his customary italics, ''to have it in just one place''.
  
=== Discussion from [[Talk:Storage amount]] ===
+
In choosing which functions to include, he have tried to select only those functions which are a) super useful, or 2) needed by another of the included functions. It's a bit less efficient in terms of individual scripts, but in terms of repeated code across a family of scripts it is far simpler.
  
Can anyone else verify, before I edit it in, that this function doesn't work on free pulls? --[[User:StDoodle|StDoodle (#1059825)]] 11:45, 21 September 2010 (UTC)
+
{{hidelink|excise}}
 +
{{Function|
 +
name=excise|
 +
return_type=string|
 +
parameter1={{Param|string|source}}|
 +
parameter2={{Param|string|start}}|
 +
parameter3={{Param|string|end}}|
 +
p1desc=The original {{pspan|source}} string|
 +
p2desc={{pspan|start}} after this string|
 +
p3desc={{pspan|end}} before this string|
 +
}}
 +
This function returns a portion of the {{pspan|source}} string, from after the first occurrence of {{pspan|start}} to just before the first occurrence of {{pspan|end}}. If either {{pspan|start}} or {{pspan|end}} are missing, it will return an empty string. You can also supply either {{pspan|start}} or {{pspan|end}} as blank strings to specify the actual start or end of the {{pspan|source}} string.
  
Verified. I believe that this was reported as a bug once. (Back when we had that awesome bug tracker.) However this behavior was clarified to be working as intended and hence, not a bug. The fact that an itemd doesn't require a pulls means that it is supposed to be treated differently. Use {{f|available_amount}} minus {{f|item_amount}} to verify presence of free pulls in Hangks. Then get them into inventory with {{f|retrieve_item}} instead of with {{f|take_storage}}. You're right that info should probably be on all relevant pages. --[[User:Bale|Bale]] 05:17, 22 September 2010 (UTC)
+
{{hidelink|normalized}}
 +
{{Function|
 +
name=normalized|
 +
return_type=string|
 +
parameter1={{Param|string|mixvar}}|
 +
parameter2={{Param|string|type}}|
 +
p1desc={{pspan|mixvar}} is the string to normalize|
 +
p2desc={{pspan|type}} is the datatype to normalize to|
 +
}}
 +
Returns {{pspan|mixvar}}, normalized to the specified mafia {{pspan|type}}, which can be any primitive type or ASH datatype constant. For example, normalized("badger", "familiar") would return "Astral Badger".
  
Perhaps we should start a special "caveats" page, and link to subheadings on it, for such info; it seems a bit much may be required to explain the situation to include it on every page. It would also be a good place to put info on things like Between Battle Scripts and adventure count in automation, etc. Thoughts? --[[User:StDoodle|StDoodle (#1059825)]] 15:10, 22 September 2010 (UTC)
+
{{hidelink|rnum}
 +
{{Function|
 +
name=rnum|
 +
return_type=string|
 +
parameter1={{Param|int|n}}|
 +
}}
 +
{{Function|
 +
name=rnum|
 +
return_type=string|
 +
parameter1={{Param|float|n}}|
 +
}}
 +
{{Function|
 +
name=rnum|
 +
return_type=string|
 +
parameter1={{Param|float|n}}|
 +
parameter2={{Param|int|place}}|
 +
p1desc={{pspan|n}} is a number|
 +
p2desc={{pspan|place}} is the number of decimal places to round to|
 +
}}
 +
Returns your number {{pspan|n}} as a human-readable string. For ints, this means it adds commas where appropriate. For floats, it also rounds to the nearest {{pspan|place}} after the decimal. Default {{pspan|place}} for the float-only version is 2, although it may display fewer digits if they are 0's. Examples: rnum(12580) => "12,580", rnum(3.14152964,3) => "3.142", rnum(4.00008) => "4", rnum(123456789.87654321) => "123,456,789.88". Recommended as a substitute for to_string().
  
I favor a caveats page. Or perhaps we could call it Tricks & Tips. We should add [[User:PhilmASTErpLus#ASH_Quirks_and_Hacks|THIS]] there also. --[[User:Bale|Bale]] 22:22, 22 September 2010 (UTC)
+
void set_avg(float toadd, string whichprop)
 +
Useful for adding spading to scripts. Adds one more statistic to an average value being stored in a property. For example, calling this script three times with the values 2, 4, and 6 for toadd would result in the property containing "4.0:3", with 4.0 being the average of the three numbers added and 3 being the amount of numbers averaged.
  
Tips, Tricks and Workarounds* sounds good to me. I agree that those items philmaterplus points out should go there. Also, we should clean up the FAQ to only include one example of non-aborting booleans, and move the full list to this new page, I think (with a link of course). Perhaps this would even be a good page to put a dedicated file link to the syntax highlighting scheme for Notepadd++ (I favor having it on the wiki, so when it's updated with new ash functions, it's... you know, updated). I'll come up with other ideas, too, and leave them on the talk page when it's there. --[[User:StDoodle|StDoodle (#1059825)]] 17:04, 23 September 2010 (UTC)
+
float get_avg(string whichprop)
 +
Returns an average value set by set_avg().
  
*Actually it doesn't sound perfect, which is why I'm not making it yet... hrmph.
+
float eval(string expr, float[string] values)
 +
By Jason Harper. Evaluates expr as a math expression, and allowing you to substitute values for variables, as described in much greater detail here. A brief version of this documentation is also included in ZLib.
  
Moving Discussion to [[Talk: Tips, Tricks and Workarounds]]. Since there may be a bit of discussion about the page before it gets created properly. We can always move the talk page if we change our minds about what it should be called. --[[User:Bale|Bale]] 21:18, 23 September 2010 (UTC)
+
boolean vprint(string message, string color, int level)
 +
boolean vprint(string message, int level)
 +
Wrapper for print() and abort(), prints message depending on vars["verbosity"] setting. It has a boolean return value so it can replace { print(message); return t/f; }
 +
if level == 0: abort with message
 +
if level > 0: prints message if verbosity >= level, returns true
 +
if level < 0: prints message if verbosity >= abs(level), returns false
 +
More detailed explanation here.
  
 +
boolean vprint_html(string message, int level)
 +
Same as above, but wraps print_html().
  
=== Quirks & Hacks from [[User:PhilmASTErpLus|PhilmASTErpLus]] ===
+
int abs(int n)
 +
float abs(float n)
 +
Returns the absolute value of n.
  
*{{f|print}} displays a blank line when given a string starting with a slash (<code>/</code>). To prevent this, the text color must be explicitly given. Easy to bypass, but weird.
+
float minmax(float a, float min, float max)
*"Anonymous" closures: ASH does not seem to accept anonymous closures by default.
+
Returns a, but no less than min and no more than max.
{{CodeSample|code=<syntaxhighlight>
 
int var1;
 
/* Do stuff */
 
{
 
    int var1;
 
    call_some_function();
 
    /* Do other stuff */
 
}
 
</syntaxhighlight>}}
 
However, you can use a workaround with a if-clause.
 
{{CodeSample|code=<syntaxhighlight>
 
int var1;
 
/* Do stuff */
 
if (true)
 
{
 
    int var1;
 
    /* Do other stuff */
 
}
 
</syntaxhighlight>}}
 
As for where stuff like this could be used...I'm using it for initializing ASH scripts without creating any unnecessary functions or variables in the "namespace".
 
  
:Great work so far, guys. One nitpick, though: I think the tip about using print() should be put in {{print|the appropriate function page}}, instead. I'll add it there ASAP. --[[User:PhilmASTErpLus|PhilmASTErpLus]] 12:18, 26 September 2010 (UTC)
+
string check_version(string soft, string prop, string thisver, int thread)
 +
Server-friendly once-daily version-checking. (Also hyphen-friendly.) If the user hasn't checked yet today, visits the specified thread in this forum to find the current version of your script. The thread must include "<b>soft version</b>" for the version info to be successfully parsed. Optionally, you may include "[requires revision XXXX]" in the post if you want to indicate a required minimum revision of mafia. If a new version is available, alerts the user in large text and provides an update link, then waits for a sufficiently annoying interval in case the user wishes to abort operation to go update the script. The return value is a blank string unless an update is found, in which case it is a <div id='versioninfo'> containing the update message. This allows this function to work equally well for relay scripts. The current version is stored to a daily property, standardized to "_version_"+prop. Example:
 +
Code:
  
== New Discussion ==
+
check_version("Hardcore Checklist","checklist","1.2.7",1045);
  
Ugh, sorry. Can't think of the best way to fix that ATM, it was half a joke. (Note to self; don't edit wiki drunk.) --[[User:StDoodle|StDoodle (#1059825)]] 06:32, 24 September 2010 (UTC)
+
boolean load_current_map(string fname, aggregate dest)
 +
Allows scripts to automatically update data files. Loads a map named fname.txt, either from disk or from the Map Manager if an update is available. (Yes, this means you shouldn't include ".txt" in the parameter.) The first time you try to load a given map on a given day, it checks if your version is current. If so, it merely loads it from disk. If not, it overwrites your local map with the map stored on the Map Manager.
 +
 
 +
int setvar(string varname, mixed dfault)
 +
Allows you to define a per-character script setting to be used across all scripts that use ZLib. The first time the script is run, the variable will be set to dfault. To reference this setting, use vars[varname] in any script that includes ZLib, keeping in mind that this will always be a string. Detailed information posted here.
 +
 
 +
int have_item(string tolookup)
 +
A residual function, used by the following and probably in several other scripts. Returns the amount of an item you have both in your inventory and equipped.
 +
 
 +
float has_goal(item whatsit)
 +
Returns the chance that an item is or results in a goal. If is_goal(item) == true or it's a bounty item, returns 1.0. Otherwise, returns the chance that you would get a goal from using the item. For example, with a goal of black pepper, has_goal($item[black picnic basket]) would return 0.58. Many thanks to aqualectrix for creating and sharing the container items results map.
 +
 
 +
float has_goal(monster m)
 +
Making use of the above function, returns the percent chance that encountering a given monster will result in a goal, taking into account +items, pickpocket availability (and +pickpocket), and Torso. For instance, with no +item and black pepper as a goal, has_goal($monster[black widow]) would return 0.087 (0.58 basket contains pepper * 0.15 basket drop rate). Also note that it will add multiple goals together, so with white pixels as a goal, a Blooper would return 2.1.
 +
 
 +
float has_goal(location l)
 +
Returns the chance that adventuring at a given location will yield a goal. For our black pepper example, has_goal($location[black forest]) would return 0.0174 (0.2 black widow appearance rate * 0.087 chance that a widow has black pepper). Presently this accounts for combat frequency modifiers but not Olfaction, and it will be off for areas with noncombats that grant goals, because it assumes that all noncombats do not yield items.
 +
 
 +
boolean obtain(int n, string cond, location locale)
 +
Attempts to get n (-existing) of cond, either by purchasing (if you have the mafia preference set), pulling from Hangk's, or adventuring at locale. It also works with choiceadvs.
 +
 
 +
boolean use_upto(int n, item doodad, boolean purchase)
 +
Gets (if purchase is true) and uses n doodads if possible. Otherwise, uses as many as you have up to n.
 +
 
 +
boolean resist(element req, boolean reallydoit)
 +
Returns whether you are able to resist a given element, or if reallydoit is true, attempts to actually achieve that resistance (casting buffs, changing gear, or equipping your Exotic Parrot), and returns its success.
 +
 
 +
int my_defstat()
 +
Returns the value of your buffed defense stat, taking into account Hero of the Half-Shell.
 +
 
 +
int get_safemox(location wear)
 +
Using mafia's location/monster data, returns the safe moxie of a given zone.
 +
 
 +
boolean auto_mcd(int safemox)
 +
boolean auto_mcd(monster mob)
 +
boolean auto_mcd(location place)
 +
If your automcd setting is true, automatically adjusts your mind-control device for maximum stat gains based on safemox (or the safe moxie for place or mob) and your threshold setting. Does not adjust for MCD-sensitive areas (certain bosses, Slime Tube), or areas with no known combats.
 +
 
 +
familiar best_fam(string ftype)
 +
Returns your heaviest familiar of a given type (currently possible: items, meat, produce, stat, delevel). If your is_100_run setting is anything other than $familiar[none], returns that familiar (so you don't have to make the check in your script).
 +
 
 +
boolean send_gift(string to, string message, int meat, int[item] goodies, string insidenote)
 +
boolean send_gift(string to, string message, int meat, int[item] goodies)
 +
Self-explanatory, really. Sends a gift to a player, able to split large amounts of items, useful return value.
 +
 
 +
boolean kmail(string to, string message, int meat, int[item] goodies, string insidenote)
 +
boolean kmail(string to, string message, int meat, int[item] goodies)
 +
boolean kmail(string to, string message, int meat)
 +
Sends a kmail to a player, returning true if the kmail is successfully sent. Handles splitting the message into multiple messages if the number of item types is too large. Returns the result of send_gift() if the intended recipient is unable to receive the message due to being in HC or somesuch. Note that you can also specify the message to be used inside gifts in that case. Use "\n" to specify a new line in the message.
 +
 
 +
How to use it:
 +
To include this library in your script, simply add the following towards the top of your script:
 +
Code:
 +
 
 +
import <zlib.ash>
 +
 
 +
Then all these functions will be available in your script. Have fun!
 +
 
 +
<h2>More Information</h2><p>See the thread for ZLib on the mafia forum [http://kolmafia.us/showthread.php?2072 here].</p>

Revision as of 23:56, 28 November 2010


Behold, a single function library containing most of zarqon's most useful functions, which will be used by most of his scripts. He grew tired of tweaking a function that was in multiple scripts and making sure he had changed them all. It would be nice, he thought in his customary italics, to have it in just one place.

In choosing which functions to include, he have tried to select only those functions which are a) super useful, or 2) needed by another of the included functions. It's a bit less efficient in terms of individual scripts, but in terms of repeated code across a family of scripts it is far simpler.

Template:Hidelink

string excise(string source ,string start ,string end )

  • The original source string
  • start after this string
  • end before this string

This function returns a portion of the source string, from after the first occurrence of start to just before the first occurrence of end. If either start or end are missing, it will return an empty string. You can also supply either start or end as blank strings to specify the actual start or end of the source string.

Template:Hidelink

string normalized(string mixvar ,string type )

  • mixvar is the string to normalize
  • type is the datatype to normalize to

Returns mixvar, normalized to the specified mafia type, which can be any primitive type or ASH datatype constant. For example, normalized("badger", "familiar") would return "Astral Badger".

{{hidelink|rnum}

string rnum(int n )

string rnum(float n )

string rnum(float n ,int place )

  • n is a number
  • place is the number of decimal places to round to

Returns your number n as a human-readable string. For ints, this means it adds commas where appropriate. For floats, it also rounds to the nearest place after the decimal. Default place for the float-only version is 2, although it may display fewer digits if they are 0's. Examples: rnum(12580) => "12,580", rnum(3.14152964,3) => "3.142", rnum(4.00008) => "4", rnum(123456789.87654321) => "123,456,789.88". Recommended as a substitute for to_string().

void set_avg(float toadd, string whichprop) Useful for adding spading to scripts. Adds one more statistic to an average value being stored in a property. For example, calling this script three times with the values 2, 4, and 6 for toadd would result in the property containing "4.0:3", with 4.0 being the average of the three numbers added and 3 being the amount of numbers averaged.

float get_avg(string whichprop) Returns an average value set by set_avg().

float eval(string expr, float[string] values) By Jason Harper. Evaluates expr as a math expression, and allowing you to substitute values for variables, as described in much greater detail here. A brief version of this documentation is also included in ZLib.

boolean vprint(string message, string color, int level) boolean vprint(string message, int level) Wrapper for print() and abort(), prints message depending on vars["verbosity"] setting. It has a boolean return value so it can replace { print(message); return t/f; } if level == 0: abort with message if level > 0: prints message if verbosity >= level, returns true if level < 0: prints message if verbosity >= abs(level), returns false More detailed explanation here.

boolean vprint_html(string message, int level) Same as above, but wraps print_html().

int abs(int n) float abs(float n) Returns the absolute value of n.

float minmax(float a, float min, float max) Returns a, but no less than min and no more than max.

string check_version(string soft, string prop, string thisver, int thread)

Server-friendly once-daily version-checking. (Also hyphen-friendly.) If the user hasn't checked yet today, visits the specified thread in this forum to find the current version of your script. The thread must include "soft version" for the version info to be successfully parsed. Optionally, you may include "[requires revision XXXX]" in the post if you want to indicate a required minimum revision of mafia. If a new version is available, alerts the user in large text and provides an update link, then waits for a sufficiently annoying interval in case the user wishes to abort operation to go update the script. The return value is a blank string unless an update is found, in which case it is a

containing the update message. This allows this function to work equally well for relay scripts. The current version is stored to a daily property, standardized to "_version_"+prop. Example:

Code:

check_version("Hardcore Checklist","checklist","1.2.7",1045);

boolean load_current_map(string fname, aggregate dest) Allows scripts to automatically update data files. Loads a map named fname.txt, either from disk or from the Map Manager if an update is available. (Yes, this means you shouldn't include ".txt" in the parameter.) The first time you try to load a given map on a given day, it checks if your version is current. If so, it merely loads it from disk. If not, it overwrites your local map with the map stored on the Map Manager.

int setvar(string varname, mixed dfault) Allows you to define a per-character script setting to be used across all scripts that use ZLib. The first time the script is run, the variable will be set to dfault. To reference this setting, use vars[varname] in any script that includes ZLib, keeping in mind that this will always be a string. Detailed information posted here.

int have_item(string tolookup) A residual function, used by the following and probably in several other scripts. Returns the amount of an item you have both in your inventory and equipped.

float has_goal(item whatsit) Returns the chance that an item is or results in a goal. If is_goal(item) == true or it's a bounty item, returns 1.0. Otherwise, returns the chance that you would get a goal from using the item. For example, with a goal of black pepper, has_goal($item[black picnic basket]) would return 0.58. Many thanks to aqualectrix for creating and sharing the container items results map.

float has_goal(monster m) Making use of the above function, returns the percent chance that encountering a given monster will result in a goal, taking into account +items, pickpocket availability (and +pickpocket), and Torso. For instance, with no +item and black pepper as a goal, has_goal($monster[black widow]) would return 0.087 (0.58 basket contains pepper * 0.15 basket drop rate). Also note that it will add multiple goals together, so with white pixels as a goal, a Blooper would return 2.1.

float has_goal(location l) Returns the chance that adventuring at a given location will yield a goal. For our black pepper example, has_goal($location[black forest]) would return 0.0174 (0.2 black widow appearance rate * 0.087 chance that a widow has black pepper). Presently this accounts for combat frequency modifiers but not Olfaction, and it will be off for areas with noncombats that grant goals, because it assumes that all noncombats do not yield items.

boolean obtain(int n, string cond, location locale) Attempts to get n (-existing) of cond, either by purchasing (if you have the mafia preference set), pulling from Hangk's, or adventuring at locale. It also works with choiceadvs.

boolean use_upto(int n, item doodad, boolean purchase) Gets (if purchase is true) and uses n doodads if possible. Otherwise, uses as many as you have up to n.

boolean resist(element req, boolean reallydoit) Returns whether you are able to resist a given element, or if reallydoit is true, attempts to actually achieve that resistance (casting buffs, changing gear, or equipping your Exotic Parrot), and returns its success.

int my_defstat() Returns the value of your buffed defense stat, taking into account Hero of the Half-Shell.

int get_safemox(location wear) Using mafia's location/monster data, returns the safe moxie of a given zone.

boolean auto_mcd(int safemox) boolean auto_mcd(monster mob) boolean auto_mcd(location place) If your automcd setting is true, automatically adjusts your mind-control device for maximum stat gains based on safemox (or the safe moxie for place or mob) and your threshold setting. Does not adjust for MCD-sensitive areas (certain bosses, Slime Tube), or areas with no known combats.

familiar best_fam(string ftype) Returns your heaviest familiar of a given type (currently possible: items, meat, produce, stat, delevel). If your is_100_run setting is anything other than $familiar[none], returns that familiar (so you don't have to make the check in your script).

boolean send_gift(string to, string message, int meat, int[item] goodies, string insidenote) boolean send_gift(string to, string message, int meat, int[item] goodies) Self-explanatory, really. Sends a gift to a player, able to split large amounts of items, useful return value.

boolean kmail(string to, string message, int meat, int[item] goodies, string insidenote) boolean kmail(string to, string message, int meat, int[item] goodies) boolean kmail(string to, string message, int meat) Sends a kmail to a player, returning true if the kmail is successfully sent. Handles splitting the message into multiple messages if the number of item types is too large. Returns the result of send_gift() if the intended recipient is unable to receive the message due to being in HC or somesuch. Note that you can also specify the message to be used inside gifts in that case. Use "\n" to specify a new line in the message.

How to use it: To include this library in your script, simply add the following towards the top of your script: Code:

import <zlib.ash>

Then all these functions will be available in your script. Have fun!

More Information

See the thread for ZLib on the mafia forum here.