#include "LocalArea.h"
/*
This next function is an unholy moster. Basically, what it does, is it goes through the LocalArea and gets the specified peice of terrain.
however, if that piece of terrain is outside the LocalArea and the function is told to look for it, it will check the neighboring LocalAreas.
The part where this becomes a pain is where we take into account that those can be rotated and flipped.
This means a giant block of code to account for every possible combination. Which means massive nested switch statements. Which are ugly.
Alos take note, most if not all of the magic number 1's are to account for width and height measuring the number of items,
while the coordinates have a zero and are thus offset by 1.
*/
const TerrainType& LocalArea::getTerrain(int x, int y, bool checkBorders)
{
if (x < 0 || x >= getWidth() || y < 0 || y >= getHeight())
{
if (checkBorders)
{
BorderArea* borderA;
try
{
borderA = &getBorder(x,y);
}
catch(std::domain_error e)
{
return TerrainType(' ');
}
int getx, gety;
switch (borderA->getDirection())
{
case DIRECTION_NORTH:
{
switch (borderA->getRotation())
{
case DEGREE_0:
{
gety = borderA->getArea().getHeight() + y;
if (borderA->getMirroring())
getx = (borderA->getArea().getWidth() - 1) - (x - borderA->getOffset());
else
getx = x - borderA->getOffset();
return borderA->getArea().getTerrain(getx, gety);
}
case DEGREE_270:
{
getx = (-y) - 1;
if (borderA->getMirroring())
gety = (borderA->getArea().getHeight() -1) - (x - borderA->getOffset());
else
gety = x - borderA->getOffset();
return borderA->getArea().getTerrain(getx, gety);
}
case DEGREE_180:
{
gety = (-y) - 1;
if (borderA->getMirroring())
getx = x - borderA->getOffset();
else
getx = (borderA->getArea().getWidth()-1) - (x - borderA->getOffset());
return borderA->getArea().getTerrain(getx, gety);
}
case DEGREE_90:
{
getx = borderA->getArea().getWidth() + y;
if (borderA->getMirroring())
gety = x - borderA->getOffset();
else
gety = (borderA->getArea().getHeight()-1) - (x - borderA->getOffset());
return borderA->getArea().getTerrain(getx, gety);
}
}
}
case DIRECTION_EAST:
{
switch (borderA->getRotation())
{
case DEGREE_0:
{
getx = x - getWidth();
if (borderA->getMirroring())
gety = (borderA->getArea().getHeight() - 1) - (y - borderA->getOffset());
else
gety = y - borderA->getOffset();
return borderA->getArea().getTerrain(getx, gety);
}
case DEGREE_270:
{
gety = x - getWidth();
if (borderA->getMirroring())
getx = y - borderA->getOffset();
else
getx = (borderA->getArea().getWidth() - 1) - (y - borderA->getOffset());
return borderA->getArea().getTerrain(getx, gety);
}
case DEGREE_180:
{
getx = (borderA->getArea().getWidth() - 1) - (x - getWidth());
if (borderA->getMirroring())
gety = y - borderA->getOffset();
else
gety = (borderA->getArea().getHeight() - 1) - (y - borderA->getOffset());
return borderA->getArea().getTerrain(getx, gety);
}
case DEGREE_90:
{
gety = (borderA->getArea().getHeight() - 1) - (x - getWidth());
if (borderA->getMirroring())
getx = (borderA->getArea().getWidth() - 1) - (y - borderA->getOffset());
else
getx = y - borderA->getOffset();
return borderA->getArea().getTerrain(getx, gety);
}
}
}
case DIRECTION_SOUTH:
{
switch (borderA->getRotation())
{
case DEGREE_0:
{
gety = y - getHeight();
if (borderA->getMirroring())
getx = (borderA->getArea().getWidth() - 1) - (x - borderA->getOffset());
else
getx = x - borderA->getOffset();
return borderA->getArea().getTerrain(getx, gety);
}
case DEGREE_270:
{
getx = (borderA->getArea().getWidth() - 1) - (y - getHeight());
if (borderA->getMirroring())
gety = (borderA->getArea().getWidth() - 1) - (x - borderA->getOffset());
else
gety = x - borderA->getOffset();
return borderA->getArea().getTerrain(getx, gety);
}
case DEGREE_180:
{
gety = (borderA->getArea().getHeight() - 1) - (y - getHeight());
if (borderA->getMirroring())
getx = x - borderA->getOffset();
else
getx = (borderA->getArea().getWidth() - 1) - (x - borderA->getOffset());
return borderA->getArea().getTerrain(getx, gety);
}
case DEGREE_90:
{
getx = y - getHeight();
if (borderA->getMirroring())
gety = x - borderA->getOffset();
else
gety = (borderA->getArea().getWidth() - 1) - (x - borderA->getOffset());
return borderA->getArea().getTerrain(getx, gety);
}
}
}
case DIRECTION_WEST:
{
switch (borderA->getRotation())
{
case DEGREE_0:
{
getx = borderA->getArea().getWidth() + x;
if (borderA->getMirroring())
gety = (borderA->getArea().getHeight() - 1) - (y - borderA->getOffset());
else
gety = y - borderA->getOffset();
return borderA->getArea().getTerrain(getx, gety);
}
case DEGREE_270:
{
gety = (borderA->getArea().getWidth() - 1) + x;
if (borderA->getMirroring())
getx = y - borderA->getOffset();
else
getx = (borderA->getArea().getWidth() - 1) - (y - borderA->getOffset());
return borderA->getArea().getTerrain(getx, gety);
}
case DEGREE_180:
{
getx = (-x) - 1;
if (borderA->getMirroring())
gety = y - borderA->getOffset();
else
gety = (borderA->getArea().getHeight() - 1) - (y - borderA->getOffset());
return borderA->getArea().getTerrain(getx, gety);
}
case DEGREE_90:
{
gety = (-x) - 1;
if (borderA->getMirroring())
getx = (borderA->getArea().getWidth() - 1) - (y - borderA->getOffset());
else
getx = y - borderA->getOffset();
return borderA->getArea().getTerrain(getx, gety);
}
}
}
}
}
else
return TerrainType('Y');
}
return *terrain[y]
}
void LocalArea::fillTerrain(const TerrainType& type)
{
unsigned int i, j;
for (i = 0; i < terrain.size(); i++)
{
for (j = 0; j < terrain
.size(); j++)
{
terrain[j] = &type;
}
}
}