<?php

    /**
    * galaxy.php
    *
    * @version 1.4
    * @copyright 2008 by Chlorel for XNova
    * @Modifié par Winjet
    */

define('INSIDE' , true);
define('INSTALL' , false);
require_once dirname(__FILE__) .'/common.php';

    includeLang('galaxy');

    $CurrentPlanet = doquery("SELECT * FROM {{table}} WHERE `id` = '". $user['current_planet'] ."';", 'planets', true);
    $lunarow       = doquery("SELECT * FROM {{table}} WHERE `id` = '". $user['current_luna'] ."';", 'lunas', true);
    $galaxyrow     = doquery("SELECT * FROM {{table}} WHERE `id_planet` = '". $CurrentPlanet['id'] ."';", 'galaxy', true);

    $dpath         = (!$user["dpath"]) ? DEFAULT_SKINPATH : $user["dpath"];
    $fleetmax      = $user['computer_tech'] + 1;
    $CurrentPlID   = $CurrentPlanet['id'];
    $CurrentMIP    = $CurrentPlanet['interplanetary_misil'];
    $CurrentRC     = $CurrentPlanet['recycler'];
    $CurrentSP     = $CurrentPlanet['spy_sonde'];
    $HavePhalanx   = $CurrentPlanet['phalanx'];
    $CurrentSystem = $CurrentPlanet['system'];
    $CurrentGalaxy = $CurrentPlanet['galaxy'];
    $CanDestroy    = $CurrentPlanet[$resource[213]] + $CurrentPlanet[$resource[214]];

    $maxfleet       = doquery("SELECT * FROM {{table}} WHERE `fleet_owner` = '". $user['id'] ."';", 'fleets');
    $maxfleet_count = mysql_num_rows($maxfleet);

    CheckPlanetUsedFields($CurrentPlanet);
    CheckPlanetUsedFields($lunarow);

    // Imperatif, dans quel mode suis-je (pour savoir dans quel etat j'ere)
    if (!isset($mode)) {
        if (isset($_GET['mode'])) {
            $mode          = intval($_GET['mode']);
        } else {
            // ca ca sent l'appel sans parametres a plein nez
            $mode          = 0;
        }
    }

    if ($mode == 0) {
        // On vient du menu
        // Y a pas de parametres de passé
        // On met ce qu'il faut pour commencer là ou l'on se trouve

        $galaxy        = $CurrentPlanet['galaxy'];
        $system        = $CurrentPlanet['system'];
        $planet        = $CurrentPlanet['planet'];
    } elseif ($mode == 1) {
        // On vient du selecteur de galaxie
        // Il nous poste :
        // $_POST['galaxy']      => Galaxie affichée dans la case a saisir
        // $_POST['galaxyLeft']  => <- A ete cliqué
        // $_POST['galaxyRight'] => -> A ete cliqué
        // $_POST['system']      => Systeme affiché dans la case a saisir
        // $_POST['systemLeft']  => <- A ete cliqué
        // $_POST['systemRight'] => -> A ete cliqué

        if ($_POST["galaxyLeft"])
                $galaxy = intval($_POST["galaxy"] - 1);
        elseif ($_POST["galaxyRight"])
                $galaxy = intval($_POST["galaxy"] + 1);
        else
            $galaxy = intval($_POST["galaxy"]);

        if ($_POST["systemLeft"])
            $system = intval($_POST["system"] - 1);
        elseif ($_POST["systemRight"])
            $system = intval($_POST["system"] + 1);
        else
            $system = intval($_POST["system"]);
            
    } elseif ($mode == 2) {
        // Mais c'est qu'il mordrait !
        // A t'on idée de vouloir lancer des MIP sur ce pauvre bonhomme !!

        $galaxy        = intval($_GET['galaxy']);
        $system        = intval($_GET['system']);
        $planet        = intval($_GET['planet']);
    } elseif ($mode == 3) {
        // Appel depuis un menu avec uniquement galaxy et system de passé !
        $galaxy        = intval($_GET['galaxy']);
        $system        = intval($_GET['system']);
    } else {
        // Si j'arrive ici ...
        // C'est qu'il y a vraiment eu un bug
        $galaxy        = 1;
        $system        = 1;
    }
    
    if ($galaxy <= 1)
        $galaxy = 1;
    if ($galaxy > MAX_GALAXY_IN_WORLD)
        $galaxy = MAX_GALAXY_IN_WORLD;
        
    if ($system <= 1)
        $system = 1;
    if ($system > MAX_SYSTEM_IN_GALAXY)
        $system = MAX_SYSTEM_IN_GALAXY;
        
    if ($planet <= 1)
        $planet = 1;
    if ($planet > MAX_PLANET_IN_SYSTEM)
        $planet = MAX_PLANET_IN_SYSTEM;

    $page  = InsertGalaxyScripts ( $CurrentPlanet );
    $page .= ShowGalaxySelector ( $galaxy, $system );    
    $page .=  ShowGalaxyFooter ( $galaxy, $system,  $CurrentMIP, $CurrentRC, $CurrentSP);
    $page .= "<th><img src=\"images/galaxy/espace.gif\" border=\"4\" height=\"570\" width=\"600\"></th>";

    $query = doquery("SELECT * FROM {{table}} WHERE `galaxy` = '$galaxy' AND `system` = '$system' AND planet >= '1' AND planet <= '16' AND planet_type =  '1' ORDER BY planet;", 'planets');
    $id = 1;
    while($row = mysql_fetch_assoc($query))
    {        
        while ($id != $row['planet'])
        {
            $PlaneteOccupee[$id - 1] = "Libre";
            $PlaneteID[$id - 1] = $galaxy.':'.$system.':'.$id;
            $Owner[$id - 1] = -1;
            $PlaneteImage[$id - 1] = '';
            $id++;
        }
        $PlaneteOccupee[$id - 1] = $row['name'];
        $PlaneteID[$id - 1] = $galaxy.':'.$system.':'.$id;
        $Owner[$id - 1] = $row['id_owner'];
        $PlaneteImage[$id - 1] = $row['image'];
        $id++;

        
    }
    
    // Nécessaire pour forcer l'affichage des planètes non colonisées
    // si la dernière planète n'a pas la dernière position
    
    // Exemple :
    // Si la position de la dernière planète renvoyée est [1:2:9]
    // La première boucle While s'arrêtera après la planète 1:2:9
    // car il n'y aura plus aucune planète habitée après donc inexistant
    // dans la base de données.
    
    // Comme on souhaite afficher les 15 planètes, il faut faire une boucle
    // jusqu'à ce qu'on a atteigne la valeur 15 pour id et comme ces planètes
    // n'existent pas, il faut qu'elles soient considérés comme Libre pour être
    // colonisé.
    while ($id <= 15)
    {
        $PlaneteOccupee[$id - 1] = "Libre";
        $PlaneteID[$id - 1] = $galaxy.':'.$system.':'.$id;
        $Owner[$id - 1] = -1;
        $PlaneteImage[$id - 1] = '';
        $id++;
    }
    
    $query1 = doquery("SELECT * FROM {{table}} ORDER BY id;", 'users');
    
    while($row = mysql_fetch_assoc($query1))
    {    
        $id = 1;
        while ($id <= 16)
        {
            if ($Owner[$id - 1] != -1 && $Owner[$id - 1] == $row['id'])
            {
                if ($row['ally_id'] == 0)
                {
                    $Alliance[$id - 1] = '';
                } else {
                    $Alliance[$id - 1] = "Alliance: <a href=alliance.php?mode=ainfo&a=".$row['ally_id'].">".$row['ally_name']."</a><br />";
                }
                
                if ($row['authlevel'] == 3)
                {
                    $UserName[$id - 1] = "<font color=red>".$row['username']."</font>";
                } elseif ($row['authlevel'] == 2)
                {
                    $UserName[$id - 1] = "<font color=green>".$row['username']."</font>";
                }
                else
                {
                    $UserName[$id - 1] = $row['username'];
                }
                
                if ($row['id'] != $user['id'])
                {
                    $buddy[$id - 1] = "<a href=buddy.php?a=".$row['id']."&u=".$row['id_owner'].">Liste amis</a><br>";
                    $mail[$id - 1] = "<a href=messages.php?mode=write&id=".$row['id'].">Envoyer un message</a><br>";
                }
                else
                {
                    $buddy[$id - 1] = '';
                    $mail[$id - 1] = '';
                }
                $id++;
            }
            else
            {
                $id++;
            }
        }
    }
            
    $query2 = doquery("SELECT * FROM {{table}} WHERE `galaxy` = '$galaxy' AND `system` = '$system' ORDER BY planet;", 'galaxy');

    while($row = mysql_fetch_assoc($query2))
    {    
        $id = 1;
        while ($id <= 16)
        {
            if ($PlaneteID[$id - 1] == $row['galaxy'].':'.$row['system'].':'.$row['planet'])
            {
                if ($row['id_luna'] != '0')
                {
                    $PresenceLune[$id - 1] = "<a href=fleet.php?galaxy=$galaxy&system=$system&planet=$id&planettype=3&target_mission=9><font color=red>Destruction de lune</font></a><br>";
                    $LuneExistante[$id - 1] = 1;
                }
                else
                {
                    $PresenceLune[$id - 1] = "";
                    $LuneExistante[$id - 1] = 0;
                }
                
                if ($CurrentPlanet['planet_type'] == 3)
                        $Phalanger[$id - 1] = "<a href=phalanx.php?galaxy=$galaxy&system=$system&planet=$id><font color=yellow>Phalanger</font></a>";
                    else
                        $Phalanger[$id - 1] = "";
                
                if ($row['metal'] != 0 || $row['crystal'] != 0)
                    $Ressource[$id - 1] = "<a href=fleet.php?galaxy=$galaxy&system=$system&planet=$id&planettype=2&target_mission=8><font color=red>D&eacute;bris d&eacute;tect&eacute;</font></a><br>";
                $id++;
            }
            else
            {
                $id++;
            }
        }
    }
        
        
    $e = 0; $f = 1;
    //         01        02       03       04      05        06       07      08       09        10       11      12        13      14        15       16
    $pos  = "376;437; 447;410; 420;323; 533;498; 344;372; 187;450; 130;340; 555;400; 206;230; 351;483; ​440;300; 336;202; 276;300; 113;192; 496;240; 424;400;";

    $count = 0;

    while($count < 15)
    {
        $missiontime = explode(";", $pos);
        $pos1 = $missiontime[ $e ];
        $pos2 = $missiontime[ $f ];
        
        $nb = $count + 1;

        $page .= "<div id=\"$e\" style=\"z-index: 6; position: absolute; left: ".$pos1."px; top: ".$pos2."px; width: 10px; height: 10px; visibility: visible;\">";
        
        if ($PlaneteOccupee[$count] != "Libre" && $PlaneteOccupee[$count] != "")
        {
            $page .= "<th width=30><a style=\"cursor: crosshair;\" onmouseover='return overlib(\" <table width=250><tr><td class=c colspan=2>Planete ".$PlaneteOccupee[$count]." [".$PlaneteID[$count]."]</td></tr><tr><th width=80><img src=images/galaxy/sp".$count.".png height=48 width=48 /></th><th align=center>Pos. ".$nb."<br>Nom : ".$PlaneteOccupee[$count]."<br> Proprietaire : <a href=stat.php?who=player&start=".$Owner[$count]." title=Statistik alt=Statistik>".$UserName[$count]."</a><br>".$PresenceLune[$count]."".$Ressource[$count]."".$Alliance[$count]."".$buddy[$count]."".$mail[$count]."<hr><a href=fleet.php?galaxy=$galaxy&system=$system&planet=$nb&planettype=1&target_mission=4>Espionner</a><br /><a href=fleet.php?galaxy=$galaxy&system=$system&planet=$nb&planettype=1&target_mission=3>Transporter</a><br/><a href=fleet.php?galaxy=$galaxy&system=$system&planet=$nb&planettype=1&target_mission=1>Attaquer</a><br /><a href=galaxy.php?mode=2&galaxy=$galaxy&system=$system&planet=$nb&current=1>Mipper</a><br>".$Phalanger[$count]."</th></tr>  </table>\",STICKY, MOUSEOFF, DELAY, 100, CENTER, OFFSETX, -100, OFFSETY, -150 );' onmouseout='return nd();'>";
        }
        else
        {
            $page .= "<th width=30><a style=\"cursor: crosshair;\" onmouseover='return overlib(\" <table width=250><tr><td class=c colspan=2><center>Cette planete est libre <br>Position : ".$PlaneteID[$count]."</center></td></tr> <th align=center><hr><a href=fleet.php?galaxy=$galaxy&system=$system&planet=$count&planettype=1&target_mission=7>Coloniser</a></th></tr>  </table>\",STICKY, MOUSEOFF, DELAY, 750, CENTER, OFFSETX, -40, OFFSETY, -40 );' onmouseout='return nd();'>";
        }
        
        if ($PlaneteImage[$count] != '')
        {
            if ($LuneExistante[$count])
            {
                $page .= $UserName[$count]."<img src=images/galaxy/sp".$count.".png height=\"48\" width=\"48\"</img><img src=images/galaxy/mond1.png height=\"15\" width=\"15\"</img>$nb</th></div>";
            }
            else
            {
                $page .= $UserName[$count]."<img src=images/galaxy/sp".$count.".png height=\"48\" width=\"48\"</img>$nb</th></div>";
            }
        }
        else
        {
            $page .=" <img src=images/galaxy/sp".$count.".png height=\"48\" width=\"48\">Libre $nb</a></th></div>";
        }
        
        $page .= "<div id=\"99\" style=\" position: absolute; left: 10px; top: 160px; width: 20px; height: 10px;\"><th width=30><a style=\"cursor: crosshair;\" onmouseover='return overlib(\" <table width=250><tr><td class=c colspan=2><center>Trou Noir</center></td></tr> <th align=center><hr><a href=fleet.php?galaxy=$galaxy&system=$system&planet=16;planettype=1&target_mission=15>Explorer</a></th></tr>  </table>\",STICKY, MOUSEOFF, DELAY, 750, CENTER, OFFSETX, -40, OFFSETY, -40 );' onmouseout='return nd();'><img src=images/galaxy/nebuleuse.png height=\"100\" width=\"100\">Trou Noir</a></th></div>";
        $count++;
        $e = $e + 2;
        $f = $f + 2;
    }

    $page .= "<div id=\"99\" style=\"  left: 600px; top: 20px; width: 600px; height: 10px;\"><center><tbody>";    
        
    if ($mode == 2)
    {
        $CurrentPlanetID = $_GET['current'];
        $page .= ShowGalaxyMISelector ( $galaxy, $system, $planet, $CurrentPlanetID, $CurrentMIP );
    }
        
    $page .= "</center></tbody></table></div>";

    display ($page, $lang[''], false, '', false);

    // -----------------------------------------------------------------------------------------------------------
    // History version
    // 1.0 - Created by Perberos
    // 1.1 - Modified by -MoF- (UGamela germany)
    // 1.2 - 1er Nettoyage Chlorel ...
    // 1.3 - 2eme Nettoyage Chlorel ... Mise en fonction et debuging complet
    // 1.4 - 3eme Nettoyage Winjet     - Suppression de certaines lignes de codes inutiles
    //                                - Optimisation du code & Indentation du code
    //                                - Affichage des bonnes coordonnées dans ShowGalaxyTitles

?>