From e92ded34e4361a9a9d9b725a9d5cd425ea1d57db Mon Sep 17 00:00:00 2001 From: NovaRain Date: Sat, 28 Apr 2018 11:16:10 +0800 Subject: [PATCH] Added "floor2" script function to work with ceil (#109) Added the missing description of ceil to notes.txt. Disabled ceil marco in lib.math.h. Removed duplications in Utils.h. --- artifacts/scripting/headers/lib.math.h | 5 ++--- artifacts/scripting/headers/sfall.h | 1 + artifacts/scripting/sfall function notes.txt | 7 +++++++ sfall/ScriptOps/MetaruleOp.hpp | 1 + sfall/ScriptOps/ScriptUtils.hpp | 4 ++++ 5 files changed, 15 insertions(+), 3 deletions(-) diff --git a/artifacts/scripting/headers/lib.math.h b/artifacts/scripting/headers/lib.math.h index 4956d93b..0d5588c1 100644 --- a/artifacts/scripting/headers/lib.math.h +++ b/artifacts/scripting/headers/lib.math.h @@ -26,15 +26,14 @@ end return intp; end*/ -procedure ceil(variable val) begin +/*procedure ceil(variable val) begin variable intp; intp := floor(val); if (abs(val-intp) > 0.0) then begin intp++; end return intp; -end - +end*/ procedure cap_number(variable num, variable min, variable max) begin if (num > max) then num := max; diff --git a/artifacts/scripting/headers/sfall.h b/artifacts/scripting/headers/sfall.h index ff1d74bd..d6e6d6dd 100644 --- a/artifacts/scripting/headers/sfall.h +++ b/artifacts/scripting/headers/sfall.h @@ -191,6 +191,7 @@ #define critter_inven_obj2(obj, type) sfall_func2("critter_inven_obj2", obj, type) #define exec_map_update_scripts sfall_func0("exec_map_update_scripts") +#define floor2(value) sfall_func1("floor2", value) #define intface_hide sfall_func0("intface_hide") #define intface_is_hidden sfall_func0("intface_is_hidden") #define intface_redraw sfall_func0("intface_redraw") diff --git a/artifacts/scripting/sfall function notes.txt b/artifacts/scripting/sfall function notes.txt index 620f8078..ce0419e1 100644 --- a/artifacts/scripting/sfall function notes.txt +++ b/artifacts/scripting/sfall function notes.txt @@ -265,6 +265,9 @@ Some utility/math functions are available: > float exponent(float x) - e^x +> int ceil(float x) +- round x to the nearest integer that is not less than x + > int round(float x) - round x to the nearest integer @@ -378,6 +381,10 @@ Some utility/math functions are available: - The file name is limited to 63 chars, including the extension - The section name is limited to 32 characters +> int sfall_func1("floor2", int/float value) +- works just like vanilla floor function, but returns correct integers for negative values +- vanilla floor function works exactly the same as ceil for negative values, much like trunc in C/C++ + ------------------------ ------ MORE INFO ------- ------------------------ diff --git a/sfall/ScriptOps/MetaruleOp.hpp b/sfall/ScriptOps/MetaruleOp.hpp index 2c075be6..1b400329 100644 --- a/sfall/ScriptOps/MetaruleOp.hpp +++ b/sfall/ScriptOps/MetaruleOp.hpp @@ -113,6 +113,7 @@ static const SfallMetarule metaruleArray[] = { {"intface_is_hidden", sf_intface_is_hidden, 0, 0}, {"exec_map_update_scripts", sf_exec_map_update_scripts, 0, 0}, {"set_ini_setting", sf_set_ini_setting, 2, 2}, + {"floor2", sf_floor2, 1, 1}, }; static void InitMetaruleTable() { diff --git a/sfall/ScriptOps/ScriptUtils.hpp b/sfall/ScriptOps/ScriptUtils.hpp index 3d90a252..d365465a 100644 --- a/sfall/ScriptOps/ScriptUtils.hpp +++ b/sfall/ScriptOps/ScriptUtils.hpp @@ -891,3 +891,7 @@ static void _stdcall op_message_str_game2() { static void __declspec(naked) op_message_str_game() { _WRAP_OPCODE(op_message_str_game2, 2, 1) } + +static void sf_floor2() { + opHandler.setReturn(static_cast(floor(opHandler.arg(0).asFloat()))); +}