Starsector API
Loading...
Searching...
No Matches
DisposableFleetManager.java
Go to the documentation of this file.
1package com.fs.starfarer.api.impl.campaign.fleets;
2
3import java.util.LinkedHashMap;
4
5import com.fs.starfarer.api.Global;
6import com.fs.starfarer.api.campaign.CampaignFleetAPI;
7import com.fs.starfarer.api.campaign.StarSystemAPI;
8import com.fs.starfarer.api.campaign.ai.CampaignFleetAIAPI;
9import com.fs.starfarer.api.campaign.ai.CampaignFleetAIAPI.EncounterOption;
10import com.fs.starfarer.api.campaign.econ.MarketAPI;
11import com.fs.starfarer.api.impl.campaign.ids.MemFlags;
12import com.fs.starfarer.api.impl.campaign.ids.Tags;
13import com.fs.starfarer.api.impl.campaign.tutorial.TutorialMissionIntel;
14import com.fs.starfarer.api.util.IntervalUtil;
15import com.fs.starfarer.api.util.Misc;
16import com.fs.starfarer.api.util.TimeoutTracker;
17
27
28 public static boolean DEBUG = false;
29
30 public static final String KEY_SYSTEM = "$core_disposableFleetSpawnSystem";
31 public static final String KEY_SPAWN_FP = "$core_disposableFleetSpawnFP";
32 //public static final float MAX_RANGE_FROM_PLAYER_LY = 3f;
34 public static final float DESPAWN_RANGE_LY = MAX_RANGE_FROM_PLAYER_LY + 1.4f;
35
36 protected IntervalUtil tracker2 = new IntervalUtil(0.75f, 1.25f);;
37 protected LinkedHashMap<String, TimeoutTracker<Boolean>> recentSpawns = new LinkedHashMap<String, TimeoutTracker<Boolean>>();
38
39 protected Object readResolve() {
40 super.readResolve();
41 return this;
42 }
43
44 protected float getExpireDaysPerFleet() {
45 return 30f;
46 }
47
48 protected String getSpawnKey(StarSystemAPI system) {
49 String sysId = system.getOptionalUniqueId();
50 if (sysId == null) sysId = system.getName();
51 return "$core_recentSpawn_" + getSpawnId() + "_" + sysId;
52 }
53
54 protected void addRecentSpawn(StarSystemAPI system) {
55 String key = getSpawnKey(system);
57 if (e < 0) e = 0;
61 }
62
63 protected float getRecentSpawnsForSystem(StarSystemAPI system) {
64 if (system == null) return 0f;
65 String key = getSpawnKey(system);
67 if (e < 0) e = 0;
68 return e / getExpireDaysPerFleet();
69 }
70
71 @Override
72 protected int getMaxFleets() {
73 return 100; // limiting is based on spawnRateMult instead
74 }
75
76 @Override
78 if (currSpawnLoc == null) return true;
79 String system = fleet.getMemoryWithoutUpdate().getString(KEY_SYSTEM);
80 float spawnFP = fleet.getMemoryWithoutUpdate().getFloat(KEY_SPAWN_FP);
82 float playerFP = player.getFleetPoints();
83
84 if (system == null || !system.equals(currSpawnLoc.getName())) return true;
85
86 if (spawnFP >= fleet.getFleetPoints() * 2f) {
87 if (fleet.getAI() instanceof CampaignFleetAIAPI) {
89 EncounterOption option = ai.pickEncounterOption(null, player, true);
90 if (option == EncounterOption.DISENGAGE) return true;
91 } else {
92 return fleet.getFleetPoints() <= playerFP * 0.5f;
93 }
94 }
95
96 return false;
97 }
98
99 @Override
100 public float getSpawnRateMult() {
101 return spawnRateMult;
102 }
103
104 protected float spawnRateMult = 1f;
105 protected StarSystemAPI currSpawnLoc = null;
106
107 protected void currSpawnLocChanged() {
108
109 }
110
111 @Override
112 public void advance(float amount) {
114 return;
115 }
116
117 super.advance(amount);
118
119
121 if (player == null) return;
122
123 float days = Global.getSector().getClock().convertToDays(amount);
124 if (DEBUG) {
125 days *= 100f;
126 }
127
128 tracker2.advance(days);
129 if (tracker2.intervalElapsed()) {
131 if (closest != currSpawnLoc) {
132 currSpawnLoc = closest;
134 }
135
137 //List<ManagedFleetData> remove = new ArrayList<ManagedFleetData>();
138 for (ManagedFleetData data : active) {
139 if (Misc.isFleetReturningToDespawn(data.fleet)) continue;
140 // if it's player-visible/in the currently active location,
141 // make it return to source when it's been beat up enough
142 // to be worth despawning
143 //if (isOkToDespawnAssumingNotPlayerVisible(data.fleet)) {
144
145 float fp = data.fleet.getFleetPoints();
146 float spawnFP = data.fleet.getMemoryWithoutUpdate().getFloat(KEY_SPAWN_FP);
147 if (fp < spawnFP * 0.33f) {
149 //remove.add(data);
150 }
151 }
152 }
153
154 //active.removeAll(remove);
155
157 }
158 }
159
160 protected boolean withReturnToSourceAssignments() {
161 return true;
162 }
163
165 return currSpawnLoc;
166 }
167
168 protected void updateSpawnRateMult() {
169 if (currSpawnLoc == null) {
170 if (DEBUG) {
171 System.out.println("No target system, spawnRateMult is 1");
172 }
173 spawnRateMult = 1f;
174 return;
175 }
176
177 float desiredNumFleets = getDesiredNumFleetsForSpawnLocation();
179 if (active != null) {
180 float activeInSystem = 0f;
181 for (ManagedFleetData data : active) {
182 if (data.spawnedFor == currSpawnLoc || data.fleet.getContainingLocation() == currSpawnLoc) {
183 activeInSystem++;
184 }
185 }
186 recentSpawns = Math.max(recentSpawns, activeInSystem);
187 }
188
189 spawnRateMult = (float) Math.pow(Math.max(0, (desiredNumFleets - recentSpawns) * 1f), 4f);
190 if (spawnRateMult < 0) spawnRateMult = 0;
191
192 //if (DEBUG || this instanceof DisposableHostileActivityFleetManager) {
193 if (DEBUG) {
194 System.out.println(String.format("ID: %s, system: %s, recent: %s, desired: %s, spawnRateMult: %s",
195 getSpawnId(),
197 "" + recentSpawns,
198 "" + desiredNumFleets,
199 "" + spawnRateMult));
200 }
201 }
202
203 protected abstract int getDesiredNumFleetsForSpawnLocation();
204
205 protected abstract CampaignFleetAPI spawnFleetImpl();
206 protected abstract String getSpawnId();
207
212 if (Global.getSector().isInNewGameAdvance()) return null;
214 if (player == null) return null;
215 StarSystemAPI nearest = null;
216 float minDist = Float.MAX_VALUE;
217 for (MarketAPI market : Global.getSector().getEconomy().getMarketsCopy()) {
218 if (market.isHidden()) continue;
219 if (market.getStarSystem() != null && market.getStarSystem().hasTag(Tags.SYSTEM_ABYSSAL)) continue;
220
221 if (market.isPlayerOwned() && market.getSize() <= 3) continue;
222 if (!market.hasSpaceport()) continue;
223
224 float distToPlayerLY = Misc.getDistanceLY(player.getLocationInHyperspace(), market.getLocationInHyperspace());
225
226 if (distToPlayerLY > MAX_RANGE_FROM_PLAYER_LY) continue;
227
228 if (distToPlayerLY < minDist && market.getStarSystem() != null) {
229 if (market.getStarSystem().getStar() != null) {
230 if (market.getStarSystem().getStar().getSpec().isPulsar()) continue;
231 }
232
233 nearest = market.getStarSystem();
234 minDist = distToPlayerLY;
235 }
236 }
237
238
239 // stick with current system longer unless something else is closer
240 if (nearest == null && currSpawnLoc != null) {
241 float distToPlayerLY = Misc.getDistanceLY(player.getLocationInHyperspace(), currSpawnLoc.getLocation());
242 if (distToPlayerLY <= DESPAWN_RANGE_LY) {
243 nearest = currSpawnLoc;
244 }
245 }
246
247 return nearest;
248 }
249
251 if (currSpawnLoc == null) return null;
252
253 // otherwise, possible for jump-point dialog to say there's nothing on other side
254 // but there will be by the time the player comes out
256 return null;
257 }
258
260 if (fleet != null) {
263 }
264
265 // do this even if fleet is null, to avoid non-stop fail-spawning of fleets
266 // if spawnFleetImpl() can't spawn one, for whatever reason
269
270 return fleet;
271 }
272
273 protected String getTravelText(StarSystemAPI system, CampaignFleetAPI fleet) {
274 return "traveling to the " + system.getBaseName() + " star system";
275 }
276
277 protected String getActionInsideText(StarSystemAPI system, CampaignFleetAPI fleet) {
279 String verb = "raiding";
280 if (patrol) verb = "patrolling";
281 return verb + " the " + system.getBaseName() + " star system";
282 }
283
284 protected String getActionOutsideText(StarSystemAPI system, CampaignFleetAPI fleet) {
286 String verb = "raiding";
287 if (patrol) verb = "patrolling";
288 return verb + " around the " + system.getBaseName() + " star system";
289 }
290
291 protected void setLocationAndOrders(CampaignFleetAPI fleet, float probStartInHyper, float probStayInHyper) {
293
294 boolean forceStartInHyper = false;
295 if (currSpawnLoc != null) {
298 if (recentSpawns > max * 0.75f || currSpawnLoc.getDaysSinceLastPlayerVisit() < 30f) {
299 forceStartInHyper = Global.getSector().getPlayerFleet() != null && Global.getSector().getPlayerFleet().isInHyperspace();
300 }
301 }
302
303 if ((float) Math.random() < probStartInHyper || forceStartInHyper) {
305 } else {
306 system.addEntity(fleet);
307 }
308 fleet.addScript(new DisposableAggroAssignmentAI(fleet, system, this, probStayInHyper));
309 }
310}
311
312
313
314
315
316
317
318
static SectorAPI getSector()
Definition Global.java:65
String getActionOutsideText(StarSystemAPI system, CampaignFleetAPI fleet)
void setLocationAndOrders(CampaignFleetAPI fleet, float probStartInHyper, float probStayInHyper)
LinkedHashMap< String, TimeoutTracker< Boolean > > recentSpawns
String getActionInsideText(StarSystemAPI system, CampaignFleetAPI fleet)
String getTravelText(StarSystemAPI system, CampaignFleetAPI fleet)
static float getDistanceLY(SectorEntityToken from, SectorEntityToken to)
Definition Misc.java:602
static void giveStandardReturnToSourceAssignments(CampaignFleetAPI fleet)
Definition Misc.java:3836
static boolean isFleetReturningToDespawn(CampaignFleetAPI fleet)
Definition Misc.java:3842
void addEntity(SectorEntityToken entity)
void addScript(EveryFrameScript script)
EncounterOption pickEncounterOption(FleetEncounterContextPlugin context, CampaignFleetAPI otherFleet)
void expire(String key, float days)
void set(String key, Object value)