#include "CTKDebrayable.h"
#include "CMountain.h"
//#include "CTronconRM.h"

CTKDebrayable::CTKDebrayable(scene::ISceneManager *smg,video::IVideoDriver* dr)
{

    smgr=smg;
    driver=dr;
    meshDepart = smgr->getMesh("media/gareTKD.obj");
    //materialDepart.setTexture(0,driver->getTexture("media/texture-pylone.png"));

    meshPoulieDepart = smgr->getMesh("media/poulie-TKD.obj");
    //materialPoulieDepart.setTexture(0,driver->getTexture("media/texture-pylone.png"));

    meshPoulieArrivee = smgr->getMesh("media/poulie-TKD.obj");
    //materialPoulieArrivee.setTexture(0,driver->getTexture("media/texture-pylone.png"));
    posMonteePoulieDepart=0;
    posDescentePoulieDepart=0;

    meshArrivee= smgr->getMesh("media/gareArriveeTKD.obj");
    //materialArrivee.setTexture(0,driver->getTexture("media/texture-pylone.png"));

    nodePoulieDepart=0;
    nodePoulieArrivee=0;

    posMonteePoulieArrivee=0;
    posDescentePoulieArrivee=0;

    typeRM=RM_TKD;
    constructionFinie=false;
    proprietesOuvert=false;
    vitesse=4.0f;
    vitesseMax=4.0f;
    espacementVehicules=20.0f;
}

void CTKDebrayable::SetPositionDepart(core::vector3df p)
{
	positionDepart=p;
}


void CTKDebrayable::SetPositionArrivee(core::vector3df p)
{
	positionArrivee=p;
}

core::vector3df CTKDebrayable::GetPositionDepart() {
    return positionDepart;
}

core::vector3df CTKDebrayable::GetPositionArrivee() {
    return positionArrivee;
}

//Methode a surcharger pour chaque RM
core::stringc CTKDebrayable::GetID() {
    return nodeDepart->getName();
}

//Methode a surcharger pour chaque RM
void CTKDebrayable::SetID(core::stringc name) {
    nodeDepart->setName(name);
}

void CTKDebrayable::DrawRemonteeDepart() {

    nodeDepart=smgr->addMeshSceneNode( meshDepart );
    nodeDepart->setID(CMountain::TKD_NODE_ID);
    nodeDepart->setScale(core::vector3df(4.0,4.0,4.0));
    nodeDepart->getMaterial(0) = materialDepart;
    nodeDepart->setMaterialFlag(video::EMF_LIGHTING, false);
    nodeDepart->setPosition(core::vector3df(positionDepart.X,positionDepart.Y-4,positionDepart.Z));
    nodeDepart->updateAbsolutePosition();

    scene::ITriangleSelector* selector = 0;
    selector =smgr->createTriangleSelector(meshDepart,nodeDepart);
    nodeDepart->setTriangleSelector(selector);
    selector->drop(); // We're done with this selector, so drop it now.

    nodePoulieDepart=smgr->addMeshSceneNode( meshPoulieDepart,nodeDepart );
    //nodePoulieDepart->getMaterial(0) = materialPoulieDepart;
    nodePoulieDepart->setMaterialFlag(video::EMF_LIGHTING, false);
    nodePoulieDepart->setScale(core::vector3df(0.23,0.23,0.23));
    nodePoulieDepart->setRotation(core::vector3df(180,0,8));
    nodePoulieDepart->setPosition(core::vector3df(1.2,3.97,0));

    meshPoulieDepartD = smgr->getMesh("media/poulie-TKD.obj");
    nodePoulieDepartD=smgr->addMeshSceneNode( meshPoulieDepartD,nodeDepart );
    //nodeGaletArriveeM->getMaterial(0) = materialPoulie1M;
    nodePoulieDepartD->setMaterialFlag(video::EMF_LIGHTING, false);
    nodePoulieDepartD->setScale(core::vector3df(0.20,0.20,0.20));
    nodePoulieDepartD->setRotation(core::vector3df(90,0,0));
    nodePoulieDepartD->setPosition(core::vector3df(0.5,4.2,-0.35));
    nodePoulieDepartD->updateAbsolutePosition();

    meshPoulieDepartM = smgr->getMesh("media/poulie-TKD.obj");
    nodePoulieDepartM=smgr->addMeshSceneNode( meshPoulieDepartM,nodeDepart );
    //nodeGaletArriveeM->getMaterial(0) = materialPoulie1M;
    nodePoulieDepartM->setMaterialFlag(video::EMF_LIGHTING, false);
    nodePoulieDepartM->setScale(core::vector3df(0.20,0.20,0.20));
    nodePoulieDepartM->setRotation(core::vector3df(-90,0,0));
    nodePoulieDepartM->setPosition(core::vector3df(-2.20,3.76,0.35));
    nodePoulieDepartM->updateAbsolutePosition();

    posMonteePoulieDepart=smgr->addEmptySceneNode(nodeDepart);
    posMonteePoulieDepart->setPosition(core::vector3df(1.2,3.98,0.33));


    posDescentePoulieDepart=smgr->addEmptySceneNode(nodeDepart);
    posDescentePoulieDepart->setPosition(core::vector3df(1.2,3.98,-0.33));


    posPoulieDepartMontee=smgr->addEmptySceneNode(nodeDepart);
    posPoulieDepartMontee->setPosition(core::vector3df(-2.20,3.50,0.35));
    //posPoulieDepartMontee->updateAbsolutePosition();

    posPoulieDepartDescente=smgr->addEmptySceneNode(nodeDepart);
    posPoulieDepartDescente->setPosition(core::vector3df(0.5,3.91,-0.35));
    //posPoulieDepartDescente->updateAbsolutePosition();


}

void CTKDebrayable::DrawRemonteeArrivee()
{
    nodeArrivee=smgr->addMeshSceneNode(meshArrivee);
    nodeArrivee->setScale(core::vector3df(4.2,4.2,4.2));
    //nodeArrivee->getMaterial(0) = materialArrivee;
    nodeArrivee->setMaterialFlag(video::EMF_LIGHTING, false);
    nodeArrivee->setPosition(positionArrivee);
    //nodeArrivee->updateAbsolutePosition();

    nodePoulieArrivee=smgr->addMeshSceneNode( meshPoulieArrivee,nodeArrivee );
    nodePoulieArrivee->setParent(nodeArrivee);
    //nodePoulieArrivee->getMaterial(0) = materialPoulieArrivee;
    nodePoulieArrivee->setMaterialFlag(video::EMF_LIGHTING, false);
    nodePoulieArrivee->setScale(core::vector3df(0.65,0.65,0.65));
    nodePoulieArrivee->setRotation(core::vector3df(-180,0,0));
    nodePoulieArrivee->setPosition(core::vector3df(0.0,3.25,-3.4));
    //nodePoulieArrivee->updateAbsolutePosition();

    meshPoulieArriveeD = smgr->getMesh("media/poulie-TKD.obj");
    nodePoulieArriveeD=smgr->addMeshSceneNode( meshPoulieArriveeD,nodeArrivee );
    //nodeGaletArriveeM->getMaterial(0) = materialPoulie1M;
    nodePoulieArriveeD->setMaterialFlag(video::EMF_LIGHTING, false);
    nodePoulieArriveeD->setScale(core::vector3df(0.20,0.20,0.20));
    nodePoulieArriveeD->setRotation(core::vector3df(0,0,90));
    nodePoulieArriveeD->setPosition(core::vector3df(1.0,2.93,-1.29));
    //nodePoulieArriveeD->updateAbsolutePosition();

    meshPoulieArriveeM = smgr->getMesh("media/poulie-TKD.obj");
    nodePoulieArriveeM=smgr->addMeshSceneNode( meshPoulieArriveeM,nodeArrivee );
    //nodeGaletArriveeM->getMaterial(0) = materialPoulie1M;
    nodePoulieArriveeM->setMaterialFlag(video::EMF_LIGHTING, false);
    nodePoulieArriveeM->setScale(core::vector3df(0.20,0.20,0.20));
    nodePoulieArriveeM->setRotation(core::vector3df(0,0,-90));
    nodePoulieArriveeM->setPosition(core::vector3df(-1.0,2.93,-1.29));
    //nodePoulieArriveeM->updateAbsolutePosition();

    contactPoulieArriveeM=smgr->addEmptySceneNode(nodeArrivee);
    contactPoulieArriveeM->setPosition(core::vector3df(-0.9,3.25,-3.4));
    //contactPoulieArriveeM->updateAbsolutePosition();

    contactPoulieArriveeD=smgr->addEmptySceneNode(nodeArrivee);
    contactPoulieArriveeD->setPosition(core::vector3df(0.9,3.25,-3.4));
    //contactPoulieArriveeD->updateAbsolutePosition();

    posMonteePoulieArrivee=smgr->addEmptySceneNode(nodeArrivee);
    posMonteePoulieArrivee->setPosition(core::vector3df(-1.0,3.2,-1.29));

    posDescentePoulieArrivee=smgr->addEmptySceneNode(nodeArrivee);
    posDescentePoulieArrivee->setPosition(core::vector3df(1.0,3.2,-1.29));


    //Oriente la rotation du départ et arrivée
    core::vector3df result=positionArrivee-positionDepart;
    result=result.getHorizontalAngle();
    nodeDepart->setRotation(core::vector3df(0,result.Y+90,0));
    nodeArrivee->setRotation(core::vector3df(0,result.Y+180,0));

    //Mise a jour troncons
    nodeDepart->updateAbsolutePosition();
    nodePoulieDepart->updateAbsolutePosition();
    nodePoulieDepartM->updateAbsolutePosition();
    nodePoulieDepartD->updateAbsolutePosition();
    posPoulieDepartMontee->updateAbsolutePosition();
    posPoulieDepartDescente->updateAbsolutePosition();
    posDescentePoulieDepart->updateAbsolutePosition();
    posMonteePoulieDepart->updateAbsolutePosition();

    nodeArrivee->updateAbsolutePosition();
    nodePoulieArrivee->updateAbsolutePosition();
    nodePoulieArriveeM->updateAbsolutePosition();
    nodePoulieArriveeD->updateAbsolutePosition();
    contactPoulieArriveeM->updateAbsolutePosition();
    contactPoulieArriveeD->updateAbsolutePosition();
    posDescentePoulieArrivee->updateAbsolutePosition();
    posMonteePoulieArrivee->updateAbsolutePosition();
    UpdateTroncons();
}

