Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add npc_loc_relative mutator #74219

Merged
merged 7 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions data/json/effects_on_condition/example_eocs.json
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@
"type": "effect_on_condition",
"id": "EOC_map_item_test2",
"effect": [
{ "set_string_var": { "mutator": "loc_relative_u", "target": "(0,1,0)" }, "target_var": { "context_val": "loc" } },
{ "set_string_var": { "mutator": "u_loc_relative", "target": "(0,1,0)" }, "target_var": { "context_val": "loc" } },
{ "map_spawn_item": "bottle_plastic", "loc": { "context_val": "loc" } }
]
},
Expand All @@ -195,12 +195,12 @@
"id": "EOC_map_condition_test",
"effect": [
{
"if": { "map_in_city": { "mutator": "loc_relative_u", "target": "(0,0,0)" } },
"if": { "map_in_city": { "mutator": "u_loc_relative", "target": "(0,0,0)" } },
"then": { "u_message": "Inside city" },
"else": { "u_message": "Outside city" }
},
{
"set_string_var": { "mutator": "loc_relative_u", "target": "(0,-1,0)" },
"set_string_var": { "mutator": "u_loc_relative", "target": "(0,-1,0)" },
"target_var": { "context_val": "loc" }
},
{
Expand All @@ -214,7 +214,7 @@
"else": { "u_message": "North furniture: Not TRANSPARENT" }
},
{
"set_string_var": { "mutator": "loc_relative_u", "target": "(1,0,0)" },
"set_string_var": { "mutator": "u_loc_relative", "target": "(1,0,0)" },
"target_var": { "context_val": "loc" }
},
{
Expand All @@ -228,7 +228,7 @@
"else": { "u_message": "East furniture: Not TRANSPARENT" }
},
{
"set_string_var": { "mutator": "loc_relative_u", "target": "(0,1,0)" },
"set_string_var": { "mutator": "u_loc_relative", "target": "(0,1,0)" },
"target_var": { "context_val": "loc" }
},
{
Expand All @@ -242,7 +242,7 @@
"else": { "u_message": "South furniture: Not TRANSPARENT" }
},
{
"set_string_var": { "mutator": "loc_relative_u", "target": "(-1,0,0)" },
"set_string_var": { "mutator": "u_loc_relative", "target": "(-1,0,0)" },
"target_var": { "context_val": "loc" }
},
{
Expand Down
20 changes: 17 additions & 3 deletions data/mods/TEST_DATA/EOC.json
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@
"type": "effect_on_condition",
"id": "EOC_try_kill",
"effect": [
{ "set_string_var": { "mutator": "loc_relative_u", "target": "(0,1,0)" }, "target_var": { "context_val": "kill_loc" } },
{ "set_string_var": { "mutator": "u_loc_relative", "target": "(0,1,0)" }, "target_var": { "context_val": "kill_loc" } },
{ "run_eoc_with": "EOC_kill", "beta_loc": { "context_val": "kill_loc" } }
]
},
Expand Down Expand Up @@ -313,7 +313,7 @@
{ "set_string_var": "fd_blood", "target_var": { "global_val": "field_id_1" } },
{ "set_string_var": "fd_blood_insect", "target_var": { "global_val": "field_id_2" } },
{
"set_string_var": { "mutator": "loc_relative_u", "target": "(0,1,0)" },
"set_string_var": { "mutator": "u_loc_relative", "target": "(0,1,0)" },
"target_var": { "global_val": "loc" }
},
{ "math": [ "key_field_strength", "=", "u_field_strength(field_id_1)" ] },
Expand Down Expand Up @@ -803,11 +803,25 @@
"type": "effect_on_condition",
"id": "EOC_map_test",
"effect": [
{ "set_string_var": { "mutator": "loc_relative_u", "target": "(10,10,0)" }, "target_var": { "context_val": "loc" } },
{ "set_string_var": { "mutator": "u_loc_relative", "target": "(10,10,0)" }, "target_var": { "context_val": "loc" } },
{ "math": [ "key_distance_loc", "=", "distance('u', _loc)" ] },
{ "math": [ "key_distance_npc", "=", "distance('u', 'npc')" ] }
]
},
{
"type": "effect_on_condition",
"id": "EOC_loc_relative_test",
"effect": [
{
"set_string_var": { "mutator": "u_loc_relative", "target": "(10,10,0)" },
"target_var": { "global_val": "map_test_loc_a" }
},
{
"set_string_var": { "mutator": "npc_loc_relative", "target": "(0,0,0)" },
"target_var": { "global_val": "map_test_loc_b" }
}
]
},
{
"type": "effect_on_condition",
"id": "EOC_martial_art_test_1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"or": [
{ "test_eoc": "EOC_CONDITION_HOMULLUS_NEAR_FACTION" },
{ "u_near_om_location": "FACTION_CAMP_ANY", "range": 2 },
{ "map_in_city": { "mutator": "loc_relative_u", "target": "(0,0,0)" } }
{ "map_in_city": { "mutator": "u_loc_relative", "target": "(0,0,0)" } }
]
},
"effect": [ { "math": [ "u_homullus_is_in_civilization", "=", "1" ] } ],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"or": [
{ "test_eoc": "EOC_CONDITION_HOMULLUS_NEAR_FACTION" },
{ "u_near_om_location": "FACTION_CAMP_ANY", "range": 2 },
{ "map_in_city": { "mutator": "loc_relative_u", "target": "(0,0,0)" } }
{ "map_in_city": { "mutator": "u_loc_relative", "target": "(0,0,0)" } }
]
},
"effect": [ { "run_eocs": "EOC_HOMULLUS_SPELL_EXPERIENCE_INCREASER_SELECTOR" } ]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
{
"or": [
{ "test_eoc": "EOC_CONDITION_HOMULLUS_NEAR_FACTION" },
{ "map_in_city": { "mutator": "loc_relative_u", "target": "(0,0,0)" } }
{ "map_in_city": { "mutator": "u_loc_relative", "target": "(0,0,0)" } }
]
}
]
Expand Down
6 changes: 3 additions & 3 deletions doc/EFFECT_ON_CONDITION.md
Original file line number Diff line number Diff line change
Expand Up @@ -1018,7 +1018,7 @@ Check the north terrain or furniture has `TRANSPARENT` flag.
"type": "effect_on_condition",
"id": "EOC_ter_furn_check",
"effect": [
{ "set_string_var": { "mutator": "loc_relative_u", "target": "(0,-1,0)" }, "target_var": { "context_val": "loc" } },
{ "set_string_var": { "mutator": "u_loc_relative", "target": "(0,-1,0)" }, "target_var": { "context_val": "loc" } },
{
"if": { "map_terrain_with_flag": "TRANSPARENT", "loc": { "context_val": "loc" } },
"then": { "u_message": "North terrain: TRANSPARENT" },
Expand Down Expand Up @@ -3624,8 +3624,8 @@ Spawn a plastic bottle on ground
"type": "effect_on_condition",
"id": "EOC_map_spawn_item",
"effect": [
{ "set_string_var": { "mutator": "loc_relative_u", "target": "(0,1,0)" }, "target_var": { "context_val": "loc" } },
{ "map_spawn_item": "bottle_plastic", "loc": { "mutator": "loc_relative_u", "target": "(0,1,0)" } }
{ "set_string_var": { "mutator": "u_loc_relative", "target": "(0,1,0)" }, "target_var": { "context_val": "loc" } },
{ "map_spawn_item": "bottle_plastic", "loc": { "mutator": "u_loc_relative", "target": "(0,1,0)" } }
]
},
```
Expand Down
2 changes: 1 addition & 1 deletion doc/NPCs.md
Original file line number Diff line number Diff line change
Expand Up @@ -1213,7 +1213,7 @@ Mutator Name | Required Keys | Description
`"ma_technique_name"` | `matec_id`: String or [variable object](#variable-object). | Returns the name of the martial arts tech with ID `matec_id`
`"ma_technique_description"` | `matec_id`: String or [variable object](#variable-object). | Returns the description of the martial arts tech with ID `matec_id`
`"valid_technique`" | `blacklist`: array of String or [variable object](#variable-object). <br/> `crit`: bool <br/> `dodge_counter`: bool <br/> `block_counter`: bool | Returns a random valid technique for the alpha talker to use against the beta talker with the provided specifications.
`"loc_relative_u"` | `target`: String or [variable object](#variable-object). | target should be a string like "(x,y,z)" where x,y,z are coordinates relative to the player. Returns the abs_ms coordinates as a string (ready to store as a location variable), in the form "(x,y,z)" of the provided point relative to the player. So `"target":"(0,1,0)"` would return the point south of the player.
`"u_/npc_loc_relative"` | `target`: String or [variable object](#variable-object). | target should be a string like "(x,y,z)" where x,y,z are coordinates relative to the player. Returns the abs_ms coordinates as a string (ready to store as a location variable), in the form "(x,y,z)" of the provided point relative to the alpha/beta talker respectively. So `"target":"(0,1,0)"` would return the point south of the talker.
`"topic_item"` | | Returns current topic_item as a string. See [Repeat Responses](#repeat-responses)

### Math
Expand Down
16 changes: 9 additions & 7 deletions src/condition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1967,17 +1967,18 @@ template<class T>
static std::function<T( const dialogue & )> get_get_str_( const JsonObject &jo,
std::function<T( const std::string & )> ret_func )
{
if( jo.get_string( "mutator" ) == "mon_faction" ) {
const std::string &mutator = jo.get_string( "mutator" );
if( mutator == "mon_faction" ) {
str_or_var mtypeid = get_str_or_var( jo.get_member( "mtype_id" ), "mtype_id" );
return [mtypeid, ret_func]( const dialogue & d ) {
return ret_func( ( static_cast<mtype_id>( mtypeid.evaluate( d ) ) )->default_faction.str() );
};
} else if( jo.get_string( "mutator" ) == "game_option" ) {
} else if( mutator == "game_option" ) {
str_or_var option = get_str_or_var( jo.get_member( "option" ), "option" );
return [option, ret_func]( const dialogue & d ) {
return ret_func( get_option<std::string>( option.evaluate( d ) ) );
};
} else if( jo.get_string( "mutator" ) == "valid_technique" ) {
} else if( mutator == "valid_technique" ) {
std::vector<str_or_var> blacklist;
if( jo.has_array( "blacklist" ) ) {
for( const JsonValue &jv : jo.get_array( "blacklist" ) ) {
Expand All @@ -1998,14 +1999,15 @@ static std::function<T( const dialogue & )> get_get_str_( const JsonObject &jo,
return ret_func( d.actor( false )->get_random_technique( *d.actor( true )->get_creature(),
crit, dodge_counter, block_counter, bl ).str() );
};
} else if( jo.get_string( "mutator" ) == "loc_relative_u" ) {
} else if( mutator == "u_loc_relative" || mutator == "npc_loc_relative" ) {
str_or_var target = get_str_or_var( jo.get_member( "target" ), "target" );
return [target, ret_func]( const dialogue & d ) {
tripoint_abs_ms char_pos = get_map().getglobal( d.actor( false )->pos() );
bool use_beta_talker = mutator == "npc_loc_relative";
return [target, use_beta_talker, ret_func]( const dialogue & d ) {
tripoint_abs_ms char_pos = get_map().getglobal( d.actor( use_beta_talker )->pos() );
tripoint_abs_ms target_pos = char_pos + tripoint::from_string( target.evaluate( d ) );
return ret_func( target_pos.to_string() );
};
} else if( jo.get_string( "mutator" ) == "topic_item" ) {
} else if( mutator == "topic_item" ) {
return [ret_func]( const dialogue & d ) {
return ret_func( d.cur_item.str() );
};
Expand Down
34 changes: 34 additions & 0 deletions tests/eoc_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ static const effect_on_condition_id
effect_on_condition_EOC_item_teleport_test( "EOC_item_teleport_test" );
static const effect_on_condition_id
effect_on_condition_EOC_jmath_test( "EOC_jmath_test" );
static const effect_on_condition_id
effect_on_condition_EOC_loc_relative_test( "EOC_loc_relative_test" );
static const effect_on_condition_id effect_on_condition_EOC_map_test( "EOC_map_test" );
static const effect_on_condition_id
effect_on_condition_EOC_martial_art_test_1( "EOC_martial_art_test_1" );
Expand Down Expand Up @@ -1280,6 +1282,38 @@ TEST_CASE( "EOC_map_test", "[eoc]" )
CHECK( globvars.get_global_value( "npctalk_var_key_distance_npc" ) == "10" );
}

TEST_CASE( "EOC_loc_relative_test", "[eoc]" )
{
global_variables &globvars = get_globals();
globvars.clear_global_values();
clear_avatar();
clear_map();

map &m = get_map();
g->place_player( tripoint_zero );

const tripoint_abs_ms start = get_avatar().get_location();
const tripoint tgt = m.getlocal( start + tripoint_north );
m.furn_set( tgt, furn_test_f_eoc );
m.furn( tgt )->examine( get_avatar(), tgt );

const tripoint target_pos = get_avatar().pos() + point_east * 10;
npc &npc_dst = spawn_npc( target_pos.xy(), "thug" );
dialogue d( get_talker_for( get_avatar() ), get_talker_for( npc_dst ) );

CHECK( effect_on_condition_EOC_loc_relative_test->activate( d ) );
tripoint_abs_ms tmp_abs_a = tripoint_abs_ms( tripoint::from_string(
globvars.get_global_value( "npctalk_var_map_test_loc_a" ) ) );
tripoint_abs_ms tmp_abs_b = tripoint_abs_ms( tripoint::from_string(
globvars.get_global_value( "npctalk_var_map_test_loc_b" ) ) );
CHECK( m.getlocal( tmp_abs_a ) == tripoint( 70, 70, 0 ) );
CHECK( m.getlocal( tmp_abs_b ) == tripoint( 70, 60, 0 ) );

globvars.clear_global_values();
clear_avatar();
clear_map();
}

TEST_CASE( "EOC_martial_art_test", "[eoc]" )
{
global_variables &globvars = get_globals();
Expand Down
Loading