(svn r23364) -Codechange: refactor AIConfig, moving it mostly to Scriptconfig
parent
c38c16773c
commit
34d7f01ccc
@ -0,0 +1,201 @@
|
||||
/* $Id$ */
|
||||
|
||||
/*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
/** @file script_config.cpp Implementation of ScriptConfig. */
|
||||
|
||||
#include "../stdafx.h"
|
||||
#include "../settings_type.h"
|
||||
#include "../core/random_func.hpp"
|
||||
#include "script_info.hpp"
|
||||
#include "script_config.hpp"
|
||||
|
||||
void ScriptConfig::Change(const char *name, int version, bool force_exact_match, bool is_random)
|
||||
{
|
||||
free(this->name);
|
||||
this->name = (name == NULL) ? NULL : strdup(name);
|
||||
this->info = (name == NULL) ? NULL : this->FindInfo(this->name, version, force_exact_match);
|
||||
this->version = (info == NULL) ? -1 : info->GetVersion();
|
||||
this->is_random = is_random;
|
||||
if (this->config_list != NULL) delete this->config_list;
|
||||
this->config_list = (info == NULL) ? NULL : new ScriptConfigItemList();
|
||||
if (this->config_list != NULL) this->PushExtraConfigList();
|
||||
|
||||
this->ClearConfigList();
|
||||
|
||||
if (_game_mode == GM_NORMAL && this->info != NULL) {
|
||||
/* If we're in an existing game and the Script is changed, set all settings
|
||||
* for the Script that have the random flag to a random value. */
|
||||
for (ScriptConfigItemList::const_iterator it = this->info->GetConfigList()->begin(); it != this->info->GetConfigList()->end(); it++) {
|
||||
if ((*it).flags & SCRIPTCONFIG_RANDOM) {
|
||||
this->SetSetting((*it).name, InteractiveRandomRange((*it).max_value - (*it).min_value) + (*it).min_value);
|
||||
}
|
||||
}
|
||||
this->AddRandomDeviation();
|
||||
}
|
||||
}
|
||||
|
||||
ScriptConfig::ScriptConfig(const ScriptConfig *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 = config->is_random;
|
||||
|
||||
for (SettingValueList::const_iterator it = config->settings.begin(); it != config->settings.end(); it++) {
|
||||
this->settings[strdup((*it).first)] = (*it).second;
|
||||
}
|
||||
this->AddRandomDeviation();
|
||||
}
|
||||
|
||||
ScriptConfig::~ScriptConfig()
|
||||
{
|
||||
free(this->name);
|
||||
this->ResetSettings();
|
||||
if (this->config_list != NULL) delete this->config_list;
|
||||
}
|
||||
|
||||
ScriptInfo *ScriptConfig::GetInfo() const
|
||||
{
|
||||
return this->info;
|
||||
}
|
||||
|
||||
const ScriptConfigItemList *ScriptConfig::GetConfigList()
|
||||
{
|
||||
if (this->info != NULL) return this->info->GetConfigList();
|
||||
if (this->config_list == NULL) {
|
||||
this->config_list = new ScriptConfigItemList();
|
||||
this->PushExtraConfigList();
|
||||
}
|
||||
return this->config_list;
|
||||
}
|
||||
|
||||
void ScriptConfig::ClearConfigList()
|
||||
{
|
||||
for (SettingValueList::iterator it = this->settings.begin(); it != this->settings.end(); it++) {
|
||||
free((*it).first);
|
||||
}
|
||||
this->settings.clear();
|
||||
}
|
||||
|
||||
int ScriptConfig::GetSetting(const char *name) const
|
||||
{
|
||||
/* Return default values if the difficulty is not set to Custom */
|
||||
if (GetGameSettings().difficulty.diff_level != 3) {
|
||||
return this->info->GetSettingDefaultValue(name);
|
||||
}
|
||||
|
||||
SettingValueList::const_iterator it = this->settings.find(name);
|
||||
if (it == this->settings.end()) return this->info->GetSettingDefaultValue(name);
|
||||
return (*it).second;
|
||||
}
|
||||
|
||||
void ScriptConfig::SetSetting(const char *name, int value)
|
||||
{
|
||||
/* You can only set Script specific settings if an Script is selected. */
|
||||
if (this->info == NULL) return;
|
||||
|
||||
const ScriptConfigItem *config_item = this->info->GetConfigItem(name);
|
||||
if (config_item == NULL) return;
|
||||
|
||||
value = Clamp(value, config_item->min_value, config_item->max_value);
|
||||
|
||||
SettingValueList::iterator it = this->settings.find(name);
|
||||
if (it != this->settings.end()) {
|
||||
(*it).second = value;
|
||||
} else {
|
||||
this->settings[strdup(name)] = value;
|
||||
}
|
||||
}
|
||||
|
||||
void ScriptConfig::ResetSettings()
|
||||
{
|
||||
for (SettingValueList::iterator it = this->settings.begin(); it != this->settings.end(); it++) {
|
||||
free((*it).first);
|
||||
}
|
||||
this->settings.clear();
|
||||
}
|
||||
|
||||
void ScriptConfig::AddRandomDeviation()
|
||||
{
|
||||
for (ScriptConfigItemList::const_iterator it = this->GetConfigList()->begin(); it != this->GetConfigList()->end(); it++) {
|
||||
if ((*it).random_deviation != 0) {
|
||||
this->SetSetting((*it).name, InteractiveRandomRange((*it).random_deviation * 2) - (*it).random_deviation + this->GetSetting((*it).name));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool ScriptConfig::HasScript() const
|
||||
{
|
||||
return this->info != NULL;
|
||||
}
|
||||
|
||||
bool ScriptConfig::IsRandom() const
|
||||
{
|
||||
return this->is_random;
|
||||
}
|
||||
|
||||
const char *ScriptConfig::GetName() const
|
||||
{
|
||||
return this->name;
|
||||
}
|
||||
|
||||
int ScriptConfig::GetVersion() const
|
||||
{
|
||||
return this->version;
|
||||
}
|
||||
|
||||
void ScriptConfig::StringToSettings(const char *value)
|
||||
{
|
||||
char *value_copy = strdup(value);
|
||||
char *s = value_copy;
|
||||
|
||||
while (s != NULL) {
|
||||
/* Analyze the string ('name=value,name=value\0') */
|
||||
char *item_name = s;
|
||||
s = strchr(s, '=');
|
||||
if (s == NULL) break;
|
||||
if (*s == '\0') break;
|
||||
*s = '\0';
|
||||
s++;
|
||||
|
||||
char *item_value = s;
|
||||
s = strchr(s, ',');
|
||||
if (s != NULL) {
|
||||
*s = '\0';
|
||||
s++;
|
||||
}
|
||||
|
||||
this->SetSetting(item_name, atoi(item_value));
|
||||
}
|
||||
free(value_copy);
|
||||
}
|
||||
|
||||
void ScriptConfig::SettingsToString(char *string, size_t size) const
|
||||
{
|
||||
string[0] = '\0';
|
||||
for (SettingValueList::const_iterator it = this->settings.begin(); it != this->settings.end(); it++) {
|
||||
char no[10];
|
||||
snprintf(no, sizeof(no), "%d", (*it).second);
|
||||
|
||||
/* Check if the string would fit in the destination */
|
||||
size_t needed_size = strlen((*it).first) + 1 + strlen(no) + 1;
|
||||
/* If it doesn't fit, skip the next settings */
|
||||
if (size <= needed_size) break;
|
||||
size -= needed_size;
|
||||
|
||||
strcat(string, (*it).first);
|
||||
strcat(string, "=");
|
||||
strcat(string, no);
|
||||
strcat(string, ",");
|
||||
}
|
||||
/* Remove the last ',', but only if at least one setting was saved. */
|
||||
size_t len = strlen(string);
|
||||
if (len > 0) string[len - 1] = '\0';
|
||||
}
|
@ -0,0 +1,191 @@
|
||||
/* $Id$ */
|
||||
|
||||
/*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
/** @file script_config.hpp ScriptConfig stores the configuration settings of every Script. */
|
||||
|
||||
#ifndef SCRIPT_CONFIG_HPP
|
||||
#define SCRIPT_CONFIG_HPP
|
||||
|
||||
#include <map>
|
||||
#include <list>
|
||||
#include "../core/smallmap_type.hpp"
|
||||
#include "../core/string_compare_type.hpp"
|
||||
#include "../company_type.h"
|
||||
|
||||
/** Bitmask of flags for Script settings. */
|
||||
enum ScriptConfigFlags {
|
||||
SCRIPTCONFIG_NONE = 0x0, ///< No flags set.
|
||||
SCRIPTCONFIG_RANDOM = 0x1, ///< When randomizing the Script, pick any value between min_value and max_value when on custom difficulty setting.
|
||||
SCRIPTCONFIG_BOOLEAN = 0x2, ///< This value is a boolean (either 0 (false) or 1 (true) ).
|
||||
SCRIPTCONFIG_INGAME = 0x4, ///< This setting can be changed while the Script is running.
|
||||
SCRIPTCONFIG_DEVELOPER = 0x8, ///< This setting will only be visible when the Script development tools are active.
|
||||
};
|
||||
|
||||
typedef SmallMap<int, char *> LabelMapping; ///< Map-type used to map the setting numbers to labels.
|
||||
|
||||
/** Info about a single Script setting. */
|
||||
struct ScriptConfigItem {
|
||||
const char *name; ///< The name of the configuration setting.
|
||||
const char *description; ///< The description of the configuration setting.
|
||||
int min_value; ///< The minimal value this configuration setting can have.
|
||||
int max_value; ///< The maximal value this configuration setting can have.
|
||||
int custom_value; ///< The default value on custom difficulty setting.
|
||||
int easy_value; ///< The default value on easy difficulty setting.
|
||||
int medium_value; ///< The default value on medium difficulty setting.
|
||||
int hard_value; ///< The default value on hard difficulty setting.
|
||||
int random_deviation; ///< The maximum random deviation from the default value.
|
||||
int step_size; ///< The step size in the gui.
|
||||
ScriptConfigFlags flags; ///< Flags for the configuration setting.
|
||||
LabelMapping *labels; ///< Text labels for the integer values.
|
||||
};
|
||||
|
||||
typedef std::list<ScriptConfigItem> ScriptConfigItemList; ///< List of ScriptConfig items.
|
||||
|
||||
extern ScriptConfigItem _start_date_config;
|
||||
|
||||
/**
|
||||
* Script settings.
|
||||
*/
|
||||
class ScriptConfig {
|
||||
protected:
|
||||
/** List with name=>value pairs of all script-specific settings */
|
||||
typedef std::map<const char *, int, StringCompare> SettingValueList;
|
||||
|
||||
public:
|
||||
ScriptConfig() :
|
||||
name(NULL),
|
||||
version(-1),
|
||||
info(NULL),
|
||||
config_list(NULL),
|
||||
is_random(false)
|
||||
{}
|
||||
|
||||
/**
|
||||
* Create a new Script config that is a copy of an existing config.
|
||||
* @param config The object to copy.
|
||||
*/
|
||||
ScriptConfig(const ScriptConfig *config);
|
||||
|
||||
/** Delete an Script configuration. */
|
||||
virtual ~ScriptConfig();
|
||||
|
||||
/**
|
||||
* Set another Script to be loaded in this slot.
|
||||
* @param name The name of the Script.
|
||||
* @param version The version of the Script to load, or -1 of latest.
|
||||
* @param force_exact_match If true try to find the exact same version
|
||||
* as specified. If false any compatible version is ok.
|
||||
* @param is_random Is the Script chosen randomly?
|
||||
*/
|
||||
void Change(const char *name, int version = -1, bool force_exact_match = false, bool is_random = false);
|
||||
|
||||
/**
|
||||
* Get the ScriptInfo linked to this ScriptConfig.
|
||||
*/
|
||||
class ScriptInfo *GetInfo() const;
|
||||
|
||||
/**
|
||||
* Get the config list for this ScriptConfig.
|
||||
*/
|
||||
const ScriptConfigItemList *GetConfigList();
|
||||
|
||||
/**
|
||||
* Where to get the config from, either default (depends on current game
|
||||
* mode) or force either newgame or normal
|
||||
*/
|
||||
enum ScriptSettingSource {
|
||||
SSS_DEFAULT, ///< Get the Script config from the current game mode
|
||||
SSS_FORCE_NEWGAME, ///< Get the newgame Script config
|
||||
SSS_FORCE_GAME, ///< Get the Script config from the current game
|
||||
};
|
||||
|
||||
/**
|
||||
* Get the value of a setting for this config. It might fallback to his
|
||||
* 'info' to find the default value (if not set or if not-custom difficulty
|
||||
* level).
|
||||
* @return The (default) value of the setting, or -1 if the setting was not
|
||||
* found.
|
||||
*/
|
||||
virtual int GetSetting(const char *name) const;
|
||||
|
||||
/**
|
||||
* Set the value of a setting for this config.
|
||||
*/
|
||||
virtual void SetSetting(const char *name, int value);
|
||||
|
||||
/**
|
||||
* Reset all settings to their default value.
|
||||
*/
|
||||
void ResetSettings();
|
||||
|
||||
/**
|
||||
* Randomize all settings the Script requested to be randomized.
|
||||
*/
|
||||
void AddRandomDeviation();
|
||||
|
||||
/**
|
||||
* Is this config attached to an Script? In other words, is there a Script
|
||||
* that is assigned to this slot.
|
||||
*/
|
||||
bool HasScript() const;
|
||||
|
||||
/**
|
||||
* Is the current Script a randomly chosen Script?
|
||||
*/
|
||||
bool IsRandom() const;
|
||||
|
||||
/**
|
||||
* Get the name of the Script.
|
||||
*/
|
||||
const char *GetName() const;
|
||||
|
||||
/**
|
||||
* Get the version of the Script.
|
||||
*/
|
||||
int GetVersion() const;
|
||||
|
||||
/**
|
||||
* Convert a string which is stored in the config file or savegames to
|
||||
* custom settings of this Script.
|
||||
*/
|
||||
void StringToSettings(const char *value);
|
||||
|
||||
/**
|
||||
* Convert the custom settings to a string that can be stored in the config
|
||||
* file or savegames.
|
||||
*/
|
||||
void SettingsToString(char *string, size_t size) const;
|
||||
|
||||
protected:
|
||||
const char *name; ///< Name of the Script
|
||||
int version; ///< Version of the Script
|
||||
class ScriptInfo *info; ///< ScriptInfo object for related to this Script version
|
||||
SettingValueList settings; ///< List with all setting=>value pairs that are configure for this Script
|
||||
ScriptConfigItemList *config_list; ///< List with all settings defined by this Script
|
||||
bool is_random; ///< True if the AI in this slot was randomly chosen.
|
||||
|
||||
/**
|
||||
* In case you have mandatory non-Script-definable config entries in your
|
||||
* list, add them to this function.
|
||||
*/
|
||||
virtual void PushExtraConfigList() {};
|
||||
|
||||
/**
|
||||
* Routine that clears the config list.
|
||||
*/
|
||||
virtual void ClearConfigList();
|
||||
|
||||
/**
|
||||
* This function should call back to the Scanner in charge of this Config,
|
||||
* to find the ScriptInfo belonging to a name+version.
|
||||
*/
|
||||
virtual ScriptInfo *FindInfo(const char *name, int version, bool force_exact_match) = 0;
|
||||
};
|
||||
|
||||
#endif /* SCRIPT_CONFIG_HPP */
|
Loading…
Reference in New Issue