Skip to content

Ported trigger_userinput from Momentum Mod #389

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

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
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
83 changes: 83 additions & 0 deletions sp/src/game/server/triggers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5678,3 +5678,86 @@ bool IsTriggerClass( CBaseEntity *pEntity )

return false;
}

#ifdef MAPBASE
LINK_ENTITY_TO_CLASS(trigger_userinput, CTriggerUserInput);

BEGIN_DATADESC(CTriggerUserInput)
DEFINE_KEYFIELD(m_eKey, FIELD_INTEGER, "lookedkey"),
DEFINE_OUTPUT(m_OnKeyPressed, "OnKeyPressed"),
DEFINE_OUTPUT(m_OnKeyHeld, "OnKeyHeld"),
DEFINE_OUTPUT(m_OnKeyReleased, "OnKeyReleased"),
END_DATADESC();

CTriggerUserInput::CTriggerUserInput()
{
m_eKey = KEY_FORWARD;
m_ButtonRep = IN_FORWARD;
}

void CTriggerUserInput::Spawn()
{
switch (m_eKey)
{
case KEY_FORWARD:
m_ButtonRep = IN_FORWARD;
break;
case KEY_BACK:
m_ButtonRep = IN_BACK;
break;
case KEY_MOVELEFT:
m_ButtonRep = IN_MOVELEFT;
break;
case KEY_MOVERIGHT:
m_ButtonRep = IN_MOVERIGHT;
break;
case KEY_JUMP:
m_ButtonRep = IN_JUMP;
break;
case KEY_DUCK:
m_ButtonRep = IN_DUCK;
break;
case KEY_ATTACK:
m_ButtonRep = IN_ATTACK;
break;
case KEY_ATTACK2:
m_ButtonRep = IN_ATTACK2;
break;
case KEY_RELOAD:
m_ButtonRep = IN_RELOAD;
break;
default:
DevWarning("Passed unhandled key press");
m_ButtonRep = 0;
break;
}

BaseClass::Spawn();
BaseClass::InitTrigger();
}

void CTriggerUserInput::Touch(CBaseEntity* pOther)
{
if (PassesTriggerFilters(pOther))
{
CBasePlayer* pPlayer = ToBasePlayer(pOther);
if (pPlayer)
{
if (pPlayer->m_afButtonPressed & m_ButtonRep)
{
m_OnKeyPressed.FireOutput(pPlayer, this);
}

if (pPlayer->m_nButtons & m_ButtonRep)
{
m_OnKeyHeld.FireOutput(pPlayer, this);
}

if (pPlayer->m_afButtonReleased & m_ButtonRep)
{
m_OnKeyReleased.FireOutput(pPlayer, this);
}
}
}
}
#endif // MAPBASE
38 changes: 38 additions & 0 deletions sp/src/game/server/triggers.h
Original file line number Diff line number Diff line change
Expand Up @@ -356,4 +356,42 @@ class CTriggerCamera : public CBaseEntity
#endif
};

#ifdef MAPBASE
//--------- CTriggerUserInput -------------------------------------------------------------------
//
// Ported from Momentum Mod
// https://github.com/momentum-mod/game/blob/2e490c7e722788ade7221ba7ba5d4d503d60a115/mp/src/game/server/momentum/mom_triggers.cpp#L1200
//
//-----------------------------------------------------------------------------------------------
class CTriggerUserInput : public CBaseTrigger
{
DECLARE_CLASS(CTriggerUserInput, CBaseTrigger);
public:
CTriggerUserInput();
void Spawn();
void Touch(CBaseEntity* pOther);
DECLARE_DATADESC();

private:
enum Key
{
KEY_FORWARD = 0,
KEY_BACK,
KEY_MOVELEFT,
KEY_MOVERIGHT,
KEY_JUMP,
KEY_DUCK,
KEY_ATTACK,
KEY_ATTACK2,
KEY_RELOAD
};
int m_ButtonRep;
Key m_eKey;
Comment on lines +376 to +389
Copy link
Member

@Blixibon Blixibon Feb 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm curious why this uses its own separate enum instead of allowing mappers to check button bits directly, but that's a question for Momentum Mod more-so than for this PR.

While I would prefer to keep functionality the same between the way it works in Mapbase and the way it works in Momentum Mod, I think it would make sense to allow for explicit button bits, especially if this may get extended to other buttons like +attack3. Perhaps there could be an alternate keyvalue to handle that.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think there is any need for parity with another mod for such a stupid simple entity. If this entity is to be added, the empty base class and this enum should be removed. I suppose keeping this separate would make changes to the order of IN_BUTTONS enum not break this entity, but that's really unlikely.

"Key" name is usually used for keyboard keys instead of player input buttons. "OnKey" outputs don't make much sense. "lookedkey" also sounds weird.

It seems more appropriate to have this as a point entity or an extension for game_ui instead of limiting it to a trigger. Mappers looking for something like this would certainly want to use it without a trigger.

Copy link
Author

@Nbc66 Nbc66 Feb 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah i agree this pr just ports what Momentum Mod did i can modify this pr to make it be a point entity that just checks the players input or be an extension of game_ui

If there is anything else you guys would like me to do pls feel free to write it here

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think there is any need for parity with another mod for such a stupid simple entity. If this entity is to be added, the empty base class and this enum should be removed. I suppose keeping this separate would make changes to the order of IN_BUTTONS enum not break this entity, but that's really unlikely.

"Key" name is usually used for keyboard keys instead of player input buttons. "OnKey" outputs don't make much sense. "lookedkey" also sounds weird.

It seems more appropriate to have this as a point entity or an extension for game_ui instead of limiting it to a trigger. Mappers looking for something like this would certainly want to use it without a trigger.

also I just realized that this trigger works with multiple people unlike the game_ui entity which is needed for each player on a map

if you got any code suggestions pls feel free to write em here

COutputEvent m_OnKeyPressed;
COutputEvent m_OnKeyHeld;
COutputEvent m_OnKeyReleased;
};
#endif // MAPBASE


#endif // TRIGGERS_H