Files
Cataclysm-DDA/tests/consumption_count_test.cpp
dumb-kevin 195d378c6c Add consumption_count(), eats_like, and monotony grouping
consumption_count() math function exposes consumption_history to EOCs.
consumption_count_total() counts all food eaten in a time window.

New eats_like comestible field groups related foods for monotony
penalties and consumption counting (e.g. butter/ghee/raw_butter).

Butter gets an inline consumption EOC: 3+ servings in 6h = nausea.
2026-03-19 17:42:52 +04:00

137 lines
4.6 KiB
C++

#include <list>
#include <memory>
#include <string>
#include "avatar.h"
#include "calendar.h"
#include "cata_catch.h"
#include "character.h"
#include "dialogue.h"
#include "item.h"
#include "math_parser.h"
#include "player_helpers.h"
#include "talker.h"
#include "type_id.h"
static const itype_id itype_butter( "butter" );
static const itype_id itype_ghee( "ghee" );
static const itype_id itype_raw_butter( "raw_butter" );
TEST_CASE( "consumption_count_math_function", "[math_parser][consumption]" )
{
clear_avatar();
avatar &guy = get_avatar();
math_exp testexp;
dialogue d( get_talker_for( guy ), std::make_unique<talker>() );
SECTION( "returns 0 with no consumption history" ) {
guy.consumption_history.clear();
REQUIRE( testexp.parse( "u_consumption_count('butter')" ) );
CHECK( testexp.eval( d ) == Approx( 0 ) );
}
SECTION( "counts matching items in history" ) {
guy.consumption_history.clear();
item butter( itype_butter );
guy.consumption_history.emplace_back( butter );
guy.consumption_history.emplace_back( butter );
guy.consumption_history.emplace_back( butter );
REQUIRE( testexp.parse( "u_consumption_count('butter')" ) );
CHECK( testexp.eval( d ) == Approx( 3 ) );
// Different item should not count
REQUIRE( testexp.parse( "u_consumption_count('apple')" ) );
CHECK( testexp.eval( d ) == Approx( 0 ) );
}
SECTION( "hours kwarg filters by time window" ) {
guy.consumption_history.clear();
item butter( itype_butter );
// Add a recent event (current turn)
guy.consumption_history.emplace_back( butter );
// Add an old event (10 hours ago)
consumption_event old_event( butter );
old_event.time = calendar::turn - 10_hours;
guy.consumption_history.push_back( old_event );
// Default 48h window should see both
REQUIRE( testexp.parse( "u_consumption_count('butter')" ) );
CHECK( testexp.eval( d ) == Approx( 2 ) );
// 6h window should see only the recent one
REQUIRE( testexp.parse( "u_consumption_count('butter', 'hours': 6)" ) );
CHECK( testexp.eval( d ) == Approx( 1 ) );
// 12h window should see both
REQUIRE( testexp.parse( "u_consumption_count('butter', 'hours': 12)" ) );
CHECK( testexp.eval( d ) == Approx( 2 ) );
}
SECTION( "eats_like groups related foods together" ) {
guy.consumption_history.clear();
item butter( itype_butter );
item ghee( itype_ghee );
item raw_butter( itype_raw_butter );
guy.consumption_history.emplace_back( butter );
guy.consumption_history.emplace_back( ghee );
guy.consumption_history.emplace_back( raw_butter );
// All three should count when querying butter (ghee and raw_butter have eats_like: butter)
REQUIRE( testexp.parse( "u_consumption_count('butter')" ) );
CHECK( testexp.eval( d ) == Approx( 3 ) );
// Querying ghee directly should also count all three (resolves to butter)
REQUIRE( testexp.parse( "u_consumption_count('ghee')" ) );
CHECK( testexp.eval( d ) == Approx( 3 ) );
}
}
TEST_CASE( "consumption_count_total_math_function", "[math_parser][consumption]" )
{
clear_avatar();
avatar &guy = get_avatar();
math_exp testexp;
dialogue d( get_talker_for( guy ), std::make_unique<talker>() );
SECTION( "returns 0 with no history" ) {
guy.consumption_history.clear();
REQUIRE( testexp.parse( "u_consumption_count_total()" ) );
CHECK( testexp.eval( d ) == Approx( 0 ) );
}
SECTION( "counts all items regardless of type" ) {
guy.consumption_history.clear();
item butter( itype_butter );
item ghee( itype_ghee );
guy.consumption_history.emplace_back( butter );
guy.consumption_history.emplace_back( butter );
guy.consumption_history.emplace_back( ghee );
REQUIRE( testexp.parse( "u_consumption_count_total()" ) );
CHECK( testexp.eval( d ) == Approx( 3 ) );
}
SECTION( "hours kwarg filters by time window" ) {
guy.consumption_history.clear();
item butter( itype_butter );
guy.consumption_history.emplace_back( butter );
consumption_event old_event( butter );
old_event.time = calendar::turn - 10_hours;
guy.consumption_history.push_back( old_event );
REQUIRE( testexp.parse( "u_consumption_count_total()" ) );
CHECK( testexp.eval( d ) == Approx( 2 ) );
REQUIRE( testexp.parse( "u_consumption_count_total('hours': 6)" ) );
CHECK( testexp.eval( d ) == Approx( 1 ) );
}
}