World::instance()->save();
try { World::instance()->save(); } catch(wpException e) { Console::instance()->log(LOG_PYTHON, e.error() + "\n"); }
int main( int argc, char **argv ){/*#if defined(_DEBUG)	InstallCrashHandler( HANDLE_HTTPREPORT, GSTSO_PARAMS | GSTSO_MODULE | GSTSO_SYMBOL | GSTSO_SRCLINE );	SetCustomMessage("A crash occurred. Please send this bug report to developers\n");	SetVersion(wp_version.verstring.c_str());#endif*/	Getopts::instance()->parse_options( argc, argv );	unsigned int i;	keeprun = 1;	QApplication app( argc, argv, false ); // we need one instance	QTranslator translator( 0 ); // must be valid thru app life.	changeServerState(STARTUP);	Console::instance()->setAttributes( true, false, true, 60, 140, 70, 12, FONT_NOSERIF );	Console::instance()->send(QString( "\n%1 %2 %3\n\n" ).arg(productString(), productBeta(), productVersion()));	Console::instance()->setAttributes( false, false, false, 0xAF, 0xAF, 0xAF, 0, FONT_FIXEDWIDTH );	Console::instance()->send("Copyright (C) 2000-2004 Wolfpack Development Team\n");	Console::instance()->send("Wolfpack Homepage: http://www.wpdev.org/\n");	Console::instance()->send("By using this software you agree to the license accompanying this release.\n");	Console::instance()->send("Compiled on " __DATE__ " " __TIME__ "\n");	Console::instance()->send("Compiled for QT " QT_VERSION_STR " (Using: ");	Console::instance()->send(qVersion());	Console::instance()->send(qSharedBuild() ? " Shared" : " Static");	Console::instance()->send(")\n");	QString pythonBuild = Py_GetVersion();	pythonBuild = pythonBuild.left(pythonBuild.find(' '));#if defined(Py_ENABLE_SHARED)	pythonBuild += " Shared";#else	pythonBuild += " Static";#endif	Console::instance()->send("Compiled for Python " PY_VERSION " (Using: ");	Console::instance()->send(pythonBuild + ")\n\n");	QString consoleTitle = QString("%1 %2 %3").arg(productString(), productBeta(), productVersion());	Console::instance()->setConsoleTitle( consoleTitle );	// Startup normal Classes	try	{		atexit( freeClasses );		startClasses();	}	catch( ... )	{		Console::instance()->log( LOG_ERROR, "Couldn't start up classes.\n" );		return 1;	}	// Startup Translator	QString languageFile = SrvParams->getString( "General", "Language File", "", true );	if ( !languageFile.isEmpty() )	{		if ( !translator.load( languageFile, "." ) )		{			Console::instance()->log( LOG_ERROR, "Couldn't load translator.\n" );			return 1;		}		qApp->installTranslator( &translator );	}	// Try to start up python	try	{		startPython( argc, argv );	}	catch( ... )	{		Console::instance()->log( LOG_ERROR, "Couldn't start up python.\n" );		return 1;	}	for( i = 1; i <= argc; ++i )		if( !parseParameter( QString( argv[ i ] ) ) )			return 1;	// Load data	DefManager->load();	Console::instance()->send( "\n" );/*	Console::instance()->send( "SIZEOF(cItem):" + QString::number( sizeof( cItem ) ) );	Sleep( 5000 );	for( int x = 0; x < 400000; ++x )	{		cItem *it = cItem::createFromScript( "e75" );	}	return 1;*/	// Scriptmanager can't be in the try{} block because it sometimes throws firstchance exceptions	// we don't like	ScriptManager::instance()->load();	Console::instance()->send( "\n" );	// Try to load several data files#if !defined(_DEBUG)	try	{#endif		// Load some MUL Data		Console::instance()->send( "Loading muls...\n" );		TileCache::instance()->load( SrvParams->mulPath() );		MultiCache::instance()->load( SrvParams->mulPath() );		Map->registerMap(0, "map0.mul", 768, 512, "statics0.mul", "staidx0.mul");		Map->registerMap(1, "map0.mul", 768, 512, "statics0.mul", "staidx0.mul");		Map->registerMap(2, "map2.mul", 288, 200, "statics2.mul", "staidx2.mul");		Map->registerMap(3, "map3.mul", 320, 256, "statics3.mul", "staidx3.mul");		// For each map we register, register a GridMap as well		MapObjects::instance()->addMap( 0, 6144, 4096 );		MapObjects::instance()->addMap( 1, 6144, 4096 );		MapObjects::instance()->addMap( 2, 2304, 1600 );		MapObjects::instance()->addMap( 3, 2560, 2048 );		Console::instance()->send( "Loading skills...\n" );		Skills->load();		Console::instance()->send( "Loading accounts...\n" );		Accounts::instance()->load();		Console::instance()->send( "Loading ip blocking rules...\n" );		cNetwork::instance()->load();		Console::instance()->send( "Loading regions...\n" );		AllTerritories::instance()->load();		Console::instance()->send( "Loading spawn regions...\n" );		SpawnRegions::instance()->load();		Console::instance()->send( "Loading resources...\n" );		Resources::instance()->load();		Console::instance()->send( "Loading makemenus...\n" );		MakeMenus::instance()->load();		Console::instance()->send( "Loading contextmenus...\n" );		ContextMenus::instance()->reload();		Console::instance()->send( "\n" );#if !defined(_DEBUG)	}	catch( wpException &exception )	{		Console::instance()->log( LOG_ERROR, exception.error() );		return 1;	}	catch( ... )	{		Console::instance()->log( LOG_ERROR, "Unknown error while loading data files.\n" );		return 1;	}#endif	SetGlobalVars();	// initial randomization call	srand( uiCurrentTime );	/*		Check for valid database driers.	*/	if( !persistentBroker->openDriver( SrvParams->databaseDriver() ) )	{		Console::instance()->log( LOG_ERROR, QString( "Unknown Worldsave Database Driver '%1', check your wolfpack.xml").arg( SrvParams->databaseDriver() ) );		return 1;	}	if( !persistentBroker->openDriver( SrvParams->accountsDriver() ) )	{		Console::instance()->log( LOG_ERROR, QString( "Unknown Account Database Driver '%1', check your wolfpack.xml").arg( SrvParams->accountsDriver() ) );		return 1;	}	// Registers our Built-in types into factory.	cPlayer::registerInFactory();	cMulti::registerInFactory();	cNPC::registerInFactory();	cItem::registerInFactory();	cCorpse::registerInFactory();	cResourceItem::registerInFactory();	// NPC AI types	Monster_Aggressive_L0::registerInFactory();	Monster_Aggressive_L1::registerInFactory();	Monster_Berserk::registerInFactory();	Human_Vendor::registerInFactory();	Human_Stablemaster::registerInFactory();	Human_Guard::registerInFactory();	Human_Guard_Called::registerInFactory();	Normal_Base::registerInFactory();	Animal_Wild::registerInFactory();	Animal_Domestic::registerInFactory();	// Script NPC AI types	QStringList aiSections = DefManager->getSections( WPDT_AI );	QStringList::const_iterator aiit = aiSections.begin();	while( aiit != aiSections.end() )	{		ScriptAI::registerInFactory( *aiit );		++aiit;	}#if !defined(_DEBUG)	try	{#endif		World::instance()->load();#if !defined(_DEBUG)	}	catch( QString &error )	{		Console::instance()->log( LOG_ERROR, error );		return 1;	}	catch( ... )	{		Console::instance()->log( LOG_ERROR, "An unknown error occured while loading the world.\n" );		return 1;	}#endif	uiCurrentTime = getNormalizedTime();	// network startup	cNetwork::startup();	if( SrvParams->enableLogin() )	{        Console::instance()->send( QString( "LoginServer running on port %1\n" ).arg( SrvParams->loginPort() ) );		if ( SrvParams->serverList().size() < 1 )			Console::instance()->log( LOG_WARNING, "LoginServer enabled but there no Game server entries found\n Check your wolfpack.xml settings" );	}	if( SrvParams->enableGame() )        Console::instance()->send( QString( "GameServer running on port %1\n" ).arg( SrvParams->gamePort() ) );	PyThreadState *_save;	ScriptManager::instance()->onServerStart();	changeServerState(RUNNING);	Console::instance()->start(); // Startup Console	QWaitCondition niceLevel;	unsigned char cycles = 0;	// This is our main loop	while (keeprun) {		++cycles;		// Update our currenttime		uiCurrentTime = getNormalizedTime();		// Every 10th cycle we sleep for a while and give other threads processing time.		if (cycles == 10) {			cycles = 0;			// Python threading - start			_save = PyEval_SaveThread();			switch( SrvParams->niceLevel() )			{				case 0: break;	// very unnice - hog all cpu time				case 1: if ( cNetwork::instance()->count() != 0) niceLevel.wait(10); else niceLevel.wait(100); break;				case 2: niceLevel.wait(10); break;				case 3: niceLevel.wait(40); break;// very nice				case 4: if ( cNetwork::instance()->count() != 0 ) niceLevel.wait(10); else niceLevel.wait(4000); break; // anti busy waiting				case 5: if ( cNetwork::instance()->count() != 0 ) niceLevel.wait(40); else niceLevel.wait(5000); break;				default: niceLevel.wait(10); break;			}			qApp->processEvents( 40 );			// Python threading - end			PyEval_RestoreThread( _save );			// Unlock the main mutex to give the gui time for processing data		}      		// Perform Threadsafe Actions		if (!actionQueue.empty()) {			actionMutex.lock();			while (actionQueue.begin() != actionQueue.end()) {				eActionType type = *(actionQueue.begin());				actionQueue.erase( actionQueue.begin() );				switch( type )				{				case RELOAD_ACCOUNTS:					Console::instance()->sendProgress( "Reloading Accounts" );					lockDataMutex();					Accounts::instance()->reload();					unlockDataMutex();					Console::instance()->sendDone();					break;				case RELOAD_CONFIGURATION:					Console::instance()->sendProgress( "Reloading Configuration" );					lockDataMutex();					SrvParams->reload();					unlockDataMutex();					Console::instance()->sendDone();					break;				case RELOAD_SCRIPTS:					DefManager->reload();				case RELOAD_PYTHON:					ScriptManager::instance()->reload();					break;				case SAVE_WORLD:					lockDataMutex();					World::instance()->save();					unlockDataMutex();					break;				}			}			actionMutex.unlock();		}		// See if we should release our data lock for a while.		Console::instance()->poll();		lockDataMutex();		cNetwork::instance()->poll();		Timing::instance()->poll();		unlockDataMutex();	}	changeServerState(SHUTDOWN);	ScriptManager::instance()->onServerStop();	cNetwork::instance()->broadcast( tr( "The server is shutting down." ) );	cNetwork::shutdown();	SrvParams->flush(); // Save config options	// Simply emptying of containers, no progressbar needed	DefManager->unload();	// Stop Python Interpreter.	ScriptManager::instance()->unload();	stopPython();	Console::instance()->stop(); // Stop the Console	return 0;}