Skip to content

Commit

Permalink
Remove: [Script] random_deviation from setting description table (Ope…
Browse files Browse the repository at this point in the history
  • Loading branch information
glx22 authored Mar 5, 2024
1 parent 0fd576b commit 845b894
Show file tree
Hide file tree
Showing 14 changed files with 28 additions and 137 deletions.
3 changes: 1 addition & 2 deletions src/ai/ai.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ class AI {
/**
* Start a new AI company.
* @param company At which slot the AI company should start.
* @param deviate Whether to apply random deviation to the configured AI.
*/
static void StartNew(CompanyID company, bool deviate = true);
static void StartNew(CompanyID company);

/**
* Called every game-tick to let AIs do something.
Expand Down
5 changes: 2 additions & 3 deletions src/ai/ai_core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
return !_networking || (_network_server && _settings_game.ai.ai_in_multiplayer);
}

/* static */ void AI::StartNew(CompanyID company, bool deviate)
/* static */ void AI::StartNew(CompanyID company)
{
assert(Company::IsValidID(company));

Expand All @@ -56,7 +56,6 @@
/* Load default data and store the name in the settings */
config->Change(info->GetName(), -1, false);
}
if (deviate) config->AddRandomDeviation(company);
config->AnchorUnchangeableSettings();

c->ai_info = info;
Expand Down Expand Up @@ -221,7 +220,7 @@
* killing the offending AI we start a random other one in it's place, just
* like what would happen if the AI was missing during loading. */
AI::Stop(c);
AI::StartNew(c, false);
AI::StartNew(c);
}
} else if (Company::IsValidAiID(c)) {
/* Update the reference in the Company struct. */
Expand Down
3 changes: 1 addition & 2 deletions src/game/game.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@ class Game {

/**
* Start up a new GameScript.
* @param deviate Whether to apply random deviation to the configured GameScript.
*/
static void StartNew(bool deviate = true);
static void StartNew();

/**
* Uninitialize the Game system.
Expand Down
3 changes: 1 addition & 2 deletions src/game/game_core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
}
}

/* static */ void Game::StartNew(bool deviate)
/* static */ void Game::StartNew()
{
if (Game::instance != nullptr) return;

Expand All @@ -83,7 +83,6 @@
GameInfo *info = config->GetInfo();
if (info == nullptr) return;

if (deviate) config->AddRandomDeviation(OWNER_DEITY);
config->AnchorUnchangeableSettings();

Backup<CompanyID> cur_company(_current_company, FILE_LINE);
Expand Down
46 changes: 8 additions & 38 deletions src/game/game_gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,18 +199,10 @@ struct GSConfigWindow : public Window {
TextColour colour;
uint idx = 0;
if (config_item.description.empty()) {
if (Game::GetInstance() == nullptr && config_item.random_deviation != 0) {
str = STR_AI_SETTINGS_JUST_DEVIATION;
} else {
str = STR_JUST_STRING1;
}
str = STR_JUST_STRING1;
colour = TC_ORANGE;
} else {
if (Game::GetInstance() == nullptr && config_item.random_deviation != 0) {
str = STR_AI_SETTINGS_SETTING_DEVIATION;
} else {
str = STR_AI_SETTINGS_SETTING;
}
str = STR_AI_SETTINGS_SETTING;
colour = TC_LIGHT_BLUE;
SetDParamStr(idx++, config_item.description);
}
Expand All @@ -225,35 +217,13 @@ struct GSConfigWindow : public Window {
DrawArrowButtons(br.left, y + button_y_offset, COLOUR_YELLOW, (this->clicked_button == i) ? 1 + (this->clicked_increase != rtl) : 0, editable && current_value > config_item.min_value, editable && current_value < config_item.max_value);
}

if (Game::GetInstance() != nullptr || config_item.random_deviation == 0) {
auto config_iterator = config_item.labels.find(current_value);
if (config_iterator != config_item.labels.end()) {
SetDParam(idx++, STR_JUST_RAW_STRING);
SetDParamStr(idx++, config_iterator->second);
} else {
SetDParam(idx++, STR_JUST_INT);
SetDParam(idx++, current_value);
}
auto config_iterator = config_item.labels.find(current_value);
if (config_iterator != config_item.labels.end()) {
SetDParam(idx++, STR_JUST_RAW_STRING);
SetDParamStr(idx++, config_iterator->second);
} else {
int min_deviated = std::max(config_item.min_value, current_value - config_item.random_deviation);
auto config_iterator = config_item.labels.find(min_deviated);
if (config_iterator != config_item.labels.end()) {
SetDParam(idx++, STR_JUST_RAW_STRING);
SetDParamStr(idx++, config_iterator->second);
} else {
SetDParam(idx++, STR_JUST_INT);
SetDParam(idx++, min_deviated);
}

int max_deviated = std::min(config_item.max_value, current_value + config_item.random_deviation);
config_iterator = config_item.labels.find(max_deviated);
if (config_iterator != config_item.labels.end()) {
SetDParam(idx++, STR_JUST_RAW_STRING);
SetDParamStr(idx++, config_iterator->second);
} else {
SetDParam(idx++, STR_JUST_INT);
SetDParam(idx++, max_deviated);
}
SetDParam(idx++, STR_JUST_INT);
SetDParam(idx++, current_value);
}
}

Expand Down
2 changes: 0 additions & 2 deletions src/lang/english.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4843,8 +4843,6 @@ STR_AI_SETTINGS_CAPTION_GAMESCRIPT :Game Script
STR_AI_SETTINGS_CLOSE :{BLACK}Close
STR_AI_SETTINGS_RESET :{BLACK}Reset
STR_AI_SETTINGS_SETTING :{RAW_STRING}: {ORANGE}{STRING1}
STR_AI_SETTINGS_SETTING_DEVIATION :{RAW_STRING}: {ORANGE}[{STRING1}, {STRING1}]
STR_AI_SETTINGS_JUST_DEVIATION :[{STRING1}, {STRING1}]


# Textfile window
Expand Down
4 changes: 2 additions & 2 deletions src/saveload/afterload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -547,11 +547,11 @@ static void StartScripts()

/* Start the AIs. */
for (const Company *c : Company::Iterate()) {
if (Company::IsValidAiID(c->index)) AI::StartNew(c->index, false);
if (Company::IsValidAiID(c->index)) AI::StartNew(c->index);
}

/* Start the GameScript. */
Game::StartNew(false);
Game::StartNew();

ShowScriptDebugWindowIfScriptError();
}
Expand Down
1 change: 1 addition & 0 deletions src/script/api/ai_changelog.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
* API removals:
* \li AIError::ERR_PRECONDITION_TOO_MANY_PARAMETERS, that error is never returned anymore.
* \li AIInfo::CONFIG_RANDOM, no longer used.
* \li AIInfo::AddSettings random_deviation is no longer used.
*
* Other changes:
* \li AIGroupList accepts an optional filter function
Expand Down
1 change: 1 addition & 0 deletions src/script/api/game_changelog.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
* API removals:
* \li GSError::ERR_PRECONDITION_TOO_MANY_PARAMETERS, that error is never returned anymore.
* \li GSInfo::CONFIG_RANDOM, no longer used.
* \li GSInfo::AddSettings random_deviation is no longer used.
*
* Other changes:
* \li GSGroupList accepts an optional filter function
Expand Down
6 changes: 0 additions & 6 deletions src/script/api/script_info_docs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,12 +224,6 @@ class ScriptInfo {
* clamped in the range [MIN(int32_t), MAX(int32_t)] (inclusive).
* - default_value The default value. Required. The value will be
* clamped in the range [MIN(int32_t), MAX(int32_t)] (inclusive).
* - random_deviation If this property has a nonzero value, then the
* actual value of the setting in game will be randomised in the range
* [user_configured_value - random_deviation, user_configured_value + random_deviation] (inclusive).
* random_deviation sign is ignored and the value is clamped in the range [0, MAX(int32_t)] (inclusive).
* The randomisation will happen just before the Script start.
* Not allowed if the CONFIG_BOOLEAN flag is set, otherwise optional.
* - step_size The increase/decrease of the value every time the user
* clicks one of the up/down arrow buttons. Optional, default is 1.
* - flags Bitmask of some flags, see ScriptConfigFlags. Required.
Expand Down
9 changes: 0 additions & 9 deletions src/script/script_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,6 @@ void ScriptConfig::ResetEditableSettings(bool yet_to_start)
}
}

void ScriptConfig::AddRandomDeviation(CompanyID owner)
{
for (const auto &item : *this->GetConfigList()) {
if (item.random_deviation != 0) {
this->SetSetting(item.name, ScriptObject::GetRandomizer(owner).Next(item.random_deviation * 2 + 1) - item.random_deviation + this->GetSetting(item.name));
}
}
}

bool ScriptConfig::HasScript() const
{
return this->info != nullptr;
Expand Down
6 changes: 0 additions & 6 deletions src/script/script_config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ struct ScriptConfigItem {
int min_value = 0; ///< The minimal value this configuration setting can have.
int max_value = 1; ///< The maximal value this configuration setting can have.
int default_value = 0; ///< The default value of this configuration setting.
int random_deviation = 0; ///< The maximum random deviation from the default value.
int step_size = 1; ///< The step size in the gui.
ScriptConfigFlags flags = SCRIPTCONFIG_NONE; ///< Flags for the configuration setting.
LabelMapping labels; ///< Text labels for the integer values.
Expand Down Expand Up @@ -132,11 +131,6 @@ class ScriptConfig {
*/
void ResetEditableSettings(bool yet_to_start);

/**
* Randomize all settings the Script requested to be randomized.
*/
void AddRandomDeviation(CompanyID owner);

/**
* Is this config attached to an Script? In other words, is there a Script
* that is assigned to this slot.
Expand Down
46 changes: 8 additions & 38 deletions src/script/script_gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -377,18 +377,10 @@ struct ScriptSettingsWindow : public Window {
TextColour colour;
uint idx = 0;
if (config_item.description.empty()) {
if (this->slot != OWNER_DEITY && !Company::IsValidID(this->slot) && config_item.random_deviation != 0) {
str = STR_AI_SETTINGS_JUST_DEVIATION;
} else {
str = STR_JUST_STRING1;
}
str = STR_JUST_STRING1;
colour = TC_ORANGE;
} else {
if (this->slot != OWNER_DEITY && !Company::IsValidID(this->slot) && config_item.random_deviation != 0) {
str = STR_AI_SETTINGS_SETTING_DEVIATION;
} else {
str = STR_AI_SETTINGS_SETTING;
}
str = STR_AI_SETTINGS_SETTING;
colour = TC_LIGHT_BLUE;
SetDParamStr(idx++, config_item.description);
}
Expand All @@ -403,35 +395,13 @@ struct ScriptSettingsWindow : public Window {
DrawArrowButtons(br.left, y + button_y_offset, COLOUR_YELLOW, (this->clicked_button == i) ? 1 + (this->clicked_increase != rtl) : 0, editable && current_value > config_item.min_value, editable && current_value < config_item.max_value);
}

if (this->slot == OWNER_DEITY || Company::IsValidID(this->slot) || config_item.random_deviation == 0) {
auto config_iterator = config_item.labels.find(current_value);
if (config_iterator != config_item.labels.end()) {
SetDParam(idx++, STR_JUST_RAW_STRING);
SetDParamStr(idx++, config_iterator->second);
} else {
SetDParam(idx++, STR_JUST_INT);
SetDParam(idx++, current_value);
}
auto config_iterator = config_item.labels.find(current_value);
if (config_iterator != config_item.labels.end()) {
SetDParam(idx++, STR_JUST_RAW_STRING);
SetDParamStr(idx++, config_iterator->second);
} else {
int min_deviated = std::max(config_item.min_value, current_value - config_item.random_deviation);
auto config_iterator = config_item.labels.find(min_deviated);
if (config_iterator != config_item.labels.end()) {
SetDParam(idx++, STR_JUST_RAW_STRING);
SetDParamStr(idx++, config_iterator->second);
} else {
SetDParam(idx++, STR_JUST_INT);
SetDParam(idx++, min_deviated);
}

int max_deviated = std::min(config_item.max_value, current_value + config_item.random_deviation);
config_iterator = config_item.labels.find(max_deviated);
if (config_iterator != config_item.labels.end()) {
SetDParam(idx++, STR_JUST_RAW_STRING);
SetDParamStr(idx++, config_iterator->second);
} else {
SetDParam(idx++, STR_JUST_INT);
SetDParam(idx++, max_deviated);
}
SetDParam(idx++, STR_JUST_INT);
SetDParam(idx++, current_value);
}
}

Expand Down
30 changes: 3 additions & 27 deletions src/script/script_info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,7 @@ SQInteger ScriptInfo::AddSetting(HSQUIRRELVM vm)
ScriptConfigItem config;
uint items = 0;

int easy_value = INT32_MIN;
int medium_value = INT32_MIN;
int hard_value = INT32_MIN;

/* Read the table, and find all properties we care about */
sq_pushnull(vm);
Expand Down Expand Up @@ -124,19 +122,15 @@ SQInteger ScriptInfo::AddSetting(HSQUIRRELVM vm)
config.max_value = ClampTo<int32_t>(res);
items |= 0x008;
} else if (key == "easy_value") {
SQInteger res;
if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
easy_value = ClampTo<int32_t>(res);
// No longer parsed.
items |= 0x010;
} else if (key == "medium_value") {
SQInteger res;
if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
medium_value = ClampTo<int32_t>(res);
items |= 0x020;
} else if (key == "hard_value") {
SQInteger res;
if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
hard_value = ClampTo<int32_t>(res);
// No longer parsed.
items |= 0x040;
} else if (key == "custom_value") {
// No longer parsed.
Expand All @@ -146,10 +140,7 @@ SQInteger ScriptInfo::AddSetting(HSQUIRRELVM vm)
config.default_value = ClampTo<int32_t>(res);
items |= 0x080;
} else if (key == "random_deviation") {
SQInteger res;
if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
config.random_deviation = ClampTo<int32_t>(abs(res));
items |= 0x200;
// No longer parsed.
} else if (key == "step_size") {
SQInteger res;
if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
Expand Down Expand Up @@ -179,27 +170,12 @@ SQInteger ScriptInfo::AddSetting(HSQUIRRELVM vm)
}

config.default_value = medium_value;
/* If not boolean and no random deviation set, calculate it based on easy/hard difference. */
if ((config.flags & SCRIPTCONFIG_BOOLEAN) == 0 && (items & 0x200) == 0) {
config.random_deviation = abs(hard_value - easy_value) / 2;
items |= 0x200;
}
items |= 0x080;
} else {
/* For compatibility, also act like the default sets the easy/medium/hard. */
items |= 0x010 | 0x020 | 0x040;
}

/* Don't allow both random_deviation and SCRIPTCONFIG_BOOLEAN to
* be set for the same config item. */
if ((items & 0x200) != 0 && (config.flags & SCRIPTCONFIG_BOOLEAN) != 0) {
this->engine->ThrowError("setting both random_deviation and CONFIG_BOOLEAN is not allowed");
return SQ_ERROR;
}

/* Reset the bit for random_deviation as it's optional. */
items &= ~0x200;

/* Make sure all properties are defined */
uint mask = (config.flags & SCRIPTCONFIG_BOOLEAN) ? 0x1F3 : 0x1FF;
if (items != mask) {
Expand Down

0 comments on commit 845b894

Please sign in to comment.