Skip to content

Commit

Permalink
visualisation off screen done
Browse files Browse the repository at this point in the history
  • Loading branch information
ljialin committed Jul 18, 2017
1 parent 23e31ed commit ce5afd0
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 59 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@ production/
*.png
*.gif
*.pyc

clients/GVGAI-JavaClient/src/sampleRandom.zip
2 changes: 1 addition & 1 deletion clients/GVGAI-JavaClient/src/TestLearningClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static void main(String[] args)
{
assert (CompetitionParameters.USE_SOCKETS);
/** Init params */
int gameId = 0;
int gameId = 2;
String shDir = "./src/utils";
String serverDir;
if (CompetitionParameters.OS_WIN) {
Expand Down
4 changes: 2 additions & 2 deletions clients/GVGAI-JavaClient/src/oneclickRunFromJavaClient.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ mkdir -p ${build_folder}
find ${src_folder} -name "*.java" > sources.txt
javac -d ${build_folder} @sources.txt
# run with screen visualisation on
java -classpath ${build_folder} TestLearningClient -shDir ${shDir} -serverDir ${serverDir} -visuals
#java -classpath ${build_folder} TestLearningClient -shDir ${shDir} -serverDir ${serverDir} -visuals
# run without screen visualisation off
#java -classpath ${build_folder} TestLearningClient -shDir ${shDir} -serverDir ${serverDir}
java -classpath ${build_folder} TestLearningClient -shDir ${shDir} -serverDir ${serverDir}
8 changes: 4 additions & 4 deletions clients/GVGAI-JavaClient/src/utils/ClientComm.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package utils;

import serialization.Types;
import serialization.SerializableStateObservation;
import serialization.Types.LEARNING_SSO_TYPE;
import utils.com.google.gson.Gson;
import serialization.SerializableStateObservation;

import java.io.*;
import java.io.IOException;
import java.lang.reflect.Constructor;

/**
Expand Down Expand Up @@ -180,7 +179,8 @@ public void processLine(String msg) throws IOException{
this.sso = gson.fromJson(json, SerializableStateObservation.class);

// If expect image
if ((sso.phase != SerializableStateObservation.Phase.INIT) &&
if ((sso.phase != SerializableStateObservation.Phase.INIT
&& sso.phase != SerializableStateObservation.Phase.ABORT) &&
(lastSsoType == LEARNING_SSO_TYPE.IMAGE || lastSsoType == LEARNING_SSO_TYPE.BOTH)) {
// If an image has been received, then save its PNG equivalent
sso.convertBytesToPng(sso.imageArray);
Expand Down
4 changes: 2 additions & 2 deletions clients/GVGAI-PythonClient/src/oneclickRunFromPythonClient.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ if [ ! -d "$DIRECTORY" ]; then
fi

# Run the client with visualisation on
python TestLearningClient.py -gameId ${game_id} -agentName ${agent_name} -serverDir ${server_dir} -shDir ${sh_dir} -visuals
#python TestLearningClient.py -gameId ${game_id} -agentName ${agent_name} -serverDir ${server_dir} -shDir ${sh_dir} -visuals
# Run the client with visualisation off
# python TestLearningClient.py -serverDir ${server_dir} -gamesDir ${server_dir} -gameId ${game_id}
python TestLearningClient.py -gameId ${game_id} -agentName ${agent_name} -serverDir ${server_dir} -shDir ${sh_dir}
9 changes: 5 additions & 4 deletions clients/GVGAI-PythonClient/src/utils/ClientComm.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,10 +201,11 @@ def processLine(self, msg):
self.parse_json(js)
# self.sso = json.loads(js, object_hook=self.as_sso)

if self.lastSsoType == LEARNING_SSO_TYPE.IMAGE or self.lastSsoType == "IMAGE" \
or self.lastSsoType == LEARNING_SSO_TYPE.BOTH or self.lastSsoType == "BOTH":
if self.sso.imageArray:
self.sso.convertBytesToPng(self.sso.imageArray)
if self.sso.phase == "ACT":
if self.lastSsoType == LEARNING_SSO_TYPE.IMAGE or self.lastSsoType == "IMAGE" \
or self.lastSsoType == LEARNING_SSO_TYPE.BOTH or self.lastSsoType == "BOTH":
if self.sso.imageArray:
self.sso.convertBytesToPng(self.sso.imageArray)

except Exception as e:
logging.exception(e)
Expand Down
89 changes: 73 additions & 16 deletions src/core/game/Game.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,5 @@
package core.game;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.util.*;

import javax.swing.JOptionPane;

import core.player.LearningPlayer;
import core.vgdl.SpriteGroup;
import core.vgdl.VGDLFactory;
import core.vgdl.VGDLRegistry;
import core.vgdl.VGDLSprite;
import core.vgdl.VGDLViewer;
import core.logging.Logger;
import core.logging.Message;
import core.competition.CompetitionParameters;
import core.content.Content;
import core.content.GameContent;
Expand All @@ -23,8 +8,11 @@
import core.game.GameDescription.InteractionData;
import core.game.GameDescription.SpriteData;
import core.game.GameDescription.TerminationData;
import core.logging.Logger;
import core.logging.Message;
import core.player.Player;
import core.termination.Termination;
import core.vgdl.*;
import ontology.Types;
import ontology.avatar.MovingAvatar;
import ontology.effects.Effect;
Expand All @@ -34,7 +22,9 @@
import tools.pathfinder.Node;
import tools.pathfinder.PathFinder;

import static tools.Utils.findMaxDivisor;
import javax.swing.*;
import java.awt.*;
import java.util.*;

/**
* Created with IntelliJ IDEA. User: Diego Date: 17/10/13 Time: 13:42 This is a
Expand Down Expand Up @@ -976,6 +966,73 @@ public double[] playGame(Player[] players, int randomSeed, boolean isHuman, int
return handleResult();
}

public double[] playOnlineGame(Player[] players, int randomSeed, boolean isHuman, int humanID) {
// Prepare some structures and references for this game.
prepareGame(players, randomSeed, humanID);

// Create and initialize the panel for the graphics.
VGDLViewer view = new VGDLViewer(this, players[humanID]);
view.justImage = true;
// JEasyFrame frame;
// frame = new JEasyFrame(view, "Java-VGDL");
// frame.setSize(0,0);

// frame.addKeyListener(ki);
// frame.addWindowListener(wi);
// frame.pack();
wi.windowClosed = false;

// Determine the delay for playing with a good fps.
double delay = CompetitionParameters.LONG_DELAY;
for (Player player : players)
if (player instanceof tracks.singlePlayer.tools.human.Agent) {
delay = 1000.0 / CompetitionParameters.DELAY; // in milliseconds
break;
}

boolean firstRun = true;

// Play until the game is ended
while (!isEnded && !wi.windowClosed) {
// Determine the time to adjust framerate.
long then = System.currentTimeMillis();

this.gameCycle(); // Execute a game cycle.

// Get the remaining time to keep fps.
long now = System.currentTimeMillis();
int remaining = (int) Math.max(0, delay - (now - then));

// Wait until de next cycle.
waitStep(remaining);

// Draw all sprites in the panel.
view.paint(this.spriteGroups);

// Update the frame title to reflect current score and tick.
// this.setTitle(frame);

if (firstRun && isHuman) {
/*if (CompetitionParameters.dialogBoxOnStartAndEnd) {
JOptionPane.showMessageDialog(frame, "Click OK to start.");
}*/

firstRun = false;
}
}


// Update the forward model for the game state sent to the controller.
fwdModel.update(this);

// performs the same event as the exit button were clicked
// closes the frame after game is over
// frame.dispose();
// frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));

return handleResult();
}

/**
* Sets the title of the game screen, depending on the game ending state.
*
Expand Down
37 changes: 12 additions & 25 deletions src/core/vgdl/VGDLViewer.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
package core.vgdl;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import javax.imageio.ImageIO;
import javax.swing.JComponent;

import core.competition.CompetitionParameters;
import core.game.Game;
import core.player.LearningPlayer;
import core.player.Player;
import ontology.Types;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

/**
* Created with IntelliJ IDEA.
* User: Diego
Expand Down Expand Up @@ -47,6 +43,7 @@ public class VGDLViewer extends JComponent
*/
public Player player;

public boolean justImage = true;

/**
* Creates the viewer for the game.
Expand Down Expand Up @@ -85,12 +82,6 @@ public void paintWithGraphics(Graphics2D g) {
g.setColor(Types.BLACK);
g.fillRect(0, size.height, size.width, size.height);

//Possible efficiency improvement: static image with immovable objects.
/*
BufferedImage mapImage = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_RGB);
Graphics2D gImage = mapImage.createGraphics();
*/

try {
int[] gameSpriteOrder = game.getSpriteOrder();
if (this.spriteGroups != null) for (Integer spriteTypeInt : gameSpriteOrder) {
Expand Down Expand Up @@ -144,21 +135,17 @@ public Dimension getPreferredSize() {

public void saveImage(String fileName) {
try {
// TYPE_INT_ARGB specifies the image format: 8-bit RGBA packed
// into integer pixels
// long t = System.currentTimeMillis();
BufferedImage bi = new BufferedImage( (int) size.getWidth(), (int) size.getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D graphics = bi.createGraphics();
paintWithGraphics(graphics);
File file = new File(fileName);
if (justImage) {
graphics.dispose();
}
ImageIO.write(bi, "png", file);
// System.out.println("Wrote image to: " + fileName);
// long t2 = System.currentTimeMillis();
// System.out.println("Time elapsed = " + (t2 - t) + "ms");
} catch (IOException ie) {
ie.printStackTrace();
}

}

}
8 changes: 3 additions & 5 deletions src/tracks/LearningMachine.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,9 @@
import tools.ElapsedCpuTimer;
import tools.StatSummary;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Random;

import static tools.Utils.findMaxDivisor;

/**
* Created by Daniel on 07.03.2017.
*/
Expand Down Expand Up @@ -291,7 +287,9 @@ public static int playOneLevel(String game_file, String level_file, int level_ti
if(isVisual) {
score = toPlay.playGame(players, randomSeed, false, 0);
} else {
score = toPlay.runGame(players, randomSeed);

score = toPlay.playOnlineGame(players, randomSeed, false, 0);
// score = toPlay.runGame(players, randomSeed);
}
toPlay.printResult();

Expand Down

0 comments on commit ce5afd0

Please sign in to comment.