Fractal Softworks Forum

Please login or register.

Login with username, password and session length
Advanced search  

News:

Starsector 0.95a is out! (03/26/21); Blog post: Skill Changes, Part 2 (07/15/21)

Author Topic: CoreAutofitPlugin.doFit might crash if requested number of s-mods is too high  (Read 144 times)

michail

  • Lieutenant
  • **
  • Posts: 50
    • View Profile

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.
Logged

Alex

  • Administrator
  • Admiral
  • *****
  • Posts: 18940
    • View Profile

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.
Logged

Histidine

  • Admiral
  • *****
  • Posts: 3281
    • View Profile
    • Bitbucket profile
    • Email

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.
Logged

Alex

  • Administrator
  • Admiral
  • *****
  • Posts: 18940
    • View Profile

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.
Logged