Ejemplo n.º 1
0
 public void decreaseEdge() {
   IOption edgeOption = getOptions().getOption("edge");
   edgeOption.setValue((Integer) edgeOption.getValue() - 1);
 }
Ejemplo n.º 2
0
  /** Host a game constructed from a scenario file */
  void scenario() {
    JFileChooser fc = new JFileChooser("data" + File.separatorChar + "scenarios");
    fc.setLocation(frame.getLocation().x + 150, frame.getLocation().y + 100);
    fc.setDialogTitle(Messages.getString("MegaMek.SelectScenarioDialog.title"));

    FileFilter filter =
        new FileFilter() {

          @Override
          public boolean accept(File f) {
            if (f.isDirectory()) {
              return true;
            }

            String ext = null;
            String s = f.getName();
            int i = s.lastIndexOf('.');

            if ((i > 0) && (i < (s.length() - 1))) {
              ext = s.substring(i + 1).toLowerCase();
            }

            if (ext != null) {
              if (ext.equalsIgnoreCase("mms")) {
                return true;
              }
              return false;
            }

            return false;
          }

          @Override
          public String getDescription() {
            return "MegaMek Scenario Files";
          }
        };
    fc.setFileFilter(filter);

    int returnVal = fc.showOpenDialog(frame);
    if ((returnVal != JFileChooser.APPROVE_OPTION) || (fc.getSelectedFile() == null)) {
      // I want a file, y'know!
      return;
    }

    ScenarioLoader sl = new ScenarioLoader(fc.getSelectedFile());
    IGame g;
    try {
      g = sl.createGame();
    } catch (Exception e) {
      e.printStackTrace();
      JOptionPane.showMessageDialog(
          frame,
          Messages.getString("MegaMek.HostScenarioAlert.message") + e.getMessage(),
          Messages.getString("MegaMek.HostScenarioAlert.title"),
          JOptionPane.ERROR_MESSAGE); // $NON-NLS-1$ //$NON-NLS-2$
      return;
    }

    // popup options dialog
    GameOptionsDialog god = new GameOptionsDialog(frame, g.getOptions(), false);
    god.update(g.getOptions());
    god.setEditable(true);
    god.setVisible(true);
    for (IBasicOption opt : god.getOptions()) {
      IOption orig = g.getOptions().getOption(opt.getName());
      orig.setValue(opt.getValue());
    }
    god = null;

    // popup planetry conditions dialog
    PlanetaryConditionsDialog pcd =
        new PlanetaryConditionsDialog(frame, g.getPlanetaryConditions());
    pcd.update(g.getPlanetaryConditions());
    pcd.setVisible(true);
    g.setPlanetaryConditions(pcd.getConditions());
    pcd = null;

    // get player types and colors set
    Player[] pa = new Player[g.getPlayersVector().size()];
    g.getPlayersVector().copyInto(pa);
    ScenarioDialog sd = new ScenarioDialog(frame, pa);
    sd.setVisible(true);
    if (!sd.bSet) {
      return;
    }

    // host with the scenario. essentially copied from host()
    HostDialog hd = new HostDialog(frame);
    boolean hasSlot = false;
    if (!("".equals(sd.localName))) {
      hasSlot = true;
    }
    hd.yourNameF.setText(sd.localName);
    hd.setVisible(true);
    // verify dialog data
    if ((hd.playerName == null) || (hd.serverPass == null) || (hd.port == 0)) {
      return;
    }
    sd.localName = hd.playerName;

    // Players should have to enter a non-blank, non-whitespace name.
    boolean foundValid = false;
    char[] nameChars = hd.playerName.toCharArray();
    for (int loop = 0; !foundValid && (loop < nameChars.length); loop++) {
      if (!Character.isWhitespace(nameChars[loop])) {
        foundValid = true;
      }
    }
    if (!foundValid) {
      JOptionPane.showMessageDialog(
          frame,
          Messages.getString("MegaMek.HostScenarioAlert1.message"),
          Messages.getString("MegaMek.HostScenarioAlert1.title"),
          JOptionPane.ERROR_MESSAGE); // $NON-NLS-1$ //$NON-NLS-2$
      return;
    }

    // kick off a RNG check
    Compute.d6();

    // start server
    try {
      server = new Server(hd.serverPass, hd.port);
    } catch (IOException ex) {
      System.err.println("could not create server socket on port " + hd.port);
      StringBuffer error = new StringBuffer();
      error
          .append("Error: could not start server at localhost")
          .append(":")
          .append(hd.port)
          .append(" (")
          .append(ex.getMessage())
          .append(").");
      JOptionPane.showMessageDialog(
          frame,
          error.toString(),
          Messages.getString("MegaMek.HostGameAlert.title"),
          JOptionPane.ERROR_MESSAGE); // $NON-NLS-1$
      return;
    }
    server.setGame(g);

    // apply any scenario damage
    sl.applyDamage(server);
    ClientGUI gui = null;
    if (!"".equals(sd.localName)) { // $NON-NLS-1$
      // initialize game
      client = new Client(hd.playerName, "localhost", hd.port); // $NON-NLS-1$
      gui = new ClientGUI(client, controller);
      controller.clientgui = gui;
      gui.initialize();
      if (!client.connect()) {
        StringBuffer error = new StringBuffer();
        error
            .append("Error: could not connect to server at localhost")
            .append(":")
            .append(hd.port)
            .append(".");
        JOptionPane.showMessageDialog(
            frame,
            error.toString(),
            Messages.getString("MegaMek.HostScenarioAlert.title"),
            JOptionPane.ERROR_MESSAGE); // $NON-NLS-1$
        frame.setVisible(false);
        client.die();
      }
    }
    optdlg = null;

    // calculate initial BV
    server.calculatePlayerBVs();

    // setup any bots
    for (int x = 0; x < pa.length; x++) {
      if (sd.playerTypes[x] == ScenarioDialog.T_BOT) {
        BotClient c = new TestBot(pa[x].getName(), "localhost", hd.port); // $NON-NLS-1$
        c.getGame().addGameListener(new BotGUI(c));
        if (!c.connect()) {
          // bots should never fail on connect
        }
      }
    }

    for (int x = 0; x < pa.length; x++) {
      if (sd.playerTypes[x] == ScenarioDialog.T_OBOT) {
        BotClient c =
            new Princess(pa[x].getName(), "localhost", hd.port, LogLevel.ERROR); // $NON-NLS-1$
        c.getGame().addGameListener(new BotGUI(c));
        if (!c.connect()) {
          // bots should never fail on connect
        }
      }
    }

    // If he didn't have a name when hasSlot was set, then the host should
    // be an observer.
    if (!hasSlot) {
      Enumeration<IPlayer> pE = server.getGame().getPlayers();
      while (pE.hasMoreElements()) {
        IPlayer tmpP = pE.nextElement();
        if (tmpP.getName().equals(sd.localName)) {
          tmpP.setObserver(true);
        }
      }
    }
    if (gui != null) {
      launch(gui.getFrame());
    }
  }