/*
    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: utility.cpp
    Authors: justin_sane
*/

#include <fstream.h>
#include <math.h>

#pragma hdrstop

#include "landsimmain.h"
#include "utility.h"

#pragma package(smart_init)


/* add a debug string to a text file */
void debug(AnsiString s)
{
    filebuf fb;
    fb.open ("debug.txt", ios::out | ios::app);

    ostream os(&fb);

    os << s.c_str();

    fb.close();
}
/* end of debug */






/* create a random floating point number += range */
float rvalplus(float max)
{
    return ((rand() % 10000) / 10000.0 * max);
}
/* end of random float */


/* returns a random floating point of either sign * max */
float rval(float max)
{
    if (rand() % 2)
        return(((rand() % 10000) / 10000.0) * max);
    else
        return((-(rand() % 10000) / 10000.0) * max);
}
/* end of rval */



/* returns a point that is (forward) away form p in the directioon (angle) */
point __fastcall forward_vector(point p, float dist, float angle)
{
    point t;

    t.x = p.x + dist * fast_cos((int)angle);
    t.y = p.y + dist * fast_sin((int)angle);

    return(t);
}
/* end of forward vector */



/* returns the sine of an interger angle from the lookup table */
float __fastcall fast_sin(int a)
{
    // range check the lower bound
    if (a < 0) a += 360;

    return (sin_table[a % 360]);
}
/* end of fast_sin */



/* returns the cosine of an interger angle from the lookup table */
float __fastcall fast_cos(int a)
{
    // range check the lower bound
    if (a < 0) a += 360;

    return (cos_table[a % 360]);
}
/* end of fast_cos */



/* create a lookup table for the trig funtions */
void make_trig_table( void )
{
    for (int i = 0; i < 360; i++)
    {
        sin_table[i] = sin(i * 3.141582 / 180.0);
        cos_table[i] = cos(i * 3.141582 / 180.0);
    }
}
/* end of make trig table */


/* return a random color */
TColor random_color( void )
{
    int n = rand() % 5;

    if (n == 0) return ((TColor)RGB(10, 10, 130)); else
    if (n == 1) return ((TColor)RGB(10, 10, 150)); else
    if (n == 2) return ((TColor)RGB(10, 10, 170)); else
    if (n == 3) return ((TColor)RGB(10, 10, 190)); else
    if (n == 4) return ((TColor)RGB(10, 10, 210)); else
     return ((TColor)RGB(10, 10, 230));
}
/* end of random color */



