Skip to content

Commit

Permalink
Merge pull request #72120 from andrei8l/jmath-no-shadow
Browse files Browse the repository at this point in the history
jmath: don't allow shadowing of built-in functions
  • Loading branch information
Maleclypse authored Mar 3, 2024
2 parents 1ae55d5 + d5695d8 commit 18bed67
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 10 deletions.
2 changes: 1 addition & 1 deletion data/mods/Magiclysm/eoc_spell_learn_boost.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"id": "mc_spell_exp_diff",
"num_args": 1,
"//": "accept the spell level, return a difference in experience between spell's current level and the next level",
"return": "spell_exp(_0 + 1) - spell_exp(_0)"
"return": "spell_exp_for_level(_0 + 1) - spell_exp_for_level(_0)"
},
{
"type": "jmath_function",
Expand Down
9 changes: 1 addition & 8 deletions data/mods/Xedra_Evolved/jmath.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,12 @@
"//": "used to transform the variable time (the smallest measure is 1 second) to the spell time (the smallest measure is 1 move, 1 second = 100 moves)",
"return": "_0 * 100"
},
{
"type": "jmath_function",
"id": "spell_exp",
"num_args": 1,
"//": "accept a spell level, return the amount of XP spell required at this level.",
"return": "2.71828182^(0.146661*(_0+62.5))-6200"
},
{
"type": "jmath_function",
"id": "spell_exp_diff",
"num_args": 1,
"//": "accept the spell level, return a difference in experience between spell's current level and the next level",
"return": "spell_exp(_0 + 1) - spell_exp(_0)"
"return": "spell_exp_for_level(_0 + 1) - spell_exp_for_level(_0)"
},
{
"type": "jmath_function",
Expand Down
1 change: 1 addition & 0 deletions doc/NPCs.md
Original file line number Diff line number Diff line change
Expand Up @@ -1348,6 +1348,7 @@ _some functions support array arguments or kwargs, denoted with square brackets
| skill(`s`/`v`) ||| u, n | Return or set skill level<br/><br/>Example:<br/>`"condition": { "math": [ "u_skill('driving')", ">=", "5"] }`<br/>`"condition": { "math": [ "u_skill(someskill)", ">=", "5"] }`|
| skill_exp(`s`/`v`) ||| u, n | Return or set skill experience<br/> Argument is skill ID. <br/><br/> Optional kwargs:<br/>`format`: `s`/`v` - must be `percentage` or `raw`.<br/><br/>Example:<br/>`{ "math": [ "u_skill_exp('driving', 'format': crt_format)", "+=", "10"] }`|
| spell_exp(`s`/`v`) ||| u, n | Return or set spell xp<br/> Example:<br/>`"condition": { "math": [ "u_spell_exp('SPELL_ID')", ">=", "5"] }`|
| spell_exp_for_level(`d`/`v`) ||| g | Return the amount of XP necessary for a spell level. <br/> Example:<br/>`"math": [ "spell_exp_for_level(u_spell_level('SPELL_ID')) * 5"] }`|
| spell_count() ||| u, n | Return number of spells the character knows.<br/><br/> Optional kwargs:<br/>`school`: `s/v` - return number of spells known of that school.<br/><br/> Example:<br/>`"condition": { "math": [ "u_spell_count('school': 'MAGUS')", ">=", "10"] }`|
| spell_level(`s`/`v`) ||| u, n | Return or set level of a given spell. -1 means the spell is not known when read and that the spell should be forgotten if written.<br/>Argument is spell ID. If `"null"` is given, return the highest level of spells the character knows (read only).<br/> Example:<br/>`"condition": { "math": [ "u_spell_level('SPELL_ID')", "==", "-1"] }`|
| spell_level_adjustment(`s`/`v`) ||| u, n | Return or set temporary caster level adjustment. Only useable by EoCs that trigger on the event `opens_spellbook`. Old values will be reset to 0 before the event triggers. To avoid overwriting values from other EoCs, it is recommended to adjust the values here with `+=` or `-=` instead of setting it to an absolute value.<br/>Argument is spell ID. If `"null"` is given, adjust all spell level.<br/><br/>Example:<br/>`{ "math": [ "u_spell_level_adjustment('SPELL_ID')", "+=", "3"] }`|
Expand Down
11 changes: 10 additions & 1 deletion src/math_parser_diag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -945,6 +945,14 @@ std::function<double( dialogue & )> spell_exp_eval( char scope,
};
}

std::function<double( dialogue & )> spell_exp_for_level_eval( char /* scope */,
std::vector<diag_value> const &params, diag_kwargs const &/* kwargs */ )
{
return[level = params[0]]( dialogue const & d ) -> double {
return spell::exp_for_level( level.dbl( d ) );
};
}

std::function<void( dialogue &, double )> spell_exp_ass( char scope,
std::vector<diag_value> const &params, diag_kwargs const &/* kwargs */ )
{
Expand Down Expand Up @@ -1397,6 +1405,7 @@ std::map<std::string_view, dialogue_func_eval> const dialogue_eval_f{
{ "skill_exp", { "un", 1, skill_exp_eval } },
{ "spell_count", { "un", 0, spell_count_eval}},
{ "spell_exp", { "un", 1, spell_exp_eval}},
{ "spell_exp_for_level", { "g", 1, spell_exp_for_level_eval}},
{ "spell_level", { "un", 1, spell_level_eval}},
{ "spell_level_adjustment", { "un", 1, spell_level_adjustment_eval } },
{ "time", { "g", 1, time_eval } },
Expand Down Expand Up @@ -1441,4 +1450,4 @@ std::map<std::string_view, dialogue_func_eval> const &get_all_diag_eval_funcs()
std::map<std::string_view, dialogue_func_ass> const &get_all_diag_ass_funcs()
{
return dialogue_assign_f;
}
}
9 changes: 9 additions & 0 deletions src/math_parser_jmath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "dialogue.h"
#include "generic_factory.h"
#include "math_parser.h"
#include "math_parser_diag.h"

namespace
{
Expand Down Expand Up @@ -50,6 +51,14 @@ void jmath_func::load( JsonObject const &jo, const std::string_view /*src*/ )
{
optional( jo, was_loaded, "num_args", num_params );
optional( jo, was_loaded, "return", _str );

for( auto const &iter : get_all_diag_eval_funcs() ) {
if( std::string const idstr = id.str(); iter.first == idstr ) {
jo.throw_error( string_format(
R"(jmath function "%s" shadows a built-in function with the same name. You must rename it.)",
idstr ) );
}
}
}

void jmath_func::finalize()
Expand Down
1 change: 1 addition & 0 deletions src/math_parser_jmath.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ struct dialogue;

struct jmath_func {
jmath_func_id id;
std::vector<std::pair<jmath_func_id, mod_id>> src;
bool was_loaded = false;
int num_params{};

Expand Down

0 comments on commit 18bed67

Please sign in to comment.