Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package data.scripts.hullmods;
- import com.fs.starfarer.api.Global;
- import java.util.*;
- import com.fs.starfarer.api.campaign.BuffManagerAPI.Buff;
- import com.fs.starfarer.api.campaign.CampaignFleetAPI;
- import com.fs.starfarer.api.combat.HullModEffect;
- import com.fs.starfarer.api.combat.HullModFleetEffect;
- import com.fs.starfarer.api.combat.MutableShipStatsAPI;
- import com.fs.starfarer.api.combat.MutableStat;
- import com.fs.starfarer.api.combat.ShipAPI;
- import com.fs.starfarer.api.combat.MutableStat.StatMod;
- import com.fs.starfarer.api.combat.ShipAPI.HullSize;
- import com.fs.starfarer.api.fleet.FleetMemberAPI;
- public class TowCable implements HullModEffect, HullModFleetEffect {
- public static final String HULLMOD_ID = "tow_cable";
- public static class TowCableBuff implements Buff {
- public int buffAmount = 1;
- private final String buffId;
- public TowCableBuff(String buffId) {
- this.buffId = buffId;
- }
- @Override
- public boolean isExpired() {
- return false;
- }
- @Override
- public String getId() {
- return buffId;
- }
- @Override
- public void apply(FleetMemberAPI member) {
- member.getStats().getMaxBurnLevel().modifyFlat(buffId, buffAmount);
- }
- @Override
- public void advance(float days) {
- }
- };
- private final static float INTERVAL = 0.2f; // in days
- private long timestamp = 0;
- private long lastTimestamp = 0;
- // We only run this once in a while, since exact timing is not an issue in the campaign map
- @Override
- public void advanceInCampaign(CampaignFleetAPI fleet) {
- if (fleet == null) {
- return;
- }
- if (timestamp == 0) {
- timestamp = Global.getSector().getClock().getTimestamp();
- lastTimestamp = timestamp;
- }
- if (Global.getSector().getClock().getElapsedDaysSince(timestamp) < INTERVAL && Global.getSector().getClock().getTimestamp() != lastTimestamp) {
- return;
- }
- timestamp = Global.getSector().getClock().getTimestamp();
- lastTimestamp = timestamp;
- List<FleetMemberAPI> all = fleet.getFleetData().getMembersListCopy();
- int numCables = 0;
- float towSpeed = Float.MAX_VALUE; // We make an assumption that all Oxen have the same burn speed
- for (FleetMemberAPI curr : all) {
- if (!curr.canBeDeployedForCombat()) {
- continue;
- }
- if (curr.getVariant().getHullMods().contains(HULLMOD_ID)) {
- numCables++;
- towSpeed = Math.min(towSpeed, getMaxBurnWithoutCables(curr));
- }
- }
- if (numCables <= 0) {
- cleanUpTowCableBuffs(fleet);
- return;
- }
- Map<FleetMemberAPI, Integer> cables = new HashMap();
- for (int cableIndex = 0; cableIndex < numCables; cableIndex++) {
- FleetMemberAPI slowest = getSlowest(all, towSpeed, cables);
- if (slowest == null) {
- break;
- }
- Integer bonus = cables.get(slowest);
- if (bonus == null) {
- bonus = Integer.valueOf(0);
- }
- bonus++;
- cables.put(slowest, bonus);
- }
- for (FleetMemberAPI curr : all) {
- if (!cables.containsKey(curr)) {
- curr.getBuffManager().removeBuff(TOW_CABLE_KEY);
- continue;
- }
- if (cables.get(curr) <= 0) {
- curr.getBuffManager().removeBuff(TOW_CABLE_KEY);
- continue;
- }
- boolean renew = true;
- for (StatMod mod : curr.getStats().getMaxBurnLevel().getFlatMods().values()) {
- if (mod.getSource().equals(TOW_CABLE_KEY)) {
- if (mod.value == cables.get(curr)) {
- renew = false;
- }
- }
- }
- if (renew) {
- TowCableBuff buff = new TowCableBuff(TOW_CABLE_KEY);
- buff.buffAmount = cables.get(curr);
- curr.getBuffManager().addBuff(buff);
- }
- }
- }
- @Override
- public void onFleetSync(CampaignFleetAPI fleet) {
- }
- public TowCable() {
- }
- @Override
- public void advanceInCampaign(FleetMemberAPI member, float amount) {
- }
- private FleetMemberAPI getSlowest(List<FleetMemberAPI> all, float speedCutoff, Map<FleetMemberAPI, Integer> cables) {
- FleetMemberAPI slowest = null;
- float minLevel = Float.MAX_VALUE;
- for (FleetMemberAPI curr : all) {
- if (!isSuitable(curr)) {
- continue;
- }
- float baseBurn = getMaxBurnWithoutCables(curr);
- Integer bonus = cables.get(curr);
- if (bonus == null) {
- bonus = new Integer(0);
- }
- if (bonus >= getMaxCablesFor(curr)) {
- continue;
- }
- float burnLevel = baseBurn + bonus;
- if (burnLevel >= speedCutoff) {
- continue;
- }
- if (burnLevel < minLevel) {
- minLevel = burnLevel;
- slowest = curr;
- }
- }
- return slowest;
- }
- private int getMaxCablesFor(FleetMemberAPI member) {
- switch (member.getHullSpec().getHullSize()) {
- case CAPITAL_SHIP:
- return 4;
- case CRUISER:
- return 3;
- case DESTROYER:
- return 2;
- case FRIGATE:
- return 1;
- }
- return 1;
- }
- private static float getMaxBurnWithoutCables(FleetMemberAPI member) {
- MutableStat burn = member.getStats().getMaxBurnLevel();
- float val = burn.getModifiedValue();
- float sub = 0;
- for (StatMod mod : burn.getFlatMods().values()) {
- if (mod.getSource().equals(TOW_CABLE_KEY)) {
- sub = mod.getValue();
- break;
- }
- }
- return Math.max(0, val - sub);
- }
- private boolean isSuitable(FleetMemberAPI member) {
- return !member.isFighterWing();
- }
- private void cleanUpTowCableBuffs(CampaignFleetAPI fleet) {
- if (fleet == null) {
- return;
- }
- for (FleetMemberAPI curr : fleet.getFleetData().getMembersListCopy()) {
- curr.getBuffManager().removeBuff(TOW_CABLE_KEY);
- }
- }
- /**
- * One instance of the buff object per ship with a Tow Cable.
- */
- public static final String TOW_CABLE_KEY = "TowCable_PersistentBuffs";
- @Override
- public void advanceInCombat(ShipAPI ship, float amount) {
- }
- @Override
- public void applyEffectsAfterShipCreation(ShipAPI ship, String id) {
- }
- @Override
- public void applyEffectsBeforeShipCreation(HullSize hullSize, MutableShipStatsAPI stats, String id) {
- }
- @Override
- public boolean isApplicableToShip(ShipAPI ship) {
- return true;
- }
- @Override
- public String getDescriptionParam(int index, HullSize hullSize) {
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement