diff --git a/src/dobjtype.cpp b/src/dobjtype.cpp index d65473b367..fbc85d8602 100644 --- a/src/dobjtype.cpp +++ b/src/dobjtype.cpp @@ -390,7 +390,12 @@ bool PType::VisitedNodeSet::Check(const PType *node) void PType::SetValue(void *addr, int val) { - assert(0 && "Cannot set value for this type"); + assert(0 && "Cannot set int value for this type"); +} + +void PType::SetValue(void *addr, double val) +{ + assert(0 && "Cannot set float value for this type"); } //========================================================================== @@ -405,6 +410,12 @@ int PType::GetValueInt(void *addr) const return 0; } +double PType::GetValueFloat(void *addr) const +{ + assert(0 && "Cannot get value for this type"); + return 0; +} + //========================================================================== // // PType :: GetStoreOp @@ -674,6 +685,11 @@ void PInt::SetValue(void *addr, int val) } } +void PInt::SetValue(void *addr, double val) +{ + SetValue(addr, (int)val); +} + //========================================================================== // // PInt :: GetValueInt @@ -706,6 +722,17 @@ int PInt::GetValueInt(void *addr) const } } +//========================================================================== +// +// PInt :: GetValueFloat +// +//========================================================================== + +double PInt::GetValueFloat(void *addr) const +{ + return GetValueInt(addr); +} + //========================================================================== // // PInt :: GetStoreOp @@ -919,6 +946,11 @@ void PFloat::SetSymbols(const PFloat::SymbolInitI *sym, size_t count) //========================================================================== void PFloat::SetValue(void *addr, int val) +{ + return SetValue(addr, (double)val); +} + +void PFloat::SetValue(void *addr, double val) { assert(((intptr_t)addr & (Align - 1)) == 0 && "unaligned address"); if (Size == 4) @@ -939,16 +971,27 @@ void PFloat::SetValue(void *addr, int val) //========================================================================== int PFloat::GetValueInt(void *addr) const +{ + return xs_ToInt(GetValueFloat(addr)); +} + +//========================================================================== +// +// PFloat :: GetValueFloat +// +//========================================================================== + +double PFloat::GetValueFloat(void *addr) const { assert(((intptr_t)addr & (Align - 1)) == 0 && "unaligned address"); if (Size == 4) { - return xs_ToInt(*(float *)addr); + return *(float *)addr; } else { assert(Size == 8); - return xs_ToInt(*(double *)addr); + return *(double *)addr; } } @@ -2273,22 +2316,23 @@ PClass *PClass::CreateDerivedClass(FName name, unsigned int size) //========================================================================== // -// PClass:: Extend +// PClass :: Extend // -// Add bytes to the end of this class. Returns the previous -// size of the class. +// Add bytes to the end of this class and possibly more to meet +// alignment restrictions. Returns the start of the extended block. // //========================================================================== -unsigned int PClass::Extend(unsigned int extension) +unsigned int PClass::Extend(unsigned int extension, unsigned int alignment) { assert(this->bRuntimeClass); unsigned int oldsize = Size; - Size += extension; + unsigned int padto = (oldsize + alignment - 1) & ~(alignment - 1); + Size = padto + extension; Defaults = (BYTE *)M_Realloc(Defaults, Size); - memset(Defaults + oldsize, 0, extension); - return oldsize; + memset(Defaults + oldsize, 0, Size - oldsize); + return padto; } //========================================================================== diff --git a/src/dobjtype.h b/src/dobjtype.h index 50998509b2..7258579bb7 100644 --- a/src/dobjtype.h +++ b/src/dobjtype.h @@ -188,9 +188,11 @@ public: // Sets the value of a variable of this type at (addr) virtual void SetValue(void *addr, int val); + virtual void SetValue(void *addr, double val); // Gets the value of a variable of this type at (addr) virtual int GetValueInt(void *addr) const; + virtual double GetValueFloat(void *addr) const; // Gets the opcode to store from a register to memory virtual int GetStoreOp() const; @@ -320,7 +322,9 @@ public: PInt(unsigned int size, bool unsign); virtual void SetValue(void *addr, int val); + virtual void SetValue(void *addr, double val); virtual int GetValueInt(void *addr) const; + virtual double GetValueFloat(void *addr) const; virtual int GetStoreOp() const; virtual int GetLoadOp() const; virtual int GetRegType() const; @@ -344,7 +348,9 @@ public: PFloat(unsigned int size); virtual void SetValue(void *addr, int val); + virtual void SetValue(void *addr, double val); virtual int GetValueInt(void *addr) const; + virtual double GetValueFloat(void *addr) const; virtual int GetStoreOp() const; virtual int GetLoadOp() const; virtual int GetRegType() const; @@ -633,7 +639,8 @@ public: void InsertIntoHash(); DObject *CreateNew() const; PClass *CreateDerivedClass(FName name, unsigned int size); - unsigned int Extend(unsigned int extension); + unsigned int Extend(unsigned int extension, unsigned int alignment); + unsigned int Extend(const PType *type) { return Extend(type->Size, type->Align); } void InitializeActorInfo(); void BuildFlatPointers(); const PClass *NativeClass() const; diff --git a/src/p_acs.cpp b/src/p_acs.cpp index 65165494b3..bffe01bfd0 100644 --- a/src/p_acs.cpp +++ b/src/p_acs.cpp @@ -4565,12 +4565,12 @@ int DLevelScript::LineFromID(int id) } } -bool GetVarAddrType(AActor *self, FName varname, int index, void *&addr, PType *&type) +bool GetVarAddrType(AActor *self, FName varname, int index, void *&addr, PType *&type, bool readonly) { PField *var = dyn_cast(self->GetClass()->Symbols.FindSymbol(varname, true)); PArray *arraytype; - if (var == NULL || (var->Flags & VARF_Native)) + if (var == NULL || (!readonly && (var->Flags & VARF_Native))) { return false; } @@ -4593,6 +4593,17 @@ bool GetVarAddrType(AActor *self, FName varname, int index, void *&addr, PType * return false; } addr = baddr; + // We don't want Int subclasses like Name or Color to be accessible, + // but we do want to support Float subclasses like Fixed. + if (!type->IsA(RUNTIME_CLASS(PInt)) || !type->IsKindOf(RUNTIME_CLASS(PFloat))) + { + // For reading, we also support Name and String types. + if (readonly && (type->IsA(RUNTIME_CLASS(PName)) || type->IsA(RUNTIME_CLASS(PString)))) + { + return true; + } + return false; + } return true; } @@ -4601,9 +4612,16 @@ static void SetUserVariable(AActor *self, FName varname, int index, int value) void *addr; PType *type; - if (GetVarAddrType(self, varname, index, addr, type)) + if (GetVarAddrType(self, varname, index, addr, type, false)) { - type->SetValue(addr, value); + if (!type->IsKindOf(RUNTIME_CLASS(PFloat))) + { + type->SetValue(addr, value); + } + else + { + type->SetValue(addr, FIXED2DBL(value)); + } } } @@ -4612,9 +4630,24 @@ static int GetUserVariable(AActor *self, FName varname, int index) void *addr; PType *type; - if (GetVarAddrType(self, varname, index, addr, type)) + if (GetVarAddrType(self, varname, index, addr, type, true)) { - return type->GetValueInt(addr); + if (type->IsKindOf(RUNTIME_CLASS(PFloat))) + { + return FLOAT2FIXED(type->GetValueFloat(addr)); + } + else if (type->IsA(RUNTIME_CLASS(PName))) + { + return GlobalACSStrings.AddString(FName(ENamedName(type->GetValueInt(addr))).GetChars()); + } + else if (type->IsA(RUNTIME_CLASS(PString))) + { + return GlobalACSStrings.AddString(*(FString *)addr); + } + else + { + return type->GetValueInt(addr); + } } return 0; } diff --git a/src/thingdef/thingdef_codeptr.cpp b/src/thingdef/thingdef_codeptr.cpp index e49d661c20..14afa7114d 100644 --- a/src/thingdef/thingdef_codeptr.cpp +++ b/src/thingdef/thingdef_codeptr.cpp @@ -4589,22 +4589,46 @@ DEFINE_ACTION_FUNCTION_PARAMS(AActor, A_SetSpecial) // //=========================================================================== -DEFINE_ACTION_FUNCTION_PARAMS(AActor, A_SetUserVar) +static PField *GetVar(AActor *self, FName varname) { - PARAM_ACTION_PROLOGUE; - PARAM_NAME (varname); - PARAM_INT (value); - PField *var = dyn_cast(self->GetClass()->Symbols.FindSymbol(varname, true)); if (var == NULL || (var->Flags & VARF_Native) || !var->Type->IsKindOf(RUNTIME_CLASS(PBasicType))) { Printf("%s is not a user variable in class %s\n", varname.GetChars(), self->GetClass()->TypeName.GetChars()); - return 0; + return nullptr; } + return var; +} + +DEFINE_ACTION_FUNCTION_PARAMS(AActor, A_SetUserVar) +{ + PARAM_ACTION_PROLOGUE; + PARAM_NAME (varname); + PARAM_INT (value); + // Set the value of the specified user variable. - var->Type->SetValue(reinterpret_cast(self) + var->Offset, value); + PField *var = GetVar(self, varname); + if (var != nullptr) + { + var->Type->SetValue(reinterpret_cast(self) + var->Offset, value); + } + return 0; +} + +DEFINE_ACTION_FUNCTION_PARAMS(AActor, A_SetUserVarFloat) +{ + PARAM_ACTION_PROLOGUE; + PARAM_NAME (varname); + PARAM_FLOAT (value); + + // Set the value of the specified user variable. + PField *var = GetVar(self, varname); + if (var != nullptr) + { + var->Type->SetValue(reinterpret_cast(self) + var->Offset, value); + } return 0; } @@ -4614,13 +4638,8 @@ DEFINE_ACTION_FUNCTION_PARAMS(AActor, A_SetUserVar) // //=========================================================================== -DEFINE_ACTION_FUNCTION_PARAMS(AActor, A_SetUserArray) +static PField *GetArrayVar(AActor *self, FName varname, int pos) { - PARAM_ACTION_PROLOGUE; - PARAM_NAME (varname); - PARAM_INT (pos); - PARAM_INT (value); - PField *var = dyn_cast(self->GetClass()->Symbols.FindSymbol(varname, true)); if (var == NULL || (var->Flags & VARF_Native) || @@ -4629,17 +4648,48 @@ DEFINE_ACTION_FUNCTION_PARAMS(AActor, A_SetUserArray) { Printf("%s is not a user array in class %s\n", varname.GetChars(), self->GetClass()->TypeName.GetChars()); - return 0; + return nullptr; } - PArray *arraytype = static_cast(var->Type); - if ((unsigned)pos >= arraytype->ElementCount) + if ((unsigned)pos >= static_cast(var->Type)->ElementCount) { Printf("%d is out of bounds in array %s in class %s\n", pos, varname.GetChars(), self->GetClass()->TypeName.GetChars()); - return 0; + return nullptr; } + return var; +} + +DEFINE_ACTION_FUNCTION_PARAMS(AActor, A_SetUserArray) +{ + PARAM_ACTION_PROLOGUE; + PARAM_NAME (varname); + PARAM_INT (pos); + PARAM_INT (value); + // Set the value of the specified user array at index pos. - arraytype->ElementType->SetValue(reinterpret_cast(self) + var->Offset + arraytype->ElementSize * pos, value); + PField *var = GetArrayVar(self, varname, pos); + if (var != nullptr) + { + PArray *arraytype = static_cast(var->Type); + arraytype->ElementType->SetValue(reinterpret_cast(self) + var->Offset + arraytype->ElementSize * pos, value); + } + return 0; +} + +DEFINE_ACTION_FUNCTION_PARAMS(AActor, A_SetUserArrayFloat) +{ + PARAM_ACTION_PROLOGUE; + PARAM_NAME (varname); + PARAM_INT (pos); + PARAM_FLOAT (value); + + // Set the value of the specified user array at index pos. + PField *var = GetArrayVar(self, varname, pos); + if (var != nullptr) + { + PArray *arraytype = static_cast(var->Type); + arraytype->ElementType->SetValue(reinterpret_cast(self) + var->Offset + arraytype->ElementSize * pos, value); + } return 0; } diff --git a/src/thingdef/thingdef_parse.cpp b/src/thingdef/thingdef_parse.cpp index 9673fba878..f94bbdf9f9 100644 --- a/src/thingdef/thingdef_parse.cpp +++ b/src/thingdef/thingdef_parse.cpp @@ -523,14 +523,14 @@ static void ParseUserVariable (FScanner &sc, PSymbolTable *symt, PClassActor *cl sc.ScriptError("Native classes may not have user variables"); } - // Read the type and make sure it's int. + // Read the type and make sure it's acceptable. sc.MustGetAnyToken(); - if (sc.TokenType != TK_Int) + if (sc.TokenType != TK_Int && sc.TokenType != TK_Float) { - sc.ScriptMessage("User variables must be of type int"); + sc.ScriptMessage("User variables must be of type 'int' or 'float'"); FScriptPosition::ErrorCounter++; } - type = TypeSInt32; + type = sc.TokenType == TK_Int ? (PType *)TypeSInt32 : (PType *)TypeFloat64; sc.MustGetToken(TK_Identifier); // For now, restrict user variables to those that begin with "user_" to guarantee @@ -577,7 +577,7 @@ static void ParseUserVariable (FScanner &sc, PSymbolTable *symt, PClassActor *cl sc.MustGetToken(';'); PField *sym = new PField(symname, type, 0); - sym->Offset = cls->Extend(sizeof(int) * maxelems); + sym->Offset = cls->Extend(type); if (symt->AddSymbol(sym) == NULL) { delete sym; diff --git a/wadsrc/static/actors/actor.txt b/wadsrc/static/actors/actor.txt index 2bac5795b6..ed5d8ad5d1 100644 --- a/wadsrc/static/actors/actor.txt +++ b/wadsrc/static/actors/actor.txt @@ -275,6 +275,8 @@ ACTOR Actor native //: Thinker action native A_SetArg(int pos, int value); action native A_SetUserVar(name varname, int value); action native A_SetUserArray(name varname, int index, int value); + action native A_SetUserVarFloat(name varname, float value); + action native A_SetUserArrayFloat(name varname, int index, float value); action native A_SetSpecial(int spec, int arg0 = 0, int arg1 = 0, int arg2 = 0, int arg3 = 0, int arg4 = 0); action native A_Quake(int intensity, int duration, int damrad, int tremrad, sound sfx = "world/quake"); action native A_QuakeEx(int intensityX, int intensityY, int intensityZ, int duration, int damrad, int tremrad, sound sfx = "world/quake", int flags = 0, float mulWaveX = 1, float mulWaveY = 1, float mulWaveZ = 1, int falloff = 0, int highpoint = 0);