Skip to content

Fix turret locks, red-alert damage and subsystem flags after a ship class change - #7923

Open
Goober5000 wants to merge 4 commits into
scp-fs2open:masterfrom
Goober5000:fix/7910_followup
Open

Goober5000 wants to merge 4 commits into
scp-fs2open:masterfrom
Goober5000:fix/7910_followup

Conversation

@Goober5000

@Goober5000 Goober5000 commented Oct 24, 2026 •

Copy link
Copy Markdown
Contributor

Follow-up to #7910. While reviewing it, I found three older bugs in what happens to a ship's subsystems when its class changes. This PR fixes them.

Stacked on #7922, which restructures the subsys_set flag setup that the third commit reuses. In draft until that PR merges. Only the last three commits belong to this PR.

Apply lock-all-turrets to every turret when a ship is created

lock-all-turrets was applied inside the loop over the parse object's +Subsystem: entries, so it reached only turrets that have an entry. FRED writes one for every turret of the ship's original class, so it normally works. But after a class change (ship select, change-ship-class before arrival, red alert), any turret of the new class without an entry started unlocked. The lock is now applied by walking the ship's actual turrets.

Match red-alert subsystem damage to the mission's entries by name

Red alert restored subsystem damage on unarrived ships by position. It assumed entry 0 is the Pilot and the class's subsystems follow in order. FRED only writes turrets plus subsystems that are damaged or carry cargo, so any gap shifted the damage onto the wrong subsystems and created duplicate entries. This happened even with unmodified FRED missions; for example:

  • GTB Ursa: its communications damage landed on its turret, and communications came back at full strength.
  • SC Lilith whose engine is damaged in the mission: the sensors' damage landed on the engine.

Each subsystem is now looked up by name, and an entry is created if it's missing, the same way parse_copy_damage does. The loop also stops at the class's subsystem count, and skips subsystems with no hitpoints, which used to produce a NaN damage percentage.

Keep runtime subsystem state when a ship's class changes

change_ship_type rebuilds the subsystems from the new class's table, so every subsystem and turret weapon flag was reset. Only hit percentages, blown-off state and AI references were carried over. Turret locks and beam-free state were lost, including the lock from lock-all-turrets, along with anything set by the ship-subsys-* sexps. This affected wings that change class in ship select as well as change-ship-class mid-mission.

The new subsystems now start from the defaults a newly created ship would have, including the lock-all-turrets lock. Then, for each subsystem the new class shares by name, the runtime flags that differ from the old subsystem's defaults are carried over:

Weapon flags Subsystem flags
Turret_Lock Untargetable
Beam_Free No_SS_targeting
Tagged_Only No_replace
No_live_debris
Vanished
Missiles_ignore_if_dead

Each class change compares against the class the ship has at that moment, so a flag that the mission changed and that matches the intermediate class's table can revert on a later class change. This situation needs per-subsystem table flags that differ between classes and two or more class changes, so it's a rare case that's deliberately not handled.

Changes made since the ship was created survive, but settings that only came from the old class's own table don't override the new class's. With lock-all-turrets, a turret that is still locked stays locked, and one freed by a sexp stays free. A turret that exists only on the new class starts locked, as it would on a newly created ship.

Forced turret targets, rotation and translation locks, cargo scan state, subsystem cargo and turret weapons are still not carried over.

🤖 Generated with Claude Code

@Goober5000 Goober5000 added fix A fix for bugs, not-a-bugs, and/or regressions. refactor A cleanup/restructure of a feature for speed, simplicity, and/or maintainability labels Oct 24, 2026
@Goober5000
Goober5000 marked this pull request as ready for review November 10, 2026 03:26

@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 built this branch and its merge base (a981ff0) and ran them side by side on retail FS2 with MediaVPs 5.0.2: Release builds on OpenGL and Vulkan, plus Debug builds for the red-alert runs, using a small test mod with three missions.

  • Turret lock at arrival: a lock-all-turrets GTB Ursa switched to a Medusa before it arrived came in with its turret free and firing on master. On this branch it's locked.
  • Mid-mission change-ship-class: a lock-all-turrets Leviathan with one turret freed, one beam-freed, its sensors untargetable and its engine at 40% was changed to a Mentu. Master came out with every turret free and the beam-free and untargetable settings gone. This branch kept all of them, and the engine stayed at 40% on both. A Mentu changed to a Leviathan still picked up the Leviathan's own untargetable armor plates, so the old class's table settings don't carry over.
  • Red alert, with "$Red-alert applies to delayed ships" turned on and the carried ships arriving 3 seconds into the second mission: on master the Ursa's communications damage went onto its turret entry and communications came back at full strength, the Lilith's sensor damage went onto its engine, and the Orion's fighterbay came back with NaN hitpoints. On this branch all three came through right, and the Debug build logged nothing new.

