Skip to content

Fix subsystem data being lost or mismatched for ship classes and modular tables - #7909

Draft
Goober5000 wants to merge 8 commits into
scp-fs2open:masterfrom
Goober5000:fix/pof_and_flags_issues
Draft

Goober5000 wants to merge 8 commits into
scp-fs2open:masterfrom
Goober5000:fix/pof_and_flags_issues

Conversation

@Goober5000

@Goober5000 Goober5000 commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Now depends on #8077; in draft until that PR is merged.

Summary

This fixes several related problems where subsystem data set by ships.tbl or the POF was silently lost, or applied to the wrong ship class. Some of them could crash the game.

1. Flags set outside $Flags: were dropped by modular tables

A .tbm that repeats $Flags: without +noreplace replaced the whole flag set. That also cleared flags set by other table entries:

  • Subsystem $Flags: dropped the internal Turret_*_fov_overridden flags set by $Turret Barrel FOV:, $Turret Base FOV: and $Maximum Barrel Elevation:. The POF values then overwrote the table values when the model was read.
  • Ship $Flags: dropped Has_awacs and Allow_landings, which are set while subsystems are parsed. For example, a ship's AWACS stopped working.

Both now keep a list of these preset flags, using the same pattern as preset_wi_flag_defs in weapons.cpp. The ship-level list replaces the existing hand-written save/restore of Afterburner, Draw_weapon_models and Has_display_name.

2. Ship classes sharing a POF lost POF subsystem data

POF subsystem properties are only parsed for the first ship class that loads a model. Other classes get them through model_copy_subsystems, which missed several fields:

  • stepped_rotation / stepped_translation: the Stepped_* flags were copied but the data they point to wasn't. Any $stepped submodel on the second class then dereferenced a null pointer every frame and crashed the game.
  • turret_fov: copied regardless of $Turret Barrel FOV:. That overwrote the destination class's own override, or passed the source class's table value on to a destination that had none.
  • turret_base_fov / turret_max_fov: not copied at all, so the POF's $base_fov and $max_fov limits were lost.
  • path_num: not copied, so AI subsystem attack paths and Lua ModelPath didn't work for the second class.
  • weapon_rotation_pbank: not copied, so $pbank barrels didn't spin while primaries were unlinked.

The POF FOV values are now stored in separate pof_turret_* fields. Each class applies its own ships.tbl overrides on top of them, which gives the right result in whichever direction the copy runs.

Before this change, which of the classes sharing a model got this data depended on which class loaded the model first, and that can be decided by something as incidental as the tech room or ship selection. Every class sharing the model now gets its paths, $pbank spin, POF FOVs and (see below) its own engine wash, regardless of load order.

3. Engine wash came from the class that loaded the model

Each thruster bank stored the $Engine Wash: of the class that loaded the model, so other classes sharing the model ignored their own. The huge-ship default wash was also written into the shared bank at runtime, after which non-huge classes sharing the model got it too. The bank now stores its engine subsystem's index, bounds-checked against each class, and the wash is looked up in the ship's own class.

4. Ship classes sharing a POF with different subsystem lists

The model stores subsystem indices for the class that loaded it, and the copy between classes assumed identical subsystem lists. That was checked only by a debug Assert, so release builds could read past the end of a class's subsystem list.

  • model_copy_subsystems now takes both counts and matches subsystems by name. A destination subsystem with no match stays unlinked and is reported when a ship of that class is loaded, as before.
  • When another class only partly matches, the fixup keeps looking for more classes to copy from until every subsystem is linked, and a subsystem that is unlinked in the source never overwrites one that is linked. Otherwise a class whose list matches the loading class could pick up only the partial class's subset, and then fail to load with an error naming the wrong class.
  • Classes with no subsystems are skipped when looking for a class to copy from.
  • The model's subsystem indices are bounds-checked in ship_get_subsys_for_submodel and the engine-glow check.
  • After all ship tables are parsed, a Warning names any ship classes that share a model but list different subsystems, or the same subsystems in a different order. In those cases hits and engine effects can go to the wrong subsystem. This is a Warning rather than an Error because existing mods have such mismatches, mostly from modular tables that add subsystems to only one class. Retail FS2 data has none.

