Skip to content

Commit

Permalink
get rid of npctalk_var_ prefix for eoc variables (#77515)
Browse files Browse the repository at this point in the history
* get rid of npctalk_var_ prefix for eoc variables

* cleanup after #77055

* deprecate more of type and context

* migrate item vars, make migration happen only once

* doc updates

* migrate u/npc/monster variables

* fix context var migration

* remove `substr( 12 )` in math parser

* move migration to a single function where possible

* no auto

* please clang

* Please clang more

* fix submap load test

* clean up after #77656

* cleanups after #77520
  • Loading branch information
GuardianDll authored Nov 17, 2024
1 parent eb8aae4 commit cf579ac
Show file tree
Hide file tree
Showing 40 changed files with 454 additions and 438 deletions.
23 changes: 4 additions & 19 deletions data/json/items/tool/science.json
Original file line number Diff line number Diff line change
Expand Up @@ -1317,25 +1317,10 @@
],
"name": { "str_sp": "Mi-go Biotech" },
"conditional_names": [
{ "type": "VAR", "condition": "npctalk_var_mbt_f_function", "value": "morale", "name": { "str_sp": "%s (happy)" } },
{
"type": "VAR",
"condition": "npctalk_var_mbt_f_function",
"value": "focus",
"name": { "str_sp": "%s (focus)" }
},
{
"type": "VAR",
"condition": "npctalk_var_mbt_f_function",
"value": "pain",
"name": { "str_sp": "%s (pain relief)" }
},
{
"type": "VAR",
"condition": "npctalk_var_mbt_f_function",
"value": "sleepiness",
"name": { "str_sp": "%s (wake up)" }
}
{ "type": "VAR", "condition": "mbt_f_function", "value": "morale", "name": { "str_sp": "%s (happy)" } },
{ "type": "VAR", "condition": "mbt_f_function", "value": "focus", "name": { "str_sp": "%s (focus)" } },
{ "type": "VAR", "condition": "mbt_f_function", "value": "pain", "name": { "str_sp": "%s (pain relief)" } },
{ "type": "VAR", "condition": "mbt_f_function", "value": "sleepiness", "name": { "str_sp": "%s (wake up)" } }
],
"description": "A piece of mi-go biotechnology.",
"material": [ "alien_resin" ],
Expand Down
4 changes: 2 additions & 2 deletions data/json/npcs/isolated_road/isolated_road_jay_convert.json
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,8 @@
},
{
"type": "var_migration",
"from": "npctalk_var_number_artisans_gunsmith_ammo_ammount",
"to": "npctalk_var_number_artisans_gunsmith_ammo_amount"
"from": "number_artisans_gunsmith_ammo_ammount",
"to": "number_artisans_gunsmith_ammo_amount"
},
{
"id": "TALK_GUNSMITH_BULLET_PICKUP",
Expand Down
4 changes: 2 additions & 2 deletions doc/JSON_INFO.md
Original file line number Diff line number Diff line change
Expand Up @@ -3973,7 +3973,7 @@ The `conditional_names` field allows defining alternate names for items that wil
},
{
"type": "VAR",
"condition": "npctalk_var_DISPLAY_NAME_MORALE",
"condition": "DISPLAY_NAME_MORALE",
"name": { "str_sp": "%s (morale)" },
"value" : "true"
},
Expand All @@ -3992,7 +3992,7 @@ You can list as many conditional names for a given item as you want. Each condit
- `COMPONENT_ID` Similar to `COMPONENT_ID_SUBSTRING`, but search the exact component match
- `FLAG` which checks if an item has the specified flag (exact match).
- `VITAMIN` which checks if an item has the specified vitamin (exact match).
- `VAR` which checks if an item has a variable with the given name (exact match) and value = `value`. Variables set with effect_on_conditions will have `npctalk_var_` in front of their name. So a variable created with: `"npc_add_var": "MORALE", "value": "Felt Great" }` would be named: `npctalk_var_MORALE`.
- `VAR` which checks if an item has a variable with the given name (exact match) and value = `value`.
- `SNIPPET_ID`which checks if an item has a snippet id variable set by an effect_on_condition with the given name (exact match) and snippets id = `value`.
2. The condition you want to look for.
3. The name to use if a match is found. Follows all the rules of a standard `name` field, with valid keys being `str`, `str_pl`, and `ctxt`. You may use %s here, which will be replaced by the name of the item. Conditional names defined prior to this one are taken into account.
Expand Down
2 changes: 1 addition & 1 deletion src/bionics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -783,7 +783,7 @@ bool Character::activate_bionic( bionic &bio, bool eff_only, bool *close_bionics

for( const effect_on_condition_id &eoc : bio.id->activated_eocs ) {
dialogue d( get_talker_for( *this ), nullptr );
write_var_value( var_type::context, "npctalk_var_act_cost", &d,
write_var_value( var_type::context, "act_cost", &d,
units::to_millijoule( bio.info().power_activate ) );
if( eoc->type == eoc_type::ACTIVATION ) {
eoc->activate( d );
Expand Down
6 changes: 3 additions & 3 deletions src/character.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -516,8 +516,8 @@ void Character::queue_effect( const std::string &name, const time_duration &dela
const time_duration &effect_duration )
{
std::unordered_map<std::string, std::string> ctx = {
{ "npctalk_var_effect", name },
{ "npctalk_var_duration", std::to_string( to_turns<int>( effect_duration ) ) }
{ "effect", name },
{ "duration", std::to_string( to_turns<int>( effect_duration ) ) }
};

effect_on_conditions::queue_effect_on_condition( delay, effect_on_condition_add_effect, *this,
Expand All @@ -529,7 +529,7 @@ int Character::count_queued_effects( const std::string &effect ) const
return std::count_if( queued_effect_on_conditions.list.begin(),
queued_effect_on_conditions.list.end(), [&effect]( const queued_eoc & eoc ) {
return eoc.eoc == effect_on_condition_add_effect &&
eoc.context.at( "npctalk_var_effect" ) == effect;
eoc.context.at( "effect" ) == effect;
} );
}

Expand Down
13 changes: 3 additions & 10 deletions src/condition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,8 @@ std::string get_talk_varname( const JsonObject &jo, std::string_view member,
jo.throw_error( "invalid " + std::string( member ) + " condition in " + jo.str() );
}
const std::string &var_basename = jo.get_string( std::string( member ) );
const std::string &type_var = jo.get_string( "type", "" );
const std::string &var_context = jo.get_string( "context", "" );
default_val = get_dbl_or_var( jo, "default", false );
return "npctalk_var" + ( type_var.empty() ? "" : "_" + type_var ) + ( var_context.empty() ? "" : "_"
+ var_context ) + "_" + var_basename;
return var_basename;
}

std::string get_talk_var_basename( const JsonObject &jo, std::string_view member,
Expand Down Expand Up @@ -432,11 +429,7 @@ static abstract_var_info<T> abstract_read_var_info( const JsonObject &jo )
}

if( jo.has_string( "var_name" ) ) {
const std::string &type_var = jo.get_string( "type", "" );
const std::string &var_context = jo.get_string( "context", "" );
name = "npctalk_var_" + type_var + ( type_var.empty() ? "" : "_" ) + var_context +
( var_context.empty() ? "" : "_" )
+ jo.get_string( "var_name" );
name = jo.get_string( "var_name" );
}
if( jo.has_member( "u_val" ) ) {
type = var_type::u;
Expand Down Expand Up @@ -1165,7 +1158,7 @@ conditional_t::func f_expects_vars( const JsonObject &jo, std::string_view membe
return [to_check]( const_dialogue const & d ) {
std::string missing_variables;
for( const str_or_var &val : to_check ) {
if( d.get_context().find( "npctalk_var_" + val.evaluate( d ) ) == d.get_context().end() ) {
if( d.get_context().find( val.evaluate( d ) ) == d.get_context().end() ) {
missing_variables += val.evaluate( d ) + ", ";
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/damage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -437,9 +437,9 @@ void damage_type::ondamage_effects( Creature *source, Creature *target, bodypart
dialogue d( source == nullptr ? nullptr : get_talker_for( source ),
target == nullptr ? nullptr : get_talker_for( target ) );

d.set_value( "npctalk_var_damage_taken", std::to_string( damage_taken ) );
d.set_value( "npctalk_var_total_damage", std::to_string( total_damage ) );
d.set_value( "npctalk_var_bp", bp.str() );
d.set_value( "damage_taken", std::to_string( damage_taken ) );
d.set_value( "total_damage", std::to_string( total_damage ) );
d.set_value( "bp", bp.str() );

if( eoc->type == eoc_type::ACTIVATION ) {
eoc->activate( d );
Expand Down
10 changes: 4 additions & 6 deletions src/debug_menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -614,11 +614,10 @@ static void edit_global_npctalk_vars()
std::string key;
string_input_popup popup_key;
popup_key
//~This is the title for an input window, where strings like npctalk_var_my_variable are concatenated. The trailing "npctalk_var_" is intended to show that their entry is automatically prepended with that. e.g. if they type "cigar" the resulting var's string is "npctalk_var_cigar"
.title( _( "Key\n npctalk_var_" ) )
.title( _( "Key\n" ) )
.width( 85 )
.edit( key );
globvars.set_global_value( "npctalk_var_" + key, query_npctalkvar_new_value() );
globvars.set_global_value( key, query_npctalkvar_new_value() );
} else if( selected_globvar > 0 && selected_globvar <= static_cast<int>( keymap_index.size() ) ) {
globvars.set_global_value( keymap_index[selected_globvar], query_npctalkvar_new_value() );
}
Expand Down Expand Up @@ -655,11 +654,10 @@ static void edit_character_npctalk_vars( Character &you )
std::string key;
string_input_popup popup_key;
popup_key
//~This is the title for an input window, where strings like npctalk_var_my_variable are concatenated. The trailing "npctalk_var_" is intended to show that their entry is automatically prepended with that. e.g. if they type "cigar" the resulting var's string is "npctalk_var_cigar"
.title( _( "Key\n npctalk_var_" ) )
.title( _( "Key\n" ) )
.width( 85 )
.edit( key );
you.set_value( "npctalk_var_" + key, query_npctalkvar_new_value() );
you.set_value( key, query_npctalkvar_new_value() );
} else if( selected_globvar > 0 && selected_globvar <= static_cast<int>( keymap_index.size() ) ) {
you.set_value( keymap_index[selected_globvar], query_npctalkvar_new_value() );
}
Expand Down
14 changes: 1 addition & 13 deletions src/dialogue_helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ var_info process_variable( const std::string &type )
ret_str = type.substr( 1, type.size() - 1 );
}

return var_info( vt, "npctalk_var_" + ret_str );
return var_info( vt, ret_str );
}

template<>
Expand All @@ -109,9 +109,6 @@ std::string str_or_var::evaluate( const_dialogue const &d ) const
return default_val.value();
}
std::string var_name = var_val.value().name;
if( var_name.find( "npctalk_var" ) != std::string::npos ) {
var_name = var_name.substr( 12 );
}
debugmsg( "No default value provided for str_or_var_part while encountering unused "
"variable %s. Add a \"default_str\" member to prevent this. %s",
var_name, d.get_callstack() );
Expand Down Expand Up @@ -139,9 +136,6 @@ std::string translation_or_var::evaluate( const_dialogue const &d ) const
return default_val.value().translated();
}
std::string var_name = var_val.value().name;
if( var_name.find( "npctalk_var" ) != std::string::npos ) {
var_name = var_name.substr( 12 );
}
debugmsg( "No default value provided for str_or_var_part while encountering unused "
"variable %s. Add a \"default_str\" member to prevent this. %s",
var_name, d.get_callstack() );
Expand Down Expand Up @@ -172,9 +166,6 @@ double dbl_or_var_part::evaluate( const_dialogue const &d ) const
return default_val.value();
}
std::string var_name = var_val.value().name;
if( var_name.find( "npctalk_var" ) != std::string::npos ) {
var_name = var_name.substr( 12 );
}
debugmsg( "No default value provided for dbl_or_var_part while encountering unused "
"variable %s. Add a \"default\" member to prevent this. %s",
var_name, d.get_callstack() );
Expand Down Expand Up @@ -212,9 +203,6 @@ time_duration duration_or_var_part::evaluate( const_dialogue const &d ) const
return default_val.value();
}
std::string var_name = var_val.value().name;
if( var_name.find( "npctalk_var" ) != std::string::npos ) {
var_name = var_name.substr( 12 );
}
debugmsg( "No default value provided for duration_or_var_part while encountering unused "
"variable %s. Add a \"default\" member to prevent this. %s",
var_name, d.get_callstack() );
Expand Down
2 changes: 1 addition & 1 deletion src/effect_on_condition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,7 @@ void eoc_events::notify( const cata::event &e, std::unique_ptr<talker> alpha,
dialogue d;
std::unordered_map<std::string, std::string> context;
for( const auto &val : e.data() ) {
context["npctalk_var_" + val.first] = val.second.get_string();
context[val.first] = val.second.get_string();
}

// if we have an NPC to trigger this event for, do so,
Expand Down
4 changes: 2 additions & 2 deletions src/faction_camp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,10 +203,10 @@ static const std::string camp_om_fortifications_spiked_trench_parameter =
faction_wall_level_n_1_string;

static const std::string var_time_between_succession =
"npctalk_var_time_between_succession";
"time_between_succession";

static const std::string var_timer_time_of_last_succession =
"npctalk_var_timer_time_of_last_succession";
"timer_time_of_last_succession";

// These strings are matched against recipe group 'building_type'. Definite candidates for JSON definitions of
// the various UI strings corresponding to these groups.
Expand Down
23 changes: 23 additions & 0 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -870,6 +870,29 @@ void game::load_map( const tripoint_abs_sm &pos_sm,
m.load( pos_sm, true, pump_events );
}

void game::legacy_migrate_npctalk_var_prefix( std::unordered_map<std::string, std::string>
map_of_vars )
{
// migrate existing variables with npctalk_var prefix to no prefix (npctalk_var_foo to just foo)
// remove after 0.J

if( savegame_loading_version >= 35 ) {
return;
}

const std::string prefix = "npctalk_var_";
for( auto i = map_of_vars.begin(); i != map_of_vars.end(); ) {
if( i->first.rfind( prefix, 0 ) == 0 ) {
auto extracted = map_of_vars.extract( i++ );
std::string new_key = extracted.key().substr( prefix.size() );
extracted.key() = new_key;
map_of_vars.insert( std::move( extracted ) );
} else {
++i;
}
}
}

// Set up all default values for a new game
bool game::start_game()
{
Expand Down
3 changes: 3 additions & 0 deletions src/game.h
Original file line number Diff line number Diff line change
Expand Up @@ -735,6 +735,9 @@ class game
* disabled).
*/
void load_map( const tripoint_abs_sm &pos_sm, bool pump_events = false );
// Removes legacy npctalk_var_ prefix from older versions of the game. Should be removed after 0.J
static void legacy_migrate_npctalk_var_prefix( std::unordered_map<std::string, std::string>
map_of_vars );
/**
* The overmap which contains the center submap of the reality bubble.
*/
Expand Down
4 changes: 2 additions & 2 deletions src/iexamine_actors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,8 @@ std::unique_ptr<iexamine_actor> cardreader_examine_actor::clone() const
void eoc_examine_actor::call( Character &you, const tripoint_bub_ms &examp ) const
{
dialogue d( get_talker_for( you ), nullptr );
d.set_value( "npctalk_var_this", get_map().furn( examp ).id().str() );
d.set_value( "npctalk_var_pos", get_map().getglobal( examp ).to_string() );
d.set_value( "this", get_map().furn( examp ).id().str() );
d.set_value( "pos", get_map().getglobal( examp ).to_string() );
for( const effect_on_condition_id &eoc : eocs ) {
eoc->activate( d );
}
Expand Down
10 changes: 5 additions & 5 deletions src/item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -339,8 +339,8 @@ item::item( const itype *type, time_point turn, int qty ) : type( type ), bday(

if( has_flag( flag_ENERGY_SHIELD ) ) {
const islot_armor *sh = find_armor_data();
set_var( "npctalk_var_MAX_ENERGY_SHIELD_HP", sh->max_energy_shield_hp );
set_var( "npctalk_var_ENERGY_SHIELD_HP", sh->max_energy_shield_hp );
set_var( "MAX_ENERGY_SHIELD_HP", sh->max_energy_shield_hp );
set_var( "ENERGY_SHIELD_HP", sh->max_energy_shield_hp );
}

if( has_flag( flag_COLLAPSE_CONTENTS ) ) {
Expand Down Expand Up @@ -9042,10 +9042,10 @@ item::armor_status item::damage_armor_durability( damage_unit &du, damage_unit &
{
//Energy shields aren't damaged by attacks but do get their health variable reduced. They are also only
//damaged by the damage types they actually protect against.
if( has_var( "npctalk_var_ENERGY_SHIELD_HP" ) && resist( du.type, false, bp ) > 0.0f ) {
double shield_hp = get_var( "npctalk_var_ENERGY_SHIELD_HP", 0.0 );
if( has_var( "ENERGY_SHIELD_HP" ) && resist( du.type, false, bp ) > 0.0f ) {
double shield_hp = get_var( "ENERGY_SHIELD_HP", 0.0 );
shield_hp -= premitigated.amount;
set_var( "npctalk_var_ENERGY_SHIELD_HP", shield_hp );
set_var( "ENERGY_SHIELD_HP", shield_hp );
if( shield_hp > 0 ) {
return armor_status::UNDAMAGED;
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/iuse_actor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5636,7 +5636,7 @@ std::optional<int> effect_on_conditons_actor::use( Character *p, item &it,
}

dialogue d( ( char_ptr == nullptr ? nullptr : get_talker_for( char_ptr ) ), get_talker_for( loc ) );
write_var_value( var_type::context, "npctalk_var_id", &d, it.typeId().str() );
write_var_value( var_type::context, "id", &d, it.typeId().str() );
for( const effect_on_condition_id &eoc : eocs ) {
if( eoc->type == eoc_type::ACTIVATION ) {
eoc->activate( d );
Expand Down
2 changes: 1 addition & 1 deletion src/magic_spell_effect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1893,7 +1893,7 @@ void spell_effect::effect_on_condition( const spell &sp, Creature &caster,
Creature *victim = creatures.creature_at<Creature>( potential_target );
dialogue d( victim ? get_talker_for( victim ) : nullptr, get_talker_for( caster ) );
const tripoint_abs_ms target_abs = get_map().getglobal( potential_target );
write_var_value( var_type::context, "npctalk_var_spell_location", &d,
write_var_value( var_type::context, "spell_location", &d,
target_abs.to_string() );
d.amend_callstack( string_format( "Spell: %s Caster: %s", sp.id().c_str(), caster.disp_name() ) );
effect_on_condition_id eoc = effect_on_condition_id( sp.effect_data() );
Expand Down
2 changes: 1 addition & 1 deletion src/mapgen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8130,7 +8130,7 @@ void set_queued_points()
{
global_variables &globvars = get_globals();
for( std::pair<const std::string, tripoint_abs_ms> &queued_point : queued_points ) {
globvars.set_global_value( "npctalk_var_" + queued_point.first, queued_point.second.to_string() );
globvars.set_global_value( queued_point.first, queued_point.second.to_string() );
}
queued_points.clear();
}
Expand Down
4 changes: 2 additions & 2 deletions src/math_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -797,7 +797,7 @@ void math_exp::math_exp_impl::new_var( std::string_view str )
scoped = scoped.substr( 1 );
}
validate_string( scoped, "variable", " \'" );
output.emplace( std::in_place_type_t<var>(), type, "npctalk_var_" + std::string{ scoped } );
output.emplace( std::in_place_type_t<var>(), type, std::string{ scoped } );
}

std::string math_exp::math_exp_impl::error( std::string_view str, std::string_view what )
Expand All @@ -815,7 +815,7 @@ std::string math_exp::math_exp_impl::error( std::string_view str, std::string_vi
std::holds_alternative<var>( output.top().data ) ) {
// NOLINTNEXTLINE(cata-translate-string-literal): debug message
mess = string_format( "%s (or unknown function %s)", mess,
std::get<var>( output.top().data ).varinfo.name.substr( 12 ) );
std::get<var>( output.top().data ).varinfo.name );
}

offset = std::max<std::ptrdiff_t>( 0, offset - 1 );
Expand Down
2 changes: 1 addition & 1 deletion src/math_parser_jmath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ double jmath_func::eval( const_dialogue const &d, std::vector<double> const &par
{
const_dialogue d_next( d );
for( std::vector<double>::size_type i = 0; i < params.size(); i++ ) {
d_next.set_value( "npctalk_var_" + std::to_string( i ), string_format( "%g", params[i] ) );
d_next.set_value( std::to_string( i ), string_format( "%g", params[i] ) );
}

return eval( d_next );
Expand Down
2 changes: 1 addition & 1 deletion src/mattack_actors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -919,7 +919,7 @@ bool melee_actor::call( monster &z ) const
//run EoCs
for( const effect_on_condition_id &eoc : eoc ) {
dialogue d( get_talker_for( z ), get_talker_for( target ) );
write_var_value( var_type::context, "npctalk_var_damage", &d, damage_total );
write_var_value( var_type::context, "damage", &d, damage_total );
eoc->activate( d );
}

Expand Down
Loading

0 comments on commit cf579ac

Please sign in to comment.