Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00012 #include "../stdafx.h"
00013 #include "../debug.h"
00014 #include "../saveload/saveload.h"
00015 #include "../gui.h"
00016
00017 #include "../script/squirrel_class.hpp"
00018
00019 #include "ai_config.hpp"
00020 #include "ai_gui.hpp"
00021 #include "ai.hpp"
00022
00023 #include "../script/script_fatalerror.hpp"
00024 #include "../script/script_suspend.hpp"
00025 #include "../script/script_storage.hpp"
00026 #include "ai_info.hpp"
00027 #include "ai_instance.hpp"
00028
00029
00030
00031 #include "../script/api/ai/ai_accounting.hpp.sq"
00032 #include "../script/api/ai/ai_airport.hpp.sq"
00033 #include "../script/api/ai/ai_base.hpp.sq"
00034 #include "../script/api/ai/ai_basestation.hpp.sq"
00035 #include "../script/api/ai/ai_bridge.hpp.sq"
00036 #include "../script/api/ai/ai_bridgelist.hpp.sq"
00037 #include "../script/api/ai/ai_cargo.hpp.sq"
00038 #include "../script/api/ai/ai_cargolist.hpp.sq"
00039 #include "../script/api/ai/ai_company.hpp.sq"
00040 #include "../script/api/ai/ai_controller.hpp.sq"
00041 #include "../script/api/ai/ai_date.hpp.sq"
00042 #include "../script/api/ai/ai_depotlist.hpp.sq"
00043 #include "../script/api/ai/ai_engine.hpp.sq"
00044 #include "../script/api/ai/ai_enginelist.hpp.sq"
00045 #include "../script/api/ai/ai_error.hpp.sq"
00046 #include "../script/api/ai/ai_event.hpp.sq"
00047 #include "../script/api/ai/ai_event_types.hpp.sq"
00048 #include "../script/api/ai/ai_execmode.hpp.sq"
00049 #include "../script/api/ai/ai_gamesettings.hpp.sq"
00050 #include "../script/api/ai/ai_group.hpp.sq"
00051 #include "../script/api/ai/ai_grouplist.hpp.sq"
00052 #include "../script/api/ai/ai_industry.hpp.sq"
00053 #include "../script/api/ai/ai_industrylist.hpp.sq"
00054 #include "../script/api/ai/ai_industrytype.hpp.sq"
00055 #include "../script/api/ai/ai_industrytypelist.hpp.sq"
00056 #include "../script/api/ai/ai_infrastructure.hpp.sq"
00057 #include "../script/api/ai/ai_list.hpp.sq"
00058 #include "../script/api/ai/ai_log.hpp.sq"
00059 #include "../script/api/ai/ai_map.hpp.sq"
00060 #include "../script/api/ai/ai_marine.hpp.sq"
00061 #include "../script/api/ai/ai_order.hpp.sq"
00062 #include "../script/api/ai/ai_rail.hpp.sq"
00063 #include "../script/api/ai/ai_railtypelist.hpp.sq"
00064 #include "../script/api/ai/ai_road.hpp.sq"
00065 #include "../script/api/ai/ai_sign.hpp.sq"
00066 #include "../script/api/ai/ai_signlist.hpp.sq"
00067 #include "../script/api/ai/ai_station.hpp.sq"
00068 #include "../script/api/ai/ai_stationlist.hpp.sq"
00069 #include "../script/api/ai/ai_subsidy.hpp.sq"
00070 #include "../script/api/ai/ai_subsidylist.hpp.sq"
00071 #include "../script/api/ai/ai_testmode.hpp.sq"
00072 #include "../script/api/ai/ai_tile.hpp.sq"
00073 #include "../script/api/ai/ai_tilelist.hpp.sq"
00074 #include "../script/api/ai/ai_town.hpp.sq"
00075 #include "../script/api/ai/ai_townlist.hpp.sq"
00076 #include "../script/api/ai/ai_tunnel.hpp.sq"
00077 #include "../script/api/ai/ai_vehicle.hpp.sq"
00078 #include "../script/api/ai/ai_vehiclelist.hpp.sq"
00079 #include "../script/api/ai/ai_waypoint.hpp.sq"
00080 #include "../script/api/ai/ai_waypointlist.hpp.sq"
00081
00082 #include "../company_base.h"
00083 #include "../company_func.h"
00084 #include "../fileio_func.h"
00085
00086 AIInstance::AIInstance() :
00087 ScriptInstance("AI")
00088 {}
00089
00090 void AIInstance::Initialize(AIInfo *info)
00091 {
00092 this->versionAPI = info->GetAPIVersion();
00093
00094
00095 SQAIController_Register(this->engine);
00096
00097 ScriptInstance::Initialize(info->GetMainScript(), info->GetInstanceName());
00098 }
00099
00100 void AIInstance::RegisterAPI()
00101 {
00102 ScriptInstance::RegisterAPI();
00103
00104
00105 SQAIList_Register(this->engine);
00106 SQAIAccounting_Register(this->engine);
00107 SQAIAirport_Register(this->engine);
00108 SQAIBase_Register(this->engine);
00109 SQAIBaseStation_Register(this->engine);
00110 SQAIBridge_Register(this->engine);
00111 SQAIBridgeList_Register(this->engine);
00112 SQAIBridgeList_Length_Register(this->engine);
00113 SQAICargo_Register(this->engine);
00114 SQAICargoList_Register(this->engine);
00115 SQAICargoList_IndustryAccepting_Register(this->engine);
00116 SQAICargoList_IndustryProducing_Register(this->engine);
00117 SQAICargoList_StationAccepting_Register(this->engine);
00118 SQAICompany_Register(this->engine);
00119 SQAIDate_Register(this->engine);
00120 SQAIDepotList_Register(this->engine);
00121 SQAIEngine_Register(this->engine);
00122 SQAIEngineList_Register(this->engine);
00123 SQAIError_Register(this->engine);
00124 SQAIEvent_Register(this->engine);
00125 SQAIEventCompanyAskMerger_Register(this->engine);
00126 SQAIEventCompanyBankrupt_Register(this->engine);
00127 SQAIEventCompanyInTrouble_Register(this->engine);
00128 SQAIEventCompanyMerger_Register(this->engine);
00129 SQAIEventCompanyNew_Register(this->engine);
00130 SQAIEventController_Register(this->engine);
00131 SQAIEventDisasterZeppelinerCleared_Register(this->engine);
00132 SQAIEventDisasterZeppelinerCrashed_Register(this->engine);
00133 SQAIEventEngineAvailable_Register(this->engine);
00134 SQAIEventEnginePreview_Register(this->engine);
00135 SQAIEventIndustryClose_Register(this->engine);
00136 SQAIEventIndustryOpen_Register(this->engine);
00137 SQAIEventStationFirstVehicle_Register(this->engine);
00138 SQAIEventSubsidyAwarded_Register(this->engine);
00139 SQAIEventSubsidyExpired_Register(this->engine);
00140 SQAIEventSubsidyOffer_Register(this->engine);
00141 SQAIEventSubsidyOfferExpired_Register(this->engine);
00142 SQAIEventTownFounded_Register(this->engine);
00143 SQAIEventVehicleCrashed_Register(this->engine);
00144 SQAIEventVehicleLost_Register(this->engine);
00145 SQAIEventVehicleUnprofitable_Register(this->engine);
00146 SQAIEventVehicleWaitingInDepot_Register(this->engine);
00147 SQAIExecMode_Register(this->engine);
00148 SQAIGameSettings_Register(this->engine);
00149 SQAIGroup_Register(this->engine);
00150 SQAIGroupList_Register(this->engine);
00151 SQAIIndustry_Register(this->engine);
00152 SQAIIndustryList_Register(this->engine);
00153 SQAIIndustryList_CargoAccepting_Register(this->engine);
00154 SQAIIndustryList_CargoProducing_Register(this->engine);
00155 SQAIIndustryType_Register(this->engine);
00156 SQAIIndustryTypeList_Register(this->engine);
00157 SQAIInfrastructure_Register(this->engine);
00158 SQAILog_Register(this->engine);
00159 SQAIMap_Register(this->engine);
00160 SQAIMarine_Register(this->engine);
00161 SQAIOrder_Register(this->engine);
00162 SQAIRail_Register(this->engine);
00163 SQAIRailTypeList_Register(this->engine);
00164 SQAIRoad_Register(this->engine);
00165 SQAISign_Register(this->engine);
00166 SQAISignList_Register(this->engine);
00167 SQAIStation_Register(this->engine);
00168 SQAIStationList_Register(this->engine);
00169 SQAIStationList_Vehicle_Register(this->engine);
00170 SQAISubsidy_Register(this->engine);
00171 SQAISubsidyList_Register(this->engine);
00172 SQAITestMode_Register(this->engine);
00173 SQAITile_Register(this->engine);
00174 SQAITileList_Register(this->engine);
00175 SQAITileList_IndustryAccepting_Register(this->engine);
00176 SQAITileList_IndustryProducing_Register(this->engine);
00177 SQAITileList_StationType_Register(this->engine);
00178 SQAITown_Register(this->engine);
00179 SQAITownEffectList_Register(this->engine);
00180 SQAITownList_Register(this->engine);
00181 SQAITunnel_Register(this->engine);
00182 SQAIVehicle_Register(this->engine);
00183 SQAIVehicleList_Register(this->engine);
00184 SQAIVehicleList_DefaultGroup_Register(this->engine);
00185 SQAIVehicleList_Depot_Register(this->engine);
00186 SQAIVehicleList_Group_Register(this->engine);
00187 SQAIVehicleList_SharedOrders_Register(this->engine);
00188 SQAIVehicleList_Station_Register(this->engine);
00189 SQAIWaypoint_Register(this->engine);
00190 SQAIWaypointList_Register(this->engine);
00191 SQAIWaypointList_Vehicle_Register(this->engine);
00192
00193 if (!this->LoadCompatibilityScripts(this->versionAPI)) this->Died();
00194 }
00195
00196 bool AIInstance::LoadCompatibilityScripts(const char *api_version)
00197 {
00198 char script_name[32];
00199 seprintf(script_name, lastof(script_name), "compat_%s.nut", api_version);
00200 char buf[MAX_PATH];
00201 Searchpath sp;
00202 FOR_ALL_SEARCHPATHS(sp) {
00203 FioAppendDirectory(buf, MAX_PATH, sp, AI_DIR);
00204 ttd_strlcat(buf, script_name, MAX_PATH);
00205 if (!FileExists(buf)) continue;
00206
00207 if (this->engine->LoadScript(buf)) return true;
00208
00209 ScriptLog::Error("Failed to load API compatibility script");
00210 DEBUG(script, 0, "Error compiling / running API compatibility script: %s", buf);
00211 return false;
00212 }
00213
00214 ScriptLog::Warning("API compatibility script not found");
00215 return true;
00216 }
00217
00218 void AIInstance::Died()
00219 {
00220 ScriptInstance::Died();
00221
00222 ShowAIDebugWindow(_current_company);
00223
00224 const AIInfo *info = AIConfig::GetConfig(_current_company)->GetInfo();
00225 if (info != NULL) {
00226 ShowErrorMessage(STR_ERROR_AI_PLEASE_REPORT_CRASH, INVALID_STRING_ID, WL_WARNING);
00227
00228 if (info->GetURL() != NULL) {
00229 ScriptLog::Info("Please report the error to the following URL:");
00230 ScriptLog::Info(info->GetURL());
00231 }
00232 }
00233 }
00234
00235 void AIInstance::LoadDummyScript()
00236 {
00237 extern void Script_CreateDummy(HSQUIRRELVM vm, StringID string, const char *type);
00238 Script_CreateDummy(this->engine->GetVM(), STR_ERROR_AI_NO_AI_FOUND, "AI");
00239 }
00240
00241 int AIInstance::GetSetting(const char *name)
00242 {
00243 return AIConfig::GetConfig(_current_company)->GetSetting(name);
00244 }
00245
00246 ScriptInfo *AIInstance::FindLibrary(const char *library, int version)
00247 {
00248 return (ScriptInfo *)AI::FindLibrary(library, version);
00249 }
00250
00258 void CcAI(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2)
00259 {
00260 Company::Get(_current_company)->ai_instance->DoCommandCallback(result, tile, p1, p2);
00261 Company::Get(_current_company)->ai_instance->Continue();
00262 }
00263
00264 CommandCallback *AIInstance::GetDoCommandCallback()
00265 {
00266 return &CcAI;
00267 }