Skip to content

Commit

Permalink
实现 strict_parameters_of_rand 选项, 用于控制是否对 rand 脚本指令进行严格的参数检查 (感谢 "差记性的…
Browse files Browse the repository at this point in the history
…小北" 反馈) (#756)
  • Loading branch information
CairoLee committed Feb 12, 2024
1 parent e3f369c commit 8201b8b
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 0 deletions.
20 changes: 20 additions & 0 deletions conf/battle/pandas.conf
Expand Up @@ -493,4 +493,24 @@ mob_default_damagemotion: 0
// 默认值为: yes
mob_setunitdata_persistence: yes

// 是否对 rand 脚本指令进行严格的参数检查? [开关选项]
//
// 若禁用严格检查, 那么当传递一些不严谨的参数组合时,
// 程序将不会再打断脚本, 也不会输出警告信息.
//
// 进行严格检查策略的案例说明:
// - rand(2,1); // 警告: 最小值 2 不能大于最大值 1
// - rand(-1); // 打断: 范围不能是负数
// - rand(1); // 打断: 范围太小了, 不足以生成随机数
// - rand(10,10); // 打断: 最小值和最大值一致, 不足以生成随机数
//
// 关闭严格检查后的表现结果:
// - rand(2,1); // 无警告, 返回 1 - 2 之间的随机数
// - rand(-1); // 无警告, 返回 0
// - rand(1); // 无警告, 返回 0
// - rand(10,10); // 无警告, 返回 10
//
// 默认值为: yes
strict_parameters_of_rand: yes

// PYHELP - BATTLECONFIG - INSERT POINT - <Section 4>
4 changes: 4 additions & 0 deletions src/config/pandas.hpp
Expand Up @@ -372,6 +372,10 @@
// 是否启用 mob_setunitdata_persistence 配置选项及其功能 [Sola丶小克]
// 此选项用于控制是否高优先级持久化保存 setunitdata 对魔物的设置
#define Pandas_BattleConfig_Mob_SetUnitData_Persistence

// 是否启用 strict_parameters_of_rand 配置选项及其功能 [Sola丶小克]
// 此选项用于控制是否对 rand 脚本指令进行严格的参数检查, 以便兼容以前早期的脚本
#define Pandas_BattleConfig_Strict_Parameters_Of_Rand
// PYHELP - BATTLECONFIG - INSERT POINT - <Section 1>
#endif // Pandas_BattleConfigure

Expand Down
3 changes: 3 additions & 0 deletions src/map/battle.cpp
Expand Up @@ -11083,6 +11083,9 @@ static const struct _battle_data {
#ifdef Pandas_BattleConfig_Mob_SetUnitData_Persistence
{ "mob_setunitdata_persistence", &battle_config.mob_setunitdata_persistence, 1, 0, 1, },
#endif // Pandas_BattleConfig_Mob_SetUnitData_Persistence
#ifdef Pandas_BattleConfig_Strict_Parameters_Of_Rand
{ "strict_parameters_of_rand", &battle_config.strict_parameters_of_rand, 1, 0, 1, },
#endif // Pandas_BattleConfig_Strict_Parameters_Of_Rand
// PYHELP - BATTLECONFIG - INSERT POINT - <Section 3>
#include <custom/battle_config_init.inc>
};
Expand Down
3 changes: 3 additions & 0 deletions src/map/battle.hpp
Expand Up @@ -820,6 +820,9 @@ struct Battle_Config
#ifdef Pandas_BattleConfig_Mob_SetUnitData_Persistence
int mob_setunitdata_persistence; // 是否高优先级持久化保存 setunitdata 对魔物的设置
#endif // Pandas_BattleConfig_Mob_SetUnitData_Persistence
#ifdef Pandas_BattleConfig_Strict_Parameters_Of_Rand
int strict_parameters_of_rand; // 是否对 rand 脚本指令进行严格的参数检查
#endif // Pandas_BattleConfig_Strict_Parameters_Of_Rand
// PYHELP - BATTLECONFIG - INSERT POINT - <Section 2>
#include <custom/battle_config_struct.inc>
};
Expand Down
24 changes: 24 additions & 0 deletions src/map/script.cpp
Expand Up @@ -6510,6 +6510,9 @@ BUILDIN_FUNC(rand)
int64 minimum;
int64 maximum;

#ifdef Pandas_BattleConfig_Strict_Parameters_Of_Rand
if (battle_config.strict_parameters_of_rand) {
#endif // Pandas_BattleConfig_Strict_Parameters_Of_Rand
// min,max
if( script_hasdata( st, 3 ) ){
minimum = script_getnum64( st, 2 );
Expand Down Expand Up @@ -6545,6 +6548,27 @@ BUILDIN_FUNC(rand)
st->state = END;
return SCRIPT_CMD_FAILURE;
}
#ifdef Pandas_BattleConfig_Strict_Parameters_Of_Rand
}
else {
// 如果不严格校验参数,那么就放弃检查参数的合法性
if (script_hasdata(st, 3)) {
minimum = script_getnum64(st, 2);
maximum = script_getnum64(st, 3);
} else {
minimum = 0;
maximum = script_getnum64(st, 2);

// The range version is exclusive maximum
maximum -= 1;

if (maximum < 1) {
script_pushint64(st, 0);
return SCRIPT_CMD_SUCCESS;
}
}
}
#endif // Pandas_BattleConfig_Strict_Parameters_Of_Rand

script_pushint64( st, rnd_value( minimum, maximum ) );

Expand Down

0 comments on commit 8201b8b

Please sign in to comment.