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.PersistentUIDataAPI.AbilitySlotAPI;
009import com.fs.starfarer.api.campaign.PersistentUIDataAPI.AbilitySlotsAPI;
010import com.fs.starfarer.api.campaign.rules.MemoryAPI;
011import com.fs.starfarer.api.util.Misc.Token;
012
013/**
014 *      AddAbility <ability id> <optional default slot>
015 */
016public class AddAbility extends BaseCommandPlugin {
017
018        public boolean execute(String ruleId, InteractionDialogAPI dialog, List<Token> params, Map<String, MemoryAPI> memoryMap) {
019                //if (dialog == null) return false;
020                
021                // if the player already had the ability, calling this command ensures they don't lose it
022                // if they spec out of the skill that granted it, but this command will be "quiet"
023                
024                String abilityId = params.get(0).getString(memoryMap);
025                if (abilityId == null) return false;
026                
027                boolean hadAbilityAlready = Global.getSector().getPlayerFleet().hasAbility(abilityId);
028                
029                boolean found = false;
030                AbilitySlotsAPI slots = Global.getSector().getUIData().getAbilitySlotsAPI();
031                int curr = slots.getCurrBarIndex();
032                OUTER: for (int i = 0; i < 5; i++) {
033                        slots.setCurrBarIndex(i);
034                        for (AbilitySlotAPI slot : slots.getCurrSlotsCopy()) {
035                                if (abilityId.equals(slot.getAbilityId())) {
036                                        found = true;
037                                        break OUTER;
038                                }
039                        }
040                        slots.setCurrBarIndex(curr);
041                }
042                if (!found) {
043                        hadAbilityAlready = false;
044                }
045                
046                Global.getSector().getCharacterData().addAbility(abilityId);
047                //Global.getSector().getUIData().getAbilitySlotsAPI().getCurrSlotsCopy().get(8).getAbilityId()
048                
049                if (!hadAbilityAlready) {
050                        boolean assignedToSlot = false;
051                        boolean doNotAssign = false;
052                        if (params.size() >= 2) {
053                                int slotIndex = (int) params.get(1).getFloat(memoryMap);
054                                if (slotIndex < 0) {
055                                        doNotAssign = true;
056                                } else {
057                                        slots.setCurrBarIndex(0);
058                                        AbilitySlotAPI slot = slots.getCurrSlotsCopy().get(slotIndex);
059                                        if (slot.getAbilityId() == null) {
060                                                slot.setAbilityId(abilityId);
061                                                assignedToSlot = true;
062                                        }
063                                }
064                        }
065                        
066                        if (!assignedToSlot && !doNotAssign) {
067                                int currBarIndex = slots.getCurrBarIndex();
068                                OUTER: for (int i = 0; i < 5; i++) {
069                                        slots.setCurrBarIndex(i);
070                                        for (int j = 0; j < 10; j++) {
071                                                AbilitySlotAPI slot = slots.getCurrSlotsCopy().get(j);
072                                                if (slot.getAbilityId() == null) {
073                                                        slot.setAbilityId(abilityId);
074                                                        assignedToSlot = true;
075                                                        break OUTER;
076                                                }               
077                                        }
078                                }
079                                slots.setCurrBarIndex(currBarIndex);
080                        }
081                        
082                        Global.getSector().getCharacterData().getMemoryWithoutUpdate().set("$ability:" + abilityId, true, 0);
083                        if (dialog != null) {
084                                AddRemoveCommodity.addAbilityGainText(abilityId, dialog.getTextPanel());
085                        }
086                }
087                
088                return true;
089        }
090        
091        
092}
093
094
095