Starsector API
Loading...
Searching...
No Matches
TransponderAbility.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.Script;
10import com.fs.starfarer.api.campaign.CampaignFleetAPI;
11import com.fs.starfarer.api.campaign.FactionAPI;
12import com.fs.starfarer.api.campaign.rules.MemoryAPI;
13import com.fs.starfarer.api.impl.campaign.CoreReputationPlugin.RepActions;
14import com.fs.starfarer.api.impl.campaign.ids.MemFlags;
15import com.fs.starfarer.api.ui.LabelAPI;
16import com.fs.starfarer.api.ui.TooltipMakerAPI;
17import com.fs.starfarer.api.util.Misc;
18
20
21 public static final float DETECTABILITY_MULT = 1f;
22 public static final float DETECTABILITY_FLAT = 1000f;
23
24// public String getSpriteName() {
25// return Global.getSettings().getSpriteName("abilities", Abilities.TRANSPONDER);
26// }
27
28
29 @Override
30 protected String getActivationText() {
31 return "Transponder on";
32 }
33
34
35
36 @Override
37 protected String getDeactivationText() {
38 //return "Transponder off";
39 // currently, doesn't work well - if something like "go dark" cancels transponder,
40 // then you get multiple overlapping messages, "Going dark" + "Transponder off"
41 return null;
42 }
43
44
45 @Override
46 protected void activateImpl() {
47 if (entity.isPlayerFleet()) {
48 List<FactionAPI> factions = getFactionsThatWouldBecomeHostile();
49 if (!factions.isEmpty()) {
50 Global.getSector().getCampaignUI().addMessage("Your identity is revealed and linked to your recent hostile actions!");
51 for (FactionAPI faction : factions) {
52 Global.getSector().adjustPlayerReputation(RepActions.COMBAT_NORMAL, faction.getId());
53 }
54 }
55 }
56
59
60// AbilityPlugin goDark = entity.getAbility(Abilities.GO_DARK);
61// if (goDark != null && goDark.isActive()) {
62// goDark.deactivate();
63// }
64 }
65
66 @Override
67 protected void applyEffect(float amount, float level) {
68 CampaignFleetAPI fleet = getFleet();
69 if (fleet == null) return;
70
71 if (level > 0) level = 1;
72 fleet.getStats().getDetectedRangeMod().modifyMult(getModId(), 1f + (DETECTABILITY_MULT - 1f) * level, "Transponder on");
74
75 if (level <= 0) {
77 }
78 }
79
80
81 @Override
82 public void deactivate() {
83 super.deactivate();
84 if (entity.isTransponderOn()) {
85 entity.setTransponderOn(false); // failsafe in case deactivation failed to actually deactivate transponder
87 }
88 }
89
90
91
92 @Override
93 protected void deactivateImpl() {
96 //cleanupImpl();
97 }
98
99
100// @Override
101// public float getActivationDays() {
102// return 0.0f;
103// }
104//
105// @Override
106// public float getDeactivationDays() {
107// return 0.1f;
108// }
109
110 @Override
111 protected void cleanupImpl() {
112 CampaignFleetAPI fleet = getFleet();
113 if (fleet == null) return;
114
116 }
117
118 @Override
119 public void createTooltip(TooltipMakerAPI tooltip, boolean expanded) {
120 Color gray = Misc.getGrayColor();
121 Color highlight = Misc.getHighlightColor();
122 Color red = Misc.getNegativeHighlightColor();
123
124 String status = " (off)";
125 if (entity.isTransponderOn()) {
126 status = " (on)";
127 }
128
130 LabelAPI title = tooltip.addTitle(spec.getName() + status);
131 title.highlightLast(status);
132 title.setHighlightColor(gray);
133 } else {
134 tooltip.addSpacer(-10f);
135 }
136
137 float pad = 10f;
138 tooltip.addPara("Transponders transmit identifying information to all fleets within range.", pad);
139
141 List<FactionAPI> factions = getFactionsThatWouldBecomeHostile();
142 if (!factions.isEmpty()) {
143 String text = "Turning the transponder on now would reveal your hostile actions to";
144 boolean first = true;
145 boolean last = false;
146 for (FactionAPI faction : factions) {
147 last = factions.indexOf(faction) == factions.size() - 1;
148 if (first || !last) {
149 text += " " + faction.getDisplayNameWithArticle() + ",";
150 } else {
151 text += " and " + faction.getDisplayNameWithArticle() + ",";
152 }
153 }
154 text = text.substring(0, text.length() - 1) + ".";
155 tooltip.addPara(text, red, pad);
156 }
157 }
158
159 tooltip.addPara("When the transponder is on, your fleet can be detected at longer range and " +
160 "full information on its composition is available at maximum range.", pad);
161 tooltip.addPara("Transponder status also affects the reputation changes from combat and trade. " +
162 "With it turned off, you might be able to trade with otherwise inhospitable factions.", pad);
163 tooltip.addPara("In most places, running with the transponder off will attract the attention of nearby patrols, " +
164 "although several factions and certain free ports allow it. " +
165 "Having the transponder off does not grant perfect anonymity.", pad);
166
167
168 //tooltip.addPara("Disables \"Go Dark\" when activated.", pad);
169 addIncompatibleToTooltip(tooltip, expanded);
170 }
171
172 public String getFactionList(List<FactionAPI> factions) {
173 String text = "";
174 boolean first = true;
175 boolean last = false;
176 for (FactionAPI faction : factions) {
177 last = factions.indexOf(faction) == factions.size() - 1;
178 if (first || !last) {
179 text += " " + faction.getDisplayNameLongWithArticle() + ",";
180 } else {
181 text += " and " + faction.getDisplayNameLongWithArticle() + ",";
182 }
183 }
184 text = text.substring(0, text.length() - 1) + "";
185 return text.trim();
186 }
187
188
189 public boolean hasTooltip() {
190 return true;
191 }
192
193
194 public List<FactionAPI> getFactionsThatWouldBecomeHostile() {
196 }
197 public static List<FactionAPI> getFactionsThatWouldBecomeHostile(CampaignFleetAPI fleet) {
198 List<FactionAPI> result = new ArrayList<FactionAPI>();
199 //CampaignFleetAPI fleet = getFleet();
200 if (fleet == null) return result;
201 if (fleet.getContainingLocation() == null) return result;
202 if (fleet.isTransponderOn()) return result;
203
204 List<CampaignFleetAPI> fleets = fleet.getContainingLocation().getFleets();
205 for (CampaignFleetAPI other : fleets) {
206 if (other.getFleetData().getNumMembers() <= 0) continue;
207
208// VisibilityLevel level = fleet.getVisibilityLevelTo(other);
209// if (level == VisibilityLevel.NONE) continue;
210
211 float dist = Misc.getDistance(fleet.getLocation(), other.getLocation());
212 fleet.setTransponderOn(true);
213 float detectRange = (other.getMaxSensorRangeToDetect(fleet) + DETECTABILITY_FLAT) * DETECTABILITY_MULT * 1.25f;
214 fleet.setTransponderOn(false);
215
216 if (dist > detectRange) continue;
217
218 MemoryAPI mem = other.getMemoryWithoutUpdate();
219
221
222 if (!result.contains(other.getFaction()) &&
224 !other.getFaction().isHostileTo(fleet.getFaction())) {
225 result.add(other.getFaction());
226 }
227 }
228 return result;
229 }
230
231 private transient boolean showAlarm = false;
232 @Override
233 public void advance(float amount) {
234 if (!Global.getSector().isPaused()) {
235 super.advance(amount);
236 }
237
238 CampaignFleetAPI fleet = getFleet();
239 if (fleet == null || !fleet.isPlayerFleet()) return;
240 sinceWarning += amount;
241
242 List<FactionAPI> factions = getFactionsThatWouldBecomeHostile();
243 if (factions.isEmpty()) {
244 showAlarm = false;
245 } else {
246 showAlarm = true;
247 }
248 }
249
250 @Override
251 public boolean runWhilePaused() {
252 return getFleet() != null && getFleet().isPlayerFleet();
253 }
254
255
256 @Override
257 public boolean showProgressIndicator() {
258 return showAlarm;
259 }
260
261 @Override
262 public float getProgressFraction() {
263 return 1f;
264 }
265
266
267 @Override
268 public Color getProgressColor() {
269 Color color = Misc.getNegativeHighlightColor();
271 }
272
273
274 public static final float CONFIRM_DURATION = 5f;
275
276
277 @Override
278 protected Object readResolve() {
279 super.readResolve();
280 sinceWarning = 1000f;
281 return this;
282 }
283
284 transient float sinceWarning = 1000f;
285
286 @Override
287 public void pressButton() {
288 if (getFleet().isPlayerFleet()) {
289 if (sinceWarning < CONFIRM_DURATION) {
290 if (entity.isPlayerFleet()) {
291 List<FactionAPI> factions = getFactionsThatWouldBecomeHostile();
292 if (!factions.isEmpty()) {
293 String list = getFactionList(factions);
294 String text = "Turning the transponder on will reveal your identity and make " + list + " hostile.\n\n" +
295 "Are you sure you want to do this?";
296 Global.getSector().getCampaignUI().showConfirmDialog(text, "Confirm", "Cancel",
297 new Script() {
298 public void run() {
302 sinceWarning = 1000f;
303 }
304 }, null);
305 } else {
306 super.pressButton();
309 sinceWarning = 1000f;
310 }
311 } else {
312 super.pressButton();
315 sinceWarning = 1000f;
316 }
317 } else {
318 sinceWarning = 0f;
319 if (isActive()) {
320 //Global.getSector().getCampaignUI().getMessageDisplay().addMessage("Use again to confirm transponder deactivation");
322 } else {
323 //Global.getSector().getCampaignUI().getMessageDisplay().addMessage("Use again to confirm transponder activation");
324 if (showAlarm) {
326 } else {
328 }
329 }
330 }
331 } else {
332 super.pressButton();
333 }
334 }
335
336 protected String getDeactivationMessage() {
337 return "Transponder shutdown initiated - use again to deactivate";
338 }
339 protected String getActivationMessage() {
340 if (showAlarm) {
341 return "Activating the transponder will reveal your identity - use again to confirm";
342 }
343 return "Transponder primed - use again to activate";
344 }
345
346
347 @Override
348 public float getCooldownFraction() {
349 if (sinceWarning < CONFIRM_DURATION) {
350 return 0f;
351 }
352 return super.getCooldownFraction();
353 }
354
355
356 @Override
357 public boolean isOnCooldown() {
358 return false;
359 }
360
361
362 @Override
363 public Color getCooldownColor() {
364 if (sinceWarning < CONFIRM_DURATION) {
365 Color color = Misc.getNegativeHighlightColor();
366 //Color color = Color.red;
367 if (!showAlarm) {
368 color = Misc.getHighlightColor();
369 //color = Color.yellow;
370 }
371 float b = 1f;
372 float t = 0.25f;
373 if (sinceWarning < t) {
374 b = sinceWarning / t;
375 }
376 if (sinceWarning > CONFIRM_DURATION - t) {
377 b = 1f - (sinceWarning - (CONFIRM_DURATION - t)) / t;
378 }
379 return Misc.scaleAlpha(color, Global.getSector().getCampaignUI().getSharedFader().getBrightness() * 0.75f * b);
380 }
381 return super.getCooldownColor();
382 }
383
384
385 @Override
387 return true;
388 }
389
390
391
392}
393
394
395
396
397
static boolean CODEX_TOOLTIP_MODE
Definition Global.java:15
static SectorAPI getSector()
Definition Global.java:65
void modifyMult(String source, float value)
void modifyFlat(String source, float value)
void addIncompatibleToTooltip(TooltipMakerAPI tooltip, boolean expanded)
void createTooltip(TooltipMakerAPI tooltip, boolean expanded)
static List< FactionAPI > getFactionsThatWouldBecomeHostile(CampaignFleetAPI fleet)
static Color getNegativeHighlightColor()
Definition Misc.java:802
static Color getGrayColor()
Definition Misc.java:826
static float getDistance(SectorEntityToken from, SectorEntityToken to)
Definition Misc.java:599
static Color scaleAlpha(Color color, float factor)
Definition Misc.java:1309
static Color getHighlightColor()
Definition Misc.java:792
boolean showConfirmDialog(String message, String ok, String cancel, Script onOk, Script onCancel)
List< CampaignFleetAPI > getFleets()
ReputationAdjustmentResult adjustPlayerReputation(Object action, String factionId)
void setTransponderOn(boolean transponderOn)
void set(String key, Object value)
void setHighlightColor(Color color)
void highlightLast(String substring)
LabelAPI addPara(String format, float pad, Color hl, String... highlights)
UIComponentAPI addSpacer(float height)