ai_instance.cpp

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