2009-01-12 17:11:45 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* 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.
|
|
|
|
* 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.
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2009-01-12 17:11:45 +00:00
|
|
|
/** @file ai_instance.cpp Implementation of AIInstance. */
|
|
|
|
|
|
|
|
#include "../stdafx.h"
|
|
|
|
#include "../debug.h"
|
2011-12-10 13:54:10 +00:00
|
|
|
#include "../error.h"
|
2009-01-12 17:11:45 +00:00
|
|
|
|
|
|
|
#include "../script/squirrel_class.hpp"
|
2009-01-16 00:05:26 +00:00
|
|
|
|
2009-04-19 15:14:23 +00:00
|
|
|
#include "ai_config.hpp"
|
2009-03-14 01:32:04 +00:00
|
|
|
#include "ai_gui.hpp"
|
2011-11-29 23:27:08 +00:00
|
|
|
#include "ai.hpp"
|
2009-01-12 17:11:45 +00:00
|
|
|
|
2011-11-29 23:21:04 +00:00
|
|
|
#include "../script/script_storage.hpp"
|
2011-11-29 23:26:35 +00:00
|
|
|
#include "ai_info.hpp"
|
2011-11-29 23:21:04 +00:00
|
|
|
#include "ai_instance.hpp"
|
|
|
|
|
2011-12-21 15:06:00 +00:00
|
|
|
/* Manually include the Text glue. */
|
|
|
|
#include "../script/api/template/template_text.hpp.sq"
|
|
|
|
|
2009-01-12 17:11:45 +00:00
|
|
|
/* Convert all AI related classes to Squirrel data.
|
2011-11-19 15:42:40 +00:00
|
|
|
* Note: this line is a marker in squirrel_export.sh. Do not change! */
|
2011-11-29 23:29:20 +00:00
|
|
|
#include "../script/api/ai/ai_accounting.hpp.sq"
|
|
|
|
#include "../script/api/ai/ai_airport.hpp.sq"
|
|
|
|
#include "../script/api/ai/ai_base.hpp.sq"
|
|
|
|
#include "../script/api/ai/ai_basestation.hpp.sq"
|
|
|
|
#include "../script/api/ai/ai_bridge.hpp.sq"
|
|
|
|
#include "../script/api/ai/ai_bridgelist.hpp.sq"
|
|
|
|
#include "../script/api/ai/ai_cargo.hpp.sq"
|
|
|
|
#include "../script/api/ai/ai_cargolist.hpp.sq"
|
|
|
|
#include "../script/api/ai/ai_company.hpp.sq"
|
|
|
|
#include "../script/api/ai/ai_controller.hpp.sq"
|
|
|
|
#include "../script/api/ai/ai_date.hpp.sq"
|
|
|
|
#include "../script/api/ai/ai_depotlist.hpp.sq"
|
|
|
|
#include "../script/api/ai/ai_engine.hpp.sq"
|
|
|
|
#include "../script/api/ai/ai_enginelist.hpp.sq"
|
|
|
|
#include "../script/api/ai/ai_error.hpp.sq"
|
|
|
|
#include "../script/api/ai/ai_event.hpp.sq"
|
|
|
|
#include "../script/api/ai/ai_event_types.hpp.sq"
|
|
|
|
#include "../script/api/ai/ai_execmode.hpp.sq"
|
|
|
|
#include "../script/api/ai/ai_gamesettings.hpp.sq"
|
|
|
|
#include "../script/api/ai/ai_group.hpp.sq"
|
|
|
|
#include "../script/api/ai/ai_grouplist.hpp.sq"
|
|
|
|
#include "../script/api/ai/ai_industry.hpp.sq"
|
|
|
|
#include "../script/api/ai/ai_industrylist.hpp.sq"
|
|
|
|
#include "../script/api/ai/ai_industrytype.hpp.sq"
|
|
|
|
#include "../script/api/ai/ai_industrytypelist.hpp.sq"
|
2011-12-03 23:40:57 +00:00
|
|
|
#include "../script/api/ai/ai_infrastructure.hpp.sq"
|
2011-11-29 23:29:20 +00:00
|
|
|
#include "../script/api/ai/ai_list.hpp.sq"
|
|
|
|
#include "../script/api/ai/ai_log.hpp.sq"
|
|
|
|
#include "../script/api/ai/ai_map.hpp.sq"
|
|
|
|
#include "../script/api/ai/ai_marine.hpp.sq"
|
|
|
|
#include "../script/api/ai/ai_order.hpp.sq"
|
|
|
|
#include "../script/api/ai/ai_rail.hpp.sq"
|
|
|
|
#include "../script/api/ai/ai_railtypelist.hpp.sq"
|
|
|
|
#include "../script/api/ai/ai_road.hpp.sq"
|
|
|
|
#include "../script/api/ai/ai_sign.hpp.sq"
|
|
|
|
#include "../script/api/ai/ai_signlist.hpp.sq"
|
|
|
|
#include "../script/api/ai/ai_station.hpp.sq"
|
|
|
|
#include "../script/api/ai/ai_stationlist.hpp.sq"
|
|
|
|
#include "../script/api/ai/ai_subsidy.hpp.sq"
|
|
|
|
#include "../script/api/ai/ai_subsidylist.hpp.sq"
|
|
|
|
#include "../script/api/ai/ai_testmode.hpp.sq"
|
|
|
|
#include "../script/api/ai/ai_tile.hpp.sq"
|
|
|
|
#include "../script/api/ai/ai_tilelist.hpp.sq"
|
|
|
|
#include "../script/api/ai/ai_town.hpp.sq"
|
|
|
|
#include "../script/api/ai/ai_townlist.hpp.sq"
|
|
|
|
#include "../script/api/ai/ai_tunnel.hpp.sq"
|
|
|
|
#include "../script/api/ai/ai_vehicle.hpp.sq"
|
|
|
|
#include "../script/api/ai/ai_vehiclelist.hpp.sq"
|
|
|
|
#include "../script/api/ai/ai_waypoint.hpp.sq"
|
|
|
|
#include "../script/api/ai/ai_waypointlist.hpp.sq"
|
2009-01-12 17:11:45 +00:00
|
|
|
|
2010-01-15 16:41:15 +00:00
|
|
|
#include "../company_base.h"
|
2011-01-22 14:52:20 +00:00
|
|
|
#include "../company_func.h"
|
2009-08-18 18:51:42 +00:00
|
|
|
#include "../fileio_func.h"
|
|
|
|
|
2011-11-13 20:43:48 +00:00
|
|
|
AIInstance::AIInstance() :
|
2011-11-29 23:21:33 +00:00
|
|
|
ScriptInstance("AI")
|
|
|
|
{}
|
2011-11-13 20:43:48 +00:00
|
|
|
|
|
|
|
void AIInstance::Initialize(AIInfo *info)
|
|
|
|
{
|
2011-11-29 23:21:33 +00:00
|
|
|
this->versionAPI = info->GetAPIVersion();
|
2009-01-12 17:11:45 +00:00
|
|
|
|
2011-11-23 13:39:36 +00:00
|
|
|
/* Register the AIController (including the "import" command) */
|
2009-01-12 17:11:45 +00:00
|
|
|
SQAIController_Register(this->engine);
|
|
|
|
|
2011-12-19 21:05:25 +00:00
|
|
|
ScriptInstance::Initialize(info->GetMainScript(), info->GetInstanceName(), _current_company);
|
2009-01-12 17:11:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AIInstance::RegisterAPI()
|
|
|
|
{
|
2011-11-29 23:21:33 +00:00
|
|
|
ScriptInstance::RegisterAPI();
|
|
|
|
|
2009-01-12 17:11:45 +00:00
|
|
|
/* Register all classes */
|
2010-08-19 15:37:28 +00:00
|
|
|
SQAIList_Register(this->engine);
|
2009-01-12 17:11:45 +00:00
|
|
|
SQAIAccounting_Register(this->engine);
|
|
|
|
SQAIAirport_Register(this->engine);
|
|
|
|
SQAIBase_Register(this->engine);
|
2009-07-31 22:30:54 +00:00
|
|
|
SQAIBaseStation_Register(this->engine);
|
2009-01-12 17:11:45 +00:00
|
|
|
SQAIBridge_Register(this->engine);
|
|
|
|
SQAIBridgeList_Register(this->engine);
|
|
|
|
SQAIBridgeList_Length_Register(this->engine);
|
|
|
|
SQAICargo_Register(this->engine);
|
|
|
|
SQAICargoList_Register(this->engine);
|
|
|
|
SQAICargoList_IndustryAccepting_Register(this->engine);
|
|
|
|
SQAICargoList_IndustryProducing_Register(this->engine);
|
2011-11-08 15:44:53 +00:00
|
|
|
SQAICargoList_StationAccepting_Register(this->engine);
|
2009-01-12 17:11:45 +00:00
|
|
|
SQAICompany_Register(this->engine);
|
|
|
|
SQAIDate_Register(this->engine);
|
|
|
|
SQAIDepotList_Register(this->engine);
|
|
|
|
SQAIEngine_Register(this->engine);
|
|
|
|
SQAIEngineList_Register(this->engine);
|
|
|
|
SQAIError_Register(this->engine);
|
|
|
|
SQAIEvent_Register(this->engine);
|
2011-12-13 00:43:59 +00:00
|
|
|
SQAIEventAircraftDestTooFar_Register(this->engine);
|
2009-09-01 12:57:04 +00:00
|
|
|
SQAIEventCompanyAskMerger_Register(this->engine);
|
2009-01-12 17:11:45 +00:00
|
|
|
SQAIEventCompanyBankrupt_Register(this->engine);
|
|
|
|
SQAIEventCompanyInTrouble_Register(this->engine);
|
|
|
|
SQAIEventCompanyMerger_Register(this->engine);
|
|
|
|
SQAIEventCompanyNew_Register(this->engine);
|
|
|
|
SQAIEventController_Register(this->engine);
|
2009-02-05 17:38:47 +00:00
|
|
|
SQAIEventDisasterZeppelinerCleared_Register(this->engine);
|
|
|
|
SQAIEventDisasterZeppelinerCrashed_Register(this->engine);
|
2009-01-12 17:11:45 +00:00
|
|
|
SQAIEventEngineAvailable_Register(this->engine);
|
|
|
|
SQAIEventEnginePreview_Register(this->engine);
|
|
|
|
SQAIEventIndustryClose_Register(this->engine);
|
|
|
|
SQAIEventIndustryOpen_Register(this->engine);
|
|
|
|
SQAIEventStationFirstVehicle_Register(this->engine);
|
|
|
|
SQAIEventSubsidyAwarded_Register(this->engine);
|
|
|
|
SQAIEventSubsidyExpired_Register(this->engine);
|
|
|
|
SQAIEventSubsidyOffer_Register(this->engine);
|
|
|
|
SQAIEventSubsidyOfferExpired_Register(this->engine);
|
2010-12-29 23:44:39 +00:00
|
|
|
SQAIEventTownFounded_Register(this->engine);
|
2009-01-12 17:11:45 +00:00
|
|
|
SQAIEventVehicleCrashed_Register(this->engine);
|
|
|
|
SQAIEventVehicleLost_Register(this->engine);
|
|
|
|
SQAIEventVehicleUnprofitable_Register(this->engine);
|
|
|
|
SQAIEventVehicleWaitingInDepot_Register(this->engine);
|
|
|
|
SQAIExecMode_Register(this->engine);
|
|
|
|
SQAIGameSettings_Register(this->engine);
|
|
|
|
SQAIGroup_Register(this->engine);
|
|
|
|
SQAIGroupList_Register(this->engine);
|
|
|
|
SQAIIndustry_Register(this->engine);
|
|
|
|
SQAIIndustryList_Register(this->engine);
|
|
|
|
SQAIIndustryList_CargoAccepting_Register(this->engine);
|
|
|
|
SQAIIndustryList_CargoProducing_Register(this->engine);
|
|
|
|
SQAIIndustryType_Register(this->engine);
|
|
|
|
SQAIIndustryTypeList_Register(this->engine);
|
2011-12-03 23:40:57 +00:00
|
|
|
SQAIInfrastructure_Register(this->engine);
|
2009-01-12 17:11:45 +00:00
|
|
|
SQAILog_Register(this->engine);
|
|
|
|
SQAIMap_Register(this->engine);
|
|
|
|
SQAIMarine_Register(this->engine);
|
|
|
|
SQAIOrder_Register(this->engine);
|
|
|
|
SQAIRail_Register(this->engine);
|
|
|
|
SQAIRailTypeList_Register(this->engine);
|
|
|
|
SQAIRoad_Register(this->engine);
|
|
|
|
SQAISign_Register(this->engine);
|
2009-05-23 14:54:55 +00:00
|
|
|
SQAISignList_Register(this->engine);
|
2009-01-12 17:11:45 +00:00
|
|
|
SQAIStation_Register(this->engine);
|
|
|
|
SQAIStationList_Register(this->engine);
|
|
|
|
SQAIStationList_Vehicle_Register(this->engine);
|
|
|
|
SQAISubsidy_Register(this->engine);
|
|
|
|
SQAISubsidyList_Register(this->engine);
|
|
|
|
SQAITestMode_Register(this->engine);
|
|
|
|
SQAITile_Register(this->engine);
|
|
|
|
SQAITileList_Register(this->engine);
|
|
|
|
SQAITileList_IndustryAccepting_Register(this->engine);
|
|
|
|
SQAITileList_IndustryProducing_Register(this->engine);
|
|
|
|
SQAITileList_StationType_Register(this->engine);
|
|
|
|
SQAITown_Register(this->engine);
|
2011-11-23 16:09:46 +00:00
|
|
|
SQAITownEffectList_Register(this->engine);
|
2009-01-12 17:11:45 +00:00
|
|
|
SQAITownList_Register(this->engine);
|
|
|
|
SQAITunnel_Register(this->engine);
|
|
|
|
SQAIVehicle_Register(this->engine);
|
|
|
|
SQAIVehicleList_Register(this->engine);
|
2009-03-04 22:37:25 +00:00
|
|
|
SQAIVehicleList_DefaultGroup_Register(this->engine);
|
2009-09-09 09:46:08 +00:00
|
|
|
SQAIVehicleList_Depot_Register(this->engine);
|
2009-03-04 22:37:25 +00:00
|
|
|
SQAIVehicleList_Group_Register(this->engine);
|
2009-02-05 17:28:37 +00:00
|
|
|
SQAIVehicleList_SharedOrders_Register(this->engine);
|
2009-01-12 17:11:45 +00:00
|
|
|
SQAIVehicleList_Station_Register(this->engine);
|
2009-02-20 10:03:16 +00:00
|
|
|
SQAIWaypoint_Register(this->engine);
|
|
|
|
SQAIWaypointList_Register(this->engine);
|
|
|
|
SQAIWaypointList_Vehicle_Register(this->engine);
|
2009-01-12 17:11:45 +00:00
|
|
|
|
2011-11-29 23:21:33 +00:00
|
|
|
if (!this->LoadCompatibilityScripts(this->versionAPI)) this->Died();
|
2009-01-12 17:11:45 +00:00
|
|
|
}
|
|
|
|
|
2009-08-18 18:51:42 +00:00
|
|
|
bool AIInstance::LoadCompatibilityScripts(const char *api_version)
|
|
|
|
{
|
|
|
|
char script_name[32];
|
|
|
|
seprintf(script_name, lastof(script_name), "compat_%s.nut", api_version);
|
|
|
|
char buf[MAX_PATH];
|
|
|
|
Searchpath sp;
|
|
|
|
FOR_ALL_SEARCHPATHS(sp) {
|
|
|
|
FioAppendDirectory(buf, MAX_PATH, sp, AI_DIR);
|
|
|
|
ttd_strlcat(buf, script_name, MAX_PATH);
|
|
|
|
if (!FileExists(buf)) continue;
|
|
|
|
|
|
|
|
if (this->engine->LoadScript(buf)) return true;
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
ScriptLog::Error("Failed to load API compatibility script");
|
2011-12-01 12:04:10 +00:00
|
|
|
DEBUG(script, 0, "Error compiling / running API compatibility script: %s", buf);
|
2009-08-18 18:51:42 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
ScriptLog::Warning("API compatibility script not found");
|
2009-08-18 18:51:42 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-01-12 17:11:45 +00:00
|
|
|
void AIInstance::Died()
|
|
|
|
{
|
2011-11-29 23:21:33 +00:00
|
|
|
ScriptInstance::Died();
|
2009-03-14 01:32:04 +00:00
|
|
|
|
|
|
|
ShowAIDebugWindow(_current_company);
|
2009-04-19 15:14:23 +00:00
|
|
|
|
|
|
|
const AIInfo *info = AIConfig::GetConfig(_current_company)->GetInfo();
|
2009-04-26 19:30:31 +00:00
|
|
|
if (info != NULL) {
|
2010-02-24 14:46:15 +00:00
|
|
|
ShowErrorMessage(STR_ERROR_AI_PLEASE_REPORT_CRASH, INVALID_STRING_ID, WL_WARNING);
|
2009-04-26 19:30:31 +00:00
|
|
|
|
|
|
|
if (info->GetURL() != NULL) {
|
2011-11-29 23:15:35 +00:00
|
|
|
ScriptLog::Info("Please report the error to the following URL:");
|
|
|
|
ScriptLog::Info(info->GetURL());
|
2009-04-26 19:30:31 +00:00
|
|
|
}
|
2009-04-19 15:14:23 +00:00
|
|
|
}
|
2009-01-12 17:11:45 +00:00
|
|
|
}
|
2011-11-29 23:21:42 +00:00
|
|
|
|
2011-11-29 23:26:52 +00:00
|
|
|
void AIInstance::LoadDummyScript()
|
|
|
|
{
|
|
|
|
extern void Script_CreateDummy(HSQUIRRELVM vm, StringID string, const char *type);
|
|
|
|
Script_CreateDummy(this->engine->GetVM(), STR_ERROR_AI_NO_AI_FOUND, "AI");
|
|
|
|
}
|
|
|
|
|
2011-11-29 23:27:01 +00:00
|
|
|
int AIInstance::GetSetting(const char *name)
|
|
|
|
{
|
|
|
|
return AIConfig::GetConfig(_current_company)->GetSetting(name);
|
|
|
|
}
|
|
|
|
|
2011-11-29 23:27:08 +00:00
|
|
|
ScriptInfo *AIInstance::FindLibrary(const char *library, int version)
|
|
|
|
{
|
|
|
|
return (ScriptInfo *)AI::FindLibrary(library, version);
|
|
|
|
}
|
|
|
|
|
2011-11-29 23:21:42 +00:00
|
|
|
/**
|
|
|
|
* DoCommand callback function for all commands executed by AIs.
|
|
|
|
* @param result The result of the command.
|
|
|
|
* @param tile The tile on which the command was executed.
|
|
|
|
* @param p1 p1 as given to DoCommandPInternal.
|
|
|
|
* @param p2 p2 as given to DoCommandPInternal.
|
|
|
|
*/
|
|
|
|
void CcAI(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2)
|
|
|
|
{
|
|
|
|
Company::Get(_current_company)->ai_instance->DoCommandCallback(result, tile, p1, p2);
|
|
|
|
Company::Get(_current_company)->ai_instance->Continue();
|
|
|
|
}
|
|
|
|
|
|
|
|
CommandCallback *AIInstance::GetDoCommandCallback()
|
|
|
|
{
|
|
|
|
return &CcAI;
|
|
|
|
}
|