Starsector API
Loading...
Searching...
No Matches
HistorianData.java
Go to the documentation of this file.
1package com.fs.starfarer.api.impl.campaign.intel.bar.events.historian;
2
3import java.util.ArrayList;
4import java.util.Collections;
5import java.util.Comparator;
6import java.util.LinkedHashSet;
7import java.util.List;
8import java.util.Random;
9import java.util.Set;
10
11import com.fs.starfarer.api.Global;
12import com.fs.starfarer.api.campaign.InteractionDialogAPI;
13import com.fs.starfarer.api.characters.FullName.Gender;
14import com.fs.starfarer.api.characters.PersonAPI;
15import com.fs.starfarer.api.impl.campaign.ids.Factions;
16import com.fs.starfarer.api.impl.campaign.intel.bar.events.historian.HistorianBackstory.HistorianBackstoryInfo;
17import com.fs.starfarer.api.util.Misc;
18import com.fs.starfarer.api.util.WeightedRandomPicker;
19
20public class HistorianData {
21
22 public static interface HistorianOffer {
23 void init(InteractionDialogAPI dialog);
24 void addPromptAndOption(InteractionDialogAPI dialog);
25 void optionSelected(String optionText, Object optionData);
26 boolean shouldRemoveOffer();
27 boolean isInteractionFinished();
28 boolean shouldEndConversationOnReturning();
29 void notifyAccepted();
30 //String getOfferId();
31 int getSortOrder();
32 HistorianOfferCreator getCreator();
33 void setCreator(HistorianOfferCreator creator);
34 }
35
36 public static interface HistorianOfferCreator {
37 HistorianOffer createOffer(Random random, List<HistorianOffer> soFar);
38 boolean ignoresLimit();
39 float getFrequency();
40 String getOfferId(BaseHistorianOffer offer);
41 void notifyAccepted(HistorianOffer offer);
42 }
43
44
45 public static final String KEY = "$core_historianData";
46
47 public static String TIER1 = "hist1t";
48 public static String TIER2 = "hist2t";
49 public static String TIER3 = "hist3t";
50
51 public static HistorianData getInstance() {
52 Object test = Global.getSector().getMemoryWithoutUpdate().get(KEY);
53 if (test == null) {// || true) {
54 test = new HistorianData();
55 Global.getSector().getMemoryWithoutUpdate().set(KEY, test);
56 }
57 return (HistorianData) test;
58 }
59
60 protected PersonAPI person;
61 protected boolean introduced = false;
62 protected int tier = 0;
63
64// protected transient List<HistorianOfferCreator> creators = new ArrayList<HistorianOfferCreator>();
65// protected transient List<HistorianBackstoryInfo> backstory = new ArrayList<HistorianBackstoryInfo>();
66 protected transient List<HistorianOfferCreator> creators;
67 protected transient List<HistorianBackstoryInfo> backstory;
68 protected Set<String> shownBackstory = new LinkedHashSet<String>();
69 protected Set<String> givenOffers = new LinkedHashSet<String>();
70
71 public HistorianData() {
72 person = Global.getSector().getFaction(Factions.INDEPENDENT).createRandomPerson();
73
74 // Diana Nesinjo
75 // Jonn Isaaneid
76
77 if (person.getGender() == Gender.MALE) {
78 person.setPortraitSprite(Global.getSettings().getSpriteName("intel", "historian_male"));
79 } else {
80 person.setPortraitSprite(Global.getSettings().getSpriteName("intel", "historian_female"));
81 }
82
83// creators.add(new DonationOfferCreator());
84// creators.add(new ShipBlueprintOfferCreator(20f));
85// creators.add(new WeaponBlueprintOfferCreator(10f));
86// creators.add(new FighterBlueprintOfferCreator(10f));
87// creators.add(new SpecialItemOfferCreator(10f));
89 //HistorianBackstory.init(backstory);
90 }
91
92 protected Object readResolve() {
93 if (creators == null) {
94 creators = new ArrayList<HistorianOfferCreator>();
100 }
101 if (backstory == null) {
102 backstory = new ArrayList<HistorianBackstoryInfo>();
103 //HistorianBackstory.init(backstory);
104 }
105 if (shownBackstory == null) {
106 shownBackstory = new LinkedHashSet<String>();
107 }
108 if (givenOffers == null) {
109 givenOffers = new LinkedHashSet<String>();
110 }
111 return this;
112 }
113
114 public HistorianBackstoryInfo pickBackstoryBit(Random random) {
115 WeightedRandomPicker<HistorianBackstoryInfo> picker = new WeightedRandomPicker<HistorianBackstoryInfo>(random);
116 for (HistorianBackstoryInfo info : backstory) {
117 if (shownBackstory.contains(info.getId())) continue;
118 picker.add(info, info.getWeight());
119 }
120 if (picker.isEmpty()) {
121 shownBackstory.clear();
122 for (HistorianBackstoryInfo info : backstory) {
123 picker.add(info, info.getWeight());
124 }
125 }
126 return picker.pick();
127 }
128
129 public List<HistorianBackstoryInfo> getBackstory() {
130 return backstory;
131 }
132
133 public Set<String> getShownBackstory() {
134 return shownBackstory;
135 }
136
137 public Set<String> getGivenOffers() {
138 return givenOffers;
139 }
140
141 public float getWeightForTags(Set<String> tags) {
142 float w = 0f;
143 if (tags.contains(TIER1)) {
144 w = 1f;
145 } else if (tags.contains(TIER2) && getTier() >= 1) {
146 w = 5f;
147 } else if (tags.contains(TIER3) && getTier() >= 2) {
148 w = 10f;
149 }
150 return w;
151 }
152
153 public int getTier() {
154 return tier;
155 }
156
157 public void setTier(int tier) {
158 this.tier = tier;
159 }
160
161 public void incrTier() {
162 tier++;
163 }
164
165 public boolean isMaxTier() {
166 return getTier() >= 2;
167 }
168
169
170 public void setRecentlyDonated() {
171 float dur = 60f + (float) Math.random() * 60f;
172 Global.getSector().getMemoryWithoutUpdate().set("$historian_recentlyDonated", true, dur);
173 }
174
175 public boolean isRecentlyDonated() {
176 return Global.getSector().getMemoryWithoutUpdate().getBoolean("$historian_recentlyDonated");
177 }
178
179 public List<HistorianOfferCreator> getCreators() {
180 return creators;
181 }
182
183 public void setCreators(List<HistorianOfferCreator> creators) {
184 this.creators = creators;
185 }
186
187 public List<HistorianOffer> getOffers(Random random, InteractionDialogAPI dialog) {
188 //random = new Random();
189
190 WeightedRandomPicker<HistorianOfferCreator> limited = new WeightedRandomPicker<HistorianOfferCreator>(random);
191 List<HistorianOffer> always = new ArrayList<HistorianOffer>();
192
193 for (HistorianOfferCreator c : creators) {
194 if (c.ignoresLimit()) {
195 HistorianOffer offer = c.createOffer(random, always);
196 if (offer != null) {
197 offer.setCreator(c);
198 always.add(offer);
199 }
200 } else {
201 limited.add(c, c.getFrequency());
202 }
203 }
204
205 List<HistorianOffer> result = new ArrayList<HistorianOffer>(always);
206
207 int num = 1 + random.nextInt(2 + getTier()) + always.size();
208 //num += 5;
209 int attempts = num + 5;
210 for (int i = 0; i < attempts && result.size() < num; i++) {
211 HistorianOfferCreator c = limited.pick();
212 if (c == null) continue;
213 HistorianOffer offer = c.createOffer(random, result);
214 if (offer != null) {
215 offer.setCreator(c);
216 result.add(offer);
217 }
218 }
219
220 //limited.add(new ShipBlueprintOffer());
221 //limited.add(new DonationOffer(25000));
222
223 Collections.sort(result, new Comparator<HistorianOffer>() {
224 public int compare(HistorianOffer o1, HistorianOffer o2) {
225 return (int) Math.signum(o1.getSortOrder() - o2.getSortOrder());
226 }
227 });
228
229
230 return result;
231 }
232
233 public PersonAPI getPerson() {
234 return person;
235 }
236
237 protected String getManOrWoman() {
238 String manOrWoman = "man";
239 if (person.getGender() == Gender.FEMALE) manOrWoman = "woman";
240 return manOrWoman;
241 }
242
243 protected String getUCHeOrShe() {
244 String heOrShe = "he";
245 if (person.getGender() == Gender.FEMALE) {
246 heOrShe = "she";
247 }
248 return Misc.ucFirst(heOrShe);
249 }
250 protected String getHeOrShe() {
251 String heOrShe = "he";
252 if (person.getGender() == Gender.FEMALE) {
253 heOrShe = "she";
254 }
255 return heOrShe;
256 }
257
258 protected String getHimOrHer() {
259 String himOrHer = "him";
260 if (person.getGender() == Gender.FEMALE) {
261 himOrHer = "her";
262 }
263 return himOrHer;
264 }
265
266 protected String getHimOrHerself() {
267 String himOrHer = "himself";
268 if (person.getGender() == Gender.FEMALE) {
269 himOrHer = "herself";
270 }
271 return himOrHer;
272 }
273
274 protected String getHisOrHer() {
275 String hisOrHer = "his";
276 if (person.getGender() == Gender.FEMALE) {
277 hisOrHer = "her";
278 }
279 return hisOrHer;
280 }
281
282 public boolean isIntroduced() {
283 return introduced;
284 }
285
286 public void setIntroduced(boolean introduced) {
287 this.introduced = introduced;
288 }
289}
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
static SettingsAPI getSettings()
Definition Global.java:51
static SectorAPI getSector()
Definition Global.java:59
List< HistorianOffer > getOffers(Random random, InteractionDialogAPI dialog)
String getSpriteName(String category, String id)