better physics

This commit is contained in:
2026-03-05 00:30:27 +02:00
parent ddbdef713b
commit 2da75ebdd8
68 changed files with 743 additions and 262450 deletions

View File

@@ -0,0 +1,257 @@
#include "assetmgr.h"
#include "tier2/ifilesystem.h"
#include "tier2/fileformats/json.h"
#include "game.h"
#define MAX_MODEL_COUNT 2048
#define MAX_MESH_COUNT 2048
#define MAX_MATERIAL_COUNT 2048
#define MAX_TEXTURE_COUNT 4096
#define MAX_SHADER_COUNT 1024
#define MAX_PHYSICS_COUNT 1024
template<typename T, uint32_t nCount>
class CAssetArc
{
struct ObjectHandle_t
{
CUtlString m_szName;
uint32_t m_uUsageCount;
T *m_pObject;
};
ObjectHandle_t m_objects[nCount] = {};
public:
uint32_t GetOrCreateObject( const char *szName, bool *pbHasBeenCreated )
{
uint32_t uFoundIndex = 1;
for ( uint32_t u = 1; u < nCount; u++ )
{
if (m_objects[u].m_pObject == NULL)
{
continue;
}
if (m_objects[u].m_szName == szName)
{
m_objects[u].m_uUsageCount++;
if (pbHasBeenCreated)
*pbHasBeenCreated = false;
return u;
}
}
for ( auto &m: m_objects)
{
if (m.m_pObject == NULL)
break;
uFoundIndex++;
}
m_objects[uFoundIndex].m_szName = szName;
m_objects[uFoundIndex].m_uUsageCount++;
m_objects[uFoundIndex].m_pObject = (T*)V_malloc(sizeof(T));
if (pbHasBeenCreated)
*pbHasBeenCreated = true;
return uFoundIndex;
}
T *GetObjectPtr( uint32_t uIndex )
{
if (uIndex >= nCount)
return 0;
return m_objects[uIndex].m_pObject;
}
void UnrefObject( uint32_t uIndex )
{
if (uIndex >= nCount)
return;
m_objects[uIndex].m_uUsageCount--;
if (m_objects[uIndex].m_uUsageCount == 0)
{
V_free(m_objects[uIndex].m_pObject);
m_objects[uIndex].m_pObject = NULL;
m_objects[uIndex].m_szName = NULL;
}
}
};
class CAssetManager: public IAssetManager
{
public:
virtual HFunnyModel LoadModel( const char *szName ) override;
virtual FunnyModel_t *GetModelByIndex( HFunnyModel hModel ) override;
virtual void UnrefModel( HFunnyModel uIndex ) override;
virtual HFunnyMesh LoadMesh( const char *szName ) override;
virtual FunnyMesh_t *GetMeshByIndex( HFunnyMesh hMesh ) override;
virtual void UnrefMesh( HFunnyMesh hMesh ) override;
virtual HFunnyMaterial LoadMaterial( const char *szName ) override;
virtual FunnyMaterial_t *GetMaterialByIndex( HFunnyMaterial hMat ) override;
virtual void UnrefMaterial( HFunnyMaterial hMat ) override;
virtual HFunnyPhysics LoadPhysics( const char *szName ) override;
virtual FunnyPhysics_t *GetPhysicsByIndex( HFunnyPhysics hPhysics ) override;
virtual void UnrefPhysics( HFunnyPhysics hPhysics ) override;
CAssetArc<FunnyModel_t, MAX_MODEL_COUNT> m_models = {};
CAssetArc<FunnyPhysics_t, MAX_PHYSICS_COUNT> m_physics = {};
};
FunnyModel_t *CAssetManager::GetModelByIndex( uint32_t uIndex )
{
return m_models.GetObjectPtr(uIndex);
}
FunnyMaterial_t *CAssetManager::GetMaterialByIndex( uint32_t uIndex )
{
}
FunnyMesh_t *CAssetManager::GetMeshByIndex( uint32_t uIndex )
{
}
uint32_t CAssetManager::LoadModel( const char *szName )
{
bool bHasBeenCreated = false;
HFunnyModel hModel = m_models.GetOrCreateObject(szName, &bHasBeenCreated);
if (!bHasBeenCreated)
return hModel;
FunnyModel_t *pModel = m_models.GetObjectPtr(hModel);
IFileHandle *pHandle = filesystem->Open(szName, FILEMODE_READ);
if (!pHandle)
return 0;
CUtlString szProperties = filesystem->ReadString(pHandle);
IJSONValue *pRoot = JSONManager()->ReadString(szProperties);
filesystem->Close(pHandle);
if (!pRoot)
return 0;
IJSONObject *pMainObject;
CUtlString szMeshData;
IVertexBuffer *pVertices;
switch (pRoot->GetType())
{
case JSON_PARAMETER_OBJECT:
{
pMainObject = pRoot->GetObject();
if (!pMainObject)
{
V_printf("Failed to load properties\n");
return 0;
}
IJSONValue *pPhysics = pMainObject->GetValue("Physics");
if (pPhysics)
pModel->m_hPhysics = LoadPhysics(pPhysics->GetStringValue());
return hModel;
}
break;
default:
return 0;
}
return 0;
}
void CAssetManager::UnrefModel( uint32_t uIndex )
{
m_models.UnrefObject(uIndex);
}
uint32_t CAssetManager::LoadMaterial( const char *szName )
{
return 0;
}
void CAssetManager::UnrefMaterial( uint32_t uIndex )
{
}
HFunnyMesh CAssetManager::LoadMesh( const char *szName )
{
return 0;
}
void CAssetManager::UnrefMesh( uint32_t uIndex )
{
}
HFunnyPhysics CAssetManager::LoadPhysics( const char *szName )
{
bool bHasBeenCreated = false;
HFunnyPhysics hPhysics = m_physics.GetOrCreateObject(szName, &bHasBeenCreated);
if (!bHasBeenCreated)
return hPhysics;
FunnyPhysics_t *pPhysics = m_physics.GetObjectPtr(hPhysics);
IFileHandle *pHandle = filesystem->Open(szName, FILEMODE_READ);
if (!pHandle)
return 0;
CUtlString szProperties = filesystem->ReadString(pHandle);
IJSONValue *pRoot = JSONManager()->ReadString(szProperties);
if (!pRoot)
return 0;
IJSONObject *pMainObject;
switch (pRoot->GetType())
{
case JSON_PARAMETER_OBJECT:
{
pMainObject = pRoot->GetObject();
if (!pMainObject)
{
V_printf("Failed to load properties\n");
m_physics.UnrefObject(hPhysics);
return 0;
}
IJSONValue *pTypeValue = pMainObject->GetValue("Type");
if (!pTypeValue)
{
V_printf("\"Type\" must be specified\n");
m_physics.UnrefObject(hPhysics);
return 0;
}
CUtlString szType = pTypeValue->GetStringValue();
if (szType == "Sphere")
{
pPhysics->m_hShape = g_pPhysics->CreateBall({1.0});
}
return hPhysics;
}
break;
default:
return 0;
}
return hPhysics;
}
FunnyPhysics_t *CAssetManager::GetPhysicsByIndex( HFunnyPhysics hPhysics )
{
return m_physics.GetObjectPtr(hPhysics);
}
void CAssetManager::UnrefPhysics( HFunnyPhysics hPhysics )
{
m_physics.UnrefObject(hPhysics);
}
static CAssetManager s_assetmgr;
IAssetManager *g_pAssetManager = &s_assetmgr;

