Skip to content

Commit

Permalink
Expose daily calories calendar to math (#73811)
Browse files Browse the repository at this point in the history
* test

* no message

* document new math

* astyle

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
GuardianDll and github-actions[bot] authored May 16, 2024
1 parent 253780d commit a2efbb9
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 0 deletions.
1 change: 1 addition & 0 deletions doc/NPCs.md
Original file line number Diff line number Diff line change
Expand Up @@ -1378,6 +1378,7 @@ _some functions support array arguments or kwargs, denoted with square brackets
| climate_control_str_heat() ||| u, n | return amount of heat climate control that character currently has (character feels better in warm places with it), in warmth points; default 0, affected by CLIMATE_CONTROL_HEAT enchantment.<br/><br/>Example:<br/>`"condition": { "math": [ "u_climate_control_str_heat()", "<", "0" ] }`|
| climate_control_str_chill() ||| u, n | return amount of chill climate control that character currently has (character feels better in cold places with it), in warmth points; default 0, affected by CLIMATE_CONTROL_HEAT enchantment.<br/><br/>Example:<br/>`"condition": { "math": [ "n_climate_control_str_chill()", "<", "0" ] }`|
| calories() ||| u, n | Return amount of calories character has. If used on item, return amount of calories this item gives when consumed (not affected by enchantments or mutations). Optional kwargs:<br/>`format`: `s/v` - return the value in specific format. Can be `percent` (return percent to the healthy amount of calories, `100` being the target, bmi 25, or 110000 kcal) or `raw`. If now used, `raw` is used by default.<br/><br/>Example:<br/>`"condition": { "math": [ "u_calories()", "<", "0" ] }`<br/>`"condition": { "math": [ "u_calories('format': 'percent')", ">", "0" ] }`<br/>`"condition": { "math": [ "u_calories()", "=", "110000" ] }`|
| get_calories_daily() ||| g | Return amount of calories character consumed before, up to 30 days, in kcal. Calorie diary is something only character has, so it can't be used with NPCs. Optional kwargs:<br/>`day`: `d/v` - picks the date the value would be pulled from, from 0 to 30. Default 0, meaning amount of calories you consumed today.<br/>`type`: `s/v` - picks the data that would be pulled. Possible values are: `spent` - how much calories character spent in different activities throughout the day; `gained` - how much calories character ate that day; `ingested` - how much calories character processed that day; `total` - `gained` minus `spent`. Default is `total`;<br/><br/>Example:<br/>`"condition": { "math": [ "get_calories_daily()", ">", "1000" ] }`<br/> `{ "math": [ "foo", "=", "get_calories_daily('type':'gained', 'day':'1')" ] }`|

#### List of Character and item aspects
These can be read or written to with `val()`.
Expand Down
26 changes: 26 additions & 0 deletions src/avatar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1635,6 +1635,32 @@ void avatar::add_gained_calories( int cal )
calorie_diary.front().gained += cal;
}

int avatar::get_daily_calories( unsigned days_ago, std::string const &type ) const
{
auto iterator = calorie_diary.begin();
if( days_ago > calorie_diary.size() ) {
debugmsg(
"trying to access calorie diary from %d days ago, but the diary only contains %d days",
days_ago, calorie_diary.size() );
return 0;
}
std::advance( iterator, days_ago );

int result{};

if( type == "spent" ) {
result = iterator->spent;
} else if( type == "gained" ) {
result = iterator->gained;
} else if( type == "ingested" ) {
result = iterator->ingested;
} else if( type == "total" ) {
result = iterator->total();
}

return result;
}

void avatar::log_activity_level( float level )
{
calorie_diary.front().activity_levels[level]++;
Expand Down
1 change: 1 addition & 0 deletions src/avatar.h
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ class avatar : public Character
void update_cardio_acc() override;
void add_spent_calories( int cal ) override;
void add_gained_calories( int cal ) override;
int get_daily_calories( unsigned days_ago, std::string const &type ) const;
void log_activity_level( float level ) override;
std::string total_daily_calories_string() const;
//set 0-3 random hobbies, with 1 and 2 being twice as likely as 0 and 3
Expand Down
27 changes: 27 additions & 0 deletions src/math_parser_diag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -889,6 +889,32 @@ std::function<void( dialogue &, double )> school_level_adjustment_ass( char scop
};
}

std::function<double( dialogue & )> get_daily_calories( char scope,
std::vector<diag_value> const &/* params */, diag_kwargs const &kwargs )
{
diag_value type_val( std::string( "total" ) );
diag_value day_val( 0.0 );

if( kwargs.count( "day" ) != 0 ) {
day_val = *kwargs.at( "day" );
}

if( kwargs.count( "type" ) != 0 ) {
type_val = *kwargs.at( "type" );
}

return[beta = is_beta( scope ), day_val, type_val ]( dialogue const & d ) {
std::string type = type_val.str( d );
int const day = day_val.dbl( d );
if( day < 0 ) {
debugmsg( "get_daily_calories(): cannot access calorie diary from the future (day < 0)" );
return 0;
}

return static_cast<talker const *>( d.actor( beta ) )->get_daily_calories( day, type );
};
}

std::function<double( dialogue & )> skill_eval( char scope,
std::vector<diag_value> const &params, diag_kwargs const &/* kwargs */ )
{
Expand Down Expand Up @@ -1560,6 +1586,7 @@ std::map<std::string_view, dialogue_func_eval> const dialogue_eval_f{
{ "pain", { "un", 0, pain_eval } },
{ "school_level", { "un", 1, school_level_eval}},
{ "school_level_adjustment", { "un", 1, school_level_adjustment_eval } },
{ "get_calories_daily", { "g", 0, get_daily_calories } },
{ "skill", { "un", 1, skill_eval } },
{ "skill_exp", { "un", 1, skill_exp_eval } },
{ "spell_count", { "un", 0, spell_count_eval}},
Expand Down
3 changes: 3 additions & 0 deletions src/talker.h
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,9 @@ class talker
virtual void learn_recipe( const recipe_id & ) {}
virtual void forget_recipe( const recipe_id & ) {}
virtual void mutate( const int &, const bool & ) {}
virtual int get_daily_calories( int, std::string const & ) const {
return 0;
}
virtual void mutate_category( const mutation_category_id &, const bool & ) {}
virtual void mutate_towards( const trait_id &, const mutation_category_id &, const bool & ) {};
virtual void set_mutation( const trait_id &, const mutation_variant * = nullptr ) {}
Expand Down
5 changes: 5 additions & 0 deletions src/talker_avatar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ int talker_avatar::trial_chance_mod( const std::string &trial_type ) const
return chance;
}

int talker_avatar::get_daily_calories( int day, std::string const &type ) const
{
return me_chr_const->as_avatar()->get_daily_calories( day, type );
}

bool talker_avatar::buy_monster( talker &seller, const mtype_id &mtype, int cost,
int count, bool pacified, const translation &name )
{
Expand Down
1 change: 1 addition & 0 deletions src/talker_avatar.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class talker_avatar: public talker_cloner<talker_avatar, talker_character>
std::vector<std::string> get_topics( bool ) override;
int parse_mod( const std::string &attribute, int factor ) const override;
int trial_chance_mod( const std::string &trial_type ) const override;
int get_daily_calories( int, std::string const & ) const override;

// inventory and such
bool buy_monster( talker &seller, const mtype_id &mtype, int cost,
Expand Down
1 change: 1 addition & 0 deletions src/talker_character.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <memory>

#include "avatar.h"
#include "character_id.h"
#include "character_martial_arts.h"
#include "effect.h"
Expand Down

0 comments on commit a2efbb9

Please sign in to comment.