Beispiel #1
0
  /**
   * Requests the game to be started. This will only be successful if all players are ready to start
   * the game.
   */
  public void requestLaunch() {
    if (freeColClient.getGame().isAllPlayersReadyToLaunch()) {
      gui.showStatusPanel(Messages.message("status.startingGame"));
      freeColClient.askServer().requestLaunch();

    } else {
      gui.errorMessage("server.notAllReady");
    }
  }
 /** {@inheritDoc} */
 @Override
 public void actionPerformed(ActionEvent ae) {
   doubleClickTimer.stop();
   Tile tile = canvas.convertToMapTile(centerX, centerY);
   if (canvas.getViewMode() == GUI.MOVE_UNITS_MODE) {
     // Clear goto order when active unit is on the tile
     Unit unit = canvas.getActiveUnit();
     if (unit != null && unit.getTile() == tile) {
       freeColClient.getInGameController().clearGotoOrders(unit);
       canvas.updateCurrentPathForActiveUnit();
     } else {
       if (tile != null && tile.hasSettlement()) {
         freeColClient.getGUI().showSettlement(tile.getSettlement());
         return;
       }
     }
   }
   freeColClient.getGUI().setSelectedTile(tile);
 }
 private boolean equipUnitIfPossible(UnitLabel unitLabel, AbstractGoods goods) {
   Unit unit = unitLabel.getUnit();
   if (unit.hasAbility(Ability.CAN_BE_EQUIPPED)) {
     for (EquipmentType equipment :
         freeColClient.getGame().getSpecification().getEquipmentTypeList()) {
       if (unit.canBeEquippedWith(equipment) && equipment.getGoodsRequired().size() == 1) {
         AbstractGoods requiredGoods = equipment.getGoodsRequired().get(0);
         if (requiredGoods.getType().equals(goods.getType())
             && requiredGoods.getAmount() <= goods.getAmount()) {
           int amount =
               Math.min(
                   goods.getAmount() / requiredGoods.getAmount(), equipment.getMaximumCount());
           freeColClient.getInGameController().equipUnit(unit, equipment, amount);
           unitLabel.updateIcon();
           return true;
         }
       }
     }
   }
   return false;
 }
  /**
   * Invoked when a mouse button was pressed.
   *
   * @param e The MouseEvent that holds all the information.
   */
  @Override
  public void mousePressed(MouseEvent e) {
    if (!e.getComponent().isEnabled()) return;

    int me = e.getButton();
    if (e.isPopupTrigger()) me = MouseEvent.BUTTON3;
    Tile tile = canvas.convertToMapTile(e.getX(), e.getY());

    switch (me) {
      case MouseEvent.BUTTON1:
        // Record initial click point for purposes of dragging
        canvas.setDragPoint(e.getX(), e.getY());
        if (canvas.isGotoStarted()) {
          PathNode path = canvas.getGotoPath();
          if (path != null) {
            canvas.stopGoto();
            // Move the unit
            freeColClient
                .getInGameController()
                .goToTile(canvas.getActiveUnit(), path.getLastNode().getTile());
          }
        } else if (doubleClickTimer.isRunning()) {
          doubleClickTimer.stop();
        } else {
          centerX = e.getX();
          centerY = e.getY();
          doubleClickTimer.start();
        }
        canvas.requestFocus();
        break;
      case MouseEvent.BUTTON2:
        if (tile != null) {
          Unit unit = canvas.getActiveUnit();
          if (unit != null && unit.getTile() != tile) {
            PathNode dragPath = unit.findPath(tile);
            canvas.startGoto();
            canvas.setGotoPath(dragPath);
          }
        }
        break;
      case MouseEvent.BUTTON3:
        // Cancel goto if one is active
        if (canvas.isGotoStarted()) canvas.stopGoto();
        canvas.showTilePopup(tile, e.getX(), e.getY());
        break;
      default:
        break;
    }
  }
  /**
   * Invoked when a mouse button was released.
   *
   * @param e The MouseEvent that holds all the information.
   */
  @Override
  public void mouseReleased(MouseEvent e) {
    try {
      if (canvas.getGotoPath() != null) { // A mouse drag has ended.
        PathNode temp = canvas.getGotoPath();
        canvas.stopGoto();

        freeColClient
            .getInGameController()
            .goToTile(canvas.getActiveUnit(), temp.getLastNode().getTile());

      } else if (canvas.isGotoStarted()) {
        canvas.stopGoto();
      }
    } catch (Exception ex) {
      logger.log(Level.WARNING, "Error in mouseReleased!", ex);
    }
  }
