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

Fix CBaseMonster::ReportAIState not handling all MONSTERSTATE enum values #3220

Open
SamVanheer opened this issue Dec 26, 2021 · 0 comments
Open

Comments

@SamVanheer
Copy link

The method CBaseMonster::ReportAIState prints the string name of the MONSTERSTATE enum value currently used by the monster. The array of names doesn't consider all values and won't print the last 2 values correctly (PlayDead is printed as "Dead", Dead isn't printed at all).

Enum definition:

halflife/dlls/util.h

Lines 171 to 184 in c7240b9

typedef enum
{
MONSTERSTATE_NONE = 0,
MONSTERSTATE_IDLE,
MONSTERSTATE_COMBAT,
MONSTERSTATE_ALERT,
MONSTERSTATE_HUNT,
MONSTERSTATE_PRONE,
MONSTERSTATE_SCRIPT,
MONSTERSTATE_PLAYDEAD,
MONSTERSTATE_DEAD
} MONSTERSTATE;

Affected code:

halflife/dlls/monsters.cpp

Lines 2895 to 2899 in c7240b9

static const char *pStateNames[] = { "None", "Idle", "Combat", "Alert", "Hunt", "Prone", "Scripted", "Dead" };
ALERT( level, "%s: ", STRING(pev->classname) );
if ( (int)m_MonsterState < ARRAYSIZE(pStateNames) )
ALERT( level, "State: %s, ", pStateNames[m_MonsterState] );

To fix this the array needs to have a matching number of elements:

static const char *pStateNames[] = { "None", "Idle", "Combat", "Alert", "Hunt", "Prone", "Scripted", "PlayDead", "Dead" };

For good measure you could also add in a static_assert (C++11 or newer) to verify that the number of enum values matches the array size:

typedef enum 
{

	MONSTERSTATE_NONE = 0,
	MONSTERSTATE_IDLE,
	MONSTERSTATE_COMBAT,
	MONSTERSTATE_ALERT,
	MONSTERSTATE_HUNT,
	MONSTERSTATE_PRONE,
	MONSTERSTATE_SCRIPT,
	MONSTERSTATE_PLAYDEAD,
	MONSTERSTATE_DEAD,

	MONSTERSTATE_COUNT //Must be last, not a valid state

} MONSTERSTATE;

//In CBaseMonster::ReportAIState
static_assert(ARRAYSIZE(pStateNames) == MONSTERSTATE_COUNT, "You forgot to update the array of monster state names");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant