224 Random random =
new Random();
226 if (
p.
seed !=
null) random =
new Random(
p.
seed);
233 Random dmodRandom =
new Random();
234 if (
p.
seed !=
null) dmodRandom = Misc.getRandom(
p.
seed, 5);
236 CoreAutofitPlugin auto =
new CoreAutofitPlugin(
fleet.getCommander());
237 auto.setRandom(random);
239 boolean upgrade = random.nextFloat() < Math.min(0.1f +
p.
quality * 0.5f, 0.5f);
240 auto.setChecked(CoreAutofitPlugin.UPGRADE, upgrade);
246 this.faction =
fleet.getFaction();
260 SortedWeapons nonPriorityWeapons =
new SortedWeapons();
261 SortedWeapons priorityWeapons =
new SortedWeapons();
264 Set<String> weaponCategories =
new LinkedHashSet<String>();
265 for (String weaponId :
faction.getKnownWeapons()) {
272 throw new RuntimeException(
"Weapon with spec id [" + weaponId +
"] not found");
275 int tier = spec.getTier();
276 String cat = spec.getAutofitCategory();
279 List<AvailableWeapon> list = priorityWeapons.getWeapons(tier).getWeapons(cat).getWeapons(spec.getSize());
280 list.add(
new AvailableWeaponImpl(spec, 1000));
282 List<AvailableWeapon> list = nonPriorityWeapons.getWeapons(tier).getWeapons(cat).getWeapons(spec.getSize());
283 list.add(
new AvailableWeaponImpl(spec, 1000));
285 weaponCategories.add(cat);
288 ListMap<AvailableFighter> nonPriorityFighters =
new ListMap<AvailableFighter>();
289 ListMap<AvailableFighter> priorityFighters =
new ListMap<AvailableFighter>();
290 Set<String> fighterCategories =
new LinkedHashSet<String>();
291 for (String wingId :
faction.getKnownFighters()) {
296 throw new RuntimeException(
"Fighter wing with spec id [" + wingId +
"] not found");
301 String cat = spec.getAutofitCategory();
306 priorityFighters.add(cat,
new AvailableFighterImpl(spec, 1000));
308 nonPriorityFighters.add(cat,
new AvailableFighterImpl(spec, 1000));
310 fighterCategories.add(cat);
319 boolean forceAutofit =
fleet.getMemoryWithoutUpdate().getBoolean(MemFlags.MEMORY_KEY_FORCE_AUTOFIT_ON_NO_AUTOFIT_SHIPS);
321 for (FleetMemberAPI member :
fleet.getFleetData().getMembersListCopy()) {
323 if (!forceAutofit && member.getHullSpec().hasTag(Tags.TAG_NO_AUTOFIT)) {
326 if (!forceAutofit && member.getVariant() !=
null && member.getVariant().hasTag(Tags.TAG_NO_AUTOFIT)) {
330 if (!
faction.isPlayerFaction()) {
331 if (!forceAutofit && member.getHullSpec().hasTag(Tags.TAG_NO_AUTOFIT_UNLESS_PLAYER)) {
334 if (!forceAutofit && member.getVariant() !=
null && member.getVariant().hasTag(Tags.TAG_NO_AUTOFIT_UNLESS_PLAYER)) {
340 if (
p.
seed !=
null) {
341 int extra = member.getShipName().hashCode();
342 random =
new Random(
p.
seed * extra);
343 auto.setRandom(random);
344 dmodRandom = Misc.getRandom(
p.
seed * extra, 5);
347 List<WeaponSize> sizes =
new ArrayList<WeaponAPI.WeaponSize>();
348 sizes.add(WeaponSize.SMALL);
349 sizes.add(WeaponSize.MEDIUM);
350 sizes.add(WeaponSize.LARGE);
352 weapons =
new ArrayList<AvailableWeapon>();
353 for (String cat : weaponCategories) {
354 for (WeaponSize size : sizes) {
355 boolean foundSome =
false;
356 for (
int tier = 0; tier < 4; tier++) {
358 if (this.p.allWeapons !=
null &&
this.p.allWeapons) {
362 List<AvailableWeapon> priority = priorityWeapons.getWeapons(tier).getWeapons(cat).getWeapons(size);
363 List<AvailableWeapon> nonPriority = nonPriorityWeapons.getWeapons(tier).getWeapons(cat).getWeapons(size);
369 boolean tierAvailable = random.nextFloat() <
p;
370 if (!tierAvailable && foundSome)
continue;
375 case LARGE: num = 2;
break;
376 case MEDIUM: num = 2;
break;
377 case SMALL: num = 2;
break;
383 if (this.p.allWeapons !=
null &&
this.p.allWeapons) {
387 Set<Integer> picks =
makePicks(num, priority.size(), random);
388 for (Integer index : picks) {
389 AvailableWeapon w = priority.get(index);
396 picks =
makePicks(num, nonPriority.size(), random);
397 for (Integer index : picks) {
398 AvailableWeapon w = nonPriority.get(index);
407 fighters =
new ArrayList<AvailableFighter>();
408 for (String cat : fighterCategories) {
409 List<AvailableFighter> priority = priorityFighters.get(cat);
411 boolean madePriorityPicks =
false;
412 if (priority !=
null) {
413 int num = random.nextInt(2) + 1;
414 if (this.p.
allWeapons !=
null &&
this.p.allWeapons) {
418 Set<Integer> picks =
makePicks(num, priority.size(), random);
419 for (Integer index : picks) {
420 AvailableFighter f = priority.get(index);
422 madePriorityPicks =
true;
426 if (!madePriorityPicks) {
427 int num = random.nextInt(2) + 1;
428 if (this.p.
allWeapons !=
null &&
this.p.allWeapons) {
432 List<AvailableFighter> nonPriority = nonPriorityFighters.get(cat);
433 Set<Integer> picks =
makePicks(num, nonPriority.size(), random);
434 for (Integer index : picks) {
435 AvailableFighter f = nonPriority.get(index);
442 ShipVariantAPI target = member.getVariant();
443 if (target.getOriginalVariant() !=
null) {
448 if (
faction.isPlayerFaction()) {
450 List<ShipVariantAPI> targets =
Global.
getSector().getAutofitVariants().getTargetVariants(member.getHullId());
451 WeightedRandomPicker<ShipVariantAPI> alts =
new WeightedRandomPicker<ShipVariantAPI>(random);
452 for (ShipVariantAPI curr : targets) {
453 if (curr.getHullSpec().getHullId().equals(target.getHullSpec().getHullId())) {
457 if (!alts.isEmpty()) {
458 target = alts.pick();
467 if (target.isStockVariant()) {
468 currVariant.setOriginalVariant(target.getHullVariantId());
471 float rProb =
faction.getDoctrine().getAutofitRandomizeProbability();
473 boolean randomize = random.nextFloat() < rProb;
474 if (member.isStation()) randomize =
false;
475 auto.setChecked(CoreAutofitPlugin.RANDOMIZE, randomize);
507 fleet.getFleetData().setSyncNeeded();
508 fleet.getFleetData().syncIfNeeded();