Wayback Machine
APR SEP Oct
Previous capture 15 Next capture
2007 2008 2009
5 captures
8 Aug 07 - 15 Sep 08
sparklines
Close Help


 

Varlock GameRules Pack Tutorial 5 of 4: Last Man Standing

 

Source
This is the fifth tutorial in what I thought would be a four-part series. I've decided to add the Last Man Standing gamerule set as a favor for Defcon from the #wavelength IRC channel. You do NOT need any of the other GRPack tutorials to add this code to your mod.

I'll start with a bit of an overview on what Last Man Standing is. The point of this gamerule set is to survive the longest. When you die, you enter spectator mode until everyone is dead except one person. That person wins the round and everyone is respawned.

We'll put most of the code in the gamerules' PlayerKilled and ClientDisconnected function. After all, the only time anyone can ever "win" the gamerules is if someone dies or disconnects. For example, if there's five people playing in a game of last man standing and three are already dead, then if one of the two surviving players either dies or disconnects, the round should be over. We only need to check the conditions for winning then, if one of those two scenarios occur (killed or disconnected).

Open lms.h. Make sure you include it into the project by right-clicking anywhere in the file and choosing "Insert File into Project". The new class is VERY small; we're only going to override two functions and add two new ones.

Take a look at lms.cpp, and be sure to add it to your project as well. The function CheckLMS goes through all the players, and if there's more than one connected and only one or less alive, it resets the round. In PlayerKilled, you'll need to add some code to set the player in spectator mode. If you don't already have a way to do this, I recommend the official spectator tutorial by Robin Walker.

There's only one more change to do; open gamerules.cpp and add an #include directive for "lms.h" and find InstallGameRules somewhere around line 315. Add this code into the function right after the SERVER_EXECUTE( ); command,

const char *szMapName = ( STRING( gpGlobals->mapname ) );
	
if ( strncmp( szMapName, "lms_", 4 ) == 0 )
	return new CRulesLMS( );
You should know that there is a possibility of the game getting stuck in an unending round, even when there's only one or fewer people alive. IIRC, occasionally when a player quits Half-Life unexpectedly (due to a crash or something else), the ClientDisconnected function is not called. Therefore, the CheckLMS function wouldn't be called. You could get the game to call CheckLMS approximately every ten seconds, just in case. I'm sure there are many ways to accomplish this.