From b54e03852d3afa75b50abbc5eb0569c89dfc2783 Mon Sep 17 00:00:00 2001 From: Dinorush <62536724+Dinorush@users.noreply.github.com> Date: Thu, 9 Mar 2023 16:57:13 -0500 Subject: Add support for entity final damage callbacks (#578) * Add support for entity final damage callbacks * Add final damage callback for non-Players/NPCs * Remove accidental duplicate code * Remove other unnecessary (duplicated) code * Implement uniboi suggestion --- .../scripts/vscripts/_codecallbacks_common.gnut | 29 ++++++++++++++++++++-- .../mod/scripts/vscripts/_entitystructs.gnut | 1 + .../mod/scripts/vscripts/mp/_codecallbacks.gnut | 14 +++++++++++ 3 files changed, 42 insertions(+), 2 deletions(-) diff --git a/Northstar.CustomServers/mod/scripts/vscripts/_codecallbacks_common.gnut b/Northstar.CustomServers/mod/scripts/vscripts/_codecallbacks_common.gnut index d2621db3..3704b5cc 100644 --- a/Northstar.CustomServers/mod/scripts/vscripts/_codecallbacks_common.gnut +++ b/Northstar.CustomServers/mod/scripts/vscripts/_codecallbacks_common.gnut @@ -46,6 +46,8 @@ global function CodeCallback_OnEntityChangedTeam global function AddEntityCallback_OnDamaged global function RemoveEntityCallback_OnDamaged +global function AddEntityCallback_OnFinalDamaged +global function RemoveEntityCallback_OnFinalDamaged global function AddEntityCallback_OnPostDamaged global function RemoveEntityCallback_OnPostDamaged global function AddEntityCallback_OnKilled @@ -121,6 +123,13 @@ void function CodeCallback_DamageEntity( entity ent, var damageInfo ) printt( " after class damage final callbacks:", DamageInfo_GetDamage( damageInfo ) ) #endif + foreach ( callbackFunc in ent.e.entFinalDamageCallbacks ) + callbackFunc( ent, damageInfo ) + + #if VERBOSE_DAMAGE_PRINTOUTS + printt( " after AddEntityCallback_OnFinalDamaged callbacks:", DamageInfo_GetDamage( damageInfo ) ) + #endif + // make destructible vehicles take more damage from DF_EXPLOSION damage type if ( "isDestructibleVehicle" in ent.s && DamageInfo_GetCustomDamageType( damageInfo ) & DF_EXPLOSION ) { @@ -566,7 +575,23 @@ void function RemoveEntityCallback_OnDamaged( entity ent, void functionref( enti Assert( index != -1, "Requested DamageCallback " + string( callbackFunc ) + " to be removed not found! " ) ent.e.entDamageCallbacks.fastremove( index ) - if ( ent.e.entDamageCallbacks.len() == 0 && ent.e.entPostDamageCallbacks.len() == 0 ) + if ( ent.e.entDamageCallbacks.len() == 0 && ent.e.entFinalDamageCallbacks.len() == 0 && ent.e.entPostDamageCallbacks.len() == 0 ) + ent.SetDamageNotifications( false ) +} + +void function AddEntityCallback_OnFinalDamaged( entity ent, void functionref( entity ent, var damageInfo ) callbackFunc ) +{ + Assert( !ent.e.entFinalDamageCallbacks.contains( callbackFunc ), "Already added " + string( callbackFunc ) + " to entity" ) + + ent.SetDamageNotifications( true ) + ent.e.entFinalDamageCallbacks.append( callbackFunc ) +} + +void function RemoveEntityCallback_OnFinalDamaged( entity ent, void functionref( entity ent, var damageInfo ) callbackFunc ) +{ + ent.e.entFinalDamageCallbacks.fastremovebyvalue( callbackFunc ) + + if ( ent.e.entFinalDamageCallbacks.len() == 0 && ent.e.entPostDamageCallbacks.len() == 0 && ent.e.entDamageCallbacks.len() == 0 ) ent.SetDamageNotifications( false ) } @@ -585,7 +610,7 @@ void function RemoveEntityCallback_OnPostDamaged( entity ent, void functionref( Assert( index != -1, "Requested PostDamageCallback " + string( callbackFunc ) + " to be removed not found! " ) ent.e.entPostDamageCallbacks.fastremove( index ) - if ( ent.e.entPostDamageCallbacks.len() == 0 && ent.e.entDamageCallbacks.len() == 0 ) + if ( ent.e.entPostDamageCallbacks.len() == 0 && ent.e.entDamageCallbacks.len() == 0 && ent.e.entFinalDamageCallbacks.len() == 0 ) ent.SetDamageNotifications( false ) } diff --git a/Northstar.CustomServers/mod/scripts/vscripts/_entitystructs.gnut b/Northstar.CustomServers/mod/scripts/vscripts/_entitystructs.gnut index 378ceae3..9dadea15 100644 --- a/Northstar.CustomServers/mod/scripts/vscripts/_entitystructs.gnut +++ b/Northstar.CustomServers/mod/scripts/vscripts/_entitystructs.gnut @@ -232,6 +232,7 @@ global struct ServerEntityStruct SpawnPointData spawnPointData array entDamageCallbacks + array entFinalDamageCallbacks array entPostDamageCallbacks array entSegmentLostCallbacks array entPostShieldDamageCallbacks diff --git a/Northstar.CustomServers/mod/scripts/vscripts/mp/_codecallbacks.gnut b/Northstar.CustomServers/mod/scripts/vscripts/mp/_codecallbacks.gnut index 425a8b8b..ff281d6e 100644 --- a/Northstar.CustomServers/mod/scripts/vscripts/mp/_codecallbacks.gnut +++ b/Northstar.CustomServers/mod/scripts/vscripts/mp/_codecallbacks.gnut @@ -283,9 +283,23 @@ void function CodeCallback_DamagePlayerOrNPC( entity ent, var damageInfo ) return RunClassDamageFinalCallbacks( ent, damageInfo ) + #if VERBOSE_DAMAGE_PRINTOUTS printt( " after class damage final callbacks:", DamageInfo_GetDamage( damageInfo ) ) #endif + if ( DamageInfo_GetDamage( damageInfo ) == 0 ) + return + + // Added via AddEntityCallback_OnFinalDamaged + foreach ( callbackFunc in ent.e.entFinalDamageCallbacks ) + { + callbackFunc( ent, damageInfo ) + } + + #if VERBOSE_DAMAGE_PRINTOUTS + printt( " afterAddEntityCallback_OnFinalDamaged callbacks:", DamageInfo_GetDamage( damageInfo ) ) + #endif + if ( DamageInfo_GetDamage( damageInfo ) == 0 ) return -- cgit v1.2.3