001package com.fs.starfarer.api.impl.hullmods; 002 003import java.util.ArrayList; 004import java.util.LinkedHashMap; 005import java.util.List; 006import java.util.Map; 007 008import com.fs.starfarer.api.Global; 009import com.fs.starfarer.api.combat.BaseHullMod; 010import com.fs.starfarer.api.combat.CombatEngineAPI; 011import com.fs.starfarer.api.combat.ShipAPI; 012import com.fs.starfarer.api.combat.ShipAPI.HullSize; 013import com.fs.starfarer.api.util.Misc; 014 015public class SharedFluxSink extends BaseHullMod { 016 017 public static float FLUX_FRACTION = 0.5f; 018 public static float HARD_FLUX_FRACTION = 0.2f; 019 public static String SINK_DATA_KEY = "core_sink_data_key"; 020 021 022 public static class FluxSinkData { 023 Map<ShipAPI, Float> dissipation = new LinkedHashMap<ShipAPI, Float>(); 024 } 025 026 027 @Override 028 public void advanceInCombat(ShipAPI ship, float amount) { 029 super.advanceInCombat(ship, amount); 030 031 if (!ship.isAlive()) return; 032 033 CombatEngineAPI engine = Global.getCombatEngine(); 034 035 String key = SINK_DATA_KEY + "_" + ship.getId(); 036 FluxSinkData data = (FluxSinkData) engine.getCustomData().get(key); 037 if (data == null) { 038 data = new FluxSinkData(); 039 engine.getCustomData().put(key, data); 040 041 for (ShipAPI module : ship.getChildModulesCopy()) { 042 if (module.getStationSlot() == null || !module.isAlive() || !Misc.isActiveModule(module)) continue; 043 float d = module.getMutableStats().getFluxDissipation().getModifiedValue(); 044 d *= FLUX_FRACTION; 045 data.dissipation.put(module, d); 046 } 047 } 048 049 050 List<ShipAPI> losses = new ArrayList<ShipAPI>(data.dissipation.keySet()); 051 List<ShipAPI> remaining = new ArrayList<ShipAPI>(); 052 float totalLiveDissipation = 0f; 053 for (ShipAPI module : ship.getChildModulesCopy()) { 054 if (module.getStationSlot() == null || !module.isAlive() || !Misc.isActiveModule(module)) continue; 055 losses.remove(module); 056 remaining.add(module); 057 if (data.dissipation.containsKey(module)) { // always should, but failsafe 058 totalLiveDissipation += data.dissipation.get(module); 059 } 060 } 061 062 float extraDissipation = 0f; 063 for (ShipAPI lost : losses) { 064 if (data.dissipation.containsKey(lost)) { // always should, but failsafe 065 extraDissipation += data.dissipation.get(lost); 066 } 067 } 068 069 for (ShipAPI module : remaining) { 070 if (!data.dissipation.containsKey(module)) continue; 071 072 float currBonus = 0f; 073 if (totalLiveDissipation > 0) { 074 currBonus = data.dissipation.get(module) / totalLiveDissipation * extraDissipation; 075 } 076 077 module.getMutableStats().getFluxDissipation().modifyFlat("shared_flux_sink", currBonus); 078 079 float hardFluxFraction = 0f; 080 float totalDissipation = module.getMutableStats().getFluxDissipation().getModifiedValue(); 081 if (totalDissipation > 0) { 082 hardFluxFraction = currBonus / totalDissipation * HARD_FLUX_FRACTION; 083 } 084 085 module.getMutableStats().getHardFluxDissipationFraction().modifyFlat("shared_flux_sink", hardFluxFraction); 086 } 087 088 } 089 090 public String getDescriptionParam(int index, HullSize hullSize, ShipAPI ship) { 091 if (index == 0) return "" + (int) Math.round(FLUX_FRACTION * 100f) + "%"; 092 if (index == 1) return "" + (int) Math.round(HARD_FLUX_FRACTION * 100f) + "%"; 093 return null; 094 } 095} 096 097 098 099