From 31f42128e1f5c75bc3ecb09306f7211e9b03f6d5 Mon Sep 17 00:00:00 2001 From: NovaRain Date: Sat, 28 Apr 2018 11:07:37 +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/Modules/Scripting/Handlers/Metarule.cpp | 2 ++ sfall/Modules/Scripting/Handlers/Utils.cpp | 10 +++++++--- sfall/Modules/Scripting/Handlers/Utils.h | 8 ++------ 6 files changed, 21 insertions(+), 12 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 0ec06c71..b225ae08 100644 --- a/artifacts/scripting/headers/sfall.h +++ b/artifacts/scripting/headers/sfall.h @@ -213,6 +213,7 @@ #define critter_inven_obj2(obj, type) sfall_func2("critter_inven_obj2", obj, type) #define display_stats sfall_func0("display_stats") #define exec_map_update_scripts sfall_func0("exec_map_update_scripts") +#define floor2(value) sfall_func1("floor2", value) #define get_cursor_mode sfall_func0("get_cursor_mode") #define get_flags(obj) sfall_func1("get_flags", obj) #define get_ini_section(file, sect) sfall_func2("get_ini_section", file, sect) diff --git a/artifacts/scripting/sfall function notes.txt b/artifacts/scripting/sfall function notes.txt index 3636d31b..2bc82c74 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++ + > int sfall_func1("item_weight", object) - gets the current weight of an object diff --git a/sfall/Modules/Scripting/Handlers/Metarule.cpp b/sfall/Modules/Scripting/Handlers/Metarule.cpp index 02e41c41..dd0fc395 100644 --- a/sfall/Modules/Scripting/Handlers/Metarule.cpp +++ b/sfall/Modules/Scripting/Handlers/Metarule.cpp @@ -23,6 +23,7 @@ #include "Interface.h" #include "Misc.h" #include "Objects.h" +#include "Utils.h" #include "Worldmap.h" #include "Metarule.h" @@ -80,6 +81,7 @@ static const SfallMetarule metarules[] = { {"critter_inven_obj2", sf_critter_inven_obj2, 2, 2, {ARG_OBJECT, ARG_INT}}, {"display_stats", sf_display_stats, 0, 0}, {"exec_map_update_scripts", sf_exec_map_update_scripts, 0, 0}, + {"floor2", sf_floor2, 1, 1, {ARG_NUMBER}}, {"get_cursor_mode", sf_get_cursor_mode, 0, 0}, {"get_flags", sf_get_flags, 1, 1, {ARG_OBJECT}}, {"get_ini_section", sf_get_ini_section, 2, 2, {ARG_STRING, ARG_STRING}}, diff --git a/sfall/Modules/Scripting/Handlers/Utils.cpp b/sfall/Modules/Scripting/Handlers/Utils.cpp index d1295755..b0faecb5 100644 --- a/sfall/Modules/Scripting/Handlers/Utils.cpp +++ b/sfall/Modules/Scripting/Handlers/Utils.cpp @@ -49,11 +49,11 @@ void sf_cos(OpcodeContext& ctx) { ctx.setReturn(cos(ctx.arg(0).asFloat())); } -void sf_tan(OpcodeContext& ctx) { +void sf_tan(OpcodeContext& ctx) { ctx.setReturn(tan(ctx.arg(0).asFloat())); } -void sf_arctan(OpcodeContext& ctx) { +void sf_arctan(OpcodeContext& ctx) { ctx.setReturn(atan2(ctx.arg(0).asFloat(), ctx.arg(1).asFloat())); } @@ -70,7 +70,7 @@ void sf_atoi(OpcodeContext& ctx) { ); } -void sf_atof(OpcodeContext& ctx) { +void sf_atof(OpcodeContext& ctx) { auto str = ctx.arg(0).asString(); ctx.setReturn( static_cast(atof(str)) @@ -345,5 +345,9 @@ void sf_message_str_game(OpcodeContext& ctx) { ctx.setReturn(msg); } +void sf_floor2(OpcodeContext& ctx) { + ctx.setReturn(static_cast(floor(ctx.arg(0).asFloat()))); +} + } } diff --git a/sfall/Modules/Scripting/Handlers/Utils.h b/sfall/Modules/Scripting/Handlers/Utils.h index 19d099d6..dfd8be06 100644 --- a/sfall/Modules/Scripting/Handlers/Utils.h +++ b/sfall/Modules/Scripting/Handlers/Utils.h @@ -57,22 +57,18 @@ void __declspec() NegateFixHook(); void sf_power(OpcodeContext&); -void sf_power(OpcodeContext&); - void sf_log(OpcodeContext&); void sf_exponent(OpcodeContext&); -void sf_exponent(OpcodeContext&); - -void sf_ceil(OpcodeContext&); - void sf_ceil(OpcodeContext&); void sf_round(OpcodeContext&); void sf_message_str_game(OpcodeContext&); +void sf_floor2(OpcodeContext&); + char* _stdcall Substring(const char* str, int pos, int length); }