001package com.fs.starfarer.api.impl.campaign.events; 002 003import java.awt.Color; 004import java.util.ArrayList; 005import java.util.Arrays; 006import java.util.HashMap; 007import java.util.List; 008import java.util.Map; 009 010import com.fs.starfarer.api.Global; 011import com.fs.starfarer.api.campaign.BattleAPI; 012import com.fs.starfarer.api.campaign.CampaignEventListener; 013import com.fs.starfarer.api.campaign.CampaignFleetAPI; 014import com.fs.starfarer.api.campaign.CargoAPI; 015import com.fs.starfarer.api.campaign.FactionAPI; 016import com.fs.starfarer.api.campaign.FleetEncounterContextPlugin; 017import com.fs.starfarer.api.campaign.InteractionDialogAPI; 018import com.fs.starfarer.api.campaign.PlayerMarketTransaction; 019import com.fs.starfarer.api.campaign.RepLevel; 020import com.fs.starfarer.api.campaign.SectorEntityToken; 021import com.fs.starfarer.api.campaign.StarSystemAPI; 022import com.fs.starfarer.api.campaign.JumpPointAPI.JumpDestination; 023import com.fs.starfarer.api.campaign.comm.MessagePriority; 024import com.fs.starfarer.api.campaign.econ.MarketAPI; 025import com.fs.starfarer.api.campaign.events.CampaignEventManagerAPI; 026import com.fs.starfarer.api.campaign.events.CampaignEventPlugin; 027import com.fs.starfarer.api.campaign.events.CampaignEventTarget; 028import com.fs.starfarer.api.campaign.rules.MemoryAPI; 029import com.fs.starfarer.api.characters.AbilityPlugin; 030import com.fs.starfarer.api.characters.PersonAPI; 031import com.fs.starfarer.api.combat.EngagementResultAPI; 032import com.fs.starfarer.api.impl.campaign.ids.Events; 033import com.fs.starfarer.api.impl.campaign.ids.Tags; 034import com.fs.starfarer.api.util.Misc; 035import com.fs.starfarer.api.util.Misc.Token; 036 037public class BaseEventPlugin implements CampaignEventPlugin, CampaignEventListener { 038 039 private String id = Misc.genUID(); 040 041 protected String eventType; 042 protected CampaignEventTarget eventTarget; 043 protected MarketAPI market; 044 protected SectorEntityToken entity; 045 protected FactionAPI faction; 046 protected String statModId; 047 protected boolean started; 048 049 protected MemoryAPI memory = null; 050 051 protected float startProbability; 052 053 public void init(String eventType, CampaignEventTarget eventTarget) { 054 init(eventType, eventTarget, true); 055 } 056 public void init(String eventType, CampaignEventTarget eventTarget, boolean addListener) { 057 this.eventType = eventType; 058 this.eventTarget = eventTarget; 059 060 setTarget(eventTarget); 061 062 if (market == null) { 063 statModId = eventType + "_" + Misc.genUID(); 064 } else { 065 statModId = eventType + "_" + market.getId() + "_" + Misc.genUID(); 066 } 067 068 if (addListener) { 069 Global.getSector().addListener(this); 070 } 071 } 072 073 public void setTarget(CampaignEventTarget eventTarget) { 074 this.eventTarget = eventTarget; 075 if (eventTarget.getEntity() != null) { 076 market = eventTarget.getEntity().getMarket(); 077 faction = eventTarget.getFaction(); 078 entity = eventTarget.getEntity(); 079 } 080 } 081 082 protected String getLoggingId() { 083 if (market != null) { 084 return "[" + market.getName() + "]"; 085 } else if (eventTarget != null && eventTarget.getEntity() != null) { 086 return "[" + eventTarget.getEntity().getName() + "]"; 087 } else { 088 return "[" + eventType + "]"; 089 } 090 } 091 092 public void cleanup() { 093 Global.getSector().removeListener(this); 094 } 095 096 public void startEvent() { 097 startEvent(false); 098 } 099 public void startEvent(boolean addListener) { 100 started = true; 101 if (addListener) { 102 Global.getSector().addListener(this); 103 } 104 } 105 106 protected boolean isEventStarted() { 107 return started; 108 } 109 110 public void advance(float amount) { 111 112 } 113 114 public CampaignEventTarget getEventTarget() { 115 return eventTarget; 116 } 117 118 public String getEventType() { 119 return eventType; 120 } 121 122 public String getStageIdForLikely() { 123 return null; 124 } 125 126 public String getStageIdForPossible() { 127 return null; 128 } 129 130 public Map<String, String> getTokenReplacements() { 131 HashMap<String, String> tokens = new HashMap<String, String>(); 132 133 CampaignFleetAPI playerFleet = Global.getSector().getPlayerFleet(); 134 tokens.put("$playerName", Global.getSector().getCharacterData().getName()); 135 //PersonAPI playerPerson = playerFleet.getCommander(); 136 PersonAPI playerPerson = Global.getSector().getPlayerPerson(); 137 if (playerPerson != null) { 138 if (playerPerson.isMale()) { 139 tokens.put("$playerHisOrHer", "his"); 140 tokens.put("$PlayerHisOrHer", "His"); 141 tokens.put("$playerHimOrHer", "him"); 142 tokens.put("$PlayerHimOrHer", "Him"); 143 tokens.put("$playerHeOrShe", "he"); 144 tokens.put("$PlayerHeOrShe", "He"); 145 } else { 146 tokens.put("$playerHisOrHer", "her"); 147 tokens.put("$PlayerHisOrHer", "Her"); 148 tokens.put("$playerHimOrHer", "her"); 149 tokens.put("$PlayerHimOrHer", "Her"); 150 tokens.put("$playerHeOrShe", "she"); 151 tokens.put("$PlayerHeOrShe", "She"); 152 } 153 } 154 155 MarketAPI market = this.market; 156 SectorEntityToken entity = this.entity; 157 FactionAPI faction = this.faction; 158 if (getEventTarget() != null) { // for special events that can change their targets 159 market = getEventTarget().getMarket(); 160 entity = getEventTarget().getEntity(); 161 faction = getEventTarget().getFaction(); 162 } 163 164 if (entity != null) { 165 tokens.put("$name", entity.getName()); 166 } 167 168 if (market != null) { 169 if (market.getPrimaryEntity().hasTag(Tags.STATION)) { 170 tokens.put("$onOrAt", "at"); 171 } else { 172 tokens.put("$onOrAt", "on"); 173 } 174 175 tokens.put("$market", market.getName()); 176 tokens.put("$marketFaction", market.getFaction().getDisplayName()); 177 tokens.put("$MarketFaction", Misc.ucFirst(market.getFaction().getDisplayName())); 178 tokens.put("$TheMarketFaction", Misc.ucFirst(market.getFaction().getDisplayNameWithArticle())); 179 tokens.put("$theMarketFaction", market.getFaction().getDisplayNameWithArticle()); 180 181 if (eventTarget.getLocation() instanceof StarSystemAPI) { 182 //tokens.put("$marketSystem", ((StarSystemAPI)eventTarget.getLocation()).getBaseName() + " star system"); 183 tokens.put("$marketSystem", ((StarSystemAPI)eventTarget.getLocation()).getBaseName()); 184 } else { 185 tokens.put("$marketSystem", "hyperspace"); 186 } 187 188 RepLevel level = playerFleet.getFaction().getRelationshipLevel(market.getFaction()); 189 tokens.put("$factionStanding", level.getDisplayName().toLowerCase()); 190 tokens.put("$FactionStanding", Misc.ucFirst(level.getDisplayName())); 191 } 192 193 if (faction != null) { 194 RepLevel level = playerFleet.getFaction().getRelationshipLevel(faction); 195 tokens.put("$factionStanding", level.getDisplayName().toLowerCase()); 196 tokens.put("$FactionStanding", Misc.ucFirst(level.getDisplayName())); 197 } 198 199 if (playerFleet != null) { 200 String fleetOrShip = "fleet"; 201 if (playerFleet.getFleetData().getMembersListCopy().size() == 1) { 202 fleetOrShip = "ship"; 203 if (playerFleet.getFleetData().getMembersListCopy().get(0).isFighterWing()) { 204 fleetOrShip = "fighter wing"; 205 } 206 } 207 tokens.put("$playerShipOrFleet", fleetOrShip); 208 } 209 210 //if (getEventTarget() != null && getEventTarget().getEntity() instanceof CampaignFleetAPI) { 211 //if (getEventTarget() != null && getEventTarget().getEntity() != null) { 212 //getEventTarget().getEntity().getFaction(); 213 if (faction != null) { 214 //CampaignFleetAPI fleet = (CampaignFleetAPI) getEventTarget().getEntity(); 215 //FactionAPI faction = getEventTarget().getEntity().getFaction(); 216 String factionName = faction.getEntityNamePrefix(); 217 if (factionName == null || factionName.isEmpty()) { 218 factionName = faction.getDisplayName(); 219 } 220 221 tokens.put("$factionIsOrAre", faction.getDisplayNameIsOrAre()); 222 223 tokens.put("$faction", factionName); 224 tokens.put("$ownerFaction", factionName); 225 tokens.put("$marketFaction", factionName); 226 tokens.put("$Faction", Misc.ucFirst(factionName)); 227 tokens.put("$OwnerFaction", Misc.ucFirst(factionName)); 228 tokens.put("$MarketFaction", Misc.ucFirst(factionName)); 229 tokens.put("$theFaction", faction.getDisplayNameWithArticle()); 230 tokens.put("$theOwnerFaction", faction.getDisplayNameWithArticle()); 231 tokens.put("$theMarketFaction", faction.getDisplayNameWithArticle()); 232 tokens.put("$TheFaction", Misc.ucFirst(faction.getDisplayNameWithArticle())); 233 tokens.put("$TheOwnerFaction", Misc.ucFirst(faction.getDisplayNameWithArticle())); 234 tokens.put("$TheMarketFaction", Misc.ucFirst(faction.getDisplayNameWithArticle())); 235 236 tokens.put("$factionLong", faction.getDisplayNameLong()); 237 tokens.put("$FactionLong", Misc.ucFirst(faction.getDisplayNameLong())); 238 tokens.put("$theFactionLong", faction.getDisplayNameLongWithArticle()); 239 tokens.put("$TheFactionLong", Misc.ucFirst(faction.getDisplayNameLongWithArticle())); 240 } 241 242 return tokens; 243 } 244 245 246 public static void addFactionNameTokens(Map<String, String> tokens, String prefix, FactionAPI faction) { 247 if (faction != null) { 248 String factionName = faction.getEntityNamePrefix(); 249 if (factionName == null || factionName.isEmpty()) { 250 factionName = faction.getDisplayName(); 251 } 252 String prefixUC = Misc.ucFirst(prefix); 253 tokens.put("$" + prefix + "Faction", factionName); 254 tokens.put("$" + prefixUC + "Faction", Misc.ucFirst(factionName)); 255 tokens.put("$the" + prefixUC + "Faction", faction.getDisplayNameWithArticle()); 256 tokens.put("$The" + prefixUC + "Faction", Misc.ucFirst(faction.getDisplayNameWithArticle())); 257 258 tokens.put("$" + prefix + "FactionLong", faction.getDisplayNameLong()); 259 tokens.put("$" + prefixUC + "FactionLong", Misc.ucFirst(faction.getDisplayNameLong())); 260 tokens.put("$the" + prefixUC + "FactionLong", faction.getDisplayNameLongWithArticle()); 261 tokens.put("$The" + prefixUC + "FactionLong", Misc.ucFirst(faction.getDisplayNameLongWithArticle())); 262 263 tokens.put("$" + prefix + "FactionIsOrAre", faction.getDisplayNameIsOrAre()); 264 } 265 } 266 267 public static void addPersonTokens(Map<String, String> tokens, String prefix, PersonAPI person) { 268 if (person != null) { 269 tokens.put("$" + prefix + "Name", person.getName().getFullName()); 270 tokens.put("$" + prefix + "LastName", person.getName().getLast()); 271 tokens.put("$" + prefix + "FirstName", person.getName().getFirst()); 272 273 274 tokens.put("$" + prefix + "Rank", person.getRank().toLowerCase()); 275 tokens.put("$" + Misc.ucFirst(prefix) + "Rank", Misc.ucFirst(person.getRank())); 276 277 tokens.put("$" + prefix + "Post", person.getPost().toLowerCase()); 278 tokens.put("$" + Misc.ucFirst(prefix) + "Post", Misc.ucFirst(person.getPost())); 279 280 281 if (person.isMale()) { 282 tokens.put("$" + prefix + "HisOrHer", "his"); 283 tokens.put("$" + Misc.ucFirst(prefix) + "HisOrHer", "His"); 284 tokens.put("$" + prefix + "HimOrHer", "him"); 285 tokens.put("$" + Misc.ucFirst(prefix) + "HimOrHer", "Him"); 286 tokens.put("$" + prefix + "HeOrShe", "he"); 287 tokens.put("$" + Misc.ucFirst(prefix) + "HeOrShe", "He"); 288 } else { 289 tokens.put("$" + prefix + "HisOrHer", "her"); 290 tokens.put("$" + Misc.ucFirst(prefix) + "HisOrHer", "Her"); 291 tokens.put("$" + prefix + "HimOrHer", "her"); 292 tokens.put("$" + Misc.ucFirst(prefix) + "HimOrHer", "Her"); 293 tokens.put("$" + prefix + "HeOrShe", "she"); 294 tokens.put("$" + Misc.ucFirst(prefix) + "HeOrShe", "She"); 295 } 296 } 297 } 298 299 public MessagePriority getWarningWhenLikelyPriority() { 300 return null; 301 } 302 303 public MessagePriority getWarningWhenPossiblePriority() { 304 return null; 305 } 306 307 public boolean isDone() { 308 return false; 309 } 310 311 public void setParam(Object param) { 312 313 } 314 315 public String getTargetName() { 316 if (eventTarget.getEntity() != null) { 317 return eventTarget.getEntity().getName() + " (" + eventTarget.getLocation().getName() + ")"; 318 } 319 return eventTarget.getLocation().getName(); 320 } 321 322 public static interface MarketFilter { 323 boolean acceptMarket(MarketAPI market); 324 } 325 326 public static MarketAPI findNearestMarket(MarketAPI from, MarketFilter filter) { 327 float minDist = Float.MAX_VALUE; 328 MarketAPI result = null; 329 for (MarketAPI market : Global.getSector().getEconomy().getMarketsCopy()) { 330 if (market == from) continue; 331 float dist = Misc.getDistance(market.getLocationInHyperspace(), from.getLocationInHyperspace()); 332 if (dist < minDist && (filter == null || filter.acceptMarket(market))) { 333 minDist = dist; 334 result = market; 335 } 336 } 337 return result; 338 } 339 340 public static List<MarketAPI> findMatchingMarkets(MarketFilter filter) { 341 List<MarketAPI> result = new ArrayList<MarketAPI>(); 342 for (MarketAPI market : Global.getSector().getEconomy().getMarketsCopy()) { 343 if (filter == null || filter.acceptMarket(market)) { 344 result.add(market); 345 } 346 } 347 return result; 348 } 349 350 public void increaseRecentUnrest(float stabilityChange) { 351 if (stabilityChange <= 0) return; 352 CampaignEventManagerAPI manager = Global.getSector().getEventManager(); 353 RecentUnrestEvent event = (RecentUnrestEvent) manager.getOngoingEvent(eventTarget, Events.RECENT_UNREST); 354 if (event == null) { 355 event = (RecentUnrestEvent) manager.startEvent(eventTarget, Events.RECENT_UNREST, null); 356 } 357 event.increaseStabilityPenalty((int) stabilityChange); 358 } 359 360 public void reportPlayerMarketTransaction(PlayerMarketTransaction transaction) { 361 362 } 363 364 public void reportFleetDespawned(CampaignFleetAPI fleet, FleetDespawnReason reason, Object param) { 365 } 366 367 public void reportFleetJumped(CampaignFleetAPI fleet, SectorEntityToken from, JumpDestination to) { 368 } 369 370 public void reportFleetReachedEntity(CampaignFleetAPI fleet, SectorEntityToken entity) { 371 } 372 373 public boolean allowMultipleOngoingForSameTarget() { 374 return false; 375 } 376 377 public String[] getHighlights(String stageId) { 378 return null; 379 } 380 381 public Color[] getHighlightColors(String stageId) { 382 String [] highlights = getHighlights(stageId); 383 if (highlights != null) { 384 Color c = Global.getSettings().getColor("buttonShortcut"); 385 Color [] colors = new Color[highlights.length]; 386 Arrays.fill(colors, c); 387 return colors; 388 } 389 return null; 390 } 391 392 393 public void addTokensToList(List<String> list, String ... keys) { 394 Map<String, String> tokens = getTokenReplacements(); 395 for (String key : keys) { 396 if (tokens.containsKey(key)) { 397 list.add(tokens.get(key)); 398 } 399 } 400 } 401 402 public String getEventName() { 403 return "BaseEventPlugin.getEventName()"; 404 } 405 406 public CampaignEventCategory getEventCategory() { 407 return CampaignEventCategory.EVENT; 408 } 409 410 protected MessagePriority getDefaultPriority() { 411 MessagePriority priority = MessagePriority.SECTOR; 412 switch (market.getSize()) { 413 case 1: 414 case 2: 415 case 3: 416 priority = MessagePriority.SYSTEM; 417 break; 418 case 4: 419 case 5: 420 case 6: 421 priority = MessagePriority.CLUSTER; 422 break; 423 case 7: 424 case 8: 425 case 9: 426 priority = MessagePriority.SECTOR; 427 break; 428 } 429 return priority; 430 } 431 432 public List<String> getRelatedCommodities() { 433 return null; 434 } 435 436 public List<PriceUpdatePlugin> getPriceUpdates() { 437 return null; 438 } 439 440 public void reportShownInteractionDialog(InteractionDialogAPI dialog) { 441 442 } 443 444 public void reportPlayerOpenedMarket(MarketAPI market) { 445 446 } 447 448 public String getCurrentMessageIcon() { 449 return null; 450 } 451 452 public String getCurrentImage() { 453 return null; 454 } 455 456 public String getEventIcon() { 457 return null; 458 } 459 460 public boolean showAllMessagesIfOngoing() { 461 return true; 462 } 463 464 public void reportPlayerReputationChange(String faction, float delta) { 465 466 } 467 468 public void reportPlayerEngagement(EngagementResultAPI result) { 469 470 } 471 472 public void reportFleetSpawned(CampaignFleetAPI fleet) { 473 474 } 475 476 public void reportPlayerOpenedMarketAndCargoUpdated(MarketAPI market) { 477 478 } 479 480 public void reportEncounterLootGenerated(FleetEncounterContextPlugin plugin, CargoAPI loot) { 481 482 } 483 484 public void reportPlayerClosedMarket(MarketAPI market) { 485 486 } 487 488 public boolean callEvent(String ruleId, InteractionDialogAPI dialog, List<Token> params, Map<String, MemoryAPI> memoryMap) { 489 return true; 490 } 491 492 public MemoryAPI getMemory() { 493 return memory; 494 } 495 496 public String getId() { 497 if (id == null) { 498 id = Misc.genUID(); 499 } 500 return id; 501 } 502 503 public String getStatModId() { 504 return statModId; 505 } 506 507 public void reportPlayerReputationChange(PersonAPI person, float delta) { 508 509 } 510 511 public void reportPlayerActivatedAbility(AbilityPlugin ability, Object param) { 512 513 } 514 515 public void reportPlayerDeactivatedAbility(AbilityPlugin ability, Object param) { 516 517 } 518 519 public void reportBattleFinished(CampaignFleetAPI primaryWinner, BattleAPI battle) { 520 521 } 522 523 public void reportBattleOccurred(CampaignFleetAPI primaryWinner, BattleAPI battle) { 524 525 } 526 public void setProbability(float p) { 527 this.startProbability = p; 528 529 } 530 public boolean useEventNameAsId() { 531 return false; 532 } 533 534 public boolean showLatestMessageIfOngoing() { 535 return true; 536 } 537 538 public void reportPlayerDumpedCargo(CargoAPI cargo) { 539 540 } 541 public void reportPlayerDidNotTakeCargo(CargoAPI cargo) { 542 543 } 544 public void reportEconomyMonthEnd() { 545 // TODO Auto-generated method stub 546 547 } 548 public void reportEconomyTick(int iterIndex) { 549 // TODO Auto-generated method stub 550 551 } 552} 553 554 555 556 557 558