Skip to content

Commit

Permalink
eoc/math_parser: port mod_load_order to math
Browse files Browse the repository at this point in the history
  • Loading branch information
andrei8l committed Jan 26, 2024
1 parent 33ae1be commit 3d25d18
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 13 deletions.
2 changes: 1 addition & 1 deletion doc/NPCs.md
Original file line number Diff line number Diff line change
Expand Up @@ -1274,7 +1274,6 @@ Example | Description
`"u_val": "grab_strength"` | Grab strength as defined in the monster definition. Read-only, returns false on characters.
`"u_val": "volume"` | Current volume in ml. read only. Cullently, doesn't work for characters, but for monsters and items.
`"u_val": "weight"` | Current weight in mg. read only.
`"mod_load_order"` | This should be a string with the name of the mod. It will return the order it was loaded in or -1 if its not loaded.
`"arithmetic"` | An arithmetic expression with no result.<br/><br/>Example:<pre>"real_count": { "arithmetic": [<br/> { "arithmetic": [ { "const":1 }, "+", { "const": 1 } ] },<br/> "+", { "const": 1 }<br/>] },</pre>
`"math"` | An array math object.

Expand Down Expand Up @@ -1413,6 +1412,7 @@ _some functions support array arguments or kwargs, denoted with square brackets
| item_rad(`s`/`v`) ||| u, n | Return irradiation of worn items with the specified flag.<br/>Argument is flag ID.<br/><br/>Optional kwargs:<br/>`aggregate`: `s`/`v` - Specify the aggregation function to run, in case there's more than one item. Valid values are `min`/`max`/`sum`/`average`/`first`/`last`. Defaults to `min` if not specified. <br/><br/>Example:<br/>`"condition": { "math": [ "u_item_rad('RAD_DETECT')", ">=", "1"] }`|
| melee_damage(`s`/`v`) ||| u, n | Return the item's melee damage. Argument is damage type. For special value `ALL`, return sum for all damage types. <br/><br/>Actor must be an item.<br/><br/>Example:<br/>`{ "math": [ "mymelee", "=", "n_melee_damage('ALL')" ] }`<br/>See `EOC_test_weapon_damage` for a complete example|
| monsters_nearby(`s`/`v`...) ||| u, n, global | Return the number of nearby monsters. Takes any number of `s`tring or `v`ariable positional parameters as monster IDs. <br/><br/>Optional kwargs:<br/>`radius`: `d`/`v` - limit to radius (rl_dist)<br/>`location`: `v` - center search on this location<br/>`attitude`: `s`/`v` - attitude filter. Must be one of `hostile`, `friendly`, `both`. Assumes `hostile` if not specified<br/><br/>The `location` kwarg is mandatory in the global scope.<br/><br/>Examples:<br/>`"condition": { "math": [ "u_monsters_nearby('radius': u_search_radius * 3)", ">", "5" ] }`<br/><br/>`"condition": { "math": [ "monsters_nearby('mon_void_maw', 'mon_void_limb', mon_fotm_var, 'radius': u_search_radius * 3, 'location': u_search_loc)", ">", "5" ] }`|
| mod_load_order(`s`/`v`) ||| N/A<br/>(global) | Returns the load order of specified mod. Argument is mod id. Returns -1 is the mod is not loaded. |
| mon_species_nearby(`s`/`v`...) ||| u, n, global | Same as `monsters_nearby()`, but arguments are monster species |
| mon_groups_nearby(`s`/`v`...) ||| u, n, global | Same as `monsters_nearby()`, but arguments are monster groups |
| moon_phase() ||| N/A<br/>(global) | Returns current phase of the Moon. <pre>MOON_NEW = 0,<br/>WAXING_CRESCENT = 1,<br/>HALF_MOON_WAXING = 2,<br/>WAXING_GIBBOUS = 3,<br/>FULL = 4,<br/>WANING_GIBBOUS = 5,<br/>HALF_MOON_WANING = 6,<br/>WANING_CRESCENT = 7 |
Expand Down
12 changes: 0 additions & 12 deletions src/condition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2284,18 +2284,6 @@ std::function<double( dialogue & )> conditional_t::get_get_dbl( J const &jo )
return d.actor( is_npc )->get_npc_anger();
};
}
} else if( jo.has_member( "mod_load_order" ) ) {
const mod_id our_mod_id = mod_id( jo.get_string( "mod_load_order" ) );
return [our_mod_id]( dialogue const & ) {
int count = 0;
for( const mod_id &mod : world_generator->active_world->active_mod_order ) {
if( our_mod_id == mod ) {
return count;
}
count++;
}
return -1;
};
} else if( jo.has_array( "arithmetic" ) ) {
talk_effect_fun_t arith;
if constexpr( std::is_same_v<JsonObject, J> ) {
Expand Down
18 changes: 18 additions & 0 deletions src/math_parser_diag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "string_input_popup.h"
#include "units.h"
#include "weather.h"
#include "worldfactory.h"

/*
General guidelines for writing dialogue functions
Expand Down Expand Up @@ -547,6 +548,22 @@ std::function<double( dialogue & )> melee_damage_eval( char scope,
};
}

std::function<double( dialogue & )> mod_order_eval( char /* scope */,
std::vector<diag_value> const &params, diag_kwargs const &/* kwargs */ )
{
return[mod_val = params[0]]( dialogue const & d ) {
int count = 0;
mod_id our_mod_id( mod_val.str( d ) );
for( const mod_id &mod : world_generator->active_world->active_mod_order ) {
if( our_mod_id == mod ) {
return count;
}
count++;
}
return -1;
};
}

template<class ID>
using f_monster_match = bool ( * )( Creature const &critter, ID const &id );

Expand Down Expand Up @@ -1249,6 +1266,7 @@ std::map<std::string_view, dialogue_func_eval> const dialogue_eval_f{
{ "item_count", { "un", 1, item_count_eval } },
{ "item_rad", { "un", 1, item_rad_eval } },
{ "melee_damage", { "un", 1, melee_damage_eval } },
{ "mod_load_order", { "g", 1, mod_order_eval } },
{ "monsters_nearby", { "ung", -1, monsters_nearby_eval } },
{ "mon_species_nearby", { "ung", -1, monster_species_nearby_eval } },
{ "mon_groups_nearby", { "ung", -1, monster_groups_nearby_eval } },
Expand Down

0 comments on commit 3d25d18

Please sign in to comment.