View File

@@ -0,0 +1,54 @@
#ifndef ASSET_MANAGER_H
#define ASSET_MANAGER_H
#include "datamap.h"
#include "iphysics.h"
#include "tier1/utlstring.h"
typedef uint32_t HFunnyMaterial;
typedef uint32_t HFunnyMesh;
typedef uint32_t HFunnyPhysics;
typedef uint32_t HFunnyModel;
struct FunnyMaterial_t
{
};
struct FunnyMesh_t
{
};
struct FunnyPhysics_t
{
HShape m_hShape;
};
struct FunnyModel_t
{
HFunnyPhysics m_hPhysics;
};
class IAssetManager
{
public:
virtual HFunnyModel LoadModel( const char *szName ) = 0;
virtual FunnyModel_t *GetModelByIndex( HFunnyModel hModel ) = 0;
virtual void UnrefModel( HFunnyModel uIndex ) = 0;
virtual HFunnyMesh LoadMesh( const char *szName ) = 0;
virtual FunnyMesh_t *GetMeshByIndex( HFunnyMesh hMesh ) = 0;
virtual void UnrefMesh( HFunnyMesh hMesh ) = 0;
virtual HFunnyMaterial LoadMaterial( const char *szName ) = 0;
virtual FunnyMaterial_t *GetMaterialByIndex( HFunnyMaterial hMat ) = 0;
virtual void UnrefMaterial( HFunnyMaterial hMat ) = 0;
virtual HFunnyPhysics LoadPhysics( const char *szName ) = 0;
virtual FunnyPhysics_t *GetPhysicsByIndex( HFunnyPhysics hPhysics ) = 0;
virtual void UnrefPhysics( HFunnyPhysics hPhysics ) = 0;
};
extern IAssetManager *g_pAssetManager;
#endif

