Example #1
0
 private void drawString(Graphics g, String text, int x, int y, int c) {
   char[] ch = text.toCharArray();
   for (int i = 0; i < ch.length; i++) {
     g.drawImage(Art.font[ch[i] - 32][c], x + i * 8, y, null);
   }
 }
Example #2
0
  public EvaluationInfo run1(int currentTrial, int totalNumberOfTrials) {
    running = true;
    adjustFPS();
    EvaluationInfo evaluationInfo = new EvaluationInfo();

    VolatileImage image = null;
    Graphics g = null;
    Graphics og = null;

    image = createVolatileImage(320, 240);
    g = getGraphics();
    og = image.getGraphics();

    if (!GlobalOptions.VisualizationOn) {
      String msgClick = "Vizualization is not available";
      drawString(og, msgClick, 160 - msgClick.length() * 4, 110, 1);
      drawString(og, msgClick, 160 - msgClick.length() * 4, 110, 7);
    }

    addFocusListener(this);

    // Remember the starting time
    long tm = System.currentTimeMillis();
    long tick = tm;
    int marioStatus = Mario.STATUS_RUNNING;

    // mario = ((LevelScene) levelScene).mario;
    int totalActionsPerfomed = 0;
    // TODO: Manage better place for this:
    levelScene.mario.resetCoins();

    while (
    /*Thread.currentThread() == animator*/ running) {
      // Display the next frame of animation.
      //                repaint();

      MDPTraining.train(this);

      levelScene.tick();
      if (gameViewer != null && gameViewer.getContinuousUpdatesState()) gameViewer.tick();

      float alpha = 0;

      //            og.setColor(Color.RED);
      if (GlobalOptions.VisualizationOn) {
        og.fillRect(0, 0, 320, 240);
        levelScene.render(og, alpha);
      }

      if (agent instanceof ServerAgent && !((ServerAgent) agent).isAvailable()) {
        System.err.println("Agent became unavailable. Simulation Stopped");
        running = false;
        break;
      }

      boolean[] action = agent.getAction(this /*DummyEnvironment*/);
      if (action != null) {
        for (int i = 0; i < Environment.numberOfButtons; ++i)
          if (action[i]) {
            ++totalActionsPerfomed;
            break;
          }
      } else {
        System.err.println("Null Action received. Skipping simulation...");
        stop();
      }

      // Apply action;
      //            levelScene.keys = action;
      ((LevelScene) levelScene).mario.keys = action;
      ((LevelScene) levelScene).mario.cheatKeys = cheatAgent.getAction(null);

      if (GlobalOptions.VisualizationOn) {

        String msg = "Agent: " + agent.getName();
        LevelScene.drawStringDropShadow(og, msg, 0, 7, 5);

        msg = "Selected Actions: ";
        LevelScene.drawStringDropShadow(og, msg, 0, 8, 6);

        msg = "";
        if (action != null) {
          for (int i = 0; i < Environment.numberOfButtons; ++i)
            msg += (action[i]) ? levelScene.keysStr[i] : "      ";
        } else msg = "NULL";
        drawString(og, msg, 6, 78, 1);

        if (!this.hasFocus() && tick / 4 % 2 == 0) {
          String msgClick = "CLICK TO PLAY";
          //                    og.setColor(Color.YELLOW);
          //                    og.drawString(msgClick, 320 + 1, 20 + 1);
          drawString(og, msgClick, 160 - msgClick.length() * 4, 110, 1);
          drawString(og, msgClick, 160 - msgClick.length() * 4, 110, 7);
        }
        og.setColor(Color.DARK_GRAY);
        LevelScene.drawStringDropShadow(og, "FPS: ", 33, 2, 7);
        LevelScene.drawStringDropShadow(
            og, ((GlobalOptions.FPS > 99) ? "\\infty" : GlobalOptions.FPS.toString()), 33, 3, 7);

        msg =
            totalNumberOfTrials == -2
                ? ""
                : currentTrial
                    + "("
                    + ((totalNumberOfTrials == -1) ? "\\infty" : totalNumberOfTrials)
                    + ")";

        LevelScene.drawStringDropShadow(og, "Trial:", 33, 4, 7);
        LevelScene.drawStringDropShadow(og, msg, 33, 5, 7);

        if (width != 320 || height != 240) {
          g.drawImage(image, 0, 0, 640 * 2, 480 * 2, null);
        } else {
          g.drawImage(image, 0, 0, null);
        }
      } else {
        // Win or Die without renderer!! independently.
        marioStatus = ((LevelScene) levelScene).mario.getStatus();
        if (marioStatus != Mario.STATUS_RUNNING) stop();
      }
      // Delay depending on how far we are behind.
      if (delay > 0)
        try {
          tm += delay;
          Thread.sleep(Math.max(0, tm - System.currentTimeMillis()));
        } catch (InterruptedException e) {
          break;
        }
      // Advance the frame
      frame++;
    }
    // =========
    evaluationInfo.agentType = agent.getClass().getSimpleName();
    evaluationInfo.agentName = agent.getName();
    evaluationInfo.marioStatus = levelScene.mario.getStatus();
    evaluationInfo.livesLeft = levelScene.mario.lives;
    evaluationInfo.lengthOfLevelPassedPhys = levelScene.mario.x;
    evaluationInfo.lengthOfLevelPassedCells = levelScene.mario.mapX;
    evaluationInfo.totalLengthOfLevelCells = levelScene.level.getWidthCells();
    evaluationInfo.totalLengthOfLevelPhys = levelScene.level.getWidthPhys();
    evaluationInfo.timeSpentOnLevel = levelScene.getStartTime();
    evaluationInfo.timeLeft = levelScene.getTimeLeft();
    evaluationInfo.totalTimeGiven = levelScene.getTotalTime();
    evaluationInfo.numberOfGainedCoins = levelScene.mario.coins;
    //        evaluationInfo.totalNumberOfCoins   = -1 ; // TODO: total Number of coins.
    evaluationInfo.totalActionsPerfomed =
        totalActionsPerfomed; // Counted during the play/simulation process
    evaluationInfo.totalFramesPerfomed = frame;
    evaluationInfo.marioMode = levelScene.mario.getMode();
    evaluationInfo.killsTotal = levelScene.mario.world.killedCreaturesTotal;
    //        evaluationInfo.Memo = "Number of attempt: " + Mario.numberOfAttempts;
    if (agent instanceof ServerAgent
        && levelScene.mario.keys
            != null /*this will happen if client quits unexpectedly in case of Server mode*/)
      ((ServerAgent) agent).integrateEvaluationInfo(evaluationInfo);
    return evaluationInfo;
  }