diff --git a/cl_dll/GameStudioModelRenderer.cpp b/cl_dll/GameStudioModelRenderer.cpp index bbc7d7f..7ae42fa 100644 --- a/cl_dll/GameStudioModelRenderer.cpp +++ b/cl_dll/GameStudioModelRenderer.cpp @@ -54,7 +54,7 @@ void CGameStudioModelRenderer::Init(void) { CStudioModelRenderer::Init(); InitPhysicsInterface(NULL); - gPhysics.InitSystem(&m_clTime, m_plighttransform, &IEngineStudio); + gPhysics.InitSystem("valve", &IEngineStudio); } int CGameStudioModelRenderer::StudioDrawRagdoll(int flags) { @@ -92,7 +92,6 @@ int CGameStudioModelRenderer::StudioDrawRagdoll(int flags) } else { - StudioSetupBones(); gPhysics.SetupBonesPhysically(m_pCurrentEntity->index); m_pCurrentEntity->origin.x = (*m_pbonetransform)[1][0][3]; m_pCurrentEntity->origin.y = (*m_pbonetransform)[1][1][3]; @@ -136,6 +135,7 @@ int CGameStudioModelRenderer::StudioDrawRagdoll(int flags) return 1; } cvar_t* _drawOriginalDead = NULL; +bool gIsShoot = false; int CGameStudioModelRenderer::StudioDrawModel(int flags) { // we need a better place to call the physics update function @@ -197,7 +197,9 @@ int CGameStudioModelRenderer::StudioDrawModel(int flags) // init pose CStudioModelRenderer::StudioDrawModel(0); - pgCorpseMgr->CreateRagdollCorpse(m_pCurrentEntity); + TEMPENTITY* tempent = pgCorpseMgr->CreateRagdollCorpse(m_pCurrentEntity); + // Set init ragdoll pose + gPhysics.SetPose(tempent->entity.index, (float*)m_pbonetransform); pgCorpseMgr->EntityDie(m_pCurrentEntity); } else @@ -368,9 +370,14 @@ TEMPENTITY* CorpseManager::CreateRagdollCorpse(cl_entity_t* ent) tempent->die = 3000; tempent->fadeSpeed = 1; tempent->entity.index = _corpseIndex++; - gPhysics.CreateRagdollControllerHeader(tempent->entity.index, IEngineStudio.Mod_Extradata(ent->model)); + gPhysics.CreateRagdollControllerModel(tempent->entity.index, (ent->model)); gPhysics.StartRagdoll(tempent->entity.index); gPhysics.SetVelocity(tempent->entity.index, (Vector3*)&ent->curstate.velocity); + + cl_entity_t* local = gEngfuncs.GetLocalPlayer(); + Vector v = (ent->origin - local->origin).Normalize(); + v = v * 5; + gPhysics.SetVelocity(tempent->entity.index, (Vector3*)&v); gEngfuncs.Con_DPrintf("corpse [%d]'s velocity is %f\n", tempent->entity.index, ent->curstate.velocity.Length()); gEngfuncs.Con_DPrintf("create corpse [%d] for entity [%d]\n", tempent->entity.index, ent->index); diff --git a/cl_dll/physics.cpp b/cl_dll/physics.cpp index 1996d4e..31d982c 100644 --- a/cl_dll/physics.cpp +++ b/cl_dll/physics.cpp @@ -1,5 +1,5 @@ -#include"physics.h" +#include #include #include #include @@ -9,11 +9,10 @@ PhsicsAPI gPhysics; - #ifdef _DEBUG const wchar_t PhyDllPath[] = L".\\gsphysics\\bin\\GoldsrcPhysics.dll"; #else// _DEBUG -const wchar_t PhyDllPath[] = L"\\gsphysics\\GoldsrcPhysics.dll"; +const wchar_t PhyDllPath[] = L".\\gsphysics\\bin\\GoldsrcPhysics.dll"; #endif //globle CLR handle @@ -66,16 +65,17 @@ int ExitCLR() return 0; } -void* GetFunctionPointer(const LPCWSTR name) +void* GetFunctionPointer(LPWSTR name) { void* pfn = NULL; - wchar_t buffer[64];//marshal args to [0xXXXX|MethodName] format - swprintf(buffer, 64, L"%p|%s", &pfn, name); + const int bufsize = 128; + wchar_t buffer[bufsize];//marshal args to [0xXXXX|MethodName] format + swprintf(buffer, bufsize, L"%p|%s", &pfn, name); DWORD dwRet = 0; HRESULT hr = pRuntimeHost->ExecuteInDefaultAppDomain(PhyDllPath, - L"GoldsrcPhysics.ExportAPIs.PhysicsMain", + L"UsrSoft.ManagedExport.ManagedExporter", L"GetFunctionPointer", buffer, &dwRet); @@ -92,26 +92,32 @@ void* GetFunctionPointer(const LPCWSTR name) extern "C" void InitPhysicsInterface(char* msg) { InitCLR(); - gPhysics.Test = (void(_stdcall*)())GetFunctionPointer(L"Test"); - gPhysics.InitSystem = (void(_stdcall*)(void* pStudioRenderer, void* lastFieldAddress, void* engineStudioAPI))GetFunctionPointer(L"InitSystem"); - gPhysics.ChangeLevel = (void(_stdcall*)(const char* mapName))GetFunctionPointer(L"ChangeLevel"); - gPhysics.LevelReset = (void(_stdcall*)())GetFunctionPointer(L"LevelReset"); - gPhysics.Update = (void(_stdcall*)(float delta))GetFunctionPointer(L"Update"); - gPhysics.Pause = (void(_stdcall*)())GetFunctionPointer(L"Pause"); - gPhysics.Resume = (void(_stdcall*)())GetFunctionPointer(L"Resume"); - gPhysics.ShotDown = (void(_stdcall*)())GetFunctionPointer(L"ShotDown"); - gPhysics.ShowConfigForm = (void(_stdcall*)())GetFunctionPointer(L"ShowConfigForm"); - gPhysics.CreateRagdollController = (void(_stdcall*)(int entityId, const char* modelName))GetFunctionPointer(L"CreateRagdollController"); - gPhysics.CreateRagdollControllerIndex = (void(_stdcall*)(int entityId, int index))GetFunctionPointer(L"CreateRagdollControllerIndex"); - gPhysics.CreateRagdollControllerHeader = (void(_stdcall*)(int entityId, void * hdr))GetFunctionPointer(L"CreateRagdollControllerHeader"); - gPhysics.StartRagdoll = (void(_stdcall*)(int entityId))GetFunctionPointer(L"StartRagdoll"); - gPhysics.StopRagdoll = (void(_stdcall*)(int entityId))GetFunctionPointer(L"StopRagdoll"); - gPhysics.SetupBonesPhysically = (void(_stdcall*)(int entityId))GetFunctionPointer(L"SetupBonesPhysically"); - gPhysics.ChangeOwner = (void(_stdcall*)(int oldEntity, int newEntity))GetFunctionPointer(L"ChangeOwner"); - gPhysics.SetVelocity = (void(_stdcall*)(int entityId, Vector3 * v))GetFunctionPointer(L"SetVelocity"); - gPhysics.DisposeRagdollController = (void(_stdcall*)(int entityId))GetFunctionPointer(L"DisposeRagdollController"); - gPhysics.Explosion = (void(_stdcall*)(Vector3 * pos, float intensity))GetFunctionPointer(L"Explosion"); - gPhysics.Shoot = (void(_stdcall*)(Vector3 * from, Vector3 * force))GetFunctionPointer(L"Shoot"); - gPhysics.PickBody = (void(_stdcall*)())GetFunctionPointer(L"PickBody"); - gPhysics.ReleaseBody = (void(_stdcall*)())GetFunctionPointer(L"ReleaseBody"); + gPhysics.Set = (void(_stdcall*)(const char* key, const char* value))GetFunctionPointer(L"GoldsrcPhysics.ExportAPIs.PhysicsMain.Set"); + gPhysics.Test = (void(_stdcall*)())GetFunctionPointer(L"GoldsrcPhysics.ExportAPIs.PhysicsMain.Test"); + gPhysics.InitSystem = (void(_stdcall*)(const char* modFolder, void* pEngineStudioAPI))GetFunctionPointer(L"GoldsrcPhysics.ExportAPIs.PhysicsMain.InitSystem"); + gPhysics.ChangeLevel = (void(_stdcall*)(const char* mapName))GetFunctionPointer(L"GoldsrcPhysics.ExportAPIs.PhysicsMain.ChangeLevel"); + gPhysics.LevelReset = (void(_stdcall*)())GetFunctionPointer(L"GoldsrcPhysics.ExportAPIs.PhysicsMain.LevelReset"); + gPhysics.Update = (void(_stdcall*)(float delta))GetFunctionPointer(L"GoldsrcPhysics.ExportAPIs.PhysicsMain.Update"); + gPhysics.Pause = (void(_stdcall*)())GetFunctionPointer(L"GoldsrcPhysics.ExportAPIs.PhysicsMain.Pause"); + gPhysics.Resume = (void(_stdcall*)())GetFunctionPointer(L"GoldsrcPhysics.ExportAPIs.PhysicsMain.Resume"); + gPhysics.ShotDown = (void(_stdcall*)())GetFunctionPointer(L"GoldsrcPhysics.ExportAPIs.PhysicsMain.ShotDown"); + gPhysics.ShowConfigForm = (void(_stdcall*)())GetFunctionPointer(L"GoldsrcPhysics.ExportAPIs.PhysicsMain.ShowConfigForm"); + gPhysics.CreateRagdollController = (void(_stdcall*)(int entityId, char* modelName))GetFunctionPointer(L"GoldsrcPhysics.ExportAPIs.PhysicsMain.CreateRagdollController"); + gPhysics.CreateRagdollControllerIndex = (void(_stdcall*)(int entityId, int index))GetFunctionPointer(L"GoldsrcPhysics.ExportAPIs.PhysicsMain.CreateRagdollControllerIndex"); + gPhysics.CreateRagdollControllerModel = (void(_stdcall*)(int entityId, void * model))GetFunctionPointer(L"GoldsrcPhysics.ExportAPIs.PhysicsMain.CreateRagdollControllerModel"); + gPhysics.StartRagdoll = (void(_stdcall*)(int entityId))GetFunctionPointer(L"GoldsrcPhysics.ExportAPIs.PhysicsMain.StartRagdoll"); + gPhysics.StopRagdoll = (void(_stdcall*)(int entityId))GetFunctionPointer(L"GoldsrcPhysics.ExportAPIs.PhysicsMain.StopRagdoll"); + gPhysics.SetupBonesPhysically = (void(_stdcall*)(int entityId))GetFunctionPointer(L"GoldsrcPhysics.ExportAPIs.PhysicsMain.SetupBonesPhysically"); + gPhysics.ChangeOwner = (void(_stdcall*)(int oldEntity, int newEntity))GetFunctionPointer(L"GoldsrcPhysics.ExportAPIs.PhysicsMain.ChangeOwner"); + gPhysics.SetVelocity = (void(_stdcall*)(int entityId, Vector3 * v))GetFunctionPointer(L"GoldsrcPhysics.ExportAPIs.PhysicsMain.SetVelocity"); + gPhysics.DisposeRagdollController = (void(_stdcall*)(int entityId))GetFunctionPointer(L"GoldsrcPhysics.ExportAPIs.PhysicsMain.DisposeRagdollController"); + gPhysics.ImpulseBone = (void(_stdcall*)(int entityId, int boneId, Vector3 * force))GetFunctionPointer(L"GoldsrcPhysics.ExportAPIs.PhysicsMain.ImpulseBone"); + gPhysics.ClearRagdoll = (void(_stdcall*)())GetFunctionPointer(L"GoldsrcPhysics.ExportAPIs.PhysicsMain.ClearRagdoll"); + gPhysics.HeadShootRagdoll = (void(_stdcall*)(int entityId, Vector3 * force))GetFunctionPointer(L"GoldsrcPhysics.ExportAPIs.PhysicsMain.HeadShootRagdoll"); + gPhysics.Explosion = (void(_stdcall*)(Vector3 * pos, float intensity))GetFunctionPointer(L"GoldsrcPhysics.ExportAPIs.PhysicsMain.Explosion"); + gPhysics.Shoot = (void(_stdcall*)(Vector3 * from, Vector3 * force))GetFunctionPointer(L"GoldsrcPhysics.ExportAPIs.PhysicsMain.Shoot"); + gPhysics.PickBodyLocal = (void(_stdcall*)(Vector3 from, Vector3 to))GetFunctionPointer(L"GoldsrcPhysics.ExportAPIs.PhysicsMain.PickBodyLocal"); + gPhysics.ReleaseBodyLocal = (void(_stdcall*)())GetFunctionPointer(L"GoldsrcPhysics.ExportAPIs.PhysicsMain.ReleaseBodyLocal"); + gPhysics.MoveBodyLocal = (void(_stdcall*)(Vector3 from, Vector3 to))GetFunctionPointer(L"GoldsrcPhysics.ExportAPIs.PhysicsMain.MoveBodyLocal"); + gPhysics.SetPose = (void(_stdcall*)(int entityId, float* pBoneWorldTransform))GetFunctionPointer(L"GoldsrcPhysics.ExportAPIs.PhysicsMain.SetPose"); } \ No newline at end of file diff --git a/cl_dll/physics.h b/cl_dll/physics.h index 47b13d6..79a2e64 100644 --- a/cl_dll/physics.h +++ b/cl_dll/physics.h @@ -1,142 +1,171 @@ + + #pragma once typedef struct { - float x, y, z; + float x, y, z; }Vector3; + typedef struct PhsicsAPI_s { - /* + /* + + */ + void(_stdcall* Set)(const char* key, const char* value); + + /* + + */ + void(_stdcall* Test)(); + + /* + + */ + void(_stdcall* InitSystem)(const char* modFolder, void* pEngineStudioAPI); + + /* + + Load map geomitry collider. + + */ + void(_stdcall* ChangeLevel)(const char* mapName); + + /* + + 地图不变,内容重置,清理在游戏中动态创建的各种CollisionObjects + cs的每一局结束可以调用 + + */ + void(_stdcall* LevelReset)(); + + /* + + Physics world update + + */ + void(_stdcall* Update)(float delta); - */ - void(_stdcall* Test)(); + /* - /* - - Init physics system - if the struct layout is different from default layout, that will throw a fatal error. - the address of StudioModelRenderer's first field. (m_clTime)>the address of StudioModelRenderer's last field. (m_plighttransform)pIEngineStudio - */ - void(_stdcall* InitSystem)(void* pStudioRenderer, void* lastFieldAddress, void* engineStudioAPI); + */ + void(_stdcall* Pause)(); - /* - - Load map geomitry collider. - - */ - void(_stdcall* ChangeLevel)(const char* mapName); + /* - /* - - ͼ䣬ãϷж̬ĸCollisionObjects - csÿһֽԵ - - */ - void(_stdcall* LevelReset)(); + */ + void(_stdcall* Resume)(); - /* - - Physics world update - - */ - void(_stdcall* Update)(float delta); + /* + + Close physics system and release physics resources. + + */ + void(_stdcall* ShotDown)(); - /* + /* + + Show configration form. + Using cvar to call this is recommended. + + */ + void(_stdcall* ShowConfigForm)(); - */ - void(_stdcall* Pause)(); + /* - /* + */ + void(_stdcall* CreateRagdollController)(int entityId, char* modelName); - */ - void(_stdcall* Resume)(); + /* - /* - - Close physics system and release physics resources. - - */ - void(_stdcall* ShotDown)(); + */ + void(_stdcall* CreateRagdollControllerIndex)(int entityId, int index); - /* - - Show configration form. - Using cvar to call this is recommended. - - */ - void(_stdcall* ShowConfigForm)(); + /* + model_t* model + */ + void(_stdcall* CreateRagdollControllerModel)(int entityId, void* model); - /* + /* - */ - void(_stdcall* CreateRagdollController)(int entityId, const char* modelName); + */ + void(_stdcall* StartRagdoll)(int entityId); - /* + /* - */ - void(_stdcall* CreateRagdollControllerIndex)(int entityId, int index); + */ + void(_stdcall* StopRagdoll)(int entityId); - /* + /* - */ - void(_stdcall* CreateRagdollControllerHeader)(int entityId, void* hdr); + */ + void(_stdcall* SetupBonesPhysically)(int entityId); - /* + /* - */ - void(_stdcall* StartRagdoll)(int entityId); + */ + void(_stdcall* ChangeOwner)(int oldEntity, int newEntity); - /* + /* - */ - void(_stdcall* StopRagdoll)(int entityId); + */ + void(_stdcall* SetVelocity)(int entityId, Vector3* v); - /* + /* - */ - void(_stdcall* SetupBonesPhysically)(int entityId); + */ + void(_stdcall* DisposeRagdollController)(int entityId); - /* + /* - */ - void(_stdcall* ChangeOwner)(int oldEntity, int newEntity); + */ + void(_stdcall* ImpulseBone)(int entityId, int boneId, Vector3* force); - /* + /* - */ - void(_stdcall* SetVelocity)(int entityId, Vector3* v); + */ + void(_stdcall* ClearRagdoll)(); - /* + /* - */ - void(_stdcall* DisposeRagdollController)(int entityId); + */ + void(_stdcall* HeadShootRagdoll)(int entityId, Vector3* force); - /* - - Set an explosion on the specified position. - The impact range is calculated automatically via intensity. - - */ - void(_stdcall* Explosion)(Vector3* pos, float intensity); + /* + + Set an explosion on the specified position. + The impact range is calculated automatically via intensity. + + */ + void(_stdcall* Explosion)(Vector3* pos, float intensity); - /* - - Shoot an invisable bullet to apply impulse to the rigidbody it hits. - eye pos.contains direction and intensity. - */ - void(_stdcall* Shoot)(Vector3* from, Vector3* force); + /* + + Shoot an invisable bullet to apply impulse to the rigidbody it hits. + eye pos.contains direction and intensity. + */ + void(_stdcall* Shoot)(Vector3* from, Vector3* force); - /* + /* + Eye pos or camera originCamera origin + direction + */ + void(_stdcall* PickBodyLocal)(Vector3 from, Vector3 to); - */ - void(_stdcall* PickBody)(); + /* + + Release picked body + + */ + void(_stdcall* ReleaseBodyLocal)(); - /* + /* + Eye pos or camera originCamera origin + direction + */ + void(_stdcall* MoveBodyLocal)(Vector3 from, Vector3 to); - */ - void(_stdcall* ReleaseBody)(); + void(_stdcall*SetPose)(int entityId, float* pBoneWorldTransform); }PhsicsAPI; diff --git a/projects/vs2019/hl_cdll.vcxproj b/projects/vs2019/hl_cdll.vcxproj index 03f5040..475dead 100644 --- a/projects/vs2019/hl_cdll.vcxproj +++ b/projects/vs2019/hl_cdll.vcxproj @@ -28,7 +28,7 @@ false true NotSet - v141_xp + v142 @@ -48,7 +48,7 @@ false - C:\Program Files (x86)\Steam\steamapps\common\Half-Life\updated\cl_dlls\ + $(Configuration)\$(ProjectName) client $(Configuration)\$(ProjectName)\int\