Skip to content

Commit

Permalink
Add DFS algorithm for battling players
Browse files Browse the repository at this point in the history
  • Loading branch information
David52920 committed Jan 12, 2021
1 parent 2e745f3 commit 995a6f8
Show file tree
Hide file tree
Showing 12 changed files with 357 additions and 133 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.benberi.cadesim.server.config.Constants;
import com.benberi.cadesim.server.config.ServerConfiguration;
import com.benberi.cadesim.server.model.player.Player;
import com.benberi.cadesim.server.ServerContext;

public class BlockadeTimeMachine {
Expand Down Expand Up @@ -119,6 +120,17 @@ else if (timeUntilBreak == 0) {
} else {
// not enabled
}
if(turnTime == 250 && !context.getPlayerManager().listBots().isEmpty()) {
try {
context.getPlayerManager().AILogic();
for (Player p : context.getPlayerManager().listRegisteredPlayers()) {
context.getPlayerManager().sendMoveBar(p);
}
}
catch (Exception e) {
e.printStackTrace();
}
}

// if turn ended
if (
Expand Down
25 changes: 25 additions & 0 deletions server/src/com/benberi/cadesim/server/model/player/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.benberi.cadesim.server.model.cade.Team;
import com.benberi.cadesim.server.model.cade.map.BlockadeMap;
import com.benberi.cadesim.server.model.cade.map.flag.Flag;
import com.benberi.cadesim.server.model.player.ai.util.AStarNode;
import com.benberi.cadesim.server.model.player.ai.util.NPC_Type;
import com.benberi.cadesim.server.model.player.collision.PlayerCollisionStorage;
import com.benberi.cadesim.server.model.player.domain.JobbersQuality;
Expand Down Expand Up @@ -142,6 +143,9 @@ public void setFirstEntry(boolean firstEntry) {
private boolean enteredSafeLandside = false; // reason why
private boolean enteredSafeOceanside = false; // "

private List<AStarNode> path;
private Position goal;

/**
* when the player was last seen alive
*/
Expand Down Expand Up @@ -958,4 +962,25 @@ public NPC_Type getType() {
public void setType(NPC_Type typeValue) {
type = typeValue;
}

public void setPath(List<AStarNode> path) {
this.path = path;

}

public void clearPath() {
if(this.path != null) this.path.clear();
}

public List<AStarNode> getPath() {
return this.path;
}

public void setGoal(Position goal) {
this.goal = goal;
}

public Position getGoal() {
return this.goal;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
import com.benberi.cadesim.server.model.player.ai.NPC_Type2;
import com.benberi.cadesim.server.model.player.ai.NPC_Type3;
import com.benberi.cadesim.server.model.player.ai.NPC_Type4;
import com.benberi.cadesim.server.model.player.ai.util.AStarNode;
import com.benberi.cadesim.server.model.player.ai.util.AStarSearch;
import com.benberi.cadesim.server.model.player.ai.util.DFS;
import com.benberi.cadesim.server.model.player.ai.util.NPC_Type;
import com.benberi.cadesim.server.model.player.collision.CollisionCalculator;
import com.benberi.cadesim.server.model.player.domain.JobbersQuality;
Expand Down Expand Up @@ -264,13 +266,15 @@ public void setGameEnded(boolean gameEnded) {
this.gameEnded = gameEnded;
}

public DFS dfs;
/**
* constructor
*/
public PlayerManager(ServerContext context) {
this.context = context;
this.collision = new CollisionCalculator(context, this);
this.algorithm = new AStarSearch(context);
this.dfs = new DFS(context);
resetTemporarySettings();
}

Expand Down Expand Up @@ -1211,6 +1215,17 @@ private void sendTime() {
}
}

static <T> List<List<T>> chopped(List<T> list, final int L) {
List<List<T>> parts = new ArrayList<List<T>>();
final int N = list.size();
for (int i = 0; i < N; i += L) {
parts.add(new ArrayList<T>(
list.subList(i, Math.min(N, i + L)))
);
}
return parts;
}

public void sendAfterTurn() {
// deal with sunk/unspawned ships first
for (Player p : listRegisteredPlayers()) {
Expand All @@ -1222,18 +1237,34 @@ public void sendAfterTurn() {
p.giveLife();
}
}
// dfs.search(listRegisteredPlayers().get(0));
// dfs.search(listRegisteredPlayers().get(1));
// then deal with individual players
for (Player p : listRegisteredPlayers()) {
p.getPackets().sendPositions();
sendMoveBar(p);
p.getPackets().sendFlags();
}

for(Player other : listBots()) {
}

public void AILogic() {
for(Player other : listBots()) {
other.clearPath();
Position destination = getMaxTilePoints(other);
if(destination == null || this.equals(destination)) {
return;
}
List<AStarNode> path = getAlgorithm().findPath(other, destination);
if(path != null && !path.isEmpty()) {
other.setPath(path);
}
other.performLogic();
}
for (Player p : listRegisteredPlayers()) {
sendMoveBar(p);
}
}

public int getPointsDefender() {
return pointsDefender;
}
Expand Down Expand Up @@ -1792,6 +1823,9 @@ public Map<Integer, Position> checkTilePoints(Player player) {
public Position getMaxTilePoints(Player player) {
Map<Integer, Position> sortedMap = new TreeMap<Integer, Position>(Comparator.reverseOrder());
sortedMap.putAll(checkTilePoints(player));
if(sortedMap.size() == 0) {
return null;
}
return (Position)sortedMap.values().toArray()[0];
}

Expand Down Expand Up @@ -1819,16 +1853,18 @@ public void spawnAI() {
int playerListSize = listRegisteredPlayers().size();
switch(ServerConfiguration.getAISetting()) {
case "off":
sendTeamInfo();
return;
case "easy":
for (int i =0; i < playerListSize ; i++) {
for (int i =0; i < playerListSize * 3; i++) {
createBot(NPC_Type.TYPE4, randomBotName(), listRegisteredPlayers().get(0).getVessel().getID(), getBotTeam(), null, getBotFace(), 0.0f);
}
// createBot(NPC_Type.TYPE3, randomBotName(), 0, getBotTeam(), null, getBotFace(), 0.0f);
// createBot(NPC_Type.TYPE1, randomBotName(), 1, getBotTeam(), null, getBotFace(), 0.0f);
// createBot(NPC_Type.TYPE2, randomBotName(), 2, getBotTeam(), null, getBotFace(), 0.0f);
// createBot(NPC_Type.TYPE3, randomBotName(), 8, getBotTeam(), null, getBotFace(), 0.0f);
respawnAI();
sendTeamInfo();
return;

case "medium":
Expand All @@ -1839,6 +1875,7 @@ public void spawnAI() {
createBot(NPC_Type.TYPE2, randomBotName(), 3, getBotTeam(), null, getBotFace(), 0.0f);
createBot(NPC_Type.TYPE3, randomBotName(), 4, getBotTeam(), null, getBotFace(), 0.0f);
respawnAI();
sendTeamInfo();
return;
case "hard":
for (int i =0; i < playerListSize * 2 ; i++) {
Expand All @@ -1850,10 +1887,12 @@ public void spawnAI() {
createBot(NPC_Type.TYPE3, randomBotName(), 8, getBotTeam(), null, getBotFace(), 0.0f);
createBot(NPC_Type.TYPE3, randomBotName(), 8, getBotTeam(), null, getBotFace(), 0.0f);
respawnAI();
sendTeamInfo();
return;
default:
return;
}

}

private void printTeams(Player pl, boolean verbose) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import com.benberi.cadesim.server.ServerContext;
import com.benberi.cadesim.server.model.player.Player;
import com.benberi.cadesim.server.model.player.ai.util.NPC_Type;
import com.benberi.cadesim.server.model.player.move.MoveType;

import io.netty.channel.Channel;

/*
Expand All @@ -25,12 +23,30 @@ public NPC_Type1(ServerContext ctx, Channel c) {

@Override
public void calculateRoute() {

// if(getPath() != null) {
// if(getVessel().has3Moves()) {
//
// for(int slot = 0; slot < 4; slot++) { //moves to enter
// if(slot == 0) {
// continue;
// }
// if(slot < getPath().size()) {
// getMoves().setMove(slot, getPath().get(slot-1).move);
// }
// }
// }else {
// for(int slot = 0; slot < 4; slot++) { //moves to enter
// if(slot < getPath().size()) {
// getMoves().setMove(slot, getPath().get(slot).move);
// }
// }
// }
// }
}

@Override
public void performLogic() {
getMoves().setMove(0,MoveType.FORWARD);
calculateRoute();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import com.benberi.cadesim.server.ServerContext;
import com.benberi.cadesim.server.model.player.Player;
import com.benberi.cadesim.server.model.player.ai.util.NPC_Type;
import com.benberi.cadesim.server.model.player.move.MoveType;

import io.netty.channel.Channel;

/*
Expand All @@ -25,12 +23,29 @@ public NPC_Type2(ServerContext ctx, Channel c) {

@Override
public void calculateRoute() {

// if(getPath() != null) {
// if(getVessel().has3Moves()) {
// for(int slot = 0; slot < 4; slot++) { //moves to enter
// if(slot == 0) {
// continue;
// }
// if(slot < getPath().size()) {
// getMoves().setMove(slot, getPath().get(slot-1).move);
// }
// }
// }else {
// for(int slot = 0; slot < 4; slot++) { //moves to enter
// if(slot < getPath().size()) {
// getMoves().setMove(slot, getPath().get(slot).move);
// }
// }
// }
// }
}

@Override
public void performLogic() {
getMoves().setMove(0,MoveType.FORWARD);
calculateRoute();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import com.benberi.cadesim.server.ServerContext;
import com.benberi.cadesim.server.model.player.Player;
import com.benberi.cadesim.server.model.player.ai.util.NPC_Type;
import com.benberi.cadesim.server.model.player.move.MoveType;

import io.netty.channel.Channel;

/*
Expand All @@ -25,12 +23,29 @@ public NPC_Type3(ServerContext ctx, Channel c) {

@Override
public void calculateRoute() {

// if(getPath() != null) {
// if(getVessel().has3Moves()) {
// for(int slot = 0; slot < 4; slot++) { //moves to enter
// if(slot == 0) {
// continue;
// }
// if(slot < getPath().size()) {
// getMoves().setMove(slot, getPath().get(slot-1).move);
// }
// }
// }else {
// for(int slot = 0; slot < 4; slot++) { //moves to enter
// if(slot < getPath().size()) {
// getMoves().setMove(slot, getPath().get(slot).move);
// }
// }
// }
// }
}

@Override
public void performLogic() {
getMoves().setMove(0, MoveType.FORWARD);
calculateRoute();
}

@Override
Expand Down
Loading

0 comments on commit 995a6f8

Please sign in to comment.