315 lines
13 KiB
C++
315 lines
13 KiB
C++
// This file is part of c-evo-evo.
|
|
//
|
|
// This software is governed by the CeCILL license under French law and
|
|
// abiding by the rules of distribution of free software. You can use,
|
|
// modify and/or redistribute the software under the terms of the CeCILL
|
|
// license as circulated by CEA, CNRS and INRIA at the following URL
|
|
// "http://www.cecill.info".
|
|
//
|
|
// As a counterpart to the access to the source code and rights to copy,
|
|
// modify and redistribute granted by the license, users are provided only
|
|
// with a limited warranty and the software's author, the holder of the
|
|
// economic rights, and the successive licensors have only limited
|
|
// liability.
|
|
//
|
|
// In this respect, the user's attention is drawn to the risks associated
|
|
// with loading, using, modifying and/or developing or reproducing the
|
|
// software by the user in light of its specific status of free software,
|
|
// that may mean that it is complicated to manipulate, and that also
|
|
// therefore means that it is reserved for developers and experienced
|
|
// professionals having in-depth computer knowledge. Users are therefore
|
|
// encouraged to load and test the software's suitability as regards their
|
|
// requirements in conditions enabling the security of their systems and/or
|
|
// data to be ensured and, more generally, to use and operate it in the
|
|
// same conditions as regards security.
|
|
//
|
|
// The fact that you are presently reading this means that you have had
|
|
// knowledge of the CeCILL license and that you accept its terms.
|
|
|
|
// --- Game Initialization --- (see Cevo/Database.h)
|
|
|
|
#include "Cevo/Database.h"
|
|
#include "DatabaseInternal.h"
|
|
|
|
#include <cassert>
|
|
#include <cstring>
|
|
#include <stdexcept>
|
|
#include <utility>
|
|
#include <vector>
|
|
|
|
namespace Cevo {
|
|
namespace Database {
|
|
|
|
// Delphi/Borland-compatible LCG (see Database.h) - only used by
|
|
// predefinedStartPositions() so far.
|
|
int delphiRandom(int max)
|
|
{
|
|
const std::int64_t temp = static_cast<std::int64_t>(134775813) * static_cast<std::int64_t>(delphiRandSeed) + 1;
|
|
delphiRandSeed = static_cast<std::int32_t>(temp);
|
|
return static_cast<int>((static_cast<std::uint64_t>(static_cast<std::uint32_t>(max)) *
|
|
static_cast<std::uint64_t>(static_cast<std::uint32_t>(delphiRandSeed))) >>
|
|
32);
|
|
}
|
|
|
|
void findContinents()
|
|
{
|
|
auto replaceCont = [](int a, int b, int stop) {
|
|
if (a < b)
|
|
std::swap(a, b);
|
|
if (a > b)
|
|
for (int i = a; i <= stop; ++i)
|
|
if (continent[i] == a)
|
|
continent[i] = b;
|
|
};
|
|
|
|
for (int y = 1; y <= ly - 2; ++y) {
|
|
for (int x = 0; x < lx; ++x) {
|
|
const int loc = x + lx * y;
|
|
continent[loc] = -1;
|
|
if ((realMap[loc] & fTerrain) >= static_cast<std::uint32_t>(fGrass)) {
|
|
if (y - 2 >= 1 && (realMap[loc - 2 * lx] & fTerrain) >= static_cast<std::uint32_t>(fGrass))
|
|
continent[loc] = continent[loc - 2 * lx];
|
|
if ((x - 1 + (y & 1)) >= 0 && y - 1 >= 1 &&
|
|
(realMap[loc - 1 + (y & 1) - lx] & fTerrain) >= static_cast<std::uint32_t>(fGrass))
|
|
continent[loc] = continent[loc - 1 + (y & 1) - lx];
|
|
if ((x + (y & 1)) < lx && y - 1 >= 1 &&
|
|
(realMap[loc + (y & 1) - lx] & fTerrain) >= static_cast<std::uint32_t>(fGrass))
|
|
continent[loc] = continent[loc + (y & 1) - lx];
|
|
if (x - 1 >= 0 && (realMap[loc - 1] & fTerrain) >= static_cast<std::uint32_t>(fGrass)) {
|
|
if (continent[loc] == -1)
|
|
continent[loc] = continent[loc - 1];
|
|
else
|
|
replaceCont(continent[loc - 1], continent[loc], loc);
|
|
}
|
|
if (continent[loc] == -1)
|
|
continent[loc] = loc;
|
|
}
|
|
}
|
|
}
|
|
|
|
// connect continents due to round earth
|
|
for (int y = 1; y <= ly - 2; ++y) {
|
|
if ((realMap[lx * y] & fTerrain) >= static_cast<std::uint32_t>(fGrass)) {
|
|
int wrong = -1;
|
|
if ((realMap[lx - 1 + lx * y] & fTerrain) >= static_cast<std::uint32_t>(fGrass))
|
|
wrong = continent[lx - 1 + lx * y];
|
|
if ((y & 1) == 0 && y - 1 >= 1 &&
|
|
(realMap[lx - 1 + lx * (y - 1)] & fTerrain) >= static_cast<std::uint32_t>(fGrass))
|
|
wrong = continent[lx - 1 + lx * (y - 1)];
|
|
if ((y & 1) == 0 && y + 1 < ly - 1 &&
|
|
(realMap[lx - 1 + lx * (y + 1)] & fTerrain) >= static_cast<std::uint32_t>(fGrass))
|
|
wrong = continent[lx - 1 + lx * (y + 1)];
|
|
if (wrong >= 0)
|
|
replaceCont(wrong, continent[lx * y], mapSize - 1);
|
|
}
|
|
}
|
|
}
|
|
|
|
void predefinedStartPositions(int human)
|
|
{
|
|
int nAlive = 0;
|
|
for (int p1 = 0; p1 < nPl; ++p1)
|
|
if ((1 << p1) & gAlive)
|
|
++nAlive;
|
|
if (nAlive == 0)
|
|
return;
|
|
|
|
std::vector<int> startLoc0(static_cast<std::size_t>(lxmax) * lymax, 0);
|
|
|
|
int nStartLoc0 = 0;
|
|
int nPrefStartLoc0 = 0;
|
|
for (int loc1 = 0; loc1 < mapSize; ++loc1) {
|
|
if (realMap[loc1] & fPrefStartPos) {
|
|
startLoc0[static_cast<std::size_t>(nStartLoc0)] = startLoc0[static_cast<std::size_t>(nPrefStartLoc0)];
|
|
startLoc0[static_cast<std::size_t>(nPrefStartLoc0)] = loc1;
|
|
++nPrefStartLoc0;
|
|
++nStartLoc0;
|
|
realMap[loc1] &= ~static_cast<std::uint32_t>(fPrefStartPos);
|
|
} else if (realMap[loc1] & fStartPos) {
|
|
startLoc0[static_cast<std::size_t>(nStartLoc0)] = loc1;
|
|
++nStartLoc0;
|
|
realMap[loc1] &= ~static_cast<std::uint32_t>(fStartPos);
|
|
}
|
|
}
|
|
assert(nStartLoc0 >= nAlive);
|
|
|
|
startLoc[0] = 0;
|
|
for (bool isHuman : { true, false }) {
|
|
for (int p1 = 0; p1 < nPl; ++p1) {
|
|
if (((1 << p1) & gAlive) && ((((1 << p1) & human) != 0) == isHuman)) {
|
|
--nStartLoc0;
|
|
int imax = nStartLoc0;
|
|
if (nPrefStartLoc0 > 0) {
|
|
--nPrefStartLoc0;
|
|
imax = nPrefStartLoc0;
|
|
}
|
|
const int i = delphiRandom(imax + 1);
|
|
startLoc[p1] = startLoc0[static_cast<std::size_t>(i)];
|
|
startLoc2[p1] = startLoc0[static_cast<std::size_t>(i)];
|
|
startLoc0[static_cast<std::size_t>(i)] = startLoc0[static_cast<std::size_t>(imax)];
|
|
startLoc0[static_cast<std::size_t>(imax)] = startLoc0[static_cast<std::size_t>(nStartLoc0)];
|
|
}
|
|
}
|
|
}
|
|
saveMapCenterLoc = startLoc[0];
|
|
}
|
|
|
|
void initGame()
|
|
{
|
|
occupant.fill(-1);
|
|
zocMap.fill(0);
|
|
observeLevel.fill(0);
|
|
usedByCity.fill(-1);
|
|
gTestFlags = 0;
|
|
gInitialized = gAlive | gWatching;
|
|
|
|
for (int p = 0; p < nPl; ++p) {
|
|
if (((1 << p) & gInitialized) == 0)
|
|
continue;
|
|
TPlayerContext &player = rw[p];
|
|
researched[p] = 0;
|
|
discovered[p] = 0;
|
|
territoryCount[p] = 0;
|
|
nTech[p] = 0;
|
|
resourceMask[p] = (difficulty[p] == 0) ? 0xFFFFFFFFu : (0xFFFFFFFFu & ~static_cast<std::uint32_t>(fSpecial2 | fModern));
|
|
grWallContinent[p] = -1;
|
|
|
|
player.Map = reinterpret_cast<TTileList *>(new std::uint32_t[static_cast<std::size_t>(mapSize)]());
|
|
player.MapObservedLast = reinterpret_cast<TTileObservedLastList *>(new std::int16_t[static_cast<std::size_t>(mapSize)]);
|
|
std::memset(*player.MapObservedLast, 0xFF, static_cast<std::size_t>(mapSize) * 2);
|
|
player.Territory = reinterpret_cast<TOwnerList *>(new std::int8_t[static_cast<std::size_t>(mapSize)]);
|
|
std::memset(*player.Territory, 0xFF, static_cast<std::size_t>(mapSize));
|
|
player.Un = reinterpret_cast<TUnList *>(new TUn[nUmax]());
|
|
player.Model = reinterpret_cast<TModelList *>(new TModel[nMmax + 1]());
|
|
player.City = reinterpret_cast<TCityList *>(new TCity[nCmax]());
|
|
player.EnemyUn = reinterpret_cast<TEnemyUnList *>(new TUnitInfo[neumax]());
|
|
player.EnemyCity = reinterpret_cast<TEnemyCityList *>(new TCityInfo[necmax]());
|
|
player.EnemyModel = reinterpret_cast<TEnemyModelList *>(new TModelInfo[nemmax]());
|
|
|
|
for (int p1 = 0; p1 < nPl; ++p1) {
|
|
if ((1 << p1) & gInitialized) {
|
|
std::memset(rwemix[p][p1].data(), 0xFF, rwemix[p][p1].size() * sizeof(std::int16_t));
|
|
std::memset(destroyed[p][p1].data(), 0, destroyed[p][p1].size() * sizeof(std::int16_t));
|
|
}
|
|
player.Attitude[p1] = atNeutral;
|
|
player.Treaty[p1] = trNoContact;
|
|
player.LastCancelTreaty[p1] = -CancelTreatyTurns - 1;
|
|
player.EvaStart[p1] = -PeaceEvaTurns - 1;
|
|
player.Tribute[p1] = 0;
|
|
player.TributePaid[p1] = 0;
|
|
if (p1 != p && ((1 << p1) & gAlive)) {
|
|
// Truncated allocation (matches the original's own GetMem
|
|
// size: TEnemyReport's UnCount[] field is oversized via the
|
|
// same "open array via overlarge static type" trick used
|
|
// throughout this port - only nMmax slots are real).
|
|
player.EnemyReport[p1] = static_cast<TEnemyReport *>(
|
|
::operator new(sizeof(TEnemyReport) - sizeof(std::uint16_t) * (INFIN + 1 - nMmax)));
|
|
std::memset(player.EnemyReport[p1]->Tech, tsNA, nAdv);
|
|
player.EnemyReport[p1]->TurnOfContact = -1;
|
|
player.EnemyReport[p1]->TurnOfCivilReport = -1;
|
|
player.EnemyReport[p1]->TurnOfMilReport = -1;
|
|
player.EnemyReport[p1]->Attitude = atNeutral;
|
|
player.EnemyReport[p1]->Government = gDespotism;
|
|
if (((1 << p) & gAlive) == 0)
|
|
player.Treaty[p1] = trNone; // supervisor
|
|
} else {
|
|
player.EnemyReport[p1] = nullptr;
|
|
}
|
|
}
|
|
player.TestFlags = gTestFlags;
|
|
player.Credibility = InitialCredibility;
|
|
player.MaxCredibility = 100;
|
|
player.nUn = 0;
|
|
player.nModel = 0;
|
|
player.nCity = 0;
|
|
player.nEnemyUn = 0;
|
|
player.nEnemyCity = 0;
|
|
player.nEnemyModel = 0;
|
|
std::uint32_t *map = *player.Map;
|
|
for (int loc1 = 0; loc1 < mapSize; ++loc1)
|
|
map[loc1] = fUNKNOWN;
|
|
std::memset(player.Tech, tsNA, nAdv);
|
|
std::memset(player.NatBuilt, 0, sizeof(player.NatBuilt));
|
|
}
|
|
|
|
// create initial models and units
|
|
for (int p = 0; p < nPl; ++p) {
|
|
if (((1 << p) & gAlive) == 0)
|
|
continue;
|
|
TPlayerContext &player = rw[p];
|
|
TModel *model = *player.Model;
|
|
player.nModel = 0;
|
|
for (int i = 0; i < nSpecialModel; ++i) {
|
|
if (SpecialModelPreq[i] == preNone) {
|
|
model[player.nModel] = SpecialModel[i];
|
|
model[player.nModel].Status = 0;
|
|
model[player.nModel].IntroTurn = 0;
|
|
model[player.nModel].Built = 0;
|
|
model[player.nModel].Lost = 0;
|
|
model[player.nModel].ID = static_cast<std::uint16_t>((p << 12) + player.nModel);
|
|
setModelFlags(model[player.nModel]);
|
|
++player.nModel;
|
|
}
|
|
}
|
|
player.nUn = 0;
|
|
unBuilt[p] = 0;
|
|
TUn *un = *player.Un;
|
|
for (int uix = 0; uix < nStartUn; ++uix) {
|
|
createUnit(p, StartUn[uix]);
|
|
--model[StartUn[uix]].Built;
|
|
un[uix].Loc = startLoc2[p];
|
|
placeUnit(p, uix);
|
|
}
|
|
foundCity(p, startLoc[p]);
|
|
founded[p] = 1;
|
|
TCity &city = (*player.City)[0];
|
|
city.ID = static_cast<std::uint16_t>(p << 12);
|
|
city.Flags = chFounded;
|
|
}
|
|
|
|
territoryCount[nPl] = mapSize;
|
|
}
|
|
|
|
void initMapGame(int human)
|
|
{
|
|
delphiRandSeed = rnd;
|
|
findContinents();
|
|
predefinedStartPositions(human);
|
|
initGame();
|
|
}
|
|
|
|
void initRandomGame()
|
|
{
|
|
delphiRandSeed = rnd;
|
|
calculatePrimitive();
|
|
createElevation();
|
|
createMap(false);
|
|
startPositions();
|
|
initGame();
|
|
}
|
|
|
|
void releaseGame()
|
|
{
|
|
for (int p1 = 0; p1 < nPl; ++p1) {
|
|
if (((1 << p1) & gInitialized) == 0)
|
|
continue;
|
|
TPlayerContext &player = rw[p1];
|
|
for (int p2 = 0; p2 < nPl; ++p2)
|
|
if (player.EnemyReport[p2])
|
|
::operator delete(player.EnemyReport[p2]);
|
|
delete[] reinterpret_cast<TUnitInfo *>(player.EnemyUn);
|
|
delete[] reinterpret_cast<TCityInfo *>(player.EnemyCity);
|
|
delete[] reinterpret_cast<TModelInfo *>(player.EnemyModel);
|
|
delete[] reinterpret_cast<TUn *>(player.Un);
|
|
delete[] reinterpret_cast<TCity *>(player.City);
|
|
delete[] reinterpret_cast<TModel *>(player.Model);
|
|
delete[] reinterpret_cast<std::int8_t *>(player.Territory);
|
|
delete[] reinterpret_cast<std::int16_t *>(player.MapObservedLast);
|
|
delete[] reinterpret_cast<std::uint32_t *>(player.Map);
|
|
}
|
|
gInitialized = 0;
|
|
}
|
|
|
|
} // namespace Database
|
|
} // namespace Cevo
|