/** * Create a new Random Movement * @param Map * @throws SlickException */ protected void setupRandomMovement(Map map) throws SlickException { Path p = null; int randomMovementDirection = new Random().nextInt(4); if (randomMovementDirection == Entity.DOWN_RIGHT) { p = map.getPathFinder().findPath(this, this.getXTile(), this.getYTile(), (this.getXTile() + 1 == map.getWidth()) ? this.getXTile() - 1 : this.getXTile() + 1, this.getYTile()); } else if (randomMovementDirection == Entity.DOWN_LEFT) { p = map.getPathFinder().findPath(this, this.getXTile(), this.getYTile(), this.getXTile(), (this.getYTile() + 1 == map.getHeight()) ? this.getYTile() - 1: this.getYTile() + 1); } else if (randomMovementDirection == Entity.UP_RIGHT) { p = map.getPathFinder().findPath(this, this.getXTile(), this.getYTile(), (this.getXTile() == 0) ? 1 : this.getXTile() - 1, this.getYTile()); } else if (randomMovementDirection == Entity.UP_LEFT) { p = map.getPathFinder().findPath(this, this.getXTile(), this.getYTile(), this.getXTile(), (this.getYTile() == 0) ? 1 : this.getYTile() - 1); } if(p == null) { state = IDLE; } else { state = RANDOMLY_MOVING; this.startMoving(p, map); } }
/** * Character will move back to the spawning point to leave the Store * @param map * @throws SlickException */ private void updateWantsToLeave(Map map) throws SlickException { // Let's make the Character go there Path p = null; // Find the Path to the Spawning Point p = map.getPathFinder().findPath(this, this.getXTile(), this.getYTile(), map.getSpawnPoint().x, map.getSpawnPoint().y); // Found a valid Path if(p != null) { // Change its state to MOVING_TO_LEAVE and make it go state = MOVING_TO_LEAVE; this.startMoving(p, map); // Say something about the store if(visitRating > 0) this.hadPositiveReaction(); else if(visitRating < 0) this.hadNegativeReaction(true); else this.hadNeutralReaction(); } else { // If the character is already at the spawn point, then it's ok if(this.getXTile() == map.getSpawnPoint().x && this.getYTile() == map.getSpawnPoint().y) { state = MOVING_TO_LEAVE; this.stopMoving(map); } // Character is stuck in the Store. It will warn the Player floatingText.show("I can't leave the store!"); state = WANTS_TO_LEAVE; } }
private Path findPathToClosestResourceCell(final EntityHarvester harv) { Pos resourcePos = getClosestResourceCellBfs(harv); if (resourcePos != null) { Path pathToResource = harv.findPathFromTo(harv, (int) resourcePos.getX(), (int) resourcePos.getY()); return pathToResource; } return null; }
public void deployEntity(EntityActor newInstance) { if (newInstance instanceof MobileEntity) { final MobileEntity me = (MobileEntity) newInstance; me.isVisible = true; newInstance.setWorld(this.world); world.spawnEntityInWorld(newInstance); Path p = new Path(); p.appendStep((int) exitPos.getX(), (int) exitPos.getY()); p.appendStep((int) rallyPos.getX(), (int) rallyPos.getY()); SubCell freeSubCell = world.blockingEntityMap.getFreeSubCell(rallyPos, SubCell.CENTER); if (freeSubCell != null) { me.currentSubcell = freeSubCell; me.desiredSubcell = freeSubCell; me.setCellPos(exitPos); me.startMovingByPath(p, this); } else { SubCell sc = SubCell.CENTER; MobileEntity blocker = world.getMobileEntityInCell(exitPos); if (blocker != null) { blocker.nudge(me, true); } blocker = world.getMobileEntityInCell(rallyPos); if (blocker != null) { blocker.nudge(me, true); } me.setCellPos(exitPos); me.currentSubcell = sc; me.desiredSubcell = sc; me.startMovingByPath(p, this); } } }
@Override public void startMovingByPath(Path p, EntityBuilding ignoreBuilding) { this.goalX = (int) p.getX(p.getLength() - 1); this.goalY = (int) p.getY(p.getLength() - 1); queueActivity(new MoveInfantry(this, p, new Pos(goalX, goalY), ignoreBuilding)); }
public Path getPath(int sx, int sy, int tx, int ty) { return pathFinder.findPath(new DummyMover(), sx, sy, tx, ty); }
/** * Finds a Path to an Asset, handles pathfinding exceptions * @param asset * @throws SlickException */ public void goToAsset(Asset asset, Map map, int nextState) throws SlickException { // Let's make the Character go there boolean pathIsValid = false; Path p = null; // Get a random usable tile from the Asset int[] randomUsableTile = asset.getRandomUsableTile(map); if(randomUsableTile != null) { // Find the Path there p = map.getPathFinder().findPath(this, this.getXTile(), this.getYTile(), randomUsableTile[0], randomUsableTile[1]); if(p != null) { pathIsValid = true; } } // Go! if(pathIsValid) { // Change its state to the next state and make it go state = nextState; // Save info about the Target Asset targetAsset = asset; targetAssetXTile = asset.getXTile(); targetAssetYTile = asset.getYTile(); targetAssetFacingDirection = asset.getFacingDirection(); // Move! this.startMoving(p, map); return; } // Something wrong with the Path? if((randomUsableTile != null) && this.getXTile() == randomUsableTile[0] && this.getYTile() == randomUsableTile[1]) { // Already at the Target state = nextState; this.stopMoving(map); } else { // Couldn't find path // Make the Character say it can't find a way to reach the Asset! this.sayCantReachAsset(asset); this.unlockAsset(asset); state = IDLE; } }
public Path getPath() { return path; }
public void setPath(Path path) { this.path = path; }
public void setPath(Path path) { this.path = path; this.keepGoing = (path != null); }
@Override public Path findPathFromTo(MobileEntity e, int aGoalX, int aGoalY) { // TODO Auto-generated method stub return null; }
public MoveInfantry(MobileEntity me, Path scriptedPath, Pos destinationCell, EntityBuilding aIgnoreBuilding) { this(me, destinationCell, 0); this.currentPath = scriptedPath; this.ignoreBuilding = aIgnoreBuilding; }
public Move(MobileEntity me, Path scriptedPath, Pos destinationCell, EntityBuilding aIgnoreBuilding) { this(me, destinationCell, 0); this.currentPath = scriptedPath; this.ignoreBuilding = aIgnoreBuilding; }
@Override public Path findPathFromTo(MobileEntity e, int aGoalX, int aGoalY) { return world.getVehiclePathfinder().findPathFromTo(this, aGoalX, aGoalY); }
private boolean tryToMoveOutEntityToUnlockedCells(EntityVehicle v) { boolean isSuccess = false; int exitX = getTileX() / 24 + 1; int exitY = getTileY() / 24 + HEIGHT_TILES - 2; boolean isExitBlocked = isCellBlocked(exitX, exitY); if (isExitBlocked) { // Try to nudge blocker MobileEntity blocker = world.getMobileEntityInCell(new Pos(exitX, exitY)); if (blocker != null) { blocker.nudge(null, true); } return false; } Path p = new Path(); p.appendStep((int) targetEntity.getCenterPosX(), (int) targetEntity.getCenterPosY()); p.appendStep(exitX, exitY); for (int i = 0; i < 6; i++) { int cellX = exitX + this.exitDirectionsX[i]; int cellY = exitY + this.exitDirectionsY[i]; if (!isCellBlocked(cellX, cellY)) { isSuccess = true; tx = cellX; ty = cellY; p.appendStep(cellX, cellY); v.startMovingByPath(p, this); return true; } } return false; }
@Override public Path findPathFromTo(MobileEntity e, int aGoalX, int aGoalY) { return world.getInfantryPathfinder().findPathFromTo((EntityInfantry) e, aGoalX, aGoalY); }
protected void drawPath(Graphics g) { if (!Main.DEBUG_MODE) { return; } if (this.currentActivity != null) { Path currentPath = null; int pathIndex = 0; // For vehicles if ((this.currentActivity instanceof Move) && ((Move) this.currentActivity).currentPath != null) { currentPath = ((Move) this.currentActivity).currentPath; pathIndex = ((Move) currentActivity).currentPathIndex; } else if ((this.currentActivity instanceof Move.MovePart) && ((Move.MovePart) this.currentActivity).parentMove.currentPath != null) { currentPath = ((Move.MovePart) this.currentActivity).parentMove.currentPath; pathIndex = ((Move.MovePart) this.currentActivity).parentMove.currentPathIndex; } // For infantry if ((this.currentActivity instanceof MoveInfantry) && ((MoveInfantry) this.currentActivity).currentPath != null) { currentPath = ((MoveInfantry) this.currentActivity).currentPath; pathIndex = ((MoveInfantry) currentActivity).currentPathIndex; } else if ((this.currentActivity instanceof MoveInfantry.MovePart) && ((MoveInfantry.MovePart) this.currentActivity).parentMove.currentPath != null) { currentPath = ((MoveInfantry.MovePart) this.currentActivity).parentMove.currentPath; pathIndex = ((MoveInfantry.MovePart) this.currentActivity).parentMove.currentPathIndex; } if (currentPath == null) { return; } g.setColor(Color.green); g.setLineWidth(1); if (pathIndex == currentPath.getLength()) { return; } g.drawLine(this.getCenterPosX(), this.getCenterPosY(), currentPath.getStep(pathIndex - 1).getX() * 24 + 12, currentPath.getStep(pathIndex - 1).getY() * 24 + 12); g.fillOval(this.goalX * 24 + 12 - 2, this.goalY * 24 + 12 - 2, 5, 5); for (int i = pathIndex - 1; i < currentPath.getLength() - 1; i++) { Step from = currentPath.getStep(i); Step to = currentPath.getStep(i + 1); g.fillOval(from.getX() * 24 + 12 - 2, from.getY() * 24 + 12 - 2, 5, 5); g.fillOval(to.getX() * 24 + 12 - 2, to.getY() * 24 + 12 - 2, 5, 5); g.drawLine(from.getX() * 24 + 12, from.getY() * 24 + 12, to.getX() * 24 + 12, to.getY() * 24 + 12); } //g.setColor(Color.orange); //g.fillOval(this.targetCellX * 24 + 12, this.targetCellY * 24 + 12, 5, 5); } // Draw grid /*final int GRID_SIZE = 3; g.setColor(Color.gray); for (int i = (int) (posX / 24 - GRID_SIZE); i < posX / 24 + GRID_SIZE; i++) { for (int j = (int) (posY / 24 - GRID_SIZE); j < posY / 24 + GRID_SIZE; j++) { g.drawRect(i * 24, j * 24, 24, 24); } }*/ }
public void startMovingByPath(Path p, EntityBuilding ignoreBuilding) { this.goalX = (int) p.getX(p.getLength() - 1); this.goalY = (int) p.getY(p.getLength() - 1); queueActivity(new Move(this, p, new Pos(goalX, goalY), ignoreBuilding)); }
public Path findPathFromTo(EntityVehicle me, int goalX, int goalY) { MobileEntity m = (MobileEntity) me; return this.pathfinder.findPath(me, (int) m.getCellPos().getX(), (int) m.getCellPos().getY(), goalX, goalY); }
public Path findPathFromTo(EntityInfantry me, int goalX, int goalY) { MobileEntity m = (MobileEntity) me; return this.pathfinder.findPath(me, (int) m.getCellPos().getX(), (int) m.getCellPos().getY(), goalX, goalY); }
public static void main(String[] args) { SimpleMap map = new SimpleMap(); /** documentation at: http://slick.ninjacave.com/javadoc/org/newdawn/slick/util/pathfinding/AStarPathFinder.html */ AStarPathFinder pathFinder = new AStarPathFinder(map, MAX_PATH_LENGTH, false); Path path = pathFinder.findPath(null, START_X, START_Y, GOAL_X, GOAL_Y); int length = path.getLength(); System.out.println("Found path of length: " + length + "."); for(int i = 0; i < length; i++) { System.out.println("Move to: " + path.getX(i) + "," + path.getY(i) + "."); } }
public PathfinderChecker(String pathScenario) { Ubik u= new Ubik(1); u.setPathScenario(pathScenario); u.init(); String s= "Scenario checked: " + pathScenario + "\n"; int np= (u.getBuilding().getFloor(0).getPersonHandler().getPersons()).size(); if(np==0) { s+="Add some person for a thorough inform \n"; return; } s+= "Persons included: " + np + "\n"; Person p=(u.getBuilding().getFloor(0).getPersonHandler().getPersons()).get(0); List<Room> lr= PositionTools.getRooms(p); s+= "List of Rooms included: \n"; for(Room r: lr){ s+="\t" + r.getName()+ "\n"; } BooleanPathfindingMap pmap = new BooleanPathfindingMap(p, 10);//perception of 10, the person can see 10 positions ahead to detect mobile obstacles AStarPathFinder pathfinder = new AStarPathFinder(pmap, maxSteps, true);//plan with no more than 1000 steps,diagonal movement allowed List<String> nrr=new ArrayList<String>(); //non reachable rooms for(Room r1: lr){ for(Room r2:lr){ if(lr.indexOf(r1)<lr.indexOf(r2)){//if r1 reaches r2, r2 also reaches r1 //check reachability Path path = pathfinder.findPath(null, r1.getCenter().x, r1.getCenter().y, r2.getCenter().x, r2.getCenter().y); //get path LOG.info("Reachability " + r1.getName() + " to " + r2.getName() + " checked"); if(path==null) nrr.add(r1.getName() + " <-> " + r2.getName()); } } } if(nrr.isEmpty()) s+="All rooms are reachable!"; else{ s+= "Not reachable rooms with " + maxSteps + " steps: \n"; for(String nonreachable: nrr){ s+="\t" + nonreachable + "\n"; } } inform=s; }
/** * Gives the Path to the Entity and tells it it should start moving * @param targetXTile * @param targetYTile * @throws SlickException * @throws NotAValidPathException */ public void startMoving(Path p, Map map) throws SlickException { path = p; currentPathStep = 1; setupNextMovementStep(map); }
public abstract Path findPathFromTo(MobileEntity e, int aGoalX, int aGoalY);