ai_instance.cpp

Go to the documentation of this file.
00001 /* $Id: ai_instance.cpp 25236 2013-05-12 13:12:55Z rubidium $ */
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 
00085 AIInstance::AIInstance() :
00086   ScriptInstance("AI")
00087 {}
00088 
00089 void AIInstance::Initialize(AIInfo *info)
00090 {
00091   this->versionAPI = info->GetAPIVersion();
00092 
00093   /* Register the AIController (including the "import" command) */
00094   SQAIController_Register(this->engine);
00095 
00096   ScriptInstance::Initialize(info->GetMainScript(), info->GetInstanceName(), _current_company);
00097 }
00098 
00099 void AIInstance::RegisterAPI()
00100 {
00101   ScriptInstance::RegisterAPI();
00102 
00103 /* Register all classes */
00104   SQAIList_Register(this->engine);
00105   SQAIAccounting_Register(this->engine);
00106   SQAIAirport_Register(this->engine);
00107   SQAIBase_Register(this->engine);
00108   SQAIBaseStation_Register(this->engine);
00109   SQAIBridge_Register(this->engine);
00110   SQAIBridgeList_Register(this->engine);
00111   SQAIBridgeList_Length_Register(this->engine);
00112   SQAICargo_Register(this->engine);
00113   SQAICargoList_Register(this->engine);
00114   SQAICargoList_IndustryAccepting_Register(this->engine);
00115   SQAICargoList_IndustryProducing_Register(this->engine);
00116   SQAICargoList_StationAccepting_Register(this->engine);
00117   SQAICompany_Register(this->engine);
00118   SQAIDate_Register(this->engine);
00119   SQAIDepotList_Register(this->engine);
00120   SQAIEngine_Register(this->engine);
00121   SQAIEngineList_Register(this->engine);
00122   SQAIError_Register(this->engine);
00123   SQAIEvent_Register(this->engine);
00124   SQAIEventAircraftDestTooFar_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   SQAIEventCompanyTown_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   SQAIEventExclusiveTransportRights_Register(this->engine);
00137   SQAIEventIndustryClose_Register(this->engine);
00138   SQAIEventIndustryOpen_Register(this->engine);
00139   SQAIEventRoadReconstruction_Register(this->engine);
00140   SQAIEventStationFirstVehicle_Register(this->engine);
00141   SQAIEventSubsidyAwarded_Register(this->engine);
00142   SQAIEventSubsidyExpired_Register(this->engine);
00143   SQAIEventSubsidyOffer_Register(this->engine);
00144   SQAIEventSubsidyOfferExpired_Register(this->engine);
00145   SQAIEventTownFounded_Register(this->engine);
00146   SQAIEventVehicleCrashed_Register(this->engine);
00147   SQAIEventVehicleLost_Register(this->engine);
00148   SQAIEventVehicleUnprofitable_Register(this->engine);
00149   SQAIEventVehicleWaitingInDepot_Register(this->engine);
00150   SQAIExecMode_Register(this->engine);
00151   SQAIGameSettings_Register(this->engine);
00152   SQAIGroup_Register(this->engine);
00153   SQAIGroupList_Register(this->engine);
00154   SQAIIndustry_Register(this->engine);
00155   SQAIIndustryList_Register(this->engine);
00156   SQAIIndustryList_CargoAccepting_Register(this->engine);
00157   SQAIIndustryList_CargoProducing_Register(this->engine);
00158   SQAIIndustryType_Register(this->engine);
00159   SQAIIndustryTypeList_Register(this->engine);
00160   SQAIInfrastructure_Register(this->engine);
00161   SQAILog_Register(this->engine);
00162   SQAIMap_Register(this->engine);
00163   SQAIMarine_Register(this->engine);
00164   SQAIOrder_Register(this->engine);
00165   SQAIRail_Register(this->engine);
00166   SQAIRailTypeList_Register(this->engine);
00167   SQAIRoad_Register(this->engine);
00168   SQAISign_Register(this->engine);
00169   SQAISignList_Register(this->engine);
00170   SQAIStation_Register(this->engine);
00171   SQAIStationList_Register(this->engine);
00172   SQAIStationList_Vehicle_Register(this->engine);
00173   SQAISubsidy_Register(this->engine);
00174   SQAISubsidyList_Register(this->engine);
00175   SQAITestMode_Register(this->engine);
00176   SQAITile_Register(this->engine);
00177   SQAITileList_Register(this->engine);
00178   SQAITileList_IndustryAccepting_Register(this->engine);
00179   SQAITileList_IndustryProducing_Register(this->engine);
00180   SQAITileList_StationType_Register(this->engine);
00181   SQAITown_Register(this->engine);
00182   SQAITownEffectList_Register(this->engine);
00183   SQAITownList_Register(this->engine);
00184   SQAITunnel_Register(this->engine);
00185   SQAIVehicle_Register(this->engine);
00186   SQAIVehicleList_Register(this->engine);
00187   SQAIVehicleList_DefaultGroup_Register(this->engine);
00188   SQAIVehicleList_Depot_Register(this->engine);
00189   SQAIVehicleList_Group_Register(this->engine);
00190   SQAIVehicleList_SharedOrders_Register(this->engine);
00191   SQAIVehicleList_Station_Register(this->engine);
00192   SQAIWaypoint_Register(this->engine);
00193   SQAIWaypointList_Register(this->engine);
00194   SQAIWaypointList_Vehicle_Register(this->engine);
00195 
00196   if (!this->LoadCompatibilityScripts(this->versionAPI, AI_DIR)) this->Died();
00197 }
00198 
00199 void AIInstance::Died()
00200 {
00201   ScriptInstance::Died();
00202 
00203   ShowAIDebugWindow(_current_company);
00204 
00205   const AIInfo *info = AIConfig::GetConfig(_current_company, AIConfig::SSS_FORCE_GAME)->GetInfo();
00206   if (info != NULL) {
00207     ShowErrorMessage(STR_ERROR_AI_PLEASE_REPORT_CRASH, INVALID_STRING_ID, WL_WARNING);
00208 
00209     if (info->GetURL() != NULL) {
00210       ScriptLog::Info("Please report the error to the following URL:");
00211       ScriptLog::Info(info->GetURL());
00212     }
00213   }
00214 }
00215 
00216 void AIInstance::LoadDummyScript()
00217 {
00218   extern void Script_CreateDummy(HSQUIRRELVM vm, StringID string, const char *type);
00219   Script_CreateDummy(this->engine->GetVM(), STR_ERROR_AI_NO_AI_FOUND, "AI");
00220 }
00221 
00222 int AIInstance::GetSetting(const char *name)
00223 {
00224   return AIConfig::GetConfig(_current_company)->GetSetting(name);
00225 }
00226 
00227 ScriptInfo *AIInstance::FindLibrary(const char *library, int version)
00228 {
00229   return (ScriptInfo *)AI::FindLibrary(library, version);
00230 }
00231 
00239 void CcAI(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2)
00240 {
00241   /*
00242    * The company might not exist anymore. Check for this.
00243    * The command checks are not useful since this callback
00244    * is also called when the command fails, which is does
00245    * when the company does not exist anymore.
00246    */
00247   const Company *c = Company::GetIfValid(_current_company);
00248   if (c == NULL || c->ai_instance == NULL) return;
00249 
00250   c->ai_instance->DoCommandCallback(result, tile, p1, p2);
00251   c->ai_instance->Continue();
00252 }
00253 
00254 CommandCallback *AIInstance::GetDoCommandCallback()
00255 {
00256   return &CcAI;
00257 }