Starsector API
Loading...
Searching...
No Matches
DurationAbilityWithCost2.java
Go to the documentation of this file.
1package com.fs.starfarer.api.impl.campaign.abilities;
2
3import java.util.ArrayList;
4import java.util.List;
5
6import java.awt.Color;
7
8import com.fs.starfarer.api.Global;
9import com.fs.starfarer.api.campaign.BattleAPI;
10import com.fs.starfarer.api.campaign.BuffManagerAPI.Buff;
11import com.fs.starfarer.api.campaign.CampaignFleetAPI;
12import com.fs.starfarer.api.campaign.econ.MarketAPI;
13import com.fs.starfarer.api.fleet.FleetMemberAPI;
14import com.fs.starfarer.api.impl.campaign.intel.BaseIntelPlugin;
15import com.fs.starfarer.api.impl.campaign.terrain.CRRecoveryBuff;
16import com.fs.starfarer.api.ui.LabelAPI;
17import com.fs.starfarer.api.ui.TooltipMakerAPI;
18import com.fs.starfarer.api.util.Misc;
19import com.fs.starfarer.api.util.Misc.FleetMemberDamageLevel;
20
22
23 public float getFuelCostMult() {
24 return 1f;
25 }
26 public float getCRCostMult() {
27 return 0.25f;
28 }
29
31 return 0.33f;
32 }
33
34 public FleetMemberDamageLevel getActivationDamageLevel() {
35 return FleetMemberDamageLevel.LOW;
36 }
37
38 public boolean canRecoverCRWhileActive() {
39 return false;
40 }
41
42 protected void deductCost() {
43 CampaignFleetAPI fleet = getFleet();
44 if (fleet == null) return;
45
46 float crCostMult = getCRCostMult();
47 if (crCostMult > 0) {
48 for (FleetMemberAPI member : getNonReadyShips()) {
49 if ((float) Math.random() < getActivationAtLowCRShipDamageProbability()) {
50 Misc.applyDamage(member, null, getActivationDamageLevel(), false, null, null,
51 true, null, member.getShipName() + " suffers damage from " + spec.getName() + " activation");
52 }
53 }
54 for (FleetMemberAPI member : fleet.getFleetData().getMembersListCopy()) {
55 float crLoss = getCRCost(member);
56 member.getRepairTracker().applyCREvent(-crLoss, Misc.ucFirst(spec.getName().toLowerCase()));
57 }
58 }
59
60 float cost = computeFuelCost();
61 fleet.getCargo().removeFuel(cost);
62 }
63
64
65 protected void applyStatsEffect(float amount, float level) {
66
67 }
68
69 protected void unapplyStatsEffect() {
70
71 }
72
73 protected void applyFleetVisual(float amount, float level) {
74
75 }
76
77 @Override
78 protected void applyEffect(float amount, float level) {
79 CampaignFleetAPI fleet = getFleet();
80 if (fleet == null) return;
81
82 applyStatsEffect(amount, level);
83 applyFleetVisual(amount, level);
84
86 String buffId = getModId();
87 float buffDur = 0.1f;
88 boolean needsSync = false;
89 for (FleetMemberAPI member : fleet.getFleetData().getMembersListCopy()) {
90 if (level <= 0) {
91 member.getBuffManager().removeBuff(buffId);
92 needsSync = true;
93 } else {
94 Buff test = member.getBuffManager().getBuff(buffId);
95 if (test instanceof CRRecoveryBuff) {
96 CRRecoveryBuff buff = (CRRecoveryBuff) test;
97 buff.setDur(buffDur);
98 } else {
99 member.getBuffManager().addBuff(new CRRecoveryBuff(buffId, 0f, buffDur));
100 needsSync = true;
101 }
102 }
103 }
104
105 if (needsSync) {
106 fleet.forceSync();
107 }
108 }
109
110 }
111
112 @Override
113 protected void deactivateImpl() {
114 cleanupImpl();
115 }
116
117 @Override
118 protected void cleanupImpl() {
120 }
121
122 protected List<FleetMemberAPI> getNonReadyShips() {
123 List<FleetMemberAPI> result = new ArrayList<FleetMemberAPI>();
124 if (getCRCostMult() <= 0f) return result;
125
126 CampaignFleetAPI fleet = getFleet();
127 if (fleet == null) return result;
128
129 for (FleetMemberAPI member : fleet.getFleetData().getMembersListCopy()) {
130 float crLoss = getCRCost(member);
131 if (Math.round(member.getRepairTracker().getCR() * 100) < Math.round(crLoss * 100)) {
132 result.add(member);
133 }
134 }
135 return result;
136 }
137
138 public float getCRCost(FleetMemberAPI member) {
139 float crCostMult = getCRCostMult();
140 float crLoss = member.getDeployCost() * crCostMult;
141 return Math.round(crLoss * 100f) / 100f;
142 }
143
144 protected float computeFuelCost() {
145 CampaignFleetAPI fleet = getFleet();
146 if (fleet == null) return 0f;
147
148 float cost = fleet.getLogistics().getFuelCostPerLightYear() * getFuelCostMult();
149 return cost;
150 }
151
152 protected float computeSupplyCost() {
153 CampaignFleetAPI fleet = getFleet();
154 if (fleet == null) return 0f;
155
156 float crCostMult = getCRCostMult();
157
158 float cost = 0f;
159 for (FleetMemberAPI member : fleet.getFleetData().getMembersListCopy()) {
160 cost += member.getDeploymentCostSupplies() * crCostMult;
161 }
162 return cost;
163 }
164
165
166 public void addInitialDescription(TooltipMakerAPI tooltip, boolean expanded) {
167
168 }
169
170 public boolean addNotUsableReasonBeforeFuelCost(TooltipMakerAPI tooltip, boolean expanded) {
171 return false;
172 }
173 public void addNotUsableReasonAfterFuelCost(TooltipMakerAPI tooltip, boolean expanded) {
174
175 }
176
177 @Override
178 public void createTooltip(TooltipMakerAPI tooltip, boolean expanded) {
179 CampaignFleetAPI fleet = getFleet();
180 if (fleet == null) return;
181
182 Color gray = Misc.getGrayColor();
183 Color highlight = Misc.getHighlightColor();
184 Color fuel = Global.getSettings().getColor("progressBarFuelColor");
185 Color bad = Misc.getNegativeHighlightColor();
186
188 LabelAPI title = tooltip.addTitle(spec.getName());
189 } else {
190 tooltip.addSpacer(-10f);
191 }
192
193 addInitialDescription(tooltip, expanded);
194
195 float pad = 10f;
196
197
198 String preventsRecovery = "";
200 preventsRecovery = " Prevents combat readiness recovery while active.";
201 }
202
204 String years = "year's";
205 if (getFuelCostMult() != 1) years = "years'";
206 if (getFuelCostMult() > 0 || getCRCostMult() > 0) {
207 tooltip.addPara("Consumes %s light " + years + " worth of fuel and reduces the combat readiness "
208 + "of all ships by %s of a combat deployment." + preventsRecovery,
209 pad,
210 highlight,
212 "" + (int) Math.round(getCRCostMult() * 100f) + "%");
213 } else if (getCRCostMult() > 0) {
214 tooltip.addPara("Reduces the combat readiness "
215 + "of all ships by %s of a combat deployment." + preventsRecovery,
216 pad,
217 highlight,
218 "" + (int) Math.round(getCRCostMult() * 100f) + "%");
219 } else if (getFuelCostMult() > 0) {
220 tooltip.addPara("Consumes %s light " + years + " worth of fuel." + preventsRecovery,
221 pad,
222 highlight,
224 }
225
226 if (getCRCostMult() > 0) {
227 tooltip.addPara("Ships with insufficient combat readiness may suffer damage when the ability is activated.", pad);
228 }
229 } else {
230 float fuelCost = computeFuelCost();
231 float supplyCost = computeSupplyCost();
232 if (supplyCost > 0 && fuelCost > 0) {
233 tooltip.addPara("Consumes %s fuel and reduces the combat readiness" +
234 " of all ships, costing up to %s supplies to recover." + preventsRecovery, pad,
235 highlight,
238 } else if (supplyCost > 0) {
239 tooltip.addPara("Reduces the combat readiness" +
240 " of all ships, costing up to %s supplies to recover." + preventsRecovery, pad,
241 highlight,
243 } else if (fuelCost > 0) {
244 tooltip.addPara("Consumes %s fuel." + preventsRecovery, pad,
245 highlight,
247 }
248
249 boolean addedReason = addNotUsableReasonBeforeFuelCost(tooltip, expanded);
250 if (!addedReason && fuelCost > 0 && fuelCost > fleet.getCargo().getFuel()) {
251 tooltip.addPara("Not enough fuel.", bad, pad);
252 } else {
253 addNotUsableReasonAfterFuelCost(tooltip, expanded);
254 }
255
256 List<FleetMemberAPI> nonReady = getNonReadyShips();
257 if (!nonReady.isEmpty()) {
258 tooltip.addPara("Some ships don't have enough combat readiness " +
259 "and may suffer damage if the ability is activated:", pad,
260 Misc.getNegativeHighlightColor(), "may suffer damage");
261 int j = 0;
262 int max = 4;
263 float initPad = 5f;
264 for (FleetMemberAPI member : nonReady) {
265 if (j >= max) {
266 if (nonReady.size() > max + 1) {
267 tooltip.addPara(BaseIntelPlugin.INDENT + "... and several other ships", initPad);
268 break;
269 }
270 }
271 String str = "";
272 if (!member.isFighterWing()) {
273 str += member.getShipName() + ", ";
274 str += member.getHullSpec().getHullNameWithDashClass();
275 } else {
276 str += member.getVariant().getFullDesignationWithHullName();
277 }
278
279 tooltip.addPara(BaseIntelPlugin.INDENT + str, initPad);
280 initPad = 0f;
281 j++;
282 }
283 }
284 }
285
286 addIncompatibleToTooltip(tooltip, expanded);
287 }
288
289 public boolean hasTooltip() {
290 return true;
291 }
292
293
294 @Override
295 public void fleetLeftBattle(BattleAPI battle, boolean engagedInHostilities) {
296 if (engagedInHostilities) {
297 deactivate();
298 }
299 }
300
301 @Override
302 public void fleetOpenedMarket(MarketAPI market) {
303 deactivate();
304 }
305
306 protected boolean showAlarm() {
307 return !getNonReadyShips().isEmpty() && !isOnCooldown() && !isActiveOrInProgress() && isUsable();
308 }
309
310 @Override
311 public boolean isOnCooldown() {
312 return super.getCooldownFraction() < 1f;
313 }
314
315 @Override
316 public boolean isUsable() {
317 return super.isUsable() &&
318 getFleet() != null &&
320 }
321 @Override
322 public float getCooldownFraction() {
323 if (showAlarm()) {
324 return 0f;
325 }
326 return super.getCooldownFraction();
327 }
328
329 @Override
330 public Color getCooldownColor() {
331 if (showAlarm()) {
332 Color color = Misc.getNegativeHighlightColor();
334 }
335 return super.getCooldownColor();
336 }
337
338 @Override
340 if (showAlarm()) {
341 return true;
342 }
343 return false;
344 }
345
346 @Override
347 protected void activateImpl() {
348
349 }
350}
351
352
353
354
355
static SettingsAPI getSettings()
Definition Global.java:57
static boolean CODEX_TOOLTIP_MODE
Definition Global.java:15
static SectorAPI getSector()
Definition Global.java:65
void addIncompatibleToTooltip(TooltipMakerAPI tooltip, boolean expanded)
boolean addNotUsableReasonBeforeFuelCost(TooltipMakerAPI tooltip, boolean expanded)
void addNotUsableReasonAfterFuelCost(TooltipMakerAPI tooltip, boolean expanded)
void fleetLeftBattle(BattleAPI battle, boolean engagedInHostilities)
static String ucFirst(String str)
Definition Misc.java:559
static Color getNegativeHighlightColor()
Definition Misc.java:802
static Color getGrayColor()
Definition Misc.java:826
static void applyDamage(FleetMemberAPI member, Random random, FleetMemberDamageLevel level, boolean withCRDamage, String crDamageId, String crDamageReason, boolean withMessage, TextPanelAPI textPanel, String messageText)
Definition Misc.java:5680
static Color scaleAlpha(Color color, float factor)
Definition Misc.java:1309
static Color getHighlightColor()
Definition Misc.java:792
static String getRoundedValueMaxOneAfterDecimal(float value)
Definition Misc.java:673
static String getRoundedValue(float value)
Definition Misc.java:647
List< FleetMemberAPI > getMembersListCopy()
LabelAPI addPara(String format, float pad, Color hl, String... highlights)
UIComponentAPI addSpacer(float height)