diff options
Diffstat (limited to 'src/game')
-rw-r--r-- | src/game/game.hpp | 45 | ||||
-rw-r--r-- | src/game/game_core.cpp | 64 | ||||
-rw-r--r-- | src/game/game_instance.cpp | 89 | ||||
-rw-r--r-- | src/game/game_instance.hpp | 36 |
4 files changed, 234 insertions, 0 deletions
diff --git a/src/game/game.hpp b/src/game/game.hpp new file mode 100644 index 000000000..de9e8213c --- /dev/null +++ b/src/game/game.hpp @@ -0,0 +1,45 @@ +/* $Id$ */ + +/* + * This file is part of OpenTTD. + * 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. + * 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. + * 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/>. + */ + +/** @file game.hpp Base functions for all Games. */ + +#ifndef GAME_HPP +#define GAME_HPP + +/** + * Main Game class. Contains all functions needed to start, stop, save and load Game Scripts. + */ +class Game { +public: + /** + * Called every game-tick to let Game do something. + */ + static void GameLoop(); + + /** + * Initialize the Game system. + */ + static void Initialize(); + + /** + * Uninitialize the Game system. + */ + static void Uninitialize(); + + /** + * Get the current GameScript instance. + */ + static class GameInstance *GetGameInstance() { return Game::instance; } + +private: + static uint frame_counter; ///< Tick counter for the Game code. + static class GameInstance *instance; ///< Instance to the current active Game. +}; + +#endif /* GAME_HPP */ diff --git a/src/game/game_core.cpp b/src/game/game_core.cpp new file mode 100644 index 000000000..7894ab2a6 --- /dev/null +++ b/src/game/game_core.cpp @@ -0,0 +1,64 @@ +/* $Id$ */ + +/* + * This file is part of OpenTTD. + * 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. + * 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. + * 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/>. + */ + +/** @file game_core.cpp Implementation of Game. */ + +#include "../stdafx.h" +#include "../command_func.h" +#include "../core/backup_type.hpp" +#include "../company_base.h" +#include "../company_func.h" +#include "../network/network.h" +#include "game.hpp" +#include "game_instance.hpp" + +/* static */ uint Game::frame_counter = 0; +/* static */ GameInstance *Game::instance = NULL; + +/* static */ void Game::GameLoop() +{ + if (_networking && !_network_server) return; + + Game::frame_counter++; + + Backup<CompanyByte> cur_company(_current_company, FILE_LINE); + cur_company.Change(OWNER_DEITY); + Game::instance->GameLoop(); + cur_company.Restore(); + + /* Occasionally collect garbage */ + if ((Game::frame_counter & 255) == 0) { + Game::instance->CollectGarbage(); + } +} + +/* static */ void Game::Initialize() +{ + if (Game::instance != NULL) Game::Uninitialize(); + + Game::frame_counter = 0; + if (Game::instance == NULL) { + /* Clients shouldn't start GameScripts */ + if (_networking && !_network_server) return; + + Backup<CompanyByte> cur_company(_current_company, FILE_LINE); + cur_company.Change(OWNER_DEITY); + + Game::instance = new GameInstance(); + Game::instance->Initialize(); + + cur_company.Restore(); + } +} + +/* static */ void Game::Uninitialize() +{ + delete Game::instance; + Game::instance = NULL; +} diff --git a/src/game/game_instance.cpp b/src/game/game_instance.cpp new file mode 100644 index 000000000..ed30062e1 --- /dev/null +++ b/src/game/game_instance.cpp @@ -0,0 +1,89 @@ +/* $Id$ */ + +/* + * This file is part of OpenTTD. + * 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. + * 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. + * 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/>. + */ + +/** @file game_instance.cpp Implementation of GameInstance. */ + +#include "../stdafx.h" +#include "../debug.h" +#include "../saveload/saveload.h" + +#include "../script/squirrel_class.hpp" + +#include "../script/script_storage.hpp" +#include "game_instance.hpp" +#include "game.hpp" + +/* Convert all Game related classes to Squirrel data. + * Note: this line is a marker in squirrel_export.sh. Do not change! */ +#include "../script/api/game/game_accounting.hpp.sq" +#include "../script/api/game/game_controller.hpp.sq" +#include "../script/api/game/game_error.hpp.sq" +#include "../script/api/game/game_event.hpp.sq" +#include "../script/api/game/game_execmode.hpp.sq" +#include "../script/api/game/game_list.hpp.sq" +#include "../script/api/game/game_log.hpp.sq" +#include "../script/api/game/game_testmode.hpp.sq" + + +GameInstance::GameInstance() : + ScriptInstance("GS") +{} + +void GameInstance::Initialize() +{ + /* Register the GameController */ + SQGSController_Register(this->engine); + + ScriptInstance::Initialize("test/main.nut", "TestGame"); +} + +void GameInstance::RegisterAPI() +{ + ScriptInstance::RegisterAPI(); + +/* Register all classes */ + SQGSList_Register(this->engine); + SQGSAccounting_Register(this->engine); + SQGSError_Register(this->engine); + SQGSEvent_Register(this->engine); + SQGSEventController_Register(this->engine); + SQGSExecMode_Register(this->engine); + SQGSLog_Register(this->engine); + SQGSTestMode_Register(this->engine); + +} + +int GameInstance::GetSetting(const char *name) +{ + return NULL; +} + +ScriptInfo *GameInstance::FindLibrary(const char *library, int version) +{ + /* 'import' is not supported with GameScripts */ + return NULL; +} + +/** + * DoCommand callback function for all commands executed by Game Scripts. + * @param result The result of the command. + * @param tile The tile on which the command was executed. + * @param p1 p1 as given to DoCommandPInternal. + * @param p2 p2 as given to DoCommandPInternal. + */ +void CcGame(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2) +{ + Game::GetGameInstance()->DoCommandCallback(result, tile, p1, p2); + Game::GetGameInstance()->Continue(); +} + +CommandCallback *GameInstance::GetDoCommandCallback() +{ + return &CcGame; +} diff --git a/src/game/game_instance.hpp b/src/game/game_instance.hpp new file mode 100644 index 000000000..10937c648 --- /dev/null +++ b/src/game/game_instance.hpp @@ -0,0 +1,36 @@ +/* $Id$ */ + +/* + * This file is part of OpenTTD. + * 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. + * 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. + * 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/>. + */ + +/** @file game_instance.hpp The GameInstance tracks games. */ + +#ifndef GAME_INSTANCE_HPP +#define GAME_INSTANCE_HPP + +#include "../script/script_instance.hpp" + +/** Runtime information about a game script like a pointer to the squirrel vm and the current state. */ +class GameInstance : public ScriptInstance { +public: + GameInstance(); + + /** + * Initialize the script and prepare it for its first run. + */ + void Initialize(); + + /* virtual */ int GetSetting(const char *name); + /* virtual */ ScriptInfo *FindLibrary(const char *library, int version); + +private: + /* virtual */ void RegisterAPI(); + /* virtual */ CommandCallback *GetDoCommandCallback(); + /* virtual */ void LoadDummyScript() {} +}; + +#endif /* GAME_INSTANCE_HPP */ |