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


#include <vcl.h>
#include <fstream>
#include <vector.h>
#include <stdio.h>
#include <math.h>

#pragma hdrstop

#include "world.h"
#include "landsimmain.h"
#include "visualize.h"
#include "utility.h"
#include "draw.h"

#pragma package(smart_init)
#pragma resource "*.dfm"

Tmainform *mainform;

__fastcall Tmainform::Tmainform(TComponent* Owner) : TForm(Owner){}



/* called on startup, ensure a same system */
void __fastcall Tmainform::FormCreate(TObject *Sender)
{
    // clear all of the recorded key presses
    for (int i = 0; i < 255; i++)
       keys[i] = false;

    // create the lookup table
    make_trig_table();

    // seed the PSR
    randomize();

    // reset the simulation variables
    world.reset();

    // start the auto timer
    Timer1->Enabled = true;
}
/* end of formcreate */


/* called during program shutdown */
void __fastcall Tmainform::FormDestroy(TObject *Sender)
{
    // turn the timer off
    Timer1->Enabled = false;

    // free all of the world data
    world.clear();
}
/* end of form destroy */


/* called when the window is resized */
void __fastcall Tmainform::FormResize(TObject *Sender)
{
    // resize the draw area
    drawarea->Height = mainform->Height - 46 - drawarea->Top;
    drawarea->Width = mainform->Width - 8 - drawarea->Left;

    // tell the world that the drawarea was resized
    world.simarea_resize(drawarea->Width, drawarea->Height);
}
/* end of form resize */


/* called 50 times per second to update the sysem and display */
void __fastcall Tmainform::Timer1Timer(TObject *Sender)
{
    // update sim variabes from keyboard data
    check_keys();

    // update all of the world values
    world.update();

    // display the entire world
    draw_screen();
}
/* end of timer */


/* The drawing area was covered, we have to repaint */
void __fastcall Tmainform::drawareaPaint(TObject *Sender)
{
    draw_screen();
}
/* end of simareaPaint */


/* draw the entire screen */
void __fastcall Tmainform::draw_screen( void )
{
    world.draw_simarea(mainform->drawarea);
    world.draw_visarea(visform->drawarea);

    char stat[1024];
    world.get_sel_stat(stat);
    status->SimpleText = stat;
}
/* end of draw screen */




/***********************
** Keyboard functions **
***********************/


/* remember a keypress */
void __fastcall Tmainform::FormKeyDown(TObject *Sender, WORD &Key, TShiftState Shift)
{
    if (Key < 0xff) keys[Key] = true;
}

/* remember a key release */
void __fastcall Tmainform::FormKeyUp(TObject *Sender, WORD &Key, TShiftState Shift)
{
    if (Key < 0xff) keys[Key] = false; // clear the key

    if (Key == 27) Application->Terminate(); // escape
    if (Key == 32) world.toggle_sel_self_control(); // space
    if (Key == 78) world.random_ant(10); // 'n' key
    if (Key == 82) world.reset();    // 'r' key
    if (Key == 80) Timer1->Enabled = !Timer1->Enabled; // 'p', toggle timer
    if (Key == 46) world.delete_selected();

//   ShowMessage(Key);
}
/* end of key release */


/* updates ants[0] from the keyboard data (manual control) */
void __fastcall Tmainform::check_keys(void)
{
    if (keys[33]) world.move(up); else          // page up
    if (keys[34]) world.move(down);             // page down

    if (keys[65]) world.move(slide_left); else  // 'a' key
    if (keys[68]) world.move(slide_right);      // 'd' key

    if (keys[38]) world.move(ang_up); else      // up arrow
    if (keys[40]) world.move(ang_down);         // down arrow

    if (keys[37]) world.move(turn_left); else   // left arrow
    if (keys[39]) world.move(turn_right); else  // right arrow
        world.move(stop_turn);

    if (keys[87]) world.move(forw); else        // 'w' key
    if (keys[83]) world.move(back); else        // 's' ket
        world.move(stop_forw);
}

/**********************************
** End of the keyboard functions **
**********************************/





/* called when the button mouse is pushed */
void __fastcall Tmainform::drawareaMouseUp(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y)
{
    // determine of the mouse was over an ant
    world.check_selection(get_point(screen_to_grid_x(X), screen_to_grid_y(Y)));
}
/* end of mouse up */







