From bee0fff84439850dea5105741ca7db42a6697605 Mon Sep 17 00:00:00 2001 From: wookieejedi Date: Mon, 19 Oct 2026 11:43:57 -0400 Subject: [PATCH 1/2] Fix spurious subsystem warning after a loadout ship class change MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When the ship select screen changes a parse object's ship class, the `+Subsystem:` entries parsed from the mission file are left untouched, so they can still refer to subsystems that only existed on the original class. If the ship is created later, in `parse_create_object_sub`, each of those names is looked up on the new class and fails. FRED writes a `+Subsystem:` line for every turret subsystem whether or not any damage or cargo is set on it, so any turret-bearing ship in a starting wing will produce this if the player swaps it for a class without that turret. #### Repro 1. A starting wing whose ships have a turret (e.g. a Y-wing with a `BombTurret`). 2. Give the wing an arrival cue that isn't true at mission start, so it isn't created during parsing. 3. In ship select, change the wing to a class that doesn't have that turret. The warning only appears when the wing arrives late. A wing present at mission start already has its ship objects, so the class change goes through `change_ship_type`, which never revisits `Subsys_status`. #### Fix Discard the subsystem entries that the new class doesn't have, at the point where the class changes, rather than silencing the lookup later. Besides the warning, this also drops the stale `$Damage:`, cargo and AI class values those entries carried, which could otherwise be applied to an unrelated subsystem. - `remove_subsys_status()` — the counterpart to the existing `insert_subsys_status()`: shifts `Subsys_status` down, decrements the owner's `subsys_count`, and fixes up the `subsys_index` of every other parse object. - `prune_subsys_status()` — drops any entry, other than `Pilot`, whose name isn't in the ship class's subsystem list. Called from the three places a parse object's class can change out from under its subsystem list: - `create_wings()` (ship select commit) - `wss_direct_restore_loadout()` - `swap_parse_object()`, which also covers the `change-ship-class` SEXP on ships that haven't arrived yet, and red alert class restores --- code/mission/missionparse.cpp | 65 ++++++++++++++++++++++++++ code/mission/missionparse.h | 4 ++ code/missionui/missionscreencommon.cpp | 4 ++ code/missionui/missionshipchoice.cpp | 4 ++ 4 files changed, 77 insertions(+) diff --git a/code/mission/missionparse.cpp b/code/mission/missionparse.cpp index cccd93941ea..0b2861bc96b 100644 --- a/code/mission/missionparse.cpp +++ b/code/mission/missionparse.cpp @@ -4511,6 +4511,10 @@ void swap_parse_object(p_object *p_obj, int new_ship_class) ship_subsystems->secondary_banks[j] = -1; } } + + // Subsystems + // Any subsystems from the mission file that the new class doesn't have are no longer relevant + prune_subsys_status(p_obj); } // Goober5000 - this needs to search the vector instead of using the registry, because it can be used during parsing before the registry is populated @@ -8980,6 +8984,67 @@ int insert_subsys_status(p_object *pobjp) return new_index; } +// wookieejedi +void remove_subsys_status(p_object *pobjp, int index) +{ + Assertion((index >= pobjp->subsys_index) && (index < pobjp->subsys_index + pobjp->subsys_count), + "Subsystem index %d does not belong to parse object %s!", index, pobjp->name); + + // shift the elements downward, which keeps each parse object's subsystems contiguous + for (int i = index; i < Subsys_index - 1; i++) + { + memcpy(&Subsys_status[i], &Subsys_status[i+1], sizeof(subsys_status)); + } + + Subsys_index--; + + // the parse object has one less subsystem + pobjp->subsys_count--; + + // we also have to adjust all the indexes in existing parse objects + // (each p_object's subsys_index points to subsystem 0 in its list) + for (auto &pobj: Parse_objects) + { + // bump down base index to account for the removed subsystem + if (pobj.subsys_index > index) + pobj.subsys_index--; + } +} + +// wookieejedi - When a parse object's ship class is changed, such as by the loadout screen, any subsystems +// that the new class doesn't have are no longer meaningful, so get rid of them. Otherwise they would be +// looked up, and warned about, if and when the ship is created. +void prune_subsys_status(p_object *pobjp) +{ + if (!Ship_info.in_bounds(pobjp->ship_class)) + return; + + auto sip = &Ship_info[pobjp->ship_class]; + + // iterate backward so that a removal doesn't shift the entries we haven't checked yet + for (int i = pobjp->subsys_count - 1; i >= 0; i--) + { + int index = pobjp->subsys_index + i; + + // the Pilot subsystem isn't an actual subsystem; it holds the ship's own weapons + if (!subsystem_stricmp(Subsys_status[index].name, NOX("pilot"))) + continue; + + bool found = false; + for (int j = 0; j < sip->n_subsystems; j++) + { + if (!subsystem_stricmp(sip->subsystems[j].subobj_name, Subsys_status[index].name)) + { + found = true; + break; + } + } + + if (!found) + remove_subsys_status(pobjp, index); + } +} + // Goober5000 subsys_status *parse_get_subsys_status(p_object *pobjp, const char *subsys_name) { diff --git a/code/mission/missionparse.h b/code/mission/missionparse.h index bc6765b6b4f..386a918db43 100644 --- a/code/mission/missionparse.h +++ b/code/mission/missionparse.h @@ -689,6 +689,10 @@ void clear_texture_replacements(); // Goober5000 subsys_status *parse_get_subsys_status(p_object *pobjp, const char *subsys_name); +// wookieejedi +void remove_subsys_status(p_object *pobjp, int index); +void prune_subsys_status(p_object *pobjp); + // MjnMixael custom_string* get_custom_string_by_name(SCP_string name); diff --git a/code/missionui/missionscreencommon.cpp b/code/missionui/missionscreencommon.cpp index 40b1a78eac3..0ee8b6fb412 100644 --- a/code/missionui/missionscreencommon.cpp +++ b/code/missionui/missionscreencommon.cpp @@ -1269,8 +1269,12 @@ void wss_direct_restore_loadout() break; } slot = &Player_loadout.unit_data[valid_wing_index*MAX_WING_SLOTS+j]; + bool class_changed = ( p_objp->ship_class != slot->ship_class ); p_objp->ship_class = slot->ship_class; wl_update_parse_object_weapons(p_objp, slot); + // the subsystems in the mission file may not exist on the class the player picked + if ( class_changed ) + prune_subsys_status(p_objp); j++; } } diff --git a/code/missionui/missionshipchoice.cpp b/code/missionui/missionshipchoice.cpp index dde6ffc8ba9..0284d8d45a5 100644 --- a/code/missionui/missionshipchoice.cpp +++ b/code/missionui/missionshipchoice.cpp @@ -2357,8 +2357,12 @@ commit_pressed_status create_wings() for ( auto &p_obj: Parse_objects ) { if ( p_obj.wingnum == WING_INDEX(wp) ) { if ( p_obj.pos_in_wing == j ) { + bool class_changed = ( p_obj.ship_class != Wss_slots[slot_index].ship_class ); p_obj.ship_class = Wss_slots[slot_index].ship_class; wl_update_parse_object_weapons(&p_obj, &Wss_slots[i*MAX_WING_SLOTS+j]); + // the subsystems in the mission file may not exist on the class the player picked + if ( class_changed ) + prune_subsys_status(&p_obj); found_pobj = true; break; } From 0fceb35032ee951a8e19109246369c3ced96e135 Mon Sep 17 00:00:00 2001 From: Dustinspace217 Date: Tue, 17 Nov 2026 19:34:08 -0800 Subject: [PATCH 2/2] Change a not-yet-arrived parse object's class through swap_parse_object in the Lua setter ParseObject.ShipClass assigned ship_class directly, so a class set from Lua before the ship arrived kept the old class's subsystem entries and hull, and skipped the shield and weapon adjustments that ship select and change-ship-class make. Before arrival it now calls swap_parse_object(), as change-ship-class does; after arrival it still only records the class. --- code/scripting/api/objs/parse_object.cpp | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/code/scripting/api/objs/parse_object.cpp b/code/scripting/api/objs/parse_object.cpp index e5d462bdb01..fb3601b7e78 100644 --- a/code/scripting/api/objs/parse_object.cpp +++ b/code/scripting/api/objs/parse_object.cpp @@ -284,7 +284,7 @@ ADE_VIRTVAR(Orientation, l_ParseObject, "orientation", "The orientation of the p return ade_set_args(L, "o", l_Matrix.Set(matrix_h(&poh->getObject()->orient))); } -ADE_VIRTVAR(ShipClass, l_ParseObject, "shipclass", "The ship class of the parsed ship.", "shipclass", "The ship class") +ADE_VIRTVAR(ShipClass, l_ParseObject, "shipclass", "The ship class of the parsed ship. Setting it before the ship arrives changes the class the way change-ship-class does, and parse subsystem handles taken earlier may then refer to a different subsystem.", "shipclass", "The ship class") { parse_object_h* poh = nullptr; int newClass = -1; @@ -297,8 +297,13 @@ ADE_VIRTVAR(ShipClass, l_ParseObject, "shipclass", "The ship class of the parsed if (!poh->isValid()) return ade_set_error(L, "o", l_Shipclass.Set(-1)); - if (ADE_SETTING_VAR && newClass >= 0) { - poh->getObject()->ship_class = newClass; + if (ADE_SETTING_VAR && newClass >= 0 && newClass != poh->getObject()->ship_class) { + auto entry = ship_registry_get(poh->getObject()->name); + if (entry != nullptr && entry->status == ShipStatus::NOT_YET_PRESENT) { + swap_parse_object(poh->getObject(), newClass); + } else { + poh->getObject()->ship_class = newClass; + } } return ade_set_args(L, "o", l_Shipclass.Set(poh->getObject()->ship_class));