Skip to content

Commit

Permalink
Allow excluding items lacking flags (#70171)
Browse files Browse the repository at this point in the history
* First

* Test fix
  • Loading branch information
Ramza13 authored Dec 15, 2023
1 parent 212dd69 commit 067f6ad
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
16 changes: 16 additions & 0 deletions data/mods/TEST_DATA/EOC.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,22 @@
"id": "EOC_run_inv_test4_nested",
"effect": [ { "npc_add_var": "key4", "type": "general", "context": "run_inv_test", "value": "yes" } ]
},
{
"type": "effect_on_condition",
"id": "EOC_run_inv_test5_nested",
"effect": [ { "npc_add_var": "key5", "type": "general", "context": "run_inv_test", "value": "yes" } ]
},
{
"type": "effect_on_condition",
"id": "EOC_run_inv_test5",
"effect": [
{
"u_run_inv_eocs": "all",
"true_eocs": [ "EOC_run_inv_test5_nested" ],
"search_data": [ { "excluded_flags": [ "BELT_CLIP" ] } ]
}
]
},
{
"type": "effect_on_condition",
"id": "EOC_item_math_test",
Expand Down
2 changes: 1 addition & 1 deletion doc/EFFECT_ON_CONDITION.md
Original file line number Diff line number Diff line change
Expand Up @@ -1901,7 +1901,7 @@ Run EOCs on items in your or NPC's inventory
| Syntax | Optionality | Value | Info |
| --- | --- | --- | --- |
| "u_run_inv_eocs" / "npc_run_inv_eocs" | **mandatory** | string or [variable object](#variable-object) | way the item would be picked; <br/>values can be:<br/>`all` - all items that match the conditions are picked;<br/> `random` - from all items that match the conditions, one picked;<br/>`manual` - menu is open with all items that can be picked, and you can choose one;<br/>`manual_mult` - same as `manual`, but multiple items can be picked |
| "search_data" | optional | `search_data` | sets the condition for the target item; lack of search_data means any item can be picked; conditions can be:<br/>`id` - id of a specific item;<br/>`category` - category of an item (case sensitive, should always be in lower case);<br/>`flags`- flag or flags the item has<br/>`material` - material of an item;<br/>`worn_only` - if true, return only items, that are worn;<br/>`wielded_only` - if true, return only wielded items |
| "search_data" | optional | `search_data` | sets the condition for the target item; lack of search_data means any item can be picked; conditions can be:<br/>`id` - id of a specific item;<br/>`category` - category of an item (case sensitive, should always be in lower case);<br/>`flags`- flag or flags the item has<br/>`excluded_flags`- flag or flags the item doesn't have<br/>`material` - material of an item;<br/>`worn_only` - if true, return only items, that are worn;<br/>`wielded_only` - if true, return only wielded items |
| "title" | optional | string or [variable object](#variable-object) | name of the menu, that would be shown, if `manual` or `manual_mult` is used |
| "true_eocs" / "false_eocs" | optional | string, [variable object](##variable-object), inline EoC, or range of all of them | if item was picked successfully, all EoCs from `true_eocs` are run, otherwise all EoCs from `false_eocs` are run; picked item is returned as npc; for example, `n_hp()` return hp of an item |

Expand Down
9 changes: 9 additions & 0 deletions src/npctalk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ struct item_search_data {
item_category_id category;
material_id material;
std::vector<flag_id> flags;
std::vector<flag_id> excluded_flags;
bool worn_only;
bool wielded_only;

Expand All @@ -172,6 +173,9 @@ struct item_search_data {
for( std::string flag : jo.get_string_array( "flags" ) ) {
flags.emplace_back( flag );
}
for( std::string flag : jo.get_string_array( "excluded_flags" ) ) {
excluded_flags.emplace_back( flag );
}
worn_only = jo.get_bool( "worn_only", false );
wielded_only = jo.get_bool( "wielded_only", false );
}
Expand All @@ -191,6 +195,11 @@ struct item_search_data {
return false;
}
}
for( flag_id flag : excluded_flags ) {
if( loc->has_flag( flag ) ) {
return false;
}
}
if( worn_only && !guy->is_worn( *loc ) ) {
return false;
}
Expand Down
10 changes: 10 additions & 0 deletions tests/eoc_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ static const effect_on_condition_id effect_on_condition_EOC_run_inv_test1( "EOC_
static const effect_on_condition_id effect_on_condition_EOC_run_inv_test2( "EOC_run_inv_test2" );
static const effect_on_condition_id effect_on_condition_EOC_run_inv_test3( "EOC_run_inv_test3" );
static const effect_on_condition_id effect_on_condition_EOC_run_inv_test4( "EOC_run_inv_test4" );
static const effect_on_condition_id effect_on_condition_EOC_run_inv_test5( "EOC_run_inv_test5" );
static const effect_on_condition_id effect_on_condition_EOC_run_until_test( "EOC_run_until_test" );
static const effect_on_condition_id effect_on_condition_EOC_run_with_test( "EOC_run_with_test" );
static const effect_on_condition_id
Expand Down Expand Up @@ -972,6 +973,15 @@ TEST_CASE( "EOC_run_inv_test", "[eoc]" )

CHECK( items_after.size() == 1 );

// Test search_data: excluded flags
CHECK( effect_on_condition_EOC_run_inv_test5->activate( d ) );

items_after = get_avatar().items_with( []( const item & it ) {
return it.get_var( "npctalk_var_general_run_inv_test_key5" ) == "yes";
} );

CHECK( items_after.size() == 3 );

// Flag test for item
CHECK( effect_on_condition_EOC_item_flag_test->activate( d ) );

Expand Down

0 comments on commit 067f6ad

Please sign in to comment.