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
 * Spotless Applied
 * Had to remove grgit.branch.current().name for now, getting errors for
unknown reason

Signed-off-by: Jamz <[email protected]>
  • Loading branch information
JamzTheMan committed Jun 1, 2018
1 parent c4b321d commit abfddaf
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 351 deletions.
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ ext {
git = org.ajoberstar.grgit.Grgit.open(currentDir: file('.'))
revision = git.head().abbreviatedId
revisionFull = git.head().id
branch = git.branch.getCurrent().getName()
// branch = grgit.branch.current().name
gdxVersion = '1.9.6'
box2DLightsVersion = '1.4'
aiVersion = '1.8.1'
Expand Down Expand Up @@ -251,7 +251,7 @@ task uberJar(type: Jar) {
'Implementation-Vendor': vendor,
'Git-Commit': revision,
'Git-Commit-SHA': revisionFull,
'Git-Branch': branch,
// 'Git-Branch': branch,
'Built-By': System.getProperty('user.name'),
'Built-Date': new Date(),
'Built-JDK': System.getProperty('java.version'),
Expand All @@ -272,7 +272,7 @@ task uberJar(type: Jar) {
// For logging Git Commit during CI
task displayGitInfo {
doLast {
println 'Git-Branch: ' + branch
// println 'Git-Branch: ' + branch
println 'Git-Commit-SHA: ' + revisionFull
}
}
Expand Down
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
10 changes: 9 additions & 1 deletion 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,16 @@ 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 @@ -279,6 +284,9 @@ public void actionPerformed(ActionEvent e) {
ioe.printStackTrace();
}

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

return button;
}

Expand Down
57 changes: 13 additions & 44 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,7 +194,9 @@ 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 All @@ -226,7 +213,7 @@ public ZoneRenderer(Zone zone) {
setZoneScale(new Scale());
zoneView = new ZoneView(zone);

//add(MapTool.getFrame().getFxPanel(), PositionalLayout.Position.NW);
// add(MapTool.getFrame().getFxPanel(), PositionalLayout.Position.NW);

// DnD
setTransferHandler(new TransferableHelper());
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;
}
}
Loading

0 comments on commit abfddaf

Please sign in to comment.