var ai_lasttime = Date.now();
var numberOfGoldMines = 0;
var numberOfSpawners = 0;
var numberOfTurrets = 0;
function updateAI()
{
    if ( b_paused ) return;
    
    if ( Date.now() > ai_lasttime+1000 )
    {
        if ( numberOfGoldMines < 2 )
        {
            aiBuildGoldMine();
        }
        else
        {
            if ( numberOfSpawners < 2 )
            {
                aiBuildSpawner();
            }
            else
            {
                // let the ai cheat a little bit
                aiPlayer.giveGold( 1 );

                if ( numberOfTurrets < 1 )
                {
                    aiBuildTurret();
                }
                else 
                {
                    // even more cheating now ;)
                    aiPlayer.giveGold( 1 );

                    if ( Math.random() <= 0.3 )
                    {
                        var r = Math.random();
                        if ( r <= 0.3 )
                        {
                            aiBuildGoldMine();
                        }
                        else if ( r <= 0.5 )
                        {
                            aiBuildSpawner();
                        }
                        else if ( r <= 0.7 )
                        {
                            aiBuildSpawnerWyvern();
                        }
                        else if ( r <= 0.9 )
                        {
                            aiBuildSpawnerArcher();
                        }
                        else 
                        {
                            aiBuildTurret();
                        }
                    }
                }
            }
        }
        ai_lasttime = Date.now();
    }
}



function restartAI()
{
    numberOfGoldMines = 0;
    numberOfSpawners = 0;
    numberOfTurrets = 0;
}



function aiBuildGoldMine()
{
    if ( aiPlayer.gold >= GOLDMINE_PRICE )
    {
        // find a position in the back row to build a gold mine
        var x = 23;
        var z = Math.floor( Math.random()*10 );

        if ( !map.isObstructed( x , z ) )
        {
            constructBuilding( x , z , aiPlayer , GOLDMINE , scene );
            ++numberOfGoldMines;
        }
    }
}



function aiBuildSpawner()
{
    if ( aiPlayer.gold >= SPAWNER_PRICE )
    {
        var x = 18+Math.floor( Math.random()*6 );
        var z = Math.floor( Math.random()*10 );

        if ( !map.isObstructed( x , z ) )
        {
            constructBuilding( x , z , aiPlayer , SPAWNER , scene );
            ++numberOfSpawners;
        }
    }
}



function aiBuildSpawnerWyvern()
{
    if ( aiPlayer.gold >= FLYING_SPAWNER_PRICE )
    {
        var x = 18+Math.floor( Math.random()*6 );
        var z = Math.floor( Math.random()*10 );

        if ( !map.isObstructed( x , z ) )
        {
            constructBuilding( x , z , aiPlayer , FLYING_SPAWNER , scene );
            ++numberOfSpawners;
        }
    }
}



function aiBuildSpawnerArcher()
{
    if ( aiPlayer.gold >= ARCHER_SPAWNER_PRICE )
    {
        var x = 18+Math.floor( Math.random()*6 );
        var z = Math.floor( Math.random()*10 );

        if ( !map.isObstructed( x , z ) )
        {
            constructBuilding( x , z , aiPlayer , ARCHER_SPAWNER , scene );
            ++numberOfSpawners;
        }
    }
}



function aiBuildTurret()
{
    if ( aiPlayer.gold >= TURRET_PRICE )
    {
        var x = 18+Math.floor( Math.random()*4 );
        var z = 2+Math.floor( Math.random()*6 );

        if ( !map.isObstructed( x , z ) )
        {
            constructBuilding( x , z , aiPlayer , TURRET , scene );
            ++numberOfTurrets;
        }
    }
}