Ejemplo n.º 1
0
 @Override
 public void setActiveStrategy(ScriptStrategy strategy, AIHelper helper) {
   synchronized (activeStrategyMutex) {
     if (activeStrategy != null) {
       activeStrategy.setActive(false, helper);
     }
     activeStrategy = strategy == null ? null : strategy.getStrategy();
     if (activeStrategy != null) {
       activeStrategy.setActive(true, helper);
     }
     activeStrategyMutex.notifyAll();
   }
 }
Ejemplo n.º 2
0
    /**
     * Runs if we need to wait for a game tick.
     *
     * @param helper
     * @return
     */
    public TickResult runForTick(AIHelper helper) {
      synchronized (tickHelperMutex) {
        printError();

        synchronized (activeStrategyMutex) {
          if (activeStrategy != null) {
            TickResult tickResult = activeStrategy.gameTick(helper);
            if (tickResult == TickResult.NO_MORE_WORK || tickResult == TickResult.ABORT) {
              activeStrategy.setActive(false, helper);
              activeStrategy = null;
              activeStrategyMutex.notifyAll();
            } else {
              return tickResult;
            }
          }
        }
        if (scriptWaitingForTick) {
          tickHelper = helper;
          scriptInsideTick = true;
          scriptWaitingForTick = false;
          tickHelperMutex.notifyAll();
          while (scriptInsideTick) {
            try {
              tickHelperMutex.wait();
            } catch (InterruptedException e) {
            }
          }
          tickHelper = null;
        }
        return finished ? TickResult.NO_MORE_WORK : TickResult.TICK_HANDLED;
      }
    }