Files
@ r26745:baeac55e5810
Branch filter:
Location: cpp/openttd-patchpack/source/src/script/script_config.cpp
r26745:baeac55e5810
6.9 KiB
text/x-c
Codechange: use smart pointers when creating StringIterators
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 | /*
* 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 "../textfile_gui.h"
#include "../string_func.h"
#include "../safeguards.h"
void ScriptConfig::Change(const char *name, int version, bool force_exact_match, bool is_random)
{
free(this->name);
this->name = (name == nullptr) ? nullptr : stredup(name);
this->info = (name == nullptr) ? nullptr : this->FindInfo(this->name, version, force_exact_match);
this->version = (info == nullptr) ? -1 : info->GetVersion();
this->is_random = is_random;
if (this->config_list != nullptr) delete this->config_list;
this->config_list = (info == nullptr) ? nullptr : new ScriptConfigItemList();
if (this->config_list != nullptr) this->PushExtraConfigList();
this->to_load_data.reset();
this->ClearConfigList();
if (_game_mode == GM_NORMAL && this->info != nullptr) {
/* 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 (const auto &item : *this->info->GetConfigList()) {
if (item.flags & SCRIPTCONFIG_RANDOM) {
this->SetSetting(item.name, InteractiveRandomRange(item.max_value + 1 - item.min_value) + item.min_value);
}
}
this->AddRandomDeviation();
}
}
ScriptConfig::ScriptConfig(const ScriptConfig *config)
{
this->name = (config->name == nullptr) ? nullptr : stredup(config->name);
this->info = config->info;
this->version = config->version;
this->config_list = nullptr;
this->is_random = config->is_random;
this->to_load_data.reset();
for (const auto &item : config->settings) {
this->settings[stredup(item.first)] = item.second;
}
/* Virtual functions get called statically in constructors, so make it explicit to remove any confusion. */
this->ScriptConfig::AddRandomDeviation();
}
ScriptConfig::~ScriptConfig()
{
free(this->name);
this->ResetSettings();
if (this->config_list != nullptr) delete this->config_list;
this->to_load_data.reset();
}
ScriptInfo *ScriptConfig::GetInfo() const
{
return this->info;
}
const ScriptConfigItemList *ScriptConfig::GetConfigList()
{
if (this->info != nullptr) return this->info->GetConfigList();
if (this->config_list == nullptr) {
this->config_list = new ScriptConfigItemList();
this->PushExtraConfigList();
}
return this->config_list;
}
void ScriptConfig::ClearConfigList()
{
for (const auto &item : this->settings) {
free(item.first);
}
this->settings.clear();
}
void ScriptConfig::AnchorUnchangeableSettings()
{
for (const auto &item : *this->GetConfigList()) {
if ((item.flags & SCRIPTCONFIG_INGAME) == 0) {
this->SetSetting(item.name, this->GetSetting(item.name));
}
}
}
int ScriptConfig::GetSetting(const char *name) const
{
const auto 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 == nullptr) return;
const ScriptConfigItem *config_item = this->info->GetConfigItem(name);
if (config_item == nullptr) return;
value = Clamp(value, config_item->min_value, config_item->max_value);
const auto it = this->settings.find(name);
if (it != this->settings.end()) {
(*it).second = value;
} else {
this->settings[stredup(name)] = value;
}
}
void ScriptConfig::ResetSettings()
{
for (const auto &item : this->settings) {
free(item.first);
}
this->settings.clear();
}
void ScriptConfig::ResetEditableSettings(bool yet_to_start)
{
if (this->info == nullptr) return ResetSettings();
for (SettingValueList::iterator it = this->settings.begin(); it != this->settings.end();) {
const ScriptConfigItem *config_item = this->info->GetConfigItem(it->first);
assert(config_item != nullptr);
bool editable = yet_to_start || (config_item->flags & SCRIPTCONFIG_INGAME) != 0;
bool visible = _settings_client.gui.ai_developer_tools || (config_item->flags & SCRIPTCONFIG_DEVELOPER) == 0;
if (editable && visible) {
free(it->first);
it = this->settings.erase(it);
} else {
it++;
}
}
}
void ScriptConfig::AddRandomDeviation()
{
for (const auto &item : *this->GetConfigList()) {
if (item.random_deviation != 0) {
this->SetSetting(item.name, InteractiveRandomRange(item.random_deviation * 2 + 1) - item.random_deviation + this->GetSetting(item.name));
}
}
}
bool ScriptConfig::HasScript() const
{
return this->info != nullptr;
}
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 std::string &value)
{
char *value_copy = stredup(value.c_str());
char *s = value_copy;
while (s != nullptr) {
/* Analyze the string ('name=value,name=value\0') */
char *item_name = s;
s = strchr(s, '=');
if (s == nullptr) break;
if (*s == '\0') break;
*s = '\0';
s++;
char *item_value = s;
s = strchr(s, ',');
if (s != nullptr) {
*s = '\0';
s++;
}
this->SetSetting(item_name, atoi(item_value));
}
free(value_copy);
}
std::string ScriptConfig::SettingsToString() const
{
char string[1024];
char *last = lastof(string);
char *s = string;
*s = '\0';
for (const auto &item : this->settings) {
char no[10];
seprintf(no, lastof(no), "%d", item.second);
/* Check if the string would fit in the destination */
size_t needed_size = strlen(item.first) + 1 + strlen(no);
/* If it doesn't fit, skip the next settings */
if (s + needed_size > last) break;
s = strecat(s, item.first, last);
s = strecat(s, "=", last);
s = strecat(s, no, last);
s = strecat(s, ",", last);
}
/* Remove the last ',', but only if at least one setting was saved. */
if (s != string) s[-1] = '\0';
return string;
}
const char *ScriptConfig::GetTextfile(TextfileType type, CompanyID slot) const
{
if (slot == INVALID_COMPANY || this->GetInfo() == nullptr) return nullptr;
return ::GetTextfile(type, (slot == OWNER_DEITY) ? GAME_DIR : AI_DIR, this->GetInfo()->GetMainScript());
}
void ScriptConfig::SetToLoadData(ScriptInstance::ScriptData *data)
{
this->to_load_data.reset(data);
}
ScriptInstance::ScriptData *ScriptConfig::GetToLoadData()
{
return this->to_load_data.get();
}
|