66using Source . Common . Physics ;
77using Source . Common . Server ;
88
9+ #if GMOD_DLL
10+ using Game . Server . GarrysMod ;
11+ #endif
12+
913using System . Numerics ;
1014
1115namespace Game . Server ;
@@ -1687,12 +1691,30 @@ public class LogicCollisionPair : LogicalEntity
16871691 string ? NameAttach2 ;
16881692 bool Disabled ;
16891693 bool Succeeded ;
1694+ #if GMOD_DLL
1695+ InlineArray2 < IPhysicsObject ? > PhysObjects ;
1696+ InlineArray2 < EHANDLE > Entities ;
1697+ InlineArray2 < int > PhysIndices ;
1698+ bool DisableOnRemove ;
1699+
1700+ public LogicCollisionPair ( ) {
1701+ Entities [ 0 ] = new ( ) ;
1702+ Entities [ 1 ] = new ( ) ;
1703+ }
1704+ #endif
16901705
16911706 public static readonly new DataMap DataDesc = new ( typeof ( LogicCollisionPair ) , LogicalEntity . DataDesc , [
16921707 DEFINE < LogicCollisionPair > . KEYFIELD ( nameof ( NameAttach1 ) , FieldType . String , "attach1" ) ,
16931708 DEFINE < LogicCollisionPair > . KEYFIELD ( nameof ( NameAttach2 ) , FieldType . String , "attach2" ) ,
16941709 DEFINE < LogicCollisionPair > . KEYFIELD ( nameof ( Disabled ) , FieldType . Boolean , "startdisabled" ) ,
16951710 DEFINE < LogicCollisionPair > . FIELD ( nameof ( Succeeded ) , FieldType . Boolean ) ,
1711+ #if GMOD_DLL
1712+ DEFINE < LogicCollisionPair > . KEYFIELD ( nameof ( DisableOnRemove ) , FieldType . Boolean , "disable_on_remove" ) ,
1713+ DEFINE < LogicCollisionPair > . FIELD ( "Entities[0]" , FieldType . EHandle ) ,
1714+ DEFINE < LogicCollisionPair > . FIELD ( "Entities[1]" , FieldType . EHandle ) ,
1715+ DEFINE < LogicCollisionPair > . FIELD ( "PhysIndices[0]" , FieldType . Integer ) ,
1716+ DEFINE < LogicCollisionPair > . FIELD ( "PhysIndices[1]" , FieldType . Integer ) ,
1717+ #endif
16961718
16971719 // Inputs
16981720 DEFINE < LogicCollisionPair > . INPUTFUNC ( FieldType . Void , "DisableCollisions" , nameof ( InputDisableCollisions ) , ( INPUTFUNCPTR ) ( ( self , data ) => ( ( LogicCollisionPair ) self ) . InputDisableCollisions ( data ) ) ) ,
@@ -1709,14 +1731,65 @@ public class LogicCollisionPair : LogicalEntity
17091731 return g_PhysWorldObject ;
17101732
17111733 IPhysicsObject ? physics = PhysicsHook . FindPhysicsObjectByName ( name , errorEntity ) ;
1712- if ( physics == null )
1734+ if ( physics == null ) {
17131735 DevWarning ( $ "{ errorEntity . GetClassname ( ) } : can't find { name } \n ") ;
1736+ #if GMOD_DLL
1737+ physics = g_PhysWorldObject ;
1738+ #endif
1739+ }
17141740 return physics ;
17151741 }
17161742
1743+ #if GMOD_DLL
1744+ public override void SetPhysObject ( int index , IPhysicsObject ? obj ) {
1745+ PhysObjects [ index ] = obj ;
1746+ BaseEntity ? entity = ( BaseEntity ? ) obj ! . GetGameData ( ) ;
1747+ Entities [ index ] . Set ( entity ) ;
1748+ PhysIndices [ index ] = 0 ;
1749+ entity = Entities [ index ] . Get ( ) ;
1750+ if ( entity != null ) {
1751+ IPhysicsObject ? [ ] list = new IPhysicsObject ? [ VPHYSICS_MAX_OBJECT_LIST_COUNT ] ;
1752+ int count = entity . VPhysicsGetObjectList ( list ) ;
1753+ if ( count > 1 ) {
1754+ for ( int i = 0 ; i < count ; i ++ ) {
1755+ if ( list [ i ] == obj )
1756+ PhysIndices [ index ] = i ;
1757+ }
1758+ }
1759+ }
1760+ }
1761+
1762+ public override void OnRestore ( ) {
1763+ BaseEntity ? entity0 = Entities [ 0 ] . Get ( ) ;
1764+ if ( entity0 != null )
1765+ PhysObjects [ 0 ] = entity0 . VPhysicsGetElement ( PhysIndices [ 0 ] ) ;
1766+ BaseEntity ? entity1 = Entities [ 1 ] . Get ( ) ;
1767+ if ( entity1 != null )
1768+ PhysObjects [ 1 ] = entity1 . VPhysicsGetElement ( PhysIndices [ 1 ] ) ;
1769+ Activate ( ) ;
1770+ }
1771+
1772+ public override void UpdateOnRemove ( ) {
1773+ if ( DisableOnRemove && ( ! Succeeded || Disabled ) )
1774+ EnableCollisions ( true ) ;
1775+ base . UpdateOnRemove ( ) ;
1776+ }
1777+ #endif
1778+
17171779 public void EnableCollisions ( bool enable ) {
1780+ #if GMOD_DLL
1781+ if ( PhysObjects [ 0 ] != null || PhysObjects [ 1 ] != null ) {
1782+ if ( ! LuaPhysObj . IsPhysicsObjectValid ( PhysObjects [ 0 ] ) )
1783+ PhysObjects [ 0 ] = null ;
1784+ if ( ! LuaPhysObj . IsPhysicsObjectValid ( PhysObjects [ 1 ] ) )
1785+ PhysObjects [ 1 ] = null ;
1786+ }
1787+ IPhysicsObject ? physics0 = PhysObjects [ 0 ] ?? FindPhysicsObjectByNameOrWorld ( NameAttach1 , this ) ;
1788+ IPhysicsObject ? physics1 = PhysObjects [ 1 ] ?? FindPhysicsObjectByNameOrWorld ( NameAttach2 , this ) ;
1789+ #else
17181790 IPhysicsObject ? physics0 = FindPhysicsObjectByNameOrWorld ( NameAttach1 , this ) ;
17191791 IPhysicsObject ? physics1 = FindPhysicsObjectByNameOrWorld ( NameAttach2 , this ) ;
1792+ #endif
17201793
17211794 // need two different objects to do anything
17221795 if ( physics0 != null && physics1 != null && physics0 != physics1 ) {
0 commit comments