halflife icon indicating copy to clipboard operation
halflife copied to clipboard

Fix CBaseMonster::ReportAIState not handling all MONSTERSTATE enum values

Open SamVanheer opened this issue 3 years ago • 0 comments

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: https://github.com/ValveSoftware/halflife/blob/c7240b965743a53a29491dd49320c88eecf6257b/dlls/util.h#L171-L184

Affected code: https://github.com/ValveSoftware/halflife/blob/c7240b965743a53a29491dd49320c88eecf6257b/dlls/monsters.cpp#L2895-L2899

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");

SamVanheer avatar Dec 26 '21 12:12 SamVanheer