From 7340371b787cdddccde58029580bef8bfc5f9c01 Mon Sep 17 00:00:00 2001 From: Goober5000 Date: Sat, 24 Oct 2026 01:44:10 -0400 Subject: [PATCH 1/4] Apply lock-all-turrets to every turret when a ship is created 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 --- code/mission/missionparse.cpp | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/code/mission/missionparse.cpp b/code/mission/missionparse.cpp index dd276c3c39d..ad105ff7078 100644 --- a/code/mission/missionparse.cpp +++ b/code/mission/missionparse.cpp @@ -2818,15 +2818,6 @@ int parse_create_object_sub(p_object *p_objp, bool standalone_ship) if (!ptr) continue; - if (shipp->flags[Ship::Ship_Flags::Lock_all_turrets_initially]) - { - // mark all turrets as locked - if(ptr->system_info->type == SUBSYSTEM_TURRET) - { - ptr->weapons.flags.set(Ship::Weapon_Flags::Turret_Lock); - } - } - if (Fred_running) { ptr->current_hits = sssp->percent; @@ -2862,6 +2853,16 @@ int parse_create_object_sub(p_object *p_objp, bool standalone_ship) ptr->turret_animation_done_time = 0; } + // this covers every turret, including any the mission file has no entry for (e.g. after a ship class change) + if (shipp->flags[Ship::Ship_Flags::Lock_all_turrets_initially]) + { + for (auto ss : list_range(&shipp->subsys_list)) + { + if (ss->system_info->type == SUBSYSTEM_TURRET) + ss->weapons.flags.set(Ship::Weapon_Flags::Turret_Lock); + } + } + // initial hull strength, shields, and velocity are all expressed as a percentage of the max value/ // so a initial_hull value of 90% means 90% of max. This way is opposite of how subsystems are dealt // with From 1c516a374ff89d45cf4e15d3d3844a9ac5f3e27e Mon Sep 17 00:00:00 2001 From: Goober5000 Date: Sat, 24 Oct 2026 01:45:37 -0400 Subject: [PATCH 2/4] Match red-alert subsystem damage to the mission's entries by name 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 --- code/missionui/redalert.cpp | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/code/missionui/redalert.cpp b/code/missionui/redalert.cpp index c3a8f845fad..4502098f2b0 100644 --- a/code/missionui/redalert.cpp +++ b/code/missionui/redalert.cpp @@ -664,7 +664,7 @@ extern int insert_subsys_status(p_object *pobjp); void red_alert_bash_subsys_status(const red_alert_ship_status *ras, p_object *pobjp) { - int i, j; + int i; ship_info *sip; model_subsystem *psub = NULL; subsys_status *sssp = NULL; @@ -677,31 +677,29 @@ void red_alert_bash_subsys_status(const red_alert_ship_status *ras, p_object *po // do this differently than the other bash_subsys_status... since the p_object may not contain // all the subsystems, iterate on the red_alert_ship_status entries rather than the p_object's // and create missing subsystems where necessary - for (i = 0; i < (int) ras->subsys_current_hits.size(); i++) + for (i = 0; (i < sz2i(ras->subsys_current_hits.size())) && (i < sip->n_subsystems); i++) { psub = &sip->subsystems[i]; - // in the p_object, subsystem 0 is the pilot, and afterwards the subsystems go in order - j = i + 1; + float max_hits = psub->max_subsys_strength * (pobjp->ship_max_hull_strength / sip->max_hull_strength); + + // a subsystem with no hitpoints has no damage to restore + if (max_hits <= 0.0f) + continue; + + // the mission file only lists some subsystems, in no fixed position, so look this one up by name + sssp = parse_get_subsys_status(pobjp, psub->subobj_name); - // this subsystem is in the p_object - if (j < pobjp->subsys_count) - { - sssp = &Subsys_status[pobjp->subsys_index + j]; - } // must create subsystem (same technique as in parse_copy_damage) - else + if (sssp == nullptr) { // jam in the new subsystem at the end of the existing list for this parse object int new_idx = insert_subsys_status(pobjp); - Assert(new_idx == pobjp->subsys_index + j); sssp = &Subsys_status[new_idx]; strcpy_s(sssp->name, psub->subobj_name); } - float max_hits = psub->max_subsys_strength * (pobjp->ship_max_hull_strength / sip->max_hull_strength); - float current_hits = ras->subsys_current_hits[i]; sssp->percent = 100.0f * (max_hits - current_hits) / max_hits; From c785851b10e2f4f12dad34b00fa1951f2bd8be2c Mon Sep 17 00:00:00 2001 From: Goober5000 Date: Sat, 24 Oct 2026 01:48:02 -0400 Subject: [PATCH 3/4] Keep runtime subsystem state when a ship's class changes 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 --- code/mission/missionparse.cpp | 2 +- code/ship/ship.cpp | 65 +++++++++++++++++++++++++++++++++++ code/ship/ship_flags.h | 2 +- 3 files changed, 67 insertions(+), 2 deletions(-) diff --git a/code/mission/missionparse.cpp b/code/mission/missionparse.cpp index ad105ff7078..31243ff413a 100644 --- a/code/mission/missionparse.cpp +++ b/code/mission/missionparse.cpp @@ -666,7 +666,7 @@ parse_object_flag_description Parse_object_flag_des { Mission::Parse_Object_Flags::SF_Nav_needslink, "This ship requires \"linking\" for autopilot ."}, { Mission::Parse_Object_Flags::SF_Hide_ship_name, "If set, the ship name can't be seen when the ship is targeted."}, { Mission::Parse_Object_Flags::SF_Set_class_dynamically, "This ship should have its class assigned rather than simply read from the mission file."}, - { Mission::Parse_Object_Flags::SF_Lock_all_turrets_initially, "Lock all turrets on this ship at mission start or on arrival."}, + { Mission::Parse_Object_Flags::SF_Lock_all_turrets_initially, "Lock all turrets on this ship at mission start, on arrival, or on a class change."}, { Mission::Parse_Object_Flags::SF_Afterburner_locked, "Will stop a ship from firing their afterburner."}, { Mission::Parse_Object_Flags::OF_Force_shields_on, "Shields will be activated regardless of other flags."}, { Mission::Parse_Object_Flags::OF_Immobile, "Will not let a ship change position or orientation. Upon destruction the ship will still do the death roll and explosion."}, diff --git a/code/ship/ship.cpp b/code/ship/ship.cpp index b732d32e118..66b2bdc9019 100644 --- a/code/ship/ship.cpp +++ b/code/ship/ship.cpp @@ -11817,6 +11817,31 @@ static void ship_model_change(int n, int ship_type) } } +// runtime subsystem flags kept across a ship class change (rotation locks and cargo scans are not kept) +static const Ship::Subsystem_Flags Runtime_subsys_flags[] = +{ + Ship::Subsystem_Flags::Untargetable, + Ship::Subsystem_Flags::No_SS_targeting, + Ship::Subsystem_Flags::No_replace, + Ship::Subsystem_Flags::No_live_debris, + Ship::Subsystem_Flags::Vanished, + Ship::Subsystem_Flags::Missiles_ignore_if_dead +}; + +static const Ship::Weapon_Flags Runtime_subsys_weapon_flags[] = +{ + Ship::Weapon_Flags::Turret_Lock, + Ship::Weapon_Flags::Beam_Free, + Ship::Weapon_Flags::Tagged_Only +}; + +// which runtime flags differ from what the subsystem's class and the mission would give it, and their values +struct saved_runtime_subsys_flags +{ + flagset changed, values; + flagset weapon_changed, weapon_values; +}; + /** * Change the ship class on a ship, and changing all required information * for consistency (ie textures, subsystems, weapons, physics) @@ -11980,6 +12005,7 @@ void change_ship_type(int n, int ship_type, int by_sexp) char **subsys_names = new char *[sip_orig->n_subsystems]; float *subsys_pcts = new float[sip_orig->n_subsystems]; SCP_vector subsys_blown_off(sip_orig->n_subsystems, false); + SCP_vector subsys_runtime_flags(sip_orig->n_subsystems); // prevent crashes in the event of a subsystem mismatch for (i = 0; i < sip_orig->n_subsystems; ++i) @@ -12080,6 +12106,23 @@ void change_ship_type(int n, int ship_type, int by_sexp) subsys_blown_off[num_saved_subsystems] = subsystem_submodels_blown_off(sp, ss); + // only carry over what changed during the mission, so the old class's table settings don't override the new class's + { + flagset default_flags; + flagset default_weapon_flags; + resolve_subsystem_flags(*ss->system_info, default_flags, default_weapon_flags); + if (sp->flags[Ship::Ship_Flags::Lock_all_turrets_initially] && ss->system_info->type == SUBSYSTEM_TURRET) + default_weapon_flags.set(Ship::Weapon_Flags::Turret_Lock); + + auto &saved = subsys_runtime_flags[num_saved_subsystems]; + saved.values = ss->flags; + saved.weapon_values = ss->weapons.flags; + for (auto flag : Runtime_subsys_flags) + saved.changed.set(flag, ss->flags[flag] != default_flags[flag]); + for (auto flag : Runtime_subsys_weapon_flags) + saved.weapon_changed.set(flag, ss->weapons.flags[flag] != default_weapon_flags[flag]); + } + num_saved_subsystems++; ss = GET_NEXT(ss); } @@ -12183,6 +12226,16 @@ void change_ship_type(int n, int ship_type, int by_sexp) // fix up the subsystems subsys_set( sp->objnum ); + // lock-all-turrets is part of every turret's default, as at ship creation; a turret freed since then is restored below + if (sp->flags[Ship::Ship_Flags::Lock_all_turrets_initially]) + { + for (auto turret : list_range(&sp->subsys_list)) + { + if (turret->system_info->type == SUBSYSTEM_TURRET) + turret->weapons.flags.set(Ship::Weapon_Flags::Turret_Lock); + } + } + // Goober5000 - restore the subsystem percentages ss = GET_FIRST(&sp->subsys_list); int ss_index = 0; @@ -12196,6 +12249,18 @@ void change_ship_type(int n, int ship_type, int by_sexp) if (subsys_blown_off[i]) blow_off_subsystem_submodels(sp, ss); + const auto &saved = subsys_runtime_flags[i]; + for (auto flag : Runtime_subsys_flags) + { + if (saved.changed[flag]) + ss->flags.set(flag, saved.values[flag]); + } + for (auto flag : Runtime_subsys_weapon_flags) + { + if (saved.weapon_changed[flag]) + ss->weapons.flags.set(flag, saved.weapon_values[flag]); + } + // MageKing17 - Every AI doing something with this subsystem must transfer to the new one. { auto it = subsystem_matches.begin(); diff --git a/code/ship/ship_flags.h b/code/ship/ship_flags.h index aa84be85049..dd3d7e3e805 100644 --- a/code/ship/ship_flags.h +++ b/code/ship/ship_flags.h @@ -101,7 +101,7 @@ namespace Ship { Hide_ship_name, // Karajorma - Hides the ships name (like the -wcsaga command line used to but for any selected ship) Afterburner_locked, // KeldorKatarn - This ship can't use its afterburners Set_class_dynamically, // Karajorma - This ship should have its class assigned rather than simply read from the mission file - Lock_all_turrets_initially, // Karajorma - Lock all turrets on this ship at mission start or on arrival + Lock_all_turrets_initially, // Karajorma - Lock all turrets on this ship at mission start, on arrival, or on a class change Force_shields_on, No_ets, // The E - This ship does not have an ETS Cloaked, // The E - This ship will not be rendered From a6b03fc636026ad3dcb8646aedc04e2048d8767c Mon Sep 17 00:00:00 2001 From: Goober5000 Date: Fri, 13 Nov 2026 12:27:42 -0500 Subject: [PATCH 4/4] Keep a parse object's own subsystems in place when inserting into an 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 --- code/mission/missionparse.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/code/mission/missionparse.cpp b/code/mission/missionparse.cpp index 31243ff413a..8cb7d350bca 100644 --- a/code/mission/missionparse.cpp +++ b/code/mission/missionparse.cpp @@ -8884,6 +8884,10 @@ int insert_subsys_status(p_object *pobjp) // (each p_object's subsys_index points to subsystem 0 in its list) for (SCP_vector::iterator ii = Parse_objects.begin(); ii != Parse_objects.end(); ++ii) { + // skip this p_object: if it had no subsystems, its base index equals new_index but must stay put + if (&*ii == pobjp) + continue; + // bump up base index to accommodate inserted subsystem if (ii->subsys_index >= new_index) ii->subsys_index++;