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 "../error.h"
00015 
00016 #include "../script/squirrel_class.hpp"
00017 
00018 #include "ai_config.hpp"
00019 #include "ai_gui.hpp"
00020 #include "ai.hpp"
00021 
00022 #include "../script/script_storage.hpp"
00023 #include "ai_info.hpp"
00024 #include "ai_instance.hpp"
00025 
00026 /* Manually include the Text glue. */
00027 #include "../script/api/template/template_text.hpp.sq"
00028 
00029 /* Convert all AI related classes to Squirrel data.
00030  * Note: this line is a marker in squirrel_export.sh. Do not change! */
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   /* Register the AIController (including the "import" command) */
00095   SQAIController_Register(this->engine);
00096 
00097   ScriptInstance::Initialize(info->GetMainScript(), info->GetInstanceName(), _current_company);
00098 }
00099 
00100 void AIInstance::RegisterAPI()
00101 {
00102   ScriptInstance::RegisterAPI();
00103 
00104 /* Register all classes */
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   SQAIEventAircraftDestTooFar_Register(this->engine);
00126   SQAIEventCompanyAskMerger_Register(this->engine);
00127   SQAIEventCompanyBankrupt_Register(this->engine);
00128   SQAIEventCompanyInTrouble_Register(this->engine);
00129   SQAIEventCompanyMerger_Register(this->engine);
00130   SQAIEventCompanyNew_Register(this->engine);
00131   SQAIEventController_Register(this->engine);
00132   SQAIEventDisasterZeppelinerCleared_Register(this->engine);
00133   SQAIEventDisasterZeppelinerCrashed_Register(this->engine);
00134   SQAIEventEngineAvailable_Register(this->engine);
00135   SQAIEventEnginePreview_Register(this->engine);
00136   SQAIEventIndustryClose_Register(this->engine);
00137   SQAIEventIndustryOpen_Register(this->engine);
00138   SQAIEventStationFirstVehicle_Register(this->engine);
00139   SQAIEventSubsidyAwarded_Register(this->engine);
00140   SQAIEventSubsidyExpired_Register(this->engine);
00141   SQAIEventSubsidyOffer_Register(this->engine);
00142   SQAIEventSubsidyOfferExpired_Register(this->engine);
00143   SQAIEventTownFounded_Register(this->engine);
00144   SQAIEventVehicleCrashed_Register(this->engine);
00145   SQAIEventVehicleLost_Register(this->engine);
00146   SQAIEventVehicleUnprofitable_Register(this->engine);
00147   SQAIEventVehicleWaitingInDepot_Register(this->engine);
00148   SQAIExecMode_Register(this->engine);
00149   SQAIGameSettings_Register(this->engine);
00150   SQAIGroup_Register(this->engine);
00151   SQAIGroupList_Register(this->engine);
00152   SQAIIndustry_Register(this->engine);
00153   SQAIIndustryList_Register(this->engine);
00154   SQAIIndustryList_CargoAccepting_Register(this->engine);
00155   SQAIIndustryList_CargoProducing_Register(this->engine);
00156   SQAIIndustryType_Register(this->engine);
00157   SQAIIndustryTypeList_Register(this->engine);
00158   SQAIInfrastructure_Register(this->engine);
00159   SQAILog_Register(this->engine);
00160   SQAIMap_Register(this->engine);
00161   SQAIMarine_Register(this->engine);
00162   SQAIOrder_Register(this->engine);
00163   SQAIRail_Register(this->engine);
00164   SQAIRailTypeList_Register(this->engine);
00165   SQAIRoad_Register(this->engine);
00166   SQAISign_Register(this->engine);
00167   SQAISignList_Register(this->engine);
00168   SQAIStation_Register(this->engine);
00169   SQAIStationList_Register(this->engine);
00170   SQAIStationList_Vehicle_Register(this->engine);
00171   SQAISubsidy_Register(this->engine);
00172   SQAISubsidyList_Register(this->engine);
00173   SQAITestMode_Register(this->engine);
00174   SQAITile_Register(this->engine);
00175   SQAITileList_Register(this->engine);
00176   SQAITileList_IndustryAccepting_Register(this->engine);
00177   SQAITileList_IndustryProducing_Register(this->engine);
00178   SQAITileList_StationType_Register(this->engine);
00179   SQAITown_Register(this->engine);
00180   SQAITownEffectList_Register(this->engine);
00181   SQAITownList_Register(this->engine);
00182   SQAITunnel_Register(this->engine);
00183   SQAIVehicle_Register(this->engine);
00184   SQAIVehicleList_Register(this->engine);
00185   SQAIVehicleList_DefaultGroup_Register(this->engine);
00186   SQAIVehicleList_Depot_Register(this->engine);
00187   SQAIVehicleList_Group_Register(this->engine);
00188   SQAIVehicleList_SharedOrders_Register(this->engine);
00189   SQAIVehicleList_Station_Register(this->engine);
00190   SQAIWaypoint_Register(this->engine);
00191   SQAIWaypointList_Register(this->engine);
00192   SQAIWaypointList_Vehicle_Register(this->engine);
00193 
00194   if (!this->LoadCompatibilityScripts(this->versionAPI)) this->Died();
00195 }
00196 
00197 bool AIInstance::LoadCompatibilityScripts(const char *api_version)
00198 {
00199   char script_name[32];
00200   seprintf(script_name, lastof(script_name), "compat_%s.nut", api_version);
00201   char buf[MAX_PATH];
00202   Searchpath sp;
00203   FOR_ALL_SEARCHPATHS(sp) {
00204     FioAppendDirectory(buf, MAX_PATH, sp, AI_DIR);
00205     ttd_strlcat(buf, script_name, MAX_PATH);
00206     if (!FileExists(buf)) continue;
00207 
00208     if (this->engine->LoadScript(buf)) return true;
00209 
00210     ScriptLog::Error("Failed to load API compatibility script");
00211     DEBUG(script, 0, "Error compiling / running API compatibility script: %s", buf);
00212     return false;
00213   }
00214 
00215   ScriptLog::Warning("API compatibility script not found");
00216   return true;
00217 }
00218 
00219 void AIInstance::Died()
00220 {
00221   ScriptInstance::Died();
00222 
00223   ShowAIDebugWindow(_current_company);
00224 
00225   const AIInfo *info = AIConfig::GetConfig(_current_company)->GetInfo();
00226   if (info != NULL) {
00227     ShowErrorMessage(STR_ERROR_AI_PLEASE_REPORT_CRASH, INVALID_STRING_ID, WL_WARNING);
00228 
00229     if (info->GetURL() != NULL) {
00230       ScriptLog::Info("Please report the error to the following URL:");
00231       ScriptLog::Info(info->GetURL());
00232     }
00233   }
00234 }
00235 
00236 void AIInstance::LoadDummyScript()
00237 {
00238   extern void Script_CreateDummy(HSQUIRRELVM vm, StringID string, const char *type);
00239   Script_CreateDummy(this->engine->GetVM(), STR_ERROR_AI_NO_AI_FOUND, "AI");
00240 }
00241 
00242 int AIInstance::GetSetting(const char *name)
00243 {
00244   return AIConfig::GetConfig(_current_company)->GetSetting(name);
00245 }
00246 
00247 ScriptInfo *AIInstance::FindLibrary(const char *library, int version)
00248 {
00249   return (ScriptInfo *)AI::FindLibrary(library, version);
00250 }
00251 
00259 void CcAI(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2)
00260 {
00261   Company::Get(_current_company)->ai_instance->DoCommandCallback(result, tile, p1, p2);
00262   Company::Get(_current_company)->ai_instance->Continue();
00263 }
00264 
00265 CommandCallback *AIInstance::GetDoCommandCallback()
00266 {
00267   return &CcAI;
00268 }