--[[ The function does curve conversion f(v) = outputValue Example of a curve, having multiple ranges of kx + b, set in a register of a String type with Json [{"k":4.167,"b":0,"range":[0,3]},{"k":0,"b":12.5,"range":[3,27]},{"k":-4.167,"b":125,"range":[27,30]}] --]] function getCurveValue (curveReg, v) local start, end_ = 1, 2 -- start & end indexes of the subranges local tab = cjson.decode(R(curveReg)) -- edge cases go first if (v < tab[start].range[start]) then return tab[start].range[start] * tab[start].k + tab[start].b, true elseif (v > tab[#tab].range[end_]) then return tab[#tab].range[end_] * tab[#tab].k + tab[#tab].b, true end for _, sub_ in ipairs(tab) do if ( (sub_.range[start] <= v) and (v < sub_.range[end_]) ) then return (v * sub_.k + sub_.b), false -- normal sequence end end end