Ejemplo n.º 1
0
  /**
   * Selects a unit from the entries in the table that pass the filter
   *
   * @param filter - the function that determines which units are permitted; if null, no filter is
   *     applied.
   * @return - the selected unit, or null if no units pass the filter.
   */
  public MechSummary generateUnit(UnitFilter filter) {
    int roll = Compute.randomInt(100);
    if (roll < salvagePct) {
      MechSummary ms = generateSalvage(filter);
      if (ms != null) {
        return ms;
      }
    }
    List<TableEntry> useUnitList = unitTable;
    int unitMapSize = unitTotal;
    if (filter != null) {
      useUnitList =
          unitTable
              .stream()
              .filter(te -> filter.include(te.getUnitEntry()))
              .collect(Collectors.toList());
      unitMapSize = useUnitList.stream().mapToInt(te -> te.weight).sum();
    }

    if (unitMapSize > 0) {
      roll = Compute.randomInt(unitMapSize);
      for (TableEntry te : useUnitList) {
        if (roll < te.weight) {
          return te.getUnitEntry();
        }
        roll -= te.weight;
      }
    }
    assert (unitMapSize == 0);
    return null;
  }
Ejemplo n.º 2
0
 /**
  * Selects a faction from the salvage list and generates a table using the same parameters as this
  * table, but from five years earlier. Generated tables are cached for later use. If the generated
  * table contains no units, it is discarded and the selected entry is deleted. This continues
  * until either a unit is generated or there are no remaining entries.
  *
  * @param filter - passed to generateUnit() in the generated table.
  * @return - a unit generated from another faction, or null if none of the factions in the salvage
  *     list contain any units that meet the parameters.
  */
 private MechSummary generateSalvage(UnitFilter filter) {
   while (salvageTotal > 0) {
     int roll = Compute.randomInt(salvageTotal);
     TableEntry salvageEntry = null;
     for (TableEntry te : salvageTable) {
       if (roll < te.weight) {
         salvageEntry = te;
         break;
       }
       roll -= te.weight;
     }
     if (salvageEntry != null) {
       UnitTable salvage =
           UnitTable.findTable(
               salvageEntry.getSalvageFaction(),
               key.getUnitType(),
               key.getYear() - 5,
               key.getRating(),
               key.getWeightClasses(),
               key.getNetworkMask(),
               key.getMovementModes(),
               key.getRoles(),
               key.getRoleStrictness(),
               key.getFaction());
       if (salvage.hasUnits()) {
         return salvage.generateUnit(filter);
       } else {
         salvageTotal -= salvageEntry.weight;
         salvageTable.remove(salvageEntry);
       }
     }
   }
   assert (salvageTable.isEmpty() && salvageTotal == 0);
   return null;
 }