//Redessine le cable, après chaque modification du TK, en fonction du nombre et des coordonnées des pylones
void CTKDebrayable::RedrawCable()
{
    core::vector3df deltaPoulieM;
    core::vector3df deltaPoulieD;
    core::vector3df deltaPoulieM2;
    core::vector3df deltaPoulieD2;

    if(nodePoulieDepart && nodePoulieArrivee) {
        driver->setTransform(video::ETS_WORLD,core::IdentityMatrix);
        //driver->draw3DLine(nodePoulieDepart->getAbsolutePosition() , nodePoulieArrivee->getAbsolutePosition(),  video::SColor(255, 0, 0, 0));

        if(arrPylones.size()==0)
        {

            driver->draw3DLine(posMonteePoulieDepart->getAbsolutePosition() , posPoulieDepartMontee->getAbsolutePosition() ,  video::SColor(255, 0, 0, 0));
            driver->draw3DLine(posDescentePoulieDepart->getAbsolutePosition() , posPoulieDepartDescente->getAbsolutePosition(),  video::SColor(255, 0, 0, 0));

            //printf("pos cable  => %f ; %f ; %f\n",posPoulieDepartMontee->getAbsolutePosition().X,posPoulieDepartMontee->getAbsolutePosition().Y,posPoulieDepartMontee->getAbsolutePosition().Z);

            driver->draw3DLine(posPoulieDepartMontee->getAbsolutePosition() , posMonteePoulieArrivee->getAbsolutePosition() ,  video::SColor(255, 0, 0, 0));
            driver->draw3DLine(posPoulieDepartDescente->getAbsolutePosition(),posDescentePoulieArrivee->getAbsolutePosition(),  video::SColor(255, 0, 0, 0));

            driver->draw3DLine(posMonteePoulieArrivee->getAbsolutePosition() , contactPoulieArriveeM->getAbsolutePosition() ,  video::SColor(255, 0, 0, 0));
            driver->draw3DLine(posDescentePoulieArrivee->getAbsolutePosition(),contactPoulieArriveeD->getAbsolutePosition(),  video::SColor(255, 0, 0, 0));


        } else {
            //Pour la montée
            for(unsigned int i=0;i<arrPylones.size();i++)
            {

                if(i==0)
                {
                    if(arrPylones[i]->monteeSupportCompression==1)
                    {
                        deltaPoulieM=core::vector3df(0.0f,0.4f,0.0f);
                    } else {
                        deltaPoulieM=core::vector3df(0.0f,-0.4f,0.0f);
                    }
                    /*if(arrPylones[i]->descenteSupportCompression==1)
                    {
                        deltaPoulieD=core::vector3df(0.0f,0.4f,0.0f);
                    } else {
                        deltaPoulieD=core::vector3df(0.0f,-0.4f,0.0f);
                    }*/
                    driver->draw3DLine(posMonteePoulieDepart->getAbsolutePosition() , arrPylones[i]->posMonteePylone->getAbsolutePosition()+deltaPoulieM,  video::SColor(255, 0, 0, 0));
                    //driver->draw3DLine(posDescentePoulieDepart->getAbsolutePosition(),arrPylones[i]->posDescentePylone->getAbsolutePosition()+deltaPoulieD,  video::SColor(255, 0, 0, 0));

                } else {
                    if(arrPylones[i]->monteeSupportCompression==1)
                    {
                        deltaPoulieM=core::vector3df(0.0f,0.4f,0.0f);
                    } else {
                        deltaPoulieM=core::vector3df(0.0f,-0.4f,0.0f);
                    }
                    /*if(arrPylones[i]->descenteSupportCompression==1)
                    {
                        deltaPoulieD=core::vector3df(0.0f,0.4f,0.0f);
                    } else {
                        deltaPoulieD=core::vector3df(0.0f,-0.4f,0.0f);
                    }*/

                    if(arrPylones[i-1]->monteeSupportCompression==1)
                    {
                        deltaPoulieM2=core::vector3df(0.0f,0.4f,0.0f);
                    } else {
                        deltaPoulieM2=core::vector3df(0.0f,-0.4f,0.0f);
                    }
                    /*if(arrPylones[i-1]->descenteSupportCompression==1)
                    {
                        deltaPoulieD2=core::vector3df(0.0f,0.4f,0.0f);
                    } else {
                        deltaPoulieD2=core::vector3df(0.0f,-0.4f,0.0f);
                    }*/

                    driver->draw3DLine(arrPylones[i-1]->posMonteePylone->getAbsolutePosition()+deltaPoulieM2, arrPylones[i]->posMonteePylone->getAbsolutePosition()+deltaPoulieM, video::SColor(255, 0, 0, 0));
                   // driver->draw3DLine(arrPylones[i-1]->posDescentePylone->getAbsolutePosition()+deltaPoulieD2, arrPylones[i]->posDescentePylone->getAbsolutePosition()+deltaPoulieD, video::SColor(255, 0, 0, 0));
                }

                if(i==arrPylones.size()-1)
                {
                    if(arrPylones[i]->monteeSupportCompression==1)
                    {
                        deltaPoulieM=core::vector3df(0.0f,0.4f,0.0f);
                    } else {
                        deltaPoulieM=core::vector3df(0.0f,-0.4f,0.0f);
                    }
                    /*if(arrPylones[i]->descenteSupportCompression==1)
                    {
                        deltaPoulieD=core::vector3df(0.0f,0.4f,0.0f);
                    } else {
                        deltaPoulieD=core::vector3df(0.0f,-0.4f,0.0f);
                    }*/

                    driver->draw3DLine(arrPylones[i]->posMonteePylone->getAbsolutePosition()+deltaPoulieM, posMonteePoulieArrivee->getAbsolutePosition(), video::SColor(255, 0, 0, 0));
                   // driver->draw3DLine(arrPylones[i]->posDescentePylone->getAbsolutePosition()+deltaPoulieD, posDescentePoulieArrivee->getAbsolutePosition(), video::SColor(255, 0, 0, 0));
                }
            }


            //Pour la descente, c'est particulier, on peut avoir des pylones sans brin descente
            int cptDes=arrPylones.size()-1;

            //cas particulier
            if(arrPylones.size()==0) {
                 driver->draw3DLine(posDescentePoulieDepart->getAbsolutePosition(), posDescentePoulieArrivee->getAbsolutePosition(), video::SColor(255, 0, 0, 0));
            }

            while(cptDes>=0) {
                if(arrPylones[cptDes]->GetType()==CPyloneRM::PYLONE_TKD_MONTEE_DESCENTE) {
                    u32 idprevious=findPreviousPyloneWithDescente(cptDes);
                    if(idprevious==-1) {
                        if(arrPylones[cptDes]->descenteSupportCompression==1)
                        {
                            deltaPoulieD=core::vector3df(0.0f,0.4f,0.0f);
                        } else {
                            deltaPoulieD=core::vector3df(0.0f,-0.4f,0.0f);
                        }
                        driver->draw3DLine(arrPylones[cptDes]->posDescentePylone->getAbsolutePosition()+deltaPoulieD, posDescentePoulieArrivee->getAbsolutePosition(), video::SColor(255, 0, 0, 0));
                    } else {
                        if(arrPylones[cptDes]->descenteSupportCompression==1)
                        {
                            deltaPoulieD=core::vector3df(0.0f,0.4f,0.0f);
                        } else {
                            deltaPoulieD=core::vector3df(0.0f,-0.4f,0.0f);
                        }
                        if(arrPylones[idprevious]->descenteSupportCompression==1)
                        {
                            deltaPoulieD2=core::vector3df(0.0f,0.4f,0.0f);
                        } else {
                            deltaPoulieD2=core::vector3df(0.0f,-0.4f,0.0f);
                        }
                        driver->draw3DLine(arrPylones[cptDes]->posDescentePylone->getAbsolutePosition()+deltaPoulieD, arrPylones[idprevious]->posDescentePylone->getAbsolutePosition()+deltaPoulieD2, video::SColor(255, 0, 0, 0));
                    }
                    cptDes--;
                    //Juste pour le dernier troncon
                    if(cptDes==-1) {
                         driver->draw3DLine(arrPylones[0]->posDescentePylone->getAbsolutePosition()+deltaPoulieD, posDescentePoulieDepart->getAbsolutePosition(), video::SColor(255, 0, 0, 0));
                    }
                } else {
                    u32 idnext=findNextPyloneWithDescente(cptDes);
                    u32 idprevious=findPreviousPyloneWithDescente(cptDes);

                    if(idprevious==-1) {
                        if(idnext==-1) {
                            driver->draw3DLine(posDescentePoulieDepart->getAbsolutePosition(), posDescentePoulieArrivee->getAbsolutePosition(), video::SColor(255, 0, 0, 0));
                            cptDes=-1;
                        } else {
                            if(arrPylones[idnext]->descenteSupportCompression==1)
                            {
                                deltaPoulieD=core::vector3df(0.0f,0.4f,0.0f);
                            } else {
                                deltaPoulieD=core::vector3df(0.0f,-0.4f,0.0f);
                            }
                            driver->draw3DLine(arrPylones[idnext]->posDescentePylone->getAbsolutePosition()+deltaPoulieD, posDescentePoulieArrivee->getAbsolutePosition(), video::SColor(255, 0, 0, 0));
                            cptDes=idnext;
                        }
                    } else {
                        if(idnext==-1) {
                            if(arrPylones[idprevious]->descenteSupportCompression==1)
                            {
                                deltaPoulieD=core::vector3df(0.0f,0.4f,0.0f);
                            } else {
                                deltaPoulieD=core::vector3df(0.0f,-0.4f,0.0f);
                            }
                            driver->draw3DLine(arrPylones[idprevious]->posDescentePylone->getAbsolutePosition()+deltaPoulieD, posDescentePoulieDepart->getAbsolutePosition(), video::SColor(255, 0, 0, 0));
                            cptDes=-1;
                        } else {
                            if(arrPylones[idprevious]->descenteSupportCompression==1)
                            {
                                deltaPoulieD=core::vector3df(0.0f,0.4f,0.0f);
                            } else {
                                deltaPoulieD=core::vector3df(0.0f,-0.4f,0.0f);
                            }
                            if(arrPylones[idnext]->descenteSupportCompression==1)
                            {
                                deltaPoulieD2=core::vector3df(0.0f,0.4f,0.0f);
                            } else {
                                deltaPoulieD2=core::vector3df(0.0f,-0.4f,0.0f);
                            }
                            driver->draw3DLine(arrPylones[idprevious]->posDescentePylone->getAbsolutePosition()+deltaPoulieD, arrPylones[idnext]->posDescentePylone->getAbsolutePosition()+deltaPoulieD2, video::SColor(255, 0, 0, 0));
                            cptDes=idnext;
                        }
                    }
                }
            }

        }

    //printf("RedrawCable entre %f %f %f  et %f %f %f \n",nodePoulieDepart->getAbsolutePosition().X,nodePoulieDepart->getAbsolutePosition().Y,nodePoulieDepart->getAbsolutePosition().Z,nodePoulieArrivee->getAbsolutePosition().X,nodePoulieArrivee->getAbsolutePosition().Y,nodePoulieArrivee->getAbsolutePosition().Z);
    }
}

