Skip to content

Commit

Permalink
Use string ids instead of hashes for snippets
Browse files Browse the repository at this point in the history
  • Loading branch information
Qrox committed Nov 9, 2019
1 parent 1bb08dd commit 01893bd
Show file tree
Hide file tree
Showing 20 changed files with 291 additions and 195 deletions.
13 changes: 12 additions & 1 deletion lang/extract_json_strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@ def warning_supressed(filename):
"PET_ARMOR",
"score",
"skill",
"snippet",
"speech",
"SPELL",
"start_location",
Expand Down Expand Up @@ -760,6 +759,17 @@ def extract_fault(item):
if "success_msg" in method:
writestr(outfile, method["success_msg"], format_strings=True, comment="success message for mending method '{}' of fault '{}'".format(method["name"], item["name"]))

def extract_snippets(item):
outfile = get_outfile("snippet")
text = item["text"];
if type(text) is not list:
text = [text];
for snip in text:
if type(snip) is str:
writestr(outfile, snip)
else:
writestr(outfile, snip["text"])

# these objects need to have their strings specially extracted
extract_specials = {
"harvest" : extract_harvest,
Expand All @@ -782,6 +792,7 @@ def extract_fault(item):
"recipe": extract_recipes,
"recipe_group": extract_recipe_group,
"scenario": extract_scenarios,
"snippet": extract_snippets,
"talk_topic": extract_talk_topic,
"trap": extract_trap,
"gate": extract_gate,
Expand Down
3 changes: 2 additions & 1 deletion src/activity_handlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4244,7 +4244,8 @@ void activity_handlers::tree_communion_do_turn( player_activity *act, player *p
p->add_morale( MORALE_TREE_COMMUNION, 1, 15, 2_hours, 1_hours );
}
if( one_in( 128 ) ) {
p->add_msg_if_player( SNIPPET.random_from_category( "tree_communion" ) );
p->add_msg_if_player( "%s", SNIPPET.random_from_category( "tree_communion" ).value_or(
translation() ) );
}
return;
}
Expand Down
4 changes: 2 additions & 2 deletions src/character.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2941,8 +2941,8 @@ void Character::print_health() const

auto iter = msg_categories.lower_bound( current_health );
if( iter != msg_categories.end() && !iter->second.empty() ) {
const std::string &msg = SNIPPET.random_from_category( iter->second );
add_msg_if_player( current_health > 0 ? m_good : m_bad, msg );
const translation msg = SNIPPET.random_from_category( iter->second ).value_or( translation() );
add_msg_if_player( current_health > 0 ? m_good : m_bad, "%s", msg );
}
}

Expand Down
38 changes: 19 additions & 19 deletions src/computer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ const species_id HUMAN( "HUMAN" );

const efftype_id effect_amigara( "amigara" );

int alerts = 0;
static int alerts = 0;

