/*************************************************************************/
/***************** W I N M E S S A G E ( ) *******************************/
/*************************************************************************/
function winmessage() {
/**************************/
/* (c)2001 www.diaware.de */
    if( gewonnen != 0 || parseInt(document.display.level.value) < 6 || parseInt(document.display.points.value) < 30000) {
        alert("das spiel ist vorbei!\nsie haben " + document.display.points.value + " punkte erreicht.\n\n" +
        "hinweis: ab erreichen der stufe 6 und mind. 30000 punkten können\nsie sich " +
        "in die bestenliste eintragen lassen.")
        return;
     }
     gewonnen = 1;
     text1 = "rohre";
     text2 = document.display.points.value;
     text3 = document.display.level.value;
     text4 = "";
     // Nun den Namen erfragen un bei OK das Formular abschicken lassen
     oeffneListenFenster();
}

/******************************************************/
/* Funktionen die per Events vom Spielfeld direkt aufgerufen werden */
/******************************************************/
function displayPipe(x,y) {
         if( !checkBeginn ) return;
         if( getxy(x,y) != 0 ) { wasEmpty = false; return; }
         wasEmpty = true;
         gsetxy(x,y,getImg(actPipe));
}

function removePipe(x,y) {
         if( !checkBeginn ) return;
         if( isSet ) { isSet = false; return; }
         if( !wasEmpty ) return;
         gsetxy(x,y,getImg(0));
}

function setPipe(x,y) {
         if( !checkBeginn ) return;
         if( !wasEmpty ) return;
         wasEmpty = false;
         isSet = true;
         /* Bild wurde gesetzt, deshalb neuen Stein auswählen */
         if( checkConnection() ) {
            window.clearTimeout(timeTimeout);
            nextLevel();
         } else {
            setNextPipe();
         }
}

function displayBin(x,y) {
         if( !checkBeginn ) return;
         gsetBin(getImg(actPipe));
}

function removeBin(x,y) {
         if( !checkBeginn ) return;
         gsetBin(getImg(1));
}

/******************************************************/
/* Funktionen für sonstige Logik */
/******************************************************/

function init() {
/**************************/
/* (c)2001 www.diaware.de */
         if( checkBeginn ) endGame();
         checkBeginn = true;
         document.display.points.value = 0;
         document.display.points2.value = 0;
         oldPoints = 0;
         document.display.tries.value = 3;
         document.display.tries2.value = 3;
         window.clearTimeout(timeTimeout);
         gewonnen = 0;
         setLevel(1);
         return;
}

function checkConnection() {
         /* teste ob verbindung besteht */
         /* wenn ja nächstes Level */
         /* beginne im anfangspunkt und suche bis es nicht weiter geht, ist das gleich dem
         endpunkt, ist verbindung ok. die zwischenstationen werden mitgezählt. entspricht
         das der anzahl der eingebauten zwischenstationen ist kommt nächstes level */
         actLevel = parseInt(document.display.level.value)
         actL = allLevel[actLevel - 1];
         /* Startposition ermitteln */
         countZS = 0; countPipes = 0; isEnd = false
         IMGs = actL[0]; Xs = actL[1]; Ys = actL[2];
         DIRs = getImgCon(IMGs);
         if( Xs <= 0 )         DIRs &= (LINKS^0xF);
         if( Xs >= (C_COL-1) ) DIRs &= (RECHTS^0xF);
         if( Ys <= 0 )         DIRs &= (OBEN^0xF);
         if( Ys >= (C_ROW-1) ) DIRs &= (UNTEN^0xF);
         do {
            again = false;
            if( Xs == actL[4] && Ys == actL[5] ) { isEnd = true; break; }
            /* modifiziere x und y in weitere richtung */
            if( DIRs == LINKS ) Xs -= 1;
            if( DIRs == RECHTS ) Xs += 1;
            if( DIRs == OBEN ) Ys -= 1;
            if( DIRs == UNTEN ) Ys += 1;
            num = getxy(Xs,Ys);
            if( num == 9 || num == 10 ) countZS++;
            countPipes++;
            DIRn = getImgCon(num);
            if( DIRs == LINKS && (DIRn & RECHTS) ) { again = true; DIRs = DIRn & (RECHTS^0xF); }
            if( DIRs == RECHTS && (DIRn & LINKS) ) { again = true; DIRs = DIRn & (LINKS^0xF); }
            if( DIRs == OBEN && (DIRn & UNTEN) ) { again = true; DIRs = DIRn & (UNTEN^0xF); }
            if( DIRs == UNTEN && (DIRn & OBEN) ) { again = true; DIRs = DIRn & (OBEN^0xF); }
         } while( again == true );
         //alert( Xs + ", " + Ys + ": " + DIRs + " --- " + isEnd + ": " + countZS)
         if( isEnd ) {
            anzZS = (actL.length - 8) / 3;
            if( anzZS < countZS ) return true;
         }
         return false;
}

function nextLevel() {
         window.clearTimeout(timeTimeout);
         actLevel = parseInt(document.display.level.value);
         actTime = parseInt(document.display.time.value);
         actPoints = parseInt(document.display.points.value) - oldPoints;
         sumPoints = actPoints + (countPipes * 125) + (actTime * 25) + (actLevel * 250)
         alert("" +
"glückwunsch! sie haben stufe " + actLevel + " geschafft.\n" +
"\n" +
"punkte in dieser stufe: " +  actPoints + "\n" +
"bonus für rohrleitung: " + (countPipes * 125) + " (" + countPipes +" Rohre je 125 Punkte)\n" +
"zeitbonus: " + (actTime * 25) + " (" + actTime +"s je 25 Punkte)\n" +
"bonus für stufe: " + (actLevel * 250) + "\n" +
"\n" +
" gesamt für diese stufe: " + sumPoints)

         oldPoints += sumPoints;
         document.display.points.value = oldPoints;
         document.display.points2.value = oldPoints;
         actLevel++;
         if( actLevel < 1 || actLevel > allLevel.length ) {
            endGame();
         } else {
            setLevel(actLevel)
         }
}

function setLevel(l) {
         l = parseInt(l);
         alert(" S T U F E   " + l );
         document.display.points.value = oldPoints
         document.display.points2.value = oldPoints
         /* Baue Spielfeld für Level l auf */
         newL = allLevel[l-1]
         /* Spielfeld löschen */
         maxPics = C_ROW * C_COL;
         for( I=0; I < maxPics; I++) document.images[firstField + I].src=leer.src;
         /* Start zeichnen */
         setxy(newL[1],newL[2],newL[0]);
         /* Ende zeichnen */
         setxy(newL[4],newL[5],newL[3]);
         /* Zwischen-Positionen zeichnen */
         anzZPos = (newL.length - 8);
         for( tt=0; tt < anzZPos; tt+=3) {
              setxy(newL[tt+9],newL[tt+10],newL[tt+8]);
         }
         /* Löcher per Zufall zeichnen */
         anzRandom = newL[6];
         for( tt=0; tt < anzRandom; tt++ ) {
             for( dummy = 0; dummy < 20; dummy++ ) {
               ranX = Math.round(Math.random() * (C_COL-1) )
               ranY = Math.round(Math.random() * (C_ROW-1) )
               //alert( ranX + ", " + ranY +": "+getxy(ranX, ranY));
               if( getxy(ranX, ranY) != 0 ) continue;
               if( getCon(ranX, ranY) > 0 ) continue;
               setxy(ranX,ranY,11);
               break;
             }
         }
         /* Zeit setzen */
         document.display.time.value = newL[7];
         document.display.time2.value = newL[7];
         timeTimeout = window.setTimeout("incTime()",1000);
         document.display.level.value = l;
         document.display.level2.value = l;
         setNextPipe();
         return;
}

function recyclePipe() {
         setNextPipe();
         gsetBin(allImg[actPipe]);
         incPoints(-75)
}

function setNextPipe() {
         if( nextPipe == 0) {
           nextPipe = randomPipe();
           actPipe = randomPipe();
         } else {
           actPipe = nextPipe;
           nextPipe = randomPipe();
         }
         gsetPrev(0,allImg[nextPipe]);
         gsetPrev(1,allImg[actPipe]);
         incPoints(50)
}

function randomPipe() {
         /* per Zufall Wert von 3 bis 8 zurückgeben */
         return ( Math.round(Math.random() * 5) + 3 ) ;
}

function endGame() {
         if( !checkBeginn ) return;
         /* Spiel ist zu ende, notwendige Schritte wie winmessage einleiten */
         window.clearTimeout(timeTimeout);
         checkBeginn = false;
         winmessage();
         return;
}

function incPoints(p) {
         actPoints = parseInt(document.display.points.value);
         document.display.points.value = actPoints + p;
         document.display.points2.value = actPoints + p;
}

function incTime() {
         if( ! checkBeginn ) return;
         actTime = parseInt(document.display.time.value);
         if( actTime <= 0 ) {
            window.setTimeout("timeOver()",0) ;
         } else {
            document.display.time.value = actTime-1;
            document.display.time2.value = actTime-1;
            timeTimeout = window.setTimeout("incTime()",1000);
         }
}

function timeOver() {
         if( !checkBeginn ) return;
         window.clearTimeout(timeTimeout);
         if( incTries() ) {
           alert("die zeit ist leider abgelaufen!\nsie können noch " +
           (parseInt(document.display.tries.value)+parseInt("1")) +
           " mal versuchen.\n\ndrücken sie ok um es erneut zu versuchen.")
           window.setTimeout("setLevel("+document.display.level.value+")",0);
         }
}

function incTries() {
         actTries = parseInt(document.display.tries.value)
         if( actTries <= 0 ) {
             triesOver();
             return false;
         }
         document.display.tries.value = actTries - 1;
         document.display.tries2.value = actTries - 1;
         return true;
}

function triesOver() {
         window.clearTimeout(timeTimeout);
         alert("leider sind alle versuche aufgebraucht!\ndas spiel ist jetzt zu ende")
         endGame();
         return;
}

function getImgCon(num) {
         if( num < 0 || num >= imgCon.length ) return 0;
         return imgCon[num];
}

function getCon(x,y) {
         /* liefert 4 Bit zurück, die Auskunft überAnschluss geben */
         /* Format: Binär => 00000lrou */
         /* Abfrage mit: if( (Value & 2) > 1 ) then "oben ist anschluss" */
         binPattern = 0;
         /* schaue nach links, d.h. dort muss rechts eine Verbindung mögl. sein */
         if( (getImgCon(getxy(x-1,y)) & RECHTS) > 0 ) binPattern += LINKS;
         /* schaue nach rechts, d.h. dort muss links eine Verbindung mögl. sein */
         if( (getImgCon(getxy(x+1,y)) & LINKS) > 0 ) binPattern += RECHTS;
         /* schaue nach oben, d.h. dort muss nach unten eine Verbindung mögl. sein */
         if( (getImgCon(getxy(x,y-1)) & UNTEN) > 0 ) binPattern += OBEN;
         /* schaue nach unten, d.h. dort muss nach oben eine Verbindung mögl. sein */
         if( (getImgCon(getxy(x,y+1)) & OBEN) > 0 ) binPattern += UNTEN;
         return binPattern;
}

function getImg(num) {
         if( num < 0 || num >= allImg.length ) return leer;
         return allImg[num];
}

function setxy(x,y,num) {
         gsetxy(x,y,allImg[num]);
}

function gsetxy(x,y,img) {
     /* setzt ein bild an die stelle (x,y) (0,0) ist oben links (xmax,ymax) ist unten rechts */
    if( x < 0 || x > (C_COL-1) || y < 0 || y > (C_ROW-1) ) return;
     my_offset = parseInt(x) + parseInt(y) * parseInt(C_COL);
     document.images[firstField + my_offset].src=img.src;
}

function gsetBin(img) {
     /* setzt ein bild an die stelle (x,y) (0,0) ist oben links (xmax,ymax) ist unten rechts */
     document.images[firstBinField].src=img.src;
}

function gsetPrev(num,img) {
     /* setzt ein bild an die stelle (x,y) (0,0) ist oben links (xmax,ymax) ist unten rechts */
     document.images[firstPrevField+num].src=img.src;
}

function gsetPreview(img) {
     /* setzt ein bild an die stelle (x,y) (0,0) ist oben links (xmax,ymax) ist unten rechts */
     document.images[firstPreviewField].src=img.src;
}

function getxy(x,y) {
    /* schaue ob stelle (x,y) frei ist, falls außerhalb des Spielfeldes, wird dies */
    /* ebenfalls als besetzt angezeigt */
    /* 0 = frei; > 0 = besetzt; -1 = außerhalb; */
    if( x < 0 || x > (C_COL-1) || y > (C_ROW-1) || y < 0 ) return -1;
    my_offset = parseInt(x) + parseInt(y) * parseInt(C_COL);

    if( document.images[firstField + my_offset].src.indexOf(leerImg) != -1 ) return 0;
    if( document.images[firstField + my_offset].src.indexOf(pfeilImg) != -1 ) return 1;
    if( document.images[firstField + my_offset].src.indexOf(stricheImg) != -1 ) return 2;
    if( document.images[firstField + my_offset].src.indexOf(loImg) != -1 ) return 3;
    if( document.images[firstField + my_offset].src.indexOf(luImg) != -1 ) return 4;
    if( document.images[firstField + my_offset].src.indexOf(roImg) != -1 ) return 5;
    if( document.images[firstField + my_offset].src.indexOf(ruImg) != -1 ) return 6;
    if( document.images[firstField + my_offset].src.indexOf(lrImg) != -1 ) return 7;
    if( document.images[firstField + my_offset].src.indexOf(ouImg) != -1 ) return 8;
    if( document.images[firstField + my_offset].src.indexOf(alrImg) != -1 ) return 9;
    if( document.images[firstField + my_offset].src.indexOf(aouImg) != -1 ) return 10;
    if( document.images[firstField + my_offset].src.indexOf(lochImg) != -1 ) return 11;
    return -1;
}
