Search in sources :

Example 1 with SOCRobotClient

use of soc.robot.SOCRobotClient in project JSettlers2 by jdmonin.

the class SOCForceEndTurnThread method run.

/**
 * If our targeted robot player is still the current player, force-end their turn.
 * If not current player but game is waiting for them to discard or pick free resources,
 * choose randomly so the game can continue.
 * Calls {@link SOCGameHandler#endGameTurnOrForce(SOCGame, int, String, Connection, boolean)}.
 */
@Override
public void run() {
    final String rname = pl.getName();
    final int plNum = pl.getPlayerNumber();
    final int gs = ga.getGameState();
    final boolean notCurrentPlayer = (ga.getCurrentPlayerNumber() != plNum);
    // waiting for the bot to discard or gain resources.
    if (notCurrentPlayer && (gs != SOCGame.WAITING_FOR_DISCARDS) && (gs != SOCGame.WAITING_FOR_PICK_GOLD_RESOURCE) && (gs != SOCGame.STARTS_WAITING_FOR_PICK_GOLD_RESOURCE)) {
        return;
    }
    Connection rconn = srv.getConnection(rname);
    System.err.println("For robot " + rname + ((notCurrentPlayer) ? ": force discard/pick" : ": force end turn") + " in game " + ga.getName() + " pn=" + plNum + " state " + gs);
    if (gs == SOCGame.WAITING_FOR_DISCARDS)
        System.err.println("  srv card count = " + pl.getResources().getTotal());
    else if (gs == SOCGame.WAITING_FOR_PICK_GOLD_RESOURCE)
        System.err.println("  pl's gold pick count = " + pl.getNeedToPickGoldHexResources());
    if (rconn == null) {
        System.err.println("L9120: internal error: can't find connection for bot " + rname);
        // shouldn't happen
        return;
    }
    // if it's the built-in type, print brain variables
    SOCClientData scd = (SOCClientData) rconn.getAppData();
    if (scd.isBuiltInRobot) {
        SOCRobotClient rcli = SOCLocalRobotClient.robotClients.get(rname);
        if (rcli != null)
            rcli.debugPrintBrainStatus(ga.getName(), false);
        else
            System.err.println("L9397: internal error: can't find robotClient for " + rname);
    } else {
        System.err.println("  Can't print brain status; robot type is " + scd.robot3rdPartyBrainClass);
    }
    hand.endGameTurnOrForce(ga, plNum, rname, rconn, false);
}
Also used : SOCRobotClient(soc.robot.SOCRobotClient) Connection(soc.server.genericServer.Connection)

Example 2 with SOCRobotClient

use of soc.robot.SOCRobotClient in project JSettlers2 by jdmonin.

the class SOCLocalRobotClient method createAndStartRobotClientThread.

/**
 * Create and start a robot client within a {@link SOCLocalRobotClient} thread.
 * After creating it, {@link Thread#yield() yield} the current thread and then sleep
 * 75 milliseconds, to give the robot time to start itself up.
 * The SOCPlayerLocalRobotRunner's run() will add the {@link SOCRobotClient} to {@link #robotClients}.
 * @param rname  Name of robot
 * @param strSocketName  Server's stringport socket name, or null
 * @param port    Server's tcp port, if <tt>strSocketName</tt> is null
 * @param cookie  Cookie for robot connections to server
 * @since 1.1.09
 * @see SOCServer#setupLocalRobots(int, int)
 * @throws ClassNotFoundException  if a robot class, or SOCDisplaylessClient,
 *           can't be loaded. This can happen due to packaging of the server-only JAR.
 * @throws LinkageError  for same reason as ClassNotFoundException
 */
public static void createAndStartRobotClientThread(final String rname, final String strSocketName, final int port, final String cookie) throws ClassNotFoundException, LinkageError {
    SOCRobotClient rcli;
    if (strSocketName != null)
        rcli = new SOCRobotClient(strSocketName, rname, "pw", cookie);
    else
        rcli = new SOCRobotClient("localhost", port, rname, "pw", cookie);
    Thread rth = new Thread(new SOCLocalRobotClient(rcli));
    rth.setDaemon(true);
    // run() will add to robotClients
    rth.start();
    Thread.yield();
    try {
        // Let that robot go for a bit.
        Thread.sleep(75);
    // robot runner thread will call its init()
    } catch (InterruptedException ie) {
    }
}
Also used : SOCRobotClient(soc.robot.SOCRobotClient)

Aggregations

SOCRobotClient (soc.robot.SOCRobotClient)2 Connection (soc.server.genericServer.Connection)1