Skip to content

Commit

Permalink
Add Inventory::ModifyBob and Inventory::ModifyBob3D
Browse files Browse the repository at this point in the history
  • Loading branch information
RicardoLuis0 authored and coelckers committed Sep 14, 2023
1 parent e61ab4c commit 0e70e95
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/common/scripting/vm/vm.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
26 changes: 26 additions & 0 deletions src/playsim/p_pspr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions wadsrc/static/zscript/actors/inventory/inventory.zs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down

0 comments on commit 0e70e95

Please sign in to comment.