Starsector API
Loading...
Searching...
No Matches
LowCRShipDamageSequence.java
Go to the documentation of this file.
1package com.fs.starfarer.api.impl.combat;
2
3import java.util.ArrayList;
4import java.util.List;
5
6import com.fs.starfarer.api.combat.BaseEveryFrameCombatPlugin;
7import com.fs.starfarer.api.combat.CombatEngineAPI;
8import com.fs.starfarer.api.combat.ShipAPI;
9import com.fs.starfarer.api.combat.WeaponAPI;
10import com.fs.starfarer.api.combat.ShipEngineControllerAPI.ShipEngineAPI;
11import com.fs.starfarer.api.input.InputEventAPI;
12import com.fs.starfarer.api.util.IntervalUtil;
13import com.fs.starfarer.api.util.WeightedRandomPicker;
14
15public class LowCRShipDamageSequence extends BaseEveryFrameCombatPlugin {
16 private ShipAPI ship;
17 private CombatEngineAPI engine;
18
19 private float severity;
20 private float elapsed = 0f;
21 private float beforeDamage;
22 private IntervalUtil tracker = new IntervalUtil(0.25f, 1f);
23
24 private WeightedRandomPicker<Object> disableTargets = new WeightedRandomPicker<Object>(true);
25 private float disableAttempts = 0;
26
27 public LowCRShipDamageSequence(ShipAPI ship, float severity) {
28 this.ship = ship;
29 this.severity = severity;
30
31 beforeDamage = 1f + 2f * (float) Math.random();
32
33 List<WeaponAPI> weapons = ship.getUsableWeapons();
34 for (WeaponAPI weapon : weapons) {
35 disableTargets.add(weapon);
36 }
37 List<ShipEngineAPI> engines = ship.getEngineController().getShipEngines();
38 for (ShipEngineAPI engine : engines) {
39 if (!engine.isSystemActivated()) {
40 disableTargets.add(engine);
41 }
42 }
43
44 disableAttempts = Math.max(1, Math.round((0.25f + 0.75f * severity) * (float) disableTargets.getItems().size() * 0.25f));
45
46 if (severity >= 1) {
47 disableAttempts *= 2f;
48 }
49 }
50
51 public void disableNext() {
52 if (disableAttempts <= 0) return;
53 disableAttempts--;
54 List<Object> remove = new ArrayList<Object>();
55// List<Object> usableWeapons = new ArrayList<Object>();
56 for (Object target : disableTargets.getItems()) {
57// if (target instanceof ShipEngineAPI) {
58// float fractionIfDisabled = ((ShipEngineAPI) target).getContribution() + ship.getEngineFractionPermanentlyDisabled();
59// if (fractionIfDisabled > 0.5f) {
60// remove.add(target);
61// }
62// } else if (target instanceof WeaponAPI) {
63// WeaponAPI weapon = (WeaponAPI) target;
64// if (weapon.getAmmo() > 0) {
65// usableWeapons.add(target);
66// }
67// }
69 remove.add(target);
70 }
71 }
72// if (usableWeapons.size() <= 1) remove.addAll(usableWeapons);
73 for (Object target : remove) {
74 disableTargets.remove(target);
75 }
76
77 Object module = disableTargets.pick();
78 if (module != null) {
79 disableTargets.remove(module);
80 ship.applyCriticalMalfunction(module);
81 }
82 }
83
84
85
86 public void init(CombatEngineAPI engine) {
87 this.engine = engine;
88 }
89
90 public void advance(float amount, List<InputEventAPI> events) {
91 if (engine.isPaused()) return;
92
93 if (!engine.isEntityInPlay(ship)) return; // ship is in refit screen
94
95// System.out.println("Advance: " + amount);
96 elapsed += amount;
97
98 if (elapsed > beforeDamage) {
99 tracker.advance(amount);
100 if (tracker.intervalElapsed()) {
101 disableNext();
102
103 }
104 if (ship.isHulk() || disableAttempts <= 0) {
105 engine.removePlugin(this);
106 }
107 }
108 }
109
110
111
112
113
114
115}
116
117
118
119
120
121
static boolean isOkToPermanentlyDisableStatic(ShipAPI ship, Object module)
void advance(float amount, List< InputEventAPI > events)