Main Function

[ LiB ]

The main function of the BetterMUD is located within the file /BetterMUD/BetterMUD.cpp on the CD, and it's a very simple function, reminiscent of the SimpleMUD's main module:

 #include "SocketLib/SocketLib.h" #include "BetterMUD/network/TelnetLogon.h" #include "BetterMUD/network/BetterTelnet.h" #include "BetterMUD/Game.h" using namespace SocketLib; using namespace BetterMUD; int main() {     try {     ListeningManager<BetterTelnet, TelnetLogon> telnetlistener;     ConnectionManager<BetterTelnet, TelnetLogon>         telnetconnectionmanager( 128, 60, 65536 );     telnetlistener.SetConnectionManager( &telnetconnectionmanager );     telnetlistener.AddPort( 5110 );     g_game.LoadAll();  while( g_game.Running() ) {   telnetlistener.Listen();   telnetconnectionmanager.Manage();   g_game.ExecuteLoop();   ThreadLib::YieldThread();   }  }     catch( BetterMUD::Exception& e ) {         std::cout << e.GetError();     }     g_game.SaveAll();     CharacterDB.Purge();     ItemDB.Purge();     AccountDB.Purge();     RoomDB.Purge();     PortalDB.Purge();     RegionDB.Purge();     return 0; } 

The actual game loop is shown in bold; everything else mainly involves loading and saving the databases. You should be familiar with the networking setup and the game loop, so I'll skip to the last part of the code.

The final section of code saves all the databases and then purges the entity databases. Why do I do this? It turns out that since I used globals for all the databases, I have absolutely no control over their order of destruction. As you'll see in Chapter 17, my Python script module objects need to exist whenever an entity is destroyed , so that the entities can remove references from the Python modules.

Unfortunately, I can't make C++ destroy the entity databases first, so sometimes the game crashes when you shut down. Instead of crashing, I purge the databases to force the databases to destroy all their entities, so that by the time the global Python script databases are destroyed, nothing points to them anymore.

[ LiB ]


MUD Game Programming
MUD Game Programming (Premier Press Game Development)
ISBN: 1592000908
EAN: 2147483647
Year: 2003
Pages: 147
Authors: Ron Penton

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