game_instance.cpp

Go to the documentation of this file.
00001 /* $Id: game_instance.cpp 26305 2014-02-06 19:41:56Z zuu $ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * 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.
00006  * 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.
00007  * 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/>.
00008  */
00009 
00012 #include "../stdafx.h"
00013 #include "../error.h"
00014 
00015 #include "../script/squirrel_class.hpp"
00016 
00017 #include "../script/script_storage.hpp"
00018 #include "../ai/ai_gui.hpp"
00019 #include "game_config.hpp"
00020 #include "game_info.hpp"
00021 #include "game_instance.hpp"
00022 #include "game_text.hpp"
00023 #include "game.hpp"
00024 
00025 /* Convert all Game related classes to Squirrel data.
00026  * Note: this line is a marker in squirrel_export.sh. Do not change! */
00027 #include "../script/api/game/game_accounting.hpp.sq"
00028 #include "../script/api/game/game_admin.hpp.sq"
00029 #include "../script/api/game/game_airport.hpp.sq"
00030 #include "../script/api/game/game_base.hpp.sq"
00031 #include "../script/api/game/game_basestation.hpp.sq"
00032 #include "../script/api/game/game_bridge.hpp.sq"
00033 #include "../script/api/game/game_bridgelist.hpp.sq"
00034 #include "../script/api/game/game_cargo.hpp.sq"
00035 #include "../script/api/game/game_cargolist.hpp.sq"
00036 #include "../script/api/game/game_cargomonitor.hpp.sq"
00037 #include "../script/api/game/game_company.hpp.sq"
00038 #include "../script/api/game/game_companymode.hpp.sq"
00039 #include "../script/api/game/game_controller.hpp.sq"
00040 #include "../script/api/game/game_date.hpp.sq"
00041 #include "../script/api/game/game_depotlist.hpp.sq"
00042 #include "../script/api/game/game_engine.hpp.sq"
00043 #include "../script/api/game/game_enginelist.hpp.sq"
00044 #include "../script/api/game/game_error.hpp.sq"
00045 #include "../script/api/game/game_event.hpp.sq"
00046 #include "../script/api/game/game_event_types.hpp.sq"
00047 #include "../script/api/game/game_execmode.hpp.sq"
00048 #include "../script/api/game/game_game.hpp.sq"
00049 #include "../script/api/game/game_gamesettings.hpp.sq"
00050 #include "../script/api/game/game_goal.hpp.sq"
00051 #include "../script/api/game/game_industry.hpp.sq"
00052 #include "../script/api/game/game_industrylist.hpp.sq"
00053 #include "../script/api/game/game_industrytype.hpp.sq"
00054 #include "../script/api/game/game_industrytypelist.hpp.sq"
00055 #include "../script/api/game/game_infrastructure.hpp.sq"
00056 #include "../script/api/game/game_list.hpp.sq"
00057 #include "../script/api/game/game_log.hpp.sq"
00058 #include "../script/api/game/game_map.hpp.sq"
00059 #include "../script/api/game/game_marine.hpp.sq"
00060 #include "../script/api/game/game_news.hpp.sq"
00061 #include "../script/api/game/game_order.hpp.sq"
00062 #include "../script/api/game/game_rail.hpp.sq"
00063 #include "../script/api/game/game_railtypelist.hpp.sq"
00064 #include "../script/api/game/game_road.hpp.sq"
00065 #include "../script/api/game/game_sign.hpp.sq"
00066 #include "../script/api/game/game_signlist.hpp.sq"
00067 #include "../script/api/game/game_station.hpp.sq"
00068 #include "../script/api/game/game_stationlist.hpp.sq"
00069 #include "../script/api/game/game_story_page.hpp.sq"
00070 #include "../script/api/game/game_storypageelementlist.hpp.sq"
00071 #include "../script/api/game/game_storypagelist.hpp.sq"
00072 #include "../script/api/game/game_subsidy.hpp.sq"
00073 #include "../script/api/game/game_subsidylist.hpp.sq"
00074 #include "../script/api/game/game_testmode.hpp.sq"
00075 #include "../script/api/game/game_text.hpp.sq"
00076 #include "../script/api/game/game_tile.hpp.sq"
00077 #include "../script/api/game/game_tilelist.hpp.sq"
00078 #include "../script/api/game/game_town.hpp.sq"
00079 #include "../script/api/game/game_townlist.hpp.sq"
00080 #include "../script/api/game/game_tunnel.hpp.sq"
00081 #include "../script/api/game/game_vehicle.hpp.sq"
00082 #include "../script/api/game/game_vehiclelist.hpp.sq"
00083 #include "../script/api/game/game_viewport.hpp.sq"
00084 #include "../script/api/game/game_waypoint.hpp.sq"
00085 #include "../script/api/game/game_waypointlist.hpp.sq"
00086 #include "../script/api/game/game_window.hpp.sq"
00087 
00088 
00089 GameInstance::GameInstance() :
00090   ScriptInstance("GS")
00091 {}
00092 
00093 void GameInstance::Initialize(GameInfo *info)
00094 {
00095   this->versionAPI = info->GetAPIVersion();
00096 
00097   /* Register the GameController */
00098   SQGSController_Register(this->engine);
00099 
00100   ScriptInstance::Initialize(info->GetMainScript(), info->GetInstanceName(), OWNER_DEITY);
00101 }
00102 
00103 void GameInstance::RegisterAPI()
00104 {
00105   ScriptInstance::RegisterAPI();
00106 
00107 /* Register all classes */
00108   SQGSList_Register(this->engine);
00109   SQGSAccounting_Register(this->engine);
00110   SQGSAdmin_Register(this->engine);
00111   SQGSAirport_Register(this->engine);
00112   SQGSBase_Register(this->engine);
00113   SQGSBaseStation_Register(this->engine);
00114   SQGSBridge_Register(this->engine);
00115   SQGSBridgeList_Register(this->engine);
00116   SQGSBridgeList_Length_Register(this->engine);
00117   SQGSCargo_Register(this->engine);
00118   SQGSCargoList_Register(this->engine);
00119   SQGSCargoList_IndustryAccepting_Register(this->engine);
00120   SQGSCargoList_IndustryProducing_Register(this->engine);
00121   SQGSCargoList_StationAccepting_Register(this->engine);
00122   SQGSCargoMonitor_Register(this->engine);
00123   SQGSCompany_Register(this->engine);
00124   SQGSCompanyMode_Register(this->engine);
00125   SQGSDate_Register(this->engine);
00126   SQGSDepotList_Register(this->engine);
00127   SQGSEngine_Register(this->engine);
00128   SQGSEngineList_Register(this->engine);
00129   SQGSError_Register(this->engine);
00130   SQGSEvent_Register(this->engine);
00131   SQGSEventAdminPort_Register(this->engine);
00132   SQGSEventCompanyBankrupt_Register(this->engine);
00133   SQGSEventCompanyInTrouble_Register(this->engine);
00134   SQGSEventCompanyMerger_Register(this->engine);
00135   SQGSEventCompanyNew_Register(this->engine);
00136   SQGSEventCompanyTown_Register(this->engine);
00137   SQGSEventController_Register(this->engine);
00138   SQGSEventExclusiveTransportRights_Register(this->engine);
00139   SQGSEventGoalQuestionAnswer_Register(this->engine);
00140   SQGSEventIndustryClose_Register(this->engine);
00141   SQGSEventIndustryOpen_Register(this->engine);
00142   SQGSEventRoadReconstruction_Register(this->engine);
00143   SQGSEventStationFirstVehicle_Register(this->engine);
00144   SQGSEventSubsidyAwarded_Register(this->engine);
00145   SQGSEventSubsidyExpired_Register(this->engine);
00146   SQGSEventSubsidyOffer_Register(this->engine);
00147   SQGSEventSubsidyOfferExpired_Register(this->engine);
00148   SQGSEventTownFounded_Register(this->engine);
00149   SQGSEventVehicleCrashed_Register(this->engine);
00150   SQGSEventWindowWidgetClick_Register(this->engine);
00151   SQGSExecMode_Register(this->engine);
00152   SQGSGame_Register(this->engine);
00153   SQGSGameSettings_Register(this->engine);
00154   SQGSGoal_Register(this->engine);
00155   SQGSIndustry_Register(this->engine);
00156   SQGSIndustryList_Register(this->engine);
00157   SQGSIndustryList_CargoAccepting_Register(this->engine);
00158   SQGSIndustryList_CargoProducing_Register(this->engine);
00159   SQGSIndustryType_Register(this->engine);
00160   SQGSIndustryTypeList_Register(this->engine);
00161   SQGSInfrastructure_Register(this->engine);
00162   SQGSLog_Register(this->engine);
00163   SQGSMap_Register(this->engine);
00164   SQGSMarine_Register(this->engine);
00165   SQGSNews_Register(this->engine);
00166   SQGSOrder_Register(this->engine);
00167   SQGSRail_Register(this->engine);
00168   SQGSRailTypeList_Register(this->engine);
00169   SQGSRoad_Register(this->engine);
00170   SQGSSign_Register(this->engine);
00171   SQGSSignList_Register(this->engine);
00172   SQGSStation_Register(this->engine);
00173   SQGSStationList_Register(this->engine);
00174   SQGSStationList_Vehicle_Register(this->engine);
00175   SQGSStoryPage_Register(this->engine);
00176   SQGSStoryPageElementList_Register(this->engine);
00177   SQGSStoryPageList_Register(this->engine);
00178   SQGSSubsidy_Register(this->engine);
00179   SQGSSubsidyList_Register(this->engine);
00180   SQGSTestMode_Register(this->engine);
00181   SQGSText_Register(this->engine);
00182   SQGSTile_Register(this->engine);
00183   SQGSTileList_Register(this->engine);
00184   SQGSTileList_IndustryAccepting_Register(this->engine);
00185   SQGSTileList_IndustryProducing_Register(this->engine);
00186   SQGSTileList_StationType_Register(this->engine);
00187   SQGSTown_Register(this->engine);
00188   SQGSTownEffectList_Register(this->engine);
00189   SQGSTownList_Register(this->engine);
00190   SQGSTunnel_Register(this->engine);
00191   SQGSVehicle_Register(this->engine);
00192   SQGSVehicleList_Register(this->engine);
00193   SQGSVehicleList_Depot_Register(this->engine);
00194   SQGSVehicleList_SharedOrders_Register(this->engine);
00195   SQGSVehicleList_Station_Register(this->engine);
00196   SQGSViewport_Register(this->engine);
00197   SQGSWaypoint_Register(this->engine);
00198   SQGSWaypointList_Register(this->engine);
00199   SQGSWaypointList_Vehicle_Register(this->engine);
00200   SQGSWindow_Register(this->engine);
00201 
00202   RegisterGameTranslation(this->engine);
00203 
00204   if (!this->LoadCompatibilityScripts(this->versionAPI, GAME_DIR)) this->Died();
00205 }
00206 
00207 int GameInstance::GetSetting(const char *name)
00208 {
00209   return GameConfig::GetConfig()->GetSetting(name);
00210 }
00211 
00212 ScriptInfo *GameInstance::FindLibrary(const char *library, int version)
00213 {
00214   return (ScriptInfo *)Game::FindLibrary(library, version);
00215 }
00216 
00217 void GameInstance::Died()
00218 {
00219   ScriptInstance::Died();
00220 
00221   ShowAIDebugWindow(OWNER_DEITY);
00222 
00223   const GameInfo *info = Game::GetInfo();
00224   if (info != NULL) {
00225     ShowErrorMessage(STR_ERROR_AI_PLEASE_REPORT_CRASH, INVALID_STRING_ID, WL_WARNING);
00226 
00227     if (info->GetURL() != NULL) {
00228       ScriptLog::Info("Please report the error to the following URL:");
00229       ScriptLog::Info(info->GetURL());
00230     }
00231   }
00232 }
00233 
00241 void CcGame(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2)
00242 {
00243   Game::GetGameInstance()->DoCommandCallback(result, tile, p1, p2);
00244   Game::GetGameInstance()->Continue();
00245 }
00246 
00247 CommandCallback *GameInstance::GetDoCommandCallback()
00248 {
00249   return &CcGame;
00250 }