diff --git a/code/mission/missionparse.cpp b/code/mission/missionparse.cpp index 848af7bc8e0..d51f54065e1 100644 --- a/code/mission/missionparse.cpp +++ b/code/mission/missionparse.cpp @@ -4417,6 +4417,10 @@ void swap_parse_object(p_object *p_obj, int new_ship_class) ship_subsystems->secondary_banks[j] = -1; } } + + // Subsystems + // Any subsystems from the mission file that the new class doesn't have are no longer relevant + prune_subsys_status(p_obj); } // Goober5000 - this needs to search the vector instead of using the registry, because it can be used during parsing before the registry is populated @@ -8898,6 +8902,67 @@ int insert_subsys_status(p_object *pobjp) return new_index; } +// wookieejedi +void remove_subsys_status(p_object *pobjp, int index) +{ + Assertion((index >= pobjp->subsys_index) && (index < pobjp->subsys_index + pobjp->subsys_count), + "Subsystem index %d does not belong to parse object %s!", index, pobjp->name); + + // shift the elements downward, which keeps each parse object's subsystems contiguous + for (int i = index; i < Subsys_index - 1; i++) + { + memcpy(&Subsys_status[i], &Subsys_status[i+1], sizeof(subsys_status)); + } + + Subsys_index--; + + // the parse object has one less subsystem + pobjp->subsys_count--; + + // we also have to adjust all the indexes in existing parse objects + // (each p_object's subsys_index points to subsystem 0 in its list) + for (auto &pobj: Parse_objects) + { + // bump down base index to account for the removed subsystem + if (pobj.subsys_index > index) + pobj.subsys_index--; + } +} + +// wookieejedi - When a parse object's ship class is changed, such as by the loadout screen, any subsystems +// that the new class doesn't have are no longer meaningful, so get rid of them. Otherwise they would be +// looked up, and warned about, if and when the ship is created. +void prune_subsys_status(p_object *pobjp) +{ + if (!Ship_info.in_bounds(pobjp->ship_class)) + return; + + auto sip = &Ship_info[pobjp->ship_class]; + + // iterate backward so that a removal doesn't shift the entries we haven't checked yet + for (int i = pobjp->subsys_count - 1; i >= 0; i--) + { + int index = pobjp->subsys_index + i; + + // the Pilot subsystem isn't an actual subsystem; it holds the ship's own weapons + if (!subsystem_stricmp(Subsys_status[index].name, NOX("pilot"))) + continue; + + bool found = false; + for (int j = 0; j < sip->n_subsystems; j++) + { + if (!subsystem_stricmp(sip->subsystems[j].subobj_name, Subsys_status[index].name)) + { + found = true; + break; + } + } + + if (!found) + remove_subsys_status(pobjp, index); + } +} + // Goober5000 subsys_status *parse_get_subsys_status(p_object *pobjp, const char *subsys_name) { diff --git a/code/mission/missionparse.h b/code/mission/missionparse.h index aa3236cc49f..0bef6062c79 100644 --- a/code/mission/missionparse.h +++ b/code/mission/missionparse.h @@ -677,6 +677,10 @@ void clear_texture_replacements(); // Goober5000 subsys_status *parse_get_subsys_status(p_object *pobjp, const char *subsys_name); +// wookieejedi +void remove_subsys_status(p_object *pobjp, int index); +void prune_subsys_status(p_object *pobjp); + // MjnMixael custom_string* get_custom_string_by_name(SCP_string name); diff --git a/code/missionui/missionscreencommon.cpp b/code/missionui/missionscreencommon.cpp index 24195a53611..93265263ab7 100644 --- a/code/missionui/missionscreencommon.cpp +++ b/code/missionui/missionscreencommon.cpp @@ -1278,8 +1278,12 @@ void wss_direct_restore_loadout() break; } slot = &Player_loadout.unit_data[valid_wing_index*MAX_WING_SLOTS+j]; + bool class_changed = ( p_objp->ship_class != slot->ship_class ); p_objp->ship_class = slot->ship_class; wl_update_parse_object_weapons(p_objp, slot); + // the subsystems in the mission file may not exist on the class the player picked + if ( class_changed ) + prune_subsys_status(p_objp); j++; } } diff --git a/code/missionui/missionshipchoice.cpp b/code/missionui/missionshipchoice.cpp index c6ba2754fb0..1a2b961aa9c 100644 --- a/code/missionui/missionshipchoice.cpp +++ b/code/missionui/missionshipchoice.cpp @@ -2373,8 +2373,12 @@ commit_pressed_status create_wings() for ( auto &p_obj: Parse_objects ) { if ( p_obj.wingnum == WING_INDEX(wp) ) { if ( p_obj.pos_in_wing == j ) { + bool class_changed = ( p_obj.ship_class != Wss_slots[slot_index].ship_class ); p_obj.ship_class = Wss_slots[slot_index].ship_class; wl_update_parse_object_weapons(&p_obj, &Wss_slots[i*MAX_WING_SLOTS+j]); + // the subsystems in the mission file may not exist on the class the player picked + if ( class_changed ) + prune_subsys_status(&p_obj); found_pobj = true; break; } diff --git a/code/scripting/api/objs/parse_object.cpp b/code/scripting/api/objs/parse_object.cpp index e5d462bdb01..fb3601b7e78 100644 --- a/code/scripting/api/objs/parse_object.cpp +++ b/code/scripting/api/objs/parse_object.cpp @@ -284,7 +284,7 @@ ADE_VIRTVAR(Orientation, l_ParseObject, "orientation", "The orientation of the p return ade_set_args(L, "o", l_Matrix.Set(matrix_h(&poh->getObject()->orient))); } -ADE_VIRTVAR(ShipClass, l_ParseObject, "shipclass", "The ship class of the parsed ship.", "shipclass", "The ship class") +ADE_VIRTVAR(ShipClass, l_ParseObject, "shipclass", "The ship class of the parsed ship. Setting it before the ship arrives changes the class the way change-ship-class does, and parse subsystem handles taken earlier may then refer to a different subsystem.", "shipclass", "The ship class") { parse_object_h* poh = nullptr; int newClass = -1; @@ -297,8 +297,13 @@ ADE_VIRTVAR(ShipClass, l_ParseObject, "shipclass", "The ship class of the parsed if (!poh->isValid()) return ade_set_error(L, "o", l_Shipclass.Set(-1)); - if (ADE_SETTING_VAR && newClass >= 0) { - poh->getObject()->ship_class = newClass; + if (ADE_SETTING_VAR && newClass >= 0 && newClass != poh->getObject()->ship_class) { + auto entry = ship_registry_get(poh->getObject()->name); + if (entry != nullptr && entry->status == ShipStatus::NOT_YET_PRESENT) { + swap_parse_object(poh->getObject(), newClass); + } else { + poh->getObject()->ship_class = newClass; + } } return ade_set_args(L, "o", l_Shipclass.Set(poh->getObject()->ship_class));