package G53IDS;
//IMPORTS
import java.util.*;
//CODE.
class agentCreator{
void spawn (int[][] map, ArrayList<agent> agentArray){
//get the width and height of the array
int width = map.length;
int height = map[0].length;
//get how large our agentArray is.
int length = agentArray.size();
//set some useful variables that we'll use in our calculations and checks.
int failCount = 0;
int failX=0, failY=0;
boolean possible = false;
//create an object to work with.
agent tempAgent = new agent();
//cycle through our agents.
for (int n =0; n <length; n++){
//reset our fail count.
failCount = 0;
//assign our tempAgent to the object in the agentArray.
tempAgent = agentArray.get(n);
//try some random X and Y coords for our agent.
tempAgent.xCoord = usefulFunctions.randomIntegerGen(1, width-1);
tempAgent.yCoord = usefulFunctions.randomIntegerGen(1, height-1);
//While our chosen coordinates are an obstacle keep trying new ones.
while (map[tempAgent.xCoord][tempAgent.yCoord]==-1){
//Keep track of how hard we're failing.
failCount++;
//set the coords to new random coords inside our area.
tempAgent.xCoord = usefulFunctions.randomIntegerGen(1, width-1);
tempAgent.yCoord = usefulFunctions.randomIntegerGen(1, height-1);
//if we've failed lots check if there exists a location in our map that is not an obstacle.
if(failCount==25){
//cycle through the x-coords
for (failX =0; failX<map.length; failX++){
//cycle through the y-coords
for (failY = 0; failY<map[0].length; failY++){
//if we find a coordinate that isn't an obstacle then state that it's possible.
if (map[failX][failY] != -1){
possible = true;
}//end if not obstacle.
}//end Y loop.
}//end X loop.
//if no coordinate has returned possible then close the program and report the problem.
if (!possible){
System.out.println("No free space for agents");
System.exit(0);
}//end if not possible.
}//end if failCount == 25.
//if we've failed loads but it is possible then we need to look at different ways of placing our agent.
if(failCount>400){
//search through map x coords.
for (failX =0; failX<map.length; failX++){
//search through map y coords.
for (failY = 0; failY<map[0].length; failY++){
//Check if the coordinate is not an obstacle.
if (map[failX][failY] != -1){
//if the coordinate we're at isn't an obstacle then check if we've already got a viable agent location set.
if (map[tempAgent.xCoord][tempAgent.yCoord] != -1){
//if we've already got a random agent location set only overide it with a very low probability so that we maintain our relative randomness of agent start locations.
if (usefulFunctions.randomIntegerGen(0, 1000000) > 999900){
//if our random check passes then set the agent X and Y coords.
tempAgent.xCoord = failX;
tempAgent.yCoord = failY;
}// end low probability of success if.
}//end if agent is already assigned to viable location.
//If the agent hasn't already been assigned to a viable location then assign him to the one we've found..
else {
//assign him to his coords.
tempAgent.xCoord = failX;
tempAgent.yCoord = failY;
}//end else.
}//end check if obstacle.
}//end y loop
}//end x loop
}//end failCount>400 check.
}// end while agent location is obstacle loop.
//overwrite our old agent with the new.
agentArray.set(n, tempAgent);
}//end agent loop.
}// end spawn method.
}//end agentCreator class.