Dedicated Server


Sometimes we will want to host a game as a server without having to bother with a graphical user interface. One reason we might want to do this is because we want to run the server on a computer that doesn't have a 3D accelerated graphics adapter. Another reason is because we might want to test our client/server connectivity and master server query capabilities. This need arises because we can't run two instances of the Torque graphical client at the same time. However, if we have the ability to run as a dedicated server, we can run multiple dedicated servers, while running one instance of the graphical client, all on the same computer. And if we have set up the dedicated servers appropriately, other players out on the network can connect to our servers.

There are a few more modules you will have to change to implement the dedicated server capabilities.

Root Main Module

In this module we'll need to add some command line switches in case we want to use the command line interface of Windows, or we'll need to we decide to embed the switches in a Windows shortcut. Either of these methods is how we can tell the game to run the server in dedicated mode. In the module main.cs located in the root game folder (which is the folder where the tge.exe executable is located for your Chapter 6 version of Emaga), locate the ParseArgs function, and scroll down until you find the statement containing $switch($currentarg). Type the following code in directly after the $switch statement:

       case "-dedicated":         $Server::Dedicated = true;         EnableWinConsole(true);         $argumentFlag[$i]++;       case "-map":         $argumentFlag[$i]++;         if ($nextArgExists)         {            $mapArgument = $nextArgument;            $argumentFlag[$i+1]++;            $i++;         }         else            Error("Error: Missing argument. Usage: -mission <filename>"); 

Both of these switches are needed to run a dedicated server. The -dedicated switch puts us into the right mode, and then the -map switch tells us which mission map to load when the server first starts running.

The result of these changes is that we can now invoke the dedicated server mode by launching the game with the following syntax from the command line (don't try it yet): tge.exe -dedicated -map control/data/maps/book_ch6.mis.

The game will launch, and all you will see will be a console window. You will be able to type in console script statements, just as you can when you use the tilde ("~") key in the graphical client interface. However, don't try this just yet, because we still need to add the actual dedicated server code!

You can also create a shortcut to the tge.exe executable and modify the Target box in the shortcut properties to match the command line syntax above. Then you can launch the server merely by double-clicking on the shortcut icon.

Control—Main Module

Next, we have a quick modification to make to control/main.cs. In the OnStart function, locate the line that contains InitializeClient. Replace that one line with these four lines:

    if ($Server::Dedicated)       InitializeDedicatedServer();    else InitializeClient(); 

Now, when the program detects that the -dedicated switch was used, as described in the previous section, it will fire up in dedicated mode, not in client mode.

Control—Initialize Module

Okay, the meat of the dedicated server code is contained in this module. Open up the module control/server/initialize.cs and type in the following lines just before the InitializeServer function.

 $pref::Master0 = "2:master.garagegames.com:28002"; $Pref::Server::ConnectionError = "You do not have the correct version of 3DGPAI1 client or the related art needed to play on this server. This is the server for Chapter 6. Please check that chapter for directions."; $Pref::Server::FloodProtectionEnabled = 1; $Pref::Server::Info = "3D Game Programming All-In-One by Kenneth C. Finney."; $Pref::Server::MaxPlayers = 64; $Pref::Server::Name = "3DGPAI1 Book - Chapter 6 Server"; $Pref::Server::Password = ""; $Pref::Server::Port = 28000; $Pref::Server::RegionMask = 2; $Pref::Server::TimeLimit = 20; $Pref::Net::LagThreshold = "400"; $pref::Net::PacketRateToClient = "10"; $pref::Net::PacketRateToServer = "32"; $pref::Net::PacketSize = "200"; $pref::Net::Port = 28000; 

You can change the string values to be anything you like as long as it suits your purposes. You should leave the RegionMask as is for now.

Next, locate the function InitializeServer again, and insert the following lines at the very beginning of the function:

 $Server::GameType = "3DGPAI1"; $Server::MissionType = "Emaga6"; $Server::Status = "Unknown"; 

This value will be updated when the server makes contact with the master server.

Finally, you will need to add this entire function to the end of the module:

 function InitializeDedicatedServer() {    EnableWinConsole(true);    Echo("\n--------- Starting Dedicated Server ---------");    $Server::Dedicated = true;    if ($mapArgument !$= "") {       CreateServer("MultiPlayer", $mapArgument);    }    else       Echo("No map specified (use -map <filename>)"); } 

This function enables the Windows console, sets the dedicated flag, and then calls CreateServer with the appropriate values. Now it may not do very much and therefore seem to be not too necessary, but the significance with the InitializeDedicatedServer function is in what it doesn't do compared with the InitializeClient function, which would have otherwise been called. So that's the reason why it exists.




3D Game Programming All in One
3D Game Programming All in One (Course Technology PTR Game Development Series)
ISBN: 159200136X
EAN: 2147483647
Year: 2006
Pages: 197

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net