Update to 13.1 (github source package)

This commit is contained in:
dP
2023-04-10 23:55:14 +04:00
parent 5f920a122d
commit 9743ba7d99
89 changed files with 1008 additions and 520 deletions

View File

@@ -218,20 +218,27 @@ public:
* store the current configuration of Scripts. Required.
* - description A single line describing the setting. Required.
* - min_value The minimum value of this setting. Required for integer
* settings and not allowed for boolean settings.
* settings and not allowed for boolean settings. The value will be
* clamped in the range [MIN(int32), MAX(int32)] (inclusive).
* - max_value The maximum value of this setting. Required for integer
* settings and not allowed for boolean settings.
* settings and not allowed for boolean settings. The value will be
* clamped in the range [MIN(int32), MAX(int32)] (inclusive).
* - easy_value The default value if the easy difficulty level
* is selected. Required.
* is selected. Required. The value will be clamped in the range
* [MIN(int32), MAX(int32)] (inclusive).
* - medium_value The default value if the medium difficulty level
* is selected. Required.
* is selected. Required. The value will be clamped in the range
* [MIN(int32), MAX(int32)] (inclusive).
* - hard_value The default value if the hard difficulty level
* is selected. Required.
* is selected. Required. The value will be clamped in the range
* [MIN(int32), MAX(int32)] (inclusive).
* - custom_value The default value if the custom difficulty level
* is selected. Required.
* is selected. Required. The value will be clamped in the range
* [MIN(int32), MAX(int32)] (inclusive).
* - random_deviation If this property has a nonzero value, then the
* actual value of the setting in game will be randomized 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)] (inclusive).
* Not allowed if the CONFIG_RANDOM 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.
@@ -247,13 +254,16 @@ public:
* user will see the corresponding name.
* @param setting_name The name of the setting.
* @param value_names A table that maps values to names. The first
* character of every identifier is ignored and the rest should
* character of every identifier is ignored, the second character
* could be '_' to indicate the value is negative, and the rest should
* be an integer of the value you define a name for. The value
* is a short description of that value.
* To define labels for a setting named "competition_level" you could
* for example call it like this:
* AddLabels("competition_level", {_0 = "no competition", _1 = "some competition",
* _2 = "a lot of competition"});
* Another example, for a setting with a negative value:
* AddLabels("amount", {__1 = "less than one", _0 = "none", _1 = "more than one"});
*
* @note This is a function provided by OpenTTD, you don't have to
* include it in your Script but should just call it from GetSettings.

View File

