Changeset - r14202:008944a6e34c
[Not reviewed]
master
0 5 0
rubidium - 14 years ago 2010-01-09 14:41:22
rubidium@openttd.org
(svn r18763) -Feature [FS#3095]: rerandomise AIs on reloading (via the debug window) when they were randomly chosen
5 files changed with 36 insertions and 16 deletions:
0 comments (0 inline, 0 general)
src/ai/ai.hpp
Show inline comments
 
@@ -45,8 +45,9 @@ public:
 
	/**
 
	 * 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.
src/ai/ai_config.cpp
Show inline comments
 
@@ -16,12 +16,13 @@
 
#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);
 
@@ -56,6 +57,7 @@ AIConfig::AIConfig(const AIConfig *confi
 
	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;
 
@@ -167,6 +169,11 @@ 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;
src/ai/ai_config.hpp
Show inline comments
 
@@ -25,7 +25,8 @@ public:
 
		name(NULL),
 
		version(-1),
 
		info(NULL),
 
		config_list(NULL)
 
		config_list(NULL),
 
		is_random_ai(false)
 
	{}
 
	AIConfig(const AIConfig *config);
 
	~AIConfig();
 
@@ -34,8 +35,9 @@ public:
 
	 * 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
 
@@ -90,6 +92,11 @@ public:
 
	bool HasAI() const;
 

	
 
	/**
 
	 * Is the current AI a randomly chosen AI?
 
	 */
 
	bool IsRandomAI() const;
 

	
 
	/**
 
	 * Get the name of the AI.
 
	 */
 
	const char *GetName() const;
 
@@ -117,6 +124,7 @@ private:
 
	class AIInfo *info;
 
	SettingValueList settings;
 
	AIConfigItemList *config_list;
 
	bool is_random_ai;
 
};
 

	
 
#endif /* AI_CONFIG_HPP */
src/ai/ai_core.cpp
Show inline comments
 
@@ -32,19 +32,20 @@
 
	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;
src/saveload/ai_sl.cpp
Show inline comments
 
@@ -22,12 +22,14 @@
 
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_STR(_ai_saveload_name,        SLE_STRB),
 
	    SLEG_STR(_ai_saveload_settings,    SLE_STRB),
 
	SLEG_CONDVAR(_ai_saveload_version,   SLE_UINT32, 108, SL_MAX_VERSION),
 
	SLEG_CONDVAR(_ai_saveload_is_random,   SLE_BOOL, 136, SL_MAX_VERSION),
 
	     SLE_END()
 
};
 

	
 
static void SaveReal_AIPL(int *index_ptr)
 
@@ -44,6 +46,7 @@ static void SaveReal_AIPL(int *index_ptr
 
		_ai_saveload_version = -1;
 
	}
 

	
 
	_ai_saveload_is_random = config->IsRandomAI();
 
	_ai_saveload_settings[0] = '\0';
 
	config->SettingsToString(_ai_saveload_settings, lengthof(_ai_saveload_settings));
 

	
 
@@ -72,13 +75,13 @@ static void Load_AIPL()
 
		AIConfig *config = AIConfig::GetConfig(index);
 
		if (StrEmpty(_ai_saveload_name)) {
 
			/* A random AI. */
 
			config->ChangeAI(NULL);
 
			config->ChangeAI(NULL, -1, true);
 
		} else {
 
			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 (!config->HasAI()) {
 
					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);
 
@@ -101,7 +104,7 @@ static void Load_AIPL()
 

	
 
		/* 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);
 
		}
 
	}
0 comments (0 inline, 0 general)