ok i will try to paste all method and methods by may be long and not really cute 
public class ArmyHolder {
int factionInt;
ArrayList<ArmyHolder> enemyArmysList= new ArrayList<ArmyHolder>();
ArrayList<UnitsStuck> unitsStuckList=new ArrayList<UnitsStuck>();
boolean underPlayerControl;
boolean followPlayer;
Vector3f armyHolderPosition;
public Vector3f getECADirection(ArmyHolder enemyArmy){
Vector3f direction=
enemyArmy.getArmyHolderPosition().subtract(armyHolderPosition);
return direction;
}
public ArmyHolder getClosestEnemyArmyHolder(){
ArmyHolder closestEnemyArmyHolder = this.getEnemyArmysList().get(0);
for(ArmyHolder enemyArmyHolder:this.getEnemyArmysList()){
if(this.armyHolderPosition.distance(enemyArmyHolder.getArmyHolderPosition())>
this.armyHolderPosition.distance(closestEnemyArmyHolder.getArmyHolderPosition())){
closestEnemyArmyHolder=enemyArmyHolder;
}
}
return closestEnemyArmyHolder;
}
public boolean readyToMove(){
boolean ready=true;
for (UnitsStuck unitsStuck:unitsStuckList){
if(this.armyHolderPosition.distance(unitsStuck.getUnitStuckLocation())>1000){
ready=false;
}
}
return ready;
}
}
other class
public class UnitsStuck {
public ArrayList<SpawnedUnitClass> spawnedUnitsList = new ArrayList<SpawnedUnitClass>();
public ArrayList<SpawnedKnightClass> spawnedKnightList= new ArrayList<SpawnedKnightClass>();
public PlayerClass player;
public ArrayList<UnitsStuck>commandedStucksList= new ArrayList<UnitsStuck>();//who i command
public ArrayList<UnitsStuck>commanderStucksList=new ArrayList<UnitsStuck>();// who commands me
public UnitsStuck target;
public ArrayList<UnitsStuck>targetedBy=new ArrayList<UnitsStuck>();
public Node banner;
public int formation;
public Vector3f unitStuckLocation;
public int attitude;//follow player or search and attack staks for now 1 attack 2 deffend 3 stay/do nothing 4 follow player
public int fighting;//if 1 move out look for enemy (probably UnitStuck target) and fight. if 2 just follow banner
public Vector2f muovereDiLAtoPostoFormazione(Vector3f vector3f){
Vector2f movimento = new Vector2f(vector3f.getZ(), vector3f.getX());
return movimento;
}
//works each time formation moves
public void assignPositionInFormation(ArmyHolder closestEnemyArmyHolder,Camera camera){
//lets say formation is squar
///spawn common soldiers
//round to less in row
//round to excess for the number of rows
if(target!=null&&this.getAttitude()!=4){
//give nobles and commoners positions
APFNS(closestEnemyArmyHolder, APFCS(closestEnemyArmyHolder,0));
}
else{
assignPositionFCSByPlayer(assignPositionFNSByPlayer(0, camera), camera);
}
}
public int assignPositionFCSByPlayer(int lastRow,Camera camera){
int soldiersId=0;
for (int l = 0; l < (int) Math.ceil(Math.sqrt(spawnedUnitsList.size()))-1; l++) {
//per uomeni in fila
for (int i = (int) Math.floor(Math.sqrt(spawnedUnitsList.size())); i > 0; i--) {
for (int j = (int) Math.floor(Math.sqrt(spawnedUnitsList.size()))/2; j >0; j--) {
spawnedUnitsList.get(soldiersId).setPositionInFormation(
new Vector2f(
this.getUnitStuckLocation().getX()-
this.muovereDiLAtoPostoFormazione(camera.getDirection()).getX()*j,
this.getUnitStuckLocation().getY()+
this.muovereDiLAtoPostoFormazione(camera.getDirection()).getY()*j
)
.subtractLocal(new Vector2f(this.getUnitStuckLocation().getX()-camera.getDirection().getX()
, this.getUnitStuckLocation().getY()-camera.getDirection().getZ()).mult(l+lastRow))
);
soldiersId++;
}
//seconda meta
for (int j = 1; j <(int) Math.floor(Math.sqrt(spawnedUnitsList.size()))/2; j++) {
spawnedUnitsList.get(soldiersId).setPositionInFormation(
new Vector2f(
this.getUnitStuckLocation().getX()+
this.muovereDiLAtoPostoFormazione(camera.getDirection()).getX()*j,
this.getUnitStuckLocation().getY()-
this.muovereDiLAtoPostoFormazione(camera.getDirection()).getY()*j
)
.subtractLocal(new Vector2f(this.getUnitStuckLocation().getX()-camera.getDirection().getX()
, this.getUnitStuckLocation().getY()-camera.getDirection().getZ()).mult(l+lastRow))
);
soldiersId++;
}
}//qui finisce formazione non nobile
lastRow++;
}
return lastRow;
}
public int assignPositionFNSByPlayer(int lastRow,Camera camera){
int soldiersId=0;
for (int l = 0; l < (int) Math.ceil(Math.sqrt(spawnedKnightList.size()))-1; l++) {
for (int i = (int) Math.floor(Math.sqrt(spawnedKnightList.size())); i > 0; i--) {
for (int j = (int) Math.floor(Math.sqrt(spawnedKnightList.size()))/2; j >0; j--) {
spawnedKnightList.get(soldiersId).setPositionInFormation(
new Vector2f(
this.getUnitStuckLocation().getX()-
this.muovereDiLAtoPostoFormazione(camera.getDirection()).getX()*j,
this.getUnitStuckLocation().getY()+
this.muovereDiLAtoPostoFormazione(camera.getDirection()).getY()*j
)
.subtractLocal(new Vector2f(this.getUnitStuckLocation().getX()-camera.getDirection().getX()
, this.getUnitStuckLocation().getY()-camera.getDirection().getZ()).mult(l+lastRow))
);
soldiersId++;
}
for (int j = 1; j <(int) Math.floor(Math.sqrt(spawnedKnightList.size()))/2; j++) {
spawnedKnightList.get(soldiersId).setPositionInFormation(
new Vector2f(
this.getUnitStuckLocation().getX()+
this.muovereDiLAtoPostoFormazione(camera.getDirection()).getX()*j,
this.getUnitStuckLocation().getY()-
this.muovereDiLAtoPostoFormazione(camera.getDirection()).getY()*j
)
.subtractLocal(new Vector2f(this.getUnitStuckLocation().getX()-camera.getDirection().getX()
, this.getUnitStuckLocation().getY()-camera.getDirection().getZ()).mult(l+lastRow))
);
soldiersId++;
}
}
lastRow++;
}
return lastRow;
}
/**This method makes Assign Position In Formation to Commoners soldiers in a Squ
* @param closestEnemyArmyHolder gives direction
* @param lastRow just in case they are not first to get positions
* @return needed to spawn noble unit behinde the rows of commoners */
public int APFCS(ArmyHolder closestEnemyArmyHolder,int lastRow){
///spawn common soldiers
//round to less in row
//round to excess for the number of rows
int soldiersId=0;
for (int l = 0; l < (int) Math.ceil(Math.sqrt(spawnedUnitsList.size()))-1; l++) {
for (int i = (int) Math.floor(Math.sqrt(spawnedUnitsList.size())); i > 0; i--) {
for (int j = (int) Math.floor(Math.sqrt(spawnedUnitsList.size()))/2; j >0; j--) {
spawnedUnitsList.get(soldiersId).setPositionInFormation(
new Vector2f(
this.getUnitStuckLocation().getX()-
this.muovereDiLAtoPostoFormazione(this.getECSDirection(closestEnemyArmyHolder)).getX()*j,
this.getUnitStuckLocation().getY()+
this.muovereDiLAtoPostoFormazione(this.getECSDirection(closestEnemyArmyHolder)).getY()*j
)
.subtractLocal(new Vector2f(this.getUnitStuckLocation().getX()-
this.getECSDirection(closestEnemyArmyHolder).getX()
, this.getUnitStuckLocation().getY()-this.getECSDirection(closestEnemyArmyHolder).getZ()).mult(l+lastRow))
);
soldiersId++;
}
for (int j = 1; j <(int) Math.floor(Math.sqrt(spawnedUnitsList.size()))/2; j++) {
spawnedUnitsList.get(soldiersId).setPositionInFormation(
new Vector2f(
this.getUnitStuckLocation().getX()+
this.muovereDiLAtoPostoFormazione(this.getECSDirection(closestEnemyArmyHolder)).getX()*j,
this.getUnitStuckLocation().getY()-
this.muovereDiLAtoPostoFormazione(this.getECSDirection(closestEnemyArmyHolder)).getY()*j
)
.subtractLocal(new Vector2f(this.getUnitStuckLocation().getX()-
this.getECSDirection(closestEnemyArmyHolder).getX()
, this.getUnitStuckLocation().getY()-this.getECSDirection(closestEnemyArmyHolder).getZ()).mult(l+lastRow))
);
soldiersId++;
}
}
lastRow++;
}
return lastRow;
}
public int APFNS(ArmyHolder closestEnemyArmyHolder,int lastRow){
int soldiersId=0;
for (int l = 0; l < (int) Math.ceil(Math.sqrt(spawnedKnightList.size()))-1; l++) {
for (int i = (int) Math.floor(Math.sqrt(spawnedKnightList.size())); i > 0; i--) {
for (int j = (int) Math.floor(Math.sqrt(spawnedKnightList.size()))/2; j >0; j--) {
spawnedUnitsList.get(soldiersId).setPositionInFormation(
new Vector2f(
this.getUnitStuckLocation().getX()-
this.muovereDiLAtoPostoFormazione(this.getECSDirection(closestEnemyArmyHolder)).getX()*(j+2),
this.getUnitStuckLocation().getY()+
this.muovereDiLAtoPostoFormazione(this.getECSDirection(closestEnemyArmyHolder)).getY()*(j+2)
)
.subtractLocal(new Vector2f(this.getUnitStuckLocation().getX()-this.getECSDirection(closestEnemyArmyHolder).getX()
, this.getUnitStuckLocation().getY()-this.getECSDirection(closestEnemyArmyHolder).getZ()).
mult(l+lastRow+1))
);
soldiersId++; //make each soldier been selected only once no metter how many for row or for men in row
}
for (int j = 1; j <(int) Math.floor(Math.sqrt(spawnedKnightList.size()))/2; j++) {
spawnedKnightList.get(soldiersId).setPositionInFormation(
new Vector2f(
this.getUnitStuckLocation().getX()+
this.muovereDiLAtoPostoFormazione(this.getECSDirection(closestEnemyArmyHolder)).getX()*(j+2),
this.getUnitStuckLocation().getY()-
this.muovereDiLAtoPostoFormazione(this.getECSDirection(closestEnemyArmyHolder)).getY()*(j+2)
)
.subtractLocal(new Vector2f(this.getUnitStuckLocation().getX()-this.getECSDirection(closestEnemyArmyHolder).getX()
, this.getUnitStuckLocation().getY()-this.getECSDirection(closestEnemyArmyHolder).getZ()).
mult(l+lastRow+1))
);
soldiersId++;
}
}
lastRow++;
}
return lastRow;
}
public boolean readyToMove(){
boolean ready=true;
if(!spawnedUnitsList.isEmpty()){
for (SpawnedUnitClass spawnedUnitClass:spawnedUnitsList){
if(this.unitStuckLocation.distance(spawnedUnitClass.getActualUnitPosition())>60){
ready=false;
}
}
}
if(!spawnedKnightList.isEmpty()){
for (SpawnedKnightClass spawnedKnightClass:spawnedKnightList){
if(this.unitStuckLocation.distance(spawnedKnightClass.getActualUnitPosition())>60){
ready=false;
}
}
}
return ready;
}
/**
* gives EnemyClosestUnitStuck as Direction
* @param enemyArmy
* @return
*/
public Vector3f getECSDirection(ArmyHolder enemyArmy){
Vector3f direction=
this.getClosestEnemyUnitStuck(enemyArmy).
getUnitStuckLocation().subtract(this.getUnitStuckLocation()).normalize();
return direction;
}
/**
* gives direction to any Vector3f coordinates ,a general method to get direction
* @param vector3f
* @return
*/
public Vector3f getDirection(Vector3f vector3f){
Vector3f direction=
vector3f.subtract(this.getUnitStuckLocation()).normalize();
return direction;
}
public UnitsStuck getClosestEnemyUnitStuck(ArmyHolder enemyArmy){
UnitsStuck closestEnemyUnitStuck = enemyArmy.getUnitsStuckList().get(0);
for(UnitsStuck enemyUnitsStuck:enemyArmy.getUnitsStuckList()){
if(this.getUnitStuckLocation().distance(enemyUnitsStuck.getUnitStuckLocation())>
this.getUnitStuckLocation().distance(closestEnemyUnitStuck.getUnitStuckLocation())){
closestEnemyUnitStuck=enemyUnitsStuck;
}
}
this.setTarget(closestEnemyUnitStuck);
closestEnemyUnitStuck.addToTargetedBy(this);
return closestEnemyUnitStuck;
}
/**
* collision of unit to an other unit if moves to next step
*/
public boolean unitToUnitCollision(boolean freeWay,SpawnedUnitClass unitClass,Vector3f willBeThere){
for(SpawnedUnitClass otherUnits: this.getSpawnedUnitsList()){
if(willBeThere.
distance(otherUnits.getActualUnitPosition())<1){
freeWay=false;
}
}
for(SpawnedKnightClass otherUnits: this.getSpawnedKnightList()){
if(willBeThere.
distance(otherUnits.getActualUnitPosition())<1){
freeWay=false;
}
}
return freeWay;
}
public void moveToFP(){
for(SpawnedUnitClass unitClass: this.getSpawnedUnitsList()) {
boolean freeWay=true;
Vector2f vector2fDirection=
unitClass.getPositionInFormation().
subtract(unitClass.getActualUnitPosition().getX(),
unitClass.getActualUnitPosition().getY())
.normalize();
Vector3f willBeThere=unitClass.getActualUnitPosition().add(vector2fDirection.getX(),
0.f, vector2fDirection.getY());
freeWay = unitToUnitCollision(freeWay ,unitClass, willBeThere);
freeWay=unitToUnitCollision(freeWay,unitClass, willBeThere);
if(freeWay==false){
willBeThere= unitClass.getActualUnitPosition().add(//trygo right and forward
vector2fDirection.getX()/10*.7f+vector2fDirection.getY()/10*.7f,
0.f, vector2fDirection.getY()/10*.7f-vector2fDirection.getX()/10*.7f);
freeWay = unitToUnitCollision(freeWay ,unitClass, willBeThere);
freeWay=unitToUnitCollision(freeWay,unitClass, willBeThere);
if(freeWay==false){
willBeThere= unitClass.getActualUnitPosition().add(//trygo left and forward
vector2fDirection.getX()/10*.7f-vector2fDirection.getY()/10*.7f,
0.f, vector2fDirection.getY()/10*.7f+vector2fDirection.getX()/10*.7f);
freeWay = unitToUnitCollision(freeWay ,unitClass, willBeThere);
freeWay=unitToUnitCollision(freeWay,unitClass, willBeThere);
if (freeWay==false) {
} else {
unitClass.setActualUnitPosition(unitClass.getActualUnitPosition().add(getDirection(willBeThere)));
unitClass.getNodeBody().setLocalTranslation(unitClass.getActualUnitPosition().add(getDirection(willBeThere)));
}
}
else{
unitClass.setActualUnitPosition(unitClass.getActualUnitPosition().add(getDirection(willBeThere)));
unitClass.getNodeBody().setLocalTranslation(unitClass.getActualUnitPosition().add(getDirection(willBeThere)));
}
}
else{
unitClass.setActualUnitPosition(unitClass.getActualUnitPosition().add(
vector2fDirection.getX()/100,
0,
vector2fDirection.getY()/100));
unitClass.getNodeBody().setLocalTranslation(unitClass.getActualUnitPosition());
}
}
}