001package com.fs.starfarer.api.impl.campaign.intel.misc; 002 003import java.awt.Color; 004import java.util.Set; 005 006import org.lwjgl.input.Keyboard; 007 008import com.fs.starfarer.api.Global; 009import com.fs.starfarer.api.campaign.CustomCampaignEntityPlugin; 010import com.fs.starfarer.api.campaign.FactionAPI; 011import com.fs.starfarer.api.campaign.SectorEntityToken; 012import com.fs.starfarer.api.campaign.comm.IntelInfoPlugin; 013import com.fs.starfarer.api.impl.campaign.CampaignObjective; 014import com.fs.starfarer.api.impl.campaign.CommRelayEntityPlugin; 015import com.fs.starfarer.api.impl.campaign.ids.Tags; 016import com.fs.starfarer.api.impl.campaign.intel.BaseIntelPlugin; 017import com.fs.starfarer.api.ui.ButtonAPI; 018import com.fs.starfarer.api.ui.IntelUIAPI; 019import com.fs.starfarer.api.ui.LabelAPI; 020import com.fs.starfarer.api.ui.SectorMapAPI; 021import com.fs.starfarer.api.ui.TooltipMakerAPI; 022import com.fs.starfarer.api.util.IntervalUtil; 023import com.fs.starfarer.api.util.Misc; 024 025public class CommSnifferIntel extends BaseIntelPlugin { 026 027 public static final String UNINSTALL = "uninstall"; 028 029 protected SectorEntityToken relay; 030 protected IntervalUtil check = new IntervalUtil(30f * 0.5f, 30f * 1.5f); 031 protected Boolean uninstalled = null; 032 033 public static CommSnifferIntel getExistingSnifferIntelForRelay(SectorEntityToken relay) { 034 for (IntelInfoPlugin p : Global.getSector().getIntelManager().getIntel(CommSnifferIntel.class)) { 035 CommSnifferIntel intel = (CommSnifferIntel) p; 036 if (intel.getRelay() == relay) { 037 return intel; 038 } 039 } 040 return null; 041 } 042 043 public CommSnifferIntel(SectorEntityToken relay) { 044 this.relay = relay; 045 Global.getSector().getIntelManager().addIntel(this, true); 046 Global.getSector().addScript(this); 047 } 048 049 @Override 050 protected void notifyEnded() { 051 super.notifyEnded(); 052 Global.getSector().removeScript(this); 053 054 CustomCampaignEntityPlugin plugin = relay.getCustomPlugin(); 055 if (plugin instanceof CampaignObjective) { 056 CampaignObjective o = (CampaignObjective) plugin; 057 o.setHacked(false); 058 } 059 } 060 061 062 @Override 063 protected void advanceImpl(float amount) { 064 super.advanceImpl(amount); 065 066 float days = Misc.getDays(amount); 067 check.advance(days); 068 069 if (check.intervalElapsed()) { 070 float p = getCurrLoseProb(); 071 if ((float) Math.random() < p) { 072 endAfterDelay(); 073 sendUpdateIfPlayerHasIntel(new Object(), false); 074 return; 075 } 076 } 077 078 if (!relay.isAlive() || !((CommRelayEntityPlugin)relay.getCustomPlugin()).isHacked()) { 079 endAfterDelay(); 080 sendUpdateIfPlayerHasIntel(new Object(), false); 081 return; 082 } 083 } 084 085 public SectorEntityToken getRelay() { 086 return relay; 087 } 088 089 protected void addBulletPoints(TooltipMakerAPI info, ListInfoMode mode) { 090 091 Color h = Misc.getHighlightColor(); 092 Color g = Misc.getGrayColor(); 093 float pad = 3f; 094 float opad = 10f; 095 096 float initPad = pad; 097 if (mode == ListInfoMode.IN_DESC) initPad = opad; 098 099 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)) { 120 CommSnifferIntel intel = (CommSnifferIntel) p; 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)) { 192 CommSnifferIntel intel = (CommSnifferIntel) i; 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() { 222 buttonPressConfirmed(UNINSTALL, null); 223 } 224 225 226 @Override 227 public void buttonPressConfirmed(Object buttonId, IntelUIAPI ui) { 228 if (buttonId == UNINSTALL) { 229 //endAfterDelay(); 230 endImmediately(); 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