/** * 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; } }
/** * Find a suitable cash in location for this unit. * * @param aiUnit The <code>AIUnit</code> to execute this mission. * @param range The maximum number of moves to search. * @param deferOK Enables deferring to a fallback colony. * @return A <code>PathNode</code> to the target, or null if not found which includes the case * when Europe should be preferred (because the unit can not get there by itself). */ private static PathNode findTargetPath(AIUnit aiUnit, int range, boolean deferOK) { if (invalidAIUnitReason(aiUnit) != null) return null; final Unit unit = aiUnit.getUnit(); final Location start = unit.getPathStartLocation(); final Player player = unit.getOwner(); final Europe europe = player.getEurope(); final Unit carrier = unit.getCarrier(); final CostDecider standardCd = CostDeciders.avoidSettlementsAndBlockingUnits(); PathNode path; if (player.getNumberOfSettlements() <= 0 || start == null) { // No settlements or not on the map, so go straight to // Europe. If Europe does not exist then this mission is // doomed. return (europe == null) ? null : unit.findPath(unit.getLocation(), europe, carrier, standardCd); } // Can the unit get to a cash in site? return unit.search(start, getGoalDecider(aiUnit, deferOK), standardCd, range, carrier); }