001package com.fs.starfarer.api.impl.campaign.missions;
002
003import java.awt.Color;
004import java.util.List;
005import java.util.Map;
006
007import com.fs.starfarer.api.Global;
008import com.fs.starfarer.api.campaign.CampaignFleetAPI;
009import com.fs.starfarer.api.campaign.InteractionDialogAPI;
010import com.fs.starfarer.api.campaign.PlanetAPI;
011import com.fs.starfarer.api.campaign.SectorEntityToken;
012import com.fs.starfarer.api.campaign.StarSystemAPI;
013import com.fs.starfarer.api.campaign.econ.MarketAPI;
014import com.fs.starfarer.api.campaign.rules.MemoryAPI;
015import com.fs.starfarer.api.impl.campaign.missions.hub.HubMissionWithSearch;
016import com.fs.starfarer.api.impl.campaign.procgen.themes.MiscellaneousThemeGenerator;
017import com.fs.starfarer.api.ui.SectorMapAPI;
018import com.fs.starfarer.api.ui.TooltipMakerAPI;
019import com.fs.starfarer.api.util.Misc;
020import com.fs.starfarer.api.util.Misc.Token;
021
022public class RecoverAPlanetkiller extends HubMissionWithSearch {
023
024        public static enum Stage {
025                GO_TO_NEXUS,
026                RECOVER_PK,
027                COMPLETED,
028                FAILED,
029        }
030        
031        public static boolean startedAtPather() {
032                return Global.getSector().getMemoryWithoutUpdate().getBoolean("$pk_startedAtPather");
033        }
034        public static boolean startedAtNexus() {
035                return Global.getSector().getMemoryWithoutUpdate().getBoolean("$pk_startedAtNexus");
036        }
037        public static boolean gotDataFromMysteryAI() {
038                return Global.getSector().getMemoryWithoutUpdate().getBoolean("$pk_gotDataFromMysteryAI");
039        }
040        public static CampaignFleetAPI getNexus() {
041                return (CampaignFleetAPI) Global.getSector().getPersistentData().get(MiscellaneousThemeGenerator.PK_NEXUS_KEY);
042        }
043        public StarSystemAPI getNexusSystem() {
044                CampaignFleetAPI nexus = getNexus();
045                if (nexus != null && nexus.isAlive()) {
046                        return nexus.getStarSystem();
047                }
048                return null;
049        }
050        public static MarketAPI getTundraMarket() {
051                PlanetAPI p = getTundra();
052                if (p == null) return null;
053                return p.getMarket();
054        }
055        public static PlanetAPI getTundra() {
056                return (PlanetAPI) Global.getSector().getPersistentData().get(MiscellaneousThemeGenerator.PK_PLANET_KEY);
057        }
058        public static StarSystemAPI getPKSystem() {
059                return (StarSystemAPI) Global.getSector().getPersistentData().get(MiscellaneousThemeGenerator.PK_SYSTEM_KEY);
060        }
061        
062        @Override
063        protected boolean create(MarketAPI createdAt, boolean barEvent) {
064                //genRandom = Misc.random;
065                setPersonOverride(null);
066                
067                setStoryMission();
068                setNoAbandon();
069                
070                if (!setGlobalReference("$pk_ref", "$pk_inProgress")) {
071                        return false;
072                }
073                
074                CampaignFleetAPI nexus = getNexus();
075                if (nexus != null && nexus.isAlive()) {
076                        makeImportant(nexus, "$pk_nexus", Stage.GO_TO_NEXUS);
077                }
078                
079                if (startedAtNexus()) {
080                        setStartingStage(Stage.RECOVER_PK);
081                } else {
082                        if (nexus == null || !nexus.isAlive() || getNexusSystem() == null) {
083                                return false;
084                        }
085                        setStartingStage(Stage.GO_TO_NEXUS);
086                }
087                
088                setSuccessStage(Stage.COMPLETED);
089                setFailureStage(Stage.FAILED);
090                
091//              setRepFactionChangesVeryHigh();
092//              setRepPersonChangesVeryHigh();
093                
094                setRepPenaltyFaction(0f);
095                setRepPenaltyPerson(0f);
096                
097                connectWithGlobalFlag(Stage.GO_TO_NEXUS, Stage.RECOVER_PK, "$pk_nexusDataGained");
098                if (nexus != null) {
099                        connectWithEntityNotAlive(Stage.GO_TO_NEXUS, Stage.RECOVER_PK, nexus);
100                }
101                setStageOnGlobalFlag(Stage.COMPLETED, "$pk_completed");
102
103                beginStageTrigger(Stage.COMPLETED);
104                triggerSetGlobalMemoryValue("$pk_recovered", true);
105                triggerSetGlobalMemoryValue("$pk_missionCompleted", true);
106                endTrigger();
107                
108                return true;
109        }
110        
111        @Override
112        public SectorEntityToken getMapLocation(SectorMapAPI map, Object currentStage) {
113                if (currentStage == Stage.RECOVER_PK) {
114                        return getPKSystem().getCenter();
115                }
116                return super.getMapLocation(map, currentStage);
117        }
118        
119//      @Override
120//      protected void endAbandonImpl() {
121//              super.endAbandonImpl();
122//              endFailureImpl(null, null);
123//      }
124
125        @Override
126        protected void endFailureImpl(InteractionDialogAPI dialog, Map<String, MemoryAPI> memoryMap) {
127                if (currentStage == Stage.FAILED) {
128                        //KantaCMD.loseProtection(dialog);
129                }
130        }
131        
132        
133        protected void updateInteractionDataImpl() {
134                CampaignFleetAPI nexus = getNexus();
135                StarSystemAPI ns = getNexusSystem();
136                StarSystemAPI pks = getPKSystem();
137                
138                if (getCurrentStage() != null) {
139                        set("$pk_stage", ((Enum)getCurrentStage()).name());
140                }
141                
142                if (ns != null) {
143                        set("$pk_nexusSystemName", ns.getNameWithLowercaseTypeShort());
144                }
145                if (pks != null) {
146                        set("$pk_pkSystemName", pks.getNameWithLowercaseTypeShort());
147                }
148        }
149        
150        @Override
151        public void addDescriptionForNonEndStage(TooltipMakerAPI info, float width, float height) {
152                float opad = 10f;
153                Color h = Misc.getHighlightColor();
154                if (currentStage == Stage.GO_TO_NEXUS) {
155                        StarSystemAPI ns = getNexusSystem();
156                        if (ns != null) {
157                                info.addPara("Recover information about a Hegemony fleet last seen in a Tri-Tachyon held "
158                                                + "system during the first AI War, more than a hundred cycles ago. The fleet is rumored to "
159                                                + "have been carrying a planetkiller weapon.", opad);
160                                info.addPara("The information should be contained in the data banks of a remnant Nexus"
161                                                + " located in the " + 
162                                                ns.getNameWithLowercaseType() + ".", opad);
163                        }
164                } else if (currentStage == Stage.RECOVER_PK) {
165                        StarSystemAPI pks = getPKSystem();
166                        if (pks != null) {
167                                if (startedAtNexus()) {
168                                        info.addPara("You've destroyed a remnant Nexus and recovered information about "
169                                                        + "a Hegemony fleet estimated to be carrying a planetkiller weapon.", opad);
170                                        info.addPara("A log entry records "
171                                                + "a large Ordo sent in pursuit; its last hyperwave report placed it "
172                                                + "near the " + pks.getNameWithLowercaseTypeShort() + 
173                                                ". The log entry dates to the first AI War, more than a hundred cycles ago", opad);
174                                if (gotDataFromMysteryAI()){
175                                        info.addPara("A mysterious voice, some kind of AI, provided you with coordinates for the " + pks.getNameWithLowercaseTypeShort() +", and implied that the planetkiller weapon could be found there.", opad);
176                                        info.addPara("Perhaps it brings 'much amusement' to this AI to not provide more detailed instructions.", opad);
177                                }
178                                } else {
179                                        info.addPara("The data banks of the remnant Nexus contained information about the Hegemony fleet rumored "
180                                                        + "to be carrying a planetkiller weapon. ", opad);
181                                        info.addPara("A log entry records "
182                                                        + "a large Ordo sent in pursuit; its last hyperwave report placed it "
183                                                        + "near the " + pks.getNameWithLowercaseTypeShort() + 
184                                                        ". The log entry dates to the first AI War, more than a hundred cycles ago", opad);
185                                }
186                        }
187                } else if (currentStage == Stage.FAILED) {
188                        // not actually possible to fail
189                }
190        }
191
192        @Override
193        public boolean addNextStepText(TooltipMakerAPI info, Color tc, float pad) {
194                Color h = Misc.getHighlightColor();
195                if (currentStage == Stage.GO_TO_NEXUS) {
196                        StarSystemAPI ns = getNexusSystem();
197                        if (ns != null) {
198                                info.addPara("Recover information from remnant Nexus in the " + ns.getNameWithLowercaseTypeShort(), tc, pad);
199                        }
200                        return true;
201                } else if (currentStage == Stage.RECOVER_PK) {
202                        StarSystemAPI pks = getPKSystem();
203                        if (pks != null) {
204                                info.addPara("Investigate the " + pks.getNameWithLowercaseTypeShort(), tc, pad);
205                        }
206                        return true;
207                }
208                return false;
209        }
210        
211        protected String getMissionTypeNoun() {
212                return "task";
213        }
214        
215        @Override
216        public String getPostfixForState() {
217                if (startingStage != null) {
218                        return "";
219                }
220                return super.getPostfixForState();
221        }
222        @Override
223        public String getBaseName() {
224                return "The Scythe of Orion";
225        }
226        
227//      Global.getSector().getListenerManager().removeListener(this);
228//      endAfterDelay();
229        
230        @Override
231        public void acceptImpl(InteractionDialogAPI dialog, Map<String, MemoryAPI> memoryMap) {
232                super.acceptImpl(dialog, memoryMap);
233                //Global.getSector().getListenerManager().addListener(this);
234        }
235
236        @Override
237        protected void notifyEnding() {
238                super.notifyEnding();
239                //Global.getSector().getListenerManager().removeListener(this);
240        }
241        
242        @Override
243        protected boolean callAction(String action, String ruleId, InteractionDialogAPI dialog, List<Token> params,
244                        Map<String, MemoryAPI> memoryMap) {
245                if (action.equals("showNexusSystem")) {
246                        StarSystemAPI system = getNexusSystem();
247                        if (system != null) {
248                                dialog.getVisualPanel().showMapMarker(system.getHyperspaceAnchor(), 
249                                                system.getNameWithLowercaseTypeShort(), getFactionForUIColors().getBaseUIColor(), 
250                                                true, getIcon(), null, getIntelTags(null));
251                        }
252                        return true;
253                } else if (action.equals("hideNexusSystem")) {
254                        dialog.getVisualPanel().removeMapMarkerFromPersonInfo();
255                        return true;
256                }
257                return super.callAction(action, ruleId, dialog, params, memoryMap);
258        }
259
260        
261}
262
263
264
265
266