TF2-DMB / CBaseNPC

Provides a friendly interface for plugins to use in order to create custom NPCs on the game Team Fortress 2
36 stars 5 forks source link

Add Locomotion callbacks + Fix memory corruption on unload #8

Closed KitRifty closed 2 years ago

KitRifty commented 2 years ago

This allows developers to customize CBaseNPC_Locomotion's behavior by giving the option to override its default functions through callbacks, in similar fashion to NextBotActionFactory. Developers can now customize how the bot climbs, jumps, collides with other entities, and even determine if an entity is traversable or not. Developers can also still call the default behavior if desired in their callbacks using the CallBaseFunction native. The default behavior will be called if no callback is set.

CallBaseFunction is restricted to be used within a callback function only as it's supposed to be a private thing. Outside of callbacks, developers should be calling the functions virtually anyway using the ILocomotion/NextBotGroundLocomotion natives.

Moreover this changes the locomotion to utilize its own vtable rather than hooks to call its functions. A lot of NextBotGroundLocomotion's functions were being overridden so it seemed more fitting to give the locomotion its own vtable, just like how CBaseNPC_Entity does it. Thanks to being able to get the vtable offsets dynamically via SourceHook, the locomotion code is now separated into its own header and source files. We did discuss about grabbing offsets dynamically in Discord, but I was surprised to see that VCall already has an Init function overload designed to do just that, so it wasn't a hard choice to make!

The CBaseNPC.GetLocomotion native has been changed to return a CBaseNPC_Locomotion object. Backwards compatibility with older plugins won't be an issue as CBaseNPC_Locomotion inherits from NextBotGroundLocomotion.

This also adds some helper macros in helpers.h in regards to using the VCall object. This was made to reduce needless repetition in the code.

This also removes the SetCollisionBounds native, as it's been deprecated and is no longer defined in the .inc files.

Let me know if you want any changes!

Kenzzer commented 2 years ago

Haven't forgotten about this, will review one final time, hopefully this month. I'm aiming at having the giant switch case eliminated before merging, will probably make commits changing that on this branch. I also need to soak deeper into this anyways, making sure I grasped everything considering this moved a lot of code.