game_core.cpp

Go to the documentation of this file.
00001 /* $Id$ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
00008  */
00009 
00012 #include "../stdafx.h"
00013 #include "../command_func.h"
00014 #include "../core/backup_type.hpp"
00015 #include "../core/bitmath_func.hpp"
00016 #include "../company_base.h"
00017 #include "../company_func.h"
00018 #include "../network/network.h"
00019 #include "../window_func.h"
00020 #include "../fileio_func.h"
00021 #include "game.hpp"
00022 #include "game_scanner.hpp"
00023 #include "game_config.hpp"
00024 #include "game_instance.hpp"
00025 #include "game_info.hpp"
00026 
00027 /* static */ uint Game::frame_counter = 0;
00028 /* static */ GameInfo *Game::info = NULL;
00029 /* static */ GameInstance *Game::instance = NULL;
00030 /* static */ GameScannerInfo *Game::scanner_info = NULL;
00031 /* static */ GameScannerLibrary *Game::scanner_library = NULL;
00032 
00033 /* static */ const char *Game::GetMainScript()
00034 {
00035     return Game::info->GetMainScript();
00036 }
00037 
00038 /* static */ void Game::GameLoop()
00039 {
00040   if (_networking && !_network_server) return;
00041   if (Game::instance == NULL) return;
00042 
00043   Game::frame_counter++;
00044 
00045   Backup<CompanyByte> cur_company(_current_company, FILE_LINE);
00046   cur_company.Change(OWNER_DEITY);
00047   Game::instance->GameLoop();
00048   cur_company.Restore();
00049 
00050   /* Occasionally collect garbage */
00051   if ((Game::frame_counter & 255) == 0) {
00052     Game::instance->CollectGarbage();
00053   }
00054 }
00055 
00056 /* static */ void Game::Initialize()
00057 {
00058   if (Game::instance != NULL) Game::Uninitialize(true);
00059 
00060   Game::frame_counter = 0;
00061 
00062   if (Game::scanner_info == NULL) {
00063     TarScanner::DoScan(TarScanner::GAME);
00064     Game::scanner_info = new GameScannerInfo();
00065     Game::scanner_info->Initialize();
00066     Game::scanner_library = new GameScannerLibrary();
00067     Game::scanner_library->Initialize();
00068   }
00069 }
00070 
00071 /* static */ void Game::StartNew()
00072 {
00073   if (Game::instance != NULL) return;
00074 
00075   /* Clients shouldn't start GameScripts */
00076   if (_networking && !_network_server) return;
00077 
00078   GameConfig *config = GameConfig::GetConfig();
00079   GameInfo *info = config->GetInfo();
00080   if (info == NULL) return;
00081 
00082   Backup<CompanyByte> cur_company(_current_company, FILE_LINE);
00083   cur_company.Change(OWNER_DEITY);
00084 
00085   Game::info = info;
00086   Game::instance = new GameInstance();
00087   Game::instance->Initialize(info);
00088 
00089   cur_company.Restore();
00090 
00091   InvalidateWindowData(WC_AI_DEBUG, 0, -1);
00092 }
00093 
00094 /* static */ void Game::Uninitialize(bool keepConfig)
00095 {
00096   Backup<CompanyByte> cur_company(_current_company, FILE_LINE);
00097 
00098   delete Game::instance;
00099   Game::instance = NULL;
00100 
00101   cur_company.Restore();
00102 
00103   if (keepConfig) {
00104     Rescan();
00105   } else {
00106     delete Game::scanner_info;
00107     delete Game::scanner_library;
00108     Game::scanner_info = NULL;
00109     Game::scanner_library = NULL;
00110 
00111     if (_settings_game.game_config != NULL) {
00112       delete _settings_game.game_config;
00113       _settings_game.game_config = NULL;
00114     }
00115     if (_settings_newgame.game_config != NULL) {
00116       delete _settings_newgame.game_config;
00117       _settings_newgame.game_config = NULL;
00118     }
00119   }
00120 }
00121 
00122 /* static */ void Game::NewEvent(ScriptEvent *event)
00123 {
00124   /* AddRef() and Release() need to be called at least once, so do it here */
00125   event->AddRef();
00126 
00127   /* Clients should ignore events */
00128   if (_networking && !_network_server) {
00129     event->Release();
00130     return;
00131   }
00132 
00133   /* Check if Game instance is alive */
00134   if (Game::instance == NULL) {
00135     event->Release();
00136     return;
00137   }
00138 
00139   /* Queue the event */
00140   Backup<CompanyByte> cur_company(_current_company, OWNER_DEITY, FILE_LINE);
00141   Game::instance->InsertEvent(event);
00142   cur_company.Restore();
00143 
00144   event->Release();
00145 }
00146 
00147 /* static */ void Game::ResetConfig()
00148 {
00149   /* Check for both newgame as current game if we can reload the GameInfo insde
00150    *  the GameConfig. If not, remove the Game from the list. */
00151   if (_settings_game.game_config != NULL && _settings_game.game_config->HasScript()) {
00152     if (!_settings_game.game_config->ResetInfo(true)) {
00153       DEBUG(script, 0, "After a reload, the GameScript by the name '%s' was no longer found, and removed from the list.", _settings_game.game_config->GetName());
00154       _settings_game.game_config->Change(NULL);
00155       if (Game::instance != NULL) {
00156         delete Game::instance;
00157         Game::instance = NULL;
00158       }
00159     } else if (Game::instance != NULL) {
00160       Game::info = _settings_game.game_config->GetInfo();
00161     }
00162   }
00163   if (_settings_newgame.game_config != NULL && _settings_newgame.game_config->HasScript()) {
00164     if (!_settings_newgame.game_config->ResetInfo(false)) {
00165       DEBUG(script, 0, "After a reload, the GameScript by the name '%s' was no longer found, and removed from the list.", _settings_newgame.game_config->GetName());
00166       _settings_newgame.game_config->Change(NULL);
00167     }
00168   }
00169 }
00170 
00171 /* static */ void Game::Rescan()
00172 {
00173   TarScanner::DoScan(TarScanner::GAME);
00174 
00175   Game::scanner_info->RescanDir();
00176   Game::scanner_library->RescanDir();
00177   ResetConfig();
00178 
00179   InvalidateWindowData(WC_AI_LIST, 0, 1);
00180   SetWindowClassesDirty(WC_AI_DEBUG);
00181   SetWindowDirty(WC_AI_SETTINGS, 0);
00182 }
00183 
00184 
00185 /* static */ void Game::Save()
00186 {
00187   if (Game::instance != NULL && (!_networking || _network_server)) {
00188     Backup<CompanyByte> cur_company(_current_company, OWNER_DEITY, FILE_LINE);
00189     Game::instance->Save();
00190     cur_company.Restore();
00191   } else {
00192     GameInstance::SaveEmpty();
00193   }
00194 }
00195 
00196 /* static */ void Game::Load(int version)
00197 {
00198   if (Game::instance != NULL && (!_networking || _network_server)) {
00199     Backup<CompanyByte> cur_company(_current_company, OWNER_DEITY, FILE_LINE);
00200     Game::instance->Load(version);
00201     cur_company.Restore();
00202   } else {
00203     /* Read, but ignore, the load data */
00204     GameInstance::LoadEmpty();
00205   }
00206 }
00207 
00208 /* static */ char *Game::GetConsoleList(char *p, const char *last, bool newest_only)
00209 {
00210   return Game::scanner_info->GetConsoleList(p, last, newest_only);
00211 }
00212 
00213 /* static */ char *Game::GetConsoleLibraryList(char *p, const char *last)
00214 {
00215    return Game::scanner_library->GetConsoleList(p, last, true);
00216 }
00217 
00218 /* static */ const ScriptInfoList *Game::GetInfoList()
00219 {
00220   return Game::scanner_info->GetInfoList();
00221 }
00222 
00223 /* static */ const ScriptInfoList *Game::GetUniqueInfoList()
00224 {
00225   return Game::scanner_info->GetUniqueInfoList();
00226 }
00227 
00228 /* static */ GameInfo *Game::FindInfo(const char *name, int version, bool force_exact_match)
00229 {
00230   return Game::scanner_info->FindInfo(name, version, force_exact_match);
00231 }
00232 
00233 /* static */ GameLibrary *Game::FindLibrary(const char *library, int version)
00234 {
00235   return Game::scanner_library->FindLibrary(library, version);
00236 }
00237 
00238 #if defined(ENABLE_NETWORK)
00239 
00246 /* static */ bool Game::HasGame(const ContentInfo *ci, bool md5sum)
00247 {
00248   return Game::scanner_info->HasScript(ci, md5sum);
00249 }
00250 
00251 /* static */ bool Game::HasGameLibrary(const ContentInfo *ci, bool md5sum)
00252 {
00253   return Game::scanner_library->HasScript(ci, md5sum);
00254 }
00255 
00256 #endif /* defined(ENABLE_NETWORK) */