Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial implementation of weapons predicting #217

Draft
wants to merge 12 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions client/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ list(APPEND CLDLL_SOURCES
"${CMAKE_SOURCE_DIR}/game_shared/stringlib.cpp"
"${CMAKE_SOURCE_DIR}/game_shared/virtualfs.cpp"
"${CMAKE_SOURCE_DIR}/game_shared/trace.cpp"
"${CMAKE_SOURCE_DIR}/game_shared/weapon_context.cpp"
"${CMAKE_SOURCE_DIR}/game_shared/seeded_random_generator.cpp"
"${CMAKE_SOURCE_DIR}/game_shared/filesystem_utils.cpp"
"${CMAKE_SOURCE_DIR}/game_shared/filesystem_manager.cpp"
"${CMAKE_SOURCE_DIR}/public/crclib.cpp"
Expand All @@ -68,6 +70,12 @@ file(GLOB RENDER_SOURCES "render/*.cpp")
# entity wrappers source files
file(GLOB ENTITIES_SOURCES "entities/*.cpp")

# game events source files
file(GLOB GAME_EVENTS_SOURCES "events/*.cpp")

# weapon shared code source files
file(GLOB WEAPONS_SHARED_SOURCES "${CMAKE_SOURCE_DIR}/game_shared/weapons/*.cpp")

# ImGui source files
if(NOT ENABLE_VGUI_COMPATIBILITY)
list(APPEND IMGUI_SOURCES
Expand All @@ -84,6 +92,8 @@ endif()

list(APPEND CLDLL_SOURCES ${RENDER_SOURCES})
list(APPEND CLDLL_SOURCES ${ENTITIES_SOURCES})
list(APPEND CLDLL_SOURCES ${GAME_EVENTS_SOURCES})
list(APPEND CLDLL_SOURCES ${WEAPONS_SHARED_SOURCES})
add_library(${PROJECT_NAME} SHARED ${CLDLL_SOURCES})

target_include_directories(${PROJECT_NAME} PRIVATE
Expand Down
8 changes: 8 additions & 0 deletions client/cdll_int.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "tri.h"
#include "mobility_int.h"
#include "imgui_manager.h"
#include "events/game_event_manager.h"
#include "utils.h"
#include "pm_shared.h"
#include "filesystem_utils.h"
Expand All @@ -38,6 +39,7 @@ int g_iXashEngineBuildNumber;
cl_enginefunc_t gEngfuncs;
mobile_engfuncs_t gMobileAPI;
render_api_t gRenderfuncs;
static CGameEventManager *g_pEventManager;
CHud gHUD;

/*
Expand Down Expand Up @@ -149,6 +151,8 @@ int Initialize( cl_enginefunc_t *pEnginefuncs, int iVersion )
BuildInfo::GetArchitecture(),
BuildInfo::GetPlatform()
);

g_pEventManager = new CGameEventManager();
return 1;
}

Expand Down Expand Up @@ -193,6 +197,10 @@ void DLLEXPORT HUD_Shutdown( void )

if (g_fRenderInitialized)
GL_Shutdown();

if (g_pEventManager) {
delete g_pEventManager;
}
}

/*
Expand Down
35 changes: 35 additions & 0 deletions client/events/base_game_event.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
base_game_event.cpp -
Copyright (C) 2024 SNMetamorph

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*/

#include "base_game_event.h"
#include "hud.h"
#include "utils.h"
#include "event_api.h"
#include "event_args.h"

CBaseGameEvent::CBaseGameEvent(event_args_s *args) :
m_arguments(args)
{
}

bool CBaseGameEvent::IsEventLocal() const
{
return gEngfuncs.pEventAPI->EV_IsLocal(m_arguments->entindex - 1) != 0;
}

bool CBaseGameEvent::IsEntityPlayer() const
{
return (m_arguments->entindex >= 1) && (m_arguments->entindex <= gEngfuncs.GetMaxClients());
}
36 changes: 36 additions & 0 deletions client/events/base_game_event.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
base_game_event.h -
Copyright (C) 2024 SNMetamorph

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*/

#pragma once
#include "event_args.h"
#include "vector.h"
#include <stdint.h>

class CBaseGameEvent
{
public:
CBaseGameEvent(event_args_s *args);
~CBaseGameEvent() = default;

protected:
Vector GetOrigin() const { return Vector(m_arguments->origin); };
Vector GetAngles() const { return Vector(m_arguments->angles); };
Vector GetVelocity() const { return Vector(m_arguments->velocity); };
int32_t GetEntityIndex() const { return m_arguments->entindex; };
bool IsEventLocal() const;
bool IsEntityPlayer() const;

event_args_t *m_arguments;
};
34 changes: 34 additions & 0 deletions client/events/game_event_manager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
game_event_manager.cpp - class that responsible for registering game events handlers
Copyright (C) 2024 SNMetamorph

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*/

#include "game_event_manager.h"
#include "hud.h"
#include "utils.h"
#include "event_api.h"
#include "event_args.h"
#include "glock_fire_event.h"

CGameEventManager::CGameEventManager()
{
RegisterGlockEvents();
}

void CGameEventManager::RegisterGlockEvents()
{
gEngfuncs.pfnHookEvent("events/glock1.sc", [](event_args_s *args) {
CGlockFireEvent event(args);
event.Execute();
});
}
31 changes: 31 additions & 0 deletions client/events/game_event_manager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
game_event_manager.h - class that responsible for registering game events handlers
Copyright (C) 2024 SNMetamorph

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*/

#pragma once

class CGameEventManager
{
public:
CGameEventManager();
~CGameEventManager() = default;

private:
CGameEventManager(const CGameEventManager&) = delete;
CGameEventManager(CGameEventManager&&) = delete;
CGameEventManager& operator=(const CGameEventManager&) = delete;
CGameEventManager& operator=(CGameEventManager&&) = delete;

void RegisterGlockEvents();
};
38 changes: 38 additions & 0 deletions client/events/game_event_utils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
game_event_utils.cpp - events-related code that used among several events
Copyright (C) 2024 SNMetamorph

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*/

#include "game_event_utils.h"
#include "hud.h"
#include "utils.h"
#include "r_efx.h"
#include "event_api.h"
#include "event_args.h"

void GameEventUtils::EjectBrass(const Vector &origin, const Vector &angles, const Vector &velocity, int modelIndex, int soundType)
{
gEngfuncs.pEfxAPI->R_TempModel(
const_cast<float*>(&origin[0]),
const_cast<float*>(&velocity[0]),
const_cast<float*>(&angles[0]),
2.5, modelIndex, soundType);
}

void GameEventUtils::SpawnMuzzleflash()
{
cl_entity_t *ent = gEngfuncs.GetViewModel();
if (ent) {
ent->curstate.effects |= EF_MUZZLEFLASH;
}
}
23 changes: 23 additions & 0 deletions client/events/game_event_utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
game_event_utils.h - events-related code that used among several events
Copyright (C) 2024 SNMetamorph

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*/

#pragma once
#include "vector.h"

namespace GameEventUtils
{
void EjectBrass(const Vector &origin, const Vector &angles, const Vector &velocity, int modelIndex, int soundType);
void SpawnMuzzleflash();
}
66 changes: 66 additions & 0 deletions client/events/glock_fire_event.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
glock_fire_event.cpp
Copyright (C) 2024 SNMetamorph

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*/

#include "glock_fire_event.h"
#include "game_event_utils.h"
#include "hud.h"
#include "const.h"
#include "utils.h"
#include "event_api.h"
#include "event_args.h"

enum glock_e {
GLOCK_IDLE1 = 0,
GLOCK_IDLE2,
GLOCK_IDLE3,
GLOCK_SHOOT,
GLOCK_SHOOT_EMPTY,
GLOCK_RELOAD,
GLOCK_RELOAD_NOT_EMPTY,
GLOCK_DRAW,
GLOCK_HOLSTER,
GLOCK_ADD_SILENCER
};

CGlockFireEvent::CGlockFireEvent(event_args_t *args) :
CBaseGameEvent(args)
{
}

void CGlockFireEvent::Execute()
{
if (IsEventLocal())
{
GameEventUtils::SpawnMuzzleflash();
gEngfuncs.pEventAPI->EV_WeaponAnimation( ClipEmpty() ? GLOCK_SHOOT_EMPTY : GLOCK_SHOOT, 2 );
// V_PunchAxis( 0, -2.0 );
}

matrix3x3 cameraMatrix(GetAngles());
Vector up = cameraMatrix.GetUp();
Vector right = cameraMatrix.GetRight();
Vector forward = cameraMatrix.GetForward();
int brassModelIndex = gEngfuncs.pEventAPI->EV_FindModelIndex("models/shell.mdl");
Vector shellVelocity = GetVelocity() + right * gEngfuncs.pfnRandomFloat(50, 70) + up * gEngfuncs.pfnRandomFloat(100, 150) + forward * 25.0f;
Vector shellOrigin = GetOrigin() + up * -12.0f + forward * 20.0f + right * 4.0f;

GameEventUtils::EjectBrass(shellOrigin, GetAngles(), shellVelocity, brassModelIndex, TE_BOUNCE_SHELL);
gEngfuncs.pEventAPI->EV_PlaySound( GetEntityIndex(), GetOrigin(), CHAN_WEAPON, "weapons/pl_gun3.wav", gEngfuncs.pfnRandomFloat(0.92, 1.0), ATTN_NORM, 0, 98 + gEngfuncs.pfnRandomLong(0, 3));
}

bool CGlockFireEvent::ClipEmpty() const
{
return m_arguments->bparam1 != 0;
}
29 changes: 29 additions & 0 deletions client/events/glock_fire_event.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
glock_fire_event.h
Copyright (C) 2024 SNMetamorph

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*/

#pragma once
#include "base_game_event.h"

class CGlockFireEvent : public CBaseGameEvent
{
public:
CGlockFireEvent(event_args_t *args);
~CGlockFireEvent() = default;

void Execute();

private:
bool ClipEmpty() const;
};
4 changes: 3 additions & 1 deletion common/entity_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,13 @@ typedef struct clientdata_s

#include "weaponinfo.h"

#define MAX_LOCAL_WEAPONS 64 // max weapons that can be predicted on the client

typedef struct local_state_s
{
entity_state_t playerstate;
clientdata_t client;
weapon_data_t weapondata[32];
weapon_data_t weapondata[MAX_LOCAL_WEAPONS];
} local_state_t;

#endif//ENTITY_STATE_H
2 changes: 2 additions & 0 deletions engine/progdefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
#ifndef PROGDEFS_H
#define PROGDEFS_H

#include "const.h"

typedef struct
{
float time;
Expand Down
Loading
Loading