Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00012 #include "../stdafx.h"
00013 #include "../core/backup_type.hpp"
00014 #include "../company_base.h"
00015 #include "../company_func.h"
00016 #include "../network/network.h"
00017 #include "../window_func.h"
00018 #include "game.hpp"
00019 #include "game_scanner.hpp"
00020 #include "game_config.hpp"
00021 #include "game_instance.hpp"
00022 #include "game_info.hpp"
00023
00024 uint Game::frame_counter = 0;
00025 GameInfo *Game::info = NULL;
00026 GameInstance *Game::instance = NULL;
00027 GameScannerInfo *Game::scanner_info = NULL;
00028 GameScannerLibrary *Game::scanner_library = NULL;
00029
00030 const char *Game::GetMainScript()
00031 {
00032 return Game::info->GetMainScript();
00033 }
00034
00035 void Game::GameLoop()
00036 {
00037 if (_networking && !_network_server) return;
00038 if (Game::instance == NULL) return;
00039
00040 Game::frame_counter++;
00041
00042 Backup<CompanyByte> cur_company(_current_company, FILE_LINE);
00043 cur_company.Change(OWNER_DEITY);
00044 Game::instance->GameLoop();
00045 cur_company.Restore();
00046
00047
00048 if ((Game::frame_counter & 255) == 0) {
00049 Game::instance->CollectGarbage();
00050 }
00051 }
00052
00053 void Game::Initialize()
00054 {
00055 if (Game::instance != NULL) Game::Uninitialize(true);
00056
00057 Game::frame_counter = 0;
00058
00059 if (Game::scanner_info == NULL) {
00060 TarScanner::DoScan(TarScanner::GAME);
00061 Game::scanner_info = new GameScannerInfo();
00062 Game::scanner_info->Initialize();
00063 Game::scanner_library = new GameScannerLibrary();
00064 Game::scanner_library->Initialize();
00065 }
00066 }
00067
00068 void Game::StartNew()
00069 {
00070 if (Game::instance != NULL) return;
00071
00072
00073 if (_networking && !_network_server) return;
00074
00075 GameConfig *config = GameConfig::GetConfig(GameConfig::SSS_FORCE_GAME);
00076 GameInfo *info = config->GetInfo();
00077 if (info == NULL) return;
00078
00079 Backup<CompanyByte> cur_company(_current_company, FILE_LINE);
00080 cur_company.Change(OWNER_DEITY);
00081
00082 Game::info = info;
00083 Game::instance = new GameInstance();
00084 Game::instance->Initialize(info);
00085
00086 cur_company.Restore();
00087
00088 InvalidateWindowData(WC_AI_DEBUG, 0, -1);
00089 }
00090
00091 void Game::Uninitialize(bool keepConfig)
00092 {
00093 Backup<CompanyByte> cur_company(_current_company, FILE_LINE);
00094
00095 delete Game::instance;
00096 Game::instance = NULL;
00097 Game::info = NULL;
00098
00099 cur_company.Restore();
00100
00101 if (keepConfig) {
00102 Rescan();
00103 } else {
00104 delete Game::scanner_info;
00105 delete Game::scanner_library;
00106 Game::scanner_info = NULL;
00107 Game::scanner_library = NULL;
00108
00109 if (_settings_game.game_config != NULL) {
00110 delete _settings_game.game_config;
00111 _settings_game.game_config = NULL;
00112 }
00113 if (_settings_newgame.game_config != NULL) {
00114 delete _settings_newgame.game_config;
00115 _settings_newgame.game_config = NULL;
00116 }
00117 }
00118 }
00119
00120 void Game::Pause()
00121 {
00122 if (Game::instance != NULL) Game::instance->Pause();
00123 }
00124
00125 void Game::Unpause()
00126 {
00127 if (Game::instance != NULL) Game::instance->Unpause();
00128 }
00129
00130 bool Game::IsPaused()
00131 {
00132 return Game::instance != NULL? Game::instance->IsPaused() : false;
00133 }
00134
00135 void Game::NewEvent(ScriptEvent *event)
00136 {
00137
00138 event->AddRef();
00139
00140
00141 if (_networking && !_network_server) {
00142 event->Release();
00143 return;
00144 }
00145
00146
00147 if (Game::instance == NULL) {
00148 event->Release();
00149 return;
00150 }
00151
00152
00153 Backup<CompanyByte> cur_company(_current_company, OWNER_DEITY, FILE_LINE);
00154 Game::instance->InsertEvent(event);
00155 cur_company.Restore();
00156
00157 event->Release();
00158 }
00159
00160 void Game::ResetConfig()
00161 {
00162
00163
00164 if (_settings_game.game_config != NULL && _settings_game.game_config->HasScript()) {
00165 if (!_settings_game.game_config->ResetInfo(true)) {
00166 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());
00167 _settings_game.game_config->Change(NULL);
00168 if (Game::instance != NULL) {
00169 delete Game::instance;
00170 Game::instance = NULL;
00171 Game::info = NULL;
00172 }
00173 } else if (Game::instance != NULL) {
00174 Game::info = _settings_game.game_config->GetInfo();
00175 }
00176 }
00177 if (_settings_newgame.game_config != NULL && _settings_newgame.game_config->HasScript()) {
00178 if (!_settings_newgame.game_config->ResetInfo(false)) {
00179 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());
00180 _settings_newgame.game_config->Change(NULL);
00181 }
00182 }
00183 }
00184
00185 void Game::Rescan()
00186 {
00187 TarScanner::DoScan(TarScanner::GAME);
00188
00189 Game::scanner_info->RescanDir();
00190 Game::scanner_library->RescanDir();
00191 ResetConfig();
00192
00193 InvalidateWindowData(WC_AI_LIST, 0, 1);
00194 SetWindowClassesDirty(WC_AI_DEBUG);
00195 InvalidateWindowClassesData(WC_AI_SETTINGS);
00196 }
00197
00198
00199 void Game::Save()
00200 {
00201 if (Game::instance != NULL && (!_networking || _network_server)) {
00202 Backup<CompanyByte> cur_company(_current_company, OWNER_DEITY, FILE_LINE);
00203 Game::instance->Save();
00204 cur_company.Restore();
00205 } else {
00206 GameInstance::SaveEmpty();
00207 }
00208 }
00209
00210 void Game::Load(int version)
00211 {
00212 if (Game::instance != NULL && (!_networking || _network_server)) {
00213 Backup<CompanyByte> cur_company(_current_company, OWNER_DEITY, FILE_LINE);
00214 Game::instance->Load(version);
00215 cur_company.Restore();
00216 } else {
00217
00218 GameInstance::LoadEmpty();
00219 }
00220 }
00221
00222 char *Game::GetConsoleList(char *p, const char *last, bool newest_only)
00223 {
00224 return Game::scanner_info->GetConsoleList(p, last, newest_only);
00225 }
00226
00227 char *Game::GetConsoleLibraryList(char *p, const char *last)
00228 {
00229 return Game::scanner_library->GetConsoleList(p, last, true);
00230 }
00231
00232 const ScriptInfoList *Game::GetInfoList()
00233 {
00234 return Game::scanner_info->GetInfoList();
00235 }
00236
00237 const ScriptInfoList *Game::GetUniqueInfoList()
00238 {
00239 return Game::scanner_info->GetUniqueInfoList();
00240 }
00241
00242 GameInfo *Game::FindInfo(const char *name, int version, bool force_exact_match)
00243 {
00244 return Game::scanner_info->FindInfo(name, version, force_exact_match);
00245 }
00246
00247 GameLibrary *Game::FindLibrary(const char *library, int version)
00248 {
00249 return Game::scanner_library->FindLibrary(library, version);
00250 }
00251
00252 #if defined(ENABLE_NETWORK)
00253
00260 bool Game::HasGame(const ContentInfo *ci, bool md5sum)
00261 {
00262 return Game::scanner_info->HasScript(ci, md5sum);
00263 }
00264
00265 bool Game::HasGameLibrary(const ContentInfo *ci, bool md5sum)
00266 {
00267 return Game::scanner_library->HasScript(ci, md5sum);
00268 }
00269
00270 #endif
00271
00272 GameScannerInfo *Game::GetScannerInfo()
00273 {
00274 return Game::scanner_info;
00275 }
00276 GameScannerLibrary *Game::GetScannerLibrary()
00277 {
00278 return Game::scanner_library;
00279 }