Starsector API
Loading...
Searching...
No Matches
DModManager.java
Go to the documentation of this file.
1package com.fs.starfarer.api.impl.campaign;
2
3import java.util.ArrayList;
4import java.util.Arrays;
5import java.util.Iterator;
6import java.util.List;
7import java.util.Random;
8
9import com.fs.starfarer.api.Global;
10import com.fs.starfarer.api.campaign.CampaignFleetAPI;
11import com.fs.starfarer.api.campaign.FleetEncounterContextPlugin.FleetMemberData;
12import com.fs.starfarer.api.campaign.FleetEncounterContextPlugin.Status;
13import com.fs.starfarer.api.combat.ShieldAPI.ShieldType;
14import com.fs.starfarer.api.combat.ShipHullSpecAPI;
15import com.fs.starfarer.api.combat.ShipHullSpecAPI.ShipTypeHints;
16import com.fs.starfarer.api.combat.ShipVariantAPI;
17import com.fs.starfarer.api.fleet.FleetMemberAPI;
18import com.fs.starfarer.api.impl.campaign.ids.HullMods;
19import com.fs.starfarer.api.impl.campaign.ids.Stats;
20import com.fs.starfarer.api.impl.campaign.ids.Tags;
21import com.fs.starfarer.api.loading.HullModSpecAPI;
22import com.fs.starfarer.api.loading.VariantSource;
23import com.fs.starfarer.api.plugins.DModAdderPlugin;
24import com.fs.starfarer.api.plugins.DModAdderPlugin.DModAdderParams;
25import com.fs.starfarer.api.util.Misc;
26import com.fs.starfarer.api.util.WeightedRandomPicker;
27
28public class DModManager {
29
30// public static final String HULLMOD_DAMAGE = "damage";
31// public static final String HULLMOD_PHASE_ALWAYS = "phaseAlways";
32// public static final String HULLMOD_DAMAGE_STRUCT = "damageStruct";
33// public static final String HULLMOD_DESTROYED_ALWAYS = "destroyedAlways";
34// public static final String HULLMOD_FIGHTER_BAY_DAMAGE = "fighterBayDamage";
35// public static final String HULLMOD_CARRIER_ALWAYS = "carrierAlways";
36
37
38 public static int MAX_DMODS_FROM_COMBAT = Global.getSettings().getInt("maxDModsAddedByCombat");
39
40 public static boolean setDHull(ShipVariantAPI variant) {
41 //if (!variant.getHullSpec().isDHull()) {
42 variant.setSource(VariantSource.REFIT);
43 //if (!variant.isDHull()) {
44 if (!variant.getHullSpec().isDefaultDHull()) {
45 String dHullId = Misc.getDHullId(variant.getHullSpec());
46 ShipHullSpecAPI dHull = Global.getSettings().getHullSpec(dHullId);
47 variant.setHullSpecAPI(dHull);
48 return true;
49 }
50 return false;
51 }
52
53 public static int reduceNextDmodsBy = 0;
54
58 public static void addDMods(FleetMemberData data, boolean own, CampaignFleetAPI recoverer, Random random) {
59 addDMods(data.getMember(), data.getStatus() == Status.DESTROYED, own, recoverer, random);
60 }
61
62 public static void addDMods(FleetMemberAPI member, boolean destroyed, boolean own, CampaignFleetAPI recoverer, Random random) {
63 ShipVariantAPI variant = member.getVariant();
64 addDMods(variant, destroyed, own, recoverer, random);
65 }
66 public static void addDMods(ShipVariantAPI variant, boolean destroyed, boolean own, CampaignFleetAPI recoverer, Random random) {
67 //int original = getNumDMods(variant);
68 if (random == null) random = new Random();
69
70// if (own) {
71// System.out.println("323ffwe");
72// }
73 DModAdderParams params = new DModAdderParams();
74 params.variant = variant;
75 params.destroyed = destroyed;
76 params.own = own;
77 params.recoverer = recoverer;
78 params.random = random;
79 DModAdderPlugin plugin = Global.getSector().getGenericPlugins().pickPlugin(DModAdderPlugin.class, params);
80 if (plugin != null) {
81 plugin.addDMods(params);
82 return;
83 }
84
85
86 if (destroyed) {
87 addAllPermaModsWithTags(variant, Tags.HULLMOD_DESTROYED_ALWAYS);
88// if (own) {
89// int added = getNumDMods(variant) - original;
90// if (added > 0) return;
91// }
92 }
93
94// if (member.getHullSpec().getHints().contains(ShipTypeHints.CIVILIAN)) {
95// addAllPermaModsWithTags(variant, Tags.HULLMOD_CIV_ALWAYS);
96// }
97
98 List<HullModSpecAPI> potentialMods = getModsWithTags(Tags.HULLMOD_DAMAGE);
99 removeUnsuitedMods(variant, potentialMods);
100
101 boolean hasStructDamage = getNumDMods(variant, Tags.HULLMOD_DAMAGE_STRUCT) > 0;
102 if (hasStructDamage) {
103 potentialMods = getModsWithoutTags(potentialMods, Tags.HULLMOD_DAMAGE_STRUCT);
104 }
105
106 //if (variant.getHullSpec().getFighterBays() > 0 || variant.isCarrier()) {
107 if (variant.getHullSpec().getFighterBays() > 0) {
108 potentialMods.addAll(getModsWithTags(Tags.HULLMOD_FIGHTER_BAY_DAMAGE));
109 }
110 //if (variant.getHullSpec().getDefenseType() == ShieldType.PHASE) {
111 if (variant.getHullSpec().isPhase()) {
112 potentialMods.addAll(getModsWithTags(Tags.HULLMOD_DAMAGE_PHASE));
113 }
114
115// if (variant.isCarrier()) {
116// if (own || true) { // bit too harsh to always add damaged flight decks to recovered enemy carriers
117// potentialMods.addAll(getModsWithTags(Tags.HULLMOD_CARRIER_ALWAYS));
118// } else {
119// addAllPermaModsWithTags(variant, Tags.HULLMOD_CARRIER_ALWAYS);
120// }
121// }
122
123 removeModsAlreadyInVariant(variant, potentialMods);
124
125 int num = 2 + random.nextInt(3);
126
127 int reduction = 0;
128 reduction += reduceNextDmodsBy;
130 if (recoverer != null) {
131 int extra = (int) recoverer.getStats().getDynamic().getValue(Stats.SHIP_DMOD_REDUCTION, 0);
132 if (extra >= 0) {
133 extra = random.nextInt(extra + 1);
134 } else {
135 extra = -1 * random.nextInt(-extra + 1);
136 }
137 reduction += extra;
138 }
139
140 num -= reduction;
141 if (num < 1) num = 1;
142
143 int already = getNumDMods(variant);
144
145 int add = num - already;
146 if (own) {
147 add = (1 - reduction);
148 }
149
150 if (add + already > MAX_DMODS_FROM_COMBAT) {
151 add = MAX_DMODS_FROM_COMBAT - already;
152 }
153 if (add <= 0) return;
154
155
156 WeightedRandomPicker<HullModSpecAPI> picker = new WeightedRandomPicker<HullModSpecAPI>(random);
157 picker.addAll(potentialMods);
158 for (int i = 0; i < add && !picker.isEmpty(); i++) {
159 HullModSpecAPI pick = picker.pickAndRemove();
160 if (pick != null) {
161 if (pick.hasTag(Tags.HULLMOD_DAMAGE_STRUCT) && getNumDMods(variant, Tags.HULLMOD_DAMAGE_STRUCT) > 0) {
162 i--;
163 continue;
164 }
165 variant.removeSuppressedMod(pick.getId());
166 variant.addPermaMod(pick.getId(), false);
167 }
168 }
169 }
170
171
172 public static void addDMods(FleetMemberAPI member, boolean canAddDestroyedMods, int num, Random random) {
173 ShipVariantAPI variant = member.getVariant();
174 addDMods(variant, canAddDestroyedMods, num, random);
175 }
176 public static void addDMods(ShipVariantAPI variant, boolean canAddDestroyedMods, int num, Random random) {
177 if (random == null) random = new Random();
178
179 DModAdderParams params = new DModAdderParams();
180 params.variant = variant;
181 params.canAddDestroyedMods = canAddDestroyedMods;
182 params.num = num;
183 params.random = random;
184 DModAdderPlugin plugin = Global.getSector().getGenericPlugins().pickPlugin(DModAdderPlugin.class, params);
185 if (plugin != null) {
186 plugin.addDMods(params);
187 return;
188 }
189
190
191// if (member.getHullSpec().getHints().contains(ShipTypeHints.CIVILIAN)) {
192// int added = addAllPermaModsWithTags(variant, Tags.HULLMOD_CIV_ALWAYS);
193// if (added > 0) {
194// num -= added;
195// if (num <= 0) return;
196// }
197// }
198
199 List<HullModSpecAPI> potentialMods = getModsWithTags(Tags.HULLMOD_DAMAGE);
200 if (canAddDestroyedMods) potentialMods.addAll(getModsWithTags(Tags.HULLMOD_DESTROYED_ALWAYS));
201
202 removeUnsuitedMods(variant, potentialMods);
203
204 boolean hasStructDamage = getNumDMods(variant, Tags.HULLMOD_DAMAGE_STRUCT) > 0;
205 if (hasStructDamage) {
206 potentialMods = getModsWithoutTags(potentialMods, Tags.HULLMOD_DAMAGE_STRUCT);
207 }
208
209 if (variant.getHullSpec().getFighterBays() > 0) {
210 //if (variant.getHullSpec().getFighterBays() > 0 || variant.isCarrier()) {
211 potentialMods.addAll(getModsWithTags(Tags.HULLMOD_FIGHTER_BAY_DAMAGE));
212 }
213 //if (variant.getHullSpec().getDefenseType() == ShieldType.PHASE) {
214 if (variant.getHullSpec().isPhase()) {
215 potentialMods.addAll(getModsWithTags(Tags.HULLMOD_DAMAGE_PHASE));
216 }
217
218 if (variant.isCarrier()) {
219 potentialMods.addAll(getModsWithTags(Tags.HULLMOD_CARRIER_ALWAYS));
220 }
221
222 potentialMods = new ArrayList<HullModSpecAPI>(potentialMods);
223
224 removeModsAlreadyInVariant(variant, potentialMods);
225
226// System.out.println("");
227// System.out.println("Adding: ");
228 WeightedRandomPicker<HullModSpecAPI> picker = new WeightedRandomPicker<HullModSpecAPI>(random);
229 picker.addAll(potentialMods);
230 int added = 0;
231 for (int i = 0; i < num && !picker.isEmpty(); i++) {
232 HullModSpecAPI pick = picker.pickAndRemove();
233 if (pick != null) {
234 if (pick.hasTag(Tags.HULLMOD_DAMAGE_STRUCT) && getNumDMods(variant, Tags.HULLMOD_DAMAGE_STRUCT) > 0) {
235 i--;
236 continue;
237 }
238 variant.removeSuppressedMod(pick.getId());
239 variant.addPermaMod(pick.getId(), false);
240 //System.out.println("Mod: " + pick.getId());
241 added++;
242 }
243 }
244// if (getNumDMods(variant) < 5) {
245// System.out.println("ewfwefew");
246// }
247 }
248
249
250 public static boolean assumeAllShipsAreAutomated = false;
251
252 public static void removeUnsuitedMods(ShipVariantAPI variant, List<HullModSpecAPI> mods) {
253 boolean auto = variant.hasHullMod(HullMods.AUTOMATED);
254 if (assumeAllShipsAreAutomated) auto = true;
255 boolean civ = variant.getHullSpec().getHints().contains(ShipTypeHints.CIVILIAN);
256 //boolean phase = variant.getHullSpec().getDefenseType() == ShieldType.PHASE;
257 boolean phase = variant.getHullSpec().isPhase();
258 boolean peakTime = variant.getHullSpec().getNoCRLossTime() < 10000;
259 boolean shields = variant.getHullSpec().getDefenseType() == ShieldType.FRONT ||
260 variant.getHullSpec().getDefenseType() == ShieldType.OMNI;
261
262 Iterator<HullModSpecAPI> iter = mods.iterator();
263 while (iter.hasNext()) {
264 HullModSpecAPI curr = iter.next();
265 if (!peakTime && curr.hasTag(Tags.HULLMOD_PEAK_TIME)) {
266 iter.remove();
267 continue;
268 }
269 if (phase && curr.hasTag(Tags.HULLMOD_NOT_PHASE)) {
270 iter.remove();
271 continue;
272 }
273 if (auto && curr.hasTag(Tags.HULLMOD_NOT_AUTO)) {
274 iter.remove();
275 continue;
276 }
277 if (civ && curr.hasTag(Tags.HULLMOD_NOT_CIV)) {
278 iter.remove();
279 continue;
280 }
281 if (civ && !curr.hasTag(Tags.HULLMOD_CIV) && !curr.hasTag(Tags.HULLMOD_CIV_ONLY)) {
282 iter.remove();
283 continue;
284 }
285 if (!civ && curr.hasTag(Tags.HULLMOD_CIV_ONLY)) {
286 iter.remove();
287 continue;
288 }
289 if (!shields && curr.hasTag(Tags.HULLMOD_REQ_SHIELDS)) {
290 iter.remove();
291 continue;
292 }
293 }
294 }
295 public static void removeModsAlreadyInVariant(ShipVariantAPI variant, List<HullModSpecAPI> mods) {
296 Iterator<HullModSpecAPI> iter = mods.iterator();
297 while (iter.hasNext()) {
298 HullModSpecAPI curr = iter.next();
299 if (variant.hasHullMod(curr.getId())) iter.remove();
300 }
301 }
302
303 public static int addAllPermaModsWithTags(ShipVariantAPI variant, String ... tags) {
304 int added = 0;
305 for (HullModSpecAPI mod : getModsWithTags(tags)) {
306 if (!variant.hasHullMod(mod.getId())) added++;
307 variant.removeSuppressedMod(mod.getId());
308 variant.addPermaMod(mod.getId(), false);
309 }
310 return added;
311 }
312
313 public static List<HullModSpecAPI> getModsWithoutTags(List<HullModSpecAPI> mods, String ... tags) {
314 List<HullModSpecAPI> result = new ArrayList<HullModSpecAPI>();
315 OUTER: for (HullModSpecAPI mod : mods) {
316 for (String tag : tags) {
317 if (mod.hasTag(tag)) continue OUTER;
318 }
319 result.add(mod);
320 }
321 return result;
322 }
323
324 public static List<HullModSpecAPI> getModsWithTags(String ... tags) {
325 List<HullModSpecAPI> result = new ArrayList<HullModSpecAPI>();
326 for (HullModSpecAPI mod : Global.getSettings().getAllHullModSpecs()) {
327 if (mod.getTags().containsAll(Arrays.asList(tags))) {
328 result.add(mod);
329 }
330 }
331 return result;
332 }
333
334 public static int getNumDMods(ShipVariantAPI variant) {
335 int count = 0;
336 for (String id : variant.getHullMods()) {
337 if (getMod(id).hasTag(Tags.HULLMOD_DMOD)) count++;
338 }
339 return count;
340 }
341
342 public static int getNumNonBuiltInDMods(ShipVariantAPI variant) {
343 int count = 0;
344 for (String id : variant.getHullMods()) {
345 if (getMod(id).hasTag(Tags.HULLMOD_DMOD)) {
346 if (variant.getHullSpec().getBuiltInMods().contains(id)) continue;
347 count++;
348 }
349 }
350 return count;
351 }
352
353 public static int getNumDMods(ShipVariantAPI variant, String ... tags) {
354 int count = 0;
355 for (String id : variant.getHullMods()) {
356 HullModSpecAPI mod = getMod(id);
357 if (!mod.getTags().containsAll(Arrays.asList(tags))) continue;
358 if (mod.hasTag(Tags.HULLMOD_DMOD)) count++;
359 }
360 return count;
361 }
362
363 public static HullModSpecAPI getMod(String id) {
364 return Global.getSettings().getHullModSpec(id);
365 }
366
367 public static void removeDMod(ShipVariantAPI v, String id) {
368 ShipHullSpecAPI base = v.getHullSpec().getDParentHull();
369
370 // so that a skin with dmods can be "restored" - i.e. just dmods suppressed w/o changing to
371 // actual base skin
372 if (!v.getHullSpec().isDefaultDHull() && !v.getHullSpec().isRestoreToBase()) {
373 base = v.getHullSpec();
374 }
375 if (base == null && v.getHullSpec().isRestoreToBase()) {
376 base = v.getHullSpec().getBaseHull();
377 }
378 if (base.isBuiltInMod(id)) {
379 v.removePermaMod(id);
380 v.addSuppressedMod(id);
381 } else {
382 v.removePermaMod(id);
383 v.removeMod(id);
384 }
385 }
386}
387
388
389
390
391
static SettingsAPI getSettings()
Definition Global.java:51
static SectorAPI getSector()
Definition Global.java:59
static void addDMods(ShipVariantAPI variant, boolean destroyed, boolean own, CampaignFleetAPI recoverer, Random random)
static List< HullModSpecAPI > getModsWithTags(String ... tags)
static void addDMods(FleetMemberAPI member, boolean canAddDestroyedMods, int num, Random random)
static int getNumNonBuiltInDMods(ShipVariantAPI variant)
static void addDMods(FleetMemberData data, boolean own, CampaignFleetAPI recoverer, Random random)
static boolean setDHull(ShipVariantAPI variant)
static List< HullModSpecAPI > getModsWithoutTags(List< HullModSpecAPI > mods, String ... tags)
static HullModSpecAPI getMod(String id)
static void addDMods(ShipVariantAPI variant, boolean canAddDestroyedMods, int num, Random random)
static int addAllPermaModsWithTags(ShipVariantAPI variant, String ... tags)
static void removeUnsuitedMods(ShipVariantAPI variant, List< HullModSpecAPI > mods)
static void addDMods(FleetMemberAPI member, boolean destroyed, boolean own, CampaignFleetAPI recoverer, Random random)
static int getNumDMods(ShipVariantAPI variant)
static int getNumDMods(ShipVariantAPI variant, String ... tags)
static void removeModsAlreadyInVariant(ShipVariantAPI variant, List< HullModSpecAPI > mods)
static void removeDMod(ShipVariantAPI v, String id)
ShipHullSpecAPI getHullSpec(String hullId)
HullModSpecAPI getHullModSpec(String modId)
List< HullModSpecAPI > getAllHullModSpecs()