Game Development Community

dev|Pro Game Development Curriculum

Game Manager with Time and Weather

by Bil Simser · 07/23/2004 (9:42 am) · 89 comments

I wanted to build a nice, pluggable time and weather system into Torque for would-be RPG creators to use. I didn't see anything that I really liked already so I built this one.

NOTE: The resource link is at the end of this article due to size restrictions when uploading to GarageGames so it's hosted offline.

www.bilsimser.org/images/weather2.jpg
Features:
* configurable game time system that handles time and weather advancement (you can use it for any game management you want like bots, spell recharge times, whatever but this mod just gives you time and weather)
* completely scripted system and easy to modify, extend and add to
* you determine how fast (or slow) time advances (real time, 1 game time hour = 75 seconds, whatever)
* weather system tracks atmospheric pressure and changes accordingly (based on values and the time of year)
* time system is streamed out to the file system on the server and can be reloaded to keep a pseudo persistant world going (at least as far as keeping track of time goes)
* weather system initiates clouds, rain, fog, lightning and cleans up when the cycle stops (pressure builds and declines so it's not constant)
* time and weather system can be queried by user to get information (yes, they can look up into the sky but someone people like to know what's going on through messages).
* configurable time system uses whatever calendar you want (real, fantasy, etc.) with day and month names to give your users a rich experience.

www.bilsimser.org/images/weather.jpg
QuickStart
So you want to get up and running with this. Easy! Here's the instructions to try out the no-hassle install:

1. Unzip the resource into your game directory. Any game will do (and this will work with an out-of-the-box starter.fps)
2. Go into the file server/game.cs and in the onServerCreated() function, add the following line:
exec("./gameMgr.cs");
3. In the same file add the following code to the startGame() function:
// Start the GameManager
new ScriptObject(GameManager);
MissionCleanup.add(GameManager);
GameManager.init();
4. Add the following line to the endGame() function:
// Stop the GameManager
GameManager.delete();
5. Launch the game and wait.

Time and weather changes will start. There are two hotkeys setup for you.
F5 will display the current time and date
F6 will display the current weather conditions

The Mod
The mod is made up of the following files and their locations:
server/scripts/gameMgr.cs - The Game Manager class
server/scripts/time.cs - Time Manager class
server/scripts/weather.cs - Weather Manager class
data/environment/* - rain and lightning graphic files
data/sounds/environment/* - rain and lightning sound files

How it Works
The GameManager class runs everything. You call the ::init method from your own game.cs file to kick it off. It will then initialize any game managers you create and start its own ::update schedule.

The ::update method just calls the same method for each manager. In the same, the TimeManager.update method handles advancing the game calendar while the WeatherManager.update will change the conditions in the game, creating rain, lightning and fog when appropriate.

The GameManager.update method runs every second and tracks a variable called pulse. The pulse variable keeps track of our game clock ticks and is used to determine when a game event fires. You can have as many game events as you like (time, weather, hit point regen, etc.)
and have them fire at different periods. Pulse is updated once per pass through the update method so you just set the frequency of how often your own event handler fires. Think of pulse as a counter.

There's also a global variable called $GameManagerPrefs::SecsPerGameHour. This is the number of real seconds that pass in one game hour and determines when a "game" hour has passed. You can use it to fire events on an hourly basis (e.g. a player needs 1 hour to recharge a spell). The time and weather system update every game hour with this:
if((%this.Pulse % %this.SecsPerGameHour) == 0)
{
    TimeManager.update();
    WeatherManager.update();
}

In the update method you can use the Pulse variable to fire an event every 30 seconds (using the Modulus operator) with this call:
if((%this.Pulse % 30) == 0)
{
    // Do something every 30 seconds real-time
}

So even though the TimeManager.update is happening every second, you don't have to do something every second (which might be taxing on your system). Just try to keep your events short and not too CPU intensive.

Tweaking the System
At the top of each file there are some global variables that can be tweaked. In the TimeManager for example, the number of days in a week, months in a year, etc. as well as the day and month names can be changed to suit your game. Just change whatever you want there and the rest will be updated.

Global Key bindings
The sample mod contains two global keybinding, F5 and F6 (which were unused so I chose them) to call the methods to display the time and weather information to to user. You can move these or rebind them to other keys as you see fit.

Utility Functions
I've added a min and max function which will return the minimum or maximum values of two numbers. I think every game needs this. There's also a handy function called dice(). Pass it the number of dice you want to roll and how many sides each die has and it will come back with a total. If you're from the old AD&D world, this is your typical 2d6, 4d8 and 3d10 rolls. Hope you find it handy.

Notes and Enhancements
A lot of extendability here but it's a fully working time keeper and weather system for someone to use in a single player or multiplayer game. For example you could build a calendar gui component to display on the screen and have it update as time advances in your game. Whatever your imagine takes you I guess.

The weather is driven by atmospheric pressure values, the current sky conditions and some random chance. It's all in one script function but parts of it could be externalized or become data driven. It's pretty basic so nothing really magical here, just easy to use.

I haven't plugged in a real day/night system as that involves engine changes and wanted to keep this as a scripting mod only so you're welcome to do that. The system keeps track of the hour of the game time day so you can still use it (like only allowing certain things to happen at night or the automatic opening and closing of stores during business hours).

With the system running, you can do some cool stuff like display the game time on the screen for players to see. As I mentioned, you can also query the TimeManager system about what the hour of the day is and do things like restrict access to areas, transform those infected with the lycanthrope disease when the moon is out, or otherwise just mess with your players lives when the sun rises and there's no coffin around to sleep in. The choices are yours.

This is, overall, pretty basic but it's a start. Read the source as there are many comments about how to expand or extend this. If you do create something better or add onto it, please let me know and I'll update this for everyone to share.

TorqueScript isn't OO but I did try to make it somewhat OO'ish. Personally I think there's too many global variables and maybe too many ScriptObjects being created so that could be improved. As well, perhaps putting the Managers into an array or list would be better. In any case, if you do improve on it or find a bug share it.

You can also find the resource here:
www.bilsimser.org/download/gamemgr.zip

Enjoy!
#41
04/10/2005 (9:22 am)
well, I've actually come a long way since that post, I have it all incorporated pretty well and have a perm server up if you query the master :) yes I was a snoob when I made that post but now I'd like to think I'm just a noob :)

Vince
#42
05/29/2005 (7:37 pm)
@Clint
Please tell me you don't have lightning doing damage to players.

Nice work on the updates guys, I really appreciate it.

Ari Rule
"I told you, if you use the "G" word in spatial chat you might wind up doing the respawn shuffle."
#43
05/29/2005 (8:48 pm)
Hey, Tom Bampton:

in your screenshot from long ago, the trees are lit up .. they shouldn't be!
#44
05/31/2005 (3:03 pm)
---------
@Clint
Please tell me you don't have lightning doing damage to players.
---------

I'm sorry BrokeAss Games, I can't tell you that :) in it's current state I do have a small chance to hit and a small ammount of damage with some explosions and ear buzzing. but it happens really really rarely.
#45
06/10/2005 (2:45 am)
@Clint: LOL! I'm inspired!

I wanted the rain to spatter on ALL objects and wound up with this after some reading on GG.

Line 19 of precipitation.cc
static const U32 dropHitMask = TerrainObjectType |
                               InteriorObjectType |
                               WaterObjectType |
                               StaticShapeObjectType |
                               ShapeBaseObjectType |
                               StaticTSObjectType;

The rain spatter on the character's bounding box looks kinda ugly but is barely noticeable.
It looks really good on vehicles like BraveTree's car.
In chase camera mode I can see the rain splashing on my back window.
Also watching rain spatter on barrels and weapons on the ground makes for some great screen shots.
There is a frame rate hit on my poor little Athlon at home.

Ari Rule
#46
07/20/2005 (3:53 pm)
anyone have any problems with torque 1.3? i couldn't get it working with all the comment changes
#47
07/21/2005 (12:51 pm)
d3wu, for what it's worth, I'm using 1.3 and had no problems, other than what I posted above.

Quote: ---------
@Clint
Please tell me you don't have lightning doing damage to players.
---------

I'm sorry BrokeAss Games, I can't tell you that :) in it's current state I do have a small chance to hit and a small ammount of damage with some explosions and ear buzzing. but it happens really really rarely.

btw, this scared the hell out of me the other day. It's such a small chance that it virtually never happens, when it did I just about jumped out of my chair :)
#48
08/30/2005 (11:40 pm)
@Clint LOL, I suggest lots more damage. >:)
The game I'm creating is very "griefer" where fairness has given way to realism.
In a MMORPG environment I can imagine the rumor mill as soon as a player gets hit by lightning.
They'd never be sure if it was an admin or just bad luck! LOL
Btw, I still love and use this resource (TGE 1.3).

Ari Rule
#49
08/31/2005 (12:00 am)
I have a problem when running in multiplayer. Tha rain starts at one of computer, but the others stay as is. What can i do?
#50
09/11/2005 (5:06 pm)
Hey all,

Well this is the first mod I've tried out and was the first time I peaked under the hood of Torque. I'm using Torque 1.3 that I got over the summer, and the Mod installed beautifully!

Before I did anything I made a copy of the m0d-source and implemented the first several bug fixes listed above. Including 2 by Tom, keybindings as suggested and the hack to weather::init() and kept a revision log.

If someone wants to suggest the best way to add an updated resource, I'll post it with the bug-fixes. I hate to add it as a "New" resource though.

Only issue I ran into which is not a mod issue really: when I did the keybindings to F5 and F6 i had to move the particle-generator GUI. It's listed in client/scripts/default.binds.cs under the "non-rebindable" section. So i changed it to f12 anyways and it didn't work. Is there another way to do this?

LAST QUESTION:
Can anyone share advice, wisdom, n00B instructions for linking this to the celstial day-night cycles? I'm trying to figure out how to install that now.


Thanks all,
#51
09/13/2005 (9:12 am)
Clint,

Cool Lighting code, more compact than what i was working on. What does the variable "%cl" actually stand for? And how do you check to see if a player got hit? I'm not intentionally trying to hit players, but if it ACCIDENTLY hits them... >:-)

Thanks,
#52
09/13/2005 (9:23 am)
Thanks RavenSlay3r,
%cl as in:

%cl = ClientGroup.getObject(%i);

%cl is the i'th client object.
Search in the script files fro %client and you'll see how it is used elsewhere. That code just loops through all the clients in turn.

for something to actually get hit, you can use the applydamage function, something like this:

function LightningData::applyDamage(%this, %obj, %hitObject, %pos, %normal)
{
	radiusDamage(%obj, %pos,  %this.damageRadius, %this.damageAmmount,"fire",40);
}
#53
09/13/2005 (10:19 pm)
*** RANDOM STORM INTENSITY ***

Here's a new piece of code that assigns random wind velocity and relative rain intensity everytime startRain() is called. It seems to be working but can someone double check me?

The first die roll should produce: 0 or 1
The second die roll should produce: 0, 1, 2, 3, or 4
I'm assuming the built in absolute value function is: abs()

//////////////////////////////////////////////////
insert into startRain()
//////////////////////////////////////////////////

// Randomly determin intensity of storm.
// First determin wind speed
// Then determin rain intensity (drops per min.):
// - Min. 2000 drops per min.
// - additional 500 drops per min for every point of wind speed.
// - Max. 2000 + (8 * 500) = 6000 drops.

$windX = (dice(1,2) - 1 ? dice(1,5) - 1 : -(dice(1,5) -1 ));
$windY = (dice(1,2) - 1 ? dice(1,5) - 1 : -(dice(1,5) -1 ));

Sky.setWindVelocity($windX, $windY, 0);
$intensity = ((abs($windX) + abs($windY)) * 500) + 2000;


$Rain = new Precipitation()
{
datablock = "HeavyRain";
minSpeed = 2.5;
maxSpeed = 3.0;
numDrops = $intensity;
boxWidth = 200;
boxHeight = 100;
minMass = 1.0;
maxMass = 2.0;
rotateWithCamVel = true;
doCollision = true;
useTurbulence = false;
};


////////////////////////////////////////////////
#54
09/13/2005 (10:25 pm)
Clint,

Thanks. In my revision %cl might get changed to %client or %myClient just because. Going to test the lighting code now. :-D


@all:
Is the complete set of Torque built in functions documented somewhere (ala http://www.php.net/docs.php)??? Or do I just have to sort through the source code?
#55
09/14/2005 (1:49 am)
WIND VELOCITY BUG


Ok i have my mod setup to initilize the weather pattern on startup, as described way-above. For testing purposes I added a line to WeatherManager::init() to force the initial weather-pressure of my choosing.

I was noticeing that for pressure settings of 1010, or 1030 (aka cloudy or clear-skies, respectively) the sky would come up clear and stay that way! No clouds, and when the weather changed there would still be NO CLOUDS.


Even if I called the line:

Sky.stormClouds(0, $time_to_storm_init);

from the command line, it did nothing! And yet if I loaded the previous version of my mission file, in the same folder - everything worked!

It turns out that setting the inital value of: "windVelocity(0 0 0)" under "Sky" in the mission folder was breaking the mod. By using windVelocity(1 1 0) the problem is resolved.

I do not understand this, beyond what i've posted. but hopefully this will save someone a headache in the future ;-)

Good Night all,
#56
09/17/2005 (1:34 pm)
anyone know where i can get this resource?
www.bilsimser.org/download/gamemgr.zip doesnt seem to work
#57
09/19/2005 (6:33 pm)
I just downloaded it and it worked fine for me.
#58
10/10/2005 (2:29 pm)
How are things coming along with your mod?
#59
10/10/2005 (2:45 pm)
Hi guys,

This thread is pretty long so it'll take sometime to put together a new updated version of this but one is on the way. Thanks for all the suggestions and corrections! Here's to a Weather 2.0 mod that will make this one look like a tinkertoy.
#60
10/11/2005 (6:19 am)
Sounds great. I can't wait for it. It would be interesting if you added a temperature/altitude calculations to determine if it would be rain or snow. Or even severity, light rains, heavy rains, hail storm, tornados, hurricanes. If you need any help let me know I would be glad to lend a hand, especially getting this ready for TGE v1.4