package src;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import src.estadisticas.EstadisticasTipoUnidad;
import src.estrategias.*;
import src.pathFinding.Astar;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import com.gameloftProgrammersCup.client.ClientCommand;
import com.gameloftProgrammersCup.client.UnitStats;
import com.gameloftProgrammersCup.clientInterfaces.Command;
import com.gameloftProgrammersCup.clientInterfaces.MapElement;
import com.gameloftProgrammersCup.clientInterfaces.Point;
public abstract class Colector extends UnidadBasica {
public int maxLoad;
public int load=0;
protected EstrategiaUnidad estrategia;
public static final int COLECTOR_LIFE = 20; //*2 (40)
public static final int COLECTOR_ACTIONSPEED = 4 ; //*20 (80)
public static final int COLECTOR_VIEWRANGE = 2 ; //*20 (40)
public static final int COLECTOR_ACTIONRANGE = 1 ; //*30 (30)
public static final int COLECTOR_MOVEMENTSPEED = 2 ; //*20 (40)
public static final int COLECTOR_MAXPAYLOAD = 20 ; //*4 (80)
public Colector(int actionRange, int actionSpeed, int life, int movementSpeed, int id, Cliente jugador, int vision,int maxCarry, EstrategiaUnidad estrategia) {
super(actionRange, actionSpeed, life, movementSpeed, id, jugador, vision);
maxLoad=maxCarry;
this.estrategia=estrategia;
// TODO Auto-generated constructor stub
}
public Command getCurrentCommand() {
// TODO Auto-generated method stub
return null;
}
public int getMaxLoad() {
// TODO Auto-generated method stub
return maxLoad;
}
public void resourceXML(Document arg0, Element arg1) {
// TODO Auto-generated method stub
}
public void setResources(int recursos) {
load=recursos;
}
public void unitXML(Document arg0, Element arg1) {
// TODO Auto-generated method stub
}
public void visionXML(Document arg0, Element arg1) {
// TODO Auto-generated method stub
}
public int resourcesStored() {
return load;
}
public int getType(){
return MapElement.TYPE_COLLECTOR;
}
@Override
public void actualizarStat(UnitStats stat, Fortaleza fortaleza) {
super.actualizarStat(stat,fortaleza);
fortaleza.addResources(stat.getResources());
this.setPosition(stat.getPosition());
System.out.println(" Error "+ (new Integer(stat.getErrorCode())).toString());
if (this.getCommand()!=null && this.getPosition().equals(this.getCommand().getDestination())){
//por algun motivo no devuelve el destinationReached, calculo si llego
// System.out.println("Error code a sustituir:"+stat.getErrorCode());
stat.setCommandErrorCode(Command.ERRORCODE_DESTINATIONREACHED);
/*if (this.getWayPoints().size()==0){
System.out.println("No deberia estar aca");
}*/
this.getWayPoints().remove(0);
}
switch(stat.getErrorCode()){
case 0://si colectaba busco la mina y cuento lo que saque
// collect en la mina
if (this.getCommand()!=null && ((ClientCommand)this.getCommand()).getCommandType()=="collect" && !this.getCommand().getDestination().equals(fortaleza.getPosition())){
Set recursos=fortaleza.getKnownResources();
Iterator it=recursos.iterator();
while (it.hasNext()){
Mina recurso=(Mina)it.next();
if (recurso.getPosition().equals(this.getCommand().getDestination())){
recurso.setResourcesStored(recurso.resourcesStored()+this.getActionSpeed());
this.setResources(stat.getPayload());
if (this.resourcesStored()==this.getMaxLoad())
this.setCommand(null);
System.out.println("Cantidad de recursos en la mina "+ recurso.resourcesStored()+ " " +stat.getPayload());
break;
}
}
}
else
{
//collect en la fortaleza
if (this.getCommand()!=null && ((ClientCommand)this.getCommand()).getCommandType()=="collect" && this.getCommand().getDestination().equals(fortaleza.getPosition())){
this.setResources(stat.getPayload());
if (this.resourcesStored()==0)
this.setCommand(null);
}
}
break;
case Command.ERRORCODE_CHARFULL:
{
Set recursos=fortaleza.getKnownResources();
Iterator it=recursos.iterator();
while (it.hasNext()){
Mina recurso=(Mina)it.next();
if (recurso.getPosition().equals(this.getCommand().getDestination())){
recurso.setResourcesStored(recurso.resourcesStored()+stat.getPayload()-this.resourcesStored());
this.setResources(stat.getPayload());
System.out.println("Cantidad de recursos en la mina "+ recurso.resourcesStored()+ " " +stat.getPayload());
break;
}
}
this.setCommand(null);
break;
}
case Command.ERRORCODE_DESTINATIONREACHED:
this.setCommand(null);
break;
case Command.ERRORCODE_CELLAVAILABLE:
//buscar otro camino
if (this.destino==null && this.wayPoints!=null && this.wayPoints.size()>1){
System.out.println("Jodido");
}
if (this.wayPoints!=null && this.wayPoints.size()>0){
Point wayPoint=(Point)this.wayPoints.get(0);
int centroX=this.getPosition().getX();
int centroY=this.getPosition().getY();
int desplazamientoX=wayPoint.getX()-centroX;
int desplazamientoY=wayPoint.getY()-centroY;
int cantidadDiagonalesExactas=Math.min(Math.abs(desplazamientoX), Math.abs(desplazamientoY));
int diagonalX=desplazamientoX>0 ? 1: -1;
int diagonalY=desplazamientoY>0 ? 1: -1;
desplazamientoX=Math.abs(desplazamientoX-cantidadDiagonalesExactas*diagonalX);
desplazamientoY=Math.abs(desplazamientoY-cantidadDiagonalesExactas*diagonalY);
Mina obstaculo;
while (desplazamientoX>0){//es uno u el otro
obstaculo=new Mina();
obstaculo.setPosition(new Point(centroX+desplazamientoX*diagonalX,centroY));
obstaculos.add(obstaculo);
desplazamientoX--;
}
while (desplazamientoY>0){//es uno u el otro
obstaculo=new Mina();
obstaculo.setPosition(new Point(centroX,centroY+desplazamientoY*diagonalY));
obstaculos.add(obstaculo);
desplazamientoY--;
}
while (cantidadDiagonalesExactas>0){
obstaculo=new Mina();
obstaculo.setPosition(new Point(wayPoint.getX()-diagonalX*cantidadDiagonalesExactas,wayPoint.getY()-diagonalY*cantidadDiagonalesExactas));
cantidadDiagonalesExactas--;
obstaculos.add(obstaculo);
}
this.setCommand(null);
this.wayPoints.clear();
}
break;
case Command.ERRORCODE_RESOURCEMAXEDOUT:
Set recursos=fortaleza.getKnownResources();
Iterator it=recursos.iterator();
Mina recurso=new Mina();
while (it.hasNext()){
recurso=(Mina)it.next();
if (this.getCommand()!=null && this.getCommand().getDestination()!=null && recurso.getPosition().equals(this.getCommand().getDestination()))
break;
}
recurso.setResourcesStored(recurso.resourcesStored()+stat.getPayload()-this.resourcesStored());
this.setResources(stat.getPayload());
// System.out.println("Cantidad de recursos en la mina "+ recurso.resourcesStored());
if (fortaleza.resourcesPayload<recurso.resourcesStored())
fortaleza.resourcesPayload=recurso.resourcesStored();
else
recurso.setResourcesStored(fortaleza.resourcesPayload);
recurso.setMaxedOut(true);
this.setCommand(null);
break;
case Command.ERRORCODE_NOTHINGTODEPOSIT://no deberiamos llegar a esto, perdemos turnos
this.setResources(0);
System.out.println("No deberia estar aca!!!!");
this.setCommand(null);
break;
}
if (this.getLife()>stat.getLife()){
this.setFavorecerDiagonalesImperfectas(true);
this.setDestino(Fortaleza.instanciaFortaleza.getPosition());
this.setWayPoints(Astar.intentoDePathFinding(this.destino, this, Fortaleza.instanciaFortaleza));
this.setCommand(null);
}
this.setLife(stat.getLife());
}
public ClientCommand actuar(){
return estrategia.accionDeTurno(this);
}
public int getTipoInternoUnidad(){
return UnidadBasica.TIPO_COLECTORNORMAL;
}
public EstrategiaUnidad getEstrategia() {
return estrategia;
}
public void setEstrategia(EstrategiaUnidad estrategia) {
this.estrategia = estrategia;
}
/**
* @override
*/
public void setEstrategiaUnidad(EstrategiaUnidad eu){
this.setEstrategia(eu);
}
}