Advertisement
DarkRevenant

Untitled

May 17th, 2014
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.47 KB | None | 0 0
  1. package data.scripts.hullmods;
  2.  
  3. import com.fs.starfarer.api.campaign.CampaignFleetAPI;
  4. import com.fs.starfarer.api.combat.HullModEffect;
  5. import com.fs.starfarer.api.combat.HullModFleetEffect;
  6. import com.fs.starfarer.api.combat.MutableShipStatsAPI;
  7. import com.fs.starfarer.api.combat.ShipAPI;
  8. import com.fs.starfarer.api.combat.ShipAPI.HullSize;
  9. import com.fs.starfarer.api.fleet.FleetMemberAPI;
  10. import com.fs.starfarer.api.fleet.RepairTrackerAPI;
  11. import java.util.*;
  12.  
  13. // NOTE: IT IS EXTREMELY IMPORTANT THAT ONLY ONE OF THESE HULLMODS ACTUALLY DOES ANYTHING!
  14. // KEEP ALL OF THESE SIMILAR HULLMODS IN THE SAME FILE!
  15. // THEY WILL STILL WORK PROPERLY AND AVOID DUPLICATION ERRORS!
  16. public class MobileHeadquarters implements HullModEffect, HullModFleetEffect {
  17.  
  18.     private static final List<String> hullmodIds = new ArrayList<>();
  19.  
  20.     static {
  21.         hullmodIds.add("mobile_headquarters");
  22.         hullmodIds.add("mobile_starbase");
  23.         hullmodIds.add("mobile_resupplier");
  24.         hullmodIds.add("mobile_autofactory");
  25.     }
  26.  
  27.     public MobileHeadquarters() {
  28.     }
  29.  
  30.     @Override
  31.     public void advanceInCampaign(FleetMemberAPI member2, float amount) {
  32.  
  33.     }
  34.  
  35.     @Override
  36.     public void advanceInCombat(ShipAPI ship, float amount) {
  37.     }
  38.  
  39.     @Override
  40.     public void applyEffectsAfterShipCreation(ShipAPI ship, String id) {
  41.     }
  42.  
  43.     @Override
  44.     public void applyEffectsBeforeShipCreation(HullSize hullSize, MutableShipStatsAPI stats, String id) {
  45.     }
  46.  
  47.     @Override
  48.     public boolean isApplicableToShip(ShipAPI ship) {
  49.         return true;
  50.     }
  51.  
  52.     public static final float MOBILE_RS_TOTAL_SUPPLIES = 250f;
  53.     public static final float MOBILE_HQ_TOTAL_SUPPLIES = 500f;
  54.     public static final float MOBILE_AF_TOTAL_SUPPLIES = 1000f;
  55.     public static final float MOBILE_SB_TOTAL_SUPPLIES = 2500f;
  56.  
  57.     @Override
  58.     public String getDescriptionParam(int index, HullSize hullSize) {
  59.         if (index == 0) {
  60.             return "" + (int) MOBILE_HQ_TOTAL_SUPPLIES;
  61.         }
  62.         return null;
  63.     }
  64.  
  65.     @Override
  66.     public void advanceInCampaign(CampaignFleetAPI fleet) {
  67.         float HQCount = 0;
  68.         float fullRecoverySuppliesRemaining;
  69.         float recoverySuppliesRemaining = 0;
  70.         float totalRecoverySupplies = 0;
  71.  
  72.         if (fleet != null) {
  73.             for (FleetMemberAPI member : fleet.getFleetData().getMembersListCopy()) {
  74.                 if (member.isMothballed() || !member.canBeDeployedForCombat()) {
  75.                     continue;
  76.                 }
  77.  
  78.                 for (String modId : member.getVariant().getHullMods()) {
  79.                     switch (modId) {
  80.                         case "mobile_starbase":
  81.                             HQCount++;
  82.                             recoverySuppliesRemaining += MOBILE_SB_TOTAL_SUPPLIES * member.getRepairTracker().getCR();
  83.                             break;
  84.                         case "mobile_autofactory":
  85.                             HQCount++;
  86.                             recoverySuppliesRemaining += MOBILE_AF_TOTAL_SUPPLIES * member.getRepairTracker().getCR();
  87.                             break;
  88.                         case "mobile_headquarters":
  89.                             HQCount++;
  90.                             recoverySuppliesRemaining += MOBILE_HQ_TOTAL_SUPPLIES * member.getRepairTracker().getCR();
  91.                             break;
  92.                         case "mobile_resupplier":
  93.                             HQCount++;
  94.                             recoverySuppliesRemaining += MOBILE_RS_TOTAL_SUPPLIES * member.getRepairTracker().getCR();
  95.                             break;
  96.                         default:
  97.                     }
  98.                 }
  99.             }
  100.  
  101.             fullRecoverySuppliesRemaining = recoverySuppliesRemaining;
  102.  
  103.             if (HQCount > 0 && (int) fullRecoverySuppliesRemaining > 0) {
  104.                 for (FleetMemberAPI member : fleet.getFleetData().getMembersInPriorityOrder()) {
  105.                     if (member.isMothballed()) {
  106.                         continue;
  107.                     }
  108.  
  109.                     boolean stop = false;
  110.                     for (String modId : member.getVariant().getHullMods()) {
  111.                         if (hullmodIds.contains(modId)) {
  112.                             stop = true;
  113.                         }
  114.                     }
  115.                     if (stop) {
  116.                         continue;
  117.                     }
  118.  
  119.                     RepairTrackerAPI repair = member.getRepairTracker();
  120.                     if (repair.getBaseCR() < repair.getMaxCR()) {
  121.                         float costToMaxCR = member.getStats().getBaseSupplyUsePerDay().getModifiedValue() * (repair.getMaxCR() - repair.getBaseCR()) / repair.getRecoveryRate();
  122.  
  123.                         // UNCOMMENT THIS, UOMOZ: costToMaxCR *= member.getDeployCost() * 10f;
  124.  
  125.                         float recoveredSupplyCost;
  126.                         if (costToMaxCR > recoverySuppliesRemaining) {
  127.                             recoveredSupplyCost = recoverySuppliesRemaining;
  128.                             repair.setCR(repair.getBaseCR() + (recoveredSupplyCost / costToMaxCR) * (repair.getMaxCR() - repair.getBaseCR()));
  129.                         } else {
  130.                             recoveredSupplyCost = costToMaxCR;
  131.                             repair.setCR(repair.getMaxCR());
  132.                         }
  133.  
  134.                         totalRecoverySupplies += recoveredSupplyCost;
  135.                         recoverySuppliesRemaining -= recoveredSupplyCost;
  136.                     }
  137.                 }
  138.  
  139.                 // The way this math works, each HQ ship will reach 0 CR at the same time
  140.                 float loadRatio = totalRecoverySupplies / fullRecoverySuppliesRemaining;
  141.                 if (loadRatio > 0) {
  142.                     for (FleetMemberAPI member : fleet.getFleetData().getMembersListCopy()) {
  143.                         if (member.isMothballed() || !member.canBeDeployedForCombat()) {
  144.                             continue;
  145.                         }
  146.  
  147.                         for (String modId : member.getVariant().getHullMods()) {
  148.                             if (hullmodIds.contains(modId)) {
  149.                                 member.getRepairTracker().applyCREvent(-1f * member.getRepairTracker().getBaseCR() * loadRatio, "Fleet maintenance");
  150.                             }
  151.                         }
  152.                     }
  153.                 }
  154.             }
  155.         }
  156.     }
  157.  
  158.     @Override
  159.     public void onFleetSync(CampaignFleetAPI fleet) {
  160.     }
  161.  
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement