Files
c-evo-evo/ai/src/Geometry.cpp
T

166 lines
3.9 KiB
C++

#include "Cevo/Geometry.h"
#include <cstdlib>
namespace Cevo {
namespace {
int g_lx = 1;
int g_ly = 1;
int g_mapSize = 1;
std::int32_t g_difficulty[nPl] = {};
constexpr int ab_v8[9] = { 5, 6, 7, 4, -1, 0, 3, 2, 1 }; // index = 3*B + A + 4
constexpr int v8_a[8] = { 1, 1, 0, -1, -1, -1, 0, 1 };
constexpr int v8_b[8] = { 0, 1, 1, 1, 0, -1, -1, -1 };
} // namespace
void InitGeometry(const TNewGameData &newGameData)
{
g_lx = newGameData.lx;
g_ly = newGameData.ly;
g_mapSize = g_lx * g_ly;
for (int i = 0; i < nPl; ++i)
g_difficulty[i] = newGameData.Difficulty[i];
}
int MapWidth() { return g_lx; }
int MapHeight() { return g_ly; }
int MapSize() { return g_mapSize; }
int Difficulty(int nation) { return g_difficulty[nation]; }
void ab_to_Loc(int Loc0, int A, int B, int &Loc)
{
const int y0 = Loc0 / g_lx;
Loc = (Loc0 + ((A - B + (y0 & 1) + g_lx + g_lx) >> 1)) % g_lx + g_lx * (y0 + A + B);
if (Loc >= g_mapSize)
Loc = -0x1000;
}
void Loc_to_ab(int Loc0, int Loc, int &A, int &B)
{
const int dx = ((Loc % g_lx * 2 + (Loc / g_lx & 1)) - (Loc0 % g_lx * 2 + (Loc0 / g_lx & 1)) + 3 * g_lx) %
(2 * g_lx) -
g_lx;
const int dy = Loc / g_lx - Loc0 / g_lx;
A = (dx + dy) / 2;
B = (dy - dx) / 2;
}
void ab_to_V8(int A, int B, int &V8)
{
V8 = ab_v8[3 * B + A + 4];
}
void V8_to_ab(int V8, int &A, int &B)
{
A = v8_a[V8];
B = v8_b[V8];
}
void ab_to_V21(int A, int B, int &V21)
{
V21 = ((A + B + 3) << 2) + ((A - B + 3) >> 1);
}
void V21_to_ab(int V21, int &A, int &B)
{
const int dy = (V21 >> 2) - 3;
const int dx = (((V21 & 3) << 1) - 3) + ((dy + 3) & 1);
A = (dx + dy) / 2;
B = (dy - dx) / 2;
}
void V8_to_Loc(int Loc0, TVicinity8Loc &VicinityLoc)
{
const int lx = g_lx;
const int y0 = Loc0 / lx;
const int x0 = Loc0 - y0 * lx;
VicinityLoc[1] = Loc0 + lx * 2;
VicinityLoc[3] = Loc0 - 1;
VicinityLoc[5] = Loc0 - lx * 2;
VicinityLoc[7] = Loc0 + 1;
Loc0 += y0 & 1;
VicinityLoc[0] = Loc0 + lx;
VicinityLoc[2] = Loc0 + lx - 1;
VicinityLoc[4] = Loc0 - lx - 1;
VicinityLoc[6] = Loc0 - lx;
// world is round!
if (x0 < lx - 1) {
if (x0 == 0) {
VicinityLoc[3] += lx;
if ((y0 & 1) == 0) {
VicinityLoc[2] += lx;
VicinityLoc[4] += lx;
}
}
} else {
VicinityLoc[7] -= lx;
if ((y0 & 1) == 1) {
VicinityLoc[0] -= lx;
VicinityLoc[6] -= lx;
}
}
// check south pole
switch (g_ly - y0) {
case 1:
VicinityLoc[0] = -0x1000;
VicinityLoc[1] = -0x1000;
VicinityLoc[2] = -0x1000;
break;
case 2:
VicinityLoc[1] = -0x1000;
break;
default:
break;
}
}
void V21_to_Loc(int Loc0, TVicinity21Loc &VicinityLoc)
{
const int y0 = Loc0 / g_lx;
int xComp0 = Loc0 - y0 * g_lx - 1;
int xCompSwitch = xComp0 - 1 + (y0 & 1);
if (xComp0 < 0)
xComp0 += g_lx;
if (xCompSwitch < 0)
xCompSwitch += g_lx;
xCompSwitch ^= xComp0;
int yComp = g_lx * (y0 - 3);
int *dst = VicinityLoc;
unsigned bit = 1;
for (int dy = 0; dy <= 6; ++dy) {
if (yComp < g_mapSize) {
xComp0 ^= xCompSwitch;
int xComp = xComp0;
for (int dx = 0; dx <= 3; ++dx) {
*dst = (bit & 0x67F7F76u) ? xComp + yComp : -1;
++xComp;
if (xComp >= g_lx)
xComp -= g_lx;
++dst;
bit <<= 1;
}
yComp += g_lx;
} else {
for (int dx = 0; dx <= 3; ++dx) {
*dst = -0x1000;
++dst;
}
}
}
}
int Distance(int Loc0, int Loc1)
{
int A, B;
Loc_to_ab(Loc0, Loc1, A, B);
const int dx = std::abs(A - B);
const int dy = std::abs(A + B);
return dx + dy + (std::abs(dx - dy) >> 1);
}
} // namespace Cevo