dmc/src/shared/player.qc

603 lines
15 KiB
Plaintext

/*
* Copyright (c) 2016-2021 Marco Cawthorne <marco@icculus.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "items.h"
#include "../../../valve/src/shared/skeleton.h"
/* all custom SendFlags bits we can possibly send */
enumflags
{
PLAYER_TOPFRAME = PLAYER_CUSTOMFIELDSTART,
PLAYER_BOTTOMFRAME,
PLAYER_AMMO1,
PLAYER_AMMO2,
PLAYER_AMMO3,
PLAYER_UNUSED5,
PLAYER_UNUSED6,
PLAYER_UNUSED7
};
noref .float ammo_shells;
noref .float ammo_nails;
noref .float ammo_rockets;
noref .float ammo_cells;
class player:NSClientPlayer
{
void(void) player;
/* animation */
PREDICTED_INT(anim_top)
PREDICTED_FLOAT(anim_top_time)
PREDICTED_FLOAT(anim_top_delay)
PREDICTED_INT(anim_bottom)
PREDICTED_FLOAT(anim_bottom_time)
/* ammo 1 */
PREDICTED_FLOAT_N(ammo_shells)
PREDICTED_FLOAT_N(ammo_nails)
PREDICTED_FLOAT_N(ammo_rockets)
PREDICTED_FLOAT_N(ammo_cells)
/* extra types */
PREDICTED_FLOAT(ammo_lava_nails)
PREDICTED_FLOAT(ammo_multi_rockets)
PREDICTED_FLOAT(ammo_plasma)
PREDICTED_FLOAT(ammo_ice_shells)
#ifdef SERVER
entity hook;
float m_quadFinishTime;
float m_invisFinishTime;
float m_invulnFinishTime;
float m_enviroFinishTime;
float m_megaHealthTime;
float m_lightningTime;
float m_quadSoundTime;
float m_invisSoundTime;
float m_invulnSoundTime;
float m_enviroSoundTime;
#endif
virtual void Physics_Jump(void);
virtual float Physics_MaxSpeed(void);
virtual void UpdatePlayerAnimation(float);
#ifdef CLIENT
////virtual void(void) draw;
//virtual float() predraw;
//virtual void(void) postdraw;
virtual void UpdatePlayerAttachments(bool);
virtual void ReceiveEntity(float,float);
virtual void PredictPreFrame(void);
virtual void PredictPostFrame(void);
virtual void UpdateAliveCam(void);
#else
virtual void EvaluateEntity(void);
virtual float SendEntity(entity, float);
virtual void Save(float);
virtual void Restore(string,string);
#endif
nonvirtual bool HasQuadDamage(void);
nonvirtual bool HasInvisibility(void);
nonvirtual bool HasInvulnerability(void);
nonvirtual bool HasEnviroSuit(void);
};
void Animation_PlayerUpdate(player);
void Animation_TimerUpdate(player, float);
void
player::UpdatePlayerAnimation(float timelength)
{
/* calculate our skeletal progression */
Animation_PlayerUpdate(this);
/* advance animation timers */
Animation_TimerUpdate(this, timelength);
}
#ifdef CLIENT
void Camera_RunPosBob(vector angles, __inout vector camera_pos);
void Camera_StrafeRoll(__inout vector camera_angle);
void Shake_Update(NSClientPlayer);
void
player::UpdateAliveCam(void)
{
vector cam_pos = GetEyePos();
Camera_RunPosBob(view_angles, cam_pos);
g_view.SetCameraOrigin(cam_pos);
Camera_StrafeRoll(view_angles);
g_view.SetCameraAngle(view_angles);
if (vehicle) {
NSVehicle veh = (NSVehicle)vehicle;
if (veh.UpdateView)
veh.UpdateView();
} else if (health) {
if (autocvar_pm_thirdPerson == TRUE) {
makevectors(view_angles);
vector vStart = [pSeat->m_vecPredictedOrigin[0], pSeat->m_vecPredictedOrigin[1], pSeat->m_vecPredictedOrigin[2] + 16] + (v_right * 4);
vector vEnd = vStart + (v_forward * -48) + [0,0,16] + (v_right * 4);
traceline(vStart, vEnd, FALSE, this);
g_view.SetCameraOrigin(trace_endpos + (v_forward * 5));
}
}
Shake_Update(this);
g_view.AddPunchAngle(punchangle);
}
.string oldmodel;
string Weapons_GetPlayermodel(player, int);
void
player::UpdatePlayerAttachments(bool visible)
{
/* draw the flashlight */
if (gflags & GF_FLASHLIGHT) {
vector src;
vector ang;
if (entnum != player_localentnum) {
src = origin + view_ofs;
ang = v_angle;
} else {
src = pSeat->m_vecPredictedOrigin + [0,0,-8];
ang = view_angles;
}
makevectors(ang);
traceline(src, src + (v_forward * 8096), MOVE_NORMAL, this);
if (serverkeyfloat("*bspversion") == BSPVER_HL) {
dynamiclight_add(trace_endpos + (v_forward * -2), 128, [1,1,1]);
} else {
float p = dynamiclight_add(src, 512, [1,1,1], 0, "textures/flashlight");
dynamiclight_set(p, LFIELD_ANGLES, ang);
dynamiclight_set(p, LFIELD_FLAGS, 3);
}
}
/* FIXME: this needs to be incorporated and simplified, now that we can handle it all in-class */
if (!visible)
return;
/* what's the current weapon model supposed to be anyway? */
p_model.oldmodel = Weapons_GetPlayermodel(this, activeweapon);
/* we changed weapons, update skeletonindex */
if (p_model.model != p_model.oldmodel) {
/* free memory */
if (p_model.skeletonindex)
skel_delete(p_model.skeletonindex);
/* set the new model and mark us updated */
setmodel(p_model, p_model.oldmodel);
p_model.model = p_model.oldmodel;
/* set the new skeletonindex */
p_model.skeletonindex = skel_create(p_model.modelindex);
/* hack this thing in here FIXME: this should be done when popping in/out of a pvs */
if (autocvar(cl_himodels, 1, "Use high-quality thisayer models over lower-definition ones"))
setcustomskin(this, "", "geomset 0 2\n");
else
setcustomskin(this, "", "geomset 0 1\n");
}
/* follow thisayer at all times */
setorigin(p_model, origin);
p_model.angles = angles;
skel_build(p_model.skeletonindex, p_model, p_model.modelindex,0, 0, -1);
/* we have to loop through all valid bones of the weapon model and match them
* to the thisayer one */
for (float i = 0; i < g_pbones.length; i++) {
vector bpos;
float pbone = gettagindex(this, g_pbones[i]);
float wbone = gettagindex(p_model, g_pbones[i]);
/* if the bone doesn't ignore in either skeletal mesh, ignore */
if (wbone <= 0 || pbone <= 0)
continue;
bpos = gettaginfo(this, pbone);
/* the most expensive bit */
skel_set_bone_world(p_model, wbone, bpos, v_forward, v_right, v_up);
}
}
void Weapons_AmmoUpdate(entity);
void HUD_AmmoNotify_Check(player pl);
void HUD_ItemNotify_Check(player pl);
/*
=================
player::ReceiveEntity
=================
*/
void
player::ReceiveEntity(float new, float flChanged)
{
/* the generic client attributes */
NSClientPlayer::ReceiveEntity(new, flChanged);
/* animation */
READENTITY_BYTE(anim_top, PLAYER_TOPFRAME)
READENTITY_FLOAT(anim_top_time, PLAYER_TOPFRAME)
READENTITY_FLOAT(anim_top_delay, PLAYER_TOPFRAME)
READENTITY_BYTE(anim_bottom, PLAYER_BOTTOMFRAME)
READENTITY_FLOAT(anim_bottom_time, PLAYER_BOTTOMFRAME)
READENTITY_BYTE(ammo_shells, PLAYER_AMMO1)
READENTITY_BYTE(ammo_nails, PLAYER_AMMO1)
READENTITY_BYTE(ammo_rockets, PLAYER_AMMO1)
READENTITY_BYTE(ammo_cells, PLAYER_AMMO1)
READENTITY_BYTE(ammo_lava_nails, PLAYER_AMMO2)
READENTITY_BYTE(ammo_multi_rockets, PLAYER_AMMO2)
READENTITY_BYTE(ammo_plasma, PLAYER_AMMO2)
READENTITY_BYTE(ammo_ice_shells, PLAYER_AMMO2)
setorigin(this, origin);
/* these only concern the current player */
CSQC_UpdateSeat();
if (this != pSeat->m_ePlayer)
return;
/* do not notify us of updates when spawning initially */
if (flChanged == UPDATE_ALL)
PredictPreFrame();
if (flChanged & PLAYER_AMMO1 || flChanged & PLAYER_AMMO2 || flChanged & PLAYER_AMMO3) {
Weapons_AmmoUpdate(this);
HUD_AmmoNotify_Check(this);
}
if (flChanged & PLAYER_ITEMS || flChanged & PLAYER_HEALTH) {
HUD_ItemNotify_Check(this);
p_model.SetRenderMode(GetRenderMode());
p_model.SetRenderFX(GetRenderFX());
p_model.SetRenderColor(GetRenderColor());
p_model.SetRenderAmt(GetRenderAmt());
}
}
/*
=================
player::PredictPostFrame
Save the last valid server values away in the _net variants of each field
so we can roll them back later.
=================
*/
void
player::PredictPreFrame(void)
{
/* the generic client attributes */
NSClientPlayer::PredictPreFrame();
SAVE_STATE(anim_top)
SAVE_STATE(anim_top_delay)
SAVE_STATE(anim_top_time)
SAVE_STATE(anim_bottom)
SAVE_STATE(anim_bottom_time)
SAVE_STATE(ammo_shells)
SAVE_STATE(ammo_nails)
SAVE_STATE(ammo_rockets)
SAVE_STATE(ammo_cells)
SAVE_STATE(ammo_lava_nails)
SAVE_STATE(ammo_multi_rockets)
SAVE_STATE(ammo_plasma)
SAVE_STATE(ammo_ice_shells)
}
/*
=================
player::PredictPostFrame
Where we roll back our values to the ones last sent/verified by the server.
=================
*/
void
player::PredictPostFrame(void)
{
/* the generic client attributes */
NSClientPlayer::PredictPostFrame();
ROLL_BACK(anim_top)
ROLL_BACK(anim_top_delay)
ROLL_BACK(anim_top_time)
ROLL_BACK(anim_bottom)
ROLL_BACK(anim_bottom_time)
ROLL_BACK(ammo_shells)
ROLL_BACK(ammo_nails)
ROLL_BACK(ammo_rockets)
ROLL_BACK(ammo_cells)
ROLL_BACK(ammo_lava_nails)
ROLL_BACK(ammo_multi_rockets)
ROLL_BACK(ammo_plasma)
ROLL_BACK(ammo_ice_shells)
}
#else
void
player::Save(float handle)
{
super::Save(handle);
SaveInt(handle, "anim_top", anim_top);
SaveFloat(handle, "anim_top_time", anim_top_time);
SaveFloat(handle, "anim_top_delay", anim_top_delay);
SaveInt(handle, "anim_bottom", anim_bottom);
SaveFloat(handle, "anim_bottom_time", anim_bottom_time);
/* ammo 1 */
SaveInt(handle, "ammo_shells", ammo_shells);
SaveInt(handle, "ammo_nails", ammo_nails);
SaveInt(handle, "ammo_rockets", ammo_rockets);
SaveInt(handle, "ammo_cells", ammo_cells);
/* extra */
SaveInt(handle, "ammo_lava_nails", ammo_lava_nails);
SaveInt(handle, "ammo_multi_rockets", ammo_multi_rockets);
SaveInt(handle, "ammo_plasma", ammo_plasma);
SaveInt(handle, "ammo_ice_shells", ammo_ice_shells);
}
void
player::Restore(string strKey, string strValue)
{
switch (strKey) {
case "anim_top":
anim_top = ReadInt(strValue);
break;
case "anim_top_time":
anim_top_time = ReadFloat(strValue);
break;
case "anim_top_delay":
anim_top_delay = ReadFloat(strValue);
break;
case "anim_bottom":
anim_bottom = ReadInt(strValue);
break;
case "anim_bottom_time":
anim_bottom_time = ReadFloat(strValue);
break;
/* AMMO 1 */
case "ammo_shells":
ammo_shells = ReadInt(strValue);
break;
case "ammo_nails":
ammo_nails = ReadInt(strValue);
break;
case "ammo_rockets":
ammo_rockets = ReadInt(strValue);
break;
case "ammo_cells":
ammo_cells = ReadInt(strValue);
break;
/* extra */
case "ammo_lava_nails":
ammo_lava_nails = ReadInt(strValue);
break;
case "ammo_multi_rockets":
ammo_multi_rockets = ReadInt(strValue);
break;
case "ammo_plasma":
ammo_plasma = ReadInt(strValue);
break;
case "ammo_ice_shells":
ammo_ice_shells = ReadInt(strValue);
break;
default:
super::Restore(strKey, strValue);
}
}
void
player::EvaluateEntity(void)
{
/* check artifacts for their effect time */
if (g_items & ITEM_QUAD) {
if (m_quadFinishTime < time) {
g_items &= ~ITEM_QUAD;
}
if ((m_quadSoundTime < time) && m_quadFinishTime < (time + 3.0f)) {
StartSoundDef("item_artifact_super_damage.expiring", CHAN_ITEM, true);
m_quadSoundTime = time + 5.0f;
}
}
if (g_items & ITEM_INVIS) {
if (m_invisFinishTime < time) {
g_items &= ~ITEM_INVIS;
Sound_Stop(this, 6);
}
if ((m_invisSoundTime < time) && m_invisFinishTime < (time + 3.0f)) {
StartSoundDef("item_artifact_invisibility.expiring", CHAN_ITEM, true);
m_invisSoundTime = time + 5.0f;
}
}
if (g_items & ITEM_INVULN) {
if (m_invulnFinishTime < time) {
g_items &= ~ITEM_INVULN;
}
if ((m_invulnSoundTime < time) && m_invulnFinishTime < (time + 3.0f)) {
StartSoundDef("item_artifact_invulnerability.expiring", CHAN_ITEM, true);
m_invulnSoundTime = time + 5.0f;
}
}
if (g_items & ITEM_ENVIROSUIT) {
if (m_enviroFinishTime < time) {
g_items &= ~ITEM_ENVIROSUIT;
}
if ((m_enviroSoundTime < time) && m_enviroFinishTime < (time + 3.0f)) {
StartSoundDef("item_artifact_envirosuit.expiring", CHAN_ITEM, true);
m_enviroSoundTime = time + 5.0f;
}
}
/* depending on who's left, set the appropriate rendering mode */
if (HasInvisibility()) {
SetRenderMode(RM_DONTRENDER);
SetRenderFX(RFX_GLOWSHELL);
SetRenderColor([0.5, 0.5, 0.5]);
SetRenderAmt(0.1f);
} else if (HasQuadDamage()) {
SetRenderFX(RFX_GLOWSHELL);
SetRenderColor([0.5, 0.5, 1.0]);
SetRenderAmt(1.0f);
} else if (HasInvulnerability()) {
SetRenderFX(RFX_GLOWSHELL);
SetRenderColor([1.0, 0.5, 0.0]);
SetRenderAmt(1.0f);
} else if (HasEnviroSuit()) {
SetRenderFX(RFX_GLOWSHELL);
SetRenderColor([0.5, 1.0, 0.5]);
SetRenderAmt(1.0f);
} else {
SetRenderMode(RM_NORMAL);
SetRenderFX(RM_NORMAL);
SetRenderColor([1.0, 1.0, 1.0]);
SetRenderAmt(1.0f);
}
if (health > 100) {
if (m_megaHealthTime < time) {
health -= 1;
m_megaHealthTime = time + 1.0f;
}
}
/* the generic client attributes */
NSClientPlayer::EvaluateEntity();
EVALUATE_FIELD(anim_top, PLAYER_TOPFRAME)
EVALUATE_FIELD(anim_top_time, PLAYER_TOPFRAME)
EVALUATE_FIELD(anim_top_delay, PLAYER_TOPFRAME)
EVALUATE_FIELD(anim_bottom, PLAYER_BOTTOMFRAME)
EVALUATE_FIELD(anim_bottom_time, PLAYER_BOTTOMFRAME)
EVALUATE_FIELD(ammo_shells, PLAYER_AMMO1)
EVALUATE_FIELD(ammo_nails, PLAYER_AMMO1)
EVALUATE_FIELD(ammo_rockets, PLAYER_AMMO1)
EVALUATE_FIELD(ammo_cells, PLAYER_AMMO1)
EVALUATE_FIELD(ammo_lava_nails, PLAYER_AMMO2)
EVALUATE_FIELD(ammo_multi_rockets, PLAYER_AMMO2)
EVALUATE_FIELD(ammo_plasma, PLAYER_AMMO2)
EVALUATE_FIELD(ammo_ice_shells, PLAYER_AMMO2)
}
/*
=================
player::SendEntity
=================
*/
float
player::SendEntity(entity ePEnt, float flChanged)
{
/* don't broadcast invisible players */
if (IsFakeSpectator() && ePEnt != this)
return (0);
if (!GetModelindex() && ePEnt != this)
return (0);
flChanged = OptimiseChangedFlags(ePEnt, flChanged);
WriteByte(MSG_ENTITY, ENT_PLAYER);
WriteFloat(MSG_ENTITY, flChanged);
/* the generic client attributes */
NSClientPlayer::SendEntity(ePEnt, flChanged);
SENDENTITY_BYTE(anim_top, PLAYER_TOPFRAME)
SENDENTITY_FLOAT(anim_top_time, PLAYER_TOPFRAME)
SENDENTITY_FLOAT(anim_top_delay, PLAYER_TOPFRAME)
SENDENTITY_BYTE(anim_bottom, PLAYER_BOTTOMFRAME)
SENDENTITY_FLOAT(anim_bottom_time, PLAYER_BOTTOMFRAME)
SENDENTITY_BYTE(ammo_shells, PLAYER_AMMO1)
SENDENTITY_BYTE(ammo_nails, PLAYER_AMMO1)
SENDENTITY_BYTE(ammo_rockets, PLAYER_AMMO1)
SENDENTITY_BYTE(ammo_cells, PLAYER_AMMO1)
SENDENTITY_BYTE(ammo_lava_nails, PLAYER_AMMO2)
SENDENTITY_BYTE(ammo_multi_rockets, PLAYER_AMMO2)
SENDENTITY_BYTE(ammo_plasma, PLAYER_AMMO2)
SENDENTITY_BYTE(ammo_ice_shells, PLAYER_AMMO2)
return (1);
}
#endif
bool
player::HasQuadDamage(void)
{
return g_items & ITEM_QUAD ? true : false;
}
bool
player::HasInvisibility(void)
{
return g_items & ITEM_INVIS ? true : false;
}
bool
player::HasInvulnerability(void)
{
return g_items & ITEM_INVULN ? true : false;
}
bool
player::HasEnviroSuit(void)
{
return g_items & ITEM_ENVIROSUIT ? true : false;
}
void
player::player(void)
{
anim_top = 0;
anim_top_time = 0;
anim_top_delay = 0;
anim_bottom = 0;
anim_bottom_time = 0;
ammo_shells = 0;
ammo_nails = 0;
ammo_rockets = 0;
ammo_cells = 0;
ammo_lava_nails = 0;
ammo_multi_rockets = 0;
ammo_plasma = 0;
ammo_ice_shells = 0;
}