Skip to content

Commit

Permalink
physenv: implement simulate properly to also update the entities afte…
Browse files Browse the repository at this point in the history
…rwards
  • Loading branch information
RaphaelIT7 committed Nov 18, 2024
1 parent bece8d3 commit 578a819
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 2 deletions.
42 changes: 41 additions & 1 deletion source/modules/physenv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <vphysics/performance.h>
#include "sourcesdk/cphysicsobject.h"
#include "sourcesdk/cphysicsenvironment.h"
#include "player.h"

class CPhysEnvModule : public IModule
{
Expand Down Expand Up @@ -161,7 +162,7 @@ LUA_FUNCTION_STATIC(physenv_SetPhysSkipType)
return 0;
}

static IPhysics* physics = NULL;
IPhysics* physics = NULL;
static IPhysicsCollision* physcollide = NULL;
void CPhysEnvModule::Init(CreateInterfaceFn* appfn, CreateInterfaceFn* gamefn)
{
Expand Down Expand Up @@ -726,6 +727,45 @@ LUA_FUNCTION_STATIC(IPhysicsEnvironment_Simulate)
ILuaPhysicsEnvironment* pOldEnvironment = g_pCurrentEnvironment; // Simulating a environment in a already simulating environment? sounds fun :^
g_pCurrentEnvironment = pLuaEnv;
pEnvironment->Simulate(deltaTime);

int activeCount = pEnvironment->GetActiveObjectCount();
IPhysicsObject **pActiveList = NULL;
if ( activeCount )
{
pActiveList = (IPhysicsObject **)stackalloc( sizeof(IPhysicsObject *)*activeCount );
pEnvironment->GetActiveObjects( pActiveList );

for ( int i = 0; i < activeCount; i++ )
{
CBaseEntity *pEntity = reinterpret_cast<CBaseEntity *>(pActiveList[i]->GetGameData());
if ( pEntity )
{
if ( pEntity->CollisionProp()->DoesVPhysicsInvalidateSurroundingBox() )
{
pEntity->CollisionProp()->MarkSurroundingBoundsDirty();
}
pEntity->VPhysicsUpdate( pActiveList[i] );
}
}
stackfree( pActiveList );
}

/*for ( entitem_t* pItem = g_pShadowEntities->m_pItemList; pItem; pItem = pItem->pNext )
{
CBaseEntity *pEntity = pItem->hEnt.Get();
if ( !pEntity )
{
Msg( "Dangling pointer to physics entity!!!\n" );
continue;
}
IPhysicsObject *pPhysics = pEntity->VPhysicsGetObject();
if ( pPhysics && !pPhysics->IsAsleep() )
{
pEntity->VPhysicsShadowUpdate( pPhysics );
}
}*/

g_pCurrentEnvironment = pOldEnvironment;
return 0;
}
Expand Down
28 changes: 27 additions & 1 deletion source/sourcesdk/baseentity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,30 @@ CBaseEntity* CGlobalEntityList::NextEnt(CBaseEntity* pCurrentEnt)

return NULL;

}
}

bool CCollisionProperty::DoesVPhysicsInvalidateSurroundingBox( ) const
{
switch ( m_nSurroundType )
{
case USE_BEST_COLLISION_BOUNDS:
return true;

case USE_OBB_COLLISION_BOUNDS:
return (GetSolid() == SOLID_VPHYSICS) && (GetOuter()->GetMoveType() == MOVETYPE_VPHYSICS) && GetOuter()->VPhysicsGetObject();

// In the case of game code, we don't really know, so we have to assume it does
case USE_GAME_CODE:
return true;

case USE_COLLISION_BOUNDS_NEVER_VPHYSICS:
case USE_HITBOXES:
case USE_ROTATION_EXPANDED_BOUNDS:
case USE_SPECIFIED_BOUNDS:
return false;

default:
Assert(0);
return true;
}
}
4 changes: 4 additions & 0 deletions source/symbols.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ namespace Symbols
Symbol::FromName("_ZN11CBaseEntity15PostConstructorEPKc"),
};

const std::vector<Symbol> CCollisionProperty_MarkSurroundingBoundsDirtySym = {
Symbol::FromName("_ZN18CCollisionProperty24MarkPartitionHandleDirtyEv"),
};

const std::vector<Symbol> CGetSym = { // 64x ToDo
Symbol::FromName("get"),
};
Expand Down
3 changes: 3 additions & 0 deletions source/symbols.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ namespace Symbols
typedef void (*CBaseEntity_PostConstructor)(void* ent, const char* szClassname);
extern const std::vector<Symbol> CBaseEntity_PostConstructorSym;

typedef void (*CCollisionProperty_MarkSurroundingBoundsDirty)(void* fancy_class);
extern const std::vector<Symbol> CCollisionProperty_MarkSurroundingBoundsDirtySym;

extern const std::vector<Symbol> CGetSym;

//---------------------------------------------------------------------------------
Expand Down
9 changes: 9 additions & 0 deletions source/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,12 @@ void CBaseEntity::CalcAbsolutePosition(void)
func_CBaseEntity_CalcAbsolutePosition(this);
}

static Symbols::CCollisionProperty_MarkSurroundingBoundsDirty func_CCollisionProperty_MarkSurroundingBoundsDirty;
void CCollisionProperty::MarkSurroundingBoundsDirty()
{
func_CCollisionProperty_MarkSurroundingBoundsDirty(this);
}

CBaseEntity* Util::GetCBaseEntityFromEdict(edict_t* edict)
{
return Util::servergameents->EdictToBaseEntity(edict);
Expand Down Expand Up @@ -216,6 +222,9 @@ void Util::AddDetour()
Detour::CheckFunction((void*)func_GetPlayer, "Get_Player");
Detour::CheckFunction((void*)func_PushEntity, "Push_Entity");
Detour::CheckFunction((void*)func_GetEntity, "Get_Entity");

func_CCollisionProperty_MarkSurroundingBoundsDirty = (Symbols::CCollisionProperty_MarkSurroundingBoundsDirty)Detour::GetFunction(server_loader.GetModule(), Symbols::CCollisionProperty_MarkSurroundingBoundsDirtySym);
Detour::CheckFunction((void*)func_CCollisionProperty_MarkSurroundingBoundsDirty, "CCollisionProperty::MarkSurroundingBoundsDirty");
#endif
}

Expand Down

0 comments on commit 578a819

Please sign in to comment.