001package com.fs.starfarer.api.impl.campaign.rulecmd.salvage.special;
002
003import java.util.List;
004
005import com.fs.starfarer.api.Global;
006import com.fs.starfarer.api.campaign.InteractionDialogAPI;
007import com.fs.starfarer.api.campaign.PlanetAPI;
008import com.fs.starfarer.api.campaign.SectorEntityToken;
009import com.fs.starfarer.api.campaign.StarSystemAPI;
010import com.fs.starfarer.api.campaign.econ.MarketAPI.SurveyLevel;
011import com.fs.starfarer.api.characters.MarketConditionSpecAPI;
012import com.fs.starfarer.api.impl.campaign.ids.Conditions;
013import com.fs.starfarer.api.impl.campaign.ids.Entities;
014import com.fs.starfarer.api.impl.campaign.intel.misc.SurveyDataForPlanetIntel;
015import com.fs.starfarer.api.impl.campaign.procgen.themes.DerelictThemeGenerator;
016import com.fs.starfarer.api.impl.campaign.rulecmd.salvage.SalvageSpecialInteraction.SalvageSpecialData;
017import com.fs.starfarer.api.impl.campaign.rulecmd.salvage.SalvageSpecialInteraction.SalvageSpecialPlugin;
018import com.fs.starfarer.api.plugins.SurveyPlugin;
019import com.fs.starfarer.api.util.Misc;
020
021public class SurveyDataSpecial extends BaseSalvageSpecial {
022
023        public static float MAX_RANGE = 30000f;
024        
025        public static enum SurveyDataSpecialType {
026                SCRAMBLED, // used when the planet is already surveyed or doesn't exist anymore or nothing is found etc
027                PLANET_SURVEY_DATA,
028                
029                // these are not really interesting enough
030                @Deprecated PLANET_INTERESTING_PROPERTY,
031                @Deprecated SYSTEM_PRELIMINARY_SURVEY,
032                @Deprecated AUTO_PICK, // generate one of the above automatically, for a nearby planet or system
033                @Deprecated AUTO_PICK_NOT_SYSTEM, // pick either property or data, but not full system
034        }
035        
036        
037        public static class SurveyDataSpecialData implements SalvageSpecialData {
038                public SurveyDataSpecialType type = null;
039                public String entityId = null;
040                
041                @Deprecated public String secondaryId = null;
042                
043                public boolean includeRuins = true;
044                public SurveyDataSpecialData(SurveyDataSpecialType type) {
045                        this.type = type;
046                }
047                
048                public SalvageSpecialPlugin createSpecialPlugin() {
049                        return new SurveyDataSpecial();
050                }
051        }
052        
053        private SurveyDataSpecialData data;
054        
055        public SurveyDataSpecial() {
056        }
057
058        @Override
059        public void init(InteractionDialogAPI dialog, Object specialData) {
060                super.init(dialog, specialData);
061                
062                data = (SurveyDataSpecialData) specialData;
063                
064                //random = new Random();
065                
066                if (data.type == SurveyDataSpecialType.PLANET_SURVEY_DATA && data.entityId == null) {
067                        List<StarSystemAPI> systems = Misc.getSystemsInRange(entity, null, true, MAX_RANGE);
068                        PlanetAPI planet = DerelictThemeGenerator.findInterestingPlanet(systems, null, false, data.includeRuins, random);
069                        if (planet != null) {
070                                data.entityId = planet.getId();
071                        }
072                }
073                
074                if (data.entityId != null) {
075                        SectorEntityToken entity = Global.getSector().getEntityById(data.entityId);
076                        StarSystemAPI system = Global.getSector().getStarSystem(data.entityId);
077                        if (entity == null && system == null) { 
078                                data.entityId = null;
079                                data.type = SurveyDataSpecialType.SCRAMBLED;
080                        }
081                } else {
082                        data.type = SurveyDataSpecialType.SCRAMBLED;
083                }
084                
085                
086                switch (data.type) {
087                case SCRAMBLED:
088                        initNothing();
089                        break;
090                case PLANET_SURVEY_DATA:
091                        initPlanetSurveyData();
092                        break;
093                default:
094                        initNothing();
095                        break;                  
096                }
097
098        }
099
100        public void initNothing() {
101                addText("The $shortName's memory banks have been scrubbed clean by hard radiation, and the systems are largely inert and non-functional.");
102                setDone(true);
103        }
104        
105
106        protected void initPlanetSurveyData() {
107                if (data.entityId == null) {
108                        initNothing();
109                        return;
110                }
111                
112                PlanetAPI planet = (PlanetAPI) Global.getSector().getEntityById(data.entityId);
113                if (planet.getMarket() != null && planet.getMarket().getSurveyLevel() == SurveyLevel.FULL) {
114                        initNothing();
115                        return;
116                }
117                
118
119                String name = planet.getName();
120                String world = planet.getSpec().getAOrAn() + " " + planet.getTypeNameWithWorld().toLowerCase();
121                //String loc = getLocationName(planet);
122                String loc = BreadcrumbSpecial.getLocatedString(planet, true);
123                loc = loc.replaceFirst("located ", "");
124                
125                String text1 = "The $shortName's memory banks are partially accessible, " +
126                                           "and contain full survey data for " + name + ", " + world + " located " + loc + ".";
127                
128                String text1ForIntel = "While exploring $aOrAn $nameInText, your crews found " +
129                                                           "partially accessible memory banks that contain full survey data for " +
130                                                           name + ", " + world + " located " + loc + ".";
131                
132                boolean debris = Entities.DEBRIS_FIELD_SHARED.equals(entity.getCustomEntityType());
133                if (debris) {
134                        text1 = "Your salvage crews find a functional memory bank in the debris. " +
135                                        "It contains full survey data for " + name + ", " + world + " located " + loc + ".";
136                }
137                
138                String conditionId = DerelictThemeGenerator.getInterestingCondition(planet, data.includeRuins);
139                if (conditionId != null) {
140                        MarketConditionSpecAPI spec = Global.getSettings().getMarketConditionSpec(conditionId);
141                        if (spec != null) {
142                                text1 += " The world is notable for ";
143                                text1ForIntel += " The world is notable for ";
144                                if (conditionId.equals(Conditions.HABITABLE)) {
145                                        text1 += "being habitable.";
146                                        text1ForIntel += "being habitable.";
147                                } else {
148                                        text1 += "having " + spec.getName().toLowerCase() + ".";
149                                        text1ForIntel += "having " + spec.getName().toLowerCase() + ".";
150                                }
151                        }
152                }
153                
154                
155                //planet.getMarket().setSurveyLevel(SurveyLevel.PRELIMINARY);
156                
157                //String subject = "Survey Data: " + name + ", " + planet.getTypeNameWithWorld();
158                
159                addText(text1);
160                Misc.setFullySurveyed(planet.getMarket(), null, false);
161                Misc.addSurveyDataFor(planet, text);
162//              text.setFontSmallInsignia();
163//              text.addParagraph("Acquired full survey data for " + name + ", " + planet.getTypeNameWithWorld().toLowerCase(),
164//                                                              planet.getSpec().getIconColor());
165//              text.setFontInsignia();
166                
167                SurveyPlugin plugin = (SurveyPlugin) Global.getSettings().getNewPluginInstance("surveyPlugin");
168                plugin.init(Global.getSector().getPlayerFleet(), planet);
169                long xp = plugin.getXP();
170                if (xp > 0) {
171                        Global.getSector().getPlayerPerson().getStats().addXP(xp, text);
172                }
173                
174                new SurveyDataForPlanetIntel(planet, getString(text1ForIntel), text);
175
176//              BreadcrumbIntel intel = new BreadcrumbIntel(entity, planet);
177//              intel.setTitle(getString(subject));
178//              intel.setText(getString(text1ForIntel));
179//              intel.setShowSpecificEntity(true);
180//              //intel.setIcon(Global.getSettings().getSpriteName("intel", "found_planet_data"));
181//              intel.setIconId("found_planet_data");
182//              Global.getSector().getIntelManager().addIntel(intel, false, text);
183                
184//              CommMessageAPI message = FleetLog.beginEntry(subject, planet);
185//              message.getSection1().addPara(getString(text1));
186//              FleetLog.addToLog(message, text);
187                
188                //unsetData();
189                setDone(true);
190        }
191        
192        
193        @Override
194        public void optionSelected(String optionText, Object optionData) {
195                super.optionSelected(optionText, optionData);
196        }
197
198        
199        
200}