2009-01-12 17:11:45 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
|
|
|
/** @file ai_core.cpp Implementation of AI. */
|
|
|
|
|
|
|
|
#include "../stdafx.h"
|
2009-02-03 22:42:42 +00:00
|
|
|
#include "../core/bitmath_func.hpp"
|
2009-01-12 17:11:45 +00:00
|
|
|
#include "../company_base.h"
|
|
|
|
#include "../company_func.h"
|
|
|
|
#include "../debug.h"
|
|
|
|
#include "../network/network.h"
|
|
|
|
#include "../settings_type.h"
|
|
|
|
#include "../window_func.h"
|
|
|
|
#include "../command_func.h"
|
|
|
|
#include "ai.hpp"
|
|
|
|
#include "ai_scanner.hpp"
|
|
|
|
#include "ai_instance.hpp"
|
|
|
|
#include "ai_config.hpp"
|
|
|
|
|
|
|
|
/* static */ uint AI::frame_counter = 0;
|
|
|
|
/* static */ AIScanner *AI::ai_scanner = NULL;
|
|
|
|
|
|
|
|
/* static */ bool AI::CanStartNew()
|
|
|
|
{
|
|
|
|
/* Only allow new AIs on the server and only when that is allowed in multiplayer */
|
|
|
|
return !_networking || (_network_server && _settings_game.ai.ai_in_multiplayer);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ void AI::StartNew(CompanyID company)
|
|
|
|
{
|
|
|
|
assert(IsValidCompanyID(company));
|
|
|
|
|
|
|
|
/* Clients shouldn't start AIs */
|
|
|
|
if (_networking && !_network_server) return;
|
|
|
|
|
|
|
|
AIInfo *info = AIConfig::GetConfig(company)->GetInfo();
|
|
|
|
if (info == NULL) {
|
|
|
|
info = AI::ai_scanner->SelectRandomAI();
|
|
|
|
assert(info != NULL);
|
|
|
|
/* Load default data and store the name in the settings */
|
2009-01-15 18:15:12 +00:00
|
|
|
AIConfig::GetConfig(company)->ChangeAI(info->GetInstanceName());
|
2009-01-12 17:11:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_current_company = company;
|
|
|
|
Company *c = GetCompany(company);
|
|
|
|
|
|
|
|
c->ai_info = info;
|
2009-01-17 15:14:13 +00:00
|
|
|
assert(c->ai_instance == NULL);
|
2009-01-12 17:11:45 +00:00
|
|
|
c->ai_instance = new AIInstance(info);
|
|
|
|
|
|
|
|
InvalidateWindowData(WC_AI_DEBUG, 0, -1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ void AI::GameLoop()
|
|
|
|
{
|
|
|
|
/* If we are in networking, only servers run this function, and that only if it is allowed */
|
|
|
|
if (_networking && (!_network_server || !_settings_game.ai.ai_in_multiplayer)) return;
|
|
|
|
|
|
|
|
/* The speed with which AIs go, is limited by the 'competitor_speed' */
|
|
|
|
AI::frame_counter++;
|
|
|
|
assert(_settings_game.difficulty.competitor_speed <= 4);
|
|
|
|
if ((AI::frame_counter & ((1 << (4 - _settings_game.difficulty.competitor_speed)) - 1)) != 0) return;
|
|
|
|
|
|
|
|
const Company *c;
|
|
|
|
FOR_ALL_COMPANIES(c) {
|
|
|
|
if (!IsHumanCompany(c->index)) {
|
|
|
|
_current_company = c->index;
|
|
|
|
c->ai_instance->GameLoop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-03 22:42:42 +00:00
|
|
|
/* Occasionally collect garbage; every 255 ticks do one company.
|
|
|
|
* Effectively collecting garbage once every two months per AI. */
|
|
|
|
if ((AI::frame_counter & 255) == 0) {
|
|
|
|
CompanyID cid = (CompanyID)GB(AI::frame_counter, 8, 4);
|
|
|
|
if (IsValidCompanyID(cid) && !IsHumanCompany(cid)) GetCompany(cid)->ai_instance->CollectGarbage();
|
|
|
|
}
|
|
|
|
|
2009-01-12 17:11:45 +00:00
|
|
|
_current_company = OWNER_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ uint AI::GetTick()
|
|
|
|
{
|
|
|
|
return AI::frame_counter;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ void AI::Stop(CompanyID company)
|
|
|
|
{
|
|
|
|
if (_networking && !_network_server) return;
|
|
|
|
|
|
|
|
_current_company = company;
|
|
|
|
Company *c = GetCompany(company);
|
|
|
|
|
|
|
|
delete c->ai_instance;
|
|
|
|
c->ai_instance = NULL;
|
|
|
|
|
|
|
|
InvalidateWindowData(WC_AI_DEBUG, 0, -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ void AI::KillAll()
|
|
|
|
{
|
|
|
|
/* It might happen there are no companies .. than we have nothing to loop */
|
|
|
|
if (GetCompanyPoolSize() == 0) return;
|
|
|
|
|
|
|
|
const Company *c;
|
|
|
|
FOR_ALL_COMPANIES(c) {
|
|
|
|
if (!IsHumanCompany(c->index)) AI::Stop(c->index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ void AI::Initialize()
|
|
|
|
{
|
|
|
|
if (AI::ai_scanner != NULL) AI::Uninitialize(true);
|
|
|
|
|
|
|
|
AI::frame_counter = 0;
|
|
|
|
if (AI::ai_scanner == NULL) AI::ai_scanner = new AIScanner();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ void AI::Uninitialize(bool keepConfig)
|
|
|
|
{
|
|
|
|
AI::KillAll();
|
|
|
|
|
|
|
|
if (keepConfig) {
|
|
|
|
/* Run a rescan, which indexes all AIInfos again, and check if we can
|
|
|
|
* still load all the AIS, while keeping the configs in place */
|
|
|
|
Rescan();
|
|
|
|
} else {
|
|
|
|
delete AI::ai_scanner;
|
|
|
|
AI::ai_scanner = NULL;
|
|
|
|
|
|
|
|
for (CompanyID c = COMPANY_FIRST; c < MAX_COMPANIES; c++) {
|
|
|
|
if (_settings_game.ai_config[c] != NULL) {
|
|
|
|
delete _settings_game.ai_config[c];
|
|
|
|
_settings_game.ai_config[c] = NULL;
|
|
|
|
}
|
|
|
|
if (_settings_newgame.ai_config[c] != NULL) {
|
|
|
|
delete _settings_newgame.ai_config[c];
|
|
|
|
_settings_newgame.ai_config[c] = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ void AI::ResetConfig()
|
|
|
|
{
|
|
|
|
/* Check for both newgame as current game if we can reload the AIInfo insde
|
|
|
|
* the AIConfig. If not, remove the AI from the list (which will assign
|
|
|
|
* a random new AI on reload). */
|
|
|
|
for (CompanyID c = COMPANY_FIRST; c < MAX_COMPANIES; c++) {
|
|
|
|
if (_settings_game.ai_config[c] != NULL && _settings_game.ai_config[c]->HasAI()) {
|
|
|
|
if (!_settings_game.ai_config[c]->ResetInfo()) {
|
|
|
|
DEBUG(ai, 0, "After a reload, the AI by the name '%s' was no longer found, and removed from the list.", _settings_game.ai_config[c]->GetName());
|
|
|
|
_settings_game.ai_config[c]->ChangeAI(NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (_settings_newgame.ai_config[c] != NULL && _settings_newgame.ai_config[c]->HasAI()) {
|
|
|
|
if (!_settings_newgame.ai_config[c]->ResetInfo()) {
|
|
|
|
DEBUG(ai, 0, "After a reload, the AI by the name '%s' was no longer found, and removed from the list.", _settings_newgame.ai_config[c]->GetName());
|
|
|
|
_settings_newgame.ai_config[c]->ChangeAI(NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ void AI::NewEvent(CompanyID company, AIEvent *event)
|
|
|
|
{
|
2009-01-24 21:38:30 +00:00
|
|
|
/* AddRef() and Release() need to be called at least once, so do it here */
|
|
|
|
event->AddRef();
|
|
|
|
|
2009-01-12 17:11:45 +00:00
|
|
|
/* Clients should ignore events */
|
2009-01-24 21:38:30 +00:00
|
|
|
if (_networking && !_network_server) {
|
|
|
|
event->Release();
|
|
|
|
return;
|
|
|
|
}
|
2009-01-12 17:11:45 +00:00
|
|
|
|
|
|
|
/* Only AIs can have an event-queue */
|
2009-01-24 21:38:30 +00:00
|
|
|
if (!IsValidCompanyID(company) || IsHumanCompany(company)) {
|
|
|
|
event->Release();
|
|
|
|
return;
|
|
|
|
}
|
2009-01-12 17:11:45 +00:00
|
|
|
|
|
|
|
/* Queue the event */
|
|
|
|
CompanyID old_company = _current_company;
|
|
|
|
_current_company = company;
|
|
|
|
AIEventController::InsertEvent(event);
|
|
|
|
_current_company = old_company;
|
2009-01-24 21:38:30 +00:00
|
|
|
|
|
|
|
event->Release();
|
2009-01-12 17:11:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ void AI::BroadcastNewEvent(AIEvent *event, CompanyID skip_company)
|
|
|
|
{
|
2009-01-24 21:38:30 +00:00
|
|
|
/* AddRef() and Release() need to be called at least once, so do it here */
|
|
|
|
event->AddRef();
|
|
|
|
|
2009-01-12 17:11:45 +00:00
|
|
|
/* Clients should ignore events */
|
2009-01-24 21:38:30 +00:00
|
|
|
if (_networking && !_network_server) {
|
|
|
|
event->Release();
|
|
|
|
return;
|
|
|
|
}
|
2009-01-12 17:11:45 +00:00
|
|
|
|
|
|
|
/* Try to send the event to all AIs */
|
|
|
|
for (CompanyID c = COMPANY_FIRST; c < MAX_COMPANIES; c++) {
|
|
|
|
if (c != skip_company) AI::NewEvent(c, event);
|
|
|
|
}
|
2009-01-24 21:38:30 +00:00
|
|
|
|
|
|
|
event->Release();
|
2009-01-12 17:11:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CcAI(bool success, TileIndex tile, uint32 p1, uint32 p2)
|
|
|
|
{
|
|
|
|
AIObject::SetLastCommandRes(success);
|
|
|
|
|
|
|
|
if (!success) {
|
|
|
|
AIObject::SetLastError(AIError::StringToError(_error_message));
|
|
|
|
} else {
|
|
|
|
AIObject::IncreaseDoCommandCosts(AIObject::GetLastCost());
|
|
|
|
}
|
|
|
|
|
|
|
|
GetCompany(_current_company)->ai_instance->Continue();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ void AI::Save(CompanyID company)
|
|
|
|
{
|
|
|
|
if (!_networking || _network_server) {
|
|
|
|
assert(IsValidCompanyID(company));
|
|
|
|
assert(GetCompany(company)->ai_instance != NULL);
|
|
|
|
|
|
|
|
CompanyID old_company = _current_company;
|
|
|
|
_current_company = company;
|
|
|
|
GetCompany(company)->ai_instance->Save();
|
|
|
|
_current_company = old_company;
|
|
|
|
} else {
|
|
|
|
AIInstance::SaveEmpty();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-13 01:46:46 +00:00
|
|
|
/* static */ void AI::Load(CompanyID company, int version)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
|
|
|
if (!_networking || _network_server) {
|
|
|
|
assert(IsValidCompanyID(company));
|
|
|
|
assert(GetCompany(company)->ai_instance != NULL);
|
|
|
|
|
|
|
|
CompanyID old_company = _current_company;
|
|
|
|
_current_company = company;
|
2009-01-13 01:46:46 +00:00
|
|
|
GetCompany(company)->ai_instance->Load(version);
|
2009-01-12 17:11:45 +00:00
|
|
|
_current_company = old_company;
|
|
|
|
} else {
|
|
|
|
/* Read, but ignore, the load data */
|
|
|
|
AIInstance::LoadEmpty();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-13 14:00:26 +00:00
|
|
|
/* static */ int AI::GetStartNextTime()
|
|
|
|
{
|
2009-01-13 16:53:03 +00:00
|
|
|
/* Find the first company which doesn't exist yet */
|
2009-01-13 14:00:26 +00:00
|
|
|
for (CompanyID c = COMPANY_FIRST; c < MAX_COMPANIES; c++) {
|
2009-01-13 16:53:03 +00:00
|
|
|
if (!IsValidCompanyID(c)) return AIConfig::GetConfig(c)->GetSetting("start_date");
|
2009-01-13 14:00:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Currently no AI can be started, check again in a year. */
|
2009-01-13 22:58:03 +00:00
|
|
|
return DAYS_IN_YEAR;
|
2009-01-13 14:00:26 +00:00
|
|
|
}
|
|
|
|
|
2009-01-12 17:11:45 +00:00
|
|
|
/* static */ char *AI::GetConsoleList(char *p, const char *last)
|
|
|
|
{
|
|
|
|
return AI::ai_scanner->GetAIConsoleList(p, last);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ const AIInfoList *AI::GetInfoList()
|
|
|
|
{
|
|
|
|
return AI::ai_scanner->GetAIInfoList();
|
|
|
|
}
|
|
|
|
|
2009-01-20 16:49:10 +00:00
|
|
|
/* static */ const AIInfoList *AI::GetUniqueInfoList()
|
|
|
|
{
|
|
|
|
return AI::ai_scanner->GetUniqueAIInfoList();
|
|
|
|
}
|
|
|
|
|
2009-01-13 01:46:46 +00:00
|
|
|
/* static */ AIInfo *AI::FindInfo(const char *name, int version)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
2009-01-13 01:46:46 +00:00
|
|
|
return AI::ai_scanner->FindInfo(name, version);
|
2009-01-12 17:11:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ bool AI::ImportLibrary(const char *library, const char *class_name, int version, HSQUIRRELVM vm)
|
|
|
|
{
|
|
|
|
return AI::ai_scanner->ImportLibrary(library, class_name, version, vm, GetCompany(_current_company)->ai_instance->GetController());
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ void AI::Rescan()
|
|
|
|
{
|
|
|
|
AI::ai_scanner->RescanAIDir();
|
|
|
|
ResetConfig();
|
|
|
|
}
|