public void run() { final String METHOD_NAME = "run()"; getOwner().methodBegin(getClass(), METHOD_NAME); try { // todo There's probably a better way to handle this than a loop that only exits on an error. //noinspection InfiniteLoopStatement while (!getDone().get()) { if (!getEventsToProcess().isEmpty()) { processGameEvents(); ecmInfo = ComputeECM.computeAllEntitiesECMInfo( getGame().getEntitiesVector()); } else if (!getDirtyUnits().isEmpty()) { Entity entity = getGame().getEntity(getDirtyUnits().pollFirst()); if ((entity != null) && isEntityOnMap(entity)) { unPause(); getOwner().log(getClass(), METHOD_NAME, "recalculating paths for " + entity.getDisplayName()); getPathEnumerator().recalculateMovesFor(entity); getOwner().log(getClass(), METHOD_NAME, "finished recalculating paths for " + entity .getDisplayName()); } } else if (getWaitWhenDone().get()) { waitForUnpause(); // paused for a reason } else { waitForUnpause(); // idling because there's nothing to do } } } finally { getOwner().methodEnd(getClass(), METHOD_NAME); } }
private void pause() { final String METHOD_NAME = "pause()"; getOwner().methodBegin(getClass(), METHOD_NAME); try { getWaitWhenDone().set(true); while (!getWaiting().get() && !getDone().get()) { try { Thread.sleep(100); } catch (InterruptedException ignored) { } } } finally { getOwner().methodEnd(getClass(), METHOD_NAME); } }
synchronized void unPause() { final String METHOD_NAME = "unpause()"; getOwner().methodBegin(getClass(), METHOD_NAME); try { getWaitWhenDone().set(false); notifyAll(); } finally { getOwner().methodEnd(getClass(), METHOD_NAME); } }
/** * Waits until the thread is not paused, and there's indication that it has * something to do */ private synchronized void waitForUnpause() { final String METHOD_NAME = "wait_for_unpause()"; getOwner().methodBegin(getClass(), METHOD_NAME); try { while (!getDone().get() && (getWaitWhenDone().get() || (getEventsToProcess().isEmpty() && getDirtyUnits().isEmpty()))) { getOwner().log(getClass(), METHOD_NAME, "waitWhenDone = " + getWaitWhenDone() + " :: eventsToProcess = " + getEventsToProcess().size() + " :: dirtyUnits = " + getDirtyUnits().size()); getWaiting().set(true); try { wait(); } catch (InterruptedException ignored) { } // System.err.println("checking WAIT conditions"); } getWaiting().set(false); } finally { getOwner().methodEnd(getClass(), METHOD_NAME); } }
void handlePacket(Packet c) { if (c == null) { getOwner().log(getClass(), "handlePacket(Packet)", LogLevel.WARNING, switch (c.getCommand()) { case Packet.COMMAND_PLAYER_UPDATE: receivePlayerInfo(c); break; case Packet.COMMAND_PLAYER_READY: getPlayer(c.getIntValue(0)).setDone(c.getBooleanValue(1)); break; case Packet.COMMAND_PLAYER_ADD: receivePlayerInfo(c); break; case Packet.COMMAND_PLAYER_REMOVE: getGame().removePlayer(c.getIntValue(0)); break; case Packet.COMMAND_CHAT: getGame().processGameEvent(new GamePlayerChatEvent(this, null, (String) c.getObject(0))); break; case Packet.COMMAND_ENTITY_ADD: receiveEntityAdd(c); break; case Packet.COMMAND_ENTITY_UPDATE: receiveEntityUpdate(c); break;
getOwner().methodBegin(getClass(), METHOD_NAME); pause(); for (Entity entity : getGame().getEntitiesVector()) { if (getDone().get()) { return; if (!isEntityOnMap(entity)) { continue; if (((!getPathEnumerator().getLastKnownLocations().containsKey(entity.getId())) || (!getPathEnumerator().getLastKnownLocations().get(entity.getId()) .equals(CoordFacingCombo.createCoordFacingCombo(entity))))) { dirtifyUnit(entity.getId()); while (!getDirtyUnits().isEmpty()) { if (getDone().get()) { return; Integer entityId = getDirtyUnits().pollFirst(); Entity entity = getGame().getEntity(entityId); if (entity != null) { getOwner().log(getClass(), METHOD_NAME, "recalculating paths for " + entity.getDisplayName()); getPathEnumerator().recalculateMovesFor(entity); getOwner().log(getClass(), METHOD_NAME, "finished recalculating paths for " + entity .getDisplayName());
getOwner().methodBegin(getClass(), METHOD_NAME); LinkedList<GameEvent> eventsToProcessIterator = new LinkedList<>(getEventsToProcess()); int numEvents = eventsToProcessIterator.size(); for (int count = 0; count < numEvents; count++) { getOwner().log(getClass(), METHOD_NAME, "Processing event " + (count + 1) + " out of " + numEvents); GameEvent event = eventsToProcessIterator.get(count); if (event == null) { continue; getOwner().log(getClass(), METHOD_NAME, "Processing " + event.toString()); getEventsToProcess().remove(event); if (event instanceof GameEntityChangeEvent) { if (getGame().getPhase() != IGame.Phase.PHASE_MOVEMENT) { continue; Entity entity = getGame().getEntity(changeEvent.getEntity().getId()); if (entity == null) { continue; // not sure how this can happen, but just to be if (getGame().getPhase() == IGame.Phase.PHASE_FIRING) { continue; if (position.equals(getPathEnumerator().getLastKnownCoords(entity.getId()))) { continue; // no sense in updating a unit if it hasn't moved getOwner().log(getClass(), METHOD_NAME, "Received entity change event for "
getOwner().methodBegin(getClass(), METHOD_NAME); if (getGame().getEntity(id) == null) { getPathEnumerator().getLastKnownLocations().remove(id); getPathEnumerator().getUnitMovableAreas().remove(id); getPathEnumerator().getUnitPaths().remove(id); getPathEnumerator().getUnitPotentialLocations().remove(id); return; if (!getGame().getEntity(id).isAero()) { TreeSet<Integer> toDirty = new TreeSet<>( getPathEnumerator().getEntitiesWithLocation( getGame().getEntity(id).getPosition(), true)); if (getPathEnumerator().getLastKnownLocations() .containsKey(id)) { if ((getGame().getEntity(id) != null) && getGame().getEntity(id).isSelectableThisTurn()) { toDirty.addAll(getPathEnumerator() .getEntitiesWithLocation(getPathEnumerator() .getLastKnownLocations().get(id) .getCoords(), true)); if ((getGame().getEntity(index) == null) || (!getGame() .getEntity(index).isSelectableThisTurn()) && (getGame() .getPhase() == IGame.Phase.PHASE_MOVEMENT)) { toRemove.add(index); if (getGame().getEntity(id) != null) {
/** * Returns the individual player assigned the index parameter. */ protected IPlayer getPlayer(int idx) { return getGame().getPlayer(idx); }
entity.getDisplayName() + " (ID " + entity.getId() + ")"); getPrecognition().ensureUpToDate(); getPrecognition().getPathEnumerator() .getUnitPaths() .get(entity.getId()); precognition.unPause(); methodEnd(getClass(), METHOD_NAME);
@SuppressWarnings("unchecked") private void receiveUpdateMinefields(Packet packet) { // only update information if you know about the minefield Vector<Minefield> newMines = new Vector<>(); for (Minefield mf : (Vector<Minefield>) packet.getObject(0)) { if (getOwner().getLocalPlayer().containsMinefield(mf)) { newMines.add(mf); } } if (newMines.size() > 0) { getGame().resetMinefieldDensity(newMines); } }
public Precognition(Princess owner) { this.owner = owner; this.game = new Game(); getGame().addGameListener(new GameListenerAdapter() { @Override public void gameEntityChange(GameEntityChangeEvent changeEvent) { getEventsToProcess().add(changeEvent); wakeUp(); } @Override public void gamePhaseChange(GamePhaseChangeEvent changeEvent) { getEventsToProcess().add(changeEvent); wakeUp(); } }); setPathEnumerator(new PathEnumerator(owner, getGame())); // Initialize ECM Info, especially important if Princess added mid-game ecmInfo = ComputeECM.computeAllEntitiesECMInfo( getGame().getEntitiesVector()); }
/** * Receives player information from the message packet. */ private void receivePlayerInfo(Packet c) { int pindex = c.getIntValue(0); IPlayer newPlayer = (IPlayer) c.getObject(1); if (getPlayer(newPlayer.getId()) == null) { getGame().addPlayer(pindex, newPlayer); } else { getGame().setPlayer(pindex, newPlayer); } PreferenceManager.getClientPreferences().setLastPlayerColor( newPlayer.getColorIndex()); PreferenceManager.getClientPreferences().setLastPlayerCategory( newPlayer.getCamoCategory()); PreferenceManager.getClientPreferences().setLastPlayerCamoName( newPlayer.getCamoFileName()); }
/** * Initialize the possible path rankers. * Has a dependency on the fire controls being initialized. */ public void initializePathRankers() { initializeFireControls(); pathRankers = new HashMap<>(); BasicPathRanker basicPathRanker = new BasicPathRanker(this); basicPathRanker.setFireControl(fireControls.get(FireControlType.Basic)); basicPathRanker.setPathEnumerator(precognition.getPathEnumerator()); pathRankers.put(PathRankerType.Basic, basicPathRanker); InfantryPathRanker infantryPathRanker = new InfantryPathRanker(this); infantryPathRanker.setFireControl(fireControls.get(FireControlType.Infantry)); infantryPathRanker.setPathEnumerator(precognition.getPathEnumerator()); pathRankers.put(PathRankerType.Infantry, infantryPathRanker); NewtonianAerospacePathRanker newtonianAerospacePathRanker = new NewtonianAerospacePathRanker(this); newtonianAerospacePathRanker.setFireControl(fireControls.get(FireControlType.Basic)); newtonianAerospacePathRanker.setPathEnumerator(precognition.getPathEnumerator()); pathRankers.put(PathRankerType.NewtonianAerospace, newtonianAerospacePathRanker); }
private ToHitData calcRealToHit(final WeaponAttackAction weaponAttackAction) { return weaponAttackAction.toHit(getGame(), owner.getPrecognition().getECMInfo()); }
void signalDone() { getDone().set(true); }
/** * Constructor - initializes a new instance of the Princess bot. * @param name The display name. * @param host The host address to which to connect. * @param port The port on the host where to connect. * @param verbosity The verbosity of the bot's reporting and logging. */ public Princess(final String name, final String host, final int port, final LogLevel verbosity) { super(name, host, port); getLogger().setLogLevel(LOGGING_CATEGORY, verbosity); setBehaviorSettings(BehaviorSettingsFactory.getInstance(getLogger()) .DEFAULT_BEHAVIOR); fireControlState = new FireControlState(); pathRankerState = new PathRankerState(); // Start-up precog now, so that it can instantiate its game instance, // and it will stay up-to date. precognition = new Precognition(this); precogThread = new Thread(precognition, "Princess-precognition (" + getName() + ")"); precogThread.start(); }
private void receiveEntityAdd(Packet packet) { @SuppressWarnings("unchecked") List<Integer> entityIds = (List<Integer>) packet.getObject(0); @SuppressWarnings("unchecked") List<Entity> entities = (List<Entity>) packet.getObject(1); assert (entityIds.size() == entities.size()); for (int i = 0; i < entityIds.size(); i++) { assert (entityIds.get(i) == entities.get(i).getId()); } getGame().addEntities(entities); }
/** * receive and process an entity nova network mode change packet * * @param c The packet containing the change. */ private void receiveEntityNovaNetworkModeChange(Packet c) { final String METHOD_NAME = "receiveEntityNovaNetworkModeChange(Packet)"; try { int entityId = c.getIntValue(0); String networkID = c.getObject(1).toString(); Entity e = getGame().getEntity(entityId); if (e != null) { e.setNewRoundNovaNetworkString(networkID); } } catch (Exception ex) { getOwner().log(getClass(), METHOD_NAME, ex); } }