Starsector API
Loading...
Searching...
No Matches
CommSnifferIntel.java
Go to the documentation of this file.
1package com.fs.starfarer.api.impl.campaign.intel.misc;
2
3import java.awt.Color;
4import java.util.Set;
5
6import org.lwjgl.input.Keyboard;
7
8import com.fs.starfarer.api.Global;
9import com.fs.starfarer.api.campaign.CustomCampaignEntityPlugin;
10import com.fs.starfarer.api.campaign.FactionAPI;
11import com.fs.starfarer.api.campaign.SectorEntityToken;
12import com.fs.starfarer.api.campaign.comm.IntelInfoPlugin;
13import com.fs.starfarer.api.impl.campaign.CampaignObjective;
14import com.fs.starfarer.api.impl.campaign.CommRelayEntityPlugin;
15import com.fs.starfarer.api.impl.campaign.ids.Tags;
16import com.fs.starfarer.api.impl.campaign.intel.BaseIntelPlugin;
17import com.fs.starfarer.api.ui.ButtonAPI;
18import com.fs.starfarer.api.ui.IntelUIAPI;
19import com.fs.starfarer.api.ui.LabelAPI;
20import com.fs.starfarer.api.ui.SectorMapAPI;
21import com.fs.starfarer.api.ui.TooltipMakerAPI;
22import com.fs.starfarer.api.util.IntervalUtil;
23import com.fs.starfarer.api.util.Misc;
24
25public class CommSnifferIntel extends BaseIntelPlugin {
26
27 public static final String UNINSTALL = "uninstall";
28
29 protected SectorEntityToken relay;
30 protected IntervalUtil check = new IntervalUtil(30f * 0.5f, 30f * 1.5f);
31 protected Boolean uninstalled = null;
32
33 public static CommSnifferIntel getExistingSnifferIntelForRelay(SectorEntityToken relay) {
34 for (IntelInfoPlugin p : Global.getSector().getIntelManager().getIntel(CommSnifferIntel.class)) {
36 if (intel.getRelay() == relay) {
37 return intel;
38 }
39 }
40 return null;
41 }
42
43 public CommSnifferIntel(SectorEntityToken relay) {
44 this.relay = relay;
45 Global.getSector().getIntelManager().addIntel(this, true);
46 Global.getSector().addScript(this);
47 }
48
49 @Override
50 protected void notifyEnded() {
51 super.notifyEnded();
52 Global.getSector().removeScript(this);
53
54 CustomCampaignEntityPlugin plugin = relay.getCustomPlugin();
55 if (plugin instanceof CampaignObjective) {
57 o.setHacked(false);
58 }
59 }
60
61
62 @Override
63 protected void advanceImpl(float amount) {
64 super.advanceImpl(amount);
65
66 float days = Misc.getDays(amount);
67 check.advance(days);
68
69 if (check.intervalElapsed()) {
70 float p = getCurrLoseProb();
71 if ((float) Math.random() < p) {
73 sendUpdateIfPlayerHasIntel(new Object(), false);
74 return;
75 }
76 }
77
78 if (!relay.isAlive() || !((CommRelayEntityPlugin)relay.getCustomPlugin()).isHacked()) {
80 sendUpdateIfPlayerHasIntel(new Object(), false);
81 return;
82 }
83 }
84
85 public SectorEntityToken getRelay() {
86 return relay;
87 }
88
89 protected void addBulletPoints(TooltipMakerAPI info, ListInfoMode mode) {
90
91 Color h = Misc.getHighlightColor();
92 Color g = Misc.getGrayColor();
93 float pad = 3f;
94 float opad = 10f;
95
96 float initPad = pad;
97 if (mode == ListInfoMode.IN_DESC) initPad = opad;
98
99 Color tc = getBulletColorForMode(mode);
100
101 bullet(info);
102 boolean isUpdate = getListInfoParam() != null;
103
104// info.addPara("Danger level: " + danger, initPad, tc, dangerColor, danger);
105// initPad = 0f;
106
107 unindent(info);
108 }
109
110 protected float getMax() {
111 return Global.getSettings().getInt("maxCommSniffersBeforeChanceToLose");
112 }
113 protected float getBaseLoseProb() {
114 return Global.getSettings().getFloat("probToLoseSnifferPerMonthPerExtra");
115 }
116
117 protected float getCurrLoseProb() {
118 float curr = 0f;
119 for (IntelInfoPlugin p : Global.getSector().getIntelManager().getIntel(CommSnifferIntel.class)) {
121 if (!intel.isEnding()) curr++;
122 }
123
124 float max = getMax();
125 if (curr <= max) return 0f;
126
127 float p = (curr - max) * getBaseLoseProb();
128 if (p > 1) p = 1;
129 return p;
130 }
131
132 @Override
133 public void createIntelInfo(TooltipMakerAPI info, ListInfoMode mode) {
134 Color c = getTitleColor(mode);
135 info.addPara(getName(), c, 0f);
136 addBulletPoints(info, mode);
137 }
138
139 @Override
140 public void createSmallDescription(TooltipMakerAPI info, float width, float height) {
141 Color h = Misc.getHighlightColor();
142 Color g = Misc.getGrayColor();
143 Color tc = Misc.getTextColor();
144 float pad = 3f;
145 float opad = 10f;
146
147 info.addImage(Global.getSector().getPlayerFaction().getLogo(), width, 128, opad);
148
149 String name = relay.getName();
150 if (name.equals(relay.getCustomEntitySpec().getDefaultName())) {
151 name = name.toLowerCase();
152 }
153
154
155 if (isEnding()) {
156 if (uninstalled != null) {
157 info.addPara("You've uninstalled the comm sniffer at " + name + ".", opad);
158 } else {
159 info.addPara("The comm sniffer installed at " + name + " is no longer responding to status queries.", opad);
160 }
161 addBulletPoints(info, ListInfoMode.IN_DESC);
162 } else {
163 info.addPara("You have installed a comm sniffer at " + name + ".", opad);
164 if (!relay.isInHyperspace()) {
165 info.addPara("It is providing you with up-to-date local intel for the " +
166 relay.getContainingLocation().getNameWithLowercaseType() + ".", opad);
167 }
168
169 addBulletPoints(info, ListInfoMode.IN_DESC);
170
171 float p = getCurrLoseProb();
172
173 String danger = null;
174 Color dangerColor = null;
175 if (p <= 0) {
176// danger = "vanishingly small";
177// dangerColor = Misc.getPositiveHighlightColor();
178 } else if (p <= 0.33f) {
179 danger = "low";
180 dangerColor = Misc.getPositiveHighlightColor();
181 } else if (p <= 0.67f) {
182 danger = "medium";
183 dangerColor = h;
184 } else {
185 danger = "high";
186 dangerColor = Misc.getNegativeHighlightColor();
187 }
188
189 //int num = Global.getSector().getIntelManager().getIntelCount(CommSnifferIntel.class, false);
190 int num = 0;
191 for (IntelInfoPlugin i : Global.getSector().getIntelManager().getIntel(CommSnifferIntel.class)) {
193 if (!intel.isEnding()) num++;
194 }
195
196 String sniffers = "sniffers";
197 String any = "any of them";
198 if (num == 1) {
199 sniffers = "sniffer";
200 any = "it";
201 }
202 if (danger == null) {
203 info.addPara("You have %s comm " + sniffers + " installed in the comm network. " +
204 "There is no danger of " + any + " being detected and cleared out.", opad,
205 h, "" + num);
206 } else {
207 LabelAPI label = info.addPara("You have %s comm " + sniffers + " installed in the comm network. The probability " +
208 "of " + any + " being detected and cleared out is %s.", opad,
209 h, "" + num, danger);
210 label.setHighlight("" + num, danger);
211 label.setHighlightColors(h, dangerColor);
212 }
213
214
215 ButtonAPI button = addGenericButton(info, width, "Uninstall comm sniffer", UNINSTALL);
216 button.setShortcut(Keyboard.KEY_U, true);
217 }
218 }
219
220
221 public void uninstall() {
223 }
224
225
226 @Override
227 public void buttonPressConfirmed(Object buttonId, IntelUIAPI ui) {
228 if (buttonId == UNINSTALL) {
229 //endAfterDelay();
231 uninstalled = true;
232 if (ui != null) {
233 //ui.updateUIForItem(this);
234 ui.recreateIntelUI();
235 }
236 }
237 }
238
239 @Override
240 public void createConfirmationPrompt(Object buttonId, TooltipMakerAPI prompt) {
241 prompt.addPara("Uninstalling this comm sniffer will reduce the chance " +
242 "that the remaining comm sniffers are detected.", 0f);
243 //prompt.addPara("Continue?", 10f);
244 }
245
246 @Override
247 public boolean doesButtonHaveConfirmDialog(Object buttonId) {
248 return true;
249 }
250
251
252 @Override
253 public String getIcon() {
254 return Global.getSettings().getSpriteName("intel", "comm_sniffer");
255 }
256
257 @Override
258 public Set<String> getIntelTags(SectorMapAPI map) {
259 Set<String> tags = super.getIntelTags(map);
260 tags.add(Tags.INTEL_COMM_SNIFFERS);
261 return tags;
262 }
263
264 public String getSortString() {
265 return "Comm Sniffer";
266 }
267
268 public String getName() {
269 String base = "Comm Sniffer";
270 if (isEnding()) {
271 if (uninstalled != null) {
272 return base + " - Uninstalled";
273 }
274 return base + " - Lost";
275 }
276 if (isNew()) {
277 return base + " Installed";
278 } else {
279 return base;
280 }
281 }
282
283 @Override
284 public FactionAPI getFactionForUIColors() {
285 return super.getFactionForUIColors();
286 }
287
288 public String getSmallDescriptionTitle() {
289 return getName();
290 }
291
292 @Override
293 public SectorEntityToken getMapLocation(SectorMapAPI map) {
294 return relay;
295 }
296
297 @Override
298 public boolean shouldRemoveIntel() {
299 //return false;
300 //return super.shouldRemoveIntel();
301 return isEnded();
302 }
303
304 @Override
305 public String getCommMessageSound() {
306 return getSoundMinorMessage();
307 }
308
309
310
311}
312
313
314
315
316
317
318
static SettingsAPI getSettings()
Definition Global.java:51
static SectorAPI getSector()
Definition Global.java:59
ButtonAPI addGenericButton(TooltipMakerAPI info, float width, String text, Object data)
void sendUpdateIfPlayerHasIntel(Object listInfoParam, TextPanelAPI textPanel)
static CommSnifferIntel getExistingSnifferIntelForRelay(SectorEntityToken relay)
void createIntelInfo(TooltipMakerAPI info, ListInfoMode mode)
void addBulletPoints(TooltipMakerAPI info, ListInfoMode mode)
void createSmallDescription(TooltipMakerAPI info, float width, float height)
void createConfirmationPrompt(Object buttonId, TooltipMakerAPI prompt)
String getSpriteName(String category, String id)