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

+24
View File
@@ -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
+67
View File
@@ -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
+68
View File
@@ -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
+46
View File
@@ -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