View File

@@ -20,10 +20,18 @@ void CBaseEntity::Precache()
void CBaseEntity::Spawn()
{
Precache();
Precache();
SetAbsOrigin({0, 0, 0});
SetAbsQAngles(0, 0, 0);
SetScale(1);
}
void CBaseEntity::SetAbsAngles( float fPitch, float fYaw, float fRoll )
void CBaseEntity::SetAbsAngles( Quat vQuat )
{
m_vRotation = vQuat;
}
void CBaseEntity::SetAbsQAngles( float fPitch, float fYaw, float fRoll )
{
versor q;
glm_euler_yzx_quat((vec3){fPitch, fYaw, fRoll}, q);

View File

@@ -61,7 +61,8 @@ public:
virtual void Precache();
virtual void Spawn();
virtual void SetAbsAngles( float fPitch, float fYaw, float fRoll );
virtual void SetAbsAngles( Quat vQuat );
virtual void SetAbsQAngles( float fPitch, float fYaw, float fRoll );
virtual void SetAbsOrigin( Vector origin );
virtual void SetScale( float fScale );

View File

@@ -1,11 +1,6 @@
#include "basemodelentity.h"
#include "game.h"
void CBaseModelEntity::Spawn()
void CBaseModelEntity::SetModel( const char *szName )
{
}
void CBaseModelEntity::Precache()
{
}

View File

@@ -9,18 +9,7 @@ class CBaseModelEntity: public CBaseEntity
{
public:
DECLARE_CLASS(CBaseModelEntity, CBaseEntity);
virtual void Spawn() override;
virtual void Precache() override;
void SetModel( const char *szName );
private:
HFunnyModel m_hModelHandle;
FunnyModel_t *m_pModel;
HShape m_hShape;
HCollider m_hCollider;
IPhysicsBody *m_pBody;
virtual void SetModel( const char *szName );
};
#endif

View File

@@ -17,9 +17,12 @@ DECLARE_BUILD_STAGE(Server)
compileProject.m_szName = "server";
compileProject.files = {
"game.cpp",
"assetmgr.cpp",
"entitysystem.cpp",
"baseentity.cpp",
"basemodelentity.cpp",
"physicsprop.cpp",
"milmoba/player.cpp",
};

View File

@@ -129,6 +129,11 @@ void CEntitySystem::Think()
}
}
//-------------------------------------------------------------------------------
// Purpose: Sends packets to clients.
// Since we are running this on server we can't really accept any packet.
// We only allow packets from the entities sent by a client
//-------------------------------------------------------------------------------
void CEntitySystem::NetThink( INetworkBase *pBase )
{

View File

@@ -14,11 +14,14 @@
IFileSystem *filesystem;
IRenderContext *g_pRenderContext;
IGameWindow *g_pMainWindow;
static CEngineVars s_vars;
CEngineVars *g_pEngineVars = &s_vars;
EngineConsts_t *g_pEngineConstants;
INetworkBase *g_pClientBridge;
INetworkBase *g_pPublicConnection;
INetworkBase *g_pCurrentConnection;
@@ -211,6 +214,7 @@ void CFunnyGameBridge::Shutdown()
#define CONNECT_INTERFACE(szName, pGlobal) if (!V_strcmp(psz, szName)) { pGlobal = (typeof(pGlobal))pInterface; return; }
void CFunnyGameBridge::ConnectInterface( const char *psz, void *pInterface )
{
CONNECT_INTERFACE(FILESYSTEM_INTERFACE_VERSION, filesystem);
CONNECT_INTERFACE("EngineConstants", g_pEngineConstants)
}

View File

@@ -5,15 +5,14 @@
void CMOBAPlayer::Spawn()
{
SetModel("game/core/models/cube.fmdl");
SetThink(Think);
m_fTimer = 0;
};
void CMOBAPlayer::Think( float fDelta )
{
m_fTimer += 1.0/60.0;
SetAbsOrigin({m_fTimer, 0, 0});
SetAbsAngles(glm_rad(90), m_fTimer, 0);
BaseClass::Think(fDelta);
SetScale(1);
};

View File

@@ -1,9 +1,9 @@
#ifndef MILMOBA_PLAYER_H
#define MILMOBA_PLAYER_H
#include "basemodelentity.h"
class CMOBAPlayer: public CBaseModelEntity
class CMOBAPlayer: public CBaseModelEntity
{
public:
DECLARE_CLASS(CMOBAPlayer, CBaseModelEntity);
@@ -12,8 +12,6 @@ public:
virtual void Spawn( void ) override;
void Think( float fDelta );
float m_fTimer;
};
#endif

View File

View File

View File

@@ -0,0 +1,43 @@
#include "physicsprop.h"
#include "game.h"
void CPhysicsProp::Precache()
{
}
void CPhysicsProp::Spawn()
{
SetThink(Think);
}
void CPhysicsProp::Think( float fDelta )
{
SetAbsOrigin(m_pBody->GetPosition());
SetAbsAngles(m_pBody->GetRotation());
}
void CPhysicsProp::SetModel( const char *szName )
{
if (m_hModel)
{
g_pAssetManager->UnrefModel(m_hModel);
}
m_hModel = g_pAssetManager->LoadModel(szName);
m_pModel = g_pAssetManager->GetModelByIndex(m_hModel);
m_pPhysics = g_pAssetManager->GetPhysicsByIndex(m_pModel->m_hPhysics);
m_hCollider = g_pPhysics->CreateCollider(m_pPhysics->m_hShape);
m_pBody = g_pPhysicsWorld->CreateRigidBody(m_hCollider, k_EPhysics_Dynamic);
}
void CPhysicsProp::EnableMovement()
{
}
void CPhysicsProp::DisableMovement()
{
}

View File

@@ -9,8 +9,19 @@ friend CBaseModelEntity;
public:
DECLARE_CLASS(CPhysicsProp, CBaseModelEntity);
virtual void Spawn() override;
virtual void Precache() override;
virtual void SetModel( const char *szName ) override;
virtual void EnableMovement();
virtual void DisableMovement();
void Think( float fDelta );
private:
HFunnyModel m_hModel;
FunnyModel_t *m_pModel;
FunnyPhysics_t *m_pPhysics;
HCollider m_hCollider;
IPhysicsBody *m_pBody;
};
#endif