[Jrisk-cvs] SF.net SVN: domination-code:[2543] Domination/src/net/yura/domination/ engine
Brought to you by:
yuranet
|
From: <yu...@us...> - 2024-06-03 21:09:24
|
Revision: 2543
http://sourceforge.net/p/domination/code/2543
Author: yuranet
Date: 2024-06-03 21:09:22 +0000 (Mon, 03 Jun 2024)
Log Message:
-----------
close ai after games
Modified Paths:
--------------
Domination/src/net/yura/domination/engine/Risk.java
Domination/src/net/yura/domination/engine/ai/AIManager.java
Domination/src/net/yura/domination/engine/ai/AISubmissive.java
Domination/src/net/yura/domination/engine/ai/AbstractAI.java
Modified: Domination/src/net/yura/domination/engine/Risk.java
===================================================================
--- Domination/src/net/yura/domination/engine/Risk.java 2024-06-02 18:13:56 UTC (rev 2542)
+++ Domination/src/net/yura/domination/engine/Risk.java 2024-06-03 21:09:22 UTC (rev 2543)
@@ -2081,7 +2081,13 @@
controller.sendMessage(output, false , true);
}
else {
- controller.startGame(unlimitedLocalMode);
+ // GetMap class used to call this method directly, now it takes a callback
+ // This is almost certainly not needed any more
+
+ // if we had failed to start the UI because of a currupt map file on android
+ // we will try to re download the map, and so after downloading the map,
+ // we should try and load the map into the UI again
+ controller.startGame(unlimitedLocalMode); // prob not used any more
}
}
private void getMapError(String exception) {
@@ -2731,6 +2737,7 @@
// does not work from here
closeBattle();
controller.closeGame();
+ ai.closeGame();
game = null;
}
@@ -2780,10 +2787,15 @@
unlimitedLocalMode = onlinePlayClient==null;
}
+ /**
+ * If we are opening a new game from outside the Risk command parser
+ * e.g. from a network game, or some other source
+ */
public void setGame(RiskGame b) {
if (game!=null) {
closeBattle();
controller.closeGame();
+ ai.closeGame();
}
inbox.clear();
game = b;
Modified: Domination/src/net/yura/domination/engine/ai/AIManager.java
===================================================================
--- Domination/src/net/yura/domination/engine/ai/AIManager.java 2024-06-02 18:13:56 UTC (rev 2542)
+++ Domination/src/net/yura/domination/engine/ai/AIManager.java 2024-06-03 21:09:22 UTC (rev 2543)
@@ -18,8 +18,6 @@
wait = w;
}
-
-
private final Map<Integer,AI> ais = new HashMap();
public AIManager() {
@@ -34,7 +32,7 @@
throw new RuntimeException(ex);
}
}
-
+
try {
for (AI ai : java.util.ServiceLoader.load(AI.class)) {
registerAI(ai);
@@ -57,6 +55,33 @@
ais.put(type, ai);
}
+ public int getTypeFromCommand(String command) {
+ for (AI ai:ais.values()) {
+ if (ai.getCommand().equals(command)) {
+ return ai.getType();
+ }
+ }
+ throw new IllegalArgumentException("unknown command: '" + command + "' not found in " + ais);
+ }
+
+ public String getCommandFromType(int type) {
+ for (AI ai:ais.values()) {
+ if (ai.getType() == type) {
+ return ai.getCommand();
+ }
+ }
+ throw new IllegalArgumentException("unknown type "+type);
+ }
+
+ public String[] getAICommands() {
+ String[] commands = new String[ais.size()];
+ int c=0;
+ for (AI ai:ais.values()) {
+ commands[c++] = ai.getCommand();
+ }
+ return commands;
+ }
+
public void play(Risk risk) {
RiskGame game = risk.getGame();
String output = getOutput(game, game.getCurrentPlayer().getType() );
@@ -101,30 +126,9 @@
return output;
}
- public int getTypeFromCommand(String command) {
- for (AI ai:ais.values()) {
- if (ai.getCommand().equals(command)) {
- return ai.getType();
- }
+ public void closeGame() {
+ for (AI ai : ais.values()) {
+ ai.setGame(null);
}
- throw new IllegalArgumentException("unknown command: '" + command + "' not found in " + ais);
}
-
- public String getCommandFromType(int type) {
- for (AI ai:ais.values()) {
- if (ai.getType() == type) {
- return ai.getCommand();
- }
- }
- throw new IllegalArgumentException("unknown type "+type);
- }
-
- public String[] getAICommands() {
- String[] commands = new String[ais.size()];
- int c=0;
- for (AI ai:ais.values()) {
- commands[c++] = ai.getCommand();
- }
- return commands;
- }
}
Modified: Domination/src/net/yura/domination/engine/ai/AISubmissive.java
===================================================================
--- Domination/src/net/yura/domination/engine/ai/AISubmissive.java 2024-06-02 18:13:56 UTC (rev 2542)
+++ Domination/src/net/yura/domination/engine/ai/AISubmissive.java 2024-06-03 21:09:22 UTC (rev 2543)
@@ -15,6 +15,11 @@
*/
public class AISubmissive implements AI {
+ protected Random r = new Random(); // this was always static
+
+ protected RiskGame game;
+ protected Player player;
+
public int getType() {
return Player.PLAYER_AI_CRAP;
}
@@ -25,14 +30,14 @@
public void setGame(RiskGame game) {
this.game = game;
- player = game.getCurrentPlayer();
+ if (game == null) {
+ player = null;
+ }
+ else {
+ player = game.getCurrentPlayer();
+ }
}
- protected Random r = new Random(); // this was always static
-
- protected RiskGame game;
- protected Player player;
-
public String getBattleWon() {
return "move all";
}
Modified: Domination/src/net/yura/domination/engine/ai/AbstractAI.java
===================================================================
--- Domination/src/net/yura/domination/engine/ai/AbstractAI.java 2024-06-02 18:13:56 UTC (rev 2542)
+++ Domination/src/net/yura/domination/engine/ai/AbstractAI.java 2024-06-03 21:09:22 UTC (rev 2543)
@@ -23,19 +23,26 @@
}
public void setGame(RiskGame game) {
- int mode = game.getGameMode();
- if (mode==RiskGame.MODE_CAPITAL) {
- current = capital;
+ if (game == null) {
+ // game over, clear game
+ current.setGame(null);
}
- else if (mode==RiskGame.MODE_SECRET_MISSION) {
- current = mission;
- }
else {
- current = domination;
+ int mode = game.getGameMode();
+
+ if (mode == RiskGame.MODE_CAPITAL) {
+ current = capital;
+ }
+ else if (mode == RiskGame.MODE_SECRET_MISSION) {
+ current = mission;
+ }
+ else {
+ current = domination;
+ }
+
+ current.setGame(game);
}
-
- current.setGame(game);
}
public String getBattleWon() {
@@ -62,5 +69,4 @@
public String getAutoDefendString() {
return current.getAutoDefendString();
}
-
}
|