u32 CTKDebrayable::findNextPyloneWithDescente(u32 startindex) {
    u32 idnext=startindex-1;
    if(idnext!=-1) {
        for(unsigned int i=idnext;i>=0;i--)
        {
            if(arrPylones[i]->GetType()==CPyloneRM::PYLONE_TKD_MONTEE_DESCENTE) {
                return i;
            }
        }
    }

     return -1;
}

u32 CTKDebrayable::findPreviousPyloneWithDescente(u32 startindex) {
    u32 idprevious=startindex+1;

    if(idprevious<arrPylones.size()) {
        for(unsigned int i=idprevious;i<arrPylones.size();i++)
        {
            if(arrPylones[i]->GetType()==CPyloneRM::PYLONE_TKD_MONTEE_DESCENTE) {
                return i;
            }
        }
    }

    return -1;
}

void CTKDebrayable::initPerchesInGareDeparts(u32 nbperches) {
    //pour toutes les perches
    for(unsigned int i=0;i<arrVehicules.size();i++)
    {
        //trouve le tronçon de la perche
        CTronconRM* t= GetTronconByNumero(arrVehicules[i]->numeroTroncon);
        if(t)
        {
            if(t->GetType()==CTronconRM::TRONCON_GARETKD) {
                u32 ordre=arrVehicules[i]->placeGareTKD;

                //Calcule le pas pour cette perche dans la glissiere
                arrVehicules[i]->pas=0.9f-(ordre*0.1f);
            }
        }

    }
}

u32 CTKDebrayable::getNbPerchesInGare() {
    u32 cpt=0;
    for(unsigned int i=0;i<arrVehicules.size();i++)
    {
        //trouve le tronçon de la perche
        CTronconRM* t= GetTronconByNumero(arrVehicules[i]->numeroTroncon);
        if(t)
        {
            if(t->GetType()==CTronconRM::TRONCON_GARETKD) {
                cpt++;
            }
        }

    }
    return cpt;
}

void CTKDebrayable::addPercheInGare(CVehicule *p) {
    u32 nbperches=getNbPerchesInGare();
    printf("add perche nb Perches = %i\n",nbperches);
    if(nbperches==0) {
        p->placeGareTKD=0;
    } else {
        p->placeGareTKD=nbperches-1;
    }


}


void CTKDebrayable::removePercheInGare() {
    //Prend la perche dans le selecteur, maj numero section
    // + mise a jour des autres perches dans la gare
    u32 nbperches=getNbPerchesInGare();
    printf("lance perche nb Perches dispos = %i\n",nbperches);
    if(nbperches>0) {
        //Trouve la perche dans le selecteur (celle dont la place est a zero)
        for(unsigned int i=0;i<arrVehicules.size();i++)
        {
            printf("Perche %i - numtroncon=%i et placeGareTk = %i\n",i,arrVehicules[i]->numeroTroncon,arrVehicules[i]->placeGareTKD);
            if(arrVehicules[i]->numeroTroncon==1 && arrVehicules[i]->placeGareTKD==0) {
                arrVehicules[i]->placeGareTKD=-1;
                CTronconRM* t2= GetTronconByNumero(2);
                if(t2)
                {
                    arrVehicules[i]->setPosition(t2->positionDebut);
                }
                arrVehicules[i]->numeroTroncon=2;
                arrVehicules[i]->pas=0;
                printf("perche %i lancee \n",i);
            }
        }

        //Met a jour la file des perches dans la gare
        for(unsigned int i=0;i<arrVehicules.size();i++)
        {
            CTronconRM* t= GetTronconByNumero(arrVehicules[i]->numeroTroncon);
            if(t)
            {
                if(t->GetType()==CTronconRM::TRONCON_GARETKD) {
                    arrVehicules[i]->placeGareTKD--;
                }
            }

        }
    }
}


core::vector3df CTKDebrayable::GetRotationRM()
{
    return nodeDepart->getRotation();
}


