Starsector API
Loading...
Searching...
No Matches
GateHaulerCMD.java
Go to the documentation of this file.
1package com.fs.starfarer.api.impl.campaign.rulecmd.missions;
2
3import java.util.ArrayList;
4import java.util.List;
5import java.util.Map;
6
7import com.fs.starfarer.api.Global;
8import com.fs.starfarer.api.campaign.BaseCampaignEntityPickerListener;
9import com.fs.starfarer.api.campaign.CampaignFleetAPI;
10import com.fs.starfarer.api.campaign.CargoAPI;
11import com.fs.starfarer.api.campaign.CargoAPI.CargoItemType;
12import com.fs.starfarer.api.campaign.InteractionDialogAPI;
13import com.fs.starfarer.api.campaign.OptionPanelAPI;
14import com.fs.starfarer.api.campaign.SectorEntityToken;
15import com.fs.starfarer.api.campaign.StarSystemAPI;
16import com.fs.starfarer.api.campaign.TextPanelAPI;
17import com.fs.starfarer.api.campaign.rules.MemoryAPI;
18import com.fs.starfarer.api.impl.campaign.DebugFlags;
19import com.fs.starfarer.api.impl.campaign.ids.Commodities;
20import com.fs.starfarer.api.impl.campaign.ids.Tags;
21import com.fs.starfarer.api.impl.campaign.intel.misc.GateHaulerIntel;
22import com.fs.starfarer.api.impl.campaign.rulecmd.BaseCommandPlugin;
23import com.fs.starfarer.api.ui.TooltipMakerAPI;
24import com.fs.starfarer.api.util.Misc;
25import com.fs.starfarer.api.util.Misc.Token;
26
31public class GateHaulerCMD extends BaseCommandPlugin {
32
33 public static int ACTIVATION_COST = 1000;
34
35 protected CampaignFleetAPI playerFleet;
36 protected SectorEntityToken entity;
37 protected SectorEntityToken stableLocation;
38 protected TextPanelAPI text;
39 protected OptionPanelAPI options;
40 protected CargoAPI playerCargo;
41 protected MemoryAPI memory;
42 protected InteractionDialogAPI dialog;
43 protected Map<String, MemoryAPI> memoryMap;
44
45 public boolean execute(String ruleId, InteractionDialogAPI dialog, List<Token> params, Map<String, MemoryAPI> memoryMap) {
46
47 this.dialog = dialog;
48 this.memoryMap = memoryMap;
49
50 String command = params.get(0).getString(memoryMap);
51 if (command == null) return false;
52
54
55 entity = dialog.getInteractionTarget();
56 text = dialog.getTextPanel();
57 options = dialog.getOptionPanel();
58
59 playerFleet = Global.getSector().getPlayerFleet();
60 playerCargo = playerFleet.getCargo();
61
63
64 if (command.equals("addIntel")) {
65 Global.getSector().getIntelManager().addIntel(new GateHaulerIntel(entity), false, text);
66 } else if (command.equals("printCost")) {
67 printCost();
68 } else if (command.equals("removeActivationCosts")) {
70 } else if (command.equals("canActivate")) {
71 return canActivate();
72 } else if (command.equals("selectDestination")) {
74 } else if (command.equals("activate")) {
75 activate();
76 } else if (command.equals("canDeploy")) {
77 return canDeploy();
78 } else if (command.equals("deploy")) {
79 deploy();
80 }
81 return true;
82 }
83
84 public void deploy() {
85 if (stableLocation == null) return;
86
87 GateHaulerIntel intel = GateHaulerIntel.get(entity);
88 if (intel != null) {
89 intel.initiateDeployment(stableLocation);
90 }
91 }
92
93 public boolean canDeploy() {
94 GateHaulerIntel intel = GateHaulerIntel.get(entity);
95 if (intel == null) return false;
96 return stableLocation != null;
97 }
98
99 public SectorEntityToken findNearestStableLocation() {
100 if (entity.getContainingLocation() == null) return null;
101 float minDist = Float.MAX_VALUE;
102 SectorEntityToken nearest = null;
103 for (SectorEntityToken curr : entity.getContainingLocation().getEntitiesWithTag(Tags.STABLE_LOCATION)) {
104 float dist = Misc.getDistance(curr, entity);
105 if (dist < minDist) {
106 minDist = dist;
107 nearest = curr;
108 }
109 }
110 return nearest;
111 }
112
113 public void activate() {
114 GateHaulerIntel intel = GateHaulerIntel.get(GateHaulerCMD.this.entity);
115 if (intel != null) {
116 intel.activate();
117 }
118 }
119
120 public void printCost() {
121 Misc.showCost(text, null, null, getResources(), getQuantities());
122
123 if (canActivate()) {
124 text.addPara("Proceed with reactivation?");
125 } else {
126 text.addPara("You do not have the necessary resources to reactivate the Gate Hauler.");
127 }
128 }
129
130 public void removeActivationCosts() {
131 CargoAPI cargo = playerCargo;
132 String [] res = getResources();
133 int [] quantities = getQuantities();
134 for (int i = 0; i < res.length; i++) {
135 String commodityId = res[i];
136 int quantity = quantities[i];
137 cargo.removeCommodity(commodityId, quantity);
138 }
139 }
140
141 public boolean canActivate() {
143 return true;
144 }
145
146 CargoAPI cargo = playerCargo;
147 String [] res = getResources();
148 int [] quantities = getQuantities();
149
150 for (int i = 0; i < res.length; i++) {
151 String commodityId = res[i];
152 int quantity = quantities[i];
153 if (quantity > cargo.getQuantity(CargoItemType.RESOURCES, commodityId)) {
154 return false;
155 }
156 }
157 return true;
158 }
159
160 public String [] getResources() {
161 return new String[] {Commodities.RARE_METALS};
162 }
163
164 public int [] getQuantities() {
165 return new int[] {ACTIVATION_COST};
166 }
167
168 public int getTravelDays(SectorEntityToken entity) {
169 GateHaulerIntel intel = GateHaulerIntel.get(GateHaulerCMD.this.entity);
170 if (intel != null) {
171 StarSystemAPI system = Misc.getStarSystemForAnchor(entity);
172 return intel.computeTransitDays(system);
173 }
174 return 365;
175 }
176
177 public void selectDestination() {
178 final ArrayList<SectorEntityToken> systems = new ArrayList<SectorEntityToken>();
179 for (StarSystemAPI curr : Global.getSector().getStarSystems()) {
180 if (curr == entity.getContainingLocation()) continue;
181 if (curr.hasTag(Tags.THEME_HIDDEN) && !"Limbo".equals(curr.getBaseName())) continue;
182 if (curr.isDeepSpace()) continue;
183 if (curr.getHyperspaceAnchor() == null) continue;
184 if (Misc.getStarSystemForAnchor(curr.getHyperspaceAnchor()) == null) continue;
185 systems.add(curr.getHyperspaceAnchor());
186 }
187 dialog.showCampaignEntityPicker("Select destination for gate hauler", "Destination:", "Execute",
188 Global.getSector().getPlayerFaction(), systems,
189 new BaseCampaignEntityPickerListener() {
190 public void pickedEntity(SectorEntityToken entity) {
191 dialog.dismiss();
192 Global.getSector().setPaused(false);
193
194 GateHaulerIntel intel = GateHaulerIntel.get(GateHaulerCMD.this.entity);
195 if (intel != null) {
196 StarSystemAPI system = Misc.getStarSystemForAnchor(entity);
197 intel.initiateDeparture(system);
198 }
199 }
200 public void cancelledEntityPicking() {
201
202 }
203 public String getMenuItemNameOverrideFor(SectorEntityToken entity) {
204 StarSystemAPI system = Misc.getStarSystemForAnchor(entity);
205 if (system != null) {
206 return system.getNameWithLowercaseTypeShort();
207 }
208 return null;
209 }
210 public String getSelectedTextOverrideFor(SectorEntityToken entity) {
211 StarSystemAPI system = Misc.getStarSystemForAnchor(entity);
212 if (system != null) {
213 return system.getNameWithLowercaseType();
214 }
215 return null;
216 }
217 public void createInfoText(TooltipMakerAPI info, SectorEntityToken entity) {
218 int days = getTravelDays(entity);
219 info.setParaSmallInsignia();
220 String daysStr = "days";
221 if (days == 1) daysStr = "day";
222 info.addPara(" Estimated gate hauler travel time: %s " + daysStr, 0f, Misc.getHighlightColor(), "" + days);
223 }
224
225 public boolean canConfirmSelection(SectorEntityToken entity) {
226 return true;
227 }
228 public float getFuelColorAlphaMult() {
229 return 0f;
230 }
231 public float getFuelRangeMult() { // just for showing it on the map when picking destination
232 return 0f;
233 }
234 });
235 }
236}
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
static SectorAPI getSector()
Definition Global.java:59
static MemoryAPI getEntityMemory(Map< String, MemoryAPI > memoryMap)
boolean execute(String ruleId, InteractionDialogAPI dialog, List< Token > params, Map< String, MemoryAPI > memoryMap)