001package com.fs.starfarer.api.impl.campaign.rulecmd.salvage.special;
002
003import com.fs.starfarer.api.Global;
004import com.fs.starfarer.api.campaign.InteractionDialogAPI;
005import com.fs.starfarer.api.campaign.LocationAPI;
006import com.fs.starfarer.api.campaign.PlanetAPI;
007import com.fs.starfarer.api.campaign.SectorEntityToken;
008import com.fs.starfarer.api.campaign.StarSystemAPI;
009import com.fs.starfarer.api.impl.campaign.ids.StarTypes;
010import com.fs.starfarer.api.impl.campaign.intel.misc.BreadcrumbIntelV2;
011import com.fs.starfarer.api.impl.campaign.procgen.Constellation;
012import com.fs.starfarer.api.impl.campaign.rulecmd.salvage.SalvageSpecialInteraction.SalvageSpecialData;
013import com.fs.starfarer.api.impl.campaign.rulecmd.salvage.SalvageSpecialInteraction.SalvageSpecialPlugin;
014
015public class DomainSurveyDerelictSpecial extends BaseSalvageSpecial {
016
017        public static enum SpecialType {
018                SCRAMBLED,
019                LOCATION_SURVEY_SHIP,
020                LOCATION_MOTHERSHIP,
021                
022//              PLANET_INTERESTING_PROPERTY,
023//              PLANET_SURVEY_DATA,
024//              SYSTEM_PRELIMINARY_SURVEY,
025                
026                ACTIVATE_PROBE,
027                ACTIVATE_SURVEY_SHIP,
028        }
029        
030        
031        public static class DomainSurveyDerelictSpecialData implements SalvageSpecialData {
032                public SpecialType type = SpecialType.SCRAMBLED;
033                public String entityId = null;
034                public String secondaryId = null;
035                public DomainSurveyDerelictSpecialData(SpecialType type) {
036                        this.type = type;
037                }
038                public SalvageSpecialPlugin createSpecialPlugin() {
039                        return new DomainSurveyDerelictSpecial();
040                }
041        }
042        
043        private DomainSurveyDerelictSpecialData data;
044        
045        public DomainSurveyDerelictSpecial() {
046        }
047
048        @Override
049        public void init(InteractionDialogAPI dialog, Object specialData) {
050                super.init(dialog, specialData);
051                
052                data = (DomainSurveyDerelictSpecialData) specialData;
053                
054                if (data.entityId != null) {
055                        SectorEntityToken entity = Global.getSector().getEntityById(data.entityId);
056                        if (entity == null) {// || 
057//                                      (entity != null && entity instanceof CustomCampaignEntityAPI && 
058//                                                      !((CustomCampaignEntityAPI)entity).isDiscoverable())) {
059                                data.entityId = null;
060                                data.type = SpecialType.SCRAMBLED;
061                        }
062                }
063                
064                switch (data.type) {
065                case SCRAMBLED:
066                        initNothing();
067                        break;
068//              case PLANET_INTERESTING_PROPERTY:
069//                      initInterestingProperty();
070//                      break;
071//              case PLANET_SURVEY_DATA:
072//                      initPlanetSurveyData();
073//                      break;
074//              case SYSTEM_PRELIMINARY_SURVEY:
075////                    PlanetAPI planet = (PlanetAPI) Global.getSector().getEntityById(data.entityId);
076////                    data.entityId = planet.getContainingLocation().getId();
077//                      initPreliminarySystemSurvey();
078//                      break;
079                case LOCATION_SURVEY_SHIP:
080                        initSurveyParentEntity();
081                        break;
082                case LOCATION_MOTHERSHIP:
083                        initSurveyParentEntity();
084                        break;
085//              case CONSTELLATION_PRELIMINARY_SURVEY:
086//                      break;
087                }
088                
089//              for (PlanetAPI curr : entity.getContainingLocation().getPlanets()) {
090//                      if (!curr.isStar()) {
091//                              planet = curr;
092//                              break;
093//                      }
094//              }
095//              
096//              dialog.getTextPanel().addParagraph("Survey data for " + planet.getName() + " gained.");
097//              //Misc.setFullySurveyed(planet.getMarket());
098//              planet.getMarket().setSurveyLevel(SurveyLevel.PRELIMINARY);
099//              
100//              setDone(true);
101//              dialog.getOptionPanel().clearOptions();
102//              dialog.getOptionPanel().addOption("Continue", "continue");
103        }
104
105        public boolean shouldShowAgain() {
106                return data != null && (
107                        data.type == SpecialType.ACTIVATE_PROBE || 
108                        data.type == SpecialType.ACTIVATE_SURVEY_SHIP
109                );
110        }
111        
112
113        private void initSurveyParentEntity() {
114                if (data.entityId == null) {
115                        initNothing();
116                        return;
117                }
118                
119                SectorEntityToken parent = Global.getSector().getEntityById(data.entityId);
120                if (parent == null || !parent.hasSensorProfile()) {
121                        initNothing();
122                        return;
123                }
124                
125                String text1 = "The $shortName's memory banks are partially accessible, and ";
126                String text1ForIntel = "While exploring $aOrAn $nameInText, your crews found " +
127                                                                "partially accessible memory banks that ";
128                //entity.getCustomEntitySpec().getAOrAn() + " " + 
129                String name = parent.getCustomEntitySpec().getNameInText();
130                String nameForTitle = parent.getCustomEntitySpec().getDefaultName();
131                //String loc = BreadcrumbSpecial.getLocatedString(parent);
132                
133                //String subject = getString("Derelict " + nameForTitle + " Location");
134                String subject = getString("Location: " + nameForTitle);
135                
136                text1 += "contain information pointing to the location of a " + name + ". ";
137                text1ForIntel += "contain information pointing to the location of a " + name + ". ";
138                //text1 += "It was last seen by this $shortName " + orbiting + loc + ".";
139                
140                String located = BreadcrumbSpecial.getLocatedString(parent, true);
141                //located = located.replaceFirst("located ", "");
142                text1 += "It was last seen by this $shortName " + located + ".";
143                text1ForIntel += "It was last seen by this $shortName " + located + ".";
144                
145                text1 = getString(text1);
146                
147                addText(text1);
148                
149                BreadcrumbIntelV2 intel = new BreadcrumbIntelV2(parent);
150                intel.setTitle(getString(subject));
151                intel.setText(getString(text1ForIntel));
152                //intel.setIcon(Global.getSettings().getSpriteName("intel", "leading_to_larger_domain_derelict"));
153                intel.setIconId("leading_to_larger_domain_derelict");
154                Global.getSector().getIntelManager().addIntel(intel, false, text);
155                
156//              CommMessageAPI message = FleetLog.beginEntry(subject, parent);
157//              message.getSection1().addPara(getString(text1));
158//              FleetLog.addToLog(message, text);
159                
160                //unsetData();
161                setDone(true);
162        }
163
164        public void initNothing() {
165                addText("The $shortName's memory banks have been scrubbed clean by hard radiation, and the systems are largely inert and non-functional.");
166                
167                //unsetData();
168                setDone(true);
169        }
170        
171        
172//      protected void initInterestingProperty() {
173//              if (data.entityId == null || data.secondaryId == null) {
174//                      initNothing();
175//                      return;
176//              }
177//              
178//              PlanetAPI planet = (PlanetAPI) Global.getSector().getEntityById(data.entityId);
179//              if (planet.getMarket() != null && planet.getMarket().getSurveyLevel() == SurveyLevel.FULL) {
180//                      initNothing();
181//                      return;
182//              }
183//              
184//              String text1 = getString("The $shortName's memory banks are partially accessible, and ");
185//              String text1ForIntel = "While exploring $aOrAn $nameInText, your crews found " +
186//                                                              "partially accessible memory banks that ";
187//              
188//              String desc = "";
189//              String world = planet.getSpec().getAOrAn() + " " + planet.getTypeNameWithWorld().toLowerCase();
190//              //String loc = getLocationDescription(planet);
191//              
192//              String loc = BreadcrumbSpecial.getLocatedString(planet);
193//              loc = loc.replaceFirst("located ", "");
194//              
195//              String subject = "";
196//              MarketConditionSpecAPI spec = Global.getSettings().getMarketConditionSpec(data.secondaryId);
197//              if (spec.getId().equals(Conditions.HABITABLE)) {
198//                      subject = "Habitable planet location";
199//                      desc = "point to the existence of " + world + " with a low hazard rating " + loc;
200//                      
201//              } else {
202//                      subject = Misc.ucFirst(spec.getName().toLowerCase()) + " location";
203//                      desc = "contain information about " + spec.getName().toLowerCase() + " on " + world + " " + loc;
204//              }
205//              
206//              desc += ".";
207//              
208//              addText(text1 + desc);
209//              
210//              BreadcrumbIntel intel = new BreadcrumbIntel(entity, planet);
211//              intel.setTitle(getString(subject));
212//              intel.setText(getString(text1ForIntel));
213//              Global.getSector().getIntelManager().addIntel(intel, false, text);
214//              
215////            CommMessageAPI message = FleetLog.beginEntry(subject, planet);
216////            message.getSection1().addPara(getString(text1 + desc));
217////            FleetLog.addToLog(message, text);
218//              
219//              //unsetData();
220//              setDone(true);
221//      }
222        
223        
224//      protected void initPlanetSurveyData() {
225//              if (data.entityId == null) {
226//                      initNothing();
227//                      return;
228//              }
229//              
230//              PlanetAPI planet = (PlanetAPI) Global.getSector().getEntityById(data.entityId);
231//              if (planet.getMarket() != null && planet.getMarket().getSurveyLevel() == SurveyLevel.FULL) {
232//                      initNothing();
233//                      return;
234//              }
235//              
236//
237//              String name = planet.getName();
238//              String world = planet.getSpec().getAOrAn() + " " + planet.getTypeNameWithWorld().toLowerCase();
239//              //String loc = getLocationName(planet);
240//              String loc = BreadcrumbSpecial.getLocatedString(planet);
241//              loc = loc.replaceFirst("located ", "");
242//              
243//              String text1 = getString("The $shortName's memory banks are partially accessible, " +
244//                              "and contain full survey data for " + name + ", " + world + " located " + loc + ".");
245//              
246//              //planet.getMarket().setSurveyLevel(SurveyLevel.PRELIMINARY);
247//              
248//              String subject = "Full survey data for " + name;
249//              
250//              addText(text1);
251//              Misc.setFullySurveyed(planet.getMarket(), null, false);
252//              Misc.addSurveyDataFor(planet, text);
253////            text.setFontSmallInsignia();
254////            text.addParagraph("Acquired full survey data for " + name + ", " + planet.getTypeNameWithWorld().toLowerCase(),
255////                                                            planet.getSpec().getIconColor());
256////            text.setFontInsignia();
257//              
258//              SurveyPlugin plugin = (SurveyPlugin) Global.getSettings().getPlugin("surveyPlugin");
259//              plugin.init(Global.getSector().getPlayerFleet(), planet);
260//              long xp = plugin.getXP();
261//              if (xp > 0) {
262//                      Global.getSector().getPlayerPerson().getStats().addXP(xp, text);
263//              }
264//              
265//              CommMessageAPI message = FleetLog.beginEntry(subject, planet);
266//              message.getSection1().addPara(getString(text1));
267//              FleetLog.addToLog(message, text);
268//              
269//              //unsetData();
270//              setDone(true);
271//      }
272        
273        
274//      protected void initPreliminarySystemSurvey() {
275//              if (data.entityId == null) {
276//                      initNothing();
277//                      return;
278//              }
279//              
280//              StarSystemAPI system = Global.getSector().getStarSystem(data.entityId);
281//              if (system == null) {
282//                      initNothing();
283//                      return;
284//              }
285//              
286//              String name = system.getNameWithLowercaseType();
287//              String text1 = getString("The $shortName's memory banks are partially accessible, " +
288//                              "and contain complete preliminary survey data for the " + name + ".");
289//              
290//              String subject = "Acquired complete preliminary survey data for the " + name;
291//              
292//              addText(text1);
293//              
294//              String data = "";
295//              Highlights h = new Highlights();
296//              for (PlanetAPI planet : system.getPlanets()) {
297//                      if (planet.isStar()) continue;
298//                      if (planet.getMarket() == null) continue;
299//                      if (!planet.getMarket().isPlanetConditionMarketOnly()) continue;
300//                      if (planet.getMarket().getSurveyLevel().ordinal() > SurveyLevel.PRELIMINARY.ordinal()) continue;
301//                      
302//                      String curr = planet.getName() + ", " + planet.getTypeNameWithWorld().toLowerCase();
303//                      data += "    " + curr + "\n";
304//                      h.append(curr, planet.getSpec().getIconColor());
305//                      
306////                    text.addParagraph("    " + planet.getName() + ", " + planet.getTypeNameWithWorld().toLowerCase(),
307////                                                            planet.getSpec().getIconColor());
308//                      planet.getMarket().setSurveyLevel(SurveyLevel.PRELIMINARY);
309//                      
310//                      //Misc.setPreliminarySurveyed(planet.getMarket(), text, true);
311//              }
312//              //data = "";
313//              if (!data.isEmpty()) {
314//                      text.setFontSmallInsignia();
315//                      text.addParagraph("Preliminary survey data for:", Misc.getTooltipTitleAndLightHighlightColor());
316//                      //data = data.substring(0, data.length() - 2);
317//                      data = "    " + data.trim();
318//                      text.addParagraph(data);
319//                      text.setHighlightsInLastPara(h);
320//                      text.setFontInsignia();
321//                      
322//                      CommMessageAPI message = FleetLog.beginEntry(subject, system.getCenter());
323//                      message.getSection1().addPara(getString(text1));
324//                      FleetLog.addToLog(message, text);
325//              } else {
326//                      text.addParagraph("However, you've already acquired this data through other means.");
327//              }
328//              
329//              //unsetData();
330//              setDone(true);
331//      }
332
333        
334//      protected void unsetData() {
335//              BaseCommandPlugin.getEntityMemory(memoryMap).unset(MemFlags.SALVAGE_SPECIAL_DATA);
336//      }
337        
338        public static String getLocationName(SectorEntityToken entity) {
339                LocationAPI loc = entity.getContainingLocation();
340                if (loc == null) {
341                        return "in an unknown location nearby"; 
342                }
343                if (loc.isHyperspace()) {
344                        return "in hyperspace nearby";
345                }
346                StarSystemAPI system = (StarSystemAPI) loc;
347                
348                if (system == Global.getSector().getCurrentLocation()) {
349                        if (system.isNebula()) {
350                                return "inside this nebula";
351                        }
352                        return "in this system";
353                }
354                
355                return "in the " + system.getNameWithLowercaseType();
356        }
357        
358        public static String getLocationDescription(SectorEntityToken entity) {
359                LocationAPI loc = entity.getContainingLocation();
360                if (loc == null) {
361                        return "in an unknown location nearby"; 
362                }
363                if (loc.isHyperspace()) {
364                        return "in hyperspace nearby";
365                }
366                StarSystemAPI system = (StarSystemAPI) loc;
367                
368                if (system == Global.getSector().getCurrentLocation()) {
369                        if (system.isNebula()) {
370                                return "inside this nebula";
371                        }
372                        return "in this system";
373                }
374
375                if (entity.getConstellation() != null && entity.getConstellation() != Global.getSector().getCurrentLocation().getConstellation()) {
376                        Constellation c = entity.getConstellation();
377                        String cText = "in the " + c.getNameWithLowercaseType();
378                        if (c.getSystems().size() == 1) {
379                                return "orbiting " + getStarDescription(system.getStar()) + " nearby";
380                        }
381                        
382                        if (system.isNebula()) {
383                                return "inside a nebula " + cText;
384                        }
385                        
386                        if (system.getTertiary() != null) {
387                                return "in a trinary star system " + cText;
388                        }
389                        
390                        if (system.getSecondary() != null) {
391                                return "in a binary star system " + cText;
392                        }
393                        
394                        //if (system.getType() == StarSystemType.SINGLE) {
395                                return "orbiting " + getStarDescription(system.getStar()) + " " + cText;
396                }
397                
398                if (system.isNebula()) {
399                        return "inside a nearby nebula";
400                }
401                
402                if (system.getTertiary() != null) {
403                        return "in a nearby trinary star system";
404                }
405                
406                if (system.getSecondary() != null) {
407                        return "in a nearby binary star system";
408                }
409                
410                //if (system.getType() == StarSystemType.SINGLE) {
411                        return "orbiting " + getStarDescription(system.getStar()) + " nearby";
412                //}
413        }
414        
415        
416        public static String getStarDescription(PlanetAPI star) {
417                String type = star.getTypeId();
418                
419                if (type.equals(StarTypes.BLACK_HOLE)) return "a black hole";
420                if (type.equals(StarTypes.NEUTRON_STAR)) return "a neutron star";
421                
422                if (type.equals(StarTypes.ORANGE) ||
423                        type.equals(StarTypes.ORANGE_GIANT)) {
424                        return "an orange star";
425                }
426                
427                if (type.equals(StarTypes.RED_DWARF) ||
428                        type.equals(StarTypes.RED_SUPERGIANT) ||
429                                type.equals(StarTypes.RED_GIANT)) {
430                        return "a red star";
431                }
432                
433                if (type.equals(StarTypes.BLUE_GIANT) ||
434                                type.equals(StarTypes.BLUE_SUPERGIANT)) {
435                        return "a blue star";
436                }
437                
438                if (type.equals(StarTypes.BROWN_DWARF) ||
439                                type.equals(StarTypes.WHITE_DWARF)) {
440                        return "a dim star";
441                }
442                
443                if (type.equals(StarTypes.YELLOW)) {
444                        return "a yellow star";
445                }
446                
447                return "a star of unknown type";
448        }
449        
450        
451        @Override
452        public void optionSelected(String optionText, Object optionData) {
453                super.optionSelected(optionText, optionData);
454                
455                if ("continue".equals(optionData)) {
456                        setDone(true);
457                }
458        }
459
460        @Override
461        public boolean endWithContinue() {
462                return super.endWithContinue();
463        }
464        
465        
466}