Difference between pages "Template:Fa" and "Monster eval"

From Kolmafia
(Difference between pages)
Jump to navigation Jump to search
imported>StDoodle
m
 
imported>Theraze
 
Line 1: Line 1:
<includeonly>{{Lnkstart}}&#35;{{{1}}}{{!}}{{{1}}}&#40;&#41;{{Lnkend}}</includeonly>
+
{{
<noinclude>
+
#vardefine:name|monster_eval}}{{
A simple way of linking to a function '''on the same page''', but displaying it with the parentheses.
+
#vardefine:return_type|float}}{{
[[Category:Basic Templates]]
+
 
</noinclude>
+
FunctionPage|
 +
name={{#var:name}}|
 +
 
 +
function1={{Function|
 +
name={{#var:name}}|
 +
aggregate={{#var:aggregate}}|
 +
return_type={{#var:return_type}}|
 +
return_also={{#var:return_also}}|
 +
parameter1={{Param|string|expression}}|
 +
p1desc={{pspan|expression}} is a mathematical expression to be solved.|
 +
}}|
 +
 
 +
function_description= Evaluates an expression in the format used by variable monsters:
 +
 
 +
* No spaces are allowed within the expression, except as part of a zone/location name.
 +
* + - * / ( ) have their usual mathematical meaning and precedence.
 +
* ^ is exponentiation, with the highest precedence.
 +
* Functions available: ceil(x) floor(x) sqrt(x) min(x,y) max(x,y)
 +
* Preferences function: pref(text)
 +
** This must be used on preferences with a float value ONLY - merely retrieving an integer pref will corrupt it!
 +
* There could be at most one of each text function in an expression.
 +
** This is no longer the case however and multiple of the same text functions should now work properly.
 +
* All upper-case letters are reserved for internally-used variables. The ones likely to be of use in user code are:
 +
** A - player's ascension count
 +
** MUS - player's adjusted muscle
 +
** MYS - player's adjusted mysticality
 +
** MOX - player's adjusted moxie
 +
** ML - player's monster level adjustment
 +
** MCD - player's MCD modifier
 +
* This wrapper allows user-defined variables to be used as well, which must have names starting with a lower-case letter (or underscore) to distinguish them from built-in variables.  Variables are supplied as a float[string] map.
 +
&nbsp;|
 +
 
 +
code1={{CodeSample|
 +
title=Code Sample|
 +
description=This script expands monster_eval() to include support for user-defined variables. It is extremely complex, but it is extremely useful to anyone who wants to use monster_eval().|
 +
code=
 +
<syntaxhighlight>
 +
float eval(string expr, float[string] vars) {
 +
  buffer b;
 +
  matcher m = create_matcher( "\\b[a-z_][a-zA-Z0-9_]*\\b", expr );
 +
  while (m.find()) {
 +
      string var = m.group(0);
 +
      if (vars contains var) {
 +
        m.append_replacement(b, vars[var].to_string());
 +
      }
 +
      // could implement functions, pref access, etc. here
 +
  }
 +
  m.append_tail(b);
 +
  return modifier_eval(b.to_string());
 +
}
 +
 
 +
# Everything below this line shows how to make use of eval().
 +
# TESTING:
 +
 
 +
float[string] v;
 +
v["pi"] = 3.14159265;
 +
v["ten"] = 10;
 +
print(eval("2+3", v));
 +
print(eval("max(pi^ten,ten^pi)", v));
 +
print(eval("sqrt(pi)*L", v));
 +
print(eval("undefined/2", v));
 +
 
 +
</syntaxhighlight>}}|
 +
}}
 +
 
 +
[[Category:Math and Numbers]]

Revision as of 14:53, 16 December 2010

Function Syntax

float monster_eval(string expression )

  • expression is a mathematical expression to be solved.

Evaluates an expression in the format used by variable monsters:

  • No spaces are allowed within the expression, except as part of a zone/location name.
  • + - * / ( ) have their usual mathematical meaning and precedence.
  • ^ is exponentiation, with the highest precedence.
  • Functions available: ceil(x) floor(x) sqrt(x) min(x,y) max(x,y)
  • Preferences function: pref(text)
    • This must be used on preferences with a float value ONLY - merely retrieving an integer pref will corrupt it!
  • There could be at most one of each text function in an expression.
    • This is no longer the case however and multiple of the same text functions should now work properly.
  • All upper-case letters are reserved for internally-used variables. The ones likely to be of use in user code are:
    • A - player's ascension count
    • MUS - player's adjusted muscle
    • MYS - player's adjusted mysticality
    • MOX - player's adjusted moxie
    • ML - player's monster level adjustment
    • MCD - player's MCD modifier
  • This wrapper allows user-defined variables to be used as well, which must have names starting with a lower-case letter (or underscore) to distinguish them from built-in variables. Variables are supplied as a float[string] map.

 

Code Sample

This script expands monster_eval() to include support for user-defined variables. It is extremely complex, but it is extremely useful to anyone who wants to use monster_eval().

float eval(string expr, float[string] vars) {
   buffer b;
   matcher m = create_matcher( "\\b[a-z_][a-zA-Z0-9_]*\\b", expr );
   while (m.find()) {
      string var = m.group(0);
      if (vars contains var) {
         m.append_replacement(b, vars[var].to_string());
      }
      // could implement functions, pref access, etc. here
   }
   m.append_tail(b);
   return modifier_eval(b.to_string());
}

# Everything below this line shows how to make use of eval().
# TESTING:

float[string] v;
v["pi"] = 3.14159265;
v["ten"] = 10;
print(eval("2+3", v));
print(eval("max(pi^ten,ten^pi)", v));
print(eval("sqrt(pi)*L", v));
print(eval("undefined/2", v));