Skip to content

Commit

Permalink
add eoc effect and condition, that searches weighed amount of items i…
Browse files Browse the repository at this point in the history
…n your inventory
  • Loading branch information
GuardianDll committed Aug 12, 2024
1 parent 2d4e043 commit e2ff4de
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/condition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -867,6 +867,45 @@ conditional_t::func f_has_items( const JsonObject &jo, const std::string_view me
}
}

conditional_t::func f_has_items_sum( const JsonObject &jo, const std::string_view member,
bool is_npc )
{
std::vector<std::pair<str_or_var, dbl_or_var>> item_and_amount;

for( const JsonObject &jsobj : jo.get_array( member ) ) {
const str_or_var item = get_str_or_var( jsobj.get_member( "item" ), "item", true );
const dbl_or_var amount = get_dbl_or_var( jsobj, "amount", true, 1 );
item_and_amount.emplace_back( item, amount );
}
return [item_and_amount, is_npc]( dialogue & d ) {
add_msg_debug( debugmode::DF_TALKER, "using _has_items_sum:" );

itype_id item;
float percent = 0.0f;
float count_desired;
float count_present;
float charges_present;
float total_present;
for( auto &pair : item_and_amount ) {
item = itype_id( pair.first.evaluate( d ) );
count_desired = pair.second.evaluate( d );
count_present = d.actor( is_npc )->get_amount( item );
charges_present = d.actor( is_npc )->charges_of( item );
total_present = std::max( count_present, charges_present );
percent += total_present / count_desired;

add_msg_debug( debugmode::DF_TALKER,
"item: %s, count_desired: %f, count_present: %f, charges_present: %f, total_present: %f, percent: %f",
item.str(), count_desired, count_present, charges_present, total_present, percent );

if( percent >= 1 ) {
return true;
}
}
return false;
};
}

conditional_t::func f_has_item_with_flag( const JsonObject &jo, std::string_view member,
bool is_npc )
{
Expand Down Expand Up @@ -2454,6 +2493,7 @@ parsers = {
{"u_has_item", "npc_has_item", jarg::member, &conditional_fun::f_has_item },
{"u_has_item_with_flag", "npc_has_item_with_flag", jarg::member, &conditional_fun::f_has_item_with_flag },
{"u_has_items", "npc_has_items", jarg::member, &conditional_fun::f_has_items },
{"u_has_items_sum", "npc_has_items_sum", jarg::array, &conditional_fun::f_has_items_sum },
{"u_has_item_category", "npc_has_item_category", jarg::member, &conditional_fun::f_has_item_category },
{"u_has_bionics", "npc_has_bionics", jarg::member, &conditional_fun::f_has_bionics },
{"u_has_any_effect", "npc_has_any_effect", jarg::array, &conditional_fun::f_has_any_effect },
Expand Down
70 changes: 70 additions & 0 deletions src/npctalk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3439,6 +3439,75 @@ talk_effect_fun_t::func f_consume_item( const JsonObject &jo, std::string_view m
};
}

talk_effect_fun_t::func f_consume_item_sum( const JsonObject &jo, std::string_view member,
const std::string_view, bool is_npc )
{
std::vector<std::pair<str_or_var, dbl_or_var>> item_and_amount;

for( const JsonObject &jsobj : jo.get_array( member ) ) {
const str_or_var item = get_str_or_var( jsobj.get_member( "item" ), "item", true );
const dbl_or_var amount = get_dbl_or_var( jsobj, "amount", true, 1 );
item_and_amount.emplace_back( item, amount );
}

return [item_and_amount, is_npc]( dialogue & d ) {
add_msg_debug( debugmode::DF_TALKER, "using _consume_item_sum:" );

itype_id item;
float percent = 0.0f;
float count_desired;
float count_present;
float charges_present;

for( auto &pair : item_and_amount ) {
item = itype_id( pair.first.evaluate( d ) );
count_desired = pair.second.evaluate( d );
count_present = d.actor( is_npc )->get_amount( item );
charges_present = d.actor( is_npc )->charges_of( item );

if( charges_present > count_present ) {
percent += charges_present / count_desired;
// if percent is equal or less than 1, it is safe to remove all charges_present
// otherwise loop to remove charges one by one
if( percent <= 1 ) {
d.actor( is_npc )->use_charges( item, charges_present, true );

add_msg_debug( debugmode::DF_TALKER,
"removing item: %s, count_desired: %f, charges_present: %f, percent: %f, removing all",
item.str(), count_desired, charges_present, percent );
} else {
percent -= charges_present / count_desired;
for( int i = 0; percent >= 1; ++i ) {
percent += 1 / count_desired;
d.actor( is_npc )->use_charges( item, 1, true );
add_msg_debug( debugmode::DF_TALKER,
"removing item: %s, count_desired: %f, charges_present: %f, percent: %f, removing one by one",
item.str(), count_desired, charges_present, percent );
}
}
} else {
percent += count_present / count_desired;
if( percent <= 1 ) {
d.actor( is_npc )->use_amount( item, count_present );
add_msg_debug( debugmode::DF_TALKER,
"removing item: %s, count_desired: %f, count_present: %f, percent: %f, removing all",
item.str(), count_desired, count_present, percent );
} else {
percent -= charges_present / count_desired;
for( int i = 0; percent >= 1; ++i ) {
percent += 1 / count_desired;
d.actor( is_npc )->use_amount( item, 1 );

add_msg_debug( debugmode::DF_TALKER,
"removing item: %s, count_desired: %f, count_present: %f, percent: %f, removing one by one",
item.str(), count_desired, count_present, percent );
}
}
}
}
};
}

talk_effect_fun_t::func f_remove_item_with( const JsonObject &jo, std::string_view member,
const std::string_view, bool is_npc )
{
Expand Down Expand Up @@ -6577,6 +6646,7 @@ parsers = {
{ "u_unset_flag", "npc_unset_flag", jarg::member, &talk_effect_fun::f_unset_flag },
{ "u_activate", "npc_activate", jarg::member, &talk_effect_fun::f_activate },
{ "u_consume_item", "npc_consume_item", jarg::member, &talk_effect_fun::f_consume_item },
{ "u_consume_item_sum", "npc_consume_item_sum", jarg::array, &talk_effect_fun::f_consume_item_sum },
{ "u_remove_item_with", "npc_remove_item_with", jarg::member, &talk_effect_fun::f_remove_item_with },
{ "u_bulk_trade_accept", "npc_bulk_trade_accept", jarg::member, &talk_effect_fun::f_bulk_trade_accept },
{ "u_bulk_donate", "npc_bulk_donate", jarg::member, &talk_effect_fun::f_bulk_trade_accept },
Expand Down

0 comments on commit e2ff4de

Please sign in to comment.