Module:Spell cost table

--

local p = {}

local gep = require('Module:Exchange')._price local coins = require('Module:Coins')._amount

local combo_runes = { ['mist rune'] = { 'air rune', 'water rune' }, ['dust rune'] = { 'air rune', 'earth rune' }, ['mud rune'] = { 'water rune', 'earth rune' }, ['smoke rune'] = { 'air rune', 'fire rune' }, ['steam rune'] = { 'water rune', 'fire rune' }, ['lava rune'] = { 'earth rune', 'fire rune' } }

local staffs = { ['staff of air'] = { 'air rune' }, ['staff of water'] = { 'water rune' }, ['kodai wand'] = { 'water rune' }, ['staff of earth'] = { 'earth rune' }, ['staff of fire'] = { 'fire rune' }, ['mud battlestaff'] = { 'water rune', 'earth rune' }, ['steam battlestaff'] = { 'water rune', 'fire rune' }, ['lava battlestaff'] = { 'earth rune', 'fire rune' }, ['smoke battlestaff'] = { 'air rune', 'fire rune' }, ['dust battlestaff'] = { 'air rune', 'earth rune' }, ['mist battlestaff'] = { 'air rune', 'water rune' }, }

local offhands = { ['tome of fire'] = { 'fire rune' }, }

function p.main(frame) local args = frame:getParent.args local runes = {} for i=1,10 do		if not args['Rune'..i] then break end local rune = string.lower(args['Rune'..i])..' rune' local num = tonumber(args['Rune'..i..'num'] or 1) table.insert(runes,{rune,num}) end local no_staff = args.nostaff ~= nil return p._main(runes,no_staff) end

function p._main(runes,no_staff) local ret = mw.html.create('table') :addClass('wikitable') :tag('caption') :wikitext('Spell cost') :done :tag('tr') :tag('th') :wikitext('Input') :done :tag('th') :wikitext('Cost') :done :done :tag('tr') :tag('td') :wikitext(make_pics(runes)) :done :tag('td') :wikitext(total_price(runes)) :done :done

local combos_used = {} local staffs_used = {} for i, v in pairs(combo_runes) do		local amtused = 0 local tblcopy = copytbl(runes) for _, w in ipairs(v) do			for j, x in ipairs(tblcopy) do				if w == x[1] then table.remove(tblcopy,j) if x[2] > amtused then amtused = x[2] end end end end if amtused > 0 then table.insert(tblcopy,{i, amtused}) table.insert(combos_used,tblcopy) end end if #combos_used > 0 then ret:tag('tr') :tag('th') :attr('colspan','2') :wikitext('Combo runes') :done :done for _, v in ipairs(combos_used) do			ret:tag('tr') :tag('td') :wikitext(make_pics(v)) :done :tag('td') :wikitext(total_price(v)) :done :done end end

local weapons = {} for i, v in pairs(staffs) do		weapons[i] = v	end for i, v in pairs(offhands) do		weapons[i] = v	end for i, v in pairs(staffs) do		for j, w in pairs(offhands) do			local nindex = i..'|'..j			local nrunes = {} local runesadd = {} -- check if the offhand is redundant to the current staff local redundant = false local runecountoff = w[2] and 2 or 1 for _, i in ipairs(w) do				for _, j in ipairs(v) do					if j == i and runecountoff == 1 then redundant = true break end end end

if not redundant then -- add runes to new indexed table for _, i in ipairs(w) do					nrunes[i] = true end for _, i in ipairs(v) do					nrunes[i] = true end -- turn into normal table for i, _ in pairs(nrunes) do					table.insert(runesadd,i) end

weapons[nindex] = runesadd end end end

-- remove redundant staves and offhands local redund = {} for i, v in pairs(staffs) do		local isused = false local tblcopy = copytbl(runes) for _, w in ipairs(v) do			for j, x in ipairs(tblcopy) do				if w == x[1] then table.remove(tblcopy,j) isused = true end end end if not isused then table.insert(redund,i) end end for i, v in pairs(offhands) do		local isused = false local tblcopy = copytbl(runes) for _, w in ipairs(v) do			for j, x in ipairs(tblcopy) do				if w == x[1] then table.remove(tblcopy,j) isused = true end end end if not isused then table.insert(redund,i) end end

for i, v in pairs(weapons) do		local isused = false local tblcopy = copytbl(runes) for _, w in ipairs(v) do			for j, x in ipairs(tblcopy) do				if w == x[1] then table.remove(tblcopy,j) isused = true end end end if isused then local redundant = false for _, w in ipairs(redund) do				if string.find(i,w) then redundant = true end end if not redundant then table.insert(tblcopy,{i, 0}) table.insert(staffs_used,tblcopy) end end end

if #staffs_used > 0 and not no_staff then ret:tag('tr') :tag('th') :attr('colspan','2') :wikitext('Staves') :done :done for _, v in ipairs(staffs_used) do			ret:tag('tr') :tag('td') :wikitext(make_pics(v)) :done :tag('td') :wikitext(total_price(v)) :done :done end end return ret end

function copytbl(tbl) local ret = {} for _, v in ipairs(tbl) do		table.insert(ret,v) end return ret end

function make_pics(arg) local runes = {} for _, v in ipairs(arg) do		if v[1]:find('%|') then local _v = mw.text.split(v[1],'%|') for _, w in ipairs(_v) do				table.insert(runes, {w, v[2]}) end else table.insert(runes, v)		end end

local ret = {}

for _, v in ipairs(runes) do		if v[2] > 0 then table.insert(ret,..v[2]..) end table.insert(ret,'') end

return table.concat(ret) end

function total_price(runes) local ret = 0 for _, v in ipairs(runes) do		if v[2] > 0 then ret = ret + gep(v[1]) * v[2] end end return coins(ret) end

return p