Starsector API
Loading...
Searching...
No Matches
AddRemoveCommodity.java
Go to the documentation of this file.
1package com.fs.starfarer.api.impl.campaign.rulecmd;
2
3import java.util.List;
4import java.util.Map;
5
6import com.fs.starfarer.api.GameState;
7import com.fs.starfarer.api.Global;
8import com.fs.starfarer.api.campaign.CampaignFleetAPI;
9import com.fs.starfarer.api.campaign.CargoAPI;
10import com.fs.starfarer.api.campaign.CargoStackAPI;
11import com.fs.starfarer.api.campaign.InteractionDialogAPI;
12import com.fs.starfarer.api.campaign.SpecialItemData;
13import com.fs.starfarer.api.campaign.TextPanelAPI;
14import com.fs.starfarer.api.campaign.econ.CommoditySpecAPI;
15import com.fs.starfarer.api.campaign.rules.MemoryAPI;
16import com.fs.starfarer.api.characters.PersonAPI;
17import com.fs.starfarer.api.combat.ShipVariantAPI;
18import com.fs.starfarer.api.fleet.FleetMemberAPI;
19import com.fs.starfarer.api.impl.campaign.ids.Strings;
20import com.fs.starfarer.api.loading.AbilitySpecAPI;
21import com.fs.starfarer.api.loading.FighterWingSpecAPI;
22import com.fs.starfarer.api.loading.WeaponSpecAPI;
23import com.fs.starfarer.api.ui.LabelAPI;
24import com.fs.starfarer.api.util.Misc;
25import com.fs.starfarer.api.util.Misc.Token;
26import com.fs.starfarer.api.util.MutableValue;
27
32
33 public boolean execute(String ruleId, InteractionDialogAPI dialog, List<Token> params, Map<String, MemoryAPI> memoryMap) {
34 if (dialog == null) return false;
35
36 String commodityId = params.get(0).getString(memoryMap);
37 float quantity = 0;
38 int next = 2;
39 if (params.get(1).isOperator()) {
40 quantity = -1 * params.get(2).getFloat(memoryMap);
41 next = 3;
42 } else {
43 quantity = params.get(1).getFloat(memoryMap);
44 }
45 boolean withText = Math.abs(quantity) >= 1;
46 if (dialog != null && params.size() >= next + 1) {
47 withText = params.get(next).getBoolean(memoryMap) && withText;
48 }
49
50 if (commodityId.equals("credits")) {
51 MutableValue credits = Global.getSector().getPlayerFleet().getCargo().getCredits();
52 if (quantity > 0) {
53 credits.add(quantity);
54 if (withText) {
55 addCreditsGainText((int) quantity, dialog.getTextPanel());
56 }
57 } else {
58 credits.subtract(Math.abs(quantity));
59 if (credits.get() < 0) credits.set(0);
60 if (withText) {
61 addCreditsLossText((int) Math.abs(quantity), dialog.getTextPanel());
62 }
63 }
64 } else {
65 if (quantity > 0) {
66 Global.getSector().getPlayerFleet().getCargo().addCommodity(commodityId, quantity);
67 if (withText) {
68 addCommodityGainText(commodityId, (int) quantity, dialog.getTextPanel());
69 }
70 } else {
71 Global.getSector().getPlayerFleet().getCargo().removeCommodity(commodityId, Math.abs(quantity));
72 if (withText) {
73 addCommodityLossText(commodityId, (int) Math.abs(quantity), dialog.getTextPanel());
74 }
75 }
76 }
77
78
79 if (!"credits".equals(commodityId)) {
80 // update $supplies, $fuel, etc if relevant
81 updatePlayerMemoryQuantity(commodityId);
82 }
83
84 return true;
85 }
86
87 public static void updatePlayerMemoryQuantity(String commodityId) {
88 if ("credits".equals(commodityId)) {
89 CampaignFleetAPI fleet = Global.getSector().getPlayerFleet();
90 MemoryAPI memory = Global.getSector().getCharacterData().getMemoryWithoutUpdate();
91 memory.set("$credits", (int)fleet.getCargo().getCredits().get(), 0);
92 memory.set("$creditsStr", Misc.getWithDGS((int)fleet.getCargo().getCredits().get()), 0);
93 memory.set("$creditsStrC", Misc.getWithDGS((int)fleet.getCargo().getCredits().get()) + Strings.C, 0);
94 return;
95 }
96
97 MemoryAPI memory = Global.getSector().getCharacterData().getMemoryWithoutUpdate();
98 String key = "$" + commodityId;
99 if (memory.contains(key)) {
100 if (memory.get(key) instanceof Integer || memory.get(key) instanceof Float) {
101 memory.set(key, (int)Global.getSector().getPlayerFleet().getCargo().getCommodityQuantity(commodityId), 0);
102 }
103 }
104 }
105
106
107 public static void addStackGainText(CargoStackAPI stack, TextPanelAPI text) {
108 addStackGainText(stack, text, false);
109 }
110 public static void addStackGainText(CargoStackAPI stack, TextPanelAPI text, boolean lowerCase) {
111 if (stack.getSize() < 1) return;
112 text.setFontSmallInsignia();
113 String name = stack.getDisplayName();
114 if (lowerCase) {
115 name = name.toLowerCase();
116 }
117 int quantity = (int) stack.getSize();
118 text.addParagraph("Gained " + Misc.getWithDGS(quantity) + Strings.X + " " + name + "", Misc.getPositiveHighlightColor());
119 text.highlightInLastPara(Misc.getHighlightColor(), Misc.getWithDGS(quantity) + Strings.X);
120 text.setFontInsignia();
121 }
122
123
124 public static void addFighterGainText(String wingId, int quantity, TextPanelAPI text) {
125 FighterWingSpecAPI spec = Global.getSettings().getFighterWingSpec(wingId);
126 if (spec == null) return;
127
128 text.setFontSmallInsignia();
129 String name = spec.getWingName();
130 text.addParagraph("Gained " + Misc.getWithDGS(quantity) + Strings.X + " " + name + "", Misc.getPositiveHighlightColor());
131 text.highlightInLastPara(Misc.getHighlightColor(), Misc.getWithDGS(quantity) + Strings.X);
132 text.setFontInsignia();
133 }
134 public static void addFighterLossText(String wingId, int quantity, TextPanelAPI text) {
135 FighterWingSpecAPI spec = Global.getSettings().getFighterWingSpec(wingId);
136 if (spec == null) return;
137
138 text.setFontSmallInsignia();
139 String name = spec.getWingName();
140 text.addParagraph("Lost " + Misc.getWithDGS(quantity) + Strings.X + " " + name + "", Misc.getNegativeHighlightColor());
141 text.highlightInLastPara(Misc.getHighlightColor(), Misc.getWithDGS(quantity) + Strings.X);
142 text.setFontInsignia();
143 }
144
145 public static void addWeaponGainText(String weaponId, int quantity, TextPanelAPI text) {
146 WeaponSpecAPI spec = Global.getSettings().getWeaponSpec(weaponId);
147 if (spec == null) return;
148
149 text.setFontSmallInsignia();
150 String name = spec.getWeaponName();
151 text.addParagraph("Gained " + Misc.getWithDGS(quantity) + Strings.X + " " + name + "", Misc.getPositiveHighlightColor());
152 text.highlightInLastPara(Misc.getHighlightColor(), Misc.getWithDGS(quantity) + Strings.X);
153 text.setFontInsignia();
154 }
155 public static void addWeaponLossText(String weaponId, int quantity, TextPanelAPI text) {
156 WeaponSpecAPI spec = Global.getSettings().getWeaponSpec(weaponId);
157 if (spec == null) return;
158
159 text.setFontSmallInsignia();
160 String name = spec.getWeaponName();
161 text.addParagraph("Lost " + Misc.getWithDGS(quantity) + Strings.X + " " + name + "", Misc.getNegativeHighlightColor());
162 text.highlightInLastPara(Misc.getHighlightColor(), Misc.getWithDGS(quantity) + Strings.X);
163 text.setFontInsignia();
164 }
165
166 public static void addItemGainText(SpecialItemData data, int quantity, TextPanelAPI text) {
167 CargoAPI cargo = Global.getFactory().createCargo(true);
168 cargo.addSpecial(data, 1);
169 CargoStackAPI stack = cargo.getStacksCopy().get(0);
170
171 text.setFontSmallInsignia();
172 String name = stack.getDisplayName();
173 if (quantity == 1) {
174 text.addParagraph("Gained " + name + "", Misc.getPositiveHighlightColor());
175 text.highlightInLastPara(Misc.getHighlightColor(), name);
176 } else {
177 text.addParagraph("Gained " + Misc.getWithDGS(quantity) + Strings.X + " " + name + "", Misc.getPositiveHighlightColor());
178 text.highlightInLastPara(Misc.getHighlightColor(), Misc.getWithDGS(quantity) + Strings.X);
179 }
180 text.setFontInsignia();
181 }
182 public static void addItemLossText(SpecialItemData data, int quantity, TextPanelAPI text) {
183 CargoAPI cargo = Global.getFactory().createCargo(true);
184 cargo.addSpecial(data, 1);
185 CargoStackAPI stack = cargo.getStacksCopy().get(0);
186
187 text.setFontSmallInsignia();
188 String name = stack.getDisplayName();
189 if (quantity == 1) {
190 text.addParagraph("Lost " + name + "", Misc.getNegativeHighlightColor());
191 text.highlightInLastPara(Misc.getHighlightColor(), name);
192 } else {
193 text.addParagraph("Lost " + Misc.getWithDGS(quantity) + Strings.X + " " + name + "", Misc.getNegativeHighlightColor());
194 text.highlightInLastPara(Misc.getHighlightColor(), Misc.getWithDGS(quantity) + Strings.X);
195 }
196 text.setFontInsignia();
197 }
198
199 public static void addCommodityGainText(String commodityId, int quantity, TextPanelAPI text) {
200 CommoditySpecAPI spec = Global.getSettings().getCommoditySpec(commodityId);
201 text.setFontSmallInsignia();
202// String units = quantity == 1 ? "unit" : "units";
203// text.addParagraph("Gained " + (int) quantity + " " + units + " of " + spec.getName().toLowerCase() + "", Misc.getPositiveHighlightColor());
204 String name = spec.getLowerCaseName();
205 //boolean special = Commodities.SURVEY_DATA.equals(spec.getDemandClass());
206 //if (!special) name = name.toLowerCase();
207 text.addParagraph("Gained " + Misc.getWithDGS(quantity) + Strings.X + " " + name + "", Misc.getPositiveHighlightColor());
208 text.highlightInLastPara(Misc.getHighlightColor(), Misc.getWithDGS(quantity) + Strings.X);
209 text.setFontInsignia();
210 }
211
212 public static void addCommodityLossText(String commodityId, int quantity, TextPanelAPI text) {
213 CommoditySpecAPI spec = Global.getSettings().getCommoditySpec(commodityId);
214 text.setFontSmallInsignia();
215// String units = quantity == 1 ? "unit" : "units";
216// text.addParagraph("Lost " + (int) quantity + " " + units + " of " + spec.getName().toLowerCase() + "", Misc.getNegativeHighlightColor());
217 String name = spec.getLowerCaseName();
218 //boolean special = Commodities.SURVEY_DATA.equals(spec.getDemandClass());
219 //if (!special) name = name.toLowerCase();
220 text.addParagraph("Lost " + Misc.getWithDGS(quantity) + Strings.X + " " + name + "", Misc.getNegativeHighlightColor());
221 text.highlightInLastPara(Misc.getHighlightColor(), Misc.getWithDGS(quantity) + Strings.X);
222 text.setFontInsignia();
223 }
224
225 public static void addCreditsGainText(int credits, TextPanelAPI text) {
226 text.setFontSmallInsignia();
227 String str = Misc.getWithDGS(credits) + Strings.C;
228 text.addParagraph("Gained " + str + "", Misc.getPositiveHighlightColor());
229 text.highlightInLastPara(Misc.getHighlightColor(), str);
230 text.setFontInsignia();
231
233 CampaignFleetAPI fleet = Global.getSector().getPlayerFleet();
234 MemoryAPI memory = Global.getSector().getCharacterData().getMemoryWithoutUpdate();
235 memory.set("$credits", (int)fleet.getCargo().getCredits().get(), 0);
236 memory.set("$creditsStr", Misc.getWithDGS((int)fleet.getCargo().getCredits().get()), 0);
237 memory.set("$creditsStrC", Misc.getWithDGS((int)fleet.getCargo().getCredits().get()) + Strings.C, 0);
238 }
239 }
240
241 public static void addCreditsLossText(int credits, TextPanelAPI text) {
242 text.setFontSmallInsignia();
243 String str = Misc.getWithDGS(credits) + Strings.C;
244 text.addParagraph("Lost " + str + "", Misc.getNegativeHighlightColor());
245 text.highlightInLastPara(Misc.getHighlightColor(), str);
246 text.setFontInsignia();
247
249 CampaignFleetAPI fleet = Global.getSector().getPlayerFleet();
250 MemoryAPI memory = Global.getSector().getCharacterData().getMemoryWithoutUpdate();
251 memory.set("$credits", (int)fleet.getCargo().getCredits().get(), 0);
252 memory.set("$creditsStr", Misc.getWithDGS((int)fleet.getCargo().getCredits().get()), 0);
253 memory.set("$creditsStrC", Misc.getWithDGS((int)fleet.getCargo().getCredits().get()) + Strings.C, 0);
254 }
255 }
256
257
258 public static void addAbilityGainText(String abilityId, TextPanelAPI text) {
259 AbilitySpecAPI ability = Global.getSettings().getAbilitySpec(abilityId);
260 text.setFontSmallInsignia();
261 String str = "\"" + ability.getName() + "\"";
262 text.addParagraph("Gained ability: " + str + "", Misc.getPositiveHighlightColor());
263 text.highlightInLastPara(Misc.getHighlightColor(), str);
264 text.setFontInsignia();
265 }
266
267
268 public static void addOfficerGainText(PersonAPI officer, TextPanelAPI text) {
269 text.setFontSmallInsignia();
270 String rank = officer.getRank();
271 if (rank != null) {
272 rank = Misc.ucFirst(rank);
273 }
274 String str = officer.getName().getFullName();
275 if (rank != null) str = rank + " " + str;
276 LabelAPI label = text.addParagraph(str + " (level " + officer.getStats().getLevel() + ") has joined your fleet", Misc.getPositiveHighlightColor());
277 label.setHighlightColors(Misc.getHighlightColor(), Misc.getHighlightColor());
278 label.setHighlight(str, "(level " + officer.getStats().getLevel() + ")");
279 //text.highlightInLastPara(Misc.getHighlightColor(), str);
280 text.setFontInsignia();
281 }
282
283 public static void addOfficerLossText(PersonAPI officer, TextPanelAPI text) {
284 text.setFontSmallInsignia();
285 String rank = officer.getRank();
286 if (rank != null) {
287 rank = Misc.ucFirst(rank);
288 }
289 String str = officer.getName().getFullName();
290 if (rank != null) str = rank + " " + str;
291 text.addParagraph(str + " has left your fleet", Misc.getNegativeHighlightColor());
292 text.highlightInLastPara(Misc.getHighlightColor(), str);
293 text.setFontInsignia();
294 }
295
296 public static void addAdminGainText(PersonAPI admin, TextPanelAPI text) {
297 text.setFontSmallInsignia();
298// String rank = admin.getRank();
299// if (rank != null) {
300// rank = Misc.ucFirst(rank);
301// }
302 String rank = "Administrator";
303 String str = admin.getName().getFullName();
304 if (rank != null) str = rank + " " + str;
305 text.addParagraph(str + " has entered your service", Misc.getPositiveHighlightColor());
306 text.highlightInLastPara(Misc.getHighlightColor(), str);
307 text.setFontInsignia();
308 }
309
310
311 public static void addFleetMemberGainText(FleetMemberAPI member, TextPanelAPI text) {
312 text.setFontSmallInsignia();
313 String str = member.getShipName() + ", " + member.getVariant().getHullSpec().getHullNameWithDashClass() + " " + member.getVariant().getHullSpec().getDesignation();
314 text.addParagraph("Acquired " + str + "", Misc.getPositiveHighlightColor());
315 text.highlightInLastPara(Misc.getHighlightColor(), str);
316 text.setFontInsignia();
317 }
318
319 public static void addFleetMemberLossText(FleetMemberAPI member, TextPanelAPI text) {
320 text.setFontSmallInsignia();
321 String str = member.getShipName() + ", " + member.getVariant().getHullSpec().getHullNameWithDashClass() + " " + member.getVariant().getHullSpec().getDesignation();
322 text.addParagraph("Lost " + str + "", Misc.getNegativeHighlightColor());
323 text.highlightInLastPara(Misc.getHighlightColor(), str);
324 text.setFontInsignia();
325 }
326
327 public static void addFleetMemberGainText(ShipVariantAPI variant, TextPanelAPI text) {
328 text.setFontSmallInsignia();
329 String str = variant.getHullSpec().getHullNameWithDashClass() + " " + variant.getHullSpec().getDesignation();
330 text.addParagraph("Acquired " + str + "", Misc.getPositiveHighlightColor());
331 text.highlightInLastPara(Misc.getHighlightColor(), str);
332 text.setFontInsignia();
333 }
334
335 public static void addCRLossText(FleetMemberAPI member, TextPanelAPI text, float crLoss) {
336 text.setFontSmallInsignia();
337 String str = member.getShipName() + ", " + member.getVariant().getHullSpec().getHullNameWithDashClass() + " " + member.getVariant().getHullSpec().getDesignation();
338 String cr = Math.round(crLoss * 100f) + "%";
339 text.addPara("%s has lost %s combat readiness", Misc.getNegativeHighlightColor(), Misc.getHighlightColor(),
340 str, cr);
341 text.highlightInLastPara(Misc.getHighlightColor(), str);
342 text.setFontInsignia();
343 }
344}
345
346
347
static SettingsAPI getSettings()
Definition Global.java:51
static FactoryAPI getFactory()
Definition Global.java:35
static GameState getCurrentState()
Definition Global.java:21
static SectorAPI getSector()
Definition Global.java:59
static void addAdminGainText(PersonAPI admin, TextPanelAPI text)
static void addWeaponLossText(String weaponId, int quantity, TextPanelAPI text)
static void addFleetMemberGainText(ShipVariantAPI variant, TextPanelAPI text)
static void addCommodityLossText(String commodityId, int quantity, TextPanelAPI text)
static void addFighterLossText(String wingId, int quantity, TextPanelAPI text)
static void addCreditsGainText(int credits, TextPanelAPI text)
static void addFleetMemberLossText(FleetMemberAPI member, TextPanelAPI text)
static void addAbilityGainText(String abilityId, TextPanelAPI text)
static void addStackGainText(CargoStackAPI stack, TextPanelAPI text, boolean lowerCase)
static void addOfficerGainText(PersonAPI officer, TextPanelAPI text)
static void addStackGainText(CargoStackAPI stack, TextPanelAPI text)
static void addItemGainText(SpecialItemData data, int quantity, TextPanelAPI text)
static void addCommodityGainText(String commodityId, int quantity, TextPanelAPI text)
static void addItemLossText(SpecialItemData data, int quantity, TextPanelAPI text)
static void addWeaponGainText(String weaponId, int quantity, TextPanelAPI text)
static void addOfficerLossText(PersonAPI officer, TextPanelAPI text)
static void addCreditsLossText(int credits, TextPanelAPI text)
boolean execute(String ruleId, InteractionDialogAPI dialog, List< Token > params, Map< String, MemoryAPI > memoryMap)
static void addFleetMemberGainText(FleetMemberAPI member, TextPanelAPI text)
static void addCRLossText(FleetMemberAPI member, TextPanelAPI text, float crLoss)
static void addFighterGainText(String wingId, int quantity, TextPanelAPI text)
CargoAPI createCargo(boolean unlimitedStacks)
AbilitySpecAPI getAbilitySpec(String abilityId)
CommoditySpecAPI getCommoditySpec(String commodityId)
FighterWingSpecAPI getFighterWingSpec(String wingId)
WeaponSpecAPI getWeaponSpec(String weaponId)