#include #include #include #include #include #include #include #include "achievement.h" #include "avatar.h" #include "bodypart.h" #include "cata_catch.h" #include "cata_utility.h" #include "character.h" #include "character_id.h" #include "coordinates.h" #include "debug_menu.h" #include "event.h" #include "event_bus.h" #include "filesystem.h" #include "map_helpers_tests.h" #include "memorial_logger.h" #include "monster.h" #include "mutation.h" #include "player_helpers.h" #include "point.h" #include "stats_tracker.h" #include "type_id.h" static const addiction_id addiction_alcohol( "alcohol" ); static const matype_id style_aikido( "style_aikido" ); static const mutation_category_id mutation_category_URSINE( "URSINE" ); static const skill_id skill_driving( "driving" ); static const spell_id spell_pain_damage( "pain_damage" ); static const trap_str_id tr_pit( "tr_pit" ); // Requires a custom sender for eg. using event types that need to be sent differently for EOCS to work template static void check_memorial_custom_sender( memorial_logger &m, const std::string &ref, std::function &&sender ) { CAPTURE( io::enum_to_string( Type ) ); CAPTURE( ref ); m.clear(); sender(); std::string result = m.dump(); CAPTURE( result ); std::vector result_lines = string_split( result, '\n' ); REQUIRE( !result_lines.empty() ); CHECK( result_lines.back().empty() ); result_lines.pop_back(); // Remove expected results by matching them against the strings we encounter. std::vector ref_lines = string_split( ref, '\n' ); for( const std::string &result_string : result_lines ) { std::string message = string_split( result_string, '|' ).back(); if( !message.empty() && message.front() == ' ' ) { message.erase( message.begin() ); } // Remove trailing carriage return, if any while( !message.empty() && *message.rbegin() == '\r' ) { message.erase( message.end() - 1 ); } ref_lines.erase( std::remove( ref_lines.begin(), ref_lines.end(), message ), ref_lines.end() ); } std::string unmatched_results; INFO( std::accumulate( begin( ref_lines ), end( ref_lines ), unmatched_results ) ); CHECK( ref_lines.empty() ); } // Send the event via event_bus::send as a sane default template static void check_memorial( memorial_logger &m, const std::string &ref, Args... args ) { check_memorial_custom_sender( m, ref, [&] { get_event_bus().send( cata::event::make( args... ) ); } ); } TEST_CASE( "memorials", "[memorial]" ) { memorial_logger &m = get_memorial(); m.clear(); clear_avatar(); get_stats().clear(); get_achievements().clear(); avatar &player_character = get_avatar(); player_character.male = false; character_id ch = player_character.getID(); std::string u_name = player_character.name; character_id ch2 = character_id( ch.get_value() + 1 ); mutagen_technique mutagen = mutagen_technique::injected_purifier; mtype_id mon( "mon_zombie_kevlar_2" ); tripoint_bub_ms loc = get_avatar().pos_bub() + tripoint::east; monster &dummy_monster = spawn_test_monster( "mon_dragon_dummy", loc ); efftype_id eff( "onfire" ); itype_id it( "marloss_seed" ); trait_id mut( "CARNIVORE" ); trait_id mut2( "SAPROPHAGE" ); trait_id mut3( "NONE" ); bionic_id cbm( "bio_alarm" ); proficiency_id prof( "prof_wound_care" ); check_memorial( m, "Activated the art_name.", ch, "art_name" ); check_memorial( m, "Activated a mininuke.", ch ); check_memorial( m, "Injected purifier.", ch, mutagen ); check_memorial( m, "Angered a group of amigara horrors!" ); check_memorial( m, "Awoke a group of dark wyrms!" ); check_memorial( m, "Became wanted by the police!", ch ); check_memorial( m, "Broke her right arm.", ch, bodypart_id( "arm_r" ) ); check_memorial( m, "Broken right arm began to mend.", ch, bodypart_id( "arm_r" ) ); check_memorial( m, "You buried monster_name.", ch, mon, "monster_name" ); check_memorial( m, "Caused a resonance cascade." ); check_memorial( m, "Caught on fire.", ch, bodypart_id( "arm_r" ), eff, 1 ); check_memorial( m, "Killed an innocent person, victim_name, in cold blood and felt terrible " "afterwards.", ch, ch2, "victim_name", "victim_class" ); check_memorial_custom_sender( m, "Killed a Kevlar hulk.", [&] { const cata::event e = cata::event::make( ch, mon, 0 ); get_event_bus().send_with_talker( player_character.as_character(), dummy_monster.as_monster(), e ); } ); check_memorial( m, "Put out the fire.", ch, bodypart_id( "arm_r" ), eff ); check_memorial( m, "Fell in a pit.", ch, tr_pit ); check_memorial( m, "Consumed a Marloss seed.", ch, it ); check_memorial( m, "Opened the Marloss Gateway.", ch ); check_memorial( m, "Became one with the bears.", ch, mutation_category_URSINE ); check_memorial( m, "Became one with the Mycus.", ch ); check_memorial( m, "Destroyed a triffid grove." ); check_memorial( m, "Succumbed to an asthma attack.", ch ); check_memorial( m, "Died of a drug overdose.", ch, eff ); check_memorial( m, "Bled to death.", ch ); check_memorial( m, "Died of hypovolemic shock.", ch ); check_memorial( m, "Died from loss of red blood cells.", ch ); check_memorial( m, "Succumbed to the infection.", ch ); check_memorial( m, "Died of starvation.", ch ); check_memorial( m, "Died of thirst.", ch ); check_memorial( m, "Dug a shaft into lava." ); check_memorial( m, "Disarmed a nuclear missile." ); check_memorial( m, "Ate a sewage sample." ); check_memorial( m, "'Carnivore' mutation turned into 'Saprophage'", ch, mut, mut2 ); check_memorial( m, "Exhumed a grave.", ch ); check_memorial( m, "Failed install of bionic: Alarm System.", ch, cbm ); check_memorial( m, "Failed to remove bionic: Alarm System.", ch, cbm ); check_memorial( m, "Succumbed to lack of sleep.", ch ); check_memorial( m, "The fuel tank of the vehicle_name exploded!", "vehicle_name" ); check_memorial( m, "Became addicted to alcohol.", ch, addiction_alcohol ); check_memorial( m, "Gained the mutation 'Carnivore'.", ch, mut ); check_memorial( m, "Gained the proficiency 'Wound Care'.", ch, prof ); check_memorial( m, "Reached skill level 8 in vehicles.", ch, skill_driving, 8 ); check_memorial( m, u_name + " was killed.\nLast words: last_words", ch, u_name, false, "last_words" ); check_memorial( m, u_name + " began their journey into the Cataclysm.", true, false, ch, u_name, player_character.custom_profession ); check_memorial( m, "Installed bionic: Alarm System.", ch, cbm ); check_memorial( m, "Installed bad bionic: Alarm System.", ch, cbm ); check_memorial( m, "Learned Aikido.", ch, style_aikido ); check_memorial( m, "Overcame addiction to alcohol.", ch, addiction_alcohol ); check_memorial( m, "npc_name became hostile.", ch2, "npc_name" ); check_memorial( m, "Opened a portal." ); check_memorial( m, "Opened a strange temple." ); check_memorial( m, "Forgot the spell Pain.", ch, spell_pain_damage ); check_memorial( m, "Learned the spell Pain.", ch, spell_pain_damage ); check_memorial( m, "Gained a spell level on Pain.", ch, spell_pain_damage, 5, mut3 ); check_memorial( m, "Released subspace specimens." ); check_memorial( m, "Removed bionic: Alarm System.", ch, cbm ); check_memorial( m, "Sealed a Hazardous Material Sarcophagus." ); check_memorial( m, "Telefragged a telefragged_name.", ch, "telefragged_name" ); check_memorial( m, "Spontaneous teleport.", ch ); check_memorial( m, "Teleported into a obstacle_name.", ch, "obstacle_name" ); check_memorial( m, "Terminated subspace specimens." ); check_memorial( m, "Threw up.", ch ); check_memorial( m, "Set off an alarm.", ch ); check_memorial( m, "Used the debug menu (WISH).", debug_menu::debug_menu_index::WISH ); } TEST_CASE( "convert_legacy_memorial_log", "[memorial]" ) { std::string eol = cata_files::eol(); // Verify that the old format can be transformed into the new format const std::string input = "| Year 1, Spring, day 0 0800.00 | prison | " "Hubert 'Daffy' Mullin began their journey into the Cataclysm." + eol + "| Year 1, Spring, day 0 0800.05 | prison | Gained the mutation 'Debug Invincibility'." + eol; const std::string json_value = R"([{"preformatted":"| Year 1, Spring, day 0 0800.00 | prison | Hubert 'Daffy' Mullin began their journey into the Cataclysm."},{"preformatted":"| Year 1, Spring, day 0 0800.05 | prison | Gained the mutation 'Debug Invincibility'."}])"; memorial_logger logger; { std::istringstream is( input ); logger.load( is ); std::ostringstream os; logger.save( os ); CHECK( os.str() == json_value ); } // Then verify that the new format is unchanged { std::istringstream is( json_value ); logger.load( is ); std::ostringstream os; logger.save( os ); CHECK( os.str() == json_value ); } // Finally, verify that dump matches legacy input CHECK( logger.dump() == input ); } TEST_CASE( "memorial_log_dumping", "[memorial]" ) { std::string eol = cata_files::eol(); // An example log file with one legacy and one "modern" entry const std::string json_value = R"([{"preformatted":"| Year 1, Spring, day 0 0800.00 | refugee center | Apolonia Trout began their journey into the Cataclysm."},{"time":15614,"oter_id":"cabin_isherwood","oter_name":"forest","message":"Used the debug menu (ENABLE_ACHIEVEMENTS)."}])"; const std::string expected_output = "| Year 1, Spring, day 0 0800.00 | refugee center | " "Apolonia Trout began their journey into the Cataclysm." + eol + "| Year 1, Mar 21 4:20:14 AM | forest | Used the debug menu (ENABLE_ACHIEVEMENTS)." + eol; memorial_logger logger; std::istringstream is( json_value ); logger.load( is ); CHECK( logger.dump() == expected_output ); }