1package com.fs.starfarer.api.impl.campaign.rulecmd.salvage;
3import java.util.ArrayList;
6import java.util.Random;
8import org.lwjgl.util.vector.Vector2f;
10import com.fs.starfarer.api.Global;
11import com.fs.starfarer.api.campaign.CampaignFleetAPI;
12import com.fs.starfarer.api.campaign.CargoAPI;
13import com.fs.starfarer.api.campaign.CargoStackAPI;
14import com.fs.starfarer.api.campaign.CoreInteractionListener;
15import com.fs.starfarer.api.campaign.FactionAPI;
16import com.fs.starfarer.api.campaign.InteractionDialogAPI;
17import com.fs.starfarer.api.campaign.OptionPanelAPI;
18import com.fs.starfarer.api.campaign.PlanetAPI;
19import com.fs.starfarer.api.campaign.SectorEntityToken;
20import com.fs.starfarer.api.campaign.StarSystemAPI;
21import com.fs.starfarer.api.campaign.TextPanelAPI;
22import com.fs.starfarer.api.campaign.comm.IntelInfoPlugin;
23import com.fs.starfarer.api.campaign.rules.MemoryAPI;
24import com.fs.starfarer.api.impl.campaign.CargoPodsEntityPlugin;
25import com.fs.starfarer.api.impl.campaign.ids.Commodities;
26import com.fs.starfarer.api.impl.campaign.ids.MemFlags;
27import com.fs.starfarer.api.impl.campaign.intel.misc.CargoPodsIntel;
28import com.fs.starfarer.api.impl.campaign.rulecmd.AddRemoveCommodity;
29import com.fs.starfarer.api.impl.campaign.rulecmd.BaseCommandPlugin;
30import com.fs.starfarer.api.impl.campaign.rulecmd.FireAll;
31import com.fs.starfarer.api.util.Misc;
32import com.fs.starfarer.api.util.Misc.Token;
40 public static final String
TRAPPED =
"$trapped";
41 public static final String
LOCKED =
"$locked";
52 protected TextPanelAPI
text;
57 protected InteractionDialogAPI
dialog;
73 public boolean execute(String ruleId, InteractionDialogAPI
dialog, List<Token> params, Map<String, MemoryAPI>
memoryMap) {
78 String command = params.get(0).getString(
memoryMap);
79 if (command ==
null)
return false;
96 if (command.equals(
"printDesc")) {
99 else if (command.equals(
"openCargo")) {
102 else if (command.equals(
"breakLocks")) {
105 else if (command.equals(
"destroy")) {
108 else if (command.equals(
"stabilize")) {
111 else if (command.equals(
"computeStabilizeData")) {
120 float stabilizeSupplies = Math.max((
int) total / 200, 2);
122 memory.set(
"$stabilizeSupplies", (
int) stabilizeSupplies, 0f);
124 float stabilizeDays = 400;
125 memory.set(
"$stabilizeDays", (
int) stabilizeDays, 0f);
130 if (
entity.isInHyperspace())
return null;
132 SectorEntityToken target =
null;
133 float minDist = Float.MAX_VALUE;
134 List<SectorEntityToken> potential =
new ArrayList<SectorEntityToken>(
entity.getContainingLocation().getPlanets());
135 SectorEntityToken center = ((StarSystemAPI)
entity.getContainingLocation()).getCenter();
136 potential.add(center);
137 potential.addAll(
entity.getContainingLocation().getJumpPoints());
138 for (SectorEntityToken curr : potential) {
139 float dist = Misc.getDistance(
entity.getLocation(), curr.getLocation());
140 dist -= curr.getRadius();
142 float maxDist = 400f;
143 if ((curr instanceof PlanetAPI && ((PlanetAPI)curr).isStar()) || curr == center) {
144 maxDist = 100000000000f;
147 if (dist < maxDist && dist < minDist) {
158 float radius = Misc.getDistance(focus.getLocation(),
entity.getLocation());
159 float orbitDays = radius / (5f + Misc.random.nextFloat() * 20f);
160 float angle = Misc.getAngleInDegreesStrict(focus.getLocation(),
entity.getLocation());
161 entity.setCircularOrbit(focus, angle, radius, orbitDays);
163 entity.getVelocity().set(0, 0);
166 if (makeDiscovered) {
167 entity.setDiscoverable(
null);
168 entity.setSensorProfile(
null);
171 entity.getMemoryWithoutUpdate().set(
"$stabilized",
true);
177 float stabilizeSupplies =
memory.getFloat(
"$stabilizeSupplies");
178 float stabilizeDays =
memory.getFloat(
"$stabilizeDays");
189 text.addParagraph(
"Your crew busy themselves attaching micro-thrusters and carefully " +
190 "sealing, balancing, and interconnecting the pods to make sure they remain in a stable orbit.");
197 memory.set(
"$stabilized",
true);
198 memory.set(
"$daysLeft", (
int) daysLeft, 0f);
201 CargoPodsIntel intel =
null;
202 for (IntelInfoPlugin iip :
Global.
getSector().getIntelManager().getIntel(CargoPodsIntel.class)) {
203 CargoPodsIntel curr = (CargoPodsIntel) iip;
204 if (curr.getPods() ==
entity) {
210 intel =
new CargoPodsIntel(
entity);
219 if (!
memory.getBoolean(
"$stabilized"))
return;
223 Vector2f vel = Misc.getUnitVectorAtDegreeAngle((
float) Math.random() * 360f);
224 vel.scale(5f + 10f * (
float) Math.random());
225 entity.getVelocity().set(vel);
227 entity.setDiscoverable(
null);
228 entity.setSensorProfile(1f);
235 String atLeastTime = Misc.getAtLeastStringForDays((
int) daysLeft);
236 text.addParagraph(
"Working with the cargo has destablized the orbit, but the pods should still be trackable for " + atLeastTime +
".");
238 memory.unset(
"$stabilized");
251 Random random =
new Random();
252 if (
memory.contains(MemFlags.SALVAGE_SEED)) {
253 long seed =
memory.getLong(MemFlags.SALVAGE_SEED);
254 random =
new Random(seed);
257 for (CargoStackAPI stack :
podsCargo.getStacksCopy()) {
258 int qty = (int) stack.getSize();
259 for (
int i = 0; i < qty; i++) {
260 if (random.nextFloat() < fractionKeep) {
261 keep.addItems(stack.getType(), stack.getData(), 1);
272 Misc.fadeAndExpire(
entity, 1f);
277 memory.set(
"$daysLeft", (
int) daysLeft, 0f);
279 boolean stabilized =
memory.getBoolean(
"$stabilized");
281 if (daysLeft >= 5000) {
282 text.addParagraph(
"The cargo pods are in a stable orbit and are unlikely to drift apart any time soon.");
284 String atLeastTime = Misc.getAtLeastStringForDays((
int) daysLeft);
285 if (stabilized && daysLeft > 20) {
286 text.addParagraph(
"The cargo pods are in a stabilized orbit, and your systems should be able to keep track of them for " + atLeastTime +
".");
288 float depth = Misc.getAbyssalDepth(
entity);
290 text.addParagraph(
"This deep in abyssal hyperspace, the cargo pods are likely to be lost forever as soon as they're out of sensor range.");
292 text.addParagraph(
"The cargo pods are in an unstable orbit, but should not drift apart and be lost for " + atLeastTime +
".");
298 String crewText =
"Sensor readings are indicative of the presence of active life support and cryosleep equipment.";
299 text.addParagraph(crewText);
304 text.addParagraph(
"The pods are locked, but you are in posession of the keycode.");
306 text.addParagraph(
"The pods are locked, and you don't have the keycode. " +
307 "It's possible to force the locks, but this carries a risk to the cargo. " +
308 "Some pods are also fitted with a self-destruct mechanism, " +
309 "ensuring the total loss of all cargo in the event of a breach attempt.");
321 dialog.getVisualPanel().showLoot(
"Cargo Pods",
podsCargo,
true,
false,
false,
new CoreInteractionListener() {
322 public void coreUIDismissed() {
326 Misc.fadeAndExpire(
entity, 1f);
332 if (!Misc.isSameCargo(preOpen,
podsCargo)) {
336 dialog.setPromptText(
"You decide to...");
static FactoryAPI getFactory()
static SectorAPI getSector()
void setElapsed(float elapsed)
void setExtraDays(float extraDays)
static void addCommodityLossText(String commodityId, int quantity, TextPanelAPI text)
static void updatePlayerMemoryQuantity(String commodityId)
static MemoryAPI getEntityMemory(Map< String, MemoryAPI > memoryMap)
static boolean fire(String ruleId, InteractionDialogAPI dialog, Map< String, MemoryAPI > memoryMap, String params)
void computeStabilizeData()
static void stabilizeOrbit(SectorEntityToken entity, boolean makeDiscovered)
static final String TRAPPED
void pruneCargo(float fractionKeep)
Map< String, MemoryAPI > memoryMap
static SectorEntityToken findOrbitFocus(SectorEntityToken entity)
CampaignFleetAPI playerFleet
static final String LOCKED
static final float BREAK_KEEP_FRACTION
static final String CAN_UNLOCK
CargoPodsEntityPlugin plugin
InteractionDialogAPI dialog
boolean execute(String ruleId, InteractionDialogAPI dialog, List< Token > params, Map< String, MemoryAPI > memoryMap)
CargoAPI createCargo(boolean unlimitedStacks)