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