Starsector API
Loading...
Searching...
No Matches
RuleBasedInteractionDialogPluginImpl.java
Go to the documentation of this file.
1package com.fs.starfarer.api.impl.campaign;
2
3import java.awt.Color;
4import java.util.HashMap;
5import java.util.Map;
6
7import com.fs.starfarer.api.Global;
8import com.fs.starfarer.api.campaign.CampaignFleetAPI;
9import com.fs.starfarer.api.campaign.InteractionDialogAPI;
10import com.fs.starfarer.api.campaign.InteractionDialogPlugin;
11import com.fs.starfarer.api.campaign.OptionPanelAPI;
12import com.fs.starfarer.api.campaign.RuleBasedDialog;
13import com.fs.starfarer.api.campaign.TextPanelAPI;
14import com.fs.starfarer.api.campaign.VisualPanelAPI;
15import com.fs.starfarer.api.campaign.econ.MarketAPI;
16import com.fs.starfarer.api.campaign.events.CampaignEventPlugin;
17import com.fs.starfarer.api.campaign.rules.MemKeys;
18import com.fs.starfarer.api.campaign.rules.MemoryAPI;
19import com.fs.starfarer.api.campaign.rules.RulesAPI;
20import com.fs.starfarer.api.characters.PersonAPI;
21import com.fs.starfarer.api.combat.EngagementResultAPI;
22import com.fs.starfarer.api.impl.campaign.ids.MemFlags;
23import com.fs.starfarer.api.impl.campaign.rulecmd.DismissDialog;
24import com.fs.starfarer.api.impl.campaign.rulecmd.DumpMemory;
25import com.fs.starfarer.api.impl.campaign.rulecmd.FireAll;
26import com.fs.starfarer.api.impl.campaign.rulecmd.FireBest;
27import com.fs.starfarer.api.util.Misc;
28
36public class RuleBasedInteractionDialogPluginImpl implements InteractionDialogPlugin, RuleBasedDialog {
37
38 public static final String FAILSAFE_LEAVE = "rbid_failsafe_leave";
39
40 private InteractionDialogAPI dialog;
41 private TextPanelAPI textPanel;
42 private OptionPanelAPI options;
43 private VisualPanelAPI visual;
44
45 private RulesAPI rules;
46 private MemoryAPI memory;
47
48 private CampaignFleetAPI playerFleet;
49
50 private static final Color HIGHLIGHT_COLOR = Global.getSettings().getColor("buttonShortcut");
51
52 private boolean embeddedMode = false;
53 public void setEmbeddedMode(boolean embeddedMode) {
54 this.embeddedMode = embeddedMode;
55 }
56
57 private final String initialTrigger;
58
60 this("OpenInteractionDialog");
61 }
62 public RuleBasedInteractionDialogPluginImpl(String initialTrigger) {
63 this.initialTrigger = initialTrigger;
64 }
65
66
67 public void reinit(boolean withContinueOnRuleFound) {
68 init(dialog);
69 }
70
71 public void init(InteractionDialogAPI dialog) {
72 this.dialog = dialog;
73
74 textPanel = dialog.getTextPanel();
75 options = dialog.getOptionPanel();
76 visual = dialog.getVisualPanel();
77
78 playerFleet = Global.getSector().getPlayerFleet();
79
80 if (!embeddedMode) {
81 visual.setVisualFade(0.25f, 0.25f);
82 }
83
84 rules = Global.getSector().getRules();
85
87
88 if (!embeddedMode) {
89 fireBest(initialTrigger);
90 if (!options.hasOptions()) {
91 options.clearOptions();
92 options.addOption("Leave", FAILSAFE_LEAVE);
95 }
96 }
97 }
98 }
99
100 public void updateMemory() {
101 if (memoryMap == null) {
102 memoryMap = new HashMap<String, MemoryAPI>();
103 } else {
104 memoryMap.clear();
105 }
106 memory = dialog.getInteractionTarget().getMemory();
107
108 memoryMap.put(MemKeys.LOCAL, memory);
109 if (dialog.getInteractionTarget().getFaction() != null) {
110 memoryMap.put(MemKeys.FACTION, dialog.getInteractionTarget().getFaction().getMemory());
111 } else {
112 memoryMap.put(MemKeys.FACTION, Global.getFactory().createMemory());
113 }
114 memoryMap.put(MemKeys.GLOBAL, Global.getSector().getMemory());
115 memoryMap.put(MemKeys.PLAYER, Global.getSector().getCharacterData().getMemory());
116
117 if (dialog.getInteractionTarget().getMarket() != null) {
118 memoryMap.put(MemKeys.MARKET, dialog.getInteractionTarget().getMarket().getMemory());
119 }
120
121 if (memory.contains(MemFlags.MEMORY_KEY_SOURCE_MARKET)) {
122 String marketId = memory.getString(MemFlags.MEMORY_KEY_SOURCE_MARKET);
123 MarketAPI market = Global.getSector().getEconomy().getMarket(marketId);
124 if (market != null) {
125 memoryMap.put(MemKeys.SOURCE_MARKET, market.getMemory());
126 }
127 }
128
129 updatePersonMemory();
130 }
131
132 private void updatePersonMemory() {
133 PersonAPI person = dialog.getInteractionTarget().getActivePerson();
134// if (person != null) {
135// memoryMap.put(MemKeys.PERSON, person.getMemory());
136// } else {
137// memoryMap.remove(MemKeys.PERSON);
138// }
139 if (person != null) {
140 memory = person.getMemory();
141 memoryMap.put(MemKeys.LOCAL, memory);
142 memoryMap.put(MemKeys.PERSON_FACTION, person.getFaction().getMemory());
143 memoryMap.put(MemKeys.ENTITY, dialog.getInteractionTarget().getMemory());
144 } else {
145 memory = dialog.getInteractionTarget().getMemory();
146 memoryMap.put(MemKeys.LOCAL, memory);
147 memoryMap.remove(MemKeys.ENTITY);
148 memoryMap.remove(MemKeys.PERSON_FACTION);
149
150 }
151 }
152
153
155 updatePersonMemory();
156 }
157
158 public void setActiveMission(CampaignEventPlugin mission) {
159 if (mission == null) {
160 memoryMap.remove(MemKeys.MISSION);
161 } else {
162 MemoryAPI memory = mission.getMemory();
163 if (memory != null) {
164 memoryMap.put(MemKeys.MISSION, memory);
165 } else {
166 memoryMap.remove(MemKeys.MISSION);
167 }
168 }
169 }
170
171
172 public boolean fireAll(String trigger) {
173 return FireAll.fire(null, dialog, memoryMap, trigger);
174 }
175
176 public boolean fireBest(String trigger) {
177 return FireBest.fire(null, dialog, memoryMap, trigger);
178 }
179
180 public void backFromEngagement(EngagementResultAPI result) {
181 // no combat here, so this won't get called
182 }
183
184 public void optionSelected(String text, Object optionData) {
185 if (optionData == null || !(optionData instanceof String)) return;
186
187 String optionId = (String) optionData;
188
189 if (text != null) {
190 //textPanel.addParagraph(text, Global.getSettings().getColor("buttonText"));
191 dialog.addOptionSelectedText(optionData);
192 }
193
194 if (optionId == FAILSAFE_LEAVE) {
195 new DismissDialog().execute(null, dialog, null, memoryMap);
196 return;
197 }
198
199 if (optionId == DumpMemory.OPTION_ID) {
200 new DumpMemory().execute(null, dialog, null, memoryMap);
201 return;
202 } else if (DevMenuOptions.isDevOption(optionData)) {
203 DevMenuOptions.execute(dialog, (String) optionData);
204 return;
205 }
206
207 memory.set("$option", optionId);
208 memory.expire("$option", 0);
209
210 boolean foundRule = fireBest("DialogOptionSelected");
211 if (!foundRule && !dialog.isCurrentOptionHadAConfirm()) {
212 textPanel.addPara("ERROR: no rule found for option " + optionId +
213 ", adding a failsafe option to exit dialog.", Misc.getNegativeHighlightColor());
214 textPanel.addPara("Note: this may break any mission interaction in the current dialog, "
215 + "it's recommended that you reload an earlier save if you use this option.");
216 textPanel.highlightInLastPara(Misc.getNegativeHighlightColor(), "recommended that you reload an earlier save");
217 options.addOption("Exit dialog", FAILSAFE_LEAVE);
218 }
219
220 }
221
222
223 private String lastOptionMousedOver = null;
224 private Map<String, MemoryAPI> memoryMap;
225 public void optionMousedOver(String optionText, Object optionData) {
226
227 }
228
229 public void advance(float amount) {
230// if (Global.getSettings().isDevMode() && Keyboard.isKeyDown(Keyboard.KEY_F2)) {
231// new DumpMemory().execute(dialog, new ArrayList<Token>(), memoryMap);
232// }
233 }
234
235 private void addText(String text) {
236 if (text == null || text.isEmpty()) return;
237
238 textPanel.addParagraph(text);
239 }
240
241 private void appendText(String text) {
242 textPanel.appendToLastParagraph(" " + text);
243 }
244
245 public Object getContext() {
246 return null;
247 }
248
249 public Map<String, MemoryAPI> getMemoryMap() {
250 return memoryMap;
251 }
252
253}
254
255
256
static SettingsAPI getSettings()
Definition Global.java:51
static FactoryAPI getFactory()
Definition Global.java:35
static SectorAPI getSector()
Definition Global.java:59
static void execute(InteractionDialogAPI dialog, String option)
static boolean isDevOption(Object optionData)
static void addOptions(InteractionDialogAPI dialog)