private static ShipInformation getShipInformation(CurrentGameState cgs, String agentId) { for (ShipInformation shipInformation : cgs.getShips()) { if (shipInformation.getId().equals(agentId)) { return shipInformation; } } return null; }
@Override public void run() { boolean continueToRun = true; while (continueToRun) { if (this.agentId == null) { this.agentId = this.endpoint.getAgentId(this.authId); } else { this.lastGameState = this.endpoint.getLastGameState(this.authId); if (this.lastGameState != null) { if (this.opponentId.isEmpty() || getShipInformation(this.lastGameState, this.opponentId) == null) { for (ShipInformation info : this.lastGameState.getShips()) { if (!info.getId().equals(this.agentId)) { this.opponentId = info.getId(); } } } ShipInformation shipInformation = getShipInformation(this.lastGameState, this.agentId); ShipInformation opponentInformation = getShipInformation(this.lastGameState, this.opponentId); if (opponentInformation != null && shipInformation != null) { float targetHeading = (float) getTheta( opponentInformation.getCenterX() - shipInformation.getCenterX(), opponentInformation.getCenterY() - shipInformation.getCenterY()); adjustHeading(radiansToDegrees(targetHeading - shipInformation.getHeading())); } this.controllerState.setFiring( !this.controllerState.isTurningPort() && !this.controllerState.isTurningStarboard()); this.endpoint.setControllerState(this.authId, this.controllerState); if (this.lastGameState.getGameEvents() != null) { for (GameEvent gameEvent : this.lastGameState.getGameEvents()) { if (gameEvent != null && gameEvent.getEvent() == GameEventType.GAME_END) { continueToRun = false; } } } } } try { Thread.sleep(UPDATE_RATE); } catch (InterruptedException ex) { continueToRun = false; } } }