Starsector API
Loading...
Searching...
No Matches
BaseGetCommodityBarEvent.java
Go to the documentation of this file.
1package com.fs.starfarer.api.impl.campaign.intel.bar.events;
2
3import java.awt.Color;
4import java.util.Map;
5import java.util.Random;
6
7import com.fs.starfarer.api.Global;
8import com.fs.starfarer.api.campaign.CargoAPI;
9import com.fs.starfarer.api.campaign.InteractionDialogAPI;
10import com.fs.starfarer.api.campaign.OptionPanelAPI;
11import com.fs.starfarer.api.campaign.PersonImportance;
12import com.fs.starfarer.api.campaign.TextPanelAPI;
13import com.fs.starfarer.api.campaign.econ.CommoditySpecAPI;
14import com.fs.starfarer.api.campaign.econ.MarketAPI;
15import com.fs.starfarer.api.campaign.rules.MemoryAPI;
16import com.fs.starfarer.api.characters.PersonAPI;
17import com.fs.starfarer.api.characters.FullName.Gender;
18import com.fs.starfarer.api.impl.campaign.ids.Commodities;
19import com.fs.starfarer.api.impl.campaign.ids.Ranks;
20import com.fs.starfarer.api.impl.campaign.rulecmd.AddRemoveCommodity;
21import com.fs.starfarer.api.ui.LabelAPI;
22import com.fs.starfarer.api.util.Misc;
23import com.fs.starfarer.api.util.WeightedRandomPicker;
24
25public abstract class BaseGetCommodityBarEvent extends BaseBarEvent {
26 public static final String OPTION_CONFIRM = "confirm";
27 public static final String OPTION_CANCEL = "cancel";
28 public static final String OPTION_CONTINUE = "continue";
29
30 protected long seed;
31 protected PersonAPI person;
32 protected int quantity;
33 protected int unitPrice;
34 protected String commodity;
35
37 seed = Misc.random.nextLong();
38 }
39
40 protected transient Random random;
41
42 protected MarketAPI market = null;
43
44 protected void regen(MarketAPI market) {
45 if (this.market == market) return;
46 this.market = market;
47
48 random = new Random(seed + market.getId().hashCode());
49
51 if (commodity == null) return;
52
55
56
57 float price = market.getSupplyPrice(commodity, 1, true);
58 unitPrice = (int) (price * getPriceMult());
59 if (unitPrice > 50) {
60 unitPrice = unitPrice / 10 * 10;
61 }
63 unitPrice = 1;
64 }
65 }
66
74
75
76
77 protected float getPriceMult() {
78 return 0.75f;
79 }
80 protected String getCommodityId() {
81 return commodity != null ? commodity : Commodities.FOOD;
82 }
83 protected int computeQuantity() {
84 int quantity = 50 + 10 * random.nextInt(6);
85
86 int size = market.getSize();
87 //quantity *= BaseIndustry.getSizeMult(size);
88 quantity *= Math.max(1, size - 2);
89 return quantity;
90 }
91 protected void adjustPerson(PersonAPI person) {
92
93 }
94 protected String getPersonFaction() {
95 return market.getFactionId();
96 }
97 protected String getPersonRank() {
98 return Ranks.CITIZEN;
99 }
100 protected String getPersonPost() {
101 return Ranks.CITIZEN;
102 }
103
104 protected String getManOrWoman() {
105 String manOrWoman = "man";
106 if (person.getGender() == Gender.FEMALE) manOrWoman = "woman";
107 return manOrWoman;
108 }
109
110 protected String getHeOrShe() {
111 String heOrShe = "he";
112 if (person.getGender() == Gender.FEMALE) {
113 heOrShe = "she";
114 }
115 return heOrShe;
116 }
117
118 protected String getHimOrHer() {
119 String himOrHer = "him";
120 if (person.getGender() == Gender.FEMALE) {
121 himOrHer = "her";
122 }
123 return himOrHer;
124 }
125
126 protected String getHimOrHerself() {
127 String himOrHer = "himself";
128 if (person.getGender() == Gender.FEMALE) {
129 himOrHer = "herself";
130 }
131 return himOrHer;
132 }
133
134 protected String getHisOrHer() {
135 String hisOrHer = "his";
136 if (person.getGender() == Gender.FEMALE) {
137 hisOrHer = "her";
138 }
139 return hisOrHer;
140 }
141
142 @Override
143 public void addPromptAndOption(InteractionDialogAPI dialog, Map<String, MemoryAPI> memoryMap) {
144 super.addPromptAndOption(dialog, memoryMap);
145
147
150
152 }
153
154 protected abstract String getPrompt();
155 protected abstract String getOptionText();
156
157 protected abstract String getConfirmText();
158 protected abstract String getCancelText();
159
160 protected abstract String getMainText();
161 protected String [] getMainTextTokens() { return null; };
162 protected Color [] getMainTextColors() { return null; };
163
164 protected String getMainText2() { return null; };
165 protected String [] getMainText2Tokens() { return null; };
166 protected Color [] getMainText2Colors() { return null; };
167
168 protected String getAcceptText() { return null; };
169 protected String [] getAcceptTextTokens() { return null; };
170 protected Color [] getAcceptTextColors() { return null; };
171
172 protected String getDeclineText() { return null; };
173 protected String [] getDeclineTextTokens() { return null; };
174 protected Color [] getDeclineTextColors() { return null; };
175
176 @Override
177 public void init(InteractionDialogAPI dialog, Map<String, MemoryAPI> memoryMap) {
178 super.init(dialog, memoryMap);
179
180 done = false;
181
183
185 if (getMainTextTokens() != null) {
189 } else {
191 }
192
193 if (getMainText2() == null) {
195 } else {
198 options.addOption("Continue", OPTION_CONTINUE);
199 }
200 }
201
202 protected boolean showCargoCap() {
203 return true;
204 }
205
206 protected void showTotalAndOptions() {
207 Color h = Misc.getHighlightColor();
209
211
212 boolean canAccept = canAccept();
213
214 if (showCargoCap() && commodity != null && quantity > 0) {
217 String str = "";
218 int cap = 0;
219 if (spec.isFuel()) {
220 cap = cargo.getFreeFuelSpace();
221 if (cap > 1) {
222 str += "Your fleet's fuel tanks can hold an additional %s units of fuel.";
223 } else {
224 str += "Your fleet's fuel tanks are currently full.";
225 }
226 } else if (spec.isPersonnel()) {
227 cap = cargo.getFreeCrewSpace();
228 if (cap > 1) {
229 str += "Your fleet's crew quarters can accommodate an additional %s personnel.";
230 } else {
231 str += "Your fleet's crew berths are currently full.";
232 }
233 } else {
234 cap = (int) cargo.getSpaceLeft();
235 if (cap > 1) {
236 str += "Your fleet's holds can accommodate an additional %s units of cargo.";
237 } else {
238 str += "Your fleet's cargo holds are currently full.";
239 }
240 }
241 text.addPara(str, h, Misc.getWithDGS(cap));
242 }
243
244 float credits = Global.getSector().getPlayerFleet().getCargo().getCredits().get();
245 int price = unitPrice * quantity;
246 if (price > 0) {
247 LabelAPI label = text.addPara("The total price is %s. You have %s available.",
248 h,
249 Misc.getDGSCredits(price),
250 Misc.getDGSCredits(credits));
251 label.setHighlightColors(canAccept ? h : n, h);
252 label.setHighlight(Misc.getDGSCredits(price), Misc.getDGSCredits(credits));
253 }
254
255
259 if (!canAccept) {
261 String tooltip = getCanNotAcceptTooltip();
262 if (tooltip != null) {
264 }
265 }
266 if (canAccept) {
268 }
270 //options.setShortcut(OPTION_CANCEL, Keyboard.KEY_ESCAPE, false, false, false, true);
271 }
272
273 protected void addStoryOption() {
274
275 }
276
277 protected boolean canAccept() {
278 float credits = Global.getSector().getPlayerFleet().getCargo().getCredits().get();
279 int price = unitPrice * quantity;
280 boolean canAfford = credits >= price;
281 return canAfford;
282 }
283
284 protected String getCanNotAcceptTooltip() {
285 return "You don't have enough credits.";
286 }
287
288
289
290 protected void doExtraConfirmActions() {
291
292 }
293
294
296
297 }
298 protected void doStandardConfirmActions() {
299 int price = unitPrice * quantity;
301 if (price > 0) cargo.getCredits().subtract(price);
303
305
306 if (price > 0) AddRemoveCommodity.addCreditsLossText(price, text);
308 }
309
310 @Override
311 public void optionSelected(String optionText, Object optionData) {
312 if (optionData == OPTION_CONTINUE) {
314 if (getMainText2Tokens() != null) {
318 } else {
320 }
322 } else if (optionData == OPTION_CONFIRM) {
323 done = true;
325
328 String acceptStr = getAcceptText();
329 if (acceptStr != null) {
330 if (getAcceptTextTokens() != null) {
334 } else {
335 text.addPara(acceptStr);
336 }
337 }
338
341
342 } else if (optionData == OPTION_CANCEL) {
343
345 String declineStr = getDeclineText();
346 if (declineStr != null) {
347 if (getDeclineTextTokens() != null) {
351 } else {
352 text.addPara(declineStr);
353 }
354 } else {
355 noContinue = true;
356 }
357 done = true;
358 }
359// else if (optionText == getStoryOptionId() && getStoryOptionId() != null) {
360//
361// }
362 }
363
364 @Override
365 public boolean isDialogFinished() {
366 return done;
367 }
368
370 return person;
371 }
372
374 return market;
375 }
376
377
380 picker.add(PersonImportance.VERY_LOW, 1f);
381 picker.add(PersonImportance.LOW, 5f);
382 picker.add(PersonImportance.MEDIUM, 10f);
383 picker.add(PersonImportance.HIGH, 5f);
384 picker.add(PersonImportance.VERY_HIGH, 1f);
385 return picker.pick();
386 }
408
409
410 public String pickOne(String ... options) {
412 for (String option : options) {
413 picker.add(option);
414 }
415 return picker.pick();
416 }
417
418}
419
420
421
422
static SettingsAPI getSettings()
Definition Global.java:57
static SectorAPI getSector()
Definition Global.java:65
void init(InteractionDialogAPI dialog, Map< String, MemoryAPI > memoryMap)
void addPromptAndOption(InteractionDialogAPI dialog, Map< String, MemoryAPI > memoryMap)
static void addCommodityGainText(String commodityId, int quantity, TextPanelAPI text)
static void addCreditsLossText(int credits, TextPanelAPI text)
static String getDGSCredits(float num)
Definition Misc.java:1390
static String getWithDGS(float num)
Definition Misc.java:1381
static Color getNegativeHighlightColor()
Definition Misc.java:802
static Color getHighlightColor()
Definition Misc.java:792
CommoditySpecAPI getCommoditySpec(String commodityId)
void addCommodity(String commodityId, float quantity)
void setTooltip(Object data, String tooltipText)
void addOption(String text, Object data)
void setEnabled(Object data, boolean enabled)
FactionAPI getFaction(String factionId)
float getSupplyPrice(String commodityId, double quantity, boolean isPlayerPrice)
void setHighlight(int start, int end)
void setHighlightColors(Color ... colors)