예제 #1
0
파일: SugarAI.java 프로젝트: pwahs/wota
  public void first_cry() {
    if (time == 1) { // was just born, figure out time and directions:

      // find time:
      if (audibleHillMessage != null) {
        if (audibleHillMessage.content < 0) {
          initialTime = audibleHillMessage.content - HillAI.initTime;
        }
      }
      if (initialTime < 5) {
        // in the beginning, figure out direction:
        int count = 0;
        int total = 0;
        for (Ant a : visibleFriends()) {
          if (a.caste == Caste.Gatherer) {
            total++;
            if (a.id < self.id) count++;
          }
        }

        dir = (360 * count) / (total + 1);
        moveInDirection(dir);
      } else { // later, just go some random direction:
        dir = SeededRandomizer.nextDouble() * 360;
      }

      // initialize hills:
      for (int i = 0; i < HillAI.NR_HILLS; ++i) {
        hills.add(new LinkedList<Snapshot>());
        indices[i] = 0;
      }

      // add my hill:
      for (Hill h : visibleHills) {
        if (h.playerID == self.playerID) {
          hills.get(HillAI.HILL_IND).add(visibleHills.get(0));
        }
      }

      nr_hill = 0;
    }
  }
예제 #2
0
  /**
   * Start the simulation and keep the view up to date. 1. Update the Graphics at the rate
   * framesPerSecond 2. Update the Simulation at the rate ticksPerSecond do nothing in the remaining
   * time or if times get in conflict only update the graphics.
   *
   * <p>keyboard/mouse input should be fetched before graphics in every loop.
   */
  public void runSimulation() {
    GameWorld gameWorld;

    while (!abortRequested) {
      gameWorld = gameWorldFactory.nextGameWorld();
      if (gameWorld == null) {
        break;
      }

      SeededRandomizer.resetSeed(gameWorld.seed);
      System.out.println("seed next game: " + gameWorld.seed);

      StatisticsLogger logger = new StatisticsLogger(gameWorld.getPlayers());
      gameWorld.setLogger(logger);

      statisticsView = new StatisticsView(gameWorld, logger);
      statisticsView.run();

      if (isGraphical) {
        try {
          Display.setTitle("Wota");
          Display.setDisplayMode(new DisplayMode(width, height));
          Display.create(new PixelFormat(8, 0, 0, 0));
        } catch (LWJGLException e) {
          e.printStackTrace();
          System.exit(0);
        }

        createKeyboard();
        gameView = new GameView(gameWorld, width, height);
        gameView.setup();
      }

      running = true;
      startTime = System.nanoTime();

      resetReferenceValues();
      long lastMeasurementTime = System.nanoTime(); // time for TPS / FPS measurements
      int measureFrameCount = 0; // Frame counter to determine TPS
      int measureTickCount = 0; // Tick counter to determine FPS

      // events for graphics update and tick are created uniformly. Call them with priority on
      // graphics
      while (running && !abortRequested) {
        if (isGraphical) {
          handleKeyboardInputs();
        }

        // only check if is not graphical since we want the
        // maximal number of updates possible
        if (ticksToDo() > SKIP_TICKS_THRESHOLD && isGraphical) {
          resetReferenceValues();
        }

        // now update simulation if tick event
        if ((!isGraphical || ticksToDo() > 0) && !paused) {
          tick(gameWorld);
          measureTickCount++;
          referenceTickCount++;
        }

        // Update Graphics if event for graphic update is swept.
        if (framesToDo() > 0) {
          statisticsView.refresh();
          frameCount++;
          measureFrameCount++;
          referenceFrameCount++;

          if (isGraphical) { // calc graphics
            if (!paused) {
              gameView.render();
            }
            // must be called in any case to catch keyboard input
            abortRequested = Display.isCloseRequested();
            Display.update();
          }
        }

        // measurements of FPS / TPS
        long timeDiff = System.nanoTime() - lastMeasurementTime;
        if (timeDiff > 1.e9 * MEASUREMENT_INTERVAL) {
          measuredFramesPerSecond = measureFrameCount * 1.e9 / timeDiff;
          measuredTicksPerSecond = measureTickCount * 1.e9 / timeDiff;

          measureFrameCount = 0;
          measureTickCount = 0;

          lastMeasurementTime = System.nanoTime();

          System.out.format("Frames per second: %.1f\n", measuredFramesPerSecond);
          System.out.format("Ticks per second: %.1f\n", measuredTicksPerSecond);
        }
      }

      // game done. Add to stats.
      List<Player> winner = gameWorld.getWinner();
      List<Player> loosers = new ArrayList<Player>(gameWorld.getPlayers());
      loosers.removeAll(winner);

      if (winner.size() == 0) {
        System.err.println("winner should not be empty.");
      } else if (winner.size() == 1) {
        resultCollection.addGame(getNames(winner), null, getNames(loosers));
      } else {
        // multiple winners count as draw. But we could still have loosers.
        resultCollection.addGame(null, getNames(winner), getNames(loosers));
      }

      System.out.println("seed last game: " + gameWorld.seed);

      statisticsView.frame.dispose();
      if (isGraphical) {
        Display.destroy();
      }
    } // last gameWorld done
    System.out.println(resultCollection);
  }