OpenTTD
ai_instance.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of OpenTTD.
3  * 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.
4  * 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.
5  * 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/>.
6  */
7 
10 #include "../stdafx.h"
11 #include "../debug.h"
12 #include "../error.h"
13 
14 #include "../script/squirrel_class.hpp"
15 
16 #include "ai_config.hpp"
17 #include "ai_gui.hpp"
18 #include "ai.hpp"
19 
20 #include "../script/script_storage.hpp"
21 #include "ai_info.hpp"
22 #include "ai_instance.hpp"
23 
24 /* Manually include the Text glue. */
25 #include "../script/api/template/template_text.hpp.sq"
26 
27 /* Convert all AI related classes to Squirrel data.
28  * Note: this line is a marker in squirrel_export.sh. Do not change! */
29 #include "../script/api/ai/ai_accounting.hpp.sq"
30 #include "../script/api/ai/ai_airport.hpp.sq"
31 #include "../script/api/ai/ai_base.hpp.sq"
32 #include "../script/api/ai/ai_basestation.hpp.sq"
33 #include "../script/api/ai/ai_bridge.hpp.sq"
34 #include "../script/api/ai/ai_bridgelist.hpp.sq"
35 #include "../script/api/ai/ai_cargo.hpp.sq"
36 #include "../script/api/ai/ai_cargolist.hpp.sq"
37 #include "../script/api/ai/ai_company.hpp.sq"
38 #include "../script/api/ai/ai_controller.hpp.sq"
39 #include "../script/api/ai/ai_date.hpp.sq"
40 #include "../script/api/ai/ai_depotlist.hpp.sq"
41 #include "../script/api/ai/ai_engine.hpp.sq"
42 #include "../script/api/ai/ai_enginelist.hpp.sq"
43 #include "../script/api/ai/ai_error.hpp.sq"
44 #include "../script/api/ai/ai_event.hpp.sq"
45 #include "../script/api/ai/ai_event_types.hpp.sq"
46 #include "../script/api/ai/ai_execmode.hpp.sq"
47 #include "../script/api/ai/ai_gamesettings.hpp.sq"
48 #include "../script/api/ai/ai_group.hpp.sq"
49 #include "../script/api/ai/ai_grouplist.hpp.sq"
50 #include "../script/api/ai/ai_industry.hpp.sq"
51 #include "../script/api/ai/ai_industrylist.hpp.sq"
52 #include "../script/api/ai/ai_industrytype.hpp.sq"
53 #include "../script/api/ai/ai_industrytypelist.hpp.sq"
54 #include "../script/api/ai/ai_infrastructure.hpp.sq"
55 #include "../script/api/ai/ai_list.hpp.sq"
56 #include "../script/api/ai/ai_log.hpp.sq"
57 #include "../script/api/ai/ai_map.hpp.sq"
58 #include "../script/api/ai/ai_marine.hpp.sq"
59 #include "../script/api/ai/ai_order.hpp.sq"
60 #include "../script/api/ai/ai_rail.hpp.sq"
61 #include "../script/api/ai/ai_railtypelist.hpp.sq"
62 #include "../script/api/ai/ai_road.hpp.sq"
63 #include "../script/api/ai/ai_roadtypelist.hpp.sq"
64 #include "../script/api/ai/ai_sign.hpp.sq"
65 #include "../script/api/ai/ai_signlist.hpp.sq"
66 #include "../script/api/ai/ai_station.hpp.sq"
67 #include "../script/api/ai/ai_stationlist.hpp.sq"
68 #include "../script/api/ai/ai_subsidy.hpp.sq"
69 #include "../script/api/ai/ai_subsidylist.hpp.sq"
70 #include "../script/api/ai/ai_testmode.hpp.sq"
71 #include "../script/api/ai/ai_tile.hpp.sq"
72 #include "../script/api/ai/ai_tilelist.hpp.sq"
73 #include "../script/api/ai/ai_town.hpp.sq"
74 #include "../script/api/ai/ai_townlist.hpp.sq"
75 #include "../script/api/ai/ai_tunnel.hpp.sq"
76 #include "../script/api/ai/ai_vehicle.hpp.sq"
77 #include "../script/api/ai/ai_vehiclelist.hpp.sq"
78 #include "../script/api/ai/ai_waypoint.hpp.sq"
79 #include "../script/api/ai/ai_waypointlist.hpp.sq"
80 
81 #include "../company_base.h"
82 #include "../company_func.h"
83 
84 #include "../safeguards.h"
85 
86 AIInstance::AIInstance() :
87  ScriptInstance("AI")
88 {}
89 
91 {
92  this->versionAPI = info->GetAPIVersion();
93 
94  /* Register the AIController (including the "import" command) */
95  SQAIController_Register(this->engine);
96 
98 }
99 
101 {
103 
104 /* Register all classes */
105  SQAIList_Register(this->engine);
106  SQAIAccounting_Register(this->engine);
107  SQAIAirport_Register(this->engine);
108  SQAIBase_Register(this->engine);
109  SQAIBaseStation_Register(this->engine);
110  SQAIBridge_Register(this->engine);
111  SQAIBridgeList_Register(this->engine);
112  SQAIBridgeList_Length_Register(this->engine);
113  SQAICargo_Register(this->engine);
114  SQAICargoList_Register(this->engine);
115  SQAICargoList_IndustryAccepting_Register(this->engine);
116  SQAICargoList_IndustryProducing_Register(this->engine);
117  SQAICargoList_StationAccepting_Register(this->engine);
118  SQAICompany_Register(this->engine);
119  SQAIDate_Register(this->engine);
120  SQAIDepotList_Register(this->engine);
121  SQAIEngine_Register(this->engine);
122  SQAIEngineList_Register(this->engine);
123  SQAIError_Register(this->engine);
124  SQAIEvent_Register(this->engine);
125  SQAIEventAircraftDestTooFar_Register(this->engine);
126  SQAIEventCompanyAskMerger_Register(this->engine);
127  SQAIEventCompanyBankrupt_Register(this->engine);
128  SQAIEventCompanyInTrouble_Register(this->engine);
129  SQAIEventCompanyMerger_Register(this->engine);
130  SQAIEventCompanyNew_Register(this->engine);
131  SQAIEventCompanyTown_Register(this->engine);
132  SQAIEventController_Register(this->engine);
133  SQAIEventDisasterZeppelinerCleared_Register(this->engine);
134  SQAIEventDisasterZeppelinerCrashed_Register(this->engine);
135  SQAIEventEngineAvailable_Register(this->engine);
136  SQAIEventEnginePreview_Register(this->engine);
137  SQAIEventExclusiveTransportRights_Register(this->engine);
138  SQAIEventIndustryClose_Register(this->engine);
139  SQAIEventIndustryOpen_Register(this->engine);
140  SQAIEventRoadReconstruction_Register(this->engine);
141  SQAIEventStationFirstVehicle_Register(this->engine);
142  SQAIEventSubsidyAwarded_Register(this->engine);
143  SQAIEventSubsidyExpired_Register(this->engine);
144  SQAIEventSubsidyOffer_Register(this->engine);
145  SQAIEventSubsidyOfferExpired_Register(this->engine);
146  SQAIEventTownFounded_Register(this->engine);
147  SQAIEventVehicleAutoReplaced_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  SQAIRoadTypeList_Register(this->engine);
171  SQAISign_Register(this->engine);
172  SQAISignList_Register(this->engine);
173  SQAIStation_Register(this->engine);
174  SQAIStationList_Register(this->engine);
175  SQAIStationList_Cargo_Register(this->engine);
176  SQAIStationList_CargoPlanned_Register(this->engine);
177  SQAIStationList_CargoPlannedByFrom_Register(this->engine);
178  SQAIStationList_CargoPlannedByVia_Register(this->engine);
179  SQAIStationList_CargoPlannedFromByVia_Register(this->engine);
180  SQAIStationList_CargoPlannedViaByFrom_Register(this->engine);
181  SQAIStationList_CargoWaiting_Register(this->engine);
182  SQAIStationList_CargoWaitingByFrom_Register(this->engine);
183  SQAIStationList_CargoWaitingByVia_Register(this->engine);
184  SQAIStationList_CargoWaitingFromByVia_Register(this->engine);
185  SQAIStationList_CargoWaitingViaByFrom_Register(this->engine);
186  SQAIStationList_Vehicle_Register(this->engine);
187  SQAISubsidy_Register(this->engine);
188  SQAISubsidyList_Register(this->engine);
189  SQAITestMode_Register(this->engine);
190  SQAITile_Register(this->engine);
191  SQAITileList_Register(this->engine);
192  SQAITileList_IndustryAccepting_Register(this->engine);
193  SQAITileList_IndustryProducing_Register(this->engine);
194  SQAITileList_StationType_Register(this->engine);
195  SQAITown_Register(this->engine);
196  SQAITownEffectList_Register(this->engine);
197  SQAITownList_Register(this->engine);
198  SQAITunnel_Register(this->engine);
199  SQAIVehicle_Register(this->engine);
200  SQAIVehicleList_Register(this->engine);
201  SQAIVehicleList_DefaultGroup_Register(this->engine);
202  SQAIVehicleList_Depot_Register(this->engine);
203  SQAIVehicleList_Group_Register(this->engine);
204  SQAIVehicleList_SharedOrders_Register(this->engine);
205  SQAIVehicleList_Station_Register(this->engine);
206  SQAIWaypoint_Register(this->engine);
207  SQAIWaypointList_Register(this->engine);
208  SQAIWaypointList_Vehicle_Register(this->engine);
209 
210  if (!this->LoadCompatibilityScripts(this->versionAPI, AI_DIR)) this->Died();
211 }
212 
214 {
216 
218 
220  if (info != nullptr) {
221  ShowErrorMessage(STR_ERROR_AI_PLEASE_REPORT_CRASH, INVALID_STRING_ID, WL_WARNING);
222 
223  if (info->GetURL() != nullptr) {
224  ScriptLog::Info("Please report the error to the following URL:");
225  ScriptLog::Info(info->GetURL());
226  }
227  }
228 }
229 
231 {
232  ScriptAllocatorScope alloc_scope(this->engine);
233  extern void Script_CreateDummy(HSQUIRRELVM vm, StringID string, const char *type);
234  Script_CreateDummy(this->engine->GetVM(), STR_ERROR_AI_NO_AI_FOUND, "AI");
235 }
236 
237 int AIInstance::GetSetting(const char *name)
238 {
240 }
241 
242 ScriptInfo *AIInstance::FindLibrary(const char *library, int version)
243 {
244  return (ScriptInfo *)AI::FindLibrary(library, version);
245 }
246 
255 void CcAI(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2, uint32 cmd)
256 {
257  /*
258  * The company might not exist anymore. Check for this.
259  * The command checks are not useful since this callback
260  * is also called when the command fails, which is does
261  * when the company does not exist anymore.
262  */
264  if (c == nullptr || c->ai_instance == nullptr) return;
265 
266  if (c->ai_instance->DoCommandCallback(result, tile, p1, p2, cmd)) {
267  c->ai_instance->Continue();
268  }
269 }
270 
272 {
273  return &CcAI;
274 }
static Titem * GetIfValid(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:302
int version
Version of the script.
Window for configuring the AIs
void ShowErrorMessage(StringID summary_msg, StringID detailed_msg, WarningLevel wl, int x=0, int y=0, const GRFFile *textref_stack_grffile=nullptr, uint textref_stack_size=0, const uint32 *textref_stack=nullptr)
Display an error message in a window.
Definition: error_gui.cpp:380
void Continue()
A script in multiplayer waits for the server to handle his DoCommand.
The AIInstance tracks an AI.
Common return value for all commands.
Definition: command_type.h:23
void CcAI(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2, uint32 cmd)
DoCommand callback function for all commands executed by AIs.
const char * GetAPIVersion() const
Get the API version this AI is written for.
Definition: ai_info.hpp:49
void Died() override
Tell the script it died.
const char * GetMainScript() const
Get the filename of the main.nut script.
Definition: script_info.hpp:92
void CommandCallback(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2, uint32 cmd)
Define a callback function for the client, after the command is finished.
Definition: command_type.h:470
Other information.
Definition: error.h:22
All static information from an Script like name, version, etc.
Definition: script_info.hpp:30
bool DoCommandCallback(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2, uint32 cmd)
DoCommand callback function for all commands executed by scripts.
static AIConfig * GetConfig(CompanyID company, ScriptSettingSource source=SSS_DEFAULT)
Get the config of a company.
Definition: ai_config.cpp:45
void Script_CreateDummy(HSQUIRRELVM vm, StringID string, const char *type)
Run the dummy AI and let it generate an error message.
Runtime information about a script like a pointer to the squirrel vm and the current state...
void Initialize(class AIInfo *info)
Initialize the AI and prepare it for its first run.
Definition: ai_instance.cpp:90
const char * GetURL() const
Get the website for this script.
Definition: script_info.hpp:87
uint32 StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:16
int GetSetting(const char *name) const override
Get the value of a setting for this config.
Definition: ai_config.cpp:89
void RegisterAPI() override
Register all API functions to the VM.
Window * ShowAIDebugWindow(CompanyID show_company)
Open the AI debug window and select the given company.
Definition: ai_gui.cpp:1525
HSQUIRRELVM GetVM()
Get the squirrel VM.
Definition: squirrel.hpp:80
const char * name
Full name of the script.
void LoadDummyScript() override
Load the dummy script.
All static information from an AI like name, version, etc.
Definition: ai_info.hpp:16
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:78
Subdirectory for all AI files.
Definition: fileio_type.h:119
static class AILibrary * FindLibrary(const char *library, int version)
Wrapper function for AIScanner::FindLibrary.
Definition: ai_core.cpp:343
virtual void RegisterAPI()
Register all API functions to the VM.
void Initialize(const char *main_script, const char *instance_name, CompanyID company)
Initialize the script and prepare it for its first run.
CompanyID _current_company
Company currently doing an action.
Definition: company_cmd.cpp:45
class Squirrel * engine
Engine used to register for Squirrel.
static const StringID INVALID_STRING_ID
Constant representing an invalid string (16bit in case it is used in savegames)
Definition: strings_type.h:17
int GetSetting(const char *name) override
Get the value of a setting of the current instance.
virtual void Died()
Tell the script it died.
Base functions for all AIs.
const char * GetInstanceName() const
Get the name of the instance of the script to create.
Definition: script_info.hpp:82
AIConfig stores the configuration settings of every AI.
AIInfo keeps track of all information of an AI, like Author, Description, ...
Get the Script config from the current game.
CommandCallback * GetDoCommandCallback() override
Get the callback handling DoCommands in case of networking.
ScriptInfo * FindLibrary(const char *library, int version) override
Find a library.