Known limitations, both pre-existing and only reported by the new Warning:

  • When classes list the same subsystems in a different order, hits and engine effects still go to the subsystem at the same index in the loading class's list.
  • If the class that loads the model first lacks some of another class's subsystems, those subsystems never get POF data. A ship of the other class then fails to load with the existing "not linked" error.

5. Engine wash pointers could dangle after a modular table

Subsystems stored $Engine Wash: as a pointer into Engine_wash_info. A modular ship table parsed later can add new engine washes, and the resulting push_back can reallocate the vector, leaving every pointer stored by earlier tables dangling. Subsystems now store an index, which is resolved when the wash is used. Num_engine_wash_types is replaced by the vector's size, and the vector is now cleared when ships.tbl is reparsed. Before, only the counter was reset, so the two could disagree if ship tables were parsed more than once in a process, as in the unit tests.

Testing

In-game, with retail data and a Debug build. GTC Fenris/Leviathan share cruiser01.pof, and SC Lilith/Cain share cruiser03.pof.

  • cruiser01.pof was edited to add $base_fov/$max_fov to the main turret and $stepped to the radar dish.
  • Test .tbm files set FOV overrides on one class in each pair. A second .tbm, parsed later, replaced ship and subsystem $Flags: without +noreplace.
  • A Lua script reported every subsystem's FOVs and path on mission start.

Results:

  • Each class reports its own barrel FOV, with no leak between classes, plus the POF base/max FOV and valid paths.
  • FOV overrides survive the $Flags: replacement.
  • The copied class's stepped radar dish rotates without crashing.
  • A hostile stealth fighter is still targetable within range of the Fenris's AWACS after the ship's $Flags: is replaced.

Engine wash, in-game, with retail data and a Debug build. A test .tbm gave the Fenris Default100, the Leviathan Default1500 and the Lilith Default1000, and left the Cain without one.

  • The Fenris and Leviathan each produced wash over their own range (about 100 m and 1500 m).
  • The Lilith and Cain produced no wash, as expected: retail cruiser03.pof has no $engine_subsystem= link, and cruisers don't get the huge-ship default.
  • The test was re-run after the change to store the wash as an index, with the same results.

Table parsing across existing mods. A Debug build was run headlessly (-noninteractive, exiting right after game_init()) against retail data and a curated subset of 84 mods: standalone mods using mod.ini, and Knossos-installed mods and total conversions with their dependencies resolved from mod.json.

  • Retail, and every mod that reached ship table parsing, passed except the seven below, which have a shared-model mismatch. With the check as a Warning, all seven start normally; each has exactly one mismatched pair:
    • Frontlines2334, denebiii, Sol_A_History_Upgraded: SD Lucifer / SD Lucifer#scan have 32 subsystems, SD Lucifer#scancapital has 30 (capital02-shielded.pof). FSPort defines all three identically. MediaVPs 3.6.12 and older FSPort-MediaVPs then add turrets to some of them, but never to #scancapital. Mods using FSPort-MediaVPs 4.7.2 with MVPS 4.7.3 are unaffected.
    • LastStand: GVD Typhon (22) vs MIND Ra (34) on Typhon.pof.
    • jad2_22, jad223: #Camera (0) vs AstLayer (3) on blip2.pof.
    • UW: Supply Wyvern vs Wyvern list their subsystems in a different order.
    • WhatIf: PVD Typhon vs PVD Typhon Refit list their subsystems in a different order.
  • A control mod adding a subsystem to only GTSG Mjolnir#home produced the expected Warning.

