Skip to content

Commit

Permalink
Fixed neighbor nodes to account for rocks in both A*, move permutations
Browse files Browse the repository at this point in the history
  • Loading branch information
David52920 committed Jan 27, 2021
1 parent da6bc88 commit 6bcf518
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;

import com.benberi.cadesim.server.model.player.domain.JobbersQuality;
import com.benberi.cadesim.server.util.Utils;
Expand Down Expand Up @@ -239,6 +240,7 @@ public boolean accept(File dir, String name) {
for (File f : mapList) {
names.add(f.getName());
}
Collections.sort(names, String.CASE_INSENSITIVE_ORDER);
ServerConfiguration.mapList = names;
}
catch (NullPointerException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,29 @@ public AStarNode(Position position, VesselFace face, MoveType move, AStarNode pa
this.hCost = hCost;
this.fCost = this.gCost + this.hCost;
}

public AStarNode MoveNone() {
return new AStarNode(position.copy(), face, MoveType.NONE, this, 0, 0);
}

public AStarNode MoveForward() {
return new AStarNode(position.copy().add(Position.Forward(face)), face, MoveType.FORWARD, this, 0, 0);
}

public AStarNode MoveLeft() {
return new AStarNode(position.copy().add(Position.Left(face)), face.getPrev(), MoveType.LEFT, this, 0, 0);
}

public AStarNode TurnLeft() {
return new AStarNode(position.copy(), face.getPrev(), MoveType.LEFT, this, 0, 0);
}

public AStarNode MoveRight() {
return new AStarNode(position.copy().add(Position.Right(face)), face.getNext(), MoveType.RIGHT, this, 0, 0);
}

public AStarNode TurnRight() {
return new AStarNode(position.copy(), face.getNext(), MoveType.RIGHT, this, 0, 0);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.benberi.cadesim.server.ServerContext;
import com.benberi.cadesim.server.model.player.Player;
import com.benberi.cadesim.server.model.player.move.MoveType;
import com.benberi.cadesim.server.model.player.vessel.VesselFace;
import com.benberi.cadesim.server.util.Position;

/**
Expand Down Expand Up @@ -57,39 +56,36 @@ public List<AStarNode> findPath(Player bot, Position goal){
openList.clear();
closedList.clear();
Collections.reverse(path);
if(path.size() > 3 && bot.getVessel().has3Moves()) {
path.subList(0, 3);
}else if(path.size() > 4 && !(bot.getVessel().has3Moves())) {
path.subList(0, 4);
}
return path;
}
openList.remove(current);
closedList.add(current);

int x = current.position.getX();
int y = current.position.getY();
switch (current.face) {
case NORTH:
neighbors.add(new AStarNode(new Position(x, y), VesselFace.NORTH, MoveType.NONE,current,0,0));
neighbors.add(new AStarNode(new Position(x, y+1), VesselFace.NORTH, MoveType.FORWARD,current,0,0));
neighbors.add(new AStarNode(new Position(x-1, y+1), VesselFace.WEST, MoveType.LEFT,current,0,0));
neighbors.add(new AStarNode(new Position(x+1, y+1), VesselFace.EAST, MoveType.RIGHT,current,0,0));
break;
case EAST:
neighbors.add(new AStarNode(new Position(x, y), VesselFace.EAST, MoveType.NONE,current,0,0));
neighbors.add(new AStarNode(new Position(x+1, y), VesselFace.EAST, MoveType.FORWARD,current,0,0));
neighbors.add(new AStarNode(new Position(x+1, y+1), VesselFace.NORTH, MoveType.LEFT,current,0,0));
neighbors.add(new AStarNode(new Position(x+1, y-1), VesselFace.SOUTH, MoveType.RIGHT,current,0,0));
break;
case SOUTH:
neighbors.add(new AStarNode(new Position(x, y), VesselFace.SOUTH, MoveType.NONE,current,0,0));
neighbors.add(new AStarNode(new Position(x, y-1), VesselFace.SOUTH, MoveType.FORWARD,current,0,0));
neighbors.add(new AStarNode(new Position(x-1, y-1), VesselFace.WEST, MoveType.RIGHT,current,0,0));
neighbors.add(new AStarNode(new Position(x+1, y-1), VesselFace.EAST, MoveType.LEFT,current,0,0));
break;
case WEST:
neighbors.add(new AStarNode(new Position(x, y), VesselFace.WEST, MoveType.NONE,current,0,0));
neighbors.add(new AStarNode(new Position(x-1, y), VesselFace.WEST, MoveType.FORWARD,current,0,0));
neighbors.add(new AStarNode(new Position(x-1, y+1), VesselFace.NORTH, MoveType.RIGHT,current,0,0));
neighbors.add(new AStarNode(new Position(x-1, y-1), VesselFace.SOUTH, MoveType.LEFT,current,0,0));
break;
}
//add specific neighbors
neighbors.add(current.MoveNone());
if(context.getMap().isRock(current.MoveForward().position.getX(), current.MoveForward().position.getY(), bot)) {
neighbors.add(current.TurnLeft()); // turn in place
neighbors.add(current.TurnRight());
}else {
neighbors.add(current.MoveForward());
if(context.getMap().isRock(current.MoveLeft().position.getX(), current.MoveLeft().position.getY(), bot)) {
neighbors.add(current.TurnLeft()); // turn in place
neighbors.add(current.TurnRight());
}else {
neighbors.add(current.MoveLeft());
}
if(context.getMap().isRock(current.MoveRight().position.getX(), current.MoveRight().position.getY(), bot)) {
neighbors.add(current.TurnLeft()); // turn in place
neighbors.add(current.TurnRight());
}else {
neighbors.add(current.MoveRight());
}
}

for(AStarNode neighborNode : neighbors) {
//skip if out of particular move
if((leftAmount == 0 && neighborNode.move == MoveType.LEFT) ||
Expand All @@ -99,20 +95,19 @@ public List<AStarNode> findPath(Player bot, Position goal){
}
// Compute the cost to get *to* the action tile.
double costToReach = current.position.distance(neighborNode.position);
if(neighborNode.move == MoveType.FORWARD) {
if(neighborNode.move == MoveType.FORWARD) { // use turns over forwards
costToReach += 0.2;
}
int at = context.getMap().getTile(neighborNode.position.getX(), neighborNode.position.getY());
if(context.getMap().isWind(at)){ // special action tiles
neighborNode.position = context.getMap().getNextActionTilePositionForTile(neighborNode.position, at);
costToReach += getActionCost(neighborNode.position, at);
costToReach += getActionCost(neighborNode, at);
}
if(context.getMap().isWhirlpool(at)) {
neighborNode.position = context.getMap().getFinalActionTilePosition(at, neighborNode.position, 0);
neighborNode.face = neighborNode.face.getNext();
costToReach += getActionCost(neighborNode.position, at);
costToReach += getActionCost(neighborNode, at);
}
if(at == 1 || at == 2) continue;
if(context.getPlayerManager().getPlayerByPosition(neighborNode.position.getX(), neighborNode.position.getY()) != null) continue; // skip if player
double gCost = current.gCost + costToReach;
double hCost = heuristicDistance(neighborNode.position,goal);
Expand All @@ -127,11 +122,11 @@ public List<AStarNode> findPath(Player bot, Position goal){
/*
* Returns a double value for cost to use a specific tile
*/
private double getActionCost(Position node, int currentTile) {
if(currentTile > 3 && currentTile < 11) {
private double getActionCost(AStarNode node, int currentTile) {
if(currentTile > 3 && currentTile < 11 && node.move == MoveType.NONE) {
return 0.2;
}else {
return 1;
return 0.6;
}
}

Expand Down
49 changes: 20 additions & 29 deletions server/src/com/benberi/cadesim/server/model/player/ai/util/DFS.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import com.benberi.cadesim.server.model.cade.map.BlockadeMap;
import com.benberi.cadesim.server.model.player.Player;
import com.benberi.cadesim.server.model.player.move.MoveType;
import com.benberi.cadesim.server.model.player.vessel.VesselFace;
import com.benberi.cadesim.server.util.Position;

public class DFS {
Expand All @@ -20,6 +19,7 @@ public DFS(ServerContext context) {
this.context = context;
}

//get all move possibilities
public void findMovePermutations(Player player) {
Position playerPosition = player.copy();
List<MoveState> neighbors = new ArrayList<>();
Expand All @@ -37,34 +37,25 @@ public void findMovePermutations(Player player) {
}
stateList.put(state.position, state);
if(state.time < (player.getVessel().has3Moves() ? 3 : 4)) {
int x = state.position.getX();
int y = state.position.getY();
switch (state.face) {
case NORTH:
neighbors.add(new MoveState(new Position(x,y), VesselFace.NORTH, MoveType.NONE,(byte) (state.time + 1)));
neighbors.add(new MoveState(new Position(x,y+1), VesselFace.NORTH, MoveType.FORWARD,(byte) (state.time + 1)));
neighbors.add(new MoveState(new Position(x-1,y+1), VesselFace.WEST, MoveType.LEFT, (byte) (state.time + 1)));
neighbors.add(new MoveState(new Position(x+1,y+1), VesselFace.EAST, MoveType.RIGHT,(byte) (state.time + 1)));
break;
case EAST:
neighbors.add(new MoveState(new Position(x,y), VesselFace.EAST, MoveType.NONE,(byte) (state.time + 1)));
neighbors.add(new MoveState(new Position(x+1,y), VesselFace.EAST, MoveType.FORWARD,(byte) (state.time + 1)));
neighbors.add(new MoveState(new Position(x+1,y+1), VesselFace.NORTH, MoveType.LEFT,(byte) (state.time + 1)));
neighbors.add(new MoveState(new Position(x+1,y-1), VesselFace.SOUTH, MoveType.RIGHT,(byte) (state.time + 1)));
break;
case SOUTH:
neighbors.add(new MoveState(new Position(x,y), VesselFace.SOUTH, MoveType.NONE,(byte) (state.time + 1)));
neighbors.add(new MoveState(new Position(x,y-1), VesselFace.SOUTH, MoveType.FORWARD,(byte) (state.time + 1)));
neighbors.add(new MoveState(new Position(x-1,y-1), VesselFace.WEST, MoveType.RIGHT,(byte) (state.time + 1)));
neighbors.add(new MoveState(new Position(x+1,y+1), VesselFace.EAST, MoveType.LEFT,(byte) (state.time + 1)));
break;
case WEST:
neighbors.add(new MoveState(new Position(x,y), VesselFace.WEST, MoveType.NONE,(byte) (state.time + 1)));
neighbors.add(new MoveState(new Position(x-1,y), VesselFace.WEST, MoveType.FORWARD,(byte) (state.time + 1)));
neighbors.add(new MoveState(new Position(x-1,y+1), VesselFace.NORTH, MoveType.RIGHT,(byte) (state.time + 1)));
neighbors.add(new MoveState(new Position(x-1,y-1), VesselFace.SOUTH, MoveType.LEFT,(byte) (state.time + 1)));
break;
}
neighbors.add(state.MoveNone());
if(context.getMap().isRock(state.MoveForward().position.getX(), state.MoveForward().position.getY(), player)) {
neighbors.add(state.TurnLeft()); // turn in place
neighbors.add(state.TurnRight());
}else {
neighbors.add(state.MoveForward());
if(context.getMap().isRock(state.MoveLeft().position.getX(), state.MoveLeft().position.getY(), player)) {
neighbors.add(state.TurnLeft()); // turn in place
neighbors.add(state.TurnRight());
}else {
neighbors.add(state.MoveLeft());
}
if(context.getMap().isRock(state.MoveRight().position.getX(), state.MoveRight().position.getY(), player)) {
neighbors.add(state.TurnLeft()); // turn in place
neighbors.add(state.TurnRight());
}else {
neighbors.add(state.MoveRight());
}
}
}
for(MoveState neighborState : neighbors) {
if(neighborState != null ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,30 @@ public MoveState(Position position, VesselFace face, MoveType move, byte time) {
visited = false;
}

public MoveState MoveNone() {
return new MoveState(position.copy(), face, MoveType.NONE, time);
}

public MoveState MoveForward() {
return new MoveState(position.copy().add(Position.Forward(face)), face, MoveType.FORWARD, time);
}

public MoveState MoveLeft() {
return new MoveState(position.copy().add(Position.Left(face)), face.getPrev(), MoveType.LEFT, time);
}

public MoveState TurnLeft() {
return new MoveState(position.copy(), face.getPrev(), MoveType.LEFT, time);
}

public MoveState MoveRight() {
return new MoveState(position.copy().add(Position.Right(face)), face.getNext(), MoveType.RIGHT, time);
}

public MoveState TurnRight() {
return new MoveState(position.copy(), face.getNext(), MoveType.RIGHT, time);
}

@Override
public boolean equals(Object o) {
if(o instanceof MoveState) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,9 @@ else if (
// complete the game refresh
playerManager.renewGame();
context.getTimeMachine().renewRound(); // bugfix - order matters
playerManager.spawnAI();
if(ServerConfiguration.getAISetting() != "off") {
playerManager.spawnAI();
}
for(Player other : playerManager.listBots()) {
other.performLogic();
}
Expand Down
53 changes: 52 additions & 1 deletion server/src/com/benberi/cadesim/server/util/Position.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.benberi.cadesim.server.util;

import com.benberi.cadesim.server.model.player.vessel.VesselFace;

/**
* 2D Position
*/
Expand Down Expand Up @@ -133,6 +135,55 @@ public boolean equals(Object o) {

@Override
public String toString() {
return "Position x: " + this.getX() + ", Position y: " + this.getY();
return "Position (x,y): " + this.getX() + "," + this.getY();
}

public static Position Forward(VesselFace face) {
switch(face) {
case NORTH:
return new Position(0,1);
case SOUTH:
return new Position(0,-1);
case WEST:
return new Position(-1,0);
case EAST:
return new Position(1,0);
default:
return new Position(0,0);
}
}

public static Position Left(VesselFace face) {
switch(face) {
case NORTH:
return new Position(-1,1);
case SOUTH:
return new Position(1,-1);
case WEST:
return new Position(-1,-1);
case EAST:
return new Position(1,1);
default:
return new Position(0,0);
}
}

public static Position Right(VesselFace face) {
switch(face) {
case NORTH:
return new Position(1,1);
case SOUTH:
return new Position(-1,-1);
case WEST:
return new Position(-1,1);
case EAST:
return new Position(1,-1);
default:
return new Position(0,0);
}
}

public Position add(Position other) {
return new Position(this.x += other.x, this.y += other.y);
}
}

0 comments on commit 6bcf518

Please sign in to comment.