From fad8ea64c3f61efacaf451169985df397f78044c Mon Sep 17 00:00:00 2001 From: Goober5000 Date: Wed, 18 Nov 2026 21:19:57 -0500 Subject: [PATCH 1/8] Add iff_close() so IFF tables can be re-parsed in tests Nothing reset the IFF module's state, so a second iff_init() in the same process failed on duplicate IFF names. IffAccessibilityTest (#7856) calls iff_init(), which broke any later test that also needed IFFs. Co-Authored-By: Claude Opus 5.5 --- code/iff_defs/iff_defs.cpp | 33 +++++++++++++++----- code/iff_defs/iff_defs.h | 3 ++ test/src/iff_defs/test_iff_accessibility.cpp | 9 +++++- 3 files changed, 36 insertions(+), 9 deletions(-) diff --git a/code/iff_defs/iff_defs.cpp b/code/iff_defs/iff_defs.cpp index 16378f3f16f..85787a84b25 100644 --- a/code/iff_defs/iff_defs.cpp +++ b/code/iff_defs/iff_defs.cpp @@ -33,6 +33,15 @@ int IFF_COLOR_TAGGED = 2; // global only to file SCP_vector > Iff_colors; // AL 1-2-97: Create two IFF colors, regular and bright +typedef struct { + int r; + int g; + int b; +} temp_color_t; + +// parallel to Iff_colors +static SCP_vector temp_colors; + flag_def_list rti_flags[] = { { "crosshairs", RTIF_CROSSHAIRS, 0 }, { "blink", RTIF_BLINK, 0 }, @@ -82,16 +91,8 @@ int iff_get_alpha_value(bool is_bright) */ int iff_init_color(int r, int g, int b) { - typedef struct { - int r; - int g; - int b; - } temp_color_t; - int i, idx; - static SCP_vector temp_colors; - Assert(r >= 0 && r <= 255); Assert(g >= 0 && g <= 255); Assert(b >= 0 && b <= 255); @@ -662,6 +663,22 @@ void iff_init() resolve_iff_data(); } +void iff_close() +{ + Iff_info.clear(); + Iff_info_names.clear(); + Iff_colors.clear(); + temp_colors.clear(); + + attack_names.clear(); + observed_color_table.clear(); + accessibility_observed_color_table.clear(); + + strcpy_s(traitor_name, "Traitor"); + Iff_traitor = 0; + AccessiblitySupported = false; +} + /** * Find the iff name * diff --git a/code/iff_defs/iff_defs.h b/code/iff_defs/iff_defs.h index 9b8ed5dfe7a..4111276bf5a 100644 --- a/code/iff_defs/iff_defs.h +++ b/code/iff_defs/iff_defs.h @@ -72,6 +72,9 @@ extern int iff_init_color(int r, int g, int b); // load the iff table extern void iff_init(); +// free the iff table +extern void iff_close(); + // search for iff extern int iff_lookup(const char *iff_name); diff --git a/test/src/iff_defs/test_iff_accessibility.cpp b/test/src/iff_defs/test_iff_accessibility.cpp index 2975b0efadc..332623c1250 100644 --- a/test/src/iff_defs/test_iff_accessibility.cpp +++ b/test/src/iff_defs/test_iff_accessibility.cpp @@ -14,6 +14,14 @@ class IffAccessibilityTest : public test::FSTestFixture { { pushModDir("iff/tbm_adds_iff"); } + + protected: + void TearDown() override + { + iff_close(); + + test::FSTestFixture::TearDown(); + } }; namespace { @@ -31,7 +39,6 @@ ::testing::AssertionResult has_rgb(int color_index, int r, int g, int b) } } // namespace -// one test: nothing clears Iff_info, so a second iff_init() in the process fails on duplicate IFF names TEST_F(IffAccessibilityTest, TbmIffsKeepTheirColorsAndBuiltInsUseAccessibilityColors) { iff_init(); From e776b779927a88a46f3d61256e3bd1396f663671 Mon Sep 17 00:00:00 2001 From: Goober5000 Date: Sun, 18 Oct 2026 15:03:04 -0400 Subject: [PATCH 2/8] Preserve internally-set flags when $Flags: is replaced 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 --- code/ship/ship.cpp | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/code/ship/ship.cpp b/code/ship/ship.cpp index b4e6907e499..5d298e108e4 100644 --- a/code/ship/ship.cpp +++ b/code/ship/ship.cpp @@ -3803,13 +3803,18 @@ static void parse_ship_values(ship_info* sip, const bool is_template, const bool if (!optional_string("+noreplace")) { // clear flags since we might have a modular table // clear only those which are actually set in the flags - bool has_afterburner = sip->flags[Ship::Info_Flags::Afterburner]; - bool draw_weapon_models = sip->flags[Ship::Info_Flags::Draw_weapon_models]; - bool has_display_name = sip->flags[Ship::Info_Flags::Has_display_name]; - sip->flags.reset(); - sip->flags.set(Ship::Info_Flags::Afterburner, has_afterburner); - sip->flags.set(Ship::Info_Flags::Draw_weapon_models, draw_weapon_models); - sip->flags.set(Ship::Info_Flags::Has_display_name, has_display_name); + // be careful to keep this up to date with any flag that $Flags itself cannot set + const Ship::Info_Flags preset_si_flag_defs[] = { + Ship::Info_Flags::Afterburner, + Ship::Info_Flags::Draw_weapon_models, + Ship::Info_Flags::Has_display_name, + Ship::Info_Flags::Has_awacs, + Ship::Info_Flags::Allow_landings + }; + + flagset cleared_si_flags; + cleared_si_flags.set_multiple_from_source(sip->flags, std::begin(preset_si_flag_defs), std::end(preset_si_flag_defs)); + sip->flags = cleared_si_flags; } for (const auto &flag : ship_strings) @@ -5278,7 +5283,16 @@ static void parse_ship_values(ship_info* sip, const bool is_template, const bool sp->flags |= tmp_flags; } else { - sp->flags = tmp_flags; + // be careful to keep this up to date with any flag that $Flags itself cannot set + const Model::Subsystem_Flags preset_subsys_flag_defs[] = { + Model::Subsystem_Flags::Turret_barrel_fov_overridden, + Model::Subsystem_Flags::Turret_base_fov_overridden, + Model::Subsystem_Flags::Turret_max_fov_overridden + }; + + flagset preset_subsys_flags; + preset_subsys_flags.set_multiple_from_source(sp->flags, std::begin(preset_subsys_flag_defs), std::end(preset_subsys_flag_defs)); + sp->flags = tmp_flags | preset_subsys_flags; } if (!errors.empty()) { From 420b7c0edb2ae4111367f5f89ccd3289acdf5751 Mon Sep 17 00:00:00 2001 From: Goober5000 Date: Sun, 18 Oct 2026 15:03:11 -0400 Subject: [PATCH 3/8] Copy all POF-derived subsystem data between ship classes sharing a model 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 --- code/model/model.h | 3 ++ code/model/modelread.cpp | 85 ++++++++++++++++++++++++---------------- 2 files changed, 54 insertions(+), 34 deletions(-) diff --git a/code/model/model.h b/code/model/model.h index dd16843f26a..c303e3ec94f 100644 --- a/code/model/model.h +++ b/code/model/model.h @@ -260,6 +260,9 @@ class model_subsystem { /* contains rotation rate info */ float turret_fov; // dot of turret_norm:vec_to_enemy > this means can see float turret_max_fov; // dot of turret_norm:vec_to_enemy <= this means barrels can elevate up to the target float turret_base_fov; // turret's base's fov, -1 means no restriction + float pof_turret_fov; // turret_fov as read from the model, before any ships.tbl override + float pof_turret_max_fov; // turret_max_fov as read from the model, before any ships.tbl override + float pof_turret_base_fov; // turret_base_fov as read from the model, before any ships.tbl override int turret_num_firing_points; // number of firing points on this turret vec3d turret_firing_point[MAX_TFP]; // in parent object's reference frame, point from which to fire. int turret_gun_sobj; // Which subobject in this model the firing points are linked to. diff --git a/code/model/modelread.cpp b/code/model/modelread.cpp index 43b3390e781..b2133b0ce2c 100644 --- a/code/model/modelread.cpp +++ b/code/model/modelread.cpp @@ -605,6 +605,17 @@ bool in(char *&p, char *str, const char *substr) return p != nullptr; } +// copy the model's turret FOVs into the live values, except for any that ships.tbl has overridden +static void model_apply_pof_turret_fovs(model_subsystem *subsystemp) +{ + if (!subsystemp->flags[Model::Subsystem_Flags::Turret_barrel_fov_overridden]) + subsystemp->turret_fov = subsystemp->pof_turret_fov; + if (!subsystemp->flags[Model::Subsystem_Flags::Turret_base_fov_overridden]) + subsystemp->turret_base_fov = subsystemp->pof_turret_base_fov; + if (!subsystemp->flags[Model::Subsystem_Flags::Turret_max_fov_overridden]) + subsystemp->turret_max_fov = subsystemp->pof_turret_max_fov; +} + const Model::Subsystem_Flags carry_flags[] = { Model::Subsystem_Flags::Crewpoint, Model::Subsystem_Flags::Rotates, Model::Subsystem_Flags::Translates, Model::Subsystem_Flags::Triggered, Model::Subsystem_Flags::Artillery, Model::Subsystem_Flags::Stepped_rotate, Model::Subsystem_Flags::Stepped_translate }; // Function to copy model data from one subsystem set to another subsystem set. This function // is called when two ships use the same model data, but since the model only gets read in one time, @@ -631,13 +642,21 @@ void model_copy_subsystems( int n_subsystems, model_subsystem *d_sp, model_subsy dest->radius = source->radius; dest->type = source->type; dest->turret_gun_sobj = source->turret_gun_sobj; + dest->path_num = source->path_num; + dest->weapon_rotation_pbank = source->weapon_rotation_pbank; + dest->stepped_rotation = source->stepped_rotation; + dest->stepped_translation = source->stepped_translation; strcpy_s(dest->name, source->name); if ( dest->type == SUBSYSTEM_TURRET ) { int nfp; - dest->turret_fov = source->turret_fov; + dest->pof_turret_fov = source->pof_turret_fov; + dest->pof_turret_base_fov = source->pof_turret_base_fov; + dest->pof_turret_max_fov = source->pof_turret_max_fov; + model_apply_pof_turret_fovs(dest); + dest->turret_num_firing_points = source->turret_num_firing_points; dest->turret_norm = source->turret_norm; @@ -691,39 +710,34 @@ void set_subsystem_info(int model_num, model_subsystem *subsystemp, char *props, subsystemp->type = SUBSYSTEM_TURRET; - // don't set the turret FOV values if they have already been set (e.g. through ships.tbl) - if (!subsystemp->flags[Model::Subsystem_Flags::Turret_barrel_fov_overridden]) { - if (in(p, props, "$fov")) { - get_user_prop_value(p + 4, buf); // get the value of the fov - float value = (float)atoi(buf); - CLAMP(value, 0.0f, 360.0f); - float angle = fl_radians(value) / 2.0f; - subsystemp->turret_fov = cosf(angle); - } else - subsystemp->turret_fov = 0.0f; - } - - if (!subsystemp->flags[Model::Subsystem_Flags::Turret_base_fov_overridden]) { - if (in(p, props, "$base_fov")) { - get_user_prop_value(p + 9, buf); // get the value of the fov - float value = (float)atoi(buf); - CLAMP(value, 0.0f, 360.0f); - float angle = fl_radians(value) / 2.0f; - subsystemp->turret_base_fov = cosf(angle); - } else - subsystemp->turret_base_fov = -1.0f; - } - - if (!subsystemp->flags[Model::Subsystem_Flags::Turret_max_fov_overridden]) { - if (in(p, props, "$max_fov")) { - get_user_prop_value(p + 8, buf); // get the value of the fov - float value = (float)atoi(buf); - CLAMP(value, 0.0f, 90.0f); - float angle = PI_2 - fl_radians(value); - subsystemp->turret_max_fov = cosf(angle); - } else - subsystemp->turret_max_fov = 1.0f; - } + if (in(p, props, "$fov")) { + get_user_prop_value(p + 4, buf); // get the value of the fov + float value = i2fl(atoi(buf)); + CLAMP(value, 0.0f, 360.0f); + float angle = fl_radians(value) / 2.0f; + subsystemp->pof_turret_fov = cosf(angle); + } else + subsystemp->pof_turret_fov = 0.0f; + + if (in(p, props, "$base_fov")) { + get_user_prop_value(p + 9, buf); // get the value of the fov + float value = i2fl(atoi(buf)); + CLAMP(value, 0.0f, 360.0f); + float angle = fl_radians(value) / 2.0f; + subsystemp->pof_turret_base_fov = cosf(angle); + } else + subsystemp->pof_turret_base_fov = -1.0f; + + if (in(p, props, "$max_fov")) { + get_user_prop_value(p + 8, buf); // get the value of the fov + float value = i2fl(atoi(buf)); + CLAMP(value, 0.0f, 90.0f); + float angle = PI_2 - fl_radians(value); + subsystemp->pof_turret_max_fov = cosf(angle); + } else + subsystemp->pof_turret_max_fov = 1.0f; + + model_apply_pof_turret_fovs(subsystemp); subsystemp->turret_num_firing_points = 0; @@ -6223,6 +6237,9 @@ void model_subsystem::reset() turret_fov = 0; turret_max_fov = 1; turret_base_fov = -1; + pof_turret_fov = 0; + pof_turret_max_fov = 1; + pof_turret_base_fov = -1; turret_num_firing_points = 0; for (auto it = std::begin(turret_firing_point); it != std::end(turret_firing_point); ++it) it->xyz.x = it->xyz.y = it->xyz.z = 0.0f; From 7e8674c65cf6f0db15cb8c87960257405c0fffad Mon Sep 17 00:00:00 2001 From: Goober5000 Date: Sun, 18 Oct 2026 20:54:25 -0400 Subject: [PATCH 4/8] Look up engine wash per ship class 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 --- code/model/model.h | 3 +-- code/model/modelread.cpp | 10 ++++++---- code/ship/shipfx.cpp | 7 +++---- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/code/model/model.h b/code/model/model.h index c303e3ec94f..de0cc519c2d 100644 --- a/code/model/model.h +++ b/code/model/model.h @@ -606,8 +606,7 @@ typedef struct thruster_bank { int num_points; std::shared_ptr points; - // Engine wash info - struct engine_wash_info *wash_info_pointer; // index into Engine_wash_info + int subsys_num; // engine subsystem for this bank, used for engine wash; index to ship_info->subsystems of the class that loaded the model int obj_num; // what subsystem number this bank is on; index to ship_info->subsystems int submodel_num; // what submodel number this bank is on; index to polymodel->submodel/polymodel_instance->submodel } thruster_bank; diff --git a/code/model/modelread.cpp b/code/model/modelread.cpp index b2133b0ce2c..1180502c5a6 100644 --- a/code/model/modelread.cpp +++ b/code/model/modelread.cpp @@ -2568,9 +2568,9 @@ modelread_status read_model_file_no_subsys(polymodel * pm, const char* filename, if (bank->num_points > 0) bank->points = make_shared(bank->num_points); + bank->subsys_num = -1; bank->obj_num = -1; bank->submodel_num = -1; - bank->wash_info_pointer = nullptr; if (pm->version >= 2117) { cfread_string_len( props, MAX_PROP_LEN, fp ); @@ -3182,13 +3182,15 @@ modelread_status read_and_process_model_file(polymodel* pm, const char* filename // start off assuming the subsys is invalid int table_error = 1; auto bank = &pm->thrusters[subsystem.first]; + engine_wash_info *wash_info_pointer = nullptr; for (int k = 0; k < n_subsystems; k++) { if (!subsystem_stricmp(subsystems[k].subobj_name, subsystem.second.subsystem_name.c_str())) { bank->submodel_num = subsystems[k].subobj_num; + bank->subsys_num = k; - bank->wash_info_pointer = subsystems[k].engine_wash_pointer; - if (bank->wash_info_pointer != nullptr) { + wash_info_pointer = subsystems[k].engine_wash_pointer; + if (wash_info_pointer != nullptr) { table_error = 0; } // also set what subsystem this is attached to but not if we only have one thruster bank @@ -3200,7 +3202,7 @@ modelread_status read_and_process_model_file(polymodel* pm, const char* filename } } - if ((bank->wash_info_pointer == nullptr) && (n_subsystems > 0)) { + if ((wash_info_pointer == nullptr) && (n_subsystems > 0)) { if (table_error) { // Warning(LOCATION, "No engine wash table entry in ships.tbl for ship model %s", filename); } diff --git a/code/ship/shipfx.cpp b/code/ship/shipfx.cpp index 3ead10b9cc2..9a64467e817 100644 --- a/code/ship/shipfx.cpp +++ b/code/ship/shipfx.cpp @@ -2712,17 +2712,16 @@ void engine_wash_ship_process(ship *shipp) } // check if thruster bank has engine wash - if (bank->wash_info_pointer == NULL) { + engine_wash_info *ewp = (bank->subsys_num >= 0 && bank->subsys_num < wash_sip->n_subsystems) ? wash_sip->subsystems[bank->subsys_num].engine_wash_pointer : nullptr; + if (ewp == nullptr) { // if huge, give default engine wash if ((wash_sip->is_huge_ship()) && !Engine_wash_info.empty()) { - bank->wash_info_pointer = &Engine_wash_info[0]; - nprintf(("wash", "Adding default engine wash to ship %s", wash_sip->name)); + ewp = Engine_wash_info.data(); } else { continue; } } - engine_wash_info *ewp = bank->wash_info_pointer; half_angle = ewp->angle; radius_mult = ewp->radius_mult; float wash_intensity; From e59e218753242b72f2469efa004115933af8b5c8 Mon Sep 17 00:00:00 2001 From: Goober5000 Date: Sun, 18 Oct 2026 20:54:50 -0400 Subject: [PATCH 5/8] Handle ship classes sharing a model with different subsystem lists 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 --- code/model/model.h | 4 ++-- code/model/modelinterp.cpp | 4 +++- code/model/modelread.cpp | 27 +++++++++++++-------------- code/ship/ship.cpp | 25 +++++++++++++++++++------ 4 files changed, 37 insertions(+), 23 deletions(-) diff --git a/code/model/model.h b/code/model/model.h index de0cc519c2d..9f142d2d072 100644 --- a/code/model/model.h +++ b/code/model/model.h @@ -1080,8 +1080,8 @@ void model_instance_snapshot_prev_all(); // Flags a submodel, and with it all its children, as Can_move void model_set_submodel_can_move(polymodel* pm, int submodel_num); -// routine to copy subsystems. Must be called when subsystems sets are the same -- see ship.cpp -void model_copy_subsystems(int n_subsystems, model_subsystem *d_sp, model_subsystem *s_sp); +// routine to copy subsystems, matched by name, between ship classes that share a model -- see ship.cpp +void model_copy_subsystems(int n_dest, model_subsystem *d_sp, int n_source, const model_subsystem *s_sp); // Flags you can pass to model_render #define MR_NORMAL (0) // Draw a normal object diff --git a/code/model/modelinterp.cpp b/code/model/modelinterp.cpp index 8b0b416a2b7..089e9fe3e23 100644 --- a/code/model/modelinterp.cpp +++ b/code/model/modelinterp.cpp @@ -1751,7 +1751,9 @@ int model_should_render_engine_glow(int objnum, int bank_obj) ship *shipp = &Ships[obj->instance]; ship_info *sip = &Ship_info[shipp->ship_info_index]; - Assert( bank_obj < sip->n_subsystems ); + // a class whose subsystems differ from those of the class that loaded the model may not have this subsystem + if (bank_obj >= sip->n_subsystems) + return 1; char subname[MAX_NAME_LEN]; // shipp->subsystems isn't always valid here so don't use it diff --git a/code/model/modelread.cpp b/code/model/modelread.cpp index 1180502c5a6..3cfede14fef 100644 --- a/code/model/modelread.cpp +++ b/code/model/modelread.cpp @@ -621,16 +621,22 @@ const Model::Subsystem_Flags carry_flags[] = { Model::Subsystem_Flags::Crewpoint // is called when two ships use the same model data, but since the model only gets read in one time, // the subsystem data is only present in one location. The ship code will call this routine to fix // this situation by copying stuff from the source subsystem set to the dest subsystem set. -void model_copy_subsystems( int n_subsystems, model_subsystem *d_sp, model_subsystem *s_sp ) +void model_copy_subsystems( int n_dest, model_subsystem *d_sp, int n_source, const model_subsystem *s_sp ) { int i, j; - model_subsystem *source, *dest; - - for (i = 0; i < n_subsystems; i++ ) { - source = &s_sp[i]; - for ( j = 0; j < n_subsystems; j++ ) { - dest = &d_sp[j]; + const model_subsystem *source; + model_subsystem *dest; + + // a destination subsystem with no match in the source stays unlinked, which is reported when a ship of that class is created + for (i = 0; i < n_dest; i++ ) { + dest = &d_sp[i]; + for ( j = 0; j < n_source; j++ ) { + source = &s_sp[j]; if ( !subsystem_stricmp( source->subobj_name, dest->subobj_name) ) { + // an unlinked source has nothing to copy, and copying it would unlink a destination that is already linked + if ( source->model_num == -1 ) + break; + for (auto const &flag : carry_flags) { if (source->flags[flag]) dest->flags.set(flag); @@ -669,13 +675,6 @@ void model_copy_subsystems( int n_subsystems, model_subsystem *d_sp, model_subsy break; } } - if ( j == n_subsystems ) - Error(LOCATION, "Subsystem '%s' could not be matched between two ship classes that share a model. " - "The destination ship will be missing this subsystem at runtime. " - "Check that subsystem names are spelled identically on both ship classes, " - "and that any modular table extensions to one ship are mirrored on the other.", - source->subobj_name); - } } diff --git a/code/ship/ship.cpp b/code/ship/ship.cpp index 5d298e108e4..79ad863975a 100644 --- a/code/ship/ship.cpp +++ b/code/ship/ship.cpp @@ -7340,26 +7340,35 @@ static void ship_copy_subsystem_fixup(ship_info *sip) for ( auto it = Ship_info.begin(); it != Ship_info.end(); ++it ) { model_subsystem *source_msp, *dest_msp; - if ( (it->model_num != model_num) || (&(*it) == sip) ){ + // a class with no subsystems can neither provide nor receive any subsystem data + if ( (it->model_num != model_num) || (&(*it) == sip) || (it->n_subsystems == 0) ){ continue; } // see if this ship has subsystems and a model for the subsystems. We only need check the first // subsystem since previous error checking would have trapped its loading as an error. - Assert( it->n_subsystems == sip->n_subsystems ); - source_msp = &(it->subsystems[0]); dest_msp = &(sip->subsystems[0]); if (source_msp->model_num != -1) { - model_copy_subsystems( sip->n_subsystems, dest_msp, source_msp ); + model_copy_subsystems( sip->n_subsystems, dest_msp, it->n_subsystems, source_msp ); } else if (dest_msp->model_num != -1) { - model_copy_subsystems( sip->n_subsystems, source_msp, dest_msp ); + model_copy_subsystems( it->n_subsystems, source_msp, sip->n_subsystems, dest_msp ); } else { // if none were found try finding a another ship to copy the data from continue; } sip->flags.set(Ship::Info_Flags::Path_fixup); - break; + + // a class with a different subsystem list may link only some of ours, so keep looking until all are linked + bool all_linked = true; + for (int i = 0; i < sip->n_subsystems; i++) { + if (sip->subsystems[i].model_num == -1) { + all_linked = false; + break; + } + } + if (all_linked) + break; } } @@ -21957,6 +21966,10 @@ ship_subsys* ship_get_subsys_for_submodel(ship* shipp, int submodel) if (pm->submodel[submodel].subsys_num == -1) return nullptr; + // a class whose subsystems differ from those of the class that loaded the model may not have this subsystem + if (pm->submodel[submodel].subsys_num >= Ship_info[shipp->ship_info_index].n_subsystems) + return nullptr; + return ship_get_indexed_subsys(shipp, pm->submodel[submodel].subsys_num); } From 171f170afc23c27fec0759af479cfcdf4b4d2495 Mon Sep 17 00:00:00 2001 From: Goober5000 Date: Sun, 18 Oct 2026 20:55:00 -0400 Subject: [PATCH 6/8] Warn when ship classes sharing a model have different subsystem lists 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 --- code/ship/ship.cpp | 43 +++++++++++++ test/src/ship/test_shared_model.cpp | 64 +++++++++++++++++++ test/src/source_groups.cmake | 1 + .../extra_subsystem/data/tables/ships.tbl | 16 +++++ .../data/tables/ships.tbl | 15 +++++ .../ship/no_subsystems/data/tables/ships.tbl | 13 ++++ .../data/tables/ships.tbl | 15 +++++ 7 files changed, 167 insertions(+) create mode 100644 test/src/ship/test_shared_model.cpp create mode 100644 test/test_data/ship/extra_subsystem/data/tables/ships.tbl create mode 100644 test/test_data/ship/identical_subsystems/data/tables/ships.tbl create mode 100644 test/test_data/ship/no_subsystems/data/tables/ships.tbl create mode 100644 test/test_data/ship/reordered_subsystems/data/tables/ships.tbl diff --git a/code/ship/ship.cpp b/code/ship/ship.cpp index 79ad863975a..aba7310c33a 100644 --- a/code/ship/ship.cpp +++ b/code/ship/ship.cpp @@ -5884,6 +5884,47 @@ static bool ballistic_possible_for_this_ship(const ship_info *sip) return false; } +/** + * Ship classes that share a model share the model's subsystem data, which is indexed by subsystem position, + * so every class using a model should list the same subsystems in the same order + */ +static void ship_check_shared_model_subsystems() +{ + SCP_unordered_map first_class_for_model; + + for (const auto &si : Ship_info) + { + if (!VALID_FNAME(si.pof_file)) + continue; + + auto result = first_class_for_model.emplace(si.pof_file, &si); + if (result.second) + continue; + + auto first = result.first->second; + if (si.n_subsystems != first->n_subsystems) + { + Warning(LOCATION, "Ship classes '%s' and '%s' share the model '%s', but '%s' has %d subsystems and '%s' has %d.\n\n" + "Ship classes that share a model should list the same subsystems in the same order. Otherwise, hits and engine effects " + "may be applied to the wrong subsystem, and a ship may fail to load if its class has subsystems the other class lacks.", + first->name, si.name, si.pof_file, first->name, static_cast(first->n_subsystems), si.name, static_cast(si.n_subsystems)); + continue; + } + + for (int i = 0; i < si.n_subsystems; ++i) + { + if (subsystem_stricmp(first->subsystems[i].subobj_name, si.subsystems[i].subobj_name) != 0) + { + Warning(LOCATION, "Ship classes '%s' and '%s' share the model '%s', but subsystem %d is '%s' on '%s' and '%s' on '%s'.\n\n" + "Ship classes that share a model should list the same subsystems in the same order. Otherwise, hits and engine effects " + "may be applied to the wrong subsystem.", + first->name, si.name, si.pof_file, i + 1, first->subsystems[i].subobj_name, first->name, si.subsystems[i].subobj_name, si.name); + break; + } + } + } +} + /** * Clean up ship entries, making sure various flags and settings are correct */ @@ -5990,6 +6031,8 @@ static void ship_parse_post_cleanup() } } + ship_check_shared_model_subsystems(); + // check also target groups here size_t n_tgt_groups = Ai_tp_list.size(); diff --git a/test/src/ship/test_shared_model.cpp b/test/src/ship/test_shared_model.cpp new file mode 100644 index 00000000000..506f951594f --- /dev/null +++ b/test/src/ship/test_shared_model.cpp @@ -0,0 +1,64 @@ + +#include + +#include "util/FSTestFixture.h" +#include "util/test_util.h" + +#include "iff_defs/iff_defs.h" +#include "ship/ship.h" +#include "species_defs/species_defs.h" + +class ShipSharedModelTest : public test::FSTestFixture +{ + public: + ShipSharedModelTest() : test::FSTestFixture(INIT_CFILE) + { + pushModDir("ship"); + } + + protected: + void SetUp() override + { + test::FSTestFixture::SetUp(); + + // ship parsing reads species data, which needs the IFFs + iff_init(); + species_init(); + } + void TearDown() override + { + ship_close(); + iff_close(); + + test::FSTestFixture::TearDown(); + } +}; + +TEST_F(ShipSharedModelTest, identical_subsystems) +{ + DEBUG_TEST(); + + ASSERT_NO_THROW(ship_init()); + ASSERT_EQ(Ship_info.size(), 2u); +} + +TEST_F(ShipSharedModelTest, reordered_subsystems) +{ + DEBUG_TEST(); + + ASSERT_THROW(ship_init(), os::dialogs::WarningException); +} + +TEST_F(ShipSharedModelTest, extra_subsystem) +{ + DEBUG_TEST(); + + ASSERT_THROW(ship_init(), os::dialogs::WarningException); +} + +TEST_F(ShipSharedModelTest, no_subsystems) +{ + DEBUG_TEST(); + + ASSERT_THROW(ship_init(), os::dialogs::WarningException); +} diff --git a/test/src/source_groups.cmake b/test/src/source_groups.cmake index f0ca41e3802..fd5e6e6deb5 100644 --- a/test/src/source_groups.cmake +++ b/test/src/source_groups.cmake @@ -119,6 +119,7 @@ add_file_folder("Scripting\\\\Lua" add_file_folder("Ship" ship/ship_info.cpp + ship/test_shared_model.cpp ) add_file_folder("Starfield" diff --git a/test/test_data/ship/extra_subsystem/data/tables/ships.tbl b/test/test_data/ship/extra_subsystem/data/tables/ships.tbl new file mode 100644 index 00000000000..a3047cc1fcd --- /dev/null +++ b/test/test_data/ship/extra_subsystem/data/tables/ships.tbl @@ -0,0 +1,16 @@ +#Ship Classes + +$Name: Test Ship A +$POF file: test_ship.pof +$Closeup_pos: 0, 0, -10 +$Subsystem: engine, 10, 0 +$Subsystem: sensors, 10, 0 + +$Name: Test Ship B +$POF file: test_ship.pof +$Closeup_pos: 0, 0, -10 +$Subsystem: engine, 10, 0 +$Subsystem: sensors, 10, 0 +$Subsystem: communications, 10, 0 + +#End diff --git a/test/test_data/ship/identical_subsystems/data/tables/ships.tbl b/test/test_data/ship/identical_subsystems/data/tables/ships.tbl new file mode 100644 index 00000000000..14c0d3bb875 --- /dev/null +++ b/test/test_data/ship/identical_subsystems/data/tables/ships.tbl @@ -0,0 +1,15 @@ +#Ship Classes + +$Name: Test Ship A +$POF file: test_ship.pof +$Closeup_pos: 0, 0, -10 +$Subsystem: engine, 10, 0 +$Subsystem: sensors, 10, 0 + +$Name: Test Ship B +$POF file: test_ship.pof +$Closeup_pos: 0, 0, -10 +$Subsystem: engine, 10, 0 +$Subsystem: sensors, 10, 0 + +#End diff --git a/test/test_data/ship/no_subsystems/data/tables/ships.tbl b/test/test_data/ship/no_subsystems/data/tables/ships.tbl new file mode 100644 index 00000000000..4b78fa36e65 --- /dev/null +++ b/test/test_data/ship/no_subsystems/data/tables/ships.tbl @@ -0,0 +1,13 @@ +#Ship Classes + +$Name: Test Ship A +$POF file: test_ship.pof +$Closeup_pos: 0, 0, -10 +$Subsystem: engine, 10, 0 +$Subsystem: sensors, 10, 0 + +$Name: Test Ship B +$POF file: test_ship.pof +$Closeup_pos: 0, 0, -10 + +#End diff --git a/test/test_data/ship/reordered_subsystems/data/tables/ships.tbl b/test/test_data/ship/reordered_subsystems/data/tables/ships.tbl new file mode 100644 index 00000000000..0dfa2ae49de --- /dev/null +++ b/test/test_data/ship/reordered_subsystems/data/tables/ships.tbl @@ -0,0 +1,15 @@ +#Ship Classes + +$Name: Test Ship A +$POF file: test_ship.pof +$Closeup_pos: 0, 0, -10 +$Subsystem: engine, 10, 0 +$Subsystem: sensors, 10, 0 + +$Name: Test Ship B +$POF file: test_ship.pof +$Closeup_pos: 0, 0, -10 +$Subsystem: sensors, 10, 0 +$Subsystem: engine, 10, 0 + +#End From 03d374e5e1a48329136a624cccf5e9e00dba2865 Mon Sep 17 00:00:00 2001 From: Goober5000 Date: Fri, 23 Oct 2026 20:26:12 -0400 Subject: [PATCH 7/8] Store engine wash as an index rather than a pointer 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 --- code/model/model.h | 2 +- code/model/modelread.cpp | 10 +++++----- code/ship/ship.cpp | 33 ++++++++++++++++----------------- code/ship/shipfx.cpp | 7 ++++--- 4 files changed, 26 insertions(+), 26 deletions(-) diff --git a/code/model/model.h b/code/model/model.h index 9f142d2d072..4c285d48375 100644 --- a/code/model/model.h +++ b/code/model/model.h @@ -279,7 +279,7 @@ class model_subsystem { /* contains rotation rate info */ gamesnd_id rotation_snd; //Sound to make when the subsystem is rotating. (ie turrets) // engine wash info - struct engine_wash_info *engine_wash_pointer; // index into Engine_wash_info + int engine_wash_index; // index into Engine_wash_info, or -1 // movement specific info int weapon_rotation_pbank; // weapon-controlled rotation - Goober5000 diff --git a/code/model/modelread.cpp b/code/model/modelread.cpp index 3cfede14fef..1dcd7eb191d 100644 --- a/code/model/modelread.cpp +++ b/code/model/modelread.cpp @@ -3181,15 +3181,15 @@ modelread_status read_and_process_model_file(polymodel* pm, const char* filename // start off assuming the subsys is invalid int table_error = 1; auto bank = &pm->thrusters[subsystem.first]; - engine_wash_info *wash_info_pointer = nullptr; + int wash_index = -1; for (int k = 0; k < n_subsystems; k++) { if (!subsystem_stricmp(subsystems[k].subobj_name, subsystem.second.subsystem_name.c_str())) { bank->submodel_num = subsystems[k].subobj_num; bank->subsys_num = k; - wash_info_pointer = subsystems[k].engine_wash_pointer; - if (wash_info_pointer != nullptr) { + wash_index = subsystems[k].engine_wash_index; + if (wash_index >= 0) { table_error = 0; } // also set what subsystem this is attached to but not if we only have one thruster bank @@ -3201,7 +3201,7 @@ modelread_status read_and_process_model_file(polymodel* pm, const char* filename } } - if ((wash_info_pointer == nullptr) && (n_subsystems > 0)) { + if ((wash_index < 0) && (n_subsystems > 0)) { if (table_error) { // Warning(LOCATION, "No engine wash table entry in ships.tbl for ship model %s", filename); } @@ -6255,7 +6255,7 @@ void model_subsystem::reset() dead_snd = gamesnd_id(); rotation_snd = gamesnd_id(); - engine_wash_pointer = NULL; + engine_wash_index = -1; weapon_rotation_pbank = 0; stepped_rotation.reset(); diff --git a/code/ship/ship.cpp b/code/ship/ship.cpp index aba7310c33a..0639b135d13 100644 --- a/code/ship/ship.cpp +++ b/code/ship/ship.cpp @@ -153,7 +153,7 @@ char TVT_wing_names[MAX_TVT_WINGS][NAME_LENGTH]; SCP_vector Engine_wash_info; -static engine_wash_info *get_engine_wash_pointer(char* engine_wash_name); +static int get_engine_wash_index(const char* engine_wash_name); static int subsys_set(int objnum, int ignore_subsys_info = 0); static void ship_add_cockpit_display(cockpit_display_info *display, int cockpit_model_num); static void ship_set_hud_cockpit_targets(); @@ -349,7 +349,6 @@ void ship_registry_rename(int entry_index, const char *new_name, bool erase_old_ } -int Num_engine_wash_types; int Num_ship_subobj_types; int Num_ship_subobjects; int Player_ship_class; // needs to be player specific, move to player structure @@ -1007,10 +1006,12 @@ static void parse_engine_wash(bool replace) //Does this engine wash exist already? //If so, load this new info into it - //Otherwise, increment Num_engine_wash_types - ewp = get_engine_wash_pointer(ewt.name); - if(ewp != NULL) + //Otherwise, add it + int idx = get_engine_wash_index(ewt.name); + if (idx >= 0) { + ewp = &Engine_wash_info[idx]; + if(replace) { nprintf(("Warning", "More than one version of engine wash %s exists; using newer version.", ewt.name)); @@ -1031,7 +1032,7 @@ static void parse_engine_wash(bool replace) return; } Engine_wash_info.push_back(ewt); - ewp = &Engine_wash_info[Num_engine_wash_types++]; + ewp = &Engine_wash_info.back(); } @@ -5013,7 +5014,7 @@ static void parse_ship_values(ship_info* sip, const bool is_template, const bool sp->secondary_bank_capacity[i] = 0; } - sp->engine_wash_pointer = NULL; + sp->engine_wash_index = -1; sp->alive_snd = gamesnd_id(); sp->dead_snd = gamesnd_id(); @@ -5111,9 +5112,9 @@ static void parse_ship_values(ship_info* sip, const bool is_template, const bool if (optional_string("$Engine Wash:")) { stuff_string(name_tmp, F_NAME, sizeof(name_tmp)); // get and set index - sp->engine_wash_pointer = get_engine_wash_pointer(name_tmp); + sp->engine_wash_index = get_engine_wash_index(name_tmp); - if(sp->engine_wash_pointer == NULL) + if (sp->engine_wash_index < 0) WarningEx(LOCATION,"Invalid engine wash name %s specified for subsystem %s in %s '%s'", name_tmp, sp->subobj_name, info_type_name, sip->name); } @@ -5410,18 +5411,16 @@ static void parse_ship_values(ship_info* sip, const bool is_template, const bool } } -static engine_wash_info *get_engine_wash_pointer(char *engine_wash_name) +static int get_engine_wash_index(const char *engine_wash_name) { - for(int i = 0; i < Num_engine_wash_types; i++) + for (size_t i = 0; i < Engine_wash_info.size(); ++i) { - if(!stricmp(engine_wash_name, Engine_wash_info[i].name)) - { - return &Engine_wash_info[i]; - } + if (!stricmp(engine_wash_name, Engine_wash_info[i].name)) + return sz2i(i); } //Didn't find anything. - return NULL; + return -1; } static void parse_ship_type(const char *filename, const bool replace) @@ -6128,7 +6127,7 @@ void ship_init() //ships.tbl { - Num_engine_wash_types = 0; + Engine_wash_info.clear(); strcpy_s(default_player_ship, ""); //Parse main TBL first diff --git a/code/ship/shipfx.cpp b/code/ship/shipfx.cpp index 9a64467e817..fccb0a67a1c 100644 --- a/code/ship/shipfx.cpp +++ b/code/ship/shipfx.cpp @@ -2712,16 +2712,17 @@ void engine_wash_ship_process(ship *shipp) } // check if thruster bank has engine wash - engine_wash_info *ewp = (bank->subsys_num >= 0 && bank->subsys_num < wash_sip->n_subsystems) ? wash_sip->subsystems[bank->subsys_num].engine_wash_pointer : nullptr; - if (ewp == nullptr) { + int wash_index = (bank->subsys_num >= 0 && bank->subsys_num < wash_sip->n_subsystems) ? wash_sip->subsystems[bank->subsys_num].engine_wash_index : -1; + if (wash_index < 0) { // if huge, give default engine wash if ((wash_sip->is_huge_ship()) && !Engine_wash_info.empty()) { - ewp = Engine_wash_info.data(); + wash_index = 0; } else { continue; } } + const engine_wash_info *ewp = &Engine_wash_info[wash_index]; half_angle = ewp->angle; radius_mult = ewp->radius_mult; float wash_intensity; From 46150f58b68ce496b2a02af4f346ea2f830d460f Mon Sep 17 00:00:00 2001 From: Goober5000 Date: Wed, 18 Nov 2026 22:47:14 -0500 Subject: [PATCH 8/8] Add a test that replacing $Flags: keeps flags set by other fields 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 --- test/src/ship/test_ship_flags.cpp | 61 +++++++++++++++++++ test/src/source_groups.cmake | 1 + .../data/tables/replace-shp.tbm | 9 +++ .../ship/replaced_flags/data/tables/ships.tbl | 19 ++++++ 4 files changed, 90 insertions(+) create mode 100644 test/src/ship/test_ship_flags.cpp create mode 100644 test/test_data/ship/replaced_flags/data/tables/replace-shp.tbm create mode 100644 test/test_data/ship/replaced_flags/data/tables/ships.tbl diff --git a/test/src/ship/test_ship_flags.cpp b/test/src/ship/test_ship_flags.cpp new file mode 100644 index 00000000000..cfc306ff829 --- /dev/null +++ b/test/src/ship/test_ship_flags.cpp @@ -0,0 +1,61 @@ + +#include + +#include "util/FSTestFixture.h" +#include "util/test_util.h" + +#include "iff_defs/iff_defs.h" +#include "ship/ship.h" +#include "species_defs/species_defs.h" + +class ShipFlagsTest : public test::FSTestFixture +{ + public: + ShipFlagsTest() : test::FSTestFixture(INIT_CFILE) + { + pushModDir("ship"); + } + + protected: + void SetUp() override + { + test::FSTestFixture::SetUp(); + + // ship parsing reads species data, which needs the IFFs + iff_init(); + species_init(); + } + void TearDown() override + { + ship_close(); + iff_close(); + + test::FSTestFixture::TearDown(); + } +}; + +// a modular table that replaces $Flags without +noreplace must keep the flags that $Flags itself cannot set +TEST_F(ShipFlagsTest, replaced_flags) +{ + DEBUG_TEST(); + + ASSERT_NO_THROW(ship_init()); + ASSERT_EQ(Ship_info.size(), 1u); + + const auto &si = Ship_info[0]; + ASSERT_EQ(si.n_subsystems, 3); + + // the replacement itself took effect + EXPECT_FALSE(si.flags[Ship::Info_Flags::In_tech_database]); + EXPECT_TRUE(si.flags[Ship::Info_Flags::Stealth]); + EXPECT_TRUE(si.subsystems[0].flags[Model::Subsystem_Flags::Carry_no_damage]); + + EXPECT_TRUE(si.flags[Ship::Info_Flags::Has_display_name]); + EXPECT_TRUE(si.flags[Ship::Info_Flags::Afterburner]); + EXPECT_TRUE(si.flags[Ship::Info_Flags::Has_awacs]); + EXPECT_TRUE(si.flags[Ship::Info_Flags::Allow_landings]); + + EXPECT_TRUE(si.subsystems[0].flags[Model::Subsystem_Flags::Turret_barrel_fov_overridden]); + EXPECT_TRUE(si.subsystems[0].flags[Model::Subsystem_Flags::Turret_base_fov_overridden]); + EXPECT_TRUE(si.subsystems[0].flags[Model::Subsystem_Flags::Turret_max_fov_overridden]); +} diff --git a/test/src/source_groups.cmake b/test/src/source_groups.cmake index fd5e6e6deb5..f8052b9cd80 100644 --- a/test/src/source_groups.cmake +++ b/test/src/source_groups.cmake @@ -120,6 +120,7 @@ add_file_folder("Scripting\\\\Lua" add_file_folder("Ship" ship/ship_info.cpp ship/test_shared_model.cpp + ship/test_ship_flags.cpp ) add_file_folder("Starfield" diff --git a/test/test_data/ship/replaced_flags/data/tables/replace-shp.tbm b/test/test_data/ship/replaced_flags/data/tables/replace-shp.tbm new file mode 100644 index 00000000000..a1ddebb9cb0 --- /dev/null +++ b/test/test_data/ship/replaced_flags/data/tables/replace-shp.tbm @@ -0,0 +1,9 @@ +#Ship Classes + +$Name: Test Ship ++nocreate +$Flags: ( "stealth" ) +$Subsystem: turret01, 10, 0 + $Flags: ( "carry no damage" ) + +#End diff --git a/test/test_data/ship/replaced_flags/data/tables/ships.tbl b/test/test_data/ship/replaced_flags/data/tables/ships.tbl new file mode 100644 index 00000000000..d3687fea7da --- /dev/null +++ b/test/test_data/ship/replaced_flags/data/tables/ships.tbl @@ -0,0 +1,19 @@ +#Ship Classes + +$Name: Test Ship +$Display Name: Shown Name +$POF file: test_ship.pof +$Flags: ( "in tech database" ) +$Afterburner: YES + +Aburn Fuel: 100 +$Closeup_pos: 0, 0, -10 +$Subsystem: turret01, 10, 0 + $Maximum Barrel Elevation: 60 + $Turret Base FOV: 180 + $Turret Barrel FOV: 120 +$Subsystem: sensors, 10, 0 + $AWACS: 1, 3000 +$Subsystem: bay, 10, 0 + $Flags: ( "allow landing" ) + +#End