computer_option::computer_option()
: name( "Unknown" ), action( COMPACT_NULL ), security( 0 )
Expand Down Expand Up @@ -539,14 +539,14 @@ void computer::activate_function( computer_action action )
// TODO: seed should probably be a member of the computer, or better: of the computer action.
// It is here to ensure one computer reporting the same text on each invocation.
const int seed = g->get_levx() + g->get_levy() + g->get_levz() + alerts;
std::string log = SNIPPET.get( SNIPPET.assign( "lab_notes", seed ) );
if( log.empty() ) {
log = _( "No data found." );
cata::optional<translation> log = SNIPPET.random_from_category( "lab_notes", seed );
if( !log.has_value() ) {
log = to_translation( "No data found." );
} else {
g->u.moves -= 70;
}

print_text( "%s", log );
print_text( "%s", log.value() );
// One's an anomaly
if( alerts == 0 ) {
query_any( _( "Local data-access error logged, alerting helpdesk. Press any key…" ) );
Expand All @@ -566,7 +566,7 @@ void computer::activate_function( computer_action action )
sfx::play_ambient_variant_sound( "radio", "inaudible_chatter", 100, sfx::channel::radio,
2000 );
print_text( "Accessing archive. Playing audio recording nr %d.\n%s", rng( 1, 9999 ),
SNIPPET.random_from_category( "radio_archive" ) );
SNIPPET.random_from_category( "radio_archive" ).value_or( translation() ) );
if( one_in( 3 ) ) {
query_any( _( "Warning: resticted data access. Attempt logged. Press any key…" ) );
alerts ++;
Expand Down Expand Up @@ -747,23 +747,23 @@ void computer::activate_function( computer_action action )
g->u.moves -= 30;
reset_terminal();
print_line( _( "NEPower Mine(%d:%d) Log" ), g->get_levx(), g->get_levy() );
print_text( "%s", SNIPPET.get( SNIPPET.assign( "amigara1" ) ) );
print_text( "%s", SNIPPET.random_from_category( "amigara1" ).value_or( translation() ) );

if( !query_bool( _( "Continue reading?" ) ) ) {
return;
}
g->u.moves -= 30;
reset_terminal();
print_line( _( "NEPower Mine(%d:%d) Log" ), g->get_levx(), g->get_levy() );
print_text( "%s", SNIPPET.get( SNIPPET.assign( "amigara2" ) ) );
print_text( "%s", SNIPPET.random_from_category( "amigara2" ).value_or( translation() ) );

if( !query_bool( _( "Continue reading?" ) ) ) {
return;
}
g->u.moves -= 30;
reset_terminal();
print_line( _( "NEPower Mine(%d:%d) Log" ), g->get_levx(), g->get_levy() );
print_text( "%s", SNIPPET.get( SNIPPET.assign( "amigara3" ) ) );
print_text( "%s", SNIPPET.random_from_category( "amigara3" ).value_or( translation() ) );

if( !query_bool( _( "Continue reading?" ) ) ) {
return;
Expand All @@ -786,7 +786,7 @@ void computer::activate_function( computer_action action )
print_line( _( "SITE %d%d%d\n"
"PERTINENT FOREMAN LOGS WILL BE PREPENDED TO NOTES" ),
g->get_levx(), g->get_levy(), abs( g->get_levz() ) );
print_text( "%s", SNIPPET.get( SNIPPET.assign( "amigara4" ) ) );
print_text( "%s", SNIPPET.random_from_category( "amigara4" ).value_or( translation() ) );
print_gibberish_line();
print_gibberish_line();
print_newline();
Expand Down Expand Up @@ -981,52 +981,52 @@ void computer::activate_function( computer_action action )

case COMPACT_SR1_MESS:
reset_terminal();
print_text( "%s", SNIPPET.get( SNIPPET.assign( "sr1_mess" ) ) );
print_text( "%s", SNIPPET.random_from_category( "sr1_mess" ).value_or( translation() ) );
query_any( _( "Press any key to continue…" ) );
break;

case COMPACT_SR2_MESS:
reset_terminal();
print_text( "%s", SNIPPET.get( SNIPPET.assign( "sr2_mess" ) ) );
print_text( "%s", SNIPPET.random_from_category( "sr2_mess" ).value_or( translation() ) );
query_any( _( "Press any key to continue…" ) );
break;

case COMPACT_SR3_MESS:
reset_terminal();
print_text( "%s", SNIPPET.get( SNIPPET.assign( "sr3_mess" ) ) );
print_text( "%s", SNIPPET.random_from_category( "sr3_mess" ).value_or( translation() ) );
query_any( _( "Press any key to continue…" ) );
break;

case COMPACT_SR4_MESS:
reset_terminal();
print_text( "%s", SNIPPET.get( SNIPPET.assign( "sr4_mess" ) ) );
print_text( "%s", SNIPPET.random_from_category( "sr4_mess" ).value_or( translation() ) );
query_any( _( "Press any key to continue…" ) );
break;

case COMPACT_SRCF_1_MESS:
reset_terminal();
print_text( "%s", SNIPPET.get( SNIPPET.assign( "scrf_1_mess" ) ) );
print_text( "%s", SNIPPET.random_from_category( "scrf_1_mess" ).value_or( translation() ) );
query_any( _( "Press any key to continue…" ) );
break;

case COMPACT_SRCF_2_MESS:
reset_terminal();
print_text( "%s", SNIPPET.get( SNIPPET.assign( "scrf_2_1_mess" ) ) );
print_text( "%s", SNIPPET.random_from_category( "scrf_2_1_mess" ).value_or( translation() ) );
query_any( _( "Press any key to continue…" ) );
reset_terminal();
print_text( "%s", SNIPPET.get( SNIPPET.assign( "scrf_2_2_mess" ) ) );
print_text( "%s", SNIPPET.random_from_category( "scrf_2_2_mess" ).value_or( translation() ) );
query_any( _( "Press any key to continue…" ) );
break;

case COMPACT_SRCF_3_MESS:
reset_terminal();
print_text( "%s", SNIPPET.get( SNIPPET.assign( "scrf_3_mess" ) ) );
print_text( "%s", SNIPPET.random_from_category( "scrf_3_mess" ).value_or( translation() ) );
query_any( _( "Press any key to continue…" ) );
break;

case COMPACT_SRCF_SEAL_ORDER:
reset_terminal();
print_text( "%s", SNIPPET.get( SNIPPET.assign( "scrf_seal_order" ) ) );
print_text( "%s", SNIPPET.random_from_category( "scrf_seal_order" ).value_or( translation() ) );
query_any( _( "Press any key to continue…" ) );
break;

Expand Down
2 changes: 1 addition & 1 deletion src/help.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,5 +189,5 @@ void help::display_help()

std::string get_hint()
{
return SNIPPET.get( SNIPPET.assign( "hint" ) );
return SNIPPET.random_from_category( "hint" ).value_or( translation() ).translated();
}
20 changes: 11 additions & 9 deletions src/item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ item::item( const itype *type, time_point turn, int qty ) : type( type ), bday(
}

if( !type->snippet_category.empty() ) {
note = SNIPPET.assign( type->snippet_category );
snippet_id = SNIPPET.random_id_from_category( type->snippet_category );
}

if( current_phase == PNULL ) {
Expand Down Expand Up @@ -2501,9 +2501,12 @@ void item::final_info( std::vector<iteminfo> &info, const iteminfo_query *parts,
insert_separation_line( info );
const std::map<std::string, std::string>::const_iterator idescription =
item_vars.find( "description" );
if( !type->snippet_category.empty() ) {
const cata::optional<translation> snippet = snippet_id.has_value()
? SNIPPET.get_snippet_by_id( snippet_id.value() )
: cata::nullopt;
if( snippet.has_value() ) {
// Just use the dynamic description
info.push_back( iteminfo( "DESCRIPTION", SNIPPET.get( note ) ) );
info.push_back( iteminfo( "DESCRIPTION", snippet.value().translated() ) );
} else if( idescription != item_vars.end() ) {
info.push_back( iteminfo( "DESCRIPTION", idescription->second ) );
} else {
Expand Down Expand Up @@ -7814,7 +7817,7 @@ bool item::use_charges( const itype_id &what, int &qty, std::list<item> &used,
return destroy;
}

void item::set_snippet( const std::string &snippet_id )
void item::set_snippet( const std::string &id )
{
if( is_null() ) {
return;
Expand All @@ -7823,13 +7826,12 @@ void item::set_snippet( const std::string &snippet_id )
debugmsg( "can not set description for item %s without snippet category", typeId().c_str() );
return;
}
const int hash = SNIPPET.get_snippet_by_id( snippet_id );
if( SNIPPET.get( hash ).empty() ) {
debugmsg( "snippet id %s is not contained in snippet category %s", snippet_id.c_str(),
type->snippet_category.c_str() );
if( !SNIPPET.get_snippet_by_id( id ).has_value() ) {
debugmsg( "snippet id %s is not contained in snippet category %s", id,
type->snippet_category );
return;
}
note = hash;
snippet_id = id;
}

const item_category &item::get_category() const
Expand Down
4 changes: 2 additions & 2 deletions src/item.h
Original file line number Diff line number Diff line change
Expand Up @@ -1195,7 +1195,7 @@ class item : public visitable<item>
* Set the snippet text (description) of this specific item, using the snippet library.
* @see snippet_library.
*/
void set_snippet( const std::string &snippet_id );
void set_snippet( const std::string &id );

bool operator<( const item &other ) const;
/** List of all @ref components in printable form, empty if this item has
Expand Down Expand Up @@ -2115,7 +2115,7 @@ class item : public visitable<item>
int burnt = 0; // How badly we're burnt
int poison = 0; // How badly poisoned is it?
int frequency = 0; // Radio frequency
int note = 0; // Associated dynamic text snippet.
cata::optional<std::string> snippet_id; // Associated dynamic text snippet id.
int irradiation = 0; // Tracks radiation dosage.
int item_counter = 0; // generic counter to be used with item flags
int specific_energy = -10; // Specific energy (0.00001 J/g). Negative value for unprocessed.
Expand Down
3 changes: 2 additions & 1 deletion src/iuse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4112,7 +4112,8 @@ static std::string get_music_description()
// of snippets {a, b, c}, but only a 50% chance
// Actual chance = 24.5% of being selected
if( one_in( 2 ) ) {
return SNIPPET.random_from_category( "music_description" );
return SNIPPET.expand( SNIPPET.random_from_category( "<music_description>" ).value_or(
translation() ).translated() );
}

return _( "a sweet guitar solo!" );
Expand Down
2 changes: 1 addition & 1 deletion src/main_menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ void main_menu::init_strings()

loading_ui ui( false );
g->load_core_data( ui );
vdaytip = SNIPPET.random_from_category( "tip" );
vdaytip = SNIPPET.random_from_category( "tip" ).value_or( translation() ).translated();
}

void main_menu::display_text( const std::string &text, const std::string &title, int &selected )
Expand Down
4 changes: 2 additions & 2 deletions src/mapgen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -834,7 +834,7 @@ class jmapgen_sign : public jmapgen_piece

if( !snippet.empty() ) {
// select a snippet from the category
signtext = SNIPPET.get( SNIPPET.assign( snippet ) );
signtext = SNIPPET.random_from_category( snippet ).value_or( translation() ).translated();
} else if( !signage.empty() ) {
signtext = signage;
}
Expand Down Expand Up @@ -886,7 +886,7 @@ class jmapgen_graffiti : public jmapgen_piece

if( !snippet.empty() ) {
// select a snippet from the category
graffiti = SNIPPET.get( SNIPPET.assign( snippet ) );
graffiti = SNIPPET.random_from_category( snippet ).value_or( translation() ).translated();
} else if( !text.empty() ) {
graffiti = text;
}
Expand Down
3 changes: 2 additions & 1 deletion src/monattack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -706,7 +706,8 @@ bool mattack::shockstorm( monster *z )
bool mattack::shocking_reveal( monster *z )
{
shockstorm( z );
std::string WHAT_A_SCOOP = SNIPPET.random_from_category( "clickbait" );
const translation WHAT_A_SCOOP = SNIPPET.random_from_category( "clickbait" ).value_or(
translation() );
sounds::sound( z->pos(), 10, sounds::sound_t::alert,
string_format( _( "the %s obnoxiously yelling \"%s!!!\"" ),
z->name(), WHAT_A_SCOOP ) );
Expand Down
4 changes: 2 additions & 2 deletions src/monster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2109,8 +2109,8 @@ void monster::die( Creature *nkiller )
g->events().send<event_type::character_kills_monster>( ch->getID(), type->id );
if( ch->is_player() && ch->has_trait( trait_KILLER ) ) {
if( one_in( 4 ) ) {
std::string snip = SNIPPET.random_from_category( "killer_on_kill" );
ch->add_msg_if_player( m_good, _( snip ) );
const translation snip = SNIPPET.random_from_category( "killer_on_kill" ).value_or( translation() );
ch->add_msg_if_player( m_good, "%s", snip );
}
ch->add_morale( MORALE_KILLER_HAS_KILLED, 5, 10, 6_hours, 4_hours );
ch->rem_morale( MORALE_KILLER_NEED_TO_KILL );
Expand Down
Loading

0 comments on commit 01893bd

Please sign in to comment.