basic game files port
This commit is contained in:
1 parent
8436242032
commit
9f24650296
10 files changed
+836
-1
No files matched your search
+2
-1
@@ -52,6 +52,7 @@ if(CEVO_WITH_APP)
|
|||||||
add_subdirectory(widget)
|
add_subdirectory(widget)
|
||||||
add_subdirectory(media)
|
add_subdirectory(media)
|
||||||
add_subdirectory(common)
|
add_subdirectory(common)
|
||||||
|
add_subdirectory(game)
|
||||||
add_subdirectory(resources)
|
add_subdirectory(resources)
|
||||||
|
|
||||||
set(TS_FILES cevo-evo_en_US.ts)
|
set(TS_FILES cevo-evo_en_US.ts)
|
||||||
@@ -91,7 +92,7 @@ else()
|
|||||||
qt5_create_translation(QM_FILES ${CMAKE_SOURCE_DIR} ${TS_FILES})
|
qt5_create_translation(QM_FILES ${CMAKE_SOURCE_DIR} ${TS_FILES})
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
target_link_libraries(cevo-evo PRIVATE Qt${QT_VERSION_MAJOR}::Widgets dpiwidgets cevowidgets uosmedia cevocommon cevoai)
|
target_link_libraries(cevo-evo PRIVATE Qt${QT_VERSION_MAJOR}::Widgets dpiwidgets cevowidgets uosmedia cevocommon cevogame cevoai)
|
||||||
|
|
||||||
# dpiwidgets/cevowidgets/uosmedia are installed to lib/cevo-evo (not the
|
# dpiwidgets/cevowidgets/uosmedia are installed to lib/cevo-evo (not the
|
||||||
# default library directory), so the installed binary needs an explicit
|
# default library directory), so the installed binary needs an explicit
|
||||||
|
|||||||
@@ -0,0 +1,45 @@
|
|||||||
|
# CevoGame: C++ port of the C-evo game engine itself (perso/cevo/*.pas, the
|
||||||
|
# unit's top-level files - as opposed to common/ which is Packages/Common's
|
||||||
|
# generic, game-agnostic utility layer). Ported incrementally, file by file;
|
||||||
|
# depends on Qt::Core (QByteArray/QIODevice/QString), not Widgets - the
|
||||||
|
# simulation/model layer has no business depending on a UI toolkit.
|
||||||
|
#
|
||||||
|
# Also depends on cevoai (ai/): perso/cevo/Protocol.pas and the AI-facing
|
||||||
|
# "AI Template/Protocol.pas" share the same core gameplay structs verbatim
|
||||||
|
# (TCity/TUn/TModel/TPlayerContext/TWonderInfo/..., and the sXxx/im*/wo*/go*/
|
||||||
|
# tr*/fa* constants) - they're the same wire format, just handed to two
|
||||||
|
# different consumers upstream. ai/include/Cevo/Protocol.h already has a
|
||||||
|
# complete, exact C++ port of that shared subset (needed there first for the
|
||||||
|
# AI plugin ABI), so game/ reuses it directly via cevoai's include dir
|
||||||
|
# instead of maintaining a second, duplicate copy.
|
||||||
|
|
||||||
|
find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Core)
|
||||||
|
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Core)
|
||||||
|
|
||||||
|
set(CEVO_GAME_HEADERS
|
||||||
|
include/Cevo/CityProcessing.h
|
||||||
|
include/Cevo/CmdList.h
|
||||||
|
include/Cevo/Database.h
|
||||||
|
include/Cevo/IPQ.h
|
||||||
|
)
|
||||||
|
|
||||||
|
set(CEVO_GAME_SOURCES
|
||||||
|
src/CityProcessing.cpp
|
||||||
|
src/CmdList.cpp
|
||||||
|
src/Database.cpp
|
||||||
|
src/IPQ.cpp
|
||||||
|
)
|
||||||
|
|
||||||
|
add_library(cevogame STATIC ${CEVO_GAME_SOURCES} ${CEVO_GAME_HEADERS})
|
||||||
|
|
||||||
|
target_include_directories(cevogame PUBLIC
|
||||||
|
${CMAKE_CURRENT_SOURCE_DIR}/include
|
||||||
|
)
|
||||||
|
|
||||||
|
target_link_libraries(cevogame PUBLIC Qt${QT_VERSION_MAJOR}::Core cevoai)
|
||||||
|
|
||||||
|
set_target_properties(cevogame PROPERTIES
|
||||||
|
CXX_STANDARD 17
|
||||||
|
CXX_STANDARD_REQUIRED ON
|
||||||
|
POSITION_INDEPENDENT_CODE ON
|
||||||
|
)
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "Cevo/Protocol.h"
|
||||||
|
|
||||||
|
namespace Cevo {
|
||||||
|
namespace CityProcessing {
|
||||||
|
|
||||||
|
// Equivalent of CityProcessing.pas (~1743 lines). PORTED INCREMENTALLY: this
|
||||||
|
// is only its "Reporting" section's two simplest functions so far - every
|
||||||
|
// other function in the original (GetCityReport/GetCityReportNew and
|
||||||
|
// everything in "Internal Tile Picking"/"Turn Processing") additionally
|
||||||
|
// needs GetTileInfo/GetUnitReport/Strongest (Database.pas functions not
|
||||||
|
// ported yet) plus several more Database.pas globals (GWonder, Difficulty,
|
||||||
|
// ServerVersion, ...) and gameplay data tables (Imp[], CorrLevel[],
|
||||||
|
// SupportFree[], ...). Add more here only once those are ported too.
|
||||||
|
|
||||||
|
// Equivalent of CityProcessing.pas's GetCityAreaInfo().
|
||||||
|
void getCityAreaInfo(int p, int loc, TCityAreaInfo &cityAreaInfo);
|
||||||
|
|
||||||
|
// Equivalent of CityProcessing.pas's CanCityGrow().
|
||||||
|
bool canCityGrow(int p, int cix);
|
||||||
|
|
||||||
|
} // namespace CityProcessing
|
||||||
|
} // namespace Cevo
|
||||||
@@ -0,0 +1,67 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <QByteArray>
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
|
class QIODevice;
|
||||||
|
|
||||||
|
namespace Cevo {
|
||||||
|
|
||||||
|
constexpr int MaxDataSize = 1024;
|
||||||
|
constexpr int CommandDataElementSize = 4;
|
||||||
|
constexpr std::uint32_t CommandDataElementCountMask = 0xF;
|
||||||
|
constexpr int CommandDataMaxSize = CommandDataElementSize * static_cast<int>(CommandDataElementCountMask);
|
||||||
|
|
||||||
|
// Equivalent of CmdList.pas's TCmdListState.
|
||||||
|
struct CmdListState {
|
||||||
|
int nLog = 0; // used size of the log, in bytes
|
||||||
|
int loadPos = 0; // position in the log when replaying/loading a game
|
||||||
|
int lastMovingUnit = -1;
|
||||||
|
std::uint32_t moveCode = 0;
|
||||||
|
std::uint32_t loadMoveCode = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Equivalent of CmdList.pas's CommandWithData().
|
||||||
|
int commandWithData(int command, int dataSize);
|
||||||
|
|
||||||
|
// Equivalent of CmdList.pas's TCmdList: records/replays the game's command
|
||||||
|
// log as a densely bit-packed byte stream. This is a serialization-
|
||||||
|
// compatibility format (same on-disk save-game / network wire layout as the
|
||||||
|
// original, little-endian only, same as upstream), so - unlike most other
|
||||||
|
// ports in this project - the exact bit layout is preserved verbatim rather
|
||||||
|
// than reimagined with "nicer" C++/Qt idioms.
|
||||||
|
class CmdList {
|
||||||
|
public:
|
||||||
|
CmdList() = default;
|
||||||
|
|
||||||
|
// Command/Player/Subject are out-parameters (Pascal `var`); `data`
|
||||||
|
// points into the log's own backing storage (valid only until the next
|
||||||
|
// mutating call) and is nullptr when the command carries no data.
|
||||||
|
void get(int &command, int &player, int &subject, const void *&data);
|
||||||
|
void getDataChanges(std::uint32_t *data, int dataSize);
|
||||||
|
void put(int command, int player, int subject, const void *data);
|
||||||
|
void putDataChanges(int command, int player, const std::uint32_t *oldData, const std::uint32_t *newData,
|
||||||
|
int dataSize);
|
||||||
|
|
||||||
|
void loadFromFile(QIODevice &file);
|
||||||
|
void saveToFile(QIODevice &file);
|
||||||
|
void appendToFile(QIODevice &file, const CmdListState &oldState);
|
||||||
|
|
||||||
|
void cut();
|
||||||
|
int progress() const;
|
||||||
|
|
||||||
|
const CmdListState &state() const { return m_state; }
|
||||||
|
void setState(const CmdListState &value) { m_state = value; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
void putData(const void *data, int length);
|
||||||
|
void completeMoveCode();
|
||||||
|
void ensurePadded(int requiredSize);
|
||||||
|
|
||||||
|
QByteArray m_log;
|
||||||
|
CmdListState m_state;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace Cevo
|
||||||
@@ -0,0 +1,68 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "Cevo/Protocol.h"
|
||||||
|
|
||||||
|
#include <array>
|
||||||
|
#include <cstdint>
|
||||||
|
|
||||||
|
namespace Cevo {
|
||||||
|
namespace Database {
|
||||||
|
|
||||||
|
// Equivalent of Database.pas's global `var` state and free functions. Kept
|
||||||
|
// as flat global state/free functions - matching the original's own
|
||||||
|
// architecture (a single shared-state game server module) - rather than
|
||||||
|
// wrapped in a class/singleton that wasn't asked for and would be a large,
|
||||||
|
// separate refactor.
|
||||||
|
//
|
||||||
|
// Database.pas is ~4080 lines with heavy dependencies on Protocol.pas's big
|
||||||
|
// gameplay structs (TPlayerContext, TModel, TCity, TUn, ...) - all already
|
||||||
|
// available via Cevo/Protocol.h (see game/CMakeLists.txt: cevogame links
|
||||||
|
// cevoai to reuse its exact port of those shared structs/constants, since
|
||||||
|
// perso/cevo/Protocol.pas and the AI-facing "AI Template/Protocol.pas"
|
||||||
|
// define them identically). Ported INCREMENTALLY regardless: this only
|
||||||
|
// covers globals/functions actually consumed by whichever caller (Database
|
||||||
|
// itself, CityProcessing, ...) has been ported so far. Add more here only
|
||||||
|
// once a new batch needs them.
|
||||||
|
//
|
||||||
|
// `rw[p].City`/`.Un`/`.Model` are pointer-to-array fields (TCityList* etc.)
|
||||||
|
// that stay null until Database's InitGame-equivalent (not ported yet)
|
||||||
|
// allocates them - dereference as `(*rw[p].City)[cix]`, or cache a decayed
|
||||||
|
// `TCity *city = *rw[p].City;` first if indexing repeatedly (same idiom
|
||||||
|
// ai/src/CustomAI.cpp already uses for `myCity`).
|
||||||
|
|
||||||
|
extern int lx;
|
||||||
|
extern int ly;
|
||||||
|
extern int mapSize; // = lx * ly
|
||||||
|
|
||||||
|
using Vicinity8Loc = std::array<int, 8>;
|
||||||
|
using Vicinity21Loc = std::array<int, 28>;
|
||||||
|
|
||||||
|
// player data
|
||||||
|
extern std::array<TPlayerContext, nPl> rw;
|
||||||
|
|
||||||
|
// map data
|
||||||
|
extern std::array<std::uint32_t, lxmax * lymax> realMap;
|
||||||
|
extern std::array<std::int8_t, lxmax * lymax> occupant; // occupying player for each tile
|
||||||
|
extern std::array<std::int8_t, lxmax * lymax> zocMap;
|
||||||
|
extern std::array<std::int32_t, lxmax * lymax> usedByCity; // -1 if not exploited
|
||||||
|
|
||||||
|
// Equivalent of Database.pas's MaskD().
|
||||||
|
void maskD(std::uint32_t *x, int count, std::uint32_t mask);
|
||||||
|
|
||||||
|
// --- Location Navigation ---
|
||||||
|
|
||||||
|
// Relative location, dx in hor and dy in ver direction from loc. Returns -1
|
||||||
|
// if the result would fall outside the map.
|
||||||
|
int dLoc(int loc, int dx, int dy);
|
||||||
|
void dxdy(int loc0, int loc1, int &dx, int &dy);
|
||||||
|
int distance(int loc0, int loc1);
|
||||||
|
void v8ToLoc(int loc0, Vicinity8Loc &vicinityLoc);
|
||||||
|
void v21ToLoc(int loc0, Vicinity21Loc &vicinityLoc);
|
||||||
|
|
||||||
|
// --- Map Creation ---
|
||||||
|
|
||||||
|
void calculatePrimitive();
|
||||||
|
bool mapGeneratorAvailable();
|
||||||
|
|
||||||
|
} // namespace Database
|
||||||
|
} // namespace Cevo
|
||||||
@@ -0,0 +1,46 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
namespace Cevo {
|
||||||
|
|
||||||
|
// Equivalent of IPQ.pas's TIPQ ("Rassim Eminli"'s indexed binary-heap
|
||||||
|
// priority queue): each item in [0, max) can be in the heap at most once.
|
||||||
|
// Used for Dijkstra/Prim-style map pathfinding (Database.pas/
|
||||||
|
// UnitProcessing.pas construct one sized to the map's tile count).
|
||||||
|
// Backed by std::vector instead of the original's GetMem/FreeMem raw
|
||||||
|
// pointer arrays - same fixed-capacity-preallocated-once shape, but with
|
||||||
|
// automatic RAII cleanup instead of a manual destructor.
|
||||||
|
class IPQ {
|
||||||
|
public:
|
||||||
|
explicit IPQ(int max);
|
||||||
|
|
||||||
|
// Equivalent of TIPQ.Empty - renamed to avoid confusion with the
|
||||||
|
// query-like `empty()` naming convention elsewhere (this resets the
|
||||||
|
// queue, it doesn't report whether it's empty).
|
||||||
|
void clear();
|
||||||
|
|
||||||
|
// Inserts `item` with `value`, or decreases its value (classic
|
||||||
|
// decrease-key) if already present with a larger one. Returns false if
|
||||||
|
// `item` was already present with a value <= the new one. O(log n).
|
||||||
|
bool put(int item, int value);
|
||||||
|
|
||||||
|
// Returns what put() would return, without changing anything.
|
||||||
|
bool testPut(int item, int value) const;
|
||||||
|
|
||||||
|
// Removes and returns (via out-params) the item with the smallest
|
||||||
|
// value. Returns false if the queue is empty. O(log n).
|
||||||
|
bool get(int &item, int &value);
|
||||||
|
|
||||||
|
private:
|
||||||
|
struct HeapItem {
|
||||||
|
int item;
|
||||||
|
int value;
|
||||||
|
};
|
||||||
|
|
||||||
|
int m_size = 0;
|
||||||
|
std::vector<HeapItem> m_heap;
|
||||||
|
std::vector<int> m_index; // m_index[item] = position in m_heap, or -1
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace Cevo
|
||||||
@@ -0,0 +1,42 @@
|
|||||||
|
#include "Cevo/CityProcessing.h"
|
||||||
|
#include "Cevo/Database.h"
|
||||||
|
|
||||||
|
namespace Cevo {
|
||||||
|
namespace CityProcessing {
|
||||||
|
|
||||||
|
void getCityAreaInfo(int p, int loc, TCityAreaInfo &cityAreaInfo)
|
||||||
|
{
|
||||||
|
// Original also has `{$IFOPT O-}Assert(1 shl P and InvalidTreatyMap = 0)`
|
||||||
|
// here - a debug-only overflow-checking assertion with no effect on
|
||||||
|
// release behavior, not ported.
|
||||||
|
Database::Vicinity21Loc radius;
|
||||||
|
Database::v21ToLoc(loc, radius);
|
||||||
|
for (int v21 = 0; v21 <= 26; ++v21) {
|
||||||
|
const int loc1 = radius[v21];
|
||||||
|
if (loc1 < 0 || loc1 >= Database::mapSize) {
|
||||||
|
cityAreaInfo.Available[v21] = faInvalid;
|
||||||
|
} else {
|
||||||
|
const int p1 = static_cast<int>(Database::realMap[loc1] >> 27);
|
||||||
|
if (p1 < nPl && p1 != p && Database::rw[p].Treaty[p1] >= trPeace)
|
||||||
|
cityAreaInfo.Available[v21] = faTreaty;
|
||||||
|
else if (Database::zocMap[loc1] > 0 && Database::occupant[loc1] != p &&
|
||||||
|
Database::rw[p].Treaty[Database::occupant[loc1]] < trAlliance)
|
||||||
|
cityAreaInfo.Available[v21] = faSiege;
|
||||||
|
else if (Database::usedByCity[loc1] != -1 && Database::usedByCity[loc1] != loc)
|
||||||
|
cityAreaInfo.Available[v21] = faNotAvailable;
|
||||||
|
else
|
||||||
|
cityAreaInfo.Available[v21] = faAvailable;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool canCityGrow(int p, int cix)
|
||||||
|
{
|
||||||
|
const TCity &city = (*Database::rw[p].City)[cix];
|
||||||
|
return city.Size < MaxCitySize &&
|
||||||
|
(city.Size < NeedAqueductSize || (city.Built[imAqueduct] == 1 && city.Size < NeedSewerSize) ||
|
||||||
|
city.Built[imSewer] == 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace CityProcessing
|
||||||
|
} // namespace Cevo
|
||||||
@@ -0,0 +1,295 @@
|
|||||||
|
#include "Cevo/CmdList.h"
|
||||||
|
#include "Cevo/Protocol.h"
|
||||||
|
|
||||||
|
#include <QIODevice>
|
||||||
|
#include <QString>
|
||||||
|
|
||||||
|
#include <cstring>
|
||||||
|
|
||||||
|
namespace Cevo {
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
// Unaligned 32-bit little-endian access, replacing the original's direct
|
||||||
|
// `Cardinal((@LogData[Pos])^)` pointer-cast reads/writes - this format is
|
||||||
|
// little-endian-only (same assumption the original already made, since a
|
||||||
|
// raw pointer cast only behaves this way on the little-endian hosts C-evo
|
||||||
|
// actually ships for).
|
||||||
|
std::uint32_t readU32(const QByteArray &buffer, int pos)
|
||||||
|
{
|
||||||
|
std::uint32_t value;
|
||||||
|
std::memcpy(&value, buffer.constData() + pos, sizeof(value));
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
void writeU32(QByteArray &buffer, int pos, std::uint32_t value)
|
||||||
|
{
|
||||||
|
std::memcpy(buffer.data() + pos, &value, sizeof(value));
|
||||||
|
}
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
int commandWithData(int command, int dataSize)
|
||||||
|
{
|
||||||
|
if (dataSize > CommandDataMaxSize) {
|
||||||
|
throw std::out_of_range(
|
||||||
|
QString("Command data size %1 out of range (0-%2).").arg(dataSize).arg(CommandDataMaxSize).toStdString());
|
||||||
|
}
|
||||||
|
const int dataElementCount = (dataSize + CommandDataElementSize - 1) / CommandDataElementSize; // Ceil
|
||||||
|
return command | (dataElementCount & static_cast<int>(CommandDataElementCountMask));
|
||||||
|
}
|
||||||
|
|
||||||
|
void CmdList::ensurePadded(int requiredSize)
|
||||||
|
{
|
||||||
|
// Get() always reads a full 32-bit word even for record formats that use
|
||||||
|
// only 1-3 of its bytes near the end of the log, so the backing buffer
|
||||||
|
// must always stay at least 4 bytes larger than any position it writes
|
||||||
|
// to or reads from (the original relied on ReallocMem's large, chunk-
|
||||||
|
// rounded over-allocation for this same safety margin).
|
||||||
|
const int padded = requiredSize + 4;
|
||||||
|
if (padded > m_log.size()) {
|
||||||
|
const int oldSize = m_log.size();
|
||||||
|
m_log.resize(padded);
|
||||||
|
std::memset(m_log.data() + oldSize, 0, padded - oldSize);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void CmdList::get(int &command, int &player, int &subject, const void *&data)
|
||||||
|
{
|
||||||
|
if (m_state.loadMoveCode > 0) {
|
||||||
|
player = -1;
|
||||||
|
std::uint32_t dirCode;
|
||||||
|
if (m_state.loadMoveCode & 1) { // FM
|
||||||
|
dirCode = (m_state.loadMoveCode >> 1) & 7;
|
||||||
|
subject = m_state.lastMovingUnit;
|
||||||
|
m_state.loadMoveCode >>= 4;
|
||||||
|
} else { // M
|
||||||
|
dirCode = (m_state.loadMoveCode >> 3) & 7;
|
||||||
|
subject = static_cast<int>((m_state.loadMoveCode >> 6) & 0xFFF);
|
||||||
|
m_state.loadMoveCode >>= 18;
|
||||||
|
m_state.lastMovingUnit = subject;
|
||||||
|
}
|
||||||
|
switch (dirCode) {
|
||||||
|
case 0: command = sMoveUnit + 0x090; break;
|
||||||
|
case 1: command = sMoveUnit + 0x0F0; break;
|
||||||
|
case 2: command = sMoveUnit + 0x390; break;
|
||||||
|
case 3: command = sMoveUnit + 0x3F0; break;
|
||||||
|
case 4: command = sMoveUnit + 0x020; break;
|
||||||
|
case 5: command = sMoveUnit + 0x060; break;
|
||||||
|
case 6: command = sMoveUnit + 0x100; break;
|
||||||
|
case 7: command = sMoveUnit + 0x300; break;
|
||||||
|
}
|
||||||
|
data = nullptr;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
ensurePadded(m_state.loadPos);
|
||||||
|
const std::uint32_t code = readU32(m_log, m_state.loadPos);
|
||||||
|
if ((code & 3) == 0) { // non-clientex command
|
||||||
|
command = static_cast<int>(((code >> 2) & 0x3FFF) + sExecute);
|
||||||
|
player = static_cast<int>((code >> 16) & 0xF);
|
||||||
|
subject = static_cast<int>((code >> 20) & 0xFFF);
|
||||||
|
m_state.loadPos += 4;
|
||||||
|
} else if ((code & 7) == 2) { // clientex command
|
||||||
|
command = static_cast<int>((code >> 3) & 0xFFFF);
|
||||||
|
player = static_cast<int>((code >> 19) & 0xF);
|
||||||
|
subject = 0;
|
||||||
|
m_state.loadPos += 3;
|
||||||
|
} else { // move command shortcut
|
||||||
|
if ((code & 1) && (code & (7u << 4)) != (6u << 4)) {
|
||||||
|
m_state.loadMoveCode = code & 0xFF;
|
||||||
|
m_state.loadPos += 1;
|
||||||
|
} else {
|
||||||
|
m_state.loadMoveCode = code & 0xFFFFFF;
|
||||||
|
m_state.loadPos += 3;
|
||||||
|
}
|
||||||
|
get(command, player, subject, data);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((command & static_cast<int>(CommandDataElementCountMask)) == 0) {
|
||||||
|
data = nullptr;
|
||||||
|
} else {
|
||||||
|
ensurePadded(m_state.loadPos);
|
||||||
|
data = m_log.constData() + m_state.loadPos;
|
||||||
|
m_state.loadPos += (command & static_cast<int>(CommandDataElementCountMask)) * CommandDataElementSize;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void CmdList::getDataChanges(std::uint32_t *data, int dataSize)
|
||||||
|
{
|
||||||
|
ensurePadded(m_state.loadPos);
|
||||||
|
std::uint32_t map0 = readU32(m_log, m_state.loadPos);
|
||||||
|
m_state.loadPos += 4;
|
||||||
|
int b0 = 0;
|
||||||
|
while (map0 > 0) {
|
||||||
|
if (map0 & 1) {
|
||||||
|
ensurePadded(m_state.loadPos);
|
||||||
|
std::uint32_t map1 = readU32(m_log, m_state.loadPos);
|
||||||
|
m_state.loadPos += 4;
|
||||||
|
for (int b1 = 0; b1 < 32; ++b1) {
|
||||||
|
if ((1u << b1) & map1) {
|
||||||
|
ensurePadded(m_state.loadPos);
|
||||||
|
if (b0 * 32 + b1 < dataSize)
|
||||||
|
data[b0 * 32 + b1] = readU32(m_log, m_state.loadPos);
|
||||||
|
m_state.loadPos += 4;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
++b0;
|
||||||
|
map0 >>= 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void CmdList::put(int command, int player, int subject, const void *data)
|
||||||
|
{
|
||||||
|
if ((command & 0xFC00) == sMoveUnit) { // move command shortcut
|
||||||
|
std::uint32_t dirCode = 0;
|
||||||
|
switch (command) {
|
||||||
|
case sMoveUnit + 0x090: dirCode = 0; break;
|
||||||
|
case sMoveUnit + 0x0F0: dirCode = 1; break;
|
||||||
|
case sMoveUnit + 0x390: dirCode = 2; break;
|
||||||
|
case sMoveUnit + 0x3F0: dirCode = 3; break;
|
||||||
|
case sMoveUnit + 0x020: dirCode = 4; break;
|
||||||
|
case sMoveUnit + 0x060: dirCode = 5; break;
|
||||||
|
case sMoveUnit + 0x100: dirCode = 6; break;
|
||||||
|
case sMoveUnit + 0x300: dirCode = 7; break;
|
||||||
|
}
|
||||||
|
std::uint32_t code;
|
||||||
|
if (subject == m_state.lastMovingUnit)
|
||||||
|
code = 1 + (dirCode << 1);
|
||||||
|
else
|
||||||
|
code = 6 + (dirCode << 3) + (static_cast<std::uint32_t>(subject) << 6);
|
||||||
|
|
||||||
|
if (m_state.moveCode == 0) {
|
||||||
|
m_state.moveCode = code;
|
||||||
|
} else if (m_state.moveCode & 1) { // FM + this
|
||||||
|
m_state.moveCode += code << 4;
|
||||||
|
putData(&m_state.moveCode, (code & 1) ? 1 : 3); // FM+FM : FM+M
|
||||||
|
m_state.moveCode = 0;
|
||||||
|
} else if (code & 1) { // M + FM
|
||||||
|
m_state.moveCode += code << 18;
|
||||||
|
putData(&m_state.moveCode, 3);
|
||||||
|
m_state.moveCode = 0;
|
||||||
|
} else { // M + M
|
||||||
|
putData(&m_state.moveCode, 3);
|
||||||
|
m_state.moveCode = code;
|
||||||
|
}
|
||||||
|
m_state.lastMovingUnit = subject;
|
||||||
|
} else {
|
||||||
|
completeMoveCode();
|
||||||
|
std::uint32_t code;
|
||||||
|
if (command >= cClientEx) {
|
||||||
|
code = 2 + (static_cast<std::uint32_t>(command) << 3) + (static_cast<std::uint32_t>(player) << 19);
|
||||||
|
putData(&code, 3);
|
||||||
|
} else {
|
||||||
|
code = (static_cast<std::uint32_t>(command - sExecute) << 2) +
|
||||||
|
(static_cast<std::uint32_t>(player) << 16) + (static_cast<std::uint32_t>(subject) << 20);
|
||||||
|
putData(&code, 4);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (command & static_cast<int>(CommandDataElementCountMask))
|
||||||
|
putData(data, (command & static_cast<int>(CommandDataElementCountMask)) * CommandDataElementSize);
|
||||||
|
}
|
||||||
|
|
||||||
|
void CmdList::putDataChanges(int command, int player, const std::uint32_t *oldData, const std::uint32_t *newData,
|
||||||
|
int dataSize)
|
||||||
|
{
|
||||||
|
if (dataSize <= 0)
|
||||||
|
return;
|
||||||
|
if (dataSize > MaxDataSize)
|
||||||
|
dataSize = MaxDataSize;
|
||||||
|
completeMoveCode();
|
||||||
|
|
||||||
|
int mapPos = m_state.nLog + 8;
|
||||||
|
int logPos = mapPos + 4;
|
||||||
|
std::uint32_t map0 = 0;
|
||||||
|
for (int b0 = 0; b0 <= (dataSize - 1) / 32; ++b0) {
|
||||||
|
ensurePadded(logPos + 4 * 32);
|
||||||
|
map0 >>= 1;
|
||||||
|
std::uint32_t map1 = 0;
|
||||||
|
int rowEnd = dataSize - 1;
|
||||||
|
if (rowEnd > b0 * 32 + 31)
|
||||||
|
rowEnd = b0 * 32 + 31;
|
||||||
|
for (int b1 = b0 * 32; b1 <= rowEnd; ++b1) {
|
||||||
|
map1 >>= 1;
|
||||||
|
if (newData[b1] != oldData[b1]) {
|
||||||
|
writeU32(m_log, logPos, newData[b1]);
|
||||||
|
logPos += 4;
|
||||||
|
map1 += 0x80000000u;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (map1 > 0) {
|
||||||
|
map1 >>= (b0 * 32 + 31 - rowEnd);
|
||||||
|
writeU32(m_log, mapPos, map1);
|
||||||
|
mapPos = logPos;
|
||||||
|
logPos += 4;
|
||||||
|
map0 += 0x80000000u;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (map0 == 0)
|
||||||
|
return; // no changes
|
||||||
|
|
||||||
|
map0 >>= (31 - (dataSize - 1) / 32);
|
||||||
|
writeU32(m_log, m_state.nLog + 4, map0);
|
||||||
|
const std::uint32_t code =
|
||||||
|
(static_cast<std::uint32_t>(command - sExecute) << 2) + (static_cast<std::uint32_t>(player) << 16);
|
||||||
|
writeU32(m_log, m_state.nLog, code);
|
||||||
|
m_state.nLog = mapPos;
|
||||||
|
}
|
||||||
|
|
||||||
|
void CmdList::putData(const void *data, int length)
|
||||||
|
{
|
||||||
|
ensurePadded(m_state.nLog + length);
|
||||||
|
std::memcpy(m_log.data() + m_state.nLog, data, length);
|
||||||
|
m_state.nLog += length;
|
||||||
|
}
|
||||||
|
|
||||||
|
void CmdList::completeMoveCode()
|
||||||
|
{
|
||||||
|
if (m_state.moveCode > 0) {
|
||||||
|
putData(&m_state.moveCode, (m_state.moveCode & 1) ? 1 : 3); // Single FM : Single M
|
||||||
|
m_state.moveCode = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void CmdList::loadFromFile(QIODevice &file)
|
||||||
|
{
|
||||||
|
std::int32_t nLog = 0;
|
||||||
|
file.read(reinterpret_cast<char *>(&nLog), 4);
|
||||||
|
m_state.nLog = nLog;
|
||||||
|
m_log = QByteArray(nLog + 4, '\0'); // zero-filled trailing slack, see ensurePadded()
|
||||||
|
file.read(m_log.data(), nLog);
|
||||||
|
m_state.loadPos = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void CmdList::saveToFile(QIODevice &file)
|
||||||
|
{
|
||||||
|
completeMoveCode();
|
||||||
|
const std::int32_t nLog = m_state.nLog;
|
||||||
|
file.write(reinterpret_cast<const char *>(&nLog), 4);
|
||||||
|
file.write(m_log.constData(), nLog);
|
||||||
|
}
|
||||||
|
|
||||||
|
void CmdList::appendToFile(QIODevice &file, const CmdListState &oldState)
|
||||||
|
{
|
||||||
|
completeMoveCode();
|
||||||
|
const std::int32_t nLog = m_state.nLog;
|
||||||
|
file.write(reinterpret_cast<const char *>(&nLog), 4);
|
||||||
|
file.seek(file.pos() + oldState.nLog);
|
||||||
|
file.write(m_log.constData() + oldState.nLog, m_state.nLog - oldState.nLog);
|
||||||
|
}
|
||||||
|
|
||||||
|
void CmdList::cut()
|
||||||
|
{
|
||||||
|
m_state.nLog = m_state.loadPos;
|
||||||
|
}
|
||||||
|
|
||||||
|
int CmdList::progress() const
|
||||||
|
{
|
||||||
|
if (m_state.loadPos == m_state.nLog && m_state.loadMoveCode == 0)
|
||||||
|
return 1000; // loading complete
|
||||||
|
if (m_state.nLog > (1 << 20))
|
||||||
|
return (m_state.loadPos >> 8) * 999 / (m_state.nLog >> 8);
|
||||||
|
return m_state.loadPos * 999 / m_state.nLog;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace Cevo
|
||||||
@@ -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
|
||||||
@@ -0,0 +1,87 @@
|
|||||||
|
#include "Cevo/IPQ.h"
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
|
namespace Cevo {
|
||||||
|
|
||||||
|
IPQ::IPQ(int max) : m_heap(static_cast<std::size_t>(max)), m_index(static_cast<std::size_t>(max), -1)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void IPQ::clear()
|
||||||
|
{
|
||||||
|
if (m_size != 0) {
|
||||||
|
std::fill(m_index.begin(), m_index.end(), -1);
|
||||||
|
m_size = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parent(i) = (i-1)/2.
|
||||||
|
bool IPQ::put(int item, int value)
|
||||||
|
{
|
||||||
|
int i = m_index[item];
|
||||||
|
if (i >= 0) {
|
||||||
|
if (m_heap[i].value <= value)
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
|
i = m_size;
|
||||||
|
++m_size;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (i > 0) {
|
||||||
|
const int j = (i - 1) >> 1; // Parent(i) = (i-1)/2
|
||||||
|
if (value >= m_heap[j].value)
|
||||||
|
break;
|
||||||
|
m_heap[i] = m_heap[j];
|
||||||
|
m_index[m_heap[i].item] = i;
|
||||||
|
i = j;
|
||||||
|
}
|
||||||
|
// Insert the new item at the insertion point found.
|
||||||
|
m_heap[i].value = value;
|
||||||
|
m_heap[i].item = item;
|
||||||
|
m_index[item] = i;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool IPQ::testPut(int item, int value) const
|
||||||
|
{
|
||||||
|
const int i = m_index[item];
|
||||||
|
return i < 0 || m_heap[i].value > value;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Left(i) = 2*i+1, Right(i) = Left(i)+1.
|
||||||
|
bool IPQ::get(int &item, int &value)
|
||||||
|
{
|
||||||
|
if (m_size == 0)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
item = m_heap[0].item;
|
||||||
|
value = m_heap[0].value;
|
||||||
|
m_index[item] = -1;
|
||||||
|
|
||||||
|
--m_size;
|
||||||
|
if (m_size > 0) {
|
||||||
|
const HeapItem last = m_heap[m_size];
|
||||||
|
int i = 0;
|
||||||
|
int j = 1;
|
||||||
|
while (j < m_size) {
|
||||||
|
// Right(i) = Left(i)+1
|
||||||
|
if (j < m_size - 1 && m_heap[j].value > m_heap[j + 1].value)
|
||||||
|
++j;
|
||||||
|
if (last.value <= m_heap[j].value)
|
||||||
|
break;
|
||||||
|
|
||||||
|
m_heap[i] = m_heap[j];
|
||||||
|
m_index[m_heap[i].item] = i;
|
||||||
|
i = j;
|
||||||
|
j = (j << 1) + 1; // Left(j) = 2*j+1
|
||||||
|
}
|
||||||
|
|
||||||
|
// Insert the former last element in the correct place in the heap.
|
||||||
|
m_heap[i] = last;
|
||||||
|
m_index[last.item] = i;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace Cevo
|
||||||
Reference in new issue
Block a user