diff --git a/code/mission/missionparse.cpp b/code/mission/missionparse.cpp index dd276c3c39d..8cb7d350bca 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."}, @@ -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 @@ -8883,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++; 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; 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