Fractal Softworks Forum

Starsector => Mods => Modding => Topic started by: Euripides on April 21, 2021, 08:28:43 AM

Title: Overload Speed Debuff
Post by: Euripides on April 21, 2021, 08:28:43 AM
When a spaceship is overloaded, it receives a movement penalty of 30% to its speed units

I'd like to make this as a mod but I do not know where to even begin. Is this something that is even possible to mod?
Title: Re: Overload Speed Debuff
Post by: Thaago on April 21, 2021, 09:55:04 AM
It would be possible yes, but would require scripting.

My off the top of the head solution would be an EveryFrameCombatPlugin that loops over all ships checking for their overload state (ShipAPI.getFluxTracker().isOverloaded()) and applies the debuff to overloaded ships and then removes them from not-overloaded ships (ShipAPI.getMutableStats().getMaxSpeed().modifyMult(.7,"modname_string") and ShipAPI.getMutableStats().getMaxSpeed().unmodify("modname_string")).

Technically either of these could be called every frame with no tracking as the game uses the 'source' string as an ID to prevent duplicates, so they won't stack in either direction. My instinct would be to maintain a set of overloaded ships and do comparisons for performance, but then again comparing the source strings is just a function call and a string comparison, while maintaining a set requires membership testing, adding, and removing... the performance gain might be minimal to 0 depending on how efficient the function call is while adding complication. Sorry this turned into a bit of a side conjecture, but yes its doable!

[Edit]
Some resources to help: https://starsector.fandom.com/wiki/Modding_Plugins for technical details on plugins.
https://starsector.fandom.com/wiki/Intro_to_Modding the very first section is relevant and talks about the boilerplate for setting up a mod.
Title: Re: Overload Speed Debuff
Post by: Lord Sputnik on April 21, 2021, 10:58:36 PM
So something like this then?

Spoiler
package data.hullmods;

import com.fs.starfarer.api.Global;
import com.fs.starfarer.api.combat.ShipAPI;
import com.fs.starfarer.api.combat.FluxTrackerAPI;
import com.fs.starfarer.api.combat.MutableShipStatsAPI;

public class SlowOverloadedShip extends BaseHullMod {

   public final float OverloadReduction = 0.7f;

   public void advanceInCombat(ShipAPI ship) {

      MutableShipStatsAPI stats = ship.getMutableStats();

      if (ship.getFluxTracker().isOverloaded() == 1) {
         
         stats.getMaxSpeed().modifyPercent("SlowOverloadedShip", OverloadReduction);

      } else {

         stats.getMaxSpeed().unmodify("SlowOverloadedShip");

      }

   }

}
[close]

I'm extremely new to modding this game so I figured this would be something simple to see if I understand the structure correctly.
Title: Re: Overload Speed Debuff
Post by: Thaago on April 21, 2021, 11:12:04 PM
I don't have an IDE set up in front of me at the moment so I can't bug check it, but yeah thats the general idea packaged into a hullmod.

You should use modifyMult instead of modifyPercent though to follow the general trend of "reductions apply multiplicatively" that bonuses and penalties have in the game. (Also I don't recall off the top of my head if modifyPercent takes the whole number of percent or the fraction, so giving it .7 might make it .7% faster...not sure).

The only downside of using hullmods is that you need to add the hullmod to everything. An everyFramePlugin has the same internal logic, it just loops over all ships manually instead of having the ships themselves do the check. IIRC the correct way to loop over all ships is to use
 
Global.getCombatEngine().getFleetManager(FleetSide.PLAYER).getDeployedCopyDFM()

which returns a list of deployedFleetMembers of the player side. There would need to be another call for the enemy. Each of these lists can be looped through and the ShipAPI instance of the DFM gotten from dfm.getShip(), and then you can use the same logic as in the hullmod. Again this is without access to testing so I might ahve messed something up, but thats the general way.