Skip to content

Commit

Permalink
Enhancement - #49: Add proper pathfinding taking VBL into account
Browse files Browse the repository at this point in the history
 * Finished POC for ASTar Pathfinding, code cleanup

Signed-off-by: Jamz <[email protected]>
  • Loading branch information
JamzTheMan committed Jun 1, 2018
1 parent c4b321d commit fca8022
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 349 deletions.
11 changes: 11 additions & 0 deletions src/main/java/net/rptools/maptool/client/AppPreferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,9 @@ public class AppPreferences {
private static final String KEY_FORCE_FACING_ARROW = "forceFacingArrow";
private static final boolean DEFAULT_FORCE_FACING_ARROW = false;

private static final String KEY_USE_ASTAR_PATHFINDING = "useAstarPathfinding";
private static final boolean DEFAULT_USE_ASTAR_PATHFINDING = true;

public static void setFillSelectionBox(boolean fill) {
prefs.putBoolean(KEY_FILL_SELECTION_BOX, fill);
}
Expand Down Expand Up @@ -1067,4 +1070,12 @@ public static boolean isShowInitGainMessage() {
public static void setShowInitGainMessage(boolean flag) {
prefs.putBoolean(KEY_SHOW_INIT_GAIN_MESSAGE, flag);
}

public static boolean isUsingAstarPathfinding() {
return prefs.getBoolean(KEY_USE_ASTAR_PATHFINDING, DEFAULT_USE_ASTAR_PATHFINDING);
}

public static void setUseAstarPathfinding(boolean show) {
prefs.putBoolean(KEY_USE_ASTAR_PATHFINDING, show);
}
}
80 changes: 1 addition & 79 deletions src/main/java/net/rptools/maptool/client/ui/MapToolFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
Expand Down Expand Up @@ -152,16 +150,6 @@
import com.jidesoft.docking.DefaultDockableHolder;
import com.jidesoft.docking.DockableFrame;

import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;

/**
*/
public class MapToolFrame extends DefaultDockableHolder implements WindowListener, AppEventListener {
Expand Down Expand Up @@ -200,7 +188,6 @@ public class MapToolFrame extends DefaultDockableHolder implements WindowListene
private JPanel visibleControlPanel;
private FullScreenFrame fullScreenFrame;
private final JPanel rendererBorderPanel;
private final JLayeredPane rendererMediaPanel;
private final List<ZoneRenderer> zoneRendererList;
private final JMenuBar menuBar;
private final StatusPanel statusPanel;
Expand Down Expand Up @@ -371,28 +358,11 @@ public MapToolFrame(JMenuBar menuBar) {
// zoneMiniMapPanel.setSize(100, 100);

zoneRendererPanel = new JPanel(new PositionalLayout(5));
// zoneRendererPanel.setBackground(Color.black);
zoneRendererPanel.setBackground(Color.black);
// zoneRendererPanel.add(zoneMiniMapPanel, PositionalLayout.Position.SE);
// zoneRendererPanel.add(getChatTypingLabel(), PositionalLayout.Position.NW);
zoneRendererPanel.add(getChatTypingPanel(), PositionalLayout.Position.NW);
zoneRendererPanel.add(getChatActionLabel(), PositionalLayout.Position.SW);
// zoneRendererPanel.setOpaque(false);
// zoneRendererPanel.setSize(2100, 1600);

rendererMediaPanel = new JLayeredPane();
fxPanel = new JFXPanel();
fxPanel.setSize(1024, 768);
// fxPanel.setOpaque(false);

// rendererMediaPanel.add(fxPanel);

// zoneRendererPanel.add(fxPanel, PositionalLayout.Position.NW);
Platform.runLater(new Runnable() {
@Override
public void run() {
initFX(fxPanel);
}
});

commandPanel = new CommandPanel();
MapTool.getMessageList().addObserver(commandPanel);
Expand Down Expand Up @@ -444,49 +414,6 @@ public void run() {
setChatTypingLabelColor(AppPreferences.getChatNotificationColor());
}

private static void initFX(JFXPanel fxPanel) {
// This method is invoked on the JavaFX thread
Scene scene = createScene();
fxPanel.setScene(scene);
}

private static Scene createScene() {
Group root = new Group();
Scene scene = new Scene(root);

root.setMouseTransparent(true);
root.setAutoSizeChildren(true);

WebView webview = new WebView();
WebEngine webEngine = webview.getEngine();
webEngine.load("http://www.youtube.com/embed/3Z6Mkfetay8?autoplay=1");

// webview.setMinWidth(2100);
// webview.setMinHeight(1280);
// webview.autosize();
// webview.setZoom(webview.getZoom() * 2);
// webview.setMouseTransparent(true);

// root.getChildren().add(webview);

try {
URL test = new File("d:/dynamic_map_test.mp4").toURI().toURL();
Media media = new Media(test.toExternalForm());
MediaPlayer mediaPlayer = new MediaPlayer(media);
MediaView mediaView = new MediaView(mediaPlayer);
mediaPlayer.setAutoPlay(true);
mediaPlayer.setCycleCount(-1);

root.getChildren().add(mediaView);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

scene.setFill(javafx.scene.paint.Color.TRANSPARENT);
return (scene);
}

public ChatNotificationTimers getChatNotificationTimers() {
return chatTyperTimers;
}
Expand Down Expand Up @@ -585,7 +512,6 @@ private void configureDocking() {

// Main panel
getDockingManager().getWorkspace().add(rendererBorderPanel);
// getDockingManager().getWorkspace().add(rendererMediaPanel);

// Docked frames
getDockingManager().addFrame(getFrame(MTFrame.CONNECTIONS));
Expand Down Expand Up @@ -1792,10 +1718,6 @@ public JFileChooser getLoadMacroSetFileChooser() {

private JFileChooser saveTableFileChooser;
private JFileChooser loadTableFileChooser;
private JFXPanel fxPanel;
public JFXPanel getFxPanel() {
return fxPanel;
}

public JFileChooser getSaveTableFileChooser() {
if (saveTableFileChooser == null) {
Expand Down
11 changes: 9 additions & 2 deletions src/main/java/net/rptools/maptool/client/ui/ToolbarPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import javax.swing.JToolBar;

import net.rptools.lib.image.ImageUtil;
import net.rptools.maptool.client.AppPreferences;
import net.rptools.maptool.client.MapTool;
import net.rptools.maptool.client.tool.BoardTool;
import net.rptools.maptool.client.tool.FacingTool;
Expand Down Expand Up @@ -263,12 +264,15 @@ public void actionPerformed(ActionEvent e) {
return button;
}

public void getTest() {

}
private JToggleButton createAiButton(final String icon, final String offIcon, String tooltip) {
final JToggleButton button = new JToggleButton();
button.setToolTipText(tooltip);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
MapTool.getFrame().getCurrentZoneRenderer().setUseAStarPathfinding(button.isSelected());
AppPreferences.setUseAstarPathfinding(button.isSelected());
}
});

Expand All @@ -278,7 +282,10 @@ public void actionPerformed(ActionEvent e) {
} catch (IOException ioe) {
ioe.printStackTrace();
}


if(AppPreferences.isUsingAstarPathfinding())
button.doClick();

return button;
}

Expand Down
55 changes: 12 additions & 43 deletions src/main/java/net/rptools/maptool/client/ui/zone/ZoneRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,7 @@
import java.awt.image.BufferedImage;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.math.BigDecimal;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand All @@ -72,22 +69,11 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.scene.text.Text;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import net.rptools.lib.CodeTimer;
import net.rptools.lib.MD5Key;
import net.rptools.lib.image.ImageUtil;
import net.rptools.lib.swing.ImageBorder;
import net.rptools.lib.swing.ImageLabel;
import net.rptools.lib.swing.PositionalLayout;
import net.rptools.lib.swing.SwingUtil;
import net.rptools.maptool.client.AppActions;
import net.rptools.maptool.client.AppConstants;
Expand All @@ -106,7 +92,6 @@
import net.rptools.maptool.client.tool.drawing.OvalExposeTool;
import net.rptools.maptool.client.tool.drawing.PolygonExposeTool;
import net.rptools.maptool.client.tool.drawing.RectangleExposeTool;
import net.rptools.maptool.client.ui.MapToolFrame;
import net.rptools.maptool.client.ui.Scale;
import net.rptools.maptool.client.ui.Tool;
import net.rptools.maptool.client.ui.htmlframe.HTMLFrameFactory;
Expand Down Expand Up @@ -209,8 +194,10 @@ public class ZoneRenderer extends JComponent implements DropTargetListener, Comp
private CodeTimer timer;

private boolean autoResizeStamp = false;
private boolean useAStarPathfinding = false;

// Show blocked grid lines during AStar moving, for debugging...
private boolean showAstarDebugging = false;

public static enum TokenMoveCompletion {
TRUE, FALSE, OTHER
}
Expand Down Expand Up @@ -389,7 +376,7 @@ public void updateMoveSelectionSet(GUID keyToken, ZonePoint offset) {
}
Token token = zone.getToken(keyToken);
set.setOffset(offset.x - token.getX(), offset.y - token.getY());
repaint(); // Jamz causes flicker when using AI
repaint(); // Jamz: may cause flicker when using AI
}

public void toggleMoveSelectionSetWaypoint(GUID keyToken, ZonePoint location) {
Expand Down Expand Up @@ -1182,7 +1169,7 @@ public void renderZone(Graphics2D g2d, PlayerView view) {
timer.stop("tokens");
}
timer.start("unowned movement");
renderMoveSelectionSets(g2d, view, getUnOwnedMovementSet(view));
showBlockedMoves(g2d, view, getUnOwnedMovementSet(view));
timer.stop("unowned movement");

// Moved below, after the renderFog() call...
Expand Down Expand Up @@ -1232,7 +1219,7 @@ public void renderZone(Graphics2D g2d, PlayerView view) {
}

timer.start("owned movement");
renderMoveSelectionSets(g2d, view, getOwnedMovementSet(view));
showBlockedMoves(g2d, view, getOwnedMovementSet(view));
timer.stop("owned movement");

// Text associated with tokens being moved is added to a list to be drawn after, i.e. on top of, the tokens
Expand Down Expand Up @@ -1936,7 +1923,7 @@ private Set<SelectionSet> getUnOwnedMovementSet(PlayerView view) {
return movementSet;
}

protected void renderMoveSelectionSets(Graphics2D g, PlayerView view, Set<SelectionSet> movementSet) {
protected void showBlockedMoves(Graphics2D g, PlayerView view, Set<SelectionSet> movementSet) {
if (selectionSetMap.isEmpty()) {
return;
}
Expand Down Expand Up @@ -2017,29 +2004,19 @@ protected void renderMoveSelectionSets(Graphics2D g, PlayerView view, Set<Select
renderPath(g, walker != null ? walker.getPath() : set.gridlessPath, token.getFootprint(zone.getGrid()));
}

// Show current Allowed Movement directions for A*
if (walker != null) {
// Show current Blocked Movement directions for A*
if (walker != null && showAstarDebugging ) {
Collection<AStarCellPoint> checkPoints = walker.getCheckedPoints();
// Color currentColor = g.getColor();
for (AStarCellPoint acp : checkPoints) {
Set<Point2D> validMoves = acp.getValidMoves();
// showValidMove(g, acp.offsetZonePoint(getZone().getGrid()), AppStyle.validMoveImage, 1.0f);

for (Point2D point : validMoves) {
ZonePoint zp = acp.offsetZonePoint(getZone().getGrid(), point.getX(), point.getY());
double r = (zp.x - 1) * 45;
showValidMove(g, zp, r, AppStyle.blockMoveImage, 1.0f);
showBlockedMoves(g, zp, r, AppStyle.blockMoveImage, 1.0f);
}
// Shape validShape = acp.getValidMoveShape(zone);
// if (validShape != null) {
// AffineTransform at = new AffineTransform();
// at.translate(getViewOffsetX(), getViewOffsetY());
// at.scale(getScale(), getScale());
// g.setColor(Color.GREEN);
// g.draw(at.createTransformedShape(validShape));
// }
}
// g.setColor(currentColor);
}
// handle flipping
BufferedImage workImage = image;
Expand Down Expand Up @@ -2472,7 +2449,7 @@ public void drawShape(Shape shape, int x, int y) {
g.draw(at.createTransformedShape(shape));
}

public void showValidMove(Graphics2D g, ZonePoint point, double angle, BufferedImage image, float size) {
public void showBlockedMoves(Graphics2D g, ZonePoint point, double angle, BufferedImage image, float size) {
// Resize image to size of 1/4 size of grid
double resizeWidth = zone.getGrid().getCellWidth() / image.getWidth() * .25;
double resizeHeight = zone.getGrid().getCellHeight() / image.getHeight() * .25;
Expand All @@ -2486,10 +2463,6 @@ public void showValidMove(Graphics2D g, ZonePoint point, double angle, BufferedI
ScreenPoint sp = ScreenPoint.fromZonePoint(this, point);

AffineTransform backup = g.getTransform();
double rx = (sp.x);
double ry = (sp.y);
// AffineTransform a = AffineTransform.getRotateInstance(Math.toRadians(angle), rx, ry);
// g.setTransform(a);

g.drawImage(image, (int) (sp.x - iwidth / 2), (int) (sp.y - iheight / 2), (int) iwidth, (int) iheight, this);
g.setTransform(backup);
Expand Down Expand Up @@ -3893,7 +3866,7 @@ public void setOffset(int x, int y) {
renderPathTask.cancel(true);
}

renderPathTask = new RenderPathWorker(walker, point, useAStarPathfinding, ZoneRenderer.this);
renderPathTask = new RenderPathWorker(walker, point, AppPreferences.isUsingAstarPathfinding(), ZoneRenderer.this);
threadPool.execute(renderPathTask);
} else {
if (gridlessPath.getCellPath().size() > 1) {
Expand Down Expand Up @@ -4402,8 +4375,4 @@ public Cursor createCustomCursor(String resource, String tokenName) {
}
return c;
}

public void setUseAStarPathfinding(boolean toggle) {
useAStarPathfinding = toggle;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ public Set<Point2D> getValidMoves() {
Set<Point2D> validMovePoints = new HashSet<Point2D>();

for (Entry<Point2D, Boolean> entry : validMoves.entrySet()) {
// System.out.println("VALUE: " + entry.getValue());
if (entry.getValue()) {
validMovePoints.add(new Point2D.Double(entry.getKey().getX() - x, entry.getKey().getY() - y));
}
Expand All @@ -106,7 +105,6 @@ public Shape getValidMoveShape(Zone zone) {
double x2 = entryBounds.getCenterX();
double y2 = entryBounds.getCenterY();
validMoveShape.lineTo(x2, y2);
// System.out.println("lineTo: " + validMoveShape.getCurrentPoint().toString());
}
}

Expand All @@ -115,21 +113,6 @@ public Shape getValidMoveShape(Zone zone) {
return validMoveShape;
}

// private void drawArrow(Graphics2D g, double node1X, double node1Y, double node2X, double node2Y) {
// double arrowAngle = Math.toRadians(45.0);
// double arrowLength = 10.0;
// double dx = node1X - node2X;
// double dy = node1Y - node2Y;
// double angle = Math.atan2(dy, dx);
// double x1 = Math.cos(angle + arrowAngle) * arrowLength + node2X;
// double y1 = Math.sin(angle + arrowAngle) * arrowLength + node2Y;
//
// double x2 = Math.cos(angle - arrowAngle) * arrowLength + node2X;
// double y2 = Math.sin(angle - arrowAngle) * arrowLength + node2Y;
// g.strokeLine(node2X, node2Y, x1, y1);
// g.strokeLine(node2X, node2Y, x2, y2);
// }

@Override
public int compareTo(AStarCellPoint other) {
return Double.compare(f, other.f);
Expand Down
Loading

0 comments on commit fca8022

Please sign in to comment.