Skip to content

Commit 8846589

Browse files
committed
Add docking and undocking test to the Lab
1 parent 2495abe commit 8846589

7 files changed

Lines changed: 339 additions & 0 deletions

File tree

code/ai/aicode.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,21 @@ void garbage_collect_path_points()
542542

543543
}
544544

545+
void reset_ai_path_points()
546+
{
547+
memset(Path_points, 0, sizeof(Path_points)); // optional: zero out for safety
548+
Ppfp = Path_points;
549+
550+
// Also clear any AI path state
551+
for (int i = 0; i < MAX_SHIPS; ++i) {
552+
if (Ai_info[i].path_length > 0) {
553+
Ai_info[i].path_start = -1;
554+
Ai_info[i].path_cur = -1;
555+
Ai_info[i].path_length = 0;
556+
}
557+
}
558+
}
559+
545560
/**
546561
* Hash two values together, return result.
547562
* Hash function: curval shifted right circular by one, newval xored in.

code/ai/aigoals.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ typedef struct ai_goal {
152152

153153
extern void ai_goal_reset(ai_goal *aigp, bool adding_goal = false, ai_goal_mode ai_mode = AI_GOAL_NONE, int ai_submode = -1, ai_goal_type type = ai_goal_type::INVALID);
154154

155+
// Reset all path points. Used in the ship lab. Missions clean up path points with the garbage collector.
156+
extern void reset_ai_path_points();
155157

156158
typedef flag_def_list_templated<ai_goal_mode> ai_goal_list;
157159

code/lab/dialogs/lab_ui.cpp

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1233,13 +1233,104 @@ void LabUi::show_object_options() const
12331233
if (getLabManager()->isSafeForShips()) {
12341234
if (Button("Destroy ship")) {
12351235
if (Objects[getLabManager()->CurrentObject].type == OBJ_SHIP) {
1236+
// If we have an undocker, delete it before destroying the current ship
1237+
getLabManager()->deleteDockerObject();
1238+
12361239
auto obj = &Objects[getLabManager()->CurrentObject];
12371240

12381241
obj->flags.remove(Object::Object_Flags::Player_ship);
12391242
ship_self_destruct(obj);
12401243
}
12411244
}
12421245

1246+
const ship* dockee_shipp = &Ships[Objects[getLabManager()->CurrentObject].instance];
1247+
auto dockee_dock_map = get_docking_point_map(Ship_info[dockee_shipp->ship_info_index].model_num);
1248+
1249+
if (!dockee_dock_map.empty()) {
1250+
1251+
if (ImGui::BeginCombo("Docker Ship Class", Ship_info[getLabManager()->DockerClass].name)) {
1252+
for (size_t i = 0; i < Ship_info.size(); ++i) {
1253+
bool is_selected = (static_cast<int>(i) == getLabManager()->DockerClass);
1254+
if (ImGui::Selectable(Ship_info[i].name, is_selected)) {
1255+
getLabManager()->DockerClass = static_cast<int>(i);
1256+
// Load model if needed
1257+
auto& dsip = Ship_info[getLabManager()->DockerClass];
1258+
if (dsip.model_num < 0) {
1259+
dsip.model_num = model_load(dsip.pof_file, &dsip);
1260+
}
1261+
auto new_dock_map = get_docking_point_map(dsip.model_num);
1262+
1263+
// Auto-select first available dockpoint (or clear if none)
1264+
if (!new_dock_map.empty()) {
1265+
getLabManager()->DockerDockPoint = new_dock_map.begin()->second;
1266+
} else {
1267+
getLabManager()->DockerDockPoint.clear();
1268+
}
1269+
}
1270+
if (is_selected)
1271+
ImGui::SetItemDefaultFocus();
1272+
}
1273+
ImGui::EndCombo();
1274+
}
1275+
1276+
auto& dsip = Ship_info[getLabManager()->DockerClass];
1277+
if (dsip.model_num < 0) {
1278+
dsip.model_num = model_load(dsip.pof_file, &dsip);
1279+
}
1280+
auto dock_map = get_docking_point_map(dsip.model_num);
1281+
1282+
// Ensure DockerDockPoint is initialized once based on the current DockerClass
1283+
if (getLabManager()->DockerDockPoint.empty()) {
1284+
if (!dock_map.empty()) {
1285+
getLabManager()->DockerDockPoint = dock_map.begin()->second;
1286+
}
1287+
}
1288+
1289+
const char* docker_label = getLabManager()->DockerDockPoint.c_str();
1290+
1291+
if (ImGui::BeginCombo("Docker Dockpoint", docker_label)) {
1292+
if (!dock_map.empty()) {
1293+
for (const auto& [index, name] : dock_map) {
1294+
bool is_selected = (name == getLabManager()->DockerDockPoint);
1295+
if (ImGui::Selectable(name.c_str(), is_selected)) {
1296+
getLabManager()->DockerDockPoint = name;
1297+
}
1298+
if (is_selected)
1299+
ImGui::SetItemDefaultFocus();
1300+
}
1301+
}
1302+
ImGui::EndCombo();
1303+
}
1304+
1305+
// Auto-select first dockpoint if none currently selected
1306+
if (getLabManager()->DockeeDockPoint.empty()) {
1307+
getLabManager()->DockeeDockPoint = dockee_dock_map.begin()->second;
1308+
}
1309+
1310+
const char* dockee_label = getLabManager()->DockeeDockPoint.c_str();
1311+
1312+
if (ImGui::BeginCombo("Dockee Dockpoint", dockee_label)) {
1313+
for (const auto& [index, name] : dockee_dock_map) {
1314+
bool is_selected = (name == getLabManager()->DockeeDockPoint);
1315+
if (ImGui::Selectable(name.c_str(), is_selected)) {
1316+
getLabManager()->DockeeDockPoint = name;
1317+
}
1318+
if (is_selected)
1319+
ImGui::SetItemDefaultFocus();
1320+
}
1321+
ImGui::EndCombo();
1322+
}
1323+
1324+
if (Button("Begin Docking Test")) {
1325+
getLabManager()->beginDockingTest();
1326+
}
1327+
1328+
if (Button("Begin Undocking Test")) {
1329+
getLabManager()->beginUndockingTest();
1330+
}
1331+
}
1332+
1333+
12431334
build_animation_options(shipp, sip);
12441335
}
12451336
}

code/lab/dialogs/lab_ui_helpers.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,22 @@
33
#include "cfile/cfile.h"
44
#include "lab/labv2_internal.h"
55

6+
SCP_map<int, SCP_string> get_docking_point_map(int model_index)
7+
{
8+
SCP_map<int, SCP_string> result;
9+
10+
polymodel* pm = model_get(model_index);
11+
if (pm == nullptr || pm->n_docks <= 0)
12+
return result;
13+
14+
for (int i = 0; i < pm->n_docks; ++i) {
15+
const char* name = pm->docking_bays[i].name;
16+
result[i] = (name && *name) ? SCP_string(name) : SCP_string("<unnamed>");
17+
}
18+
19+
return result;
20+
}
21+
622

723
SCP_string get_ship_table_text(ship_info* sip)
824
{

code/lab/dialogs/lab_ui_helpers.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#include "asteroid/asteroid.h"
44
#include "ship/ship.h"
55

6+
SCP_map<int, SCP_string> get_docking_point_map(int model_index);
7+
68
SCP_string get_ship_table_text(ship_info* sip);
79

810
SCP_string get_weapon_table_text(weapon_info* wip);

0 commit comments

Comments
 (0)