Module:Ability progression

-- local p = {} local lib      = require('Module:Feature') local col      = require('Module:color') local userError = require('Dev:User error')

function p.test(frame) local args; if frame.args[1] then args = frame.args else args = frame:getParent.args end return expr(args[1]) end

function p.pp(frame) local args; if frame.args[1] then args = frame.args else args = frame:getParent.args end local start2, end2 local values, values2 = "", "" local count, countstatic    = 30, 30 local fill, fill1      = 18, 18 local to, to1                 = 0, 0 local displayformula          = args["formula"] local round               = args["round"]   or 2 local round1               = args["round1"] local key                 = args["key"]     or "" local key1                 = args["key1"] local label               = args["label"] local label1               = args["label1"]  or args["type"] local orig                = mw.text.split(args[1] or "", "%;") local orig1                = mw.text.split(args[2] or "", "%;") local _, bot_to_hits          = string.gsub(args[1] or "", "to", "") local _, top_to_hits          = string.gsub(args[2] or "", "to", "") local _, bot_x_hits           = string.gsub(args[1] or "", "x",  "") local _, top_x_hits           = string.gsub(args[2] or "", "x",  "") local i = 1 if bot_to_hits + bot_x_hits ~= 0 or top_to_hits + top_x_hits ~= 0 then local lastvalue="" while orig[i] ~= nil and orig[i] ~= "" do           if (string.find(orig[i], "to") ~= nil or string.find(orig[i], "x") ~= nil) and string.find(orig[i], "%<") == nil then local start if string.find(orig[i],"to") ~= nil then start=string.sub(orig[i],1,string.find(orig[i],"to")-1) end if string.find(orig[i],"x") == nil and pcall(expr,start.."*2")~=false then local finish,times if string.find(orig[i], "by") ~= nil then finish, times = orig[i]:match".* *to *([^ ]*) *by *([^ ]*)" else if string.find(orig[i], "for") ~= nil then finish, times = orig[i]:match".* *to *([^ ]*) *for *([^ ]*)" else finish = orig[i]:match".* *to *([^ ]*)" end end if finish ~= nil and pcall(expr, finish .. "*2") ~= false and ((times ~= "" and times ~= nil and pcall(expr, times .. "*2") ~= false) or times == "" or times == nil) then start = tonumber(expr(start)) finish = tonumber(expr(finish)) if times ~= "" and times ~= nil then times = tonumber(expr(times)) if string.find(orig[i], "by") ~= nil then times = rounding((finish-start)/times+1,0) end fill = fill - times if string.find(orig[i], "by") ~= nil and lastvalue == start then fill = fill + 1 end else to = 1 end lastvalue = finish else fill = fill - 1 end else local times local doubleto = orig[i] times = orig[i]:match".* *for *([^ ]*)" if times ~= "" and times ~= nil and pcall(expr, times .. "*2") ~= false then doubleto = string.sub(doubleto,1,#doubleto-#times-3) else times = nil end local _,occurences = string.gsub(orig[i],"to","") if occurences >0 and ((times ~= "" and times ~= nil and pcall(expr, times .. "*2") ~= false) or times == "" or times == nil) then local times2=2 doubleto = string_to_formula(doubleto,times2) end local useformula = string.gsub(doubleto,"then","1") if useformula ~= nil and pcall(expr, string.gsub(useformula, "x", "1") .. "*2") ~= false and ((times ~= "" and times ~= nil and pcall(expr, times .. "*2") ~= false) or times == "" or times == nil) then if times ~= "" and times ~= nil then times = tonumber(expr(times)) fill = fill - times else to = 1 end else fill = fill - 1 end end else fill = fill - 1 if pcall(expr, string.gsub(orig[i],"then",lastvalue) .. "*2") ~= false then lastvalue=expr(string.gsub(orig[i],"then",lastvalue)) end end i = i + 1 end end i = 1 if bot_to_hits + bot_x_hits ~= 0 or top_to_hits + top_x_hits ~= 0 then local lastvalue1 = "" while orig1[i] ~= nil and orig1[i] ~= "" do           if (string.find(orig1[i], "to") ~= nil or string.find(orig1[i], "x") ~= nil) and string.find(orig1[i], "%<") == nil then local start1 if string.find(orig1[i],"to") ~= nil then start1=string.sub(orig1[i],1,string.find(orig1[i],"to")-1) end if string.find(orig1[i],"x") == nil and pcall(expr,start1.."*2")~=false then local finish1,times1 if string.find(orig1[i], "by") ~= nil then finish1, times1 = orig1[i]:match".* *to *([^ ]*) *by *([^ ]*)" else if string.find(orig1[i], "for") ~= nil then finish1, times1 = orig1[i]:match".* *to *([^ ]*) *for *([^ ]*)" else finish1 = orig1[i]:match".* *to *([^ ]*)" end end if finish1 ~= nil and pcall(expr, finish1 .. "*2") ~= false and ((times1 ~= "" and times1 ~= nil and pcall(expr, times1 .. "*2") ~= false) or times1 == "" or times1 == nil) then start1 = tonumber(expr(start1)) finish1 = tonumber(expr(finish1)) if times1 ~= "" and times1 ~= nil then times1 = tonumber(expr(times1)) if string.find(orig1[i], "by") ~= nil then times1 = rounding((finish1-start1)/times1+1,0) end fill1 = fill1 - times1 if string.find(orig1[i], "by") ~= nil and lastvalue1 == start1 then fill1 = fill1 + 1 end else to1 = 1 end lastvalue1 = finish1 else fill1 = fill1 - 1 end else local times1 local doubleto1 = orig1[i] times1 = orig1[i]:match".* *for *([^ ]*)" if times1 ~= "" and times1 ~= nil and pcall(expr, times1 .. "*2") ~= false then doubleto1 = string.sub(doubleto1,1,#doubleto1-#times1-3) else times1 = nil end local _,occurences1 = string.gsub(orig1[i],"to","") if occurences1 >0 and ((times1 ~= "" and times1 ~= nil and pcall(expr, times1 .. "*2") ~= false) or times1 == "" or times1 == nil) then local times2=2 doubleto1 = string_to_formula(doubleto1,times2) end local useformula1 = string.gsub(doubleto1,"then","1") if useformula1 ~= nil and pcall(expr, string.gsub(useformula1, "x", "1") .. "*2") ~= false and ((times1 ~= "" and times1 ~= nil and pcall(expr, times1 .. "*2") ~= false) or times1 == "" or times1 == nil) then if times1 ~= "" and times1 ~= nil then times1 = tonumber(expr(times1)) fill1 = fill1 - times1 else to1 = 1 end else fill1 = fill1 - 1 end end else fill1 = fill1 - 1 if pcall(expr, string.gsub(orig1[i],"then",lastvalue1) .. "*2") ~= false then lastvalue1=expr(string.gsub(orig1[i],"then",lastvalue1)) end end i = i + 1 end end if 18 - fill >= 18 - fill1 then if to == 0 then fill1 = fill1 - fill end else if to1 == 0 then fill = fill - fill1 end end if fill < 0 then fill = 0 end if fill1 < 0 then fill1 = 0 end local lastvalue = "" i = 1 while orig[i] ~= nil and orig[i] ~= "" do       if (string.find(orig[i], "to") ~= nil or string.find(orig[i], "x") ~= nil) and string.find(orig[i], "%<") == nil then local start if string.find(orig[i],"to") ~= nil then start=string.sub(orig[i],1,string.find(orig[i],"to")-1) end if string.find(orig[i],"x") == nil and pcall(expr,start.."*2")~=false then local finish,times if string.find(orig[i], "by") ~= nil then finish, times = orig[i]:match".* *to *([^ ]*) *by *([^ ]*)" else if string.find(orig[i], "for") ~= nil then finish, times = orig[i]:match".* *to *([^ ]*) *for *([^ ]*)" else finish = orig[i]:match".* *to *([^ ]*)" end end if finish ~= nil and pcall(expr, finish .. "*2") ~= false and ((times ~= "" and times ~= nil and pcall(expr, times .. "*2") ~= false) or times == "" or times == nil) then start = tonumber(expr(start)) finish = tonumber(expr(finish)) if displayformula == nil and orig[2] == nil then start2 = start finish2 = finish end if times == "" or times == nil then times = fill fill = 0 else times = tonumber(expr(times)) if string.find(orig[i], "by") ~= nil then times = tonumber(rounding((finish-start)/times+1,0)) end end local x                   if lastvalue == start then x = 2 if string.find(orig[i], "by") == nil then times = times + 1 end else x = 1 end lastvalue = finish count = count - times if count < 0 then return userError("Maximum size exceeded", "LuaError") end local formula = start local scale = (finish - start) / (times - 1) while x <= times do                       if x ~= 1 then formula = formula + scale end local values3 = rounding(formula, round) if values == "" then values = fd(values3) values2 = values3 else values = values .. " / " .. fd(values3) values2 = values2 .. " / " .. values3 end x = x + 1 end else count = count - 1 if count < 0 then return userError("Maximum size exceeded", "LuaError") end local values3 = orig[i] lastvalue = orig[i] if values == "" then values = fd(values3) values2 = values3 else values = values .. " / " .. fd(values3) values2 = values2 .. " / " .. values3 end end else local times local doubleto = orig[i] times = orig[i]:match".* *for *([^ ]*)" if times ~= "" and times ~= nil and pcall(expr, times .. "*2") ~= false then doubleto = string.sub(doubleto,1,#doubleto-#times-3) else times = nil end local _,occurences = string.gsub(orig[i],"to","") if occurences >0 and ((times ~= "" and times ~= nil and pcall(expr, times .. "*2") ~= false) or times == "" or times == nil) then local times2 if times == "" or times==nil then times2=fill else times2=tonumber(expr(times)) end doubleto = string_to_formula(doubleto,times2) end local useformula = string.gsub(doubleto,"then",lastvalue) if useformula ~= nil and pcall(expr, string.gsub(useformula, "x", "1") .. "*2") ~= false and ((times ~= "" and times ~= nil and pcall(expr, times .. "*2") ~= false) or times == "" or times == nil) then if displayformula == nil and orig[2] == nil then useformula2 = useformula end if times == "" or times==nil then times=fill fill=0 else times=tonumber(expr(times)) end local x = 1 while x <= times do                       count = count - 1 if count < 0 then return userError("Maximum size exceeded", "LuaError") end local formula = string.gsub(useformula, "x", x)                       local values3 = expr(formula) lastvalue = values3 values3 = rounding(values3,round) if values == "" then values = fd(values3) values2 = values3 else values = values .. " / " .. fd(values3) values2 = values2 .. " / " .. values3 end x = x + 1 end else count = count - 1 if count < 0 then return userError("Maximum size exceeded", "LuaError") end local values3 = orig[i] lastvalue = values3 if values == "" then values = fd(values3) values2 = values3 else values = values .. " / " .. fd(values3) values2 = values2 .. " / " .. values3 end end end else count = count - 1 if count < 0 then return userError("Maximum size exceeded", "LuaError") end local values3 if pcall(expr, string.gsub(orig[i],"then",lastvalue) .. "*2") ~= false then values3 = expr(string.gsub(orig[i],"then",lastvalue)) lastvalue = values3 values3 = rounding(values3,round) else values3 = orig[i] end if values == "" then values = fd(values3) values2 = values3 else values = values .. " / " .. fd(values3) values2 = values2 .. " / " .. values3 end end i = i + 1 end local s = "" s = s .. '<span class="pp-tooltip2" style="position:relative; border-bottom:1px dotted; cursor:help;'   if args["color"] ~= nil then        s = s .. " color:" ..col.keyword{args["color"]} ..";"    end    s = s .. '"' if label ~= nil then s = s .. ' data-bot_label="' .. label .. '"' end if label1 ~= nil then s = s .. ' data-top_label="' .. string.gsub(string.gsub(label1, "'", ""), "", "") .. '"' end if displayformula ~= nil then s = s .. ' data-displayformula="' .. displayformula .. '"' end if useformula2 ~= nil then s = s .. ' data-useformula="' .. useformula2 .. '"' end if values2 ~= nil then s = s .. ' data-bot_values="' .. values2 .. '"' end if args[2] ~= nil then s = s .. ' data-top_values="' .. args[2] .. '"' end if round ~= 2 then s = s .. ' data-bot_round="' .. round .. '"' end if round1 ~= nil then s = s .. ' data-top_round="' .. round1 .. '"' end if key ~= "" then s = s .. ' data-bot_key="' .. key .. '"' end if key1 ~= nil then s = s .. ' data-top_key="' .. key1 .. '"' end if start2 ~= nil then s = s .. ' data-start="' .. start2 .. '"' s = s .. ' data-finish="' .. finish2 .. '"' end if top_to_hits + top_x_hits ~= 0 or start2 ~= nil then s = s .. ' data-top_fill="' .. fill1 .. '"' end s = s .. ">"   if (countstatic - count > 5 and args["changedisplay"] ~= "true") or (countstatic - count <= 5 and args["changedisplay"] == "true") then s = s .. fd(string.sub(values2, 1, (string.find(values2, "%/") or 1) - 2)) .. key .. " &minus; " .. fd(last(values2, "%/")) .. key else s = s .. values .. key end if args["showtype"] ~= "false" then s = s .. " (based on " .. (args["type"] or args["label1"] or "level") ..")" end s = s .. " "   return s end

function p.pptooltip(frame) local args; if frame.args[1] then args = frame.args else args = frame:getParent.args end

local label = args["bot_label"] local label1 = args["top_label"] local round  = args["bot_round"] local round1  = args["top_round"] local key    = args["bot_key"] local key1    = args["top_key"] local displayformula = args["displayformula"] local useformula = args["useformula"] local fill1   = tonumber(args["top_fill"]) local count1   = 30 local start   = tonumber(args["start"]) local finish   = tonumber(args["finish"]) if label1 == "" then label1 = "level" end if label == "" then label = "value" end if tonumber(round) == nil then round = 2 end if round1 == "" then round1 = 2 end local orig = mw.text.split(args["bot_values"], " %/") local orig1 = mw.text.split(args["top_values"], "%;") local s = "" local i = 1 local j = 1 local lastvalue1 = "" while (orig1[i]~=nil and orig1[i]~="") or (orig[j]~=nil and orig[j]~="") do       s= s .. ' ' if orig1[1]~="" then if orig1[i]~=nil and orig1[i]~="" then if (string.find(orig1[i],"to")~=nil or string.find(orig1[i],"x")~=nil) and string.find(orig1[i],"%<")==nil then local start1 if string.find(orig1[i],"to") ~= nil then start1=string.sub(orig1[i],1,string.find(orig1[i],"to")-1) end if string.find(orig1[i],"x")==nil and pcall(expr,start1.."*2")~=false then local finish1,times1 if string.find(orig1[i], "by") ~= nil then finish1, times1 = orig1[i]:match".* *to *([^ ]*) *by *([^ ]*)" else if string.find(orig1[i], "for") ~= nil then finish1,times1 = orig1[i]:match".* *to *([^ ]*) *for *([^ ]*)" else finish1 = orig1[i]:match".* *to *([^ ]*)" end end if finish1~=nil and pcall(expr,finish1.."*2")~=false and ((times1 ~= "" and times1 ~= nil and pcall(expr, times1 .. "*2") ~= false) or times1 == "" or times1 == nil) then start1=tonumber(expr(start1)) finish1=tonumber(expr(finish1)) if times1 == "" or times1==nil then times1=fill1 fill1=0 else times1=tonumber(expr(times1)) if string.find(orig1[i], "by") ~= nil then times1 = tonumber(rounding((finish1-start1)/times1+1,0)) end end if displayformula=="" and orig1[2]==nil then start3=start1 finish3=finish1 times3=times1 end local x1,formula1 if lastvalue1 == start1 then x1 = 2 formula1 = start1 if string.find(orig1[i], "by") == nil then times1 = times1 + 1 end else x1 = 1 end while x1<=times1 do                               count1=count1-1 if count1<0 then return userError("Maximum size exceeded", "LuaError") end if x1==1 then formula1=start1 else formula1=formula1+(finish1-start1)/(times1-1) if lastvalue1 == start1 and x1 == 2 then else s = s .. ' ' j=j+1 end end s = s .. fd(rounding(formula1,round1))..key1 x1=x1+1 end lastvalue1 = finish1 else count1=count1-1 if count1<0 then return userError("Maximum size exceeded", "LuaError") end s = s .. fd(orig1[i]) lastvalue1 = orig1[i] end else local times1 local doubleto1 = orig1[i] times1 = orig1[i]:match".* *for *([^ ]*)" if pcall(expr, times1 .. "*2") ~= false then doubleto1 = string.sub(doubleto1,1,#doubleto1-#times1-3) else times1 = nil end local _,occurences1 = string.gsub(orig1[i],"to","") if occurences1 >0 and ((times1 ~= "" and times1 ~= nil and pcall(expr, times1 .. "*2") ~= false) or times1 == "" or times1 == nil) then local times2 if times1 == "" or times1==nil then times2=fill1 else times2=tonumber(expr(times1)) end doubleto1 = string_to_formula(doubleto1,times2) end local useformula1 = string.gsub(doubleto1,"then",lastvalue1) if useformula1~=nil and pcall(expr,string.gsub(useformula1,"x","1").."*2")~=false and ((times1 ~= "" and times1 ~= nil and pcall(expr, times1 .. "*2") ~= false) or times1 == "" or times1 == nil) then if times1 == "" or times1==nil then times1=fill1 fill1=0 else times1=tonumber(expr(times1)) end local x1=1 while x1<=times1 do                               count1=count1-1 if count1<0 then return userError("Maximum size exceeded", "LuaError") end local formula1=string.gsub(useformula1,"x",x1) if x1 ~= 1 then s = s .. ' ' j=j+1 end local values3 = expr(formula1) lastvalue1 = values3 s = s .. fd(rounding(values3,round1))..key1 x1=x1+1 end else count1=count1-1 if count1<0 then return userError("Maximum size exceeded", "LuaError") end s = s .. fd(orig1[i]) lastvalue1 = orig1[i] end end else count1=count1-1 if count1<0 then return userError("Maximum size exceeded", "LuaError") end if pcall(expr, string.gsub(orig1[i],"then",lastvalue1) .. "*2") ~= false then local values3 = expr(string.gsub(orig1[i],"then",lastvalue1)) lastvalue1 = values3 s = s .. fd(rounding(values3,round1))..key1 else s = s .. fd(orig1[i]) end end end else s = s .. j       end i=i+1 j=j+1 end s = s .. ' '    s = s .. ' ' .. mw.ustring.gsub(label, "^.", mw.ustring.upper) .. ' '   i = 1 while orig[i] ~= nil and orig[i] ~= "" do       s = s .. ' '       s = s .. fd(orig[i]) if pcall(expr, orig[i] .. "*2") ~= false then s = s .. key end s = s .. ' '       i = i + 1 end s = s .. ' '

local formula = "" if displayformula ~= "" then formula = formula .. "Formula: " .. displayformula else if orig1[1] ~= "" then if start ~= nil and start3 ~= nil then local scale = (finish-start) / (times3 - 1) local scale1 = (finish3-start3) / (times3 - 1) formula = formula .. "Formula: " if start ~= 0 then formula = formula .. rounding(start, round + 1) .. key if scale >= 0 then formula = formula .. "+"                   end end if label1 == "level" then formula = formula .. rounding(scale, round + 1) .. key .. " every " if math.abs(scale1) ~= 1 then formula = formula .. rounding(scale1, round1 + 1) end formula = formula .. key1 .. " x"                   if scale1 > 1 then formula = formula .. "s" end if start3 ~= 0 and start3 ~= 1 then formula = formula .. " starting from x " .. rounding(start3, round1 + 1) .. key1 .. " and" else formula = formula .. ","                   end formula = formula .. " capped at x " .. rounding(finish3, round1 + 1) .. key1 else formula = formula .. rounding(scale, round + 1) .. key .. " every " .. rounding(scale1, round1 + 1) .. key1 .. " x"                   if start3 ~= 0 and start3 ~= 1 then formula = formula .. " starting from " .. rounding(start3, round1 + 1) .. key1 .. " x and" else formula = formula .. ","                   end formula = formula .. " capped at " .. rounding(finish3, round1 + 1) .. key1 .. " x"               end end else if start ~= nil then local scale = (finish-start) / (fill1 - 1) formula = formula .. "Formula: " if tonumber(tostring(start)) == tonumber(tostring(scale)) then formula = formula .. rounding(start, round + 1) .. key .. "&#42;x" else formula = formula .. rounding(start-scale, round + 1) .. key if scale >=0 then formula = formula .. "+"                       if scale == 1 then formula = formula .. "x" else formula = formula .. rounding(scale, round + 1) .. key .. "&#42;x" end else if scale == -1 then formula = formula .. "-x" else formula = formula .. rounding(scale, round + 1) .. key .. "&#42;x" end end end end if useformula ~= "" then formula = formula .. "Formula: " .. useformula end end end if displayformula == "" then formula = string.gsub(string.gsub(string.gsub(string.gsub(string.gsub(string.gsub(formula,"%/"," &divide; "),"%*","&#42;"),"&#42;"," &times; "),"%+"," + "),"%-"," &minus; "),"x",label1) else formula = string.gsub(string.gsub(string.gsub(string.gsub(string.gsub(formula,"%/","&divide;"),"%*","&#42;"),"&#42;","&times;"),"%+","+"),"%-","&minus;") end formula = fdmulti(formula) local s2 = ' ' .. formula s2 = s2 .. ' '   s2 = s2 .. ' ' .. mw.ustring.gsub(label1, "^.", mw.ustring.upper) return s2 .. s .. " " end

function p.ap(frame) local args; if frame.args == nil then args = lib.arguments(frame) else args = lib.arguments(frame.args) end local values="" local orig=args local count=6 local fill local formula if args["skill"]=="R" then fill=3 else fill=5 end i=1 local temp,a,b,c local round=args["round"] or nil while orig[i]~=nil and orig[i]~="" do       if (string.find(orig[i],"to")~=nil or string.find(orig[i],"x")~=nil) and string.find(orig[i],"%<")==nil then if string.find(orig[i],"to") ~= nil then start=string.sub(orig[i],1,string.find(orig[i],"to")-1) end if string.find(orig[i],"x")==nil and pcall(expr,start.."*2")~=false then finish,times=orig[i]:match".* *to *([^ ]*) *([^ ]*)" if finish~=nil and pcall(expr,finish.."*2")~=false and ((times ~= "" and times ~= nil and pcall(expr, times .. "*2") ~= false) or times == "" or times == nil) then start=tonumber(expr(start)) finish=tonumber(expr(finish)) if times == "" or times==nil then times=fill fill=0 else times=tonumber(expr(times)) end count=count-times if count<0 then return userError("Maximum size exceeded", "LuaError") end x=1 while x<=times do                       if x==1 then formula = start scale = (finish - start) / (times - 1) else formula = formula + scale end if values=="" then values=fd(rounding(formula,round)) else values=values.." / "..fd(rounding(formula,round)) end x=x+1 end else count=count-1 if count<0 then return userError("Maximum size exceeded", "LuaError") end if values=="" then values=fd(rounding(orig[i],round)) else values=values.." / "..fd(rounding(orig[i],round)) end end else local times local doubleto = orig[i] if last(orig[i]," ") ~= last(orig[i],"to ") then times = last(orig[i]," ") if pcall(expr, times .. "*2") ~= false then doubleto = string.sub(doubleto,1,#doubleto-#times) else times = nil end end local _, occurences = string.gsub(orig[i], "to", "") if occurences >0 and ((times ~= "" and times ~= nil and pcall(expr, times .. "*2") ~= false) or times == "" or times == nil) then local times2 if times == "" or times==nil then times2=fill else times2=tonumber(expr(times)) end doubleto = string_to_formula(doubleto,times2) end useformula = doubleto if useformula~=nil and pcall(expr,string.gsub(useformula,"x","1").."*2")~=false and ((times ~= "" and times ~= nil and pcall(expr, times .. "*2") ~= false) or times == "" or times == nil) then if times == "" or times==nil then times=fill fill=0 else times=tonumber(expr(times)) end x=1 while x<=times do                       count=count-1 if count<0 then return userError("Maximum size exceeded", "LuaError") end formula=string.gsub(useformula,"x",x) if values=="" then values=fd(rounding(expr(formula),round)) else values=values.." / "..fd(rounding(expr(formula),round)) end x=x+1 end else count=count-1 if count<0 then return userError("Maximum size exceeded", "LuaError") end if values=="" then values=fd(rounding(orig[i],round)) else values=values.." / "..fd(rounding(orig[i],round)) end end end else count=count-1 if count<0 then return userError("Maximum size exceeded", "LuaError") end if values=="" then values=fd(rounding(orig[i],round)) else values=values.." / "..fd(rounding(orig[i],round)) end end i=i+1 end return values end

function string_to_formula(args,times) local val = args while string.find(val, "to") ~= nil do       local to = mw.text.split(val, "to") local _, occurences1 = string.gsub(to[1], "%(", "")       local _, occurences2 = string.gsub(to[2], "%)", "") local para1 = mw.text.split(to[1], "%(")       local para2 = mw.text.split(to[2], "%)") local start,finish="","" local times2 local occurences3=0 local j = occurences1 + 1 while j >= 2 do           if j == occurences1+1 then start=para1[j] else start = "(" .. para1[j] .. start           end            _, times2 = string.gsub(para1[j], "%)", "") occurences3 = occurences3 + times2 if occurences1 + 2 - j > occurences3 then break end j = j - 1 end if pcall(expr, start .. "*2") == false then return args end occurences3=0 j = 1 while j <= occurences2 do           if j == 1 then finish = para2[j] else finish = finish .. para2[j] .. ")"           end            _, times2 = string.gsub(para2[j], "%(", "") occurences3 = occurences3 + times2 if j > occurences3 then break end j = j + 1 end if pcall(expr, finish .. "*2") == false then return args end start = string.gsub(start,"([%-%+%*%/%^%(%)])","%%%1") finish = string.gsub(finish,"([%-%+%*%/%^%(%)])","%%%1") val = string.gsub(val, start .. "to" .. finish, "(" .. start .. ")" .. "+(" .. "(" .. finish .. ")" .. "-" .. "(" .. start .. ")" .. ")/(" .. times .. "-1)*(x-1)") end return val end function expr(val) if string.find(val,"[0-9]")==nil then return val end local val=string.gsub(string.gsub(string.gsub(string.gsub(val," ",""),"([%/%*%+%-%^])"," %1 "),"([^%d%(%)]) %- ","%1-"),"([^%d%(%)]) %+ ","%1+") local i=1 local j=1 local a,b,switcher,switcher2,switcher3,temp while i>0 do       while j>0 do            temp=val if string.find(val,"%)")~=nil then               switchertemp=mw.text.split( val, "%)") temp=last(switchertemp[1],"%(")           end            if string.find(temp," %^ ")~=nil then                a=mw.text.split(temp," %^ ")                b=mw.text.split(a[2]," ")                switcher=last( a[1]," ")                switcher2=b[1]                switcher3=switcher^switcher2                if string.find(switcher2,"%-")~=nil then                     switcher2="%-"..switcher2*(-1)                end                val=string.gsub(val,switcher.." %^ "..switcher2,switcher3)                break            end            if string.find(temp," %/ ")~=nil then                a=mw.text.split(temp," %/ ")                b=mw.text.split(a[2]," ")                switcher=last( a[1]," ")                switcher2=b[1]                switcher3=switcher/switcher2                if string.find(switcher2,"%-")~=nil then                     switcher2="%-"..switcher2*(-1) end val=string.gsub(val,switcher.." %/ "..switcher2,switcher3) break end if string.find(temp," %* ")~=nil then a=mw.text.split( temp," %* ") b=mw.text.split(a[2]," ") switcher=last( a[1]," ") switcher2=b[1] switcher3=switcher*switcher2 if string.find(switcher2,"%-")~=nil then switcher2="%-"..switcher2*(-1) end val=string.gsub(val,switcher.." %* "..switcher2,switcher3) break end if string.find(temp," %- ")~=nil then a=mw.text.split( temp," %- ") b=mw.text.split(a[2]," ") switcher=last( a[1]," ") switcher2=b[1] if switcher=="" then if string.find(switcher2,"%-")~=nil then switcher2="%-"..switcher2*(-1) end val=string.gsub(val," %- "..switcher2,"%-"..switcher2) break else switcher3=switcher-switcher2 if string.find(switcher2,"%-")~=nil then switcher2="%-"..switcher2*(-1) end val=string.gsub(val,switcher.." %- "..switcher2,switcher3) break end end if string.find(temp," %+ ")~=nil then a=mw.text.split( temp," %+ ") b=mw.text.split(a[2]," ") switcher=last( a[1]," ") switcher2=b[1] if switcher=="" then if string.find(switcher2,"%-")~=nil then switcher2="%-"..switcher2*(-1) end val=string.gsub(val," %+ "..switcher2,switcher2) break else switcher3=switcher+switcher2 if string.find(switcher2,"%-")~=nil then switcher2="%-"..switcher2*(-1) end val=string.gsub(val,switcher.." %+ "..switcher2,switcher3) break end end if string.find(val,"%)")~=nil and string.find(val,"%(")~=nil then if string.find(temp,"%-")~=nil then temp="%-"..temp*(-1) end val=string.gsub(val,"%("..temp.."%)",temp) break else i=0 end break end end return temp end

function last(val,sign) local a=mw.text.split(val,sign) local j=1 while j>0 do       if a[j] ~=nil then j=j+1 else break end end return a[j-1] end

function fd(val) if string.find(val,"[0-9]") == nil or string.find(val,"%.") == nil then return val end local a,b,c if string.find(val,"span") ~= nil then a,b,c = val:match"([^.]*).(.*) (.*)" if string.find(val,"span")<=string.find(val,"%.") then return a .. ". " .. b .. " " .. c       else return a .. ". " .. b .. " " .. c       end else a,b,c = val:match"([^.]*).([0-9]*)(.*)" return a .. ". " .. b .. " " .. c   end end

function fdmulti(val) if string.find(val,"[0-9]") == nil or string.find(val,"%.") == nil then return val end local val   = mw.text.split(val,"%.") local values = val[1] local i     = 2 local b,c while val[i] ~= nil do       if string.find(val[i],"span") ~= nil then b,c   = val[i]:match"(.*) (.*)" if string.find(tostring(b),"[0-9]")==nil then values=values.."."..val[i] else if string.find(val[i],"span")<=string.find(val[i],"%.") then values = values .. ". " .. b .. " " .. c               else values = values .. ". " .. b .. " " .. c               end end else b,c   = val[i]:match"([0-9]*)(.*)" if string.find(tostring(b),"[0-9]")==nil then values=values.."."..val[i] else values = values .. ". " .. b .. " " .. c           end end i = i + 1 end return values end

function rounding(val, decimals) local round local val = tostring(val) if decimals == nil or string.find(val,"[0-9]") == nil or string.find(val,"<span") ~= nil or string.find(val,"%.") == nil then return val end local a,b,c,d = val:match"([^0-9]*)([0-9]*).([0-9]*)(.*)" val=tonumber(b.."."..c)   if decimals == "abs" then round = math.abs(val) end if decimals == "floor" then round = math.floor(val) end if decimals == "ceil" then round = math.ceil(val) end if decimals == "trunc" then round = string.sub(val,1,(string.find(val,"%.") or 0) - 1) end if round == nil then local mult = 10^(decimals) round = math.floor(val * mult + 0.5) / mult end return a..round..d end

return p

-- --