001package com.fs.starfarer.api.impl.campaign.rulecmd;
002
003import java.util.List;
004import java.util.Map;
005
006import com.fs.starfarer.api.GameState;
007import com.fs.starfarer.api.Global;
008import com.fs.starfarer.api.campaign.CampaignFleetAPI;
009import com.fs.starfarer.api.campaign.CargoAPI;
010import com.fs.starfarer.api.campaign.CargoStackAPI;
011import com.fs.starfarer.api.campaign.InteractionDialogAPI;
012import com.fs.starfarer.api.campaign.SpecialItemData;
013import com.fs.starfarer.api.campaign.TextPanelAPI;
014import com.fs.starfarer.api.campaign.econ.CommoditySpecAPI;
015import com.fs.starfarer.api.campaign.rules.MemoryAPI;
016import com.fs.starfarer.api.characters.PersonAPI;
017import com.fs.starfarer.api.combat.ShipVariantAPI;
018import com.fs.starfarer.api.fleet.FleetMemberAPI;
019import com.fs.starfarer.api.impl.campaign.ids.Strings;
020import com.fs.starfarer.api.loading.AbilitySpecAPI;
021import com.fs.starfarer.api.loading.FighterWingSpecAPI;
022import com.fs.starfarer.api.loading.WeaponSpecAPI;
023import com.fs.starfarer.api.ui.LabelAPI;
024import com.fs.starfarer.api.util.Misc;
025import com.fs.starfarer.api.util.Misc.Token;
026import com.fs.starfarer.api.util.MutableValue;
027
028/**
029 *      AddRemoveCommodity <commodity id> <quantity> <withText>
030 */
031public class AddRemoveCommodity extends BaseCommandPlugin {
032
033        public boolean execute(String ruleId, InteractionDialogAPI dialog, List<Token> params, Map<String, MemoryAPI> memoryMap) {
034                if (dialog == null) return false;
035                
036                String commodityId = params.get(0).getString(memoryMap);
037                float quantity = 0;
038                int next = 2;
039                if (params.get(1).isOperator()) {
040                        quantity = -1 * params.get(2).getFloat(memoryMap);
041                        next = 3;
042                } else {
043                        quantity = params.get(1).getFloat(memoryMap);
044                }
045                boolean withText = Math.abs(quantity) >= 1;
046                if (dialog != null && params.size() >= next + 1) {
047                        withText = params.get(next).getBoolean(memoryMap) && withText;
048                }
049                
050                if (commodityId.equals("credits")) {
051                        MutableValue credits = Global.getSector().getPlayerFleet().getCargo().getCredits();
052                        if (quantity > 0) {
053                                credits.add(quantity);
054                                if (withText) {
055                                        addCreditsGainText((int) quantity, dialog.getTextPanel());
056                                }
057                        } else {
058                                credits.subtract(Math.abs(quantity));
059                                if (credits.get() < 0) credits.set(0);
060                                if (withText) {
061                                        addCreditsLossText((int) Math.abs(quantity), dialog.getTextPanel());
062                                }
063                        }
064                } else {
065                        if (quantity > 0) {
066                                Global.getSector().getPlayerFleet().getCargo().addCommodity(commodityId, quantity);
067                                if (withText) {
068                                        addCommodityGainText(commodityId, (int) quantity, dialog.getTextPanel());
069                                }
070                        } else {
071                                Global.getSector().getPlayerFleet().getCargo().removeCommodity(commodityId, Math.abs(quantity));
072                                if (withText) {
073                                        addCommodityLossText(commodityId, (int) Math.abs(quantity), dialog.getTextPanel());
074                                }
075                        }
076                }
077                
078                
079                if (!"credits".equals(commodityId)) {
080                        // update $supplies, $fuel, etc if relevant
081                        updatePlayerMemoryQuantity(commodityId);
082                }
083                
084                return true;
085        }
086        
087        public static void updatePlayerMemoryQuantity(String commodityId) {
088                if ("credits".equals(commodityId)) {
089                        CampaignFleetAPI fleet = Global.getSector().getPlayerFleet();
090                        MemoryAPI memory = Global.getSector().getCharacterData().getMemoryWithoutUpdate();
091                        memory.set("$credits", (int)fleet.getCargo().getCredits().get(), 0);
092                        memory.set("$creditsStr", Misc.getWithDGS((int)fleet.getCargo().getCredits().get()), 0);
093                        memory.set("$creditsStrC", Misc.getWithDGS((int)fleet.getCargo().getCredits().get()) + Strings.C, 0);
094                        return;
095                }
096                
097                MemoryAPI memory = Global.getSector().getCharacterData().getMemoryWithoutUpdate();
098                String key = "$" + commodityId;
099                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        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;
128                text.setFontSmallInsignia();
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());
135                text.highlightInLastPara(Misc.getHighlightColor(), Misc.getWithDGS(quantity) + Strings.X);
136                text.setFontInsignia();
137        }
138        
139        public static void addFighterGainText(String wingId, int quantity, TextPanelAPI text) {
140                FighterWingSpecAPI spec = Global.getSettings().getFighterWingSpec(wingId);
141                if (spec == null) return;
142                
143                text.setFontSmallInsignia();
144                String name = spec.getWingName();
145                text.addParagraph("Gained " + Misc.getWithDGS(quantity) + Strings.X + " " + name + "", Misc.getPositiveHighlightColor());
146                text.highlightInLastPara(Misc.getHighlightColor(), Misc.getWithDGS(quantity) + Strings.X);
147                text.setFontInsignia();
148        }
149        public static void addFighterLossText(String wingId, int quantity, TextPanelAPI text) {
150                FighterWingSpecAPI spec = Global.getSettings().getFighterWingSpec(wingId);
151                if (spec == null) return;
152                
153                text.setFontSmallInsignia();
154                String name = spec.getWingName();
155                text.addParagraph("Lost " + Misc.getWithDGS(quantity) + Strings.X + " " + name + "", Misc.getNegativeHighlightColor());
156                text.highlightInLastPara(Misc.getHighlightColor(), Misc.getWithDGS(quantity) + Strings.X);
157                text.setFontInsignia();
158        }
159        
160        public static void addWeaponGainText(String weaponId, int quantity, TextPanelAPI text) {
161                WeaponSpecAPI spec = Global.getSettings().getWeaponSpec(weaponId);
162                if (spec == null) return;
163                
164                text.setFontSmallInsignia();
165                String name = spec.getWeaponName();
166                text.addParagraph("Gained " + Misc.getWithDGS(quantity) + Strings.X + " " + name + "", Misc.getPositiveHighlightColor());
167                text.highlightInLastPara(Misc.getHighlightColor(), Misc.getWithDGS(quantity) + Strings.X);
168                text.setFontInsignia();
169        }
170        public static void addWeaponLossText(String weaponId, int quantity, TextPanelAPI text) {
171                WeaponSpecAPI spec = Global.getSettings().getWeaponSpec(weaponId);
172                if (spec == null) return;
173                
174                text.setFontSmallInsignia();
175                String name = spec.getWeaponName();
176                text.addParagraph("Lost " + Misc.getWithDGS(quantity) + Strings.X + " " + name + "", Misc.getNegativeHighlightColor());
177                text.highlightInLastPara(Misc.getHighlightColor(), Misc.getWithDGS(quantity) + Strings.X);
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                
186                text.setFontSmallInsignia();
187                String name = stack.getDisplayName();
188                if (quantity == 1) {
189                        text.addParagraph("Gained " + name + "", Misc.getPositiveHighlightColor());
190                        text.highlightInLastPara(Misc.getHighlightColor(), name);
191                } else {
192                        text.addParagraph("Gained " + Misc.getWithDGS(quantity) + Strings.X + " " + name + "", Misc.getPositiveHighlightColor());
193                        text.highlightInLastPara(Misc.getHighlightColor(), Misc.getWithDGS(quantity) + Strings.X);
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                
202                text.setFontSmallInsignia();
203                String name = stack.getDisplayName();
204                if (quantity == 1) {
205                        text.addParagraph("Lost " + name + "", Misc.getNegativeHighlightColor());
206                        text.highlightInLastPara(Misc.getHighlightColor(), name);
207                } else {
208                        text.addParagraph("Lost " + Misc.getWithDGS(quantity) + Strings.X + " " + name + "", Misc.getNegativeHighlightColor());
209                        text.highlightInLastPara(Misc.getHighlightColor(), Misc.getWithDGS(quantity) + Strings.X);
210                }
211                text.setFontInsignia();
212        }
213        
214        public static void addCommodityGainText(String commodityId, int quantity, TextPanelAPI text) {
215                CommoditySpecAPI spec = Global.getSettings().getCommoditySpec(commodityId);
216                text.setFontSmallInsignia();
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());
223                text.highlightInLastPara(Misc.getHighlightColor(), Misc.getWithDGS(quantity) + Strings.X);
224                text.setFontInsignia();
225        }
226        
227        public static void addCommodityLossText(String commodityId, int quantity, TextPanelAPI text) {
228                CommoditySpecAPI spec = Global.getSettings().getCommoditySpec(commodityId);
229                text.setFontSmallInsignia();
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());
236                text.highlightInLastPara(Misc.getHighlightColor(), Misc.getWithDGS(quantity) + Strings.X);
237                text.setFontInsignia();
238        }
239        
240        public static void addCreditsGainText(int credits, TextPanelAPI text) {
241                text.setFontSmallInsignia();
242                String str = Misc.getWithDGS(credits) + Strings.C;
243                text.addParagraph("Gained " + str + "", Misc.getPositiveHighlightColor());
244                text.highlightInLastPara(Misc.getHighlightColor(), str);
245                text.setFontInsignia();
246                
247                if (Global.getCurrentState() == GameState.CAMPAIGN) {
248                        CampaignFleetAPI fleet = Global.getSector().getPlayerFleet();
249                        MemoryAPI memory = Global.getSector().getCharacterData().getMemoryWithoutUpdate();
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) {
257                text.setFontSmallInsignia();
258                String str = Misc.getWithDGS(credits) + Strings.C;
259                text.addParagraph("Lost " + str + "", Misc.getNegativeHighlightColor());
260                text.highlightInLastPara(Misc.getHighlightColor(), str);
261                text.setFontInsignia();
262                
263                if (Global.getCurrentState() == GameState.CAMPAIGN) {
264                        CampaignFleetAPI fleet = Global.getSector().getPlayerFleet();
265                        MemoryAPI memory = Global.getSector().getCharacterData().getMemoryWithoutUpdate();
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);
275                text.setFontSmallInsignia();
276                String str = "\"" + ability.getName() + "\"";
277                text.addParagraph("Gained ability: " + str + "", Misc.getPositiveHighlightColor());
278                text.highlightInLastPara(Misc.getHighlightColor(), str);
279                text.setFontInsignia();
280        }
281        
282        
283        public static void addOfficerGainText(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                LabelAPI label = text.addParagraph(str + " (level " + officer.getStats().getLevel() + ") has joined your fleet", Misc.getPositiveHighlightColor());
292                label.setHighlightColors(Misc.getHighlightColor(), Misc.getHighlightColor());
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) {
299                text.setFontSmallInsignia();
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());
307                text.highlightInLastPara(Misc.getHighlightColor(), str);
308                text.setFontInsignia();
309        }
310        
311        public static void addAdminGainText(PersonAPI admin, TextPanelAPI text) {
312                text.setFontSmallInsignia();
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());
321                text.highlightInLastPara(Misc.getHighlightColor(), str);
322                text.setFontInsignia();
323        }
324        
325        
326        public static void addFleetMemberGainText(FleetMemberAPI member, TextPanelAPI text) {
327                text.setFontSmallInsignia();
328                String str = member.getShipName() + ", " + member.getVariant().getHullSpec().getHullNameWithDashClass() + " " + member.getVariant().getHullSpec().getDesignation(); 
329                text.addParagraph("Acquired " + str + "", Misc.getPositiveHighlightColor());
330                text.highlightInLastPara(Misc.getHighlightColor(), str);
331                text.setFontInsignia();
332        }
333        
334        public static void addFleetMemberLossText(FleetMemberAPI member, TextPanelAPI text) {
335                text.setFontSmallInsignia();
336                String str = member.getShipName() + ", " + member.getVariant().getHullSpec().getHullNameWithDashClass() + " " + member.getVariant().getHullSpec().getDesignation(); 
337                text.addParagraph("Lost " + str + "", Misc.getNegativeHighlightColor());
338                text.highlightInLastPara(Misc.getHighlightColor(), str);
339                text.setFontInsignia();
340        }
341        
342        public static void addFleetMemberGainText(ShipVariantAPI variant, TextPanelAPI text) {
343                text.setFontSmallInsignia();
344                String str = variant.getHullSpec().getHullNameWithDashClass() + " " + variant.getHullSpec().getDesignation(); 
345                text.addParagraph("Acquired " + str + "", Misc.getPositiveHighlightColor());
346                text.highlightInLastPara(Misc.getHighlightColor(), str);
347                text.setFontInsignia();
348        }
349        
350        public static void addCRLossText(FleetMemberAPI member, TextPanelAPI text, float crLoss) {
351                text.setFontSmallInsignia();
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);
356                text.highlightInLastPara(Misc.getHighlightColor(), str);
357                text.setFontInsignia();
358        }
359}
360
361
362