001package com.fs.starfarer.api.impl.campaign.rulecmd.missions;
002
003import java.awt.Color;
004import java.util.ArrayList;
005import java.util.List;
006import java.util.Map;
007
008import com.fs.starfarer.api.Global;
009import com.fs.starfarer.api.campaign.BaseCampaignEntityPickerListener;
010import com.fs.starfarer.api.campaign.CampaignFleetAPI;
011import com.fs.starfarer.api.campaign.CargoAPI;
012import com.fs.starfarer.api.campaign.InteractionDialogAPI;
013import com.fs.starfarer.api.campaign.JumpPointAPI.JumpDestination;
014import com.fs.starfarer.api.campaign.LocationAPI;
015import com.fs.starfarer.api.campaign.OptionPanelAPI;
016import com.fs.starfarer.api.campaign.SectorEntityToken;
017import com.fs.starfarer.api.campaign.StarSystemAPI;
018import com.fs.starfarer.api.campaign.TextPanelAPI;
019import com.fs.starfarer.api.campaign.listeners.ListenerUtil;
020import com.fs.starfarer.api.campaign.rules.MemoryAPI;
021import com.fs.starfarer.api.impl.campaign.GateEntityPlugin;
022import com.fs.starfarer.api.impl.campaign.GateExplosionScript;
023import com.fs.starfarer.api.impl.campaign.ids.Tags;
024import com.fs.starfarer.api.impl.campaign.rulecmd.BaseCommandPlugin;
025import com.fs.starfarer.api.ui.TooltipMakerAPI;
026import com.fs.starfarer.api.util.Misc;
027import com.fs.starfarer.api.util.Misc.Token;
028
029/**
030 * NotifyEvent $eventHandle <params> 
031 * 
032 */
033public class GateCMD extends BaseCommandPlugin {
034        
035        protected CampaignFleetAPI playerFleet;
036        protected SectorEntityToken entity;
037        protected TextPanelAPI text;
038        protected OptionPanelAPI options;
039        protected CargoAPI playerCargo;
040        protected MemoryAPI memory;
041        protected InteractionDialogAPI dialog;
042        protected Map<String, MemoryAPI> memoryMap;
043        
044        public boolean execute(String ruleId, InteractionDialogAPI dialog, List<Token> params, Map<String, MemoryAPI> memoryMap) {
045                
046                this.dialog = dialog;
047                this.memoryMap = memoryMap;
048                
049                String command = params.get(0).getString(memoryMap);
050                if (command == null) return false;
051                
052                memory = getEntityMemory(memoryMap);
053                
054                entity = dialog.getInteractionTarget();
055                text = dialog.getTextPanel();
056                options = dialog.getOptionPanel();
057                
058                playerFleet = Global.getSector().getPlayerFleet();
059                playerCargo = playerFleet.getCargo();
060                
061                if (command.equals("selectDestination")) {
062                        selectDestination();
063                } else if (command.equals("notifyScanned")) {
064                        notifyScanned(entity);
065                } else if (command.equals("explode")) {
066                        explode(entity);
067                } else if (command.equals("isPopulated")) {
068                        return isPopulated(entity);
069                }
070                return true;
071        }
072        
073        public static boolean isPopulated(SectorEntityToken targetGate) {
074                if (targetGate.getContainingLocation() == null) return false;
075                
076                LocationAPI loc = targetGate.getContainingLocation();
077                if (loc.hasTag(Tags.THEME_CORE_POPULATED)) return true;
078                
079                return !Misc.getMarketsInLocation(loc).isEmpty();
080        }
081        
082        public static int computeFuelCost(SectorEntityToken targetGate) {
083                float dist = Misc.getDistanceToPlayerLY(targetGate);
084                float fuelPerLY = Global.getSector().getPlayerFleet().getLogistics().getFuelCostPerLightYear();
085                
086                return (int) Math.ceil(dist * fuelPerLY * Misc.GATE_FUEL_COST_MULT);
087        }
088
089        protected void selectDestination() {
090                final ArrayList<SectorEntityToken> gates = 
091                                new ArrayList<SectorEntityToken>(GateEntityPlugin.getGateData().scanned);
092                gates.remove(entity);
093                dialog.showCampaignEntityPicker("Select destination", "Destination:", "Initiate transit", 
094                                Global.getSector().getPlayerFaction(), gates, 
095                        new BaseCampaignEntityPickerListener() {
096                                public void pickedEntity(SectorEntityToken entity) {
097                                        int cost = computeFuelCost(entity);
098                                        Global.getSector().getPlayerFleet().getCargo().removeFuel(cost);
099                                        
100                                        dialog.dismiss();
101                                        Global.getSector().setPaused(false);
102                                        JumpDestination dest = new JumpDestination(entity, null);
103                                        Global.getSector().doHyperspaceTransition(playerFleet, GateCMD.this.entity, dest, 2f);
104                                        
105                                        float distLY = Misc.getDistanceLY(entity, GateCMD.this.entity);
106                                        if (entity.getCustomPlugin() instanceof GateEntityPlugin) {
107                                                GateEntityPlugin plugin = (GateEntityPlugin) entity.getCustomPlugin();
108                                                plugin.showBeingUsed(distLY);
109                                        }
110                                        if (GateCMD.this.entity.getCustomPlugin() instanceof GateEntityPlugin) {
111                                                GateEntityPlugin plugin = (GateEntityPlugin) GateCMD.this.entity.getCustomPlugin();
112                                                plugin.showBeingUsed(distLY);
113                                        }
114                                        
115                                        ListenerUtil.reportFleetTransitingGate(Global.getSector().getPlayerFleet(),
116                                                                                                                   GateCMD.this.entity, entity);
117                                }
118                                public void cancelledEntityPicking() {
119                                        
120                                }
121                                public String getMenuItemNameOverrideFor(SectorEntityToken entity) {
122                                        return null;
123                                }
124                                public String getSelectedTextOverrideFor(SectorEntityToken entity) {
125                                        return entity.getName() + " - " + entity.getContainingLocation().getNameWithTypeShort();
126                                }
127                                public void createInfoText(TooltipMakerAPI info, SectorEntityToken entity) {
128                                        
129                                        int cost = computeFuelCost(entity);
130                                        int available = (int) Global.getSector().getPlayerFleet().getCargo().getFuel();
131                                        
132                                        Color reqColor = Misc.getHighlightColor();
133                                        Color availableColor = Misc.getHighlightColor();
134                                        if (cost > available) {
135                                                reqColor = Misc.getNegativeHighlightColor();
136                                        }
137                                        
138                                        info.setParaSmallInsignia();
139//                                      LabelAPI label = info.addPara("Transit requires %s fuel. "
140//                                                      + "You have %s units of fuel available.", 0f,
141//                                                      Misc.getTextColor(),
142//                                                      //Misc.getGrayColor(),
143//                                                      availColor, Misc.getWithDGS(cost), Misc.getWithDGS(available));
144//                                      label.setHighlightColors(reqColor, availColor);
145                                        
146                                        info.beginGrid(200f, 2, Misc.getGrayColor());
147                                        info.setGridFontSmallInsignia();
148                                        info.addToGrid(0, 0, "    Fuel required:", Misc.getWithDGS(cost), reqColor);
149                                        info.addToGrid(1, 0, "    Fuel available:", Misc.getWithDGS(available), availableColor);
150                                        info.addGrid(0);;
151                                }
152                                public boolean canConfirmSelection(SectorEntityToken entity) {
153                                        int cost = computeFuelCost(entity);
154                                        int available = (int) Global.getSector().getPlayerFleet().getCargo().getFuel();
155                                        return cost <= available;
156                                }
157                                public float getFuelColorAlphaMult() {
158                                        return 0.5f;
159                                }
160                                public float getFuelRangeMult() { // just for showing it on the map when picking destination
161                                        if (true) return 0f;
162                                        if (Misc.GATE_FUEL_COST_MULT <= 0) return 0f;
163                                        return 1f / Misc.GATE_FUEL_COST_MULT;
164                                }
165                        });
166        }
167        
168        public static void notifyScanned(SectorEntityToken gate) {
169                GateEntityPlugin.getGateData().scanned.add(gate);
170                gate.getCustomPlugin().advance(0f); // makes gate activate if already did quest
171        }
172        
173
174        
175        
176        public static void explode(SectorEntityToken gate) {
177                gate.getContainingLocation().addScript(new GateExplosionScript(gate));
178                
179                // If the player blew up the Gate at a responsible distance from the core worlds, take note.
180                // Askonia is near the center of the core worlds, so use distance to there to gauge.
181                
182                if(Global.getSector().getMemoryWithoutUpdate().contains("$gaATG_useJanusPrototype") && 
183                        !Global.getSector().getMemoryWithoutUpdate().contains("$ggaATG_firstJanusResults")) {
184                        StarSystemAPI askonia = Global.getSector().getStarSystem("askonia");
185                        if(askonia != null) {
186                                float dist = Misc.getDistance(gate.getLocationInHyperspace(), askonia.getLocation());
187                                System.out.print("dist = " + dist);
188                                if(dist > 12000.0f) {
189                                        Global.getSector().getMemoryWithoutUpdate().set("$gaATG_didRemoteFirstJanusTest", true );
190                                }
191                        }
192                }
193                
194        }
195}
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210