From 7c759f9fcfb1116c716abf4a76dc337684c11c38 Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Wed, 26 Oct 2016 14:04:49 +0200 Subject: [PATCH] - removed the FxDamageValue hack and implemented it properly using FxReturnStatement. - added handling of damage functions for ZScript. --- src/namedef.h | 2 ++ src/scripting/codegeneration/codegen.h | 19 ------------------- src/scripting/decorate/thingdef_parse.cpp | 2 +- src/scripting/thingdef.cpp | 22 ++++++++++++++++++++++ src/scripting/thingdef.h | 1 + src/scripting/thingdef_properties.cpp | 14 ++------------ src/scripting/vm/vmbuilder.cpp | 4 ---- src/scripting/zscript/zcc_compile.cpp | 9 +++++++++ 8 files changed, 37 insertions(+), 36 deletions(-) diff --git a/src/namedef.h b/src/namedef.h index 973557874..3587643e8 100644 --- a/src/namedef.h +++ b/src/namedef.h @@ -727,3 +727,5 @@ xx(__decorate_internal_int__) xx(__decorate_internal_bool__) xx(__decorate_internal_state__) xx(__decorate_internal_float__) + +xx(DamageFunction) diff --git a/src/scripting/codegeneration/codegen.h b/src/scripting/codegeneration/codegen.h index d099daa22..f6468e61f 100644 --- a/src/scripting/codegeneration/codegen.h +++ b/src/scripting/codegeneration/codegen.h @@ -1531,25 +1531,6 @@ public: // //========================================================================== -class FxDamageValue : public FxExpression -{ - FxExpression *val; - -public: - - FxDamageValue(FxExpression *v); - ~FxDamageValue(); - FxExpression *Resolve(FCompileContext&); - - ExpEmit Emit(VMFunctionBuilder *build); -}; - -//========================================================================== -// -// -// -//========================================================================== - class FxNop : public FxExpression { public: diff --git a/src/scripting/decorate/thingdef_parse.cpp b/src/scripting/decorate/thingdef_parse.cpp index bd4885c87..b344a1228 100644 --- a/src/scripting/decorate/thingdef_parse.cpp +++ b/src/scripting/decorate/thingdef_parse.cpp @@ -664,7 +664,7 @@ static bool ParsePropertyParams(FScanner &sc, FPropertyInfo *prop, AActor *defau { conv.i = -1; params.Push(conv); - x = new FxDamageValue(new FxIntCast(ParseExpression(sc, bag.Info), true)); + x = ParseExpression(sc, bag.Info); sc.MustGetStringName(")"); conv.exp = x; params.Push(conv); diff --git a/src/scripting/thingdef.cpp b/src/scripting/thingdef.cpp index 183c3f69e..8c42e8987 100644 --- a/src/scripting/thingdef.cpp +++ b/src/scripting/thingdef.cpp @@ -184,6 +184,28 @@ PFunction *FindClassMemberFunction(PClass *selfcls, PClass *funccls, FName name, return funcsym; } +//========================================================================== +// +// CreateDamageFunction +// +// creates a damage function from the given expression +// +//========================================================================== + +void CreateDamageFunction(PClassActor *info, AActor *defaults, FxExpression *id, bool fromDecorate) +{ + if (id == nullptr) + { + defaults->DamageFunc = nullptr; + } + else + { + auto dmg = new FxReturnStatement(new FxIntCast(id, true), id->ScriptPosition); + auto funcsym = CreateAnonymousFunction(info, TypeSInt32, VARF_Method); + defaults->DamageFunc = FunctionBuildList.AddFunction(funcsym, dmg, FStringf("%s.DamageFunction", info->TypeName.GetChars()), fromDecorate); + } +} + //========================================================================== // // LoadActors diff --git a/src/scripting/thingdef.h b/src/scripting/thingdef.h index 44a441bee..b2d34231d 100644 --- a/src/scripting/thingdef.h +++ b/src/scripting/thingdef.h @@ -155,6 +155,7 @@ FName CheckCastKludges(FName in); void SetImplicitArgs(TArray *args, TArray *argflags, TArray *argnames, PClass *cls, DWORD funcflags); PFunction *CreateAnonymousFunction(PClass *containingclass, PType *returntype, int flags); PFunction *FindClassMemberFunction(PClass *cls, PClass *funccls, FName name, FScriptPosition &sc, bool *error); +void CreateDamageFunction(PClassActor *info, AActor *defaults, FxExpression *id, bool fromDecorate); //========================================================================== // diff --git a/src/scripting/thingdef_properties.cpp b/src/scripting/thingdef_properties.cpp index d3fb452cb..6f1455e43 100644 --- a/src/scripting/thingdef_properties.cpp +++ b/src/scripting/thingdef_properties.cpp @@ -647,19 +647,9 @@ DEFINE_PROPERTY(damage, X, Actor) // compatibility reasons, expressions must be enclosed within // parentheses. - // Store this expression here for now. It will be converted to a function - // later once all actors have been processed. defaults->DamageVal = dmgval; - - if (id == nullptr) - { - defaults->DamageFunc = nullptr; - } - else - { - auto funcsym = CreateAnonymousFunction(bag.Info, TypeSInt32, VARF_Method); - defaults->DamageFunc = FunctionBuildList.AddFunction(funcsym, id, FStringf("%s.DamageFunction", bag.Info->TypeName.GetChars()), !bag.fromZScript); - } + // Only DECORATE can get here with a valid expression. + CreateDamageFunction(bag.Info, defaults, id, true); } //========================================================================== diff --git a/src/scripting/vm/vmbuilder.cpp b/src/scripting/vm/vmbuilder.cpp index c72c2b567..7f8a643c6 100644 --- a/src/scripting/vm/vmbuilder.cpp +++ b/src/scripting/vm/vmbuilder.cpp @@ -684,10 +684,6 @@ void FFunctionBuildList::Build() for (auto &item : mItems) { - if (item.PrintableName.CompareNoCase("Middle.StateFunction.3") == 0) - { - int a = 0; - } assert(item.Code != NULL); // We don't know the return type in advance for anonymous functions. diff --git a/src/scripting/zscript/zcc_compile.cpp b/src/scripting/zscript/zcc_compile.cpp index afc74e75e..f0f884f68 100644 --- a/src/scripting/zscript/zcc_compile.cpp +++ b/src/scripting/zscript/zcc_compile.cpp @@ -1773,8 +1773,16 @@ void ZCCCompiler::ProcessDefaultProperty(PClassActor *cls, ZCC_PropertyStmt *pro if (namenode->SiblingNext == namenode) { + if (namenode->Id == NAME_DamageFunction) + { + auto x = ConvertNode(prop->Values); + CreateDamageFunction(cls, (AActor *)bag.Info->Defaults, x, false); + return; + } + // a one-name property propname = FName(namenode->Id); + } else if (namenode->SiblingNext->SiblingNext == namenode) { @@ -1787,6 +1795,7 @@ void ZCCCompiler::ProcessDefaultProperty(PClassActor *cls, ZCC_PropertyStmt *pro return; } + FPropertyInfo *property = FindProperty(propname); if (property != nullptr && property->category != CAT_INFO)