I've left a few notes inline. None of them block.

Comment thread code/ship/ship.cpp Resolved
Comment thread code/ship/ship.cpp Resolved
Comment thread code/missionui/redalert.cpp Resolved
Comment thread code/ship/ship.cpp Resolved
Goober5000 and others added 4 commits November 13, 2026 13:35
The flag was applied only inside the loop over the parse object's subsystem
entries.  FRED writes an entry for every turret of the ship's original
class, but after a class change (ship select, change-ship-class on a ship
that hasn't arrived, red alert) the new class's turrets that have no entry
started unlocked.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Red alert restored a ship's subsystem damage by position, assuming that the
parse object's first entry is the Pilot and the class's subsystems follow in
order.  FRED only writes turrets plus subsystems that are damaged or carry
cargo, so any gap shifted the damage onto the wrong subsystems and created
duplicate entries, even for unmodified FRED missions; for example, a GTB
Ursa's communications damage landed on its turret.

Look each subsystem up by name instead, creating an entry if there is none,
as parse_copy_damage does.  Also stop at the class's subsystem count, and
skip subsystems with no hitpoints, which produced a NaN damage percentage.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
change_ship_type rebuilds the ship's subsystems, which resets every
subsystem and turret weapon flag to what the new class's table gives it.
Only hit percentages, blown-off state and AI references were carried over,
so turret locks and beam-free state, including the lock applied by
lock-all-turrets, and flags set by the ship-subsys sexps were lost.  This
affects wings that change class in ship select as well as change-ship-class
during a mission.

The new subsystems now start from the same defaults a newly created ship
would have, including the lock from lock-all-turrets.  Then, for each
subsystem the new class shares by name, the runtime flags that differ from
the old subsystem's defaults are carried over, so changes made since the
ship was created survive but the old class's own table settings don't
override the new class's.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…empty list

insert_subsys_status increments the parse object's subsystem count and then
bumps the base index of every parse object at or after the new entry.  When
the parse object had no subsystems, its own base index equals the new
entry's, so it was bumped as well: its one entry then became the next ship's
first entry, or lay past the end of the array.  The removed red-alert Assert
was the only thing that caught this.

FRED always writes a Pilot entry, so this needs a hand-edited mission, but it
affected both red alert and parse_copy_damage.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

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

Thanks for the updates. All four of my 11-10 notes are addressed, and I'm still approving.

I built the new head (a6b03fc) and master 51041cf as Release builds and ran them on retail FS2 with MediaVPs 5.0.2, in SM1-01 on OpenGL, and the branch again on Vulkan. I created a Leviathan, ran turret-lock-all, freed Turret01, made its sensors untargetable, and changed it to a Mentu. On this branch Turret02 to Turret08 stayed locked, Turret01 stayed free, the Mentu-only turrets came up free and the sensors stayed untargetable, the same on both renderers. Master came out with every turret free and the sensors targetable.

I also checked whether alter-ship-flag lock-all-turrets on a ship that has already arrived could change the baseline the class change compares against. It can't, since the flag isn't in Ship_flag_names, and in the run neither the sexp nor Lua setFlag changed it.

Smaller:

  • code/mission/missionparse.cpp:8890: I didn't rerun the hand-edited empty-list red-alert case on this head, because I couldn't load a hand-edited mission in this run, but the skip reads right to me.
  • code/ship/ship.cpp:12252: ship-subsys-guardian-threshold sets a field rather than a flag, so subsys_set zeroes it at ship.cpp:7950, the restore loop doesn't bring it back, and a guarded engine can be destroyed after change-ship-class. Carrying subsys_guardian_threshold for matched subsystems would fix it, or the description could drop "anything set by the ship-subsys-* sexps" and list it under what isn't carried over.
  • code/ship/ship.cpp:12230: in multiplayer turret-free only changes the server, so after a class change on a lock-all ship a freed shared turret stays free on the server and locked on the client, where aiturret.cpp:2198 then skips local tracking. Master's class change happened to reset both sides, and syncing the free and lock sexps is a separate fix, so a line in the description's limits would cover it here.
  • code/mission/missionparse.cpp:670 and ship_flags.h:104: "on a class change" can read as every turret relocking, while shared turrets a sexp freed stay free. This builds on my earlier help-text note; maybe "after a class change, turrets the new class adds also start locked".
  • code/ship/ship.cpp:12113: this corrects my 11-10 approval, since the Mentu has no armor plates, so that run only showed new subsystems getting their table defaults, not the old class's table flags staying behind. The code compares against the old class's table as intended; I just haven't run two classes whose tables disagree on a shared subsystem.

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. refactor A cleanup/restructure of a feature for speed, simplicity, and/or maintainability

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants