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"
|
2020-04-18 22:53:30 +00:00
|
|
|
#include "../script/api/ai/ai_priorityqueue.hpp.sq"
|
2011-11-29 23:29:20 +00:00
|
|
|
#include "../script/api/ai/ai_rail.hpp.sq"
|
|
|
|
#include "../script/api/ai/ai_railtypelist.hpp.sq"
|
|
|
|
#include "../script/api/ai/ai_road.hpp.sq"
|
2019-04-06 19:50:00 +00:00
|
|
|
#include "../script/api/ai/ai_roadtypelist.hpp.sq"
|
2011-11-29 23:29:20 +00:00
|
|
|
#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
|
|
|
|
2014-04-23 20:13:33 +00:00
|
|
|
#include "../safeguards.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);
|
2012-05-26 14:16:32 +00:00
|
|
|
SQAIEventCompanyTown_Register(this->engine);
|
2009-01-12 17:11:45 +00:00
|
|
|
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);
|
2012-05-26 14:16:38 +00:00
|
|
|
SQAIEventExclusiveTransportRights_Register(this->engine);
|
2009-01-12 17:11:45 +00:00
|
|
|
SQAIEventIndustryClose_Register(this->engine);
|
|
|
|
SQAIEventIndustryOpen_Register(this->engine);
|
2012-05-26 14:16:45 +00:00
|
|
|
SQAIEventRoadReconstruction_Register(this->engine);
|
2009-01-12 17:11:45 +00:00
|
|
|
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);
|
2019-09-01 16:27:18 +00:00
|
|
|
SQAIEventVehicleAutoReplaced_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);
|
2020-04-18 22:53:30 +00:00
|
|
|
SQAIPriorityQueue_Register(this->engine);
|
2009-01-12 17:11:45 +00:00
|
|
|
SQAIRail_Register(this->engine);
|
|
|
|
SQAIRailTypeList_Register(this->engine);
|
|
|
|
SQAIRoad_Register(this->engine);
|
2019-04-06 19:50:00 +00:00
|
|
|
SQAIRoadTypeList_Register(this->engine);
|
2009-01-12 17:11:45 +00:00
|
|
|
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);
|
2014-09-21 16:20:48 +00:00
|
|
|
SQAIStationList_Cargo_Register(this->engine);
|
|
|
|
SQAIStationList_CargoPlanned_Register(this->engine);
|
|
|
|
SQAIStationList_CargoPlannedByFrom_Register(this->engine);
|
|
|
|
SQAIStationList_CargoPlannedByVia_Register(this->engine);
|
|
|
|
SQAIStationList_CargoPlannedFromByVia_Register(this->engine);
|
|
|
|
SQAIStationList_CargoPlannedViaByFrom_Register(this->engine);
|
|
|
|
SQAIStationList_CargoWaiting_Register(this->engine);
|
|
|
|
SQAIStationList_CargoWaitingByFrom_Register(this->engine);
|
|
|
|
SQAIStationList_CargoWaitingByVia_Register(this->engine);
|
|
|
|
SQAIStationList_CargoWaitingFromByVia_Register(this->engine);
|
|
|
|
SQAIStationList_CargoWaitingViaByFrom_Register(this->engine);
|
2009-01-12 17:11:45 +00:00
|
|
|
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
|
|
|
|
2012-08-13 19:22:26 +00:00
|
|
|
if (!this->LoadCompatibilityScripts(this->versionAPI, AI_DIR)) this->Died();
|
2009-08-18 18:51:42 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2012-04-09 12:35:01 +00:00
|
|
|
const AIInfo *info = AIConfig::GetConfig(_current_company, AIConfig::SSS_FORCE_GAME)->GetInfo();
|
2019-04-10 21:07:06 +00:00
|
|
|
if (info != nullptr) {
|
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
|
|
|
|
2019-04-10 21:07:06 +00:00
|
|
|
if (info->GetURL() != nullptr) {
|
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()
|
|
|
|
{
|
2019-04-15 17:49:30 +00:00
|
|
|
ScriptAllocatorScope alloc_scope(this->engine);
|
2011-11-29 23:26:52 +00:00
|
|
|
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.
|
2019-09-07 16:37:01 +00:00
|
|
|
* @param cmd cmd as given to DoCommandPInternal.
|
2011-11-29 23:21:42 +00:00
|
|
|
*/
|
2019-09-07 16:37:01 +00:00
|
|
|
void CcAI(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2, uint32 cmd)
|
2011-11-29 23:21:42 +00:00
|
|
|
{
|
2013-05-12 13:12:55 +00:00
|
|
|
/*
|
|
|
|
* The company might not exist anymore. Check for this.
|
|
|
|
* The command checks are not useful since this callback
|
|
|
|
* is also called when the command fails, which is does
|
|
|
|
* when the company does not exist anymore.
|
|
|
|
*/
|
|
|
|
const Company *c = Company::GetIfValid(_current_company);
|
2019-04-10 21:07:06 +00:00
|
|
|
if (c == nullptr || c->ai_instance == nullptr) return;
|
2013-05-12 13:12:55 +00:00
|
|
|
|
2019-09-07 16:37:01 +00:00
|
|
|
if (c->ai_instance->DoCommandCallback(result, tile, p1, p2, cmd)) {
|
|
|
|
c->ai_instance->Continue();
|
|
|
|
}
|
2011-11-29 23:21:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CommandCallback *AIInstance::GetDoCommandCallback()
|
|
|
|
{
|
|
|
|
return &CcAI;
|
|
|
|
}
|