Fractal Softworks Forum

Starsector => Bug Reports & Support => Bug Reports & Support (modded) => Topic started by: michail on June 06, 2021, 10:27:25 AM

Title: CoreAutofitPlugin.doFit might crash if requested number of s-mods is too high
Post by: michail on June 06, 2021, 10:27:25 AM
Breaking point for a random fleet spawned in with a console command seems to be 5. Haven't had a crash with 4 (yet). The culprit is this block, starting at line 446:

Code
			if (remaining > 0) {
List<String> mods = new ArrayList<String>();
mods.add(HullMods.FLUX_DISTRIBUTOR);
mods.add(HullMods.FLUX_COIL);
if (current.getHullSize() == HullSize.FRIGATE || current.hasHullMod(HullMods.SAFETYOVERRIDES)) {
mods.add(HullMods.HARDENED_SUBSYSTEMS);
mods.add(HullMods.REINFORCEDHULL);
} else {
mods.add(HullMods.REINFORCEDHULL);
mods.add(HullMods.HARDENED_SUBSYSTEMS);
}
mods.add(HullMods.BLAST_DOORS);
while (!mods.isEmpty() && current.hasHullMod(mods.get(0))) {
mods.remove(0);
}
for (int i = 0; i < remaining; i++) {
current.setNumFluxCapacitors(0);
current.setNumFluxVents(0);
String modId = mods.get(Math.min(i, mods.size() - 1));
addHullmods(current, delegate, modId);
convertToSMods(current, 1);
// addExtraVents(current);
// addExtraCaps(current);
}

The for-loop doesn't check whether the list is empty or not and calls "get" with -1 for index.
Title: Re: CoreAutofitPlugin.doFit might crash if requested number of s-mods is too high
Post by: Alex on June 06, 2021, 11:07:30 AM
Thanks! Hmm, I think it'd crash only if "mods" was empty due to the ship already having all of those mods. Fixed it up.
Title: Re: CoreAutofitPlugin.doFit might crash if requested number of s-mods is too high
Post by: Histidine on June 07, 2021, 09:30:01 PM
There seems to be a bug in the doFit code for handling S-mods.

- As per OP's code block, it won't add the candidate S-mods if they already exist on the ship... regardless of whether the existing hullmods are built in or not.
- Normally this wouldn't be a problem, since earlier (line 428) we already converted as many existing normal hullmods to S-mods as we could.
- However, immediately after, we then use the surplus OP to squeeze in as many normal hullmods as we can (all five of the same ones that the remaining loop can add, in fact).

The remaining loop should check whether the existing mods are actually built-in to the hull or variant before removing them from the mods array.
Title: Re: CoreAutofitPlugin.doFit might crash if requested number of s-mods is too high
Post by: Alex on June 08, 2021, 09:04:36 AM
Ah, that makes sense, thank you. What I've done is replace the loop that removes stuff from mods with this:
Iterator<String> iter = mods.iterator();
while (iter.hasNext()) {
   String modId = iter.next();
   if (current.getPermaMods().contains(modId)) {
      iter.remove();
   }
}

And made addHullmods a no-OP if the mod is already on the variant. I think that should do it.