package ds.moteur;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import ds.io.Sauvegardable;
import ds.moteur.route.Section;
import ds.moteur.route.cc.PointEntree;
import ds.moteur.route.cc.PointSortie;
public class Terrain implements Sauvegardable{
public final static double DISTANCE_CONTACT = 0.1;
private String nom;
private List<Section> sections;
private List<PointEntree> insertions;
public Terrain(String nom){
this.nom = nom;
this.sections = new ArrayList<Section>();
this.insertions = new ArrayList<PointEntree>();
}
public String getNom() {
return nom;
}
public void addSection(Section section){
this.sections.add(section);
}
public List<Section> getSections() {
return sections;
}
public void addInsertion(PointEntree entree){
this.insertions.add(entree);
}
public List<PointEntree> getInsertions() {
return insertions;
}
protected void calculerTerrainAbsolu(){
for (Section section : sections){
section.rendreAbsolu();
}
}
protected void lierSortieEntree(int section1, int sortie, int section2, int entree){
PointSortie pointSortie = sections.get(section1).getSorties().get(sortie);
PointEntree pointEntree = sections.get(section2).getEntrees().get(entree);
pointSortie.lier(pointEntree);
if (pointSortie.verifierLien()>DISTANCE_CONTACT){
System.out.println("Lien incorrect entre sortie : " + pointSortie.x + "/" + pointSortie.y
+ " et entr�e : " + pointEntree.x + "/" + pointEntree.y);
}
}
@Deprecated
public boolean essayerLienBidirectionnel(Section s1, Section s2){
boolean reussi1;
boolean reussi2;
reussi1 = essayerLierUnidirectionnel(s1,s2);
reussi2 = essayerLierUnidirectionnel(s2,s1);
return reussi1 || reussi2;
}
private boolean essayerLierUnidirectionnel(Section s1, Section s2){
boolean reussi = false;
for (PointSortie sortie : s1.getSorties()){
for (PointEntree entree : s2.getEntrees()){
if (sortie.getDistance(entree)<DISTANCE_CONTACT){
if (sortie.getLien() == null){
sortie.lier(entree);
reussi = true;
//System.out.println("Lien �tablit en " + sortie.x + "/" + sortie.y);
}
}
}
}
return reussi;
}
protected void etablirConnexion(){
for (Section section : this.sections){
for (Section sectionConnexion : section.getConnexions()){
boolean reussi = this.essayerLierUnidirectionnel(section, sectionConnexion);
if (!reussi){
System.out.println("Connexion d�fectueuse sur : " + section.toString());
}
}
}
}
public void load(DataInputStream dis) throws IOException {
//Reconstitution des sections
int nSections = dis.readShort();
for(int i=0; i<nSections; i++){
Section section = new Section();;
section.load(dis);
this.addSection(section);
}
//Reconstitution des connexions entre sections
for(int i=0; i<nSections; i++){
int nConnexions = dis.readShort();
for(int j=0; j<nConnexions; j++){
int index = dis.readShort();
sections.get(i).addConnexion(sections.get(index));
}
}
this.etablirConnexion();
//Reconstitution des points d'insertion
int nInsertions = dis.readShort();
for(int i=0; i<nInsertions; i++){
int indexSection = dis.readShort();
Section section = sections.get(indexSection);
int indexEntree = dis.readShort();
PointEntree entree = section.getEntrees().get(indexEntree);
this.addInsertion(entree);
}
}
public void save(DataOutputStream dos) throws IOException {
//Sauvegarde des sections
dos.writeShort(sections.size());
for (Section section : sections){
section.save(dos);
}
//Sauvegarde des connexions entre sections
for (Section section : sections){
dos.writeShort(section.getConnexions().size());
for(Section connexion : section.getConnexions()){
int index = sections.indexOf(connexion);
dos.writeShort(index);
}
}
//Sauvegarde des points d'insertion
dos.writeShort(insertions.size());
for (PointEntree entree : insertions){
Section section = entree.getCourbes().get(0).getOrigine();
int indexSection = sections.indexOf(section);
dos.writeShort(indexSection);
int indexEntree = section.getEntrees().indexOf(entree);
dos.writeShort(indexEntree);
}
}
}