Skip to content

Fix spurious subsystem warning after a loadout ship class change - #7910

Open
wookieejedi wants to merge 3 commits into
scp-fs2open:masterfrom
wookieejedi:subs-ship-change-fix
Open

wookieejedi wants to merge 3 commits into
scp-fs2open:masterfrom
wookieejedi:subs-ship-change-fix

Conversation

@wookieejedi

Copy link
Copy Markdown
Member

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.
  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 turret subsystem 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

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
@wookieejedi wookieejedi added the cleanup A modification or rewrite of code to make it more understandable or easier to maintain. label Oct 19, 2026

@Goober5000 Goober5000 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A review from Claude, including some findings I specifically asked it about:

The core change looks right to me: remove_subsys_status mirrors insert_subsys_status, the backward iteration and the subsys_index fix-up are correct (including for other parse objects with no entries), and only pruning when the class actually changed is a nice touch. I also checked the reverse swap (a class without a turret swapped for one with it): the new turret simply has no entry and gets the class defaults, which is correct. A few things I noticed while checking it against the rest of the class-change code:

1. One more path changes a parse object's class. The Lua ParseObject.ShipClass setter assigns ship_class directly (parse_object.cpp:301), so it gets neither the pruning nor the hull/shield/weapon fix-ups in swap_parse_object. Calling swap_parse_object there would cover both. I didn't find any other paths.

2. Subsystems that exist on both classes keep the old class's settings. An entry is kept whenever the new class has a subsystem of the same name, including its +Primary Banks:/+Secondary Banks:. FRED writes those relative to the old class's turret defaults (missionsave.cpp:4763), and only the Pilot entry has its bank count clamped on creation (missionparse.cpp:2664-2671), so a same-named turret with fewer banks on the new class can end up with more banks than it's tabled for. The PR's reasoning about stale values applied to an unrelated subsystem seems to apply here too. At minimum the turret bank count could be clamped; you might also want to drop turret weapon overrides when the class changes.

3. Minor: remove_subsys_status can use a single memmove. The loop shifts entries down by one within the same array, so the whole range can be moved in one call; since the source and destination overlap, it has to be memmove rather than memcpy:

memmove(&Subsys_status[index], &Subsys_status[index + 1], sizeof(subsys_status) * (Subsys_index - 1 - index));

This needs to happen before Subsys_index is decremented, and a count of zero (removing the last entry) is fine.

For context, #7909 touches the class/model side of the same area (classes sharing a model with different subsystem lists). The two PRs don't overlap and should merge independently, and both match subsystem names with subsystem_stricmp, so they agree on what counts as the same subsystem.

@dustinspace217 dustinspace217 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reproduced the warning on master and this fixes it. Setup: a copy of SM2-04 with Gamma's four ships changed to GTB Medusa, each with a +Subsystem: turret01a line the way FRED writes it, a misspelled bogus01 entry added to Gamma 1 and Gamma 2, and Gamma set to arrive 8 seconds in. Retail FS2 data, MediaVPs 5.0.2, Debug builds of master (61ad242) and this branch, OpenGL.

  • Ship select, Gamma 1 to Ursa and Gamma 3 to Artemis D.H.: master warns about turret01a on both, plus bogus01 on Gamma 1 and Gamma 2. This branch only warns about bogus01 on Gamma 2, whose class I left alone, so a real bad name still gets reported. Gamma 1's bogus01 goes away with the class change, which seems right since it can't apply to the new class either.
  • Restart after dying (the direct loadout restore): same split between master and this branch.
  • change-ship-class on Gamma 4 before it arrives: master warns about turret01a, this branch doesn't.

Ship classes and weapons of the arriving ships matched master in every run. Multiplayer team select changes ships that already exist and never touches parse objects, and the change-ship-class packet calls swap_parse_object on clients too, so both sides prune the same way. I didn't test a red alert carry-over, and nothing here reaches rendering, so I only ran OpenGL.

I agree with Goober's first point: the Lua ShipClass setter is the one path left that skips this.

}

// wookieejedi
void remove_subsys_status(p_object *pobjp, int index)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor, possibly fine to accept as is: this is the first code that moves entries inside one parse object's range (insert_subsys_status only appends). Lua parse_subsystem handles store an offset from subsys_index, so a handle a script took before a class change can afterwards point at a different subsystem instead of going invalid. It only matters if a script holds those handles across ship select or change-ship-class.

…ct 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.
@dustinspace217

Copy link
Copy Markdown
Contributor

I pushed one commit for the Lua point Goober and I raised: ParseObject.ShipClass now goes through
swap_parse_object() while the ship hasn't arrived, the same as change-ship-class, and keeps the plain assignment
after arrival. On SM1-04 (retail plus MediaVPs 5.0.3, Debug, OpenGL), setting the freighter Maul to a GTF Ulysses from
Lua at the briefing: with the PR alone it arrived with four "Unable to find 'turret0N'" warnings and 5000 max hull;
with the commit, the turret warnings are gone and it has the Ulysses' 180. The setter's doc now says parse subsystem handles taken before the
change can point at a different subsystem afterward.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cleanup A modification or rewrite of code to make it more understandable or easier to maintain.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants