Modifier eval: Difference between revisions

From Kolmafia
Jump to navigation Jump to search
imported>Bale
Created page with '{{ #vardefine:name|modifier_eval}}{{ #vardefine:return_type|float}}{{ FunctionPage| name={{#var:name}}| function_category=Math and Numbers| function1={{Function| name={{#var:na…'
 
imported>Bale
mNo edit summary
Line 5: Line 5:
FunctionPage|
FunctionPage|
name={{#var:name}}|
name={{#var:name}}|
function_category=Math and Numbers|


function1={{Function|
function1={{Function|
Line 73: Line 72:
special=Note that modifier_eval '''never''' generates errors - a malformed expression will just print a message, and return an arbitrary value.|
special=Note that modifier_eval '''never''' generates errors - a malformed expression will just print a message, and return an arbitrary value.|
}}
}}
[[Category:Math and Numbers]]

Revision as of 05:06, 22 May 2010

Function Syntax

float modifier_eval(string expression )

  • expression is a mathematical expression to be solved.

Evaluates an expression in the format used by variable modifiers:

  • 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)
  • Location functions: loc(text) zone(text)
    • These have a value of 1 if the current adventure location or zone contains the specified text, 0 elsewhere.
  • Familiar function: fam(text)
    • This has a value of 1 if the player's familiar type contains the text, else 0.
  • Preferences function: pref(text)
    • This must be used on preferences with a float value ONLY - merely retrieving an integer pref will corrupt it!
  • There can be at most one of each text function in an expression.
  • All upper-case letters are reserved for internally-used variables. The ones likely to be of use in user code are:
    • D - drunkenness
    • F - fullness
    • G - Grimace darkness (0..5)
    • L - player level
    • M - total moonlight (0..9)
    • S - spleenness
    • X - gender (-1=male, 1=female)
  • 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 modifier_eval() to include support for user-defined variables. It is extremely complex, but it is extremely useful to anyone who wants to use modifier_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));

Special

Note that modifier_eval never generates errors - a malformed expression will just print a message, and return an arbitrary value.