Add: INR currency (#8136)
[openttd-github.git] / src / game / game_core.cpp
blobfbe233c9f00facbe95e8ac7c0c4c1d1c0172a5d9
1 /*
2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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/>.
6 */
8 /** @file game_core.cpp Implementation of Game. */
10 #include "../stdafx.h"
11 #include "../core/backup_type.hpp"
12 #include "../company_base.h"
13 #include "../company_func.h"
14 #include "../network/network.h"
15 #include "../window_func.h"
16 #include "../framerate_type.h"
17 #include "game.hpp"
18 #include "game_scanner.hpp"
19 #include "game_config.hpp"
20 #include "game_instance.hpp"
21 #include "game_info.hpp"
23 #include "../safeguards.h"
25 /* static */ uint Game::frame_counter = 0;
26 /* static */ GameInfo *Game::info = nullptr;
27 /* static */ GameInstance *Game::instance = nullptr;
28 /* static */ GameScannerInfo *Game::scanner_info = nullptr;
29 /* static */ GameScannerLibrary *Game::scanner_library = nullptr;
31 /* static */ void Game::GameLoop()
33 if (_networking && !_network_server) {
34 PerformanceMeasurer::SetInactive(PFE_GAMESCRIPT);
35 return;
37 if (Game::instance == nullptr) {
38 PerformanceMeasurer::SetInactive(PFE_GAMESCRIPT);
39 return;
42 PerformanceMeasurer framerate(PFE_GAMESCRIPT);
44 Game::frame_counter++;
46 Backup<CompanyID> cur_company(_current_company, FILE_LINE);
47 cur_company.Change(OWNER_DEITY);
48 Game::instance->GameLoop();
49 cur_company.Restore();
51 /* Occasionally collect garbage */
52 if ((Game::frame_counter & 255) == 0) {
53 Game::instance->CollectGarbage();
57 /* static */ void Game::Initialize()
59 if (Game::instance != nullptr) Game::Uninitialize(true);
61 Game::frame_counter = 0;
63 if (Game::scanner_info == nullptr) {
64 TarScanner::DoScan(TarScanner::GAME);
65 Game::scanner_info = new GameScannerInfo();
66 Game::scanner_info->Initialize();
67 Game::scanner_library = new GameScannerLibrary();
68 Game::scanner_library->Initialize();
72 /* static */ void Game::StartNew()
74 if (Game::instance != nullptr) return;
76 /* Clients shouldn't start GameScripts */
77 if (_networking && !_network_server) return;
79 GameConfig *config = GameConfig::GetConfig(GameConfig::SSS_FORCE_GAME);
80 GameInfo *info = config->GetInfo();
81 if (info == nullptr) return;
83 config->AnchorUnchangeableSettings();
85 Backup<CompanyID> cur_company(_current_company, FILE_LINE);
86 cur_company.Change(OWNER_DEITY);
88 Game::info = info;
89 Game::instance = new GameInstance();
90 Game::instance->Initialize(info);
92 cur_company.Restore();
94 InvalidateWindowData(WC_AI_DEBUG, 0, -1);
97 /* static */ void Game::Uninitialize(bool keepConfig)
99 Backup<CompanyID> cur_company(_current_company, FILE_LINE);
101 delete Game::instance;
102 Game::instance = nullptr;
103 Game::info = nullptr;
105 cur_company.Restore();
107 if (keepConfig) {
108 Rescan();
109 } else {
110 delete Game::scanner_info;
111 delete Game::scanner_library;
112 Game::scanner_info = nullptr;
113 Game::scanner_library = nullptr;
115 if (_settings_game.game_config != nullptr) {
116 delete _settings_game.game_config;
117 _settings_game.game_config = nullptr;
119 if (_settings_newgame.game_config != nullptr) {
120 delete _settings_newgame.game_config;
121 _settings_newgame.game_config = nullptr;
126 /* static */ void Game::Pause()
128 if (Game::instance != nullptr) Game::instance->Pause();
131 /* static */ void Game::Unpause()
133 if (Game::instance != nullptr) Game::instance->Unpause();
136 /* static */ bool Game::IsPaused()
138 return Game::instance != nullptr? Game::instance->IsPaused() : false;
141 /* static */ void Game::NewEvent(ScriptEvent *event)
143 /* AddRef() and Release() need to be called at least once, so do it here */
144 event->AddRef();
146 /* Clients should ignore events */
147 if (_networking && !_network_server) {
148 event->Release();
149 return;
152 /* Check if Game instance is alive */
153 if (Game::instance == nullptr) {
154 event->Release();
155 return;
158 /* Queue the event */
159 Backup<CompanyID> cur_company(_current_company, OWNER_DEITY, FILE_LINE);
160 Game::instance->InsertEvent(event);
161 cur_company.Restore();
163 event->Release();
166 /* static */ void Game::ResetConfig()
168 /* Check for both newgame as current game if we can reload the GameInfo inside
169 * the GameConfig. If not, remove the Game from the list. */
170 if (_settings_game.game_config != nullptr && _settings_game.game_config->HasScript()) {
171 if (!_settings_game.game_config->ResetInfo(true)) {
172 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());
173 _settings_game.game_config->Change(nullptr);
174 if (Game::instance != nullptr) {
175 delete Game::instance;
176 Game::instance = nullptr;
177 Game::info = nullptr;
179 } else if (Game::instance != nullptr) {
180 Game::info = _settings_game.game_config->GetInfo();
183 if (_settings_newgame.game_config != nullptr && _settings_newgame.game_config->HasScript()) {
184 if (!_settings_newgame.game_config->ResetInfo(false)) {
185 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());
186 _settings_newgame.game_config->Change(nullptr);
191 /* static */ void Game::Rescan()
193 TarScanner::DoScan(TarScanner::GAME);
195 Game::scanner_info->RescanDir();
196 Game::scanner_library->RescanDir();
197 ResetConfig();
199 InvalidateWindowData(WC_AI_LIST, 0, 1);
200 SetWindowClassesDirty(WC_AI_DEBUG);
201 InvalidateWindowClassesData(WC_AI_SETTINGS);
205 /* static */ void Game::Save()
207 if (Game::instance != nullptr && (!_networking || _network_server)) {
208 Backup<CompanyID> cur_company(_current_company, OWNER_DEITY, FILE_LINE);
209 Game::instance->Save();
210 cur_company.Restore();
211 } else {
212 GameInstance::SaveEmpty();
216 /* static */ void Game::Load(int version)
218 if (Game::instance != nullptr && (!_networking || _network_server)) {
219 Backup<CompanyID> cur_company(_current_company, OWNER_DEITY, FILE_LINE);
220 Game::instance->Load(version);
221 cur_company.Restore();
222 } else {
223 /* Read, but ignore, the load data */
224 GameInstance::LoadEmpty();
228 /* static */ char *Game::GetConsoleList(char *p, const char *last, bool newest_only)
230 return Game::scanner_info->GetConsoleList(p, last, newest_only);
233 /* static */ char *Game::GetConsoleLibraryList(char *p, const char *last)
235 return Game::scanner_library->GetConsoleList(p, last, true);
238 /* static */ const ScriptInfoList *Game::GetInfoList()
240 return Game::scanner_info->GetInfoList();
243 /* static */ const ScriptInfoList *Game::GetUniqueInfoList()
245 return Game::scanner_info->GetUniqueInfoList();
248 /* static */ GameInfo *Game::FindInfo(const char *name, int version, bool force_exact_match)
250 return Game::scanner_info->FindInfo(name, version, force_exact_match);
253 /* static */ GameLibrary *Game::FindLibrary(const char *library, int version)
255 return Game::scanner_library->FindLibrary(library, version);
259 * Check whether we have an Game (library) with the exact characteristics as ci.
260 * @param ci the characteristics to search on (shortname and md5sum)
261 * @param md5sum whether to check the MD5 checksum
262 * @return true iff we have an Game (library) matching.
264 /* static */ bool Game::HasGame(const ContentInfo *ci, bool md5sum)
266 return Game::scanner_info->HasScript(ci, md5sum);
269 /* static */ bool Game::HasGameLibrary(const ContentInfo *ci, bool md5sum)
271 return Game::scanner_library->HasScript(ci, md5sum);
274 /* static */ GameScannerInfo *Game::GetScannerInfo()
276 return Game::scanner_info;
278 /* static */ GameScannerLibrary *Game::GetScannerLibrary()
280 return Game::scanner_library;