Starsector API
Loading...
Searching...
No Matches
ProcGenTestPluginImpl.java
Go to the documentation of this file.
1package com.fs.starfarer.api.impl.campaign.procgen;
2
3import java.awt.Color;
4import java.util.ArrayList;
5import java.util.Collections;
6import java.util.Comparator;
7import java.util.HashMap;
8import java.util.HashSet;
9import java.util.List;
10import java.util.Map;
11import java.util.Set;
12
13import com.fs.starfarer.api.Global;
14import com.fs.starfarer.api.campaign.CampaignFleetAPI;
15import com.fs.starfarer.api.campaign.InteractionDialogAPI;
16import com.fs.starfarer.api.campaign.InteractionDialogPlugin;
17import com.fs.starfarer.api.campaign.OptionPanelAPI;
18import com.fs.starfarer.api.campaign.PlanetAPI;
19import com.fs.starfarer.api.campaign.StarSystemAPI;
20import com.fs.starfarer.api.campaign.TextPanelAPI;
21import com.fs.starfarer.api.campaign.VisualPanelAPI;
22import com.fs.starfarer.api.campaign.rules.MemoryAPI;
23import com.fs.starfarer.api.combat.EngagementResultAPI;
24import com.fs.starfarer.api.impl.campaign.ids.Conditions;
25import com.fs.starfarer.api.impl.campaign.ids.Planets;
26import com.fs.starfarer.api.impl.campaign.procgen.StarSystemGenerator.CustomConstellationParams;
27import com.fs.starfarer.api.impl.campaign.procgen.StarSystemGenerator.StarSystemType;
28import com.fs.starfarer.api.impl.campaign.procgen.themes.RemnantThemeGenerator;
29import com.fs.starfarer.api.impl.campaign.procgen.themes.ThemeGenContext;
30import com.fs.starfarer.api.util.Misc;
31
32public class ProcGenTestPluginImpl implements InteractionDialogPlugin {
33
34 protected static enum OptionId {
35 INIT,
36 GEN_YOUNG,
37 GEN_AVERAGE,
38 GEN_OLD,
39 GEN_CUSTOM,
40 GEN_SALVAGE,
41 PRINT_STATS,
42 LEAVE,
43 }
44
45 protected InteractionDialogAPI dialog;
46 protected TextPanelAPI textPanel;
47 protected OptionPanelAPI options;
48 protected VisualPanelAPI visual;
49
50 protected CampaignFleetAPI playerFleet;
51 protected PlanetAPI planet;
52
53 protected static final Color HIGHLIGHT_COLOR = Global.getSettings().getColor("buttonShortcut");
54
55 public void init(InteractionDialogAPI dialog) {
56 this.dialog = dialog;
57
58 textPanel = dialog.getTextPanel();
59 options = dialog.getOptionPanel();
60 visual = dialog.getVisualPanel();
61
62 playerFleet = Global.getSector().getPlayerFleet();
63 planet = (PlanetAPI) dialog.getInteractionTarget();
64
65 visual.setVisualFade(0.25f, 0.25f);
66
67 //visual.showImageVisual(planet.getCustomInteractionDialogImageVisual());
68
69 dialog.setOptionOnEscape("Leave", OptionId.LEAVE);
70 optionSelected(null, OptionId.INIT);
71 }
72
73 public Map<String, MemoryAPI> getMemoryMap() {
74 return null;
75 }
76
77 public void backFromEngagement(EngagementResultAPI result) {
78 // no combat here, so this won't get called
79 }
80
81 public void optionSelected(String text, Object optionData) {
82 if (optionData == null) return;
83
84 OptionId option = (OptionId) optionData;
85
86 if (text != null) {
87 //textPanel.addParagraph(text, Global.getSettings().getColor("buttonText"));
88 dialog.addOptionSelectedText(option);
89 }
90
91 // make >1 for faster stats-gathering
92 int genCount = 1;
93
94 Constellation constellation = null;
95 switch (option) {
96 case INIT:
98 break;
99 case GEN_YOUNG:
100 for (int i = 0; i < genCount; i++) {
101 constellation = new StarSystemGenerator(new CustomConstellationParams(StarAge.YOUNG)).generate();
102 }
103 addText("Generated star system.");
104 optionSelected(null, OptionId.LEAVE);
105 break;
106 case GEN_AVERAGE:
107 for (int i = 0; i < genCount; i++) {
108 constellation = new StarSystemGenerator(new CustomConstellationParams(StarAge.AVERAGE)).generate();
109 }
110 addText("Generated star system.");
111 optionSelected(null, OptionId.LEAVE);
112 break;
113 case GEN_OLD:
114 for (int i = 0; i < genCount; i++) {
115 constellation = new StarSystemGenerator(new CustomConstellationParams(StarAge.OLD)).generate();
116 }
117 addText("Generated star system.");
118 optionSelected(null, OptionId.LEAVE);
119 break;
120 case GEN_SALVAGE:
121 ThemeGenContext context = new ThemeGenContext();
122 Set<Constellation> c = new HashSet<Constellation>();
123 for (StarSystemAPI system : Global.getSector().getStarSystems()) {
124 if (system.getConstellation() == null) continue;
125 for (StarSystemAPI curr : system.getConstellation().getSystems()) {
126 if (curr.isProcgen()) {
127 c.add(system.getConstellation());
128 break;
129 }
130 }
131 }
132 context.constellations = new ArrayList<Constellation>(c);
133 //SectorThemeGenerator.generate(context);
134 new RemnantThemeGenerator().generateForSector(context, 1f);
135 break;
136 case GEN_CUSTOM:
137 CustomConstellationParams params = new CustomConstellationParams(StarAge.YOUNG);
138
139 params.numStars = 7;
140 params.forceNebula = true;
141
142 params.systemTypes.add(StarSystemType.TRINARY_2CLOSE);
143 params.systemTypes.add(StarSystemType.SINGLE);
144 params.systemTypes.add(StarSystemType.TRINARY_1CLOSE_1FAR);
145 params.systemTypes.add(StarSystemType.NEBULA);
146 params.systemTypes.add(StarSystemType.SINGLE);
147 params.systemTypes.add(StarSystemType.BINARY_CLOSE);
148 params.systemTypes.add(StarSystemType.BINARY_CLOSE);
149 //params.systemTypes.add(StarSystemType.TRINARY_2FAR);
150
151 params.starTypes.add("black_hole");
152 params.starTypes.add("star_blue_giant");
153 params.starTypes.add("star_orange");
154
155
156 params.starTypes.add("star_neutron");
157 params.starTypes.add("star_neutron");
158 params.starTypes.add("star_neutron");
159 params.starTypes.add("star_neutron");
160 params.starTypes.add("nebula_center_average");
161 params.starTypes.add("black_hole");
162 params.starTypes.add("black_hole");
163 params.starTypes.add("black_hole");
164 params.starTypes.add("star_blue_giant");
165 //params.starTypes.add("black_hole");
166
167 constellation = new StarSystemGenerator(params).generate();
168 addText("Generated star system.");
169 optionSelected(null, OptionId.LEAVE);
170 break;
171 case PRINT_STATS:
172 printStats();
173 break;
174 case LEAVE:
175 //Global.getSector().setPaused(false);
176 dialog.dismiss();
177 break;
178 }
179
180// if (constellation != null) {
181// DerelictThemeGenerator gen = new DerelictThemeGenerator();
182// for (StarSystemAPI system : constellation.systems) {
183// gen.generateForSystem(system, null);
184// }
185// }
186 }
187
188 protected void printStats() {
189
190 final Map<String, Integer> counts = new HashMap<String, Integer>();
191 final Map<String, Integer> hab = new HashMap<String, Integer>();
192
193 int totalPlanets = 0;
194 int totalSystems = 0;
195 int totalHab = 0;
196
197 int totalPlanetsInSystemsWithTerran = 0;
198 int maxPlanetsInSystemsWithTerran = 0;
199
200 for (StarSystemAPI system : Global.getSector().getStarSystems()) {
201 if (!system.isProcgen()) continue;
202
203 String starType = null;
204 if (system.getStar() != null) {
205 starType = system.getStar().getSpec().getName();
206 }
207
208 totalSystems++;
209
210 int planets = 0;
211 Set<String> seen = new HashSet<String>();
212 for (PlanetAPI planet : system.getPlanets()) {
213 if (planet.getMarket() == null) continue;
214 if (!planet.getMarket().isPlanetConditionMarketOnly()) continue;
215
216 //String type = planet.getSpec().getPlanetType();
217 String type = planet.getSpec().getName();
218
219 seen.add(planet.getSpec().getPlanetType());
220 planets++;
221
222 Integer count = 0;
223 if (counts.containsKey(type)) {
224 count = counts.get(type);
225 }
226 count++;
227 counts.put(type, count);
228
229 if (planet.getMarket().hasCondition(Conditions.HABITABLE)) {
230 totalHab++;
231
232 if (starType != null) {
233 count = 0;
234 if (hab.containsKey(starType)) {
235 count = hab.get(starType);
236 }
237 count++;
238 hab.put(starType, count);
239
240 }
241 }
242
243 totalPlanets++;
244 }
245
246 if (seen.contains(Planets.PLANET_TERRAN)) {
247 if (planets > maxPlanetsInSystemsWithTerran) {
248 maxPlanetsInSystemsWithTerran = planets;
249 }
250 totalPlanetsInSystemsWithTerran += planets;
251 }
252 }
253
254 List<String> list = new ArrayList<String>(counts.keySet());
255 Collections.sort(list, new Comparator<String>() {
256 public int compare(String o1, String o2) {
257 return counts.get(o2).compareTo(counts.get(o1));
258 }
259 });
260 List<String> habList = new ArrayList<String>(hab.keySet());
261 Collections.sort(habList, new Comparator<String>() {
262 public int compare(String o1, String o2) {
263 return hab.get(o2).compareTo(hab.get(o1));
264 }
265 });
266
267 textPanel.addParagraph("");
268 print(String.format("Star systems: %4d", totalSystems));
269 print(String.format("Planets: %4d", totalPlanets));
270 print(String.format("Habitable %4d", totalHab));
271 print(String.format("Planets in systems with terran worlds: %4d", totalPlanetsInSystemsWithTerran));
272 print(String.format("Max planets in system with terran world: %4d", maxPlanetsInSystemsWithTerran));
273 if (totalPlanets > 0) {
274 print("Planet totals:");
275 for (String type : list) {
276 Integer count = counts.get(type);
277 String value = Misc.getRoundedValueMaxOneAfterDecimal((count * 100f) / totalPlanets) + "%";
278 value += " (" + count + ")";
279 print(String.format(" %-20s%10s", type, value));
280 }
281 print("");
282 }
283
284 if (totalHab > 0) {
285 print("Habitable totals by star:");
286 for (String type : habList) {
287 Integer count = hab.get(type);
288 String value = Misc.getRoundedValueMaxOneAfterDecimal((count * 100f) / totalHab) + "%";
289 value += " (" + count + ")";
290 print(String.format(" %-20s%10s", type, value));
291 }
292 print("");
293 }
294 }
295
296 protected void print(String str) {
297 textPanel.appendToLastParagraph("\n" + str);
298 System.out.println(str);
299 }
300
301 protected void createInitialOptions() {
302 options.clearOptions();
303 options.addOption("Generate young constellation", OptionId.GEN_YOUNG, null);
304 options.addOption("Generate average constellation", OptionId.GEN_AVERAGE, null);
305 options.addOption("Generate old constellation", OptionId.GEN_OLD, null);
306 options.addOption("Generate preset constellation", OptionId.GEN_CUSTOM, null);
307 options.addOption("Generate salvage entities", OptionId.GEN_SALVAGE, null);
308 options.addOption("Print stats", OptionId.PRINT_STATS, null);
309 options.addOption("Leave", OptionId.LEAVE, null);
310 }
311
312
313 protected OptionId lastOptionMousedOver = null;
314 public void optionMousedOver(String optionText, Object optionData) {
315
316 }
317
318 public void advance(float amount) {
319
320 }
321
322 protected void addText(String text) {
323 textPanel.addParagraph(text);
324 }
325
326 protected void appendText(String text) {
327 textPanel.appendToLastParagraph(" " + text);
328 }
329
330 protected String getString(String id) {
331 String str = Global.getSettings().getString("planetInteractionDialog", id);
332
333 String fleetOrShip = "fleet";
334 if (playerFleet.getFleetData().getMembersListCopy().size() == 1) {
335 fleetOrShip = "ship";
336 if (playerFleet.getFleetData().getMembersListCopy().get(0).isFighterWing()) {
337 fleetOrShip = "fighter wing";
338 }
339 }
340 str = str.replaceAll("\\$fleetOrShip", fleetOrShip);
341 str = str.replaceAll("\\$planetName", planet.getName());
342
343 return str;
344 }
345
346
347 public Object getContext() {
348 return null;
349 }
350}
351
352
353
static SettingsAPI getSettings()
Definition Global.java:51
static SectorAPI getSector()
Definition Global.java:59
String getString(String category, String id)