basic game files port

This commit is contained in:
Geoffray Levasseur committed 2026-10-22 17:52:16 +02:00
1 parent 8436242032
commit 9f24650296
10 files changed
+836 -1

No files matched your search

+160
View File
@@ -0,0 +1,160 @@
#include "Cevo/Database.h"
#include <cassert>
#include <cmath>
#include <stdexcept>
namespace Cevo {
namespace Database {
int lx = 0;
int ly = 0;
int mapSize = 0;
std::array<TPlayerContext, nPl> rw{};
std::array<std::uint32_t, lxmax * lymax> realMap{};
std::array<std::int8_t, lxmax * lymax> occupant{};
std::array<std::int8_t, lxmax * lymax> zocMap{};
std::array<std::int32_t, lxmax * lymax> usedByCity{};
namespace {
// Equivalent of Database.pas's private (implementation-section) `Primitive`
// global - not exposed in the header, matching the original's own
// interface/implementation visibility split.
int primitive = 0;
} // namespace
void maskD(std::uint32_t *x, int count, std::uint32_t mask)
{
for (int i = 0; i < count; ++i)
x[i] &= mask;
}
int dLoc(int loc, int dx, int dy)
{
// Faithful port of a likely upstream operator-precedence slip: Pascal's
// `not` binds tighter than `and`, so this only negates the first clause
// instead of the whole precondition. The very next line's assert
// re-checks the FULL intended condition correctly, and this isn't a
// memory-safety issue, so it's preserved as-is rather than silently
// "fixed" (see established policy in repo memory).
if (!(loc >= 0) && (loc < mapSize) && (dx + lx >= 0))
throw std::runtime_error("Relative location error");
assert((loc >= 0) && (loc < mapSize) && (dx + lx >= 0));
const int y0 = loc / lx;
int result = (loc + ((dx + (y0 & 1) + lx + lx) >> 1)) % lx + lx * (y0 + dy);
if (result < 0 || result >= mapSize)
result = -1;
return result;
}
void dxdy(int loc0, int loc1, int &dx, int &dy)
{
dx = (((loc1 % lx) * 2 + ((loc1 / lx) & 1)) - ((loc0 % lx) * 2 + ((loc0 / lx) & 1)) + 3 * lx) % (2 * lx) - lx;
dy = loc1 / lx - loc0 / lx;
}
int distance(int loc0, int loc1)
{
int dx, dy;
dxdy(loc0, loc1, dx, dy);
dx = std::abs(dx);
dy = std::abs(dy);
return dx + dy + (std::abs(dx - dy) >> 1);
}
void v8ToLoc(int loc0, Vicinity8Loc &vicinityLoc)
{
const int lx0 = lx;
int y0 = loc0 / lx0;
const int x0 = loc0 - y0 * lx0;
y0 &= 1;
vicinityLoc[1] = loc0 + lx0 * 2;
vicinityLoc[3] = loc0 - 1;
vicinityLoc[5] = loc0 - lx0 * 2;
vicinityLoc[7] = loc0 + 1;
loc0 += y0;
vicinityLoc[0] = loc0 + lx0;
vicinityLoc[2] = loc0 + lx0 - 1;
vicinityLoc[4] = loc0 - lx0 - 1;
vicinityLoc[6] = loc0 - lx0;
// world is round!
if (x0 < lx0 - 1) {
if (x0 == 0) {
vicinityLoc[3] += lx0;
if (y0 == 0) {
vicinityLoc[2] += lx0;
vicinityLoc[4] += lx0;
}
}
} else {
vicinityLoc[7] -= lx0;
if (y0 == 1) {
vicinityLoc[0] -= lx0;
vicinityLoc[6] -= lx0;
}
}
}
void v21ToLoc(int loc0, Vicinity21Loc &vicinityLoc)
{
const int y0 = loc0 / lx;
int xComp0 = loc0 - y0 * lx - 1; // loc0 % lx - 1
int xCompSwitch = xComp0 - 1 + (y0 & 1);
if (xComp0 < 0)
xComp0 += lx;
if (xCompSwitch < 0)
xCompSwitch += lx;
xCompSwitch ^= xComp0;
int yComp = lx * (y0 - 3);
int *dst = vicinityLoc.data();
int bit = 1;
for (int dy = 0; dy < 7; ++dy) {
xComp0 ^= xCompSwitch;
int xComp = xComp0;
for (int dx = 0; dx < 4; ++dx) {
*dst = (bit & 0x67F7F76) != 0 ? xComp + yComp : -1;
++xComp;
if (xComp >= lx)
xComp -= lx;
++dst;
bit <<= 1;
}
yComp += lx;
}
}
void calculatePrimitive()
{
primitive = 1;
int i = 2;
while (i * i <= mapSize + 1) { // test whether prime
if ((mapSize + 1) % i == 0)
primitive = 0;
++i;
}
if (primitive > 0) {
int j = 0;
do {
++primitive;
i = 1;
j = 0;
do {
++j;
i = i * primitive % (mapSize + 1);
} while (!(i == 1 || j == mapSize + 1));
} while (!(j == mapSize));
}
}
bool mapGeneratorAvailable()
{
return primitive > 0 && lx >= 20 && ly >= 40;
}
} // namespace Database
} // namespace Cevo