Beispiel #6
0
  /**
   * Sets a nation's state.
   *
   * @param nation The <code>Nation</code> to set.
   * @param state The <code>NationState</code> value to set.
   */
  public void setAvailable(Nation nation, NationState state) {
    freeColClient.getGame().getNationOptions().getNations().put(nation, state);

    freeColClient.askServer().setAvailable(nation, state);
  }
Beispiel #7
0
  /**
   * Sets this client's player's nation type.
   *
   * @param nationType Which nation type this player wishes to set.
   */
  public void setNationType(NationType nationType) {
    freeColClient.getMyPlayer().setNationType(nationType);

    freeColClient.askServer().setNationType(nationType);
  }
Beispiel #8
0
  /**
   * Sets this client's player's nation.
   *
   * @param nation Which <code>Nation</code> this player wishes to set.
   */
  public void setNation(Nation nation) {
    freeColClient.getMyPlayer().setNation(nation);

    freeColClient.askServer().setNation(nation);
  }
Beispiel #9
0
  /**
   * Sets this client to be (or not be) ready to start the game.
   *
   * @param ready Indicates whether or not this client is ready to start the game.
   */
  public void setReady(boolean ready) {
    freeColClient.getMyPlayer().setReady(ready);

    freeColClient.askServer().setReady(ready);
  }
Beispiel #10
0
  /** Starts the game. */
  public void startGame() {
    ResourceMapping gameMapping = new ResourceMapping();
    for (Player player : freeColClient.getGame().getPlayers()) {
      addPlayerResources(player.getNationID(), gameMapping);
    }
    // Unknown nation is not in getPlayers() list.
    addPlayerResources(Nation.UNKNOWN_NATION_ID, gameMapping);
    ResourceManager.addGameMapping(gameMapping);

    Player myPlayer = freeColClient.getMyPlayer();
    if (!freeColClient.isHeadless()) {
      gui.closeMainPanel();
      gui.closeMenus();
      gui.closeStatusPanel();
      gui.playSound(null); // Stop the long introduction sound
      gui.playSound("sound.intro." + myPlayer.getNationID());
    }
    freeColClient.askServer().registerMessageHandler(freeColClient.getInGameInputHandler());

    if (!freeColClient.isHeadless()) {
      freeColClient.setInGame(true);
      gui.setupInGameMenuBar();
    }

    InGameController igc = freeColClient.getInGameController();
    gui.setSelectedTile((Tile) myPlayer.getEntryLocation(), false);
    if (freeColClient.currentPlayerIsMyPlayer()) {
      igc.nextActiveUnit();
    }

    gui.setUpMouseListenersForCanvas();

    if (FreeColDebugger.isInDebugMode() && FreeColDebugger.getDebugRunTurns() > 0) {
      freeColClient.skipTurns(FreeColDebugger.getDebugRunTurns());
    } else if (freeColClient.getGame().getTurn().getNumber() == 1) {
      ModelMessage message =
          new ModelMessage(ModelMessage.MessageType.TUTORIAL, "tutorial.startGame", myPlayer);
      String direction = myPlayer.getNation().startsOnEastCoast() ? "west" : "east";
      message.add("%direction%", direction);
      myPlayer.addModelMessage(message);
      // force view of tutorial message
      igc.nextModelMessage();
    }
  }
Beispiel #11
0
  /**
   * Sends the {@link MapGeneratorOptions} to the server. This method should be called after
   * updating that object.
   */
  public void sendMapGeneratorOptions() {
    OptionGroup mapOptions = freeColClient.getGame().getMapGeneratorOptions();

    freeColClient.askServer().updateMapGeneratorOption(mapOptions);
  }
Beispiel #12
0
 /**
  * Get the controller.
  *
  * @return The <code>InGameController</code>.
  */
 protected InGameController getInGameController() {
   return freeColClient.getInGameController();
 }
Beispiel #13
0
 /**
  * Sends a chat message.
  *
  * @param message The text of the message.
  */
 public void chat(String message) {
   freeColClient.askServer().chat(freeColClient.getMyPlayer(), message);
 }
Beispiel #14
0
 /**
  * Gets the game.
  *
  * @return The <code>Game</code>.
  */
 protected Game getGame() {
   return freeColClient.getGame();
 }
