/** * Generates the indicated number of civilian entities. * * @param list Generated entities are added to this list * @param num The number of civilian entities to generate * @param campaign */ protected void addCivilianUnits(ArrayList<Entity> list, int num, Campaign campaign) { RandomUnitGenerator.getInstance().setChosenRAT("CivilianUnits"); ArrayList<MechSummary> msl = RandomUnitGenerator.getInstance().generate(num); List<Entity> entities = msl.stream().map(ms -> createEntityWithCrew("IND", RandomSkillsGenerator.L_GREEN, campaign, ms)) .collect(Collectors.<Entity>toList()); list.addAll(entities); }
public static synchronized RandomUnitGenerator getInstance() { if (null == rug) { rug = new RandomUnitGenerator(); } if (!rug.initialized && !rug.initializing) { rug.initializing = true; interrupted = false; dispose = false; rug.loader = new Thread(new Runnable() { public void run() { long start = System.currentTimeMillis(); rug.populateUnits(); long end = System.currentTimeMillis(); System.out.println("Loaded Rats in: " + (end - start) + "ms."); System.out.flush(); } }, "Random Unit Generator unit populator"); rug.loader.setPriority(Thread.NORM_PRIORITY - 1); rug.loader.start(); } return rug; }
private void parseEraNode(Node eraNode, String name, int era) { Set<String> allRatNames = RandomUnitGenerator.getInstance().getRatMap().keySet(); NodeList nl = eraNode.getChildNodes(); for (int i = 0; i < nl.getLength(); i++) { Node ratNode = nl.item(i); if (ratNode.getNodeName().equals("rat")) { RAT rat = RAT.createFromXml(ratNode); if (rat != null && allRatNames.contains(rat.ratName)) { allRATs.get(name).get(era).add(rat); } } } }
public ArrayList<MechSummary> generate(int numRolls) { return generate(numRolls, getChosenRAT(), null); }
RandomUnitGenerator testRug = Mockito.spy(new RandomUnitGenerator()); testRug.initRats(); Mockito.when(mockRatEntry.getUnits()).thenReturn(testUnits); Mockito.when(mockRatEntry.getWeights()).thenReturn(testWeights); testRug.addRat("mockRat", mockRatEntry); Mockito.when(mockOtherRat.getUnits()).thenReturn(otherUnits); Mockito.when(mockOtherRat.getWeights()).thenReturn(otherWeights); testRug.addRat("Other RAT", mockOtherRat); Mockito.doReturn(mockMech1).when(testRug).getMechByName(M1); MechSummary mockMech2 = Mockito.mock(MechSummary.class); Mockito.doReturn(mockMech2).when(testRug).getMechByName(M2); MechSummary mockMech3 = Mockito.mock(MechSummary.class); Mockito.doReturn(mockMech3).when(testRug).getMechByName(M3); MechSummary mockMech4 = Mockito.mock(MechSummary.class); Mockito.doReturn(mockMech4).when(testRug).getMechByName(M4); MechSummary mockMech5 = Mockito.mock(MechSummary.class); Mockito.doReturn(mockMech5).when(testRug).getMechByName(M5); MechSummary mockMech6 = Mockito.mock(MechSummary.class); Mockito.doReturn(mockMech6).when(testRug).getMechByName(M6); MechSummary mockMech7 = Mockito.mock(MechSummary.class); Mockito.doReturn(mockMech7).when(testRug).getMechByName(M7); MechSummary mockMech8 = Mockito.mock(MechSummary.class); Mockito.doReturn(mockMech8).when(testRug).getMechByName(M8); Mockito.doReturn(0.1).when(testRug).getRandom(); Assert.assertEquals(expected, testRug.generate(1, "mockRat"));
for(Iterator<String> rats = RandomUnitGenerator.getInstance().getRatList(); rats.hasNext();) { String currentName = rats.next(); if(currentName.contains("Turrets")) { RandomUnitGenerator.getInstance().setChosenRAT(ratName); ArrayList<MechSummary> msl = RandomUnitGenerator.getInstance().generate(num); return msl;
Map<String, RatEntry> ratMap = getRatMap(); if (null != ratMap) { RatEntry re = ratMap.get(ratName); double rand = getRandom(); int i = 0; while (i < re.getWeights().size() && rand > re.getWeights().get(i)) { units.addAll(generate(1, name.replaceFirst("@", ""), filter)); continue; MechSummary unit = getMechByName(name); if (null != unit) { units.add(unit);
@Override public List<MechSummary> generate(int count, String faction, int unitType, int weightClass, int year, int quality, Predicate<MechSummary> filter) { RAT rat = findRAT(faction, unitType, weightClass, year, quality); if (rat != null) { if (unitType == UnitType.TANK) { filter = filter.and(ms -> ms.getUnitType().equals("Tank")); } else if (unitType == UnitType.VTOL) { filter = filter.and(ms -> ms.getUnitType().equals("VTOL")); } return RandomUnitGenerator.getInstance().generate(count, rat.ratName, filter); } return new ArrayList<MechSummary>(); }
public synchronized void populateUnits() { initRats(); initRatTree(); // Give the MSC some time to initialize MechSummaryCache msc = MechSummaryCache.getInstance(); long waitLimit = System.currentTimeMillis() + 3000; /* 3 seconds */ while( !interrupted && !msc.isInitialized() && waitLimit > System.currentTimeMillis() ) { try { Thread.sleep(50); } catch(InterruptedException e) { // Ignore } } loadRatsFromDirectory(Configuration.armyTablesDir(), msc); cleanupNode(ratTree); if (!interrupted) { rug.initialized = true; rug.notifyListenersOfInitialization(); } if (dispose) { clear(); dispose = false; } }
while (!RandomUnitGenerator.getInstance().isInitialized()) { try { Thread.sleep(50);
m_clientgui = cl; m_client = cl.getClient(); rug = RandomUnitGenerator.getInstance(); rug.registerListener(this); if (rug.isInitialized()){ m_ratStatus = new JLabel(Messages .getString("RandomArmyDialog.ratStatusDoneLoading"));
/** * Stop processing AtB events and release memory. */ public void shutdownAtB() { RandomFactionGenerator.getInstance().dispose(); RandomUnitGenerator.getInstance().dispose(); RandomNameGenerator.getInstance().dispose(); atbEventProcessor.shutdown(); }
/** * Generate a list of units from the RAT * * @return - a string giving the name */ public ArrayList<MechSummary> generate(int numRolls, String ratName) { return generate(numRolls, ratName, null); }
public void doneLoading() { RandomUnitGenerator.getInstance(); } });
private void updateRATs() { Iterator<String> rats = rug.getRatList(); if(null == rats) { return; } RandomUnitGenerator.RatTreeNode ratTree = rug.getRatTree(); DefaultMutableTreeNode root = new DefaultMutableTreeNode(ratTree.name); createRatTreeNodes(root, ratTree); m_treeRAT.setModel(new DefaultTreeModel(root)); String selectedRATPath = GUIPreferences.getInstance().getRATSelectedRAT(); if (!selectedRATPath.equals("")) { String[] nodes = selectedRATPath.replace('[', ' ') .replace(']', ' ').split(","); TreePath path = findPathByName(nodes); m_treeRAT.setSelectionPath(path); } }
public void valueChanged(TreeSelectionEvent ev) { if (ev.getSource().equals(m_treeRAT)) { DefaultMutableTreeNode node = (DefaultMutableTreeNode)m_treeRAT.getLastSelectedPathComponent(); if (node == null) { return; } Object nodeInfo = node.getUserObject(); if (node.isLeaf()) { String ratName = (String)nodeInfo; rug.setChosenRAT(ratName); } } }
@Override public List<MechSummary> generate(int count, String faction, int unitType, int weightClass, int year, int quality, Collection<EntityMovementMode> movementModes, Predicate<MechSummary> filter) { RAT rat = findRAT(faction, unitType, weightClass, year, quality); if (rat != null) { if (!movementModes.isEmpty()) { Predicate<MechSummary> moveFilter = ms -> movementModes.contains(EntityMovementMode.getMode(ms.getUnitSubType())); if (filter == null) { filter = moveFilter; } else { filter = filter.and(moveFilter); } } return RandomUnitGenerator.getInstance().generate(count, rat.ratName, filter); } return new ArrayList<MechSummary>(); }
/** * Initializes the unit generator based on the method chosen in campaignOptions. * Called when the unit generator is first used or when the method has been * changed in campaignOptions. */ public void initUnitGenerator() { if (unitGenerator != null && unitGenerator instanceof RATManager) { MekHQ.unregisterHandler(unitGenerator); } if (campaignOptions.useStaticRATs()) { RATManager rm = new RATManager(); while (!RandomUnitGenerator.getInstance().isInitialized()) { try { Thread.sleep(50); } catch (InterruptedException e) { e.printStackTrace(); } } rm.setSelectedRATs(campaignOptions.getRATs()); rm.setIgnoreRatEra(campaignOptions.canIgnoreRatEra()); unitGenerator = rm; } else { RATGeneratorConnector rgc = new RATGeneratorConnector(getGameYear()); unitGenerator = rgc; } }
public ArrayList<MechSummary> generate(int numRolls, Predicate<MechSummary> filter) { return generate(numRolls, getChosenRAT(), filter); }
private void menuLoadXmlActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_menuLoadActionPerformed File f = selectLoadCampaignFile(); if (null == f) { return; } boolean hadAtB = getCampaign().getCampaignOptions().getUseAtB(); DataLoadingDialog dataLoadingDialog = new DataLoadingDialog( getApplication(), getFrame(), f); // TODO: does this effectively deal with memory management issues? dataLoadingDialog.setVisible(true); if (hadAtB && !getCampaign().getCampaignOptions().getUseAtB()) { RandomFactionGenerator.getInstance().dispose(); RandomUnitGenerator.getInstance().dispose(); RandomNameGenerator.getInstance().dispose(); } //Unregister event handlers for CampaignGUI and tabs for (int i = 0; i < tabMain.getTabCount(); i++) { if (tabMain.getComponentAt(i) instanceof CampaignGuiTab) { ((CampaignGuiTab)tabMain.getComponentAt(i)).disposeTab(); } } MekHQ.unregisterHandler(this); }