OpenTTD
game_core.cpp
Go to the documentation of this file.
1 /* $Id$ */
2 
3 /*
4  * This file is part of OpenTTD.
5  * 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.
6  * 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.
7  * 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/>.
8  */
9 
12 #include "../stdafx.h"
13 #include "../core/backup_type.hpp"
14 #include "../company_base.h"
15 #include "../company_func.h"
16 #include "../network/network.h"
17 #include "../window_func.h"
18 #include "../framerate_type.h"
19 #include "game.hpp"
20 #include "game_scanner.hpp"
21 #include "game_config.hpp"
22 #include "game_instance.hpp"
23 #include "game_info.hpp"
24 
25 #include "../safeguards.h"
26 
27 /* static */ uint Game::frame_counter = 0;
28 /* static */ GameInfo *Game::info = NULL;
29 /* static */ GameInstance *Game::instance = NULL;
30 /* static */ GameScannerInfo *Game::scanner_info = NULL;
31 /* static */ GameScannerLibrary *Game::scanner_library = NULL;
32 
33 /* static */ void Game::GameLoop()
34 {
35  if (_networking && !_network_server) {
37  return;
38  }
39  if (Game::instance == NULL) {
41  return;
42  }
43 
45 
47 
48  Backup<CompanyByte> cur_company(_current_company, FILE_LINE);
49  cur_company.Change(OWNER_DEITY);
50  Game::instance->GameLoop();
51  cur_company.Restore();
52 
53  /* Occasionally collect garbage */
54  if ((Game::frame_counter & 255) == 0) {
55  Game::instance->CollectGarbage();
56  }
57 }
58 
59 /* static */ void Game::Initialize()
60 {
61  if (Game::instance != NULL) Game::Uninitialize(true);
62 
64 
65  if (Game::scanner_info == NULL) {
67  Game::scanner_info = new GameScannerInfo();
68  Game::scanner_info->Initialize();
69  Game::scanner_library = new GameScannerLibrary();
70  Game::scanner_library->Initialize();
71  }
72 }
73 
74 /* static */ void Game::StartNew()
75 {
76  if (Game::instance != NULL) return;
77 
78  /* Clients shouldn't start GameScripts */
79  if (_networking && !_network_server) return;
80 
82  GameInfo *info = config->GetInfo();
83  if (info == NULL) return;
84 
86 
87  Backup<CompanyByte> cur_company(_current_company, FILE_LINE);
88  cur_company.Change(OWNER_DEITY);
89 
90  Game::info = info;
91  Game::instance = new GameInstance();
92  Game::instance->Initialize(info);
93 
94  cur_company.Restore();
95 
97 }
98 
99 /* static */ void Game::Uninitialize(bool keepConfig)
100 {
101  Backup<CompanyByte> cur_company(_current_company, FILE_LINE);
102 
103  delete Game::instance;
104  Game::instance = NULL;
105  Game::info = NULL;
106 
107  cur_company.Restore();
108 
109  if (keepConfig) {
110  Rescan();
111  } else {
112  delete Game::scanner_info;
113  delete Game::scanner_library;
114  Game::scanner_info = NULL;
115  Game::scanner_library = NULL;
116 
117  if (_settings_game.game_config != NULL) {
120  }
121  if (_settings_newgame.game_config != NULL) {
124  }
125  }
126 }
127 
128 /* static */ void Game::Pause()
129 {
130  if (Game::instance != NULL) Game::instance->Pause();
131 }
132 
133 /* static */ void Game::Unpause()
134 {
135  if (Game::instance != NULL) Game::instance->Unpause();
136 }
137 
138 /* static */ bool Game::IsPaused()
139 {
140  return Game::instance != NULL? Game::instance->IsPaused() : false;
141 }
142 
143 /* static */ void Game::NewEvent(ScriptEvent *event)
144 {
145  /* AddRef() and Release() need to be called at least once, so do it here */
146  event->AddRef();
147 
148  /* Clients should ignore events */
149  if (_networking && !_network_server) {
150  event->Release();
151  return;
152  }
153 
154  /* Check if Game instance is alive */
155  if (Game::instance == NULL) {
156  event->Release();
157  return;
158  }
159 
160  /* Queue the event */
161  Backup<CompanyByte> cur_company(_current_company, OWNER_DEITY, FILE_LINE);
162  Game::instance->InsertEvent(event);
163  cur_company.Restore();
164 
165  event->Release();
166 }
167 
168 /* static */ void Game::ResetConfig()
169 {
170  /* Check for both newgame as current game if we can reload the GameInfo inside
171  * the GameConfig. If not, remove the Game from the list. */
173  if (!_settings_game.game_config->ResetInfo(true)) {
174  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());
176  if (Game::instance != NULL) {
177  delete Game::instance;
178  Game::instance = NULL;
179  Game::info = NULL;
180  }
181  } else if (Game::instance != NULL) {
182  Game::info = _settings_game.game_config->GetInfo();
183  }
184  }
186  if (!_settings_newgame.game_config->ResetInfo(false)) {
187  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());
189  }
190  }
191 }
192 
193 /* static */ void Game::Rescan()
194 {
196 
197  Game::scanner_info->RescanDir();
198  Game::scanner_library->RescanDir();
199  ResetConfig();
200 
204 }
205 
206 
207 /* static */ void Game::Save()
208 {
209  if (Game::instance != NULL && (!_networking || _network_server)) {
210  Backup<CompanyByte> cur_company(_current_company, OWNER_DEITY, FILE_LINE);
211  Game::instance->Save();
212  cur_company.Restore();
213  } else {
215  }
216 }
217 
218 /* static */ void Game::Load(int version)
219 {
220  if (Game::instance != NULL && (!_networking || _network_server)) {
221  Backup<CompanyByte> cur_company(_current_company, OWNER_DEITY, FILE_LINE);
222  Game::instance->Load(version);
223  cur_company.Restore();
224  } else {
225  /* Read, but ignore, the load data */
227  }
228 }
229 
230 /* static */ char *Game::GetConsoleList(char *p, const char *last, bool newest_only)
231 {
232  return Game::scanner_info->GetConsoleList(p, last, newest_only);
233 }
234 
235 /* static */ char *Game::GetConsoleLibraryList(char *p, const char *last)
236 {
237  return Game::scanner_library->GetConsoleList(p, last, true);
238 }
239 
240 /* static */ const ScriptInfoList *Game::GetInfoList()
241 {
242  return Game::scanner_info->GetInfoList();
243 }
244 
246 {
247  return Game::scanner_info->GetUniqueInfoList();
248 }
249 
250 /* static */ GameInfo *Game::FindInfo(const char *name, int version, bool force_exact_match)
251 {
252  return Game::scanner_info->FindInfo(name, version, force_exact_match);
253 }
254 
255 /* static */ GameLibrary *Game::FindLibrary(const char *library, int version)
256 {
257  return Game::scanner_library->FindLibrary(library, version);
258 }
259 
260 #if defined(ENABLE_NETWORK)
261 
268 /* static */ bool Game::HasGame(const ContentInfo *ci, bool md5sum)
269 {
270  return Game::scanner_info->HasScript(ci, md5sum);
271 }
272 
273 /* static */ bool Game::HasGameLibrary(const ContentInfo *ci, bool md5sum)
274 {
275  return Game::scanner_library->HasScript(ci, md5sum);
276 }
277 
278 #endif /* defined(ENABLE_NETWORK) */
279 
281 {
282  return Game::scanner_info;
283 }
285 {
286  return Game::scanner_library;
287 }
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:129
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition: settings.cpp:77
static void NewEvent(class ScriptEvent *event)
Queue a new event for a Game Script.
Definition: game_core.cpp:143
bool _networking
are we in networking mode?
Definition: network.cpp:56
int version
Version of the script.
static void GameLoop()
Called every game-tick to let Game do something.
Definition: game_core.cpp:33
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:21
static void Uninitialize(bool keepConfig)
Uninitialize the Game system.
Definition: game_core.cpp:99
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:268
static GameScannerInfo * GetScannerInfo()
Gets the ScriptScanner instance that is used to find Game scripts.
Definition: game_core.cpp:280
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:86
static const ScriptInfoList * GetUniqueInfoList()
Wrapper function for GameScanner::GetUniqueInfoList.
Definition: game_core.cpp:245
bool ResetInfo(bool force_exact_match)
When ever the Game Scanner is reloaded, all infos become invalid.
Definition: game_config.cpp:42
static GameConfig * GetConfig(ScriptSettingSource source=SSS_DEFAULT)
Get the config of a company.
Definition: game_config.cpp:20
AI debug window; Window numbers:
Definition: window_type.h:658
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:18
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:23
static class GameInfo * info
Current selected GameInfo.
Definition: game.hpp:130
static char * GetConsoleList(char *p, const char *last, bool newest_only=false)
Wrapper function for GameScanner::GetConsoleList.
Definition: game_core.cpp:230
static class GameInstance * instance
Instance to the current active Game.
Definition: game.hpp:127
The object is owned by a superuser / goal script.
Definition: company_type.h:29
All static information from an Game library like name, version, etc.
Definition: game_info.hpp:52
static void Save()
Save data from a GameScript to a savegame.
Definition: game_core.cpp:207
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:3319
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:106
static char * GetConsoleLibraryList(char *p, const char *last)
Wrapper function for GameScanner::GetConsoleLibraryList.
Definition: game_core.cpp:235
static class GameInfo * FindInfo(const char *name, int version, bool force_exact_match)
Wrapper function for GameScannerInfo::FindInfo.
Definition: game_core.cpp:250
static bool IsPaused()
Checks if the Game Script is paused.
Definition: game_core.cpp:138
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:78
static class GameScannerInfo * scanner_info
Scanner for Game scripts.
Definition: game.hpp:128
The GameInstance tracks games.
static class GameLibrary * FindLibrary(const char *library, int version)
Wrapper function for GameScanner::FindLibrary.
Definition: game_core.cpp:255
static const ScriptInfoList * GetInfoList()
Wrapper function for GameScanner::GetInfoList.
Definition: game_core.cpp:240
bool IsPaused()
Checks if the script is paused.
static void StartNew()
Start up a new GameScript.
Definition: game_core.cpp:74
static uint frame_counter
Tick counter for the Game code.
Definition: game.hpp:126
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:637
const char * name
Full name of the script.
#define DEBUG(name, level,...)
Output a line of debugging information.
Definition: debug.h:36
static void Initialize()
Initialize the Game system.
Definition: game_core.cpp:59
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
CompanyByte _current_company
Company currently doing an action.
Definition: company_cmd.cpp:47
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:133
static GameScannerLibrary * GetScannerLibrary()
Gets the ScriptScanner instance that is used to find Game Libraries.
Definition: game_core.cpp:284
Base functions for all Games.
bool _network_server
network-server is active
Definition: network.cpp:57
AI list; Window numbers:
Definition: window_type.h:279
static void Load(int version)
Load data for a GameScript from a savegame.
Definition: game_core.cpp:218
AI settings; Window numbers:
Definition: window_type.h:170
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:3229
Get the Script config from the current game.
void CollectGarbage() const
Let the VM collect any garbage.
Container for all important information about a piece of content.
Definition: tcp_content.h:58
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:128
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:3301