Skip to content

Commit

Permalink
Introduce C4EnumInfo for unified serialization and script constant re…
Browse files Browse the repository at this point in the history
…gistration
  • Loading branch information
maxmitti committed Dec 7, 2024
1 parent 8edc3ef commit 8f0716b
Show file tree
Hide file tree
Showing 34 changed files with 810 additions and 576 deletions.
1 change: 1 addition & 0 deletions cmake/filelists/Engine.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ src/C4Effect.cpp
src/C4Effects.h
src/C4EnumeratedObjectPtr.cpp
src/C4EnumeratedObjectPtr.h
src/C4EnumInfo.h
src/C4Extra.cpp
src/C4Extra.h
src/C4Facet.cpp
Expand Down
2 changes: 2 additions & 0 deletions src/C4AulScriptStrict.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

#include <cstdint>

#include "C4EnumInfo.h"

enum class C4AulScriptStrict : std::uint8_t
{
NONSTRICT = 0,
Expand Down
32 changes: 1 addition & 31 deletions src/C4Command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,6 @@ const int32_t JumpAngle = 35, JumpLowAngle = 80, JumpAngleRange = 10, JumpHighAn
const int32_t FlightAngleRange = 60;
const int32_t LetGoHangleAngle = 110;

StdEnumAdapt<int32_t>::Entry EnumAdaptCommandEntries[C4CMD_Last - C4CMD_First + 2];

const char *CommandName(int32_t iCommand)
{
static const char *szCommandName[] =
{
"None", "Follow", "MoveTo", "Enter", "Exit", "Grab", "Build", "Throw", "Chop",
"UnGrab", "Jump", "Wait", "Get", "Put", "Drop", "Dig", "Activate", "PushTo",
"Construct", "Transfer", "Attack", "Context", "Buy", "Sell", "Acquire",
"Energy", "Retry", "Home", "Call", "Take", "Take2"
};

if (!Inside<int32_t>(iCommand, C4CMD_First, C4CMD_Last)) return "None";

return szCommandName[iCommand];
}

const char *LoadCommandNameResStr(const std::int32_t command)
{
static constexpr C4ResStrTableKeyFormat<> CommandNameIds[]
Expand All @@ -70,19 +53,6 @@ const char *LoadCommandNameResStr(const std::int32_t command)
return LoadResStr(CommandNameIds[command]);
}

bool InitEnumAdaptCommandEntries()
{
for (int32_t i = C4CMD_First; i <= C4CMD_Last; i++)
{
EnumAdaptCommandEntries[i - C4CMD_First].Name = CommandName(i);
EnumAdaptCommandEntries[i - C4CMD_First].Val = i;
}
EnumAdaptCommandEntries[C4CMD_Last - C4CMD_First + 1].Name = nullptr;
return true;
}

const bool InitEnumAdaptCommandEntriesDummy = InitEnumAdaptCommandEntries();

int32_t CommandByName(const char *szCommand)
{
for (int32_t cnt = C4CMD_First; cnt <= C4CMD_Last; cnt++)
Expand Down Expand Up @@ -2384,7 +2354,7 @@ void C4Command::CompileFunc(StdCompiler *pComp)
else
pComp->NoSeparator();
// Command name
pComp->Value(mkEnumAdaptT<uint8_t>(Command, EnumAdaptCommandEntries));
pComp->Value(mkEnumAdapt(Command, C4EnumAdaptPrefixMode::None, C4CMD_EnumInfo));
pComp->Separator(StdCompiler::SEP_SEP);
// Target X/Y
pComp->Value(Tx); pComp->Separator(StdCompiler::SEP_SEP);
Expand Down
43 changes: 35 additions & 8 deletions src/C4Command.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

#include <string>

const int32_t C4CMD_None = 0,
constexpr std::uint8_t C4CMD_None = 0,
C4CMD_Follow = 1,
C4CMD_MoveTo = 2,
C4CMD_Enter = 3,
Expand Down Expand Up @@ -56,22 +56,49 @@ const int32_t C4CMD_None = 0,
C4CMD_Take = 29, // carlo
C4CMD_Take2 = 30; // carlo

const int32_t C4CMD_First = C4CMD_Follow,
constexpr std::uint8_t C4CMD_First = C4CMD_Follow,
C4CMD_Last = C4CMD_Take2; // carlo

const int32_t C4CMD_Mode_SilentSub = 0, // subcommand; failure will cause base to fail (no message in case of failure)
constexpr std::int32_t C4CMD_Mode_SilentSub = 0, // subcommand; failure will cause base to fail (no message in case of failure)
C4CMD_Mode_Base = 1, // regular base command
C4CMD_Mode_SilentBase = 2, // silent base command (no message in case of failure)
C4CMD_Mode_Sub = 3; // subcommand; failure will cause base to fail

// MoveTo and Enter command options: Include push target
const int32_t C4CMD_MoveTo_NoPosAdjust = 1,
constexpr std::int32_t C4CMD_MoveTo_NoPosAdjust = 1,
C4CMD_MoveTo_PushTarget = 2;

const int32_t C4CMD_Enter_PushTarget = 2;
constexpr std::int32_t C4CMD_Enter_PushTarget = 2;

constexpr const char *CommandName(int32_t iCommand) noexcept
{
if (!Inside<int32_t>(iCommand, C4CMD_First, C4CMD_Last)) return "None";

constexpr const char *szCommandName[] =
{
"None", "Follow", "MoveTo", "Enter", "Exit", "Grab", "Build", "Throw", "Chop",
"UnGrab", "Jump", "Wait", "Get", "Put", "Drop", "Dig", "Activate", "PushTo",
"Construct", "Transfer", "Attack", "Context", "Buy", "Sell", "Acquire",
"Energy", "Retry", "Home", "Call", "Take", "Take2"
};

return szCommandName[iCommand];
}

inline constexpr auto C4CMD_EnumInfo = mkEnumInfo<std::uint8_t>("C4CMD_",
[]()
{
struct Values {
C4EnumValue<std::uint8_t> values[C4CMD_Last + 1];
} values;
for (std::uint8_t cmd = 0; cmd <= C4CMD_Last; ++cmd)
{
values.values[cmd] = {cmd, CommandName(cmd)};
}
return values;
}().values
);

const char *CommandName(int32_t iCommand);
C4ResStrTableKey CommandNameID(int32_t iCommand);
int32_t CommandByName(const char *szCommand);

class C4Command
Expand All @@ -82,7 +109,7 @@ class C4Command

public:
C4Object *cObj;
int32_t Command;
std::uint8_t Command;
C4Value Tx;
int32_t Ty;
C4EnumeratedObjectPtr Target, Target2;
Expand Down
57 changes: 32 additions & 25 deletions src/C4Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <C4Application.h>
#include "C4GameControl.h"
#include <C4Log.h>
#include <C4EnumInfo.h>
#include <C4Network2.h>
#include "C4Network2IO.h"
#include "C4Network2Reference.h"
Expand All @@ -42,6 +43,19 @@
#endif

#include <format>
#ifdef C4ENGINE
// put this here, because X11 includes break C4EnumAdaptPrefixMode due to #define None when it is in StdWindow.h

template<>
struct C4EnumInfo<DisplayMode>
{
static inline constexpr auto data = mkEnumInfo<DisplayMode>("",
{
{DisplayMode::Fullscreen, "Fullscreen"},
{DisplayMode::Window, "Window"}
});
};
#endif

bool isGermanSystem()
{
Expand Down Expand Up @@ -118,18 +132,24 @@ void C4ConfigGeneral::CompileFunc(StdCompiler *pComp)

#ifdef C4ENGINE

void C4ConfigDeveloper::ConsoleScriptStrictnessWrapper::CompileFunc(StdCompiler *const comp)
template<>
struct C4EnumInfo<C4AulScriptStrict>
{
StdEnumEntry<C4AulScriptStrict> ConsoleScriptStrictnessValues[] =
{
{"NonStrict", C4AulScriptStrict::NONSTRICT},
{"Strict1", C4AulScriptStrict::STRICT1},
{"Strict2", C4AulScriptStrict::STRICT2},
{"Strict3", C4AulScriptStrict::STRICT3},
{"MaxStrict", MaxStrictSentinel}
};
static inline constexpr auto data = mkEnumInfo<C4AulScriptStrict>("",
{
{C4AulScriptStrict::NONSTRICT, "NonStrict"},
{C4AulScriptStrict::STRICT1, "Strict1"},
{C4AulScriptStrict::STRICT2, "Strict2"},
{C4AulScriptStrict::STRICT3, "Strict3"},
{C4ConfigDeveloper::ConsoleScriptStrictnessWrapper::MaxStrictSentinel, "MaxStrict"}
}
);
};


comp->Value(mkEnumAdaptT<C4AulScriptStrict>(Strictness, ConsoleScriptStrictnessValues));
void C4ConfigDeveloper::ConsoleScriptStrictnessWrapper::CompileFunc(StdCompiler *const comp)
{
comp->Value(mkEnumAdapt(Strictness));

if (comp->isCompiler() && Strictness != MaxStrictSentinel)
{
Expand Down Expand Up @@ -160,14 +180,7 @@ void C4ConfigGraphics::CompileFunc(StdCompiler *pComp)
pComp->Value(mkNamingAdapt(SmokeLevel, "SmokeLevel", 200, false, true));
pComp->Value(mkNamingAdapt(VerboseObjectLoading, "VerboseObjectLoading", 0, false, true));

StdEnumEntry<int32_t> UpperBoardDisplayModes[] =
{
{"Hide", C4UpperBoard::Hide},
{"Full", C4UpperBoard::Full},
{"Small", C4UpperBoard::Small},
{"Mini", C4UpperBoard::Mini}
};
pComp->Value(mkNamingAdapt(mkEnumAdaptT<int32_t>(UpperBoard, UpperBoardDisplayModes), "UpperBoard", C4UpperBoard::Full, false, true));
pComp->Value(mkNamingAdapt(mkEnumAdapt<C4UpperBoard::DisplayMode>(UpperBoard), "UpperBoard", C4UpperBoard::Full, false, true));

pComp->Value(mkNamingAdapt(ShowClock, "ShowClock", false, false, true));
pComp->Value(mkNamingAdapt(ShowCrewNames, "ShowCrewNames", true, false, true));
Expand All @@ -194,13 +207,7 @@ void C4ConfigGraphics::CompileFunc(StdCompiler *pComp)
pComp->Value(mkNamingAdapt(Shader, "Shader", false, false, true));
pComp->Value(mkNamingAdapt(AutoFrameSkip, "AutoFrameSkip", true, false, true));
pComp->Value(mkNamingAdapt(CacheTexturesInRAM, "CacheTexturesInRAM", 100));

StdEnumEntry<DisplayMode> DisplayModes[] =
{
{"Fullscreen", DisplayMode::Fullscreen},
{"Window", DisplayMode::Window}
};
pComp->Value(mkNamingAdapt(mkEnumAdaptT<int>(UseDisplayMode, DisplayModes), "DisplayMode", DisplayMode::Fullscreen, false, true));
pComp->Value(mkNamingAdapt(mkEnumAdapt(UseDisplayMode), "DisplayMode", DisplayMode::Fullscreen, false, true));

#ifdef _WIN32
pComp->Value(mkNamingAdapt(Maximized, "Maximized", false, false, true));
Expand Down
Loading

0 comments on commit 8f0716b

Please sign in to comment.