@@ -82,20 +82,18 @@ ScriptObject::ActiveInstance::~ActiveInstance()
return GetStorage()->mode_instance;
}
/* static */ void ScriptObject::SetLastCommand(TileIndex tile, const CommandDataBuffer &data, Commands cmd)
/* static */ void ScriptObject::SetLastCommand(const CommandDataBuffer &data, Commands cmd)
{
ScriptStorage *s = GetStorage();
Debug(script, 6, "SetLastCommand company={:02d} tile={:06x} cmd={} data={}", s->root_company, tile, cmd, FormatArrayAsHex(data));
s->last_tile = tile;
Debug(script, 6, "SetLastCommand company={:02d} cmd={} data={}", s->root_company, cmd, FormatArrayAsHex(data));
s->last_data = data;
s->last_cmd = cmd;
}
/* static */ bool ScriptObject::CheckLastCommand(TileIndex tile, const CommandDataBuffer &data, Commands cmd)
/* static */ bool ScriptObject::CheckLastCommand(const CommandDataBuffer &data, Commands cmd)
{
ScriptStorage *s = GetStorage();
Debug(script, 6, "CheckLastCommand company={:02d} tile={:06x} cmd={} data={}", s->root_company, tile, cmd, FormatArrayAsHex(data));
if (s->last_tile != tile) return false;
Debug(script, 6, "CheckLastCommand company={:02d} cmd={} data={}", s->root_company, cmd, FormatArrayAsHex(data));
if (s->last_cmd != cmd) return false;
if (s->last_data != data) return false;
return true;

View File

@@ -105,12 +105,12 @@ protected:
/**
* Store the latest command executed by the script.
*/
static void SetLastCommand(TileIndex tile, const CommandDataBuffer &data, Commands cmd);
static void SetLastCommand(const CommandDataBuffer &data, Commands cmd);
/**
* Check if it's the latest command executed by the script.
*/
static bool CheckLastCommand(TileIndex tile, const CommandDataBuffer &data, Commands cmd);
static bool CheckLastCommand(const CommandDataBuffer &data, Commands cmd);
/**
* Sets the DoCommand costs counter to a value.
@@ -340,7 +340,7 @@ bool ScriptObject::ScriptDoCommandHelper<Tcmd, Tret(*)(DoCommandFlag, Targs...)>
if constexpr ((::GetCommandFlags<Tcmd>() & CMD_CLIENT_ID) != 0) ScriptObjectInternal::SetClientIds(args, std::index_sequence_for<Targs...>{});
/* Store the command for command callback validation. */
if (!estimate_only && networking) ScriptObject::SetLastCommand(tile, EndianBufferWriter<CommandDataBuffer>::FromValue(args), Tcmd);
if (!estimate_only && networking) ScriptObject::SetLastCommand(EndianBufferWriter<CommandDataBuffer>::FromValue(args), Tcmd);
/* Try to perform the command. */
Tret res = ::Command<Tcmd>::Unsafe((StringID)0, networking ? ScriptObject::GetDoCommandCallback() : nullptr, false, estimate_only, tile, args);

View File

@@ -215,7 +215,7 @@ std::string ScriptConfig::SettingsToString() const
char *s = string;
*s = '\0';
for (const auto &item : this->settings) {
char no[10];
char no[INT32_DIGITS_WITH_SIGN_AND_TERMINATION];
seprintf(no, lastof(no), "%d", item.second);
/* Check if the string would fit in the destination */

View File

@@ -18,6 +18,9 @@
#include "../textfile_gui.h"
#include "script_instance.hpp"
/** Maximum of 10 digits for MIN / MAX_INT32, 1 for the sign and 1 for '\0'. */
static const int INT32_DIGITS_WITH_SIGN_AND_TERMINATION = 10 + 1 + 1;
/** Bitmask of flags for Script settings. */
enum ScriptConfigFlags {
SCRIPTCONFIG_NONE = 0x0, ///< No flags set.

View File

@@ -146,42 +146,42 @@ SQInteger ScriptInfo::AddSetting(HSQUIRRELVM vm)
} else if (strcmp(key, "min_value") == 0) {
SQInteger res;
if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
config.min_value = res;
config.min_value = ClampToI32(res);
items |= 0x004;
} else if (strcmp(key, "max_value") == 0) {
SQInteger res;
if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
config.max_value = res;
config.max_value = ClampToI32(res);
items |= 0x008;
} else if (strcmp(key, "easy_value") == 0) {
SQInteger res;
if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
config.easy_value = res;
config.easy_value = ClampToI32(res);
items |= 0x010;
} else if (strcmp(key, "medium_value") == 0) {
SQInteger res;
if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
config.medium_value = res;
config.medium_value = ClampToI32(res);
items |= 0x020;
} else if (strcmp(key, "hard_value") == 0) {
SQInteger res;
if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
config.hard_value = res;
config.hard_value = ClampToI32(res);
items |= 0x040;
} else if (strcmp(key, "random_deviation") == 0) {
SQInteger res;
if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
config.random_deviation = res;
config.random_deviation = ClampToI32(abs(res));
items |= 0x200;
} else if (strcmp(key, "custom_value") == 0) {
SQInteger res;
if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
config.custom_value = res;
config.custom_value = ClampToI32(res);
items |= 0x080;
} else if (strcmp(key, "step_size") == 0) {
SQInteger res;
if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
config.step_size = res;
config.step_size = ClampToI32(res);
} else if (strcmp(key, "flags") == 0) {
SQInteger res;
if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
@@ -252,7 +252,14 @@ SQInteger ScriptInfo::AddLabels(HSQUIRRELVM vm)
if (SQ_FAILED(sq_getstring(vm, -1, &label))) return SQ_ERROR;
/* Because squirrel doesn't support identifiers starting with a digit,
* we skip the first character. */
int key = atoi(key_string + 1);
key_string++;
int sign = 1;
if (*key_string == '_') {
/* When the second character is '_', it indicates the value is negative. */
sign = -1;
key_string++;
}
int key = atoi(key_string) * sign;
StrMakeValidInPlace(const_cast<char *>(label));
/* !Contains() prevents stredup from leaking. */

View File

@@ -744,11 +744,11 @@ SQInteger ScriptInstance::GetOpsTillSuspend()
return this->engine->GetOpsTillSuspend();
}
bool ScriptInstance::DoCommandCallback(const CommandCost &result, TileIndex tile, const CommandDataBuffer &data, CommandDataBuffer result_data, Commands cmd)
bool ScriptInstance::DoCommandCallback(const CommandCost &result, const CommandDataBuffer &data, CommandDataBuffer result_data, Commands cmd)
{
ScriptObject::ActiveInstance active(this);
if (!ScriptObject::CheckLastCommand(tile, data, cmd)) {
if (!ScriptObject::CheckLastCommand(data, cmd)) {
Debug(script, 1, "DoCommandCallback terminating a script, last command does not match expected command");
return false;
}
@@ -763,7 +763,7 @@ bool ScriptInstance::DoCommandCallback(const CommandCost &result, TileIndex tile
ScriptObject::SetLastCost(result.GetCost());
}
ScriptObject::SetLastCommand(INVALID_TILE, {}, CMD_END);
ScriptObject::SetLastCommand({}, CMD_END);
return true;
}

View File

@@ -217,7 +217,7 @@ public:
* @param cmd cmd as given to DoCommandPInternal.
* @return true if we handled result.
*/
bool DoCommandCallback(const CommandCost &result, TileIndex tile, const CommandDataBuffer &data, CommandDataBuffer result_data, Commands cmd);
bool DoCommandCallback(const CommandCost &result, const CommandDataBuffer &data, CommandDataBuffer result_data, Commands cmd);
/**
* Insert an event for this script.

View File

@@ -44,7 +44,6 @@ private:
uint last_error; ///< The last error of the command.
bool last_command_res; ///< The last result of the command.
TileIndex last_tile; ///< The last tile passed to a command.
CommandDataBuffer last_data; ///< The last data passed to a command.
Commands last_cmd; ///< The last cmd passed to a command.
CommandDataBuffer last_cmd_ret; ///< The extra data returned by the last command.
@@ -69,7 +68,6 @@ public:
last_cost (0),
last_error (STR_NULL),
last_command_res (true),
last_tile (INVALID_TILE),
last_cmd (CMD_END),
/* calback_value (can't be set) */
road_type (INVALID_ROADTYPE),

View File

@@ -566,6 +566,10 @@ void Squirrel::Initialize()
sq_pushroottable(this->vm);
squirrel_register_global_std(this);
/* Set consts table as delegate of root table, so consts/enums defined via require() are accessible */
sq_pushconsttable(this->vm);
sq_setdelegate(this->vm, -2);
}
class SQFile {
@@ -764,6 +768,12 @@ void Squirrel::Uninitialize()
{
ScriptAllocatorScope alloc_scope(this);
/* Remove the delegation */
sq_pushroottable(this->vm);
sq_pushnull(this->vm);
sq_setdelegate(this->vm, -2);
sq_pop(this->vm, 1);
/* Clean up the stuff */
sq_pop(this->vm, 1);
sq_close(this->vm);