OpenTTD
game_core.cpp
Go to the documentation of this file.
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  */
7 
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"
22 
23 #include "../safeguards.h"
24 
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;
30 
31 /* static */ void Game::GameLoop()
32 {
33  if (_networking && !_network_server) {
35  return;
36  }
37  if (Game::instance == nullptr) {
39  return;
40  }
41 
43 
45 
46  Backup<CompanyID> cur_company(_current_company, FILE_LINE);
47  cur_company.Change(OWNER_DEITY);
48  Game::instance->GameLoop();
49  cur_company.Restore();
50 
51  /* Occasionally collect garbage */
52  if ((Game::frame_counter & 255) == 0) {
53  Game::instance->CollectGarbage();
54  }
55 }
56 
57 /* static */ void Game::Initialize()
58 {
59  if (Game::instance != nullptr) Game::Uninitialize(true);
60 
62 
63  if (Game::scanner_info == nullptr) {
65  Game::scanner_info = new GameScannerInfo();
66  Game::scanner_info->Initialize();
67  Game::scanner_library = new GameScannerLibrary();
68  Game::scanner_library->Initialize();
69  }
70 }
71 
72 /* static */ void Game::StartNew()
73 {
74  if (Game::instance != nullptr) return;
75 
76  /* Clients shouldn't start GameScripts */
77  if (_networking && !_network_server) return;
78 
80  GameInfo *info = config->GetInfo();
81  if (info == nullptr) return;
82 
84 
85  Backup<CompanyID> cur_company(_current_company, FILE_LINE);
86  cur_company.Change(OWNER_DEITY);
87 
88  Game::info = info;
89  Game::instance = new GameInstance();
90  Game::instance->Initialize(info);
91 
92  cur_company.Restore();
93 
95 }
96 
97 /* static */ void Game::Uninitialize(bool keepConfig)
98 {
99  Backup<CompanyID> cur_company(_current_company, FILE_LINE);
100 
101  delete Game::instance;
102  Game::instance = nullptr;
103  Game::info = nullptr;
104 
105  cur_company.Restore();
106 
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;
114 
115  if (_settings_game.game_config != nullptr) {
117  _settings_game.game_config = nullptr;
118  }
119  if (_settings_newgame.game_config != nullptr) {
121  _settings_newgame.game_config = nullptr;
122  }
123  }
124 }
125 
126 /* static */ void Game::Pause()
127 {
128  if (Game::instance != nullptr) Game::instance->Pause();
129 }
130 
131 /* static */ void Game::Unpause()
132 {
133  if (Game::instance != nullptr) Game::instance->Unpause();
134 }
135 
136 /* static */ bool Game::IsPaused()
137 {
138  return Game::instance != nullptr? Game::instance->IsPaused() : false;
139 }
140 
141 /* static */ void Game::NewEvent(ScriptEvent *event)
142 {
143  /* AddRef() and Release() need to be called at least once, so do it here */
144  event->AddRef();
145 
146  /* Clients should ignore events */
147  if (_networking && !_network_server) {
148  event->Release();
149  return;
150  }
151 
152  /* Check if Game instance is alive */
153  if (Game::instance == nullptr) {
154  event->Release();
155  return;
156  }
157 
158  /* Queue the event */
159  Backup<CompanyID> cur_company(_current_company, OWNER_DEITY, FILE_LINE);
160  Game::instance->InsertEvent(event);
161  cur_company.Restore();
162 
163  event->Release();
164 }
165 
166 /* static */ void Game::ResetConfig()
167 {
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. */
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());
174  if (Game::instance != nullptr) {
175  delete Game::instance;
176  Game::instance = nullptr;
177  Game::info = nullptr;
178  }
179  } else if (Game::instance != nullptr) {
180  Game::info = _settings_game.game_config->GetInfo();
181  }
182  }
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());
187  }
188  }
189 }
190 
191 /* static */ void Game::Rescan()
192 {
194 
195  Game::scanner_info->RescanDir();
196  Game::scanner_library->RescanDir();
197  ResetConfig();
198 
202 }
203 
204 
205 /* static */ void Game::Save()
206 {
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 {
213  }
214 }
215 
216 /* static */ void Game::Load(int version)
217 {
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 */
225  }
226 }
227 
228 /* static */ char *Game::GetConsoleList(char *p, const char *last, bool newest_only)
229 {
230  return Game::scanner_info->GetConsoleList(p, last, newest_only);
231 }
232 
233 /* static */ char *Game::GetConsoleLibraryList(char *p, const char *last)
234 {
235  return Game::scanner_library->GetConsoleList(p, last, true);
236 }
237 
238 /* static */ const ScriptInfoList *Game::GetInfoList()
239 {
240  return Game::scanner_info->GetInfoList();
241 }
242 
244 {
245  return Game::scanner_info->GetUniqueInfoList();
246 }
247 
248 /* static */ GameInfo *Game::FindInfo(const char *name, int version, bool force_exact_match)
249 {
250  return Game::scanner_info->FindInfo(name, version, force_exact_match);
251 }
252 
253 /* static */ GameLibrary *Game::FindLibrary(const char *library, int version)
254 {
255  return Game::scanner_library->FindLibrary(library, version);
256 }
257 
264 /* static */ bool Game::HasGame(const ContentInfo *ci, bool md5sum)
265 {
266  return Game::scanner_info->HasScript(ci, md5sum);
267 }
268 
269 /* static */ bool Game::HasGameLibrary(const ContentInfo *ci, bool md5sum)
270 {
271  return Game::scanner_library->HasScript(ci, md5sum);
272 }
273 
275 {
276  return Game::scanner_info;
277 }
279 {
280  return Game::scanner_library;
281 }
const ScriptInfoList * GetUniqueInfoList()
Get the list of the latest version of all registered scripts.
static class GameScannerLibrary * scanner_library
Scanner for GS Libraries.
Definition: game.hpp:125
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition: settings.cpp:79
static void NewEvent(class ScriptEvent *event)
Queue a new event for a Game Script.
Definition: game_core.cpp:141
bool _networking
are we in networking mode?
Definition: network.cpp:52
int version
Version of the script.
static void GameLoop()
Called every game-tick to let Game do something.
Definition: game_core.cpp:31
GameConfig stores the configuration settings of every Game.
class GameLibrary * FindLibrary(const char *library, int version)
Find a library in the pool.
void InsertEvent(class ScriptEvent *event)
Insert an event for this script.
std::map< const char *, class ScriptInfo *, StringCompare > ScriptInfoList
A list that maps AI names to their AIInfo object.
Definition: ai.hpp:19
static void Uninitialize(bool keepConfig)
Uninitialize the Game system.
Definition: game_core.cpp:97
class GameInfo * FindInfo(const char *nameParam, int versionParam, bool force_exact_match)
Check if we have a game by name and version available in our list.
const ScriptInfoList * GetInfoList()
Get the list of all registered scripts.
static bool HasGame(const struct ContentInfo *ci, bool md5sum)
Wrapper function for GameScanner::HasGame.
Definition: game_core.cpp:264
static GameScannerInfo * GetScannerInfo()
Gets the ScriptScanner instance that is used to find Game scripts.
Definition: game_core.cpp:274
void Change(const char *name, int version=-1, bool force_exact_match=false, bool is_random=false)
Set another Script to be loaded in this slot.
void Change(const U &new_value)
Change the value of the variable.
Definition: backup_type.hpp:84
static const ScriptInfoList * GetUniqueInfoList()
Wrapper function for GameScanner::GetUniqueInfoList.
Definition: game_core.cpp:243
bool ResetInfo(bool force_exact_match)
When ever the Game Scanner is reloaded, all infos become invalid.
Definition: game_config.cpp:40
static GameConfig * GetConfig(ScriptSettingSource source=SSS_DEFAULT)
Get the config of a company.
Definition: game_config.cpp:18
AI debug window; Window numbers:
Definition: window_type.h:656
Game script execution.
RAII class for measuring simple elements of performance.
All static information from an Game like name, version, etc.
Definition: game_info.hpp:16
void Save()
Call the script Save function and save all data in the savegame.
void Unpause()
Resume execution of the script.
const char * GetName() const
Get the name of the Script.
Class to backup a specific variable and restore it later.
Definition: backup_type.hpp:21
static class GameInfo * info
Current selected GameInfo.
Definition: game.hpp:126
static char * GetConsoleList(char *p, const char *last, bool newest_only=false)
Wrapper function for GameScanner::GetConsoleList.
Definition: game_core.cpp:228
static class GameInstance * instance
Instance to the current active Game.
Definition: game.hpp:123
All static information from an Game library like name, version, etc.
Definition: game_info.hpp:50
static void Save()
Save data from a GameScript to a savegame.
Definition: game_core.cpp:205
void InvalidateWindowClassesData(WindowClass cls, int data, bool gui_scope)
Mark window data of all windows of a given class as invalid (in need of re-computing) Note that by de...
Definition: window.cpp:3334
Runtime information about a game script like a pointer to the squirrel vm and the current state...
bool HasScript(const struct ContentInfo *ci, bool md5sum)
Check whether we have a script with the exact characteristics as ci.
Scan for game scripts.
Definition: fileio_func.h:102
static char * GetConsoleLibraryList(char *p, const char *last)
Wrapper function for GameScanner::GetConsoleLibraryList.
Definition: game_core.cpp:233
static class GameInfo * FindInfo(const char *name, int version, bool force_exact_match)
Wrapper function for GameScannerInfo::FindInfo.
Definition: game_core.cpp:248
static bool IsPaused()
Checks if the Game Script is paused.
Definition: game_core.cpp:136
bool HasScript() const
Is this config attached to an Script? In other words, is there a Script that is assigned to this slot...
GameSettings _settings_newgame
Game settings for new games (updated from the intro screen).
Definition: settings.cpp:80
static class GameScannerInfo * scanner_info
Scanner for Game scripts.
Definition: game.hpp:124
The GameInstance tracks games.
static class GameLibrary * FindLibrary(const char *library, int version)
Wrapper function for GameScanner::FindLibrary.
Definition: game_core.cpp:253
static const ScriptInfoList * GetInfoList()
Wrapper function for GameScanner::GetInfoList.
Definition: game_core.cpp:238
bool IsPaused()
Checks if the script is paused.
static void StartNew()
Start up a new GameScript.
Definition: game_core.cpp:72
static uint frame_counter
Tick counter for the Game code.
Definition: game.hpp:122
class GameConfig * game_config
settings for gamescript
void RescanDir()
Rescan the script dir.
uint DoScan(Subdirectory sd)
Perform the scanning of a particular subdirectory.
Definition: fileio.cpp:608
const char * name
Full name of the script.
#define DEBUG(name, level,...)
Output a line of debugging information.
Definition: debug.h:35
static void Initialize()
Initialize the Game system.
Definition: game_core.cpp:57
void Pause()
Suspends the script for the current tick and then pause the execution of script.
GameInfo keeps track of all information of an Game, like Author, Description, ... ...
static void SaveEmpty()
Don&#39;t save any data in the savegame.
declarations of the class for Game scanner
void AnchorUnchangeableSettings()
As long as the default of a setting has not been changed, the value of the setting is not stored...
char * GetConsoleList(char *p, const char *last, bool newest_only) const
Get the list of registered scripts to print on the console.
static void Unpause()
Resume execution of the Game Script.
Definition: game_core.cpp:131
static GameScannerLibrary * GetScannerLibrary()
Gets the ScriptScanner instance that is used to find Game Libraries.
Definition: game_core.cpp:278
Base functions for all Games.
bool _network_server
network-server is active
Definition: network.cpp:53
CompanyID _current_company
Company currently doing an action.
Definition: company_cmd.cpp:45
AI list; Window numbers:
Definition: window_type.h:277
static void Load(int version)
Load data for a GameScript from a savegame.
Definition: game_core.cpp:216
AI settings; Window numbers:
Definition: window_type.h:168
void Restore()
Restore the variable.
void Initialize(class GameInfo *info)
Initialize the script and prepare it for its first run.
static void LoadEmpty()
Load and discard data from a savegame.
void Load(int version)
Load data from a savegame and store it on the stack.
void SetWindowClassesDirty(WindowClass cls)
Mark all windows of a particular class as dirty (in need of repainting)
Definition: window.cpp:3243
Get the Script config from the current game.
void CollectGarbage() const
Let the VM collect any garbage.
The object is owned by a superuser / goal script.
Definition: company_type.h:27
Container for all important information about a piece of content.
Definition: tcp_content.h:54
static void SetInactive(PerformanceElement elem)
Mark a performance element as not currently in use.
static void Pause()
Suspends the Game Script and then pause the execution of the script.
Definition: game_core.cpp:126
void GameLoop()
Run the GameLoop of a script.
void InvalidateWindowData(WindowClass cls, WindowNumber number, int data, bool gui_scope)
Mark window data of the window of a given class and specific window number as invalid (in need of re-...
Definition: window.cpp:3316