Named Game of the Year by over 50 publications, Valve's début title blends action and adventure with award-winning technology to create a frighteningly realistic world where players must think to survive. Also includes an exciting multiplayer mode that allows you to play against friends and enemies around the world.

Post tutorial Report RSS How to add a Christmas Easter Egg! (or any date!)

New to coding and ever wondered how TFC did the Christmas easter egg? Fear nevermore!

Posted by on - Basic Server Side Coding

NOTE:

This will NOT teach you how to code in C++. This is just showing one example of figuring out how to do one thing. Learn all the basics of C++ here, it's helpful and free.

Welcome, this is my first tutorial. I don't know how to make tutorials, so this will be a simple explanation. Also do note, the code could probably be improved, I am not an expert. I may get some terms wrong and I may have some sort of broken code. PM Me if you know where I messed up! Thank you!

I'll try my best to run down each step and part and make it have some sort of sense when done. From time to time, you can click terms, this is so you can understand what the term means in the context of C++! This is assuming you just started.

Let's get down to business. The easter egg in TFC checks the system time on the server. So if someone who is hosting the server and has the date wrong, this won't work. However if you're hosting your own, just set the date.

Time is checked by a C++ header called time.h! It is a header that is pretty standard for different time functions. You can do anything with time, pretty much. Count, subtract, compare, etc. Check out some examples of it's usage.

Well, where should we put the Easter egg and into what context? Well, for today, let's turn Barney into Santa! You'll need to make a Santa Model with Barney's animations, but I you can change Barney into Otis or something. (Just rename Otis.mdl into barney_santa.mdl to test.)

The file you're looking for is in the server side called barney.cpp.

This file contains all the logic for Barney (that isn't elsewhere in the code). It'll seem complicated since it's AI programming (and I still haven't figured it out too well either.). Don't worry, we're just adding in code, very simple code at that.

At the top, you'll see a bunch of #includes. They are including different functions from different classes and libraries.

Under #include "soundent.h" put in

#include  <time.h>

You are including time.h part of the C++ standard library, which holds different functions for different system things.

Now, after including you will need to add this line of code somewhere Under CBarney::Spawn() {

LPSYSTEMTIME sysDate;

        sysDate = (LPSYSTEMTIME) malloc(sizeof(SYSTEMTIME));
       GetLocalTime(sysDate);

LPSYSTEMTIME is an object, it handles time. It is defined within the time.h, and used to create a new object in our code.

Now, where it says "SET_MODEL(ENT(pev), "models/barney.mdl");", we are gonna replace it with:

if (sysDate->wMonth == 12 &amp;&amp; sysDate->wDay == 25 ) {
                SET_MODEL(ENT(pev), "models/barney_santa.mdl");
        }
        else {
                SET_MODEL(ENT(pev), "models/barney.mdl");
        }

sysDate->wMonth and sysDate->wDay are variables within the LPSYSTEMTIME object that is filled in with the GetLocalTime function. The variable is only filled when you call the GetLocalTime() function on sysDate. It isn't filled out automatically at runtime.

In Half-Life there are lots of classes like this. For example, HL has one called "gpGlobals" which holds the "time" variable. The "gpGlobals->time" is what a lot of weapons and timers use to measure time in goldsrc. So you'll notice in the shotgun code for "m_flNextPrimaryAttack = gpGlobals->time + 0.75;". It is .75% of a second later. Change it to say 0.01, it will shoot fast, change it to 9, it will take 9 seconds to shoot again.

Okay after that explanation, we need to precache the model.

Under CBarney::Precache() { put in:

	PRECACHE_MODEL("models/barney_santa.mdl");

Why? What the goldsrc engine does before you go into a match or map is precache every asset used in the map context. So you're about to go into black mesa, it will precache all the sirens, scientists, textures and such. It's so you load what you need and your ram isn't completely filled with every single asset in the game (take notes, studios when you port to PC!)

Ok! You're set. Compile and test. You'll have to change your date to reflect the if statement that you made.

Cheers! I hope this helped you make a bit of understanding on how to use classes and figure out a concept that makes a bit more sense to do. If something is too vague. Let me know! More tutorials may come soon.

If you have any questions, PM me!

Here's the example code for today (taken from the old 2.3 SDK! Not the newest one from git!): Pastebin.com

Post comment Comments
vhetutor
vhetutor - - 250 comments

Pretty cool tutorial man! Easy to follow if you have some knowledge in programming, and you explained what everything was as well! Good job man!

Reply Good karma Bad karma+1 vote
shepard62fr
shepard62fr - - 491 comments

Nice tutorial, the only drawback is that this code will compile on Windows only, not a big deal if you don't care about Linux/Mac, but if you care, here's a quick version I made in 5 minutes : Pastebin.com

Reply Good karma Bad karma+1 vote
H.
H. - - 36 comments

Nice one. I'm going to use it for my coop mod. Thanks for the share.

Reply Good karma Bad karma+1 vote
Guest
Guest - - 690,498 comments

This comment is currently awaiting admin approval, join now to view.

Post a comment

Your comment will be anonymous unless you join the community. Or sign in with your social account: