001package com.fs.starfarer.api.impl.campaign.rulecmd.missions;
002
003import java.util.ArrayList;
004import java.util.List;
005import java.util.Map;
006
007import com.fs.starfarer.api.Global;
008import com.fs.starfarer.api.campaign.BaseCampaignEntityPickerListener;
009import com.fs.starfarer.api.campaign.CampaignFleetAPI;
010import com.fs.starfarer.api.campaign.CargoAPI;
011import com.fs.starfarer.api.campaign.CargoAPI.CargoItemType;
012import com.fs.starfarer.api.campaign.InteractionDialogAPI;
013import com.fs.starfarer.api.campaign.LocationAPI;
014import com.fs.starfarer.api.campaign.OptionPanelAPI;
015import com.fs.starfarer.api.campaign.SectorEntityToken;
016import com.fs.starfarer.api.campaign.StarSystemAPI;
017import com.fs.starfarer.api.campaign.TextPanelAPI;
018import com.fs.starfarer.api.campaign.rules.MemoryAPI;
019import com.fs.starfarer.api.impl.campaign.DebugFlags;
020import com.fs.starfarer.api.impl.campaign.ids.Commodities;
021import com.fs.starfarer.api.impl.campaign.ids.Tags;
022import com.fs.starfarer.api.impl.campaign.intel.misc.GateHaulerIntel;
023import com.fs.starfarer.api.impl.campaign.rulecmd.BaseCommandPlugin;
024import com.fs.starfarer.api.ui.TooltipMakerAPI;
025import com.fs.starfarer.api.util.Misc;
026import com.fs.starfarer.api.util.Misc.Token;
027
028/**
029 * NotifyEvent $eventHandle <params> 
030 * 
031 */
032public class GateHaulerCMD extends BaseCommandPlugin {
033        
034        public static int ACTIVATION_COST = 1000;
035        
036        protected CampaignFleetAPI playerFleet;
037        protected SectorEntityToken entity;
038        protected SectorEntityToken stableLocation;
039        protected TextPanelAPI text;
040        protected OptionPanelAPI options;
041        protected CargoAPI playerCargo;
042        protected MemoryAPI memory;
043        protected InteractionDialogAPI dialog;
044        protected Map<String, MemoryAPI> memoryMap;
045        
046        public boolean execute(String ruleId, InteractionDialogAPI dialog, List<Token> params, Map<String, MemoryAPI> memoryMap) {
047                
048                this.dialog = dialog;
049                this.memoryMap = memoryMap;
050                
051                String command = params.get(0).getString(memoryMap);
052                if (command == null) return false;
053                
054                memory = getEntityMemory(memoryMap);
055                
056                entity = dialog.getInteractionTarget();
057                text = dialog.getTextPanel();
058                options = dialog.getOptionPanel();
059                
060                playerFleet = Global.getSector().getPlayerFleet();
061                playerCargo = playerFleet.getCargo();
062                
063                stableLocation = findNearestStableLocation();
064                
065                if (command.equals("addIntel")) {
066                        Global.getSector().getIntelManager().addIntel(new GateHaulerIntel(entity), false, text);
067                } else if (command.equals("printCost")) {
068                        printCost();
069                } else if (command.equals("removeActivationCosts")) {
070                        removeActivationCosts();
071                } else if (command.equals("canActivate")) {
072                        return canActivate();
073                } else if (command.equals("selectDestination")) {
074                        selectDestination();
075                } else if (command.equals("activate")) {
076                        activate();
077                } else if (command.equals("canDeploy")) {
078                        return canDeploy();
079                } else if (command.equals("deploy")) {
080                        deploy();
081                } else if (command.equals("isInCurrentSystem")) {
082                        return isInCurrentSystem();
083                } else if (command.equals("wasDeployedToCurrentSystem")) {
084                        return wasDeployedToCurrentSystem();
085                }
086                return true;
087        }
088        
089        public boolean wasDeployedToCurrentSystem() {
090                LocationAPI loc = Global.getSector().getCurrentLocation();
091                if (loc == null) return false;
092                return loc.getMemoryWithoutUpdate().getBoolean("$deployedGateHaulerHere");
093                
094        }
095        public boolean isInCurrentSystem() {
096                GateHaulerIntel intel = GateHaulerIntel.get(entity);
097                if (intel != null) {
098                        if (intel.getAction() == null) {
099                                return intel.getGateHauler().getContainingLocation() == Global.getSector().getCurrentLocation();
100                        }
101                }
102                return false;
103        }
104        
105        public void deploy() {
106                if (stableLocation == null) return;
107                
108                GateHaulerIntel intel = GateHaulerIntel.get(entity);
109                if (intel != null) {
110                        intel.initiateDeployment(stableLocation);
111                }
112        }
113        
114        public boolean canDeploy() {
115                GateHaulerIntel intel = GateHaulerIntel.get(entity);
116                if (intel == null) return false;
117                return stableLocation != null;
118        }
119        
120        public SectorEntityToken findNearestStableLocation() {
121                if (entity.getContainingLocation() == null) return null;
122                float minDist = Float.MAX_VALUE;
123                SectorEntityToken nearest = null;
124                for (SectorEntityToken curr : entity.getContainingLocation().getEntitiesWithTag(Tags.STABLE_LOCATION)) {
125                        float dist = Misc.getDistance(curr, entity);
126                        if (dist < minDist) {
127                                minDist = dist;
128                                nearest = curr;
129                        }
130                }
131                return nearest;
132        }
133        
134        public void activate() {
135                GateHaulerIntel intel = GateHaulerIntel.get(GateHaulerCMD.this.entity);
136                if (intel != null) {
137                        intel.activate();
138                }
139        }
140
141        public void printCost() {
142                Misc.showCost(text, null, null, getResources(), getQuantities());
143                
144                if (canActivate()) {
145                        text.addPara("Proceed with reactivation?");
146                } else {
147                        text.addPara("You do not have the necessary resources to reactivate the Gate Hauler.");
148                }
149        }
150        
151        public void removeActivationCosts() {
152                CargoAPI cargo = playerCargo;
153                String [] res = getResources();
154                int [] quantities = getQuantities();
155                for (int i = 0; i < res.length; i++) {
156                        String commodityId = res[i];
157                        int quantity = quantities[i];
158                        cargo.removeCommodity(commodityId, quantity);
159                }
160        }
161        
162        public boolean canActivate() {
163                if (DebugFlags.OBJECTIVES_DEBUG) {
164                        return true;
165                }
166                
167                CargoAPI cargo = playerCargo;
168                String [] res = getResources();
169                int [] quantities = getQuantities();
170                
171                for (int i = 0; i < res.length; i++) {
172                        String commodityId = res[i];
173                        int quantity = quantities[i];
174                        if (quantity > cargo.getQuantity(CargoItemType.RESOURCES, commodityId)) {
175                                return false;
176                        }
177                }
178                return true;
179        }
180        
181        public String [] getResources() {
182                return new String[] {Commodities.RARE_METALS};
183        }
184
185        public int [] getQuantities() {
186                return new int[] {ACTIVATION_COST};
187        }
188        
189        public int getTravelDays(SectorEntityToken entity) {
190                GateHaulerIntel intel = GateHaulerIntel.get(GateHaulerCMD.this.entity);
191                if (intel != null) {
192                        StarSystemAPI system = Misc.getStarSystemForAnchor(entity);
193                        return intel.computeTransitDays(system);
194                }
195                return 365;
196        }
197        
198        public void selectDestination() {
199                final ArrayList<SectorEntityToken> systems = new ArrayList<SectorEntityToken>();
200                for (StarSystemAPI curr : Global.getSector().getStarSystems()) {
201                        if (curr == entity.getContainingLocation()) continue;
202                        if (curr.hasTag(Tags.THEME_HIDDEN) && !"Limbo".equals(curr.getBaseName())) continue;
203                        if (curr.isDeepSpace()) continue;
204                        if (curr.getHyperspaceAnchor() == null) continue;
205                        if (Misc.getStarSystemForAnchor(curr.getHyperspaceAnchor()) == null) continue;
206                        systems.add(curr.getHyperspaceAnchor());
207                }
208                dialog.showCampaignEntityPicker("Select destination for gate hauler", "Destination:", "Execute", 
209                                Global.getSector().getPlayerFaction(), systems, 
210                        new BaseCampaignEntityPickerListener() {
211                                public void pickedEntity(SectorEntityToken entity) {
212                                        dialog.dismiss();
213                                        Global.getSector().setPaused(false);
214                                        
215                                        GateHaulerIntel intel = GateHaulerIntel.get(GateHaulerCMD.this.entity);
216                                        if (intel != null) {
217                                                StarSystemAPI system = Misc.getStarSystemForAnchor(entity);
218                                                intel.initiateDeparture(system);
219                                        }
220                                }
221                                public void cancelledEntityPicking() {
222                                        
223                                }
224                                public String getMenuItemNameOverrideFor(SectorEntityToken entity) {
225                                        StarSystemAPI system = Misc.getStarSystemForAnchor(entity);
226                                        if (system != null) {
227                                                return system.getNameWithLowercaseTypeShort();
228                                        }
229                                        return null;
230                                }
231                                public String getSelectedTextOverrideFor(SectorEntityToken entity) {
232                                        StarSystemAPI system = Misc.getStarSystemForAnchor(entity);
233                                        if (system != null) {
234                                                return system.getNameWithLowercaseType();
235                                        }
236                                        return null;
237                                }
238                                public void createInfoText(TooltipMakerAPI info, SectorEntityToken entity) {
239                                        int days = getTravelDays(entity);
240                                        info.setParaSmallInsignia();
241                                        String daysStr = "days";
242                                        if (days == 1) daysStr = "day";
243                                        info.addPara("    Estimated gate hauler travel time: %s " + daysStr, 0f, Misc.getHighlightColor(), "" + days);
244                                }
245
246                                public boolean canConfirmSelection(SectorEntityToken entity) {
247                                        return true;
248                                }
249                                public float getFuelColorAlphaMult() {
250                                        return 0f;
251                                }
252                                public float getFuelRangeMult() { // just for showing it on the map when picking destination
253                                        return 0f;
254                                }
255                        });
256        }
257}
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272