Starsector API
Loading...
Searching...
No Matches
SourceBasedFleetManager.java
Go to the documentation of this file.
1package com.fs.starfarer.api.impl.campaign.fleets;
2
3import java.util.ArrayList;
4import java.util.Iterator;
5import java.util.List;
6
7import org.lwjgl.util.vector.Vector2f;
8
9import com.fs.starfarer.api.EveryFrameScript;
10import com.fs.starfarer.api.Global;
11import com.fs.starfarer.api.campaign.BattleAPI;
12import com.fs.starfarer.api.campaign.CampaignEventListener.FleetDespawnReason;
13import com.fs.starfarer.api.campaign.CampaignFleetAPI;
14import com.fs.starfarer.api.campaign.SectorEntityToken;
15import com.fs.starfarer.api.campaign.listeners.FleetEventListener;
16import com.fs.starfarer.api.util.Misc;
17
27public abstract class SourceBasedFleetManager implements FleetEventListener, EveryFrameScript {
28
29 public static float DESPAWN_THRESHOLD_PAD_LY = 1;
30 public static float DESPAWN_MIN_DIST_LY = 3;
31
32 protected List<CampaignFleetAPI> fleets = new ArrayList<CampaignFleetAPI>();
33 protected float thresholdLY = 4f;
34 protected SectorEntityToken source;
35
36 public static boolean DEBUG = true;
37 protected int minFleets;
38 protected int maxFleets;
39 protected float respawnDelay;
40
41 protected float destroyed = 0;
42
43 protected Vector2f sourceLocation = new Vector2f();
44
45 public SourceBasedFleetManager(SectorEntityToken source, float thresholdLY, int minFleets, int maxFleets, float respawnDelay) {
46 this.source = source;
47 this.thresholdLY = thresholdLY;
48 this.minFleets = minFleets;
49 this.maxFleets = maxFleets;
50 this.respawnDelay = respawnDelay;
51 }
52
53 public float getThresholdLY() {
54 return thresholdLY;
55 }
56
57 public SectorEntityToken getSource() {
58 return source;
59 }
60
61 protected abstract CampaignFleetAPI spawnFleet();
62
63 public void advance(float amount) {
64 CampaignFleetAPI player = Global.getSector().getPlayerFleet();
65
66// if (destroyed > 0) {
67// System.out.println("destroyed: " + destroyed);
68// }
69 float days = Global.getSector().getClock().convertToDays(amount);
70 destroyed -= days / respawnDelay;
71 if (destroyed < 0) destroyed = 0;
72
73
74 // clean up orphaned, juuust in case - could've been directly removed elsewhere, say, instead of despawned.
75 Iterator<CampaignFleetAPI> iter = fleets.iterator();
76 while (iter.hasNext()) {
77 if (!iter.next().isAlive()) {
78 iter.remove();
79 }
80 }
81
82// if (source != null && source.getContainingLocation().getName().toLowerCase().contains("idimmeron")) {
83// System.out.println("wefwefwefw");
84// }
85
86 if (source != null) {
87 if (!source.isAlive()) {
88 source = null;
89 } else {
90 sourceLocation.set(source.getLocationInHyperspace());
91 }
92 }
93
94 float distFromSource = Misc.getDistanceLY(player.getLocationInHyperspace(), sourceLocation);
95 float f = 0f;
96 if (distFromSource < thresholdLY) {
97 f = (thresholdLY - distFromSource) / (thresholdLY * 0.1f);
98 if (f > 1) f = 1;
99 }
100 int currMax = minFleets + Math.round((maxFleets - minFleets) * f);
101 currMax -= Math.ceil(destroyed);
102
103 if (source == null) {
104 currMax = 0;
105 }
106
107 // try to despawn some fleets if above maximum
108 if (currMax < fleets.size()) {
109 for (CampaignFleetAPI fleet : new ArrayList<CampaignFleetAPI>(fleets)) {
110 float distFromPlayer = Misc.getDistanceLY(player.getLocationInHyperspace(), fleet.getLocationInHyperspace());
111 if (distFromPlayer > DESPAWN_MIN_DIST_LY && distFromPlayer > thresholdLY + DESPAWN_THRESHOLD_PAD_LY) {
112 fleet.despawn(FleetDespawnReason.PLAYER_FAR_AWAY, null);
113 if (fleets.size() <= currMax) break;
114 }
115 }
116
117 }
118
119 // spawn some if below maximum
120 if (currMax > fleets.size()) {
121 CampaignFleetAPI fleet = spawnFleet();
122 if (fleet != null) {
123 fleets.add(fleet);
124 //if (shouldAddEventListenerToFleet()) {
125 fleet.addEventListener(this);
126 //}
127 }
128 }
129
130 if (source == null && fleets.size() == 0) {
131 setDone(true);
132 }
133 }
134
135// protected boolean shouldAddEventListenerToFleet() {
136// return true;
137// }
138
139 private boolean done = false;
140 public boolean isDone() {
141 return done;
142 }
143
144 public void setDone(boolean done) {
145 this.done = done;
146 }
147
148 public boolean runWhilePaused() {
149 return false;
150 //return Global.getSettings().isDevMode();
151 }
152
153
154 public void reportFleetDespawnedToListener(CampaignFleetAPI fleet, FleetDespawnReason reason, Object param) {
155 if (reason == FleetDespawnReason.DESTROYED_BY_BATTLE) {
156 destroyed++;
157 }
158 fleets.remove(fleet);
159 }
160
161 public void reportBattleOccurred(CampaignFleetAPI fleet, CampaignFleetAPI primaryWinner, BattleAPI battle) {
162
163 }
164
165// public static void main(String[] args) {
166// int minFleets = 4;
167// int maxFleets = 10;
168// float thresholdLY = 1f;
169//
170// for (float d = 0; d < 3f; d += 0.03f) {
171// float f = 0f;
172// if (d < thresholdLY) {
173// f = (thresholdLY - d) / (thresholdLY * 0.1f);
174// if (f > 1) f = 1;
175// }
176// int numFleets = minFleets + Math.round((maxFleets - minFleets) * f);
177// System.out.println("Num fleets: " + numFleets + " at range " + d);
178// }
179// }
180}
181
182
183
184
static SectorAPI getSector()
Definition Global.java:59
void reportFleetDespawnedToListener(CampaignFleetAPI fleet, FleetDespawnReason reason, Object param)
SourceBasedFleetManager(SectorEntityToken source, float thresholdLY, int minFleets, int maxFleets, float respawnDelay)
void reportBattleOccurred(CampaignFleetAPI fleet, CampaignFleetAPI primaryWinner, BattleAPI battle)