From a92cff20ea59b68827a6f579d0ba9ccb40276d6b Mon Sep 17 00:00:00 2001 From: Howaner Date: Tue, 5 Aug 2014 13:47:10 +0200 Subject: Added Clamp() function to the lua api. --- src/Bindings/ManualBindings.cpp | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/Bindings/ManualBindings.cpp b/src/Bindings/ManualBindings.cpp index 042ffb19e..b41cd94bc 100644 --- a/src/Bindings/ManualBindings.cpp +++ b/src/Bindings/ManualBindings.cpp @@ -82,6 +82,33 @@ static int lua_do_error(lua_State* L, const char * a_pFormat, ...) // Lua bound functions with special return types +static int tolua_Clamp(lua_State * tolua_S) +{ + cLuaState LuaState(tolua_S); + int NumArgs = lua_gettop(LuaState); + if (NumArgs != 3) + { + return lua_do_error(LuaState, "Error in function call '#funcname#': Requires 3 arguments, got %i", NumArgs); + } + + if (!lua_isnumber(LuaState, 1) || !lua_isnumber(LuaState, 2) || !lua_isnumber(LuaState, 3)) + { + return lua_do_error(LuaState, "Error in function call '#funcname#': Expected a number for parameters #1, #2 and #3"); + } + + int Min = (int)tolua_tonumber(LuaState, 1, 0); + int Number = (int)tolua_tonumber(LuaState, 2, 0); + int Max = (int)tolua_tonumber(LuaState, 3, 0); + + int Result = std::min(std::max(Min, Number), Max); + LuaState.Push(Result); + return 1; +} + + + + + static int tolua_StringSplit(lua_State * tolua_S) { cLuaState LuaState(tolua_S); @@ -3103,6 +3130,7 @@ static int tolua_cCompositeChat_UnderlineUrls(lua_State * tolua_S) void ManualBindings::Bind(lua_State * tolua_S) { tolua_beginmodule(tolua_S, NULL); + tolua_function(tolua_S, "Clamp", tolua_Clamp); tolua_function(tolua_S, "StringSplit", tolua_StringSplit); tolua_function(tolua_S, "StringSplitAndTrim", tolua_StringSplitAndTrim); tolua_function(tolua_S, "LOG", tolua_LOG); -- cgit v1.2.3 From 43de9af8786243c90832b78f8c19ec92fb218392 Mon Sep 17 00:00:00 2001 From: Howaner Date: Tue, 5 Aug 2014 13:54:04 +0200 Subject: Added api documentation for Clamp() --- MCServer/Plugins/APIDump/APIDesc.lua | 5 +++-- src/Bindings/ManualBindings.cpp | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/MCServer/Plugins/APIDump/APIDesc.lua b/MCServer/Plugins/APIDump/APIDesc.lua index ad3b24ca5..64ba80c5f 100644 --- a/MCServer/Plugins/APIDump/APIDesc.lua +++ b/MCServer/Plugins/APIDump/APIDesc.lua @@ -2760,15 +2760,16 @@ end Functions = { AddFaceDirection = {Params = "BlockX, BlockY, BlockZ, BlockFace, [IsInverse]", Return = "BlockX, BlockY, BlockZ", Notes = "Returns the coords of a block adjacent to the specified block through the specified {{Globals#BlockFaces|face}}"}, - BlockFaceToString = { Params = "{{Globals#BlockFaces|eBlockFace}}", Return = "string", Notes = "Returns the string representation of the {{Globals#BlockFaces|eBlockFace}} constant. Uses the axis-direction-based names, such as BLOCK_FACE_XP." }, + BlockFaceToString = {Params = "{{Globals#BlockFaces|eBlockFace}}", Return = "string", Notes = "Returns the string representation of the {{Globals#BlockFaces|eBlockFace}} constant. Uses the axis-direction-based names, such as BLOCK_FACE_XP." }, BlockStringToType = {Params = "BlockTypeString", Return = "BLOCKTYPE", Notes = "Returns the block type parsed from the given string"}, + Clamp = {Params = "Number, Min, Max", Return = "number", Notes = "Clamp the number to the specified range."}, ClickActionToString = {Params = "{{Globals#ClickAction|ClickAction}}", Return = "string", Notes = "Returns a string description of the ClickAction enumerated value"}, DamageTypeToString = {Params = "{{Globals#DamageType|DamageType}}", Return = "string", Notes = "Converts the {{Globals#DamageType|DamageType}} enumerated value to a string representation "}, EscapeString = {Params = "string", Return = "string", Notes = "Returns a copy of the string with all quotes and backslashes escaped by a backslash"}, GetChar = {Params = "String, Pos", Return = "string", Notes = "Returns one character from the string, specified by position "}, GetIniItemSet = { Params = "IniFile, SectionName, KeyName, DefaultValue", Return = "{{cItem}}", Notes = "Returns the item that has been read from the specified INI file value. If the value is not present in the INI file, the DefaultValue is stored in the file and parsed as the result. Returns empty item if the value cannot be parsed. " }, GetTime = {Return = "number", Notes = "Returns the current OS time, as a unix time stamp (number of seconds since Jan 1, 1970)"}, - IsBiomeNoDownfall = { Params = "Biome", Return = "bool", Notes = "Returns true if the biome is 'dry', that is, there is no precipitation during rains and storms." }, + IsBiomeNoDownfall = {Params = "Biome", Return = "bool", Notes = "Returns true if the biome is 'dry', that is, there is no precipitation during rains and storms." }, IsValidBlock = {Params = "BlockType", Return = "bool", Notes = "Returns true if BlockType is a known block type"}, IsValidItem = {Params = "ItemType", Return = "bool", Notes = "Returns true if ItemType is a known item type"}, ItemToFullString = {Params = "{{cItem|cItem}}", Return = "string", Notes = "Returns the string representation of the item, in the format 'ItemTypeText:ItemDamage * Count'"}, diff --git a/src/Bindings/ManualBindings.cpp b/src/Bindings/ManualBindings.cpp index b41cd94bc..81065a7e1 100644 --- a/src/Bindings/ManualBindings.cpp +++ b/src/Bindings/ManualBindings.cpp @@ -96,8 +96,8 @@ static int tolua_Clamp(lua_State * tolua_S) return lua_do_error(LuaState, "Error in function call '#funcname#': Expected a number for parameters #1, #2 and #3"); } - int Min = (int)tolua_tonumber(LuaState, 1, 0); - int Number = (int)tolua_tonumber(LuaState, 2, 0); + int Number = (int)tolua_tonumber(LuaState, 1, 0); + int Min = (int)tolua_tonumber(LuaState, 2, 0); int Max = (int)tolua_tonumber(LuaState, 3, 0); int Result = std::min(std::max(Min, Number), Max); -- cgit v1.2.3 From 5f04488a97b5cdd705368937d977827ef4465d4a Mon Sep 17 00:00:00 2001 From: Howaner Date: Tue, 5 Aug 2014 18:39:18 +0200 Subject: Made lua clamp() compatible with all number types. --- src/Bindings/ManualBindings.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Bindings/ManualBindings.cpp b/src/Bindings/ManualBindings.cpp index 81065a7e1..9ba1501c5 100644 --- a/src/Bindings/ManualBindings.cpp +++ b/src/Bindings/ManualBindings.cpp @@ -96,11 +96,11 @@ static int tolua_Clamp(lua_State * tolua_S) return lua_do_error(LuaState, "Error in function call '#funcname#': Expected a number for parameters #1, #2 and #3"); } - int Number = (int)tolua_tonumber(LuaState, 1, 0); - int Min = (int)tolua_tonumber(LuaState, 2, 0); - int Max = (int)tolua_tonumber(LuaState, 3, 0); + lua_Number Number = tolua_tonumber(LuaState, 1, 0); + lua_Number Min = tolua_tonumber(LuaState, 2, 0); + lua_Number Max = tolua_tonumber(LuaState, 3, 0); - int Result = std::min(std::max(Min, Number), Max); + lua_Number Result = Clamp(Number, Min, Max); LuaState.Push(Result); return 1; } -- cgit v1.2.3