001package com.fs.starfarer.api.impl.campaign.rulecmd;
002
003import java.util.List;
004import java.util.Map;
005
006import com.fs.starfarer.api.campaign.InteractionDialogAPI;
007import com.fs.starfarer.api.campaign.RuleBasedDialog;
008import com.fs.starfarer.api.campaign.rules.MemKeys;
009import com.fs.starfarer.api.campaign.rules.MemoryAPI;
010import com.fs.starfarer.api.impl.campaign.FleetInteractionDialogPluginImpl;
011import com.fs.starfarer.api.impl.campaign.ids.MemFlags;
012import com.fs.starfarer.api.util.Misc;
013import com.fs.starfarer.api.util.Misc.Token;
014
015public class EndConversation extends BaseCommandPlugin {
016
017        public boolean execute(String ruleId, InteractionDialogAPI dialog, List<Token> params, Map<String, MemoryAPI> memoryMap) {
018                FleetInteractionDialogPluginImpl.inConversation = false;
019                
020                boolean doNotFire = false;
021                boolean withContinue = true;
022                if (params.size() > 0) {
023                        String str = params.get(0).getString(memoryMap);
024                        if (str != null && str.equals("DO_NOT_FIRE")) {
025                                doNotFire = true;
026                        }
027                        if (str != null && str.equals("NO_CONTINUE")) {
028                                withContinue = false;
029                        }
030                }
031                
032                MemoryAPI memory = memoryMap.get(MemKeys.LOCAL);
033                if (memory != null && memory.getBoolean("$doNotFireOnConvEnd")) {
034                        doNotFire = true;
035                }
036                
037                if (!withContinue) {
038                        new ShowDefaultVisual().execute(null, dialog, Misc.tokenize(""), memoryMap);
039                }
040                
041                if (dialog.getPlugin() instanceof RuleBasedDialog) {
042                        dialog.getInteractionTarget().setActivePerson(null);
043                        ((RuleBasedDialog) dialog.getPlugin()).notifyActivePersonChanged();
044                        
045                        if (!doNotFire) {
046                                if (dialog.getPlugin() instanceof FleetInteractionDialogPluginImpl) {
047                                        //((FleetInteractionDialogPluginImpl)dialog.getPlugin()).optionSelected(null, OptionId.INIT);
048                                        ((RuleBasedDialog) dialog.getPlugin()).reinit(withContinue);
049                                } else {
050                                        MemoryAPI local = memoryMap.get(MemKeys.LOCAL);
051                                        MemoryAPI marketMem = memoryMap.get(MemKeys.MARKET);
052                                        boolean custom = marketMem != null && marketMem.getBoolean(MemFlags.MARKET_HAS_CUSTOM_INTERACTION_OPTIONS);
053                                        if (!custom && local != null && local.getBoolean("$hasMarket") && !local.contains("$menuState")) {
054                                                FireBest.fire(null, dialog, memoryMap, "MarketPostOpen");
055                                        } else {
056                                                FireAll.fire(null, dialog, memoryMap, "PopulateOptions");
057                                        }
058                                }
059                        }
060                }
061                return true;
062        }
063
064}