philmasterplus
Active member
Item Search V0.3
Warning: The latest release introduces large changes.
When you update this script, you need to download both item_search.ash and PUtils.ash. Also, you can remove item_search_keywords.txt from your (Mafia Root)\data directory, since it is no longer used.
What is this?
A script library that will help you search for multiple items by keywords and categories. As a script writer, I often found a need to find items that fell into a specific category or matched certain keywords. However, using foreach loops to check each item for every search was messy. This script is the solution.
Warning: many of the functions in this script deal with large maps, and thus can be very slow depending on your computer's performance.
This script is still in its development stages. Various features are subject to sudden changes. If you are unsure whether updating this script would cause problems with another script, do NOT download the latest version.
For Users
item_search.ash requires zlib.ash and PUtils.ash, the latter of which is included along at the moment. PUtils.ash contains various miscellaneous functions, some of which are used by item_search.ash.
How a Text Query Search Works
The keywords map contains various keywords that items can be classified into, as well as its name and initials. For a full list of available keywords returned by each classifiers, type the following command into the CLI:
The search() function handles three special characters: the vertical bar ("|"), the whitespace (" "), and the exclamation mark ("!"), interpreted in that order.
Did I tell you that brimstone will also match the Brimstone Chicken Sandwich? If you don't like it, you can add equipment or !consumable to exclude the Sandwich.
To test your query using the default classifier settings, type the following into the CLI:
For Script Authors
To use this script, enter import <item_search.ash> at the top of your script. As said above, it requires ZLib and PUtils.ash (included below). It uses no saved configuration, one or two text files loaded from zarqon's Map Manager, and several KoLmafia internal data files.
The functionality of item_search.ash is wrapped around the record item_search, which contains all the information needed to perform a search:
In nearly all cases, you will use only one instance of item_search in your script.
Classifier Functions
As of v0.2, item_search.ash supports classifier functions. These functions are used to generate keywords for each item in build_keywords(). The list of default classifier functions is stored in the boolean [string] item_search_classifiers, where the function name is stored. Should you wish to deactivate one of the classifiers (to speed up build_keywords()), you can assing a boolean value of false to the map, or just remove the particular function from the map. To view the default list of classifier functions and their availability by default, see item_search_classifiers.txt.
Note: the classifier function classify_potion_effects() is disabled by default (marked false) because searching for effect names yielded undesireable results (E.g. swim matches the shark cartilage. Guess why.). You can reactivate it by using
in your script before running create_item_search() or add().
You can also create custom classifier functions and add them to the item_search_classifiers map so as to support custom keywords. A classifier function must be of the form:
It must return a whitespace-separated list of keywords, followed by a single whitespace.
Search Functions (at last!)
The following is a list of available functions that can be used to create and perform a search. Most of the search-related functions return the item_search record, so you can chain multiple operations using "Java-like" function calls.
item_search create_item_search( boolean [item] item_list )
item_search add( item_search ims , item i , int default_price )
item_search update_prices( item_search ims , float price_age )
item_search update_prices_all( item_search ims , float price_age )
item_search search( item_search ims , string query )
item_search search_mall_price( item_search ims , int min_p , int max_p )
item_search search_autosell_price( item_search ims , int min_p , int max_p )
item_search search_custom( item_search ims , string func_name )
item_search reset( item_search ims )
If all this sounds weird and woozy, here is a sample code to show how awesome this can be:
Thanks To
Change Log
2010-07-08 v0.1: Initial release
2010-07-12 v0.2: Completely restructured how search works. Added classifier functions. Added negative search (!). Deprecated item_search_keywords.txt and added item_search_classfiers.txt. Added optional support for potion effects, adjustable via classfiers.
2010-07-14 v0.3: Slight speed improvements. Added classify_rare_items(). classify_consumables(), classify_packages(), and classify_rare_items() now require a call to activate_functionality( string functionality ) at the start of a script.
Warning: The latest release introduces large changes.
When you update this script, you need to download both item_search.ash and PUtils.ash. Also, you can remove item_search_keywords.txt from your (Mafia Root)\data directory, since it is no longer used.
What is this?
A script library that will help you search for multiple items by keywords and categories. As a script writer, I often found a need to find items that fell into a specific category or matched certain keywords. However, using foreach loops to check each item for every search was messy. This script is the solution.
Warning: many of the functions in this script deal with large maps, and thus can be very slow depending on your computer's performance.
This script is still in its development stages. Various features are subject to sudden changes. If you are unsure whether updating this script would cause problems with another script, do NOT download the latest version.
For Users
item_search.ash requires zlib.ash and PUtils.ash, the latter of which is included along at the moment. PUtils.ash contains various miscellaneous functions, some of which are used by item_search.ash.
How a Text Query Search Works
The keywords map contains various keywords that items can be classified into, as well as its name and initials. For a full list of available keywords returned by each classifiers, type the following command into the CLI:
Code:
> item_search.ash showkeywords
The search() function handles three special characters: the vertical bar ("|"), the whitespace (" "), and the exclamation mark ("!"), interpreted in that order.
- Whitespace (" "): Denotes an AND-search. For example, knob potion will return all potions with the words knob in them.
- Vertical bar ("|"): Denotes an OR-search. brimstone accessory|hat will return a list of all hats and Bad Moon rewards that are accessories. brimstone accessory|brimstone hat will return all Bad Moon rewards that are accessories or hats.
- Exclamation mark ("!"): Denotes a NOT-search. !accessory !hat brimstone will return all Bad Moon rewards that are neither accessories nor hats.
Did I tell you that brimstone will also match the Brimstone Chicken Sandwich? If you don't like it, you can add equipment or !consumable to exclude the Sandwich.
To test your query using the default classifier settings, type the following into the CLI:
Code:
> item_search.ash [text query]
For Script Authors
To use this script, enter import <item_search.ash> at the top of your script. As said above, it requires ZLib and PUtils.ash (included below). It uses no saved configuration, one or two text files loaded from zarqon's Map Manager, and several KoLmafia internal data files.
The functionality of item_search.ash is wrapped around the record item_search, which contains all the information needed to perform a search:
PHP:
record item_search
{
int [item] prices; #Mall prices are stored here.
string [item] keywords;
boolean [item] current_selection; #The last search results are stored here. By default, includes all items in the keywords map.
};
In nearly all cases, you will use only one instance of item_search in your script.
Classifier Functions
As of v0.2, item_search.ash supports classifier functions. These functions are used to generate keywords for each item in build_keywords(). The list of default classifier functions is stored in the boolean [string] item_search_classifiers, where the function name is stored. Should you wish to deactivate one of the classifiers (to speed up build_keywords()), you can assing a boolean value of false to the map, or just remove the particular function from the map. To view the default list of classifier functions and their availability by default, see item_search_classifiers.txt.
Note: the classifier function classify_potion_effects() is disabled by default (marked false) because searching for effect names yielded undesireable results (E.g. swim matches the shark cartilage. Guess why.). You can reactivate it by using
PHP:
item_search_classifiers[ "classify_potion_effects" ] = true;
You can also create custom classifier functions and add them to the item_search_classifiers map so as to support custom keywords. A classifier function must be of the form:
PHP:
string classify_something( item i );
Search Functions (at last!)
The following is a list of available functions that can be used to create and perform a search. Most of the search-related functions return the item_search record, so you can chain multiple operations using "Java-like" function calls.
item_search create_item_search( boolean [item] item_list )
A "constructor" for item_search that generates keywords for each item in item_list. You would usually call this only once at the beginning of your script.
Most of the time, you would want to use $items[] for the item_list. However, if your script deals with a specific list of items, you can use that list instead to drastically reduce search time.
Most of the time, you would want to use $items[] for the item_list. However, if your script deals with a specific list of items, you can use that list instead to drastically reduce search time.
item_search add( item_search ims , item i , int default_price )
Implemented in v0.2, this function adds a new item to the item_search record, automatically adding the keyword and using the default price. It does not add the item to the current_selection map. To do so, you will need to use reset().
item_search update_prices( item_search ims , float price_age )
Updates prices for all items in item_search.current_selection, using historical prices younger than price_age. Will not modify the original price if historical_price() or mall_price() returns 0 (item does not exist in mall).
item_search update_prices_all( item_search ims , float price_age )
Same as above, except that it updates prices for all items in the keywords map instead.
item_search search( item_search ims , string query )
The flower of item_search.ash, this function searches for all items that match the given query from current_selection, removing any that fails to match.
See "For Users" for explanation on formatting a text query.
See "For Users" for explanation on formatting a text query.
item_search search_mall_price( item_search ims , int min_p , int max_p )
Finds all items within a certain mall price range (inclusive). Untradeable items have a fixed price( usually 0 ).
Warning: does not check whether an item's price is valid. See update_prices() for manually updating mall prices.
Warning: does not check whether an item's price is valid. See update_prices() for manually updating mall prices.
item_search search_autosell_price( item_search ims , int min_p , int max_p )
Finds all items within a certain autosell price range (inclusive), using autosell_price(). Remember, autosell_price() may not work the way you'd expect.
item_search search_custom( item_search ims , string func_name )
Implemented in v0.2, this function receives a custom function and uses it to narrow the search. The passed function must be of the form:
Same as above, it does not guarantee that the passed mall price is valid.
PHP:
boolean func_name( item i , string keywords , int mall_price )
item_search reset( item_search ims )
Resets current_selection to all items stored in the keywords map.
If all this sounds weird and woozy, here is a sample code to show how awesome this can be:
PHP:
item_search my_search = create_item_search( $items[] );
foreach it in my_search.search( "familiar larva" )
.update_prices( 1 ).search_mall_price( 100000 , 100000000 )
.current_selection
print( it + " is an expensive familiar, probably an IOTM." );
my_search.reset().search( "tiny plastic crimbo" ).update_prices( 1 );
foreach it in my_search.current_selection
print( it + " is available at " + my_search.prices[ it ] + " in the mall." );
Thanks To
- zarqon: For his wonderful work on zlib.ash
- Bale, heeheehee, StDoodle: Help on various ASH functions and quirks.
Change Log
2010-07-08 v0.1: Initial release
2010-07-12 v0.2: Completely restructured how search works. Added classifier functions. Added negative search (!). Deprecated item_search_keywords.txt and added item_search_classfiers.txt. Added optional support for potion effects, adjustable via classfiers.
2010-07-14 v0.3: Slight speed improvements. Added classify_rare_items(). classify_consumables(), classify_packages(), and classify_rare_items() now require a call to activate_functionality( string functionality ) at the start of a script.
Attachments
Last edited: