Commit 39efe8a3 authored by Thomas Dupriez's avatar Thomas Dupriez

Merge remote-tracking branch 'origin/master'

parents 8d39557c b2134fa4
......@@ -2,26 +2,64 @@ package artificial_intelligence;
import java.util.ArrayList;
import artificial_intelligence.AIEntities.AbstractEntity;
import artificial_intelligence.AIEntities.BasicEntity;
import artificial_intelligence.AIEntities.*;
import core.gamestate.GameContent;
import core.relayer.Relayer;
/**
* This class is used to stop all AI thread when the game finishes.
* @author Guerquin Arnaud
* @author Guerquin Arnaud && Ramusat Yann
*
* TODO replace this class by AI_Controler (more general). Thanks avoiding using this class.
* This class allows to create a new entity of indicated type.
* This class provides a way to start or stop all the current working entities (for example when initializing and quitting a map).
*
*/
public class AIControler {
private static ArrayList<AbstractEntity> list=new ArrayList<>();
public static void add(GameContent gameContent,Relayer dmr){
AbstractEntity ai=new BasicEntity(gameContent,dmr);
/**
* Create a new AI of indicated type given as context a gameContent and a relayer to contact.
* @param gameContent
* @param dmr
* @param type the behaviour of the AI to create
*/
public static void add(GameContent gameContent, Relayer dmr, EnumBehaviourType type) {
AbstractEntity ai = null;
switch(type) {
case Agressive:
ai = new AgressiveEntity(gameContent,dmr);
break;
case Basic:
ai = new BasicEntity(gameContent,dmr);
break;
case Defender:
ai = new DefenderEntity(gameContent,dmr);
break;
default:
System.out.println("Trying to instanciate unrecognized type of AI.");
break;
}
list.add(ai);
}
/**
* Return all the AIs active or created but not active.
* @return the list of AIs
*/
public static ArrayList<AbstractEntity> getList() {
return list;
}
/**
* In order to know how many AIs are created.
* @return the size of the list
*/
public static int getNumber() {
return list.size();
}
/**
* To use for example at the end of a map.
*/
public static void killAll(){
for(AbstractEntity ai :list){
ai.setEndThread(false);
......@@ -29,6 +67,9 @@ public class AIControler {
list.clear();
}
/**
* To start all the AIs at the begining of the map and after init complete inialization.
*/
public static void startAll() {
for(AbstractEntity ai :list){
ai.start();
......
package artificial_intelligence;
import artificial_intelligence.AIEntities.*;
import core.gamestate.GameContent;
import core.relayer.Relayer;
import java.util.ArrayList;
/**
* Created by Yann RAMUSAT on 30/12/15.
*
* This class allows to create a new entity of the indicated type.
* This class provides a way to start or stop all the current working entities (for example when initializing and quitting a map).
*
* Note: A part of this code (killAll and startAll) is inspired by the previous class AIControler implemented by Arnaud Guerquin.
*/
public class AI_Controler {
/*** SINGLETON PART ***/
private static AI_Controler ourInstance = new AI_Controler();
public static AI_Controler getInstance() {
return ourInstance;
}
private AI_Controler() {
}
/** CONTROLER PART ***/
private ArrayList<AbstractEntity> list=new ArrayList<>();
public void killAll(){
for(AbstractEntity ai :list){
ai.setEndThread(false);
}
list.clear();
}
public void startAll() {
for(AbstractEntity ai :list){
ai.start();
}
}
/*** FACTORY PART ***/
public void add(GameContent gameContent, Relayer dmr, EnumBehaviourType type) {
AbstractEntity ai = null;
switch(type) {
case Agressive:
ai = new AgressiveEntity(gameContent,dmr);
break;
case Basic:
ai = new BasicEntity(gameContent,dmr);
break;
case Defender:
ai = new DefenderEntity(gameContent,dmr);
break;
default:
System.out.println("Trying to instanciate unrecognized type of AI.");
break;
}
list.add(ai);
}
public int getNumber() {
return list.size();
}
}
......@@ -6,7 +6,7 @@ import artificial_intelligence.GameOpt;
import core.zone.Direction;
/**
* @author Remy Garnier && Yann Ramusat
* @author Remy GARNIER && Yann RAMUSAT
*
* This class provides essentials tools for pathfinding and other ubiquitous abilities.
*/
......@@ -31,7 +31,7 @@ public class AI {
}
/**
*
* Set a game opt.
* @param Game Options
*/
public void setOpt(GameOpt opt) {
......@@ -39,8 +39,16 @@ public class AI {
}
/**
* Implementation of the A* algorithm
*/
* Implementation of the A* algorithm.
*
* @param obj_x
* @param obj_y
* @param source_x
* @param source_y
* @param width
* @param height
* @return the direction to go
*/
public static Direction alpha_star(int obj_x, int obj_y, int source_x, int source_y,int width,int height) {
AITile obj = new AITile(obj_x, obj_y);
AITile begin = new AITile(source_x, source_y);
......
......@@ -3,12 +3,12 @@ package artificial_intelligence.AlphaStar;
import core.zone.Direction;
/**
* @author Remy Garnier && Yann Ramusat
* @author Remy GARNIER && Yann RAMUSAT
*
* This class stands for a tile like in MapGen but with the needs of AI.
*/
class AITile {
public class AITile {
private int x;
private int y;
......@@ -33,7 +33,10 @@ class AITile {
public boolean isObstacle(){return false;}
/**
* Return information about the closest tiles.
* @return
*/
public AITile[] neighbors(){
AITile[] neighbors=new AITile[4];
neighbors[0]=new AITile(x,y+32);
......@@ -49,7 +52,9 @@ class AITile {
return neighbors;
}
//Sert à renvoyer une orientatation lorsque l'onveut arriver sur la ca
/**
* Sert à renvoyer une orientation lorsque l'on veut arriver sur la case
*/
public core.zone.Direction orientation(AITile start)
{
//On a cherché le chemin dans le sens inverse, on doit le retourner pour arriver dans le von sen
......
......@@ -10,10 +10,10 @@ import java.util.Comparator;
* Comparator for tiles. Needed for the A* algorithm.
*/
class AITileComparator implements Comparator<AITile> {
public class AITileComparator implements Comparator<AITile> {
private AITile obj;
AITileComparator(AITile obj) {
public AITileComparator(AITile obj) {
this.obj = obj;
}
......
......@@ -11,13 +11,20 @@ public class AITileQueueElement implements Comparable<AITileQueueElement>{
private AITile caseElement;
private double value;
/**
* Enqueue.
* @param element
* @param d
*/
public AITileQueueElement(AITile element, double d) {
caseElement=Objects.requireNonNull(element);
value=d;
}
/**
* Getter caseElement
* @return the tile
*/
public AITile getCase() {
return caseElement;
}
......
package artificial_intelligence.test.unitTest;
import artificial_intelligence.AIEntities.AbstractEntity;
import artificial_intelligence.AIEntities.AgressiveEntity;
import artificial_intelligence.AIEntities.BasicEntity;
import artificial_intelligence.AIEntities.EnumBehaviourType;
import artificial_intelligence.AI_Controler;
import core.gamestate.Character;
import core.gamestate.Entity;
import core.gamestate.GameState;
import core.relayer.Relayer;
import map_generation.map.SurfacesMapGeneration;
import map_generation.map.Map;
import map_generation.map.MapPoint;
import org.junit.Before;
import artificial_intelligence.AIControler;
import org.junit.Test;
import java.util.ArrayList;
......@@ -19,21 +12,25 @@ import java.util.ArrayList;
import static org.junit.Assert.*;
/**
* Created by Yann RAMUSAT on 23/11/15.
* Created by Yann RAMUSAT on 30/12/15.
*
* This is the unit test class for the controler.
* This is the unit test class for the AIControler class.
*/
public class AI_ControlerTest {
public class AIControlerTest {
@Test
public void testadd() throws Exception {
AI_Controler.getInstance().add(null, null, EnumBehaviourType.Basic);
assertEquals(1, AI_Controler.getInstance().getNumber());
AIControler.add(null, null, EnumBehaviourType.Basic);
assertEquals(1, AIControler.getNumber());
AIControler.add(null, null, EnumBehaviourType.Agressive);
boolean obt = AIControler.getList().get(1) instanceof AgressiveEntity;
assertEquals(true, obt);
}
@Test
public void testkillAll() throws Exception {
AI_Controler.getInstance().killAll();
assertEquals(0, AI_Controler.getInstance().getNumber());
AIControler.killAll();
assertEquals(0, AIControler.getNumber());
}
}
\ No newline at end of file
package artificial_intelligence.test.unitTest;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
/**
* Created by Yann Ramusat on 31/12/15.
*
* TestSuite for the AI package.
* Will launch automatically all the JUnits tests.
*
* Note: very helpful, code found in NetworkTests.
*/
@RunWith(Suite.class)
@Suite.SuiteClasses({
AIControlerTest.class,
AITileComparatorTest.class
})
public class AITests{}
package artificial_intelligence.test.unitTest;
import artificial_intelligence.AlphaStar.AITileComparator;
import artificial_intelligence.AlphaStar.AITile;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
/**
* Created by Yann RAMUSAT on 31/12/15.
*
* This is the unit test class for the AITileComparator class.
*/
public class AITileComparatorTest {
@Test
public void compareTest() {
AITile obj = new AITile(0,0);
AITile c1 = new AITile(2,1);
AITile c2 = new AITile(1,2);
AITileComparator comparator = new AITileComparator(obj);
int res = comparator.compare(c1,c2);
assertEquals(0, res);
}
@Test
public void heuristiqueTest() {
AITile obj = new AITile(0,0);
AITile c = new AITile(1,1);
AITileComparator comparator = new AITileComparator(obj);
assertEquals(0, comparator.heuristique(c));
}
}
......@@ -28,7 +28,7 @@ public class Point implements Cloneable, Serializable{
* The point Y axis coordinate
*/
int posY;
public final static int TileScale= GraphicalBridgeConfiguration.TILE_SCALE; // Scale between the entity coordinate and tile coordinate
public final static int TileScale= 32; // Scale between the entity coordinate and tile coordinate
/**
* This point is used as a default for Zone.
......
......@@ -18,7 +18,7 @@ import graphics.guiSkeleton.sprites.Sprite;
*
* Each entity should have one instance of a subclass of this class
* It is used by the gui to know how to display the said entity
*/
*/
abstract public class EntityDisplayer2 implements Serializable {
// // les images doivent avoir leurs animations alignées horizontalement
// //The Entity this Displayer is associated to
......
......@@ -7,6 +7,8 @@ import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.InputStream;
import java.io.ByteArrayInputStream;
/** First iteration of the panel for ingame programming
Created by Alexis Ghyselen **/
......@@ -49,7 +51,7 @@ public class IGPpanel extends graphics.guiSkeleton.guiPanel.GUIPanel{
add(titlePanel, c);
/** Creation of the buttonPanel **/
JPanel buttonPanel = new JPanel();
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridBagLayout());
buttonPanel.setOpaque(true);
buttonPanel.setBackground(GUIColorsAndFonts.backGround1);
......@@ -89,6 +91,7 @@ public class IGPpanel extends graphics.guiSkeleton.guiPanel.GUIPanel{
prflabel[i].setText("");
prflabel[i].setForeground(GUIColorsAndFonts.fontColor1);
prflabel[i].setBackground(GUIColorsAndFonts.buttonBackground1);
prflabel[i].addActionListener(parselistener);
c2 = new GridBagConstraints();
c2.gridx=3; c2.gridy=(i+1); c2.gridwidth=2; c2.gridheight=1; c2.weightx=3; c2.weighty=1; c2.fill=GridBagConstraints.BOTH;
buttonPanel.add(prflabel[i],c2);
......@@ -99,9 +102,9 @@ public class IGPpanel extends graphics.guiSkeleton.guiPanel.GUIPanel{
c2 = new GridBagConstraints();
c2.gridx=5; c2.gridy=(i+1); c2.gridwidth=2; c2.gridheight=1; c2.weightx=3; c2.weighty=1; c2.fill=GridBagConstraints.BOTH;
buttonPanel.add(hintlabel[i],c2);
}
/** Back button **/
JButton exitButton = new JButton();
exitButton.setFont(GUIColorsAndFonts.buttonFont1);
......@@ -154,7 +157,7 @@ public class IGPpanel extends graphics.guiSkeleton.guiPanel.GUIPanel{
hintlabel[vswitch].setText(s);
bswitch[vswitch].setBackground(GUIColorsAndFonts.buttonBackground1);
vswitch = -1;
}
}
};
......@@ -166,8 +169,34 @@ public class IGPpanel extends graphics.guiSkeleton.guiPanel.GUIPanel{
}
};
/**
* ActionListener to parse an instruction.
* Should be modified soon.
*/
private ActionListener parselistener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Script parser = new Script(stringToStream(((JTextField)e.getSource()).getText()));
if (parser.checkInput()) {
System.out.println("L'instruction est correcte");
} else {
System.out.println("Erreur dans l'instruction");
}
}
};
/**
* Convert a String to an equivalent InputStream.
* @param the String to convert.
* @return the equivalent InputStream.
*/
private InputStream stringToStream(String s) {
InputStream is = new ByteArrayInputStream(s.getBytes());
return is;
}
@Override
public void initialise() {
......@@ -187,11 +216,10 @@ public class IGPpanel extends graphics.guiSkeleton.guiPanel.GUIPanel{
public void keyReleasedHandler(Input e) {
}
@Override
public void keyTypedHandler(Input e) {
}
}
}
......@@ -3,14 +3,21 @@ package ingame_programming;
/** Interpréteur du langage de script */
public class Script implements ScriptConstants {
/* TODO. Pour l'instant, on analyse les phrases depuis la ligne de commande. */
public static void main(String args[]) throws ParseException {
Script parser = new Script(System.in);
parser.Instruction();
/**
* Check whether the input is correct.
* @return a boolean about the correctness of the input string.
*/
public boolean checkInput() {
try {
Instruction();
return true;
} catch(ParseException e) {
return false;
}
}
static final public void Instruction() throws ParseException {
final public void Instruction() throws ParseException {
String s, ss;
Token t;
int count = 0;
......@@ -66,7 +73,7 @@ public class Script implements ScriptConstants {
}
/** Détail des options. */
static final public String Option() throws ParseException {
final public String Option() throws ParseException {
String s, ss;
Token t;
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
......@@ -190,17 +197,16 @@ public class Script implements ScriptConstants {
throw new Error("Missing return statement in function");
}
static private boolean jj_initialized_once = false;
/** Generated Token Manager. */
static public ScriptTokenManager token_source;
static SimpleCharStream jj_input_stream;
public ScriptTokenManager token_source;
SimpleCharStream jj_input_stream;
/** Current token. */
static public Token token;
public Token token;
/** Next token. */
static public Token jj_nt;
static private int jj_ntk;
static private int jj_gen;
static final private int[] jj_la1 = new int[6];
public Token jj_nt;
private int jj_ntk;
private int jj_gen;
final private int[] jj_la1 = new int[6];
static private int[] jj_la1_0;
static {
jj_la1_init_0();
......@@ -215,13 +221,6 @@ public class Script implements ScriptConstants {
}
/** Constructor with InputStream and supplied encoding */
public Script(java.io.InputStream stream, String encoding) {
if (jj_initialized_once) {
System.out.println("ERROR: Second call to constructor of static parser. ");
System.out.println(" You must either use ReInit() or set the JavaCC option STATIC to false");
System.out.println(" during parser generation.");
throw new Error();
}
jj_initialized_once = true;
try { jj_input_stream = new SimpleCharStream(stream, encoding, 1, 1); } catch(java.io.UnsupportedEncodingException e) { throw new RuntimeException(e); }
token_source = new ScriptTokenManager(jj_input_stream);
token = new Token();
......@@ -231,11 +230,11 @@ public class Script implements ScriptConstants {
}
/** Reinitialise. */
static public void ReInit(java.io.InputStream stream) {
public void ReInit(java.io.InputStream stream) {
ReInit(stream, null);
}
/** Reinitialise. */
static public void ReInit(java.io.InputStream stream, String encoding) {
public void ReInit(java.io.InputStream stream, String encoding) {
try { jj_input_stream.ReInit(stream, encoding, 1, 1); } catch(java.io.UnsupportedEncodingException e) { throw new RuntimeException(e); }
token_source.ReInit(jj_input_stream);
token = new Token();
......@@ -246,13 +245,6 @@ public class Script implements ScriptConstants {
/** Constructor. */
public Script(java.io.Reader stream) {
if (jj_initialized_once) {
System.out.println("ERROR: Second call to constructor of static parser. ");
System.out.println(" You must either use ReInit() or set the JavaCC option STATIC to false");
System.out.println(" during parser generation.");
throw new Error();
}
jj_initialized_once = true;
jj_input_stream = new SimpleCharStream(stream, 1, 1);
token_source = new ScriptTokenManager(jj_input_stream);
token = new Token();
......@@ -262,7 +254,7 @@ public class Script implements ScriptConstants {
}
/** Reinitialise. */
static public void ReInit(java.io.Reader stream) {
public void ReInit(java.io.Reader stream) {
jj_input_stream.ReInit(stream, 1, 1);
token_source.ReInit(jj_input_stream);
token = new Token();
......@@ -273,13 +265,6 @@ public class Script implements ScriptConstants {
/** Constructor with generated Token Manager. */
public Script(ScriptTokenManager tm) {
if (jj_initialized_once) {
System.out.println("ERROR: Second call to constructor of static parser. ");
System.out.println(" You must either use ReInit() or set the JavaCC option STATIC to false");
System.out.println(" during parser generation.");
throw new Error();
}
jj_initialized_once = true;
token_source = tm;
token = new Token();
jj_ntk = -1;
......@@ -296,7 +281,7 @@ public class Script implements ScriptConstants {
for (int i = 0; i < 6; i++) jj_la1[i] = -1;
}
static private Token jj_consume_token(int kind) throws ParseException {
private Token jj_consume_token(int kind) throws ParseException {
Token oldToken;
if ((oldToken = token).next != null) token = token.next;
else token = token.next = token_source.getNextToken();
......@@ -312,7 +297,7 @@ public class Script implements ScriptConstants {
/** Get the next Token. */
static final public Token getNextToken() {
final public Token getNextToken() {
if (token.next != null) token = token.next;
else token = token.next = token_source.getNextToken();
jj_ntk = -1;
......@@ -321,7 +306,7 @@ public class Script implements ScriptConstants {
}
/** Get the specific Token. */
static final public Token getToken(int index) {
final public Token getToken(int index) {
Token t = token;