OpenTTD: src/game/game_instance.cpp Source File

OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

game_instance.cpp
Go to the documentation of this file.
1 /* $Id: game_instance.cpp 26893 2014-09-21 16:20:48Z fonsinchen $ */
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 "../error.h"
14
15 #include "../script/squirrel_class.hpp"
16
17 #include "../script/script_storage.hpp"
18 #include "../ai/ai_gui.hpp"
19 #include "game_config.hpp"
20 #include "game_info.hpp"
21 #include "game_instance.hpp"
22 #include "game_text.hpp"
23 #include "game.hpp"
24
25 /* Convert all Game related classes to Squirrel data.
26 * Note: this line is a marker in squirrel_export.sh. Do not change! */
27 #include "../script/api/game/game_accounting.hpp.sq"
28 #include "../script/api/game/game_admin.hpp.sq"
29 #include "../script/api/game/game_airport.hpp.sq"
30 #include "../script/api/game/game_base.hpp.sq"
31 #include "../script/api/game/game_basestation.hpp.sq"
32 #include "../script/api/game/game_bridge.hpp.sq"
33 #include "../script/api/game/game_bridgelist.hpp.sq"
34 #include "../script/api/game/game_cargo.hpp.sq"
35 #include "../script/api/game/game_cargolist.hpp.sq"
36 #include "../script/api/game/game_cargomonitor.hpp.sq"
37 #include "../script/api/game/game_company.hpp.sq"
38 #include "../script/api/game/game_companymode.hpp.sq"
39 #include "../script/api/game/game_controller.hpp.sq"
40 #include "../script/api/game/game_date.hpp.sq"
41 #include "../script/api/game/game_depotlist.hpp.sq"
42 #include "../script/api/game/game_engine.hpp.sq"
43 #include "../script/api/game/game_enginelist.hpp.sq"
44 #include "../script/api/game/game_error.hpp.sq"
45 #include "../script/api/game/game_event.hpp.sq"
46 #include "../script/api/game/game_event_types.hpp.sq"
47 #include "../script/api/game/game_execmode.hpp.sq"
48 #include "../script/api/game/game_game.hpp.sq"
49 #include "../script/api/game/game_gamesettings.hpp.sq"
50 #include "../script/api/game/game_goal.hpp.sq"
51 #include "../script/api/game/game_industry.hpp.sq"
52 #include "../script/api/game/game_industrylist.hpp.sq"
53 #include "../script/api/game/game_industrytype.hpp.sq"
54 #include "../script/api/game/game_industrytypelist.hpp.sq"
55 #include "../script/api/game/game_infrastructure.hpp.sq"
56 #include "../script/api/game/game_list.hpp.sq"
57 #include "../script/api/game/game_log.hpp.sq"
58 #include "../script/api/game/game_map.hpp.sq"
59 #include "../script/api/game/game_marine.hpp.sq"
60 #include "../script/api/game/game_news.hpp.sq"
61 #include "../script/api/game/game_order.hpp.sq"
62 #include "../script/api/game/game_rail.hpp.sq"
63 #include "../script/api/game/game_railtypelist.hpp.sq"
64 #include "../script/api/game/game_road.hpp.sq"
65 #include "../script/api/game/game_sign.hpp.sq"
66 #include "../script/api/game/game_signlist.hpp.sq"
67 #include "../script/api/game/game_station.hpp.sq"
68 #include "../script/api/game/game_stationlist.hpp.sq"
69 #include "../script/api/game/game_story_page.hpp.sq"
70 #include "../script/api/game/game_storypageelementlist.hpp.sq"
71 #include "../script/api/game/game_storypagelist.hpp.sq"
72 #include "../script/api/game/game_subsidy.hpp.sq"
73 #include "../script/api/game/game_subsidylist.hpp.sq"
74 #include "../script/api/game/game_testmode.hpp.sq"
75 #include "../script/api/game/game_text.hpp.sq"
76 #include "../script/api/game/game_tile.hpp.sq"
77 #include "../script/api/game/game_tilelist.hpp.sq"
78 #include "../script/api/game/game_town.hpp.sq"
79 #include "../script/api/game/game_townlist.hpp.sq"
80 #include "../script/api/game/game_tunnel.hpp.sq"
81 #include "../script/api/game/game_vehicle.hpp.sq"
82 #include "../script/api/game/game_vehiclelist.hpp.sq"
83 #include "../script/api/game/game_viewport.hpp.sq"
84 #include "../script/api/game/game_waypoint.hpp.sq"
85 #include "../script/api/game/game_waypointlist.hpp.sq"
86 #include "../script/api/game/game_window.hpp.sq"
87
88 #include "../safeguards.h"
89
90
91 GameInstance::GameInstance() :
92 ScriptInstance("GS")
93 {}
94
95 void GameInstance::Initialize(GameInfo *info)
96 {
97 this->versionAPI = info->GetAPIVersion();
98
99 /* Register the GameController */
100 SQGSController_Register(this->engine);
101
102 ScriptInstance::Initialize(info->GetMainScript(), info->GetInstanceName(), OWNER_DEITY);
103 }
104
105 void GameInstance::RegisterAPI()
106 {
107 ScriptInstance::RegisterAPI();
108
109 /* Register all classes */
110 SQGSList_Register(this->engine);
111 SQGSAccounting_Register(this->engine);
112 SQGSAdmin_Register(this->engine);
113 SQGSAirport_Register(this->engine);
114 SQGSBase_Register(this->engine);
115 SQGSBaseStation_Register(this->engine);
116 SQGSBridge_Register(this->engine);
117 SQGSBridgeList_Register(this->engine);
118 SQGSBridgeList_Length_Register(this->engine);
119 SQGSCargo_Register(this->engine);
120 SQGSCargoList_Register(this->engine);
121 SQGSCargoList_IndustryAccepting_Register(this->engine);
122 SQGSCargoList_IndustryProducing_Register(this->engine);
123 SQGSCargoList_StationAccepting_Register(this->engine);
124 SQGSCargoMonitor_Register(this->engine);
125 SQGSCompany_Register(this->engine);
126 SQGSCompanyMode_Register(this->engine);
127 SQGSDate_Register(this->engine);
128 SQGSDepotList_Register(this->engine);
129 SQGSEngine_Register(this->engine);
130 SQGSEngineList_Register(this->engine);
131 SQGSError_Register(this->engine);
132 SQGSEvent_Register(this->engine);
133 SQGSEventAdminPort_Register(this->engine);
134 SQGSEventCompanyBankrupt_Register(this->engine);
135 SQGSEventCompanyInTrouble_Register(this->engine);
136 SQGSEventCompanyMerger_Register(this->engine);
137 SQGSEventCompanyNew_Register(this->engine);
138 SQGSEventCompanyTown_Register(this->engine);
139 SQGSEventController_Register(this->engine);
140 SQGSEventExclusiveTransportRights_Register(this->engine);
141 SQGSEventGoalQuestionAnswer_Register(this->engine);
142 SQGSEventIndustryClose_Register(this->engine);
143 SQGSEventIndustryOpen_Register(this->engine);
144 SQGSEventRoadReconstruction_Register(this->engine);
145 SQGSEventStationFirstVehicle_Register(this->engine);
146 SQGSEventSubsidyAwarded_Register(this->engine);
147 SQGSEventSubsidyExpired_Register(this->engine);
148 SQGSEventSubsidyOffer_Register(this->engine);
149 SQGSEventSubsidyOfferExpired_Register(this->engine);
150 SQGSEventTownFounded_Register(this->engine);
151 SQGSEventVehicleCrashed_Register(this->engine);
152 SQGSEventWindowWidgetClick_Register(this->engine);
153 SQGSExecMode_Register(this->engine);
154 SQGSGame_Register(this->engine);
155 SQGSGameSettings_Register(this->engine);
156 SQGSGoal_Register(this->engine);
157 SQGSIndustry_Register(this->engine);
158 SQGSIndustryList_Register(this->engine);
159 SQGSIndustryList_CargoAccepting_Register(this->engine);
160 SQGSIndustryList_CargoProducing_Register(this->engine);
161 SQGSIndustryType_Register(this->engine);
162 SQGSIndustryTypeList_Register(this->engine);
163 SQGSInfrastructure_Register(this->engine);
164 SQGSLog_Register(this->engine);
165 SQGSMap_Register(this->engine);
166 SQGSMarine_Register(this->engine);
167 SQGSNews_Register(this->engine);
168 SQGSOrder_Register(this->engine);
169 SQGSRail_Register(this->engine);
170 SQGSRailTypeList_Register(this->engine);
171 SQGSRoad_Register(this->engine);
172 SQGSSign_Register(this->engine);
173 SQGSSignList_Register(this->engine);
174 SQGSStation_Register(this->engine);
175 SQGSStationList_Register(this->engine);
176 SQGSStationList_Cargo_Register(this->engine);
177 SQGSStationList_CargoPlanned_Register(this->engine);
178 SQGSStationList_CargoPlannedByFrom_Register(this->engine);
179 SQGSStationList_CargoPlannedByVia_Register(this->engine);
180 SQGSStationList_CargoPlannedFromByVia_Register(this->engine);
181 SQGSStationList_CargoPlannedViaByFrom_Register(this->engine);
182 SQGSStationList_CargoWaiting_Register(this->engine);
183 SQGSStationList_CargoWaitingByFrom_Register(this->engine);
184 SQGSStationList_CargoWaitingByVia_Register(this->engine);
185 SQGSStationList_CargoWaitingFromByVia_Register(this->engine);
186 SQGSStationList_CargoWaitingViaByFrom_Register(this->engine);
187 SQGSStationList_Vehicle_Register(this->engine);
188 SQGSStoryPage_Register(this->engine);
189 SQGSStoryPageElementList_Register(this->engine);
190 SQGSStoryPageList_Register(this->engine);
191 SQGSSubsidy_Register(this->engine);
192 SQGSSubsidyList_Register(this->engine);
193 SQGSTestMode_Register(this->engine);
194 SQGSText_Register(this->engine);
195 SQGSTile_Register(this->engine);
196 SQGSTileList_Register(this->engine);
197 SQGSTileList_IndustryAccepting_Register(this->engine);
198 SQGSTileList_IndustryProducing_Register(this->engine);
199 SQGSTileList_StationType_Register(this->engine);
200 SQGSTown_Register(this->engine);
201 SQGSTownEffectList_Register(this->engine);
202 SQGSTownList_Register(this->engine);
203 SQGSTunnel_Register(this->engine);
204 SQGSVehicle_Register(this->engine);
205 SQGSVehicleList_Register(this->engine);
206 SQGSVehicleList_Depot_Register(this->engine);
207 SQGSVehicleList_SharedOrders_Register(this->engine);
208 SQGSVehicleList_Station_Register(this->engine);
209 SQGSViewport_Register(this->engine);
210 SQGSWaypoint_Register(this->engine);
211 SQGSWaypointList_Register(this->engine);
212 SQGSWaypointList_Vehicle_Register(this->engine);
213 SQGSWindow_Register(this->engine);
214
215 RegisterGameTranslation(this->engine);
216
217 if (!this->LoadCompatibilityScripts(this->versionAPI, GAME_DIR)) this->Died();
218 }
219
220 int GameInstance::GetSetting(const char *name)
221 {
222 return GameConfig::GetConfig()->GetSetting(name);
223 }
224
225 ScriptInfo *GameInstance::FindLibrary(const char *library, int version)
226 {
227 return (ScriptInfo *)Game::FindLibrary(library, version);
228 }
229
230 void GameInstance::Died()
231 {
232 ScriptInstance::Died();
233
234 ShowAIDebugWindow(OWNER_DEITY);
235
236 const GameInfo *info = Game::GetInfo();
237 if (info != NULL) {
238 ShowErrorMessage(STR_ERROR_AI_PLEASE_REPORT_CRASH, INVALID_STRING_ID, WL_WARNING);
239
240 if (info->GetURL() != NULL) {
241 ScriptLog::Info("Please report the error to the following URL:");
242 ScriptLog::Info(info->GetURL());
243 }
244 }
245 }
246
254 void CcGame(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2)
255 {
256 Game::GetGameInstance()->DoCommandCallback(result, tile, p1, p2);
257 Game::GetGameInstance()->Continue();
258 }
259
260 CommandCallback *GameInstance::GetDoCommandCallback()
261 {
262 return &CcGame;
263 }