Exemple #1
0
 /**
  * We only want to show double-blind visibility indicators on our own mechs and teammates mechs
  * (assuming team vision option).
  */
 private boolean trackThisEntitiesVisibilityInfo(Entity e) {
   IPlayer localPlayer = bv.getLocalPlayer();
   if (localPlayer == null) {
     return false;
   }
   if (bv.game.getOptions().booleanOption("double_blind") // $NON-NLS-1$
       && ((e.getOwner().getId() == localPlayer.getId())
           || (bv.game.getOptions().booleanOption("team_vision") // $NON-NLS-1$
               && (e.getOwner().getTeam() == localPlayer.getTeam())))) {
     return true;
   }
   return false;
 }
Exemple #2
0
  @Override
  public StringBuffer getTooltip() {

    // Tooltip info for a sensor blip
    if (onlyDetectedBySensors())
      return new StringBuffer(Messages.getString("BoardView1.sensorReturn"));

    // No sensor blip...
    Infantry thisInfantry = null;
    if (entity instanceof Infantry) thisInfantry = (Infantry) entity;
    GunEmplacement thisGunEmp = null;
    if (entity instanceof GunEmplacement) thisGunEmp = (GunEmplacement) entity;
    Aero thisAero = null;
    if (entity instanceof Aero) thisAero = (Aero) entity;

    tooltipString = new StringBuffer();

    // Unit Chassis and Player
    addToTT(
        "Unit",
        NOBR,
        Integer.toHexString(PlayerColors.getColorRGB(entity.getOwner().getColorIndex())),
        entity.getChassis(),
        entity.getOwner().getName());

    // Pilot Info
    // Nickname > Name > "Pilot"
    String pnameStr = "Pilot";

    if ((entity.getCrew().getName() != null) && !entity.getCrew().getName().equals(""))
      pnameStr = entity.getCrew().getName();

    if ((entity.getCrew().getNickname() != null) && !entity.getCrew().getNickname().equals(""))
      pnameStr = "'" + entity.getCrew().getNickname() + "'";

    addToTT("Pilot", BR, pnameStr, entity.getCrew().getGunnery(), entity.getCrew().getPiloting());

    // Pilot Status
    if (!entity.getCrew().getStatusDesc().equals(""))
      addToTT("PilotStatus", NOBR, entity.getCrew().getStatusDesc());

    // Pilot Advantages
    int numAdv = entity.getCrew().countOptions(PilotOptions.LVL3_ADVANTAGES);
    if (numAdv == 1) addToTT("Adv1", NOBR, numAdv);
    else if (numAdv > 1) addToTT("Advs", NOBR, numAdv);

    // Pilot Manei Domini
    if ((entity.getCrew().countOptions(PilotOptions.MD_ADVANTAGES) > 0)) addToTT("MD", NOBR);

    // Unit movement ability
    if (thisGunEmp == null) {
      addToTT("Movement", BR, entity.getWalkMP(), entity.getRunMPasString());
      if (entity.getJumpMP() > 0) tooltipString.append("/" + entity.getJumpMP());
    }

    // Armor and Internals
    addToTT("ArmorInternals", BR, entity.getTotalArmor(), entity.getTotalInternal());

    // Heat, not shown for units with 999 heat sinks (vehicles)
    if (entity.getHeatCapacity() != 999) {
      if (entity.heat == 0) addToTT("Heat0", BR);
      else addToTT("Heat", BR, entity.heat);
    }

    // Actual Movement
    if (thisGunEmp == null) {
      // In the Movement Phase, unit not done
      if (!entity.isDone() && this.bv.game.getPhase() == Phase.PHASE_MOVEMENT) {
        // "Has not yet moved" only during movement phase
        addToTT("NotYetMoved", BR);

        // In the Movement Phase, unit is done - or in the Firing Phase
      } else if ((entity.isDone() && this.bv.game.getPhase() == Phase.PHASE_MOVEMENT)
          || this.bv.game.getPhase() == Phase.PHASE_FIRING) {
        int tmm = Compute.getTargetMovementModifier(bv.game, entity.getId()).getValue();
        // Unit didn't move
        if (entity.moved == EntityMovementType.MOVE_NONE) {
          addToTT("NoMove", BR, tmm);

          // Unit did move
        } else {
          // Colored arrow
          // get the color resource
          String guipName = "AdvancedMoveDefaultColor";
          if ((entity.moved == EntityMovementType.MOVE_RUN)
              || (entity.moved == EntityMovementType.MOVE_VTOL_RUN)
              || (entity.moved == EntityMovementType.MOVE_OVER_THRUST))
            guipName = "AdvancedMoveRunColor";
          else if (entity.moved == EntityMovementType.MOVE_SPRINT)
            guipName = "AdvancedMoveSprintColor";
          else if (entity.moved == EntityMovementType.MOVE_JUMP) guipName = "AdvancedMoveJumpColor";

          // HTML color String from Preferences
          String moveTypeColor =
              Integer.toHexString(
                  GUIPreferences.getInstance().getColor(guipName).getRGB() & 0xFFFFFF);

          // Arrow
          addToTT("Arrow", BR, moveTypeColor);

          // Actual movement and modifier
          addToTT(
              "MovementF",
              NOBR,
              entity.getMovementString(entity.moved),
              entity.delta_distance,
              tmm);
        }
        // Special Moves
        if (entity.isEvading()) addToTT("Evade", NOBR);

        if ((thisInfantry != null) && (thisInfantry.isTakingCover())) addToTT("TakingCover", NOBR);

        if (entity.isCharging()) addToTT("Charging", NOBR);

        if (entity.isMakingDfa()) addToTT("DFA", NOBR);
      }
    }

    // ASF Velocity
    if (thisAero != null) {
      addToTT("AeroVelocity", BR, thisAero.getCurrentVelocity());
    }

    // Gun Emplacement Status
    if (thisGunEmp != null) {
      if (thisGunEmp.isTurret() && thisGunEmp.isTurretLocked(thisGunEmp.getLocTurret()))
        addToTT("TurretLocked", BR);
    }

    // Unit Immobile
    if ((thisGunEmp == null) && (entity.isImmobile())) addToTT("Immobile", BR);

    if (entity.isHiddenActivating()) {
      addToTT(
          "HiddenActivating",
          BR,
          IGame.Phase.getDisplayableName(entity.getHiddenActivationPhase()));
    } else if (entity.isHidden()) {
      addToTT("Hidden", BR);
    }

    // Jammed by ECM
    if (isAffectedByECM()) {
      addToTT("Jammed", BR);
    }

    // If DB, add information about who sees this Entity
    if (bv.game.getOptions().booleanOption("double_blind")) {
      StringBuffer playerList = new StringBuffer();
      boolean teamVision = bv.game.getOptions().booleanOption("team_vision");
      for (IPlayer player : entity.getWhoCanSee()) {
        if (player.isEnemyOf(entity.getOwner()) || !teamVision) {
          playerList.append(player.getName());
          playerList.append(", ");
        }
      }
      if (playerList.length() > 1) {
        playerList.delete(playerList.length() - 2, playerList.length());
        addToTT("SeenBy", BR, playerList.toString());
      }
    }

    // If sensors, display what sensors this unit is using
    if (bv.game.getOptions().booleanOption("tacops_sensors")) {
      addToTT("Sensors", BR, entity.getSensorDesc());
    }

    // Weapon List
    if (GUIPreferences.getInstance().getBoolean(GUIPreferences.SHOW_WPS_IN_TT)) {

      ArrayList<Mounted> weapons = entity.getWeaponList();
      HashMap<String, Integer> wpNames = new HashMap<String, Integer>();

      // Gather names, counts, Clan/IS
      // When clan then the number will be stored as negative
      for (Mounted curWp : weapons) {
        String weapDesc = curWp.getDesc();
        // Append ranges
        WeaponType wtype = (WeaponType) curWp.getType();
        int ranges[];
        if (entity instanceof Aero) {
          ranges = wtype.getATRanges();
        } else {
          ranges = wtype.getRanges(curWp);
        }
        String rangeString = "(";
        if ((ranges[RangeType.RANGE_MINIMUM] != WeaponType.WEAPON_NA)
            && (ranges[RangeType.RANGE_MINIMUM] != 0)) {
          rangeString += ranges[RangeType.RANGE_MINIMUM] + "/";
        } else {
          rangeString += "-/";
        }
        int maxRange = RangeType.RANGE_LONG;
        if (bv.game.getOptions().booleanOption(OptionsConstants.AC_TAC_OPS_RANGE)) {
          maxRange = RangeType.RANGE_EXTREME;
        }
        for (int i = RangeType.RANGE_SHORT; i <= maxRange; i++) {
          rangeString += ranges[i];
          if (i != maxRange) {
            rangeString += "/";
          }
        }

        weapDesc += rangeString + ")";
        if (wpNames.containsKey(weapDesc)) {
          int number = wpNames.get(weapDesc);
          if (number > 0) wpNames.put(weapDesc, number + 1);
          else wpNames.put(weapDesc, number - 1);
        } else {
          WeaponType wpT = ((WeaponType) curWp.getType());

          if (entity.isClan() && TechConstants.isClan(wpT.getTechLevel(entity.getYear())))
            wpNames.put(weapDesc, -1);
          else wpNames.put(weapDesc, 1);
        }
      }

      // Print to Tooltip
      tooltipString.append("<FONT SIZE=\"-2\">");

      for (Entry<String, Integer> entry : wpNames.entrySet()) {
        // Check if weapon is destroyed, text gray and strikethrough if so, remove the "x "/"*"
        // Also remove "+", means currently selected for firing
        boolean wpDest = false;
        String nameStr = entry.getKey();
        if (entry.getKey().startsWith("x ")) {
          nameStr = entry.getKey().substring(2, entry.getKey().length());
          wpDest = true;
        }

        if (entry.getKey().startsWith("*")) {
          nameStr = entry.getKey().substring(1, entry.getKey().length());
          wpDest = true;
        }

        if (entry.getKey().startsWith("+")) {
          nameStr = entry.getKey().substring(1, entry.getKey().length());
          nameStr = nameStr.concat(" <I>(Firing)</I>");
        }

        // normal coloring
        tooltipString.append("<FONT COLOR=#8080FF>");
        // but: color gray and strikethrough when weapon destroyed
        if (wpDest) tooltipString.append("<FONT COLOR=#a0a0a0><S>");

        String clanStr = "";
        if (entry.getValue() < 0) clanStr = Messages.getString("BoardView1.Tooltip.Clan");

        // when more than 5 weapons are present, they will be grouped
        // and listed with a multiplier
        if (weapons.size() > 5) {
          addToTT("WeaponN", BR, Math.abs(entry.getValue()), clanStr, nameStr);

        } else { // few weapons: list each weapon separately
          for (int i = 0; i < Math.abs(entry.getValue()); i++) {
            addToTT("Weapon", BR, Math.abs(entry.getValue()), clanStr, nameStr);
          }
        }
        // Weapon destroyed? End strikethrough
        if (wpDest) tooltipString.append("</S>");
        tooltipString.append("</FONT>");
      }
      tooltipString.append("</FONT>");
    }
    return tooltipString;
  }
  /** 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());
    }
  }