Maintain a circular buffer of recent commands, add to crashlog.
[openttd-joker.git] / src / ai / ai_instance.cpp
blobc03e74599230c79ee47a32f0e6daadb66766e461
1 /* $Id$ */
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 */
10 /** @file ai_instance.cpp Implementation of AIInstance. */
12 #include "../stdafx.h"
13 #include "../debug.h"
14 #include "../error.h"
16 #include "../script/squirrel_class.hpp"
18 #include "ai_config.hpp"
19 #include "ai_gui.hpp"
20 #include "ai.hpp"
22 #include "../script/script_storage.hpp"
23 #include "ai_info.hpp"
24 #include "ai_instance.hpp"
26 /* Manually include the Text glue. */
27 #include "../script/api/template/template_text.hpp.sq"
29 /* Convert all AI related classes to Squirrel data.
30 * Note: this line is a marker in squirrel_export.sh. Do not change! */
31 #include "../script/api/ai/ai_accounting.hpp.sq"
32 #include "../script/api/ai/ai_airport.hpp.sq"
33 #include "../script/api/ai/ai_base.hpp.sq"
34 #include "../script/api/ai/ai_basestation.hpp.sq"
35 #include "../script/api/ai/ai_bridge.hpp.sq"
36 #include "../script/api/ai/ai_bridgelist.hpp.sq"
37 #include "../script/api/ai/ai_cargo.hpp.sq"
38 #include "../script/api/ai/ai_cargolist.hpp.sq"
39 #include "../script/api/ai/ai_company.hpp.sq"
40 #include "../script/api/ai/ai_controller.hpp.sq"
41 #include "../script/api/ai/ai_date.hpp.sq"
42 #include "../script/api/ai/ai_depotlist.hpp.sq"
43 #include "../script/api/ai/ai_engine.hpp.sq"
44 #include "../script/api/ai/ai_enginelist.hpp.sq"
45 #include "../script/api/ai/ai_error.hpp.sq"
46 #include "../script/api/ai/ai_event.hpp.sq"
47 #include "../script/api/ai/ai_event_types.hpp.sq"
48 #include "../script/api/ai/ai_execmode.hpp.sq"
49 #include "../script/api/ai/ai_gamesettings.hpp.sq"
50 #include "../script/api/ai/ai_group.hpp.sq"
51 #include "../script/api/ai/ai_grouplist.hpp.sq"
52 #include "../script/api/ai/ai_industry.hpp.sq"
53 #include "../script/api/ai/ai_industrylist.hpp.sq"
54 #include "../script/api/ai/ai_industrytype.hpp.sq"
55 #include "../script/api/ai/ai_industrytypelist.hpp.sq"
56 #include "../script/api/ai/ai_infrastructure.hpp.sq"
57 #include "../script/api/ai/ai_list.hpp.sq"
58 #include "../script/api/ai/ai_log.hpp.sq"
59 #include "../script/api/ai/ai_map.hpp.sq"
60 #include "../script/api/ai/ai_marine.hpp.sq"
61 #include "../script/api/ai/ai_order.hpp.sq"
62 #include "../script/api/ai/ai_rail.hpp.sq"
63 #include "../script/api/ai/ai_railtypelist.hpp.sq"
64 #include "../script/api/ai/ai_road.hpp.sq"
65 #include "../script/api/ai/ai_sign.hpp.sq"
66 #include "../script/api/ai/ai_signlist.hpp.sq"
67 #include "../script/api/ai/ai_station.hpp.sq"
68 #include "../script/api/ai/ai_stationlist.hpp.sq"
69 #include "../script/api/ai/ai_subsidy.hpp.sq"
70 #include "../script/api/ai/ai_subsidylist.hpp.sq"
71 #include "../script/api/ai/ai_testmode.hpp.sq"
72 #include "../script/api/ai/ai_tile.hpp.sq"
73 #include "../script/api/ai/ai_tilelist.hpp.sq"
74 #include "../script/api/ai/ai_town.hpp.sq"
75 #include "../script/api/ai/ai_townlist.hpp.sq"
76 #include "../script/api/ai/ai_tunnel.hpp.sq"
77 #include "../script/api/ai/ai_vehicle.hpp.sq"
78 #include "../script/api/ai/ai_vehiclelist.hpp.sq"
79 #include "../script/api/ai/ai_waypoint.hpp.sq"
80 #include "../script/api/ai/ai_waypointlist.hpp.sq"
82 #include "../company_base.h"
83 #include "../company_func.h"
85 #include "../safeguards.h"
87 AIInstance::AIInstance() :
88 ScriptInstance("AI")
91 void AIInstance::Initialize(AIInfo *info)
93 this->versionAPI = info->GetAPIVersion();
95 /* Register the AIController (including the "import" command) */
96 SQAIController_Register(this->engine);
98 ScriptInstance::Initialize(info->GetMainScript(), info->GetInstanceName(), _current_company);
101 void AIInstance::RegisterAPI()
103 ScriptInstance::RegisterAPI();
105 /* Register all classes */
106 SQAIList_Register(this->engine);
107 SQAIAccounting_Register(this->engine);
108 SQAIAirport_Register(this->engine);
109 SQAIBase_Register(this->engine);
110 SQAIBaseStation_Register(this->engine);
111 SQAIBridge_Register(this->engine);
112 SQAIBridgeList_Register(this->engine);
113 SQAIBridgeList_Length_Register(this->engine);
114 SQAICargo_Register(this->engine);
115 SQAICargoList_Register(this->engine);
116 SQAICargoList_IndustryAccepting_Register(this->engine);
117 SQAICargoList_IndustryProducing_Register(this->engine);
118 SQAICargoList_StationAccepting_Register(this->engine);
119 SQAICompany_Register(this->engine);
120 SQAIDate_Register(this->engine);
121 SQAIDepotList_Register(this->engine);
122 SQAIEngine_Register(this->engine);
123 SQAIEngineList_Register(this->engine);
124 SQAIError_Register(this->engine);
125 SQAIEvent_Register(this->engine);
126 SQAIEventAircraftDestTooFar_Register(this->engine);
127 SQAIEventCompanyAskMerger_Register(this->engine);
128 SQAIEventCompanyBankrupt_Register(this->engine);
129 SQAIEventCompanyInTrouble_Register(this->engine);
130 SQAIEventCompanyMerger_Register(this->engine);
131 SQAIEventCompanyNew_Register(this->engine);
132 SQAIEventCompanyTown_Register(this->engine);
133 SQAIEventController_Register(this->engine);
134 SQAIEventDisasterZeppelinerCleared_Register(this->engine);
135 SQAIEventDisasterZeppelinerCrashed_Register(this->engine);
136 SQAIEventEngineAvailable_Register(this->engine);
137 SQAIEventEnginePreview_Register(this->engine);
138 SQAIEventExclusiveTransportRights_Register(this->engine);
139 SQAIEventIndustryClose_Register(this->engine);
140 SQAIEventIndustryOpen_Register(this->engine);
141 SQAIEventRoadReconstruction_Register(this->engine);
142 SQAIEventStationFirstVehicle_Register(this->engine);
143 SQAIEventSubsidyAwarded_Register(this->engine);
144 SQAIEventSubsidyExpired_Register(this->engine);
145 SQAIEventSubsidyOffer_Register(this->engine);
146 SQAIEventSubsidyOfferExpired_Register(this->engine);
147 SQAIEventTownFounded_Register(this->engine);
148 SQAIEventVehicleCrashed_Register(this->engine);
149 SQAIEventVehicleLost_Register(this->engine);
150 SQAIEventVehicleUnprofitable_Register(this->engine);
151 SQAIEventVehicleWaitingInDepot_Register(this->engine);
152 SQAIExecMode_Register(this->engine);
153 SQAIGameSettings_Register(this->engine);
154 SQAIGroup_Register(this->engine);
155 SQAIGroupList_Register(this->engine);
156 SQAIIndustry_Register(this->engine);
157 SQAIIndustryList_Register(this->engine);
158 SQAIIndustryList_CargoAccepting_Register(this->engine);
159 SQAIIndustryList_CargoProducing_Register(this->engine);
160 SQAIIndustryType_Register(this->engine);
161 SQAIIndustryTypeList_Register(this->engine);
162 SQAIInfrastructure_Register(this->engine);
163 SQAILog_Register(this->engine);
164 SQAIMap_Register(this->engine);
165 SQAIMarine_Register(this->engine);
166 SQAIOrder_Register(this->engine);
167 SQAIRail_Register(this->engine);
168 SQAIRailTypeList_Register(this->engine);
169 SQAIRoad_Register(this->engine);
170 SQAISign_Register(this->engine);
171 SQAISignList_Register(this->engine);
172 SQAIStation_Register(this->engine);
173 SQAIStationList_Register(this->engine);
174 SQAIStationList_Cargo_Register(this->engine);
175 SQAIStationList_CargoPlanned_Register(this->engine);
176 SQAIStationList_CargoPlannedByFrom_Register(this->engine);
177 SQAIStationList_CargoPlannedByVia_Register(this->engine);
178 SQAIStationList_CargoPlannedFromByVia_Register(this->engine);
179 SQAIStationList_CargoPlannedViaByFrom_Register(this->engine);
180 SQAIStationList_CargoWaiting_Register(this->engine);
181 SQAIStationList_CargoWaitingByFrom_Register(this->engine);
182 SQAIStationList_CargoWaitingByVia_Register(this->engine);
183 SQAIStationList_CargoWaitingFromByVia_Register(this->engine);
184 SQAIStationList_CargoWaitingViaByFrom_Register(this->engine);
185 SQAIStationList_Vehicle_Register(this->engine);
186 SQAISubsidy_Register(this->engine);
187 SQAISubsidyList_Register(this->engine);
188 SQAITestMode_Register(this->engine);
189 SQAITile_Register(this->engine);
190 SQAITileList_Register(this->engine);
191 SQAITileList_IndustryAccepting_Register(this->engine);
192 SQAITileList_IndustryProducing_Register(this->engine);
193 SQAITileList_StationType_Register(this->engine);
194 SQAITown_Register(this->engine);
195 SQAITownEffectList_Register(this->engine);
196 SQAITownList_Register(this->engine);
197 SQAITunnel_Register(this->engine);
198 SQAIVehicle_Register(this->engine);
199 SQAIVehicleList_Register(this->engine);
200 SQAIVehicleList_DefaultGroup_Register(this->engine);
201 SQAIVehicleList_Depot_Register(this->engine);
202 SQAIVehicleList_Group_Register(this->engine);
203 SQAIVehicleList_SharedOrders_Register(this->engine);
204 SQAIVehicleList_Station_Register(this->engine);
205 SQAIWaypoint_Register(this->engine);
206 SQAIWaypointList_Register(this->engine);
207 SQAIWaypointList_Vehicle_Register(this->engine);
209 if (!this->LoadCompatibilityScripts(this->versionAPI, AI_DIR)) this->Died();
212 void AIInstance::Died()
214 ScriptInstance::Died();
216 ShowAIDebugWindow(_current_company);
218 const AIInfo *info = AIConfig::GetConfig(_current_company, AIConfig::SSS_FORCE_GAME)->GetInfo();
219 if (info != NULL) {
220 ShowErrorMessage(STR_ERROR_AI_PLEASE_REPORT_CRASH, INVALID_STRING_ID, WL_WARNING);
222 if (info->GetURL() != NULL) {
223 ScriptLog::Info("Please report the error to the following URL:");
224 ScriptLog::Info(info->GetURL());
229 void AIInstance::LoadDummyScript()
231 extern void Script_CreateDummy(HSQUIRRELVM vm, StringID string, const char *type);
232 Script_CreateDummy(this->engine->GetVM(), STR_ERROR_AI_NO_AI_FOUND, "AI");
235 int AIInstance::GetSetting(const char *name)
237 return AIConfig::GetConfig(_current_company)->GetSetting(name);
240 ScriptInfo *AIInstance::FindLibrary(const char *library, int version)
242 return (ScriptInfo *)AI::FindLibrary(library, version);
246 * DoCommand callback function for all commands executed by AIs.
247 * @param result The result of the command.
248 * @param tile The tile on which the command was executed.
249 * @param p1 p1 as given to DoCommandPInternal.
250 * @param p2 p2 as given to DoCommandPInternal.
252 void CcAI(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2)
255 * The company might not exist anymore. Check for this.
256 * The command checks are not useful since this callback
257 * is also called when the command fails, which is does
258 * when the company does not exist anymore.
260 const Company *c = Company::GetIfValid(_current_company);
261 if (c == NULL || c->ai_instance == NULL) return;
263 c->ai_instance->DoCommandCallback(result, tile, p1, p2);
264 c->ai_instance->Continue();
267 CommandCallback *AIInstance::GetDoCommandCallback()
269 return &CcAI;