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/code/model/model.h b/code/model/model.h index dd16843f26a..4c285d48375 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. @@ -276,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 @@ -603,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; @@ -1078,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 43b3390e781..1dcd7eb191d 100644 --- a/code/model/modelread.cpp +++ b/code/model/modelread.cpp @@ -605,21 +605,38 @@ 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, // 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); @@ -631,13 +648,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; @@ -650,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); - } } @@ -691,39 +709,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; @@ -2554,9 +2567,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 ); @@ -3168,13 +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]; + 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; - bank->wash_info_pointer = subsystems[k].engine_wash_pointer; - if (bank->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 @@ -3186,7 +3201,7 @@ modelread_status read_and_process_model_file(polymodel* pm, const char* filename } } - if ((bank->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); } @@ -6223,6 +6238,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; @@ -6237,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 b4e6907e499..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(); } @@ -3803,13 +3804,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) @@ -5008,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(); @@ -5106,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); } @@ -5278,7 +5284,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()) { @@ -5396,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) @@ -5870,6 +5883,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 */ @@ -5976,6 +6030,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(); @@ -6071,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 @@ -7326,26 +7382,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; } } @@ -21943,6 +22008,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); } diff --git a/code/ship/shipfx.cpp b/code/ship/shipfx.cpp index 3ead10b9cc2..fccb0a67a1c 100644 --- a/code/ship/shipfx.cpp +++ b/code/ship/shipfx.cpp @@ -2712,17 +2712,17 @@ void engine_wash_ship_process(ship *shipp) } // check if thruster bank has engine wash - if (bank->wash_info_pointer == NULL) { + 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()) { - bank->wash_info_pointer = &Engine_wash_info[0]; - nprintf(("wash", "Adding default engine wash to ship %s", wash_sip->name)); + wash_index = 0; } else { continue; } } - engine_wash_info *ewp = bank->wash_info_pointer; + const engine_wash_info *ewp = &Engine_wash_info[wash_index]; half_angle = ewp->angle; radius_mult = ewp->radius_mult; float wash_intensity; 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(); 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/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 f0ca41e3802..f8052b9cd80 100644 --- a/test/src/source_groups.cmake +++ b/test/src/source_groups.cmake @@ -119,6 +119,8 @@ 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/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 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