Commit e70f5d4c authored by Ghyselen's avatar Ghyselen

Modification for IGPEntity and removing some bugs

parent 09da0877
......@@ -259,21 +259,24 @@ public class FollowerBehaviour {
/** Give the current Character to target, and adapt "ruletoconsider" to this result
* @param list Is the list of all visible character
*/
public Character FindTarget(List<Character> list) {
public List<Character> FindTarget(List<Character> list) {
ruletoconsider = -1;
int k = 0;
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()) {
ruletoconsider = k;
return result.get(0);
return result;
}
else {
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 */
......
......@@ -21,6 +21,69 @@ public class IGPEntity extends AbstractEntity {
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
public void act() {
// Looking at the FollowerBehaviour generated by the IGPpanel
......@@ -32,25 +95,27 @@ public class IGPEntity extends AbstractEntity {
return;
}
// 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();
Character victim = null;
if (victimlist.isEmpty()) {
action = "nothing";
}
else {
victim = nearest(victimlist, relayer.getCharacter(), 2);
if (victim == null) {
return;
}
}
switch (action) {
case "attaquer":
// Goto the victim and try to attack it
double r = random();
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();
}
move_and_attack(victim);
/* Second possible use with A*
int dX = victim.getX();
int dY = victim.getY();
......@@ -63,82 +128,41 @@ public class IGPEntity extends AbstractEntity {
this.relayer.move(dir);
}
this.relayer.tryToCastAbility(0); */
break;
case "proteger":
/* 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) {
double r2 = random();
if(r2<0.9){
//this.move_to_victim(gameState.getAllEntities().get(this.Choose_victim()));
if (relayer.getCharacter().getCollisionBox().intersect(gameState.getAllEntities().get(newvictim).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(gameState.getAllEntities().get(newvictim));
}
}
else{
this.random_move();
}
}
else{
this.waiting_move();
Character newvictim = nearest(chlist,victim,1 - victim.getFaction());
if (newvictim!= null) {
move_and_attack(newvictim);
}
/* Other instruction using A*
if (newvictim != -1) {
int tardX = entityList.get(newvictim).getX();
int tardY = entityList.get(newvictim).getY();
Direction dir2 = AI.alpha_star(map,sX2, sY2, tardX, tardY,map.getWidth(),map.getHeight());
if (relayer.getCharacter().getCollisionBox().intersect(entityList.get(newvictim).getCollisionBox())) {
this.relayer.move(Direction.NONE);
} else {
this.relayer.move(dir2);
}
this.relayer.tryToCastAbility(0);
int sX2 = relayer.getCharacter().getX();
int sY2 = relayer.getCharacter().getY();
int tardX = entityList.get(newvictim).getX();
int tardY = entityList.get(newvictim).getY();
Direction dir2 = AI.alpha_star(map,sX2, sY2, tardX, tardY,map.getWidth(),map.getHeight());
if (relayer.getCharacter().getCollisionBox().intersect(entityList.get(newvictim).getCollisionBox())) {
this.relayer.move(Direction.NONE);
} else {
this.relayer.move(dir2);
}
this.relayer.tryToCastAbility(0);
} */
break;
case "soigner" :
// 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){
//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();
}
follow(victim);
/* Other way using A*
int dX4 = victim.getX();
int dY4 = victim.getY();
......@@ -150,18 +174,20 @@ public class IGPEntity extends AbstractEntity {
} else {
this.relayer.move(dir4);
} */
break;
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;
try {
owner = gameState.getEntity(relayer.getCharacter().getOwner());
}catch(Exception e){
return;
}
double r4 = random();
if(r4<0.9){
//this.move_to_victim(gameState.getAllEntities().get(this.Choose_victim()));
double r = random();
if(r<0.9){
if (relayer.getCharacter().getCollisionBox().intersect(owner.getCollisionBox())) {
this.relayer.move(Direction.NONE);
}
......@@ -172,6 +198,8 @@ public class IGPEntity extends AbstractEntity {
else{
this.random_move();
}
/* other way using A*
int dX3 = owner.getX();
int dY3 = owner.getY();
......@@ -183,6 +211,7 @@ public class IGPEntity extends AbstractEntity {
} else {
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