Skip to content

Commit

Permalink
New dialog contidions "u_using_martial_art", "map_in_city" (CleverRav…
Browse files Browse the repository at this point in the history
…en#68905)

* u_using_martial_art map_in_city

* doc and clang error

* fix test

* clang error
  • Loading branch information
lispcoc authored Oct 27, 2023
1 parent 4592e87 commit 304311d
Show file tree
Hide file tree
Showing 10 changed files with 129 additions and 1 deletion.
10 changes: 9 additions & 1 deletion data/json/effects_on_condition/example_eocs.json
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,15 @@
"type": "effect_on_condition",
"id": "EOC_map_condition_test",
"effect": [
{ "set_string_var": { "mutator": "loc_relative_u", "target": "(0,-1,0)" }, "target_var": { "context_val": "loc" } },
{
"if": { "map_in_city": { "mutator": "loc_relative_u", "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)" },
"target_var": { "context_val": "loc" }
},
{
"if": { "map_terrain_with_flag": "TRANSPARENT", "loc": { "context_val": "loc" } },
"then": { "u_message": "North terrain: TRANSPARENT" },
Expand Down
15 changes: 15 additions & 0 deletions data/mods/TEST_DATA/EOC.json
Original file line number Diff line number Diff line change
Expand Up @@ -613,5 +613,20 @@
"condition": { "u_know_recipe": "cattail_jelly" },
"effect": { "u_forget_recipe": "cattail_jelly" },
"false_effect": { "math": [ "fail_var", "=", "1" ] }
},
{
"type": "effect_on_condition",
"id": "EOC_martial_art_test_1",
"//": "tests adding a martial art to the player",
"condition": { "not": { "u_has_martial_art": "style_aikido" } },
"effect": { "u_learn_martial_art": "style_aikido" },
"false_effect": { "math": [ "fail_var", "=", "1" ] }
},
{
"type": "effect_on_condition",
"id": "EOC_martial_art_test_2",
"//": "tests forgetting a martial art from the player",
"condition": { "not": { "u_using_martial_art": "style_aikido" } },
"effect": { "u_forget_martial_art": "style_aikido" }
}
]
34 changes: 34 additions & 0 deletions doc/EFFECT_ON_CONDITION.md
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,21 @@ Checks do alpha talker has `FEATHERS` mutation
{ "u_has_martial_art": "style_aikido" }
```

### `u_using_martial_art``npc_using_martial_art`
- type: string or [variable object](##variable-object)
- return true if alpha or beta talker using the martial art

#### Valid talkers:

| Avatar | Character | NPC | Monster | Furniture | Item |
| ------ | --------- | --------- | ---- | ------- | --- |
| ✔️ | ✔️ | ✔️ ||||

#### Examples
```json
{ "u_using_martial_art": "style_aikido" }
```

### `u_has_flag``npc_has_flag`
- type: string or [variable object](##variable-object)
- return true if alpha or beta talker has specific flag; special flag `MUTATION_THRESHOLD` can be used to check do alpha talker has any mutant threshold; for monsters both json flags (applied by effects) and monster flags can be checked
Expand Down Expand Up @@ -927,6 +942,25 @@ Check the north terrain or furniture has `TRANSPARENT` flag.
},
```

### `map_in_city`
- type: location string or [variable object](##variable-object)
- return true if the location is in a city

#### Valid talkers:

No talker is needed.

#### Examples
Check the location is in a city.
```json
{ "u_location_variable": { "context_val": "loc" } },
{
"if": { "map_in_city": { "context_val": "loc" } },
"then": { "u_message": "Inside city" },
"else": { "u_message": "Outside city" }
},
```

# Reusable EOCs:
The code base supports the use of reusable EOCs, you can use these to get guaranteed effects by passing in specific variables. The codebase supports the following:

Expand Down
3 changes: 3 additions & 0 deletions src/character_martial_arts.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ class character_martial_arts

std::string enumerate_known_styles( const itype_id &weap ) const;
std::string selected_style_name( const Character &owner ) const;
const matype_id &selected_style() const {
return style_selected;
}
};

#endif // CATA_SRC_CHARACTER_MARTIAL_ARTS_H
22 changes: 22 additions & 0 deletions src/condition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1412,6 +1412,17 @@ void conditional_t::set_map_ter_furn_with_flag( const JsonObject &jo, std::strin
};
}

void conditional_t::set_map_in_city( const JsonObject &jo, std::string_view member )
{
str_or_var target = get_str_or_var( jo.get_member( member ), member, true );
condition = [target]( dialogue const & d ) {
tripoint_abs_ms target_pos = tripoint_abs_ms( tripoint::from_string( target.evaluate( d ) ) );
city_reference c = overmap_buffer.closest_city( project_to<coords::sm>( target_pos ) );
c.distance = rl_dist( c.abs_sm_pos, project_to<coords::sm>( target_pos ) );
return c && c.get_distance_from_bounds() <= 0;
};
}

void conditional_t::set_mod_is_loaded( const JsonObject &jo, std::string_view member )
{
str_or_var compared_mod = get_str_or_var( jo.get_member( member ), member, true );
Expand Down Expand Up @@ -3264,13 +3275,23 @@ void conditional_t::set_has_move_mode( const JsonObject &jo, std::string_view me
};
}

void conditional_t::set_using_martial_art( const JsonObject &jo, std::string_view member,
bool is_npc )
{
str_or_var style_to_check = get_str_or_var( jo.get_member( member ), member, true );
condition = [style_to_check, is_npc]( dialogue const & d ) {
return d.actor( is_npc )->using_martial_art( matype_id( style_to_check.evaluate( d ) ) );
};
}

static const
std::vector<condition_parser>
parsers = {
{"u_has_any_trait", "npc_has_any_trait", jarg::array, &conditional_t::set_has_any_trait },
{"u_has_trait", "npc_has_trait", jarg::member, &conditional_t::set_has_trait },
{"u_has_visible_trait", "npc_has_visible_trait", jarg::member, &conditional_t::set_has_visible_trait },
{"u_has_martial_art", "npc_has_martial_art", jarg::member, &conditional_t::set_has_martial_art },
{"u_using_martial_art", "npc_using_martial_art", jarg::member, &conditional_t::set_using_martial_art },
{"u_has_flag", "npc_has_flag", jarg::member, &conditional_t::set_has_flag },
{"u_has_species", "npc_has_species", jarg::member, &conditional_t::set_has_species },
{"u_bodytype", "npc_bodytype", jarg::member, &conditional_t::set_bodytype },
Expand Down Expand Up @@ -3331,6 +3352,7 @@ parsers = {
{"is_weather", jarg::member, &conditional_t::set_is_weather },
{"map_terrain_with_flag", jarg::member, &conditional_t::set_map_ter_furn_with_flag },
{"map_furniture_with_flag", jarg::member, &conditional_t::set_map_ter_furn_with_flag },
{"map_in_city", jarg::member, &conditional_t::set_map_in_city },
{"mod_is_loaded", jarg::member, &conditional_t::set_mod_is_loaded },
{"u_has_faction_trust", jarg::member | jarg::array, &conditional_t::set_has_faction_trust },
{"compare_int", jarg::member, &conditional_t::set_compare_num },
Expand Down
2 changes: 2 additions & 0 deletions src/condition.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ struct conditional_t {
void set_at_om_location( const JsonObject &jo, std::string_view member, bool is_npc = false );
void set_near_om_location( const JsonObject &jo, std::string_view member, bool is_npc = false );
void set_has_move_mode( const JsonObject &jo, std::string_view member, bool is_npc = false );
void set_using_martial_art( const JsonObject &jo, std::string_view member, bool is_npc = false );
void set_npc_role_nearby( const JsonObject &jo, std::string_view member );
void set_npc_allies( const JsonObject &jo, std::string_view member );
void set_npc_allies_global( const JsonObject &jo, std::string_view member );
Expand All @@ -147,6 +148,7 @@ struct conditional_t {
void set_is_season( const JsonObject &jo, std::string_view member );
void set_is_weather( const JsonObject &jo, std::string_view member );
void set_map_ter_furn_with_flag( const JsonObject &jo, std::string_view member );
void set_map_in_city( const JsonObject &jo, std::string_view member );
void set_mod_is_loaded( const JsonObject &jo, std::string_view member );
void set_mission_goal( const JsonObject &jo, std::string_view member, bool is_npc );
void set_has_faction_trust( const JsonObject &jo, std::string_view member );
Expand Down
3 changes: 3 additions & 0 deletions src/talker.h
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,9 @@ class talker
virtual bool knows_martial_art( const matype_id & ) const {
return false;
}
virtual bool using_martial_art( const matype_id & ) const {
return false;
}
};
template <class T, class B = talker>
class talker_cloner : public B
Expand Down
5 changes: 5 additions & 0 deletions src/talker_character.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -934,6 +934,11 @@ bool talker_character_const::knows_martial_art( const matype_id &id ) const
return me_chr_const->martial_arts_data->has_martialart( id );
}

bool talker_character_const::using_martial_art( const matype_id &id ) const
{
return me_chr_const->martial_arts_data->selected_style() == id;
}

void talker_character::add_bionic( const bionic_id &new_bionic )
{
me_chr->add_bionic( new_bionic );
Expand Down
1 change: 1 addition & 0 deletions src/talker_character.h
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ class talker_character_const: public talker_cloner<talker_character_const>
units::temperature get_body_temp() const override;
units::temperature_delta get_body_temp_delta() const override;
bool knows_martial_art( const matype_id &id ) const override;
bool using_martial_art( const matype_id &id ) const override;
protected:
talker_character_const() = default;
const Character *me_chr_const;
Expand Down
35 changes: 35 additions & 0 deletions tests/eoc_test.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "avatar.h"
#include "calendar.h"
#include "cata_catch.h"
#include "character_martial_arts.h"
#include "effect_on_condition.h"
#include "game.h"
#include "map_helpers.h"
Expand Down Expand Up @@ -36,6 +37,10 @@ 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_martial_art_test_1( "EOC_martial_art_test_1" );
static const effect_on_condition_id
effect_on_condition_EOC_martial_art_test_2( "EOC_martial_art_test_2" );
static const effect_on_condition_id
effect_on_condition_EOC_math_armor( "EOC_math_armor" );
static const effect_on_condition_id
effect_on_condition_EOC_math_diag_assign( "EOC_math_diag_assign" );
Expand Down Expand Up @@ -92,6 +97,9 @@ static const itype_id itype_backpack( "backpack" );
static const itype_id itype_sword_wood( "sword_wood" );
static const itype_id itype_test_knife_combat( "test_knife_combat" );

static const matype_id style_aikido( "style_aikido" );
static const matype_id style_none( "style_none" );

static const mtype_id mon_zombie( "mon_zombie" );
static const mtype_id mon_zombie_smoker( "mon_zombie_smoker" );
static const mtype_id mon_zombie_tough( "mon_zombie_tough" );
Expand Down Expand Up @@ -990,3 +998,30 @@ TEST_CASE( "EOC_map_test", "[eoc]" )
CHECK( globvars.get_global_value( "npctalk_var_this" ) == "test_f_eoc" );
CHECK( globvars.get_global_value( "npctalk_var_pos" ) == m.getglobal( tgt ).to_string() );
}

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

dialogue d( get_talker_for( get_avatar() ), std::make_unique<talker>() );

REQUIRE_FALSE( get_avatar().has_martialart( style_aikido ) );
REQUIRE( globvars.get_global_value( "fail_var" ).empty() );

CHECK( effect_on_condition_EOC_martial_art_test_1->activate( d ) );
CHECK( globvars.get_global_value( "fail_var" ).empty() );
CHECK( get_avatar().has_martialart( style_aikido ) );

get_avatar().martial_arts_data->set_style( style_aikido );

CHECK_FALSE( effect_on_condition_EOC_martial_art_test_2->activate( d ) );
CHECK( get_avatar().has_martialart( style_aikido ) );

get_avatar().martial_arts_data->set_style( style_none );

CHECK( effect_on_condition_EOC_martial_art_test_2->activate( d ) );
CHECK( !get_avatar().has_martialart( style_aikido ) );
}

0 comments on commit 304311d

Please sign in to comment.