Skip to content

Commit

Permalink
client: refactored player flashlight implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
SNMetamorph committed May 18, 2024
1 parent b14f441 commit 4809cac
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 106 deletions.
1 change: 0 additions & 1 deletion client/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ list(APPEND CLDLL_SOURCES
"battery.cpp"
"cdll_int.cpp"
"death.cpp"
"flashlight.cpp"
"geiger.cpp"
"health.cpp"
"entity.cpp"
Expand Down
7 changes: 0 additions & 7 deletions client/entity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#include "gl_studio.h"
#include "gl_cvars.h"
#include "gl_rpart.h"
#include "flashlight.h"
#include "exportdef.h"

void Game_AddObjects( void );
Expand Down Expand Up @@ -186,12 +185,6 @@ void DLLEXPORT HUD_ProcessPlayerState( struct entity_state_s *dst, const struct
// g_iPlayerClass = dst->playerclass;
// g_iTeamNumber = dst->team;
//}

// buz: get flashlight status
if (dst->effects & EF_DIMLIGHT)
g_PlayerFlashlight.TurnOn();
else
g_PlayerFlashlight.TurnOff();
}

/*
Expand Down
78 changes: 0 additions & 78 deletions client/flashlight.cpp

This file was deleted.

16 changes: 0 additions & 16 deletions client/flashlight.h

This file was deleted.

4 changes: 0 additions & 4 deletions client/r_view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#include "r_studioint.h"
#include "gl_studio.h"
#include "gl_cvars.h"
#include "flashlight.h"
#include <mathlib.h>

// thirdperson camera
Expand Down Expand Up @@ -986,9 +985,6 @@ void V_CalcFirstPersonRefdef( struct ref_params_s *pparams )

// smooth player view in multiplayer
V_InterpolatePos( pparams );

// update flashlight state
g_PlayerFlashlight.Update(pparams);
}

//==========================
Expand Down
66 changes: 66 additions & 0 deletions client/render/gl_scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ GNU General Public License for more details.
#include "screenfade.h"
#include "shake.h"
#include "cl_env_dynlight.h"
#include <unordered_map>

/*
===============
Expand Down Expand Up @@ -397,6 +398,67 @@ int R_ComputeFxBlend( cl_entity_t *e )
return blend;
}

static void R_SetupPlayerFlashlight(cl_entity_t *ent)
{
pmtrace_t ptr;
Vector origin, vecEnd, angles;
Vector forward, right, up;
static std::unordered_map<int, float> cached_smooth_values;

if (UTIL_IsLocal(ent->index))
{
vec3_t viewOffset;
cl_entity_t *localPlayer = gEngfuncs.GetLocalPlayer();
gEngfuncs.pEventAPI->EV_LocalPlayerViewheight(viewOffset);
origin = localPlayer->origin + viewOffset;

gEngfuncs.GetViewAngles(angles);
gEngfuncs.pfnAngleVectors(angles, forward, right, up);
origin += (up * 6.0f) + (right * 5.0f) + (forward * 2.0f);
vecEnd = origin + forward * 700.0f;
}
else
{
angles = ent->angles;
angles.x = -angles.x * 3;
gEngfuncs.pfnAngleVectors(angles, forward, right, up);
origin = ent->curstate.origin;
vecEnd = origin + forward * 700.0f;
}

gEngfuncs.pEventAPI->EV_SetTraceHull(2);
gEngfuncs.pEventAPI->EV_PlayerTrace(origin, vecEnd, 0, -1, &ptr);

float addideal = 0.0f;
if (ptr.fraction < 1.0f) {
addideal = (1.0f - ptr.fraction) * 30.0f;
}

float &add = cached_smooth_values[ent->index];
float speed = (add - addideal) * 10.0f;
if (speed < 0) speed *= -1.0f;

if (add < addideal)
{
add += GET_FRAMETIME() * speed;
if (add > addideal) add = addideal;
}
else if (add > addideal)
{
add -= GET_FRAMETIME() * speed;
if (add < addideal) add = addideal;
}

CDynLight *flashlight = CL_AllocDlight(FLASHLIGHT_KEY - ent->index);
R_SetupLightParams(flashlight, origin, angles, 700.0f, 35.0f + add, LIGHT_SPOT);
R_SetupLightTexture(flashlight, tr.flashlightTexture);

flashlight->flags = DLF_PARENTENTITY_NOSHADOW;
flashlight->parentEntity = ent;
flashlight->color = Vector(1.4f, 1.4f, 1.4f); // make model dymanic lighting happy
flashlight->die = tr.time + 0.05f;
}

static bool R_HandleLightEntity(cl_entity_t *ent)
{
// not light entity
Expand Down Expand Up @@ -523,6 +585,10 @@ qboolean R_AddEntity( struct cl_entity_s *clent, int entityType )
if (R_HandleLightEntity(clent))
return true;

if (clent->curstate.effects & EF_DIMLIGHT) {
R_SetupPlayerFlashlight(clent);
}

if (clent->curstate.effects & EF_SCREENMOVIE)
{
// update cin sound properly
Expand Down

0 comments on commit 4809cac

Please sign in to comment.