Unit tests were added for the shared-model check, covering identical, reordered, extra and missing subsystem lists. The full suite passes.

Builds: Freespace2 and FRED2 build cleanly in Debug at every commit. All unit tests pass (one is skipped by the suite).

🤖 Generated with Claude Code

@Goober5000 Goober5000 added fix A fix for bugs, not-a-bugs, and/or regressions. models Issues or features having to do with model data (like animations or geometry) labels Oct 19, 2026
@Goober5000
Goober5000 force-pushed the fix/pof_and_flags_issues branch 2 times, most recently from 17549e5 to 5aade26 Compare October 24, 2026 01:06

@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.

The crash, dangling-pointer and flag fixes check out against master. A few things:

  1. Engine wash now reads wash_sip->subsystems[bank->subsys_num], which is the engine's position in the class that loaded the model. Master took the wash from that class's engine found by name, so a reordered class still washed; now it can read another subsystem's empty setting. Could the bank resolve the engine by name instead? Matching on subobj_num won't work for $enginelarge special points, since those are -1.
  2. ship_copy_subsystem_fixup still stops at the first class whose subsystems[0] is linked. With name matching, a partly matching class can now be that source, so a third class can miss a subsystem the loading class has. Keep looking while any destination subsystem is unlinked?
  3. The "keep this up to date" comment on the preset lists would also cover Stealth and the legacy +untargetable-style tags, which are rightly left out since $Flags sets them too. Maybe "flags that $Flags itself cannot set"?
  4. Worth a line in the description that the second class now gets paths, $pbank spin, POF FOVs and its own wash, since today that depends on which class loads the model (even the tech room can pick it).

Optional: a table-only test for the $Flags fix. Heads-up: #7835 resets Ships_inited in ship_close, so the fixture's manual resets can go once both land.

