Spoiler
package data.scripts.campaign.fleet;
import com.fs.starfarer.api.campaign.BuffManagerAPI;
import com.fs.starfarer.api.campaign.CampaignFleetAPI;
import com.fs.starfarer.api.campaign.SectorEntityToken;
import com.fs.starfarer.api.campaign.StarSystemAPI;
import com.fs.starfarer.api.fleet.FleetMemberAPI;
import com.fs.starfarer.api.impl.campaign.procgen.themes.RemnantAssignmentAI;
import static com.fs.starfarer.api.campaign.BuffManagerAPI.*;
public class Knight_ArrivalFleetBuffAndAI extends RemnantAssignmentAI {
private static final String BUFF_ID = "Knight_arrivalFBA";
public Knight_ArrivalFleetBuffAndAI(CampaignFleetAPI fleet, StarSystemAPI homeSystem, SectorEntityToken source) {
super(fleet, homeSystem, source);
}
@Override
public void advance(float amount) {
super.advance(amount);
boolean needSync = false;
for (FleetMemberAPI member : fleet.getFleetData().getMembersListCopy()) {
Buff buff = member.getBuffManager().getBuff(BUFF_ID);
if (buff instanceof EnhanceBuff) {
((EnhanceBuff) buff).setDur(0.2f);
} else {
member.getBuffManager().addBuff(new EnhanceBuff(BUFF_ID, 0.2f));
needSync = true;
}
}
if (needSync) {
fleet.forceSync();
}
}
public class EnhanceBuff implements Buff {
private static final float DAMAGE_REDUCTION = 20f;
private static final float DAMAGE_INCREASE = 20f;
private static final float HULL_BONUS = 20f;
private String id;
private float dur;
public EnhanceBuff(String id, float dur) {
this.id = id;
this.dur = dur;
}
public void advance(float days) {
dur -= days;
}
public void apply(FleetMemberAPI member) {
member.getStats().getHullBonus().modifyMult(getId(), 2f);
member.getStats().getEnergyDamageTakenMult().modifyPercent(getId(), -DAMAGE_REDUCTION);
member.getStats().getHighExplosiveDamageTakenMult().modifyPercent(getId(), -DAMAGE_REDUCTION);
member.getStats().getKineticDamageTakenMult().modifyPercent(getId(), -DAMAGE_REDUCTION);
member.getStats().getFragmentationDamageTakenMult().modifyPercent(getId(), -DAMAGE_REDUCTION);
member.getStats().getEnergyWeaponDamageMult().modifyPercent(getId(), DAMAGE_INCREASE);
member.getStats().getMissileWeaponDamageMult().modifyPercent(getId(), DAMAGE_INCREASE);
member.getStats().getBallisticWeaponDamageMult().modifyPercent(getId(), DAMAGE_INCREASE);
}
public String getId() {
return id;
}
public boolean isExpired() {
return dur <= 0;
}
public float getDur() {
return dur;
}
public void setDur(float dur) {
this.dur = dur;
}
}
}
Why such buffs doesn't work on AI's fleet(I have add them to the fleet)
Hmm - tested this out, by pasting this class into core code, and then doing:
fleet.addScript(new Knight_ArrivalFleetBuffAndAI(fleet, (StarSystemAPI) engine.getCurrentLocation(), engine.getPlayerFleet()));
To a newly-spawned pirate fleet. Made no code changes and it seems to work correctly - it applies the stats in the campaign screen and when spawning the ships in combat. So, I'm not sure why it wouldn't work for you, that's very strange!
Btw, side note: you might want to use an IntervalUtil to apply the buffs instead of checking to see if they're present for every fleet member every frame. This wouldn't matter if there's just one fleet like that but if there are many fleets this sort of thing could add up and be bad for performance.
I'd do something like:
protected IntervalUtil interval = new IntervalUtil(0.1f, 0.15f);
public Knight_ArrivalFleetBuffAndAI(CampaignFleetAPI fleet, StarSystemAPI homeSystem, SectorEntityToken source) {
super(fleet, homeSystem, source);
interval.forceIntervalElapsed();
}
@Override
public void advance(float amount) {
super.advance(amount);
float days = Global.getSector().getClock().convertToDays(amount);
interval.advance(days);
if (interval.intervalElapsed()) {
// the same code you've already got to refresh/add a buff as needed
}
}
That'd make sure the work is not done on every frame and is spread out between different frames when there are multiple fleets.
I don't mean this and it's not a good idea I think...
And Alex I found that Misc.getInflater() won't find my campaign plugin which has overriden the pickFleetInflater
Is your plugin returning a higher PickPriority?
The core one returns this:
return new PluginPick<FleetInflater>(new DefaultFleetInflater(p), PickPriority.CORE_GENERAL);
For yours, you probably want:
return new PluginPick<FleetInflater>(new DefaultFleetInflater(p), PickPriority.MOD_GENERAL);
Or a higher priority. If you're already doing this, I'd need to have a look at the code.
It's telling that I got through that, and even Alex didn't have an explanation for why stuff didn't Just Work.
Well, I had no idea you were adding new ships and assumed it was vanilla ones! If you'd mentioned this, I'd have pointed you to default_ship_roles right off the bat.
If a question is, for example, phrased like this:
So, do Pirates never get to have Capitals?
I literally just have no idea what exactly you're asking, you know? So many ways this could be interpreted. I think if you try to be more specific, it'll save everyone a lot of headaches; for a lot of your questions, it does seem like you're possibly assuming I know exactly what you're doing. Which I don't, so I end up making assumptions, and this can backfire (as it did here).