<?php

/* =============================================================================
Ogame lib by SpideR <spider312@free.fr> - http://spiderou.net

Used on : http://ogame.spiderou.net/

Released under license : 
    WTFPL - Do What The Fuck You Want To Public License
    http://sam.zoy.org/wtfpl/

Very early begining     of the lib, under active developpement, DON'T USE IT
============================================================================= */

// =========================================================== [ CONSTANTS ] ===
if ( ! defined('limit') )
    
define('limit',5000) ; // Limit 

// ============================================================= [ CLASSES ] ===
// ----------------------------------------------------------- [ BUILDINGS ] ---
class ressources {
    
/*
    Manages ressources in ogame, permitting some simple operation like setting,
    getting, math operations, and displaying
    */
    // Constructor
    
function ressources($m=0,$c=0,$d=0) {
        
$this->$m ;
        
$this->$c ;
        
$this->$d ;
    }
    
// theorically private
    
function format($value) {
        
$int floor($value) ;
        
$suf '' ;
        if ( 
$int limit ) {
            
$int round($int/1000,1) ;
            
$suf 'k' ;
            if ( 
$int limit ) {
                
$int round($int/1000,1) ;
                
$suf 'M' ;
                if ( 
$int $lim ) {
                    
$int round($int/1000,1) ;
                    
$suf 'G' ;
                }
            }
        }
        if ( 
$suf != '' )
            
$suf ' '.$suf ;
        return 
$int.$suf ;
    }
    
// theorically public
    
function display() {
        
/*
        return for current object, a string for display of ressources contained by
        the object
        */
        
$return='' ;
        if ( 
$this->)
            
$return .= $this->format($this->m).'M' ;
        if ( 
$this->)
            
$return .= ($return=='')?'':', '.$this->format($this->c).'C' ;
        if ( 
$this->)
            
$return .= ($return=='')?'':', '.$this->format($this->d).'D' ;
        return 
$return ;
    }
}
// ----------------------------------------------------------- [ BUILDINGS ] ---
class batiment {
    
/*
    Manages buildings
    On object of this type is used to get prices and productions of any one 
    building type in ogame, but bust be initialised with correct values
    */
    // Constructor
    
function batiment($name="noname",$price=null,$rate=2,$e=0,$prod_h=null) {
        
/*
        $price and $prod must be "ressources" object, or null
        */
        
$this->name $name ;
        if ( 
$price != null )
            
$this->price[1] = $price ;
        else
            
$this->price[1] = new ressources() ;
        
$this->rate $rate ;
        if ( 
$prod_h != null )
            
$this->prod_h[1] = $prod_h ;
        else
            
$this->prod_h[1] = new ressources() ;
        
$this->$e ;
    }
    
// Private ?
    
function multiply($ress=null,$rate=1) {
        if ( 
$ress == null )
            return new 
ressources ;
        else
            return new 
ressources($ress->m*$rate,$ress->c*$rate,$ress->d*$rate) ;
    }
    
// Public ?
    
function get_price($lvl=1) {
        
/*
        Get a "ressources" object representing the price of a level of this type
        of building
        */
        
if ( ! ( $lvl ) )
            return new 
ressources() ;
        else
            if ( isset(
$this->price[$lvl]) )
                return 
$this->price[$lvl] ;
            else {
                
$this->price[$lvl] = $this->multiply($this->get_price($lvl-1),$this->rate) ;
                return 
$this->price[$lvl] ;
            }
    }
    function 
get_prod($lvl=1) {
        
/*
        Get a "ressources" object representing the production of a level of this 
        type of building
        */        
if ( ! ( $lvl ) )
            return new 
ressources() ;
        else
            if ( isset(
$this->prod_h[$lvl]) )
                return 
$this->prod_h[$lvl] ;
            else {
                
$base $this->get_prod(1) ;
                
$this->prod_h[$lvl] = new ressources($base->m*$lvl*pow(1.1,$lvl),$base->c*$lvl*pow(1.1,$lvl),$base->d*$lvl*pow(1.1,$lvl)) ;
                return 
$this->prod_h[$lvl] ;
            }
    }
}
// =========================================================== [ FUNCTIONS ] ===
function some_buildings() {
    
/*
    Initialise some buildings with default values
    Returns an array of "batiment" objects
    */
    
$return['mm'] = new batiment('Mine de métal',new ressources(60,15),1.5,-10,new ressources(30)) ;
    
$return['mc'] = new batiment('Mine de cristal',new ressources(48,24),1.6,-10,new ressources(0,20)) ;
    
$return['md'] = new batiment('Synthétiseur de deutérium',new ressources(225,75),1.5,-20,new ressources(0,0,10)) ;
    
$return['cs'] = new batiment('Centrale électrique solaire',new ressources(75,30),1.5,20,new ressources()) ;
    
$return['cf'] = new batiment('Centrale électrique de fusion',new ressources(900,360,180),1.8,50,new ressources(0,0,-10)) ;
    
$return['n']  = new batiment('Usine de nanites',new ressources(1000000,500000,100000));
    return 
$return ;
}
function 
hangarcapacity($lvl=0) {
    
/*
    Gets the level of a hangar (int)
    Returns capacity of that level of hangar
    */
    
if ( $lvl )
        return 
100000 50000 floor(pow(1.6,$lvl)) ;
    else 
        return 
100000 ;
}
// --- [ Display ] ---
    // Colorize in green >0 in red <0
function colorize($int) {
    function 
subvalue($int) {
        if ( 
$int )
            
$str .= 'pos' ;
        elseif ( 
$int )
            
$str .= 'neg' ;
        else
            
$str .= 'nul' ;
        return 
$str ;
    }
    return 
' <span class="'.subvalue($int).'">('.$int.')</span>' ;
}
// ---------------------------------------------------------------- [ Time ] ---
function disp_time($mins=0) {
    
/*
    Gets a time in minutes (int)
    Returns a string formating this value in '00d 00h 00min' format
    */
    
$mins intval($mins) ;
    
$only_mins $mins 60 ;
    
$time_disp $only_mins 'min' ;
    
$hours = ( $mins $only_mins ) / 60 ;
    if ( 
$hours ) {
        
$only_hours $hours 24 ;
        
$time_disp $only_hours 'h ' $time_disp ;
    }
    
$days = ( $hours $only_hours ) / 24 ;
    if ( 
$days )
        
$time_disp $days 'd ' $time_disp ;
    return 
$time_disp ;
}
// ------------------------------------------------------------ [ Location ] ---
function tempbylocation($loc=1) {
    
/*
    Gets the location of a planet in his system (int 1-15)
    Returns the theorical temperature (int) of that planet
    */
    
switch ($loc) {
        case 
1:
        case 
2:
        case 
3:
            return 
127 $loc ;
            break ;
        case 
4:
        case 
5:
        case 
6:
            return 
65 $loc ;
            break ;
        case 
7:
        case 
8:
        case 
9:
            return 
35 $loc ;
            break ;
        case 
10:
        case 
11:
        case 
12:
            return 
15 11 $loc ;
            break ;
        case 
13:
        case 
14:
        case 
15:
            return -
40 14 $loc ;
            break ;
        default:
            return 
;
    }
}
function 
deutratebytemp($temp=140) {
    
/*
    Gets the temperature of a planet (int)
    Returns the rate (float) applied to the deut production of that planet
    */
    
return 1.28 0.002 $temp ;
}
function 
solarsatprodbytemp($temp=-20) {
    
/*
    Gets the temperature of a planet (int)
    Returns the energy production (int) for one solar satellite on that planet
    */
    
return floor(20 $temp 4) ;
}
?>