Nothing reset the IFF module's state, so a second iff_init() in the same
process failed on duplicate IFF names.  IffAccessibilityTest (scp-fs2open#7856) calls
iff_init(), which broke any later test that also needed IFFs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Goober5000 and others added 7 commits November 18, 2026 22:15
A modular table that repeats $Flags: without +noreplace replaced the whole
flagset, dropping flags that are set by other table entries:

- subsystem $Flags: dropped the turret FOV override flags set by
  $Turret Barrel FOV:, $Turret Base FOV: and $Maximum Barrel Elevation:,
  so the POF values silently replaced the tabled ones
- ship $Flags: dropped Has_awacs and Allow_landings, which are set while
  parsing subsystems

Both now keep a list of preset flags, following the preset_wi_flag_defs
pattern used for weapons.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
POF subsystem properties are only read for the first ship class that loads
a model; other classes receive them through model_copy_subsystems, which
missed several fields:

- stepped_rotation / stepped_translation were not copied even though their
  flags were, causing a null dereference (crash) for $stepped submodels
- turret_fov was copied over the destination's $Turret Barrel FOV:, or
  leaked the source class's tabled value into the destination
- turret_base_fov and turret_max_fov were not copied at all
- path_num and weapon_rotation_pbank were not copied

The POF FOV values are now kept in separate pof_ fields so that each class
can apply its own ships.tbl overrides on top of them, in either copy
direction.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The thruster bank stored the $Engine Wash: pointer of the first ship class
that loaded the model, so other classes sharing the model used that class's
wash and ignored their own.  The huge-ship default wash was also written into
the shared bank at runtime, after which non-huge classes sharing the model
picked it up too.

The bank now stores the index of its engine subsystem, and the wash is
resolved from the ship's own class each time.  The index belongs to the
class that loaded the model, so it is bounds-checked against the ship's
class.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Some mods share a model between ship classes whose subsystem lists differ,
for example when a modular table adds subsystems to only one of them.  The
model stores subsystem indices for the class that loaded it, and the copy
between classes assumed identical lists (checked only by a debug Assert), so
such mods could read past the end of a class's subsystems.

- model_copy_subsystems takes both counts and walks the destination's
  subsystems, matching them by name; an unmatched one stays unlinked and is
  reported when a ship of that class is created, as before
- skip classes with no subsystems when looking for one to copy from
- bounds-check the model's subsystem indices in ship_get_subsys_for_submodel
  and the engine glow check

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A POF's subsystem data is read only for the first ship class that loads the
model, and the model stores indices into that class's subsystem list.  When
classes sharing the model list different subsystems, or the same ones in a
different order, hits and engine effects can be applied to the wrong
subsystem, and a ship can fail to load if its class has subsystems the
loading class lacks.

Check this once after all ship tables are parsed and raise a Warning naming
both classes, the model, and the first difference.  A sweep of existing mods
found such mismatches in several of them, mostly from modular tables that add
subsystems to only one of the classes, so this is a Warning rather than an
Error.

Add unit tests for identical, reordered, extra and missing subsystems.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Subsystems stored a pointer into Engine_wash_info, but a modular ship table
parsed later can add new engine washes, and the resulting push_back can
reallocate the vector, leaving every previously stored pointer dangling.

Store an index instead and resolve it when the wash is used.  Also drop
Num_engine_wash_types in favor of the vector's size, and clear the vector
when ships.tbl is reparsed so the two cannot disagree.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A modular table replaces a ship's and a subsystem's $Flags: without
+noreplace. The test checks that the replacement takes effect and that
Has_display_name, Afterburner, Has_awacs, Allow_landings and the
turret FOV override flags survive it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Goober5000
Goober5000 force-pushed the fix/pof_and_flags_issues branch from 5673cf5 to 46150f5 Compare November 19, 2026 05:16
@Goober5000

Copy link
Copy Markdown
Contributor Author

@dustinspace217 Claude's reply...

Thanks for the careful review! Here's where each point landed:

  1. Engine wash by index: no change here, on purpose. Ship classes that share a model are expected to list the same subsystems in the same order. A reordered class breaks that, and the new Warning names it when the tables are parsed. In that case the bank can pick up the wrong wash or none, but it can't crash or corrupt anything: the index is bounds-checked against the ship's own class, and every stored wash index is either -1 or a valid index into Engine_wash_info. Rather than accommodating the reordered list, I'd leave it to the Warning to tell the mod author to fix the table.

  2. Fixup stopping at the first partial match: fixed. ship_copy_subsystem_fixup now keeps looking for source classes until all of the destination's subsystems are linked. model_copy_subsystems skips a source subsystem that is itself unlinked, so a later partial source can't overwrite one that's already linked. Mismatched lists still fail, but the error now names the class that's actually missing subsystems, not one whose list is correct.

  3. Comment wording: both ship preset lists now say "any flag that $Flags itself cannot set". I left the original comment in weapons.cpp as it was, because that list follows a different rule: 8 of its 19 flags can also be set by weapon $Flags, and +override keeps them anyway. The old wording is accurate there.

  4. Description: added. Every class sharing the model now gets its paths, $pbank spin, POF FOVs and its own engine wash, no matter which class loads the model first.

Optional test: added as ShipFlagsTest.replaced_flags. A modular table replaces a ship's and a subsystem's $Flags: without +noreplace. The test first checks that the replacement happened, then that Has_display_name, Afterburner, Has_awacs, Allow_landings and the three turret FOV override flags survived it.

#7835: the fixture's manual Ships_inited resets are gone. The branch is now rebased onto #8077, which adds the iff_close() the test fixtures use, so this PR should merge after that one.

Every commit builds cleanly in Debug (Freespace2, FRED2, qtfred and unittests), and the full unit suite passes at the tip.

@Goober5000
Goober5000 marked this pull request as draft November 19, 2026 05:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fix A fix for bugs, not-a-bugs, and/or regressions. models Issues or features having to do with model data (like animations or geometry)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants