added tribes and keybinding
This commit is contained in:
1 parent
d1074096aa
commit
199cf2479c
7 files changed
+1383
No files matched your search
@@ -0,0 +1,552 @@
|
||||
// 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.
|
||||
|
||||
#include "Tribes.h"
|
||||
|
||||
#include "Cevo/CevoScreenTools.h"
|
||||
#include "Global.h"
|
||||
|
||||
#include <QFile>
|
||||
#include <QFileInfo>
|
||||
#include <QTextStream>
|
||||
|
||||
#include <limits>
|
||||
#include <vector>
|
||||
|
||||
namespace Cevo {
|
||||
namespace Tribes {
|
||||
|
||||
std::array<std::unique_ptr<Tribe>, MaxPlayers> tribe;
|
||||
GraphicSet *hGrStdUnits = nullptr;
|
||||
|
||||
namespace {
|
||||
|
||||
// Shared single-line parser cursor, exactly like Tribes.pas's module-level
|
||||
// `Input` + Get/GetNum - populated with one script line at a time by the
|
||||
// various scanning loops below.
|
||||
QString input;
|
||||
|
||||
QString get()
|
||||
{
|
||||
while (!input.isEmpty() && (input.at(0) == QLatin1Char(' ') || input.at(0) == QLatin1Char('\t')))
|
||||
input.remove(0, 1);
|
||||
int p = input.indexOf(QLatin1Char(','));
|
||||
if (p < 0)
|
||||
p = input.length();
|
||||
const QString result = input.left(p);
|
||||
input.remove(0, p + 1);
|
||||
return result;
|
||||
}
|
||||
|
||||
int getNum()
|
||||
{
|
||||
bool ok = false;
|
||||
const int value = get().toInt(&ok);
|
||||
return ok ? value : 0;
|
||||
}
|
||||
|
||||
QStringList loadScriptLines(const QString &fileName)
|
||||
{
|
||||
QStringList lines;
|
||||
QFile file(fileName);
|
||||
if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
||||
QTextStream stream(&file);
|
||||
while (!stream.atEnd())
|
||||
lines.append(stream.readLine());
|
||||
}
|
||||
return lines;
|
||||
}
|
||||
|
||||
// TChosenModelPictureInfo equivalent.
|
||||
struct ChosenModelPictureInfo {
|
||||
int hash = 0;
|
||||
GraphicSet *hGr = nullptr;
|
||||
int pix = 0;
|
||||
QString modelName;
|
||||
};
|
||||
|
||||
QStringList stdUnitScript;
|
||||
std::vector<ChosenModelPictureInfo> pictureList;
|
||||
|
||||
} // namespace
|
||||
|
||||
std::uint8_t ModelPictureInfo::commandDataSize() const
|
||||
{
|
||||
// 4 ints (trix/mix/pix/Hash) + a 1-byte length prefix + the (UTF-8)
|
||||
// name bytes, matching the original's SizeOf()-based ShortString size.
|
||||
return static_cast<std::uint8_t>(4 * sizeof(std::int32_t) + 1 +
|
||||
static_cast<std::size_t>(grName.toUtf8().size()));
|
||||
}
|
||||
|
||||
void init()
|
||||
{
|
||||
stdUnitScript = loadScriptLines(localizedFilePath(QStringLiteral("Tribes/StdUnits.txt")));
|
||||
pictureList.clear();
|
||||
}
|
||||
|
||||
void done()
|
||||
{
|
||||
pictureList.clear();
|
||||
stdUnitScript.clear();
|
||||
}
|
||||
|
||||
QString cityName(int founder)
|
||||
{
|
||||
if (!generateNames)
|
||||
return QStringLiteral("%1.%2").arg(founder >> 12).arg(founder & 0xFFF);
|
||||
const int founderTribe = founder >> 12;
|
||||
if (founderTribe < 0 || founderTribe >= MaxPlayers || !tribe[static_cast<std::size_t>(founderTribe)])
|
||||
return QStringLiteral("%1.%2").arg(founder >> 12).arg(founder & 0xFFF);
|
||||
return tribe[static_cast<std::size_t>(founderTribe)]->getCityName(founder & 0xFFF);
|
||||
}
|
||||
|
||||
int modelCode(const TModelInfo &modelInfo)
|
||||
{
|
||||
int result = 0;
|
||||
switch (modelInfo.Kind) {
|
||||
case mkSelfDeveloped:
|
||||
case mkEnemyDeveloped:
|
||||
switch (modelInfo.Domain) {
|
||||
case dGround:
|
||||
if ((modelInfo.Attack >= modelInfo.Defense * 4) ||
|
||||
(modelInfo.Attack > 0 && modelInfo.MaxUpgrade < 10 &&
|
||||
(modelInfo.Cap & (1u << (mcArtillery - mcFirstNonCap))) != 0)) {
|
||||
result = 170;
|
||||
if (modelInfo.MaxUpgrade >= 12)
|
||||
result += 3;
|
||||
else if (modelInfo.MaxUpgrade >= 10 || modelInfo.Weight > 7)
|
||||
result += 2;
|
||||
else if (modelInfo.MaxUpgrade >= 4)
|
||||
result += 1;
|
||||
} else {
|
||||
result = 100;
|
||||
if (modelInfo.MaxUpgrade >= 12)
|
||||
result += 6;
|
||||
else if (modelInfo.MaxUpgrade >= 10 || modelInfo.Weight > 7)
|
||||
result += 5;
|
||||
else if (modelInfo.MaxUpgrade >= 6)
|
||||
result += 4;
|
||||
else if (modelInfo.MaxUpgrade >= 4)
|
||||
result += 3;
|
||||
else if (modelInfo.MaxUpgrade >= 2)
|
||||
result += 2;
|
||||
else if (modelInfo.MaxUpgrade >= 1)
|
||||
result += 1;
|
||||
if (modelInfo.Speed >= 250) {
|
||||
if (result >= 105 && modelInfo.Attack <= modelInfo.Defense)
|
||||
result = 110;
|
||||
else
|
||||
result += 30;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case dSea:
|
||||
result = 200;
|
||||
if (modelInfo.MaxUpgrade >= 8)
|
||||
result += 3;
|
||||
else if (modelInfo.MaxUpgrade >= 6)
|
||||
result += 2;
|
||||
else if (modelInfo.MaxUpgrade >= 3)
|
||||
result += 1;
|
||||
if ((modelInfo.Cap & (1u << (mcSub - mcFirstNonCap))) != 0)
|
||||
result = 240;
|
||||
else if (modelInfo.ATrans_Fuel > 0)
|
||||
result = 220;
|
||||
else if (result >= 202 && modelInfo.Attack == 0 && modelInfo.TTrans > 0)
|
||||
result = 210;
|
||||
break;
|
||||
case dAir:
|
||||
result = 300;
|
||||
if (modelInfo.Bombs > 0 || modelInfo.TTrans > 0)
|
||||
result += 10;
|
||||
if (modelInfo.Speed > 850)
|
||||
result += 1;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case mkSpecial_TownGuard:
|
||||
result = 41;
|
||||
break;
|
||||
case mkSpecial_Boat:
|
||||
result = 64;
|
||||
break;
|
||||
case mkSpecial_SubCabin:
|
||||
result = 71;
|
||||
break;
|
||||
case mkSpecial_Glider:
|
||||
result = 73;
|
||||
break;
|
||||
case mkSlaves:
|
||||
result = 74;
|
||||
break;
|
||||
case mkSettler:
|
||||
result = modelInfo.Speed > 150 ? 11 : 10;
|
||||
break;
|
||||
case mkDiplomat:
|
||||
result = 21;
|
||||
break;
|
||||
case mkCaravan:
|
||||
result = 30;
|
||||
break;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
void findStdModelPicture(int code, int &pix, QString &name)
|
||||
{
|
||||
for (const QString &line : stdUnitScript) {
|
||||
input = line;
|
||||
pix = getNum();
|
||||
if (code == getNum()) {
|
||||
name = get();
|
||||
return;
|
||||
}
|
||||
}
|
||||
pix = -1;
|
||||
}
|
||||
|
||||
bool getTribeInfo(const QString &fileName, QString &name, QColor &color)
|
||||
{
|
||||
name.clear();
|
||||
color = colorFromDelphi(0xFFFFFF);
|
||||
int found = 0;
|
||||
|
||||
QFile file(localizedFilePath(QStringLiteral("Tribes/") + fileName + CevoTribeExt));
|
||||
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
|
||||
return false;
|
||||
QTextStream stream(&file);
|
||||
while (!stream.atEnd()) {
|
||||
const QString line = stream.readLine();
|
||||
if (line.left(7) == QLatin1String("#CHOOSE")) {
|
||||
name = line.mid(8);
|
||||
found |= 1;
|
||||
if (found == 3)
|
||||
break;
|
||||
} else if (line.left(6) == QLatin1String("#COLOR")) {
|
||||
color = hexStringToColor(line.mid(6));
|
||||
found |= 2;
|
||||
if (found == 3)
|
||||
break;
|
||||
}
|
||||
}
|
||||
return found == 3;
|
||||
}
|
||||
|
||||
void findPosition(GraphicSet *hGr, int x, int y, int xmax, int ymax, QColor mark, int &xp, int &yp)
|
||||
{
|
||||
xp = 0;
|
||||
yp = 0;
|
||||
if (!hGr)
|
||||
return;
|
||||
const QRgb markRgb = mark.rgb() & 0xFFFFFFu;
|
||||
const QImage &image = hGr->data.image();
|
||||
while (xp < xmax && (static_cast<QRgb>(image.pixel(x + 1 + xp, y)) & 0xFFFFFFu) != markRgb)
|
||||
++xp;
|
||||
while (yp < ymax && (static_cast<QRgb>(image.pixel(x, y + 1 + yp)) & 0xFFFFFFu) != markRgb)
|
||||
++yp;
|
||||
}
|
||||
|
||||
Tribe::Tribe(const QString &fileName)
|
||||
{
|
||||
m_name.fill(QString());
|
||||
m_script = loadScriptLines(fileName);
|
||||
m_cityLine0 = 0;
|
||||
m_nCityLines = 0;
|
||||
|
||||
for (int line = 0; line < m_script.size(); ++line) {
|
||||
input = m_script.at(line);
|
||||
if (m_cityLine0 > 0 && m_nCityLines == 0 && (input.isEmpty() || input.at(0) == QLatin1Char('#')))
|
||||
m_nCityLines = line - m_cityLine0;
|
||||
|
||||
if (input.length() >= 3 && input.at(0) == QLatin1Char('#') && input.at(1) >= QLatin1Char('a') &&
|
||||
input.at(1) <= QLatin1Char('z') && input.at(2) == QLatin1Char(' ')) {
|
||||
m_name[static_cast<std::size_t>(input.at(1).unicode() - QLatin1Char('a').unicode())] = input.mid(3);
|
||||
} else if (input.left(6) == QLatin1String("#COLOR")) {
|
||||
color = hexStringToColor(input.mid(6));
|
||||
} else if (input.left(7) == QLatin1String("#CITIES")) {
|
||||
m_cityLine0 = line + 1;
|
||||
} else if (input.left(8) == QLatin1String("#SYMBOLS")) {
|
||||
input.remove(0, 9);
|
||||
const QString item = get();
|
||||
symPix = getNum();
|
||||
symHGr = loadGraphicSet(item + PngExt);
|
||||
}
|
||||
}
|
||||
|
||||
m_modelPicture.fill(ModelPicture());
|
||||
m_modelName.fill(QString());
|
||||
numberName = -1;
|
||||
cAge = -1;
|
||||
mixSlaves = -1;
|
||||
}
|
||||
|
||||
Tribe::~Tribe() = default;
|
||||
|
||||
QString Tribe::getCityName(int i) const
|
||||
{
|
||||
QString result;
|
||||
if (m_nCityLines > i) {
|
||||
result = m_script.at(m_cityLine0 + i);
|
||||
while (!result.isEmpty() && (result.at(0) == QLatin1Char(' ') || result.at(0) == QLatin1Char('\t')))
|
||||
result.remove(0, 1);
|
||||
} else {
|
||||
result = tPhrase(QStringLiteral("GENCITY")).arg(i + 1);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
void Tribe::setCityName(int i, const QString &newName)
|
||||
{
|
||||
while (m_nCityLines <= i) {
|
||||
m_script.insert(m_cityLine0 + m_nCityLines, tPhrase(QStringLiteral("GENCITY")).arg(m_nCityLines + 1));
|
||||
++m_nCityLines;
|
||||
}
|
||||
m_script[m_cityLine0 + i] = newName;
|
||||
}
|
||||
|
||||
QString Tribe::tString(QString templateText) const
|
||||
{
|
||||
for (;;) {
|
||||
int p = templateText.indexOf(QLatin1Char('#'));
|
||||
if (p < 0 || p == templateText.length() - 1)
|
||||
break;
|
||||
QChar variant = templateText.at(p + 1);
|
||||
const bool caseUp = variant >= QLatin1Char('A') && variant <= QLatin1Char('Z');
|
||||
if (caseUp)
|
||||
variant = QChar(variant.unicode() + 32);
|
||||
templateText.remove(p, 2);
|
||||
if (variant >= QLatin1Char('a') && variant <= QLatin1Char('z')) {
|
||||
const QString replacement = numberName < 0
|
||||
? m_name[static_cast<std::size_t>(variant.unicode() - QLatin1Char('a').unicode())]
|
||||
: QStringLiteral("P%1").arg(numberName);
|
||||
templateText.insert(p, replacement);
|
||||
if (caseUp && templateText.length() > p) {
|
||||
const QChar c = templateText.at(p);
|
||||
if ((c >= QLatin1Char('a') && c <= QLatin1Char('z')) || (c.unicode() >= 0xE0 && c.unicode() <= 0xFF))
|
||||
templateText[p] = QChar(c.unicode() - 32);
|
||||
}
|
||||
}
|
||||
}
|
||||
return templateText;
|
||||
}
|
||||
|
||||
QString Tribe::tPhrase(const QString &item) const
|
||||
{
|
||||
// Phrases/Phrases2 (StringTables.pas) aren't ported. "GENCITY" is the
|
||||
// only phrase this file ever looks up (a generated-city-name fallback
|
||||
// once a tribe's scripted name list runs out) - fall back to a plain
|
||||
// English template rather than failing on an unrecognized key.
|
||||
Q_UNUSED(item);
|
||||
return tString(QStringLiteral("City %1"));
|
||||
}
|
||||
|
||||
void Tribe::initAge(int age)
|
||||
{
|
||||
if (age == cAge)
|
||||
return;
|
||||
cAge = age;
|
||||
|
||||
const QString marker = QStringLiteral("#AGE") + QString::number(age) + QLatin1Char(' ');
|
||||
int i = 0;
|
||||
while (i < m_script.size() && m_script.at(i).left(6) != marker)
|
||||
++i;
|
||||
if (i >= m_script.size())
|
||||
return;
|
||||
|
||||
input = m_script.at(i);
|
||||
input.remove(0, 6);
|
||||
QString item = get();
|
||||
cPix = getNum();
|
||||
|
||||
if (age < 2) {
|
||||
if (item.compare(QLatin1String("stdcities"), Qt::CaseInsensitive) == 0) {
|
||||
if (cPix == 3) {
|
||||
cPix = 0;
|
||||
} else if (cPix == 6) {
|
||||
cPix = 0;
|
||||
item = QStringLiteral("Nation2");
|
||||
}
|
||||
}
|
||||
cHGr = loadGraphicSet(item + PngExt);
|
||||
for (int x = 0; x < 4; ++x) {
|
||||
findPosition(cHGr, x * 65, cPix * 49, 63, 47, colorFromDelphi(0x00FFFF), cityPicture[static_cast<std::size_t>(x)].xShield,
|
||||
cityPicture[static_cast<std::size_t>(x)].yShield);
|
||||
}
|
||||
} else {
|
||||
cHGr = nullptr;
|
||||
}
|
||||
|
||||
get();
|
||||
getNum();
|
||||
item = get();
|
||||
if (item.isEmpty()) {
|
||||
faceHGr = nullptr;
|
||||
} else {
|
||||
faceHGr = loadGraphicSet(item + PngExt);
|
||||
facePix = getNum();
|
||||
if (faceHGr) {
|
||||
const int px = facePix % 10 * 65;
|
||||
const int py = facePix / 10 * 49 + 48;
|
||||
const QRgb markRgb = colorFromDelphi(0x00FFFF).rgb() & 0xFFFFFFu;
|
||||
if ((static_cast<QRgb>(faceHGr->data.image().pixel(px, py)) & 0xFFFFFFu) == markRgb) {
|
||||
faceHGr->data.image().setPixel(px, py, qRgb(0, 0, 0));
|
||||
constexpr int gray = 0xB8B8B8;
|
||||
imageOpBCC(faceHGr->data, Templates->data, facePix % 10 * 65 + 1, facePix / 10 * 49 + 1, 1, 25, 64, 48,
|
||||
gray, colorToDelphi(color));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Tribe::setModelPicture(const ModelPictureInfo &info, bool isNew)
|
||||
{
|
||||
if (!isNew) {
|
||||
int i = static_cast<int>(pictureList.size()) - 1;
|
||||
while (i >= 0 && pictureList[static_cast<std::size_t>(i)].hash != info.hash)
|
||||
--i;
|
||||
Q_ASSERT(i >= 0);
|
||||
Q_ASSERT(pictureList[static_cast<std::size_t>(i)].hGr == loadGraphicSet(info.grName));
|
||||
Q_ASSERT(pictureList[static_cast<std::size_t>(i)].pix == info.pix);
|
||||
ModelPicture &mp = modelPictureAt(info.mix);
|
||||
mp.hGr = pictureList[static_cast<std::size_t>(i)].hGr;
|
||||
mp.pix = pictureList[static_cast<std::size_t>(i)].pix;
|
||||
modelNameAt(info.mix) = pictureList[static_cast<std::size_t>(i)].modelName;
|
||||
} else {
|
||||
ModelPicture &mp = modelPictureAt(info.mix);
|
||||
mp.hGr = loadGraphicSet(info.grName);
|
||||
mp.pix = info.pix;
|
||||
if (mp.hGr && info.pix >= 0 && info.pix < static_cast<int>(mp.hGr->pixUsed.size()))
|
||||
++mp.hGr->pixUsed[static_cast<std::size_t>(info.pix)];
|
||||
modelNameAt(info.mix).clear();
|
||||
|
||||
bool ok = false;
|
||||
const QString unitsMarker = QStringLiteral("#UNITS ") + QFileInfo(info.grName).completeBaseName();
|
||||
for (const QString &line : m_script) {
|
||||
input = line;
|
||||
if (input == unitsMarker)
|
||||
ok = true;
|
||||
else if (!input.isEmpty() && input.at(0) == QLatin1Char('#'))
|
||||
ok = false;
|
||||
else if (ok && getNum() == info.pix) {
|
||||
get();
|
||||
modelNameAt(info.mix) = get();
|
||||
}
|
||||
}
|
||||
|
||||
if (modelNameAt(info.mix).isEmpty()) {
|
||||
for (const QString &line : stdUnitScript) {
|
||||
input = line;
|
||||
if (getNum() == info.pix) {
|
||||
get();
|
||||
modelNameAt(info.mix) = get();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (info.hash != 0) {
|
||||
ChosenModelPictureInfo newInfo;
|
||||
newInfo.hash = info.hash;
|
||||
newInfo.hGr = modelPictureAt(info.mix).hGr;
|
||||
newInfo.pix = info.pix;
|
||||
newInfo.modelName = modelNameAt(info.mix);
|
||||
pictureList.push_back(newInfo);
|
||||
}
|
||||
}
|
||||
|
||||
ModelPicture &finalMp = modelPictureAt(info.mix);
|
||||
findPosition(finalMp.hGr, finalMp.pix % 10 * 65, finalMp.pix / 10 * 49, 63, 47, colorFromDelphi(0xFFFFFF),
|
||||
finalMp.xShield, finalMp.yShield);
|
||||
}
|
||||
|
||||
bool Tribe::chooseModelPicture(ModelPictureInfo &picture, int code, int turn, bool forceNew)
|
||||
{
|
||||
if (!forceNew && picture.hash > 0) {
|
||||
for (const auto &entry : pictureList) {
|
||||
if (entry.hash == picture.hash) {
|
||||
picture.grName = entry.hGr ? entry.hGr->name : QString();
|
||||
picture.pix = entry.pix;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
picture.pix = 0;
|
||||
ModelPictureInfo testPic = picture;
|
||||
int leastUsed = std::numeric_limits<int>::max();
|
||||
int cnt = 0;
|
||||
GraphicSet *hGr = hGrStdUnits;
|
||||
|
||||
auto check = [&]() {
|
||||
testPic.pix = getNum();
|
||||
if (code == getNum()) {
|
||||
int used;
|
||||
if (forceNew || !hGr) {
|
||||
used = 0;
|
||||
} else {
|
||||
used = (testPic.pix >= 0 && testPic.pix < static_cast<int>(hGr->pixUsed.size()))
|
||||
? 4 * hGr->pixUsed[static_cast<std::size_t>(testPic.pix)]
|
||||
: 0;
|
||||
if (hGr == hGrStdUnits)
|
||||
used += 2;
|
||||
}
|
||||
if (used < leastUsed) {
|
||||
cnt = 0;
|
||||
leastUsed = used;
|
||||
}
|
||||
if (used == leastUsed) {
|
||||
++cnt;
|
||||
if (turn % cnt == 0)
|
||||
picture = testPic;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
testPic.grName = QStringLiteral("StdUnits");
|
||||
for (const QString &line : stdUnitScript) {
|
||||
input = line;
|
||||
check();
|
||||
}
|
||||
|
||||
bool ok = false;
|
||||
for (const QString &line : m_script) {
|
||||
input = line;
|
||||
if (input.left(6) == QLatin1String("#UNITS")) {
|
||||
ok = true;
|
||||
testPic.grName = input.mid(7);
|
||||
hGr = grExt.searchByName(testPic.grName);
|
||||
} else if (!input.isEmpty() && input.at(0) == QLatin1Char('#')) {
|
||||
ok = false;
|
||||
} else if (ok) {
|
||||
check();
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace Tribes
|
||||
} // namespace Cevo
|
||||
Reference in new issue
Block a user