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")) {
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)) {
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
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;
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());
120 text.setFontInsignia();
121 }
122
123 public static void addStackLossText(CargoStackAPI stack, TextPanelAPI text) {
124 addStackLossText(stack, text, false);
125 }
126 public static void addStackLossText(CargoStackAPI stack, TextPanelAPI text, boolean lowerCase) {
127 if (stack.getSize() < 1) return;
129 String name = stack.getDisplayName();
130 if (lowerCase) {
131 name = name.toLowerCase();
132 }
133 int quantity = (int) stack.getSize();
134 text.addParagraph("Lost " + Misc.getWithDGS(quantity) + Strings.X + " " + name + "", Misc.getNegativeHighlightColor());
136 text.setFontInsignia();
137 }
138
139 public static void addFighterGainText(String wingId, int quantity, TextPanelAPI text) {
141 if (spec == null) return;
142
144 String name = spec.getWingName();
145 text.addParagraph("Gained " + Misc.getWithDGS(quantity) + Strings.X + " " + name + "", Misc.getPositiveHighlightColor());
147 text.setFontInsignia();
148 }
149 public static void addFighterLossText(String wingId, int quantity, TextPanelAPI text) {
151 if (spec == null) return;
152
154 String name = spec.getWingName();
155 text.addParagraph("Lost " + Misc.getWithDGS(quantity) + Strings.X + " " + name + "", Misc.getNegativeHighlightColor());
157 text.setFontInsignia();
158 }
159
160 public static void addWeaponGainText(String weaponId, int quantity, TextPanelAPI text) {
162 if (spec == null) return;
163
165 String name = spec.getWeaponName();
166 text.addParagraph("Gained " + Misc.getWithDGS(quantity) + Strings.X + " " + name + "", Misc.getPositiveHighlightColor());
168 text.setFontInsignia();
169 }
170 public static void addWeaponLossText(String weaponId, int quantity, TextPanelAPI text) {
172 if (spec == null) return;
173
175 String name = spec.getWeaponName();
176 text.addParagraph("Lost " + Misc.getWithDGS(quantity) + Strings.X + " " + name + "", Misc.getNegativeHighlightColor());
178 text.setFontInsignia();
179 }
180
181 public static void addItemGainText(SpecialItemData data, int quantity, TextPanelAPI text) {
182 CargoAPI cargo = Global.getFactory().createCargo(true);
183 cargo.addSpecial(data, 1);
184 CargoStackAPI stack = cargo.getStacksCopy().get(0);
185
187 String name = stack.getDisplayName();
188 if (quantity == 1) {
189 text.addParagraph("Gained " + name + "", Misc.getPositiveHighlightColor());
191 } else {
192 text.addParagraph("Gained " + Misc.getWithDGS(quantity) + Strings.X + " " + name + "", Misc.getPositiveHighlightColor());
194 }
195 text.setFontInsignia();
196 }
197 public static void addItemLossText(SpecialItemData data, int quantity, TextPanelAPI text) {
198 CargoAPI cargo = Global.getFactory().createCargo(true);
199 cargo.addSpecial(data, 1);
200 CargoStackAPI stack = cargo.getStacksCopy().get(0);
201
203 String name = stack.getDisplayName();
204 if (quantity == 1) {
205 text.addParagraph("Lost " + name + "", Misc.getNegativeHighlightColor());
207 } else {
208 text.addParagraph("Lost " + Misc.getWithDGS(quantity) + Strings.X + " " + name + "", Misc.getNegativeHighlightColor());
210 }
211 text.setFontInsignia();
212 }
213
214 public static void addCommodityGainText(String commodityId, int quantity, TextPanelAPI text) {
217// String units = quantity == 1 ? "unit" : "units";
218// text.addParagraph("Gained " + (int) quantity + " " + units + " of " + spec.getName().toLowerCase() + "", Misc.getPositiveHighlightColor());
219 String name = spec.getLowerCaseName();
220 //boolean special = Commodities.SURVEY_DATA.equals(spec.getDemandClass());
221 //if (!special) name = name.toLowerCase();
222 text.addParagraph("Gained " + Misc.getWithDGS(quantity) + Strings.X + " " + name + "", Misc.getPositiveHighlightColor());
224 text.setFontInsignia();
225 }
226
227 public static void addCommodityLossText(String commodityId, int quantity, TextPanelAPI text) {
230// String units = quantity == 1 ? "unit" : "units";
231// text.addParagraph("Lost " + (int) quantity + " " + units + " of " + spec.getName().toLowerCase() + "", Misc.getNegativeHighlightColor());
232 String name = spec.getLowerCaseName();
233 //boolean special = Commodities.SURVEY_DATA.equals(spec.getDemandClass());
234 //if (!special) name = name.toLowerCase();
235 text.addParagraph("Lost " + Misc.getWithDGS(quantity) + Strings.X + " " + name + "", Misc.getNegativeHighlightColor());
237 text.setFontInsignia();
238 }
239
240 public static void addCreditsGainText(int credits, TextPanelAPI text) {
242 String str = Misc.getWithDGS(credits) + Strings.C;
243 text.addParagraph("Gained " + str + "", Misc.getPositiveHighlightColor());
245 text.setFontInsignia();
246
250 memory.set("$credits", (int)fleet.getCargo().getCredits().get(), 0);
251 memory.set("$creditsStr", Misc.getWithDGS((int)fleet.getCargo().getCredits().get()), 0);
252 memory.set("$creditsStrC", Misc.getWithDGS((int)fleet.getCargo().getCredits().get()) + Strings.C, 0);
253 }
254 }
255
256 public static void addCreditsLossText(int credits, TextPanelAPI text) {
258 String str = Misc.getWithDGS(credits) + Strings.C;
259 text.addParagraph("Lost " + str + "", Misc.getNegativeHighlightColor());
261 text.setFontInsignia();
262
266 memory.set("$credits", (int)fleet.getCargo().getCredits().get(), 0);
267 memory.set("$creditsStr", Misc.getWithDGS((int)fleet.getCargo().getCredits().get()), 0);
268 memory.set("$creditsStrC", Misc.getWithDGS((int)fleet.getCargo().getCredits().get()) + Strings.C, 0);
269 }
270 }
271
272
273 public static void addAbilityGainText(String abilityId, TextPanelAPI text) {
274 AbilitySpecAPI ability = Global.getSettings().getAbilitySpec(abilityId);
276 String str = "\"" + ability.getName() + "\"";
277 text.addParagraph("Gained ability: " + str + "", Misc.getPositiveHighlightColor());
279 text.setFontInsignia();
280 }
281
282
283 public static void addOfficerGainText(PersonAPI officer, TextPanelAPI text) {
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 LabelAPI label = text.addParagraph(str + " (level " + officer.getStats().getLevel() + ") has joined your fleet", Misc.getPositiveHighlightColor());
293 label.setHighlight(str, "(level " + officer.getStats().getLevel() + ")");
294 //text.highlightInLastPara(Misc.getHighlightColor(), str);
295 text.setFontInsignia();
296 }
297
298 public static void addOfficerLossText(PersonAPI officer, TextPanelAPI text) {
300 String rank = officer.getRank();
301 if (rank != null) {
302 rank = Misc.ucFirst(rank);
303 }
304 String str = officer.getName().getFullName();
305 if (rank != null) str = rank + " " + str;
306 text.addParagraph(str + " has left your fleet", Misc.getNegativeHighlightColor());
308 text.setFontInsignia();
309 }
310
311 public static void addAdminGainText(PersonAPI admin, TextPanelAPI text) {
313// String rank = admin.getRank();
314// if (rank != null) {
315// rank = Misc.ucFirst(rank);
316// }
317 String rank = "Administrator";
318 String str = admin.getName().getFullName();
319 if (rank != null) str = rank + " " + str;
320 text.addParagraph(str + " has entered your service", Misc.getPositiveHighlightColor());
322 text.setFontInsignia();
323 }
324
325
326 public static void addFleetMemberGainText(FleetMemberAPI member, TextPanelAPI text) {
328 String str = member.getShipName() + ", " + member.getVariant().getHullSpec().getHullNameWithDashClass() + " " + member.getVariant().getHullSpec().getDesignation();
329 text.addParagraph("Acquired " + str + "", Misc.getPositiveHighlightColor());
331 text.setFontInsignia();
332 }
333
334 public static void addFleetMemberLossText(FleetMemberAPI member, TextPanelAPI text) {
336 String str = member.getShipName() + ", " + member.getVariant().getHullSpec().getHullNameWithDashClass() + " " + member.getVariant().getHullSpec().getDesignation();
337 text.addParagraph("Lost " + str + "", Misc.getNegativeHighlightColor());
339 text.setFontInsignia();
340 }
341
342 public static void addFleetMemberGainText(ShipVariantAPI variant, TextPanelAPI text) {
344 String str = variant.getHullSpec().getHullNameWithDashClass() + " " + variant.getHullSpec().getDesignation();
345 text.addParagraph("Acquired " + str + "", Misc.getPositiveHighlightColor());
347 text.setFontInsignia();
348 }
349
350 public static void addCRLossText(FleetMemberAPI member, TextPanelAPI text, float crLoss) {
352 String str = member.getShipName() + ", " + member.getVariant().getHullSpec().getHullNameWithDashClass() + " " + member.getVariant().getHullSpec().getDesignation();
353 String cr = Math.round(crLoss * 100f) + "%";
354 text.addPara("%s has lost %s combat readiness", Misc.getNegativeHighlightColor(), Misc.getHighlightColor(),
355 str, cr);
357 text.setFontInsignia();
358 }
359}
360
361
362
static SettingsAPI getSettings()
Definition Global.java:57
static FactoryAPI getFactory()
Definition Global.java:41
static GameState getCurrentState()
Definition Global.java:27
static SectorAPI getSector()
Definition Global.java:65
static void addAdminGainText(PersonAPI admin, TextPanelAPI text)
static void addStackLossText(CargoStackAPI stack, 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 addStackLossText(CargoStackAPI stack, TextPanelAPI text, boolean lowerCase)
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)
static String getWithDGS(float num)
Definition Misc.java:1381
static String ucFirst(String str)
Definition Misc.java:559
static Color getNegativeHighlightColor()
Definition Misc.java:802
static Color getHighlightColor()
Definition Misc.java:792
static Color getPositiveHighlightColor()
Definition Misc.java:822
CargoAPI createCargo(boolean unlimitedStacks)
AbilitySpecAPI getAbilitySpec(String abilityId)
CommoditySpecAPI getCommoditySpec(String commodityId)
FighterWingSpecAPI getFighterWingSpec(String wingId)
WeaponSpecAPI getWeaponSpec(String weaponId)
void addSpecial(SpecialItemData data, float quantity)
void removeCommodity(String id, float quantity)
List< CargoStackAPI > getStacksCopy()
void addCommodity(String commodityId, float quantity)
void highlightInLastPara(Color color, String ...strings)
void set(String key, Object value)
MutableCharacterStatsAPI getStats()
void setHighlight(int start, int end)
void setHighlightColors(Color ... colors)