Skip to content

Commit

Permalink
Rename, fix and add section script functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Fulgen301 committed Nov 15, 2023
1 parent b6214f4 commit a9ae9dd
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 16 deletions.
16 changes: 12 additions & 4 deletions src/C4Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2507,20 +2507,28 @@ bool C4Game::InitKeyboard()
return true;
}

bool C4Game::CreateScenarioSection(const char *const name)
std::int32_t C4Game::CreateSection(const char *const name)
{
auto section = std::make_unique<C4Section>(name);

if (section->InitSection(ScenarioFile))
{
const auto size = static_cast<std::int32_t>(Sections.size());
Sections.emplace_back(std::move(section));

return Sections.back()->InitMaterialTexture()
if (Sections.back()->InitMaterialTexture()
&& Sections.back()->InitSecondPart()
&& Sections.back()->InitThirdPart();
&& Sections.back()->InitThirdPart())
{
return size;
}
else
{
Sections.pop_back();
}
}

return false;
return -1;
}

bool C4Game::InitSystem()
Expand Down
2 changes: 1 addition & 1 deletion src/C4Game.h
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ class C4Game
bool SlowDown();
bool InitKeyboard(); // register main keyboard input functions

bool CreateScenarioSection(const char *name);
std::int32_t CreateSection(const char *name);

protected:
bool InitSystem();
Expand Down
38 changes: 27 additions & 11 deletions src/C4Script.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6120,22 +6120,22 @@ static void FnSetRestoreInfos(C4AulContext *ctx, C4ValueInt what)
Game.RestartRestoreInfos.What = static_cast<std::underlying_type_t<C4NetworkRestartInfos::RestoreInfo>>(what);
}

static bool FnCreateScenarioSection(C4AulContext *ctx, C4String *name)
static C4ValueInt FnCreateSection(C4AulContext *ctx, C4String *name)
{
if (const char *const par{FnStringPar(name)}; par)
{
return Game.CreateScenarioSection(par);
return Game.CreateSection(par);
}

return false;
return -1;
}

static C4ValueInt FnGetScenarioSectionCount(C4AulContext *ctx)
static C4ValueInt FnGetSectionCount(C4AulContext *ctx)
{
return static_cast<C4ValueInt>(Game.Sections.size());
}

static C4ValueInt FnGetScenarioSectionByName(C4AulContext *ctx, C4String *name, std::optional<C4ValueInt> index)
static C4ValueInt FnGetSectionByName(C4AulContext *ctx, C4String *name, std::optional<C4ValueInt> index)
{
if (!name) return false;

Expand All @@ -6162,7 +6162,7 @@ static C4ValueInt FnGetScenarioSectionByName(C4AulContext *ctx, C4String *name,
return -1;
}

static bool FnMoveObjectToSection(C4AulContext *ctx, C4ValueInt targetSection, C4Object *obj)
static bool FnMoveToSection(C4AulContext *ctx, C4ValueInt targetSection, C4Object *obj)
{
if (!obj) if (!(obj = ctx->Obj)) return false;
C4Section *const section{Game.GetSectionByIndex(targetSection)};
Expand All @@ -6176,11 +6176,26 @@ static bool FnMoveObjectToSection(C4AulContext *ctx, C4ValueInt targetSection, C
return true;
}

C4ValueInt FnGetSection(C4AulContext *ctx, C4Object *obj)
C4ValueInt FnGetSection(C4AulContext *ctx)
{
return Game.GetSectionIndex(ctx->GetSection());
}

bool FnSwitchToSection(C4AulContext *ctx, C4ValueInt sectionIndex)
{
if (!ctx->Caller) return false;

C4Section *const section{Game.GetSectionByIndex(sectionIndex)};
if (!section)
{
return false;
}

ctx->Caller->Section = section;

return true;
}

template<std::size_t ParCount>
class C4AulEngineFuncHelper : public C4AulFunc
{
Expand Down Expand Up @@ -7102,11 +7117,12 @@ void InitFunctionMap(C4AulScriptEngine *pEngine)
AddFunc(pEngine, "GetKeys", FnGetKeys);
AddFunc(pEngine, "GetValues", FnGetValues);
AddFunc(pEngine, "SetRestoreInfos", FnSetRestoreInfos);
AddFunc(pEngine, "CreateScenarioSection", FnCreateScenarioSection);
AddFunc(pEngine, "GetScenarioSectionCount", FnGetScenarioSectionCount);
AddFunc(pEngine, "GetScenarioSectionByName", FnGetScenarioSectionByName);
AddFunc(pEngine, "MoveObjectToSection", FnMoveObjectToSection);
AddFunc(pEngine, "CreateSection", FnCreateSection);
AddFunc(pEngine, "GetSectionCount", FnGetSectionCount);
AddFunc(pEngine, "GetSectionByName", FnGetSectionByName);
AddFunc(pEngine, "MoveToSection", FnMoveToSection);
AddFunc(pEngine, "GetSection", FnGetSection);
AddFunc(pEngine, "SwitchToSection", FnSwitchToSection);
new C4AulDefCastFunc<C4V_C4ID, C4V_Int>{pEngine, "ScoreboardCol"};
new C4AulDefCastFunc<C4V_Any, C4V_Int>{pEngine, "CastInt"};
new C4AulDefCastFunc<C4V_Any, C4V_Bool>{pEngine, "CastBool"};
Expand Down

0 comments on commit a9ae9dd

Please sign in to comment.