Savage_Me55iah

CC_API_booleanmanip

Jul 19th, 2017
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.82 KB | None | 0 0
  1. --Script by Savage_Me55iah of CraftAU.com.au--
  2.  
  3. --Other Savage_Me55iah scripts @ http://pastebin.com/u/Savage_Me55iah
  4.  
  5. --INSTRUCTIONS ON USE--
  6. --Instruction video on http://youtube.com/savagemessiah5
  7.  
  8. --Contact Savage_Me55iah @ CraftAU.com.au or http://www.computercraft.info/forums2/ with suggestions/fixes/bugs
  9.  
  10. local scriptName = "booleanmanip"
  11. fileDetail = fileDetail or {}
  12. fileDetail[scriptName] = {["version"] = 0.1, ["pastebin"] = "irkktmgP"}
  13.  
  14. local tArgs = {...}
  15. if tArgs[1] == "update" then
  16.     print("UPDATING") shell.run("pastebin get "..fileDetail[scriptName].pastebin.." "..scriptName.."_update")
  17.     if fs.exists(scriptName.."_update") then fs.remove(shell.getRunningProgram()) fs.copy(scriptName.."_update", shell.getRunningProgram()) fs.remove(scriptName.."_update") print("UPDATE SUCCESS") else print("UPDATE FAILED") end
  18. end
  19.  
  20. -----------------------------------------------------------------------------------------------------------
  21. -- Variables --
  22. -----------------------------------------------------------------------------------------------------------
  23.  
  24. local errorLoc = "Datastore/Errors/Error"
  25.  
  26. -----------------------------------------------------------------------------------------------------------
  27. -- API Functions --
  28. -----------------------------------------------------------------------------------------------------------
  29.  
  30. -- BACKSLASH WAS MESSING WITH MY SCRIPT EDITOR, I WILL NOT BE JUDGED!
  31. function BS() local result = "\\" return result end
  32.  
  33. -- booleanmanip.check(functionNum, variable) -- [1] returns true if function ran correctly OR if false variable was nil [2] returns variable OR false if nil
  34. -- used for bypassing nil errors
  35. function check(param_functionNum, param_value)
  36.     local result, functionNum = {false, nil}, tostring(param_functionNum).."/001"
  37.     if tostring(param_value) ~= "nil" then result = {true, param_value} else result = {false, false} end
  38.     return result
  39. end
  40.  
  41. -- booleanmanip.quickChoice(functionNum, boolean, value, value) -- [1] returns function ran correctly [2] returns result
  42. -- removes alot of standard "if" blocks
  43. function quickChoice(param_functionNum, param_boolean, param_true, param_false)
  44.     local result, functionNum, variable = {false, false}, param_functionNum.."/002", check(functionNum, param_boolean)[2]
  45.     if type(variable) == "boolean" then
  46.         if variable == true then result = {true, param_true or true} else result = {true, param_false or false} end
  47.     else
  48.         logger.logger(functionNum, {"Function Error, Expected: booleanmanip.quickChoice(functionNum, boolean, value, value)"}, 2, " ")
  49.     end
  50.     return result
  51. end
  52.  
  53. -- booleanmanip.massCheck(functionNum, "and", {boolean, boolean, etc}) -- [1] returns function ran correctly [2] returns result
  54. -- removes alot of "and/or"
  55. function massCheck(param_functionNum, param_function, param_booleans)
  56.     local result, functionNum, variable_function ={false, false}, param_functionNum.."/003", tostring(param_function)
  57.     if variable_function == "and" or variable_function == "or" then
  58.         if type(param_booleans) == "table" then
  59.             if param_function == "and" then
  60.                 result = {false, true}
  61.                 for a,b in pairs(param_booleans) do result = {true, quickChoice(functionNum, b, result[2], false)[2]} end
  62.             else
  63.                 for a,b in pairs(param_booleans) do result = {true, quickChoice(functionNum, b, true, result[2])[2]} end
  64.             end
  65.         else
  66.             logger.logger(functionNum, {"Function Error, Expected: booleanmanip.massCheck(functionNum, (and/or), {boolean, boolean, etc})"}, 2, " ")
  67.         end
  68.     else
  69.         logger.logger(functionNum, {"Function Error, Expected: booleanmanip.massCheck(functionNum, (and/or), {boolean, boolean, etc})"}, 2, " ")
  70.     end
  71.     return result
  72. end
  73.  
  74. -- booleanmanip.multiCompare(functionNum, "and", variable, {variable, variable, etc}) -- [1] returns function ran correctly [2] returns result
  75. -- compares multiple variables to 1 variable
  76. function multiCompare(param_functionNum, param_function, param_arg, param_variables)
  77.     local result, functionNum, variable_function = {false, false}, param_functionNum.."/004", tostring(param_function)
  78.     if massCheck(functionNum, "and", {massCheck(functionNum, "or", {(variable_function == "and"), (variable_function == "or")})[2], check(functionNum, param_arg)[1], (type(check(functionNum, param_variables)[2]) == "table")})[2] then
  79.         if param_function == "and" then
  80.             result = {false, true}
  81.             for a,b in pairs(param_variables) do result = {true, quickChoice(functionNum, (param_arg == check(functionNum, b)[2]), result[2], false)[2]} end
  82.         else
  83.             for a,b in pairs(param_variables) do result = {true, quickChoice(functionNum, (param_arg == check(functionNum, b)[2]), true, result[2])[2]} end
  84.         end
  85.     else
  86.         logger.logger(functionNum, {"Function Error, Expected: booleanmanip.multiCompare(functionNum, (and/or), variable, {variable, variable, etc})"}, 2, " ")
  87.     end
  88.     return result
  89. end
  90.  
  91. -- tolerance(param_functionNum, "relative", 10, 5, 6, -1) -- [1] returns function ran correctly [2] returns result
  92. -- returns boolean if value is within tolerance
  93. function tolerance(param_functionNum, param_function, param_target, param_value, param_tolerancePositive, param_toleranceNegative)
  94.     local result, functionNum = {false, false}, param_functionNum.."/005"
  95.     local tolerancePositive, toleranceNegative = param_tolerancePositive or 0, param_toleranceNegative or 0
  96.     if multiCompare(functionNum, "and", "number", {type(check(functionNum, param_target)[2]), type(check(functionNum, param_value)[2])})[2] and type(check(functionNum, param_function)[2]) == "string" then
  97.         if param_function == "relative" then
  98.             tolerancePositive, toleranceNegative = param_target + tolerancePositive, param_target + toleranceNegative
  99.         end
  100.         result = {true, quickChoice(functionNum, massCheck(functionNum, "and", {(param_value <= tolerancePositive), (param_value >= toleranceNegative)})[2])[2]}
  101.     else
  102.         logger.logger(functionNum, {"Function Error, Expected: booleanmanip.tolerance(functionNum, relative, number, number, number, number)"}, 2, " ")
  103.     end
  104.     return result
  105. end
  106.  
  107. -- booleanmanip.variableCheck(functionNum, {type, type, etc}, {variable, variable, etc}) -- [1] returns function ran correctly [2] returns result
  108. -- returns boolean if all of the variables in the 2nd table matches any of the types in the 1st table
  109. function variableCheck(param_functionNum, param_type, param_variable)
  110.     local result, functionNum = {false, false}, param_functionNum.."/007"
  111.     local tempTable = {}
  112.     if multiCompare(functionNum, "and", "table", {type(param_type), type(param_variable)})[2] then
  113.         for a,b in pairs(param_variable) do
  114.             tempTable[a] = false
  115.             for c,d in pairs(param_type) do
  116.                 if massCheck(functionNum, "and", {check(functionNum, b)[1], check(functionNum, d)[1]})[2] then
  117.                     tempTable[a] = quickChoice(functionNum, (d == type(b)), true, tempTable[a])[2]
  118.         end end end
  119.         for a,b in pairs(tempTable) do result = {true, quickChoice(functionNum, tempTable[a], tempTable[a], false)[2]} end
  120.     else
  121.         logger.logger(functionNum, {"Function Error, Expected: booleanmanip.variableCheck(functionNum, {type}, {variable, variable, etc})"}, 2, " ")
  122.     end
  123.     return result
  124. end
  125.  
  126.  
  127. -- booleanmanip.even(functionNum, number) -- [1] returns function ran correctly [2] returns result
  128. -- returns boolean if value is an even number
  129. function even(param_functionNum, param_variable)
  130.     local result, functionNum = {false, false}, param_functionNum.."/006"
  131.     if variableCheck(functionNum, {"number"}, {param_variable})[2] then
  132.         result = {true, quickChoice(functionNum, (param_variable % 2 == 0))[2]}
  133.     else
  134.         logger.logger(functionNum, {"Function Error, Expected: booleanmanip.even(functionNum, number)"}, 2, " ")
  135.     end
  136.     return result
  137. end
  138.  
  139.  
  140. -----------------------------------------------------------------------------------------------------------
  141. -- Old Functions --
  142. -----------------------------------------------------------------------------------------------------------
Add Comment
Please, Sign In to add comment