Starsector API
Loading...
Searching...
No Matches
SharedFluxSink.java
Go to the documentation of this file.
1package com.fs.starfarer.api.impl.hullmods;
2
3import java.util.ArrayList;
4import java.util.LinkedHashMap;
5import java.util.List;
6import java.util.Map;
7
8import com.fs.starfarer.api.Global;
9import com.fs.starfarer.api.combat.BaseHullMod;
10import com.fs.starfarer.api.combat.CombatEngineAPI;
11import com.fs.starfarer.api.combat.ShipAPI;
12import com.fs.starfarer.api.combat.ShipAPI.HullSize;
13import com.fs.starfarer.api.util.Misc;
14
15public class SharedFluxSink extends BaseHullMod {
16
17 public static float FLUX_FRACTION = 0.5f;
18 public static float HARD_FLUX_FRACTION = 0.2f;
19 public static String SINK_DATA_KEY = "core_sink_data_key";
20
21
22 public static class FluxSinkData {
23 Map<ShipAPI, Float> dissipation = new LinkedHashMap<ShipAPI, Float>();
24 }
25
26
27 @Override
28 public void advanceInCombat(ShipAPI ship, float amount) {
29 super.advanceInCombat(ship, amount);
30
31 if (!ship.isAlive()) return;
32
33 CombatEngineAPI engine = Global.getCombatEngine();
34
35 String key = SINK_DATA_KEY + "_" + ship.getId();
36 FluxSinkData data = (FluxSinkData) engine.getCustomData().get(key);
37 if (data == null) {
38 data = new FluxSinkData();
39 engine.getCustomData().put(key, data);
40
41 for (ShipAPI module : ship.getChildModulesCopy()) {
42 if (module.getStationSlot() == null || !module.isAlive() || !Misc.isActiveModule(module)) continue;
43 float d = module.getMutableStats().getFluxDissipation().getModifiedValue();
44 d *= FLUX_FRACTION;
45 data.dissipation.put(module, d);
46 }
47 }
48
49
50 List<ShipAPI> losses = new ArrayList<ShipAPI>(data.dissipation.keySet());
51 List<ShipAPI> remaining = new ArrayList<ShipAPI>();
52 float totalLiveDissipation = 0f;
53 for (ShipAPI module : ship.getChildModulesCopy()) {
54 if (module.getStationSlot() == null || !module.isAlive() || !Misc.isActiveModule(module)) continue;
55 losses.remove(module);
56 remaining.add(module);
57 if (data.dissipation.containsKey(module)) { // always should, but failsafe
58 totalLiveDissipation += data.dissipation.get(module);
59 }
60 }
61
62 float extraDissipation = 0f;
63 for (ShipAPI lost : losses) {
64 if (data.dissipation.containsKey(lost)) { // always should, but failsafe
65 extraDissipation += data.dissipation.get(lost);
66 }
67 }
68
69 for (ShipAPI module : remaining) {
70 if (!data.dissipation.containsKey(module)) continue;
71
72 float currBonus = 0f;
73 if (totalLiveDissipation > 0) {
74 currBonus = data.dissipation.get(module) / totalLiveDissipation * extraDissipation;
75 }
76
77 module.getMutableStats().getFluxDissipation().modifyFlat("shared_flux_sink", currBonus);
78
79 float hardFluxFraction = 0f;
80 float totalDissipation = module.getMutableStats().getFluxDissipation().getModifiedValue();
81 if (totalDissipation > 0) {
82 hardFluxFraction = currBonus / totalDissipation * HARD_FLUX_FRACTION;
83 }
84
85 module.getMutableStats().getHardFluxDissipationFraction().modifyFlat("shared_flux_sink", hardFluxFraction);
86 }
87
88 }
89
90 public String getDescriptionParam(int index, HullSize hullSize, ShipAPI ship) {
91 if (index == 0) return "" + (int) Math.round(FLUX_FRACTION * 100f) + "%";
92 if (index == 1) return "" + (int) Math.round(HARD_FLUX_FRACTION * 100f) + "%";
93 return null;
94 }
95}
96
97
98
99
static CombatEngineAPI getCombatEngine()
Definition Global.java:63
String getDescriptionParam(int index, HullSize hullSize, ShipAPI ship)
void advanceInCombat(ShipAPI ship, float amount)