Advertisement
DarkRevenant

Untitled

Apr 15th, 2014
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.27 KB | None | 0 0
  1. package data.scripts.hullmods;
  2.  
  3. import com.fs.starfarer.api.Global;
  4. import java.util.*;
  5.  
  6. import com.fs.starfarer.api.campaign.BuffManagerAPI.Buff;
  7. import com.fs.starfarer.api.campaign.CampaignFleetAPI;
  8. import com.fs.starfarer.api.combat.HullModEffect;
  9. import com.fs.starfarer.api.combat.HullModFleetEffect;
  10. import com.fs.starfarer.api.combat.MutableShipStatsAPI;
  11. import com.fs.starfarer.api.combat.MutableStat;
  12. import com.fs.starfarer.api.combat.ShipAPI;
  13. import com.fs.starfarer.api.combat.MutableStat.StatMod;
  14. import com.fs.starfarer.api.combat.ShipAPI.HullSize;
  15. import com.fs.starfarer.api.fleet.FleetMemberAPI;
  16.  
  17. public class TowCable implements HullModEffect, HullModFleetEffect {
  18.  
  19.     public static final String HULLMOD_ID = "tow_cable";
  20.  
  21.     public static class TowCableBuff implements Buff {
  22.  
  23.         public int buffAmount = 1;
  24.         private final String buffId;
  25.  
  26.         public TowCableBuff(String buffId) {
  27.             this.buffId = buffId;
  28.         }
  29.  
  30.         @Override
  31.         public boolean isExpired() {
  32.             return false;
  33.         }
  34.  
  35.         @Override
  36.         public String getId() {
  37.             return buffId;
  38.         }
  39.  
  40.         @Override
  41.         public void apply(FleetMemberAPI member) {
  42.             member.getStats().getMaxBurnLevel().modifyFlat(buffId, buffAmount);
  43.         }
  44.  
  45.         @Override
  46.         public void advance(float days) {
  47.         }
  48.     };
  49.  
  50.     private final static float INTERVAL = 0.2f; // in days
  51.     private long timestamp = 0;
  52.     private long lastTimestamp = 0;
  53.  
  54.     // We only run this once in a while, since exact timing is not an issue in the campaign map
  55.     @Override
  56.     public void advanceInCampaign(CampaignFleetAPI fleet) {
  57.         if (fleet == null) {
  58.             return;
  59.         }
  60.  
  61.         if (timestamp == 0) {
  62.             timestamp = Global.getSector().getClock().getTimestamp();
  63.             lastTimestamp = timestamp;
  64.         }
  65.  
  66.         if (Global.getSector().getClock().getElapsedDaysSince(timestamp) < INTERVAL && Global.getSector().getClock().getTimestamp() != lastTimestamp) {
  67.             return;
  68.         }
  69.  
  70.         timestamp = Global.getSector().getClock().getTimestamp();
  71.         lastTimestamp = timestamp;
  72.         List<FleetMemberAPI> all = fleet.getFleetData().getMembersListCopy();
  73.  
  74.         int numCables = 0;
  75.         float towSpeed = Float.MAX_VALUE; // We make an assumption that all Oxen have the same burn speed
  76.         for (FleetMemberAPI curr : all) {
  77.             if (!curr.canBeDeployedForCombat()) {
  78.                 continue;
  79.             }
  80.  
  81.             if (curr.getVariant().getHullMods().contains(HULLMOD_ID)) {
  82.                 numCables++;
  83.                 towSpeed = Math.min(towSpeed, getMaxBurnWithoutCables(curr));
  84.             }
  85.         }
  86.         if (numCables <= 0) {
  87.             cleanUpTowCableBuffs(fleet);
  88.             return;
  89.         }
  90.  
  91.         Map<FleetMemberAPI, Integer> cables = new HashMap();
  92.  
  93.         for (int cableIndex = 0; cableIndex < numCables; cableIndex++) {
  94.             FleetMemberAPI slowest = getSlowest(all, towSpeed, cables);
  95.             if (slowest == null) {
  96.                 break;
  97.             }
  98.             Integer bonus = cables.get(slowest);
  99.             if (bonus == null) {
  100.                 bonus = Integer.valueOf(0);
  101.             }
  102.             bonus++;
  103.             cables.put(slowest, bonus);
  104.         }
  105.  
  106.         for (FleetMemberAPI curr : all) {
  107.             if (!cables.containsKey(curr)) {
  108.                 curr.getBuffManager().removeBuff(TOW_CABLE_KEY);
  109.                 continue;
  110.             }
  111.  
  112.             if (cables.get(curr) <= 0) {
  113.                 curr.getBuffManager().removeBuff(TOW_CABLE_KEY);
  114.                 continue;
  115.             }
  116.  
  117.             boolean renew = true;
  118.             for (StatMod mod : curr.getStats().getMaxBurnLevel().getFlatMods().values()) {
  119.                 if (mod.getSource().equals(TOW_CABLE_KEY)) {
  120.                     if (mod.value == cables.get(curr)) {
  121.                         renew = false;
  122.                     }
  123.                 }
  124.             }
  125.  
  126.             if (renew) {
  127.                 TowCableBuff buff = new TowCableBuff(TOW_CABLE_KEY);
  128.                 buff.buffAmount = cables.get(curr);
  129.                 curr.getBuffManager().addBuff(buff);
  130.             }
  131.         }
  132.     }
  133.  
  134.     @Override
  135.     public void onFleetSync(CampaignFleetAPI fleet) {
  136.     }
  137.  
  138.     public TowCable() {
  139.  
  140.     }
  141.  
  142.     @Override
  143.     public void advanceInCampaign(FleetMemberAPI member, float amount) {
  144.     }
  145.  
  146.     private FleetMemberAPI getSlowest(List<FleetMemberAPI> all, float speedCutoff, Map<FleetMemberAPI, Integer> cables) {
  147.         FleetMemberAPI slowest = null;
  148.         float minLevel = Float.MAX_VALUE;
  149.         for (FleetMemberAPI curr : all) {
  150.             if (!isSuitable(curr)) {
  151.                 continue;
  152.             }
  153.  
  154.             float baseBurn = getMaxBurnWithoutCables(curr);
  155.             Integer bonus = cables.get(curr);
  156.             if (bonus == null) {
  157.                 bonus = new Integer(0);
  158.             }
  159.  
  160.             if (bonus >= getMaxCablesFor(curr)) {
  161.                 continue;
  162.             }
  163.  
  164.             float burnLevel = baseBurn + bonus;
  165.  
  166.             if (burnLevel >= speedCutoff) {
  167.                 continue;
  168.             }
  169.  
  170.             if (burnLevel < minLevel) {
  171.                 minLevel = burnLevel;
  172.                 slowest = curr;
  173.             }
  174.         }
  175.         return slowest;
  176.     }
  177.  
  178.     private int getMaxCablesFor(FleetMemberAPI member) {
  179.         switch (member.getHullSpec().getHullSize()) {
  180.             case CAPITAL_SHIP:
  181.                 return 4;
  182.             case CRUISER:
  183.                 return 3;
  184.             case DESTROYER:
  185.                 return 2;
  186.             case FRIGATE:
  187.                 return 1;
  188.         }
  189.         return 1;
  190.     }
  191.  
  192.     private static float getMaxBurnWithoutCables(FleetMemberAPI member) {
  193.         MutableStat burn = member.getStats().getMaxBurnLevel();
  194.         float val = burn.getModifiedValue();
  195.         float sub = 0;
  196.         for (StatMod mod : burn.getFlatMods().values()) {
  197.             if (mod.getSource().equals(TOW_CABLE_KEY)) {
  198.                 sub = mod.getValue();
  199.                 break;
  200.             }
  201.         }
  202.         return Math.max(0, val - sub);
  203.     }
  204.  
  205.     private boolean isSuitable(FleetMemberAPI member) {
  206.         return !member.isFighterWing();
  207.     }
  208.  
  209.     private void cleanUpTowCableBuffs(CampaignFleetAPI fleet) {
  210.         if (fleet == null) {
  211.             return;
  212.         }
  213.         for (FleetMemberAPI curr : fleet.getFleetData().getMembersListCopy()) {
  214.             curr.getBuffManager().removeBuff(TOW_CABLE_KEY);
  215.         }
  216.     }
  217.  
  218.     /**
  219.      * One instance of the buff object per ship with a Tow Cable.
  220.      */
  221.     public static final String TOW_CABLE_KEY = "TowCable_PersistentBuffs";
  222.  
  223.     @Override
  224.     public void advanceInCombat(ShipAPI ship, float amount) {
  225.     }
  226.  
  227.     @Override
  228.     public void applyEffectsAfterShipCreation(ShipAPI ship, String id) {
  229.     }
  230.  
  231.     @Override
  232.     public void applyEffectsBeforeShipCreation(HullSize hullSize, MutableShipStatsAPI stats, String id) {
  233.     }
  234.  
  235.     @Override
  236.     public boolean isApplicableToShip(ShipAPI ship) {
  237.         return true;
  238.     }
  239.  
  240.     @Override
  241.     public String getDescriptionParam(int index, HullSize hullSize) {
  242.         return null;
  243.     }
  244.  
  245. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement