1package com.fs.starfarer.api.impl.campaign.intel.events;
4import java.util.ArrayList;
5import java.util.Collections;
6import java.util.Comparator;
8import java.util.Random;
10import org.lwjgl.util.vector.Vector2f;
12import com.fs.starfarer.api.Global;
13import com.fs.starfarer.api.campaign.CampaignFleetAPI;
14import com.fs.starfarer.api.campaign.StarSystemAPI;
15import com.fs.starfarer.api.campaign.comm.IntelInfoPlugin;
16import com.fs.starfarer.api.campaign.comm.IntelInfoPlugin.ListInfoMode;
17import com.fs.starfarer.api.campaign.econ.MarketAPI;
18import com.fs.starfarer.api.impl.campaign.ids.Factions;
19import com.fs.starfarer.api.impl.campaign.intel.bases.PirateBaseIntel;
20import com.fs.starfarer.api.impl.campaign.intel.events.BaseEventIntel.EventStageData;
21import com.fs.starfarer.api.impl.campaign.intel.events.HostileActivityEventIntel.HAERandomEventData;
22import com.fs.starfarer.api.impl.campaign.intel.events.HostileActivityEventIntel.Stage;
23import com.fs.starfarer.api.impl.campaign.intel.group.FleetGroupIntel;
24import com.fs.starfarer.api.impl.campaign.intel.group.FleetGroupIntel.FGIEventListener;
25import com.fs.starfarer.api.impl.campaign.intel.group.GenericRaidFGI;
26import com.fs.starfarer.api.impl.campaign.intel.group.GenericRaidFGI.GenericRaidParams;
27import com.fs.starfarer.api.impl.campaign.missions.FleetCreatorMission;
28import com.fs.starfarer.api.impl.campaign.missions.FleetCreatorMission.FleetStyle;
29import com.fs.starfarer.api.impl.campaign.procgen.StarSystemGenerator;
30import com.fs.starfarer.api.impl.campaign.rulecmd.KantaCMD;
31import com.fs.starfarer.api.ui.TooltipMakerAPI;
32import com.fs.starfarer.api.ui.TooltipMakerAPI.TooltipCreator;
33import com.fs.starfarer.api.util.Misc;
34import com.fs.starfarer.api.util.WeightedRandomPicker;
43 public static String
RAID_KEY =
"$PirateRaid_ref";
70 return super.getProgress(
intel);
74 return "Pirate activity";
78 if (first)
return "Pirates";
85 return Misc.getGrayColor();
87 return Global.
getSector().getFaction(Factions.PIRATES).getBaseUIColor();
92 public void createTooltip(TooltipMakerAPI tooltip,
boolean expanded, Object tooltipParam) {
94 tooltip.addPara(
"Piracy follows interstellar civilization almost without exception.", 0f);
95 if (KantaCMD.playerHasProtection()) {
96 tooltip.addPara(
"However, you have %s, which is enough dissuade most pirates from attacking your interests.",
97 opad, Misc.getPositiveHighlightColor(),
"Kanta's protection");
99 if (KantaCMD.playerEverHadProtection()) {
100 tooltip.addPara(
"You've %s, and it's not the sort of thing you can do over.",
101 opad, Misc.getNegativeHighlightColor(),
"lost Kanta's protection");
103 tooltip.addPara(
"Having %s, however, should be enough dissuade most pirates from attacking your interests.",
104 opad, Misc.getHighlightColor(),
"Kanta's protection");
117 return Global.
getSector().getFaction(Factions.PIRATES).getBaseUIColor();
122 return Misc.getGrayColor();
124 return Global.
getSector().getFaction(Factions.PIRATES).getBaseUIColor();
132 return super.getMaxNumFleets(system);
135 public CampaignFleetAPI
createFleet(StarSystemAPI system, Random random) {
143 difficulty += (int) Math.round(f * 7f);
158 if (difficulty < minDiff) difficulty = minDiff;
160 difficulty += random.nextInt(4);
162 FleetCreatorMission m =
new FleetCreatorMission(random);
165 Vector2f loc = system.getLocation();
166 String factionId = Factions.PIRATES;
168 m.createStandardFleet(difficulty, factionId, loc);
169 m.triggerSetPirateFleet();
170 m.triggerMakeLowRepImpact();
173 CampaignFleetAPI fleet = m.createFleet();
182 ListInfoMode mode,
boolean isUpdate, Color tc,
float initPad) {
183 info.addPara(
"Rumors of pirate raid", tc, initPad);
189 ListInfoMode mode,
boolean isUpdate, Color tc,
float initPad) {
190 info.addPara(
"Pirate raid averted", tc, initPad);
208 info.addPara(
"There are rumors that a pirate raid targeting your colonies "
209 +
"may be organized in the near future. If the raid is successful, the targeted colonies will "
210 +
"suffer from reduced stability.", small,
211 Misc.getNegativeHighlightColor(),
"reduced stability");
213 if (stage.id == Stage.HA_EVENT) {
215 info.addPara(
"If the raid is defeated, your colonies will "
216 +
"permanently gain increased accessibility.",
217 opad, Misc.getPositiveHighlightColor(),
"increased accessibility");
219 info.addPara(
"If the raid is defeated, your colonies will gain "
220 +
"increased accessibility for several cycles.",
221 opad, Misc.getPositiveHighlightColor(),
"increased accessibility");
226 stage.addResetReq(info,
false,
"crisis", -1, -1, opad);
252 if (stage.id == Stage.HA_EVENT || stage.id == Stage.MINOR_EVENT) {
253 if (stage.id == Stage.MINOR_EVENT) {
256 public void createTooltip(TooltipMakerAPI tooltip,
boolean expanded, Object tooltipParam) {
258 tooltip.addTitle(
"Pirate raid");
259 tooltip.addPara(
"A pirate raid will be launched against one of your "
260 +
"star systems.", opad);
284 if (KantaCMD.playerHasProtection())
return 0f;
288 if (stage.id == Stage.HA_EVENT || stage.id == Stage.MINOR_EVENT) {
291 if (target !=
null && source !=
null) {
305 HAERandomEventData data =
new HAERandomEventData(
this, stage);
306 stage.rollData = data;
313 if (source ==
null || target ==
null) {
317 stage.rollData =
null;
324 for (StarSystemAPI system : Misc.getPlayerSystems(
false)) {
326 if (mag < 0.1f && stage.id != Stage.MINOR_EVENT) {
330 picker.add(system, mag * mag);
333 return picker.pick();
337 if (target ==
null)
return null;
339 List<MarketAPI> list =
new ArrayList<MarketAPI>();
342 for (IntelInfoPlugin curr :
Global.
getSector().getIntelManager().getIntel(PirateBaseIntel.class)) {
343 PirateBaseIntel base = (PirateBaseIntel) curr;
344 if (base.playerHasDealWithBaseCommander())
continue;
346 float dist = Misc.getDistance(target.getLocation(), base.getMarket().getLocationInHyperspace());
347 if (dist > maxDist)
continue;
349 list.add(base.getMarket());
352 for (MarketAPI market :
Global.
getSector().getEconomy().getMarketsCopy()) {
353 if (Factions.PIRATES.equals(market.getFaction().getId())) {
354 for (MarketAPI other : Misc.getMarketsInLocation(market.getContainingLocation())) {
355 if (other == market)
continue;
356 if (!other.getFaction().isHostileTo(market.getFaction()))
continue;
357 if (other.getSize() <= market.getSize() - 2)
continue;
359 float dist = Misc.getDistance(market.getPrimaryEntity().getLocation(), other.getPrimaryEntity().getLocation());
360 if (dist < 8000)
continue;
367 Collections.sort(list,
new Comparator<MarketAPI>() {
368 public int compare(MarketAPI m1, MarketAPI m2) {
369 float d1 = Misc.getDistance(target.getLocation(), m1.getLocationInHyperspace());
370 float d2 = Misc.getDistance(target.getLocation(), m2.getLocationInHyperspace());
371 return (
int) Math.signum(d1 - d2);
376 for (
int i = 0; i < list.size() && i < 4; i++) {
377 MarketAPI market = list.get(i);
378 float dist = Misc.getDistance(target.getLocation(), market.getLocationInHyperspace());
379 float w = 100000f / (dist * dist);
380 picker.add(market, w);
383 return picker.pick();
391 if (GenericRaidFGI.get(
RAID_KEY) !=
null) {
392 GenericRaidFGI.get(
RAID_KEY).finish(
false);
396 if (
intel ==
null)
return;
405 public boolean startRaid(MarketAPI source, StarSystemAPI target, EventStageData stage, Random random) {
406 GenericRaidParams params =
new GenericRaidParams(
new Random(random.nextLong()),
true);
407 params.factionId = source.getFactionId();
408 params.source = source;
410 params.prepDays = 7f + random.nextFloat() * 14f;
411 params.payloadDays = 27f + 7f * random.nextFloat();
413 params.raidParams.where = target;
414 for (MarketAPI market : Misc.getMarketsInLocation(target)) {
415 if (market.getFaction().isHostileTo(source.getFaction()) || market.getFaction().isPlayerFaction()) {
416 params.raidParams.allowedTargets.add(market);
419 if (params.raidParams.allowedTargets.isEmpty())
return false;
420 params.raidParams.allowNonHostileTargets =
true;
422 params.style = FleetStyle.STANDARD;
424 if (stage.id == Stage.MINOR_EVENT) {
425 params.fleetSizes.add(5);
426 params.fleetSizes.add(3);
432 if (mag1 > 1f) mag1 = 1f;
434 float totalDifficulty = (0.25f + mag1 * 0.25f + mag2 * 0.5f) * 100f;
437 if (r.nextFloat() < 0.33f) {
438 params.style = FleetStyle.QUANTITY;
441 while (totalDifficulty > 0) {
442 float max = Math.min(10f, totalDifficulty * 0.5f);
443 float min = Math.max(2, max - 2);
444 if (max < min) max = min;
446 int diff = Math.round(StarSystemGenerator.getNormalRandom(r, min, max));
448 params.fleetSizes.add(diff);
449 totalDifficulty -= diff;
453 PirateBaseIntel base = PirateBaseIntel.getIntelFor(source);
455 if (Misc.isHiddenBase(source) && !base.isPlayerVisible()) {
457 base.sendUpdateIfPlayerHasIntel(PirateBaseIntel.DISCOVERED_PARAM,
false);
461 GenericRaidFGI raid =
new GenericRaidFGI(params);
462 if (stage.id == Stage.HA_EVENT) {
463 raid.setListener(
this);
477 public static void main(String[] args) {
478 Random r =
new Random();
479 int [] counts =
new int[11];
480 for (
int i = 0; i < 10000; i++) {
484 for (
int i = 0; i < counts.length; i++) {
485 System.out.println(i +
": " + counts[i]);
490 double r = random.nextGaussian();
499 return min + (float) r * (max - min);
static SettingsAPI getSettings()
static SectorAPI getSector()
void sendUpdateIfPlayerHasIntel(Object listInfoParam, TextPanelAPI textPanel)
void addBorder(TooltipMakerAPI info, Color c)
float getEffectMagnitude(StarSystemAPI system)
Random getRandomizedStageRandom()
TooltipCreator getDefaultEventTooltip(final String title, final HostileActivityEventIntel intel, final EventStageData stage)
HostileActivityEventIntel intel
HAERandomEventData getRollDataForEvent()
static HostileActivityEventIntel get()
float getMarketPresenceFactor(StarSystemAPI system)
static PiracyRespiteScript get()
int getMaxNumFleets(StarSystemAPI system)
float getEventFrequency(HostileActivityEventIntel intel, EventStageData stage)
Color getDescColor(BaseEventIntel intel)
boolean shouldShow(BaseEventIntel intel)
TooltipCreator getMainRowTooltip(BaseEventIntel intel)
boolean startRaid(MarketAPI source, StarSystemAPI target, EventStageData stage, Random random)
static String SMALL_RAID_KEY
static void main(String[] args)
static final String DEFEATED_LARGE_PIRATE_RAID
boolean fireEvent(HostileActivityEventIntel intel, EventStageData stage)
static boolean isDefeatedLargePirateRaid()
Color getNameColor(float mag)
Color getNameColorForThreatList()
void rollEvent(HostileActivityEventIntel intel, EventStageData stage)
String getNameForThreatList(boolean first)
static void setDefeatedLargePirateRaid(boolean value)
String getDesc(BaseEventIntel intel)
PirateHostileActivityFactor(HostileActivityEventIntel intel)
MarketAPI findRaidSource(HostileActivityEventIntel intel, EventStageData stage, final StarSystemAPI target)
CampaignFleetAPI createFleet(StarSystemAPI system, Random random)
String getProgressStr(BaseEventIntel intel)
StarSystemAPI findRaidTarget(HostileActivityEventIntel intel, EventStageData stage)
String getEventStageIcon(HostileActivityEventIntel intel, EventStageData stage)
int getProgress(BaseEventIntel intel)
static float getNormalRandom(Random random, float min, float max)
void addBulletPointForEventReset(HostileActivityEventIntel intel, EventStageData stage, TooltipMakerAPI info, ListInfoMode mode, boolean isUpdate, Color tc, float initPad)
void addBulletPointForEvent(HostileActivityEventIntel intel, EventStageData stage, TooltipMakerAPI info, ListInfoMode mode, boolean isUpdate, Color tc, float initPad)
static void avertOrAbortRaid()
TooltipCreator getStageTooltipImpl(final HostileActivityEventIntel intel, final EventStageData stage)
void reportFGIAborted(FleetGroupIntel intel)
void addStageDescriptionForEvent(HostileActivityEventIntel intel, EventStageData stage, TooltipMakerAPI info)
float getFloat(String key)