Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion code/autopilot/autopilot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1555,7 +1555,11 @@ void SelectNav(const char *Nav)
// Deselects any navpoint selected.
void DeselectNav()
{
CurrentNav = 0;
// Shutdown removes goals using the selected nav and must precede clearing it.
if (AutoPilotEngaged) {
EndAutoPilot();
}
CurrentNav = -1;
}

//+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Expand Down
2 changes: 1 addition & 1 deletion code/parse/sexp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38222,7 +38222,7 @@ SCP_vector<sexp_help_struct> Sexp_help = {
"\t1:\tName of the nav point." },

{ OP_NAV_UNSELECT, "nav-deselect (Action operator)\r\n"
"\tDeselects any navpoint selected.\r\n\r\n"
"\tDeselects any navpoint selected. Ends autopilot if it is engaged.\r\n\r\n"
"Takes no arguments..." },

{ OP_NAV_SET_COLOR, "nav-set-color (Action operator)\r\n"
Expand Down
127 changes: 127 additions & 0 deletions test/src/autopilot/test_nav_deselect.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
#include <gtest/gtest.h>

#include "autopilot/autopilot.h"
#include "playerman/player.h"
#include "ship/ship.h"

// defined in autopilot.cpp, not in its header
extern bool CinematicStarted;
// defined in aicode.cpp, not in its header
extern object* Autopilot_flight_leader;

class NavDeselectTest : public ::testing::Test {
protected:
int saved_current_nav = -1;
bool saved_engaged = false;
NavPoint saved_navs[MAX_NAVPOINTS];

void SetUp() override {
saved_current_nav = CurrentNav;
saved_engaged = AutoPilotEngaged;
AutoPilotEngaged = false;
for (int i = 0; i < MAX_NAVPOINTS; ++i) {
saved_navs[i] = Navs[i];
Navs[i].clear();
}
CurrentNav = -1;

strcpy_s(Navs[0].m_NavName, "First");
Navs[0].flags = NP_SHIP;
Navs[0].target_index = 0;
strcpy_s(Navs[1].m_NavName, "Second");
Navs[1].flags = NP_SHIP;
Navs[1].target_index = 1;
}

void TearDown() override {
CurrentNav = saved_current_nav;
AutoPilotEngaged = saved_engaged;
for (int i = 0; i < MAX_NAVPOINTS; ++i) {
Navs[i] = saved_navs[i];
}
}
};

// Exercise actual shutdown with a backed nav target, without importing unrelated
// live ships/wing goals from other tests into EndAutoPilot's global scans.
class NavDeselectEngagedTest : public NavDeselectTest {
protected:
bool saved_player_ai = false;
bool saved_cinematic = false;
object* saved_leader = nullptr;
int saved_target_instance = -1;
char saved_target_name[NAME_LENGTH];
int saved_ship_objnums[MAX_SHIPS];
flagset<Ship::Wing_Flags> saved_wing_flags[MAX_WINGS];

void SetUp() override {
NavDeselectTest::SetUp();
saved_player_ai = Player_use_ai;
saved_cinematic = CinematicStarted;
saved_leader = Autopilot_flight_leader;
saved_target_instance = Objects[1].instance;
strcpy_s(saved_target_name, Ships[0].ship_name);
for (int i = 0; i < MAX_SHIPS; ++i) {
saved_ship_objnums[i] = Ships[i].objnum;
Ships[i].objnum = -1;
}
for (int i = 0; i < MAX_WINGS; ++i) {
saved_wing_flags[i] = Wings[i].flags;
Wings[i].flags.remove(Ship::Wing_Flags::Nav_carry);
}
Objects[1].instance = 0;
strcpy_s(Ships[0].ship_name, "Deselect shutdown target");
CinematicStarted = false;
}

void TearDown() override {
Player_use_ai = saved_player_ai;
CinematicStarted = saved_cinematic;
Autopilot_flight_leader = saved_leader;
Objects[1].instance = saved_target_instance;
strcpy_s(Ships[0].ship_name, saved_target_name);
for (int i = 0; i < MAX_SHIPS; ++i)
Ships[i].objnum = saved_ship_objnums[i];
for (int i = 0; i < MAX_WINGS; ++i)
Wings[i].flags = saved_wing_flags[i];
NavDeselectTest::TearDown();
}
};

TEST_F(NavDeselectEngagedTest, EndsEngagedAutopilotAndClearsSelection) {
// Goal removal is not exercised because no ship or wing carries a nav goal.
SelectNav("Second");
ASSERT_EQ(1, CurrentNav);
AutoPilotEngaged = true;
Player_use_ai = true;
Autopilot_flight_leader = &Objects[1];

DeselectNav();

EXPECT_EQ(-1, CurrentNav);
EXPECT_FALSE(AutoPilotEngaged);
EXPECT_FALSE(Player_use_ai);
EXPECT_EQ(nullptr, Autopilot_flight_leader);
DeselectNav();
EXPECT_EQ(-1, CurrentNav);
}

TEST_F(NavDeselectTest, ClearsEachSelectedNavAndRepeatedDeselect) {
SelectNav("First");
ASSERT_EQ(0, CurrentNav);
DeselectNav();
EXPECT_EQ(-1, CurrentNav);

SelectNav("Second");
ASSERT_EQ(1, CurrentNav);
DeselectNav();
EXPECT_EQ(-1, CurrentNav);
DeselectNav();
EXPECT_EQ(-1, CurrentNav);
}

TEST_F(NavDeselectTest, NoSelectionKeepsCanAutopilotDisabled) {
DeselectNav();
ASSERT_EQ(-1, CurrentNav);
EXPECT_FALSE(CanAutopilot(&vmd_zero_vector, false));
}
4 changes: 4 additions & 0 deletions test/src/source_groups.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ add_file_folder("Actions\\\\Expression"
actions/expression/test_ExpressionParser.cpp
)

add_file_folder("Autopilot"
autopilot/test_nav_deselect.cpp
)

add_file_folder("Bmpman"
bmpman/test_eff.cpp
)
Expand Down
Loading