@@ -42,14 +42,15 @@ public:
*/
static bool CanStartNew();
/**
* Start a new AI company.
* @param company At which slot the AI company should start.
* @param rerandomise_ai Whether to rerandomise the configured AI.
static void StartNew(CompanyID company);
static void StartNew(CompanyID company, bool rerandomise_ai = true);
* Called every game-tick to let AIs do something.
static void GameLoop();
@@ -13,18 +13,19 @@
#include "../openttd.h"
#include "../settings_type.h"
#include "../core/random_func.hpp"
#include "ai.hpp"
#include "ai_config.hpp"
void AIConfig::ChangeAI(const char *name, int version)
void AIConfig::ChangeAI(const char *name, int version, bool is_random_ai)
{
free((void *)this->name);
this->name = (name == NULL) ? NULL : strdup(name);
this->info = (name == NULL) ? NULL : AI::FindInfo(this->name, version);
this->version = (info == NULL) ? -1 : info->GetVersion();
this->is_random_ai = is_random_ai;
if (this->config_list != NULL) delete this->config_list;
this->config_list = (info == NULL) ? NULL : new AIConfigItemList();
if (this->config_list != NULL) this->config_list->push_back(_start_date_config);
/* The special casing for start_date is here to ensure that the
* start_date setting won't change even if you chose another AI. */
@@ -53,12 +54,13 @@ void AIConfig::ChangeAI(const char *name
AIConfig::AIConfig(const AIConfig *config)
this->name = (config->name == NULL) ? NULL : strdup(config->name);
this->info = config->info;
this->version = config->version;
this->config_list = NULL;
this->is_random_ai = config->is_random_ai;
for (SettingValueList::const_iterator it = config->settings.begin(); it != config->settings.end(); it++) {
this->settings[strdup((*it).first)] = (*it).second;
}
this->AddRandomDeviation();
@@ -164,12 +166,17 @@ void AIConfig::AddRandomDeviation()
bool AIConfig::HasAI() const
return this->info != NULL;
bool AIConfig::IsRandomAI() const
return this->is_random_ai;
const char *AIConfig::GetName() const
return this->name;
int AIConfig::GetVersion() const
@@ -22,23 +22,25 @@ private:
public:
AIConfig() :
name(NULL),
version(-1),
info(NULL),
config_list(NULL)
config_list(NULL),
is_random_ai(false)
{}
AIConfig(const AIConfig *config);
~AIConfig();
* Set another AI to be loaded in this slot.
* @param name The name of the AI.
* @param version The version of the AI to load, or -1 of latest.
* @param is_random Is the AI chosen randomly?
void ChangeAI(const char *name, int version = -1);
void ChangeAI(const char *name, int version = -1, bool is_random = false);
* When ever the AI Scanner is reloaded, all infos become invalid. This
* function tells AIConfig about this.
* @return True if the reset was successfull, false if the AI was no longer
* found.
@@ -87,12 +89,17 @@ public:
* Is this config attached to an AI?
bool HasAI() const;
* Is the current AI a randomly chosen AI?
bool IsRandomAI() const;
* Get the name of the AI.
const char *GetName() const;
* Get the version of the AI.
@@ -114,9 +121,10 @@ public:
private:
const char *name;
int version;
class AIInfo *info;
SettingValueList settings;
AIConfigItemList *config_list;
bool is_random_ai;
};
#endif /* AI_CONFIG_HPP */
@@ -29,25 +29,26 @@
/* 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)
/* static */ void AI::StartNew(CompanyID company, bool rerandomise_ai)
assert(Company::IsValidID(company));
/* Clients shouldn't start AIs */
if (_networking && !_network_server) return;
AIInfo *info = AIConfig::GetConfig(company)->GetInfo();
if (info == NULL) {
AIConfig *config = AIConfig::GetConfig(company);
AIInfo *info = config->GetInfo();
if (info == NULL || (rerandomise_ai && config->IsRandomAI())) {
info = AI::ai_scanner->SelectRandomAI();
assert(info != NULL);
/* Load default data and store the name in the settings */
AIConfig::GetConfig(company)->ChangeAI(info->GetName());
config->ChangeAI(info->GetName(), -1, true);
_current_company = company;
Company *c = Company::Get(company);
c->ai_info = info;
@@ -19,18 +19,20 @@
#include "../network/network.h"
#include "../ai/ai_instance.hpp"
static char _ai_saveload_name[64];
static int _ai_saveload_version;
static char _ai_saveload_settings[1024];
static bool _ai_saveload_is_random;
static const SaveLoad _ai_company[] = {
SLEG_STR(_ai_saveload_name, SLE_STRB),
SLEG_STR(_ai_saveload_settings, SLE_STRB),
SLEG_CONDVAR(_ai_saveload_version, SLE_UINT32, 108, SL_MAX_VERSION),
SLE_END()
SLEG_CONDVAR(_ai_saveload_is_random, SLE_BOOL, 136, SL_MAX_VERSION),
static void SaveReal_AIPL(int *index_ptr)
CompanyID index = (CompanyID)*index_ptr;
AIConfig *config = AIConfig::GetConfig(index);
@@ -41,12 +43,13 @@ static void SaveReal_AIPL(int *index_ptr
} else {
/* No AI is configured for this so store an empty string as name. */
_ai_saveload_name[0] = '\0';
_ai_saveload_version = -1;
_ai_saveload_is_random = config->IsRandomAI();
_ai_saveload_settings[0] = '\0';
config->SettingsToString(_ai_saveload_settings, lengthof(_ai_saveload_settings));
SlObject(NULL, _ai_company);
/* If the AI was active, store his data too */
if (Company::IsValidAiID(index)) AI::Save(index);
@@ -69,19 +72,19 @@ static void Load_AIPL()
continue;
if (StrEmpty(_ai_saveload_name)) {
/* A random AI. */
config->ChangeAI(NULL);
config->ChangeAI(NULL, -1, true);
config->ChangeAI(_ai_saveload_name, _ai_saveload_version);
config->ChangeAI(_ai_saveload_name, _ai_saveload_version, _ai_saveload_is_random);
if (!config->HasAI()) {
/* No version of the AI available that can load the data. Try to load the
* latest version of the AI instead. */
config->ChangeAI(_ai_saveload_name, -1);
config->ChangeAI(_ai_saveload_name, -1, _ai_saveload_is_random);
if (strcmp(_ai_saveload_name, "%_dummy") != 0) {
DEBUG(ai, 0, "The savegame has an AI by the name '%s', version %d which is no longer available.", _ai_saveload_name, _ai_saveload_version);
DEBUG(ai, 0, "A random other AI will be loaded in its place.");
DEBUG(ai, 0, "The savegame had no AIs available at the time of saving.");
@@ -98,13 +101,13 @@ static void Load_AIPL()
config->StringToSettings(_ai_saveload_settings);
/* Start the AI directly if it was active in the savegame */
if (Company::IsValidAiID(index)) {
AI::StartNew(index);
AI::StartNew(index, false);
AI::Load(index, _ai_saveload_version);
static void Save_AIPL()
Status change: