Commit e70f5d4c authored by Ghyselen's avatar Ghyselen

Modification for IGPEntity and removing some bugs

parent 09da0877
...@@ -259,21 +259,24 @@ public class FollowerBehaviour { ...@@ -259,21 +259,24 @@ public class FollowerBehaviour {
/** Give the current Character to target, and adapt "ruletoconsider" to this result /** Give the current Character to target, and adapt "ruletoconsider" to this result
* @param list Is the list of all visible character * @param list Is the list of all visible character
*/ */
public Character FindTarget(List<Character> list) { public List<Character> FindTarget(List<Character> list) {
ruletoconsider = -1; ruletoconsider = -1;
int k = 0; int k = 0;
while(k<numberOfRules) { while(k<numberOfRules) {
List<Character> result = findCharacter(list,targets.get(k)); ArrayList<String> copyk = new ArrayList<String>();
copyk.addAll(targets.get(k));
List<Character> result = findCharacter(list,copyk);
if (! result.isEmpty()) { if (! result.isEmpty()) {
ruletoconsider = k; ruletoconsider = k;
return result.get(0); return result;
} }
else { else {
k = k+1; k = k+1;
} }
} }
return list.get(0); ArrayList<Character> emptyList = new ArrayList<Character>();
return emptyList;
} }
/** A debug fonction that allows to Print the Instructions according to the FollowerBehaviour */ /** A debug fonction that allows to Print the Instructions according to the FollowerBehaviour */
......
...@@ -21,6 +21,69 @@ public class IGPEntity extends AbstractEntity { ...@@ -21,6 +21,69 @@ public class IGPEntity extends AbstractEntity {
super(gameContent,relayer); super(gameContent,relayer);
} }
private void move_and_attack(Character victim) {
double r = random();
if (r<0.9) {
if (relayer.getCharacter().getCollisionBox().intersect(victim.getCollisionBox())) {
this.relayer.move(Direction.NONE);
this.relayer.tryToCastAbility(0);
}
else {
this.move_to_victim(victim);
}
}
else{
this.random_move();
}
}
private void follow(Character victim) {
double r = random();
if (r<0.9) {
if (relayer.getCharacter().getCollisionBox().intersect(victim.getCollisionBox())) {
this.relayer.move(Direction.NONE);
}
else {
this.move_to_victim(victim);
}
}
else{
this.random_move();
}
}
private Character nearest(List <Character> entityList , Character victim , int faction) {
int dX = victim.getX();
int dY = victim.getY();
double dmin =0 ;
double dmax = 32*15; // tiles of length 32 : so 15 tiles
int newvictim = -1;
for (int i=0;i<entityList.size();i++) {
Character act_entity = entityList.get(i);
if (act_entity.getFaction() == faction || faction == 2) { // if act_entity is in the asked faction, or if faction = 2 that means that we don't care of the faction
double d = abs(act_entity.getX() - dX)+abs(act_entity.getY() - dY);
if (d<dmax) {
if (dmin == 0) {
dmin = d;
newvictim = i;
}
else {
if (d != 0 && dmin < d) {
dmin = d;
newvictim = i;
}
}
}
}
}
Character victim2 = null;
if (newvictim != -1) {
victim2 = entityList.get(newvictim);
}
return victim2;
}
@Override @Override
public void act() { public void act() {
// Looking at the FollowerBehaviour generated by the IGPpanel // Looking at the FollowerBehaviour generated by the IGPpanel
...@@ -32,25 +95,27 @@ public class IGPEntity extends AbstractEntity { ...@@ -32,25 +95,27 @@ public class IGPEntity extends AbstractEntity {
return; return;
} }
// Using the FollowerBehaviour to find the "victim" of our action and find the action // Using the FollowerBehaviour to find the "victim" of our action and find the action
Character victim = fb.FindTarget(chlist); List<Character> victimlist = fb.FindTarget(chlist);
ArrayList<Character> me = new ArrayList<Character>();
me.add(relayer.getCharacter());
victimlist.removeAll(me);
String action = fb.getAction(); String action = fb.getAction();
Character victim = null;
if (victimlist.isEmpty()) {
action = "nothing";
}
else {
victim = nearest(victimlist, relayer.getCharacter(), 2);
if (victim == null) {
return;
}
}
switch (action) { switch (action) {
case "attaquer": case "attaquer":
// Goto the victim and try to attack it // Goto the victim and try to attack it
double r = random(); move_and_attack(victim);
if(r<0.9){
//this.move_to_victim(gameState.getAllEntities().get(this.Choose_victim()));
if (relayer.getCharacter().getCollisionBox().intersect(victim.getCollisionBox())) {
this.relayer.move(Direction.NONE);
this.relayer.tryToCastAbility(0); // if we are close enough we fight and don't move
}
else {
this.move_to_victim(victim);
}
}
else{
this.random_move();
}
/* Second possible use with A* /* Second possible use with A*
int dX = victim.getX(); int dX = victim.getX();
int dY = victim.getY(); int dY = victim.getY();
...@@ -63,82 +128,41 @@ public class IGPEntity extends AbstractEntity { ...@@ -63,82 +128,41 @@ public class IGPEntity extends AbstractEntity {
this.relayer.move(dir); this.relayer.move(dir);
} }
this.relayer.tryToCastAbility(0); */ this.relayer.tryToCastAbility(0); */
break; break;
case "proteger": case "proteger":
/* Find the nearest ennemy from the character we have to protect and try to attack him */ /* Find the nearest ennemy from the character we have to protect and try to attack him */
int dX2 = victim.getX();
int dY2 = victim.getY();
int sX2 = relayer.getCharacter().getX();
int sY2 = relayer.getCharacter().getY();
double dmin =0 ;
double dmax = 32*15; // tiles of length 32 : so 15 tiles
int newvictim = -1;
List<Entity> entityList=gameState.getAllEntities();
for (int i=0;i<entityList.size();i++) {
Entity act_entity = entityList.get(i);
if (act_entity.getFaction() != victim.getFaction()) { // if act_entity is in another faction that victim
double d = abs(act_entity.getX() - dX2)+abs(act_entity.getY() - dY2);
if (d<dmax) {
if (dmin == 0) {
dmin = d;
newvictim = i;
} else {
if (d != 0 && dmin < d) {
dmin = d;
newvictim = i;
}
}
}
}
}
if (newvictim!=-1) { Character newvictim = nearest(chlist,victim,1 - victim.getFaction());
double r2 = random();
if(r2<0.9){
//this.move_to_victim(gameState.getAllEntities().get(this.Choose_victim())); if (newvictim!= null) {
if (relayer.getCharacter().getCollisionBox().intersect(gameState.getAllEntities().get(newvictim).getCollisionBox())) { move_and_attack(newvictim);
this.relayer.move(Direction.NONE);
this.relayer.tryToCastAbility(0); // if we are close enough we fight and don't move
}
else {
this.move_to_victim(gameState.getAllEntities().get(newvictim));
}
}
else{
this.random_move();
}
}
else{
this.waiting_move();
} }
/* Other instruction using A* /* Other instruction using A*
if (newvictim != -1) { if (newvictim != -1) {
int tardX = entityList.get(newvictim).getX(); int sX2 = relayer.getCharacter().getX();
int tardY = entityList.get(newvictim).getY(); int sY2 = relayer.getCharacter().getY();
Direction dir2 = AI.alpha_star(map,sX2, sY2, tardX, tardY,map.getWidth(),map.getHeight()); int tardX = entityList.get(newvictim).getX();
if (relayer.getCharacter().getCollisionBox().intersect(entityList.get(newvictim).getCollisionBox())) { int tardY = entityList.get(newvictim).getY();
this.relayer.move(Direction.NONE); Direction dir2 = AI.alpha_star(map,sX2, sY2, tardX, tardY,map.getWidth(),map.getHeight());
} else { if (relayer.getCharacter().getCollisionBox().intersect(entityList.get(newvictim).getCollisionBox())) {
this.relayer.move(dir2); this.relayer.move(Direction.NONE);
} } else {
this.relayer.tryToCastAbility(0); this.relayer.move(dir2);
}
this.relayer.tryToCastAbility(0);
} */ } */
break; break;
case "soigner" : case "soigner" :
// This case is not implemented in the game, so "soigner" will be considered as "follow" by the IA // This case is not implemented in the game, so "soigner" will be considered as "follow" by the IA
double r3 = random();
if(r3<0.9){ follow(victim);
//this.move_to_victim(gameState.getAllEntities().get(this.Choose_victim()));
if (relayer.getCharacter().getCollisionBox().intersect(victim.getCollisionBox())) {
this.relayer.move(Direction.NONE);
}
else {
this.move_to_victim(victim);
}
}
else{
this.random_move();
}
/* Other way using A* /* Other way using A*
int dX4 = victim.getX(); int dX4 = victim.getX();
int dY4 = victim.getY(); int dY4 = victim.getY();
...@@ -150,18 +174,20 @@ public class IGPEntity extends AbstractEntity { ...@@ -150,18 +174,20 @@ public class IGPEntity extends AbstractEntity {
} else { } else {
this.relayer.move(dir4); this.relayer.move(dir4);
} */ } */
break; break;
default: default:
// In the default case, so the aciton "nothing", the Follower will just follow his owner // In the default case, so the action "nothing", the Follower will just follow his owner
Entity owner =null; Entity owner =null;
try { try {
owner = gameState.getEntity(relayer.getCharacter().getOwner()); owner = gameState.getEntity(relayer.getCharacter().getOwner());
}catch(Exception e){ }catch(Exception e){
return; return;
} }
double r4 = random(); double r = random();
if(r4<0.9){ if(r<0.9){
//this.move_to_victim(gameState.getAllEntities().get(this.Choose_victim()));
if (relayer.getCharacter().getCollisionBox().intersect(owner.getCollisionBox())) { if (relayer.getCharacter().getCollisionBox().intersect(owner.getCollisionBox())) {
this.relayer.move(Direction.NONE); this.relayer.move(Direction.NONE);
} }
...@@ -172,6 +198,8 @@ public class IGPEntity extends AbstractEntity { ...@@ -172,6 +198,8 @@ public class IGPEntity extends AbstractEntity {
else{ else{
this.random_move(); this.random_move();
} }
/* other way using A* /* other way using A*
int dX3 = owner.getX(); int dX3 = owner.getX();
int dY3 = owner.getY(); int dY3 = owner.getY();
...@@ -183,6 +211,7 @@ public class IGPEntity extends AbstractEntity { ...@@ -183,6 +211,7 @@ public class IGPEntity extends AbstractEntity {
} else { } else {
this.relayer.move(dir3); this.relayer.move(dir3);
} */ } */
} }
} }
} }
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