Skip to content

Commit

Permalink
#49: Add proper pathfinding taking VBL into account
Browse files Browse the repository at this point in the history
 - More progress on A*
 - Added Terrain Modifiers calculation and option on Token properties
 - WIP on multi threading...

Task-Url: #49

Signed-off-by: Jamz <[email protected]>
  • Loading branch information
JamzTheMan committed Apr 6, 2018
1 parent 3a8b4a2 commit c3e5a7a
Show file tree
Hide file tree
Showing 21 changed files with 677 additions and 451 deletions.
8 changes: 7 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,12 @@ dependencies {
compile "com.badlogicgames.gdx:gdx-box2d:$gdxVersion"
compile "com.badlogicgames.box2dlights:box2dlights:$box2DLightsVersion"

// https://mvnrepository.com/artifact/org.locationtech.jts/jts-core
// https://locationtech.github.io/jts/jts-features.html
compile group: 'org.locationtech.jts', name: 'jts-core', version: '1.15.0'
// https://mvnrepository.com/artifact/org.locationtech.jts/jts
//compile group: 'org.locationtech.jts', name: 'jts', version: '1.15.0', ext: 'pom'

// Not in use at this time, left for easy reference later...
//compile "com.badlogicgames.gdx:gdx-controllers:$gdxVersion"
//compile "com.badlogicgames.gdx:gdx-freetype:$gdxVersion"
Expand Down Expand Up @@ -316,7 +322,7 @@ task deploy(dependsOn: [displayGitInfo, uberJar, copyPackageExtras, prepareInnoS
"-appclass", mainClassName,
"-srcdir", "build/libs",
"-outdir", "releases/release-"+version,
"-outfile", project.name,
"-outfile", vendor + "-" + project.name,
"-name", project.name,
"-title", project.name,
"-vendor", vendor,
Expand Down
17 changes: 16 additions & 1 deletion src/main/java/net/rptools/maptool/client/tool/PointerTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import javax.swing.JComponent;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand Down Expand Up @@ -235,12 +236,26 @@ public void startTokenDrag(Token keyToken) {
// Not allowed
return;
}
renderer.addMoveSelectionSet(p.getName(), tokenBeingDragged.getId(), renderer.getOwnedTokens(renderer.getSelectedTokenSet()), false);

// TokenDragWorker tdw = new TokenDragWorker();
// tdw.execute();

renderer.addMoveSelectionSet(p.getName(), tokenBeingDragged.getId(), renderer.getOwnedTokens(renderer.getSelectedTokenSet()), false, true);
MapTool.serverCommand().startTokenMove(p.getName(), renderer.getZone().getId(), tokenBeingDragged.getId(), renderer.getOwnedTokens(renderer.getSelectedTokenSet()));

isDraggingToken = true;
}

// public class TokenDragWorker extends SwingWorker<Void, Void> {
// @Override
// protected Void doInBackground() throws Exception {
// Player p = MapTool.getPlayer();
// renderer.addMoveSelectionSet(p.getName(), tokenBeingDragged.getId(), renderer.getOwnedTokens(renderer.getSelectedTokenSet()), false, true);
// //MapTool.serverCommand().startTokenMove(p.getName(), renderer.getZone().getId(), tokenBeingDragged.getId(), renderer.getOwnedTokens(renderer.getSelectedTokenSet()));
// return null;
// }
// }

public void stopTokenDrag() {
renderer.commitMoveSelectionSet(tokenBeingDragged.getId()); // TODO: figure out a better way
isDraggingToken = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,8 @@ public void run() {
getPortraitPanel().setImageId(token.getPortraitImage());
getTokenLayoutPanel().setToken(token);
getImageTableCombo().setSelectedItem(token.getImageTableName());
getTokenOpacitySlider()
.setValue(new BigDecimal(token.getTokenOpacity()).multiply(new BigDecimal(100)).intValue());
getTokenOpacitySlider().setValue(new BigDecimal(token.getTokenOpacity()).multiply(new BigDecimal(100)).intValue());
getTerrainModifier().setText(Double.toString(token.getTerrainModifier()));

// Jamz: Init the VBL tab...
JTabbedPane tabbedPane = getTabbedPane();
Expand Down Expand Up @@ -494,7 +494,7 @@ public void stateChanged(ChangeEvent e) {
JSlider source = (JSlider) e.getSource();
if (!source.getValueIsAdjusting()) {
int value = (int) source.getValue();
getTokenOpacityLabel().setText(new BigDecimal(value).toString() + "%");
getTokenOpacityValueLabel().setText(new BigDecimal(value).toString() + "%");
}
}
}
Expand All @@ -503,8 +503,12 @@ public JSlider getTokenOpacitySlider() {
return (JSlider) getComponent("tokenOpacitySlider");
}

public JLabel getTokenOpacityLabel() {
return (JLabel) getComponent("tokenOpacityLabel");
public JLabel getTokenOpacityValueLabel() {
return (JLabel) getComponent("tokenOpacityValueLabel");
}

public JTextField getTerrainModifier() {
return (JTextField) getComponent("terrainModifier");
}

public void initOKButton() {
Expand Down Expand Up @@ -546,8 +550,14 @@ public boolean commit() {
token.setPropertyType((String) getPropertyTypeCombo().getSelectedItem());
token.setSightType((String) getSightTypeCombo().getSelectedItem());
token.setImageTableName((String) getImageTableCombo().getSelectedItem());
token.setTokenOpacity(
new BigDecimal(getTokenOpacitySlider().getValue()).divide(new BigDecimal(100)).floatValue());
token.setTokenOpacity(new BigDecimal(getTokenOpacitySlider().getValue()).divide(new BigDecimal(100)).floatValue());

try {
token.setTerrainModifier(Double.parseDouble(getTerrainModifier().getText()));
} catch (NumberFormatException e) {
// User didn't enter a valid float...
token.setTerrainModifier(1);
}

// Get the states
Component[] stateComponents = getStatesPanel().getComponents();
Expand Down
119 changes: 119 additions & 0 deletions src/main/java/net/rptools/maptool/client/ui/zone/RenderPathWorker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
* This software Copyright by the RPTools.net development team, and licensed under the Affero GPL Version 3 or, at your option, any later version.
*
* MapTool Source Code is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the GNU Affero General Public License * along with this source Code. If not, please visit <http://www.gnu.org/licenses/> and specifically the Affero license text
* at <http://www.gnu.org/licenses/agpl.html>.
*/
package net.rptools.maptool.client.ui.zone;

import java.awt.Graphics2D;
import java.util.List;

import javax.swing.SwingWorker;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import net.rptools.maptool.client.ui.zone.ZoneRenderer.SelectionSet;
import net.rptools.maptool.client.walker.ZoneWalker;
import net.rptools.maptool.model.CellPoint;
import net.rptools.maptool.model.Path;
import net.rptools.maptool.model.TokenFootprint;

public class RenderPathWorker extends SwingWorker<Void, Void> {
private static final Logger log = LogManager.getLogger(RenderPathWorker.class);

ZoneRenderer zoneRenderer;
Graphics2D g;
ZoneWalker walker;
TokenFootprint footprint;
public static boolean isWorking = false;
public static boolean cancel = false;
Path<CellPoint> path;
CellPoint startPoint, endPoint;
boolean setWaypoint = false;
private boolean restrictMovement = false;
int doSomeWork = 0;

long start = System.currentTimeMillis();

RenderPathWorker(ZoneRenderer zoneRenderer, Graphics2D g, ZoneWalker walker, TokenFootprint footprint) {
this.zoneRenderer = zoneRenderer;
this.g = g;
this.walker = walker;
this.footprint = footprint;
}

RenderPathWorker(ZoneWalker walker, CellPoint startPoint, CellPoint endPoint) {
this.walker = walker;
this.startPoint = startPoint;
this.endPoint = endPoint;
doSomeWork = 1;
}

public RenderPathWorker(ZoneWalker walker, CellPoint endPoint, boolean restrictMovement) {
this.walker = walker;
this.endPoint = endPoint;
doSomeWork = 2;
this.restrictMovement = restrictMovement;
}

@Override
protected Void doInBackground() throws Exception {
log.info("start RenderPathWorker: " + (System.currentTimeMillis() - start) + "ms");
// log.info("Worker working!");
isWorking = true;
RenderPathWorker something = this;
while (!isCancelled()) {
Thread.sleep(20);
if ((System.currentTimeMillis() - start) > 500)
break;
}
log.info("\n\nwas cancelled: " + isCancelled());
// log.info("something? " + (something == null));

if (doSomeWork == 1) {
walker.setWaypoints(startPoint, endPoint);
} else if (doSomeWork == 2) {
walker.replaceLastWaypoint(endPoint, restrictMovement);
} else {
path = walker.getPath(this); // walker.getPath() is where the real magic happens!
log.info("Time for walker.getPath(): " + (System.currentTimeMillis() - start) + "ms");
process(null);
}

// ZoneRenderer.setRenderedPath(walker.getPath(something));
// this.cancel(false);
// zoneRenderer.renderPath(g, path, footprint);
// log.info("renderPathWorker.isCancelled? " + isCancelled());

// while (!this.isCancelled()) {
// //log.info("Swingworker waiting...");
// }

return null;
}

@Override
protected void process(List<Void> v) {
// ZoneRenderer.setRenderedPath(path);
zoneRenderer.renderPath(g, path, footprint); // invokes UI thread?
log.info("process renderPath done: " + (System.currentTimeMillis() - start) + "ms");
}

// This is invoked in the UI thread!
@Override
protected void done() {
isWorking = false;
cancel = false;
// log.info("Swingworker done! calling renderPath: " + (System.currentTimeMillis() - start) + "ms");
// zoneRenderer.renderPath(g, path, footprint);
// ZoneRenderer.setRenderedPath(path);

// isStarted = false;
log.info("Time for RenderPathWorker: " + (System.currentTimeMillis() - start) + "ms");
}

}
Loading

0 comments on commit c3e5a7a

Please sign in to comment.