Starsector API
Loading...
Searching...
No Matches
BaseEventManager.java
Go to the documentation of this file.
1package com.fs.starfarer.api.impl.campaign.intel;
2
3import java.util.ArrayList;
4import java.util.List;
5import java.util.Random;
6
7import com.fs.starfarer.api.EveryFrameScript;
8import com.fs.starfarer.api.Global;
9import com.fs.starfarer.api.impl.campaign.DebugFlags;
10import com.fs.starfarer.api.impl.campaign.intel.bases.LuddicPathBaseManager;
11import com.fs.starfarer.api.impl.campaign.intel.bases.PirateBaseManager;
12import com.fs.starfarer.api.util.IntervalUtil;
13
14public abstract class BaseEventManager implements EveryFrameScript {
15
16
17 protected List<EveryFrameScript> active = new ArrayList<EveryFrameScript>();
18 protected IntervalUtil tracker;
19 protected IntervalUtil trackerMax;
20 protected int currMax = 0;
21 protected Random randomBase = new Random();
22
24 float interval = getBaseInterval();
25 tracker = new IntervalUtil(interval * 0.75f, interval * 1.25f);
26
27 interval = getUpdateMaxInterval();
28 trackerMax = new IntervalUtil(interval * 0.75f, interval * 1.25f);
29 updateMax();
31 }
32
33 protected void updateMax() {
34 int min = getMinConcurrent();
35 int max = getMaxConcurrent();
36
37 currMax = min + randomBase.nextInt(max - min + 1);
38 }
39
40 protected Object readResolve() {
41 if (randomBase == null) randomBase = new Random();
42 return this;
43 }
44
45 protected abstract int getMinConcurrent();
46 protected abstract int getMaxConcurrent();
47 protected abstract EveryFrameScript createEvent();
48
49
50 protected float getUpdateMaxInterval() {
51 return 10f;
52 }
53 protected float getBaseInterval() {
54 return 1f;
55 }
56
57 protected float getIntervalRateMult() {
58 return 1f;
59 }
60
61 protected int getHardLimit() {
62 return Global.getSector().getEconomy().getNumMarkets() * 2;
63 }
64
65
66
67 public void advance(float amount) {
68 float days = Global.getSector().getClock().convertToDays(amount);
69
70// if (this instanceof GenericMissionManager) {
71// CountingMap<String> counts = new CountingMap<String>();
72// for (EveryFrameScript script : active) {
73// counts.add(script.getClass().getSimpleName());
74// }
75// System.out.println("-------------------------------");
76// System.out.println("MAX: " + getCurrMax());
77// for (String key : counts.keySet()) {
78// System.out.println("" + counts.getCount(key) + " <- " + key);
79// }
80// System.out.println("-------------------------------");
81// }
82
83 trackerMax.advance(days);
84 if (trackerMax.intervalElapsed()) {
85 int count = getActiveCount();
86 // if we reduced the count before, wait until the number of active events
87 // drops down to match the reduction before updating currMax again
88 // otherwise, since events can last a while, will usually get closer to max active
89 // despite currMax periodically being low
90 if (count <= currMax || count == 0) {
91 if (randomBase.nextFloat() < 0.05f) {
92 updateMax();
93 } else {
94 if (randomBase.nextFloat() < 0.5f) {
95 currMax--;
96 } else {
97 currMax++;
98 }
99
100 int min = getMinConcurrent();
101 int max = getMaxConcurrent();
102 if (currMax < min) currMax = min;
103 if (currMax > max) currMax = max;
104 }
105
106 int limit = getHardLimit();
107 if (currMax > limit) currMax = limit;
108 }
109 }
110
111
112 List<EveryFrameScript> remove = new ArrayList<EveryFrameScript>();
113 for (EveryFrameScript event : active) {
114 event.advance(amount);
115 if (event.isDone()) {
116 remove.add(event);
117 }
118 }
119 active.removeAll(remove);
120 //days *= 1000f;
121 if (Global.getSettings().isDevMode()) {
122 int count = getActiveCount();
123 if (count <= 0) {
124 days *= 1000f;
125 }
126 }
127 tracker.advance(days * getIntervalRateMult());
128 if (this instanceof PirateBaseManager && DebugFlags.RAID_DEBUG) {
129 tracker.advance(days * 1000000f);
130 }
131 if (this instanceof LuddicPathBaseManager && DebugFlags.PATHER_BASE_DEBUG) {
132 tracker.advance(days * 1000000f);
133 }
134 if (!tracker.intervalElapsed()) return;
135
136
137// if (this instanceof FactionHostilityManager) {
138// System.out.println("wefwefe");
139// }
140// if (this instanceof PirateBaseManager) {
141// System.out.println("wefwefwef");
142// }
143
144 int count = getActiveCount();
145
146 if (count < currMax) {
148 addActive(event);
149 }
150 }
151
152 public int getActiveCount() {
153 int count = 0;
154 for (EveryFrameScript s : active) {
155 if (s instanceof BaseIntelPlugin) {
157 if (intel.isEnding()) continue;
158 }
159 count++;
160 }
161 return count;
162 }
163
164 public int getOngoing() {
165 return active.size();
166 }
167
168 public int getCurrMax() {
169 return currMax;
170 }
171
172 public boolean belowMax() {
173 return getCurrMax() > getOngoing();
174 }
175
176 public void addActive(EveryFrameScript event) {
177 if (event != null) {
178 active.add(event);
179 }
180 }
181
182 public boolean isDone() {
183 return false;
184 }
185 public boolean runWhilePaused() {
186 return false;
187 }
188
189 public List<EveryFrameScript> getActive() {
190 return active;
191 }
192
193 public IntervalUtil getTracker() {
194 return tracker;
195 }
196
197
198
199}
200
201
202
static SettingsAPI getSettings()
Definition Global.java:51
static SectorAPI getSector()
Definition Global.java:59