Fix scientist voice pitch - FreeSlave/halflife-updated GitHub Wiki

Introduction

Scientists in Half-Life have 4 different heads: Glasses, Einstein, Luther and Slick. The voice pitch of the scientist depends on the head. E.g. the Glasses one has a bit higher pitch, while Luther speaks in a bit lower pitch.

Note: the fix was merged into halflife-updated. If your project is based on halflife-updated, then merge/rebase upon the main repository.

The Problem

If scientist's head is random, the scientist may get the incorrect voice pitch.

The Research

Pitch is configured in CScientist::TalkInit, which is called from CScientist::Precache, which is called from CScientist::Spawn, and the scientist's body gets randomized after that. So the problem arises from the execution order. The code that sets the pitch does not know what head will be randomized to.

The bug gets fixed in the game after save-load because Precache gets called again (independent of Spawn) and this time the head is already known at this point. Still, the problem exists on the first level load, so let's fix it.

The Solution

The solution is simple as moving these lines

if (pev->body == -1)
{														 // -1 chooses a random head
	pev->body = RANDOM_LONG(0, NUM_SCIENTIST_HEADS - 1); // pick a head, any head
}

before the Precache call, at the start of the Spawn function. This way we ensure that the scientist head is known when TalkInit is called.

Appendix

There's another little bug that causes Slick scientists to have a higher pitch (just like Glasses). This is due to wrong calculation in the switch in TalkInit:

switch (pev->body % 3)

This should be

switch (pev->body % NUM_SCIENTIST_HEADS)

Patch