Beispiel #15
0
 /**
  * Get the client options
  *
  * @return The <code>ClientOptions</code>.
  */
 protected ClientOptions getClientOptions() {
   return freeColClient.getClientOptions();
 }
Beispiel #16
0
 /**
  * Get the action manager.
  *
  * @return The <code>ActionManager</code>.
  */
 protected ActionManager getActionManager() {
   return freeColClient.getActionManager();
 }
Beispiel #17
0
 /**
  * Get the connect controller.
  *
  * @return The <code>ConnectController</code>.
  */
 protected ConnectController getConnectController() {
   return freeColClient.getConnectController();
 }
Beispiel #18
0
  /** The constructor that will add the items to this panel. */
  public ReportLabourPanel(FreeColClient freeColClient) {
    super(freeColClient, "reportLabourAction");

    this.data = new HashMap<>();
    this.unitCount = new TypeCountMap<>();
    for (Unit unit : getMyPlayer().getUnits()) {
      UnitType type = unit.getType();
      this.unitCount.incrementCount(type, 1);
      Map<Location, Integer> unitMap = this.data.get(type);
      if (unitMap == null) {
        unitMap = new HashMap<>();
        this.data.put(type, unitMap);
      }

      Location location = unit.getLocation();
      if (location == null) {
        logger.warning("Unit has null location: " + unit);
      } else if (location.getSettlement() != null) {
        location = location.getSettlement();
      } else if (unit.isInEurope()) {
        location = getMyPlayer().getEurope();
      } else if (location.getTile() != null) {
        location = location.getTile();
      }
      Integer count = unitMap.get(location);
      if (count == null) {
        unitMap.put(location, 1);
      } else {
        unitMap.put(location, count + 1);
      }
    }

    this.colonies = freeColClient.getMySortedColonies();

    DefaultListModel<LabourUnitPanel> model = new DefaultListModel<>();
    for (UnitType unitType : getSpecification().getUnitTypeList()) {
      if (unitType.isPerson() && unitType.isAvailableTo(getMyPlayer())) {
        int count = this.unitCount.getCount(unitType);
        model.addElement(new LabourUnitPanel(unitType, count));
      }
    }
    Action selectAction =
        new AbstractAction() {
          @Override
          public void actionPerformed(ActionEvent e) {
            showDetails();
          }
        };
    Action quitAction =
        new AbstractAction() {
          @Override
          public void actionPerformed(ActionEvent e) {
            getGUI().removeFromCanvas(ReportLabourPanel.this);
          }
        };

    // Add all the components
    this.panelList = new JList<>(model);
    this.panelList.getInputMap().put(KeyStroke.getKeyStroke("ENTER"), "select");
    this.panelList.getActionMap().put("select", selectAction);
    this.panelList.getInputMap().put(KeyStroke.getKeyStroke("ESCAPE"), "quit");
    this.panelList.getActionMap().put("quit", quitAction);
    this.panelList.addMouseListener(
        new MouseAdapter() {
          @Override
          public void mouseClicked(MouseEvent e) {
            if (e.getClickCount() == 2) {
              showDetails();
            }
          }
        });
    this.panelList.setOpaque(false);
    this.panelList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    this.panelList.setLayoutOrientation(JList.HORIZONTAL_WRAP);
    this.panelList.setVisibleRowCount(-1);
    this.panelList.setCellRenderer(new LabourUnitPanelRenderer());

    this.scrollPane.setViewportView(this.panelList);
  }
