From 0e70e95fa42c31acc44310c1e7a9d1af9a25bc98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Lu=C3=ADs=20Vaz=20Silva?= Date: Wed, 1 Feb 2023 12:30:21 -0300 Subject: [PATCH] Add Inventory::ModifyBob and Inventory::ModifyBob3D --- src/common/scripting/vm/vm.h | 12 +++++++++ src/playsim/p_pspr.cpp | 26 +++++++++++++++++++ .../zscript/actors/inventory/inventory.zs | 4 +++ 3 files changed, 42 insertions(+) diff --git a/src/common/scripting/vm/vm.h b/src/common/scripting/vm/vm.h index 4b9334c0482..73c4b48bbdd 100644 --- a/src/common/scripting/vm/vm.h +++ b/src/common/scripting/vm/vm.h @@ -830,4 +830,16 @@ unsigned GetVirtualIndex(PClass *cls, const char *funcname); VMFunction *func = clss->Virtuals.Size() > VIndex? clss->Virtuals[VIndex] : nullptr; \ if (func != nullptr) +#define IFOVERRIDENVIRTUALPTRNAME(self, clsname, funcname) \ + static VMFunction *orig_func = nullptr; \ + static unsigned VIndex = ~0u; \ + if (VIndex == ~0u) { \ + PClass *cls = PClass::FindClass(clsname); \ + VIndex = GetVirtualIndex(cls, #funcname); \ + orig_func = cls->Virtuals.Size() > VIndex? cls->Virtuals[VIndex] : nullptr; \ + assert(VIndex != ~0u); \ + } \ + auto *clss = self->GetClass(); \ + VMFunction *func = clss->Virtuals.Size() > VIndex? clss->Virtuals[VIndex] : nullptr; \ + if (func && func != orig_func ) #endif diff --git a/src/playsim/p_pspr.cpp b/src/playsim/p_pspr.cpp index d65b4d00379..91e9dafc8e2 100644 --- a/src/playsim/p_pspr.cpp +++ b/src/playsim/p_pspr.cpp @@ -641,6 +641,19 @@ void P_BobWeapon (player_t *player, float *x, float *y, double ticfrac) DVector2 result; VMReturn ret(&result); VMCall(func, param, 2, &ret, 1); + + auto inv = player->mo->Inventory; + while(inv != nullptr && !(inv->ObjectFlags & OF_EuthanizeMe)) // same loop as ModifyDamage, except it actually checks if it's overriden before calling + { + auto nextinv = inv->Inventory; + IFOVERRIDENVIRTUALPTRNAME(inv, NAME_Inventory, ModifyBob) + { + VMValue param[] = { (DObject*)inv, result.X, result.Y, ticfrac }; + VMCall(func, param, 4, &ret, 1); + } + inv = nextinv; + } + *x = (float)result.X; *y = (float)result.Y; return; @@ -658,6 +671,19 @@ void P_BobWeapon3D (player_t *player, FVector3 *translation, FVector3 *rotation, returns[0].Vec3At(&t); returns[1].Vec3At(&r); VMCall(func, param, 2, returns, 2); + + auto inv = player->mo->Inventory; + while(inv != nullptr && !(inv->ObjectFlags & OF_EuthanizeMe)) + { + auto nextinv = inv->Inventory; + IFOVERRIDENVIRTUALPTRNAME(inv, NAME_Inventory, ModifyBob3D) + { + VMValue param[] = { (DObject*)inv, t.X, t.Y, t.Z, r.X, r.Y, r.Z, ticfrac }; + VMCall(func, param, 8, returns, 2); + } + inv = nextinv; + } + translation->X = (float)t.X; translation->Y = (float)t.Y; translation->Z = (float)t.Z; diff --git a/wadsrc/static/zscript/actors/inventory/inventory.zs b/wadsrc/static/zscript/actors/inventory/inventory.zs index 88bbd2f103c..35c1a3d9583 100644 --- a/wadsrc/static/zscript/actors/inventory/inventory.zs +++ b/wadsrc/static/zscript/actors/inventory/inventory.zs @@ -1021,6 +1021,10 @@ class Inventory : Actor //=========================================================================== virtual void ModifyDamage(int damage, Name damageType, out int newdamage, bool passive, Actor inflictor = null, Actor source = null, int flags = 0) {} + + virtual Vector2 ModifyBob(Vector2 Bob, double ticfrac) {return Bob;} + + virtual Vector3, Vector3 ModifyBob3D(Vector3 Translation, Vector3 Rotation, double ticfrac) {return Translation, Rotation;} virtual bool Use (bool pickup) { return false; }