Starsector API
Loading...
Searching...
No Matches
DisposableHostileActivityFleetManager.java
Go to the documentation of this file.
1package com.fs.starfarer.api.impl.campaign.intel.events;
2
3import java.util.Random;
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.econ.MarketAPI;
9import com.fs.starfarer.api.impl.campaign.fleets.DisposableFleetManager;
10import com.fs.starfarer.api.impl.campaign.ids.Factions;
11import com.fs.starfarer.api.util.Misc;
12import com.fs.starfarer.api.util.WeightedRandomPicker;
13
19public class DisposableHostileActivityFleetManager extends DisposableFleetManager {
20
21 protected Random random = new Random();
22
24 //updateSpawnRateMult();
25 }
26
27 protected Object readResolve() {
28 super.readResolve();
29 return this;
30 }
31
32
33 @Override
34 protected String getSpawnId() {
35 return "hostile_activity";
36 }
37
39 if (currSpawnLoc == null) return null;
41 }
42
43 @Override
46 if (intel == null) return 0;
47
48 float delay = 30f;
49 if (!Global.getSettings().isDevMode()) {
50 delay = 90f;
51 }
52 Long timestamp = intel.getPlayerVisibleTimestamp();
53 if (timestamp != null) {
54 float daysSince = Global.getSector().getClock().getElapsedDaysSince(timestamp);
55 if (daysSince < delay) return 0;
56 }
57
58 if (currSpawnLoc != null) {
59 boolean longEnough = false;
60 for (MarketAPI market : Misc.getMarketsInLocation(currSpawnLoc, Factions.PLAYER)) {
61 if (market.getDaysInExistence() >= delay) {
62 longEnough = true;
63 }
64 }
65 if (!longEnough) {
66 return 0;
67 }
68 }
69
70 float mag = intel.getTotalActivityMagnitude(currSpawnLoc);
71
72 // less than half the fleets when market presence is minimal
73 float mag2 = intel.getMarketPresenceFactor(currSpawnLoc);
74 mag = Misc.interpolate(mag, mag2, 0.6f);
75
76 //float mag2 = intel.getTotalActivityMagnitude(true);
77 // about half the fleets when effect is fully suppressed
78 // keeping the fleets the same size as w/o suppression, though; this is handled
79 // in BaseHostileActivityPlugin.getEffectMagnitudeAdjustedBySuppression()
80 //mag = Misc.interpolate(mag, mag2, 0.5f);
81
82 if (mag <= 0f) return 0;
83 //if (mag > 2f) mag = 2f;
84 if (mag > 1f) mag = 1f;
85
86 float desiredNumFleets = 1f;
87
88 float max = Global.getSettings().getFloat("maxHostileActivityFleetsPerSystem");
89
90 float mult = intel.getNumFleetsMultiplier(currSpawnLoc);
91
92 desiredNumFleets += (int)Math.round(mag * (max - 1f) * mult);
93
94 return (int) Math.round(desiredNumFleets);
95 }
96
97
98 protected StarSystemAPI pickCurrentSpawnLocation() {
100 }
101 protected StarSystemAPI pickNearestPopulatedSystem() {
102 if (Global.getSector().isInNewGameAdvance()) return null;
103 CampaignFleetAPI player = Global.getSector().getPlayerFleet();
104 if (player == null) return null;
105 StarSystemAPI nearest = null;
106
107 float minDist = Float.MAX_VALUE;
108
109
110// List<MarketAPI> markets = Misc.getPlayerMarkets(false);
111// List<StarSystemAPI> systems = new ArrayList<StarSystemAPI>();
112// for (MarketAPI market : markets) {
113// StarSystemAPI system = market.getStarSystem();
114// if (system != null && !systems.contains(system)) {
115// systems.add(system);
116// }
117// }
118
119// for (IntelInfoPlugin intel : Global.getSector().getIntelManager().getIntel(HostileActivityIntel.class)) {
120// StarSystemAPI system = ((HostileActivityIntel)intel).getSystem();
121 for (StarSystemAPI system : Misc.getSystemsWithPlayerColonies(false)) {
122
123 float distToPlayerLY = Misc.getDistanceLY(player.getLocationInHyperspace(), system.getLocation());
124 if (distToPlayerLY > MAX_RANGE_FROM_PLAYER_LY) continue;
125
126 if (distToPlayerLY < minDist) {
127 //if (system.getStar() != null && system.getStar().getSpec().isPulsar()) continue;
128 nearest = system;
129 minDist = distToPlayerLY;
130 }
131 }
132
133 // stick with current system longer unless something else is closer
134 if (nearest == null && currSpawnLoc != null) {
135 float distToPlayerLY = Misc.getDistanceLY(player.getLocationInHyperspace(), currSpawnLoc.getLocation());
136 if (distToPlayerLY <= DESPAWN_RANGE_LY) {
137 nearest = currSpawnLoc;
138 }
139 }
140
141 return nearest;
142 }
143
144 protected CampaignFleetAPI spawnFleetImpl() {
145 StarSystemAPI system = currSpawnLoc;
146 if (system == null) return null;
147
148 CampaignFleetAPI player = Global.getSector().getPlayerFleet();
149 if (player == null) return null;
150
152 if (intel == null) return null;
153
154 String idKey = "$dhafm_ID";
155
156 WeightedRandomPicker<HostileActivityFactor> picker = new WeightedRandomPicker<HostileActivityFactor>(random);
157
158 for (EventFactor factor : intel.getFactors()) {
159 if (!(factor instanceof HostileActivityFactor)) {
160 continue;
161 }
162 //if (factor instanceof LuddicPathHostileActivityFactor) continue;
163
165 int count = 0;
166 for (ManagedFleetData data : active) {
167 if (data.fleet != null &&
168 curr.getId().equals(data.fleet.getMemoryWithoutUpdate().getString(idKey))) {
169 count++;
170 }
171 }
172 if (count < curr.getMaxNumFleets(currSpawnLoc)) {
173 picker.add(curr, curr.getSpawnFrequency(currSpawnLoc));
174 }
175 }
176
177 HostileActivityFactor pick = picker.pick();
178 if (pick == null) return null;
179
180
181 CampaignFleetAPI fleet = pick.createFleet(currSpawnLoc, random);
182 if (fleet == null || fleet.isEmpty()) return null;
183
184 fleet.getMemoryWithoutUpdate().set(idKey, pick.getId());
185
186 setLocationAndOrders(fleet, pick.getSpawnInHyperProbability(currSpawnLoc), pick.getStayInHyperProbability(currSpawnLoc));
187
188 return fleet;
189 }
190
191}
192
193
194
195
196
197
198
199
static SettingsAPI getSettings()
Definition Global.java:51
static SectorAPI getSector()
Definition Global.java:59
CampaignFleetAPI createFleet(StarSystemAPI system, Random random)