Beispiel #19
0
  /**
   * Sends the {@link GameOptions} to the server. This method should be called after updating that
   * object.
   */
  public void sendGameOptions() {
    OptionGroup gameOptions =
        freeColClient.getGame().getSpecification().getOptionGroup("gameOptions");

    freeColClient.askServer().updateGameOptions(gameOptions);
  }
  /**
   * Create a panel to define trade route cargos.
   *
   * @param freeColClient The <code>FreeColClient</code> for the game.
   * @param newRoute The <code>TradeRoute</code> to operate on.
   */
  public TradeRouteInputPanel(FreeColClient freeColClient, TradeRoute newRoute) {
    super(freeColClient, new MigLayout("wrap 4, fill", "[]20[fill]rel"));

    final Game game = freeColClient.getGame();
    final Player player = getMyPlayer();
    final TradeRoute tradeRoute = newRoute.copy(game, TradeRoute.class);

    this.newRoute = newRoute;
    this.cargoHandler = new CargoHandler();
    this.dragListener = new DragListener(freeColClient, this);
    this.dropListener = new DropListener();

    this.stopListModel = new DefaultListModel<>();
    for (TradeRouteStop stop : tradeRoute.getStops()) {
      this.stopListModel.addElement(stop);
    }

    this.stopList = new JList<>(this.stopListModel);
    this.stopList.setCellRenderer(new StopRenderer());
    this.stopList.setFixedCellHeight(48);
    this.stopList.setDragEnabled(true);
    this.stopList.setTransferHandler(new StopListHandler());
    this.stopList.addKeyListener(
        new KeyListener() {

          @Override
          public void keyTyped(KeyEvent e) {
            if (e.getKeyChar() == KeyEvent.VK_DELETE) {
              deleteCurrentlySelectedStops();
            }
          }

          @Override
          public void keyPressed(KeyEvent e) {} // Ignore

          @Override
          public void keyReleased(KeyEvent e) {} // Ignore
        });
    this.stopList.addListSelectionListener(this);
    JScrollPane tradeRouteView = new JScrollPane(stopList);

    JLabel nameLabel = Utility.localizedLabel("tradeRouteInputPanel.nameLabel");
    this.tradeRouteName = new JTextField(tradeRoute.getName());

    JLabel destinationLabel = Utility.localizedLabel("tradeRouteInputPanel.destinationLabel");
    this.destinationSelector = new JComboBox<>();
    this.destinationSelector.setRenderer(new DestinationCellRenderer());
    StringTemplate template = StringTemplate.template("tradeRouteInputPanel.allColonies");
    this.destinationSelector.addItem(Messages.message(template));
    if (player.getEurope() != null) {
      this.destinationSelector.addItem(player.getEurope().getId());
    }
    for (Colony colony : freeColClient.getMySortedColonies()) {
      this.destinationSelector.addItem(colony.getId());
    }

    this.messagesBox = new JCheckBox(Messages.message("tradeRouteInputPanel.silence"));
    this.messagesBox.setSelected(tradeRoute.isSilent());
    this.messagesBox.addActionListener(
        (ActionEvent ae) -> {
          tradeRoute.setSilent(messagesBox.isSelected());
        });

    this.addStopButton = Utility.localizedButton("tradeRouteInputPanel.addStop");
    this.addStopButton.addActionListener(
        (ActionEvent ae) -> {
          addSelectedStops();
        });

    this.removeStopButton = Utility.localizedButton("tradeRouteInputPanel.removeStop");
    this.removeStopButton.addActionListener(
        (ActionEvent ae) -> {
          deleteCurrentlySelectedStops();
        });

    this.goodsPanel = new GoodsPanel();
    this.goodsPanel.setTransferHandler(this.cargoHandler);
    this.goodsPanel.setEnabled(false);
    this.cargoPanel = new CargoPanel();
    this.cargoPanel.setTransferHandler(this.cargoHandler);

    JButton cancelButton = Utility.localizedButton("cancel");
    cancelButton.setActionCommand(CANCEL);
    cancelButton.addActionListener(this);
    setCancelComponent(cancelButton);

    add(Utility.localizedHeader("tradeRouteInputPanel.editRoute", false), "span, align center");
    add(tradeRouteView, "span 1 5, grow");
    add(nameLabel);
    add(this.tradeRouteName, "span");
    add(destinationLabel);
    add(this.destinationSelector, "span");
    add(this.messagesBox);
    add(this.addStopButton);
    add(this.removeStopButton, "span");
    add(this.goodsPanel, "span");
    add(this.cargoPanel, "span, height 80:, growy");
    add(okButton, "newline 20, span, split 2, tag ok");
    add(cancelButton, "tag cancel");

    // update cargo panel if stop is selected
    if (this.stopListModel.getSize() > 0) {
      this.stopList.setSelectedIndex(0);
      TradeRouteStop selectedStop = this.stopListModel.firstElement();
      this.cargoPanel.initialize(selectedStop);
    }

    // update buttons according to selection
    updateButtons();

    getGUI().restoreSavedSize(this, getPreferredSize());
  }
Beispiel #21
0
 /**
  * Get the GUI.
  *
  * @return The GUI.
  */
 protected GUI getGUI() {
   return freeColClient.getGUI();
 }