/* 
    Neural Network Simulator

    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License
    as published by the Free Software Foundation; either version 2
    of the License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

    (C) 2006 Jason Hunt
    nulluser@gmail.gom 
*/


#include <windows.h>

#include "utility.h"
#include "plant.h"
#include "world.h"
#include "console.h"

extern world_type world;


/* Add a plant to the world */
void add_plant(double x, double y, double x_size, double y_size)
{
    plant_type *t = world.plants;
    
    world.plants = (plant_type *) malloc(sizeof(plant_type));
    
    if (world.plants == NULL)
    {
        write_console("Unable to get plant memory\n");
        while(1);
    }
    
    world.plants->x = x;
    world.plants->y = y;
    
    world.plants->x_size = x_size;
    world.plants->y_size = y_size;

    world.plants->next = t;
}
/* End of add_plant */


/* Add a random plant */
void add_random_plant()
{
    add_plant(f_rand(world.plant_size, world.x_size - world.plant_size), 
              f_rand(world.plant_size, world.y_size - world.plant_size),
              world.plant_size, world.plant_size);
}
/* End of add_random_plant */


/* Draw all plants to the dc */
void draw_plants(HDC dc)
{
    plant_type *p = world.plants;

    SelectObject(dc, world.green_brush);
    SelectObject(dc, world.green_pen);
    
    while (p != NULL)
    {
        RECT r;
        get_bounding_rect(p->x, p->y, p->x_size, p->y_size, r);

        Ellipse(dc, r.left, r.top, r.right, r.bottom);                
                
        p = p->next;            
    }
}
/* End of draw plants */


/* Remove a plant */
void remove_plant(plant_type *p)
{
    add_random_plant();

    plant_type *t = world.plants, *prev = NULL;
    
    // Only one node
    if (t->next == NULL)
    {
        free(t);
        world.plants = NULL;
        return;                                        
    }
    
    // First node
    if (t == p)
    {
        world.plants = t->next;        
        free(t);
        return;            
    }
        
    // Interior node    
    while (t != NULL)
    {
        if (t == p)
        {
            prev->next = t->next;
            free(t);
            return;
        }
        prev = t;
        t= t->next;
    }    
}
/* End of remove plant */