//Anime le tk
void CTKDebrayable::AnimateRm(core::vector3df posCamera,f32 deltaTime)
{
    if(nodePoulieDepart==0 || nodePoulieArrivee==0) {
        return;
    }

    if(enMarche) {
        core::vector3df rotation=nodePoulieDepart->getRotation();
        //rotation.X=0;
        rotation.Y-=vitesse*deltaTime*50.0f;
        //rotation.Z=0;
        if(rotation.Y<0.0f) {
            rotation.Y=rotation.Y+360.0f;
        }
        nodePoulieDepart->setRotation(rotation);

        rotation=nodePoulieDepartD->getRotation();
        rotation.Z+=vitesse*deltaTime*51.0f;
        if(rotation.Z>360.0f) {
            rotation.Z=rotation.Z-360.0f;
        }
        nodePoulieDepartD->setRotation(rotation);

        rotation=nodePoulieDepartM->getRotation();
        rotation.Z-=vitesse*deltaTime*51.0f;
        if(rotation.Z<0.0f) {
            rotation.Z=rotation.Z+360.0f;
        }
        nodePoulieDepartM->setRotation(rotation);


        rotation=nodePoulieArriveeM->getRotation();
        rotation.Y-=vitesse*deltaTime*51.0f;
        if(rotation.Y<0.0f) {
            rotation.Y=rotation.Y+360.0f;
        }
        nodePoulieArriveeM->setRotation(rotation);

        rotation=nodePoulieArriveeD->getRotation();
        rotation.Y-=vitesse*deltaTime*51.0f;
        if(rotation.Y<0.0f) {
            rotation.Y=rotation.Y+360.0f;
        }
        nodePoulieArriveeD->setRotation(rotation);

        rotation=nodePoulieArrivee->getRotation();
        core::matrix4 m;
        core::matrix4 mp;
        m.setRotationDegrees(rotation);
        mp.setRotationDegrees(core::vector3df(0.0f,vitesse*deltaTime*30.0f,0.0f));

        m*=mp;
        rotation=m.getRotationDegrees();
        nodePoulieArrivee->setRotation(rotation);

        //Gère le son du moteur selon distance caméra
        core::vector3df diffcamera=posCamera-nodeDepart->getPosition();
        f32 distanceCamera=abs(diffcamera.getLength());

        if(soundMoteur) {
            if(distanceCamera>100.0f)
            {
                if(soundMoteur->isPlaying()) {
                    soundMoteur->stop();
                }
            } else {
                soundMoteur->setVolume(1.0f-(distanceCamera/100.0f)); // 50% volume
                if(!soundMoteur->isPlaying())
                {
                     soundMoteur->play();
                }

            }
        }


        //Anime les pylones
        for(unsigned int i=0;i<arrPylones.size();i++)
        {
            arrPylones[i]->AnimatePylone(vitesse*deltaTime*50.0f);
        }

        //Anime les perches
        for(unsigned int i=0;i<arrVehicules.size();i++)
        {
            //trouve le tronçon de la perche
            CTronconRM* t= GetTronconByNumero(arrVehicules[i]->numeroTroncon);
            if(t)
            {
                //Mouvement rectiligne pour un troncon de ligne normale
                if(t->GetType()==CTronconRM::TRONCON_LIGNE)
                {
                    core::vector3df lastPosition=arrVehicules[i]->getPosition();
                    //printf("lastPosition = %f ; %f ; %f\n",lastPosition.X,lastPosition.Y,lastPosition.Z);
                    //printf("positionFin = %f ; %f ; %f\n",t->positionFin.X,t->positionFin.Y,t->positionFin.Z);
                    core::vector3df difference=t->positionFin-t->positionDebut;
                    f32 longueur=difference.getLength();

                    arrVehicules[i]->pas+=1.4*deltaTime*vitesse/longueur;
                    /*if(i==0) {
                        printf("perche %i  troncon=%i pas=%f  sur longueur=%f  deltatime=%f\n",i,t->numero,arrVehicules[i]->pas,longueur,deltaTime);
                    }*/
                    core::vector3df vecPosPerche;

                    vecPosPerche.X=t->positionDebut.X+difference.X*arrVehicules[i]->pas;
                    vecPosPerche.Y=t->positionDebut.Y+difference.Y*arrVehicules[i]->pas;
                    vecPosPerche.Z=t->positionDebut.Z+difference.Z*arrVehicules[i]->pas;

                    arrVehicules[i]->setPosition(vecPosPerche);

                    core::vector3df newPosition=arrVehicules[i]->getPosition();
                    //printf("2-perche %i => %f ; %f ; %f\n",i,newPosition.X,newPosition.Y,newPosition.Z);
                    //Verifie si on a atteint la fin du troncon. On se laisse une marge pour eviter d'avoir un mouvement qui rate la fin
                    if(newPosition.X>=t->positionFin.X-0.1f && newPosition.X<=t->positionFin.X+0.1f && newPosition.Y>=t->positionFin.Y-0.1f && newPosition.Y<=t->positionFin.Y+0.1f && newPosition.Z>=t->positionFin.Z-0.1f && newPosition.Z<=t->positionFin.Z+0.1f)
                    {
                        arrVehicules[i]->setPosition(t->positionFin);
                        arrVehicules[i]->pas=0;
                        //printf("Changement troncon perche %i\n",i);
                        //On passe au troncon suivant, sauf si c est le dernier
                        if(t->numero<arrTroncons.size()-1) {
                            arrVehicules[i]->numeroTroncon++;
                        } else {
                            arrVehicules[i]->numeroTroncon=0;
                        }

                        //Si on entre en gare, il faut ajouter la perche a la gare
                        if(arrVehicules[i]->numeroTroncon==1) {
                            addPercheInGare(arrVehicules[i]);
                        }
                    }
                }

                //Demi cercle autour de la poulie d'arrivée
                if(t->GetType()==CTronconRM::TRONCON_GARETKD)
                {
                    core::vector3df lastPosition=arrVehicules[i]->getPosition();
                    //printf("lastPosition = %f ; %f ; %f\n",lastPosition.X,lastPosition.Y,lastPosition.Z);
                    //printf("positionFin = %f ; %f ; %f\n",t->positionFin.X,t->positionFin.Y,t->positionFin.Z);
                    core::vector3df difference=t->positionFin-t->positionDebut;
                    f32 longueur=difference.getLength();

                    core::vector3df vecPosPerche;
                    if(arrVehicules[i]->placeGareTKD>-1) {
                        arrVehicules[i]->pas+=deltaTime*vitesse/longueur*2;
                        if(arrVehicules[i]->pas>=0.9-(arrVehicules[i]->placeGareTKD*0.09)) {
                            arrVehicules[i]->pas=0.9-(arrVehicules[i]->placeGareTKD*0.09);
                            vecPosPerche.X=t->positionDebut.X+difference.X*arrVehicules[i]->pas;
                            vecPosPerche.Y=t->positionDebut.Y+difference.Y*arrVehicules[i]->pas;
                            vecPosPerche.Z=t->positionDebut.Z+difference.Z*arrVehicules[i]->pas;
                        }
                        vecPosPerche.X=t->positionDebut.X+difference.X*arrVehicules[i]->pas;
                        vecPosPerche.Y=t->positionDebut.Y+difference.Y*arrVehicules[i]->pas;
                        vecPosPerche.Z=t->positionDebut.Z+difference.Z*arrVehicules[i]->pas;

                        arrVehicules[i]->setPosition(vecPosPerche);
                    }
                }

                //Demi cercle autour de la poulie d'arrivée
                if(t->GetType()==CTronconRM::TRONCON_POULIEARRIVEE)
                {
                    core::vector3df lastPosition=arrVehicules[i]->getPosition();
                    core::vector3df lastRotation=arrVehicules[i]->getRotation();

                    //printf("getRotation-perche %i => %f ; %f ; %f\n",i,lastRotation.X,lastRotation.Y,lastRotation.Z);

                    core::vector3df vecteurRayon=contactPoulieArriveeD->getPosition()-contactPoulieArriveeM->getPosition();
                    f32 rayon=(vecteurRayon.getLength()/2)+1.4f;
                    //core::vector3df centreCercle=posMonteePoulieArrivee->getPosition()+(posDescentePoulieArrivee->getPosition()-posMonteePoulieArrivee-> getPosition())/2.0f;

                    //arrVehicules[i]->pas+=0.005f;
                    arrVehicules[i]->pas+=(vitesse*deltaTime*5.0f)/(core::PI*rayon);
                    f32 angle=core::PI-arrVehicules[i]->pas;
                    //f32 angle=arrVehicules[i]->pas-core::PI;

                    //Rotation de la perche
                    core::quaternion rot(lastRotation * core::DEGTORAD);
                    core::quaternion toAdd;
                    toAdd.fromAngleAxis(-(vitesse*deltaTime*5.0f)/(core::PI*rayon), core::vector3df(0.0f,1.0f,0.0f));
                    //toAdd.fromAngleAxis((vitesse*deltaTime*5.0f)/(core::PI*rayon), core::vector3df(0.0f,1.0f,0.0f));
                    rot *= toAdd;

                    core::vector3df r;
                    rot.toEuler(r);
                    arrVehicules[i]->setRotation(r * core::RADTODEG);


                    core::vector3df vecPosPyl;
                    f32 diffY=posDescentePoulieArrivee->getAbsolutePosition().Y-posMonteePoulieArrivee->getAbsolutePosition().Y;
                    f32 deltaY=diffY*arrVehicules[i]->pas/core::PI;
                    vecPosPyl.X=t->centreRotation.X+rayon*cos(angle-(nodeDepart->getRotation().Y*core::PI/180.0f)+core::PI/2.0f);
                    vecPosPyl.Y=t->centreRotation.Y+deltaY-(diffY/2.0f);
                    vecPosPyl.Z=t->centreRotation.Z+rayon*sin(angle-(nodeDepart->getRotation().Y*core::PI/180.0f)+core::PI/2.0f);
                    //printf("Rotation-perche %i angle=%f => %f ; %f ; %f\n",i,angle,vecPosPyl.X,vecPosPyl.Y,vecPosPyl.Z);
                    arrVehicules[i]->setPosition(vecPosPyl);



                    //Verifie si on a atteint la fin du troncon. On se laisse une marge pour eviter d'avoir un mouvement qui rate la fin
                    //if(angle>=0)
                    if(angle<=0)
                    {
                        arrVehicules[i]->setPosition(t->positionFin);
                        arrVehicules[i]->pas=0;
                        arrVehicules[i]->setRotation(core::vector3df(0.0f,nodeDepart->getRotation().Y+180.0f,0.0f));
                        //printf("Changement troncon poulie arrivee perche %i\n",i);
                        //On passe au troncon suivant, sauf si c est le dernier
                        if(t->numero<arrTroncons.size()-1) {
                            arrVehicules[i]->numeroTroncon++;
                        } else {
                            arrVehicules[i]->numeroTroncon=0;
                        }
                    }
                }

                //Demi cercle autour de la poulie de départ
                if(t->GetType()==CTronconRM::TRONCON_POULIEDEPART)
                {
                    core::vector3df lastPosition=arrVehicules[i]->getPosition();
                    core::vector3df lastRotation=arrVehicules[i]->getRotation();

                    //printf("getRotation-perche %i => %f ; %f ; %f\n",i,lastRotation.X,lastRotation.Y,lastRotation.Z);

                    core::vector3df vecteurRayon=posDescentePoulieDepart->getPosition()-posMonteePoulieDepart->getPosition();
                    f32 rayon=(vecteurRayon.getLength()/2.0f)+0.6f;

                    //arrVehicules[i]->pas+=0.005f;
                    arrVehicules[i]->pas+=(vitesse*deltaTime*4.0f)/(core::PI*rayon);

                    core::vector3df vecPosPyl;
                    f32 angle=core::PI-arrVehicules[i]->pas;
                    /*vecPosPyl.X=t->centreRotation.X+rayon*cos(angle-(nodeDepart->getRotation().Y*core::PI/180.0f)-core::PI/2.0f);
                    vecPosPyl.Y=t->centreRotation.Y;
                    vecPosPyl.Z=t->centreRotation.Z+rayon*sin(angle-(nodeDepart->getRotation().Y*core::PI/180.0f)-core::PI/2.0f);*/

                    vecPosPyl.X=t->centreRotation.X+rayon*cos(-angle-(nodeDepart->getRotation().Y*core::PI/180.0f)+core::PI/2.0f);
                    vecPosPyl.Y=t->centreRotation.Y-0.2f;
                    vecPosPyl.Z=t->centreRotation.Z+rayon*sin(-angle-(nodeDepart->getRotation().Y*core::PI/180.0f)+core::PI/2.0f);

                    //printf("Rotation-perche %i angle=%f => %f ; %f ; %f\n",i,angle,vecPosPyl.X,vecPosPyl.Y,vecPosPyl.Z);
                    arrVehicules[i]->setPosition(vecPosPyl);

                    //Rotation de la perche
                    core::quaternion rot(lastRotation * core::DEGTORAD);
                    core::quaternion toAdd;
                    toAdd.fromAngleAxis((vitesse*deltaTime*4.0f)/(core::PI*rayon), core::vector3df(0.0f,1.0f,0.0f));
                    rot *= toAdd;

                    core::vector3df r;
                    rot.toEuler(r);
                    arrVehicules[i]->setRotation(r * core::RADTODEG);

                     //Verifie si on a atteint la fin du troncon. On se laisse une marge pour eviter d'avoir un mouvement qui rate la fin
                    if(angle<=0)
                    {
                        arrVehicules[i]->setPosition(t->positionFin);
                        arrVehicules[i]->pas=0;
                        arrVehicules[i]->setRotation(core::vector3df(0.0f,nodeDepart->getRotation().Y,0.0f));
                        //printf("Changement troncon poulie arrivee perche %i\n",i);
                        //On passe au troncon suivant, sauf si c est le dernier
                        if(t->numero<arrTroncons.size()-1) {
                            arrVehicules[i]->numeroTroncon++;
                        } else {
                            arrVehicules[i]->numeroTroncon=0;
                        }
                    }
                }

            } else {
                printf("ERREUR GetTronconByNumero for CTKFixe t->0\n");
            }
        }
    } else {
        if(constructionFinie && soundMoteur->isPlaying()) {
            soundMoteur->stop();
        }
    }
}

//Fonction qui met à jour chaque troncon du RM, c'est ce qui permet de connaitre
// pour chacun de ces tronons le point de départ et le point d'arrivée
void CTKDebrayable::UpdateTroncons()
{
    if(!nodePoulieDepart || !nodePoulieArrivee) {
        RedrawCable();
        return;
    }

    core::vector3df deltaPoulieM;
    core::vector3df deltaPoulieD;

    arrTroncons.clear();
    //Cas particulier si j'ai pas de pylone
    if(arrPylones.size()==0)
    {
        printf("------------------------\n");
        CTronconRM* tronconDep=new CTronconRM();
        tronconDep->numero=0;
        tronconDep->SetType(CTronconRM::TRONCON_POULIEDEPART);
        tronconDep->centreRotation=nodePoulieDepart->getAbsolutePosition();
        arrTroncons.push_back(tronconDep);
        printf("Troncon 0 POULIEDEPART %f %f %f\n",tronconDep->centreRotation.X,tronconDep->centreRotation.Y,tronconDep->centreRotation.Z);

        CTronconRM* tronconLigneGare=new CTronconRM();
        tronconLigneGare->numero=1;
        tronconLigneGare->SetType(CTronconRM::TRONCON_GARETKD);
        tronconLigneGare->positionDebut=posMonteePoulieDepart->getAbsolutePosition();
        tronconLigneGare->positionFin=posPoulieDepartMontee->getAbsolutePosition();
        tronconLigneGare->enMontee=true;
        arrTroncons.push_back(tronconLigneGare);
        printf("Troncon 1 TRONCON_GARETKD deb %f %f %f et fin %f %f %f\n",tronconLigneGare->positionDebut.X,tronconLigneGare->positionDebut.Y,tronconLigneGare->positionDebut.Z,tronconLigneGare->positionFin.X,tronconLigneGare->positionFin.Y,tronconLigneGare->positionFin.Z);

        CTronconRM* tronconLigneMontee=new CTronconRM();
        tronconLigneMontee->numero=2;
        tronconLigneMontee->SetType(CTronconRM::TRONCON_LIGNE);
        tronconLigneMontee->positionDebut=posPoulieDepartMontee->getAbsolutePosition();
        tronconLigneMontee->positionFin=posMonteePoulieArrivee->getAbsolutePosition();
        tronconLigneMontee->enMontee=true;
        arrTroncons.push_back(tronconLigneMontee);
        printf("Troncon 2 TRONCON_LIGNE deb %f %f %f et fin %f %f %f\n",tronconLigneMontee->positionDebut.X,tronconLigneMontee->positionDebut.Y,tronconLigneMontee->positionDebut.Z,tronconLigneMontee->positionFin.X,tronconLigneMontee->positionFin.Y,tronconLigneMontee->positionFin.Z);


        CTronconRM* tronconPoulieArr=new CTronconRM();
        tronconPoulieArr->numero=3;
        tronconPoulieArr->SetType(CTronconRM::TRONCON_LIGNE);
        tronconPoulieArr->positionDebut=posMonteePoulieArrivee->getAbsolutePosition();
        tronconPoulieArr->positionFin=contactPoulieArriveeM->getAbsolutePosition();
        tronconPoulieArr->enMontee=true;
        arrTroncons.push_back(tronconPoulieArr);
        printf("Troncon 3 TRONCON_LIGNE deb %f %f %f et fin %f %f %f\n",tronconPoulieArr->positionDebut.X,tronconPoulieArr->positionDebut.Y,tronconPoulieArr->positionDebut.Z,tronconPoulieArr->positionFin.X,tronconPoulieArr->positionFin.Y,tronconPoulieArr->positionFin.Z);


        CTronconRM* tronconArr=new CTronconRM();
        tronconArr->numero=4;
        tronconArr->SetType(CTronconRM::TRONCON_POULIEARRIVEE);
        tronconArr->centreRotation=nodePoulieArrivee->getAbsolutePosition();
        arrTroncons.push_back(tronconArr);
        printf("Troncon 4 POULIEARRIVEE %f %f %f\n",tronconArr->centreRotation.X,tronconArr->centreRotation.Y,tronconArr->centreRotation.Z);


        CTronconRM *tronconPoulieArrD=new CTronconRM();
        tronconPoulieArrD->numero=5;
        tronconPoulieArrD->SetType(CTronconRM::TRONCON_LIGNE);
        tronconPoulieArrD->positionDebut= contactPoulieArriveeD->getAbsolutePosition();
        tronconPoulieArrD->positionFin=posDescentePoulieArrivee->getAbsolutePosition();
        tronconPoulieArrD->enMontee=false;
        arrTroncons.push_back(tronconPoulieArrD);
        printf("Troncon 5 TRONCON_LIGNE deb %f %f %f et fin %f %f %f\n",tronconPoulieArrD->positionDebut.X,tronconPoulieArrD->positionDebut.Y,tronconPoulieArrD->positionDebut.Z,tronconPoulieArrD->positionFin.X,tronconPoulieArrD->positionFin.Y,tronconPoulieArrD->positionFin.Z);


        CTronconRM *tronconLigneDescente=new CTronconRM();
        tronconLigneDescente->numero=6;
        tronconLigneDescente->SetType(CTronconRM::TRONCON_LIGNE);
        tronconLigneDescente->positionDebut=posDescentePoulieArrivee->getAbsolutePosition();
        tronconLigneDescente->positionFin=posPoulieDepartDescente->getAbsolutePosition();
        tronconLigneDescente->enMontee=false;
        arrTroncons.push_back(tronconLigneDescente);
        printf("Troncon 6 TRONCON_LIGNE deb %f %f %f et fin %f %f %f\n",tronconLigneDescente->positionDebut.X,tronconLigneDescente->positionDebut.Y,tronconLigneDescente->positionDebut.Z,tronconLigneDescente->positionFin.X,tronconLigneDescente->positionFin.Y,tronconLigneDescente->positionFin.Z);


        CTronconRM *tronconLigneDescente2=new CTronconRM();
        tronconLigneDescente2->numero=7;
        tronconLigneDescente2->SetType(CTronconRM::TRONCON_LIGNE);
        tronconLigneDescente2->positionDebut=posPoulieDepartDescente->getAbsolutePosition();
        tronconLigneDescente2->positionFin=posDescentePoulieDepart->getAbsolutePosition();
        tronconLigneDescente2->enMontee=false;
        arrTroncons.push_back(tronconLigneDescente2);
        printf("Troncon 7 TRONCON_LIGNE deb %f %f %f et fin %f %f %f\n",tronconLigneDescente2->positionDebut.X,tronconLigneDescente2->positionDebut.Y,tronconLigneDescente2->positionDebut.Z,tronconLigneDescente2->positionFin.X,tronconLigneDescente2->positionFin.Y,tronconLigneDescente2->positionFin.Z);


    } else { //Sinon c'est qu'il y a au moins 1 pylone
        printf("--ALERTE TRONCON--\n");
        CTronconRM* tronconDep=new CTronconRM();
        tronconDep->numero=0;
        tronconDep->SetType(CTronconRM::TRONCON_POULIEDEPART);
        tronconDep->centreRotation=nodePoulieDepart->getAbsolutePosition();
        arrTroncons.push_back(tronconDep);
        printf("------------------------\n");
        printf("troncon %i POULIE Depart\n",tronconDep->numero);

        //u32 nbTr=4+2*arrPylones.size();
        core::vector3df v2;
        core::vector3df v1;
        core::vector3df v2last;
        core::vector3df v1last;

        //Pour la montée
        for(unsigned int i=0;i<arrPylones.size();i++)
        {

            CTronconRM* tronconPM=new CTronconRM();
            tronconPM->numero=i+1;
            tronconPM->SetType(CTronconRM::TRONCON_LIGNE);

            if(i==0) {
                tronconPM->positionDebut=posMonteePoulieDepart->getAbsolutePosition();
                tronconPM->positionFin=arrPylones[0]->posMonteePylone->getAbsolutePosition();
                v1=nodePoulieDepart->getAbsolutePosition();
                if(arrPylones.size()>1)
                {
                    v2=arrPylones[1]->posMonteePylone->getAbsolutePosition();
                } else {
                    v2=nodePoulieArrivee->getAbsolutePosition();
                }
                arrPylones[0]->monteeSupportCompression=CompressionOrSupport(v1,arrPylones[0]->posMonteePylone->getAbsolutePosition(),v2);
                //Ajuste selon compression/support
                if(arrPylones[0]->monteeSupportCompression==1)
                {
                    deltaPoulieM=core::vector3df(0.0f,0.4f,0.0f);
                    printf("P0 support\n");
                } else {
                    deltaPoulieM=core::vector3df(0.0f,-0.4f,0.0f);
                    printf("P0 compression\n");
                }
                tronconPM->positionFin=arrPylones[0]->posMonteePylone->getAbsolutePosition()+deltaPoulieM;

            } else {
                if(i<arrPylones.size()-1) {

                    tronconPM->positionDebut=arrPylones[i-1]->posMonteePylone->getAbsolutePosition();
                    if(arrPylones[i-1]->monteeSupportCompression==1)
                    {
                        deltaPoulieM=core::vector3df(0.0f,0.4f,0.0f);
                    } else {
                        deltaPoulieM=core::vector3df(0.0f,-0.4f,0.0f);
                    }
                    tronconPM->positionDebut=arrPylones[i-1]->posMonteePylone->getAbsolutePosition()+deltaPoulieM;

                    tronconPM->positionFin=arrPylones[i]->posMonteePylone->getAbsolutePosition();

                    if(i>=arrPylones.size()-1) {
                        //tronconPM->positionFin=posMonteePoulieArrivee->getAbsolutePosition();
                        v2=posMonteePoulieArrivee->getAbsolutePosition();
                    } else {
                        //tronconPM->positionFin=arrPylones[i+1]->posMonteePylone->getAbsolutePosition();
                        v2=arrPylones[i+1]->posMonteePylone->getAbsolutePosition();
                    }

                    v1=arrPylones[i-1]->posMonteePylone->getAbsolutePosition();
                    arrPylones[i]->monteeSupportCompression=CompressionOrSupport(v1,arrPylones[i]->posMonteePylone->getAbsolutePosition(),v2);
                    //printf("P%i angle montee compression=%i\n",i,arrPylones[i]->monteeSupportCompression);
                    //Ajuste selon compression/support
                    if(arrPylones[i]->monteeSupportCompression==1)
                    {
                        deltaPoulieM=core::vector3df(0.0f,0.4f,0.0f);
                        printf("Pi support\n");
                    } else {
                        deltaPoulieM=core::vector3df(0.0f,-0.4f,0.0f);
                        printf("Pi compression\n");
                    }
                    tronconPM->positionFin=arrPylones[i]->posMonteePylone->getAbsolutePosition()+deltaPoulieM;
                } else {
                    //dernier troncon
                    tronconPM->positionDebut=arrPylones[i-1]->posMonteePylone->getAbsolutePosition();
                    if(arrPylones[i-1]->monteeSupportCompression==1)
                    {
                        deltaPoulieM=core::vector3df(0.0f,0.4f,0.0f);
                    } else {
                        deltaPoulieM=core::vector3df(0.0f,-0.4f,0.0f);
                    }
                    tronconPM->positionDebut=arrPylones[i-1]->posMonteePylone->getAbsolutePosition()+deltaPoulieM;
                    tronconPM->positionFin=arrPylones[i]->posMonteePylone->getAbsolutePosition();
                    v2=nodePoulieArrivee->getAbsolutePosition();
                    if(arrPylones.size()<2)
                    {
                        v1=nodePoulieDepart->getAbsolutePosition();
                    } else {
                        v1=arrPylones[i-1]->posMonteePylone->getAbsolutePosition();
                    }
                    //arrPylones[i]->monteeSupportCompression=CompressionOrSupport(v1,arrPylones[i]->posMonteePylone->getAbsolutePosition(),v2);
                    arrPylones[i]->monteeSupportCompression=CompressionOrSupport(v1,arrPylones[i]->posMonteePylone->getAbsolutePosition(),v2);
                    //Ajuste le cable aux poulies
                    if(arrPylones[i]->monteeSupportCompression==1)
                    {
                        deltaPoulieM=core::vector3df(0.0f,0.4f,0.0f);
                        printf("Pi-1 support\n");
                    } else {
                        deltaPoulieM=core::vector3df(0.0f,-0.4f,0.0f);
                        printf("Pi-1 compression\n");
                    }
                    tronconPM->positionFin=arrPylones[i]->posMonteePylone->getAbsolutePosition()+deltaPoulieM;
                }

            }
            printf("troncon %i LIGNE montee debut = %f ; %f ; %f  fin = %f ; %f ; %f\n",tronconPM->numero,tronconPM->positionDebut.X,tronconPM->positionDebut.Y,tronconPM->positionDebut.Z,tronconPM->positionFin.X,tronconPM->positionFin.Y,tronconPM->positionFin.Z);
            tronconPM->enMontee=true;
            arrTroncons.push_back(tronconPM);

        }

        //dernier troncon
        //if(i==arrPylones.size()-1) {
        CTronconRM* tronconPM2=new CTronconRM();
        //tronconPM2->numero=i+2;
        tronconPM2->numero=arrPylones.size()+1;
        tronconPM2->SetType(CTronconRM::TRONCON_LIGNE);
        //tronconPM2->positionDebut=arrPylones[i]->posMonteePylone->getAbsolutePosition();
        tronconPM2->positionDebut=arrPylones[arrPylones.size()-1]->posMonteePylone->getAbsolutePosition();
        tronconPM2->positionFin=posMonteePoulieArrivee->getAbsolutePosition();
        v2=nodePoulieArrivee->getAbsolutePosition();
        if(arrPylones.size()<2)
        {
            v1=nodePoulieDepart->getAbsolutePosition();
        } else {
            v1=arrPylones[arrPylones.size()-2]->posMonteePylone->getAbsolutePosition();
        }
        //arrPylones[i]->monteeSupportCompression=CompressionOrSupport(v1,arrPylones[i]->posMonteePylone->getAbsolutePosition(),v2);
        arrPylones[arrPylones.size()-1]->monteeSupportCompression=CompressionOrSupport(v1,arrPylones[arrPylones.size()-1]->posMonteePylone->getAbsolutePosition(),v2);
        //Ajuste selon compression/support
        //if(arrPylones[i]->monteeSupportCompression==1)
        if(arrPylones[arrPylones.size()-1]->monteeSupportCompression==1)
        {
            deltaPoulieM=core::vector3df(0.0f,0.4f,0.0f);
            printf("P last support\n");
        } else {
            deltaPoulieM=core::vector3df(0.0f,-0.4f,0.0f);
            printf("P last compression\n");
        }
        //tronconPM2->positionDebut=arrPylones[i]->posMonteePylone->getAbsolutePosition()+deltaPoulieM;
        tronconPM2->positionDebut=arrPylones[arrPylones.size()-1]->posMonteePylone->getAbsolutePosition()+deltaPoulieM;
        tronconPM2->enMontee=true;
        printf("troncon %i LIGNE montee debut = %f ; %f ; %f  fin = %f ; %f ; %f\n",tronconPM2->numero,tronconPM2->positionDebut.X,tronconPM2->positionDebut.Y,tronconPM2->positionDebut.Z,tronconPM2->positionFin.X,tronconPM2->positionFin.Y,tronconPM2->positionFin.Z);
        arrTroncons.push_back(tronconPM2);
        //}

        //Pour la poulie retour
        CTronconRM* tronconArr=new CTronconRM();
        tronconArr->numero=arrPylones.size()+2;
        tronconArr->SetType(CTronconRM::TRONCON_POULIEARRIVEE);
        tronconArr->centreRotation=nodePoulieArrivee->getAbsolutePosition();
        arrTroncons.push_back(tronconArr);

        printf("troncon %i POULIE Arrivee\n",tronconArr->numero);

        //Pour la descente
        int cptDes=arrPylones.size()-1;
        u32 currentNumero=arrPylones.size()+2;

        while(cptDes>=0) {
            u32 idnext=findNextPyloneWithDescente(cptDes);
            u32 idprevious=findPreviousPyloneWithDescente(cptDes);

            CTronconRM* tronconD=new CTronconRM();
            tronconD->numero=currentNumero+1;
            tronconD->SetType(CTronconRM::TRONCON_LIGNE);
            //si je traite le 1er pylone
            if(cptDes==arrPylones.size()-1) {
                tronconD->positionDebut=posDescentePoulieArrivee->getAbsolutePosition();
                v1=nodePoulieArrivee->getAbsolutePosition();
                if(arrPylones[cptDes]->GetType()==CPyloneRM::PYLONE_TKD_MONTEE_DESCENTE) {
                    tronconD->positionFin=arrPylones[cptDes]->posDescentePylone->getAbsolutePosition();
                    u32 idnext2=findNextPyloneWithDescente(cptDes);
                    if(idnext2==-1) {
                        v2=nodePoulieDepart->getAbsolutePosition();
                    } else {
                        v2=arrPylones[idnext2]->posDescentePylone->getAbsolutePosition();
                    }
                    arrPylones[cptDes]->descenteSupportCompression=CompressionOrSupport(v1,arrPylones[cptDes]->posDescentePylone->getAbsolutePosition(),v2);
                    //Ajuste selon compression/support
                    if(arrPylones[cptDes]->descenteSupportCompression==1)
                    {
                        deltaPoulieD=core::vector3df(0.0f,0.4f,0.0f);
                    } else {
                        deltaPoulieD=core::vector3df(0.0f,-0.4f,0.0f);
                    }
                    tronconD->positionFin=arrPylones[cptDes]->posDescentePylone->getAbsolutePosition()+deltaPoulieD;
                } else {
                    u32 idnext2=findNextPyloneWithDescente(cptDes);
                    if(idnext2==-1) {
                        tronconD->positionFin=posDescentePoulieDepart->getAbsolutePosition();
                        cptDes=-1;
                    } else {
                        tronconD->positionFin=arrPylones[idnext2]->posDescentePylone->getAbsolutePosition();
                        u32 idnext3=findNextPyloneWithDescente(idnext2);
                        if(idnext3==-1) {
                            v2=nodePoulieDepart->getAbsolutePosition();
                        } else {
                            v2=arrPylones[idnext3]->posDescentePylone->getAbsolutePosition();
                        }
                        arrPylones[idnext2]->descenteSupportCompression=CompressionOrSupport(v1,arrPylones[idnext2]->posMonteePylone->getAbsolutePosition(),v2);
                        //Ajuste selon compression/support
                        if(arrPylones[idnext2]->descenteSupportCompression==1)
                        {
                            deltaPoulieD=core::vector3df(0.0f,0.4f,0.0f);
                        } else {
                            deltaPoulieD=core::vector3df(0.0f,-0.4f,0.0f);
                        }
                        tronconD->positionFin=arrPylones[idnext2]->posDescentePylone->getAbsolutePosition()+deltaPoulieD;
                        cptDes=idnext2;
                        continue;
                    }

                }


            } else {
                //sinon pylone normal
                if(arrPylones[cptDes]->GetType()==CPyloneRM::PYLONE_TKD_MONTEE_DESCENTE) {
                    if(idprevious==-1) {
                        tronconD->positionDebut=posDescentePoulieArrivee->getAbsolutePosition();
                        v1=posDescentePoulieArrivee->getAbsolutePosition();
                    } else {
                        tronconD->positionDebut=arrPylones[idprevious]->posDescentePylone->getAbsolutePosition();
                        if(arrPylones[idprevious]->descenteSupportCompression==1)
                        {
                            deltaPoulieD=core::vector3df(0.0f,0.4f,0.0f);
                        } else {
                            deltaPoulieD=core::vector3df(0.0f,-0.4f,0.0f);
                        }
                        tronconD->positionDebut=arrPylones[idprevious]->posDescentePylone->getAbsolutePosition()+deltaPoulieD;
                        v1=arrPylones[idprevious]->posDescentePylone->getAbsolutePosition();
                    }

                    if(idnext==-1) {
                         v2=posDescentePoulieDepart->getAbsolutePosition();
                    } else {
                         v2=arrPylones[idnext]->posDescentePylone->getAbsolutePosition();
                    }

                    tronconD->positionFin=arrPylones[cptDes]->posDescentePylone->getAbsolutePosition();

                    arrPylones[cptDes]->descenteSupportCompression=CompressionOrSupport(v1,arrPylones[cptDes]->posDescentePylone->getAbsolutePosition(),v2);
                    //Ajuste selon compression/support
                    if(arrPylones[cptDes]->descenteSupportCompression==1)
                    {
                        deltaPoulieD=core::vector3df(0.0f,0.4f,0.0f);
                    } else {
                        deltaPoulieD=core::vector3df(0.0f,-0.4f,0.0f);
                    }
                    tronconD->positionFin=arrPylones[cptDes]->posDescentePylone->getAbsolutePosition()+deltaPoulieD;
                } else {
                    if(idprevious==-1) {
                        tronconD->positionDebut=posDescentePoulieArrivee->getAbsolutePosition();
                        v1=nodePoulieArrivee->getAbsolutePosition();
                    } else {
                        tronconD->positionDebut=arrPylones[idprevious]->posDescentePylone->getAbsolutePosition();

                        u32 idprevious2=findPreviousPyloneWithDescente(idprevious);
                        if(idprevious2==-1) {
                            v1=nodePoulieArrivee->getAbsolutePosition();
                        } else {
                            v1=arrPylones[idprevious2]->posDescentePylone->getAbsolutePosition();
                            if(arrPylones[idprevious2]->descenteSupportCompression==1)
                            {
                                deltaPoulieD=core::vector3df(0.0f,0.4f,0.0f);
                            } else {
                                deltaPoulieD=core::vector3df(0.0f,-0.4f,0.0f);
                            }
                            tronconD->positionDebut=arrPylones[idprevious2]->posDescentePylone->getAbsolutePosition()+deltaPoulieD;
                        }

                    }

                    if(idnext==-1) {
                        tronconD->positionFin=posDescentePoulieDepart->getAbsolutePosition();
                        v2=nodePoulieDepart->getAbsolutePosition();
                        cptDes=-1;
                        continue;
                    } else {
                        tronconD->positionFin=arrPylones[idnext]->posDescentePylone->getAbsolutePosition();
                        u32 idnext2=findNextPyloneWithDescente(idnext);

                        if(idnext2==-1) {
                           v2=nodePoulieDepart->getAbsolutePosition();
                        } else {
                           v2=arrPylones[idnext2]->posDescentePylone->getAbsolutePosition();
                        }

                        arrPylones[idnext]->descenteSupportCompression=CompressionOrSupport(v1,arrPylones[idnext]->posDescentePylone->getAbsolutePosition(),v2);
                        //Ajuste selon compression/support
                        if(arrPylones[idnext]->descenteSupportCompression==1)
                        {
                            deltaPoulieD=core::vector3df(0.0f,0.4f,0.0f);
                        } else {
                            deltaPoulieD=core::vector3df(0.0f,-0.4f,0.0f);
                        }
                        tronconD->positionFin=arrPylones[idnext]->posDescentePylone->getAbsolutePosition()+deltaPoulieD;
                        cptDes=idnext;
                        continue;

                    }

                }
            }

            if(cptDes==0) { //Sinon si je traite le dernier pylone
                CTronconRM* tronconD2=new CTronconRM();
                tronconD2->numero=currentNumero+2;
                tronconD2->SetType(CTronconRM::TRONCON_LIGNE);

                if(arrPylones[cptDes]->GetType()==CPyloneRM::PYLONE_TKD_MONTEE_DESCENTE) {
                    tronconD2->positionDebut=arrPylones[cptDes]->posDescentePylone->getAbsolutePosition();
                    tronconD2->positionFin=posDescentePoulieDepart->getAbsolutePosition();
                    v2=nodePoulieDepart->getAbsolutePosition();
                    u32 idprevious2=findPreviousPyloneWithDescente(cptDes);
                    if(idprevious2==-1) {
                        v1=nodePoulieArrivee->getAbsolutePosition();
                    } else {
                        v1=arrPylones[idprevious2]->posDescentePylone->getAbsolutePosition();
                    }
                     arrPylones[cptDes]->descenteSupportCompression=CompressionOrSupport(v1,arrPylones[cptDes]->posDescentePylone->getAbsolutePosition(),v2);
                    //Ajuste selon compression/support
                    if(arrPylones[cptDes]->descenteSupportCompression==1)
                    {
                        deltaPoulieD=core::vector3df(0.0f,0.4f,0.0f);
                    } else {
                        deltaPoulieD=core::vector3df(0.0f,-0.4f,0.0f);
                    }
                    tronconD2->positionDebut=arrPylones[cptDes]->posDescentePylone->getAbsolutePosition()+deltaPoulieD;
                } else {
                    if(idprevious==-1) {
                        tronconD2->positionDebut=posDescentePoulieArrivee->getAbsolutePosition();
                        tronconD2->positionFin=posDescentePoulieDepart->getAbsolutePosition();
                        cptDes=-1;
                    } else {
                        tronconD2->positionDebut=arrPylones[idprevious]->posDescentePylone->getAbsolutePosition();
                        v2=nodePoulieDepart->getAbsolutePosition();
                        v1=arrPylones[idprevious]->posDescentePylone->getAbsolutePosition();
                        arrPylones[idprevious]->descenteSupportCompression=CompressionOrSupport(v1,arrPylones[idprevious]->posDescentePylone->getAbsolutePosition(),v2);
                        //Ajuste selon compression/support
                        if(arrPylones[idprevious]->descenteSupportCompression==1)
                        {
                            deltaPoulieD=core::vector3df(0.0f,0.4f,0.0f);
                        } else {
                            deltaPoulieD=core::vector3df(0.0f,-0.4f,0.0f);
                        }
                        tronconD2->positionDebut=arrPylones[idprevious]->posDescentePylone->getAbsolutePosition()+deltaPoulieD;
                        tronconD2->positionFin=posDescentePoulieDepart->getAbsolutePosition();

                        idprevious--;
                    }

                }
                printf("troncon %i LIGNE descente debut = %f ; %f ; %f  fin = %f ; %f ; %f\n",tronconD2->numero,tronconD2->positionDebut.X,tronconD2->positionDebut.Y,tronconD2->positionDebut.Z,tronconD2->positionFin.X,tronconD2->positionFin.Y,tronconD2->positionFin.Z);
                tronconD2->enMontee=false;
                arrTroncons.push_back(tronconD2);

            }

            printf("troncon %i LIGNE descente debut = %f ; %f ; %f  fin = %f ; %f ; %f\n",tronconD->numero,tronconD->positionDebut.X,tronconD->positionDebut.Y,tronconD->positionDebut.Z,tronconD->positionFin.X,tronconD->positionFin.Y,tronconD->positionFin.Z);
            tronconD->enMontee=false;
            arrTroncons.push_back(tronconD);

            currentNumero++;
            cptDes--;
        }


        printf("------------------------\n");
        RedrawCable();
    }
}

//Permet de placer les perches sur le TK
void CTKDebrayable::PlaceVehicules()
{
    //La première perche commence toujours au troncon 1 (le 0 etant la poulie de départ)
    CTronconRM* t=GetTronconByNumero(1);

    CVehicule *perche=new CVehicule(smgr,driver);
    perche->SetType(CVehicule::VEHICULE_TKD);
    perche->numeroTroncon=1;
    perche->placeGareTKD=0;
    perche->pas=0;
    perche->DrawVehicule(t->positionDebut,core::vector3df(0.0f,nodeDepart->getRotation().Y,0.0f));
    //perche->DrawVehicule(posMonteePoulieDepart->getAbsolutePosition(),core::vector3df(0.0f,nodeDepart->getRotation().Y,0.0f));
    arrVehicules.push_back(perche);
    addPercheInGare(perche);

    /*CTronconRM* t=GetTronconByNumero(1);
    core::vector3df difference=t->positionFin-t->positionDebut;
    f32 longueur=difference.getLength();

    CVehicule *perche2=new CVehicule(smgr,driver);
    perche2->SetType(CVehicule::VEHICULE_TKD);
    perche2->numeroTroncon=2;
    perche2->placeGareTKD=-1;
    perche2->pas=0;
    core::vector3df vecPosPerche;
    vecPosPerche.X=(t->positionDebut.X+difference.X*perche2->pas);
    vecPosPerche.Y=(t->positionDebut.Y+difference.Y*perche2->pas);
    vecPosPerche.Z=(t->positionDebut.Z+difference.Z*perche2->pas);
    perche2->DrawVehicule(vecPosPerche,core::vector3df(0.0f,nodeDepart->getRotation().Y,0.0f));
    arrVehicules.push_back(perche2);*/
    //addPercheInGare(perche2);
    /*if(arrTroncons.size()>0) {

        f32 deltatroncon=0;
        for(unsigned int i=0;i<arrTroncons.size();i++){
            CTronconRM* tr=GetTronconByNumero(i);

            //On prend en compte que les troncons de type ligne
            if(tr->GetType()==CTronconRM::TRONCON_LIGNE) {
                core::vector3df difference=tr->positionFin-tr->positionDebut;
                f32 longueur=difference.getLength()-deltatroncon;
                u32 nbPerchesTr=(u32)(longueur/espacementVehicules);
                deltatroncon=longueur-nbPerchesTr*espacementVehicules;
                difference=difference.normalize();
                printf("troncon->%i nbperches=%i difference=%f ; %f ; %f\n",i,nbPerchesTr,difference.X,difference.Y,difference.Z);

                for(int j=1 ;j<=nbPerchesTr;j++)
                {
                    core::vector3df vecPosPerche;
                    //vecPosPerche.X=(tr->positionDebut.X+difference.X*j*20)-(difference.X*deltatroncon);
                    //vecPosPerche.Y=(tr->positionDebut.Y+difference.Y*j*20)-(difference.Y*deltatroncon);
                    //vecPosPerche.Z=(tr->positionDebut.Z+difference.Z*j*20)-(difference.Z*deltatroncon);

                    vecPosPerche.X=(tr->positionDebut.X+difference.X*j*espacementVehicules)-(difference.X*deltatroncon);
                    vecPosPerche.Y=(tr->positionDebut.Y+difference.Y*j*espacementVehicules)-(difference.Y*deltatroncon);
                    vecPosPerche.Z=(tr->positionDebut.Z+difference.Z*j*espacementVehicules)-(difference.Z*deltatroncon);

                    CVehicule *perche=new CVehicule(smgr,driver);
                    perche->SetType(CVehicule::VEHICULE_TKF);
                    //printf("perche %i ->setPosition %f,%f,%f troncon:%i\n",j,vecPosPerche.X,vecPosPerche.Y,vecPosPerche.Z,tr->numero);
                    perche->numeroTroncon=tr->numero;
                    //perche->pas=j*40/nbPerchesTr;
                    perche->pas=((j*espacementVehicules)-deltatroncon)/longueur;
                    if(tr->enMontee)
                    {
                        perche->DrawVehicule(vecPosPerche,core::vector3df(0.0f,nodeDepart->getRotation().Y,0.0f));
                    } else {
                        perche->DrawVehicule(vecPosPerche,core::vector3df(0.0f,nodeDepart->getRotation().Y+180.0f,0.0f));
                    }

                    //perche->setPosition(posMonteePoulieDepart->getAbsolutePosition());
                    arrVehicules.push_back(perche);
                }

            }
        }
    }*/

}

