Skip to content

Commit

Permalink
Merge pull request #69581 from prharvey/debug_msg_macro
Browse files Browse the repository at this point in the history
Make add_msg_debug and variants a macro.
  • Loading branch information
Maleclypse authored Jan 24, 2024
2 parents 0fc803f + 399e984 commit de8a47f
Show file tree
Hide file tree
Showing 19 changed files with 54 additions and 277 deletions.
4 changes: 2 additions & 2 deletions src/activity_actor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3903,8 +3903,8 @@ void workout_activity_actor::do_turn( player_activity &act, Character &who )
who.add_morale( MORALE_FEELING_GOOD, intensity_modifier, 20, 6_hours, 30_minutes );
}
if( calendar::once_every( 2_minutes ) ) {
who.add_msg_debug_if_player( debugmode::DF_ACT_WORKOUT, who.activity_level_str() );
who.add_msg_debug_if_player( debugmode::DF_ACT_WORKOUT, act.id().c_str() );
add_msg_debug_if( who.is_avatar(), debugmode::DF_ACT_WORKOUT, who.activity_level_str() );
add_msg_debug_if( who.is_avatar(), debugmode::DF_ACT_WORKOUT, act.id().c_str() );
}
} else if( !rest_mode ) {
rest_mode = true;
Expand Down
15 changes: 1 addition & 14 deletions src/character.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -877,26 +877,13 @@ void Character::add_msg_if_player( const game_message_params &params, const std:
Messages::add_msg( params, msg );
}

void Character::add_msg_debug_if_player( debugmode::debug_filter type,
const std::string &msg ) const
{
Messages::add_msg_debug( type, msg );
}

void Character::add_msg_player_or_npc( const game_message_params &params,
const std::string &player_msg,
const std::string &/*npc_msg*/ ) const
{
Messages::add_msg( params, player_msg );
}

void Character::add_msg_debug_player_or_npc( debugmode::debug_filter type,
const std::string &player_msg,
const std::string &/*npc_msg*/ ) const
{
Messages::add_msg_debug( type, player_msg );
}

void Character::add_msg_player_or_say( const std::string &player_msg,
const std::string &/*npc_speech*/ ) const
{
Expand Down Expand Up @@ -5152,7 +5139,7 @@ needs_rates Character::calc_needs_rates() const

rates.kcal = get_bmr();

add_msg_debug_if_player( debugmode::DF_CHAR_CALORIES, "Metabolic rate: %.2f", rates.hunger );
add_msg_debug_if( is_avatar(), debugmode::DF_CHAR_CALORIES, "Metabolic rate: %.2f", rates.hunger );

static const std::string player_thirst_rate( "PLAYER_THIRST_RATE" );
rates.thirst = get_option< float >( player_thirst_rate );
Expand Down
6 changes: 0 additions & 6 deletions src/character.h
Original file line number Diff line number Diff line change
Expand Up @@ -728,17 +728,11 @@ class Character : public Creature, public visitable
using Creature::add_msg_if_player;
void add_msg_if_player( const std::string &msg ) const override;
void add_msg_if_player( const game_message_params &params, const std::string &msg ) const override;
using Creature::add_msg_debug_if_player;
void add_msg_debug_if_player( debugmode::debug_filter type,
const std::string &msg ) const override;
using Creature::add_msg_player_or_npc;
void add_msg_player_or_npc( const std::string &player_msg,
const std::string &npc_str ) const override;
void add_msg_player_or_npc( const game_message_params &params, const std::string &player_msg,
const std::string &npc_msg ) const override;
using Creature::add_msg_debug_player_or_npc;
void add_msg_debug_player_or_npc( debugmode::debug_filter type, const std::string &player_msg,
const std::string &npc_msg ) const override;
using Creature::add_msg_player_or_say;
void add_msg_player_or_say( const std::string &player_msg,
const std::string &npc_speech ) const override;
Expand Down
6 changes: 0 additions & 6 deletions src/creature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3151,12 +3151,6 @@ void Creature::add_msg_player_or_npc( const game_message_params &params, const t
return add_msg_player_or_npc( params, pc.translated(), npc.translated() );
}

void Creature::add_msg_debug_player_or_npc( debugmode::debug_filter type, const translation &pc,
const translation &npc ) const
{
return add_msg_debug_player_or_npc( type, pc.translated(), npc.translated() );
}

void Creature::add_msg_player_or_say( const translation &pc, const translation &npc ) const
{
return add_msg_player_or_say( pc.translated(), npc.translated() );
Expand Down
62 changes: 0 additions & 62 deletions src/creature.h
Original file line number Diff line number Diff line change
Expand Up @@ -1115,68 +1115,6 @@ class Creature : public viewer
string_format( npc_msg, std::forward<Args>( args )... ) );
}

virtual void add_msg_debug_if_player( debugmode::debug_filter /*type*/,
const std::string &/*msg*/ ) const {}
template<typename ...Args>
void add_msg_debug_if_player( debugmode::debug_filter type, const char *const msg,
Args &&... args ) const {
// expanding for string formatting can be expensive
if( debug_mode ) {
return add_msg_debug_if_player( type, string_format( msg, std::forward<Args>( args )... ) );
}
}
template<typename ...Args>
void add_msg_debug_if_player( debugmode::debug_filter type, const std::string &msg,
Args &&... args ) const {
if( debug_mode ) {
return add_msg_debug_if_player( type, string_format( msg, std::forward<Args>( args )... ) );
}
}

virtual void add_msg_debug_if_npc( debugmode::debug_filter /*type*/,
const std::string &/*msg*/ ) const {}
template<typename ...Args>
void add_msg_debug_if_npc( debugmode::debug_filter type, const char *const msg,
Args &&... args ) const {
// expanding for string formatting can be expensive
if( debug_mode ) {
return add_msg_debug_if_npc( type, string_format( msg, std::forward<Args>( args )... ) );
}
}
template<typename ...Args>
void add_msg_debug_if_npc( debugmode::debug_filter type, const std::string &msg,
Args &&... args ) const {
if( debug_mode ) {
return add_msg_debug_if_npc( type, string_format( msg, std::forward<Args>( args )... ) );
}
}

virtual void add_msg_debug_player_or_npc( debugmode::debug_filter /*type*/,
const std::string &/*player_msg*/,
const std::string &/*npc_msg*/ ) const {}
void add_msg_debug_player_or_npc( debugmode::debug_filter /*type*/,
const translation &/*player_msg*/,
const translation &/*npc_msg*/ ) const;
template<typename ...Args>
void add_msg_debug_player_or_npc( debugmode::debug_filter type, const char *const player_msg,
const char *const npc_msg, Args &&... args ) const {
// expanding for string formatting can be expensive
if( debug_mode ) {
return add_msg_debug_player_or_npc( type, string_format( player_msg,
std::forward<Args>( args )... ),
string_format( npc_msg, std::forward<Args>( args )... ) );
}
}
template<typename ...Args>
void add_msg_debug_player_or_npc( debugmode::debug_filter type, const std::string &player_msg,
const std::string &npc_msg, Args &&... args ) const {
if( debug_mode ) {
return add_msg_debug_player_or_npc( type, string_format( player_msg,
std::forward<Args>( args )... ),
string_format( npc_msg, std::forward<Args>( args )... ) );
}
}

virtual void add_msg_player_or_say( const std::string &/*player_msg*/,
const std::string &/*npc_speech*/ ) const {}
virtual void add_msg_player_or_say( const game_message_params &/*params*/,
Expand Down
2 changes: 1 addition & 1 deletion src/debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ bool debug_mode = false;

namespace debugmode
{
std::list<debug_filter> enabled_filters;
std::unordered_set<debug_filter> enabled_filters;
std::string filter_name( debug_filter value )
{
// see debug.h for commentary
Expand Down
4 changes: 2 additions & 2 deletions src/debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#define CATA_SRC_DEBUG_H

#include "string_formatter.h"
#include <list>
#include <unordered_set>

/**
* debugmsg(msg, ...)
Expand Down Expand Up @@ -280,7 +280,7 @@ enum debug_filter : int {
DF_LAST // This is always the last entry
};

extern std::list<debug_filter> enabled_filters;
extern std::unordered_set<debug_filter> enabled_filters;
std::string filter_name( debug_filter value );
} // namespace debugmode

Expand Down
3 changes: 1 addition & 2 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5329,8 +5329,7 @@ bool game::spawn_npc( const tripoint &p, const string_id<npc_template> &npc_clas
std::vector<trait_id> &traits, std::optional<time_duration> lifespan )
{
if( !unique_id.empty() && g->unique_npc_exists( unique_id ) ) {
get_avatar().add_msg_debug_if_player( debugmode::DF_NPC, "NPC with unique id %s already exists.",
unique_id );
add_msg_debug( debugmode::DF_NPC, "NPC with unique id %s already exists.", unique_id );
return false;
}
shared_ptr_fast<npc> tmp = make_shared_fast<npc>();
Expand Down
16 changes: 7 additions & 9 deletions src/handle_action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1864,7 +1864,7 @@ static void handle_debug_mode()
first_time = false;
debugmode::enabled_filters.clear();
for( int i = 0; i < debugmode::DF_LAST; ++i ) {
debugmode::enabled_filters.emplace_back( static_cast<debugmode::debug_filter>( i ) );
debugmode::enabled_filters.emplace( static_cast<debugmode::debug_filter>( i ) );
}
}

Expand All @@ -1891,9 +1891,8 @@ static void handle_debug_mode()

entry.extratxt.left = 1;

const bool active = std::find(
debugmode::enabled_filters.begin(), debugmode::enabled_filters.end(),
static_cast<debugmode::debug_filter>( i ) ) != debugmode::enabled_filters.end();
const bool active = debugmode::enabled_filters.count( static_cast<debugmode::debug_filter>
( i ) ) == 1;

if( toggle_value && active ) {
toggle_value = false;
Expand Down Expand Up @@ -1926,7 +1925,7 @@ static void handle_debug_mode()
debugmode_entry_setup( dbmenu.entries[i + 2], toggle_value );

if( toggle_value ) {
debugmode::enabled_filters.emplace_back( static_cast<debugmode::debug_filter>( i ) );
debugmode::enabled_filters.emplace( static_cast<debugmode::debug_filter>( i ) );
}
}

Expand All @@ -1935,9 +1934,8 @@ static void handle_debug_mode()
} else if( dbmenu.ret > 1 ) {
uilist_entry &entry = dbmenu.entries[dbmenu.ret];

const auto filter_iter = std::find(
debugmode::enabled_filters.begin(), debugmode::enabled_filters.end(),
static_cast<debugmode::debug_filter>( dbmenu.ret - 2 ) );
const auto filter_iter = debugmode::enabled_filters.find( static_cast<debugmode::debug_filter>
( dbmenu.ret - 2 ) );

const bool active = filter_iter != debugmode::enabled_filters.end();

Expand All @@ -1946,7 +1944,7 @@ static void handle_debug_mode()
if( active ) {
debugmode::enabled_filters.erase( filter_iter );
} else {
debugmode::enabled_filters.push_back(
debugmode::enabled_filters.emplace(
static_cast<debugmode::debug_filter>( dbmenu.ret - 2 ) );
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/mapgen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1829,8 +1829,7 @@ class jmapgen_npc : public jmapgen_piece
return;
}
if( !unique_id.empty() && g->unique_npc_exists( unique_id ) ) {
get_avatar().add_msg_debug_if_player( debugmode::DF_NPC, "NPC with unique id %s already exists.",
unique_id );
add_msg_debug( debugmode::DF_NPC, "NPC with unique id %s already exists.", unique_id );
return;
}
tripoint const dst( x.get(), y.get(), dat.m.get_abs_sub().z() );
Expand Down
38 changes: 5 additions & 33 deletions src/messages.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,11 @@ std::vector<std::pair<std::string, std::string>> Messages::recent_messages( cons
return player_messages.recent_messages( count );
}

bool Messages::has_debug_filter( debugmode::debug_filter type )
{
return debug_mode && debugmode::enabled_filters.count( type ) == 1;
}

void Messages::serialize( JsonOut &json )
{
json.member( "player_messages" );
Expand Down Expand Up @@ -457,18 +462,6 @@ void Messages::add_msg( const game_message_params &params, std::string msg )
player_messages.add_msg_string( std::move( msg ), params );
}

void Messages::add_msg_debug( debugmode::debug_filter type, std::string msg )
{
if( debug_mode &&
std::find(
debugmode::enabled_filters.begin(), debugmode::enabled_filters.end(),
type ) == debugmode::enabled_filters.end() ) {
return;
}

player_messages.add_msg_string( std::move( msg ), m_debug );
}

void Messages::clear_messages()
{
player_messages.messages.clear();
Expand Down Expand Up @@ -1012,11 +1005,6 @@ void add_msg( const game_message_params &params, std::string msg )
Messages::add_msg( params, std::move( msg ) );
}

void add_msg_debug( debugmode::debug_filter type, std::string msg )
{
Messages::add_msg_debug( type, std::move( msg ) );
}

void add_msg_if_player_sees( const tripoint &target, std::string msg )
{
if( get_player_view().sees( target ) ) {
Expand Down Expand Up @@ -1046,19 +1034,3 @@ void add_msg_if_player_sees( const Creature &target, const game_message_params &
Messages::add_msg( params, std::move( msg ) );
}
}

void add_msg_debug_if_player_sees( const tripoint &target, debugmode::debug_filter type,
std::string msg )
{
if( get_player_view().sees( target ) ) {
Messages::add_msg_debug( type, std::move( msg ) );
}
}

void add_msg_debug_if_player_sees( const Creature &target, debugmode::debug_filter type,
std::string msg )
{
if( get_player_view().sees( target ) ) {
Messages::add_msg_debug( type, std::move( msg ) );
}
}
Loading

0 comments on commit de8a47f

Please sign in to comment.