Commit 4a24d57a authored by Toussaint Etienne's avatar Toussaint Etienne

Merge branch 'master' of gitlab.crans.org:genie_logiciel_2015/the_dungeon_project

* 'master' of gitlab.crans.org:genie_logiciel_2015/the_dungeon_project:
  AI.java
parents 907c834a 0310db95
package artificial_intelligence.AlphaStar;
import java.util.PriorityQueue;
import artificial_intelligence.GameOpt;
import core.zone.Direction;
import logging.Logging;
import map_generation.map.Map;
/**
* @author Remy GARNIER && Yann RAMUSAT
*
* This class implements algorithms A* to moves some AIs
*/
public class AI {
/* Some example of general options for the AI */
private GameOpt opt;
/**
* Constructor
*/
public AI(GameOpt opt) {
this.opt = opt;
}
/**
*
* @return the Game Options of the game
*/
public GameOpt getOpt() {
return opt;
}
/**
* Set a game opt.
* @param opt Game Options
*/
public void setOpt(GameOpt opt) {
this.opt = opt;
}
/**
* Simple algorithm to get the direction when the both object share the Same AI Tile
* @param (obj_x,obj_y): Objective coordinates
* @param (source_x,source_y): Object coordinates
* @return the direction to go
*
* */
public static Direction DirectPath(int obj_x, int obj_y, int source_x, int source_y)
{
int disx=(obj_x-source_x);
int disy=(obj_y-source_y);
//In the case where the object are in the same AITile
if (java.lang.Math.abs(disy)>java.lang.Math.abs(disx))
{
if (disy>0) {
return Direction.UP;}
else
{
return Direction.DOWN;}}
else {
if (disx < 0) {
return Direction.RIGHT;
} else {
return Direction.LEFT;
}}}
/**
* Implementation of the A* algorithm.
*
* @param (obj_x,obj_y): Objective coordinates
* @param (source_x,source_y): Object coordinates
* @param (width,heigth): size of the map
* @param map: Map
* @return the direction to go
*/
public static Direction alpha_star(Map map, 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);
//On inversera l'objectif et le départ, il est plus simple de partir de l'arrivée
int disx=(obj_x-source_x);
int disy=(obj_y-source_y);
//In the case where the object are in the same AITile
if (java.lang.Math.abs(disx)<32 && java.lang.Math.abs(disy)<32)
{
return DirectPath(obj_x,obj_y,source_x,source_y);
}
AITileComparator comparator = new AITileComparator(begin);
Direction[][] direction = new Direction[width][height];
int[][] value=new int[width][height];
value[obj_x/32][obj_y/32]=0;
direction[obj_x/32][obj_y/32]=Direction.NONE;
PriorityQueue<AITileQueueElement> queue= new PriorityQueue<>();
queue.add(new AITileQueueElement(obj,0.0));
while(!queue.isEmpty()) {
AITile a = queue.remove().getCase();
if (comparator.compare(a, begin) == 0) {
return direction[a.getX()/32][a.getY()/32];
}
AITile[] neighbhor = a.neighbors();
int vala = value[a.getX()/32][a.getY()/32];
int patha = vala - comparator.heuristique(a);
for (int i = 0; i < 4; i++)
{
AITile b = neighbhor[i];
if (!b.isObstacle(map)) {
int valb = value[b.getX()/32][b.getY()/32];
if (valb == 0.0 && comparator.compare(b, obj) != 0) {
int newval = comparator.heuristique(b) + 1 + patha;
value[b.getX()/32][b.getY()/32]= newval;
queue.add(new AITileQueueElement(b, newval));
direction[b.getX()/32][b.getY()/32]=a.orientation(b);
}
}
}
}
return Direction.NONE;
}
}
\ No newline at end of file
package artificial_intelligence.AlphaStar;
import artificial_intelligence.AIEntities.AbstractEntity;
import core.relayer.Relayer;
import core.zone.Direction;
import map_generation.map.Map;
import map_generation.tiles.Tile;
import map_generation.tiles.TilePropertyVector;
import java.util.ArrayList;
import java.util.List;
/**
* @author Remy GARNIER && Yann RAMUSAT
*
* This class stands for a tile like in MapGen but with the needs of AI.
*/
public class AITile {
private int x;
private int y;
public AITile(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public boolean isObstacle(Map map){
//Map map=AbstractEntity.GetMap();
//Relayer relayer=AbstractEntity.GetRelay();
Tile tile=map.getTileAt(x/32,y/32);
TilePropertyVector tpv=tile.getTilePropertyVector();
List list= new ArrayList<TilePropertyVector.TileProperty>();
list.add(TilePropertyVector.TileProperty.IMPASSABLE);
TilePropertyVector impassable=new TilePropertyVector(list);
return tpv.intersect(impassable);}
/**
* Return information about the closest tiles.
* @return
*/
public AITile[] neighbors(){
AITile[] neighbors=new AITile[4];
neighbors[0]=new AITile(x,y+32);
neighbors[1]=new AITile(x+32,y);
if(y!=0)
{neighbors[2]=new AITile(x,y-32);}
else //Useless, but necesserary for the moment TODO: Improve it
{neighbors[2]=new AITile(x,0);}
if(x!=0)
{neighbors[3]=new AITile(x-32,y);}
else //Useless, but necesserary for the moment TODO: Improve it
{neighbors[3]=new AITile(0,y);}
return neighbors;
}
/**
* 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
int difx= (x-start.getX())/32;
int dify= (y-start.getY())/32;
switch (difx+2*dify)
{
case 2:
return Direction.LEFT;
case -2:
return Direction.RIGHT;
case 1:
return Direction.UP;
case -1:
return Direction.DOWN;
default:
return Direction.NONE;
}
}
// others information could be here and be handled by Case Comparator if u want.
}
\ No newline at end of file
package artificial_intelligence.AlphaStar;
import artificial_intelligence.AlphaStar.AITile;
import java.util.Comparator;
/**
* @author Yann RAMUSAT
*
* Comparator for tiles. Needed for the A* algorithm.
*/
public class AITileComparator implements Comparator<AITile> {
private AITile obj;
public AITileComparator(AITile obj) {
this.obj = obj;
}
/**
* Allows to compare two tiles with respect to their distances to the obj tile.
* @param c1
* @param c2
* @return
*/
@Override
public int compare(AITile c1, AITile c2)
{
// juste pour tester, mais il faudra changer car la conversion est trop violente
return heuristique(c1)- heuristique(c2);
}
/**
* Retourne l'heuristique associee a la case (par exemple sa distance euclidienne par rapport a la case objectif)
* @return
*/
public int heuristique(AITile c) {
int x=(c.getX()-obj.getX())/32;
int y=(c.getY()-obj.getY())/32;
return x*x+y*y;
}
}
\ No newline at end of file
package artificial_intelligence.AlphaStar;
import java.util.Objects;
/**
* @author Yann RAMUSAT and Remy GARNIER
*
* Specific queue for the A* algorithm.
*/
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;
}
@Override
public int compareTo(AITileQueueElement cqe)
{
return (value>cqe.value)?1:-1;
}
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment