From e180a472bddaa855e5a0c448c75d15e203a8757c Mon Sep 17 00:00:00 2001 From: David Brown Date: Fri, 5 Jul 2019 18:30:10 +0100 Subject: [PATCH] npc running off fix --- src/npc.cpp | 8 ++++++-- src/npcmove.cpp | 19 ++++++++++++++++++- src/player.cpp | 4 ++++ src/player.h | 2 +- 4 files changed, 29 insertions(+), 4 deletions(-) diff --git a/src/npc.cpp b/src/npc.cpp index 55be1fc78282b..d3e9e4ee5ca27 100644 --- a/src/npc.cpp +++ b/src/npc.cpp @@ -620,6 +620,10 @@ void npc::revert_after_activity() { mission = previous_mission; attitude = previous_attitude; + activity = player_activity(); + current_activity.clear(); + clear_destination(); + backlog.clear(); } npc_mission npc::get_previous_mission() @@ -1688,7 +1692,7 @@ bool npc::is_patrolling() const bool npc::has_player_activity() const { - return activity && mission == NPC_MISSION_ACTIVITY; + return activity && mission == NPC_MISSION_ACTIVITY && attitude == NPCATT_ACTIVITY; } bool npc::is_travelling() const @@ -2654,7 +2658,7 @@ void npc::set_mission( npc_mission new_mission ) bool npc::has_activity() const { - return mission == NPC_MISSION_ACTIVITY; + return mission == NPC_MISSION_ACTIVITY && attitude == NPCATT_ACTIVITY; } npc_attitude npc::get_attitude() const diff --git a/src/npcmove.cpp b/src/npcmove.cpp index 123457f38e21a..4bb42503d289f 100644 --- a/src/npcmove.cpp +++ b/src/npcmove.cpp @@ -764,7 +764,12 @@ void npc::move() if( action == npc_undecided && attitude == NPCATT_ACTIVITY ) { std::vector activity_route = get_auto_move_route(); if( !activity_route.empty() && !has_destination_activity() ) { - const tripoint final_destination = activity_route.back(); + tripoint final_destination; + if( destination_point ) { + final_destination = g->m.getlocal( *destination_point ); + } else { + final_destination = activity_route.back(); + } update_path( final_destination ); if( !path.empty() ) { move_to_next(); @@ -780,6 +785,14 @@ void npc::move() } if( action == npc_undecided ) { + // an interrupted activity can cause this situation. stops allied NPCs zooming off like random NPCs + if( attitude == NPCATT_ACTIVITY && !activity ) { + revert_after_activity(); + if( is_ally( g->u ) ) { + attitude = NPCATT_FOLLOW; + mission = NPC_MISSION_NULL; + } + } if( is_stationary( true ) ) { // if we're in a vehicle, stay in the vehicle if( in_vehicle ) { @@ -2989,7 +3002,9 @@ bool npc::do_player_activity() if( !backlog.empty() ) { activity = backlog.front(); backlog.pop_front(); + current_activity = activity.get_verb(); } else { + current_activity.clear(); revert_after_activity(); // if we loaded after being out of the bubble for a while, we might have more // moves than we need, so clear them @@ -3841,6 +3856,8 @@ std::string npc_action_name( npc_action action ) return "Avoid friendly fire"; case npc_escape_explosion: return "Escape explosion"; + case npc_player_activity: + return "Performing activity"; default: return "Unnamed action"; } diff --git a/src/player.cpp b/src/player.cpp index 2eeaa2162e21c..eda751165a634 100644 --- a/src/player.cpp +++ b/src/player.cpp @@ -10487,6 +10487,10 @@ void player::assign_activity( const player_activity &act, bool allow_resume ) if( activity.rooted() ) { rooted_message(); } + if( is_npc() ) { + npc *guy = dynamic_cast( this ); + guy->current_activity = activity.get_verb(); + } } bool player::has_activity( const activity_id &type ) const diff --git a/src/player.h b/src/player.h index bb4284d7f4e4d..d2e3fde638228 100644 --- a/src/player.h +++ b/src/player.h @@ -1531,6 +1531,7 @@ class player : public Character player_activity activity; std::list backlog; int volume; + cata::optional destination_point; const profession *prof; start_location_id start_location; @@ -1828,7 +1829,6 @@ class player : public Character std::vector auto_move_route; player_activity destination_activity; - cata::optional destination_point; // Used to make sure auto move is canceled if we stumble off course cata::optional next_expected_position; /** warnings from a faction about bad behaviour */