1package com.fs.starfarer.api.impl.campaign.intel.bar.events;
3import java.util.ArrayList;
4import java.util.HashSet;
5import java.util.LinkedHashMap;
6import java.util.LinkedHashSet;
10import com.fs.starfarer.api.EveryFrameScript;
11import com.fs.starfarer.api.Global;
12import com.fs.starfarer.api.campaign.SectorEntityToken;
13import com.fs.starfarer.api.characters.PersonAPI;
14import com.fs.starfarer.api.impl.campaign.DebugFlags;
15import com.fs.starfarer.api.impl.campaign.intel.bar.PortsideBarData;
16import com.fs.starfarer.api.impl.campaign.intel.bar.PortsideBarEvent;
17import com.fs.starfarer.api.loading.BarEventSpec;
18import com.fs.starfarer.api.util.IntervalUtil;
19import com.fs.starfarer.api.util.Misc;
20import com.fs.starfarer.api.util.TimeoutTracker;
21import com.fs.starfarer.api.util.WeightedRandomPicker;
25 public static interface GenericBarEventCreator {
27 float getBarEventFrequencyWeight();
28 float getBarEventActiveDuration();
29 float getBarEventTimeoutDuration();
30 float getBarEventAcceptedTimeoutDuration();
40 String getBarEventId();
41 boolean wasAutoAdded();
45 public static final String
KEY =
"$core_genericBarEventManager";
52 protected List<GenericBarEventCreator>
creators =
new ArrayList<GenericBarEventCreator>();
53 protected LinkedHashMap<PortsideBarEvent, GenericBarEventCreator>
barEventCreators =
new LinkedHashMap<PortsideBarEvent, GenericBarEventCreator>();
55 protected IntervalUtil
tracker =
new IntervalUtil(0.4f, 0.6f);
56 protected IntervalUtil
tracker2 =
new IntervalUtil(20f, 40f);
57 protected TimeoutTracker<PortsideBarEvent>
active =
new TimeoutTracker<PortsideBarEvent>();
58 protected TimeoutTracker<GenericBarEventCreator>
timeout =
new TimeoutTracker<GenericBarEventCreator>();
60 protected long seed = Misc.genRandomSeed();
74 public long getSeed(SectorEntityToken entity, PersonAPI person, String extra) {
77 if (entity !=
null) mult *= (long) entity.getName().hashCode();
78 if (person !=
null) mult *= (long) person.getNameString().hashCode();
79 if (extra !=
null) mult *= (long) extra.hashCode();
81 return seed + mult * 181783497276652981L;
85 seed = Misc.genRandomSeed();
91 timeout =
new TimeoutTracker<GenericBarEventCreator>();
94 tracker2 =
new IntervalUtil(20f, 40f);
109 Set<String> validEvents =
new HashSet<String>();
110 Set<String> alreadyHaveCreatorsFor =
new HashSet<String>();
111 for (BarEventSpec spec : specs) {
112 validEvents.add(spec.getId());
115 for (GenericBarEventCreator curr :
new ArrayList<GenericBarEventCreator>(
creators)) {
116 if (!curr.wasAutoAdded())
continue;
118 if (!validEvents.contains(curr.getBarEventId())) {
122 alreadyHaveCreatorsFor.add(curr.getBarEventId());
126 for (BarEventSpec spec : specs) {
127 if (!alreadyHaveCreatorsFor.contains(spec.getId())) {
141 for (GenericBarEventCreator script :
creators) {
142 if (clazz.isInstance(script))
return true;
160 for (GenericBarEventCreator curr :
creators) {
161 if (curr.getClass().equals(creatorClass)) {
172 if (creator !=
null) {
173 float dur = creator.getBarEventAcceptedTimeoutDuration();
174 dur = Math.max(dur,
timeout.getRemaining(creator));
185 float days = Misc.getDays(amount);
205 if (
tracker.intervalElapsed()) {
206 for (
int i = 0; i < 5; i++) {
207 List<PortsideBarEvent> orphaned =
new ArrayList<PortsideBarEvent>(
barEventCreators.keySet());
214 if (creator !=
null) {
215 float dur = creator.getBarEventTimeoutDuration();
216 dur = Math.max(dur,
timeout.getRemaining(creator));
223 float max = Math.round(
creators.size() * f);
229 if (max < 1) max = 1;
231 if (
active.getItems().size() >= max)
return;
234 Set<String> activeCreators =
new LinkedHashSet<String>();
236 activeCreators.add(curr.getBarEventId());
239 WeightedRandomPicker<GenericBarEventCreator> priority =
new WeightedRandomPicker<GenericBarEventCreator>();
240 WeightedRandomPicker<GenericBarEventCreator> picker =
new WeightedRandomPicker<GenericBarEventCreator>();
241 for (GenericBarEventCreator curr :
creators) {
243 if (activeCreators.contains(curr.getBarEventId()))
continue;
244 if (
timeout.contains(curr))
continue;
246 if (curr.isPriority()) {
247 priority.add(curr, curr.getBarEventFrequencyWeight());
249 picker.add(curr, curr.getBarEventFrequencyWeight());
253 GenericBarEventCreator pick = priority.pick();
254 if (pick ==
null) pick = picker.pick();
255 if (pick ==
null)
return;
258 if (event ==
null)
return;
260 active.add(event, pick.getBarEventActiveDuration());
static SettingsAPI getSettings()
static SectorAPI getSector()
void advance(float amount)
void updateBarEventCreatorsFromSpecs()
List< GenericBarEventCreator > creators
TimeoutTracker< PortsideBarEvent > getActive()
void addEventCreator(GenericBarEventCreator creator)
TimeoutTracker< GenericBarEventCreator > getTimeout()
LinkedHashMap< PortsideBarEvent, GenericBarEventCreator > barEventCreators
void notifyWasInteractedWith(PortsideBarEvent event)
TimeoutTracker< PortsideBarEvent > active
static BarEventManager getInstance()
GenericBarEventCreator getCreatorFor(PortsideBarEvent event)
void setTimeout(Class creatorClass, float duration)
long getSeed(SectorEntityToken entity, PersonAPI person, String extra)
boolean hasEventCreator(Class<?> clazz)
TimeoutTracker< GenericBarEventCreator > timeout
List< GenericBarEventCreator > getCreators()
void setWasAutoAdded(boolean wasAutoAdded)
List< BarEventSpec > getAllBarEventSpecs()
float getFloat(String key)