/*
    ANTLand simulation
    (C) 2004 justin_sane

    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.

    Contact: justinsanemsu@msu.edu

    FILE: obstacleunit.cpp
    Authors: justin_sane
*/


#pragma hdrstop

#include "world.h"
#include "landsimmain.h"
#include "obstacle.h"
#include "draw.h"
#include "point.h"
#include "utility.h"
#include "collide.h"

#pragma package(smart_init)


/* adds a random obstacle */
obstacle_class::obstacle_class(vector<obstacle_class> &obstacles, vector<ant_class *> &ants)
{
    int x_size = 1;  int y_size = 1;
    int x_pos = - 1; int y_pos =  -1;

    color = random_color();

    //look for a landing site
    while (grid_area_blocked(get_point(x_pos - x_size, y_pos - y_size), obstacles, ants) ||
           grid_area_blocked(get_point(x_pos + x_size, y_pos - y_size), obstacles, ants) ||
           grid_area_blocked(get_point(x_pos - x_size, y_pos + y_size), obstacles, ants) ||
           grid_area_blocked(get_point(x_pos + x_size, y_pos + y_size), obstacles, ants))

    {
        x_size = rvalplus(3) + 1;
        y_size = rvalplus(3) + 1;

        x_pos = rvalplus(sim_x_size - x_size) + x_size;
        y_pos = rvalplus(sim_y_size - y_size) + y_size;
    }

    upper_left.x = x_pos - x_size;
    upper_left.y = y_pos + y_size;

    upper_left  = get_point(x_pos - x_size,  y_pos + y_size);
    lower_right = get_point(x_pos + x_size,  y_pos - y_size);

    refresh_screen_points();
}
/* End of random_obstacle */


/*  calculate the screen cords */
void obstacle_class::refresh_screen_points( void )
{
    screen_upper_left  = get_intpoint(grid_to_screen_x(upper_left.x), grid_to_screen_y(upper_left.y));
    screen_lower_right = get_intpoint(grid_to_screen_x(lower_right.x), grid_to_screen_y(lower_right.y));
}
/* end of refresh_screen_points */


/* returns true if a point is inside the ocstacle */
bool __fastcall obstacle_class::point_inside(point p)
{
    return(p.x >= upper_left.x  && p.x <= lower_right.x &&
           p.y >= lower_right.y && p.y <= upper_left.y);
}
/* end of point_inside */



/* draw this obstacle to a buffer */
void __fastcall obstacle_class::draw(Graphics::TBitmap *buffer)
{
    buffer->Canvas->Brush->Color = color;

    buffer->Canvas->Rectangle(TRect(screen_upper_left.x,  screen_upper_left.y,
                                    screen_lower_right.x, screen_lower_right.y));

    buffer->Canvas->Brush->Color = clWhite;
}
/* end of draw */