void CTKDebrayable::PlaySoundMoteur(bool play)
{
    if(play){
        soundMoteur = OpenSound(deviceA.get(), "media/tkf-sound.mp3", true);
        if (!soundMoteur)
        {
            printf("Erreur chargement son moteur TKF\n");
            return;
        } else {
            soundMoteur->setRepeat(true);
            soundMoteur->setVolume(1.0f); // 50% volume
            soundMoteur->play();
        }
    } else {
        if (soundMoteur) {
            soundMoteur->stop();
        }
    }



}

CTKDebrayable::~CTKDebrayable()
{
    if(meshDepart!=0) {
        delete meshDepart;
        meshDepart=0;
    }

    if(nodeDepart!=0) {
        delete nodeDepart;
        nodeDepart=0;
    }

    if(meshArrivee!=0) {
        delete meshArrivee;
        meshArrivee=0;
    }

    if(nodeArrivee!=0) {
        delete nodeArrivee;
        nodeArrivee=0;
    }

    if(meshPoulieDepart!=0) {
        delete meshPoulieDepart;
        meshPoulieDepart=0;
    }

    if(nodePoulieDepart!=0) {
        delete nodePoulieDepart;
        nodePoulieDepart=0;
    }

    if(posMonteePoulieDepart!=0) {
        delete posMonteePoulieDepart;
        posMonteePoulieDepart=0;
    }

    if(posDescentePoulieDepart!=0) {
        delete posDescentePoulieDepart;
        posDescentePoulieDepart=0;
    }



}
