Starsector API
Loading...
Searching...
No Matches
WormholeManager.java
Go to the documentation of this file.
1package com.fs.starfarer.api.impl.campaign.shared;
2
3import java.util.ArrayList;
4import java.util.Collections;
5import java.util.Comparator;
6import java.util.List;
7
8import org.json.JSONException;
9import org.json.JSONObject;
10
11import com.fs.starfarer.api.Global;
12import com.fs.starfarer.api.campaign.CargoAPI.CargoItemType;
13import com.fs.starfarer.api.campaign.InteractionDialogAPI;
14import com.fs.starfarer.api.campaign.JumpPointAPI;
15import com.fs.starfarer.api.campaign.JumpPointAPI.JumpDestination;
16import com.fs.starfarer.api.campaign.LocationAPI;
17import com.fs.starfarer.api.campaign.SectorEntityToken;
18import com.fs.starfarer.api.campaign.SpecialItemData;
19import com.fs.starfarer.api.impl.campaign.JumpPointInteractionDialogPluginImpl;
20import com.fs.starfarer.api.impl.campaign.ids.Entities;
21import com.fs.starfarer.api.impl.campaign.ids.Factions;
22import com.fs.starfarer.api.impl.campaign.ids.Items;
23import com.fs.starfarer.api.impl.campaign.rulecmd.AddRemoveCommodity;
24
25public class WormholeManager {
26
27 public static String KEY = "$core_wormholeManager";
28
29 public static String WORMHOLE = "$wormhole";
30 public static String LIMBO_STABLE_LOCATION = "$limboStableLocation";
31 public static String GOT_WORMHOLE_CALIBRATION_DATA = "$gotWormholeCalibrationData";
32
33
34// public static float UNSTABLE_DURATION_MIN = 365f * 0.75f;
35// public static float UNSTABLE_DURATION_MAX = 365 * 1.25f;
36 public static float UNSTABLE_DURATION_MIN = 180f * 0.75f;
37 public static float UNSTABLE_DURATION_MAX = 180f * 1.25f;
38
39 public static WormholeManager get() {
40 Object obj = Global.getSector().getMemoryWithoutUpdate().get(KEY);
41 if (obj != null) return (WormholeManager) obj;
42
44 Global.getSector().getMemoryWithoutUpdate().set(KEY, data);
45 return data;
46 }
47
48 public static class WormholeItemData {
49 public String category;
50 public String id;
51 public String name;
52 public WormholeItemData(String jsonStr) {
53 try {
54 JSONObject json = new JSONObject(jsonStr);
55 category = json.getString("cat");
56 id = json.getString("id");
57 name = json.getString("name");
58 } catch (JSONException e) {
59 throw new RuntimeException("Unable to parse Wormhole Anchor data [" + jsonStr + "]", e);
60 }
61 }
62 public WormholeItemData(String category, String id, String name) {
63 this.category = category;
64 this.id = id;
65 this.name = name;
66 }
67
68 public String toJsonStr() {
69 try {
70 JSONObject json = new JSONObject();
71 json.put("cat", category);
72 json.put("id", id);
73 json.put("name", name);
74 return json.toString();
75 } catch (JSONException e) {
76 return null;
77 }
78 }
79 }
80
81 public static class WormholeData {
82 public SpecialItemData item;
83 public JumpPointAPI jumpPoint;
84 public WormholeItemData itemData;
85 public WormholeData(SpecialItemData item, JumpPointAPI jumpPoint, WormholeItemData itemData) {
86 this.item = item;
87 this.jumpPoint = jumpPoint;
88 this.itemData = itemData;
89 }
90 }
91
92 public static boolean willWormholeBecomeUnstable(SectorEntityToken stableLocation) {
93 boolean makeUnstable = true;
94 if (stableLocation.getMemoryWithoutUpdate().getBoolean(LIMBO_STABLE_LOCATION) &&
95 Global.getSector().getPlayerMemoryWithoutUpdate().getBoolean(GOT_WORMHOLE_CALIBRATION_DATA)) {
96 makeUnstable = false;
97 }
98 return makeUnstable;
99 }
100
101
102 protected List<WormholeData> deployed = new ArrayList<WormholeData>();
103
104
106 List<WormholeData> sorted = new ArrayList<WormholeData>(deployed);
107 Collections.sort(sorted, new Comparator<WormholeData>() {
108 public int compare(WormholeData o1, WormholeData o2) {
109 return o1.itemData.name.compareTo(o2.itemData.name);
110 }
111 });
112
113 for (WormholeData data : deployed) {
114 JumpPointAPI jp = data.jumpPoint;
115 jp.clearDestinations();
116 boolean added = false;
117 for (WormholeData other : sorted) {
118 if (other == data) continue;
119 if (!data.itemData.category.equals(other.itemData.category)) continue;
120
121 jp.addDestination(new JumpDestination(other.jumpPoint, "wormhole terminus " + other.itemData.name));
122 added = true;
123 }
124 if (added) {
125 jp.setStandardWormholeToStarfieldVisual();
126 } else {
127 jp.setStandardWormholeToNothingVisual();
128 }
129 }
130 }
131
132 public JumpPointAPI addWormhole(SpecialItemData item, SectorEntityToken stableLocation, InteractionDialogAPI dialog) {
133 WormholeItemData itemData = new WormholeItemData(item.getData());
134
135 WormholeData data = getDeployed(itemData.id);
136 if (data != null) return data.jumpPoint;
137
138 boolean makeUnstable = willWormholeBecomeUnstable(stableLocation);
139
140 LocationAPI loc = stableLocation.getContainingLocation();
141 JumpPointAPI wormhole = Global.getFactory().createJumpPoint(null, "Wormhole Terminus " + itemData.name);
142 wormhole.setRadius(75f);
143 //wormhole.setStandardWormholeToStarfieldVisual();
144
145 wormhole.getMemoryWithoutUpdate().set(WORMHOLE, true);
146 if (makeUnstable) {
147 float dur = UNSTABLE_DURATION_MIN + (UNSTABLE_DURATION_MAX - UNSTABLE_DURATION_MIN) * (float) Math.random();
148 if (Global.getSettings().isDevMode() && !Global.getSettings().getBoolean("playtestingMode")) {
149 dur *= 0.01f;
150 }
151 wormhole.getMemoryWithoutUpdate().set(JumpPointInteractionDialogPluginImpl.UNSTABLE_KEY, true, dur);
152 }
153
154 loc.addEntity(wormhole);
155 if (stableLocation.getOrbit() != null) {
156 wormhole.setOrbit(stableLocation.getOrbit().makeCopy());
157 }
158 wormhole.setLocation(stableLocation.getLocation().x, stableLocation.getLocation().y);
159 loc.removeEntity(stableLocation);
160 //Misc.fadeAndExpire(stableLocation);
161
162 // DO NOT set this, it causes the original stable location to be shown on the map
163 // when the player is not in-system
164 //wormhole.getMemoryWithoutUpdate().set("$originalStableLocation", stableLocation);
165
166 deployed.add(new WormholeData(item, wormhole, itemData));
167
168 if (dialog != null && dialog.getTextPanel() != null) {
169 Global.getSector().getPlayerFleet().getCargo().removeItems(CargoItemType.SPECIAL, item, 1);
170 AddRemoveCommodity.addItemLossText(item, 1, dialog.getTextPanel());
171 }
172
174
175 return wormhole;
176 }
177
178 public void removeWormhole(JumpPointAPI jp, InteractionDialogAPI dialog) {
179 for (WormholeData data : deployed) {
180 if (data.jumpPoint == jp) {
181 LocationAPI loc = jp.getContainingLocation();
182 SectorEntityToken stableLocation = loc.addCustomEntity(null,
183 null,
184 Entities.STABLE_LOCATION, // type of object, defined in custom_entities.json
185 Factions.NEUTRAL); // faction
186 if (jp.getOrbit() != null) {
187 stableLocation.setOrbit(jp.getOrbit().makeCopy());
188 }
189 //Misc.fadeAndExpire(jp);
190 loc.removeEntity(jp);
191
192 if (dialog != null && dialog.getTextPanel() != null) {
193 Global.getSector().getPlayerFleet().getCargo().addItems(CargoItemType.SPECIAL, data.item, 1);
194 AddRemoveCommodity.addItemGainText(data.item, 1, dialog.getTextPanel());
195 }
196
197 deployed.remove(data);
198
200 break;
201 }
202 }
203 }
204
205 public boolean isDeployed(String id) {
206 if (id == null) return false;
207 for (WormholeData data : deployed) {
208 if (id.equals(data.itemData.id)) return true;
209 }
210 return false;
211 }
212 public WormholeData getDeployed(String id) {
213 if (id == null) return null;
214 for (WormholeData data : deployed) {
215 if (id.equals(data.itemData.id)) return data;
216 }
217 return null;
218 }
219
220 public static SpecialItemData createWormholeAnchor(String id, String name) {
221 return createWormholeAnchor("standard", id, name);
222 }
223 public static SpecialItemData createWormholeAnchor(String category, String id, String name) {
224 WormholeItemData itemData = new WormholeItemData(category, id, name);
225 SpecialItemData item = new SpecialItemData(Items.WORMHOLE_ANCHOR, itemData.toJsonStr());
226 return item;
227 }
228
229}
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
static SettingsAPI getSettings()
Definition Global.java:51
static FactoryAPI getFactory()
Definition Global.java:35
static SectorAPI getSector()
Definition Global.java:59
void removeWormhole(JumpPointAPI jp, InteractionDialogAPI dialog)
JumpPointAPI addWormhole(SpecialItemData item, SectorEntityToken stableLocation, InteractionDialogAPI dialog)
static boolean willWormholeBecomeUnstable(SectorEntityToken stableLocation)
static SpecialItemData createWormholeAnchor(String category, String id, String name)
static SpecialItemData createWormholeAnchor(String id, String name)
JumpPointAPI createJumpPoint(String id, String name)
boolean getBoolean(String key)