# How to make the AI Chase in Multiplayer

Im Currently develop a little Dungeon Crawler. The Singeplayer works fine, but it also should have multiplayer so i implemented it. But there's one problem. I have no idea how to make the AI chase the player and the other players. In singleplayer it works like this :

if(distanceToPlayer <= 400f){

entity.chase(playerEntity);

}


Heres my full chasing code :

// in enemy update method i put all multiplayers into a map with their login number..

 // Pick a random Multiplayer and chase him

boolean isAvaible = false;

//Convert Map to Object array
Object[] values = ((EnemyEntity)arg0).mpSortedPos.values().toArray();

int rdm = 0;

float randomValue = 0;

// wenn map isnt empty
if(((EnemyEntity)arg0).mpSortedPos.size() > 0){

rdm = generator.nextInt(values.length);

randomValue = (float)values[rdm];

isAvaible = true;

}

//selecting the key
// +2 because the first joined player is the second Player in the game so it begins with connection 2, the second with 3 and so on.
int key = rdm + 2;

// Only printing stuff
System.out.println("Found a Map : "+((EnemyEntity)arg0).mpSortedPos + "Size of da HashMap : "+ ((EnemyEntity)arg0).mpSortedPos.size());
System.out.println("Found a Random Value : "+rdm + " "+randomValue );
System.out.println("Found a Key: "+key + " "+((EnemyEntity)arg0).mpSortedPos.get(key) );

// When the multiplayer is near the enemy than
if (/*((EnemyEntity)arg0).distanceToNextPlayer < 400 && arg0.isAlive() == true */ isAvaible && ((EnemyEntity)arg0).mpSortedPos.get(rdm) != null && ((EnemyEntity)arg0).mpSortedPos.get(rdm) < 400) {

((EnemyEntity)arg0).activated = true;

arg0.stateMachine.changeState(CHASE_ENTITY);

}


This is my code for multiplayer chasing but it doesnt work ... any ideas ? What could i improve ?

But how should i do that with multiple players ? Any ideas ? :)

• How much do you know about route finding algorithms like A* or Dijkstra's algorithm? – Philipp Sep 29 '16 at 11:18