Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions code/mission/missionparse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -8898,6 +8902,67 @@ int insert_subsys_status(p_object *pobjp)
return new_index;
}

// wookieejedi
void remove_subsys_status(p_object *pobjp, int index)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor, possibly fine to accept as is: this is the first code that moves entries inside one parse object's range (insert_subsys_status only appends). Lua parse_subsystem handles store an offset from subsys_index, so a handle a script took before a class change can afterwards point at a different subsystem instead of going invalid. It only matters if a script holds those handles across ship select or change-ship-class.

{
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)
{
Expand Down
4 changes: 4 additions & 0 deletions code/mission/missionparse.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
4 changes: 4 additions & 0 deletions code/missionui/missionscreencommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
}
}
Expand Down
4 changes: 4 additions & 0 deletions code/missionui/missionshipchoice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
11 changes: 8 additions & 3 deletions code/scripting/api/objs/parse_object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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));
Expand Down
Loading