#include <iostream>
#include <iomanip>
#include <conio.h>
#include <windows.h>
#include <time.h>
#include <string>
using namespace std;

#include "common.h"
#include "weapon.h"


void weapon::display()
{
	cout << left << setw(10) << name << "Ammo: " << setw(4) << myAmmo;
}

/////////////  GUNS //////////

pistol::pistol() 
{ 
	myAmmo = 6; 
	name="Pistol"; 
	ammoCost = 1; 
	range = 10;
}

rifle::rifle()
{
	myAmmo = 0; 
	name="Rifle"; 
	ammoCost = 5; 
	range = GRID_COLS;
}

void gun::fire(int direction, position pos)
{	
	square *sq=NULL;
	if ( myAmmo > 0 ) {
		position bulletPos=pos;

		bool condition;

		if ( direction == DIR_LEFT || direction == DIR_RIGHT) condition = (bulletPos.col >=0 && abs(bulletPos.col-pos.col) < range && bulletPos.col < GRID_COLS);
		if ( direction == DIR_UP || direction == DIR_DOWN)	  condition = (bulletPos.row >=0 && abs(bulletPos.row-pos.row) < range && bulletPos.row < GRID_ROWS);

		clock_t t1,t2;
		sq=gridSquare(bulletPos);

		while ( condition )
		{
			if ( sq->sqIcon == ZOMBIE_ICON )
			{
				// hit a zombie
				sq->sqIcon=CORPSE_ICON;
				sq->BGcolor=CORPSE_COLOR;
				sq->FGcolor=BKG_COLOR;
				break; // only hit one zombie
			} else if ( sq->sqIcon == EXIT_ICON )
			{
				break; // blocked by exit
			} 


			// draw the bullet
			if ( sq->sqIcon == EMPTY_ICON )
			{
				sq->sqIcon=BULLET_ICON;
				sq->BGcolor=BRwhite;
				sq->FGcolor=BKG_COLOR;
				updateGridSquare(bulletPos);
			}
			// delay a moment
			t1=clock();
			do { t2=clock(); } while ( (t2-t1) < BULLET_SPEED );
			// clear bullet drawing
			if ( sq->sqIcon == BULLET_ICON ) 
			{
				sq->sqIcon=EMPTY_ICON;
				updateGridSquare(bulletPos);
			}


			if ( direction == DIR_LEFT || direction == DIR_RIGHT) 
			{	
				if ( direction == DIR_LEFT )
					bulletPos.col--;
				else
					bulletPos.col++;
				condition = (bulletPos.col >=0  && bulletPos.col < GRID_COLS && abs(bulletPos.col-pos.col) < range);
			} else if ( direction == DIR_UP || direction == DIR_DOWN)	  
			{
				if ( direction == DIR_UP )
					bulletPos.row--;
				else
					bulletPos.row++;
				condition = (bulletPos.row >=0 && bulletPos.row < GRID_ROWS && abs(bulletPos.row-pos.row) < range);
			}
			if ( condition )
				sq=gridSquare(bulletPos);

		} // end tracking bullet (while loop)


		myAmmo--;
	} // end if I have ammo
} // end gun fire


////////// MINES /////////

mine::mine(){
	myAmmo = 3; 
	name="Mine"; 
	ammoCost=3; 
	range = BLAST_RANGE;
} 

void mine::fire(int direction, position pos)
{	
	square *sq=NULL;

	if ( direction == DIR_LEFT )	
		pos.col--;
	else if ( direction == DIR_RIGHT )	
		pos.col++;
	else if ( direction == DIR_UP )		
		pos.row--;
	else if ( direction == DIR_DOWN )	
		pos.row++;

	if ( myAmmo > 0 ) {
		if ( pos.row >=0 && pos.row < GRID_ROWS && pos.col >=0 && pos.col < GRID_COLS )
		{
			sq=gridSquare(pos);
			if ( sq->sqIcon == EMPTY_ICON || sq->sqIcon == CORPSE_ICON )
			{
				sq->sqIcon =MINE_ICON;
				sq->BGcolor=MINE_COLOR;
				sq->FGcolor=BKG_COLOR;
				updateGridSquare(pos);
				myAmmo--;
			} else if ( sq->sqIcon == ZOMBIE_ICON )
			{
				// hit a zombie
				sq->sqIcon=CORPSE_ICON;
				sq->BGcolor=CORPSE_COLOR;
				sq->FGcolor=BKG_COLOR;
				explosion(pos);
				//updateGridSquare(pos);
				myAmmo--;
			}
		}
	}
} // end mine fire