Clearing missions drops the NPC pointers to them

- clearing all missions also empties mission lists of NPCs the overmaps hold
- NPC saved after missions are cleared refers to none of them
This commit is contained in:
dobbry-vechur
2026-10-15 22:01:40 +04:00
parent 657938959b
commit fd7c5f859d
3 changed files with 59 additions and 0 deletions
+6
View File
@@ -25,6 +25,7 @@
#include "item_location.h"
#include "kill_tracker.h"
#include "map.h"
#include "memory_fast.h"
#include "monster.h"
#include "npc.h"
#include "omdata.h"
@@ -240,6 +241,11 @@ std::vector<int> mission::to_uid_vector( const std::vector<mission *> &vec )
void mission::clear_all()
{
for( const shared_ptr_fast<npc> &guy : overmap_buffer.get_overmap_npcs() ) {
guy->chatbin.missions.clear();
guy->chatbin.missions_assigned.clear();
guy->chatbin.mission_selected = nullptr;
}
world_missions.clear();
}
+2
View File
@@ -412,6 +412,8 @@ class mission
static mission *find( int id, bool ok_missing = false );
/**
* Remove all active missions, used to cleanup on exit and before reloading a new game.
* Also clears the mission pointers of NPCs the overmaps own. The avatar's mission lists
* are not touched; see avatar::reset_all_missions.
*/
static void clear_all();
/**
+51
View File
@@ -1,16 +1,24 @@
#include <functional>
#include <memory>
#include <sstream>
#include <string>
#include <vector>
#include "avatar.h"
#include "cata_catch.h"
#include "cata_scope_helpers.h"
#include "character_id.h"
#include "coordinates.h"
#include "dialogue_chatbin.h"
#include "game.h"
#include "item.h"
#include "json.h"
#include "map.h"
#include "map_helpers.h"
#include "memory_fast.h"
#include "mission.h"
#include "npc.h"
#include "overmapbuffer.h"
#include "player_helpers.h"
#include "point.h"
#include "type_id.h"
@@ -265,3 +273,46 @@ TEST_CASE( "automatic_find_item_mission_batch_performance", "[.][performance][mi
SUCCEED();
}
TEST_CASE( "clearing_missions_leaves_no_npc_holding_one", "[mission]" )
{
clear_avatar();
// an overmap-owned NPC far outside the reality bubble, never loaded
shared_ptr_fast<npc> guy = make_shared_fast<npc>();
guy->normalize();
guy->randomize();
guy->spawn_at_omt( get_avatar().pos_abs_omt() + tripoint( 30, 0, 0 ) );
overmap_buffer.insert_npc( guy );
const character_id guy_id = guy->getID();
on_out_of_scope forget_guy( [guy_id]() {
overmap_buffer.remove_npc( guy_id );
} );
REQUIRE( overmap_buffer.find_npc( guy_id ) == guy );
bool loaded = false;
for( const npc &active : g->all_npcs() ) {
loaded |= active.getID() == guy_id;
}
REQUIRE_FALSE( loaded );
mission *offered = mission::reserve_new( mission_TEST_MISSION_GOAL_CONDITION1, guy_id );
mission *taken = mission::reserve_new( mission_TEST_MISSION_GOAL_CONDITION2, guy_id );
guy->add_new_mission( offered );
guy->chatbin.missions_assigned.push_back( taken );
guy->chatbin.mission_selected = taken;
REQUIRE( guy->chatbin.missions.size() == 1 );
SECTION( "every mission cleared, then the NPC saved" ) {
mission::clear_all();
CHECK( guy->chatbin.missions.empty() );
CHECK( guy->chatbin.missions_assigned.empty() );
CHECK( guy->chatbin.mission_selected == nullptr );
std::ostringstream saved;
JsonOut jsout( saved );
guy->serialize( jsout );
const std::string json = saved.str();
CAPTURE( json );
CHECK( json.find( "\"missions\":[]" ) != std::string::npos );
CHECK( json.find( "\"missions_assigned\":[]" ) != std::string::npos );
CHECK( json.find( "\"mission_selected\"" ) == std::string::npos );
}
}