#include "Block.h"

Block::Block(){}

Block::Block(int i, int j)
{
	BWAPI::Position tl(i, j); // Top left
	topLeft = tl;
	size = BLOCKSIZE;
	value = 0;
}

Block::Block(BWAPI::Position p)
{
	topLeft = p;
	size = BLOCKSIZE;
	value = 0;
}

BWAPI::Position Block::getTopLeft(){
	return topLeft;
}

BWAPI::Position Block::getBottomRight(){
	return BWAPI::Position(topLeft.x + size, topLeft.y + size);;
}

BWAPI::Position Block::getCenter(){
	return BWAPI::Position(topLeft.x + size / 2, topLeft.y + size / 2);
}

double Block::getValue(){
	return value;
}

void Block::setValue(double v){
	value = v;
}

void Block::addValue(double v){
	value += v;
}

bool Block::hasInside(BWAPI::Position p){
	return p.x >= topLeft.x && p.x <= topLeft.x + size && p.y >= topLeft.y && p.y <= topLeft.y + size;
}

bool Block::operator==(const Block &block) const {
	return (topLeft.x == block.topLeft.x && topLeft.y == block.topLeft.y);
}

std::ostream& operator<<(std::ostream &strm, const Block &block){
	return strm << "(" << block.topLeft.x << "," << block.topLeft.y << ")";
}


bool Block::operator<(const Block& block) const
{
	return (std::hash<int>()(topLeft.x) ^ std::hash<int>()(topLeft.y)) < (std::hash<int>()(block.topLeft.x) ^ std::hash<int>()(block.topLeft.y));
}

void Block::draw(bool isSolid){
	if (isSolid)
		BWAPI::Broodwar->drawBoxMap(getTopLeft(), getBottomRight(), BWAPI::Colors::Green, isSolid);
	BWAPI::Broodwar->drawBoxMap(getTopLeft(), getBottomRight(), BWAPI::Colors::Orange);
}


int Block::manhattanDistance(BWAPI::Position p1, BWAPI::Position p2){
	int x, y;
	x = abs(p1.x - p2.x) / BLOCKSIZE;
	y = abs(p1.y - p2.y) / BLOCKSIZE;
	return x + y;
}
