Skip to content

Commit

Permalink
Add string compare condition
Browse files Browse the repository at this point in the history
  • Loading branch information
Ramza13 committed Feb 17, 2022
1 parent 97018ef commit 5a68f03
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 5 deletions.
54 changes: 53 additions & 1 deletion src/condition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,25 @@ duration_or_var get_duration_or_var( const JsonObject &jo, std::string member, b
return ret_val;
}

str_or_var get_str_or_var( const JsonValue &jv, std::string member, bool required,
std::string default_val )
{
str_or_var ret_val;
if( jv.test_string() ) {
ret_val.str_val = jv.get_string();
} else if( jv.test_object() ) {
var_info var = read_var_info( jv.get_object(), true );
ret_val.type = var.type;
ret_val.var_val = var.name;
ret_val.default_val = var.default_val;
} else if( required ) {
jv.throw_error( "No valid value for " + member );
} else {
ret_val.str_val = default_val;
}
return ret_val;
}

tripoint get_tripoint_from_var( talker *target, cata::optional<std::string> target_var,
var_type vtype, talker *var_source )
{
Expand All @@ -188,7 +207,9 @@ tripoint get_tripoint_from_var( talker *target, cata::optional<std::string> targ
var_info read_var_info( JsonObject jo, bool require_default )
{
std::string default_val;
if( jo.has_string( "default" ) ) {
if( jo.has_string( "default_str" ) ) {
default_val = jo.get_string( "default_str" );
} else if( jo.has_string( "default" ) ) {
default_val = std::to_string( to_turns<int>( read_from_json_string<time_duration>
( jo.get_member( "default" ), time_duration::units ) ) );
} else if( jo.has_int( "default" ) ) {
Expand Down Expand Up @@ -1104,6 +1125,35 @@ static tripoint get_tripoint_from_string( std::string type, T &d )
return tripoint();
}

template<class T>
void conditional_t<T>::set_compare_string( const JsonObject &jo, const std::string &member )
{
str_or_var first, second;
JsonArray objects = jo.get_array( member );
if( objects.size() != 2 ) {
jo.throw_error( "incorrect number of values. Expected 2 in " + jo.str() );
condition = []( const T & ) {
return false;
};
return;
}

if( objects.has_object( 0 ) ) {
first = get_str_or_var( objects.next(), member, true );
} else {
first.str_val = objects.next_string();
}
if( objects.has_object( 1 ) ) {
second = get_str_or_var( objects.next(), member, true );
} else {
second.str_val = objects.next_string();
}

condition = [first, second]( const T & d ) {
return first.evaluate( d.actor( first.is_npc() ) ) == second.evaluate( d.actor( second.is_npc() ) );
};
}

template<class T>
void conditional_t<T>::set_compare_int( const JsonObject &jo, const std::string &member )
{
Expand Down Expand Up @@ -1938,6 +1988,8 @@ conditional_t<T>::conditional_t( const JsonObject &jo )
set_has_faction_trust( jo, "u_has_faction_trust" );
} else if( jo.has_member( "compare_int" ) ) {
set_compare_int( jo, "compare_int" );
} else if( jo.has_member( "compare_string" ) ) {
set_compare_string( jo, "compare_string" );
} else {
for( const std::string &sub_member : dialogue_data::simple_string_conds ) {
if( jo.has_string( sub_member ) ) {
Expand Down
5 changes: 4 additions & 1 deletion src/condition.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,12 @@ const std::unordered_set<std::string> complex_conds = { {
"is_temperature", "is_windpower", "is_humidity", "is_pressure", "u_is_height", "npc_is_height",
"u_has_worn_with_flag", "npc_has_worn_with_flag", "u_has_wielded_with_flag", "npc_has_wielded_with_flag",
"u_has_pain", "npc_has_pain", "u_has_power", "npc_has_power", "u_has_focus", "npc_has_focus", "u_has_morale",
"npc_has_morale", "u_is_on_terrain", "npc_is_on_terrain", "u_is_in_field", "npc_is_in_field", "compare_int"
"npc_has_morale", "u_is_on_terrain", "npc_is_on_terrain", "u_is_in_field", "npc_is_in_field", "compare_int", "compare_string"
}
};
} // namespace dialogue_data
str_or_var get_str_or_var( const JsonValue &jv, std::string member, bool required = true,
std::string default_val = "" );
int_or_var get_int_or_var( const JsonObject &jo, std::string member, bool required = true,
int default_val = 0 );
int_or_var_part get_int_or_var_part( const JsonValue &jv, std::string member, bool required = true,
Expand Down Expand Up @@ -171,6 +173,7 @@ struct conditional_t {
void set_u_know_recipe( const JsonObject &jo, const std::string &member );
void set_mission_has_generic_rewards();
void set_can_see( bool is_npc = false );
void set_compare_string( const JsonObject &jo, const std::string &member );
void set_compare_int( const JsonObject &jo, const std::string &member );
static std::function<int( const T & )> get_get_int( const JsonObject &jo );
static std::function<int( const T & )> get_get_int( std::string value, const JsonObject &jo );
Expand Down
40 changes: 37 additions & 3 deletions src/debug_menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ std::string enum_to_string<debug_menu::debug_menu_index>( debug_menu::debug_menu
case debug_menu::debug_menu_index::TEST_WEATHER: return "TEST_WEATHER";
case debug_menu::debug_menu_index::WRITE_GLOBAL_EOCS: return "WRITE_GLOBAL_EOCS";
case debug_menu::debug_menu_index::WRITE_GLOBAL_VARS: return "WRITE_GLOBAL_VARS";
case debug_menu::debug_menu_index::EDIT_GLOBAL_VARS: return "SET_GLOBAL_VARS";
case debug_menu::debug_menu_index::SAVE_SCREENSHOT: return "SAVE_SCREENSHOT";
case debug_menu::debug_menu_index::GAME_REPORT: return "GAME_REPORT";
case debug_menu::debug_menu_index::DISPLAY_SCENTS_LOCAL: return "DISPLAY_SCENTS_LOCAL";
Expand All @@ -214,7 +215,7 @@ std::string enum_to_string<debug_menu::debug_menu_index>( debug_menu::debug_menu
case debug_menu::debug_menu_index::EDIT_CAMP_LARDER: return "EDIT_CAMP_LARDER";
case debug_menu::debug_menu_index::VEHICLE_BATTERY_CHARGE: return "VEHICLE_BATTERY_CHARGE";
case debug_menu::debug_menu_index::GENERATE_EFFECT_LIST: return "GENERATE_EFFECT_LIST";
case debug_menu::debug_menu_index::ACTIVATE_EOC: return "ACTIVATE_EOC";
case debug_menu::debug_menu_index::ACTIVATE_EOC: return "ACTIVATE_EOC";
// *INDENT-ON*
case debug_menu::debug_menu_index::last:
break;
Expand Down Expand Up @@ -316,7 +317,8 @@ static int info_uilist( bool display_all_entries = true )
{ uilist_entry( debug_menu_index::PRINT_NPC_MAGIC, true, 'M', _( "Print NPC magic info to console" ) ) },
{ uilist_entry( debug_menu_index::TEST_WEATHER, true, 'W', _( "Test weather" ) ) },
{ uilist_entry( debug_menu_index::WRITE_GLOBAL_EOCS, true, 'C', _( "Write global effect_on_condition(s) to eocs.output" ) ) },
{ uilist_entry( debug_menu_index::WRITE_GLOBAL_VARS, true, 'G', _( "Write global vars(s) to var_list.output" ) ) },
{ uilist_entry( debug_menu_index::WRITE_GLOBAL_VARS, true, 'G', _( "Write global var(s) to var_list.output" ) ) },
{ uilist_entry( debug_menu_index::EDIT_GLOBAL_VARS, true, 'e', _( "Edit global var(s)" ) ) },
{ uilist_entry( debug_menu_index::TEST_MAP_EXTRA_DISTRIBUTION, true, 'e', _( "Test map extra list" ) ) },
{ uilist_entry( debug_menu_index::GENERATE_EFFECT_LIST, true, 'L', _( "Generate effect list" ) ) },
};
Expand Down Expand Up @@ -1634,7 +1636,7 @@ static void character_edit_menu()
D_DESC, D_SKILLS, D_THEORY, D_PROF, D_STATS, D_SPELLS, D_ITEMS, D_DELETE_ITEMS, D_ITEM_WORN,
D_HP, D_STAMINA, D_MORALE, D_PAIN, D_NEEDS, D_HEALTHY, D_STATUS, D_MISSION_ADD, D_MISSION_EDIT,
D_TELE, D_MUTATE, D_CLASS, D_ATTITUDE, D_OPINION, D_ADD_EFFECT, D_ASTHMA, D_PRINT_VARS,
D_WRITE_EOCS, D_KILL_XP
D_WRITE_EOCS, D_KILL_XP, D_EDIT_VARS
};
nmenu.addentry( D_DESC, true, 'D', "%s",
_( "Edit [D]escription - Name, Age, Height or Blood type" ) );
Expand Down Expand Up @@ -1667,6 +1669,9 @@ static void character_edit_menu()
nmenu.addentry( D_PRINT_VARS, true, 'V', "%s", _( "Print [V]ars to file" ) );
nmenu.addentry( D_WRITE_EOCS, true, 'w', "%s",
_( "[w]rite effect_on_condition(s) to eocs.output." ) );
nmenu.addentry( D_EDIT_VARS, true, 'v', "%s",
_( "Edit [v]ars" ) );

if( you.is_npc() ) {
nmenu.addentry( D_MISSION_ADD, true, 'm', "%s", _( "Add [m]ission" ) );
nmenu.addentry( D_CLASS, true, 'c', "%s", _( "Randomize with [c]lass" ) );
Expand Down Expand Up @@ -1897,6 +1902,19 @@ static void character_edit_menu()
effect_on_conditions::write_eocs_to_file( you );
popup( _( "effect_on_condition list written to eocs.output" ) );
}
case D_EDIT_VARS: {
std::string key, value;
string_input_popup popup_key, popup_val;
popup_key
.title( _( "Key" ) )
.width( 85 )
.edit( key );
popup_val
.title( _( "Value" ) )
.width( 85 )
.edit( value );
you.set_value( "npctalk_var_" + key, value );
}
break;
}
}
Expand Down Expand Up @@ -2915,6 +2933,22 @@ void debug()
}
break;

case debug_menu_index::EDIT_GLOBAL_VARS: {
std::string key, value;
string_input_popup popup_key, popup_val;
popup_key
.title( _( "Key" ) )
.width( 85 )
.edit( key );
popup_val
.title( _( "Value" ) )
.width( 85 )
.edit( value );
global_variables &globvars = get_globals();
globvars.set_global_value( "npctalk_var_" + key, value );
}
break;

case debug_menu_index::SAVE_SCREENSHOT:
g->queue_screenshot = true;
break;
Expand Down
1 change: 1 addition & 0 deletions src/debug_menu.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ enum class debug_menu_index : int {
EDIT_CAMP_LARDER,
WRITE_GLOBAL_EOCS,
WRITE_GLOBAL_VARS,
EDIT_GLOBAL_VARS,
ACTIVATE_EOC,
last
};
Expand Down
24 changes: 24 additions & 0 deletions src/dialogue.h
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,30 @@ static std::string read_var_value( var_type type, std::string name, talker *talk
return "";
}

struct str_or_var {
cata::optional<std::string> str_val;
cata::optional<std::string> var_val;
cata::optional<std::string> default_val;
var_type type = var_type::u;
bool is_npc() const {
return type == var_type::npc;
}
std::string evaluate( talker *talk ) const {
if( str_val.has_value() ) {
return str_val.value();
} else if( var_val.has_value() ) {
std::string val = read_var_value( type, var_val.value(), talk );
if( !val.empty() ) {
return std::string( val );
}
return default_val.value();
} else {
debugmsg( "No valid value." );
return "";
}
}
};

struct int_or_var_part {
cata::optional<int> int_val;
cata::optional<std::string> var_val;
Expand Down

0 comments on commit 5a68f03

Please sign in to comment.