001package com.fs.starfarer.api.impl.campaign.rulecmd;
002
003import java.util.List;
004import java.util.Map;
005
006import com.fs.starfarer.api.Global;
007import com.fs.starfarer.api.campaign.InteractionDialogAPI;
008import com.fs.starfarer.api.campaign.rules.MemoryAPI;
009import com.fs.starfarer.api.util.Misc.Token;
010import com.fs.starfarer.api.util.MutableValue;
011
012/**
013 *      AddRemoveCommodity <commodity id> <quantity> <withText>
014 */
015public class RemoveCommodity extends BaseCommandPlugin {
016
017        public boolean execute(String ruleId, InteractionDialogAPI dialog, List<Token> params, Map<String, MemoryAPI> memoryMap) {
018                if (dialog == null) return false;
019                
020                String commodityId = params.get(0).getString(memoryMap);
021                float quantity = 0;
022                int next = 2;
023                if (params.get(1).isOperator()) {
024                        quantity = -1 * params.get(2).getFloat(memoryMap);
025                        next = 3;
026                } else {
027                        quantity = params.get(1).getFloat(memoryMap);
028                }
029                
030                quantity = -quantity;
031                
032                boolean withText = Math.abs(quantity) >= 1;
033                if (dialog != null && params.size() >= next + 1) {
034                        withText = params.get(next).getBoolean(memoryMap) && withText;
035                }
036                
037                if (commodityId.equals("credits")) {
038                        MutableValue credits = Global.getSector().getPlayerFleet().getCargo().getCredits();
039                        if (quantity > 0) {
040                                credits.add(quantity);
041                                if (withText) {
042                                        AddRemoveCommodity.addCreditsGainText((int) quantity, dialog.getTextPanel());
043                                }
044                        } else {
045                                credits.subtract(Math.abs(quantity));
046                                if (credits.get() < 0) credits.set(0);
047                                if (withText) {
048                                        AddRemoveCommodity.addCreditsLossText((int) Math.abs(quantity), dialog.getTextPanel());
049                                }
050                        }
051                } else {
052//                      if (quantity < 0) {
053//                              quantity = -1f * Math.max(Math.abs(quantity), Global.getSector().getPlayerFleet().getCargo().getCommodityQuantity(commodityId));
054//                      }
055                        if (quantity > 0) {
056                                Global.getSector().getPlayerFleet().getCargo().addCommodity(commodityId, quantity);
057                                if (withText) {
058                                        AddRemoveCommodity.addCommodityGainText(commodityId, (int) quantity, dialog.getTextPanel());
059                                }
060                        } else {
061                                Global.getSector().getPlayerFleet().getCargo().removeCommodity(commodityId, Math.abs(quantity));
062                                if (withText) {
063                                        AddRemoveCommodity.addCommodityLossText(commodityId, (int) Math.abs(quantity), dialog.getTextPanel());
064                                }
065                        }
066                }
067                
068                if (!"credits".equals(commodityId)) {
069                        // update $supplies, $fuel, etc if relevant
070                        AddRemoveCommodity.updatePlayerMemoryQuantity(commodityId);
071                }
072                
073                return true;
074        }
075        
076
077}
078
079
080