001package com.fs.starfarer.api.impl.campaign.abilities;
002
003import java.awt.Color;
004
005import com.fs.starfarer.api.Global;
006import com.fs.starfarer.api.campaign.CampaignFleetAPI;
007import com.fs.starfarer.api.campaign.PlanetAPI;
008import com.fs.starfarer.api.campaign.SectorEntityToken.VisibilityLevel;
009import com.fs.starfarer.api.campaign.StarSystemAPI;
010import com.fs.starfarer.api.campaign.econ.MarketAPI;
011import com.fs.starfarer.api.campaign.econ.MarketAPI.SurveyLevel;
012import com.fs.starfarer.api.impl.campaign.ids.Pings;
013import com.fs.starfarer.api.impl.campaign.intel.misc.RemoteSurveyDataForPlanetIntel;
014import com.fs.starfarer.api.plugins.SurveyPlugin;
015import com.fs.starfarer.api.ui.LabelAPI;
016import com.fs.starfarer.api.ui.TooltipMakerAPI;
017import com.fs.starfarer.api.util.Misc;
018
019public class RemoteSurveyAbility extends BaseDurationAbility {
020
021        public static final String ALREADY_DID_IN_SYSTEM = "$core_didRemoteSurveyInSystem";
022        
023        public static final float SURVEY_RANGE = 10000f;
024        public static final float DETECTABILITY_RANGE_BONUS = 5000f;
025        public static final float ACCELERATION_MULT = 4f;
026        
027        
028        protected boolean performed = false;
029        
030        @Override
031        protected void activateImpl() {
032//              CampaignFleetAPI fleet = getFleet();
033//              if (fleet == null) return;
034                
035//              PlanetAPI target = getTargetPlanet();
036//              if (target != null) {
037//                      GenericProbeParams params = new GenericProbeParams();
038//                      params.travelInDir(fleet.getFacing(), 2f);
039//                      params.travelTo(target);
040//                      params.assumeOrbit(target, 200f, 10f);
041//                      params.emitPing(Pings.REMOTE_SURVEY);
042//                      params.wait(3f);
043//                      params.performAction(new Script() {
044//                              @Override
045//                              public void run() {
046//                                      if (target.getMarket() != null && 
047//                                                      target.getMarket().getSurveyLevel() != SurveyLevel.FULL) {
048//                                              Misc.setFullySurveyed(target.getMarket(), null, false);
049//                                              String text = "Remote survey telemery received.";
050//                                              new SurveyDataForPlanetIntel(target, text, null);
051//                                      }
052//                              }
053//                      });
054//                      CustomCampaignEntityAPI entity = fleet.getContainingLocation().addCustomEntity(null,
055//                                      "Remote Survey Probe", Entities.GENERIC_PROBE_ACTIVE, Factions.PLAYER, params);
056//                      entity.setLocation(fleet.getLocation().x, fleet.getLocation().y);
057//                      entity.setFacing(fleet.getFacing());
058//                      
059//                      GenericProbeEntityPlugin plugin = (GenericProbeEntityPlugin) entity.getCustomPlugin();
060//                      plugin.getMovement().setLocation(entity.getLocation());
061//                      plugin.getMovement().setFacing(entity.getFacing());
062//                      Vector2f vel = Misc.getUnitVectorAtDegreeAngle(entity.getFacing());
063////                    vel.scale(100f);
064////                    entity.getVelocity().set(vel);
065////                    plugin.getMovement().setVelocity(vel);
066//                      
067//                      Misc.fadeIn(entity, 1f);
068//              }
069                
070                if (entity.isInCurrentLocation()) {
071                        VisibilityLevel level = entity.getVisibilityLevelToPlayerFleet();
072                        if (level != VisibilityLevel.NONE) {
073                                Global.getSector().addPing(entity, Pings.REMOTE_SURVEY);
074                        }
075                        
076                        performed = false;
077                }
078        }
079        
080
081        @Override
082        protected void applyEffect(float amount, float level) {
083                CampaignFleetAPI fleet = getFleet();
084                if (fleet == null) return;
085                
086                //float b = fleet.getStats().getDynamic().getValue(Stats.SENSOR_BURST_BURN_PENALTY_MULT);
087                //float b = 1f;
088                //fleet.getStats().getFleetwideMaxBurnMod().modifyMult(getModId(), 1f + (0f - 1f * level) * b, "Remote survey");
089                fleet.getStats().getFleetwideMaxBurnMod().modifyMult(getModId(), 0f, "Remote survey");
090                fleet.getStats().getDetectedRangeMod().modifyFlat(getModId(), DETECTABILITY_RANGE_BONUS * level, "Remote survey");
091                fleet.getStats().getAccelerationMult().modifyMult(getModId(), 1f + (ACCELERATION_MULT - 1f) * level);
092                
093                if (!performed && level >= 1f) {
094                        // do the actual survey stuff
095                        PlanetAPI planet = findBestPlanet();
096                        if (planet != null && planet.getMarket() != null) {
097                                MarketAPI market = planet.getMarket();
098                                market.setSurveyLevel(SurveyLevel.PRELIMINARY);
099                                //Misc.setPreliminarySurveyed(market, null, true);
100                                
101                                new RemoteSurveyDataForPlanetIntel(planet);
102                                
103                                if (planet.getStarSystem() != null) {
104                                        planet.getStarSystem().getMemoryWithoutUpdate().set(ALREADY_DID_IN_SYSTEM, true);
105                                }
106                                
107                        }
108                        performed = true;
109                }
110        }
111        
112        
113        public boolean isUsable() {
114                if (!super.isUsable()) return false;
115                if (getFleet() == null) return false;
116                
117                CampaignFleetAPI fleet = getFleet();
118                if (fleet.isInHyperspace() || fleet.isInHyperspaceTransition()) return false;
119                
120                if (findBestPlanet() == null) return false;
121                
122                return true;
123        }
124        
125        public PlanetAPI findBestPlanet() {
126                
127                CampaignFleetAPI fleet = getFleet();
128                if (fleet == null || fleet.isInHyperspace() || fleet.getStarSystem() == null) return null;
129                
130                StarSystemAPI system = fleet.getStarSystem();
131                if (system.getMemoryWithoutUpdate().contains(ALREADY_DID_IN_SYSTEM)) return null;
132                
133                SurveyPlugin plugin = (SurveyPlugin) Global.getSettings().getNewPluginInstance("surveyPlugin");
134                
135                int bestScore = 0;
136                PlanetAPI best = null;
137                for (PlanetAPI planet : system.getPlanets()) {
138                        if (planet.isStar()) continue;
139                        if (planet.getMarket() == null) continue;
140                        
141                        SurveyLevel level = planet.getMarket().getSurveyLevel();
142                        if (level == SurveyLevel.FULL) continue;
143                        
144                        int score = plugin.getSurveyDataScore(planet);
145                        if (score > bestScore) {
146                                bestScore = score;
147                                best = planet;
148                        }
149                }
150                
151                return best;
152        }
153        
154
155        @Override
156        protected void deactivateImpl() {
157                cleanupImpl();
158        }
159        
160        @Override
161        protected void cleanupImpl() {
162                CampaignFleetAPI fleet = getFleet();
163                if (fleet == null) return;
164                
165                fleet.getStats().getDetectedRangeMod().unmodify(getModId());
166                fleet.getStats().getFleetwideMaxBurnMod().unmodify(getModId());
167                fleet.getStats().getAccelerationMult().unmodify(getModId());
168        }
169
170        
171        @Override
172        public void createTooltip(TooltipMakerAPI tooltip, boolean expanded) {
173                
174                CampaignFleetAPI fleet = getFleet();
175                if (fleet == null) return;
176                
177                Color gray = Misc.getGrayColor();
178                Color highlight = Misc.getHighlightColor();
179                Color bad = Misc.getNegativeHighlightColor();
180                
181                if (!Global.CODEX_TOOLTIP_MODE) {
182                        LabelAPI title = tooltip.addTitle(spec.getName());
183                } else {
184                        tooltip.addSpacer(-10f);
185                }
186
187                float pad = 10f;
188                tooltip.addPara("Coordinate the fleet's active sensor network to scan all the planets in the system and "
189                                + "identify the most promising candidate for a full survey operation.", 
190                                pad, highlight,
191                                "" + (int)SURVEY_RANGE);
192                
193                tooltip.addPara("Increases the range at which the fleet can be detected by %s* units and brings the fleet to a near-stop as drives are powered down to reduce interference.",
194                                pad, highlight,
195                                "" + (int)DETECTABILITY_RANGE_BONUS
196                );
197                
198                if (!Global.CODEX_TOOLTIP_MODE) {
199                        //List<PlanetAPI> planets = getAllPlanetsInRange();
200                        PlanetAPI planet = findBestPlanet();
201                        if (planet == null) {
202                                if (fleet.isInHyperspace()) {
203                                        tooltip.addPara("Can not be used in hyperspace.", bad, pad);
204                                } else if (fleet.getStarSystem() != null &&
205                                                fleet.getStarSystem().getMemoryWithoutUpdate().contains(ALREADY_DID_IN_SYSTEM)) {
206                                        tooltip.addPara("Remote survey already performed in this star system.", bad, pad);
207                                } else {
208                                        tooltip.addPara("No suitable planets in the star system.", bad, pad);
209                                }
210                        }
211                }
212                
213                tooltip.addPara("*2000 units = 1 map grid cell", gray, pad);
214                
215                
216                addIncompatibleToTooltip(tooltip, expanded);
217                
218        }
219
220        public boolean hasTooltip() {
221                return true;
222        }
223        
224}
225
226
227
228
229