Skip to content

Commit

Permalink
Enhanced point_servercommand, point_clientcommand: Added reset of cva…
Browse files Browse the repository at this point in the history
…r values on remove an entity or change level
  • Loading branch information
s1lentq committed Jun 8, 2019
1 parent 1146aa5 commit cee63d9
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 17 deletions.
83 changes: 70 additions & 13 deletions regamedll/dlls/addons/point_command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,18 @@
void CPointBaseCommand::KeyValue(KeyValueData *pkvd)
{
// add this field to the command list
if (m_uiCommandsCount < MAX_POINT_CMDS)
if (m_vecCommands.Count() < MAX_POINT_CMDS)
{
char command[128];
if (pkvd->szValue[0] != '\0' && Q_strcmp(pkvd->szValue, "-") != 0)
if (pkvd->szValue[0] != '\0' &&
Q_strcmp(pkvd->szValue, "-") != 0)
{
Q_snprintf(command, sizeof(command), "%s \"%s\"", pkvd->szKeyName, pkvd->szValue);
m_vecCommands.AddToTail({ pkvd->szKeyName, pkvd->szValue });
}
else
{
Q_strlcpy(command, pkvd->szKeyName);
m_vecCommands.AddToTail(pkvd->szKeyName);
}

m_iszCommands[m_uiCommandsCount++] = ALLOC_STRING(command);
pkvd->fHandled = TRUE;
return;
}
Expand Down Expand Up @@ -61,12 +60,24 @@ void CPointClientCommand::Use(CBaseEntity *pActivator, CBaseEntity *pCaller, USE

if (pClient)
{
for (size_t cmd = 0; cmd < m_uiCommandsCount; cmd++) {
CLIENT_COMMAND(pClient, UTIL_VarArgs("%s\n", m_iszCommands[cmd].str()));
for (auto &cmd : m_vecCommands) {
Execute(pClient, "%s \"%s\"\n", cmd.name, cmd.value);
}
}
}

void CPointClientCommand::Execute(edict_t *pEdict, const char *pszFmt, ...)
{
va_list argptr;
char command[128];

va_start(argptr, pszFmt);
Q_vsnprintf(command, sizeof(command), pszFmt, argptr);
va_end(argptr);

CLIENT_COMMAND(pEdict, command);
}

LINK_ENTITY_TO_CLASS(point_servercommand, CPointServerCommand, CCSPointServerCommand)

void CPointServerCommand::Use(CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value)
Expand All @@ -78,20 +89,66 @@ void CPointServerCommand::Use(CBaseEntity *pActivator, CBaseEntity *pCaller, USE
}
#endif

for (size_t cmd = 0; cmd < m_uiCommandsCount; cmd++) {
Execute(m_iszCommands[cmd]);
for (auto &cmd : m_vecCommands)
{
cvar_t *pCVar = CVAR_GET_POINTER(cmd.name);
if (pCVar &&
pCVar->string &&
pCVar->string[0] != '\0' &&
Q_stricmp(cmd.value, pCVar->string) != 0) {
Q_strlcpy(cmd.valueInitial, pCVar->string);
}

if (cmd.value[0] != '\0')
{
Execute(nullptr, "%s \"%s\"\n", cmd.name, cmd.value);
}
else
{
Execute(nullptr, "%s\n", cmd.name);
}
}
}

void CPointServerCommand::Execute(const char *command)
void CPointServerCommand::Execute(edict_t *pEdict, const char *pszFmt, ...)
{
va_list argptr;
char command[128];

va_start(argptr, pszFmt);
Q_vsnprintf(command, sizeof(command), pszFmt, argptr);
va_end(argptr);

if (!IS_DEDICATED_SERVER())
{
// potentially dangerous for untrusted maps
// so try to use it for passing through filtered svc_stufftext
CLIENT_COMMAND(INDEXENT(1), UTIL_VarArgs("%s\n", command));
CLIENT_COMMAND(pEdict ? pEdict : INDEXENT(1), command);
return;
}

SERVER_COMMAND(UTIL_VarArgs("%s\n", command));
SERVER_COMMAND(command);
}

void CPointBaseCommand::OnDestroy()
{
if (!(pev->spawnflags & SF_POINT_CMD_NORESET))
{
bool bAtLeastOneCmdReset = false;
for (auto &cmd : m_vecCommands)
{
if (cmd.valueInitial[0] != '\0')
{
Execute(nullptr, "%s \"%s\"\n", cmd.name, cmd.valueInitial);
bAtLeastOneCmdReset = true;
}
}

if (bAtLeastOneCmdReset)
{
SERVER_EXECUTE();
}
}

m_vecCommands.RemoveAll();
}
33 changes: 29 additions & 4 deletions regamedll/dlls/addons/point_command.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,41 @@

#pragma once

#include <utlvector.h>

const int MAX_POINT_CMDS = 16; // maximum number of commands a single point_[server/client]command entity may be assigned

#define SF_POINT_CMD_NORESET BIT(0) // it is not allowed to be resetting to initial value on remove an entity or change level

class CPointBaseCommand: public CPointEntity {
public:
virtual void OnDestroy();
virtual void KeyValue(KeyValueData *pkvd);
virtual void Use(CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value) = 0;
virtual void Execute(edict_t *pEdict, const char *pszFmt, ...) = 0;

protected:
size_t m_uiCommandsCount;
string_t m_iszCommands[MAX_POINT_CMDS];

template <size_t SIZE>
struct command_t
{
command_t(const char *_name, const char *_value = nullptr)
{
value[0] = '\0';
valueInitial[0] = '\0';

Q_strlcpy(name, _name);

if (_value)
{
Q_strlcpy(value, _value);
}
}

char name[SIZE], value[SIZE], valueInitial[SIZE];
};

CUtlVector<command_t<64u>> m_vecCommands;
};

// It issues commands to the client console
Expand All @@ -36,7 +61,7 @@ class CPointClientCommand: public CPointBaseCommand {
virtual void Use(CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value);

private:
void Execute(edict_t *pEdict, const char *command);
void Execute(edict_t *pEdict, const char *pszFmt, ...);
};

// It issues commands to the server console
Expand All @@ -45,5 +70,5 @@ class CPointServerCommand: public CPointBaseCommand {
virtual void Use(CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value);

private:
void Execute(const char *command);
void Execute(edict_t *pEdict, const char *pszFmt, ...);
};
4 changes: 4 additions & 0 deletions regamedll/extra/Toolkit/GameDefinitionFile/regamedll-cs.fgd
Original file line number Diff line number Diff line change
Expand Up @@ -3029,6 +3029,10 @@

@BaseClass base(Targetname) = BaseCommand
[
spawnflags(flags) =
[
1 : "No reset cmd on remove an entity or change level" : 0
]
]

@PointClass base(BaseCommand) size(-8 -8 -8, 8 8 8) = point_servercommand : "It issues commands to the server console"
Expand Down

0 comments on commit cee63d9

Please sign in to comment.