From c27976b2e5e789be461b17d7f4e158bb00467f9f Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Wed, 26 Jan 2022 00:16:21 +0100 Subject: [PATCH] - added script exports for Duke's iterators --- source/common/scripting/vm/vm.h | 7 + source/games/duke/all.cpp | 1 + source/games/duke/src/game.cpp | 9 + source/games/duke/src/vmiterators.cpp | 196 ++++++++++++++++++ wadsrc/static/zscript/games/duke/dukeactor.zs | 32 +++ 5 files changed, 245 insertions(+) create mode 100644 source/games/duke/src/vmiterators.cpp diff --git a/source/common/scripting/vm/vm.h b/source/common/scripting/vm/vm.h index d0a34a975..4bd342a49 100644 --- a/source/common/scripting/vm/vm.h +++ b/source/common/scripting/vm/vm.h @@ -708,6 +708,13 @@ struct AFuncDesc extern FieldDesc const *const VMGlobal_##name##_HookPtr; \ MSVC_FSEG FieldDesc const *const VMGlobal_##name##_HookPtr GCC_FSEG = &VMGlobal_##name; +#define DEFINE_GLOBAL_UNSIZED(name) \ + static const FieldDesc VMGlobal_##name = { "", #name, (size_t)&name, ~0u, 0 }; \ + extern FieldDesc const *const VMGlobal_##name##_HookPtr; \ + MSVC_FSEG FieldDesc const *const VMGlobal_##name##_HookPtr GCC_FSEG = &VMGlobal_##name; + + + class AActor; diff --git a/source/games/duke/all.cpp b/source/games/duke/all.cpp index c540326e5..9141ff0d4 100644 --- a/source/games/duke/all.cpp +++ b/source/games/duke/all.cpp @@ -19,3 +19,4 @@ #include "src/sbar.cpp" #include "src/sectors.cpp" #include "src/spawn.cpp" +#include "src/vmiterators.cpp" diff --git a/source/games/duke/src/game.cpp b/source/games/duke/src/game.cpp index e274fd16a..f99ae1433 100644 --- a/source/games/duke/src/game.cpp +++ b/source/games/duke/src/game.cpp @@ -398,6 +398,14 @@ void GameInterface::app_init() S_ParseDeveloperCommentary(); } +// For now just a placeholder. Later this will contain all static level data. +struct DukeLevel +{ + +}; + +DukeLevel dlevel; + DEFINE_FIELD(DDukeActor, ownerActor) DEFINE_FIELD(DDukeActor, hitOwnerActor) DEFINE_FIELD(DDukeActor, cgg) @@ -417,4 +425,5 @@ DEFINE_FIELD(DDukeActor, seek_actor) DEFINE_FIELD(DDukeActor, flags1) DEFINE_FIELD(DDukeActor, flags2) +DEFINE_GLOBAL_UNSIZED(dlevel) END_DUKE_NS diff --git a/source/games/duke/src/vmiterators.cpp b/source/games/duke/src/vmiterators.cpp new file mode 100644 index 000000000..3a696fa2e --- /dev/null +++ b/source/games/duke/src/vmiterators.cpp @@ -0,0 +1,196 @@ +//----------------------------------------------------------------------------- +// +// Copyright 2016-2022 Christoph Oelckers +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see http://www.gnu.org/licenses/ +// +//----------------------------------------------------------------------------- +// +// VM iterators +// +// These classes are thin wrappers which wrap the standard iterators into a DObject +// so that the VM can use them +// +//----------------------------------------------------------------------------- + +BEGIN_DUKE_NS + +//========================================================================== +// +// scriptable stat iterator +// +//========================================================================== + +class DDukeStatIterator : public DObject +{ + DECLARE_ABSTRACT_CLASS(DDukeStatIterator, DObject) + +public: + + DukeStatIterator it; + + DDukeStatIterator(int statnum) + : it(statnum) + { + } +}; + +IMPLEMENT_CLASS(DDukeStatIterator, true, false); + +static DDukeStatIterator *CreateStatIterator(int statnum) +{ + return Create(statnum); +} + +DEFINE_ACTION_FUNCTION_NATIVE(_DukeLevel, CreateStatIterator, CreateStatIterator) +{ + PARAM_PROLOGUE; + PARAM_INT(statnum); + ACTION_RETURN_OBJECT(CreateStatIterator(statnum)); +} + +static DDukeActor *NextStat(DDukeStatIterator *self) +{ + return self->it.Next(); +} + +DEFINE_ACTION_FUNCTION_NATIVE(DDukeStatIterator, Next, NextStat) +{ + PARAM_SELF_PROLOGUE(DDukeStatIterator); + ACTION_RETURN_OBJECT(NextStat(self)); +} + +static void ResetStat(DDukeStatIterator *self, int stat) +{ + self->it.Reset(stat); +} + +DEFINE_ACTION_FUNCTION_NATIVE(DDukeStatIterator, Reset, ResetStat) +{ + PARAM_SELF_PROLOGUE(DDukeStatIterator); + PARAM_INT(stat); + ResetStat(self, stat); + return 0; +} + +//========================================================================== +// +// scriptable sector iterator +// +//========================================================================== + +class DDukeSectIterator : public DObject +{ + DECLARE_ABSTRACT_CLASS(DDukeSectIterator, DObject) + +public: + + DukeSectIterator it; + + DDukeSectIterator(int Sectnum) + : it(Sectnum) + { + } +}; + +IMPLEMENT_CLASS(DDukeSectIterator, true, false); + +static DDukeSectIterator *CreateSectIterator(int Sectnum) +{ + return Create(Sectnum); +} + +DEFINE_ACTION_FUNCTION_NATIVE(_DukeLevel, CreateSectorIterator, CreateSectIterator) +{ + PARAM_PROLOGUE; + PARAM_INT(Sectnum); + ACTION_RETURN_OBJECT(CreateSectIterator(Sectnum)); +} + +static DDukeActor *NextSect(DDukeSectIterator *self) +{ + return self->it.Next(); +} + +DEFINE_ACTION_FUNCTION_NATIVE(DDukeSectIterator, Next, NextSect) +{ + PARAM_SELF_PROLOGUE(DDukeSectIterator); + ACTION_RETURN_OBJECT(NextSect(self)); +} + +static void ResetSect(DDukeSectIterator *self, int Sect) +{ + self->it.Reset(Sect); +} + +DEFINE_ACTION_FUNCTION_NATIVE(DDukeSectIterator, Reset, ResetSect) +{ + PARAM_SELF_PROLOGUE(DDukeSectIterator); + PARAM_INT(Sect); + ResetSect(self, Sect); + return 0; +} + +//========================================================================== +// +// scriptable sprite iterator +// +//========================================================================== + +class DDukeSpriteIterator : public DObject +{ + DECLARE_ABSTRACT_CLASS(DDukeSpriteIterator, DObject) + +public: + + DukeSpriteIterator it; +}; + +IMPLEMENT_CLASS(DDukeSpriteIterator, true, false); + +static DDukeSpriteIterator *CreateSpriteIterator() +{ + return Create(); +} + +DEFINE_ACTION_FUNCTION_NATIVE(_DukeLevel, CreateSpriteIterator, CreateSpriteIterator) +{ + PARAM_PROLOGUE; + ACTION_RETURN_OBJECT(CreateSpriteIterator()); +} + +static DDukeActor *NextSprite(DDukeSpriteIterator *self) +{ + return self->it.Next(); +} + +DEFINE_ACTION_FUNCTION_NATIVE(DDukeSpriteIterator, Next, NextSprite) +{ + PARAM_SELF_PROLOGUE(DDukeSpriteIterator); + ACTION_RETURN_OBJECT(NextSprite(self)); +} + +static void ResetSprite(DDukeSpriteIterator *self) +{ + self->it.Reset(); +} + +DEFINE_ACTION_FUNCTION_NATIVE(DDukeSpriteIterator, Reset, ResetSprite) +{ + PARAM_SELF_PROLOGUE(DDukeSpriteIterator); + ResetSprite(self); + return 0; +} + +END_DUKE_NS diff --git a/wadsrc/static/zscript/games/duke/dukeactor.zs b/wadsrc/static/zscript/games/duke/dukeactor.zs index 6f6932e87..8c20c71e1 100644 --- a/wadsrc/static/zscript/games/duke/dukeactor.zs +++ b/wadsrc/static/zscript/games/duke/dukeactor.zs @@ -40,3 +40,35 @@ class DukeActor : CoreActor native flagdef NoInterpolate: flags1, 20; } + +extend struct _ +{ + native DukeLevel dlevel; +} + +// The level struct is a wrapper to group all level related global variables and static functions into one object. +// On the script side we do not really want scattered global data that is publicly accessible. +struct DukeLevel +{ + native static DukeStatIterator CreateStatIterator(int stat); + native static DukeSectIterator CreateSectorIterator(int sect); + native static DukeSpriteIterator CreateSpriteIterator(); +} + +class DukeStatIterator native +{ + native DukeActor Next(); + native void Reset(int stat); +} + +class DukeSectIterator native +{ + native DukeActor Next(); + native void Reset(int sect); +} + +class DukeSpriteIterator native +{ + native DukeActor Next(); + native void Reset(); +}