Class RandomOperationNameGenerator

java.lang.Object
mekhq.campaign.mission.contract.contractGeneration.RandomOperationNameGenerator

public class RandomOperationNameGenerator extends Object
Supplies the two word pools used to build a contract's operation codename (for example, RED and EAGLE in "Operation RED EAGLE"). One shared, objective-agnostic descriptor pool provides the leading modifier; a per-ContractObjectiveType noun pool provides the head word, which is where the objective flavor lives. A name is one weighted draw from the descriptor pool combined with one weighted draw from the objective's noun pool, so a pool of D descriptors and N nouns yields D * N distinct names per objective.

Structured after RandomCompanyNameGenerator: a lazily created, double-checked singleton whose pools are loaded from word,weight CSV files (a base file plus an optional userdata/ override merged on top). The file paths are the explicit per-pool constants in MHQConstants. The singleton is created during startup data loading so the pools are normally ready long before the first contract is generated.

Thread safety: a single background thread builds every pool into local maps that no other thread can see, then publishes them all at once through volatile fields before marking the generator initialized. Published pools are never modified afterwards, so readers only ever see complete pools. A draw that arrives while loading is still in progress waits (up to LOAD_WAIT_SECONDS) for it to finish rather than failing straight away.

Save File Formatting: word,weight. The word is a String that does not include a ','; weight is an integer weight used during generation. The first line of each file is a header and is skipped.

  • Method Details

    • getWeightedDescriptor

      public static megamek.common.util.weightedMaps.WeightedIntMap<String> getWeightedDescriptor()
    • getWeightedNoun

      public static megamek.common.util.weightedMaps.WeightedIntMap<String> getWeightedNoun(ContractObjectiveType objectiveType)
    • getInstance

      public static RandomOperationNameGenerator getInstance()
      Returns the singleton instance, creating it and starting the background load of every word pool on first use. Applies the double-checked locking pattern so only one instance is ever created.
      Returns:
      the RandomOperationNameGenerator instance
    • generateDescriptor

      @Nullable public String generateDescriptor()
      Draws a random descriptor (the leading modifier word).
      Returns:
      a weighted-random descriptor, or null if the pool did not finish loading in time or is empty
    • generateNoun

      @Nullable public String generateNoun(ContractObjectiveType objectiveType)
      Draws a random noun (the head word) themed to the given objective. Falls back to the ContractObjectiveType.UNDEFINED pool when the objective's own pool is empty (for example, when its data file is missing).
      Parameters:
      objectiveType - the contract's objective, selecting the noun pool
      Returns:
      a weighted-random noun, or null if no pool is available or the pools did not finish loading in time