001package com.fs.starfarer.api.impl.campaign.procgen;
002
003import java.awt.Color;
004import java.util.ArrayList;
005import java.util.Collections;
006import java.util.Comparator;
007import java.util.HashMap;
008import java.util.HashSet;
009import java.util.List;
010import java.util.Map;
011import java.util.Set;
012
013import com.fs.starfarer.api.Global;
014import com.fs.starfarer.api.campaign.CampaignFleetAPI;
015import com.fs.starfarer.api.campaign.InteractionDialogAPI;
016import com.fs.starfarer.api.campaign.InteractionDialogPlugin;
017import com.fs.starfarer.api.campaign.OptionPanelAPI;
018import com.fs.starfarer.api.campaign.PlanetAPI;
019import com.fs.starfarer.api.campaign.StarSystemAPI;
020import com.fs.starfarer.api.campaign.TextPanelAPI;
021import com.fs.starfarer.api.campaign.VisualPanelAPI;
022import com.fs.starfarer.api.campaign.rules.MemoryAPI;
023import com.fs.starfarer.api.combat.EngagementResultAPI;
024import com.fs.starfarer.api.impl.campaign.ids.Conditions;
025import com.fs.starfarer.api.impl.campaign.ids.Planets;
026import com.fs.starfarer.api.impl.campaign.procgen.StarSystemGenerator.CustomConstellationParams;
027import com.fs.starfarer.api.impl.campaign.procgen.StarSystemGenerator.StarSystemType;
028import com.fs.starfarer.api.impl.campaign.procgen.themes.RemnantThemeGenerator;
029import com.fs.starfarer.api.impl.campaign.procgen.themes.ThemeGenContext;
030import com.fs.starfarer.api.util.Misc;
031
032public class ProcGenTestPluginImpl implements InteractionDialogPlugin {
033
034        protected static enum OptionId {
035                INIT,
036                GEN_YOUNG,
037                GEN_AVERAGE,
038                GEN_OLD,
039                GEN_CUSTOM,
040                GEN_SALVAGE,
041                PRINT_STATS,
042                LEAVE,
043        }
044        
045        protected InteractionDialogAPI dialog;
046        protected TextPanelAPI textPanel;
047        protected OptionPanelAPI options;
048        protected VisualPanelAPI visual;
049        
050        protected CampaignFleetAPI playerFleet;
051        protected PlanetAPI planet;
052        
053        protected static final Color HIGHLIGHT_COLOR = Global.getSettings().getColor("buttonShortcut");
054        
055        public void init(InteractionDialogAPI dialog) {
056                this.dialog = dialog;
057                
058                textPanel = dialog.getTextPanel();
059                options = dialog.getOptionPanel();
060                visual = dialog.getVisualPanel();
061
062                playerFleet = Global.getSector().getPlayerFleet();
063                planet = (PlanetAPI) dialog.getInteractionTarget();
064                
065                visual.setVisualFade(0.25f, 0.25f);
066                
067                //visual.showImageVisual(planet.getCustomInteractionDialogImageVisual());
068        
069                dialog.setOptionOnEscape("Leave", OptionId.LEAVE);
070                optionSelected(null, OptionId.INIT);
071        }
072        
073        public Map<String, MemoryAPI> getMemoryMap() {
074                return null;
075        }
076        
077        public void backFromEngagement(EngagementResultAPI result) {
078                // no combat here, so this won't get called
079        }
080        
081        public void optionSelected(String text, Object optionData) {
082                if (optionData == null) return;
083                
084                OptionId option = (OptionId) optionData;
085                
086                if (text != null) {
087                        //textPanel.addParagraph(text, Global.getSettings().getColor("buttonText"));
088                        dialog.addOptionSelectedText(option);
089                }
090                
091                // make >1 for faster stats-gathering
092                int genCount = 1;
093                
094                Constellation constellation = null;
095                switch (option) {
096                case INIT:
097                        createInitialOptions();
098                        break;
099                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