Fractal Softworks Forum

Please login or register.

Login with username, password and session length
Advanced search  

News:

Starsector 0.97a is out! (02/02/24); New blog post: Simulator Enhancements (03/13/24)

Author Topic: Custom Hullmod Woes  (Read 1054 times)

Yunru

  • Admiral
  • *****
  • Posts: 1560
    • View Profile
Custom Hullmod Woes
« on: January 01, 2022, 06:15:52 AM »

So I'm trying to make this hullmod work, and it just doesn't. I've no idea why, except maybe some modules voodoo.

What's supposed to happen is that it gets the modified speed from all active engines (designated by a dummy hullmod), totals it, and then divides it by the total number of engines.

This should, in theory, mean that putting things like Unstable Injectors on the engine modules increases the speed of the ship. In practice, it's having no effect.
Code
package data.scripts.hullmods;

import com.fs.starfarer.api.combat.BaseHullMod;
import com.fs.starfarer.api.combat.ShipAPI;

public class YunruModuleEngines extends BaseHullMod {

    @Override
    public void advanceInCombat(ShipAPI ship, float amount) {
        super.advanceInCombat(ship, amount);

if (!ship.isAlive()) return;

        Float yunruMaxEngines = 0f;
        Float yunruEngineCount = 0f;
        Float yunruFlameoutCount = 0f;
        Float yunruMaxSpeed = 0f;
        Float yunruSpeedDif = 0f;
        Float yunruMaxAcc = 0f;
        Float yunruAccDif = 0f;
       
        for (ShipAPI module : ship.getChildModulesCopy()) {
            if (module.getVariant().getHullMods().contains("yunru_engine_module")) yunruMaxEngines += 1f;
            if (module.getStationSlot() == null || !module.isAlive() || !module.getVariant().getHullMods().contains("yunru_engine_module")) continue;
            if (module.getEngineController().isFlamedOut() == true) yunruFlameoutCount += 1f;
            if (module.getEngineController().isDisabled() == true) continue;
            yunruEngineCount += 1f;
            yunruMaxSpeed += module.getMutableStats().getMaxSpeed().getModifiedValue();
            yunruMaxAcc += module.getMutableStats().getAcceleration().getModifiedValue();
        }

        yunruMaxSpeed /= yunruMaxEngines;
        yunruMaxAcc /= yunruMaxEngines;

        yunruSpeedDif += yunruMaxSpeed-ship.getMutableStats().getMaxSpeed().getModifiedValue();
        yunruAccDif += yunruMaxAcc-ship.getMutableStats().getAcceleration().getModifiedValue();

        ship.getMutableStats().getMaxSpeed().modifyFlat("yunruModuleEngines", yunruSpeedDif);
        ship.getMutableStats().getAcceleration().modifyFlat("yunruModuleEngines", yunruAccDif);

        if (yunruFlameoutCount.equals(yunruMaxEngines) || yunruEngineCount.equals(0f)) ship.getEngineController().forceFlameout();

    }

}

sphr

  • Ensign
  • *
  • Posts: 46
    • View Profile
Re: Custom Hullmod Woes
« Reply #1 on: January 01, 2022, 12:31:28 PM »

I am just starting to pick up modding so I could be wrong.

I suspect some ship stats cannot be changed after creation (i.e. the method with before ship creation of something).  For advanceInCombat, it may be too late? (the ship already created).

Let's wait for somebody who knows for sure to confirm.
Logged

SafariJohn

  • Admiral
  • *****
  • Posts: 3010
    • View Profile
Re: Custom Hullmod Woes
« Reply #2 on: January 01, 2022, 12:49:38 PM »

What you are doing should be possible. Looks like it should work, too. Run through it with a debugger: see if any modules actually get iterated over and if the numbers are coming out the way you expect.

Also you aren't checking for division by zero when you average your speed and acceleration. Pretty sure that will crash when a ship loses all its modules.
Logged

Yunru

  • Admiral
  • *****
  • Posts: 1560
    • View Profile
Re: Custom Hullmod Woes
« Reply #3 on: January 01, 2022, 03:16:10 PM »

What you are doing should be possible. Looks like it should work, too. Run through it with a debugger: see if any modules actually get iterated over and if the numbers are coming out the way you expect.
How would I do that with VSCode?

Quote
Also you aren't checking for division by zero when you average your speed and acceleration. Pretty sure that will crash when a ship loses all its modules.
Thanks for the catch!
Edit: Wait no, it should be fine. It divides it by the total number of engine modules, alive, dead, or disabled. Obviously the ship could have none, but then it shouldn't be using the hullmod.

IonDragonX

  • Admiral
  • *****
  • Posts: 816
    • View Profile
Re: Custom Hullmod Woes
« Reply #4 on: January 01, 2022, 03:24:24 PM »

Obviously the ship could have none, but then it shouldn't be using the hullmod.
You shouldn't leave the chance. If the ship has 0 or [null] engines, then prevent the hullmod being installed.
Logged

SafariJohn

  • Admiral
  • *****
  • Posts: 3010
    • View Profile
Re: Custom Hullmod Woes
« Reply #5 on: January 01, 2022, 03:27:55 PM »

I don't know how to debug with VSCode, sorry, I use Netbeans. There should be tutorials on the internet.

Avoiding the div0 error is trivial, just
Code: java
if (yunruMaxEngines > 0) {
        yunruMaxSpeed /= yunruMaxEngines;
        yunruMaxAcc /= yunruMaxEngines;
}
Logged

Yunru

  • Admiral
  • *****
  • Posts: 1560
    • View Profile
Re: Custom Hullmod Woes
« Reply #6 on: January 03, 2022, 04:43:36 AM »

So what I've caught just from playing around with it, is that it is doing something, in that the bar is constantly flickering.

What seems to be happening is that the speed is being set higher, then reset to its base. As for why... Could it be because the modifier is the difference, so once it's set high, the difference becomes 0 and it effectively unapplies itself?

Edit: Yup, that's definitely it. Not sure how I can fix it to take account of if the core has speed boosts, but ignoring that inconvenient fact, I now have it working.

Thaago

  • Global Moderator
  • Admiral
  • *****
  • Posts: 7174
  • Harpoon Affectionado
    • View Profile
Re: Custom Hullmod Woes
« Reply #7 on: January 03, 2022, 10:50:54 AM »

Right, the way that mutable stats work is that any modifier to them has an identifying string, and that string has an associated value. You can check on a flat modifier via its MutableStat.StatMod, gotten from getFlatStatMod(string).

How about using getBaseValue() instead of getModifiedValue()? Then your difference will not go to 0. Or you can have a check before doing the modifying to make sure that the amount you are modifying is not equal to 0. Or subtract the statmod from the current value to get the modifed without-this-bonus value and use that to compute the correct difference.
Logged

Yunru

  • Admiral
  • *****
  • Posts: 1560
    • View Profile
Re: Custom Hullmod Woes
« Reply #8 on: January 03, 2022, 10:59:01 AM »

I can't get the base value because I want to account for things like Hullmods that may be present.

I "solved" it for my use case by just skipping the core and applying the difference between the core's base speed and the total modified speed, which works fine but thus doesn't cover having an engine on the main module.

I tried skipping modification if the difference was zero, but it wasn't a complete fix as the max speed still jittered wildly.

SafariJohn

  • Admiral
  • *****
  • Posts: 3010
    • View Profile
Re: Custom Hullmod Woes
« Reply #9 on: January 03, 2022, 05:30:13 PM »

Try unapplying your modifier at the beginning of the advance. Then you'll get the speed and accel minus your mod so you can calculate where your mod should be.
Logged